1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Vendor Specific Extended Capabilities auxiliary bus driver
5 * Copyright (c) 2021, Intel Corporation.
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/delay.h>
19 #include <linux/kernel.h>
20 #include <linux/idr.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/types.h>
27 /* Intel DVSEC offsets */
28 #define INTEL_DVSEC_ENTRIES 0xA
29 #define INTEL_DVSEC_SIZE 0xB
30 #define INTEL_DVSEC_TABLE 0xC
31 #define INTEL_DVSEC_TABLE_BAR(x) ((x) & GENMASK(2, 0))
32 #define INTEL_DVSEC_TABLE_OFFSET(x) ((x) & GENMASK(31, 3))
33 #define TABLE_OFFSET_SHIFT 3
34 #define PMT_XA_START 0
35 #define PMT_XA_MAX INT_MAX
36 #define PMT_XA_LIMIT XA_LIMIT(PMT_XA_START, PMT_XA_MAX)
38 static DEFINE_IDA(intel_vsec_ida);
39 static DEFINE_IDA(intel_vsec_sdsi_ida);
40 static DEFINE_XARRAY_ALLOC(auxdev_array);
43 * struct intel_vsec_header - Common fields of Intel VSEC and DVSEC registers.
44 * @rev: Revision ID of the VSEC/DVSEC register space
45 * @length: Length of the VSEC/DVSEC register space
46 * @id: ID of the feature
47 * @num_entries: Number of instances of the feature
48 * @entry_size: Size of the discovery table for each feature
49 * @tbir: BAR containing the discovery tables
50 * @offset: BAR offset of start of the first discovery table
52 struct intel_vsec_header {
63 VSEC_ID_TELEMETRY = 2,
69 static enum intel_vsec_id intel_vsec_allow_list[] = {
76 static const char *intel_vsec_name(enum intel_vsec_id id)
79 case VSEC_ID_TELEMETRY:
85 case VSEC_ID_CRASHLOG:
96 static bool intel_vsec_allowed(u16 id)
100 for (i = 0; i < ARRAY_SIZE(intel_vsec_allow_list); i++)
101 if (intel_vsec_allow_list[i] == id)
107 static bool intel_vsec_disabled(u16 id, unsigned long quirks)
110 case VSEC_ID_WATCHER:
111 return !!(quirks & VSEC_QUIRK_NO_WATCHER);
113 case VSEC_ID_CRASHLOG:
114 return !!(quirks & VSEC_QUIRK_NO_CRASHLOG);
121 static void intel_vsec_remove_aux(void *data)
123 auxiliary_device_delete(data);
124 auxiliary_device_uninit(data);
127 static void intel_vsec_dev_release(struct device *dev)
129 struct intel_vsec_device *intel_vsec_dev = dev_to_ivdev(dev);
131 ida_free(intel_vsec_dev->ida, intel_vsec_dev->auxdev.id);
132 kfree(intel_vsec_dev->resource);
133 kfree(intel_vsec_dev);
136 static int intel_vsec_add_aux(struct pci_dev *pdev, struct intel_vsec_device *intel_vsec_dev,
139 struct auxiliary_device *auxdev = &intel_vsec_dev->auxdev;
142 ret = ida_alloc(intel_vsec_dev->ida, GFP_KERNEL);
144 kfree(intel_vsec_dev);
150 auxdev->dev.parent = &pdev->dev;
151 auxdev->dev.release = intel_vsec_dev_release;
153 ret = auxiliary_device_init(auxdev);
155 ida_free(intel_vsec_dev->ida, auxdev->id);
156 kfree(intel_vsec_dev->resource);
157 kfree(intel_vsec_dev);
161 ret = auxiliary_device_add(auxdev);
163 auxiliary_device_uninit(auxdev);
167 ret = devm_add_action_or_reset(&pdev->dev, intel_vsec_remove_aux,
172 /* Add auxdev to list */
173 ret = xa_alloc(&auxdev_array, &id, intel_vsec_dev, PMT_XA_LIMIT,
181 static int intel_vsec_add_dev(struct pci_dev *pdev, struct intel_vsec_header *header,
182 struct intel_vsec_platform_info *info)
184 struct intel_vsec_device *intel_vsec_dev;
185 struct resource *res, *tmp;
186 unsigned long quirks = info->quirks;
189 if (!intel_vsec_allowed(header->id) || intel_vsec_disabled(header->id, quirks))
192 if (!header->num_entries) {
193 dev_dbg(&pdev->dev, "Invalid 0 entry count for header id %d\n", header->id);
197 if (!header->entry_size) {
198 dev_dbg(&pdev->dev, "Invalid 0 entry size for header id %d\n", header->id);
202 intel_vsec_dev = kzalloc(sizeof(*intel_vsec_dev), GFP_KERNEL);
206 res = kcalloc(header->num_entries, sizeof(*res), GFP_KERNEL);
208 kfree(intel_vsec_dev);
212 if (quirks & VSEC_QUIRK_TABLE_SHIFT)
213 header->offset >>= TABLE_OFFSET_SHIFT;
216 * The DVSEC/VSEC contains the starting offset and count for a block of
217 * discovery tables. Create a resource array of these tables to the
218 * auxiliary device driver.
220 for (i = 0, tmp = res; i < header->num_entries; i++, tmp++) {
221 tmp->start = pdev->resource[header->tbir].start +
222 header->offset + i * (header->entry_size * sizeof(u32));
223 tmp->end = tmp->start + (header->entry_size * sizeof(u32)) - 1;
224 tmp->flags = IORESOURCE_MEM;
227 intel_vsec_dev->pcidev = pdev;
228 intel_vsec_dev->resource = res;
229 intel_vsec_dev->num_resources = header->num_entries;
230 intel_vsec_dev->info = info;
232 if (header->id == VSEC_ID_SDSI)
233 intel_vsec_dev->ida = &intel_vsec_sdsi_ida;
235 intel_vsec_dev->ida = &intel_vsec_ida;
237 return intel_vsec_add_aux(pdev, intel_vsec_dev, intel_vsec_name(header->id));
240 static bool intel_vsec_walk_header(struct pci_dev *pdev,
241 struct intel_vsec_platform_info *info)
243 struct intel_vsec_header **header = info->capabilities;
244 bool have_devices = false;
247 for ( ; *header; header++) {
248 ret = intel_vsec_add_dev(pdev, *header, info);
250 dev_info(&pdev->dev, "Could not add device for DVSEC id %d\n",
259 static bool intel_vsec_walk_dvsec(struct pci_dev *pdev,
260 struct intel_vsec_platform_info *info)
262 bool have_devices = false;
266 struct intel_vsec_header header;
271 pos = pci_find_next_ext_capability(pdev, pos, PCI_EXT_CAP_ID_DVSEC);
275 pci_read_config_dword(pdev, pos + PCI_DVSEC_HEADER1, &hdr);
276 vid = PCI_DVSEC_HEADER1_VID(hdr);
277 if (vid != PCI_VENDOR_ID_INTEL)
280 /* Support only revision 1 */
281 header.rev = PCI_DVSEC_HEADER1_REV(hdr);
282 if (header.rev != 1) {
283 dev_info(&pdev->dev, "Unsupported DVSEC revision %d\n", header.rev);
287 header.length = PCI_DVSEC_HEADER1_LEN(hdr);
289 pci_read_config_byte(pdev, pos + INTEL_DVSEC_ENTRIES, &header.num_entries);
290 pci_read_config_byte(pdev, pos + INTEL_DVSEC_SIZE, &header.entry_size);
291 pci_read_config_dword(pdev, pos + INTEL_DVSEC_TABLE, &table);
293 header.tbir = INTEL_DVSEC_TABLE_BAR(table);
294 header.offset = INTEL_DVSEC_TABLE_OFFSET(table);
296 pci_read_config_dword(pdev, pos + PCI_DVSEC_HEADER2, &hdr);
297 header.id = PCI_DVSEC_HEADER2_ID(hdr);
299 ret = intel_vsec_add_dev(pdev, &header, info);
309 static bool intel_vsec_walk_vsec(struct pci_dev *pdev,
310 struct intel_vsec_platform_info *info)
312 bool have_devices = false;
316 struct intel_vsec_header header;
320 pos = pci_find_next_ext_capability(pdev, pos, PCI_EXT_CAP_ID_VNDR);
324 pci_read_config_dword(pdev, pos + PCI_VNDR_HEADER, &hdr);
326 /* Support only revision 1 */
327 header.rev = PCI_VNDR_HEADER_REV(hdr);
328 if (header.rev != 1) {
329 dev_info(&pdev->dev, "Unsupported VSEC revision %d\n", header.rev);
333 header.id = PCI_VNDR_HEADER_ID(hdr);
334 header.length = PCI_VNDR_HEADER_LEN(hdr);
336 /* entry, size, and table offset are the same as DVSEC */
337 pci_read_config_byte(pdev, pos + INTEL_DVSEC_ENTRIES, &header.num_entries);
338 pci_read_config_byte(pdev, pos + INTEL_DVSEC_SIZE, &header.entry_size);
339 pci_read_config_dword(pdev, pos + INTEL_DVSEC_TABLE, &table);
341 header.tbir = INTEL_DVSEC_TABLE_BAR(table);
342 header.offset = INTEL_DVSEC_TABLE_OFFSET(table);
344 ret = intel_vsec_add_dev(pdev, &header, info);
354 static int intel_vsec_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
356 struct intel_vsec_platform_info *info;
357 bool have_devices = false;
360 ret = pcim_enable_device(pdev);
364 pci_save_state(pdev);
365 info = (struct intel_vsec_platform_info *)id->driver_data;
369 if (intel_vsec_walk_dvsec(pdev, info))
372 if (intel_vsec_walk_vsec(pdev, info))
375 if (info && (info->quirks & VSEC_QUIRK_NO_DVSEC) &&
376 intel_vsec_walk_header(pdev, info))
386 static const struct intel_vsec_platform_info tgl_info = {
387 .quirks = VSEC_QUIRK_NO_WATCHER | VSEC_QUIRK_NO_CRASHLOG |
388 VSEC_QUIRK_TABLE_SHIFT | VSEC_QUIRK_EARLY_HW,
392 static struct intel_vsec_header dg1_telemetry = {
401 static struct intel_vsec_header *dg1_capabilities[] = {
406 static const struct intel_vsec_platform_info dg1_info = {
407 .capabilities = dg1_capabilities,
408 .quirks = VSEC_QUIRK_NO_DVSEC | VSEC_QUIRK_EARLY_HW,
411 #define PCI_DEVICE_ID_INTEL_VSEC_ADL 0x467d
412 #define PCI_DEVICE_ID_INTEL_VSEC_DG1 0x490e
413 #define PCI_DEVICE_ID_INTEL_VSEC_OOBMSM 0x09a7
414 #define PCI_DEVICE_ID_INTEL_VSEC_RPL 0xa77d
415 #define PCI_DEVICE_ID_INTEL_VSEC_TGL 0x9a0d
416 static const struct pci_device_id intel_vsec_pci_ids[] = {
417 { PCI_DEVICE_DATA(INTEL, VSEC_ADL, &tgl_info) },
418 { PCI_DEVICE_DATA(INTEL, VSEC_DG1, &dg1_info) },
419 { PCI_DEVICE_DATA(INTEL, VSEC_OOBMSM, &(struct intel_vsec_platform_info) {}) },
420 { PCI_DEVICE_DATA(INTEL, VSEC_RPL, &tgl_info) },
421 { PCI_DEVICE_DATA(INTEL, VSEC_TGL, &tgl_info) },
424 MODULE_DEVICE_TABLE(pci, intel_vsec_pci_ids);
426 static pci_ers_result_t intel_vsec_pci_error_detected(struct pci_dev *pdev,
427 pci_channel_state_t state)
429 pci_ers_result_t status = PCI_ERS_RESULT_NEED_RESET;
431 dev_info(&pdev->dev, "PCI error detected, state %d", state);
433 if (state == pci_channel_io_perm_failure)
434 status = PCI_ERS_RESULT_DISCONNECT;
436 pci_disable_device(pdev);
441 static pci_ers_result_t intel_vsec_pci_slot_reset(struct pci_dev *pdev)
443 struct intel_vsec_device *intel_vsec_dev;
444 pci_ers_result_t status = PCI_ERS_RESULT_DISCONNECT;
445 const struct pci_device_id *pci_dev_id;
448 dev_info(&pdev->dev, "Resetting PCI slot\n");
451 if (pci_enable_device(pdev)) {
453 "Failed to re-enable PCI device after reset.\n");
457 status = PCI_ERS_RESULT_RECOVERED;
459 xa_for_each(&auxdev_array, index, intel_vsec_dev) {
460 /* check if pdev doesn't match */
461 if (pdev != intel_vsec_dev->pcidev)
463 devm_release_action(&pdev->dev, intel_vsec_remove_aux,
464 &intel_vsec_dev->auxdev);
466 pci_disable_device(pdev);
467 pci_restore_state(pdev);
468 pci_dev_id = pci_match_id(intel_vsec_pci_ids, pdev);
469 intel_vsec_pci_probe(pdev, pci_dev_id);
475 static void intel_vsec_pci_resume(struct pci_dev *pdev)
477 dev_info(&pdev->dev, "Done resuming PCI device\n");
480 static const struct pci_error_handlers intel_vsec_pci_err_handlers = {
481 .error_detected = intel_vsec_pci_error_detected,
482 .slot_reset = intel_vsec_pci_slot_reset,
483 .resume = intel_vsec_pci_resume,
486 static struct pci_driver intel_vsec_pci_driver = {
487 .name = "intel_vsec",
488 .id_table = intel_vsec_pci_ids,
489 .probe = intel_vsec_pci_probe,
490 .err_handler = &intel_vsec_pci_err_handlers,
492 module_pci_driver(intel_vsec_pci_driver);
495 MODULE_DESCRIPTION("Intel Extended Capabilities auxiliary bus driver");
496 MODULE_LICENSE("GPL v2");