1 // SPDX-License-Identifier: GPL-2.0
3 * Intel On Demand (Software Defined Silicon) driver
5 * Copyright (c) 2022, Intel Corporation.
11 #include <linux/auxiliary_bus.h>
12 #include <linux/bits.h>
13 #include <linux/bitfield.h>
14 #include <linux/device.h>
15 #include <linux/iopoll.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/pci.h>
19 #include <linux/slab.h>
20 #include <linux/sysfs.h>
21 #include <linux/types.h>
22 #include <linux/uaccess.h>
26 #define ACCESS_TYPE_BARID 2
27 #define ACCESS_TYPE_LOCAL 3
29 #define SDSI_MIN_SIZE_DWORDS 276
30 #define SDSI_SIZE_MAILBOX 1024
31 #define SDSI_SIZE_REGS 80
32 #define SDSI_SIZE_CMD sizeof(u64)
35 * Write messages are currently up to the size of the mailbox
36 * while read messages are up to 4 times the size of the
37 * mailbox, sent in packets
39 #define SDSI_SIZE_WRITE_MSG SDSI_SIZE_MAILBOX
40 #define SDSI_SIZE_READ_MSG (SDSI_SIZE_MAILBOX * 4)
42 #define SDSI_ENABLED_FEATURES_OFFSET 16
43 #define SDSI_FEATURE_SDSI BIT(3)
44 #define SDSI_FEATURE_METERING BIT(26)
46 #define SDSI_SOCKET_ID_OFFSET 64
47 #define SDSI_SOCKET_ID GENMASK(3, 0)
49 #define SDSI_MBOX_CMD_SUCCESS 0x40
50 #define SDSI_MBOX_CMD_TIMEOUT 0x80
52 #define MBOX_TIMEOUT_US 2000
53 #define MBOX_TIMEOUT_ACQUIRE_US 1000
54 #define MBOX_POLLING_PERIOD_US 100
55 #define MBOX_ACQUIRE_NUM_RETRIES 5
56 #define MBOX_ACQUIRE_RETRY_DELAY_MS 500
57 #define MBOX_MAX_PACKETS 4
59 #define MBOX_OWNER_NONE 0x00
60 #define MBOX_OWNER_INBAND 0x01
62 #define CTRL_RUN_BUSY BIT(0)
63 #define CTRL_READ_WRITE BIT(1)
64 #define CTRL_SOM BIT(2)
65 #define CTRL_EOM BIT(3)
66 #define CTRL_OWNER GENMASK(5, 4)
67 #define CTRL_COMPLETE BIT(6)
68 #define CTRL_READY BIT(7)
69 #define CTRL_STATUS GENMASK(15, 8)
70 #define CTRL_PACKET_SIZE GENMASK(31, 16)
71 #define CTRL_MSG_SIZE GENMASK(63, 48)
73 #define DISC_TABLE_SIZE 12
74 #define DT_ACCESS_TYPE GENMASK(3, 0)
75 #define DT_SIZE GENMASK(27, 12)
76 #define DT_TBIR GENMASK(2, 0)
77 #define DT_OFFSET(v) ((v) & GENMASK(31, 3))
79 #define SDSI_GUID_V1 0x006DD191
80 #define GUID_V1_CNTRL_SIZE 8
81 #define GUID_V1_REGS_SIZE 72
82 #define SDSI_GUID_V2 0xF210D9EF
83 #define GUID_V2_CNTRL_SIZE 16
84 #define GUID_V2_REGS_SIZE 80
87 SDSI_CMD_PROVISION_AKC = 0x0004,
88 SDSI_CMD_PROVISION_CAP = 0x0008,
89 SDSI_CMD_READ_STATE = 0x0010,
90 SDSI_CMD_READ_METER = 0x0014,
93 struct sdsi_mbox_info {
106 struct mutex mb_lock; /* Mailbox access lock */
108 void __iomem *control_addr;
109 void __iomem *mbox_addr;
110 void __iomem *regs_addr;
118 /* SDSi mailbox operations must be performed using 64bit mov instructions */
119 static __always_inline void
120 sdsi_memcpy64_toio(u64 __iomem *to, const u64 *from, size_t count_bytes)
122 size_t count = count_bytes / sizeof(*to);
125 for (i = 0; i < count; i++)
126 writeq(from[i], &to[i]);
129 static __always_inline void
130 sdsi_memcpy64_fromio(u64 *to, const u64 __iomem *from, size_t count_bytes)
132 size_t count = count_bytes / sizeof(*to);
135 for (i = 0; i < count; i++)
136 to[i] = readq(&from[i]);
139 static inline void sdsi_complete_transaction(struct sdsi_priv *priv)
141 u64 control = FIELD_PREP(CTRL_COMPLETE, 1);
143 lockdep_assert_held(&priv->mb_lock);
144 writeq(control, priv->control_addr);
147 static int sdsi_status_to_errno(u32 status)
150 case SDSI_MBOX_CMD_SUCCESS:
152 case SDSI_MBOX_CMD_TIMEOUT:
159 static int sdsi_mbox_cmd_read(struct sdsi_priv *priv, struct sdsi_mbox_info *info,
162 struct device *dev = priv->dev;
163 u32 total, loop, eom, status, message_size;
167 lockdep_assert_held(&priv->mb_lock);
169 /* Format and send the read command */
170 control = FIELD_PREP(CTRL_EOM, 1) |
171 FIELD_PREP(CTRL_SOM, 1) |
172 FIELD_PREP(CTRL_RUN_BUSY, 1) |
173 FIELD_PREP(CTRL_PACKET_SIZE, info->size);
174 writeq(control, priv->control_addr);
176 /* For reads, data sizes that are larger than the mailbox size are read in packets. */
180 void *buf = info->buffer + (SDSI_SIZE_MAILBOX * loop);
183 /* Poll on ready bit */
184 ret = readq_poll_timeout(priv->control_addr, control, control & CTRL_READY,
185 MBOX_POLLING_PERIOD_US, MBOX_TIMEOUT_US);
189 eom = FIELD_GET(CTRL_EOM, control);
190 status = FIELD_GET(CTRL_STATUS, control);
191 packet_size = FIELD_GET(CTRL_PACKET_SIZE, control);
192 message_size = FIELD_GET(CTRL_MSG_SIZE, control);
194 ret = sdsi_status_to_errno(status);
198 /* Only the last packet can be less than the mailbox size. */
199 if (!eom && packet_size != SDSI_SIZE_MAILBOX) {
200 dev_err(dev, "Invalid packet size\n");
205 if (packet_size > SDSI_SIZE_MAILBOX) {
206 dev_err(dev, "Packet size too large\n");
211 sdsi_memcpy64_fromio(buf, priv->mbox_addr, round_up(packet_size, SDSI_SIZE_CMD));
213 total += packet_size;
215 sdsi_complete_transaction(priv);
216 } while (!eom && ++loop < MBOX_MAX_PACKETS);
219 sdsi_complete_transaction(priv);
224 dev_err(dev, "Exceeded read attempts\n");
228 /* Message size check is only valid for multi-packet transfers */
229 if (loop && total != message_size)
230 dev_warn(dev, "Read count %u differs from expected count %u\n",
231 total, message_size);
238 static int sdsi_mbox_cmd_write(struct sdsi_priv *priv, struct sdsi_mbox_info *info)
244 lockdep_assert_held(&priv->mb_lock);
246 /* Write rest of the payload */
247 sdsi_memcpy64_toio(priv->mbox_addr + SDSI_SIZE_CMD, info->payload + 1,
248 info->size - SDSI_SIZE_CMD);
250 /* Format and send the write command */
251 control = FIELD_PREP(CTRL_EOM, 1) |
252 FIELD_PREP(CTRL_SOM, 1) |
253 FIELD_PREP(CTRL_RUN_BUSY, 1) |
254 FIELD_PREP(CTRL_READ_WRITE, 1) |
255 FIELD_PREP(CTRL_PACKET_SIZE, info->size);
256 writeq(control, priv->control_addr);
258 /* Poll on ready bit */
259 ret = readq_poll_timeout(priv->control_addr, control, control & CTRL_READY,
260 MBOX_POLLING_PERIOD_US, MBOX_TIMEOUT_US);
265 status = FIELD_GET(CTRL_STATUS, control);
266 ret = sdsi_status_to_errno(status);
269 sdsi_complete_transaction(priv);
274 static int sdsi_mbox_acquire(struct sdsi_priv *priv, struct sdsi_mbox_info *info)
278 int ret, retries = 0;
280 lockdep_assert_held(&priv->mb_lock);
282 /* Check mailbox is available */
283 control = readq(priv->control_addr);
284 owner = FIELD_GET(CTRL_OWNER, control);
285 if (owner != MBOX_OWNER_NONE)
289 * If there has been no recent transaction and no one owns the mailbox,
290 * we should acquire it in under 1ms. However, if we've accessed it
291 * recently it may take up to 2.1 seconds to acquire it again.
294 /* Write first qword of payload */
295 writeq(info->payload[0], priv->mbox_addr);
297 /* Check for ownership */
298 ret = readq_poll_timeout(priv->control_addr, control,
299 FIELD_GET(CTRL_OWNER, control) == MBOX_OWNER_INBAND,
300 MBOX_POLLING_PERIOD_US, MBOX_TIMEOUT_ACQUIRE_US);
302 if (FIELD_GET(CTRL_OWNER, control) == MBOX_OWNER_NONE &&
303 retries++ < MBOX_ACQUIRE_NUM_RETRIES) {
304 msleep(MBOX_ACQUIRE_RETRY_DELAY_MS);
308 /* Either we got it or someone else did. */
315 static int sdsi_mbox_write(struct sdsi_priv *priv, struct sdsi_mbox_info *info)
319 lockdep_assert_held(&priv->mb_lock);
321 ret = sdsi_mbox_acquire(priv, info);
325 return sdsi_mbox_cmd_write(priv, info);
328 static int sdsi_mbox_read(struct sdsi_priv *priv, struct sdsi_mbox_info *info, size_t *data_size)
332 lockdep_assert_held(&priv->mb_lock);
334 ret = sdsi_mbox_acquire(priv, info);
338 return sdsi_mbox_cmd_read(priv, info, data_size);
341 static ssize_t sdsi_provision(struct sdsi_priv *priv, char *buf, size_t count,
342 enum sdsi_command command)
344 struct sdsi_mbox_info info;
347 if (count > (SDSI_SIZE_WRITE_MSG - SDSI_SIZE_CMD))
350 /* Qword aligned message + command qword */
351 info.size = round_up(count, SDSI_SIZE_CMD) + SDSI_SIZE_CMD;
353 info.payload = kzalloc(info.size, GFP_KERNEL);
357 /* Copy message to payload buffer */
358 memcpy(info.payload, buf, count);
360 /* Command is last qword of payload buffer */
361 info.payload[(info.size - SDSI_SIZE_CMD) / SDSI_SIZE_CMD] = command;
363 ret = mutex_lock_interruptible(&priv->mb_lock);
366 ret = sdsi_mbox_write(priv, &info);
367 mutex_unlock(&priv->mb_lock);
378 static ssize_t provision_akc_write(struct file *filp, struct kobject *kobj,
379 struct bin_attribute *attr, char *buf, loff_t off,
382 struct device *dev = kobj_to_dev(kobj);
383 struct sdsi_priv *priv = dev_get_drvdata(dev);
388 return sdsi_provision(priv, buf, count, SDSI_CMD_PROVISION_AKC);
390 static BIN_ATTR_WO(provision_akc, SDSI_SIZE_WRITE_MSG);
392 static ssize_t provision_cap_write(struct file *filp, struct kobject *kobj,
393 struct bin_attribute *attr, char *buf, loff_t off,
396 struct device *dev = kobj_to_dev(kobj);
397 struct sdsi_priv *priv = dev_get_drvdata(dev);
402 return sdsi_provision(priv, buf, count, SDSI_CMD_PROVISION_CAP);
404 static BIN_ATTR_WO(provision_cap, SDSI_SIZE_WRITE_MSG);
407 certificate_read(u64 command, struct sdsi_priv *priv, char *buf, loff_t off,
410 struct sdsi_mbox_info info;
417 /* Buffer for return data */
418 info.buffer = kmalloc(SDSI_SIZE_READ_MSG, GFP_KERNEL);
422 info.payload = &command;
423 info.size = sizeof(command);
425 ret = mutex_lock_interruptible(&priv->mb_lock);
428 ret = sdsi_mbox_read(priv, &info, &size);
429 mutex_unlock(&priv->mb_lock);
436 memcpy(buf, info.buffer, size);
448 state_certificate_read(struct file *filp, struct kobject *kobj,
449 struct bin_attribute *attr, char *buf, loff_t off,
452 struct device *dev = kobj_to_dev(kobj);
453 struct sdsi_priv *priv = dev_get_drvdata(dev);
455 return certificate_read(SDSI_CMD_READ_STATE, priv, buf, off, count);
457 static BIN_ATTR_ADMIN_RO(state_certificate, SDSI_SIZE_READ_MSG);
460 meter_certificate_read(struct file *filp, struct kobject *kobj,
461 struct bin_attribute *attr, char *buf, loff_t off,
464 struct device *dev = kobj_to_dev(kobj);
465 struct sdsi_priv *priv = dev_get_drvdata(dev);
467 return certificate_read(SDSI_CMD_READ_METER, priv, buf, off, count);
469 static BIN_ATTR_ADMIN_RO(meter_certificate, SDSI_SIZE_READ_MSG);
471 static ssize_t registers_read(struct file *filp, struct kobject *kobj,
472 struct bin_attribute *attr, char *buf, loff_t off,
475 struct device *dev = kobj_to_dev(kobj);
476 struct sdsi_priv *priv = dev_get_drvdata(dev);
477 void __iomem *addr = priv->regs_addr;
478 int size = priv->registers_size;
481 * The check below is performed by the sysfs caller based on the static
482 * file size. But this may be greater than the actual size which is based
483 * on the GUID. So check here again based on actual size before reading.
488 if (off + count > size)
491 memcpy_fromio(buf, addr + off, count);
495 static BIN_ATTR_ADMIN_RO(registers, SDSI_SIZE_REGS);
497 static struct bin_attribute *sdsi_bin_attrs[] = {
499 &bin_attr_state_certificate,
500 &bin_attr_meter_certificate,
501 &bin_attr_provision_akc,
502 &bin_attr_provision_cap,
507 sdsi_battr_is_visible(struct kobject *kobj, struct bin_attribute *attr, int n)
509 struct device *dev = kobj_to_dev(kobj);
510 struct sdsi_priv *priv = dev_get_drvdata(dev);
512 /* Registers file is always readable if the device is present */
513 if (attr == &bin_attr_registers)
514 return attr->attr.mode;
516 /* All other attributes not visible if BIOS has not enabled On Demand */
517 if (!(priv->features & SDSI_FEATURE_SDSI))
520 if (attr == &bin_attr_meter_certificate)
521 return (priv->features & SDSI_FEATURE_METERING) ?
524 return attr->attr.mode;
527 static ssize_t guid_show(struct device *dev, struct device_attribute *attr, char *buf)
529 struct sdsi_priv *priv = dev_get_drvdata(dev);
531 return sysfs_emit(buf, "0x%x\n", priv->guid);
533 static DEVICE_ATTR_RO(guid);
535 static struct attribute *sdsi_attrs[] = {
540 static const struct attribute_group sdsi_group = {
542 .bin_attrs = sdsi_bin_attrs,
543 .is_bin_visible = sdsi_battr_is_visible,
545 __ATTRIBUTE_GROUPS(sdsi);
547 static int sdsi_get_layout(struct sdsi_priv *priv, struct disc_table *table)
549 switch (table->guid) {
551 priv->control_size = GUID_V1_CNTRL_SIZE;
552 priv->registers_size = GUID_V1_REGS_SIZE;
555 priv->control_size = GUID_V2_CNTRL_SIZE;
556 priv->registers_size = GUID_V2_REGS_SIZE;
559 dev_err(priv->dev, "Unrecognized GUID 0x%x\n", table->guid);
565 static int sdsi_map_mbox_registers(struct sdsi_priv *priv, struct pci_dev *parent,
566 struct disc_table *disc_table, struct resource *disc_res)
568 u32 access_type = FIELD_GET(DT_ACCESS_TYPE, disc_table->access_info);
569 u32 size = FIELD_GET(DT_SIZE, disc_table->access_info);
570 u32 tbir = FIELD_GET(DT_TBIR, disc_table->offset);
571 u32 offset = DT_OFFSET(disc_table->offset);
572 struct resource res = {};
574 /* Starting location of SDSi MMIO region based on access type */
575 switch (access_type) {
576 case ACCESS_TYPE_LOCAL:
578 dev_err(priv->dev, "Unsupported BAR index %u for access type %u\n",
584 * For access_type LOCAL, the base address is as follows:
585 * base address = end of discovery region + base offset + 1
587 res.start = disc_res->end + offset + 1;
590 case ACCESS_TYPE_BARID:
591 res.start = pci_resource_start(parent, tbir) + offset;
595 dev_err(priv->dev, "Unrecognized access_type %u\n", access_type);
599 res.end = res.start + size * sizeof(u32) - 1;
600 res.flags = IORESOURCE_MEM;
602 priv->control_addr = devm_ioremap_resource(priv->dev, &res);
603 if (IS_ERR(priv->control_addr))
604 return PTR_ERR(priv->control_addr);
606 priv->mbox_addr = priv->control_addr + priv->control_size;
607 priv->regs_addr = priv->mbox_addr + SDSI_SIZE_MAILBOX;
609 priv->features = readq(priv->regs_addr + SDSI_ENABLED_FEATURES_OFFSET);
614 static int sdsi_probe(struct auxiliary_device *auxdev, const struct auxiliary_device_id *id)
616 struct intel_vsec_device *intel_cap_dev = auxdev_to_ivdev(auxdev);
617 struct disc_table disc_table;
618 struct resource *disc_res;
619 void __iomem *disc_addr;
620 struct sdsi_priv *priv;
623 priv = devm_kzalloc(&auxdev->dev, sizeof(*priv), GFP_KERNEL);
627 priv->dev = &auxdev->dev;
628 mutex_init(&priv->mb_lock);
629 auxiliary_set_drvdata(auxdev, priv);
631 /* Get the SDSi discovery table */
632 disc_res = &intel_cap_dev->resource[0];
633 disc_addr = devm_ioremap_resource(&auxdev->dev, disc_res);
634 if (IS_ERR(disc_addr))
635 return PTR_ERR(disc_addr);
637 memcpy_fromio(&disc_table, disc_addr, DISC_TABLE_SIZE);
639 priv->guid = disc_table.guid;
641 /* Get guid based layout info */
642 ret = sdsi_get_layout(priv, &disc_table);
646 /* Map the SDSi mailbox registers */
647 ret = sdsi_map_mbox_registers(priv, intel_cap_dev->pcidev, &disc_table, disc_res);
654 static const struct auxiliary_device_id sdsi_aux_id_table[] = {
655 { .name = "intel_vsec.sdsi" },
658 MODULE_DEVICE_TABLE(auxiliary, sdsi_aux_id_table);
660 static struct auxiliary_driver sdsi_aux_driver = {
662 .dev_groups = sdsi_groups,
664 .id_table = sdsi_aux_id_table,
666 /* No remove. All resources are handled under devm */
668 module_auxiliary_driver(sdsi_aux_driver);
671 MODULE_DESCRIPTION("Intel On Demand (SDSi) driver");
672 MODULE_LICENSE("GPL");