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/cleanup.h>
19 #include <linux/delay.h>
20 #include <linux/kernel.h>
21 #include <linux/idr.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
24 #include <linux/types.h>
28 #define PMT_XA_START 0
29 #define PMT_XA_MAX INT_MAX
30 #define PMT_XA_LIMIT XA_LIMIT(PMT_XA_START, PMT_XA_MAX)
32 static DEFINE_IDA(intel_vsec_ida);
33 static DEFINE_IDA(intel_vsec_sdsi_ida);
34 static DEFINE_XARRAY_ALLOC(auxdev_array);
36 static const char *intel_vsec_name(enum intel_vsec_id id)
39 case VSEC_ID_TELEMETRY:
45 case VSEC_ID_CRASHLOG:
59 static bool intel_vsec_supported(u16 id, unsigned long caps)
62 case VSEC_ID_TELEMETRY:
63 return !!(caps & VSEC_CAP_TELEMETRY);
65 return !!(caps & VSEC_CAP_WATCHER);
66 case VSEC_ID_CRASHLOG:
67 return !!(caps & VSEC_CAP_CRASHLOG);
69 return !!(caps & VSEC_CAP_SDSI);
71 return !!(caps & VSEC_CAP_TPMI);
77 static void intel_vsec_remove_aux(void *data)
79 auxiliary_device_delete(data);
80 auxiliary_device_uninit(data);
83 static DEFINE_MUTEX(vsec_ida_lock);
85 static void intel_vsec_dev_release(struct device *dev)
87 struct intel_vsec_device *intel_vsec_dev = dev_to_ivdev(dev);
89 xa_erase(&auxdev_array, intel_vsec_dev->id);
91 mutex_lock(&vsec_ida_lock);
92 ida_free(intel_vsec_dev->ida, intel_vsec_dev->auxdev.id);
93 mutex_unlock(&vsec_ida_lock);
95 kfree(intel_vsec_dev->resource);
96 kfree(intel_vsec_dev);
99 int intel_vsec_add_aux(struct pci_dev *pdev, struct device *parent,
100 struct intel_vsec_device *intel_vsec_dev,
103 struct auxiliary_device *auxdev = &intel_vsec_dev->auxdev;
109 ret = xa_alloc(&auxdev_array, &intel_vsec_dev->id, intel_vsec_dev,
110 PMT_XA_LIMIT, GFP_KERNEL);
112 kfree(intel_vsec_dev->resource);
113 kfree(intel_vsec_dev);
117 mutex_lock(&vsec_ida_lock);
118 id = ida_alloc(intel_vsec_dev->ida, GFP_KERNEL);
119 mutex_unlock(&vsec_ida_lock);
121 xa_erase(&auxdev_array, intel_vsec_dev->id);
122 kfree(intel_vsec_dev->resource);
123 kfree(intel_vsec_dev);
129 auxdev->dev.parent = parent;
130 auxdev->dev.release = intel_vsec_dev_release;
132 ret = auxiliary_device_init(auxdev);
134 intel_vsec_dev_release(&auxdev->dev);
138 ret = auxiliary_device_add(auxdev);
140 auxiliary_device_uninit(auxdev);
144 return devm_add_action_or_reset(parent, intel_vsec_remove_aux,
147 EXPORT_SYMBOL_NS_GPL(intel_vsec_add_aux, INTEL_VSEC);
149 static int intel_vsec_add_dev(struct pci_dev *pdev, struct intel_vsec_header *header,
150 struct intel_vsec_platform_info *info)
152 struct intel_vsec_device __free(kfree) *intel_vsec_dev = NULL;
153 struct resource __free(kfree) *res = NULL;
154 struct resource *tmp;
155 struct device *parent;
156 unsigned long quirks = info->quirks;
161 parent = info->parent;
165 if (!intel_vsec_supported(header->id, info->caps))
168 if (!header->num_entries) {
169 dev_dbg(&pdev->dev, "Invalid 0 entry count for header id %d\n", header->id);
173 if (!header->entry_size) {
174 dev_dbg(&pdev->dev, "Invalid 0 entry size for header id %d\n", header->id);
178 intel_vsec_dev = kzalloc(sizeof(*intel_vsec_dev), GFP_KERNEL);
182 res = kcalloc(header->num_entries, sizeof(*res), GFP_KERNEL);
186 if (quirks & VSEC_QUIRK_TABLE_SHIFT)
187 header->offset >>= TABLE_OFFSET_SHIFT;
190 base_addr = info->base_addr;
192 base_addr = pdev->resource[header->tbir].start;
195 * The DVSEC/VSEC contains the starting offset and count for a block of
196 * discovery tables. Create a resource array of these tables to the
197 * auxiliary device driver.
199 for (i = 0, tmp = res; i < header->num_entries; i++, tmp++) {
200 tmp->start = base_addr + header->offset + i * (header->entry_size * sizeof(u32));
201 tmp->end = tmp->start + (header->entry_size * sizeof(u32)) - 1;
202 tmp->flags = IORESOURCE_MEM;
204 /* Check resource is not in use */
205 if (!request_mem_region(tmp->start, resource_size(tmp), ""))
208 release_mem_region(tmp->start, resource_size(tmp));
211 intel_vsec_dev->pcidev = pdev;
212 intel_vsec_dev->resource = no_free_ptr(res);
213 intel_vsec_dev->num_resources = header->num_entries;
214 intel_vsec_dev->quirks = info->quirks;
215 intel_vsec_dev->base_addr = info->base_addr;
217 if (header->id == VSEC_ID_SDSI)
218 intel_vsec_dev->ida = &intel_vsec_sdsi_ida;
220 intel_vsec_dev->ida = &intel_vsec_ida;
223 * Pass the ownership of intel_vsec_dev and resource within it to
224 * intel_vsec_add_aux()
226 return intel_vsec_add_aux(pdev, parent, no_free_ptr(intel_vsec_dev),
227 intel_vsec_name(header->id));
230 static bool intel_vsec_walk_header(struct pci_dev *pdev,
231 struct intel_vsec_platform_info *info)
233 struct intel_vsec_header **header = info->headers;
234 bool have_devices = false;
237 for ( ; *header; header++) {
238 ret = intel_vsec_add_dev(pdev, *header, info);
246 static bool intel_vsec_walk_dvsec(struct pci_dev *pdev,
247 struct intel_vsec_platform_info *info)
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, info);
296 static bool intel_vsec_walk_vsec(struct pci_dev *pdev,
297 struct intel_vsec_platform_info *info)
299 bool have_devices = false;
303 struct intel_vsec_header header;
307 pos = pci_find_next_ext_capability(pdev, pos, PCI_EXT_CAP_ID_VNDR);
311 pci_read_config_dword(pdev, pos + PCI_VNDR_HEADER, &hdr);
313 /* Support only revision 1 */
314 header.rev = PCI_VNDR_HEADER_REV(hdr);
315 if (header.rev != 1) {
316 dev_info(&pdev->dev, "Unsupported VSEC revision %d\n", header.rev);
320 header.id = PCI_VNDR_HEADER_ID(hdr);
321 header.length = PCI_VNDR_HEADER_LEN(hdr);
323 /* entry, size, and table offset are the same as DVSEC */
324 pci_read_config_byte(pdev, pos + INTEL_DVSEC_ENTRIES, &header.num_entries);
325 pci_read_config_byte(pdev, pos + INTEL_DVSEC_SIZE, &header.entry_size);
326 pci_read_config_dword(pdev, pos + INTEL_DVSEC_TABLE, &table);
328 header.tbir = INTEL_DVSEC_TABLE_BAR(table);
329 header.offset = INTEL_DVSEC_TABLE_OFFSET(table);
331 ret = intel_vsec_add_dev(pdev, &header, info);
341 void intel_vsec_register(struct pci_dev *pdev,
342 struct intel_vsec_platform_info *info)
347 intel_vsec_walk_header(pdev, info);
349 EXPORT_SYMBOL_NS_GPL(intel_vsec_register, INTEL_VSEC);
351 static int intel_vsec_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
353 struct intel_vsec_platform_info *info;
354 bool have_devices = false;
357 ret = pcim_enable_device(pdev);
361 pci_save_state(pdev);
362 info = (struct intel_vsec_platform_info *)id->driver_data;
366 if (intel_vsec_walk_dvsec(pdev, info))
369 if (intel_vsec_walk_vsec(pdev, info))
372 if (info && (info->quirks & VSEC_QUIRK_NO_DVSEC) &&
373 intel_vsec_walk_header(pdev, info))
383 static struct intel_vsec_header dg1_header = {
392 static struct intel_vsec_header *dg1_headers[] = {
397 static const struct intel_vsec_platform_info dg1_info = {
398 .caps = VSEC_CAP_TELEMETRY,
399 .headers = dg1_headers,
400 .quirks = VSEC_QUIRK_NO_DVSEC | VSEC_QUIRK_EARLY_HW,
404 static const struct intel_vsec_platform_info mtl_info = {
405 .caps = VSEC_CAP_TELEMETRY,
409 static const struct intel_vsec_platform_info oobmsm_info = {
410 .caps = VSEC_CAP_TELEMETRY | VSEC_CAP_SDSI | VSEC_CAP_TPMI,
414 static const struct intel_vsec_platform_info tgl_info = {
415 .caps = VSEC_CAP_TELEMETRY,
416 .quirks = VSEC_QUIRK_TABLE_SHIFT | VSEC_QUIRK_EARLY_HW,
420 static const struct intel_vsec_platform_info lnl_info = {
421 .caps = VSEC_CAP_TELEMETRY | VSEC_CAP_WATCHER,
424 #define PCI_DEVICE_ID_INTEL_VSEC_ADL 0x467d
425 #define PCI_DEVICE_ID_INTEL_VSEC_DG1 0x490e
426 #define PCI_DEVICE_ID_INTEL_VSEC_MTL_M 0x7d0d
427 #define PCI_DEVICE_ID_INTEL_VSEC_MTL_S 0xad0d
428 #define PCI_DEVICE_ID_INTEL_VSEC_OOBMSM 0x09a7
429 #define PCI_DEVICE_ID_INTEL_VSEC_RPL 0xa77d
430 #define PCI_DEVICE_ID_INTEL_VSEC_TGL 0x9a0d
431 #define PCI_DEVICE_ID_INTEL_VSEC_LNL_M 0x647d
432 static const struct pci_device_id intel_vsec_pci_ids[] = {
433 { PCI_DEVICE_DATA(INTEL, VSEC_ADL, &tgl_info) },
434 { PCI_DEVICE_DATA(INTEL, VSEC_DG1, &dg1_info) },
435 { PCI_DEVICE_DATA(INTEL, VSEC_MTL_M, &mtl_info) },
436 { PCI_DEVICE_DATA(INTEL, VSEC_MTL_S, &mtl_info) },
437 { PCI_DEVICE_DATA(INTEL, VSEC_OOBMSM, &oobmsm_info) },
438 { PCI_DEVICE_DATA(INTEL, VSEC_RPL, &tgl_info) },
439 { PCI_DEVICE_DATA(INTEL, VSEC_TGL, &tgl_info) },
440 { PCI_DEVICE_DATA(INTEL, VSEC_LNL_M, &lnl_info) },
443 MODULE_DEVICE_TABLE(pci, intel_vsec_pci_ids);
445 static pci_ers_result_t intel_vsec_pci_error_detected(struct pci_dev *pdev,
446 pci_channel_state_t state)
448 pci_ers_result_t status = PCI_ERS_RESULT_NEED_RESET;
450 dev_info(&pdev->dev, "PCI error detected, state %d", state);
452 if (state == pci_channel_io_perm_failure)
453 status = PCI_ERS_RESULT_DISCONNECT;
455 pci_disable_device(pdev);
460 static pci_ers_result_t intel_vsec_pci_slot_reset(struct pci_dev *pdev)
462 struct intel_vsec_device *intel_vsec_dev;
463 pci_ers_result_t status = PCI_ERS_RESULT_DISCONNECT;
464 const struct pci_device_id *pci_dev_id;
467 dev_info(&pdev->dev, "Resetting PCI slot\n");
470 if (pci_enable_device(pdev)) {
472 "Failed to re-enable PCI device after reset.\n");
476 status = PCI_ERS_RESULT_RECOVERED;
478 xa_for_each(&auxdev_array, index, intel_vsec_dev) {
479 /* check if pdev doesn't match */
480 if (pdev != intel_vsec_dev->pcidev)
482 devm_release_action(&pdev->dev, intel_vsec_remove_aux,
483 &intel_vsec_dev->auxdev);
485 pci_disable_device(pdev);
486 pci_restore_state(pdev);
487 pci_dev_id = pci_match_id(intel_vsec_pci_ids, pdev);
488 intel_vsec_pci_probe(pdev, pci_dev_id);
494 static void intel_vsec_pci_resume(struct pci_dev *pdev)
496 dev_info(&pdev->dev, "Done resuming PCI device\n");
499 static const struct pci_error_handlers intel_vsec_pci_err_handlers = {
500 .error_detected = intel_vsec_pci_error_detected,
501 .slot_reset = intel_vsec_pci_slot_reset,
502 .resume = intel_vsec_pci_resume,
505 static struct pci_driver intel_vsec_pci_driver = {
506 .name = "intel_vsec",
507 .id_table = intel_vsec_pci_ids,
508 .probe = intel_vsec_pci_probe,
509 .err_handler = &intel_vsec_pci_err_handlers,
511 module_pci_driver(intel_vsec_pci_driver);
514 MODULE_DESCRIPTION("Intel Extended Capabilities auxiliary bus driver");
515 MODULE_LICENSE("GPL v2");