From patchwork Tue Feb 9 22:53:55 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Suthikulpanit, Suravee" X-Patchwork-Id: 61602 Delivered-To: patch@linaro.org Received: by 10.112.43.199 with SMTP id y7csp2316820lbl; Tue, 9 Feb 2016 14:55:07 -0800 (PST) X-Received: by 10.98.18.207 with SMTP id 76mr52993111pfs.53.1455058507076; Tue, 09 Feb 2016 14:55:07 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q10si450676pfq.39.2016.02.09.14.55.06; Tue, 09 Feb 2016 14:55:07 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933345AbcBIWzE (ORCPT + 30 others); Tue, 9 Feb 2016 17:55:04 -0500 Received: from mail-bn1on0058.outbound.protection.outlook.com ([157.56.110.58]:36798 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S965038AbcBIWyo (ORCPT ); Tue, 9 Feb 2016 17:54:44 -0500 Authentication-Results: 8bytes.org; dkim=none (message not signed) header.d=none; 8bytes.org; dmarc=none action=none header.from=amd.com; Received: from localhost.localdomain (58.10.104.205) by BLUPR12MB0434.namprd12.prod.outlook.com (10.162.92.14) with Microsoft SMTP Server (TLS) id 15.1.403.16; Tue, 9 Feb 2016 22:54:38 +0000 From: Suravee Suthikulpanit To: , , , , CC: , , , Suravee Suthikulpanit Subject: [PATCH V3 5/5] perf/amd/iommu: Enable support for multiple IOMMUs Date: Tue, 9 Feb 2016 16:53:55 -0600 Message-ID: <1455058435-8716-6-git-send-email-Suravee.Suthikulpanit@amd.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1455058435-8716-1-git-send-email-Suravee.Suthikulpanit@amd.com> References: <1455058435-8716-1-git-send-email-Suravee.Suthikulpanit@amd.com> MIME-Version: 1.0 X-Originating-IP: [58.10.104.205] X-ClientProxiedBy: SG2PR03CA0004.apcprd03.prod.outlook.com (25.160.233.14) To BLUPR12MB0434.namprd12.prod.outlook.com (25.162.92.14) X-MS-Office365-Filtering-Correlation-Id: 96281113-fee5-4ded-b70a-08d331a3fef3 X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0434; 2:3JqVrAJ41LMovGS8dN3GF7rDlGnlx+JacarF4aGRtog2hhngIHIrpaLLf+ewIsovBU9mc74/O1Mp9M580EeDCkfP/PmcQZGoybKFIaCzPH6dcz0A4ZunchiWnQOS6tFLug2K4Nrh6Sf5/u2nIRpoEJvbamPFmzE0KJIeXM+/JSmNVsnXnZD3E8efg2PBRQ2l; 3:t+3y5eJZEh6ueLsukmu5MeuPZcKT/XcejihVBpV8EHS9qWAJ4TNz0Gue4rOKIlV7bO+SnENnQDLeUfgJrelcS1DZcFGan0JIZsmJ+2kMLHvl1gQYVA9YHvnTq6Vz40mT; 25:fWX/f20QzjanW2t6k1+8KSzh3FW/3IeEW+oo9zta5dJg/LXQ61y4jOLyB8UJ4+Jd0xfTFby1hMppik7gqi3gtVp2JVKldsbtyrHfieam/kTkWFPJ8PcLLjqR2Cpk5F7HE85AQKEGT8lMgRcanavhsQQ2/SE+XieUhSUJCeg/JZBdmwHVydlY3mIlphUMPqXExWvAWMhkrAFqsRGwjfwGfVANLAeqKwXglSE3o4mU1Y03eXxTHBQcP88RPYYsXi+VkEF3sceVBwlCEp2SjMaqEhIF24poJUUVP4RWPy3h4hcVrOj3HmjX6WBhWcADSLAC X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BLUPR12MB0434; X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0434; 20:wqgWJydcKx/yy17vuVE2/gbXZ794yX2peN/zqWYICA8zPZyySZ8nPkpPQR9Jvi9q8nRItizDxj20778ARIpr2BAr4H+31meNbyGIkll7MTwSMJLVz+yQCLTzGMqiEv7E1wRUDVHGm97A/HlDl6wdayFYhk2rWSP7qFitXaSqMNqM6IzNlNVCdbTD4gjr8UyZ8+rvEsynxD6vhEU1mDKNjrPuDwvxMrpYSBOenHTuPrcFDbx91f+GNzzrNr9RjLO8eHo5yEZb8piQ/LzDsjJ/HrmKZWLHv9p2/CPmFAbyg/q2rDes3ZYoIACQAABOkJoKyj1+dRq2xiIDJlLytleYTEOvczQwxwPC8Z4hhWDwABPqS5QwMNA4QUVb25ucY2+MbI/Xr8b4hjSjYKkmcbS+dAXbb/gDLuFXCAu9QKIBXQH+wO3qhnxFjKQw1XgcQefendR7u416h7JtooahqK08cYAN0x0A5WnMZP7pdH3GTNBIRwZmQ5MXe+phv+leqC+9; 4:Nk6M2/JT94N0Fen2lIqSdjrscXUjkVKpfB0NxKAlaLzKWShm9AlCaai285jgeg0QKnJ8NcoPdt32uyQuD91Z1G9Mb6x/j+SWaJMLws+6J+dVDWESuSxG8ZIOt2VjdGm9eGvs5c8XkZEaDbea6QHPcmYWY9v2HX9rjDpv1bx27xi+tXuTEFjipoNV/KPJHYO27vh10dqucQHGQT57e9PU+4YNTa+Fdx4t/asSQCYTBdsGKSR9amSAucY2LcYTtBHYl1P6z7yvX+TXrDCBFN8LOaG4lbKei5zYC1NGqfNEZwZk+8tRQhUU9umUi9de87mhz2PuDE9e37OtVGzxMPIXsuYq5IM+p0fs180MwS8e1iJH47rpM4dN6zX6Adc20/OrOYFqrFbKweCCYhIB4r6Mgg== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046); SRVR:BLUPR12MB0434; BCL:0; PCL:0; RULEID:; SRVR:BLUPR12MB0434; X-Forefront-PRVS: 08476BC6EF X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(6009001)(6069001)(19580405001)(19580395003)(48376002)(122386002)(36756003)(77096005)(5001960100002)(5003940100001)(92566002)(86362001)(50986999)(42186005)(76176999)(2906002)(87976001)(40100003)(5008740100001)(189998001)(4326007)(5004730100002)(50466002)(47776003)(66066001)(229853001)(50226001)(5001770100001)(3846002)(6116002)(1096002)(2950100001)(2201001)(586003); DIR:OUT; SFP:1101; SCL:1; SRVR:BLUPR12MB0434; H:localhost.localdomain; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0434; 23:5Y9eQW0gcmXxNqTZL/IGvmUk3UQ8Jpp6ALqr57pagIuhS1Oe+Puo9xHFbi1dSSy3amNAA5+YSMtaRgnnjNsZKcQxd8AEfKDz/lO4Un1zOqc1qkNJohpyONFXK4ybItSv1DhWGrrj7Ci93k7atAbGcGgRBqJxwKfV2M5Jy6CZPwD05IZ/ZxBxwovmIf6wd/XGFsq9sbKRloSzElPSi3fwGfp8VQV/AwRQcWLjmaegHRW2noVzVlkeJS8JoSThkTo0Y6iczf74rUS2SQfb/EGgH54w0lacPZeRdtqV2JPwrhhtDiIbI29BSRcj4F+tVFo9V9fG7y9LQwJlk37+AthHKS3bKOASlnlH2rHy7qJyZ2CFK8/SB/gpz/vl1HD5754WcpnKSezyT/etPfr8C+WpaOf7FawRyoo2KJ426jRekD6zzd2ene3d97iCNnYZkR0Xa9HVL1BwGjsZEfBzikS0oa33CqCr1gBeza0uA+dy7bzYu9OcJBe8Fo8x5KJaIQL+xDf18YDIiG6KuplXlCLy0HsGDVAWumeZ1z1IMN6WHajaBRIe+ynJSs1VYa+bdkniYg+1BSWfqAANq6uwyjzK/XSbo2yK57+Y9LtAF8NrHiIzZWCa1wsAo8/WPm7zbnL6HNuA1Kdng/w9Zwo47g1GC3cUiroetqFqQ56kmAUuWWAn5uyzzIqw+18GYE3Fq3Wxb/IOHJ0mvvxjD1DSRmRVNFHSx+fBdq8hOKosdquke2g0B01lxrQlTpgNgT7RGffK7k6e1usnoeuNX4FnppN0HqdcuAzMF7bhCdpjrcCYM16Y4oOM1BKf3zLzP8mXpBIziY14aQJEPRutYVwixweNv4ZBBLHVGllEdAiNQMe4Cu0/wZc8TGNibmBOVmTogP2P3GMmr8QsAfeU3KYhqn/I1lRT9CR43n5Y0EfaLH7wg7n2wOHNJe/95yCl14rD5eRa X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0434; 5:zy4tuafBaUVaHoa8k2iGV8E941PgaW8bXp1llziQK1MkxisWqLAQKitVSkeO6k9J1uoPXoiy/1xuUhL6ni+Mf+KjjDOZT2LnJnfvMMuGm8dW4rimGa9ru2mOcH7raUKxlKi/ip08RSW+hF4Vazcgbg==; 24:rYvZ8BKfg1xKkw/eICmA9Ao2fCbTDZo0Q1R98RnGBqF26g0oKIp2Ql3cEcaAX/1YQniu/xwXcqhjXuh+wT4zhzw2Hp75kJPKUP8LC3LWLuI=; 20:+k6d5wrYiXbIWNKHFOoOUi3vfcn+azS7CPc2dPddpUBWMoGBPBcQRK6cjYpiaZsgjrQiToa0ztm5NODe4cap7y7acFX/NRPjVE+fFENnY3s0SRr+O1BJPgz3PFzDIcMRY2Nok44V204kY8wGQgYJ5lco8e+L55YpDPm/ca4Gsh+SDaDhakkRrZTSL468x9OXbF6TaUrsIfiB8WQ12GZPgD0vKEryMIRHm58UyweJuZAP3e4T0wd6pXF2xJskFeRR X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Feb 2016 22:54:38.3217 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR12MB0434 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The current amd_iommu_pc_get_set_reg_val() does not support muli-IOMMU system. This patch replace amd_iommu_pc_get_set_reg_val() with amd_iommu_pc_set_reg_val() and amd_iommu_pc_[set|get]_cnt_vals(). Also, the current struct hw_perf_event.prev_count can only store the previous counter value only from one IOMMU. So, this patch introduces a new data structure, perf_amd_iommu.prev_cnts, to track previous value of each performance counter of each bank of each IOMMU. Last, this patch introduce perf_iommu_cnts to temporary hold counter values from each IOMMU for internal use when manages counters among multiple IOMMUs. Note that this implementation makes an assumption that the counters on all IOMMUs will be programmed the same way (i.e with the same events). Signed-off-by: Suravee Suthikulpanit --- arch/x86/kernel/cpu/perf_event_amd_iommu.c | 125 +++++++++++++++++++++-------- drivers/iommu/amd_iommu_init.c | 87 +++++++++++++++++--- include/linux/perf/perf_event_amd_iommu.h | 8 +- 3 files changed, 174 insertions(+), 46 deletions(-) -- 2.5.0 diff --git a/arch/x86/kernel/cpu/perf_event_amd_iommu.c b/arch/x86/kernel/cpu/perf_event_amd_iommu.c index 791bbcf..ce6ba3f 100644 --- a/arch/x86/kernel/cpu/perf_event_amd_iommu.c +++ b/arch/x86/kernel/cpu/perf_event_amd_iommu.c @@ -42,6 +42,12 @@ struct perf_amd_iommu { u64 cntr_assign_mask; raw_spinlock_t lock; const struct attribute_group *attr_groups[4]; + + /* This is a 3D array used to store the previous count values + * from each performance counter of each bank of each IOMMU. + * I.E. size of array = (num iommus * num banks * num counters) + */ + local64_t *prev_cnts; }; #define format_group attr_groups[0] @@ -121,6 +127,11 @@ static struct amd_iommu_event_desc amd_iommu_v2_event_descs[] = { { /* end: all zeroes */ }, }; +/* This is an array used to temporary hold the current values + * read from a particular perf counter from each IOMMU. + */ +static u64 *perf_iommu_cnts; + /*--------------------------------------------- * sysfs cpumask attributes *---------------------------------------------*/ @@ -256,44 +267,46 @@ static void perf_iommu_enable_event(struct perf_event *ev) u64 reg = 0ULL; reg = csource; - amd_iommu_pc_get_set_reg_val(devid, + amd_iommu_pc_set_reg_val(devid, _GET_BANK(ev), _GET_CNTR(ev) , - IOMMU_PC_COUNTER_SRC_REG, ®, true); + 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, + amd_iommu_pc_set_reg_val(devid, _GET_BANK(ev), _GET_CNTR(ev) , - IOMMU_PC_DEVID_MATCH_REG, ®, true); + 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, + amd_iommu_pc_set_reg_val(devid, _GET_BANK(ev), _GET_CNTR(ev) , - IOMMU_PC_PASID_MATCH_REG, ®, true); + 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, + amd_iommu_pc_set_reg_val(devid, _GET_BANK(ev), _GET_CNTR(ev) , - IOMMU_PC_DOMID_MATCH_REG, ®, true); + 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), + amd_iommu_pc_set_reg_val(_GET_DEVID(event), _GET_BANK(event), _GET_CNTR(event), - IOMMU_PC_COUNTER_SRC_REG, ®, true); + IOMMU_PC_COUNTER_SRC_REG, ®); } static void perf_iommu_start(struct perf_event *event, int flags) { struct hw_perf_event *hwc = &event->hw; + struct perf_amd_iommu *perf_iommu = + container_of(event->pmu, struct perf_amd_iommu, pmu); pr_debug("perf: amd_iommu:perf_iommu_start\n"); if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED))) @@ -303,10 +316,19 @@ static void perf_iommu_start(struct perf_event *event, int flags) 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); + int i; + + for (i = 0; i < amd_iommu_get_num_iommus(); i++) { + int index = get_iommu_bnk_cnt_evt_idx(perf_iommu, i, + _GET_BANK(event), _GET_CNTR(event)); + + perf_iommu_cnts[i] = local64_read( + &perf_iommu->prev_cnts[index]); + } + + amd_iommu_pc_set_cnt_vals(_GET_BANK(event), _GET_CNTR(event), + amd_iommu_get_num_iommus(), + perf_iommu_cnts); } perf_iommu_enable_event(event); @@ -316,29 +338,47 @@ 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; + int i; u64 delta = 0ULL; 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); + struct perf_amd_iommu *perf_iommu = + container_of(event->pmu, struct perf_amd_iommu, pmu); - /* IOMMU pc counter register is only 48 bits */ - count &= 0xFFFFFFFFFFFFULL; + pr_debug("perf: amd_iommu:perf_iommu_read\n"); - prev_raw_count = local64_read(&hwc->prev_count); - if (local64_cmpxchg(&hwc->prev_count, prev_raw_count, - count) != prev_raw_count) + if (amd_iommu_pc_get_cnt_vals(_GET_BANK(event), _GET_CNTR(event), + amd_iommu_get_num_iommus(), + perf_iommu_cnts)) return; - /* Handling 48-bit counter overflowing */ - delta = (count << COUNTER_SHIFT) - (prev_raw_count << COUNTER_SHIFT); - delta >>= COUNTER_SHIFT; - local64_add(delta, &event->count); - + /* Now we re-aggregating event counts and prev-counts + * from all IOMMUs + */ + local64_set(&hwc->prev_count, 0); + + for (i = 0; i < amd_iommu_get_num_iommus(); i++) { + int indx = get_iommu_bnk_cnt_evt_idx(perf_iommu, i, + _GET_BANK(event), _GET_CNTR(event)); + u64 prev_raw_count = local64_read(&perf_iommu->prev_cnts[indx]); + + /* IOMMU pc counter register is only 48 bits */ + perf_iommu_cnts[i] &= 0xFFFFFFFFFFFFULL; + + /* + * Since we do not enable counter overflow interrupts, + * we do not have to worry about prev_count changing on us + */ + local64_set(&perf_iommu->prev_cnts[indx], + perf_iommu_cnts[i]); + + local64_add(prev_raw_count, &hwc->prev_count); + + /* Handling 48-bit counter overflowing */ + delta = (perf_iommu_cnts[i] << COUNTER_SHIFT) - + (prev_raw_count << COUNTER_SHIFT); + delta >>= COUNTER_SHIFT; + local64_add(delta, &event->count); + } } static void perf_iommu_stop(struct perf_event *event, int flags) @@ -428,10 +468,14 @@ 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; - } + kfree(__perf_iommu.events_group); + __perf_iommu.events_group = NULL; + + kfree(__perf_iommu.prev_cnts); + __perf_iommu.prev_cnts = NULL; + + kfree(perf_iommu_cnts); + perf_iommu_cnts = NULL; } static __init int _init_perf_amd_iommu( @@ -461,6 +505,17 @@ static __init int _init_perf_amd_iommu( perf_iommu->null_group = NULL; perf_iommu->pmu.attr_groups = perf_iommu->attr_groups; + perf_iommu->prev_cnts = kzalloc(sizeof(*perf_iommu->prev_cnts) * + (amd_iommu_get_num_iommus() * perf_iommu->max_banks * + perf_iommu->max_counters), GFP_KERNEL); + if (!perf_iommu->prev_cnts) + return -ENOMEM; + + perf_iommu_cnts = kzalloc(sizeof(*perf_iommu_cnts) * + amd_iommu_get_num_iommus(), GFP_KERNEL); + if (!perf_iommu_cnts) + return -ENOMEM; + ret = perf_pmu_register(&perf_iommu->pmu, name, -1); if (ret) { pr_err("perf: amd_iommu: Failed to initialized.\n"); diff --git a/drivers/iommu/amd_iommu_init.c b/drivers/iommu/amd_iommu_init.c index 531b2e1..7b1b561 100644 --- a/drivers/iommu/amd_iommu_init.c +++ b/drivers/iommu/amd_iommu_init.c @@ -1133,6 +1133,9 @@ static int __init init_iommu_all(struct acpi_table_header *table) return 0; } +static int _amd_iommu_pc_get_set_reg_val(struct amd_iommu *iommu, + u8 bank, u8 cntr, u8 fxn, + u64 *value, bool is_write); static void init_iommu_perf_ctr(struct amd_iommu *iommu) { @@ -1144,8 +1147,8 @@ static void init_iommu_perf_ctr(struct amd_iommu *iommu) amd_iommu_pc_present = true; /* Check if the performance counters can be written to */ - if ((0 != amd_iommu_pc_get_set_reg_val(0, 0, 0, 0, &val, true)) || - (0 != amd_iommu_pc_get_set_reg_val(0, 0, 0, 0, &val2, false)) || + if ((_amd_iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val, true)) || + (_amd_iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val2, false)) || (val != val2)) { pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n"); amd_iommu_pc_present = false; @@ -2294,10 +2297,10 @@ u8 amd_iommu_pc_get_max_counters(void) } EXPORT_SYMBOL(amd_iommu_pc_get_max_counters); -int amd_iommu_pc_get_set_reg_val(u16 devid, u8 bank, u8 cntr, u8 fxn, - u64 *value, bool is_write) +static int _amd_iommu_pc_get_set_reg_val(struct amd_iommu *iommu, + u8 bank, u8 cntr, u8 fxn, + u64 *value, bool is_write) { - struct amd_iommu *iommu; u32 offset; u32 max_offset_lim; @@ -2305,9 +2308,6 @@ int amd_iommu_pc_get_set_reg_val(u16 devid, u8 bank, u8 cntr, u8 fxn, if (!amd_iommu_pc_present) return -ENODEV; - /* Locate the iommu associated with the device ID */ - iommu = amd_iommu_rlookup_table[devid]; - /* Check for valid iommu and pc register indexing */ if (WARN_ON((iommu == NULL) || (fxn > 0x28) || (fxn & 7))) return -ENODEV; @@ -2332,4 +2332,73 @@ int amd_iommu_pc_get_set_reg_val(u16 devid, u8 bank, u8 cntr, u8 fxn, return 0; } -EXPORT_SYMBOL(amd_iommu_pc_get_set_reg_val); + +int amd_iommu_pc_set_reg_val(u16 devid, u8 bank, u8 cntr, u8 fxn, u64 *value) +{ + struct amd_iommu *iommu; + + for_each_iommu(iommu) { + int ret = _amd_iommu_pc_get_set_reg_val(iommu, bank, cntr, + fxn, value, true); + if (ret) + return ret; + } + + return 0; +} +EXPORT_SYMBOL(amd_iommu_pc_set_reg_val); + +int amd_iommu_pc_set_cnt_vals(u8 bank, u8 cntr, int num, u64 *value) +{ + struct amd_iommu *iommu; + int i = 0; + + if (num > amd_iommus_present) + return -EINVAL; + + for_each_iommu(iommu) { + int ret = _amd_iommu_pc_get_set_reg_val(iommu, bank, cntr, + IOMMU_PC_COUNTER_REG, + &value[i], true); + if (ret) + return ret; + if (i++ == amd_iommus_present) + break; + } + + return 0; +} +EXPORT_SYMBOL(amd_iommu_pc_set_cnt_vals); + +int amd_iommu_pc_get_cnt_vals(u8 bank, u8 cntr, int num, u64 *value) +{ + struct amd_iommu *iommu; + int i = 0, ret; + + if (!num) + return -EINVAL; + + /* + * Here, we read the specified counters on all IOMMU, + * which should have been programmed the same way. + * and aggregate the counter values. + */ + for_each_iommu(iommu) { + u64 tmp; + + if (i >= num) + return -EINVAL; + + ret = _amd_iommu_pc_get_set_reg_val(iommu, bank, cntr, + IOMMU_PC_COUNTER_REG, + &tmp, false); + if (ret) + return ret; + + /* IOMMU pc counter register is only 48 bits */ + value[i] = tmp & 0xFFFFFFFFFFFFULL; + } + + return 0; +} +EXPORT_SYMBOL(amd_iommu_pc_get_cnt_vals); diff --git a/include/linux/perf/perf_event_amd_iommu.h b/include/linux/perf/perf_event_amd_iommu.h index cb820c2..be1a17d 100644 --- a/include/linux/perf/perf_event_amd_iommu.h +++ b/include/linux/perf/perf_event_amd_iommu.h @@ -33,7 +33,11 @@ extern u8 amd_iommu_pc_get_max_banks(void); extern u8 amd_iommu_pc_get_max_counters(void); -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_val(u16 devid, u8 bank, u8 cntr, u8 fxn, + u64 *value); + +extern int amd_iommu_pc_set_cnt_vals(u8 bank, u8 cntr, int num, u64 *value); + +extern int amd_iommu_pc_get_cnt_vals(u8 bank, u8 cntr, int num, u64 *value); #endif /*_PERF_EVENT_AMD_IOMMU_H_*/