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/overflow.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
21 #include <linux/sysfs.h>
22 #include <linux/types.h>
23 #include <linux/uaccess.h>
27 #define ACCESS_TYPE_BARID 2
28 #define ACCESS_TYPE_LOCAL 3
30 #define SDSI_MIN_SIZE_DWORDS 276
31 #define SDSI_SIZE_MAILBOX 1024
32 #define SDSI_SIZE_REGS 80
33 #define SDSI_SIZE_CMD sizeof(u64)
36 * Write messages are currently up to the size of the mailbox
37 * while read messages are up to 4 times the size of the
38 * mailbox, sent in packets
40 #define SDSI_SIZE_WRITE_MSG SDSI_SIZE_MAILBOX
41 #define SDSI_SIZE_READ_MSG (SDSI_SIZE_MAILBOX * 4)
43 #define SDSI_ENABLED_FEATURES_OFFSET 16
44 #define SDSI_FEATURE_SDSI BIT(3)
45 #define SDSI_FEATURE_METERING BIT(26)
47 #define SDSI_SOCKET_ID_OFFSET 64
48 #define SDSI_SOCKET_ID GENMASK(3, 0)
50 #define SDSI_MBOX_CMD_SUCCESS 0x40
51 #define SDSI_MBOX_CMD_TIMEOUT 0x80
53 #define MBOX_TIMEOUT_US 500000
54 #define MBOX_TIMEOUT_ACQUIRE_US 1000
55 #define MBOX_POLLING_PERIOD_US 100
56 #define MBOX_ACQUIRE_NUM_RETRIES 5
57 #define MBOX_ACQUIRE_RETRY_DELAY_MS 500
58 #define MBOX_MAX_PACKETS 4
60 #define MBOX_OWNER_NONE 0x00
61 #define MBOX_OWNER_INBAND 0x01
63 #define CTRL_RUN_BUSY BIT(0)
64 #define CTRL_READ_WRITE BIT(1)
65 #define CTRL_SOM BIT(2)
66 #define CTRL_EOM BIT(3)
67 #define CTRL_OWNER GENMASK(5, 4)
68 #define CTRL_COMPLETE BIT(6)
69 #define CTRL_READY BIT(7)
70 #define CTRL_INBAND_LOCK BIT(32)
71 #define CTRL_METER_ENABLE_DRAM BIT(33)
72 #define CTRL_STATUS GENMASK(15, 8)
73 #define CTRL_PACKET_SIZE GENMASK(31, 16)
74 #define CTRL_MSG_SIZE GENMASK(63, 48)
76 #define DISC_TABLE_SIZE 12
77 #define DT_ACCESS_TYPE GENMASK(3, 0)
78 #define DT_SIZE GENMASK(27, 12)
79 #define DT_TBIR GENMASK(2, 0)
80 #define DT_OFFSET(v) ((v) & GENMASK(31, 3))
82 #define SDSI_GUID_V1 0x006DD191
83 #define GUID_V1_CNTRL_SIZE 8
84 #define GUID_V1_REGS_SIZE 72
85 #define SDSI_GUID_V2 0xF210D9EF
86 #define GUID_V2_CNTRL_SIZE 16
87 #define GUID_V2_REGS_SIZE 80
90 SDSI_CMD_PROVISION_AKC = 0x0004,
91 SDSI_CMD_PROVISION_CAP = 0x0008,
92 SDSI_CMD_READ_STATE = 0x0010,
93 SDSI_CMD_READ_METER = 0x0014,
96 struct sdsi_mbox_info {
110 struct mutex mb_lock; /* Mailbox access lock */
112 void __iomem *control_addr;
113 void __iomem *mbox_addr;
114 void __iomem *regs_addr;
122 /* SDSi mailbox operations must be performed using 64bit mov instructions */
123 static __always_inline void
124 sdsi_memcpy64_toio(u64 __iomem *to, const u64 *from, size_t count_bytes)
126 size_t count = count_bytes / sizeof(*to);
129 for (i = 0; i < count; i++)
130 writeq(from[i], &to[i]);
133 static __always_inline void
134 sdsi_memcpy64_fromio(u64 *to, const u64 __iomem *from, size_t count_bytes)
136 size_t count = count_bytes / sizeof(*to);
139 for (i = 0; i < count; i++)
140 to[i] = readq(&from[i]);
143 static inline void sdsi_complete_transaction(struct sdsi_priv *priv)
145 u64 control = FIELD_PREP(CTRL_COMPLETE, 1);
147 lockdep_assert_held(&priv->mb_lock);
148 writeq(control, priv->control_addr);
151 static int sdsi_status_to_errno(u32 status)
154 case SDSI_MBOX_CMD_SUCCESS:
156 case SDSI_MBOX_CMD_TIMEOUT:
163 static int sdsi_mbox_poll(struct sdsi_priv *priv, struct sdsi_mbox_info *info,
166 struct device *dev = priv->dev;
167 u32 total, loop, eom, status, message_size;
171 lockdep_assert_held(&priv->mb_lock);
173 /* For reads, data sizes that are larger than the mailbox size are read in packets. */
179 /* Poll on ready bit */
180 ret = readq_poll_timeout(priv->control_addr, control, control & CTRL_READY,
181 MBOX_POLLING_PERIOD_US, MBOX_TIMEOUT_US);
185 eom = FIELD_GET(CTRL_EOM, control);
186 status = FIELD_GET(CTRL_STATUS, control);
187 packet_size = FIELD_GET(CTRL_PACKET_SIZE, control);
188 message_size = FIELD_GET(CTRL_MSG_SIZE, control);
190 ret = sdsi_status_to_errno(status);
195 sdsi_complete_transaction(priv);
199 /* Only the last packet can be less than the mailbox size. */
200 if (!eom && packet_size != SDSI_SIZE_MAILBOX) {
201 dev_err(dev, "Invalid packet size\n");
206 if (packet_size > SDSI_SIZE_MAILBOX) {
207 dev_err(dev, "Packet size too large\n");
213 void *buf = info->buffer + array_size(SDSI_SIZE_MAILBOX, loop);
215 sdsi_memcpy64_fromio(buf, priv->mbox_addr,
216 round_up(packet_size, SDSI_SIZE_CMD));
217 total += packet_size;
220 sdsi_complete_transaction(priv);
221 } while (!eom && ++loop < MBOX_MAX_PACKETS);
224 sdsi_complete_transaction(priv);
229 dev_err(dev, "Exceeded read attempts\n");
233 /* Message size check is only valid for multi-packet transfers */
234 if (loop && total != message_size)
235 dev_warn(dev, "Read count %u differs from expected count %u\n",
236 total, message_size);
244 static int sdsi_mbox_cmd_read(struct sdsi_priv *priv, struct sdsi_mbox_info *info,
249 lockdep_assert_held(&priv->mb_lock);
251 /* Format and send the read command */
252 control = FIELD_PREP(CTRL_EOM, 1) |
253 FIELD_PREP(CTRL_SOM, 1) |
254 FIELD_PREP(CTRL_RUN_BUSY, 1) |
255 FIELD_PREP(CTRL_PACKET_SIZE, info->size) |
257 writeq(control, priv->control_addr);
259 return sdsi_mbox_poll(priv, info, data_size);
262 static int sdsi_mbox_cmd_write(struct sdsi_priv *priv, struct sdsi_mbox_info *info,
267 lockdep_assert_held(&priv->mb_lock);
269 /* Write rest of the payload */
270 sdsi_memcpy64_toio(priv->mbox_addr + SDSI_SIZE_CMD, info->payload + 1,
271 info->size - SDSI_SIZE_CMD);
273 /* Format and send the write command */
274 control = FIELD_PREP(CTRL_EOM, 1) |
275 FIELD_PREP(CTRL_SOM, 1) |
276 FIELD_PREP(CTRL_RUN_BUSY, 1) |
277 FIELD_PREP(CTRL_READ_WRITE, 1) |
278 FIELD_PREP(CTRL_MSG_SIZE, info->size) |
279 FIELD_PREP(CTRL_PACKET_SIZE, info->size);
280 writeq(control, priv->control_addr);
282 return sdsi_mbox_poll(priv, info, data_size);
285 static int sdsi_mbox_acquire(struct sdsi_priv *priv, struct sdsi_mbox_info *info)
289 int ret, retries = 0;
291 lockdep_assert_held(&priv->mb_lock);
293 /* Check mailbox is available */
294 control = readq(priv->control_addr);
295 owner = FIELD_GET(CTRL_OWNER, control);
296 if (owner != MBOX_OWNER_NONE)
300 * If there has been no recent transaction and no one owns the mailbox,
301 * we should acquire it in under 1ms. However, if we've accessed it
302 * recently it may take up to 2.1 seconds to acquire it again.
305 /* Write first qword of payload */
306 writeq(info->payload[0], priv->mbox_addr);
308 /* Check for ownership */
309 ret = readq_poll_timeout(priv->control_addr, control,
310 FIELD_GET(CTRL_OWNER, control) == MBOX_OWNER_INBAND,
311 MBOX_POLLING_PERIOD_US, MBOX_TIMEOUT_ACQUIRE_US);
313 if (FIELD_GET(CTRL_OWNER, control) == MBOX_OWNER_NONE &&
314 retries++ < MBOX_ACQUIRE_NUM_RETRIES) {
315 msleep(MBOX_ACQUIRE_RETRY_DELAY_MS);
319 /* Either we got it or someone else did. */
326 static int sdsi_mbox_write(struct sdsi_priv *priv, struct sdsi_mbox_info *info,
331 lockdep_assert_held(&priv->mb_lock);
333 ret = sdsi_mbox_acquire(priv, info);
337 return sdsi_mbox_cmd_write(priv, info, data_size);
340 static int sdsi_mbox_read(struct sdsi_priv *priv, struct sdsi_mbox_info *info, size_t *data_size)
344 lockdep_assert_held(&priv->mb_lock);
346 ret = sdsi_mbox_acquire(priv, info);
350 return sdsi_mbox_cmd_read(priv, info, data_size);
353 static bool sdsi_ib_locked(struct sdsi_priv *priv)
355 return !!FIELD_GET(CTRL_INBAND_LOCK, readq(priv->control_addr));
358 static ssize_t sdsi_provision(struct sdsi_priv *priv, char *buf, size_t count,
359 enum sdsi_command command)
361 struct sdsi_mbox_info info = {};
364 if (count > (SDSI_SIZE_WRITE_MSG - SDSI_SIZE_CMD))
367 /* Make sure In-band lock is not set */
368 if (sdsi_ib_locked(priv))
371 /* Qword aligned message + command qword */
372 info.size = round_up(count, SDSI_SIZE_CMD) + SDSI_SIZE_CMD;
374 info.payload = kzalloc(info.size, GFP_KERNEL);
378 /* Copy message to payload buffer */
379 memcpy(info.payload, buf, count);
381 /* Command is last qword of payload buffer */
382 info.payload[(info.size - SDSI_SIZE_CMD) / SDSI_SIZE_CMD] = command;
384 ret = mutex_lock_interruptible(&priv->mb_lock);
388 ret = sdsi_mbox_write(priv, &info, NULL);
390 mutex_unlock(&priv->mb_lock);
401 static ssize_t provision_akc_write(struct file *filp, struct kobject *kobj,
402 struct bin_attribute *attr, char *buf, loff_t off,
405 struct device *dev = kobj_to_dev(kobj);
406 struct sdsi_priv *priv = dev_get_drvdata(dev);
411 return sdsi_provision(priv, buf, count, SDSI_CMD_PROVISION_AKC);
413 static BIN_ATTR_WO(provision_akc, SDSI_SIZE_WRITE_MSG);
415 static ssize_t provision_cap_write(struct file *filp, struct kobject *kobj,
416 struct bin_attribute *attr, char *buf, loff_t off,
419 struct device *dev = kobj_to_dev(kobj);
420 struct sdsi_priv *priv = dev_get_drvdata(dev);
425 return sdsi_provision(priv, buf, count, SDSI_CMD_PROVISION_CAP);
427 static BIN_ATTR_WO(provision_cap, SDSI_SIZE_WRITE_MSG);
430 certificate_read(u64 command, u64 control_flags, struct sdsi_priv *priv,
431 char *buf, loff_t off, size_t count)
433 struct sdsi_mbox_info info = {};
440 /* Buffer for return data */
441 info.buffer = kmalloc(SDSI_SIZE_READ_MSG, GFP_KERNEL);
445 info.payload = &command;
446 info.size = sizeof(command);
447 info.control_flags = control_flags;
449 ret = mutex_lock_interruptible(&priv->mb_lock);
452 ret = sdsi_mbox_read(priv, &info, &size);
453 mutex_unlock(&priv->mb_lock);
460 memcpy(buf, info.buffer, size);
472 state_certificate_read(struct file *filp, struct kobject *kobj,
473 struct bin_attribute *attr, char *buf, loff_t off,
476 struct device *dev = kobj_to_dev(kobj);
477 struct sdsi_priv *priv = dev_get_drvdata(dev);
479 return certificate_read(SDSI_CMD_READ_STATE, 0, priv, buf, off, count);
481 static BIN_ATTR_ADMIN_RO(state_certificate, SDSI_SIZE_READ_MSG);
484 meter_certificate_read(struct file *filp, struct kobject *kobj,
485 struct bin_attribute *attr, char *buf, loff_t off,
488 struct device *dev = kobj_to_dev(kobj);
489 struct sdsi_priv *priv = dev_get_drvdata(dev);
491 return certificate_read(SDSI_CMD_READ_METER, 0, priv, buf, off, count);
493 static BIN_ATTR_ADMIN_RO(meter_certificate, SDSI_SIZE_READ_MSG);
496 meter_current_read(struct file *filp, struct kobject *kobj,
497 struct bin_attribute *attr, char *buf, loff_t off,
500 struct device *dev = kobj_to_dev(kobj);
501 struct sdsi_priv *priv = dev_get_drvdata(dev);
503 return certificate_read(SDSI_CMD_READ_METER, CTRL_METER_ENABLE_DRAM,
504 priv, buf, off, count);
506 static BIN_ATTR_ADMIN_RO(meter_current, SDSI_SIZE_READ_MSG);
508 static ssize_t registers_read(struct file *filp, struct kobject *kobj,
509 struct bin_attribute *attr, char *buf, loff_t off,
512 struct device *dev = kobj_to_dev(kobj);
513 struct sdsi_priv *priv = dev_get_drvdata(dev);
514 void __iomem *addr = priv->regs_addr;
515 int size = priv->registers_size;
518 * The check below is performed by the sysfs caller based on the static
519 * file size. But this may be greater than the actual size which is based
520 * on the GUID. So check here again based on actual size before reading.
525 if (off + count > size)
528 memcpy_fromio(buf, addr + off, count);
532 static BIN_ATTR_ADMIN_RO(registers, SDSI_SIZE_REGS);
534 static struct bin_attribute *sdsi_bin_attrs[] = {
536 &bin_attr_state_certificate,
537 &bin_attr_meter_certificate,
538 &bin_attr_meter_current,
539 &bin_attr_provision_akc,
540 &bin_attr_provision_cap,
545 sdsi_battr_is_visible(struct kobject *kobj, struct bin_attribute *attr, int n)
547 struct device *dev = kobj_to_dev(kobj);
548 struct sdsi_priv *priv = dev_get_drvdata(dev);
550 /* Registers file is always readable if the device is present */
551 if (attr == &bin_attr_registers)
552 return attr->attr.mode;
554 /* All other attributes not visible if BIOS has not enabled On Demand */
555 if (!(priv->features & SDSI_FEATURE_SDSI))
558 if (attr == &bin_attr_meter_certificate || attr == &bin_attr_meter_current)
559 return (priv->features & SDSI_FEATURE_METERING) ?
562 return attr->attr.mode;
565 static ssize_t guid_show(struct device *dev, struct device_attribute *attr, char *buf)
567 struct sdsi_priv *priv = dev_get_drvdata(dev);
569 return sysfs_emit(buf, "0x%x\n", priv->guid);
571 static DEVICE_ATTR_RO(guid);
573 static struct attribute *sdsi_attrs[] = {
578 static const struct attribute_group sdsi_group = {
580 .bin_attrs = sdsi_bin_attrs,
581 .is_bin_visible = sdsi_battr_is_visible,
583 __ATTRIBUTE_GROUPS(sdsi);
585 static int sdsi_get_layout(struct sdsi_priv *priv, struct disc_table *table)
587 switch (table->guid) {
589 priv->control_size = GUID_V1_CNTRL_SIZE;
590 priv->registers_size = GUID_V1_REGS_SIZE;
593 priv->control_size = GUID_V2_CNTRL_SIZE;
594 priv->registers_size = GUID_V2_REGS_SIZE;
597 dev_err(priv->dev, "Unrecognized GUID 0x%x\n", table->guid);
603 static int sdsi_map_mbox_registers(struct sdsi_priv *priv, struct pci_dev *parent,
604 struct disc_table *disc_table, struct resource *disc_res)
606 u32 access_type = FIELD_GET(DT_ACCESS_TYPE, disc_table->access_info);
607 u32 size = FIELD_GET(DT_SIZE, disc_table->access_info);
608 u32 tbir = FIELD_GET(DT_TBIR, disc_table->offset);
609 u32 offset = DT_OFFSET(disc_table->offset);
610 struct resource res = {};
612 /* Starting location of SDSi MMIO region based on access type */
613 switch (access_type) {
614 case ACCESS_TYPE_LOCAL:
616 dev_err(priv->dev, "Unsupported BAR index %u for access type %u\n",
622 * For access_type LOCAL, the base address is as follows:
623 * base address = end of discovery region + base offset + 1
625 res.start = disc_res->end + offset + 1;
628 case ACCESS_TYPE_BARID:
629 res.start = pci_resource_start(parent, tbir) + offset;
633 dev_err(priv->dev, "Unrecognized access_type %u\n", access_type);
637 res.end = res.start + size * sizeof(u32) - 1;
638 res.flags = IORESOURCE_MEM;
640 priv->control_addr = devm_ioremap_resource(priv->dev, &res);
641 if (IS_ERR(priv->control_addr))
642 return PTR_ERR(priv->control_addr);
644 priv->mbox_addr = priv->control_addr + priv->control_size;
645 priv->regs_addr = priv->mbox_addr + SDSI_SIZE_MAILBOX;
647 priv->features = readq(priv->regs_addr + SDSI_ENABLED_FEATURES_OFFSET);
652 static int sdsi_probe(struct auxiliary_device *auxdev, const struct auxiliary_device_id *id)
654 struct intel_vsec_device *intel_cap_dev = auxdev_to_ivdev(auxdev);
655 struct disc_table disc_table;
656 struct resource *disc_res;
657 void __iomem *disc_addr;
658 struct sdsi_priv *priv;
661 priv = devm_kzalloc(&auxdev->dev, sizeof(*priv), GFP_KERNEL);
665 priv->dev = &auxdev->dev;
666 mutex_init(&priv->mb_lock);
667 auxiliary_set_drvdata(auxdev, priv);
669 /* Get the SDSi discovery table */
670 disc_res = &intel_cap_dev->resource[0];
671 disc_addr = devm_ioremap_resource(&auxdev->dev, disc_res);
672 if (IS_ERR(disc_addr))
673 return PTR_ERR(disc_addr);
675 memcpy_fromio(&disc_table, disc_addr, DISC_TABLE_SIZE);
677 priv->guid = disc_table.guid;
679 /* Get guid based layout info */
680 ret = sdsi_get_layout(priv, &disc_table);
684 /* Map the SDSi mailbox registers */
685 ret = sdsi_map_mbox_registers(priv, intel_cap_dev->pcidev, &disc_table, disc_res);
692 static const struct auxiliary_device_id sdsi_aux_id_table[] = {
693 { .name = "intel_vsec.sdsi" },
696 MODULE_DEVICE_TABLE(auxiliary, sdsi_aux_id_table);
698 static struct auxiliary_driver sdsi_aux_driver = {
700 .dev_groups = sdsi_groups,
702 .id_table = sdsi_aux_id_table,
704 /* No remove. All resources are handled under devm */
706 module_auxiliary_driver(sdsi_aux_driver);
709 MODULE_DESCRIPTION("Intel On Demand (SDSi) driver");
710 MODULE_LICENSE("GPL");