1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
9 * Architecture Overview
10 * =====================
11 * CDX is a Hardware Architecture designed for AMD FPGA devices. It
12 * consists of sophisticated mechanism for interaction between FPGA,
13 * Firmware and the APUs (Application CPUs).
15 * Firmware resides on RPU (Realtime CPUs) which interacts with
16 * the FPGA program manager and the APUs. The RPU provides memory-mapped
17 * interface (RPU if) which is used to communicate with APUs.
19 * The diagram below shows an overview of the CDX architecture:
21 * +--------------------------------------+
22 * | Application CPUs (APU) |
24 * | CDX device drivers|
30 * +-----------------------------|--------+
31 * | (discover, config,
34 * +------------------------| RPU if |----+
37 * | Realtime CPUs (RPU) |
39 * +--------------------------------------+
41 * +---------------------|----------------+
43 * | +-----------------------+ |
45 * | +-------+ +-------+ +-------+ |
46 * | | dev 1 | | dev 2 | | dev 3 | |
47 * | +-------+ +-------+ +-------+ |
48 * +--------------------------------------+
50 * The RPU firmware extracts the device information from the loaded FPGA
51 * image and implements a mechanism that allows the APU drivers to
52 * enumerate such devices (device personality and resource details) via
53 * a dedicated communication channel. RPU mediates operations such as
54 * discover, reset and rescan of the FPGA devices for the APU. This is
55 * done using memory mapped interface provided by the RPU to APU.
58 #include <linux/init.h>
59 #include <linux/kernel.h>
60 #include <linux/of_device.h>
61 #include <linux/slab.h>
63 #include <linux/idr.h>
64 #include <linux/cdx/cdx_bus.h>
65 #include <linux/iommu.h>
66 #include <linux/dma-map-ops.h>
69 /* Default DMA mask for devices on a CDX bus */
70 #define CDX_DEFAULT_DMA_MASK (~0ULL)
71 #define MAX_CDX_CONTROLLERS 16
73 /* IDA for CDX controllers registered with the CDX bus */
74 static DEFINE_IDA(cdx_controller_ida);
75 /* Lock to protect controller ops */
76 static DEFINE_MUTEX(cdx_controller_lock);
78 static char *compat_node_name = "xlnx,versal-net-cdx";
81 * cdx_dev_reset - Reset a CDX device
84 * Return: -errno on failure, 0 on success.
86 int cdx_dev_reset(struct device *dev)
88 struct cdx_device *cdx_dev = to_cdx_device(dev);
89 struct cdx_controller *cdx = cdx_dev->cdx;
90 struct cdx_device_config dev_config = {0};
91 struct cdx_driver *cdx_drv;
94 cdx_drv = to_cdx_driver(dev->driver);
95 /* Notify driver that device is being reset */
96 if (cdx_drv && cdx_drv->reset_prepare)
97 cdx_drv->reset_prepare(cdx_dev);
99 dev_config.type = CDX_DEV_RESET_CONF;
100 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
101 cdx_dev->dev_num, &dev_config);
103 dev_err(dev, "cdx device reset failed\n");
105 /* Notify driver that device reset is complete */
106 if (cdx_drv && cdx_drv->reset_done)
107 cdx_drv->reset_done(cdx_dev);
111 EXPORT_SYMBOL_GPL(cdx_dev_reset);
114 * reset_cdx_device - Reset a CDX device
116 * @data: This is always passed as NULL, and is not used in this API,
117 * but is required here as the device_for_each_child() API expects
118 * the passed function to have this as an argument.
120 * Return: -errno on failure, 0 on success.
122 static int reset_cdx_device(struct device *dev, void *data)
124 return cdx_dev_reset(dev);
128 * cdx_unregister_device - Unregister a CDX device
130 * @data: This is always passed as NULL, and is not used in this API,
131 * but is required here as the bus_for_each_dev() API expects
132 * the passed function (cdx_unregister_device) to have this
135 * Return: 0 on success.
137 static int cdx_unregister_device(struct device *dev,
140 struct cdx_device *cdx_dev = to_cdx_device(dev);
141 struct cdx_controller *cdx = cdx_dev->cdx;
143 if (cdx_dev->is_bus) {
144 device_for_each_child(dev, NULL, cdx_unregister_device);
145 if (cdx_dev->enabled && cdx->ops->bus_disable)
146 cdx->ops->bus_disable(cdx, cdx_dev->bus_num);
148 kfree(cdx_dev->driver_override);
149 cdx_dev->driver_override = NULL;
153 * Do not free cdx_dev here as it would be freed in
154 * cdx_device_release() called from within put_device().
156 device_del(&cdx_dev->dev);
157 put_device(&cdx_dev->dev);
162 static void cdx_unregister_devices(struct bus_type *bus)
164 /* Reset all the devices attached to cdx bus */
165 bus_for_each_dev(bus, NULL, NULL, cdx_unregister_device);
169 * cdx_match_one_device - Tell if a CDX device structure has a matching
170 * CDX device id structure
171 * @id: single CDX device id structure to match
172 * @dev: the CDX device structure to match against
174 * Return: matching cdx_device_id structure or NULL if there is no match.
176 static inline const struct cdx_device_id *
177 cdx_match_one_device(const struct cdx_device_id *id,
178 const struct cdx_device *dev)
180 /* Use vendor ID and device ID for matching */
181 if ((id->vendor == CDX_ANY_ID || id->vendor == dev->vendor) &&
182 (id->device == CDX_ANY_ID || id->device == dev->device) &&
183 (id->subvendor == CDX_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
184 (id->subdevice == CDX_ANY_ID || id->subdevice == dev->subsystem_device) &&
185 !((id->class ^ dev->class) & id->class_mask))
191 * cdx_match_id - See if a CDX device matches a given cdx_id table
192 * @ids: array of CDX device ID structures to search in
193 * @dev: the CDX device structure to match against.
195 * Used by a driver to check whether a CDX device is in its list of
196 * supported devices. Returns the matching cdx_device_id structure or
197 * NULL if there is no match.
199 * Return: matching cdx_device_id structure or NULL if there is no match.
201 static inline const struct cdx_device_id *
202 cdx_match_id(const struct cdx_device_id *ids, struct cdx_device *dev)
205 while (ids->vendor || ids->device) {
206 if (cdx_match_one_device(ids, dev))
214 int cdx_set_master(struct cdx_device *cdx_dev)
216 struct cdx_controller *cdx = cdx_dev->cdx;
217 struct cdx_device_config dev_config;
218 int ret = -EOPNOTSUPP;
220 dev_config.type = CDX_DEV_BUS_MASTER_CONF;
221 dev_config.bus_master_enable = true;
222 if (cdx->ops->dev_configure)
223 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
224 cdx_dev->dev_num, &dev_config);
228 EXPORT_SYMBOL_GPL(cdx_set_master);
230 int cdx_clear_master(struct cdx_device *cdx_dev)
232 struct cdx_controller *cdx = cdx_dev->cdx;
233 struct cdx_device_config dev_config;
234 int ret = -EOPNOTSUPP;
236 dev_config.type = CDX_DEV_BUS_MASTER_CONF;
237 dev_config.bus_master_enable = false;
238 if (cdx->ops->dev_configure)
239 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
240 cdx_dev->dev_num, &dev_config);
244 EXPORT_SYMBOL_GPL(cdx_clear_master);
247 * cdx_bus_match - device to driver matching callback
248 * @dev: the cdx device to match against
249 * @drv: the device driver to search for matching cdx device
252 * Return: true on success, false otherwise.
254 static int cdx_bus_match(struct device *dev, struct device_driver *drv)
256 struct cdx_device *cdx_dev = to_cdx_device(dev);
257 struct cdx_driver *cdx_drv = to_cdx_driver(drv);
258 const struct cdx_device_id *found_id = NULL;
259 const struct cdx_device_id *ids;
264 ids = cdx_drv->match_id_table;
266 /* When driver_override is set, only bind to the matching driver */
267 if (cdx_dev->driver_override && strcmp(cdx_dev->driver_override, drv->name))
270 found_id = cdx_match_id(ids, cdx_dev);
276 * In case override_only was set, enforce driver_override
279 if (!found_id->override_only)
281 if (cdx_dev->driver_override)
285 found_id = cdx_match_id(ids, cdx_dev);
291 static int cdx_probe(struct device *dev)
293 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
294 struct cdx_device *cdx_dev = to_cdx_device(dev);
297 error = cdx_drv->probe(cdx_dev);
299 dev_err_probe(dev, error, "%s failed\n", __func__);
306 static void cdx_remove(struct device *dev)
308 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
309 struct cdx_device *cdx_dev = to_cdx_device(dev);
311 if (cdx_drv && cdx_drv->remove)
312 cdx_drv->remove(cdx_dev);
315 static void cdx_shutdown(struct device *dev)
317 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
318 struct cdx_device *cdx_dev = to_cdx_device(dev);
320 if (cdx_drv && cdx_drv->shutdown)
321 cdx_drv->shutdown(cdx_dev);
324 static int cdx_dma_configure(struct device *dev)
326 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
327 struct cdx_device *cdx_dev = to_cdx_device(dev);
328 struct cdx_controller *cdx = cdx_dev->cdx;
329 u32 input_id = cdx_dev->req_id;
332 ret = of_dma_configure_id(dev, cdx->dev->of_node, 0, &input_id);
333 if (ret && ret != -EPROBE_DEFER) {
334 dev_err(dev, "of_dma_configure_id() failed\n");
338 if (!ret && !cdx_drv->driver_managed_dma) {
339 ret = iommu_device_use_default_domain(dev);
341 arch_teardown_dma_ops(dev);
347 static void cdx_dma_cleanup(struct device *dev)
349 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
351 if (!cdx_drv->driver_managed_dma)
352 iommu_device_unuse_default_domain(dev);
355 /* show configuration fields */
356 #define cdx_config_attr(field, format_string) \
358 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
360 struct cdx_device *cdx_dev = to_cdx_device(dev); \
361 return sysfs_emit(buf, format_string, cdx_dev->field); \
363 static DEVICE_ATTR_RO(field)
365 cdx_config_attr(vendor, "0x%04x\n");
366 cdx_config_attr(device, "0x%04x\n");
367 cdx_config_attr(subsystem_vendor, "0x%04x\n");
368 cdx_config_attr(subsystem_device, "0x%04x\n");
369 cdx_config_attr(revision, "0x%02x\n");
370 cdx_config_attr(class, "0x%06x\n");
372 static ssize_t remove_store(struct device *dev,
373 struct device_attribute *attr,
374 const char *buf, size_t count)
378 if (kstrtobool(buf, &val) < 0)
384 if (device_remove_file_self(dev, attr)) {
387 ret = cdx_unregister_device(dev, NULL);
394 static DEVICE_ATTR_WO(remove);
396 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
397 const char *buf, size_t count)
399 struct cdx_device *cdx_dev = to_cdx_device(dev);
403 if (kstrtobool(buf, &val) < 0)
410 /* Reset all the devices attached to cdx bus */
411 ret = device_for_each_child(dev, NULL, reset_cdx_device);
413 ret = cdx_dev_reset(dev);
415 return ret < 0 ? ret : count;
417 static DEVICE_ATTR_WO(reset);
419 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
422 struct cdx_device *cdx_dev = to_cdx_device(dev);
424 return sprintf(buf, "cdx:v%04Xd%04Xsv%04Xsd%04Xc%06X\n", cdx_dev->vendor,
425 cdx_dev->device, cdx_dev->subsystem_vendor, cdx_dev->subsystem_device,
428 static DEVICE_ATTR_RO(modalias);
430 static ssize_t driver_override_store(struct device *dev,
431 struct device_attribute *attr,
432 const char *buf, size_t count)
434 struct cdx_device *cdx_dev = to_cdx_device(dev);
437 if (WARN_ON(dev->bus != &cdx_bus_type))
440 ret = driver_set_override(dev, &cdx_dev->driver_override, buf, count);
447 static ssize_t driver_override_show(struct device *dev,
448 struct device_attribute *attr, char *buf)
450 struct cdx_device *cdx_dev = to_cdx_device(dev);
452 return sysfs_emit(buf, "%s\n", cdx_dev->driver_override);
454 static DEVICE_ATTR_RW(driver_override);
456 static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
457 const char *buf, size_t count)
459 struct cdx_device *cdx_dev = to_cdx_device(dev);
460 struct cdx_controller *cdx = cdx_dev->cdx;
464 if (kstrtobool(buf, &enable) < 0)
467 if (enable == cdx_dev->enabled)
470 if (enable && cdx->ops->bus_enable)
471 ret = cdx->ops->bus_enable(cdx, cdx_dev->bus_num);
472 else if (!enable && cdx->ops->bus_disable)
473 ret = cdx->ops->bus_disable(cdx, cdx_dev->bus_num);
478 cdx_dev->enabled = enable;
480 return ret < 0 ? ret : count;
483 static ssize_t enable_show(struct device *dev, struct device_attribute *attr, char *buf)
485 struct cdx_device *cdx_dev = to_cdx_device(dev);
487 return sysfs_emit(buf, "%u\n", cdx_dev->enabled);
489 static DEVICE_ATTR_RW(enable);
491 static umode_t cdx_dev_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n)
493 struct device *dev = kobj_to_dev(kobj);
494 struct cdx_device *cdx_dev;
496 cdx_dev = to_cdx_device(dev);
497 if (!cdx_dev->is_bus)
503 static umode_t cdx_bus_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n)
505 struct device *dev = kobj_to_dev(kobj);
506 struct cdx_device *cdx_dev;
508 cdx_dev = to_cdx_device(dev);
515 static struct attribute *cdx_dev_attrs[] = {
516 &dev_attr_remove.attr,
517 &dev_attr_reset.attr,
518 &dev_attr_vendor.attr,
519 &dev_attr_device.attr,
520 &dev_attr_subsystem_vendor.attr,
521 &dev_attr_subsystem_device.attr,
522 &dev_attr_class.attr,
523 &dev_attr_revision.attr,
524 &dev_attr_modalias.attr,
525 &dev_attr_driver_override.attr,
529 static const struct attribute_group cdx_dev_group = {
530 .attrs = cdx_dev_attrs,
531 .is_visible = cdx_dev_attrs_are_visible,
534 static struct attribute *cdx_bus_dev_attrs[] = {
535 &dev_attr_enable.attr,
536 &dev_attr_reset.attr,
540 static const struct attribute_group cdx_bus_dev_group = {
541 .attrs = cdx_bus_dev_attrs,
542 .is_visible = cdx_bus_attrs_are_visible,
545 static const struct attribute_group *cdx_dev_groups[] = {
551 static ssize_t rescan_store(const struct bus_type *bus,
552 const char *buf, size_t count)
554 struct cdx_controller *cdx;
555 struct platform_device *pd;
556 struct device_node *np;
559 if (kstrtobool(buf, &val) < 0)
565 mutex_lock(&cdx_controller_lock);
567 /* Unregister all the devices on the bus */
568 cdx_unregister_devices(&cdx_bus_type);
570 /* Rescan all the devices */
571 for_each_compatible_node(np, NULL, compat_node_name) {
575 pd = of_find_device_by_node(np);
579 cdx = platform_get_drvdata(pd);
580 if (cdx && cdx->controller_registered && cdx->ops->scan)
583 put_device(&pd->dev);
586 mutex_unlock(&cdx_controller_lock);
590 static BUS_ATTR_WO(rescan);
592 static struct attribute *cdx_bus_attrs[] = {
593 &bus_attr_rescan.attr,
596 ATTRIBUTE_GROUPS(cdx_bus);
598 struct bus_type cdx_bus_type = {
600 .match = cdx_bus_match,
602 .remove = cdx_remove,
603 .shutdown = cdx_shutdown,
604 .dma_configure = cdx_dma_configure,
605 .dma_cleanup = cdx_dma_cleanup,
606 .bus_groups = cdx_bus_groups,
607 .dev_groups = cdx_dev_groups,
609 EXPORT_SYMBOL_GPL(cdx_bus_type);
611 int __cdx_driver_register(struct cdx_driver *cdx_driver,
612 struct module *owner)
616 cdx_driver->driver.owner = owner;
617 cdx_driver->driver.bus = &cdx_bus_type;
619 error = driver_register(&cdx_driver->driver);
621 pr_err("driver_register() failed for %s: %d\n",
622 cdx_driver->driver.name, error);
628 EXPORT_SYMBOL_GPL(__cdx_driver_register);
630 void cdx_driver_unregister(struct cdx_driver *cdx_driver)
632 driver_unregister(&cdx_driver->driver);
634 EXPORT_SYMBOL_GPL(cdx_driver_unregister);
636 static void cdx_device_release(struct device *dev)
638 struct cdx_device *cdx_dev = to_cdx_device(dev);
643 int cdx_device_add(struct cdx_dev_params *dev_params)
645 struct cdx_controller *cdx = dev_params->cdx;
646 struct cdx_device *cdx_dev;
649 cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL);
653 /* Populate resource */
654 memcpy(cdx_dev->res, dev_params->res, sizeof(struct resource) *
655 dev_params->res_count);
656 cdx_dev->res_count = dev_params->res_count;
658 /* Populate CDX dev params */
659 cdx_dev->req_id = dev_params->req_id;
660 cdx_dev->vendor = dev_params->vendor;
661 cdx_dev->device = dev_params->device;
662 cdx_dev->subsystem_vendor = dev_params->subsys_vendor;
663 cdx_dev->subsystem_device = dev_params->subsys_device;
664 cdx_dev->class = dev_params->class;
665 cdx_dev->revision = dev_params->revision;
666 cdx_dev->bus_num = dev_params->bus_num;
667 cdx_dev->dev_num = dev_params->dev_num;
668 cdx_dev->cdx = dev_params->cdx;
669 cdx_dev->dma_mask = CDX_DEFAULT_DMA_MASK;
671 /* Initialize generic device */
672 device_initialize(&cdx_dev->dev);
673 cdx_dev->dev.parent = dev_params->parent;
674 cdx_dev->dev.bus = &cdx_bus_type;
675 cdx_dev->dev.dma_mask = &cdx_dev->dma_mask;
676 cdx_dev->dev.release = cdx_device_release;
679 dev_set_name(&cdx_dev->dev, "cdx-%02x:%02x",
680 ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (cdx_dev->bus_num & CDX_BUS_NUM_MASK)),
683 ret = device_add(&cdx_dev->dev);
685 dev_err(&cdx_dev->dev,
686 "cdx device add failed: %d", ret);
693 * Do not free cdx_dev here as it would be freed in
694 * cdx_device_release() called from put_device().
696 put_device(&cdx_dev->dev);
700 EXPORT_SYMBOL_NS_GPL(cdx_device_add, CDX_BUS_CONTROLLER);
702 struct device *cdx_bus_add(struct cdx_controller *cdx, u8 bus_num)
704 struct cdx_device *cdx_dev;
707 cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL);
711 device_initialize(&cdx_dev->dev);
714 cdx_dev->dev.parent = cdx->dev;
715 cdx_dev->dev.bus = &cdx_bus_type;
716 cdx_dev->dev.release = cdx_device_release;
717 cdx_dev->is_bus = true;
718 cdx_dev->bus_num = bus_num;
720 dev_set_name(&cdx_dev->dev, "cdx-%02x",
721 ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (bus_num & CDX_BUS_NUM_MASK)));
723 ret = device_add(&cdx_dev->dev);
725 dev_err(&cdx_dev->dev, "cdx bus device add failed: %d\n", ret);
726 goto device_add_fail;
729 if (cdx->ops->bus_enable) {
730 ret = cdx->ops->bus_enable(cdx, bus_num);
731 if (ret && ret != -EALREADY) {
732 dev_err(cdx->dev, "cdx bus enable failed: %d\n", ret);
733 goto bus_enable_fail;
737 cdx_dev->enabled = true;
738 return &cdx_dev->dev;
741 device_del(&cdx_dev->dev);
743 put_device(&cdx_dev->dev);
747 EXPORT_SYMBOL_NS_GPL(cdx_bus_add, CDX_BUS_CONTROLLER);
749 int cdx_register_controller(struct cdx_controller *cdx)
753 ret = ida_alloc_range(&cdx_controller_ida, 0, MAX_CDX_CONTROLLERS - 1, GFP_KERNEL);
756 "No free index available. Maximum controllers already registered\n");
757 cdx->id = (u8)MAX_CDX_CONTROLLERS;
761 mutex_lock(&cdx_controller_lock);
764 /* Scan all the devices */
767 cdx->controller_registered = true;
768 mutex_unlock(&cdx_controller_lock);
772 EXPORT_SYMBOL_NS_GPL(cdx_register_controller, CDX_BUS_CONTROLLER);
774 void cdx_unregister_controller(struct cdx_controller *cdx)
776 if (cdx->id >= MAX_CDX_CONTROLLERS)
779 mutex_lock(&cdx_controller_lock);
781 cdx->controller_registered = false;
782 device_for_each_child(cdx->dev, NULL, cdx_unregister_device);
783 ida_free(&cdx_controller_ida, cdx->id);
785 mutex_unlock(&cdx_controller_lock);
787 EXPORT_SYMBOL_NS_GPL(cdx_unregister_controller, CDX_BUS_CONTROLLER);
789 static int __init cdx_bus_init(void)
791 return bus_register(&cdx_bus_type);
793 postcore_initcall(cdx_bus_init);