1 // SPDX-License-Identifier: GPL-2.0
3 * Intel 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_CONTROL 8
31 #define SDSI_SIZE_MAILBOX 1024
32 #define SDSI_SIZE_REGS 72
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_ENABLED BIT(3)
45 #define SDSI_SOCKET_ID_OFFSET 64
46 #define SDSI_SOCKET_ID GENMASK(3, 0)
48 #define SDSI_MBOX_CMD_SUCCESS 0x40
49 #define SDSI_MBOX_CMD_TIMEOUT 0x80
51 #define MBOX_TIMEOUT_US 2000
52 #define MBOX_TIMEOUT_ACQUIRE_US 1000
53 #define MBOX_POLLING_PERIOD_US 100
54 #define MBOX_ACQUIRE_NUM_RETRIES 5
55 #define MBOX_ACQUIRE_RETRY_DELAY_MS 500
56 #define MBOX_MAX_PACKETS 4
58 #define MBOX_OWNER_NONE 0x00
59 #define MBOX_OWNER_INBAND 0x01
61 #define CTRL_RUN_BUSY BIT(0)
62 #define CTRL_READ_WRITE BIT(1)
63 #define CTRL_SOM BIT(2)
64 #define CTRL_EOM BIT(3)
65 #define CTRL_OWNER GENMASK(5, 4)
66 #define CTRL_COMPLETE BIT(6)
67 #define CTRL_READY BIT(7)
68 #define CTRL_STATUS GENMASK(15, 8)
69 #define CTRL_PACKET_SIZE GENMASK(31, 16)
70 #define CTRL_MSG_SIZE GENMASK(63, 48)
72 #define DISC_TABLE_SIZE 12
73 #define DT_ACCESS_TYPE GENMASK(3, 0)
74 #define DT_SIZE GENMASK(27, 12)
75 #define DT_TBIR GENMASK(2, 0)
76 #define DT_OFFSET(v) ((v) & GENMASK(31, 3))
79 SDSI_CMD_PROVISION_AKC = 0x04,
80 SDSI_CMD_PROVISION_CAP = 0x08,
81 SDSI_CMD_READ_STATE = 0x10,
84 struct sdsi_mbox_info {
97 struct mutex mb_lock; /* Mailbox access lock */
99 void __iomem *control_addr;
100 void __iomem *mbox_addr;
101 void __iomem *regs_addr;
106 /* SDSi mailbox operations must be performed using 64bit mov instructions */
107 static __always_inline void
108 sdsi_memcpy64_toio(u64 __iomem *to, const u64 *from, size_t count_bytes)
110 size_t count = count_bytes / sizeof(*to);
113 for (i = 0; i < count; i++)
114 writeq(from[i], &to[i]);
117 static __always_inline void
118 sdsi_memcpy64_fromio(u64 *to, const u64 __iomem *from, size_t count_bytes)
120 size_t count = count_bytes / sizeof(*to);
123 for (i = 0; i < count; i++)
124 to[i] = readq(&from[i]);
127 static inline void sdsi_complete_transaction(struct sdsi_priv *priv)
129 u64 control = FIELD_PREP(CTRL_COMPLETE, 1);
131 lockdep_assert_held(&priv->mb_lock);
132 writeq(control, priv->control_addr);
135 static int sdsi_status_to_errno(u32 status)
138 case SDSI_MBOX_CMD_SUCCESS:
140 case SDSI_MBOX_CMD_TIMEOUT:
147 static int sdsi_mbox_cmd_read(struct sdsi_priv *priv, struct sdsi_mbox_info *info,
150 struct device *dev = priv->dev;
151 u32 total, loop, eom, status, message_size;
155 lockdep_assert_held(&priv->mb_lock);
157 /* Format and send the read command */
158 control = FIELD_PREP(CTRL_EOM, 1) |
159 FIELD_PREP(CTRL_SOM, 1) |
160 FIELD_PREP(CTRL_RUN_BUSY, 1) |
161 FIELD_PREP(CTRL_PACKET_SIZE, info->size);
162 writeq(control, priv->control_addr);
164 /* For reads, data sizes that are larger than the mailbox size are read in packets. */
168 void *buf = info->buffer + (SDSI_SIZE_MAILBOX * loop);
171 /* Poll on ready bit */
172 ret = readq_poll_timeout(priv->control_addr, control, control & CTRL_READY,
173 MBOX_POLLING_PERIOD_US, MBOX_TIMEOUT_US);
177 eom = FIELD_GET(CTRL_EOM, control);
178 status = FIELD_GET(CTRL_STATUS, control);
179 packet_size = FIELD_GET(CTRL_PACKET_SIZE, control);
180 message_size = FIELD_GET(CTRL_MSG_SIZE, control);
182 ret = sdsi_status_to_errno(status);
186 /* Only the last packet can be less than the mailbox size. */
187 if (!eom && packet_size != SDSI_SIZE_MAILBOX) {
188 dev_err(dev, "Invalid packet size\n");
193 if (packet_size > SDSI_SIZE_MAILBOX) {
194 dev_err(dev, "Packet size too large\n");
199 sdsi_memcpy64_fromio(buf, priv->mbox_addr, round_up(packet_size, SDSI_SIZE_CMD));
201 total += packet_size;
203 sdsi_complete_transaction(priv);
204 } while (!eom && ++loop < MBOX_MAX_PACKETS);
207 sdsi_complete_transaction(priv);
212 dev_err(dev, "Exceeded read attempts\n");
216 /* Message size check is only valid for multi-packet transfers */
217 if (loop && total != message_size)
218 dev_warn(dev, "Read count %u differs from expected count %u\n",
219 total, message_size);
226 static int sdsi_mbox_cmd_write(struct sdsi_priv *priv, struct sdsi_mbox_info *info)
232 lockdep_assert_held(&priv->mb_lock);
234 /* Write rest of the payload */
235 sdsi_memcpy64_toio(priv->mbox_addr + SDSI_SIZE_CMD, info->payload + 1,
236 info->size - SDSI_SIZE_CMD);
238 /* Format and send the write command */
239 control = FIELD_PREP(CTRL_EOM, 1) |
240 FIELD_PREP(CTRL_SOM, 1) |
241 FIELD_PREP(CTRL_RUN_BUSY, 1) |
242 FIELD_PREP(CTRL_READ_WRITE, 1) |
243 FIELD_PREP(CTRL_PACKET_SIZE, info->size);
244 writeq(control, priv->control_addr);
246 /* Poll on ready bit */
247 ret = readq_poll_timeout(priv->control_addr, control, control & CTRL_READY,
248 MBOX_POLLING_PERIOD_US, MBOX_TIMEOUT_US);
253 status = FIELD_GET(CTRL_STATUS, control);
254 ret = sdsi_status_to_errno(status);
257 sdsi_complete_transaction(priv);
262 static int sdsi_mbox_acquire(struct sdsi_priv *priv, struct sdsi_mbox_info *info)
266 int ret, retries = 0;
268 lockdep_assert_held(&priv->mb_lock);
270 /* Check mailbox is available */
271 control = readq(priv->control_addr);
272 owner = FIELD_GET(CTRL_OWNER, control);
273 if (owner != MBOX_OWNER_NONE)
277 * If there has been no recent transaction and no one owns the mailbox,
278 * we should acquire it in under 1ms. However, if we've accessed it
279 * recently it may take up to 2.1 seconds to acquire it again.
282 /* Write first qword of payload */
283 writeq(info->payload[0], priv->mbox_addr);
285 /* Check for ownership */
286 ret = readq_poll_timeout(priv->control_addr, control,
287 FIELD_GET(CTRL_OWNER, control) == MBOX_OWNER_INBAND,
288 MBOX_POLLING_PERIOD_US, MBOX_TIMEOUT_ACQUIRE_US);
290 if (FIELD_GET(CTRL_OWNER, control) == MBOX_OWNER_NONE &&
291 retries++ < MBOX_ACQUIRE_NUM_RETRIES) {
292 msleep(MBOX_ACQUIRE_RETRY_DELAY_MS);
296 /* Either we got it or someone else did. */
303 static int sdsi_mbox_write(struct sdsi_priv *priv, struct sdsi_mbox_info *info)
307 lockdep_assert_held(&priv->mb_lock);
309 ret = sdsi_mbox_acquire(priv, info);
313 return sdsi_mbox_cmd_write(priv, info);
316 static int sdsi_mbox_read(struct sdsi_priv *priv, struct sdsi_mbox_info *info, size_t *data_size)
320 lockdep_assert_held(&priv->mb_lock);
322 ret = sdsi_mbox_acquire(priv, info);
326 return sdsi_mbox_cmd_read(priv, info, data_size);
329 static ssize_t sdsi_provision(struct sdsi_priv *priv, char *buf, size_t count,
330 enum sdsi_command command)
332 struct sdsi_mbox_info info;
335 if (!priv->sdsi_enabled)
338 if (count > (SDSI_SIZE_WRITE_MSG - SDSI_SIZE_CMD))
341 /* Qword aligned message + command qword */
342 info.size = round_up(count, SDSI_SIZE_CMD) + SDSI_SIZE_CMD;
344 info.payload = kzalloc(info.size, GFP_KERNEL);
348 /* Copy message to payload buffer */
349 memcpy(info.payload, buf, count);
351 /* Command is last qword of payload buffer */
352 info.payload[(info.size - SDSI_SIZE_CMD) / SDSI_SIZE_CMD] = command;
354 ret = mutex_lock_interruptible(&priv->mb_lock);
357 ret = sdsi_mbox_write(priv, &info);
358 mutex_unlock(&priv->mb_lock);
369 static ssize_t provision_akc_write(struct file *filp, struct kobject *kobj,
370 struct bin_attribute *attr, char *buf, loff_t off,
373 struct device *dev = kobj_to_dev(kobj);
374 struct sdsi_priv *priv = dev_get_drvdata(dev);
379 return sdsi_provision(priv, buf, count, SDSI_CMD_PROVISION_AKC);
381 static BIN_ATTR_WO(provision_akc, SDSI_SIZE_WRITE_MSG);
383 static ssize_t provision_cap_write(struct file *filp, struct kobject *kobj,
384 struct bin_attribute *attr, char *buf, loff_t off,
387 struct device *dev = kobj_to_dev(kobj);
388 struct sdsi_priv *priv = dev_get_drvdata(dev);
393 return sdsi_provision(priv, buf, count, SDSI_CMD_PROVISION_CAP);
395 static BIN_ATTR_WO(provision_cap, SDSI_SIZE_WRITE_MSG);
397 static long state_certificate_read(struct file *filp, struct kobject *kobj,
398 struct bin_attribute *attr, char *buf, loff_t off,
401 struct device *dev = kobj_to_dev(kobj);
402 struct sdsi_priv *priv = dev_get_drvdata(dev);
403 u64 command = SDSI_CMD_READ_STATE;
404 struct sdsi_mbox_info info;
408 if (!priv->sdsi_enabled)
414 /* Buffer for return data */
415 info.buffer = kmalloc(SDSI_SIZE_READ_MSG, GFP_KERNEL);
419 info.payload = &command;
420 info.size = sizeof(command);
422 ret = mutex_lock_interruptible(&priv->mb_lock);
425 ret = sdsi_mbox_read(priv, &info, &size);
426 mutex_unlock(&priv->mb_lock);
433 memcpy(buf, info.buffer, size);
443 static BIN_ATTR(state_certificate, 0400, state_certificate_read, NULL, SDSI_SIZE_READ_MSG);
445 static ssize_t registers_read(struct file *filp, struct kobject *kobj,
446 struct bin_attribute *attr, char *buf, loff_t off,
449 struct device *dev = kobj_to_dev(kobj);
450 struct sdsi_priv *priv = dev_get_drvdata(dev);
451 void __iomem *addr = priv->regs_addr;
453 memcpy_fromio(buf, addr + off, count);
457 static BIN_ATTR(registers, 0400, registers_read, NULL, SDSI_SIZE_REGS);
459 static struct bin_attribute *sdsi_bin_attrs[] = {
461 &bin_attr_state_certificate,
462 &bin_attr_provision_akc,
463 &bin_attr_provision_cap,
467 static ssize_t guid_show(struct device *dev, struct device_attribute *attr, char *buf)
469 struct sdsi_priv *priv = dev_get_drvdata(dev);
471 return sysfs_emit(buf, "0x%x\n", priv->guid);
473 static DEVICE_ATTR_RO(guid);
475 static struct attribute *sdsi_attrs[] = {
480 static const struct attribute_group sdsi_group = {
482 .bin_attrs = sdsi_bin_attrs,
484 __ATTRIBUTE_GROUPS(sdsi);
486 static int sdsi_map_mbox_registers(struct sdsi_priv *priv, struct pci_dev *parent,
487 struct disc_table *disc_table, struct resource *disc_res)
489 u32 access_type = FIELD_GET(DT_ACCESS_TYPE, disc_table->access_info);
490 u32 size = FIELD_GET(DT_SIZE, disc_table->access_info);
491 u32 tbir = FIELD_GET(DT_TBIR, disc_table->offset);
492 u32 offset = DT_OFFSET(disc_table->offset);
494 struct resource res = {};
496 /* Starting location of SDSi MMIO region based on access type */
497 switch (access_type) {
498 case ACCESS_TYPE_LOCAL:
500 dev_err(priv->dev, "Unsupported BAR index %u for access type %u\n",
506 * For access_type LOCAL, the base address is as follows:
507 * base address = end of discovery region + base offset + 1
509 res.start = disc_res->end + offset + 1;
512 case ACCESS_TYPE_BARID:
513 res.start = pci_resource_start(parent, tbir) + offset;
517 dev_err(priv->dev, "Unrecognized access_type %u\n", access_type);
521 res.end = res.start + size * sizeof(u32) - 1;
522 res.flags = IORESOURCE_MEM;
524 priv->control_addr = devm_ioremap_resource(priv->dev, &res);
525 if (IS_ERR(priv->control_addr))
526 return PTR_ERR(priv->control_addr);
528 priv->mbox_addr = priv->control_addr + SDSI_SIZE_CONTROL;
529 priv->regs_addr = priv->mbox_addr + SDSI_SIZE_MAILBOX;
531 features_offset = readq(priv->regs_addr + SDSI_ENABLED_FEATURES_OFFSET);
532 priv->sdsi_enabled = !!(features_offset & SDSI_ENABLED);
537 static int sdsi_probe(struct auxiliary_device *auxdev, const struct auxiliary_device_id *id)
539 struct intel_vsec_device *intel_cap_dev = auxdev_to_ivdev(auxdev);
540 struct disc_table disc_table;
541 struct resource *disc_res;
542 void __iomem *disc_addr;
543 struct sdsi_priv *priv;
546 priv = devm_kzalloc(&auxdev->dev, sizeof(*priv), GFP_KERNEL);
550 priv->dev = &auxdev->dev;
551 mutex_init(&priv->mb_lock);
552 auxiliary_set_drvdata(auxdev, priv);
554 /* Get the SDSi discovery table */
555 disc_res = &intel_cap_dev->resource[0];
556 disc_addr = devm_ioremap_resource(&auxdev->dev, disc_res);
557 if (IS_ERR(disc_addr))
558 return PTR_ERR(disc_addr);
560 memcpy_fromio(&disc_table, disc_addr, DISC_TABLE_SIZE);
562 priv->guid = disc_table.guid;
564 /* Map the SDSi mailbox registers */
565 ret = sdsi_map_mbox_registers(priv, intel_cap_dev->pcidev, &disc_table, disc_res);
572 static const struct auxiliary_device_id sdsi_aux_id_table[] = {
573 { .name = "intel_vsec.sdsi" },
576 MODULE_DEVICE_TABLE(auxiliary, sdsi_aux_id_table);
578 static struct auxiliary_driver sdsi_aux_driver = {
580 .dev_groups = sdsi_groups,
582 .id_table = sdsi_aux_id_table,
584 /* No remove. All resources are handled under devm */
586 module_auxiliary_driver(sdsi_aux_driver);
589 MODULE_DESCRIPTION("Intel Software Defined Silicon driver");
590 MODULE_LICENSE("GPL");