perf/amd/iommu: Enable multi-IOMMU support
Subsystems (3)
v6 cover perf/amd/iommu: Enable multi-IOMMU support
From: Suravee Suthikulpanit <[email protected]>
This patch series modifies the existing IOMMU and Perf drivers to support systems with multiple IOMMUs by allocating an amd_iommu PMU per IOMMU instance. This allows users to specify performance events and filters separately for each IOMMU.
This has been tested on the new family17h-based server w/ multiple IOMMUs.
Git branch containing this patch series is available here:
https://github.com/ssuthiku/linux.git perf-iommu-v6
Changes from V5 (https://lkml.org/lkml/2016/2/23/370)
* Rebased onto v4.9.
* Remove the patch which consolidates function delclarations since we have not yet agreed on the appropriate place for the new header file.
Suravee Suthikulpanit (7):
perf/amd/iommu: Misc fix up perf_iommu_read perf/amd/iommu: Modify functions to query max banks and counters perf/amd/iommu: Modify IOMMU API to allow specifying IOMMU index perf/amd/iommu: Declare pr_fmt and remove unnecessary pr_debug perf/amd/iommu: Clean up perf_iommu_enable_event iommu/amd: Introduce amd_iommu_get_num_iommus() perf/amd/iommu: Enable support for multiple IOMMUs
arch/x86/events/amd/iommu.c | 206 ++++++++++++++++++++-------------------- arch/x86/events/amd/iommu.h | 17 ++-- drivers/iommu/amd_iommu_init.c | 119 ++++++++++++++++------- drivers/iommu/amd_iommu_proto.h | 15 ++- 4 files changed, 209 insertions(+), 148 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v6 1/7 perf/amd/iommu: Misc fix up perf_iommu_read
This patch contains the following minor fixup:
* Fixed overflow handling since u64 delta would lose the MSB sign bit.
* Remove unnecessary local64_set().
* Coding style and make use of GENMASK_ULL macro.
--- arch/x86/events/amd/iommu.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-)
arch/x86/events/amd/iommu.c +13 -12
@@ -319,29 +319,30 @@ static void perf_iommu_start(struct perf_event *event, int flags)static void perf_iommu_read (struct perf_event *event) { -u64 count =0ULL ; -u64 prev_raw_count =0ULL ; -u64 delta =0ULL ; +u64 cnt, prev; +s64 delta;struct hw_perf_event *hwc = &event->hw;pr_debug ("perf: amd_iommu:perf_iommu_read\n" );amd_iommu_pc_get_set_reg_val (_GET_DEVID (event),_GET_BANK (event),_GET_CNTR (event), -IOMMU_PC_COUNTER_REG , &count, false); +IOMMU_PC_COUNTER_REG , &cnt, false);/* IOMMU pc counter register is only 48 bits */ - count &=0xFFFFFFFFFFFFULL ; + cnt &=GENMASK_ULL (48 ,0 ); - prev_raw_count =local64_read (&hwc->prev_count); -if (local64_cmpxchg (&hwc->prev_count, prev_raw_count, - count) != prev_raw_count) -return ; + prev =local64_read (&hwc->prev_count); -/* Handling 48-bit counter overflowing */ - delta = (count <<COUNTER_SHIFT ) - (prev_raw_count <<COUNTER_SHIFT ); +/* +* Since we do not enable counter overflow interrupts, +* we do not have to worry about prev_count changing on us. +*/ +local64_set (&hwc->prev_count, cnt); + +/* Handle 48-bit counter overflow */ + delta = (cnt <<COUNTER_SHIFT ) - (prev <<COUNTER_SHIFT ); delta >>=COUNTER_SHIFT ;local64_add (delta, &event->count); - }static void perf_iommu_stop (struct perf_event *event,int flags)
Signature · 1 line
1.8.3.1
PATCH v6 2/7 perf/amd/iommu: Modify functions to query max banks and counters
In reply to Suravee SuthikulpanitCurrently, amd_iommu_pc_get_max_[banks|counters]() use end-point device ID to locate an IOMMU and check the reported max banks/counters. The logic assumes that the IOMMU_BASE_DEVID belongs to the first IOMMU, and uses it to acquire a reference to the first IOMMU, which does not work on certain systems. Instead, we modify the function to take IOMMU index, and use it to query the corresponded AMD IOMMU instance.
Note that we currently hard-code the IOMMU index to 0, since the current AMD IOMMU perf implementation only supports single IOMMU. Subsequent patch will add support for multi-IOMMU, and will use proper IOMMU index.
--- arch/x86/events/amd/iommu.c | 17 +++++++---------- arch/x86/events/amd/iommu.h | 7 ++----- drivers/iommu/amd_iommu_init.c | 35 +++++++++++++++++++++-------------- drivers/iommu/amd_iommu_proto.h | 4 ++-- 4 files changed, 32 insertions(+), 31 deletions(-)
arch/x86/events/amd/iommu.c +7 -10
@@ -237,14 +237,6 @@ static int perf_iommu_event_init(struct perf_event *event)return -EINVAL ; } -/* integrate with iommu base devid (0000), assume one iommu */ - perf_iommu->max_banks = -amd_iommu_pc_get_max_banks (IOMMU_BASE_DEVID ); - perf_iommu->max_counters = -amd_iommu_pc_get_max_counters (IOMMU_BASE_DEVID ); -if ((perf_iommu->max_banks ==0 ) || (perf_iommu->max_counters ==0 )) -return -EINVAL ; -/* update the hw_perf_event struct with the iommu config data */ hwc->config = config; hwc->extra_reg.config = config1;
@@ -456,6 +448,11 @@ static __init int _init_perf_amd_iommu(if (_init_events_attrs (perf_iommu) !=0 )pr_err ("perf: amd_iommu: Only support raw events.\n" ); + perf_iommu->max_banks =amd_iommu_pc_get_max_banks (0 ); + perf_iommu->max_counters =amd_iommu_pc_get_max_counters (0 ); +if (!perf_iommu->max_banks || !perf_iommu->max_counters) +return -EINVAL ; +/* Init null attributes */ perf_iommu->null_group =NULL ; perf_iommu->pmu.attr_groups = perf_iommu->attr_groups;
@@ -466,8 +463,8 @@ static __init int _init_perf_amd_iommu(amd_iommu_pc_exit (); }else {pr_info ("perf: amd_iommu: Detected. (%d banks, %d counters/bank)\n" , -amd_iommu_pc_get_max_banks (IOMMU_BASE_DEVID ), -amd_iommu_pc_get_max_counters (IOMMU_BASE_DEVID )); +amd_iommu_pc_get_max_banks (0 ), +amd_iommu_pc_get_max_counters (0 )); }return ret;
arch/x86/events/amd/iommu.h +2 -5
@@ -24,15 +24,12 @@#define PC_MAX_SPEC_BNKS 64#define PC_MAX_SPEC_CNTRS 16 -/* iommu pc reg masks*/ -#define IOMMU_BASE_DEVID 0x0000 -/* amd_iommu_init.c external support functions */ extern bool amd_iommu_pc_supported (void ); -extern u8 amd_iommu_pc_get_max_banks (u16 devid); +extern u8 amd_iommu_pc_get_max_banks (uint devid); -extern u8 amd_iommu_pc_get_max_counters (u16 devid); +extern u8 amd_iommu_pc_get_max_counters (uint devid);extern int amd_iommu_pc_get_set_reg_val (u16 devid,u8 bank,u8 cntr,u8 fxn,u64 *value,bool is_write);
drivers/iommu/amd_iommu_init.c +21 -14
@@ -2706,6 +2706,19 @@ bool amd_iommu_v2_supported(void) }EXPORT_SYMBOL (amd_iommu_v2_supported); +static struct amd_iommu *get_amd_iommu (uint idx) +{ +uint i =0 ; +struct amd_iommu *iommu =NULL ; + +for_each_iommu (iommu) { +if (i == idx) +break ; + i++; + } +return iommu; +} +/**************************************************************************** * * IOMMU EFR Performance Counter support functionality. This code allows
@@ -2713,17 +2726,14 @@ bool amd_iommu_v2_supported(void)* ****************************************************************************/ -u8 amd_iommu_pc_get_max_banks (u16 devid) +u8 amd_iommu_pc_get_max_banks (uint idx) { -struct amd_iommu *iommu; -u8 ret =0 ; +struct amd_iommu *iommu =get_amd_iommu (idx); -/* locate the iommu governing the devid */ - iommu = amd_iommu_rlookup_table[devid];if (iommu) - ret = iommu->max_banks; +return iommu->max_banks; -return ret; +return 0 ; }EXPORT_SYMBOL (amd_iommu_pc_get_max_banks);
@@ -2733,17 +2743,14 @@ bool amd_iommu_pc_supported(void) }EXPORT_SYMBOL (amd_iommu_pc_supported); -u8 amd_iommu_pc_get_max_counters (u16 devid) +u8 amd_iommu_pc_get_max_counters (uint idx) { -struct amd_iommu *iommu; -u8 ret =0 ; +struct amd_iommu *iommu =get_amd_iommu (idx); -/* locate the iommu governing the devid */ - iommu = amd_iommu_rlookup_table[devid];if (iommu) - ret = iommu->max_counters; +return iommu->max_counters; -return ret; +return 0 ; }EXPORT_SYMBOL (amd_iommu_pc_get_max_counters);
drivers/iommu/amd_iommu_proto.h +2 -2
@@ -58,8 +58,8 @@ extern int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,/* IOMMU Performance Counter functions */ extern bool amd_iommu_pc_supported (void ); -extern u8 amd_iommu_pc_get_max_banks (u16 devid); -extern u8 amd_iommu_pc_get_max_counters (u16 devid); +extern u8 amd_iommu_pc_get_max_banks (uint devid); +extern u8 amd_iommu_pc_get_max_counters (uint devid);extern int amd_iommu_pc_get_set_reg_val (u16 devid,u8 bank,u8 cntr,u8 fxn,u64 *value,bool is_write);
Signature · 1 line
1.8.3.1
PATCH v6 3/7 perf/amd/iommu: Modify IOMMU API to allow specifying IOMMU index
In reply to Suravee SuthikulpanitThe current amd_iommu_pc_get_set_reg_val() cannot support multi-IOMMU. It is also confusing since it is trying to support set and get in one function.
So, this patch breaks it down to amd_iommu_pc_[get|set]_counter(), and modifies them to allow callers to specify IOMMU index. This prepares the driver for supporting multi-IOMMU in subsequent patch.
--- arch/x86/events/amd/iommu.c | 45 +++++++++++------------- arch/x86/events/amd/iommu.h | 8 +++-- drivers/iommu/amd_iommu_init.c | 77 +++++++++++++++++++++++++++++++---------- drivers/iommu/amd_iommu_proto.h | 10 ++++-- 4 files changed, 93 insertions(+), 47 deletions(-)
arch/x86/events/amd/iommu.c +21 -24
@@ -248,46 +248,44 @@ static void perf_iommu_enable_event(struct perf_event *ev) {u8 csource =_GET_CSOURCE (ev);u16 devid =_GET_DEVID (ev); +u8 bank =_GET_BANK (ev); +u8 cntr =_GET_CNTR (ev);u64 reg =0ULL ; reg = csource; -amd_iommu_pc_get_set_reg_val (devid, -_GET_BANK (ev),_GET_CNTR (ev) , -IOMMU_PC_COUNTER_SRC_REG , ®, true); +amd_iommu_pc_set_reg (0 , devid, bank, cntr, +IOMMU_PC_COUNTER_SRC_REG , ®); reg =0ULL | devid | (_GET_DEVID_MASK (ev) <<32 );if (reg) reg |= (1UL <<31 ); -amd_iommu_pc_get_set_reg_val (devid, -_GET_BANK (ev),_GET_CNTR (ev) , -IOMMU_PC_DEVID_MATCH_REG , ®, true); +amd_iommu_pc_set_reg (0 , devid, bank, cntr, +IOMMU_PC_DEVID_MATCH_REG , ®); reg =0ULL |_GET_PASID (ev) | (_GET_PASID_MASK (ev) <<32 );if (reg) reg |= (1UL <<31 ); -amd_iommu_pc_get_set_reg_val (devid, -_GET_BANK (ev),_GET_CNTR (ev) , -IOMMU_PC_PASID_MATCH_REG , ®, true); +amd_iommu_pc_set_reg (0 , devid, bank, cntr, +IOMMU_PC_PASID_MATCH_REG , ®); reg =0ULL |_GET_DOMID (ev) | (_GET_DOMID_MASK (ev) <<32 );if (reg) reg |= (1UL <<31 ); -amd_iommu_pc_get_set_reg_val (devid, -_GET_BANK (ev),_GET_CNTR (ev) , -IOMMU_PC_DOMID_MATCH_REG , ®, true); +amd_iommu_pc_set_reg (0 , devid, bank, cntr, +IOMMU_PC_DOMID_MATCH_REG , ®); }static void perf_iommu_disable_event (struct perf_event *event) {u64 reg =0ULL ; -amd_iommu_pc_get_set_reg_val (_GET_DEVID (event), -_GET_BANK (event),_GET_CNTR (event), -IOMMU_PC_COUNTER_SRC_REG , ®, true); +amd_iommu_pc_set_reg (0 ,_GET_DEVID (event),_GET_BANK (event), +_GET_CNTR (event),IOMMU_PC_COUNTER_SRC_REG , ®); }static void perf_iommu_start (struct perf_event *event,int flags) { +u64 val;struct hw_perf_event *hwc = &event->hw;pr_debug ("perf: amd_iommu:perf_iommu_start\n" );
@@ -297,13 +295,13 @@ static void perf_iommu_start(struct perf_event *event, int flags)WARN_ON_ONCE (!(hwc->state &PERF_HES_UPTODATE )); hwc->state =0 ; -if (flags &PERF_EF_RELOAD ) { -u64 prev_raw_count =local64_read (&hwc->prev_count); -amd_iommu_pc_get_set_reg_val (_GET_DEVID (event), -_GET_BANK (event),_GET_CNTR (event), -IOMMU_PC_COUNTER_REG , &prev_raw_count, true); - } +if (!(flags &PERF_EF_RELOAD )) + goto enable; + + val =local64_read (&hwc->prev_count); +amd_iommu_pc_set_counter (0 ,_GET_BANK (event),_GET_CNTR (event), &val); +enable:perf_iommu_enable_event (event);perf_event_update_userpage (event);
@@ -316,9 +314,8 @@ static void perf_iommu_read(struct perf_event *event)struct hw_perf_event *hwc = &event->hw;pr_debug ("perf: amd_iommu:perf_iommu_read\n" ); -amd_iommu_pc_get_set_reg_val (_GET_DEVID (event), -_GET_BANK (event),_GET_CNTR (event), -IOMMU_PC_COUNTER_REG , &cnt, false); +if (amd_iommu_pc_get_counter (0 ,_GET_BANK (event),_GET_CNTR (event), &cnt)) +return ;/* IOMMU pc counter register is only 48 bits */ cnt &=GENMASK_ULL (48 ,0 );
arch/x86/events/amd/iommu.h +6 -2
@@ -31,7 +31,11 @@extern u8 amd_iommu_pc_get_max_counters (uint devid); -extern int amd_iommu_pc_get_set_reg_val (u16 devid,u8 bank,u8 cntr, -u8 fxn,u64 *value,bool is_write); +extern int amd_iommu_pc_set_reg (uint idx,u16 devid,u8 bank,u8 cntr, +u8 fxn,u64 *value); + +extern int amd_iommu_pc_set_counter (uint idx,u8 bank,u8 cntr,u64 *value); + +extern int amd_iommu_pc_get_counter (uint idx,u8 bank,u8 cntr,u64 *value);#endif /*_PERF_EVENT_AMD_IOMMU_H_*/
drivers/iommu/amd_iommu_init.c +58 -19
114 lines not on this page · raw message
drivers/iommu/amd_iommu_proto.h +8 -2
@@ -57,11 +57,17 @@ extern int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,extern struct iommu_domain *amd_iommu_get_v2_domain (struct pci_dev *pdev);/* IOMMU Performance Counter functions */ +#ifndef IOMMU_PC_COUNTER_REG +#define IOMMU_PC_COUNTER_REG 0x00 +#endif +extern bool amd_iommu_pc_supported (void );extern u8 amd_iommu_pc_get_max_banks (uint devid);extern u8 amd_iommu_pc_get_max_counters (uint devid); -extern int amd_iommu_pc_get_set_reg_val (u16 devid,u8 bank,u8 cntr,u8 fxn, -u64 *value,bool is_write); +extern int amd_iommu_pc_set_reg (uint idx,u16 devid,u8 bank,u8 cntr, +u8 fxn,u64 *value); +extern int amd_iommu_pc_set_counter (uint idx,u8 bank,u8 cntr,u64 *value); +extern int amd_iommu_pc_get_counter (uint idx,u8 bank,u8 cntr,u64 *value);#ifdef CONFIG_IRQ_REMAP extern int amd_iommu_create_irq_domain (struct amd_iommu *iommu);
Signature · 1 line
1.8.3.1
PATCH v6 4/7 perf/amd/iommu: Declare pr_fmt and remove unnecessary pr_debug
In reply to Suravee SuthikulpanitThis patch declare pr_fmt for perf/amd_iommu and remove unnecessary pr_debug.
--- arch/x86/events/amd/iommu.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
arch/x86/events/amd/iommu.c +4 -8
@@ -11,6 +11,8 @@* published by the Free Software Foundation. */ +#define pr_fmt (fmt) "perf/amd_iommu: " fmt +#include <linux/perf_event.h> #include <linux/init.h> #include <linux/cpumask.h>
@@ -288,7 +290,6 @@ static void perf_iommu_start(struct perf_event *event, int flags)u64 val;struct hw_perf_event *hwc = &event->hw; -pr_debug ("perf: amd_iommu:perf_iommu_start\n" );if (WARN_ON_ONCE (!(hwc->state &PERF_HES_STOPPED )))return ;
@@ -312,7 +313,6 @@ static void perf_iommu_read(struct perf_event *event)u64 cnt, prev;s64 delta;struct hw_perf_event *hwc = &event->hw; -pr_debug ("perf: amd_iommu:perf_iommu_read\n" );if (amd_iommu_pc_get_counter (0 ,_GET_BANK (event),_GET_CNTR (event), &cnt))return ;
@@ -339,8 +339,6 @@ static void perf_iommu_stop(struct perf_event *event, int flags)struct hw_perf_event *hwc = &event->hw;u64 config; -pr_debug ("perf: amd_iommu:perf_iommu_stop\n" ); -if (hwc->state &PERF_HES_UPTODATE )return ;
@@ -362,7 +360,6 @@ static int perf_iommu_add(struct perf_event *event, int flags)struct perf_amd_iommu *perf_iommu = container_of(event->pmu, struct perf_amd_iommu, pmu); -pr_debug ("perf: amd_iommu:perf_iommu_add\n" ); event->hw.state =PERF_HES_UPTODATE |PERF_HES_STOPPED ;/* request an iommu bank/counter */
@@ -383,7 +380,6 @@ static void perf_iommu_del(struct perf_event *event, int flags)struct perf_amd_iommu *perf_iommu = container_of(event->pmu, struct perf_amd_iommu, pmu); -pr_debug ("perf: amd_iommu:perf_iommu_del\n" );perf_iommu_stop (event,PERF_EF_UPDATE );/* clear the assigned iommu bank/counter */
@@ -443,7 +439,7 @@ static __init int _init_perf_amd_iommu(/* Init events attributes */ if (_init_events_attrs (perf_iommu) !=0 ) -pr_err ("perf: amd_iommu: Only support raw events.\n" ); +pr_err ("Only support raw events.\n" ); perf_iommu->max_banks =amd_iommu_pc_get_max_banks (0 ); perf_iommu->max_counters =amd_iommu_pc_get_max_counters (0 );
@@ -456,7 +452,7 @@ static __init int _init_perf_amd_iommu( ret =perf_pmu_register (&perf_iommu->pmu, name,-1 );if (ret) { -pr_err ("perf: amd_iommu: Failed to initialized.\n" ); +pr_err ("Error initializing AMD IOMMU perf counters.\n" );amd_iommu_pc_exit (); }else { pr_info("perf: amd_iommu: Detected. (%d banks, %d counters/bank)\n",
Signature · 1 line
1.8.3.1
PATCH v6 5/7 perf/amd/iommu: Clean up perf_iommu_enable_event
In reply to Suravee SuthikulpanitThis patch cleans up:
* Various bitwise operations in perf_iommu_enable_event
* Make use macros BIT(x)
This should not affect logic and functionality.
--- arch/x86/events/amd/iommu.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/x86/events/amd/iommu.c +6 -6
@@ -258,21 +258,21 @@ static void perf_iommu_enable_event(struct perf_event *ev)amd_iommu_pc_set_reg (0 , devid, bank, cntr,IOMMU_PC_COUNTER_SRC_REG , ®); - reg =0ULL | devid | (_GET_DEVID_MASK (ev) <<32 ); + reg = devid | (_GET_DEVID_MASK (ev) <<32 );if (reg) - reg |= (1UL <<31 ); + reg |=BIT (31 );amd_iommu_pc_set_reg (0 , devid, bank, cntr,IOMMU_PC_DEVID_MATCH_REG , ®); - reg =0ULL |_GET_PASID (ev) | (_GET_PASID_MASK (ev) <<32 ); + reg =_GET_PASID (ev) | (_GET_PASID_MASK (ev) <<32 );if (reg) - reg |= (1UL <<31 ); + reg |=BIT (31 );amd_iommu_pc_set_reg (0 , devid, bank, cntr,IOMMU_PC_PASID_MATCH_REG , ®); - reg =0ULL |_GET_DOMID (ev) | (_GET_DOMID_MASK (ev) <<32 ); + reg =_GET_DOMID (ev) | (_GET_DOMID_MASK (ev) <<32 );if (reg) - reg |= (1UL <<31 ); + reg |=BIT (31 );amd_iommu_pc_set_reg (0 , devid, bank, cntr,IOMMU_PC_DOMID_MATCH_REG , ®); }
Signature · 1 line
1.8.3.1
PATCH v6 6/7 iommu/amd: Introduce amd_iommu_get_num_iommus()
In reply to Suravee SuthikulpanitThis patch introduces amd_iommu_get_num_iommus(). This is intended for Perf AMD IOMMU driver.
--- arch/x86/events/amd/iommu.h | 2 ++ drivers/iommu/amd_iommu_init.c | 7 ++++++- drivers/iommu/amd_iommu_proto.h | 1 + 3 files changed, 9 insertions(+), 1 deletion(-)
arch/x86/events/amd/iommu.h +2 -0
@@ -25,6 +25,8 @@#define PC_MAX_SPEC_CNTRS 16/* amd_iommu_init.c external support functions */ +extern int amd_iommu_get_num_iommus (void ); +extern bool amd_iommu_pc_supported (void );extern u8 amd_iommu_pc_get_max_banks (uint devid);
drivers/iommu/amd_iommu_init.c +6 -1
@@ -1329,7 +1329,7 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)/* Add IOMMU to internal data structures */ list_add_tail (&iommu->list, &amd_iommu_list); - iommu->index = amd_iommus_present++; + iommu->index = amd_iommus_present++;if (unlikely (iommu->index >=MAX_IOMMUS )) {WARN (1 ,"AMD-Vi: System has more IOMMUs than supported by this driver\n" );
@@ -2717,6 +2717,11 @@ static struct amd_iommu *get_amd_iommu(uint idx)return iommu; } +int amd_iommu_get_num_iommus (void ) +{ +return amd_iommus_present; +} +/**************************************************************************** * * IOMMU EFR Performance Counter support functionality. This code allows
drivers/iommu/amd_iommu_proto.h +1 -0
@@ -61,6 +61,7 @@ extern int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,#define IOMMU_PC_COUNTER_REG 0x00#endif +extern int amd_iommu_get_num_iommus (void );extern bool amd_iommu_pc_supported (void );extern u8 amd_iommu_pc_get_max_banks (uint devid);extern u8 amd_iommu_pc_get_max_counters (uint devid);
Signature · 1 line
1.8.3.1
PATCH v6 7/7 perf/amd/iommu: Enable support for multiple IOMMUs
In reply to Suravee SuthikulpanitThis patch adds multi-IOMMU support for perf by exposing an AMD IOMMU PMU for each IOMMU found in the system via:
/sys/device/amd_iommu_x /* where x is the IOMMU index. */
This allows users to specify different events to be programed onto performance counters of each IOMMU.
--- arch/x86/events/amd/iommu.c | 119 ++++++++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 55 deletions(-)
arch/x86/events/amd/iommu.c +64 -55
@@ -35,10 +35,13 @@#define _GET_PASID_MASK (ev) ((ev->hw.extra_reg.config >> 16) & 0xFFFFULL)#define _GET_DOMID_MASK (ev) ((ev->hw.extra_reg.config >> 32) & 0xFFFFULL) -static struct perf_amd_iommu __perf_iommu; +#define PERF_AMD_IOMMU_NAME_SZ 16struct perf_amd_iommu { +struct list_head list;struct pmu pmu; +uint idx; +char name[PERF_AMD_IOMMU_NAME_SZ ];u8 max_banks;u8 max_counters;u64 cntr_assign_mask;
@@ -46,6 +49,8 @@ struct perf_amd_iommu {const struct attribute_group *attr_groups[4 ]; }; +LIST_HEAD (perf_amd_iommu_list); +#define format_group attr_groups[0]#define cpumask_group attr_groups[1]#define events_group attr_groups[2]
@@ -204,8 +209,7 @@ static int clear_avail_iommu_bnk_cntr(struct perf_amd_iommu *perf_iommu,static int perf_iommu_event_init (struct perf_event *event) {struct hw_perf_event *hwc = &event->hw; -struct perf_amd_iommu *perf_iommu; -u64 config, config1; + struct perf_amd_iommu *pi = container_of(event->pmu, struct perf_amd_iommu, pmu);/* test the event attr type check for PMU enumeration */ if (event->attr.type != event->pmu->type)
@@ -227,27 +231,17 @@ static int perf_iommu_event_init(struct perf_event *event)if (event->cpu <0 )return -EINVAL ; - perf_iommu = &__perf_iommu; - -if (event->pmu != &perf_iommu->pmu) -return -ENOENT ; - -if (perf_iommu) { - config = event->attr.config; - config1 = event->attr.config1; - }else { -return -EINVAL ; - } -/* update the hw_perf_event struct with the iommu config data */ - hwc->config = config; - hwc->extra_reg.config = config1; + hwc->idx = pi->idx; + hwc->config = event->attr.config; + hwc->extra_reg.config = event->attr.config1;return 0 ; }static void perf_iommu_enable_event (struct perf_event *ev) { +struct hw_perf_event *hwc = &ev->hw;u8 csource =_GET_CSOURCE (ev);u16 devid =_GET_DEVID (ev);u8 bank =_GET_BANK (ev);
@@ -255,33 +249,34 @@ static void perf_iommu_enable_event(struct perf_event *ev)u64 reg =0ULL ; reg = csource; -amd_iommu_pc_set_reg (0 , devid, bank, cntr, +amd_iommu_pc_set_reg (hwc->idx, devid, bank, cntr,IOMMU_PC_COUNTER_SRC_REG , ®); reg = devid | (_GET_DEVID_MASK (ev) <<32 );if (reg) reg |=BIT (31 ); -amd_iommu_pc_set_reg (0 , devid, bank, cntr, +amd_iommu_pc_set_reg (hwc->idx, devid, bank, cntr,IOMMU_PC_DEVID_MATCH_REG , ®); reg =_GET_PASID (ev) | (_GET_PASID_MASK (ev) <<32 );if (reg) reg |=BIT (31 ); -amd_iommu_pc_set_reg (0 , devid, bank, cntr, +amd_iommu_pc_set_reg (hwc->idx, devid, bank, cntr,IOMMU_PC_PASID_MATCH_REG , ®); reg =_GET_DOMID (ev) | (_GET_DOMID_MASK (ev) <<32 );if (reg) reg |=BIT (31 ); -amd_iommu_pc_set_reg (0 , devid, bank, cntr, +amd_iommu_pc_set_reg (hwc->idx, devid, bank, cntr,IOMMU_PC_DOMID_MATCH_REG , ®); }static void perf_iommu_disable_event (struct perf_event *event) { +struct hw_perf_event *hwc = &event->hw;u64 reg =0ULL ; -amd_iommu_pc_set_reg (0 ,_GET_DEVID (event),_GET_BANK (event), +amd_iommu_pc_set_reg (hwc->idx,_GET_DEVID (event),_GET_BANK (event),_GET_CNTR (event),IOMMU_PC_COUNTER_SRC_REG , ®); }
@@ -301,7 +296,7 @@ static void perf_iommu_start(struct perf_event *event, int flags) val =local64_read (&hwc->prev_count); -amd_iommu_pc_set_counter (0 ,_GET_BANK (event),_GET_CNTR (event), &val); +amd_iommu_pc_set_counter (hwc->idx,_GET_BANK (event),_GET_CNTR (event), &val); enable:perf_iommu_enable_event (event);perf_event_update_userpage (event);
@@ -314,7 +309,7 @@ static void perf_iommu_read(struct perf_event *event)s64 delta;struct hw_perf_event *hwc = &event->hw; -if (amd_iommu_pc_get_counter (0 ,_GET_BANK (event),_GET_CNTR (event), &cnt)) +if (amd_iommu_pc_get_counter (hwc->idx,_GET_BANK (event),_GET_CNTR (event), &cnt))return ;/* IOMMU pc counter register is only 48 bits */
@@ -417,14 +412,20 @@ static __init int _init_events_attrs(struct perf_amd_iommu *perf_iommu)static __init void amd_iommu_pc_exit (void ) { -if (__perf_iommu.events_group !=NULL ) { -kfree (__perf_iommu.events_group); - __perf_iommu.events_group =NULL ; +struct perf_amd_iommu *pi, *next; + +list_for_each_entry_safe (pi, next, &perf_amd_iommu_list, list) { +list_del (&pi->list); + +kfree (pi->events_group); + pi->events_group =NULL ; + +kfree (pi); } } -static __init int _init_perf_amd_iommu ( -struct perf_amd_iommu *perf_iommu,char *name) +static __init int +init_one_perf_amd_iommu (struct perf_amd_iommu *perf_iommu,uint idx) {int ret;
@@ -441,54 +442,62 @@ static __init int _init_perf_amd_iommu(if (_init_events_attrs (perf_iommu) !=0 )pr_err ("Only support raw events.\n" ); - perf_iommu->max_banks =amd_iommu_pc_get_max_banks (0 ); - perf_iommu->max_counters =amd_iommu_pc_get_max_counters (0 ); +snprintf (perf_iommu->name,PERF_AMD_IOMMU_NAME_SZ ,"amd_iommu_%u" , idx); + perf_iommu->idx = idx; + perf_iommu->max_banks =amd_iommu_pc_get_max_banks (idx); + perf_iommu->max_counters =amd_iommu_pc_get_max_counters (idx);if (!perf_iommu->max_banks || !perf_iommu->max_counters)return -EINVAL ;/* Init null attributes */ perf_iommu->null_group =NULL ; + +/* Setting up PMU */ + perf_iommu->pmu.event_init = perf_iommu_event_init, + perf_iommu->pmu.add = perf_iommu_add, + perf_iommu->pmu.del = perf_iommu_del, + perf_iommu->pmu.start = perf_iommu_start, + perf_iommu->pmu.stop = perf_iommu_stop, + perf_iommu->pmu.read = perf_iommu_read, + perf_iommu->pmu.task_ctx_nr = perf_invalid_context; perf_iommu->pmu.attr_groups = perf_iommu->attr_groups; - ret =perf_pmu_register (&perf_iommu->pmu, name,-1 ); + ret =perf_pmu_register (&perf_iommu->pmu, perf_iommu->name,-1 );if (ret) {pr_err ("Error initializing AMD IOMMU perf counters.\n" );amd_iommu_pc_exit (); }else { -pr_info ("perf: amd_iommu: Detected. (%d banks, %d counters/bank)\n" , -amd_iommu_pc_get_max_banks (0 ), -amd_iommu_pc_get_max_counters (0 )); +pr_info ("Detected %s, w/ %d banks, %d counters/bank\n" , + perf_iommu->name, +amd_iommu_pc_get_max_banks (idx), +amd_iommu_pc_get_max_counters (idx)); + +list_add_tail (&perf_iommu->list, &perf_amd_iommu_list); }return ret; } -static struct perf_amd_iommu __perf_iommu = { - .pmu = { - .task_ctx_nr = perf_invalid_context, - .event_init = perf_iommu_event_init, - .add = perf_iommu_add, - .del = perf_iommu_del, - .start = perf_iommu_start, - .stop = perf_iommu_stop, - .read = perf_iommu_read, - }, - .max_banks =0x00 , - .max_counters =0x00 , - .cntr_assign_mask =0ULL , - .format_group =NULL , - .cpumask_group =NULL , - .events_group =NULL , - .null_group =NULL , -}; -static __init int amd_iommu_pc_init (void ) { +uint i; +/* Make sure the IOMMU PC resource is available */ if (!amd_iommu_pc_supported ())return -ENODEV ; -_init_perf_amd_iommu (&__perf_iommu,"amd_iommu" ); +for (i =0 ; i <amd_iommu_get_num_iommus (); i++) { +int ret; +struct perf_amd_iommu *pi; + + pi =kzalloc (sizeof (struct perf_amd_iommu ),GFP_KERNEL ); +if (!pi) +return -ENOMEM ; + + ret =init_one_perf_amd_iommu (pi, i); +if (ret) +return ret; + }return 0 ; }
Signature · 1 line
1.8.3.1
Joerg Roedel
maintainerRe: [PATCH V6 2/7] perf/amd/iommu: Modify functions to query max banks and counters
In reply to Suravee SuthikulpanitQuoted 7 lines · Suravee Suthikulpanit
arch/x86/events/amd/iommu.h +0
-1@@ -24,15 +24,12 @@#define PC_MAX_SPEC_BNKS 64#define PC_MAX_SPEC_CNTRS 16 -/* iommu pc reg masks*/
arch/x86/events/amd/iommu.h line 32
Quoted 10 lines
arch/x86/events/amd/iommu.h +2
-4-#define IOMMU_BASE_DEVID 0x0000 -/* amd_iommu_init.c external support functions */ extern bool amd_iommu_pc_supported (void ); -extern u8 amd_iommu_pc_get_max_banks (u16 devid); +extern u8 amd_iommu_pc_get_max_banks (uint devid); -extern u8 amd_iommu_pc_get_max_counters (u16 devid); +extern u8 amd_iommu_pc_get_max_counters (uint devid);
The parameters are now iommu-indexes and not device-ids anymore. You should reflect that in the prototypes here too.
drivers/iommu/amd_iommu_proto.h extern int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid, · line 62
Quoted 12 lines
drivers/iommu/amd_iommu_proto.h +2
-2@@ -58,8 +58,8 @@ extern int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,/* IOMMU Performance Counter functions */ extern bool amd_iommu_pc_supported (void ); -extern u8 amd_iommu_pc_get_max_banks (u16 devid); -extern u8 amd_iommu_pc_get_max_counters (u16 devid); +extern u8 amd_iommu_pc_get_max_banks (uint devid); +extern u8 amd_iommu_pc_get_max_counters (uint devid);
Same here, and why do you need to declare the prototypes in two different header files?
Re: [PATCH V6 2/7] perf/amd/iommu: Modify functions to query max banks and counters
Quoted 13 lines · Joerg Roedel
Quoted 12 lines · Suravee Suthikulpanit
arch/x86/events/amd/iommu.h +0
-3@@ -24,15 +24,12 @@#define PC_MAX_SPEC_BNKS 64#define PC_MAX_SPEC_CNTRS 16 -/* iommu pc reg masks*/ -#define IOMMU_BASE_DEVID 0x0000 -/* amd_iommu_init.c external support functions */ extern bool amd_iommu_pc_supported (void );
Quoted 8 lines
arch/x86/events/amd/iommu.h line 32
Quoted 5 lines
arch/x86/events/amd/iommu.h +2
-2-extern u8 amd_iommu_pc_get_max_banks (u16 devid); +extern u8 amd_iommu_pc_get_max_banks (uint devid); -extern u8 amd_iommu_pc_get_max_counters (u16 devid); +extern u8 amd_iommu_pc_get_max_counters (uint devid);The parameters are now iommu-indexes and not device-ids anymore. You should reflect that in the prototypes here too.
Ahh.. thanks for pointing out. I missed this part.
Quoted 6 lines
Quoted 6 lines
drivers/iommu/amd_iommu_proto.h +0
-0@@ -58,8 +58,8 @@ extern int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
Quoted 9 lines
drivers/iommu/amd_iommu_proto.h extern int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid, · line 62
Quoted 6 lines
drivers/iommu/amd_iommu_proto.h +2
-2/* IOMMU Performance Counter functions */ extern bool amd_iommu_pc_supported (void ); -extern u8 amd_iommu_pc_get_max_banks (u16 devid); -extern u8 amd_iommu_pc_get_max_counters (u16 devid); +extern u8 amd_iommu_pc_get_max_banks (uint devid); +extern u8 amd_iommu_pc_get_max_counters (uint devid);Same here, and why do you need to declare the prototypes in two different header files?
Hm.. actually good point. I will remove the unnecessary function declarations in the amd_iommu_proto.h.
Thanks,
Suravee