1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Driver to enumerate TPMI features and create devices
4 *
5 * Copyright (c) 2023, Intel Corporation.
6 * All Rights Reserved.
7 *
8 * The TPMI (Topology Aware Register and PM Capsule Interface) provides a
9 * flexible, extendable and PCIe enumerable MMIO interface for PM features.
10 *
11 * For example Intel RAPL (Running Average Power Limit) provides a MMIO
12 * interface using TPMI. This has advantage over traditional MSR
13 * (Model Specific Register) interface, where a thread needs to be scheduled
14 * on the target CPU to read or write. Also the RAPL features vary between
15 * CPU models, and hence lot of model specific code. Here TPMI provides an
16 * architectural interface by providing hierarchical tables and fields,
17 * which will not need any model specific implementation.
18 *
19 * The TPMI interface uses a PCI VSEC structure to expose the location of
20 * MMIO region.
21 *
22 * This VSEC structure is present in the PCI configuration space of the
23 * Intel Out-of-Band (OOB) device, which is handled by the Intel VSEC
24 * driver. The Intel VSEC driver parses VSEC structures present in the PCI
25 * configuration space of the given device and creates an auxiliary device
26 * object for each of them. In particular, it creates an auxiliary device
27 * object representing TPMI that can be bound by an auxiliary driver.
28 *
29 * This TPMI driver will bind to the TPMI auxiliary device object created
30 * by the Intel VSEC driver.
31 *
32 * The TPMI specification defines a PFS (PM Feature Structure) table.
33 * This table is present in the TPMI MMIO region. The starting address
34 * of PFS is derived from the tBIR (Bar Indicator Register) and "Address"
35 * field from the VSEC header.
36 *
37 * Each TPMI PM feature has one entry in the PFS with a unique TPMI
38 * ID and its access details. The TPMI driver creates device nodes
39 * for the supported PM features.
40 *
41 * The names of the devices created by the TPMI driver start with the
42 * "intel_vsec.tpmi-" prefix which is followed by a specific name of the
43 * given PM feature (for example, "intel_vsec.tpmi-rapl.0").
44 *
45 * The device nodes are create by using interface "intel_vsec_add_aux()"
46 * provided by the Intel VSEC driver.
47 */
48
49 #include <linux/align.h>
50 #include <linux/auxiliary_bus.h>
51 #include <linux/bitfield.h>
52 #include <linux/debugfs.h>
53 #include <linux/delay.h>
54 #include <linux/intel_tpmi.h>
55 #include <linux/intel_vsec.h>
56 #include <linux/io.h>
57 #include <linux/iopoll.h>
58 #include <linux/module.h>
59 #include <linux/notifier.h>
60 #include <linux/pci.h>
61 #include <linux/security.h>
62 #include <linux/sizes.h>
63 #include <linux/string_helpers.h>
64
65 /**
66 * struct intel_tpmi_pfs_entry - TPMI PM Feature Structure (PFS) entry
67 * @tpmi_id: TPMI feature identifier (what the feature is and its data format).
68 * @num_entries: Number of feature interface instances present in the PFS.
69 * This represents the maximum number of Power domains in the SoC.
70 * @entry_size: Interface instance entry size in 32-bit words.
71 * @cap_offset: Offset from the PM_Features base address to the base of the PM VSEC
72 * register bank in KB.
73 * @attribute: Feature attribute: 0=BIOS. 1=OS. 2-3=Reserved.
74 * @reserved: Bits for use in the future.
75 *
76 * Represents one TPMI feature entry data in the PFS retrieved as is
77 * from the hardware.
78 */
79 struct intel_tpmi_pfs_entry {
80 u64 tpmi_id:8;
81 u64 num_entries:8;
82 u64 entry_size:16;
83 u64 cap_offset:16;
84 u64 attribute:2;
85 u64 reserved:14;
86 } __packed;
87
88 /**
89 * struct intel_tpmi_pm_feature - TPMI PM Feature information for a TPMI ID
90 * @pfs_header: PFS header retireved from the hardware.
91 * @vsec_offset: Starting MMIO address for this feature in bytes. Essentially
92 * this offset = "Address" from VSEC header + PFS Capability
93 * offset for this feature entry.
94 * @vsec_dev: Pointer to intel_vsec_device structure for this TPMI device
95 *
96 * Represents TPMI instance information for one TPMI ID.
97 */
98 struct intel_tpmi_pm_feature {
99 struct intel_tpmi_pfs_entry pfs_header;
100 u64 vsec_offset;
101 struct intel_vsec_device *vsec_dev;
102 };
103
104 /**
105 * struct intel_tpmi_info - TPMI information for all IDs in an instance
106 * @tpmi_features: Pointer to a list of TPMI feature instances
107 * @vsec_dev: Pointer to intel_vsec_device structure for this TPMI device
108 * @feature_count: Number of TPMI of TPMI instances pointed by tpmi_features
109 * @pfs_start: Start of PFS offset for the TPMI instances in this device
110 * @plat_info: Stores platform info which can be used by the client drivers
111 * @tpmi_control_mem: Memory mapped IO for getting control information
112 * @dbgfs_dir: debugfs entry pointer
113 *
114 * Stores the information for all TPMI devices enumerated from a single PCI device.
115 */
116 struct intel_tpmi_info {
117 struct intel_tpmi_pm_feature *tpmi_features;
118 struct intel_vsec_device *vsec_dev;
119 int feature_count;
120 u64 pfs_start;
121 struct oobmsm_plat_info plat_info;
122 void __iomem *tpmi_control_mem;
123 struct dentry *dbgfs_dir;
124 };
125
126 /**
127 * struct tpmi_info_header - CPU package ID to PCI device mapping information
128 * @fn: PCI function number
129 * @dev: PCI device number
130 * @bus: PCI bus number
131 * @pkg: CPU Package id
132 * @segment: PCI segment id
133 * @partition: Package Partition id
134 * @cdie_mask: Bitmap of compute dies in the current partition
135 * @reserved: Reserved for future use
136 * @lock: When set to 1 the register is locked and becomes read-only
137 * until next reset. Not for use by the OS driver.
138 *
139 * The structure to read hardware provided mapping information.
140 */
141 struct tpmi_info_header {
142 u64 fn:3;
143 u64 dev:5;
144 u64 bus:8;
145 u64 pkg:8;
146 u64 segment:8;
147 u64 partition:2;
148 u64 cdie_mask:16;
149 u64 reserved:13;
150 u64 lock:1;
151 } __packed;
152
153 /**
154 * struct tpmi_feature_state - Structure to read hardware state of a feature
155 * @enabled: Enable state of a feature, 1: enabled, 0: disabled
156 * @reserved_1: Reserved for future use
157 * @write_blocked: Writes are blocked means all write operations are ignored
158 * @read_blocked: Reads are blocked means will read 0xFFs
159 * @pcs_select: Interface used by out of band software, not used in OS
160 * @reserved_2: Reserved for future use
161 * @id: TPMI ID of the feature
162 * @reserved_3: Reserved for future use
163 * @locked: When set to 1, OS can't change this register.
164 *
165 * The structure is used to read hardware state of a TPMI feature. This
166 * information is used for debug and restricting operations for this feature.
167 */
168 struct tpmi_feature_state {
169 u32 enabled:1;
170 u32 reserved_1:3;
171 u32 write_blocked:1;
172 u32 read_blocked:1;
173 u32 pcs_select:1;
174 u32 reserved_2:1;
175 u32 id:8;
176 u32 reserved_3:15;
177 u32 locked:1;
178 } __packed;
179
180 /*
181 * The size from hardware is in u32 units. This size is from a trusted hardware,
182 * but better to verify for pre silicon platforms. Set size to 0, when invalid.
183 */
184 #define TPMI_GET_SINGLE_ENTRY_SIZE(pfs) \
185 ({ \
186 pfs->pfs_header.entry_size > SZ_1K ? 0 : pfs->pfs_header.entry_size << 2; \
187 })
188
189 /* Used during auxbus device creation */
190 static DEFINE_IDA(intel_vsec_tpmi_ida);
191
192 static BLOCKING_NOTIFIER_HEAD(tpmi_notify_list);
193
tpmi_register_notifier(struct notifier_block * nb)194 int tpmi_register_notifier(struct notifier_block *nb)
195 {
196 return blocking_notifier_chain_register(&tpmi_notify_list, nb);
197 }
198 EXPORT_SYMBOL_NS_GPL(tpmi_register_notifier, "INTEL_TPMI");
199
tpmi_unregister_notifier(struct notifier_block * nb)200 int tpmi_unregister_notifier(struct notifier_block *nb)
201 {
202 return blocking_notifier_chain_unregister(&tpmi_notify_list, nb);
203 }
204 EXPORT_SYMBOL_NS_GPL(tpmi_unregister_notifier, "INTEL_TPMI");
205
tpmi_get_platform_data(struct auxiliary_device * auxdev)206 struct oobmsm_plat_info *tpmi_get_platform_data(struct auxiliary_device *auxdev)
207 {
208 struct intel_vsec_device *vsec_dev = auxdev_to_ivdev(auxdev);
209
210 return vsec_dev->priv_data;
211 }
212 EXPORT_SYMBOL_NS_GPL(tpmi_get_platform_data, "INTEL_TPMI");
213
tpmi_get_resource_count(struct auxiliary_device * auxdev)214 int tpmi_get_resource_count(struct auxiliary_device *auxdev)
215 {
216 struct intel_vsec_device *vsec_dev = auxdev_to_ivdev(auxdev);
217
218 if (vsec_dev)
219 return vsec_dev->num_resources;
220
221 return 0;
222 }
223 EXPORT_SYMBOL_NS_GPL(tpmi_get_resource_count, "INTEL_TPMI");
224
tpmi_get_resource_at_index(struct auxiliary_device * auxdev,int index)225 struct resource *tpmi_get_resource_at_index(struct auxiliary_device *auxdev, int index)
226 {
227 struct intel_vsec_device *vsec_dev = auxdev_to_ivdev(auxdev);
228
229 if (vsec_dev && index < vsec_dev->num_resources)
230 return &vsec_dev->resource[index];
231
232 return NULL;
233 }
234 EXPORT_SYMBOL_NS_GPL(tpmi_get_resource_at_index, "INTEL_TPMI");
235
236 /* TPMI Control Interface */
237
238 #define TPMI_CONTROL_STATUS_OFFSET 0x00
239 #define TPMI_COMMAND_OFFSET 0x08
240 #define TMPI_CONTROL_DATA_VAL_OFFSET 0x0c
241
242 /*
243 * Spec is calling for max 1 seconds to get ownership at the worst
244 * case. Read at 10 ms timeouts and repeat up to 1 second.
245 */
246 #define TPMI_CONTROL_TIMEOUT_US (10 * USEC_PER_MSEC)
247 #define TPMI_CONTROL_TIMEOUT_MAX_US (1 * USEC_PER_SEC)
248
249 #define TPMI_RB_TIMEOUT_US (10 * USEC_PER_MSEC)
250 #define TPMI_RB_TIMEOUT_MAX_US USEC_PER_SEC
251
252 /* TPMI Control status register defines */
253
254 #define TPMI_CONTROL_STATUS_RB BIT_ULL(0)
255
256 #define TPMI_CONTROL_STATUS_OWNER GENMASK_ULL(5, 4)
257 #define TPMI_OWNER_NONE 0
258 #define TPMI_OWNER_IN_BAND 1
259
260 #define TPMI_CONTROL_STATUS_CPL BIT_ULL(6)
261 #define TPMI_CONTROL_STATUS_RESULT GENMASK_ULL(15, 8)
262 #define TPMI_CONTROL_STATUS_LEN GENMASK_ULL(31, 16)
263
264 #define TPMI_CMD_PKT_LEN 2
265 #define TPMI_CMD_STATUS_SUCCESS 0x40
266
267 /* TPMI command data registers */
268 #define TMPI_CONTROL_DATA_CMD GENMASK_ULL(7, 0)
269 #define TPMI_CONTROL_DATA_VAL_FEATURE GENMASK_ULL(48, 40)
270
271 /* Command to send via control interface */
272 #define TPMI_CONTROL_GET_STATE_CMD 0x10
273
274 #define TPMI_CONTROL_CMD_MASK GENMASK_ULL(48, 40)
275
276 #define TPMI_CMD_LEN_MASK GENMASK_ULL(18, 16)
277
278 /* Mutex to complete get feature status without interruption */
279 static DEFINE_MUTEX(tpmi_dev_lock);
280
tpmi_wait_for_owner(struct intel_tpmi_info * tpmi_info,u8 owner)281 static int tpmi_wait_for_owner(struct intel_tpmi_info *tpmi_info, u8 owner)
282 {
283 u64 control;
284
285 return readq_poll_timeout(tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET,
286 control, owner == FIELD_GET(TPMI_CONTROL_STATUS_OWNER, control),
287 TPMI_CONTROL_TIMEOUT_US, TPMI_CONTROL_TIMEOUT_MAX_US);
288 }
289
tpmi_read_feature_status(struct intel_tpmi_info * tpmi_info,int feature_id,struct tpmi_feature_state * feature_state)290 static int tpmi_read_feature_status(struct intel_tpmi_info *tpmi_info, int feature_id,
291 struct tpmi_feature_state *feature_state)
292 {
293 u64 control, data;
294 int ret;
295
296 if (!tpmi_info->tpmi_control_mem)
297 return -EFAULT;
298
299 mutex_lock(&tpmi_dev_lock);
300
301 /* Wait for owner bit set to 0 (none) */
302 ret = tpmi_wait_for_owner(tpmi_info, TPMI_OWNER_NONE);
303 if (ret)
304 goto err_unlock;
305
306 /* set command id to 0x10 for TPMI_GET_STATE */
307 data = FIELD_PREP(TMPI_CONTROL_DATA_CMD, TPMI_CONTROL_GET_STATE_CMD);
308
309 /* 32 bits for DATA offset and +8 for feature_id field */
310 data |= FIELD_PREP(TPMI_CONTROL_DATA_VAL_FEATURE, feature_id);
311
312 /* Write at command offset for qword access */
313 writeq(data, tpmi_info->tpmi_control_mem + TPMI_COMMAND_OFFSET);
314
315 /* Wait for owner bit set to in-band */
316 ret = tpmi_wait_for_owner(tpmi_info, TPMI_OWNER_IN_BAND);
317 if (ret)
318 goto err_unlock;
319
320 /* Set Run Busy and packet length of 2 dwords */
321 control = TPMI_CONTROL_STATUS_RB;
322 control |= FIELD_PREP(TPMI_CONTROL_STATUS_LEN, TPMI_CMD_PKT_LEN);
323
324 /* Write at status offset for qword access */
325 writeq(control, tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET);
326
327 /* Wait for Run Busy clear */
328 ret = readq_poll_timeout(tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET,
329 control, !(control & TPMI_CONTROL_STATUS_RB),
330 TPMI_RB_TIMEOUT_US, TPMI_RB_TIMEOUT_MAX_US);
331 if (ret)
332 goto done_proc;
333
334 control = FIELD_GET(TPMI_CONTROL_STATUS_RESULT, control);
335 if (control != TPMI_CMD_STATUS_SUCCESS) {
336 ret = -EBUSY;
337 goto done_proc;
338 }
339
340 /* Response is ready */
341 memcpy_fromio(feature_state, tpmi_info->tpmi_control_mem + TMPI_CONTROL_DATA_VAL_OFFSET,
342 sizeof(*feature_state));
343
344 ret = 0;
345
346 done_proc:
347 /* Set CPL "completion" bit */
348 writeq(TPMI_CONTROL_STATUS_CPL, tpmi_info->tpmi_control_mem + TPMI_CONTROL_STATUS_OFFSET);
349
350 err_unlock:
351 mutex_unlock(&tpmi_dev_lock);
352
353 return ret;
354 }
355
tpmi_get_feature_status(struct auxiliary_device * auxdev,int feature_id,bool * read_blocked,bool * write_blocked)356 int tpmi_get_feature_status(struct auxiliary_device *auxdev,
357 int feature_id, bool *read_blocked, bool *write_blocked)
358 {
359 struct intel_vsec_device *intel_vsec_dev = dev_to_ivdev(auxdev->dev.parent);
360 struct intel_tpmi_info *tpmi_info = auxiliary_get_drvdata(&intel_vsec_dev->auxdev);
361 struct tpmi_feature_state feature_state;
362 int ret;
363
364 ret = tpmi_read_feature_status(tpmi_info, feature_id, &feature_state);
365 if (ret)
366 return ret;
367
368 *read_blocked = feature_state.read_blocked;
369 *write_blocked = feature_state.write_blocked;
370
371 return 0;
372 }
373 EXPORT_SYMBOL_NS_GPL(tpmi_get_feature_status, "INTEL_TPMI");
374
tpmi_get_debugfs_dir(struct auxiliary_device * auxdev)375 struct dentry *tpmi_get_debugfs_dir(struct auxiliary_device *auxdev)
376 {
377 struct intel_vsec_device *intel_vsec_dev = dev_to_ivdev(auxdev->dev.parent);
378 struct intel_tpmi_info *tpmi_info = auxiliary_get_drvdata(&intel_vsec_dev->auxdev);
379
380 return tpmi_info->dbgfs_dir;
381 }
382 EXPORT_SYMBOL_NS_GPL(tpmi_get_debugfs_dir, "INTEL_TPMI");
383
tpmi_pfs_dbg_show(struct seq_file * s,void * unused)384 static int tpmi_pfs_dbg_show(struct seq_file *s, void *unused)
385 {
386 struct intel_tpmi_info *tpmi_info = s->private;
387 int locked, disabled, read_blocked, write_blocked;
388 struct tpmi_feature_state feature_state;
389 struct intel_tpmi_pm_feature *pfs;
390 int ret, i;
391
392
393 seq_printf(s, "tpmi PFS start offset 0x:%llx\n", tpmi_info->pfs_start);
394 seq_puts(s, "tpmi_id\t\tentries\t\tsize\t\tcap_offset\tattribute\tvsec_offset\tlocked\tdisabled\tread_blocked\twrite_blocked\n");
395 for (i = 0; i < tpmi_info->feature_count; ++i) {
396 pfs = &tpmi_info->tpmi_features[i];
397 ret = tpmi_read_feature_status(tpmi_info, pfs->pfs_header.tpmi_id, &feature_state);
398 if (ret) {
399 locked = 'U';
400 disabled = 'U';
401 read_blocked = 'U';
402 write_blocked = 'U';
403 } else {
404 disabled = feature_state.enabled ? 'N' : 'Y';
405 locked = feature_state.locked ? 'Y' : 'N';
406 read_blocked = feature_state.read_blocked ? 'Y' : 'N';
407 write_blocked = feature_state.write_blocked ? 'Y' : 'N';
408 }
409 seq_printf(s, "0x%02x\t\t0x%02x\t\t0x%04x\t\t0x%04x\t\t0x%02x\t\t0x%016llx\t%c\t%c\t\t%c\t\t%c\n",
410 pfs->pfs_header.tpmi_id, pfs->pfs_header.num_entries,
411 pfs->pfs_header.entry_size, pfs->pfs_header.cap_offset,
412 pfs->pfs_header.attribute, pfs->vsec_offset, locked, disabled,
413 read_blocked, write_blocked);
414 }
415
416 return 0;
417 }
418 DEFINE_SHOW_ATTRIBUTE(tpmi_pfs_dbg);
419
420 #define MEM_DUMP_COLUMN_COUNT 8
421
tpmi_mem_dump_show(struct seq_file * s,void * unused)422 static int tpmi_mem_dump_show(struct seq_file *s, void *unused)
423 {
424 size_t row_size = MEM_DUMP_COLUMN_COUNT * sizeof(u32);
425 struct intel_tpmi_pm_feature *pfs = s->private;
426 int count, ret = 0;
427 void __iomem *mem;
428 u32 size;
429 u64 off;
430 u8 *buffer;
431
432 size = TPMI_GET_SINGLE_ENTRY_SIZE(pfs);
433 if (!size)
434 return -EIO;
435
436 buffer = kmalloc(size, GFP_KERNEL);
437 if (!buffer)
438 return -ENOMEM;
439
440 off = pfs->vsec_offset;
441
442 mutex_lock(&tpmi_dev_lock);
443
444 for (count = 0; count < pfs->pfs_header.num_entries; ++count) {
445 seq_printf(s, "TPMI Instance:%d offset:0x%llx\n", count, off);
446
447 mem = ioremap(off, size);
448 if (!mem) {
449 ret = -ENOMEM;
450 break;
451 }
452
453 memcpy_fromio(buffer, mem, size);
454
455 seq_hex_dump(s, " ", DUMP_PREFIX_OFFSET, row_size, sizeof(u32), buffer, size,
456 false);
457
458 iounmap(mem);
459
460 off += size;
461 }
462
463 mutex_unlock(&tpmi_dev_lock);
464
465 kfree(buffer);
466
467 return ret;
468 }
469 DEFINE_SHOW_ATTRIBUTE(tpmi_mem_dump);
470
mem_write(struct file * file,const char __user * userbuf,size_t len,loff_t * ppos)471 static ssize_t mem_write(struct file *file, const char __user *userbuf, size_t len, loff_t *ppos)
472 {
473 struct seq_file *m = file->private_data;
474 struct intel_tpmi_pm_feature *pfs = m->private;
475 u32 addr, value, punit, size;
476 u32 num_elems, *array;
477 void __iomem *mem;
478 int ret;
479
480 size = TPMI_GET_SINGLE_ENTRY_SIZE(pfs);
481 if (!size)
482 return -EIO;
483
484 ret = parse_int_array_user(userbuf, len, (int **)&array);
485 if (ret < 0)
486 return ret;
487
488 num_elems = *array;
489 if (num_elems != 3) {
490 ret = -EINVAL;
491 goto exit_write;
492 }
493
494 punit = array[1];
495 addr = array[2];
496 value = array[3];
497
498 if (!IS_ALIGNED(addr, sizeof(u32)))
499 return -EINVAL;
500
501 if (punit >= pfs->pfs_header.num_entries) {
502 ret = -EINVAL;
503 goto exit_write;
504 }
505
506 if (addr >= size) {
507 ret = -EINVAL;
508 goto exit_write;
509 }
510
511 mutex_lock(&tpmi_dev_lock);
512
513 mem = ioremap(pfs->vsec_offset + punit * size, size);
514 if (!mem) {
515 ret = -ENOMEM;
516 goto unlock_mem_write;
517 }
518
519 writel(value, mem + addr);
520
521 iounmap(mem);
522
523 ret = len;
524
525 unlock_mem_write:
526 mutex_unlock(&tpmi_dev_lock);
527
528 exit_write:
529 kfree(array);
530
531 return ret;
532 }
533
mem_write_show(struct seq_file * s,void * unused)534 static int mem_write_show(struct seq_file *s, void *unused)
535 {
536 return 0;
537 }
538
mem_write_open(struct inode * inode,struct file * file)539 static int mem_write_open(struct inode *inode, struct file *file)
540 {
541 return single_open(file, mem_write_show, inode->i_private);
542 }
543
544 static const struct file_operations mem_write_ops = {
545 .open = mem_write_open,
546 .read = seq_read,
547 .write = mem_write,
548 .llseek = seq_lseek,
549 .release = single_release,
550 };
551
552 #define tpmi_to_dev(info) ((info)->vsec_dev->dev)
553
tpmi_dbgfs_register(struct intel_tpmi_info * tpmi_info)554 static void tpmi_dbgfs_register(struct intel_tpmi_info *tpmi_info)
555 {
556 char name[64];
557 int i;
558
559 snprintf(name, sizeof(name), "tpmi-%s", dev_name(tpmi_to_dev(tpmi_info)));
560 tpmi_info->dbgfs_dir = debugfs_create_dir(name, NULL);
561
562 debugfs_create_file("pfs_dump", 0444, tpmi_info->dbgfs_dir, tpmi_info, &tpmi_pfs_dbg_fops);
563
564 for (i = 0; i < tpmi_info->feature_count; ++i) {
565 struct intel_tpmi_pm_feature *pfs;
566 struct dentry *dir;
567
568 pfs = &tpmi_info->tpmi_features[i];
569 snprintf(name, sizeof(name), "tpmi-id-%02x", pfs->pfs_header.tpmi_id);
570 dir = debugfs_create_dir(name, tpmi_info->dbgfs_dir);
571
572 debugfs_create_file("mem_dump", 0444, dir, pfs, &tpmi_mem_dump_fops);
573 debugfs_create_file("mem_write", 0644, dir, pfs, &mem_write_ops);
574 }
575 }
576
tpmi_set_control_base(struct auxiliary_device * auxdev,struct intel_tpmi_info * tpmi_info,struct intel_tpmi_pm_feature * pfs)577 static void tpmi_set_control_base(struct auxiliary_device *auxdev,
578 struct intel_tpmi_info *tpmi_info,
579 struct intel_tpmi_pm_feature *pfs)
580 {
581 void __iomem *mem;
582 u32 size;
583
584 size = TPMI_GET_SINGLE_ENTRY_SIZE(pfs);
585 if (!size)
586 return;
587
588 mem = devm_ioremap(&auxdev->dev, pfs->vsec_offset, size);
589 if (!mem)
590 return;
591
592 /* mem is pointing to TPMI CONTROL base */
593 tpmi_info->tpmi_control_mem = mem;
594 }
595
intel_tpmi_name(enum intel_tpmi_id id)596 static const char *intel_tpmi_name(enum intel_tpmi_id id)
597 {
598 switch (id) {
599 case TPMI_ID_RAPL:
600 return "rapl";
601 case TPMI_ID_PEM:
602 return "pem";
603 case TPMI_ID_UNCORE:
604 return "uncore";
605 case TPMI_ID_SST:
606 return "sst";
607 case TPMI_ID_PLR:
608 return "plr";
609 default:
610 return NULL;
611 }
612 }
613
614 /* String Length for tpmi-"feature_name(upto 8 bytes)" */
615 #define TPMI_FEATURE_NAME_LEN 14
616
tpmi_create_device(struct intel_tpmi_info * tpmi_info,struct intel_tpmi_pm_feature * pfs,u64 pfs_start)617 static int tpmi_create_device(struct intel_tpmi_info *tpmi_info,
618 struct intel_tpmi_pm_feature *pfs,
619 u64 pfs_start)
620 {
621 struct intel_vsec_device *vsec_dev = tpmi_info->vsec_dev;
622 char feature_id_name[TPMI_FEATURE_NAME_LEN];
623 struct intel_vsec_device *feature_vsec_dev;
624 struct tpmi_feature_state feature_state;
625 struct resource *res, *tmp;
626 const char *name;
627 int i, ret;
628
629 ret = tpmi_read_feature_status(tpmi_info, pfs->pfs_header.tpmi_id, &feature_state);
630 if (ret)
631 return ret;
632
633 /*
634 * If not enabled, continue to look at other features in the PFS, so return -EOPNOTSUPP.
635 * This will not cause failure of loading of this driver.
636 */
637 if (!feature_state.enabled)
638 return -EOPNOTSUPP;
639
640 name = intel_tpmi_name(pfs->pfs_header.tpmi_id);
641 if (!name)
642 return -EOPNOTSUPP;
643
644 res = kzalloc_objs(*res, pfs->pfs_header.num_entries);
645 if (!res)
646 return -ENOMEM;
647
648 feature_vsec_dev = kzalloc_obj(*feature_vsec_dev);
649 if (!feature_vsec_dev) {
650 kfree(res);
651 return -ENOMEM;
652 }
653
654 snprintf(feature_id_name, sizeof(feature_id_name), "tpmi-%s", name);
655
656 for (i = 0, tmp = res; i < pfs->pfs_header.num_entries; i++, tmp++) {
657 u64 entry_size_bytes = pfs->pfs_header.entry_size * sizeof(u32);
658
659 tmp->start = pfs->vsec_offset + entry_size_bytes * i;
660 tmp->end = tmp->start + entry_size_bytes - 1;
661 tmp->flags = IORESOURCE_MEM;
662 }
663
664 feature_vsec_dev->dev = vsec_dev->dev;
665 feature_vsec_dev->resource = res;
666 feature_vsec_dev->num_resources = pfs->pfs_header.num_entries;
667 feature_vsec_dev->priv_data = &tpmi_info->plat_info;
668 feature_vsec_dev->priv_data_size = sizeof(tpmi_info->plat_info);
669 feature_vsec_dev->ida = &intel_vsec_tpmi_ida;
670
671 /*
672 * intel_vsec_add_aux() is resource managed, no explicit
673 * delete is required on error or on module unload.
674 * feature_vsec_dev and res memory are also freed as part of
675 * device deletion.
676 */
677 return intel_vsec_add_aux(&vsec_dev->auxdev.dev,
678 feature_vsec_dev, feature_id_name);
679 }
680
tpmi_create_devices(struct intel_tpmi_info * tpmi_info)681 static int tpmi_create_devices(struct intel_tpmi_info *tpmi_info)
682 {
683 struct intel_vsec_device *vsec_dev = tpmi_info->vsec_dev;
684 int ret, i;
685
686 for (i = 0; i < vsec_dev->num_resources; i++) {
687 ret = tpmi_create_device(tpmi_info, &tpmi_info->tpmi_features[i],
688 tpmi_info->pfs_start);
689 /*
690 * Fail, if the supported features fails to create device,
691 * otherwise, continue. Even if one device failed to create,
692 * fail the loading of driver. Since intel_vsec_add_aux()
693 * is resource managed, no clean up is required for the
694 * successfully created devices.
695 */
696 if (ret && ret != -EOPNOTSUPP)
697 return ret;
698 }
699
700 return 0;
701 }
702
703 #define TPMI_INFO_BUS_INFO_OFFSET 0x08
704 #define TPMI_INFO_MAJOR_VERSION 0x00
705 #define TPMI_INFO_MINOR_VERSION 0x02
706
tpmi_process_info(struct intel_tpmi_info * tpmi_info,struct intel_tpmi_pm_feature * pfs)707 static int tpmi_process_info(struct intel_tpmi_info *tpmi_info,
708 struct intel_tpmi_pm_feature *pfs)
709 {
710 struct tpmi_info_header header;
711 void __iomem *info_mem;
712 u64 feature_header;
713 int ret = 0;
714
715 info_mem = ioremap(pfs->vsec_offset, pfs->pfs_header.entry_size * sizeof(u32));
716 if (!info_mem)
717 return -ENOMEM;
718
719 feature_header = readq(info_mem);
720 if (TPMI_MAJOR_VERSION(feature_header) != TPMI_INFO_MAJOR_VERSION) {
721 ret = -ENODEV;
722 goto error_info_header;
723 }
724
725 memcpy_fromio(&header, info_mem + TPMI_INFO_BUS_INFO_OFFSET, sizeof(header));
726
727 tpmi_info->plat_info.package_id = header.pkg;
728 tpmi_info->plat_info.bus_number = header.bus;
729 tpmi_info->plat_info.device_number = header.dev;
730 tpmi_info->plat_info.function_number = header.fn;
731
732 if (TPMI_MINOR_VERSION(feature_header) >= TPMI_INFO_MINOR_VERSION) {
733 tpmi_info->plat_info.cdie_mask = header.cdie_mask;
734 tpmi_info->plat_info.partition = header.partition;
735 tpmi_info->plat_info.segment = header.segment;
736 }
737
738 error_info_header:
739 iounmap(info_mem);
740
741 return ret;
742 }
743
tpmi_fetch_pfs_header(struct intel_tpmi_pm_feature * pfs,u64 start,int size)744 static int tpmi_fetch_pfs_header(struct intel_tpmi_pm_feature *pfs, u64 start, int size)
745 {
746 void __iomem *pfs_mem;
747
748 pfs_mem = ioremap(start, size);
749 if (!pfs_mem)
750 return -ENOMEM;
751
752 memcpy_fromio(&pfs->pfs_header, pfs_mem, sizeof(pfs->pfs_header));
753
754 iounmap(pfs_mem);
755
756 return 0;
757 }
758
759 #define TPMI_CAP_OFFSET_UNIT 1024
760
intel_vsec_tpmi_init(struct auxiliary_device * auxdev)761 static int intel_vsec_tpmi_init(struct auxiliary_device *auxdev)
762 {
763 struct intel_vsec_device *vsec_dev = auxdev_to_ivdev(auxdev);
764 struct pci_dev *pci_dev = to_pci_dev(vsec_dev->dev);
765 struct intel_tpmi_info *tpmi_info;
766 u64 pfs_start = 0;
767 int ret, i;
768
769 tpmi_info = devm_kzalloc(&auxdev->dev, sizeof(*tpmi_info), GFP_KERNEL);
770 if (!tpmi_info)
771 return -ENOMEM;
772
773 tpmi_info->vsec_dev = vsec_dev;
774 tpmi_info->feature_count = vsec_dev->num_resources;
775 tpmi_info->plat_info.bus_number = pci_dev->bus->number;
776
777 tpmi_info->tpmi_features = devm_kcalloc(&auxdev->dev, vsec_dev->num_resources,
778 sizeof(*tpmi_info->tpmi_features),
779 GFP_KERNEL);
780 if (!tpmi_info->tpmi_features)
781 return -ENOMEM;
782
783 for (i = 0; i < vsec_dev->num_resources; i++) {
784 struct intel_tpmi_pm_feature *pfs;
785 struct resource *res;
786 u64 res_start;
787 int size, ret;
788
789 pfs = &tpmi_info->tpmi_features[i];
790 pfs->vsec_dev = vsec_dev;
791
792 res = &vsec_dev->resource[i];
793 if (!res)
794 continue;
795
796 res_start = res->start;
797 size = resource_size(res);
798 if (size < 0)
799 continue;
800
801 ret = tpmi_fetch_pfs_header(pfs, res_start, size);
802 if (ret)
803 continue;
804
805 if (!pfs_start)
806 pfs_start = res_start;
807
808 pfs->vsec_offset = pfs_start + pfs->pfs_header.cap_offset * TPMI_CAP_OFFSET_UNIT;
809
810 /*
811 * Process TPMI_INFO to get PCI device to CPU package ID.
812 * Device nodes for TPMI features are not created in this
813 * for loop. So, the mapping information will be available
814 * when actual device nodes created outside this
815 * loop via tpmi_create_devices().
816 */
817 if (pfs->pfs_header.tpmi_id == TPMI_INFO_ID) {
818 ret = tpmi_process_info(tpmi_info, pfs);
819 if (ret)
820 return ret;
821
822 ret = intel_vsec_set_mapping(&tpmi_info->plat_info, vsec_dev);
823 if (ret)
824 return ret;
825 }
826
827 if (pfs->pfs_header.tpmi_id == TPMI_CONTROL_ID)
828 tpmi_set_control_base(auxdev, tpmi_info, pfs);
829 }
830
831 tpmi_info->pfs_start = pfs_start;
832
833 auxiliary_set_drvdata(auxdev, tpmi_info);
834
835 /*
836 * Allow debugfs when security policy allows. Everything this debugfs
837 * interface provides, can also be done via /dev/mem access. If
838 * /dev/mem interface is locked, don't allow debugfs to present any
839 * information. Also check for CAP_SYS_RAWIO as /dev/mem interface.
840 */
841 if (!security_locked_down(LOCKDOWN_DEV_MEM) && capable(CAP_SYS_RAWIO))
842 tpmi_dbgfs_register(tpmi_info);
843
844 ret = tpmi_create_devices(tpmi_info);
845 if (ret) {
846 debugfs_remove_recursive(tpmi_info->dbgfs_dir);
847 return ret;
848 }
849
850 blocking_notifier_call_chain(&tpmi_notify_list, TPMI_CORE_INIT, auxdev);
851
852 return 0;
853 }
854
tpmi_probe(struct auxiliary_device * auxdev,const struct auxiliary_device_id * id)855 static int tpmi_probe(struct auxiliary_device *auxdev,
856 const struct auxiliary_device_id *id)
857 {
858 return intel_vsec_tpmi_init(auxdev);
859 }
860
tpmi_remove(struct auxiliary_device * auxdev)861 static void tpmi_remove(struct auxiliary_device *auxdev)
862 {
863 struct intel_tpmi_info *tpmi_info = auxiliary_get_drvdata(auxdev);
864
865 blocking_notifier_call_chain(&tpmi_notify_list, TPMI_CORE_EXIT, auxdev);
866
867 debugfs_remove_recursive(tpmi_info->dbgfs_dir);
868 }
869
870 static const struct auxiliary_device_id tpmi_id_table[] = {
871 { .name = "intel_vsec.tpmi" },
872 {}
873 };
874 MODULE_DEVICE_TABLE(auxiliary, tpmi_id_table);
875
876 static struct auxiliary_driver tpmi_aux_driver = {
877 .id_table = tpmi_id_table,
878 .probe = tpmi_probe,
879 .remove = tpmi_remove,
880 };
881
882 module_auxiliary_driver(tpmi_aux_driver);
883
884 MODULE_IMPORT_NS("INTEL_VSEC");
885 MODULE_DESCRIPTION("Intel TPMI enumeration module");
886 MODULE_LICENSE("GPL");
887