1 // SPDX-License-Identifier: GPL-2.0
3 * Root Complex Event Collector Support
6 * Sean V Kelley <sean.v.kelley@intel.com>
7 * Qiuxu Zhuo <qiuxu.zhuo@intel.com>
9 * Copyright (C) 2020 Intel Corp.
12 #include <linux/kernel.h>
13 #include <linux/pci.h>
14 #include <linux/pci_regs.h>
18 struct walk_rcec_data {
20 int (*user_callback)(struct pci_dev *dev, void *data);
24 static bool rcec_assoc_rciep(struct pci_dev *rcec, struct pci_dev *rciep)
26 unsigned long bitmap = rcec->rcec_ea->bitmap;
29 /* An RCiEP found on a different bus in range */
30 if (rcec->bus->number != rciep->bus->number)
33 /* Same bus, so check bitmap */
34 for_each_set_bit(devn, &bitmap, 32)
35 if (devn == PCI_SLOT(rciep->devfn))
41 static int link_rcec_helper(struct pci_dev *dev, void *data)
43 struct walk_rcec_data *rcec_data = data;
44 struct pci_dev *rcec = rcec_data->rcec;
46 if ((pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) &&
47 rcec_assoc_rciep(rcec, dev)) {
49 pci_dbg(dev, "PME & error events signaled via %s\n",
56 static int walk_rcec_helper(struct pci_dev *dev, void *data)
58 struct walk_rcec_data *rcec_data = data;
59 struct pci_dev *rcec = rcec_data->rcec;
61 if ((pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) &&
62 rcec_assoc_rciep(rcec, dev))
63 rcec_data->user_callback(dev, rcec_data->user_data);
68 static void walk_rcec(int (*cb)(struct pci_dev *dev, void *data),
71 struct walk_rcec_data *rcec_data = userdata;
72 struct pci_dev *rcec = rcec_data->rcec;
73 u8 nextbusn, lastbusn;
80 /* Walk own bus for bitmap based association */
81 pci_walk_bus(rcec->bus, cb, rcec_data);
83 nextbusn = rcec->rcec_ea->nextbusn;
84 lastbusn = rcec->rcec_ea->lastbusn;
86 /* All RCiEP devices are on the same bus as the RCEC */
87 if (nextbusn == 0xff && lastbusn == 0x00)
90 for (bnr = nextbusn; bnr <= lastbusn; bnr++) {
91 /* No association indicated (PCIe 5.0-1, 7.9.10.3) */
92 if (bnr == rcec->bus->number)
95 bus = pci_find_bus(pci_domain_nr(rcec->bus), bnr);
99 /* Find RCiEP devices on the given bus ranges */
100 pci_walk_bus(bus, cb, rcec_data);
105 * pcie_link_rcec - Link RCiEP devices associated with RCEC.
106 * @rcec: RCEC whose RCiEP devices should be linked.
108 * Link the given RCEC to each RCiEP device found.
110 void pcie_link_rcec(struct pci_dev *rcec)
112 struct walk_rcec_data rcec_data;
117 rcec_data.rcec = rcec;
118 rcec_data.user_callback = NULL;
119 rcec_data.user_data = NULL;
121 walk_rcec(link_rcec_helper, &rcec_data);
125 * pcie_walk_rcec - Walk RCiEP devices associating with RCEC and call callback.
126 * @rcec: RCEC whose RCiEP devices should be walked
127 * @cb: Callback to be called for each RCiEP device found
128 * @userdata: Arbitrary pointer to be passed to callback
130 * Walk the given RCEC. Call the callback on each RCiEP found.
132 * If @cb returns anything other than 0, break out.
134 void pcie_walk_rcec(struct pci_dev *rcec, int (*cb)(struct pci_dev *, void *),
137 struct walk_rcec_data rcec_data;
142 rcec_data.rcec = rcec;
143 rcec_data.user_callback = cb;
144 rcec_data.user_data = userdata;
146 walk_rcec(walk_rcec_helper, &rcec_data);
149 void pci_rcec_init(struct pci_dev *dev)
151 struct rcec_ea *rcec_ea;
155 /* Only for Root Complex Event Collectors */
156 if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_EC)
159 rcec = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_RCEC);
163 rcec_ea = kzalloc(sizeof(*rcec_ea), GFP_KERNEL);
167 pci_read_config_dword(dev, rcec + PCI_RCEC_RCIEP_BITMAP,
170 /* Check whether RCEC BUSN register is present */
171 pci_read_config_dword(dev, rcec, &hdr);
172 ver = PCI_EXT_CAP_VER(hdr);
173 if (ver >= PCI_RCEC_BUSN_REG_VER) {
174 pci_read_config_dword(dev, rcec + PCI_RCEC_BUSN, &busn);
175 rcec_ea->nextbusn = PCI_RCEC_BUSN_NEXT(busn);
176 rcec_ea->lastbusn = PCI_RCEC_BUSN_LAST(busn);
178 /* Avoid later ver check by setting nextbusn */
179 rcec_ea->nextbusn = 0xff;
180 rcec_ea->lastbusn = 0x00;
183 dev->rcec_ea = rcec_ea;
186 void pci_rcec_exit(struct pci_dev *dev)