Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Intel Vendor Specific Extended Capabilities auxiliary bus driver
4 *
5 * Copyright (c) 2021, Intel Corporation.
6 * All Rights Reserved.
7 *
8 * Author: David E. Box <david.e.box@linux.intel.com>
9 *
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.
14 */
15
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>
24
25#include "vsec.h"
26
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)
37
38static DEFINE_IDA(intel_vsec_ida);
39static DEFINE_IDA(intel_vsec_sdsi_ida);
40static DEFINE_XARRAY_ALLOC(auxdev_array);
41
42/**
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
51 */
52struct intel_vsec_header {
53 u8 rev;
54 u16 length;
55 u16 id;
56 u8 num_entries;
57 u8 entry_size;
58 u8 tbir;
59 u32 offset;
60};
61
62enum intel_vsec_id {
63 VSEC_ID_TELEMETRY = 2,
64 VSEC_ID_WATCHER = 3,
65 VSEC_ID_CRASHLOG = 4,
66 VSEC_ID_SDSI = 65,
67};
68
69static enum intel_vsec_id intel_vsec_allow_list[] = {
70 VSEC_ID_TELEMETRY,
71 VSEC_ID_WATCHER,
72 VSEC_ID_CRASHLOG,
73 VSEC_ID_SDSI,
74};
75
76static const char *intel_vsec_name(enum intel_vsec_id id)
77{
78 switch (id) {
79 case VSEC_ID_TELEMETRY:
80 return "telemetry";
81
82 case VSEC_ID_WATCHER:
83 return "watcher";
84
85 case VSEC_ID_CRASHLOG:
86 return "crashlog";
87
88 case VSEC_ID_SDSI:
89 return "sdsi";
90
91 default:
92 return NULL;
93 }
94}
95
96static bool intel_vsec_allowed(u16 id)
97{
98 int i;
99
100 for (i = 0; i < ARRAY_SIZE(intel_vsec_allow_list); i++)
101 if (intel_vsec_allow_list[i] == id)
102 return true;
103
104 return false;
105}
106
107static bool intel_vsec_disabled(u16 id, unsigned long quirks)
108{
109 switch (id) {
110 case VSEC_ID_WATCHER:
111 return !!(quirks & VSEC_QUIRK_NO_WATCHER);
112
113 case VSEC_ID_CRASHLOG:
114 return !!(quirks & VSEC_QUIRK_NO_CRASHLOG);
115
116 default:
117 return false;
118 }
119}
120
121static void intel_vsec_remove_aux(void *data)
122{
123 auxiliary_device_delete(data);
124 auxiliary_device_uninit(data);
125}
126
127static void intel_vsec_dev_release(struct device *dev)
128{
129 struct intel_vsec_device *intel_vsec_dev = dev_to_ivdev(dev);
130
131 ida_free(intel_vsec_dev->ida, intel_vsec_dev->auxdev.id);
132 kfree(intel_vsec_dev->resource);
133 kfree(intel_vsec_dev);
134}
135
136static int intel_vsec_add_aux(struct pci_dev *pdev, struct intel_vsec_device *intel_vsec_dev,
137 const char *name)
138{
139 struct auxiliary_device *auxdev = &intel_vsec_dev->auxdev;
140 int ret, id;
141
142 ret = ida_alloc(intel_vsec_dev->ida, GFP_KERNEL);
143 if (ret < 0) {
144 kfree(intel_vsec_dev);
145 return ret;
146 }
147
148 auxdev->id = ret;
149 auxdev->name = name;
150 auxdev->dev.parent = &pdev->dev;
151 auxdev->dev.release = intel_vsec_dev_release;
152
153 ret = auxiliary_device_init(auxdev);
154 if (ret < 0) {
155 ida_free(intel_vsec_dev->ida, auxdev->id);
156 kfree(intel_vsec_dev->resource);
157 kfree(intel_vsec_dev);
158 return ret;
159 }
160
161 ret = auxiliary_device_add(auxdev);
162 if (ret < 0) {
163 auxiliary_device_uninit(auxdev);
164 return ret;
165 }
166
167 ret = devm_add_action_or_reset(&pdev->dev, intel_vsec_remove_aux,
168 auxdev);
169 if (ret < 0)
170 return ret;
171
172 /* Add auxdev to list */
173 ret = xa_alloc(&auxdev_array, &id, intel_vsec_dev, PMT_XA_LIMIT,
174 GFP_KERNEL);
175 if (ret)
176 return ret;
177
178 return 0;
179}
180
181static int intel_vsec_add_dev(struct pci_dev *pdev, struct intel_vsec_header *header,
182 struct intel_vsec_platform_info *info)
183{
184 struct intel_vsec_device *intel_vsec_dev;
185 struct resource *res, *tmp;
186 unsigned long quirks = info->quirks;
187 int i;
188
189 if (!intel_vsec_allowed(header->id) || intel_vsec_disabled(header->id, quirks))
190 return -EINVAL;
191
192 if (!header->num_entries) {
193 dev_dbg(&pdev->dev, "Invalid 0 entry count for header id %d\n", header->id);
194 return -EINVAL;
195 }
196
197 if (!header->entry_size) {
198 dev_dbg(&pdev->dev, "Invalid 0 entry size for header id %d\n", header->id);
199 return -EINVAL;
200 }
201
202 intel_vsec_dev = kzalloc(sizeof(*intel_vsec_dev), GFP_KERNEL);
203 if (!intel_vsec_dev)
204 return -ENOMEM;
205
206 res = kcalloc(header->num_entries, sizeof(*res), GFP_KERNEL);
207 if (!res) {
208 kfree(intel_vsec_dev);
209 return -ENOMEM;
210 }
211
212 if (quirks & VSEC_QUIRK_TABLE_SHIFT)
213 header->offset >>= TABLE_OFFSET_SHIFT;
214
215 /*
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.
219 */
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;
225 }
226
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;
231
232 if (header->id == VSEC_ID_SDSI)
233 intel_vsec_dev->ida = &intel_vsec_sdsi_ida;
234 else
235 intel_vsec_dev->ida = &intel_vsec_ida;
236
237 return intel_vsec_add_aux(pdev, intel_vsec_dev, intel_vsec_name(header->id));
238}
239
240static bool intel_vsec_walk_header(struct pci_dev *pdev,
241 struct intel_vsec_platform_info *info)
242{
243 struct intel_vsec_header **header = info->capabilities;
244 bool have_devices = false;
245 int ret;
246
247 for ( ; *header; header++) {
248 ret = intel_vsec_add_dev(pdev, *header, info);
249 if (ret)
250 dev_info(&pdev->dev, "Could not add device for DVSEC id %d\n",
251 (*header)->id);
252 else
253 have_devices = true;
254 }
255
256 return have_devices;
257}
258
259static bool intel_vsec_walk_dvsec(struct pci_dev *pdev,
260 struct intel_vsec_platform_info *info)
261{
262 bool have_devices = false;
263 int pos = 0;
264
265 do {
266 struct intel_vsec_header header;
267 u32 table, hdr;
268 u16 vid;
269 int ret;
270
271 pos = pci_find_next_ext_capability(pdev, pos, PCI_EXT_CAP_ID_DVSEC);
272 if (!pos)
273 break;
274
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)
278 continue;
279
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);
284 continue;
285 }
286
287 header.length = PCI_DVSEC_HEADER1_LEN(hdr);
288
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);
292
293 header.tbir = INTEL_DVSEC_TABLE_BAR(table);
294 header.offset = INTEL_DVSEC_TABLE_OFFSET(table);
295
296 pci_read_config_dword(pdev, pos + PCI_DVSEC_HEADER2, &hdr);
297 header.id = PCI_DVSEC_HEADER2_ID(hdr);
298
299 ret = intel_vsec_add_dev(pdev, &header, info);
300 if (ret)
301 continue;
302
303 have_devices = true;
304 } while (true);
305
306 return have_devices;
307}
308
309static bool intel_vsec_walk_vsec(struct pci_dev *pdev,
310 struct intel_vsec_platform_info *info)
311{
312 bool have_devices = false;
313 int pos = 0;
314
315 do {
316 struct intel_vsec_header header;
317 u32 table, hdr;
318 int ret;
319
320 pos = pci_find_next_ext_capability(pdev, pos, PCI_EXT_CAP_ID_VNDR);
321 if (!pos)
322 break;
323
324 pci_read_config_dword(pdev, pos + PCI_VNDR_HEADER, &hdr);
325
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);
330 continue;
331 }
332
333 header.id = PCI_VNDR_HEADER_ID(hdr);
334 header.length = PCI_VNDR_HEADER_LEN(hdr);
335
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);
340
341 header.tbir = INTEL_DVSEC_TABLE_BAR(table);
342 header.offset = INTEL_DVSEC_TABLE_OFFSET(table);
343
344 ret = intel_vsec_add_dev(pdev, &header, info);
345 if (ret)
346 continue;
347
348 have_devices = true;
349 } while (true);
350
351 return have_devices;
352}
353
354static int intel_vsec_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
355{
356 struct intel_vsec_platform_info *info;
357 bool have_devices = false;
358 int ret;
359
360 ret = pcim_enable_device(pdev);
361 if (ret)
362 return ret;
363
364 pci_save_state(pdev);
365 info = (struct intel_vsec_platform_info *)id->driver_data;
366 if (!info)
367 return -EINVAL;
368
369 if (intel_vsec_walk_dvsec(pdev, info))
370 have_devices = true;
371
372 if (intel_vsec_walk_vsec(pdev, info))
373 have_devices = true;
374
375 if (info && (info->quirks & VSEC_QUIRK_NO_DVSEC) &&
376 intel_vsec_walk_header(pdev, info))
377 have_devices = true;
378
379 if (!have_devices)
380 return -ENODEV;
381
382 return 0;
383}
384
385/* TGL info */
386static 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,
389};
390
391/* DG1 info */
392static struct intel_vsec_header dg1_telemetry = {
393 .length = 0x10,
394 .id = 2,
395 .num_entries = 1,
396 .entry_size = 3,
397 .tbir = 0,
398 .offset = 0x466000,
399};
400
401static struct intel_vsec_header *dg1_capabilities[] = {
402 &dg1_telemetry,
403 NULL
404};
405
406static const struct intel_vsec_platform_info dg1_info = {
407 .capabilities = dg1_capabilities,
408 .quirks = VSEC_QUIRK_NO_DVSEC | VSEC_QUIRK_EARLY_HW,
409};
410
411/* MTL info */
412static const struct intel_vsec_platform_info mtl_info = {
413 .quirks = VSEC_QUIRK_NO_WATCHER | VSEC_QUIRK_NO_CRASHLOG,
414};
415
416#define PCI_DEVICE_ID_INTEL_VSEC_ADL 0x467d
417#define PCI_DEVICE_ID_INTEL_VSEC_DG1 0x490e
418#define PCI_DEVICE_ID_INTEL_VSEC_MTL_M 0x7d0d
419#define PCI_DEVICE_ID_INTEL_VSEC_MTL_S 0xad0d
420#define PCI_DEVICE_ID_INTEL_VSEC_OOBMSM 0x09a7
421#define PCI_DEVICE_ID_INTEL_VSEC_RPL 0xa77d
422#define PCI_DEVICE_ID_INTEL_VSEC_TGL 0x9a0d
423static const struct pci_device_id intel_vsec_pci_ids[] = {
424 { PCI_DEVICE_DATA(INTEL, VSEC_ADL, &tgl_info) },
425 { PCI_DEVICE_DATA(INTEL, VSEC_DG1, &dg1_info) },
426 { PCI_DEVICE_DATA(INTEL, VSEC_MTL_M, &mtl_info) },
427 { PCI_DEVICE_DATA(INTEL, VSEC_MTL_S, &mtl_info) },
428 { PCI_DEVICE_DATA(INTEL, VSEC_OOBMSM, &(struct intel_vsec_platform_info) {}) },
429 { PCI_DEVICE_DATA(INTEL, VSEC_RPL, &tgl_info) },
430 { PCI_DEVICE_DATA(INTEL, VSEC_TGL, &tgl_info) },
431 { }
432};
433MODULE_DEVICE_TABLE(pci, intel_vsec_pci_ids);
434
435static pci_ers_result_t intel_vsec_pci_error_detected(struct pci_dev *pdev,
436 pci_channel_state_t state)
437{
438 pci_ers_result_t status = PCI_ERS_RESULT_NEED_RESET;
439
440 dev_info(&pdev->dev, "PCI error detected, state %d", state);
441
442 if (state == pci_channel_io_perm_failure)
443 status = PCI_ERS_RESULT_DISCONNECT;
444 else
445 pci_disable_device(pdev);
446
447 return status;
448}
449
450static pci_ers_result_t intel_vsec_pci_slot_reset(struct pci_dev *pdev)
451{
452 struct intel_vsec_device *intel_vsec_dev;
453 pci_ers_result_t status = PCI_ERS_RESULT_DISCONNECT;
454 const struct pci_device_id *pci_dev_id;
455 unsigned long index;
456
457 dev_info(&pdev->dev, "Resetting PCI slot\n");
458
459 msleep(2000);
460 if (pci_enable_device(pdev)) {
461 dev_info(&pdev->dev,
462 "Failed to re-enable PCI device after reset.\n");
463 goto out;
464 }
465
466 status = PCI_ERS_RESULT_RECOVERED;
467
468 xa_for_each(&auxdev_array, index, intel_vsec_dev) {
469 /* check if pdev doesn't match */
470 if (pdev != intel_vsec_dev->pcidev)
471 continue;
472 devm_release_action(&pdev->dev, intel_vsec_remove_aux,
473 &intel_vsec_dev->auxdev);
474 }
475 pci_disable_device(pdev);
476 pci_restore_state(pdev);
477 pci_dev_id = pci_match_id(intel_vsec_pci_ids, pdev);
478 intel_vsec_pci_probe(pdev, pci_dev_id);
479
480out:
481 return status;
482}
483
484static void intel_vsec_pci_resume(struct pci_dev *pdev)
485{
486 dev_info(&pdev->dev, "Done resuming PCI device\n");
487}
488
489static const struct pci_error_handlers intel_vsec_pci_err_handlers = {
490 .error_detected = intel_vsec_pci_error_detected,
491 .slot_reset = intel_vsec_pci_slot_reset,
492 .resume = intel_vsec_pci_resume,
493};
494
495static struct pci_driver intel_vsec_pci_driver = {
496 .name = "intel_vsec",
497 .id_table = intel_vsec_pci_ids,
498 .probe = intel_vsec_pci_probe,
499 .err_handler = &intel_vsec_pci_err_handlers,
500};
501module_pci_driver(intel_vsec_pci_driver);
502
503MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
504MODULE_DESCRIPTION("Intel Extended Capabilities auxiliary bus driver");
505MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Intel Vendor Specific Extended Capabilities auxiliary bus driver
4 *
5 * Copyright (c) 2021, Intel Corporation.
6 * All Rights Reserved.
7 *
8 * Author: David E. Box <david.e.box@linux.intel.com>
9 *
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.
14 */
15
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>
25
26#include "vsec.h"
27
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)
31
32static DEFINE_IDA(intel_vsec_ida);
33static DEFINE_IDA(intel_vsec_sdsi_ida);
34static DEFINE_XARRAY_ALLOC(auxdev_array);
35
36static const char *intel_vsec_name(enum intel_vsec_id id)
37{
38 switch (id) {
39 case VSEC_ID_TELEMETRY:
40 return "telemetry";
41
42 case VSEC_ID_WATCHER:
43 return "watcher";
44
45 case VSEC_ID_CRASHLOG:
46 return "crashlog";
47
48 case VSEC_ID_SDSI:
49 return "sdsi";
50
51 case VSEC_ID_TPMI:
52 return "tpmi";
53
54 default:
55 return NULL;
56 }
57}
58
59static bool intel_vsec_supported(u16 id, unsigned long caps)
60{
61 switch (id) {
62 case VSEC_ID_TELEMETRY:
63 return !!(caps & VSEC_CAP_TELEMETRY);
64 case VSEC_ID_WATCHER:
65 return !!(caps & VSEC_CAP_WATCHER);
66 case VSEC_ID_CRASHLOG:
67 return !!(caps & VSEC_CAP_CRASHLOG);
68 case VSEC_ID_SDSI:
69 return !!(caps & VSEC_CAP_SDSI);
70 case VSEC_ID_TPMI:
71 return !!(caps & VSEC_CAP_TPMI);
72 default:
73 return false;
74 }
75}
76
77static void intel_vsec_remove_aux(void *data)
78{
79 auxiliary_device_delete(data);
80 auxiliary_device_uninit(data);
81}
82
83static DEFINE_MUTEX(vsec_ida_lock);
84
85static void intel_vsec_dev_release(struct device *dev)
86{
87 struct intel_vsec_device *intel_vsec_dev = dev_to_ivdev(dev);
88
89 xa_erase(&auxdev_array, intel_vsec_dev->id);
90
91 mutex_lock(&vsec_ida_lock);
92 ida_free(intel_vsec_dev->ida, intel_vsec_dev->auxdev.id);
93 mutex_unlock(&vsec_ida_lock);
94
95 kfree(intel_vsec_dev->resource);
96 kfree(intel_vsec_dev);
97}
98
99int intel_vsec_add_aux(struct pci_dev *pdev, struct device *parent,
100 struct intel_vsec_device *intel_vsec_dev,
101 const char *name)
102{
103 struct auxiliary_device *auxdev = &intel_vsec_dev->auxdev;
104 int ret, id;
105
106 if (!parent)
107 return -EINVAL;
108
109 ret = xa_alloc(&auxdev_array, &intel_vsec_dev->id, intel_vsec_dev,
110 PMT_XA_LIMIT, GFP_KERNEL);
111 if (ret < 0) {
112 kfree(intel_vsec_dev->resource);
113 kfree(intel_vsec_dev);
114 return ret;
115 }
116
117 mutex_lock(&vsec_ida_lock);
118 id = ida_alloc(intel_vsec_dev->ida, GFP_KERNEL);
119 mutex_unlock(&vsec_ida_lock);
120 if (id < 0) {
121 xa_erase(&auxdev_array, intel_vsec_dev->id);
122 kfree(intel_vsec_dev->resource);
123 kfree(intel_vsec_dev);
124 return id;
125 }
126
127 auxdev->id = id;
128 auxdev->name = name;
129 auxdev->dev.parent = parent;
130 auxdev->dev.release = intel_vsec_dev_release;
131
132 ret = auxiliary_device_init(auxdev);
133 if (ret < 0) {
134 intel_vsec_dev_release(&auxdev->dev);
135 return ret;
136 }
137
138 ret = auxiliary_device_add(auxdev);
139 if (ret < 0) {
140 auxiliary_device_uninit(auxdev);
141 return ret;
142 }
143
144 return devm_add_action_or_reset(parent, intel_vsec_remove_aux,
145 auxdev);
146}
147EXPORT_SYMBOL_NS_GPL(intel_vsec_add_aux, INTEL_VSEC);
148
149static int intel_vsec_add_dev(struct pci_dev *pdev, struct intel_vsec_header *header,
150 struct intel_vsec_platform_info *info)
151{
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;
157 u64 base_addr;
158 int i;
159
160 if (info->parent)
161 parent = info->parent;
162 else
163 parent = &pdev->dev;
164
165 if (!intel_vsec_supported(header->id, info->caps))
166 return -EINVAL;
167
168 if (!header->num_entries) {
169 dev_dbg(&pdev->dev, "Invalid 0 entry count for header id %d\n", header->id);
170 return -EINVAL;
171 }
172
173 if (!header->entry_size) {
174 dev_dbg(&pdev->dev, "Invalid 0 entry size for header id %d\n", header->id);
175 return -EINVAL;
176 }
177
178 intel_vsec_dev = kzalloc(sizeof(*intel_vsec_dev), GFP_KERNEL);
179 if (!intel_vsec_dev)
180 return -ENOMEM;
181
182 res = kcalloc(header->num_entries, sizeof(*res), GFP_KERNEL);
183 if (!res)
184 return -ENOMEM;
185
186 if (quirks & VSEC_QUIRK_TABLE_SHIFT)
187 header->offset >>= TABLE_OFFSET_SHIFT;
188
189 if (info->base_addr)
190 base_addr = info->base_addr;
191 else
192 base_addr = pdev->resource[header->tbir].start;
193
194 /*
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.
198 */
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;
203
204 /* Check resource is not in use */
205 if (!request_mem_region(tmp->start, resource_size(tmp), ""))
206 return -EBUSY;
207
208 release_mem_region(tmp->start, resource_size(tmp));
209 }
210
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;
216
217 if (header->id == VSEC_ID_SDSI)
218 intel_vsec_dev->ida = &intel_vsec_sdsi_ida;
219 else
220 intel_vsec_dev->ida = &intel_vsec_ida;
221
222 /*
223 * Pass the ownership of intel_vsec_dev and resource within it to
224 * intel_vsec_add_aux()
225 */
226 return intel_vsec_add_aux(pdev, parent, no_free_ptr(intel_vsec_dev),
227 intel_vsec_name(header->id));
228}
229
230static bool intel_vsec_walk_header(struct pci_dev *pdev,
231 struct intel_vsec_platform_info *info)
232{
233 struct intel_vsec_header **header = info->headers;
234 bool have_devices = false;
235 int ret;
236
237 for ( ; *header; header++) {
238 ret = intel_vsec_add_dev(pdev, *header, info);
239 if (ret)
240 dev_info(&pdev->dev, "Could not add device for VSEC id %d\n",
241 (*header)->id);
242 else
243 have_devices = true;
244 }
245
246 return have_devices;
247}
248
249static bool intel_vsec_walk_dvsec(struct pci_dev *pdev,
250 struct intel_vsec_platform_info *info)
251{
252 bool have_devices = false;
253 int pos = 0;
254
255 do {
256 struct intel_vsec_header header;
257 u32 table, hdr;
258 u16 vid;
259 int ret;
260
261 pos = pci_find_next_ext_capability(pdev, pos, PCI_EXT_CAP_ID_DVSEC);
262 if (!pos)
263 break;
264
265 pci_read_config_dword(pdev, pos + PCI_DVSEC_HEADER1, &hdr);
266 vid = PCI_DVSEC_HEADER1_VID(hdr);
267 if (vid != PCI_VENDOR_ID_INTEL)
268 continue;
269
270 /* Support only revision 1 */
271 header.rev = PCI_DVSEC_HEADER1_REV(hdr);
272 if (header.rev != 1) {
273 dev_info(&pdev->dev, "Unsupported DVSEC revision %d\n", header.rev);
274 continue;
275 }
276
277 header.length = PCI_DVSEC_HEADER1_LEN(hdr);
278
279 pci_read_config_byte(pdev, pos + INTEL_DVSEC_ENTRIES, &header.num_entries);
280 pci_read_config_byte(pdev, pos + INTEL_DVSEC_SIZE, &header.entry_size);
281 pci_read_config_dword(pdev, pos + INTEL_DVSEC_TABLE, &table);
282
283 header.tbir = INTEL_DVSEC_TABLE_BAR(table);
284 header.offset = INTEL_DVSEC_TABLE_OFFSET(table);
285
286 pci_read_config_dword(pdev, pos + PCI_DVSEC_HEADER2, &hdr);
287 header.id = PCI_DVSEC_HEADER2_ID(hdr);
288
289 ret = intel_vsec_add_dev(pdev, &header, info);
290 if (ret)
291 continue;
292
293 have_devices = true;
294 } while (true);
295
296 return have_devices;
297}
298
299static bool intel_vsec_walk_vsec(struct pci_dev *pdev,
300 struct intel_vsec_platform_info *info)
301{
302 bool have_devices = false;
303 int pos = 0;
304
305 do {
306 struct intel_vsec_header header;
307 u32 table, hdr;
308 int ret;
309
310 pos = pci_find_next_ext_capability(pdev, pos, PCI_EXT_CAP_ID_VNDR);
311 if (!pos)
312 break;
313
314 pci_read_config_dword(pdev, pos + PCI_VNDR_HEADER, &hdr);
315
316 /* Support only revision 1 */
317 header.rev = PCI_VNDR_HEADER_REV(hdr);
318 if (header.rev != 1) {
319 dev_info(&pdev->dev, "Unsupported VSEC revision %d\n", header.rev);
320 continue;
321 }
322
323 header.id = PCI_VNDR_HEADER_ID(hdr);
324 header.length = PCI_VNDR_HEADER_LEN(hdr);
325
326 /* entry, size, and table offset are the same as DVSEC */
327 pci_read_config_byte(pdev, pos + INTEL_DVSEC_ENTRIES, &header.num_entries);
328 pci_read_config_byte(pdev, pos + INTEL_DVSEC_SIZE, &header.entry_size);
329 pci_read_config_dword(pdev, pos + INTEL_DVSEC_TABLE, &table);
330
331 header.tbir = INTEL_DVSEC_TABLE_BAR(table);
332 header.offset = INTEL_DVSEC_TABLE_OFFSET(table);
333
334 ret = intel_vsec_add_dev(pdev, &header, info);
335 if (ret)
336 continue;
337
338 have_devices = true;
339 } while (true);
340
341 return have_devices;
342}
343
344void intel_vsec_register(struct pci_dev *pdev,
345 struct intel_vsec_platform_info *info)
346{
347 if (!pdev || !info)
348 return;
349
350 intel_vsec_walk_header(pdev, info);
351}
352EXPORT_SYMBOL_NS_GPL(intel_vsec_register, INTEL_VSEC);
353
354static int intel_vsec_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
355{
356 struct intel_vsec_platform_info *info;
357 bool have_devices = false;
358 int ret;
359
360 ret = pcim_enable_device(pdev);
361 if (ret)
362 return ret;
363
364 pci_save_state(pdev);
365 info = (struct intel_vsec_platform_info *)id->driver_data;
366 if (!info)
367 return -EINVAL;
368
369 if (intel_vsec_walk_dvsec(pdev, info))
370 have_devices = true;
371
372 if (intel_vsec_walk_vsec(pdev, info))
373 have_devices = true;
374
375 if (info && (info->quirks & VSEC_QUIRK_NO_DVSEC) &&
376 intel_vsec_walk_header(pdev, info))
377 have_devices = true;
378
379 if (!have_devices)
380 return -ENODEV;
381
382 return 0;
383}
384
385/* DG1 info */
386static struct intel_vsec_header dg1_header = {
387 .length = 0x10,
388 .id = 2,
389 .num_entries = 1,
390 .entry_size = 3,
391 .tbir = 0,
392 .offset = 0x466000,
393};
394
395static struct intel_vsec_header *dg1_headers[] = {
396 &dg1_header,
397 NULL
398};
399
400static const struct intel_vsec_platform_info dg1_info = {
401 .caps = VSEC_CAP_TELEMETRY,
402 .headers = dg1_headers,
403 .quirks = VSEC_QUIRK_NO_DVSEC | VSEC_QUIRK_EARLY_HW,
404};
405
406/* MTL info */
407static const struct intel_vsec_platform_info mtl_info = {
408 .caps = VSEC_CAP_TELEMETRY,
409};
410
411/* OOBMSM info */
412static const struct intel_vsec_platform_info oobmsm_info = {
413 .caps = VSEC_CAP_TELEMETRY | VSEC_CAP_SDSI | VSEC_CAP_TPMI,
414};
415
416/* TGL info */
417static const struct intel_vsec_platform_info tgl_info = {
418 .caps = VSEC_CAP_TELEMETRY,
419 .quirks = VSEC_QUIRK_TABLE_SHIFT | VSEC_QUIRK_EARLY_HW,
420};
421
422/* LNL info */
423static const struct intel_vsec_platform_info lnl_info = {
424 .caps = VSEC_CAP_TELEMETRY | VSEC_CAP_WATCHER,
425};
426
427#define PCI_DEVICE_ID_INTEL_VSEC_ADL 0x467d
428#define PCI_DEVICE_ID_INTEL_VSEC_DG1 0x490e
429#define PCI_DEVICE_ID_INTEL_VSEC_MTL_M 0x7d0d
430#define PCI_DEVICE_ID_INTEL_VSEC_MTL_S 0xad0d
431#define PCI_DEVICE_ID_INTEL_VSEC_OOBMSM 0x09a7
432#define PCI_DEVICE_ID_INTEL_VSEC_RPL 0xa77d
433#define PCI_DEVICE_ID_INTEL_VSEC_TGL 0x9a0d
434#define PCI_DEVICE_ID_INTEL_VSEC_LNL_M 0x647d
435static const struct pci_device_id intel_vsec_pci_ids[] = {
436 { PCI_DEVICE_DATA(INTEL, VSEC_ADL, &tgl_info) },
437 { PCI_DEVICE_DATA(INTEL, VSEC_DG1, &dg1_info) },
438 { PCI_DEVICE_DATA(INTEL, VSEC_MTL_M, &mtl_info) },
439 { PCI_DEVICE_DATA(INTEL, VSEC_MTL_S, &mtl_info) },
440 { PCI_DEVICE_DATA(INTEL, VSEC_OOBMSM, &oobmsm_info) },
441 { PCI_DEVICE_DATA(INTEL, VSEC_RPL, &tgl_info) },
442 { PCI_DEVICE_DATA(INTEL, VSEC_TGL, &tgl_info) },
443 { PCI_DEVICE_DATA(INTEL, VSEC_LNL_M, &lnl_info) },
444 { }
445};
446MODULE_DEVICE_TABLE(pci, intel_vsec_pci_ids);
447
448static pci_ers_result_t intel_vsec_pci_error_detected(struct pci_dev *pdev,
449 pci_channel_state_t state)
450{
451 pci_ers_result_t status = PCI_ERS_RESULT_NEED_RESET;
452
453 dev_info(&pdev->dev, "PCI error detected, state %d", state);
454
455 if (state == pci_channel_io_perm_failure)
456 status = PCI_ERS_RESULT_DISCONNECT;
457 else
458 pci_disable_device(pdev);
459
460 return status;
461}
462
463static pci_ers_result_t intel_vsec_pci_slot_reset(struct pci_dev *pdev)
464{
465 struct intel_vsec_device *intel_vsec_dev;
466 pci_ers_result_t status = PCI_ERS_RESULT_DISCONNECT;
467 const struct pci_device_id *pci_dev_id;
468 unsigned long index;
469
470 dev_info(&pdev->dev, "Resetting PCI slot\n");
471
472 msleep(2000);
473 if (pci_enable_device(pdev)) {
474 dev_info(&pdev->dev,
475 "Failed to re-enable PCI device after reset.\n");
476 goto out;
477 }
478
479 status = PCI_ERS_RESULT_RECOVERED;
480
481 xa_for_each(&auxdev_array, index, intel_vsec_dev) {
482 /* check if pdev doesn't match */
483 if (pdev != intel_vsec_dev->pcidev)
484 continue;
485 devm_release_action(&pdev->dev, intel_vsec_remove_aux,
486 &intel_vsec_dev->auxdev);
487 }
488 pci_disable_device(pdev);
489 pci_restore_state(pdev);
490 pci_dev_id = pci_match_id(intel_vsec_pci_ids, pdev);
491 intel_vsec_pci_probe(pdev, pci_dev_id);
492
493out:
494 return status;
495}
496
497static void intel_vsec_pci_resume(struct pci_dev *pdev)
498{
499 dev_info(&pdev->dev, "Done resuming PCI device\n");
500}
501
502static const struct pci_error_handlers intel_vsec_pci_err_handlers = {
503 .error_detected = intel_vsec_pci_error_detected,
504 .slot_reset = intel_vsec_pci_slot_reset,
505 .resume = intel_vsec_pci_resume,
506};
507
508static struct pci_driver intel_vsec_pci_driver = {
509 .name = "intel_vsec",
510 .id_table = intel_vsec_pci_ids,
511 .probe = intel_vsec_pci_probe,
512 .err_handler = &intel_vsec_pci_err_handlers,
513};
514module_pci_driver(intel_vsec_pci_driver);
515
516MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
517MODULE_DESCRIPTION("Intel Extended Capabilities auxiliary bus driver");
518MODULE_LICENSE("GPL v2");