blob: f0d94c8b382a6527294744dfae95eea3078ff2f3 [file] [log] [blame]
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -05001/*
2 * Copyright (C) 2013 Advanced Micro Devices, Inc.
3 *
4 * Author: Steven Kinney <Steven.Kinney@amd.com>
5 * Author: Suravee Suthikulpanit <Suraveee.Suthikulpanit@amd.com>
6 *
7 * Perf: amd_iommu - AMD IOMMU Performance Counter PMU implementation
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Suravee Suthikulpanitf9573e52017-02-24 02:48:13 -060014#define pr_fmt(fmt) "perf/amd_iommu: " fmt
15
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050016#include <linux/perf_event.h>
Paul Gortmakereb008eb2016-07-13 20:19:01 -040017#include <linux/init.h>
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050018#include <linux/cpumask.h>
19#include <linux/slab.h>
20
Borislav Petkov27f6d222016-02-10 10:55:23 +010021#include "../perf_event.h"
Borislav Petkov5b265472016-02-08 17:09:07 +010022#include "iommu.h"
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050023
24#define COUNTER_SHIFT 16
25
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -060026/* iommu pmu conf masks */
27#define GET_CSOURCE(x) ((x)->conf & 0xFFULL)
28#define GET_DEVID(x) (((x)->conf >> 8) & 0xFFFFULL)
29#define GET_DOMID(x) (((x)->conf >> 24) & 0xFFFFULL)
30#define GET_PASID(x) (((x)->conf >> 40) & 0xFFFFFULL)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050031
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -060032/* iommu pmu conf1 masks */
33#define GET_DEVID_MASK(x) ((x)->conf1 & 0xFFFFULL)
34#define GET_DOMID_MASK(x) (((x)->conf1 >> 16) & 0xFFFFULL)
35#define GET_PASID_MASK(x) (((x)->conf1 >> 32) & 0xFFFFFULL)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050036
37static struct perf_amd_iommu __perf_iommu;
38
39struct perf_amd_iommu {
40 struct pmu pmu;
41 u8 max_banks;
42 u8 max_counters;
43 u64 cntr_assign_mask;
44 raw_spinlock_t lock;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050045};
46
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050047/*---------------------------------------------
48 * sysfs format attributes
49 *---------------------------------------------*/
50PMU_FORMAT_ATTR(csource, "config:0-7");
51PMU_FORMAT_ATTR(devid, "config:8-23");
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -060052PMU_FORMAT_ATTR(domid, "config:24-39");
53PMU_FORMAT_ATTR(pasid, "config:40-59");
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050054PMU_FORMAT_ATTR(devid_mask, "config1:0-15");
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -060055PMU_FORMAT_ATTR(domid_mask, "config1:16-31");
56PMU_FORMAT_ATTR(pasid_mask, "config1:32-51");
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050057
58static struct attribute *iommu_format_attrs[] = {
59 &format_attr_csource.attr,
60 &format_attr_devid.attr,
61 &format_attr_pasid.attr,
62 &format_attr_domid.attr,
63 &format_attr_devid_mask.attr,
64 &format_attr_pasid_mask.attr,
65 &format_attr_domid_mask.attr,
66 NULL,
67};
68
69static struct attribute_group amd_iommu_format_group = {
70 .name = "format",
71 .attrs = iommu_format_attrs,
72};
73
74/*---------------------------------------------
75 * sysfs events attributes
76 *---------------------------------------------*/
Suravee Suthikulpanit51686542017-02-24 02:48:20 -060077static struct attribute_group amd_iommu_events_group = {
78 .name = "events",
79};
80
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050081struct amd_iommu_event_desc {
82 struct kobj_attribute attr;
83 const char *event;
84};
85
86static ssize_t _iommu_event_show(struct kobject *kobj,
87 struct kobj_attribute *attr, char *buf)
88{
89 struct amd_iommu_event_desc *event =
90 container_of(attr, struct amd_iommu_event_desc, attr);
91 return sprintf(buf, "%s\n", event->event);
92}
93
94#define AMD_IOMMU_EVENT_DESC(_name, _event) \
95{ \
96 .attr = __ATTR(_name, 0444, _iommu_event_show, NULL), \
97 .event = _event, \
98}
99
100static struct amd_iommu_event_desc amd_iommu_v2_event_descs[] = {
101 AMD_IOMMU_EVENT_DESC(mem_pass_untrans, "csource=0x01"),
102 AMD_IOMMU_EVENT_DESC(mem_pass_pretrans, "csource=0x02"),
103 AMD_IOMMU_EVENT_DESC(mem_pass_excl, "csource=0x03"),
104 AMD_IOMMU_EVENT_DESC(mem_target_abort, "csource=0x04"),
105 AMD_IOMMU_EVENT_DESC(mem_trans_total, "csource=0x05"),
106 AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pte_hit, "csource=0x06"),
107 AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pte_mis, "csource=0x07"),
108 AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pde_hit, "csource=0x08"),
109 AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pde_mis, "csource=0x09"),
110 AMD_IOMMU_EVENT_DESC(mem_dte_hit, "csource=0x0a"),
111 AMD_IOMMU_EVENT_DESC(mem_dte_mis, "csource=0x0b"),
112 AMD_IOMMU_EVENT_DESC(page_tbl_read_tot, "csource=0x0c"),
113 AMD_IOMMU_EVENT_DESC(page_tbl_read_nst, "csource=0x0d"),
114 AMD_IOMMU_EVENT_DESC(page_tbl_read_gst, "csource=0x0e"),
115 AMD_IOMMU_EVENT_DESC(int_dte_hit, "csource=0x0f"),
116 AMD_IOMMU_EVENT_DESC(int_dte_mis, "csource=0x10"),
117 AMD_IOMMU_EVENT_DESC(cmd_processed, "csource=0x11"),
118 AMD_IOMMU_EVENT_DESC(cmd_processed_inv, "csource=0x12"),
119 AMD_IOMMU_EVENT_DESC(tlb_inv, "csource=0x13"),
Suravee Suthikulpanitf8519152016-02-28 22:23:29 -0600120 AMD_IOMMU_EVENT_DESC(ign_rd_wr_mmio_1ff8h, "csource=0x14"),
121 AMD_IOMMU_EVENT_DESC(vapic_int_non_guest, "csource=0x15"),
122 AMD_IOMMU_EVENT_DESC(vapic_int_guest, "csource=0x16"),
123 AMD_IOMMU_EVENT_DESC(smi_recv, "csource=0x17"),
124 AMD_IOMMU_EVENT_DESC(smi_blk, "csource=0x18"),
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500125 { /* end: all zeroes */ },
126};
127
128/*---------------------------------------------
129 * sysfs cpumask attributes
130 *---------------------------------------------*/
131static cpumask_t iommu_cpumask;
132
133static ssize_t _iommu_cpumask_show(struct device *dev,
134 struct device_attribute *attr,
135 char *buf)
136{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100137 return cpumap_print_to_pagebuf(true, buf, &iommu_cpumask);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500138}
139static DEVICE_ATTR(cpumask, S_IRUGO, _iommu_cpumask_show, NULL);
140
141static struct attribute *iommu_cpumask_attrs[] = {
142 &dev_attr_cpumask.attr,
143 NULL,
144};
145
146static struct attribute_group amd_iommu_cpumask_group = {
147 .attrs = iommu_cpumask_attrs,
148};
149
150/*---------------------------------------------*/
151
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600152static int get_next_avail_iommu_bnk_cntr(struct perf_event *event)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500153{
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600154 struct perf_amd_iommu *piommu = container_of(event->pmu, struct perf_amd_iommu, pmu);
155 int max_cntrs = piommu->max_counters;
156 int max_banks = piommu->max_banks;
157 u32 shift, bank, cntr;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500158 unsigned long flags;
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600159 int retval;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500160
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600161 raw_spin_lock_irqsave(&piommu->lock, flags);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500162
163 for (bank = 0, shift = 0; bank < max_banks; bank++) {
164 for (cntr = 0; cntr < max_cntrs; cntr++) {
165 shift = bank + (bank*3) + cntr;
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600166 if (piommu->cntr_assign_mask & BIT_ULL(shift)) {
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500167 continue;
168 } else {
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600169 piommu->cntr_assign_mask |= BIT_ULL(shift);
170 event->hw.iommu_bank = bank;
171 event->hw.iommu_cntr = cntr;
172 retval = 0;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500173 goto out;
174 }
175 }
176 }
177 retval = -ENOSPC;
178out:
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600179 raw_spin_unlock_irqrestore(&piommu->lock, flags);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500180 return retval;
181}
182
183static int clear_avail_iommu_bnk_cntr(struct perf_amd_iommu *perf_iommu,
184 u8 bank, u8 cntr)
185{
186 unsigned long flags;
187 int max_banks, max_cntrs;
188 int shift = 0;
189
190 max_banks = perf_iommu->max_banks;
191 max_cntrs = perf_iommu->max_counters;
192
193 if ((bank > max_banks) || (cntr > max_cntrs))
194 return -EINVAL;
195
196 shift = bank + cntr + (bank*3);
197
198 raw_spin_lock_irqsave(&perf_iommu->lock, flags);
199 perf_iommu->cntr_assign_mask &= ~(1ULL<<shift);
200 raw_spin_unlock_irqrestore(&perf_iommu->lock, flags);
201
202 return 0;
203}
204
205static int perf_iommu_event_init(struct perf_event *event)
206{
207 struct hw_perf_event *hwc = &event->hw;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500208
209 /* test the event attr type check for PMU enumeration */
210 if (event->attr.type != event->pmu->type)
211 return -ENOENT;
212
213 /*
214 * IOMMU counters are shared across all cores.
215 * Therefore, it does not support per-process mode.
216 * Also, it does not support event sampling mode.
217 */
218 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
219 return -EINVAL;
220
221 /* IOMMU counters do not have usr/os/guest/host bits */
222 if (event->attr.exclude_user || event->attr.exclude_kernel ||
223 event->attr.exclude_host || event->attr.exclude_guest)
224 return -EINVAL;
225
226 if (event->cpu < 0)
227 return -EINVAL;
228
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500229 /* update the hw_perf_event struct with the iommu config data */
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600230 hwc->conf = event->attr.config;
231 hwc->conf1 = event->attr.config1;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500232
233 return 0;
234}
235
236static void perf_iommu_enable_event(struct perf_event *ev)
237{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600238 struct amd_iommu *iommu = get_amd_iommu(0);
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600239 struct hw_perf_event *hwc = &ev->hw;
240 u8 bank = hwc->iommu_bank;
241 u8 cntr = hwc->iommu_cntr;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500242 u64 reg = 0ULL;
243
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600244 reg = GET_CSOURCE(hwc);
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600245 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_COUNTER_SRC_REG, &reg);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500246
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600247 reg = GET_DEVID_MASK(hwc);
248 reg = GET_DEVID(hwc) | (reg << 32);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500249 if (reg)
Suravee Suthikulpanit6aad0c62017-02-24 02:48:14 -0600250 reg |= BIT(31);
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600251 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_DEVID_MATCH_REG, &reg);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500252
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600253 reg = GET_PASID_MASK(hwc);
254 reg = GET_PASID(hwc) | (reg << 32);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500255 if (reg)
Suravee Suthikulpanit6aad0c62017-02-24 02:48:14 -0600256 reg |= BIT(31);
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600257 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_PASID_MATCH_REG, &reg);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500258
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600259 reg = GET_DOMID_MASK(hwc);
260 reg = GET_DOMID(hwc) | (reg << 32);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500261 if (reg)
Suravee Suthikulpanit6aad0c62017-02-24 02:48:14 -0600262 reg |= BIT(31);
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600263 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_DOMID_MATCH_REG, &reg);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500264}
265
266static void perf_iommu_disable_event(struct perf_event *event)
267{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600268 struct amd_iommu *iommu = get_amd_iommu(0);
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600269 struct hw_perf_event *hwc = &event->hw;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500270 u64 reg = 0ULL;
271
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600272 amd_iommu_pc_set_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600273 IOMMU_PC_COUNTER_SRC_REG, &reg);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500274}
275
276static void perf_iommu_start(struct perf_event *event, int flags)
277{
278 struct hw_perf_event *hwc = &event->hw;
279
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500280 if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
281 return;
282
283 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
284 hwc->state = 0;
285
286 if (flags & PERF_EF_RELOAD) {
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600287 u64 prev_raw_count = local64_read(&hwc->prev_count);
288 struct amd_iommu *iommu = get_amd_iommu(0);
289
290 amd_iommu_pc_set_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600291 IOMMU_PC_COUNTER_REG, &prev_raw_count);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500292 }
293
294 perf_iommu_enable_event(event);
295 perf_event_update_userpage(event);
296
297}
298
299static void perf_iommu_read(struct perf_event *event)
300{
Suravee Suthikulpanitdc6ca5e2017-02-24 02:48:15 -0600301 u64 count, prev, delta;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500302 struct hw_perf_event *hwc = &event->hw;
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600303 struct amd_iommu *iommu = get_amd_iommu(0);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500304
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600305 if (amd_iommu_pc_get_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600306 IOMMU_PC_COUNTER_REG, &count))
307 return;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500308
309 /* IOMMU pc counter register is only 48 bits */
Suravee Suthikulpanitdc6ca5e2017-02-24 02:48:15 -0600310 count &= GENMASK_ULL(47, 0);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500311
Suravee Suthikulpanitdc6ca5e2017-02-24 02:48:15 -0600312 prev = local64_read(&hwc->prev_count);
313 if (local64_cmpxchg(&hwc->prev_count, prev, count) != prev)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500314 return;
315
Suravee Suthikulpanitdc6ca5e2017-02-24 02:48:15 -0600316 /* Handle 48-bit counter overflow */
317 delta = (count << COUNTER_SHIFT) - (prev << COUNTER_SHIFT);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500318 delta >>= COUNTER_SHIFT;
319 local64_add(delta, &event->count);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500320}
321
322static void perf_iommu_stop(struct perf_event *event, int flags)
323{
324 struct hw_perf_event *hwc = &event->hw;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500325
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500326 if (hwc->state & PERF_HES_UPTODATE)
327 return;
328
329 perf_iommu_disable_event(event);
330 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
331 hwc->state |= PERF_HES_STOPPED;
332
333 if (hwc->state & PERF_HES_UPTODATE)
334 return;
335
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500336 perf_iommu_read(event);
337 hwc->state |= PERF_HES_UPTODATE;
338}
339
340static int perf_iommu_add(struct perf_event *event, int flags)
341{
342 int retval;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500343
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500344 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
345
346 /* request an iommu bank/counter */
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600347 retval = get_next_avail_iommu_bnk_cntr(event);
348 if (retval)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500349 return retval;
350
351 if (flags & PERF_EF_START)
352 perf_iommu_start(event, PERF_EF_RELOAD);
353
354 return 0;
355}
356
357static void perf_iommu_del(struct perf_event *event, int flags)
358{
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600359 struct hw_perf_event *hwc = &event->hw;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500360 struct perf_amd_iommu *perf_iommu =
361 container_of(event->pmu, struct perf_amd_iommu, pmu);
362
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500363 perf_iommu_stop(event, PERF_EF_UPDATE);
364
365 /* clear the assigned iommu bank/counter */
366 clear_avail_iommu_bnk_cntr(perf_iommu,
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600367 hwc->iommu_bank, hwc->iommu_cntr);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500368
369 perf_event_update_userpage(event);
370}
371
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600372static __init int _init_events_attrs(void)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500373{
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500374 int i = 0, j;
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600375 struct attribute **attrs;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500376
377 while (amd_iommu_v2_event_descs[i].attr.attr.name)
378 i++;
379
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600380 attrs = kzalloc(sizeof(struct attribute **) * (i + 1), GFP_KERNEL);
381 if (!attrs)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500382 return -ENOMEM;
383
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500384 for (j = 0; j < i; j++)
385 attrs[j] = &amd_iommu_v2_event_descs[j].attr.attr;
386
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600387 amd_iommu_events_group.attrs = attrs;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500388 return 0;
389}
390
391static __init void amd_iommu_pc_exit(void)
392{
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600393 kfree(amd_iommu_events_group.attrs);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500394}
395
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600396const struct attribute_group *amd_iommu_attr_groups[] = {
397 &amd_iommu_format_group,
398 &amd_iommu_cpumask_group,
399 &amd_iommu_events_group,
400 NULL,
401};
402
403static __init int
404_init_perf_amd_iommu(struct perf_amd_iommu *perf_iommu, char *name)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500405{
406 int ret;
407
408 raw_spin_lock_init(&perf_iommu->lock);
409
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500410 /* Init cpumask attributes to only core 0 */
411 cpumask_set_cpu(0, &iommu_cpumask);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500412
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -0600413 perf_iommu->max_banks = amd_iommu_pc_get_max_banks(0);
414 perf_iommu->max_counters = amd_iommu_pc_get_max_counters(0);
415 if (!perf_iommu->max_banks || !perf_iommu->max_counters)
416 return -EINVAL;
417
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600418 perf_iommu->pmu.attr_groups = amd_iommu_attr_groups;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500419 ret = perf_pmu_register(&perf_iommu->pmu, name, -1);
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600420 if (ret)
Suravee Suthikulpanitf9573e52017-02-24 02:48:13 -0600421 pr_err("Error initializing AMD IOMMU perf counters.\n");
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600422 else
Suravee Suthikulpanitf9573e52017-02-24 02:48:13 -0600423 pr_info("Detected AMD IOMMU (%d banks, %d counters/bank).\n",
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -0600424 amd_iommu_pc_get_max_banks(0),
425 amd_iommu_pc_get_max_counters(0));
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500426 return ret;
427}
428
429static struct perf_amd_iommu __perf_iommu = {
430 .pmu = {
Peter Zijlstra84827162016-04-24 00:42:55 +0200431 .task_ctx_nr = perf_invalid_context,
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500432 .event_init = perf_iommu_event_init,
433 .add = perf_iommu_add,
434 .del = perf_iommu_del,
435 .start = perf_iommu_start,
436 .stop = perf_iommu_stop,
437 .read = perf_iommu_read,
438 },
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500439};
440
441static __init int amd_iommu_pc_init(void)
442{
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600443 int ret;
444
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500445 /* Make sure the IOMMU PC resource is available */
Peter Zijlstra100ac532013-07-03 09:55:42 +0200446 if (!amd_iommu_pc_supported())
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500447 return -ENODEV;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500448
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600449 ret = _init_events_attrs();
450 if (ret)
451 return ret;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500452
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600453 ret = _init_perf_amd_iommu(&__perf_iommu, "amd_iommu");
454 if (ret)
455 amd_iommu_pc_exit();
456
457 return ret;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500458}
459
460device_initcall(amd_iommu_pc_init);