1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This driver adds support for PCIe PMU RCiEP device. Related
4  * perf events are bandwidth, latency etc.
5  *
6  * Copyright (C) 2021 HiSilicon Limited
7  * Author: Qi Liu <liuqi115@huawei.com>
8  */
9 #include <linux/bitfield.h>
10 #include <linux/bitmap.h>
11 #include <linux/bug.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/pci.h>
20 #include <linux/perf_event.h>
21 
22 #define DRV_NAME "hisi_pcie_pmu"
23 /* Define registers */
24 #define HISI_PCIE_GLOBAL_CTRL		0x00
25 #define HISI_PCIE_EVENT_CTRL		0x010
26 #define HISI_PCIE_CNT			0x090
27 #define HISI_PCIE_EXT_CNT		0x110
28 #define HISI_PCIE_INT_STAT		0x150
29 #define HISI_PCIE_INT_MASK		0x154
30 #define HISI_PCIE_REG_BDF		0xfe0
31 #define HISI_PCIE_REG_VERSION		0xfe4
32 #define HISI_PCIE_REG_INFO		0xfe8
33 
34 /* Define command in HISI_PCIE_GLOBAL_CTRL */
35 #define HISI_PCIE_GLOBAL_EN		0x01
36 #define HISI_PCIE_GLOBAL_NONE		0
37 
38 /* Define command in HISI_PCIE_EVENT_CTRL */
39 #define HISI_PCIE_EVENT_EN		BIT_ULL(20)
40 #define HISI_PCIE_RESET_CNT		BIT_ULL(22)
41 #define HISI_PCIE_INIT_SET		BIT_ULL(34)
42 #define HISI_PCIE_THR_EN		BIT_ULL(26)
43 #define HISI_PCIE_TARGET_EN		BIT_ULL(32)
44 #define HISI_PCIE_TRIG_EN		BIT_ULL(52)
45 
46 /* Define offsets in HISI_PCIE_EVENT_CTRL */
47 #define HISI_PCIE_EVENT_M		GENMASK_ULL(15, 0)
48 #define HISI_PCIE_THR_MODE_M		GENMASK_ULL(27, 27)
49 #define HISI_PCIE_THR_M			GENMASK_ULL(31, 28)
50 #define HISI_PCIE_LEN_M			GENMASK_ULL(35, 34)
51 #define HISI_PCIE_TARGET_M		GENMASK_ULL(52, 36)
52 #define HISI_PCIE_TRIG_MODE_M		GENMASK_ULL(53, 53)
53 #define HISI_PCIE_TRIG_M		GENMASK_ULL(59, 56)
54 
55 /* Default config of TLP length mode, will count both TLP headers and payloads */
56 #define HISI_PCIE_LEN_M_DEFAULT		3ULL
57 
58 #define HISI_PCIE_MAX_COUNTERS		8
59 #define HISI_PCIE_REG_STEP		8
60 #define HISI_PCIE_THR_MAX_VAL		10
61 #define HISI_PCIE_TRIG_MAX_VAL		10
62 #define HISI_PCIE_MAX_PERIOD		(GENMASK_ULL(63, 0))
63 #define HISI_PCIE_INIT_VAL		BIT_ULL(63)
64 
65 struct hisi_pcie_pmu {
66 	struct perf_event *hw_events[HISI_PCIE_MAX_COUNTERS];
67 	struct hlist_node node;
68 	struct pci_dev *pdev;
69 	struct pmu pmu;
70 	void __iomem *base;
71 	int irq;
72 	u32 identifier;
73 	/* Minimum and maximum BDF of root ports monitored by PMU */
74 	u16 bdf_min;
75 	u16 bdf_max;
76 	int on_cpu;
77 };
78 
79 struct hisi_pcie_reg_pair {
80 	u16 lo;
81 	u16 hi;
82 };
83 
84 #define to_pcie_pmu(p)  (container_of((p), struct hisi_pcie_pmu, pmu))
85 #define GET_PCI_DEVFN(bdf)  ((bdf) & 0xff)
86 
87 #define HISI_PCIE_PMU_FILTER_ATTR(_name, _config, _hi, _lo)		  \
88 	static u64 hisi_pcie_get_##_name(struct perf_event *event)	  \
89 	{								  \
90 		return FIELD_GET(GENMASK(_hi, _lo), event->attr._config); \
91 	}								  \
92 
93 HISI_PCIE_PMU_FILTER_ATTR(event, config, 16, 0);
94 HISI_PCIE_PMU_FILTER_ATTR(thr_len, config1, 3, 0);
95 HISI_PCIE_PMU_FILTER_ATTR(thr_mode, config1, 4, 4);
96 HISI_PCIE_PMU_FILTER_ATTR(trig_len, config1, 8, 5);
97 HISI_PCIE_PMU_FILTER_ATTR(trig_mode, config1, 9, 9);
98 HISI_PCIE_PMU_FILTER_ATTR(len_mode, config1, 11, 10);
99 HISI_PCIE_PMU_FILTER_ATTR(port, config2, 15, 0);
100 HISI_PCIE_PMU_FILTER_ATTR(bdf, config2, 31, 16);
101 
hisi_pcie_format_sysfs_show(struct device * dev,struct device_attribute * attr,char * buf)102 static ssize_t hisi_pcie_format_sysfs_show(struct device *dev, struct device_attribute *attr,
103 					   char *buf)
104 {
105 	struct dev_ext_attribute *eattr;
106 
107 	eattr = container_of(attr, struct dev_ext_attribute, attr);
108 
109 	return sysfs_emit(buf, "%s\n", (char *)eattr->var);
110 }
111 
hisi_pcie_event_sysfs_show(struct device * dev,struct device_attribute * attr,char * buf)112 static ssize_t hisi_pcie_event_sysfs_show(struct device *dev, struct device_attribute *attr,
113 					  char *buf)
114 {
115 	struct perf_pmu_events_attr *pmu_attr =
116 		container_of(attr, struct perf_pmu_events_attr, attr);
117 
118 	return sysfs_emit(buf, "config=0x%llx\n", pmu_attr->id);
119 }
120 
121 #define HISI_PCIE_PMU_FORMAT_ATTR(_name, _format)                              \
122 	(&((struct dev_ext_attribute[]){                                       \
123 		{ .attr = __ATTR(_name, 0444, hisi_pcie_format_sysfs_show,     \
124 				 NULL),                                        \
125 		  .var = (void *)_format }                                     \
126 	})[0].attr.attr)
127 
128 #define HISI_PCIE_PMU_EVENT_ATTR(_name, _id)			\
129 	PMU_EVENT_ATTR_ID(_name, hisi_pcie_event_sysfs_show, _id)
130 
cpumask_show(struct device * dev,struct device_attribute * attr,char * buf)131 static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, char *buf)
132 {
133 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(dev_get_drvdata(dev));
134 
135 	return cpumap_print_to_pagebuf(true, buf, cpumask_of(pcie_pmu->on_cpu));
136 }
137 static DEVICE_ATTR_RO(cpumask);
138 
identifier_show(struct device * dev,struct device_attribute * attr,char * buf)139 static ssize_t identifier_show(struct device *dev, struct device_attribute *attr, char *buf)
140 {
141 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(dev_get_drvdata(dev));
142 
143 	return sysfs_emit(buf, "%#x\n", pcie_pmu->identifier);
144 }
145 static DEVICE_ATTR_RO(identifier);
146 
bus_show(struct device * dev,struct device_attribute * attr,char * buf)147 static ssize_t bus_show(struct device *dev, struct device_attribute *attr, char *buf)
148 {
149 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(dev_get_drvdata(dev));
150 
151 	return sysfs_emit(buf, "%#04x\n", PCI_BUS_NUM(pcie_pmu->bdf_min));
152 }
153 static DEVICE_ATTR_RO(bus);
154 
155 static struct hisi_pcie_reg_pair
hisi_pcie_parse_reg_value(struct hisi_pcie_pmu * pcie_pmu,u32 reg_off)156 hisi_pcie_parse_reg_value(struct hisi_pcie_pmu *pcie_pmu, u32 reg_off)
157 {
158 	u32 val = readl_relaxed(pcie_pmu->base + reg_off);
159 	struct hisi_pcie_reg_pair regs = {
160 		.lo = val,
161 		.hi = val >> 16,
162 	};
163 
164 	return regs;
165 }
166 
167 /*
168  * Hardware counter and ext_counter work together for bandwidth, latency, bus
169  * utilization and buffer occupancy events. For example, RX memory write latency
170  * events(index = 0x0010), counter counts total delay cycles and ext_counter
171  * counts RX memory write PCIe packets number.
172  *
173  * As we don't want PMU driver to process these two data, "delay cycles" can
174  * be treated as an independent event(index = 0x0010), "RX memory write packets
175  * number" as another(index = 0x10010). BIT 16 is used to distinguish and 0-15
176  * bits are "real" event index, which can be used to set HISI_PCIE_EVENT_CTRL.
177  */
178 #define EXT_COUNTER_IS_USED(idx)		((idx) & BIT(16))
179 
hisi_pcie_get_real_event(struct perf_event * event)180 static u32 hisi_pcie_get_real_event(struct perf_event *event)
181 {
182 	return hisi_pcie_get_event(event) & GENMASK(15, 0);
183 }
184 
hisi_pcie_pmu_get_offset(u32 offset,u32 idx)185 static u32 hisi_pcie_pmu_get_offset(u32 offset, u32 idx)
186 {
187 	return offset + HISI_PCIE_REG_STEP * idx;
188 }
189 
hisi_pcie_pmu_readl(struct hisi_pcie_pmu * pcie_pmu,u32 reg_offset,u32 idx)190 static u32 hisi_pcie_pmu_readl(struct hisi_pcie_pmu *pcie_pmu, u32 reg_offset,
191 			       u32 idx)
192 {
193 	u32 offset = hisi_pcie_pmu_get_offset(reg_offset, idx);
194 
195 	return readl_relaxed(pcie_pmu->base + offset);
196 }
197 
hisi_pcie_pmu_writel(struct hisi_pcie_pmu * pcie_pmu,u32 reg_offset,u32 idx,u32 val)198 static void hisi_pcie_pmu_writel(struct hisi_pcie_pmu *pcie_pmu, u32 reg_offset, u32 idx, u32 val)
199 {
200 	u32 offset = hisi_pcie_pmu_get_offset(reg_offset, idx);
201 
202 	writel_relaxed(val, pcie_pmu->base + offset);
203 }
204 
hisi_pcie_pmu_readq(struct hisi_pcie_pmu * pcie_pmu,u32 reg_offset,u32 idx)205 static u64 hisi_pcie_pmu_readq(struct hisi_pcie_pmu *pcie_pmu, u32 reg_offset, u32 idx)
206 {
207 	u32 offset = hisi_pcie_pmu_get_offset(reg_offset, idx);
208 
209 	return readq_relaxed(pcie_pmu->base + offset);
210 }
211 
hisi_pcie_pmu_writeq(struct hisi_pcie_pmu * pcie_pmu,u32 reg_offset,u32 idx,u64 val)212 static void hisi_pcie_pmu_writeq(struct hisi_pcie_pmu *pcie_pmu, u32 reg_offset, u32 idx, u64 val)
213 {
214 	u32 offset = hisi_pcie_pmu_get_offset(reg_offset, idx);
215 
216 	writeq_relaxed(val, pcie_pmu->base + offset);
217 }
218 
hisi_pcie_pmu_config_filter(struct perf_event * event)219 static void hisi_pcie_pmu_config_filter(struct perf_event *event)
220 {
221 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
222 	struct hw_perf_event *hwc = &event->hw;
223 	u64 port, trig_len, thr_len, len_mode;
224 	u64 reg = HISI_PCIE_INIT_SET;
225 
226 	/* Config HISI_PCIE_EVENT_CTRL according to event. */
227 	reg |= FIELD_PREP(HISI_PCIE_EVENT_M, hisi_pcie_get_real_event(event));
228 
229 	/* Config HISI_PCIE_EVENT_CTRL according to root port or EP device. */
230 	port = hisi_pcie_get_port(event);
231 	if (port)
232 		reg |= FIELD_PREP(HISI_PCIE_TARGET_M, port);
233 	else
234 		reg |= HISI_PCIE_TARGET_EN |
235 		       FIELD_PREP(HISI_PCIE_TARGET_M, hisi_pcie_get_bdf(event));
236 
237 	/* Config HISI_PCIE_EVENT_CTRL according to trigger condition. */
238 	trig_len = hisi_pcie_get_trig_len(event);
239 	if (trig_len) {
240 		reg |= FIELD_PREP(HISI_PCIE_TRIG_M, trig_len);
241 		reg |= FIELD_PREP(HISI_PCIE_TRIG_MODE_M, hisi_pcie_get_trig_mode(event));
242 		reg |= HISI_PCIE_TRIG_EN;
243 	}
244 
245 	/* Config HISI_PCIE_EVENT_CTRL according to threshold condition. */
246 	thr_len = hisi_pcie_get_thr_len(event);
247 	if (thr_len) {
248 		reg |= FIELD_PREP(HISI_PCIE_THR_M, thr_len);
249 		reg |= FIELD_PREP(HISI_PCIE_THR_MODE_M, hisi_pcie_get_thr_mode(event));
250 		reg |= HISI_PCIE_THR_EN;
251 	}
252 
253 	len_mode = hisi_pcie_get_len_mode(event);
254 	if (len_mode)
255 		reg |= FIELD_PREP(HISI_PCIE_LEN_M, len_mode);
256 	else
257 		reg |= FIELD_PREP(HISI_PCIE_LEN_M, HISI_PCIE_LEN_M_DEFAULT);
258 
259 	hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, hwc->idx, reg);
260 }
261 
hisi_pcie_pmu_clear_filter(struct perf_event * event)262 static void hisi_pcie_pmu_clear_filter(struct perf_event *event)
263 {
264 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
265 	struct hw_perf_event *hwc = &event->hw;
266 
267 	hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, hwc->idx, HISI_PCIE_INIT_SET);
268 }
269 
hisi_pcie_pmu_valid_requester_id(struct hisi_pcie_pmu * pcie_pmu,u32 bdf)270 static bool hisi_pcie_pmu_valid_requester_id(struct hisi_pcie_pmu *pcie_pmu, u32 bdf)
271 {
272 	struct pci_dev *root_port, *pdev;
273 	u16 rp_bdf;
274 
275 	pdev = pci_get_domain_bus_and_slot(pci_domain_nr(pcie_pmu->pdev->bus), PCI_BUS_NUM(bdf),
276 					   GET_PCI_DEVFN(bdf));
277 	if (!pdev)
278 		return false;
279 
280 	root_port = pcie_find_root_port(pdev);
281 	if (!root_port) {
282 		pci_dev_put(pdev);
283 		return false;
284 	}
285 
286 	pci_dev_put(pdev);
287 	rp_bdf = pci_dev_id(root_port);
288 	return rp_bdf >= pcie_pmu->bdf_min && rp_bdf <= pcie_pmu->bdf_max;
289 }
290 
hisi_pcie_pmu_valid_filter(struct perf_event * event,struct hisi_pcie_pmu * pcie_pmu)291 static bool hisi_pcie_pmu_valid_filter(struct perf_event *event,
292 				       struct hisi_pcie_pmu *pcie_pmu)
293 {
294 	u32 requester_id = hisi_pcie_get_bdf(event);
295 
296 	if (hisi_pcie_get_thr_len(event) > HISI_PCIE_THR_MAX_VAL)
297 		return false;
298 
299 	if (hisi_pcie_get_trig_len(event) > HISI_PCIE_TRIG_MAX_VAL)
300 		return false;
301 
302 	if (requester_id) {
303 		if (!hisi_pcie_pmu_valid_requester_id(pcie_pmu, requester_id))
304 			return false;
305 	}
306 
307 	return true;
308 }
309 
hisi_pcie_pmu_cmp_event(struct perf_event * target,struct perf_event * event)310 static bool hisi_pcie_pmu_cmp_event(struct perf_event *target,
311 					struct perf_event *event)
312 {
313 	return hisi_pcie_get_real_event(target) == hisi_pcie_get_real_event(event);
314 }
315 
hisi_pcie_pmu_validate_event_group(struct perf_event * event)316 static bool hisi_pcie_pmu_validate_event_group(struct perf_event *event)
317 {
318 	struct perf_event *sibling, *leader = event->group_leader;
319 	struct perf_event *event_group[HISI_PCIE_MAX_COUNTERS];
320 	int counters = 1;
321 	int num;
322 
323 	event_group[0] = leader;
324 	if (!is_software_event(leader)) {
325 		if (leader->pmu != event->pmu)
326 			return false;
327 
328 		if (leader != event && !hisi_pcie_pmu_cmp_event(leader, event))
329 			event_group[counters++] = event;
330 	}
331 
332 	for_each_sibling_event(sibling, event->group_leader) {
333 		if (is_software_event(sibling))
334 			continue;
335 
336 		if (sibling->pmu != event->pmu)
337 			return false;
338 
339 		for (num = 0; num < counters; num++) {
340 			/*
341 			 * If we find a related event, then it's a valid group
342 			 * since we don't need to allocate a new counter for it.
343 			 */
344 			if (hisi_pcie_pmu_cmp_event(event_group[num], sibling))
345 				break;
346 		}
347 
348 		/*
349 		 * Otherwise it's a new event but if there's no available counter,
350 		 * fail the check since we cannot schedule all the events in
351 		 * the group simultaneously.
352 		 */
353 		if (num == HISI_PCIE_MAX_COUNTERS)
354 			return false;
355 
356 		if (num == counters)
357 			event_group[counters++] = sibling;
358 	}
359 
360 	return true;
361 }
362 
hisi_pcie_pmu_event_init(struct perf_event * event)363 static int hisi_pcie_pmu_event_init(struct perf_event *event)
364 {
365 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
366 	struct hw_perf_event *hwc = &event->hw;
367 
368 	/* Check the type first before going on, otherwise it's not our event */
369 	if (event->attr.type != event->pmu->type)
370 		return -ENOENT;
371 
372 	event->cpu = pcie_pmu->on_cpu;
373 
374 	if (EXT_COUNTER_IS_USED(hisi_pcie_get_event(event)))
375 		hwc->event_base = HISI_PCIE_EXT_CNT;
376 	else
377 		hwc->event_base = HISI_PCIE_CNT;
378 
379 	/* Sampling is not supported. */
380 	if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
381 		return -EOPNOTSUPP;
382 
383 	if (!hisi_pcie_pmu_valid_filter(event, pcie_pmu))
384 		return -EINVAL;
385 
386 	if (!hisi_pcie_pmu_validate_event_group(event))
387 		return -EINVAL;
388 
389 	return 0;
390 }
391 
hisi_pcie_pmu_read_counter(struct perf_event * event)392 static u64 hisi_pcie_pmu_read_counter(struct perf_event *event)
393 {
394 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
395 	u32 idx = event->hw.idx;
396 
397 	return hisi_pcie_pmu_readq(pcie_pmu, event->hw.event_base, idx);
398 }
399 
hisi_pcie_pmu_find_related_event(struct hisi_pcie_pmu * pcie_pmu,struct perf_event * event)400 static int hisi_pcie_pmu_find_related_event(struct hisi_pcie_pmu *pcie_pmu,
401 					    struct perf_event *event)
402 {
403 	struct perf_event *sibling;
404 	int idx;
405 
406 	for (idx = 0; idx < HISI_PCIE_MAX_COUNTERS; idx++) {
407 		sibling = pcie_pmu->hw_events[idx];
408 		if (!sibling)
409 			continue;
410 
411 		if (!hisi_pcie_pmu_cmp_event(sibling, event))
412 			continue;
413 
414 		/* Related events must be used in group */
415 		if (sibling->group_leader == event->group_leader)
416 			return idx;
417 		else
418 			return -EINVAL;
419 	}
420 
421 	return idx;
422 }
423 
hisi_pcie_pmu_get_event_idx(struct hisi_pcie_pmu * pcie_pmu)424 static int hisi_pcie_pmu_get_event_idx(struct hisi_pcie_pmu *pcie_pmu)
425 {
426 	int idx;
427 
428 	for (idx = 0; idx < HISI_PCIE_MAX_COUNTERS; idx++) {
429 		if (!pcie_pmu->hw_events[idx])
430 			return idx;
431 	}
432 
433 	return -EINVAL;
434 }
435 
hisi_pcie_pmu_event_update(struct perf_event * event)436 static void hisi_pcie_pmu_event_update(struct perf_event *event)
437 {
438 	struct hw_perf_event *hwc = &event->hw;
439 	u64 new_cnt, prev_cnt, delta;
440 
441 	do {
442 		prev_cnt = local64_read(&hwc->prev_count);
443 		new_cnt = hisi_pcie_pmu_read_counter(event);
444 	} while (local64_cmpxchg(&hwc->prev_count, prev_cnt,
445 				 new_cnt) != prev_cnt);
446 
447 	delta = (new_cnt - prev_cnt) & HISI_PCIE_MAX_PERIOD;
448 	local64_add(delta, &event->count);
449 }
450 
hisi_pcie_pmu_read(struct perf_event * event)451 static void hisi_pcie_pmu_read(struct perf_event *event)
452 {
453 	hisi_pcie_pmu_event_update(event);
454 }
455 
hisi_pcie_pmu_set_period(struct perf_event * event)456 static void hisi_pcie_pmu_set_period(struct perf_event *event)
457 {
458 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
459 	struct hw_perf_event *hwc = &event->hw;
460 	int idx = hwc->idx;
461 
462 	local64_set(&hwc->prev_count, HISI_PCIE_INIT_VAL);
463 	hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_CNT, idx, HISI_PCIE_INIT_VAL);
464 	hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EXT_CNT, idx, HISI_PCIE_INIT_VAL);
465 }
466 
hisi_pcie_pmu_enable_counter(struct hisi_pcie_pmu * pcie_pmu,struct hw_perf_event * hwc)467 static void hisi_pcie_pmu_enable_counter(struct hisi_pcie_pmu *pcie_pmu, struct hw_perf_event *hwc)
468 {
469 	u32 idx = hwc->idx;
470 	u64 val;
471 
472 	val = hisi_pcie_pmu_readq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx);
473 	val |= HISI_PCIE_EVENT_EN;
474 	hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx, val);
475 }
476 
hisi_pcie_pmu_disable_counter(struct hisi_pcie_pmu * pcie_pmu,struct hw_perf_event * hwc)477 static void hisi_pcie_pmu_disable_counter(struct hisi_pcie_pmu *pcie_pmu, struct hw_perf_event *hwc)
478 {
479 	u32 idx = hwc->idx;
480 	u64 val;
481 
482 	val = hisi_pcie_pmu_readq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx);
483 	val &= ~HISI_PCIE_EVENT_EN;
484 	hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx, val);
485 }
486 
hisi_pcie_pmu_enable_int(struct hisi_pcie_pmu * pcie_pmu,struct hw_perf_event * hwc)487 static void hisi_pcie_pmu_enable_int(struct hisi_pcie_pmu *pcie_pmu, struct hw_perf_event *hwc)
488 {
489 	u32 idx = hwc->idx;
490 
491 	hisi_pcie_pmu_writel(pcie_pmu, HISI_PCIE_INT_MASK, idx, 0);
492 }
493 
hisi_pcie_pmu_disable_int(struct hisi_pcie_pmu * pcie_pmu,struct hw_perf_event * hwc)494 static void hisi_pcie_pmu_disable_int(struct hisi_pcie_pmu *pcie_pmu, struct hw_perf_event *hwc)
495 {
496 	u32 idx = hwc->idx;
497 
498 	hisi_pcie_pmu_writel(pcie_pmu, HISI_PCIE_INT_MASK, idx, 1);
499 }
500 
hisi_pcie_pmu_reset_counter(struct hisi_pcie_pmu * pcie_pmu,int idx)501 static void hisi_pcie_pmu_reset_counter(struct hisi_pcie_pmu *pcie_pmu, int idx)
502 {
503 	hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx, HISI_PCIE_RESET_CNT);
504 	hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx, HISI_PCIE_INIT_SET);
505 }
506 
hisi_pcie_pmu_start(struct perf_event * event,int flags)507 static void hisi_pcie_pmu_start(struct perf_event *event, int flags)
508 {
509 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
510 	struct hw_perf_event *hwc = &event->hw;
511 	int idx = hwc->idx;
512 	u64 prev_cnt;
513 
514 	if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
515 		return;
516 
517 	WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
518 	hwc->state = 0;
519 
520 	hisi_pcie_pmu_config_filter(event);
521 	hisi_pcie_pmu_enable_counter(pcie_pmu, hwc);
522 	hisi_pcie_pmu_enable_int(pcie_pmu, hwc);
523 	hisi_pcie_pmu_set_period(event);
524 
525 	if (flags & PERF_EF_RELOAD) {
526 		prev_cnt = local64_read(&hwc->prev_count);
527 		hisi_pcie_pmu_writeq(pcie_pmu, hwc->event_base, idx, prev_cnt);
528 	}
529 
530 	perf_event_update_userpage(event);
531 }
532 
hisi_pcie_pmu_stop(struct perf_event * event,int flags)533 static void hisi_pcie_pmu_stop(struct perf_event *event, int flags)
534 {
535 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
536 	struct hw_perf_event *hwc = &event->hw;
537 
538 	hisi_pcie_pmu_event_update(event);
539 	hisi_pcie_pmu_disable_int(pcie_pmu, hwc);
540 	hisi_pcie_pmu_disable_counter(pcie_pmu, hwc);
541 	hisi_pcie_pmu_clear_filter(event);
542 	WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
543 	hwc->state |= PERF_HES_STOPPED;
544 
545 	if (hwc->state & PERF_HES_UPTODATE)
546 		return;
547 
548 	hwc->state |= PERF_HES_UPTODATE;
549 }
550 
hisi_pcie_pmu_add(struct perf_event * event,int flags)551 static int hisi_pcie_pmu_add(struct perf_event *event, int flags)
552 {
553 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
554 	struct hw_perf_event *hwc = &event->hw;
555 	int idx;
556 
557 	hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
558 
559 	/* Check all working events to find a related event. */
560 	idx = hisi_pcie_pmu_find_related_event(pcie_pmu, event);
561 	if (idx < 0)
562 		return idx;
563 
564 	/* Current event shares an enabled counter with the related event */
565 	if (idx < HISI_PCIE_MAX_COUNTERS) {
566 		hwc->idx = idx;
567 		goto start_count;
568 	}
569 
570 	idx = hisi_pcie_pmu_get_event_idx(pcie_pmu);
571 	if (idx < 0)
572 		return idx;
573 
574 	hwc->idx = idx;
575 	pcie_pmu->hw_events[idx] = event;
576 	/* Reset Counter to avoid previous statistic interference. */
577 	hisi_pcie_pmu_reset_counter(pcie_pmu, idx);
578 
579 start_count:
580 	if (flags & PERF_EF_START)
581 		hisi_pcie_pmu_start(event, PERF_EF_RELOAD);
582 
583 	return 0;
584 }
585 
hisi_pcie_pmu_del(struct perf_event * event,int flags)586 static void hisi_pcie_pmu_del(struct perf_event *event, int flags)
587 {
588 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
589 	struct hw_perf_event *hwc = &event->hw;
590 
591 	hisi_pcie_pmu_stop(event, PERF_EF_UPDATE);
592 	pcie_pmu->hw_events[hwc->idx] = NULL;
593 	perf_event_update_userpage(event);
594 }
595 
hisi_pcie_pmu_enable(struct pmu * pmu)596 static void hisi_pcie_pmu_enable(struct pmu *pmu)
597 {
598 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(pmu);
599 	int num;
600 
601 	for (num = 0; num < HISI_PCIE_MAX_COUNTERS; num++) {
602 		if (pcie_pmu->hw_events[num])
603 			break;
604 	}
605 
606 	if (num == HISI_PCIE_MAX_COUNTERS)
607 		return;
608 
609 	writel(HISI_PCIE_GLOBAL_EN, pcie_pmu->base + HISI_PCIE_GLOBAL_CTRL);
610 }
611 
hisi_pcie_pmu_disable(struct pmu * pmu)612 static void hisi_pcie_pmu_disable(struct pmu *pmu)
613 {
614 	struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(pmu);
615 
616 	writel(HISI_PCIE_GLOBAL_NONE, pcie_pmu->base + HISI_PCIE_GLOBAL_CTRL);
617 }
618 
hisi_pcie_pmu_irq(int irq,void * data)619 static irqreturn_t hisi_pcie_pmu_irq(int irq, void *data)
620 {
621 	struct hisi_pcie_pmu *pcie_pmu = data;
622 	irqreturn_t ret = IRQ_NONE;
623 	struct perf_event *event;
624 	u32 overflown;
625 	int idx;
626 
627 	for (idx = 0; idx < HISI_PCIE_MAX_COUNTERS; idx++) {
628 		overflown = hisi_pcie_pmu_readl(pcie_pmu, HISI_PCIE_INT_STAT, idx);
629 		if (!overflown)
630 			continue;
631 
632 		/* Clear status of interrupt. */
633 		hisi_pcie_pmu_writel(pcie_pmu, HISI_PCIE_INT_STAT, idx, 1);
634 		event = pcie_pmu->hw_events[idx];
635 		if (!event)
636 			continue;
637 
638 		hisi_pcie_pmu_event_update(event);
639 		hisi_pcie_pmu_set_period(event);
640 		ret = IRQ_HANDLED;
641 	}
642 
643 	return ret;
644 }
645 
hisi_pcie_pmu_irq_register(struct pci_dev * pdev,struct hisi_pcie_pmu * pcie_pmu)646 static int hisi_pcie_pmu_irq_register(struct pci_dev *pdev, struct hisi_pcie_pmu *pcie_pmu)
647 {
648 	int irq, ret;
649 
650 	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
651 	if (ret < 0) {
652 		pci_err(pdev, "Failed to enable MSI vectors: %d\n", ret);
653 		return ret;
654 	}
655 
656 	irq = pci_irq_vector(pdev, 0);
657 	ret = request_irq(irq, hisi_pcie_pmu_irq, IRQF_NOBALANCING | IRQF_NO_THREAD, DRV_NAME,
658 			  pcie_pmu);
659 	if (ret) {
660 		pci_err(pdev, "Failed to register IRQ: %d\n", ret);
661 		pci_free_irq_vectors(pdev);
662 		return ret;
663 	}
664 
665 	pcie_pmu->irq = irq;
666 
667 	return 0;
668 }
669 
hisi_pcie_pmu_irq_unregister(struct pci_dev * pdev,struct hisi_pcie_pmu * pcie_pmu)670 static void hisi_pcie_pmu_irq_unregister(struct pci_dev *pdev, struct hisi_pcie_pmu *pcie_pmu)
671 {
672 	free_irq(pcie_pmu->irq, pcie_pmu);
673 	pci_free_irq_vectors(pdev);
674 }
675 
hisi_pcie_pmu_online_cpu(unsigned int cpu,struct hlist_node * node)676 static int hisi_pcie_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
677 {
678 	struct hisi_pcie_pmu *pcie_pmu = hlist_entry_safe(node, struct hisi_pcie_pmu, node);
679 
680 	if (pcie_pmu->on_cpu == -1) {
681 		pcie_pmu->on_cpu = cpumask_local_spread(0, dev_to_node(&pcie_pmu->pdev->dev));
682 		WARN_ON(irq_set_affinity(pcie_pmu->irq, cpumask_of(pcie_pmu->on_cpu)));
683 	}
684 
685 	return 0;
686 }
687 
hisi_pcie_pmu_offline_cpu(unsigned int cpu,struct hlist_node * node)688 static int hisi_pcie_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
689 {
690 	struct hisi_pcie_pmu *pcie_pmu = hlist_entry_safe(node, struct hisi_pcie_pmu, node);
691 	unsigned int target;
692 	cpumask_t mask;
693 	int numa_node;
694 
695 	/* Nothing to do if this CPU doesn't own the PMU */
696 	if (pcie_pmu->on_cpu != cpu)
697 		return 0;
698 
699 	pcie_pmu->on_cpu = -1;
700 
701 	/* Choose a local CPU from all online cpus. */
702 	numa_node = dev_to_node(&pcie_pmu->pdev->dev);
703 	if (cpumask_and(&mask, cpumask_of_node(numa_node), cpu_online_mask) &&
704 	    cpumask_andnot(&mask, &mask, cpumask_of(cpu)))
705 		target = cpumask_any(&mask);
706 	else
707 		target = cpumask_any_but(cpu_online_mask, cpu);
708 
709 	if (target >= nr_cpu_ids) {
710 		pci_err(pcie_pmu->pdev, "There is no CPU to set\n");
711 		return 0;
712 	}
713 
714 	perf_pmu_migrate_context(&pcie_pmu->pmu, cpu, target);
715 	/* Use this CPU for event counting */
716 	pcie_pmu->on_cpu = target;
717 	WARN_ON(irq_set_affinity(pcie_pmu->irq, cpumask_of(target)));
718 
719 	return 0;
720 }
721 
722 static struct attribute *hisi_pcie_pmu_events_attr[] = {
723 	HISI_PCIE_PMU_EVENT_ATTR(rx_mwr_latency, 0x0010),
724 	HISI_PCIE_PMU_EVENT_ATTR(rx_mwr_cnt, 0x10010),
725 	HISI_PCIE_PMU_EVENT_ATTR(rx_mrd_latency, 0x0210),
726 	HISI_PCIE_PMU_EVENT_ATTR(rx_mrd_cnt, 0x10210),
727 	HISI_PCIE_PMU_EVENT_ATTR(tx_mrd_latency, 0x0011),
728 	HISI_PCIE_PMU_EVENT_ATTR(tx_mrd_cnt, 0x10011),
729 	HISI_PCIE_PMU_EVENT_ATTR(rx_mrd_flux, 0x0804),
730 	HISI_PCIE_PMU_EVENT_ATTR(rx_mrd_time, 0x10804),
731 	HISI_PCIE_PMU_EVENT_ATTR(tx_mrd_flux, 0x0405),
732 	HISI_PCIE_PMU_EVENT_ATTR(tx_mrd_time, 0x10405),
733 	NULL
734 };
735 
736 static struct attribute_group hisi_pcie_pmu_events_group = {
737 	.name = "events",
738 	.attrs = hisi_pcie_pmu_events_attr,
739 };
740 
741 static struct attribute *hisi_pcie_pmu_format_attr[] = {
742 	HISI_PCIE_PMU_FORMAT_ATTR(event, "config:0-16"),
743 	HISI_PCIE_PMU_FORMAT_ATTR(thr_len, "config1:0-3"),
744 	HISI_PCIE_PMU_FORMAT_ATTR(thr_mode, "config1:4"),
745 	HISI_PCIE_PMU_FORMAT_ATTR(trig_len, "config1:5-8"),
746 	HISI_PCIE_PMU_FORMAT_ATTR(trig_mode, "config1:9"),
747 	HISI_PCIE_PMU_FORMAT_ATTR(len_mode, "config1:10-11"),
748 	HISI_PCIE_PMU_FORMAT_ATTR(port, "config2:0-15"),
749 	HISI_PCIE_PMU_FORMAT_ATTR(bdf, "config2:16-31"),
750 	NULL
751 };
752 
753 static const struct attribute_group hisi_pcie_pmu_format_group = {
754 	.name = "format",
755 	.attrs = hisi_pcie_pmu_format_attr,
756 };
757 
758 static struct attribute *hisi_pcie_pmu_bus_attrs[] = {
759 	&dev_attr_bus.attr,
760 	NULL
761 };
762 
763 static const struct attribute_group hisi_pcie_pmu_bus_attr_group = {
764 	.attrs = hisi_pcie_pmu_bus_attrs,
765 };
766 
767 static struct attribute *hisi_pcie_pmu_cpumask_attrs[] = {
768 	&dev_attr_cpumask.attr,
769 	NULL
770 };
771 
772 static const struct attribute_group hisi_pcie_pmu_cpumask_attr_group = {
773 	.attrs = hisi_pcie_pmu_cpumask_attrs,
774 };
775 
776 static struct attribute *hisi_pcie_pmu_identifier_attrs[] = {
777 	&dev_attr_identifier.attr,
778 	NULL
779 };
780 
781 static const struct attribute_group hisi_pcie_pmu_identifier_attr_group = {
782 	.attrs = hisi_pcie_pmu_identifier_attrs,
783 };
784 
785 static const struct attribute_group *hisi_pcie_pmu_attr_groups[] = {
786 	&hisi_pcie_pmu_events_group,
787 	&hisi_pcie_pmu_format_group,
788 	&hisi_pcie_pmu_bus_attr_group,
789 	&hisi_pcie_pmu_cpumask_attr_group,
790 	&hisi_pcie_pmu_identifier_attr_group,
791 	NULL
792 };
793 
hisi_pcie_alloc_pmu(struct pci_dev * pdev,struct hisi_pcie_pmu * pcie_pmu)794 static int hisi_pcie_alloc_pmu(struct pci_dev *pdev, struct hisi_pcie_pmu *pcie_pmu)
795 {
796 	struct hisi_pcie_reg_pair regs;
797 	u16 sicl_id, core_id;
798 	char *name;
799 
800 	regs = hisi_pcie_parse_reg_value(pcie_pmu, HISI_PCIE_REG_BDF);
801 	pcie_pmu->bdf_min = regs.lo;
802 	pcie_pmu->bdf_max = regs.hi;
803 
804 	regs = hisi_pcie_parse_reg_value(pcie_pmu, HISI_PCIE_REG_INFO);
805 	sicl_id = regs.hi;
806 	core_id = regs.lo;
807 
808 	name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "hisi_pcie%u_core%u", sicl_id, core_id);
809 	if (!name)
810 		return -ENOMEM;
811 
812 	pcie_pmu->pdev = pdev;
813 	pcie_pmu->on_cpu = -1;
814 	pcie_pmu->identifier = readl(pcie_pmu->base + HISI_PCIE_REG_VERSION);
815 	pcie_pmu->pmu = (struct pmu) {
816 		.name		= name,
817 		.module		= THIS_MODULE,
818 		.event_init	= hisi_pcie_pmu_event_init,
819 		.pmu_enable	= hisi_pcie_pmu_enable,
820 		.pmu_disable	= hisi_pcie_pmu_disable,
821 		.add		= hisi_pcie_pmu_add,
822 		.del		= hisi_pcie_pmu_del,
823 		.start		= hisi_pcie_pmu_start,
824 		.stop		= hisi_pcie_pmu_stop,
825 		.read		= hisi_pcie_pmu_read,
826 		.task_ctx_nr	= perf_invalid_context,
827 		.attr_groups	= hisi_pcie_pmu_attr_groups,
828 		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
829 	};
830 
831 	return 0;
832 }
833 
hisi_pcie_init_pmu(struct pci_dev * pdev,struct hisi_pcie_pmu * pcie_pmu)834 static int hisi_pcie_init_pmu(struct pci_dev *pdev, struct hisi_pcie_pmu *pcie_pmu)
835 {
836 	int ret;
837 
838 	pcie_pmu->base = pci_ioremap_bar(pdev, 2);
839 	if (!pcie_pmu->base) {
840 		pci_err(pdev, "Ioremap failed for pcie_pmu resource\n");
841 		return -ENOMEM;
842 	}
843 
844 	ret = hisi_pcie_alloc_pmu(pdev, pcie_pmu);
845 	if (ret)
846 		goto err_iounmap;
847 
848 	ret = hisi_pcie_pmu_irq_register(pdev, pcie_pmu);
849 	if (ret)
850 		goto err_iounmap;
851 
852 	ret = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE, &pcie_pmu->node);
853 	if (ret) {
854 		pci_err(pdev, "Failed to register hotplug: %d\n", ret);
855 		goto err_irq_unregister;
856 	}
857 
858 	ret = perf_pmu_register(&pcie_pmu->pmu, pcie_pmu->pmu.name, -1);
859 	if (ret) {
860 		pci_err(pdev, "Failed to register PCIe PMU: %d\n", ret);
861 		goto err_hotplug_unregister;
862 	}
863 
864 	return ret;
865 
866 err_hotplug_unregister:
867 	cpuhp_state_remove_instance_nocalls(
868 		CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE, &pcie_pmu->node);
869 
870 err_irq_unregister:
871 	hisi_pcie_pmu_irq_unregister(pdev, pcie_pmu);
872 
873 err_iounmap:
874 	iounmap(pcie_pmu->base);
875 
876 	return ret;
877 }
878 
hisi_pcie_uninit_pmu(struct pci_dev * pdev)879 static void hisi_pcie_uninit_pmu(struct pci_dev *pdev)
880 {
881 	struct hisi_pcie_pmu *pcie_pmu = pci_get_drvdata(pdev);
882 
883 	perf_pmu_unregister(&pcie_pmu->pmu);
884 	cpuhp_state_remove_instance_nocalls(
885 		CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE, &pcie_pmu->node);
886 	hisi_pcie_pmu_irq_unregister(pdev, pcie_pmu);
887 	iounmap(pcie_pmu->base);
888 }
889 
hisi_pcie_init_dev(struct pci_dev * pdev)890 static int hisi_pcie_init_dev(struct pci_dev *pdev)
891 {
892 	int ret;
893 
894 	ret = pcim_enable_device(pdev);
895 	if (ret) {
896 		pci_err(pdev, "Failed to enable PCI device: %d\n", ret);
897 		return ret;
898 	}
899 
900 	ret = pcim_iomap_regions(pdev, BIT(2), DRV_NAME);
901 	if (ret < 0) {
902 		pci_err(pdev, "Failed to request PCI mem regions: %d\n", ret);
903 		return ret;
904 	}
905 
906 	pci_set_master(pdev);
907 
908 	return 0;
909 }
910 
hisi_pcie_pmu_probe(struct pci_dev * pdev,const struct pci_device_id * id)911 static int hisi_pcie_pmu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
912 {
913 	struct hisi_pcie_pmu *pcie_pmu;
914 	int ret;
915 
916 	pcie_pmu = devm_kzalloc(&pdev->dev, sizeof(*pcie_pmu), GFP_KERNEL);
917 	if (!pcie_pmu)
918 		return -ENOMEM;
919 
920 	ret = hisi_pcie_init_dev(pdev);
921 	if (ret)
922 		return ret;
923 
924 	ret = hisi_pcie_init_pmu(pdev, pcie_pmu);
925 	if (ret)
926 		return ret;
927 
928 	pci_set_drvdata(pdev, pcie_pmu);
929 
930 	return ret;
931 }
932 
hisi_pcie_pmu_remove(struct pci_dev * pdev)933 static void hisi_pcie_pmu_remove(struct pci_dev *pdev)
934 {
935 	hisi_pcie_uninit_pmu(pdev);
936 	pci_set_drvdata(pdev, NULL);
937 }
938 
939 static const struct pci_device_id hisi_pcie_pmu_ids[] = {
940 	{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, 0xa12d) },
941 	{ 0, }
942 };
943 MODULE_DEVICE_TABLE(pci, hisi_pcie_pmu_ids);
944 
945 static struct pci_driver hisi_pcie_pmu_driver = {
946 	.name = DRV_NAME,
947 	.id_table = hisi_pcie_pmu_ids,
948 	.probe = hisi_pcie_pmu_probe,
949 	.remove = hisi_pcie_pmu_remove,
950 };
951 
hisi_pcie_module_init(void)952 static int __init hisi_pcie_module_init(void)
953 {
954 	int ret;
955 
956 	ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE,
957 				      "AP_PERF_ARM_HISI_PCIE_PMU_ONLINE",
958 				      hisi_pcie_pmu_online_cpu,
959 				      hisi_pcie_pmu_offline_cpu);
960 	if (ret) {
961 		pr_err("Failed to setup PCIe PMU hotplug: %d\n", ret);
962 		return ret;
963 	}
964 
965 	ret = pci_register_driver(&hisi_pcie_pmu_driver);
966 	if (ret)
967 		cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE);
968 
969 	return ret;
970 }
971 module_init(hisi_pcie_module_init);
972 
hisi_pcie_module_exit(void)973 static void __exit hisi_pcie_module_exit(void)
974 {
975 	pci_unregister_driver(&hisi_pcie_pmu_driver);
976 	cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE);
977 }
978 module_exit(hisi_pcie_module_exit);
979 
980 MODULE_DESCRIPTION("HiSilicon PCIe PMU driver");
981 MODULE_LICENSE("GPL v2");
982 MODULE_AUTHOR("Qi Liu <liuqi115@huawei.com>");
983