1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2020 Intel Corporation. */
4 #include <linux/io-64-nonatomic-lo-hi.h>
5 #include <linux/firmware.h>
6 #include <linux/device.h>
7 #include <linux/slab.h>
14 static DECLARE_RWSEM(cxl_memdev_rwsem
);
17 * An entire PCI topology full of devices should be enough for any
20 #define CXL_MEM_MAX_DEVS 65536
22 static int cxl_mem_major
;
23 static DEFINE_IDA(cxl_memdev_ida
);
25 static void cxl_memdev_release(struct device
*dev
)
27 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
29 ida_free(&cxl_memdev_ida
, cxlmd
->id
);
33 static char *cxl_memdev_devnode(const struct device
*dev
, umode_t
*mode
, kuid_t
*uid
,
36 return kasprintf(GFP_KERNEL
, "cxl/%s", dev_name(dev
));
39 static ssize_t
firmware_version_show(struct device
*dev
,
40 struct device_attribute
*attr
, char *buf
)
42 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
43 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
44 struct cxl_memdev_state
*mds
= to_cxl_memdev_state(cxlds
);
47 return sysfs_emit(buf
, "\n");
48 return sysfs_emit(buf
, "%.16s\n", mds
->firmware_version
);
50 static DEVICE_ATTR_RO(firmware_version
);
52 static ssize_t
payload_max_show(struct device
*dev
,
53 struct device_attribute
*attr
, char *buf
)
55 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
56 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
57 struct cxl_memdev_state
*mds
= to_cxl_memdev_state(cxlds
);
60 return sysfs_emit(buf
, "\n");
61 return sysfs_emit(buf
, "%zu\n", cxlds
->cxl_mbox
.payload_size
);
63 static DEVICE_ATTR_RO(payload_max
);
65 static ssize_t
label_storage_size_show(struct device
*dev
,
66 struct device_attribute
*attr
, char *buf
)
68 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
69 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
70 struct cxl_memdev_state
*mds
= to_cxl_memdev_state(cxlds
);
73 return sysfs_emit(buf
, "\n");
74 return sysfs_emit(buf
, "%zu\n", mds
->lsa_size
);
76 static DEVICE_ATTR_RO(label_storage_size
);
78 static ssize_t
ram_size_show(struct device
*dev
, struct device_attribute
*attr
,
81 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
82 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
83 unsigned long long len
= resource_size(&cxlds
->ram_res
);
85 return sysfs_emit(buf
, "%#llx\n", len
);
88 static struct device_attribute dev_attr_ram_size
=
89 __ATTR(size
, 0444, ram_size_show
, NULL
);
91 static ssize_t
pmem_size_show(struct device
*dev
, struct device_attribute
*attr
,
94 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
95 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
96 unsigned long long len
= resource_size(&cxlds
->pmem_res
);
98 return sysfs_emit(buf
, "%#llx\n", len
);
101 static struct device_attribute dev_attr_pmem_size
=
102 __ATTR(size
, 0444, pmem_size_show
, NULL
);
104 static ssize_t
serial_show(struct device
*dev
, struct device_attribute
*attr
,
107 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
108 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
110 return sysfs_emit(buf
, "%#llx\n", cxlds
->serial
);
112 static DEVICE_ATTR_RO(serial
);
114 static ssize_t
numa_node_show(struct device
*dev
, struct device_attribute
*attr
,
117 return sysfs_emit(buf
, "%d\n", dev_to_node(dev
));
119 static DEVICE_ATTR_RO(numa_node
);
121 static ssize_t
security_state_show(struct device
*dev
,
122 struct device_attribute
*attr
,
125 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
126 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
127 struct cxl_mailbox
*cxl_mbox
= &cxlds
->cxl_mbox
;
128 struct cxl_memdev_state
*mds
= to_cxl_memdev_state(cxlds
);
129 unsigned long state
= mds
->security
.state
;
132 /* sync with latest submission state */
133 mutex_lock(&cxl_mbox
->mbox_mutex
);
134 if (mds
->security
.sanitize_active
)
135 rc
= sysfs_emit(buf
, "sanitize\n");
136 mutex_unlock(&cxl_mbox
->mbox_mutex
);
140 if (!(state
& CXL_PMEM_SEC_STATE_USER_PASS_SET
))
141 return sysfs_emit(buf
, "disabled\n");
142 if (state
& CXL_PMEM_SEC_STATE_FROZEN
||
143 state
& CXL_PMEM_SEC_STATE_MASTER_PLIMIT
||
144 state
& CXL_PMEM_SEC_STATE_USER_PLIMIT
)
145 return sysfs_emit(buf
, "frozen\n");
146 if (state
& CXL_PMEM_SEC_STATE_LOCKED
)
147 return sysfs_emit(buf
, "locked\n");
149 return sysfs_emit(buf
, "unlocked\n");
151 static struct device_attribute dev_attr_security_state
=
152 __ATTR(state
, 0444, security_state_show
, NULL
);
154 static ssize_t
security_sanitize_store(struct device
*dev
,
155 struct device_attribute
*attr
,
156 const char *buf
, size_t len
)
158 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
162 if (kstrtobool(buf
, &sanitize
) || !sanitize
)
165 rc
= cxl_mem_sanitize(cxlmd
, CXL_MBOX_OP_SANITIZE
);
171 static struct device_attribute dev_attr_security_sanitize
=
172 __ATTR(sanitize
, 0200, NULL
, security_sanitize_store
);
174 static ssize_t
security_erase_store(struct device
*dev
,
175 struct device_attribute
*attr
,
176 const char *buf
, size_t len
)
178 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
182 if (kstrtobool(buf
, &erase
) || !erase
)
185 rc
= cxl_mem_sanitize(cxlmd
, CXL_MBOX_OP_SECURE_ERASE
);
191 static struct device_attribute dev_attr_security_erase
=
192 __ATTR(erase
, 0200, NULL
, security_erase_store
);
194 static int cxl_get_poison_by_memdev(struct cxl_memdev
*cxlmd
)
196 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
200 /* CXL 3.0 Spec 8.2.9.8.4.1 Separate pmem and ram poison requests */
201 if (resource_size(&cxlds
->pmem_res
)) {
202 offset
= cxlds
->pmem_res
.start
;
203 length
= resource_size(&cxlds
->pmem_res
);
204 rc
= cxl_mem_get_poison(cxlmd
, offset
, length
, NULL
);
208 if (resource_size(&cxlds
->ram_res
)) {
209 offset
= cxlds
->ram_res
.start
;
210 length
= resource_size(&cxlds
->ram_res
);
211 rc
= cxl_mem_get_poison(cxlmd
, offset
, length
, NULL
);
213 * Invalid Physical Address is not an error for
214 * volatile addresses. Device support is optional.
222 int cxl_trigger_poison_list(struct cxl_memdev
*cxlmd
)
224 struct cxl_port
*port
;
227 port
= cxlmd
->endpoint
;
228 if (!port
|| !is_cxl_endpoint(port
))
231 rc
= down_read_interruptible(&cxl_region_rwsem
);
235 rc
= down_read_interruptible(&cxl_dpa_rwsem
);
237 up_read(&cxl_region_rwsem
);
241 if (cxl_num_decoders_committed(port
) == 0) {
242 /* No regions mapped to this memdev */
243 rc
= cxl_get_poison_by_memdev(cxlmd
);
245 /* Regions mapped, collect poison by endpoint */
246 rc
= cxl_get_poison_by_endpoint(port
);
248 up_read(&cxl_dpa_rwsem
);
249 up_read(&cxl_region_rwsem
);
253 EXPORT_SYMBOL_NS_GPL(cxl_trigger_poison_list
, "CXL");
255 static int cxl_validate_poison_dpa(struct cxl_memdev
*cxlmd
, u64 dpa
)
257 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
259 if (!IS_ENABLED(CONFIG_DEBUG_FS
))
262 if (!resource_size(&cxlds
->dpa_res
)) {
263 dev_dbg(cxlds
->dev
, "device has no dpa resource\n");
266 if (dpa
< cxlds
->dpa_res
.start
|| dpa
> cxlds
->dpa_res
.end
) {
267 dev_dbg(cxlds
->dev
, "dpa:0x%llx not in resource:%pR\n",
268 dpa
, &cxlds
->dpa_res
);
271 if (!IS_ALIGNED(dpa
, 64)) {
272 dev_dbg(cxlds
->dev
, "dpa:0x%llx is not 64-byte aligned\n", dpa
);
279 int cxl_inject_poison(struct cxl_memdev
*cxlmd
, u64 dpa
)
281 struct cxl_mailbox
*cxl_mbox
= &cxlmd
->cxlds
->cxl_mbox
;
282 struct cxl_mbox_inject_poison inject
;
283 struct cxl_poison_record record
;
284 struct cxl_mbox_cmd mbox_cmd
;
285 struct cxl_region
*cxlr
;
288 if (!IS_ENABLED(CONFIG_DEBUG_FS
))
291 rc
= down_read_interruptible(&cxl_region_rwsem
);
295 rc
= down_read_interruptible(&cxl_dpa_rwsem
);
297 up_read(&cxl_region_rwsem
);
301 rc
= cxl_validate_poison_dpa(cxlmd
, dpa
);
305 inject
.address
= cpu_to_le64(dpa
);
306 mbox_cmd
= (struct cxl_mbox_cmd
) {
307 .opcode
= CXL_MBOX_OP_INJECT_POISON
,
308 .size_in
= sizeof(inject
),
309 .payload_in
= &inject
,
311 rc
= cxl_internal_send_cmd(cxl_mbox
, &mbox_cmd
);
315 cxlr
= cxl_dpa_to_region(cxlmd
, dpa
);
317 dev_warn_once(cxl_mbox
->host
,
318 "poison inject dpa:%#llx region: %s\n", dpa
,
319 dev_name(&cxlr
->dev
));
321 record
= (struct cxl_poison_record
) {
322 .address
= cpu_to_le64(dpa
),
323 .length
= cpu_to_le32(1),
325 trace_cxl_poison(cxlmd
, cxlr
, &record
, 0, 0, CXL_POISON_TRACE_INJECT
);
327 up_read(&cxl_dpa_rwsem
);
328 up_read(&cxl_region_rwsem
);
332 EXPORT_SYMBOL_NS_GPL(cxl_inject_poison
, "CXL");
334 int cxl_clear_poison(struct cxl_memdev
*cxlmd
, u64 dpa
)
336 struct cxl_mailbox
*cxl_mbox
= &cxlmd
->cxlds
->cxl_mbox
;
337 struct cxl_mbox_clear_poison clear
;
338 struct cxl_poison_record record
;
339 struct cxl_mbox_cmd mbox_cmd
;
340 struct cxl_region
*cxlr
;
343 if (!IS_ENABLED(CONFIG_DEBUG_FS
))
346 rc
= down_read_interruptible(&cxl_region_rwsem
);
350 rc
= down_read_interruptible(&cxl_dpa_rwsem
);
352 up_read(&cxl_region_rwsem
);
356 rc
= cxl_validate_poison_dpa(cxlmd
, dpa
);
361 * In CXL 3.0 Spec 8.2.9.8.4.3, the Clear Poison mailbox command
362 * is defined to accept 64 bytes of write-data, along with the
363 * address to clear. This driver uses zeroes as write-data.
365 clear
= (struct cxl_mbox_clear_poison
) {
366 .address
= cpu_to_le64(dpa
)
369 mbox_cmd
= (struct cxl_mbox_cmd
) {
370 .opcode
= CXL_MBOX_OP_CLEAR_POISON
,
371 .size_in
= sizeof(clear
),
372 .payload_in
= &clear
,
375 rc
= cxl_internal_send_cmd(cxl_mbox
, &mbox_cmd
);
379 cxlr
= cxl_dpa_to_region(cxlmd
, dpa
);
381 dev_warn_once(cxl_mbox
->host
,
382 "poison clear dpa:%#llx region: %s\n", dpa
,
383 dev_name(&cxlr
->dev
));
385 record
= (struct cxl_poison_record
) {
386 .address
= cpu_to_le64(dpa
),
387 .length
= cpu_to_le32(1),
389 trace_cxl_poison(cxlmd
, cxlr
, &record
, 0, 0, CXL_POISON_TRACE_CLEAR
);
391 up_read(&cxl_dpa_rwsem
);
392 up_read(&cxl_region_rwsem
);
396 EXPORT_SYMBOL_NS_GPL(cxl_clear_poison
, "CXL");
398 static struct attribute
*cxl_memdev_attributes
[] = {
399 &dev_attr_serial
.attr
,
400 &dev_attr_firmware_version
.attr
,
401 &dev_attr_payload_max
.attr
,
402 &dev_attr_label_storage_size
.attr
,
403 &dev_attr_numa_node
.attr
,
407 static ssize_t
pmem_qos_class_show(struct device
*dev
,
408 struct device_attribute
*attr
, char *buf
)
410 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
411 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
412 struct cxl_memdev_state
*mds
= to_cxl_memdev_state(cxlds
);
414 return sysfs_emit(buf
, "%d\n", mds
->pmem_perf
.qos_class
);
417 static struct device_attribute dev_attr_pmem_qos_class
=
418 __ATTR(qos_class
, 0444, pmem_qos_class_show
, NULL
);
420 static struct attribute
*cxl_memdev_pmem_attributes
[] = {
421 &dev_attr_pmem_size
.attr
,
422 &dev_attr_pmem_qos_class
.attr
,
426 static ssize_t
ram_qos_class_show(struct device
*dev
,
427 struct device_attribute
*attr
, char *buf
)
429 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
430 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
431 struct cxl_memdev_state
*mds
= to_cxl_memdev_state(cxlds
);
433 return sysfs_emit(buf
, "%d\n", mds
->ram_perf
.qos_class
);
436 static struct device_attribute dev_attr_ram_qos_class
=
437 __ATTR(qos_class
, 0444, ram_qos_class_show
, NULL
);
439 static struct attribute
*cxl_memdev_ram_attributes
[] = {
440 &dev_attr_ram_size
.attr
,
441 &dev_attr_ram_qos_class
.attr
,
445 static struct attribute
*cxl_memdev_security_attributes
[] = {
446 &dev_attr_security_state
.attr
,
447 &dev_attr_security_sanitize
.attr
,
448 &dev_attr_security_erase
.attr
,
452 static umode_t
cxl_memdev_visible(struct kobject
*kobj
, struct attribute
*a
,
455 if (!IS_ENABLED(CONFIG_NUMA
) && a
== &dev_attr_numa_node
.attr
)
460 static struct attribute_group cxl_memdev_attribute_group
= {
461 .attrs
= cxl_memdev_attributes
,
462 .is_visible
= cxl_memdev_visible
,
465 static umode_t
cxl_ram_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
467 struct device
*dev
= kobj_to_dev(kobj
);
468 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
469 struct cxl_memdev_state
*mds
= to_cxl_memdev_state(cxlmd
->cxlds
);
471 if (a
== &dev_attr_ram_qos_class
.attr
)
472 if (mds
->ram_perf
.qos_class
== CXL_QOS_CLASS_INVALID
)
478 static struct attribute_group cxl_memdev_ram_attribute_group
= {
480 .attrs
= cxl_memdev_ram_attributes
,
481 .is_visible
= cxl_ram_visible
,
484 static umode_t
cxl_pmem_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
486 struct device
*dev
= kobj_to_dev(kobj
);
487 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
488 struct cxl_memdev_state
*mds
= to_cxl_memdev_state(cxlmd
->cxlds
);
490 if (a
== &dev_attr_pmem_qos_class
.attr
)
491 if (mds
->pmem_perf
.qos_class
== CXL_QOS_CLASS_INVALID
)
497 static struct attribute_group cxl_memdev_pmem_attribute_group
= {
499 .attrs
= cxl_memdev_pmem_attributes
,
500 .is_visible
= cxl_pmem_visible
,
503 static umode_t
cxl_memdev_security_visible(struct kobject
*kobj
,
504 struct attribute
*a
, int n
)
506 struct device
*dev
= kobj_to_dev(kobj
);
507 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
508 struct cxl_memdev_state
*mds
= to_cxl_memdev_state(cxlmd
->cxlds
);
510 if (a
== &dev_attr_security_sanitize
.attr
&&
511 !test_bit(CXL_SEC_ENABLED_SANITIZE
, mds
->security
.enabled_cmds
))
514 if (a
== &dev_attr_security_erase
.attr
&&
515 !test_bit(CXL_SEC_ENABLED_SECURE_ERASE
, mds
->security
.enabled_cmds
))
521 static struct attribute_group cxl_memdev_security_attribute_group
= {
523 .attrs
= cxl_memdev_security_attributes
,
524 .is_visible
= cxl_memdev_security_visible
,
527 static const struct attribute_group
*cxl_memdev_attribute_groups
[] = {
528 &cxl_memdev_attribute_group
,
529 &cxl_memdev_ram_attribute_group
,
530 &cxl_memdev_pmem_attribute_group
,
531 &cxl_memdev_security_attribute_group
,
535 void cxl_memdev_update_perf(struct cxl_memdev
*cxlmd
)
537 sysfs_update_group(&cxlmd
->dev
.kobj
, &cxl_memdev_ram_attribute_group
);
538 sysfs_update_group(&cxlmd
->dev
.kobj
, &cxl_memdev_pmem_attribute_group
);
540 EXPORT_SYMBOL_NS_GPL(cxl_memdev_update_perf
, "CXL");
542 static const struct device_type cxl_memdev_type
= {
543 .name
= "cxl_memdev",
544 .release
= cxl_memdev_release
,
545 .devnode
= cxl_memdev_devnode
,
546 .groups
= cxl_memdev_attribute_groups
,
549 bool is_cxl_memdev(const struct device
*dev
)
551 return dev
->type
== &cxl_memdev_type
;
553 EXPORT_SYMBOL_NS_GPL(is_cxl_memdev
, "CXL");
556 * set_exclusive_cxl_commands() - atomically disable user cxl commands
557 * @mds: The device state to operate on
558 * @cmds: bitmap of commands to mark exclusive
560 * Grab the cxl_memdev_rwsem in write mode to flush in-flight
561 * invocations of the ioctl path and then disable future execution of
562 * commands with the command ids set in @cmds.
564 void set_exclusive_cxl_commands(struct cxl_memdev_state
*mds
,
567 down_write(&cxl_memdev_rwsem
);
568 bitmap_or(mds
->exclusive_cmds
, mds
->exclusive_cmds
, cmds
,
569 CXL_MEM_COMMAND_ID_MAX
);
570 up_write(&cxl_memdev_rwsem
);
572 EXPORT_SYMBOL_NS_GPL(set_exclusive_cxl_commands
, "CXL");
575 * clear_exclusive_cxl_commands() - atomically enable user cxl commands
576 * @mds: The device state to modify
577 * @cmds: bitmap of commands to mark available for userspace
579 void clear_exclusive_cxl_commands(struct cxl_memdev_state
*mds
,
582 down_write(&cxl_memdev_rwsem
);
583 bitmap_andnot(mds
->exclusive_cmds
, mds
->exclusive_cmds
, cmds
,
584 CXL_MEM_COMMAND_ID_MAX
);
585 up_write(&cxl_memdev_rwsem
);
587 EXPORT_SYMBOL_NS_GPL(clear_exclusive_cxl_commands
, "CXL");
589 static void cxl_memdev_shutdown(struct device
*dev
)
591 struct cxl_memdev
*cxlmd
= to_cxl_memdev(dev
);
593 down_write(&cxl_memdev_rwsem
);
595 up_write(&cxl_memdev_rwsem
);
598 static void cxl_memdev_unregister(void *_cxlmd
)
600 struct cxl_memdev
*cxlmd
= _cxlmd
;
601 struct device
*dev
= &cxlmd
->dev
;
603 cdev_device_del(&cxlmd
->cdev
, dev
);
604 cxl_memdev_shutdown(dev
);
608 static void detach_memdev(struct work_struct
*work
)
610 struct cxl_memdev
*cxlmd
;
612 cxlmd
= container_of(work
, typeof(*cxlmd
), detach_work
);
613 device_release_driver(&cxlmd
->dev
);
614 put_device(&cxlmd
->dev
);
617 static struct lock_class_key cxl_memdev_key
;
619 static struct cxl_memdev
*cxl_memdev_alloc(struct cxl_dev_state
*cxlds
,
620 const struct file_operations
*fops
)
622 struct cxl_memdev
*cxlmd
;
627 cxlmd
= kzalloc(sizeof(*cxlmd
), GFP_KERNEL
);
629 return ERR_PTR(-ENOMEM
);
631 rc
= ida_alloc_max(&cxl_memdev_ida
, CXL_MEM_MAX_DEVS
- 1, GFP_KERNEL
);
638 device_initialize(dev
);
639 lockdep_set_class(&dev
->mutex
, &cxl_memdev_key
);
640 dev
->parent
= cxlds
->dev
;
641 dev
->bus
= &cxl_bus_type
;
642 dev
->devt
= MKDEV(cxl_mem_major
, cxlmd
->id
);
643 dev
->type
= &cxl_memdev_type
;
644 device_set_pm_not_required(dev
);
645 INIT_WORK(&cxlmd
->detach_work
, detach_memdev
);
648 cdev_init(cdev
, fops
);
656 static long __cxl_memdev_ioctl(struct cxl_memdev
*cxlmd
, unsigned int cmd
,
660 case CXL_MEM_QUERY_COMMANDS
:
661 return cxl_query_cmd(cxlmd
, (void __user
*)arg
);
662 case CXL_MEM_SEND_COMMAND
:
663 return cxl_send_cmd(cxlmd
, (void __user
*)arg
);
669 static long cxl_memdev_ioctl(struct file
*file
, unsigned int cmd
,
672 struct cxl_memdev
*cxlmd
= file
->private_data
;
673 struct cxl_dev_state
*cxlds
;
676 down_read(&cxl_memdev_rwsem
);
677 cxlds
= cxlmd
->cxlds
;
678 if (cxlds
&& cxlds
->type
== CXL_DEVTYPE_CLASSMEM
)
679 rc
= __cxl_memdev_ioctl(cxlmd
, cmd
, arg
);
680 up_read(&cxl_memdev_rwsem
);
685 static int cxl_memdev_open(struct inode
*inode
, struct file
*file
)
687 struct cxl_memdev
*cxlmd
=
688 container_of(inode
->i_cdev
, typeof(*cxlmd
), cdev
);
690 get_device(&cxlmd
->dev
);
691 file
->private_data
= cxlmd
;
696 static int cxl_memdev_release_file(struct inode
*inode
, struct file
*file
)
698 struct cxl_memdev
*cxlmd
=
699 container_of(inode
->i_cdev
, typeof(*cxlmd
), cdev
);
701 put_device(&cxlmd
->dev
);
707 * cxl_mem_get_fw_info - Get Firmware info
708 * @mds: The device data for the operation
710 * Retrieve firmware info for the device specified.
712 * Return: 0 if no error: or the result of the mailbox command.
714 * See CXL-3.0 8.2.9.3.1 Get FW Info
716 static int cxl_mem_get_fw_info(struct cxl_memdev_state
*mds
)
718 struct cxl_mailbox
*cxl_mbox
= &mds
->cxlds
.cxl_mbox
;
719 struct cxl_mbox_get_fw_info info
;
720 struct cxl_mbox_cmd mbox_cmd
;
723 mbox_cmd
= (struct cxl_mbox_cmd
) {
724 .opcode
= CXL_MBOX_OP_GET_FW_INFO
,
725 .size_out
= sizeof(info
),
726 .payload_out
= &info
,
729 rc
= cxl_internal_send_cmd(cxl_mbox
, &mbox_cmd
);
733 mds
->fw
.num_slots
= info
.num_slots
;
734 mds
->fw
.cur_slot
= FIELD_GET(CXL_FW_INFO_SLOT_INFO_CUR_MASK
,
741 * cxl_mem_activate_fw - Activate Firmware
742 * @mds: The device data for the operation
743 * @slot: slot number to activate
745 * Activate firmware in a given slot for the device specified.
747 * Return: 0 if no error: or the result of the mailbox command.
749 * See CXL-3.0 8.2.9.3.3 Activate FW
751 static int cxl_mem_activate_fw(struct cxl_memdev_state
*mds
, int slot
)
753 struct cxl_mailbox
*cxl_mbox
= &mds
->cxlds
.cxl_mbox
;
754 struct cxl_mbox_activate_fw activate
;
755 struct cxl_mbox_cmd mbox_cmd
;
757 if (slot
== 0 || slot
> mds
->fw
.num_slots
)
760 mbox_cmd
= (struct cxl_mbox_cmd
) {
761 .opcode
= CXL_MBOX_OP_ACTIVATE_FW
,
762 .size_in
= sizeof(activate
),
763 .payload_in
= &activate
,
766 /* Only offline activation supported for now */
767 activate
.action
= CXL_FW_ACTIVATE_OFFLINE
;
768 activate
.slot
= slot
;
770 return cxl_internal_send_cmd(cxl_mbox
, &mbox_cmd
);
774 * cxl_mem_abort_fw_xfer - Abort an in-progress FW transfer
775 * @mds: The device data for the operation
777 * Abort an in-progress firmware transfer for the device specified.
779 * Return: 0 if no error: or the result of the mailbox command.
781 * See CXL-3.0 8.2.9.3.2 Transfer FW
783 static int cxl_mem_abort_fw_xfer(struct cxl_memdev_state
*mds
)
785 struct cxl_mailbox
*cxl_mbox
= &mds
->cxlds
.cxl_mbox
;
786 struct cxl_mbox_transfer_fw
*transfer
;
787 struct cxl_mbox_cmd mbox_cmd
;
790 transfer
= kzalloc(struct_size(transfer
, data
, 0), GFP_KERNEL
);
794 /* Set a 1s poll interval and a total wait time of 30s */
795 mbox_cmd
= (struct cxl_mbox_cmd
) {
796 .opcode
= CXL_MBOX_OP_TRANSFER_FW
,
797 .size_in
= sizeof(*transfer
),
798 .payload_in
= transfer
,
799 .poll_interval_ms
= 1000,
803 transfer
->action
= CXL_FW_TRANSFER_ACTION_ABORT
;
805 rc
= cxl_internal_send_cmd(cxl_mbox
, &mbox_cmd
);
810 static void cxl_fw_cleanup(struct fw_upload
*fwl
)
812 struct cxl_memdev_state
*mds
= fwl
->dd_handle
;
814 mds
->fw
.next_slot
= 0;
817 static int cxl_fw_do_cancel(struct fw_upload
*fwl
)
819 struct cxl_memdev_state
*mds
= fwl
->dd_handle
;
820 struct cxl_dev_state
*cxlds
= &mds
->cxlds
;
821 struct cxl_memdev
*cxlmd
= cxlds
->cxlmd
;
824 rc
= cxl_mem_abort_fw_xfer(mds
);
826 dev_err(&cxlmd
->dev
, "Error aborting FW transfer: %d\n", rc
);
828 return FW_UPLOAD_ERR_CANCELED
;
831 static enum fw_upload_err
cxl_fw_prepare(struct fw_upload
*fwl
, const u8
*data
,
834 struct cxl_memdev_state
*mds
= fwl
->dd_handle
;
835 struct cxl_mbox_transfer_fw
*transfer
;
836 struct cxl_mailbox
*cxl_mbox
= &mds
->cxlds
.cxl_mbox
;
839 return FW_UPLOAD_ERR_INVALID_SIZE
;
841 mds
->fw
.oneshot
= struct_size(transfer
, data
, size
) <
842 cxl_mbox
->payload_size
;
844 if (cxl_mem_get_fw_info(mds
))
845 return FW_UPLOAD_ERR_HW_ERROR
;
848 * So far no state has been changed, hence no other cleanup is
849 * necessary. Simply return the cancelled status.
851 if (test_and_clear_bit(CXL_FW_CANCEL
, mds
->fw
.state
))
852 return FW_UPLOAD_ERR_CANCELED
;
854 return FW_UPLOAD_ERR_NONE
;
857 static enum fw_upload_err
cxl_fw_write(struct fw_upload
*fwl
, const u8
*data
,
858 u32 offset
, u32 size
, u32
*written
)
860 struct cxl_memdev_state
*mds
= fwl
->dd_handle
;
861 struct cxl_dev_state
*cxlds
= &mds
->cxlds
;
862 struct cxl_mailbox
*cxl_mbox
= &cxlds
->cxl_mbox
;
863 struct cxl_memdev
*cxlmd
= cxlds
->cxlmd
;
864 struct cxl_mbox_transfer_fw
*transfer
;
865 struct cxl_mbox_cmd mbox_cmd
;
866 u32 cur_size
, remaining
;
872 /* Offset has to be aligned to 128B (CXL-3.0 8.2.9.3.2 Table 8-57) */
873 if (!IS_ALIGNED(offset
, CXL_FW_TRANSFER_ALIGNMENT
)) {
875 "misaligned offset for FW transfer slice (%u)\n",
877 return FW_UPLOAD_ERR_RW_ERROR
;
881 * Pick transfer size based on mds->payload_size @size must bw 128-byte
882 * aligned, ->payload_size is a power of 2 starting at 256 bytes, and
883 * sizeof(*transfer) is 128. These constraints imply that @cur_size
884 * will always be 128b aligned.
886 cur_size
= min_t(size_t, size
, cxl_mbox
->payload_size
- sizeof(*transfer
));
888 remaining
= size
- cur_size
;
889 size_in
= struct_size(transfer
, data
, cur_size
);
891 if (test_and_clear_bit(CXL_FW_CANCEL
, mds
->fw
.state
))
892 return cxl_fw_do_cancel(fwl
);
895 * Slot numbers are 1-indexed
896 * cur_slot is the 0-indexed next_slot (i.e. 'cur_slot - 1 + 1')
897 * Check for rollover using modulo, and 1-index it by adding 1
899 mds
->fw
.next_slot
= (mds
->fw
.cur_slot
% mds
->fw
.num_slots
) + 1;
901 /* Do the transfer via mailbox cmd */
902 transfer
= kzalloc(size_in
, GFP_KERNEL
);
904 return FW_UPLOAD_ERR_RW_ERROR
;
906 transfer
->offset
= cpu_to_le32(offset
/ CXL_FW_TRANSFER_ALIGNMENT
);
907 memcpy(transfer
->data
, data
+ offset
, cur_size
);
908 if (mds
->fw
.oneshot
) {
909 transfer
->action
= CXL_FW_TRANSFER_ACTION_FULL
;
910 transfer
->slot
= mds
->fw
.next_slot
;
913 transfer
->action
= CXL_FW_TRANSFER_ACTION_INITIATE
;
914 } else if (remaining
== 0) {
915 transfer
->action
= CXL_FW_TRANSFER_ACTION_END
;
916 transfer
->slot
= mds
->fw
.next_slot
;
918 transfer
->action
= CXL_FW_TRANSFER_ACTION_CONTINUE
;
922 mbox_cmd
= (struct cxl_mbox_cmd
) {
923 .opcode
= CXL_MBOX_OP_TRANSFER_FW
,
925 .payload_in
= transfer
,
926 .poll_interval_ms
= 1000,
930 rc
= cxl_internal_send_cmd(cxl_mbox
, &mbox_cmd
);
932 rc
= FW_UPLOAD_ERR_RW_ERROR
;
938 /* Activate FW if oneshot or if the last slice was written */
939 if (mds
->fw
.oneshot
|| remaining
== 0) {
940 dev_dbg(&cxlmd
->dev
, "Activating firmware slot: %d\n",
942 rc
= cxl_mem_activate_fw(mds
, mds
->fw
.next_slot
);
944 dev_err(&cxlmd
->dev
, "Error activating firmware: %d\n",
946 rc
= FW_UPLOAD_ERR_HW_ERROR
;
951 rc
= FW_UPLOAD_ERR_NONE
;
958 static enum fw_upload_err
cxl_fw_poll_complete(struct fw_upload
*fwl
)
960 struct cxl_memdev_state
*mds
= fwl
->dd_handle
;
963 * cxl_internal_send_cmd() handles background operations synchronously.
964 * No need to wait for completions here - any errors would've been
965 * reported and handled during the ->write() call(s).
966 * Just check if a cancel request was received, and return success.
968 if (test_and_clear_bit(CXL_FW_CANCEL
, mds
->fw
.state
))
969 return cxl_fw_do_cancel(fwl
);
971 return FW_UPLOAD_ERR_NONE
;
974 static void cxl_fw_cancel(struct fw_upload
*fwl
)
976 struct cxl_memdev_state
*mds
= fwl
->dd_handle
;
978 set_bit(CXL_FW_CANCEL
, mds
->fw
.state
);
981 static const struct fw_upload_ops cxl_memdev_fw_ops
= {
982 .prepare
= cxl_fw_prepare
,
983 .write
= cxl_fw_write
,
984 .poll_complete
= cxl_fw_poll_complete
,
985 .cancel
= cxl_fw_cancel
,
986 .cleanup
= cxl_fw_cleanup
,
989 static void cxl_remove_fw_upload(void *fwl
)
991 firmware_upload_unregister(fwl
);
994 int devm_cxl_setup_fw_upload(struct device
*host
, struct cxl_memdev_state
*mds
)
996 struct cxl_dev_state
*cxlds
= &mds
->cxlds
;
997 struct device
*dev
= &cxlds
->cxlmd
->dev
;
998 struct fw_upload
*fwl
;
1000 if (!test_bit(CXL_MEM_COMMAND_ID_GET_FW_INFO
, mds
->enabled_cmds
))
1003 fwl
= firmware_upload_register(THIS_MODULE
, dev
, dev_name(dev
),
1004 &cxl_memdev_fw_ops
, mds
);
1006 return PTR_ERR(fwl
);
1007 return devm_add_action_or_reset(host
, cxl_remove_fw_upload
, fwl
);
1009 EXPORT_SYMBOL_NS_GPL(devm_cxl_setup_fw_upload
, "CXL");
1011 static const struct file_operations cxl_memdev_fops
= {
1012 .owner
= THIS_MODULE
,
1013 .unlocked_ioctl
= cxl_memdev_ioctl
,
1014 .open
= cxl_memdev_open
,
1015 .release
= cxl_memdev_release_file
,
1016 .compat_ioctl
= compat_ptr_ioctl
,
1017 .llseek
= noop_llseek
,
1020 struct cxl_memdev
*devm_cxl_add_memdev(struct device
*host
,
1021 struct cxl_dev_state
*cxlds
)
1023 struct cxl_memdev
*cxlmd
;
1028 cxlmd
= cxl_memdev_alloc(cxlds
, &cxl_memdev_fops
);
1033 rc
= dev_set_name(dev
, "mem%d", cxlmd
->id
);
1038 * Activate ioctl operations, no cxl_memdev_rwsem manipulation
1039 * needed as this is ordered with cdev_add() publishing the device.
1041 cxlmd
->cxlds
= cxlds
;
1042 cxlds
->cxlmd
= cxlmd
;
1044 cdev
= &cxlmd
->cdev
;
1045 rc
= cdev_device_add(cdev
, dev
);
1049 rc
= devm_add_action_or_reset(host
, cxl_memdev_unregister
, cxlmd
);
1056 * The cdev was briefly live, shutdown any ioctl operations that
1059 cxl_memdev_shutdown(dev
);
1063 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_memdev
, "CXL");
1065 static void sanitize_teardown_notifier(void *data
)
1067 struct cxl_memdev_state
*mds
= data
;
1068 struct cxl_mailbox
*cxl_mbox
= &mds
->cxlds
.cxl_mbox
;
1069 struct kernfs_node
*state
;
1072 * Prevent new irq triggered invocations of the workqueue and
1073 * flush inflight invocations.
1075 mutex_lock(&cxl_mbox
->mbox_mutex
);
1076 state
= mds
->security
.sanitize_node
;
1077 mds
->security
.sanitize_node
= NULL
;
1078 mutex_unlock(&cxl_mbox
->mbox_mutex
);
1080 cancel_delayed_work_sync(&mds
->security
.poll_dwork
);
1084 int devm_cxl_sanitize_setup_notifier(struct device
*host
,
1085 struct cxl_memdev
*cxlmd
)
1087 struct cxl_dev_state
*cxlds
= cxlmd
->cxlds
;
1088 struct cxl_memdev_state
*mds
= to_cxl_memdev_state(cxlds
);
1089 struct kernfs_node
*sec
;
1091 if (!test_bit(CXL_SEC_ENABLED_SANITIZE
, mds
->security
.enabled_cmds
))
1095 * Note, the expectation is that @cxlmd would have failed to be
1096 * created if these sysfs_get_dirent calls fail.
1098 sec
= sysfs_get_dirent(cxlmd
->dev
.kobj
.sd
, "security");
1101 mds
->security
.sanitize_node
= sysfs_get_dirent(sec
, "state");
1103 if (!mds
->security
.sanitize_node
)
1106 return devm_add_action_or_reset(host
, sanitize_teardown_notifier
, mds
);
1108 EXPORT_SYMBOL_NS_GPL(devm_cxl_sanitize_setup_notifier
, "CXL");
1110 __init
int cxl_memdev_init(void)
1115 rc
= alloc_chrdev_region(&devt
, 0, CXL_MEM_MAX_DEVS
, "cxl");
1119 cxl_mem_major
= MAJOR(devt
);
1124 void cxl_memdev_exit(void)
1126 unregister_chrdev_region(MKDEV(cxl_mem_major
, 0), CXL_MEM_MAX_DEVS
);