1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Advanced Micro Devices, Inc.
5 * Author: Steven Kinney <Steven.Kinney@amd.com>
6 * Author: Suravee Suthikulpanit <Suraveee.Suthikulpanit@amd.com>
8 * Perf: amd_iommu - AMD IOMMU Performance Counter PMU implementation
11 #define pr_fmt(fmt) "perf/amd_iommu: " fmt
13 #include <linux/perf_event.h>
14 #include <linux/init.h>
15 #include <linux/cpumask.h>
16 #include <linux/slab.h>
18 #include "../perf_event.h"
21 /* iommu pmu conf masks */
22 #define GET_CSOURCE(x) ((x)->conf & 0xFFULL)
23 #define GET_DEVID(x) (((x)->conf >> 8) & 0xFFFFULL)
24 #define GET_DOMID(x) (((x)->conf >> 24) & 0xFFFFULL)
25 #define GET_PASID(x) (((x)->conf >> 40) & 0xFFFFFULL)
27 /* iommu pmu conf1 masks */
28 #define GET_DEVID_MASK(x) ((x)->conf1 & 0xFFFFULL)
29 #define GET_DOMID_MASK(x) (((x)->conf1 >> 16) & 0xFFFFULL)
30 #define GET_PASID_MASK(x) (((x)->conf1 >> 32) & 0xFFFFFULL)
32 #define IOMMU_NAME_SIZE 16
34 struct perf_amd_iommu {
35 struct list_head list;
37 struct amd_iommu *iommu;
38 char name[IOMMU_NAME_SIZE];
45 static LIST_HEAD(perf_amd_iommu_list);
47 /*---------------------------------------------
48 * sysfs format attributes
49 *---------------------------------------------*/
50 PMU_FORMAT_ATTR(csource, "config:0-7");
51 PMU_FORMAT_ATTR(devid, "config:8-23");
52 PMU_FORMAT_ATTR(domid, "config:24-39");
53 PMU_FORMAT_ATTR(pasid, "config:40-59");
54 PMU_FORMAT_ATTR(devid_mask, "config1:0-15");
55 PMU_FORMAT_ATTR(domid_mask, "config1:16-31");
56 PMU_FORMAT_ATTR(pasid_mask, "config1:32-51");
58 static 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,
69 static struct attribute_group amd_iommu_format_group = {
71 .attrs = iommu_format_attrs,
74 /*---------------------------------------------
75 * sysfs events attributes
76 *---------------------------------------------*/
77 static struct attribute_group amd_iommu_events_group = {
81 struct amd_iommu_event_desc {
82 struct device_attribute attr;
86 static ssize_t _iommu_event_show(struct device *dev,
87 struct device_attribute *attr, char *buf)
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);
94 #define AMD_IOMMU_EVENT_DESC(_name, _event) \
96 .attr = __ATTR(_name, 0444, _iommu_event_show, NULL), \
100 static 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"),
120 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"),
125 { /* end: all zeroes */ },
128 /*---------------------------------------------
129 * sysfs cpumask attributes
130 *---------------------------------------------*/
131 static cpumask_t iommu_cpumask;
133 static ssize_t _iommu_cpumask_show(struct device *dev,
134 struct device_attribute *attr,
137 return cpumap_print_to_pagebuf(true, buf, &iommu_cpumask);
139 static DEVICE_ATTR(cpumask, S_IRUGO, _iommu_cpumask_show, NULL);
141 static struct attribute *iommu_cpumask_attrs[] = {
142 &dev_attr_cpumask.attr,
146 static struct attribute_group amd_iommu_cpumask_group = {
147 .attrs = iommu_cpumask_attrs,
150 /*---------------------------------------------*/
152 static int get_next_avail_iommu_bnk_cntr(struct perf_event *event)
154 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;
161 raw_spin_lock_irqsave(&piommu->lock, flags);
163 for (bank = 0, shift = 0; bank < max_banks; bank++) {
164 for (cntr = 0; cntr < max_cntrs; cntr++) {
165 shift = bank + (bank*3) + cntr;
166 if (piommu->cntr_assign_mask & BIT_ULL(shift)) {
169 piommu->cntr_assign_mask |= BIT_ULL(shift);
170 event->hw.iommu_bank = bank;
171 event->hw.iommu_cntr = cntr;
179 raw_spin_unlock_irqrestore(&piommu->lock, flags);
183 static int clear_avail_iommu_bnk_cntr(struct perf_amd_iommu *perf_iommu,
187 int max_banks, max_cntrs;
190 max_banks = perf_iommu->max_banks;
191 max_cntrs = perf_iommu->max_counters;
193 if ((bank > max_banks) || (cntr > max_cntrs))
196 shift = bank + cntr + (bank*3);
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);
205 static int perf_iommu_event_init(struct perf_event *event)
207 struct hw_perf_event *hwc = &event->hw;
209 /* test the event attr type check for PMU enumeration */
210 if (event->attr.type != event->pmu->type)
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.
218 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
224 /* update the hw_perf_event struct with the iommu config data */
225 hwc->conf = event->attr.config;
226 hwc->conf1 = event->attr.config1;
231 static inline struct amd_iommu *perf_event_2_iommu(struct perf_event *ev)
233 return (container_of(ev->pmu, struct perf_amd_iommu, pmu))->iommu;
236 static void perf_iommu_enable_event(struct perf_event *ev)
238 struct amd_iommu *iommu = perf_event_2_iommu(ev);
239 struct hw_perf_event *hwc = &ev->hw;
240 u8 bank = hwc->iommu_bank;
241 u8 cntr = hwc->iommu_cntr;
244 reg = GET_CSOURCE(hwc);
245 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_COUNTER_SRC_REG, ®);
247 reg = GET_DEVID_MASK(hwc);
248 reg = GET_DEVID(hwc) | (reg << 32);
251 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_DEVID_MATCH_REG, ®);
253 reg = GET_PASID_MASK(hwc);
254 reg = GET_PASID(hwc) | (reg << 32);
257 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_PASID_MATCH_REG, ®);
259 reg = GET_DOMID_MASK(hwc);
260 reg = GET_DOMID(hwc) | (reg << 32);
263 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_DOMID_MATCH_REG, ®);
266 static void perf_iommu_disable_event(struct perf_event *event)
268 struct amd_iommu *iommu = perf_event_2_iommu(event);
269 struct hw_perf_event *hwc = &event->hw;
272 amd_iommu_pc_set_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
273 IOMMU_PC_COUNTER_SRC_REG, ®);
276 static void perf_iommu_start(struct perf_event *event, int flags)
278 struct hw_perf_event *hwc = &event->hw;
280 if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
283 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
287 * To account for power-gating, which prevents write to
288 * the counter, we need to enable the counter
289 * before setting up counter register.
291 perf_iommu_enable_event(event);
293 if (flags & PERF_EF_RELOAD) {
295 struct amd_iommu *iommu = perf_event_2_iommu(event);
298 * Since the IOMMU PMU only support counting mode,
299 * the counter always start with value zero.
301 amd_iommu_pc_set_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
302 IOMMU_PC_COUNTER_REG, &count);
305 perf_event_update_userpage(event);
308 static void perf_iommu_read(struct perf_event *event)
311 struct hw_perf_event *hwc = &event->hw;
312 struct amd_iommu *iommu = perf_event_2_iommu(event);
314 if (amd_iommu_pc_get_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
315 IOMMU_PC_COUNTER_REG, &count))
318 /* IOMMU pc counter register is only 48 bits */
319 count &= GENMASK_ULL(47, 0);
322 * Since the counter always start with value zero,
323 * simply just accumulate the count for the event.
325 local64_add(count, &event->count);
328 static void perf_iommu_stop(struct perf_event *event, int flags)
330 struct hw_perf_event *hwc = &event->hw;
332 if (hwc->state & PERF_HES_UPTODATE)
336 * To account for power-gating, in which reading the counter would
337 * return zero, we need to read the register before disabling.
339 perf_iommu_read(event);
340 hwc->state |= PERF_HES_UPTODATE;
342 perf_iommu_disable_event(event);
343 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
344 hwc->state |= PERF_HES_STOPPED;
347 static int perf_iommu_add(struct perf_event *event, int flags)
351 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
353 /* request an iommu bank/counter */
354 retval = get_next_avail_iommu_bnk_cntr(event);
358 if (flags & PERF_EF_START)
359 perf_iommu_start(event, PERF_EF_RELOAD);
364 static void perf_iommu_del(struct perf_event *event, int flags)
366 struct hw_perf_event *hwc = &event->hw;
367 struct perf_amd_iommu *perf_iommu =
368 container_of(event->pmu, struct perf_amd_iommu, pmu);
370 perf_iommu_stop(event, PERF_EF_UPDATE);
372 /* clear the assigned iommu bank/counter */
373 clear_avail_iommu_bnk_cntr(perf_iommu,
374 hwc->iommu_bank, hwc->iommu_cntr);
376 perf_event_update_userpage(event);
379 static __init int _init_events_attrs(void)
382 struct attribute **attrs;
384 while (amd_iommu_v2_event_descs[i].attr.attr.name)
387 attrs = kcalloc(i + 1, sizeof(*attrs), GFP_KERNEL);
391 for (j = 0; j < i; j++)
392 attrs[j] = &amd_iommu_v2_event_descs[j].attr.attr;
394 amd_iommu_events_group.attrs = attrs;
398 static const struct attribute_group *amd_iommu_attr_groups[] = {
399 &amd_iommu_format_group,
400 &amd_iommu_cpumask_group,
401 &amd_iommu_events_group,
405 static const struct pmu iommu_pmu __initconst = {
406 .event_init = perf_iommu_event_init,
407 .add = perf_iommu_add,
408 .del = perf_iommu_del,
409 .start = perf_iommu_start,
410 .stop = perf_iommu_stop,
411 .read = perf_iommu_read,
412 .task_ctx_nr = perf_invalid_context,
413 .attr_groups = amd_iommu_attr_groups,
414 .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
417 static __init int init_one_iommu(unsigned int idx)
419 struct perf_amd_iommu *perf_iommu;
422 perf_iommu = kzalloc(sizeof(struct perf_amd_iommu), GFP_KERNEL);
426 raw_spin_lock_init(&perf_iommu->lock);
428 perf_iommu->pmu = iommu_pmu;
429 perf_iommu->iommu = get_amd_iommu(idx);
430 perf_iommu->max_banks = amd_iommu_pc_get_max_banks(idx);
431 perf_iommu->max_counters = amd_iommu_pc_get_max_counters(idx);
433 if (!perf_iommu->iommu ||
434 !perf_iommu->max_banks ||
435 !perf_iommu->max_counters) {
440 snprintf(perf_iommu->name, IOMMU_NAME_SIZE, "amd_iommu_%u", idx);
442 ret = perf_pmu_register(&perf_iommu->pmu, perf_iommu->name, -1);
444 pr_info("Detected AMD IOMMU #%d (%d banks, %d counters/bank).\n",
445 idx, perf_iommu->max_banks, perf_iommu->max_counters);
446 list_add_tail(&perf_iommu->list, &perf_amd_iommu_list);
448 pr_warn("Error initializing IOMMU %d.\n", idx);
454 static __init int amd_iommu_pc_init(void)
456 unsigned int i, cnt = 0;
459 /* Make sure the IOMMU PC resource is available */
460 if (!amd_iommu_pc_supported())
463 ret = _init_events_attrs();
468 * An IOMMU PMU is specific to an IOMMU, and can function independently.
469 * So we go through all IOMMUs and ignore the one that fails init
470 * unless all IOMMU are failing.
472 for (i = 0; i < amd_iommu_get_num_iommus(); i++) {
473 ret = init_one_iommu(i);
479 kfree(amd_iommu_events_group.attrs);
483 /* Init cpumask attributes to only core 0 */
484 cpumask_set_cpu(0, &iommu_cpumask);
488 device_initcall(amd_iommu_pc_init);