blob: 3641e24fdac5801a32b2d23b89c1ce7775807e2f [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
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -050037#define IOMMU_NAME_SIZE 16
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050038
39struct perf_amd_iommu {
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -050040 struct list_head list;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050041 struct pmu pmu;
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -050042 struct amd_iommu *iommu;
43 char name[IOMMU_NAME_SIZE];
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050044 u8 max_banks;
45 u8 max_counters;
46 u64 cntr_assign_mask;
47 raw_spinlock_t lock;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050048};
49
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -050050static LIST_HEAD(perf_amd_iommu_list);
51
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050052/*---------------------------------------------
53 * sysfs format attributes
54 *---------------------------------------------*/
55PMU_FORMAT_ATTR(csource, "config:0-7");
56PMU_FORMAT_ATTR(devid, "config:8-23");
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -060057PMU_FORMAT_ATTR(domid, "config:24-39");
58PMU_FORMAT_ATTR(pasid, "config:40-59");
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050059PMU_FORMAT_ATTR(devid_mask, "config1:0-15");
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -060060PMU_FORMAT_ATTR(domid_mask, "config1:16-31");
61PMU_FORMAT_ATTR(pasid_mask, "config1:32-51");
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050062
63static struct attribute *iommu_format_attrs[] = {
64 &format_attr_csource.attr,
65 &format_attr_devid.attr,
66 &format_attr_pasid.attr,
67 &format_attr_domid.attr,
68 &format_attr_devid_mask.attr,
69 &format_attr_pasid_mask.attr,
70 &format_attr_domid_mask.attr,
71 NULL,
72};
73
74static struct attribute_group amd_iommu_format_group = {
75 .name = "format",
76 .attrs = iommu_format_attrs,
77};
78
79/*---------------------------------------------
80 * sysfs events attributes
81 *---------------------------------------------*/
Suravee Suthikulpanit51686542017-02-24 02:48:20 -060082static struct attribute_group amd_iommu_events_group = {
83 .name = "events",
84};
85
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -050086struct amd_iommu_event_desc {
87 struct kobj_attribute attr;
88 const char *event;
89};
90
91static ssize_t _iommu_event_show(struct kobject *kobj,
92 struct kobj_attribute *attr, char *buf)
93{
94 struct amd_iommu_event_desc *event =
95 container_of(attr, struct amd_iommu_event_desc, attr);
96 return sprintf(buf, "%s\n", event->event);
97}
98
99#define AMD_IOMMU_EVENT_DESC(_name, _event) \
100{ \
101 .attr = __ATTR(_name, 0444, _iommu_event_show, NULL), \
102 .event = _event, \
103}
104
105static struct amd_iommu_event_desc amd_iommu_v2_event_descs[] = {
106 AMD_IOMMU_EVENT_DESC(mem_pass_untrans, "csource=0x01"),
107 AMD_IOMMU_EVENT_DESC(mem_pass_pretrans, "csource=0x02"),
108 AMD_IOMMU_EVENT_DESC(mem_pass_excl, "csource=0x03"),
109 AMD_IOMMU_EVENT_DESC(mem_target_abort, "csource=0x04"),
110 AMD_IOMMU_EVENT_DESC(mem_trans_total, "csource=0x05"),
111 AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pte_hit, "csource=0x06"),
112 AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pte_mis, "csource=0x07"),
113 AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pde_hit, "csource=0x08"),
114 AMD_IOMMU_EVENT_DESC(mem_iommu_tlb_pde_mis, "csource=0x09"),
115 AMD_IOMMU_EVENT_DESC(mem_dte_hit, "csource=0x0a"),
116 AMD_IOMMU_EVENT_DESC(mem_dte_mis, "csource=0x0b"),
117 AMD_IOMMU_EVENT_DESC(page_tbl_read_tot, "csource=0x0c"),
118 AMD_IOMMU_EVENT_DESC(page_tbl_read_nst, "csource=0x0d"),
119 AMD_IOMMU_EVENT_DESC(page_tbl_read_gst, "csource=0x0e"),
120 AMD_IOMMU_EVENT_DESC(int_dte_hit, "csource=0x0f"),
121 AMD_IOMMU_EVENT_DESC(int_dte_mis, "csource=0x10"),
122 AMD_IOMMU_EVENT_DESC(cmd_processed, "csource=0x11"),
123 AMD_IOMMU_EVENT_DESC(cmd_processed_inv, "csource=0x12"),
124 AMD_IOMMU_EVENT_DESC(tlb_inv, "csource=0x13"),
Suravee Suthikulpanitf8519152016-02-28 22:23:29 -0600125 AMD_IOMMU_EVENT_DESC(ign_rd_wr_mmio_1ff8h, "csource=0x14"),
126 AMD_IOMMU_EVENT_DESC(vapic_int_non_guest, "csource=0x15"),
127 AMD_IOMMU_EVENT_DESC(vapic_int_guest, "csource=0x16"),
128 AMD_IOMMU_EVENT_DESC(smi_recv, "csource=0x17"),
129 AMD_IOMMU_EVENT_DESC(smi_blk, "csource=0x18"),
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500130 { /* end: all zeroes */ },
131};
132
133/*---------------------------------------------
134 * sysfs cpumask attributes
135 *---------------------------------------------*/
136static cpumask_t iommu_cpumask;
137
138static ssize_t _iommu_cpumask_show(struct device *dev,
139 struct device_attribute *attr,
140 char *buf)
141{
Sudeep Holla5aaba362014-09-30 14:48:22 +0100142 return cpumap_print_to_pagebuf(true, buf, &iommu_cpumask);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500143}
144static DEVICE_ATTR(cpumask, S_IRUGO, _iommu_cpumask_show, NULL);
145
146static struct attribute *iommu_cpumask_attrs[] = {
147 &dev_attr_cpumask.attr,
148 NULL,
149};
150
151static struct attribute_group amd_iommu_cpumask_group = {
152 .attrs = iommu_cpumask_attrs,
153};
154
155/*---------------------------------------------*/
156
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600157static int get_next_avail_iommu_bnk_cntr(struct perf_event *event)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500158{
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600159 struct perf_amd_iommu *piommu = container_of(event->pmu, struct perf_amd_iommu, pmu);
160 int max_cntrs = piommu->max_counters;
161 int max_banks = piommu->max_banks;
162 u32 shift, bank, cntr;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500163 unsigned long flags;
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600164 int retval;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500165
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600166 raw_spin_lock_irqsave(&piommu->lock, flags);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500167
168 for (bank = 0, shift = 0; bank < max_banks; bank++) {
169 for (cntr = 0; cntr < max_cntrs; cntr++) {
170 shift = bank + (bank*3) + cntr;
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600171 if (piommu->cntr_assign_mask & BIT_ULL(shift)) {
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500172 continue;
173 } else {
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600174 piommu->cntr_assign_mask |= BIT_ULL(shift);
175 event->hw.iommu_bank = bank;
176 event->hw.iommu_cntr = cntr;
177 retval = 0;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500178 goto out;
179 }
180 }
181 }
182 retval = -ENOSPC;
183out:
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600184 raw_spin_unlock_irqrestore(&piommu->lock, flags);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500185 return retval;
186}
187
188static int clear_avail_iommu_bnk_cntr(struct perf_amd_iommu *perf_iommu,
189 u8 bank, u8 cntr)
190{
191 unsigned long flags;
192 int max_banks, max_cntrs;
193 int shift = 0;
194
195 max_banks = perf_iommu->max_banks;
196 max_cntrs = perf_iommu->max_counters;
197
198 if ((bank > max_banks) || (cntr > max_cntrs))
199 return -EINVAL;
200
201 shift = bank + cntr + (bank*3);
202
203 raw_spin_lock_irqsave(&perf_iommu->lock, flags);
204 perf_iommu->cntr_assign_mask &= ~(1ULL<<shift);
205 raw_spin_unlock_irqrestore(&perf_iommu->lock, flags);
206
207 return 0;
208}
209
210static int perf_iommu_event_init(struct perf_event *event)
211{
212 struct hw_perf_event *hwc = &event->hw;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500213
214 /* test the event attr type check for PMU enumeration */
215 if (event->attr.type != event->pmu->type)
216 return -ENOENT;
217
218 /*
219 * IOMMU counters are shared across all cores.
220 * Therefore, it does not support per-process mode.
221 * Also, it does not support event sampling mode.
222 */
223 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
224 return -EINVAL;
225
226 /* IOMMU counters do not have usr/os/guest/host bits */
227 if (event->attr.exclude_user || event->attr.exclude_kernel ||
228 event->attr.exclude_host || event->attr.exclude_guest)
229 return -EINVAL;
230
231 if (event->cpu < 0)
232 return -EINVAL;
233
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500234 /* update the hw_perf_event struct with the iommu config data */
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600235 hwc->conf = event->attr.config;
236 hwc->conf1 = event->attr.config1;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500237
238 return 0;
239}
240
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500241static inline struct amd_iommu *perf_event_2_iommu(struct perf_event *ev)
242{
243 return (container_of(ev->pmu, struct perf_amd_iommu, pmu))->iommu;
244}
245
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500246static void perf_iommu_enable_event(struct perf_event *ev)
247{
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500248 struct amd_iommu *iommu = perf_event_2_iommu(ev);
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600249 struct hw_perf_event *hwc = &ev->hw;
250 u8 bank = hwc->iommu_bank;
251 u8 cntr = hwc->iommu_cntr;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500252 u64 reg = 0ULL;
253
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600254 reg = GET_CSOURCE(hwc);
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600255 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_COUNTER_SRC_REG, &reg);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500256
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600257 reg = GET_DEVID_MASK(hwc);
258 reg = GET_DEVID(hwc) | (reg << 32);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500259 if (reg)
Suravee Suthikulpanit6aad0c62017-02-24 02:48:14 -0600260 reg |= BIT(31);
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600261 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_DEVID_MATCH_REG, &reg);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500262
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600263 reg = GET_PASID_MASK(hwc);
264 reg = GET_PASID(hwc) | (reg << 32);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500265 if (reg)
Suravee Suthikulpanit6aad0c62017-02-24 02:48:14 -0600266 reg |= BIT(31);
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600267 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_PASID_MATCH_REG, &reg);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500268
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600269 reg = GET_DOMID_MASK(hwc);
270 reg = GET_DOMID(hwc) | (reg << 32);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500271 if (reg)
Suravee Suthikulpanit6aad0c62017-02-24 02:48:14 -0600272 reg |= BIT(31);
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600273 amd_iommu_pc_set_reg(iommu, bank, cntr, IOMMU_PC_DOMID_MATCH_REG, &reg);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500274}
275
276static void perf_iommu_disable_event(struct perf_event *event)
277{
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500278 struct amd_iommu *iommu = perf_event_2_iommu(event);
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600279 struct hw_perf_event *hwc = &event->hw;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500280 u64 reg = 0ULL;
281
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600282 amd_iommu_pc_set_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600283 IOMMU_PC_COUNTER_SRC_REG, &reg);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500284}
285
286static void perf_iommu_start(struct perf_event *event, int flags)
287{
288 struct hw_perf_event *hwc = &event->hw;
289
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500290 if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
291 return;
292
293 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
294 hwc->state = 0;
295
296 if (flags & PERF_EF_RELOAD) {
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600297 u64 prev_raw_count = local64_read(&hwc->prev_count);
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500298 struct amd_iommu *iommu = perf_event_2_iommu(event);
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600299
300 amd_iommu_pc_set_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600301 IOMMU_PC_COUNTER_REG, &prev_raw_count);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500302 }
303
304 perf_iommu_enable_event(event);
305 perf_event_update_userpage(event);
306
307}
308
309static void perf_iommu_read(struct perf_event *event)
310{
Suravee Suthikulpanitdc6ca5e2017-02-24 02:48:15 -0600311 u64 count, prev, delta;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500312 struct hw_perf_event *hwc = &event->hw;
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500313 struct amd_iommu *iommu = perf_event_2_iommu(event);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500314
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600315 if (amd_iommu_pc_get_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -0600316 IOMMU_PC_COUNTER_REG, &count))
317 return;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500318
319 /* IOMMU pc counter register is only 48 bits */
Suravee Suthikulpanitdc6ca5e2017-02-24 02:48:15 -0600320 count &= GENMASK_ULL(47, 0);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500321
Suravee Suthikulpanitdc6ca5e2017-02-24 02:48:15 -0600322 prev = local64_read(&hwc->prev_count);
323 if (local64_cmpxchg(&hwc->prev_count, prev, count) != prev)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500324 return;
325
Suravee Suthikulpanitdc6ca5e2017-02-24 02:48:15 -0600326 /* Handle 48-bit counter overflow */
327 delta = (count << COUNTER_SHIFT) - (prev << COUNTER_SHIFT);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500328 delta >>= COUNTER_SHIFT;
329 local64_add(delta, &event->count);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500330}
331
332static void perf_iommu_stop(struct perf_event *event, int flags)
333{
334 struct hw_perf_event *hwc = &event->hw;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500335
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500336 if (hwc->state & PERF_HES_UPTODATE)
337 return;
338
339 perf_iommu_disable_event(event);
340 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
341 hwc->state |= PERF_HES_STOPPED;
342
343 if (hwc->state & PERF_HES_UPTODATE)
344 return;
345
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500346 perf_iommu_read(event);
347 hwc->state |= PERF_HES_UPTODATE;
348}
349
350static int perf_iommu_add(struct perf_event *event, int flags)
351{
352 int retval;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500353
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500354 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
355
356 /* request an iommu bank/counter */
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600357 retval = get_next_avail_iommu_bnk_cntr(event);
358 if (retval)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500359 return retval;
360
361 if (flags & PERF_EF_START)
362 perf_iommu_start(event, PERF_EF_RELOAD);
363
364 return 0;
365}
366
367static void perf_iommu_del(struct perf_event *event, int flags)
368{
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600369 struct hw_perf_event *hwc = &event->hw;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500370 struct perf_amd_iommu *perf_iommu =
371 container_of(event->pmu, struct perf_amd_iommu, pmu);
372
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500373 perf_iommu_stop(event, PERF_EF_UPDATE);
374
375 /* clear the assigned iommu bank/counter */
376 clear_avail_iommu_bnk_cntr(perf_iommu,
Suravee Suthikulpanitcf25f902017-02-24 02:48:21 -0600377 hwc->iommu_bank, hwc->iommu_cntr);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500378
379 perf_event_update_userpage(event);
380}
381
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600382static __init int _init_events_attrs(void)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500383{
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500384 int i = 0, j;
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600385 struct attribute **attrs;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500386
387 while (amd_iommu_v2_event_descs[i].attr.attr.name)
388 i++;
389
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600390 attrs = kzalloc(sizeof(struct attribute **) * (i + 1), GFP_KERNEL);
391 if (!attrs)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500392 return -ENOMEM;
393
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500394 for (j = 0; j < i; j++)
395 attrs[j] = &amd_iommu_v2_event_descs[j].attr.attr;
396
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600397 amd_iommu_events_group.attrs = attrs;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500398 return 0;
399}
400
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600401const struct attribute_group *amd_iommu_attr_groups[] = {
402 &amd_iommu_format_group,
403 &amd_iommu_cpumask_group,
404 &amd_iommu_events_group,
405 NULL,
406};
407
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500408static struct pmu iommu_pmu = {
409 .event_init = perf_iommu_event_init,
410 .add = perf_iommu_add,
411 .del = perf_iommu_del,
412 .start = perf_iommu_start,
413 .stop = perf_iommu_stop,
414 .read = perf_iommu_read,
415 .task_ctx_nr = perf_invalid_context,
416 .attr_groups = amd_iommu_attr_groups,
417};
418
419static __init int init_one_iommu(unsigned int idx)
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500420{
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500421 struct perf_amd_iommu *perf_iommu;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500422 int ret;
423
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500424 perf_iommu = kzalloc(sizeof(struct perf_amd_iommu), GFP_KERNEL);
425 if (!perf_iommu)
426 return -ENOMEM;
427
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500428 raw_spin_lock_init(&perf_iommu->lock);
429
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500430 perf_iommu->pmu = iommu_pmu;
431 perf_iommu->iommu = get_amd_iommu(idx);
432 perf_iommu->max_banks = amd_iommu_pc_get_max_banks(idx);
433 perf_iommu->max_counters = amd_iommu_pc_get_max_counters(idx);
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500434
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500435 if (!perf_iommu->iommu ||
436 !perf_iommu->max_banks ||
437 !perf_iommu->max_counters) {
438 kfree(perf_iommu);
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -0600439 return -EINVAL;
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500440 }
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -0600441
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500442 snprintf(perf_iommu->name, IOMMU_NAME_SIZE, "amd_iommu_%u", idx);
443
444 ret = perf_pmu_register(&perf_iommu->pmu, perf_iommu->name, -1);
445 if (!ret) {
446 pr_info("Detected AMD IOMMU #%d (%d banks, %d counters/bank).\n",
447 idx, perf_iommu->max_banks, perf_iommu->max_counters);
448 list_add_tail(&perf_iommu->list, &perf_amd_iommu_list);
449 } else {
450 pr_warn("Error initializing IOMMU %d.\n", idx);
451 kfree(perf_iommu);
452 }
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500453 return ret;
454}
455
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500456static __init int amd_iommu_pc_init(void)
457{
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500458 unsigned int i, cnt = 0;
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600459 int ret;
460
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500461 /* Make sure the IOMMU PC resource is available */
Peter Zijlstra100ac532013-07-03 09:55:42 +0200462 if (!amd_iommu_pc_supported())
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500463 return -ENODEV;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500464
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600465 ret = _init_events_attrs();
466 if (ret)
467 return ret;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500468
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500469 /*
470 * An IOMMU PMU is specific to an IOMMU, and can function independently.
471 * So we go through all IOMMUs and ignore the one that fails init
472 * unless all IOMMU are failing.
473 */
474 for (i = 0; i < amd_iommu_get_num_iommus(); i++) {
475 ret = init_one_iommu(i);
476 if (!ret)
477 cnt++;
478 }
Suravee Suthikulpanit51686542017-02-24 02:48:20 -0600479
Suravee Suthikulpanit25df39f2017-03-22 02:02:42 -0500480 if (!cnt) {
481 kfree(amd_iommu_events_group.attrs);
482 return -ENODEV;
483 }
484
485 /* Init cpumask attributes to only core 0 */
486 cpumask_set_cpu(0, &iommu_cpumask);
487 return 0;
Suravee Suthikulpanit7be62962013-06-05 16:11:49 -0500488}
489
490device_initcall(amd_iommu_pc_init);