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