1 // SPDX-License-Identifier: GPL-2.0-only
3 * This driver adds support for PCIe PMU RCiEP device. Related
4 * perf events are bandwidth, latency etc.
6 * Copyright (C) 2021 HiSilicon Limited
7 * Author: Qi Liu <liuqi115@huawei.com>
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>
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
34 /* Define command in HISI_PCIE_GLOBAL_CTRL */
35 #define HISI_PCIE_GLOBAL_EN 0x01
36 #define HISI_PCIE_GLOBAL_NONE 0
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)
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)
55 /* Default config of TLP length mode, will count both TLP headers and payloads */
56 #define HISI_PCIE_LEN_M_DEFAULT 3ULL
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)
65 struct hisi_pcie_pmu {
66 struct perf_event *hw_events[HISI_PCIE_MAX_COUNTERS];
67 struct hlist_node node;
73 /* Minimum and maximum BDF of root ports monitored by PMU */
79 struct hisi_pcie_reg_pair {
84 #define to_pcie_pmu(p) (container_of((p), struct hisi_pcie_pmu, pmu))
85 #define GET_PCI_DEVFN(bdf) ((bdf) & 0xff)
87 #define HISI_PCIE_PMU_FILTER_ATTR(_name, _config, _hi, _lo) \
88 static u64 hisi_pcie_get_##_name(struct perf_event *event) \
90 return FIELD_GET(GENMASK(_hi, _lo), event->attr._config); \
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);
102 static ssize_t hisi_pcie_format_sysfs_show(struct device *dev, struct device_attribute *attr,
105 struct dev_ext_attribute *eattr;
107 eattr = container_of(attr, struct dev_ext_attribute, attr);
109 return sysfs_emit(buf, "%s\n", (char *)eattr->var);
112 static ssize_t hisi_pcie_event_sysfs_show(struct device *dev, struct device_attribute *attr,
115 struct perf_pmu_events_attr *pmu_attr =
116 container_of(attr, struct perf_pmu_events_attr, attr);
118 return sysfs_emit(buf, "config=0x%llx\n", pmu_attr->id);
121 #define HISI_PCIE_PMU_FORMAT_ATTR(_name, _format) \
122 (&((struct dev_ext_attribute[]){ \
123 { .attr = __ATTR(_name, 0444, hisi_pcie_format_sysfs_show, \
125 .var = (void *)_format } \
128 #define HISI_PCIE_PMU_EVENT_ATTR(_name, _id) \
129 PMU_EVENT_ATTR_ID(_name, hisi_pcie_event_sysfs_show, _id)
131 static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, char *buf)
133 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(dev_get_drvdata(dev));
135 return cpumap_print_to_pagebuf(true, buf, cpumask_of(pcie_pmu->on_cpu));
137 static DEVICE_ATTR_RO(cpumask);
139 static ssize_t identifier_show(struct device *dev, struct device_attribute *attr, char *buf)
141 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(dev_get_drvdata(dev));
143 return sysfs_emit(buf, "%#x\n", pcie_pmu->identifier);
145 static DEVICE_ATTR_RO(identifier);
147 static ssize_t bus_show(struct device *dev, struct device_attribute *attr, char *buf)
149 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(dev_get_drvdata(dev));
151 return sysfs_emit(buf, "%#04x\n", PCI_BUS_NUM(pcie_pmu->bdf_min));
153 static DEVICE_ATTR_RO(bus);
155 static struct hisi_pcie_reg_pair
156 hisi_pcie_parse_reg_value(struct hisi_pcie_pmu *pcie_pmu, u32 reg_off)
158 u32 val = readl_relaxed(pcie_pmu->base + reg_off);
159 struct hisi_pcie_reg_pair regs = {
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.
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.
178 #define EXT_COUNTER_IS_USED(idx) ((idx) & BIT(16))
180 static u32 hisi_pcie_get_real_event(struct perf_event *event)
182 return hisi_pcie_get_event(event) & GENMASK(15, 0);
185 static u32 hisi_pcie_pmu_get_offset(u32 offset, u32 idx)
187 return offset + HISI_PCIE_REG_STEP * idx;
190 static u32 hisi_pcie_pmu_readl(struct hisi_pcie_pmu *pcie_pmu, u32 reg_offset,
193 u32 offset = hisi_pcie_pmu_get_offset(reg_offset, idx);
195 return readl_relaxed(pcie_pmu->base + offset);
198 static void hisi_pcie_pmu_writel(struct hisi_pcie_pmu *pcie_pmu, u32 reg_offset, u32 idx, u32 val)
200 u32 offset = hisi_pcie_pmu_get_offset(reg_offset, idx);
202 writel_relaxed(val, pcie_pmu->base + offset);
205 static u64 hisi_pcie_pmu_readq(struct hisi_pcie_pmu *pcie_pmu, u32 reg_offset, u32 idx)
207 u32 offset = hisi_pcie_pmu_get_offset(reg_offset, idx);
209 return readq_relaxed(pcie_pmu->base + offset);
212 static void hisi_pcie_pmu_writeq(struct hisi_pcie_pmu *pcie_pmu, u32 reg_offset, u32 idx, u64 val)
214 u32 offset = hisi_pcie_pmu_get_offset(reg_offset, idx);
216 writeq_relaxed(val, pcie_pmu->base + offset);
219 static void hisi_pcie_pmu_config_filter(struct perf_event *event)
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;
226 /* Config HISI_PCIE_EVENT_CTRL according to event. */
227 reg |= FIELD_PREP(HISI_PCIE_EVENT_M, hisi_pcie_get_real_event(event));
229 /* Config HISI_PCIE_EVENT_CTRL according to root port or EP device. */
230 port = hisi_pcie_get_port(event);
232 reg |= FIELD_PREP(HISI_PCIE_TARGET_M, port);
234 reg |= HISI_PCIE_TARGET_EN |
235 FIELD_PREP(HISI_PCIE_TARGET_M, hisi_pcie_get_bdf(event));
237 /* Config HISI_PCIE_EVENT_CTRL according to trigger condition. */
238 trig_len = hisi_pcie_get_trig_len(event);
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;
245 /* Config HISI_PCIE_EVENT_CTRL according to threshold condition. */
246 thr_len = hisi_pcie_get_thr_len(event);
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;
253 len_mode = hisi_pcie_get_len_mode(event);
255 reg |= FIELD_PREP(HISI_PCIE_LEN_M, len_mode);
257 reg |= FIELD_PREP(HISI_PCIE_LEN_M, HISI_PCIE_LEN_M_DEFAULT);
259 hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, hwc->idx, reg);
262 static void hisi_pcie_pmu_clear_filter(struct perf_event *event)
264 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
265 struct hw_perf_event *hwc = &event->hw;
267 hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, hwc->idx, HISI_PCIE_INIT_SET);
270 static bool hisi_pcie_pmu_valid_requester_id(struct hisi_pcie_pmu *pcie_pmu, u32 bdf)
272 struct pci_dev *root_port, *pdev;
275 pdev = pci_get_domain_bus_and_slot(pci_domain_nr(pcie_pmu->pdev->bus), PCI_BUS_NUM(bdf),
280 root_port = pcie_find_root_port(pdev);
287 rp_bdf = pci_dev_id(root_port);
288 return rp_bdf >= pcie_pmu->bdf_min && rp_bdf <= pcie_pmu->bdf_max;
291 static bool hisi_pcie_pmu_valid_filter(struct perf_event *event,
292 struct hisi_pcie_pmu *pcie_pmu)
294 u32 requester_id = hisi_pcie_get_bdf(event);
296 if (hisi_pcie_get_thr_len(event) > HISI_PCIE_THR_MAX_VAL)
299 if (hisi_pcie_get_trig_len(event) > HISI_PCIE_TRIG_MAX_VAL)
303 if (!hisi_pcie_pmu_valid_requester_id(pcie_pmu, requester_id))
310 static bool hisi_pcie_pmu_cmp_event(struct perf_event *target,
311 struct perf_event *event)
313 return hisi_pcie_get_real_event(target) == hisi_pcie_get_real_event(event);
316 static bool hisi_pcie_pmu_validate_event_group(struct perf_event *event)
318 struct perf_event *sibling, *leader = event->group_leader;
319 struct perf_event *event_group[HISI_PCIE_MAX_COUNTERS];
323 event_group[0] = leader;
324 if (!is_software_event(leader)) {
325 if (leader->pmu != event->pmu)
328 if (leader != event && !hisi_pcie_pmu_cmp_event(leader, event))
329 event_group[counters++] = event;
332 for_each_sibling_event(sibling, event->group_leader) {
333 if (is_software_event(sibling))
336 if (sibling->pmu != event->pmu)
339 for (num = 0; num < counters; num++) {
340 if (hisi_pcie_pmu_cmp_event(event_group[num], sibling))
345 event_group[counters++] = sibling;
348 return counters <= HISI_PCIE_MAX_COUNTERS;
351 static int hisi_pcie_pmu_event_init(struct perf_event *event)
353 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
354 struct hw_perf_event *hwc = &event->hw;
356 /* Check the type first before going on, otherwise it's not our event */
357 if (event->attr.type != event->pmu->type)
360 if (EXT_COUNTER_IS_USED(hisi_pcie_get_event(event)))
361 hwc->event_base = HISI_PCIE_EXT_CNT;
363 hwc->event_base = HISI_PCIE_CNT;
365 /* Sampling is not supported. */
366 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
369 if (!hisi_pcie_pmu_valid_filter(event, pcie_pmu))
372 if (!hisi_pcie_pmu_validate_event_group(event))
375 event->cpu = pcie_pmu->on_cpu;
380 static u64 hisi_pcie_pmu_read_counter(struct perf_event *event)
382 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
383 u32 idx = event->hw.idx;
385 return hisi_pcie_pmu_readq(pcie_pmu, event->hw.event_base, idx);
388 static int hisi_pcie_pmu_find_related_event(struct hisi_pcie_pmu *pcie_pmu,
389 struct perf_event *event)
391 struct perf_event *sibling;
394 for (idx = 0; idx < HISI_PCIE_MAX_COUNTERS; idx++) {
395 sibling = pcie_pmu->hw_events[idx];
399 if (!hisi_pcie_pmu_cmp_event(sibling, event))
402 /* Related events must be used in group */
403 if (sibling->group_leader == event->group_leader)
412 static int hisi_pcie_pmu_get_event_idx(struct hisi_pcie_pmu *pcie_pmu)
416 for (idx = 0; idx < HISI_PCIE_MAX_COUNTERS; idx++) {
417 if (!pcie_pmu->hw_events[idx])
424 static void hisi_pcie_pmu_event_update(struct perf_event *event)
426 struct hw_perf_event *hwc = &event->hw;
427 u64 new_cnt, prev_cnt, delta;
430 prev_cnt = local64_read(&hwc->prev_count);
431 new_cnt = hisi_pcie_pmu_read_counter(event);
432 } while (local64_cmpxchg(&hwc->prev_count, prev_cnt,
433 new_cnt) != prev_cnt);
435 delta = (new_cnt - prev_cnt) & HISI_PCIE_MAX_PERIOD;
436 local64_add(delta, &event->count);
439 static void hisi_pcie_pmu_read(struct perf_event *event)
441 hisi_pcie_pmu_event_update(event);
444 static void hisi_pcie_pmu_set_period(struct perf_event *event)
446 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
447 struct hw_perf_event *hwc = &event->hw;
450 local64_set(&hwc->prev_count, HISI_PCIE_INIT_VAL);
451 hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_CNT, idx, HISI_PCIE_INIT_VAL);
452 hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EXT_CNT, idx, HISI_PCIE_INIT_VAL);
455 static void hisi_pcie_pmu_enable_counter(struct hisi_pcie_pmu *pcie_pmu, struct hw_perf_event *hwc)
460 val = hisi_pcie_pmu_readq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx);
461 val |= HISI_PCIE_EVENT_EN;
462 hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx, val);
465 static void hisi_pcie_pmu_disable_counter(struct hisi_pcie_pmu *pcie_pmu, struct hw_perf_event *hwc)
470 val = hisi_pcie_pmu_readq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx);
471 val &= ~HISI_PCIE_EVENT_EN;
472 hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx, val);
475 static void hisi_pcie_pmu_enable_int(struct hisi_pcie_pmu *pcie_pmu, struct hw_perf_event *hwc)
479 hisi_pcie_pmu_writel(pcie_pmu, HISI_PCIE_INT_MASK, idx, 0);
482 static void hisi_pcie_pmu_disable_int(struct hisi_pcie_pmu *pcie_pmu, struct hw_perf_event *hwc)
486 hisi_pcie_pmu_writel(pcie_pmu, HISI_PCIE_INT_MASK, idx, 1);
489 static void hisi_pcie_pmu_reset_counter(struct hisi_pcie_pmu *pcie_pmu, int idx)
491 hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx, HISI_PCIE_RESET_CNT);
492 hisi_pcie_pmu_writeq(pcie_pmu, HISI_PCIE_EVENT_CTRL, idx, HISI_PCIE_INIT_SET);
495 static void hisi_pcie_pmu_start(struct perf_event *event, int flags)
497 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
498 struct hw_perf_event *hwc = &event->hw;
502 if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
505 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
508 hisi_pcie_pmu_config_filter(event);
509 hisi_pcie_pmu_enable_counter(pcie_pmu, hwc);
510 hisi_pcie_pmu_enable_int(pcie_pmu, hwc);
511 hisi_pcie_pmu_set_period(event);
513 if (flags & PERF_EF_RELOAD) {
514 prev_cnt = local64_read(&hwc->prev_count);
515 hisi_pcie_pmu_writeq(pcie_pmu, hwc->event_base, idx, prev_cnt);
518 perf_event_update_userpage(event);
521 static void hisi_pcie_pmu_stop(struct perf_event *event, int flags)
523 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
524 struct hw_perf_event *hwc = &event->hw;
526 hisi_pcie_pmu_event_update(event);
527 hisi_pcie_pmu_disable_int(pcie_pmu, hwc);
528 hisi_pcie_pmu_disable_counter(pcie_pmu, hwc);
529 hisi_pcie_pmu_clear_filter(event);
530 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
531 hwc->state |= PERF_HES_STOPPED;
533 if (hwc->state & PERF_HES_UPTODATE)
536 hwc->state |= PERF_HES_UPTODATE;
539 static int hisi_pcie_pmu_add(struct perf_event *event, int flags)
541 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
542 struct hw_perf_event *hwc = &event->hw;
545 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
547 /* Check all working events to find a related event. */
548 idx = hisi_pcie_pmu_find_related_event(pcie_pmu, event);
552 /* Current event shares an enabled counter with the related event */
553 if (idx < HISI_PCIE_MAX_COUNTERS) {
558 idx = hisi_pcie_pmu_get_event_idx(pcie_pmu);
563 pcie_pmu->hw_events[idx] = event;
564 /* Reset Counter to avoid previous statistic interference. */
565 hisi_pcie_pmu_reset_counter(pcie_pmu, idx);
568 if (flags & PERF_EF_START)
569 hisi_pcie_pmu_start(event, PERF_EF_RELOAD);
574 static void hisi_pcie_pmu_del(struct perf_event *event, int flags)
576 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(event->pmu);
577 struct hw_perf_event *hwc = &event->hw;
579 hisi_pcie_pmu_stop(event, PERF_EF_UPDATE);
580 pcie_pmu->hw_events[hwc->idx] = NULL;
581 perf_event_update_userpage(event);
584 static void hisi_pcie_pmu_enable(struct pmu *pmu)
586 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(pmu);
589 for (num = 0; num < HISI_PCIE_MAX_COUNTERS; num++) {
590 if (pcie_pmu->hw_events[num])
594 if (num == HISI_PCIE_MAX_COUNTERS)
597 writel(HISI_PCIE_GLOBAL_EN, pcie_pmu->base + HISI_PCIE_GLOBAL_CTRL);
600 static void hisi_pcie_pmu_disable(struct pmu *pmu)
602 struct hisi_pcie_pmu *pcie_pmu = to_pcie_pmu(pmu);
604 writel(HISI_PCIE_GLOBAL_NONE, pcie_pmu->base + HISI_PCIE_GLOBAL_CTRL);
607 static irqreturn_t hisi_pcie_pmu_irq(int irq, void *data)
609 struct hisi_pcie_pmu *pcie_pmu = data;
610 irqreturn_t ret = IRQ_NONE;
611 struct perf_event *event;
615 for (idx = 0; idx < HISI_PCIE_MAX_COUNTERS; idx++) {
616 overflown = hisi_pcie_pmu_readl(pcie_pmu, HISI_PCIE_INT_STAT, idx);
620 /* Clear status of interrupt. */
621 hisi_pcie_pmu_writel(pcie_pmu, HISI_PCIE_INT_STAT, idx, 1);
622 event = pcie_pmu->hw_events[idx];
626 hisi_pcie_pmu_event_update(event);
627 hisi_pcie_pmu_set_period(event);
634 static int hisi_pcie_pmu_irq_register(struct pci_dev *pdev, struct hisi_pcie_pmu *pcie_pmu)
638 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
640 pci_err(pdev, "Failed to enable MSI vectors: %d\n", ret);
644 irq = pci_irq_vector(pdev, 0);
645 ret = request_irq(irq, hisi_pcie_pmu_irq, IRQF_NOBALANCING | IRQF_NO_THREAD, DRV_NAME,
648 pci_err(pdev, "Failed to register IRQ: %d\n", ret);
649 pci_free_irq_vectors(pdev);
658 static void hisi_pcie_pmu_irq_unregister(struct pci_dev *pdev, struct hisi_pcie_pmu *pcie_pmu)
660 free_irq(pcie_pmu->irq, pcie_pmu);
661 pci_free_irq_vectors(pdev);
664 static int hisi_pcie_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
666 struct hisi_pcie_pmu *pcie_pmu = hlist_entry_safe(node, struct hisi_pcie_pmu, node);
668 if (pcie_pmu->on_cpu == -1) {
669 pcie_pmu->on_cpu = cpumask_local_spread(0, dev_to_node(&pcie_pmu->pdev->dev));
670 WARN_ON(irq_set_affinity(pcie_pmu->irq, cpumask_of(pcie_pmu->on_cpu)));
676 static int hisi_pcie_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
678 struct hisi_pcie_pmu *pcie_pmu = hlist_entry_safe(node, struct hisi_pcie_pmu, node);
683 /* Nothing to do if this CPU doesn't own the PMU */
684 if (pcie_pmu->on_cpu != cpu)
687 pcie_pmu->on_cpu = -1;
689 /* Choose a local CPU from all online cpus. */
690 numa_node = dev_to_node(&pcie_pmu->pdev->dev);
691 if (cpumask_and(&mask, cpumask_of_node(numa_node), cpu_online_mask) &&
692 cpumask_andnot(&mask, &mask, cpumask_of(cpu)))
693 target = cpumask_any(&mask);
695 target = cpumask_any_but(cpu_online_mask, cpu);
697 if (target >= nr_cpu_ids) {
698 pci_err(pcie_pmu->pdev, "There is no CPU to set\n");
702 perf_pmu_migrate_context(&pcie_pmu->pmu, cpu, target);
703 /* Use this CPU for event counting */
704 pcie_pmu->on_cpu = target;
705 WARN_ON(irq_set_affinity(pcie_pmu->irq, cpumask_of(target)));
710 static struct attribute *hisi_pcie_pmu_events_attr[] = {
711 HISI_PCIE_PMU_EVENT_ATTR(rx_mwr_latency, 0x0010),
712 HISI_PCIE_PMU_EVENT_ATTR(rx_mwr_cnt, 0x10010),
713 HISI_PCIE_PMU_EVENT_ATTR(rx_mrd_latency, 0x0210),
714 HISI_PCIE_PMU_EVENT_ATTR(rx_mrd_cnt, 0x10210),
715 HISI_PCIE_PMU_EVENT_ATTR(tx_mrd_latency, 0x0011),
716 HISI_PCIE_PMU_EVENT_ATTR(tx_mrd_cnt, 0x10011),
717 HISI_PCIE_PMU_EVENT_ATTR(rx_mrd_flux, 0x0804),
718 HISI_PCIE_PMU_EVENT_ATTR(rx_mrd_time, 0x10804),
719 HISI_PCIE_PMU_EVENT_ATTR(tx_mrd_flux, 0x0405),
720 HISI_PCIE_PMU_EVENT_ATTR(tx_mrd_time, 0x10405),
724 static struct attribute_group hisi_pcie_pmu_events_group = {
726 .attrs = hisi_pcie_pmu_events_attr,
729 static struct attribute *hisi_pcie_pmu_format_attr[] = {
730 HISI_PCIE_PMU_FORMAT_ATTR(event, "config:0-16"),
731 HISI_PCIE_PMU_FORMAT_ATTR(thr_len, "config1:0-3"),
732 HISI_PCIE_PMU_FORMAT_ATTR(thr_mode, "config1:4"),
733 HISI_PCIE_PMU_FORMAT_ATTR(trig_len, "config1:5-8"),
734 HISI_PCIE_PMU_FORMAT_ATTR(trig_mode, "config1:9"),
735 HISI_PCIE_PMU_FORMAT_ATTR(len_mode, "config1:10-11"),
736 HISI_PCIE_PMU_FORMAT_ATTR(port, "config2:0-15"),
737 HISI_PCIE_PMU_FORMAT_ATTR(bdf, "config2:16-31"),
741 static const struct attribute_group hisi_pcie_pmu_format_group = {
743 .attrs = hisi_pcie_pmu_format_attr,
746 static struct attribute *hisi_pcie_pmu_bus_attrs[] = {
751 static const struct attribute_group hisi_pcie_pmu_bus_attr_group = {
752 .attrs = hisi_pcie_pmu_bus_attrs,
755 static struct attribute *hisi_pcie_pmu_cpumask_attrs[] = {
756 &dev_attr_cpumask.attr,
760 static const struct attribute_group hisi_pcie_pmu_cpumask_attr_group = {
761 .attrs = hisi_pcie_pmu_cpumask_attrs,
764 static struct attribute *hisi_pcie_pmu_identifier_attrs[] = {
765 &dev_attr_identifier.attr,
769 static const struct attribute_group hisi_pcie_pmu_identifier_attr_group = {
770 .attrs = hisi_pcie_pmu_identifier_attrs,
773 static const struct attribute_group *hisi_pcie_pmu_attr_groups[] = {
774 &hisi_pcie_pmu_events_group,
775 &hisi_pcie_pmu_format_group,
776 &hisi_pcie_pmu_bus_attr_group,
777 &hisi_pcie_pmu_cpumask_attr_group,
778 &hisi_pcie_pmu_identifier_attr_group,
782 static int hisi_pcie_alloc_pmu(struct pci_dev *pdev, struct hisi_pcie_pmu *pcie_pmu)
784 struct hisi_pcie_reg_pair regs;
785 u16 sicl_id, core_id;
788 regs = hisi_pcie_parse_reg_value(pcie_pmu, HISI_PCIE_REG_BDF);
789 pcie_pmu->bdf_min = regs.lo;
790 pcie_pmu->bdf_max = regs.hi;
792 regs = hisi_pcie_parse_reg_value(pcie_pmu, HISI_PCIE_REG_INFO);
796 name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "hisi_pcie%u_core%u", sicl_id, core_id);
800 pcie_pmu->pdev = pdev;
801 pcie_pmu->on_cpu = -1;
802 pcie_pmu->identifier = readl(pcie_pmu->base + HISI_PCIE_REG_VERSION);
803 pcie_pmu->pmu = (struct pmu) {
805 .module = THIS_MODULE,
806 .event_init = hisi_pcie_pmu_event_init,
807 .pmu_enable = hisi_pcie_pmu_enable,
808 .pmu_disable = hisi_pcie_pmu_disable,
809 .add = hisi_pcie_pmu_add,
810 .del = hisi_pcie_pmu_del,
811 .start = hisi_pcie_pmu_start,
812 .stop = hisi_pcie_pmu_stop,
813 .read = hisi_pcie_pmu_read,
814 .task_ctx_nr = perf_invalid_context,
815 .attr_groups = hisi_pcie_pmu_attr_groups,
816 .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
822 static int hisi_pcie_init_pmu(struct pci_dev *pdev, struct hisi_pcie_pmu *pcie_pmu)
826 pcie_pmu->base = pci_ioremap_bar(pdev, 2);
827 if (!pcie_pmu->base) {
828 pci_err(pdev, "Ioremap failed for pcie_pmu resource\n");
832 ret = hisi_pcie_alloc_pmu(pdev, pcie_pmu);
836 ret = hisi_pcie_pmu_irq_register(pdev, pcie_pmu);
840 ret = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE, &pcie_pmu->node);
842 pci_err(pdev, "Failed to register hotplug: %d\n", ret);
843 goto err_irq_unregister;
846 ret = perf_pmu_register(&pcie_pmu->pmu, pcie_pmu->pmu.name, -1);
848 pci_err(pdev, "Failed to register PCIe PMU: %d\n", ret);
849 goto err_hotplug_unregister;
854 err_hotplug_unregister:
855 cpuhp_state_remove_instance_nocalls(
856 CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE, &pcie_pmu->node);
859 hisi_pcie_pmu_irq_unregister(pdev, pcie_pmu);
862 iounmap(pcie_pmu->base);
867 static void hisi_pcie_uninit_pmu(struct pci_dev *pdev)
869 struct hisi_pcie_pmu *pcie_pmu = pci_get_drvdata(pdev);
871 perf_pmu_unregister(&pcie_pmu->pmu);
872 cpuhp_state_remove_instance_nocalls(
873 CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE, &pcie_pmu->node);
874 hisi_pcie_pmu_irq_unregister(pdev, pcie_pmu);
875 iounmap(pcie_pmu->base);
878 static int hisi_pcie_init_dev(struct pci_dev *pdev)
882 ret = pcim_enable_device(pdev);
884 pci_err(pdev, "Failed to enable PCI device: %d\n", ret);
888 ret = pcim_iomap_regions(pdev, BIT(2), DRV_NAME);
890 pci_err(pdev, "Failed to request PCI mem regions: %d\n", ret);
894 pci_set_master(pdev);
899 static int hisi_pcie_pmu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
901 struct hisi_pcie_pmu *pcie_pmu;
904 pcie_pmu = devm_kzalloc(&pdev->dev, sizeof(*pcie_pmu), GFP_KERNEL);
908 ret = hisi_pcie_init_dev(pdev);
912 ret = hisi_pcie_init_pmu(pdev, pcie_pmu);
916 pci_set_drvdata(pdev, pcie_pmu);
921 static void hisi_pcie_pmu_remove(struct pci_dev *pdev)
923 hisi_pcie_uninit_pmu(pdev);
924 pci_set_drvdata(pdev, NULL);
927 static const struct pci_device_id hisi_pcie_pmu_ids[] = {
928 { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, 0xa12d) },
931 MODULE_DEVICE_TABLE(pci, hisi_pcie_pmu_ids);
933 static struct pci_driver hisi_pcie_pmu_driver = {
935 .id_table = hisi_pcie_pmu_ids,
936 .probe = hisi_pcie_pmu_probe,
937 .remove = hisi_pcie_pmu_remove,
940 static int __init hisi_pcie_module_init(void)
944 ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE,
945 "AP_PERF_ARM_HISI_PCIE_PMU_ONLINE",
946 hisi_pcie_pmu_online_cpu,
947 hisi_pcie_pmu_offline_cpu);
949 pr_err("Failed to setup PCIe PMU hotplug: %d\n", ret);
953 ret = pci_register_driver(&hisi_pcie_pmu_driver);
955 cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE);
959 module_init(hisi_pcie_module_init);
961 static void __exit hisi_pcie_module_exit(void)
963 pci_unregister_driver(&hisi_pcie_pmu_driver);
964 cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_HISI_PCIE_PMU_ONLINE);
966 module_exit(hisi_pcie_module_exit);
968 MODULE_DESCRIPTION("HiSilicon PCIe PMU driver");
969 MODULE_LICENSE("GPL v2");
970 MODULE_AUTHOR("Qi Liu <liuqi115@huawei.com>");