1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Vendor Specific Extended Capabilities auxiliary bus driver
5 * Copyright (c) 2021, Intel Corporation.
8 * Author: David E. Box <david.e.box@linux.intel.com>
10 * This driver discovers and creates auxiliary devices for Intel defined PCIe
11 * "Vendor Specific" and "Designated Vendor Specific" Extended Capabilities,
12 * VSEC and DVSEC respectively. The driver supports features on specific PCIe
13 * endpoints that exist primarily to expose them.
16 #include <linux/auxiliary_bus.h>
17 #include <linux/bits.h>
18 #include <linux/kernel.h>
19 #include <linux/idr.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/types.h>
26 /* Intel DVSEC offsets */
27 #define INTEL_DVSEC_ENTRIES 0xA
28 #define INTEL_DVSEC_SIZE 0xB
29 #define INTEL_DVSEC_TABLE 0xC
30 #define INTEL_DVSEC_TABLE_BAR(x) ((x) & GENMASK(2, 0))
31 #define INTEL_DVSEC_TABLE_OFFSET(x) ((x) & GENMASK(31, 3))
32 #define TABLE_OFFSET_SHIFT 3
34 static DEFINE_IDA(intel_vsec_ida);
35 static DEFINE_IDA(intel_vsec_sdsi_ida);
38 * struct intel_vsec_header - Common fields of Intel VSEC and DVSEC registers.
39 * @rev: Revision ID of the VSEC/DVSEC register space
40 * @length: Length of the VSEC/DVSEC register space
41 * @id: ID of the feature
42 * @num_entries: Number of instances of the feature
43 * @entry_size: Size of the discovery table for each feature
44 * @tbir: BAR containing the discovery tables
45 * @offset: BAR offset of start of the first discovery table
47 struct intel_vsec_header {
57 /* Platform specific data */
58 struct intel_vsec_platform_info {
59 struct intel_vsec_header **capabilities;
64 VSEC_ID_TELEMETRY = 2,
70 static enum intel_vsec_id intel_vsec_allow_list[] = {
77 static const char *intel_vsec_name(enum intel_vsec_id id)
80 case VSEC_ID_TELEMETRY:
86 case VSEC_ID_CRASHLOG:
97 static bool intel_vsec_allowed(u16 id)
101 for (i = 0; i < ARRAY_SIZE(intel_vsec_allow_list); i++)
102 if (intel_vsec_allow_list[i] == id)
108 static bool intel_vsec_disabled(u16 id, unsigned long quirks)
111 case VSEC_ID_WATCHER:
112 return !!(quirks & VSEC_QUIRK_NO_WATCHER);
114 case VSEC_ID_CRASHLOG:
115 return !!(quirks & VSEC_QUIRK_NO_CRASHLOG);
122 static void intel_vsec_remove_aux(void *data)
124 auxiliary_device_delete(data);
125 auxiliary_device_uninit(data);
128 static void intel_vsec_dev_release(struct device *dev)
130 struct intel_vsec_device *intel_vsec_dev = dev_to_ivdev(dev);
132 ida_free(intel_vsec_dev->ida, intel_vsec_dev->auxdev.id);
133 kfree(intel_vsec_dev->resource);
134 kfree(intel_vsec_dev);
137 static int intel_vsec_add_aux(struct pci_dev *pdev, struct intel_vsec_device *intel_vsec_dev,
140 struct auxiliary_device *auxdev = &intel_vsec_dev->auxdev;
143 ret = ida_alloc(intel_vsec_dev->ida, GFP_KERNEL);
145 kfree(intel_vsec_dev);
151 auxdev->dev.parent = &pdev->dev;
152 auxdev->dev.release = intel_vsec_dev_release;
154 ret = auxiliary_device_init(auxdev);
156 ida_free(intel_vsec_dev->ida, auxdev->id);
157 kfree(intel_vsec_dev->resource);
158 kfree(intel_vsec_dev);
162 ret = auxiliary_device_add(auxdev);
164 auxiliary_device_uninit(auxdev);
168 return devm_add_action_or_reset(&pdev->dev, intel_vsec_remove_aux, auxdev);
171 static int intel_vsec_add_dev(struct pci_dev *pdev, struct intel_vsec_header *header,
172 unsigned long quirks)
174 struct intel_vsec_device *intel_vsec_dev;
175 struct resource *res, *tmp;
178 if (!intel_vsec_allowed(header->id) || intel_vsec_disabled(header->id, quirks))
181 if (!header->num_entries) {
182 dev_dbg(&pdev->dev, "Invalid 0 entry count for header id %d\n", header->id);
186 if (!header->entry_size) {
187 dev_dbg(&pdev->dev, "Invalid 0 entry size for header id %d\n", header->id);
191 intel_vsec_dev = kzalloc(sizeof(*intel_vsec_dev), GFP_KERNEL);
195 res = kcalloc(header->num_entries, sizeof(*res), GFP_KERNEL);
197 kfree(intel_vsec_dev);
201 if (quirks & VSEC_QUIRK_TABLE_SHIFT)
202 header->offset >>= TABLE_OFFSET_SHIFT;
205 * The DVSEC/VSEC contains the starting offset and count for a block of
206 * discovery tables. Create a resource array of these tables to the
207 * auxiliary device driver.
209 for (i = 0, tmp = res; i < header->num_entries; i++, tmp++) {
210 tmp->start = pdev->resource[header->tbir].start +
211 header->offset + i * (header->entry_size * sizeof(u32));
212 tmp->end = tmp->start + (header->entry_size * sizeof(u32)) - 1;
213 tmp->flags = IORESOURCE_MEM;
216 intel_vsec_dev->pcidev = pdev;
217 intel_vsec_dev->resource = res;
218 intel_vsec_dev->num_resources = header->num_entries;
219 intel_vsec_dev->quirks = quirks;
221 if (header->id == VSEC_ID_SDSI)
222 intel_vsec_dev->ida = &intel_vsec_sdsi_ida;
224 intel_vsec_dev->ida = &intel_vsec_ida;
226 return intel_vsec_add_aux(pdev, intel_vsec_dev, intel_vsec_name(header->id));
229 static bool intel_vsec_walk_header(struct pci_dev *pdev, unsigned long quirks,
230 struct intel_vsec_header **header)
232 bool have_devices = false;
235 for ( ; *header; header++) {
236 ret = intel_vsec_add_dev(pdev, *header, quirks);
238 dev_info(&pdev->dev, "Could not add device for DVSEC id %d\n",
247 static bool intel_vsec_walk_dvsec(struct pci_dev *pdev, unsigned long quirks)
249 bool have_devices = false;
253 struct intel_vsec_header header;
258 pos = pci_find_next_ext_capability(pdev, pos, PCI_EXT_CAP_ID_DVSEC);
262 pci_read_config_dword(pdev, pos + PCI_DVSEC_HEADER1, &hdr);
263 vid = PCI_DVSEC_HEADER1_VID(hdr);
264 if (vid != PCI_VENDOR_ID_INTEL)
267 /* Support only revision 1 */
268 header.rev = PCI_DVSEC_HEADER1_REV(hdr);
269 if (header.rev != 1) {
270 dev_info(&pdev->dev, "Unsupported DVSEC revision %d\n", header.rev);
274 header.length = PCI_DVSEC_HEADER1_LEN(hdr);
276 pci_read_config_byte(pdev, pos + INTEL_DVSEC_ENTRIES, &header.num_entries);
277 pci_read_config_byte(pdev, pos + INTEL_DVSEC_SIZE, &header.entry_size);
278 pci_read_config_dword(pdev, pos + INTEL_DVSEC_TABLE, &table);
280 header.tbir = INTEL_DVSEC_TABLE_BAR(table);
281 header.offset = INTEL_DVSEC_TABLE_OFFSET(table);
283 pci_read_config_dword(pdev, pos + PCI_DVSEC_HEADER2, &hdr);
284 header.id = PCI_DVSEC_HEADER2_ID(hdr);
286 ret = intel_vsec_add_dev(pdev, &header, quirks);
296 static bool intel_vsec_walk_vsec(struct pci_dev *pdev, unsigned long quirks)
298 bool have_devices = false;
302 struct intel_vsec_header header;
306 pos = pci_find_next_ext_capability(pdev, pos, PCI_EXT_CAP_ID_VNDR);
310 pci_read_config_dword(pdev, pos + PCI_VNDR_HEADER, &hdr);
312 /* Support only revision 1 */
313 header.rev = PCI_VNDR_HEADER_REV(hdr);
314 if (header.rev != 1) {
315 dev_info(&pdev->dev, "Unsupported VSEC revision %d\n", header.rev);
319 header.id = PCI_VNDR_HEADER_ID(hdr);
320 header.length = PCI_VNDR_HEADER_LEN(hdr);
322 /* entry, size, and table offset are the same as DVSEC */
323 pci_read_config_byte(pdev, pos + INTEL_DVSEC_ENTRIES, &header.num_entries);
324 pci_read_config_byte(pdev, pos + INTEL_DVSEC_SIZE, &header.entry_size);
325 pci_read_config_dword(pdev, pos + INTEL_DVSEC_TABLE, &table);
327 header.tbir = INTEL_DVSEC_TABLE_BAR(table);
328 header.offset = INTEL_DVSEC_TABLE_OFFSET(table);
330 ret = intel_vsec_add_dev(pdev, &header, quirks);
340 static int intel_vsec_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
342 struct intel_vsec_platform_info *info;
343 bool have_devices = false;
344 unsigned long quirks = 0;
347 ret = pcim_enable_device(pdev);
351 info = (struct intel_vsec_platform_info *)id->driver_data;
353 quirks = info->quirks;
355 if (intel_vsec_walk_dvsec(pdev, quirks))
358 if (intel_vsec_walk_vsec(pdev, quirks))
361 if (info && (info->quirks & VSEC_QUIRK_NO_DVSEC) &&
362 intel_vsec_walk_header(pdev, quirks, info->capabilities))
372 static const struct intel_vsec_platform_info tgl_info = {
373 .quirks = VSEC_QUIRK_NO_WATCHER | VSEC_QUIRK_NO_CRASHLOG | VSEC_QUIRK_TABLE_SHIFT,
377 static struct intel_vsec_header dg1_telemetry = {
386 static struct intel_vsec_header *dg1_capabilities[] = {
391 static const struct intel_vsec_platform_info dg1_info = {
392 .capabilities = dg1_capabilities,
393 .quirks = VSEC_QUIRK_NO_DVSEC,
396 #define PCI_DEVICE_ID_INTEL_VSEC_ADL 0x467d
397 #define PCI_DEVICE_ID_INTEL_VSEC_DG1 0x490e
398 #define PCI_DEVICE_ID_INTEL_VSEC_OOBMSM 0x09a7
399 #define PCI_DEVICE_ID_INTEL_VSEC_TGL 0x9a0d
400 static const struct pci_device_id intel_vsec_pci_ids[] = {
401 { PCI_DEVICE_DATA(INTEL, VSEC_ADL, &tgl_info) },
402 { PCI_DEVICE_DATA(INTEL, VSEC_DG1, &dg1_info) },
403 { PCI_DEVICE_DATA(INTEL, VSEC_OOBMSM, NULL) },
404 { PCI_DEVICE_DATA(INTEL, VSEC_TGL, &tgl_info) },
407 MODULE_DEVICE_TABLE(pci, intel_vsec_pci_ids);
409 static struct pci_driver intel_vsec_pci_driver = {
410 .name = "intel_vsec",
411 .id_table = intel_vsec_pci_ids,
412 .probe = intel_vsec_pci_probe,
414 module_pci_driver(intel_vsec_pci_driver);
416 MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
417 MODULE_DESCRIPTION("Intel Extended Capabilities auxiliary bus driver");
418 MODULE_LICENSE("GPL v2");