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/xarray.h>
64 #include <linux/cdx/cdx_bus.h>
67 /* Default DMA mask for devices on a CDX bus */
68 #define CDX_DEFAULT_DMA_MASK (~0ULL)
69 #define MAX_CDX_CONTROLLERS 16
71 /* CDX controllers registered with the CDX bus */
72 static DEFINE_XARRAY_ALLOC(cdx_controllers);
75 * cdx_dev_reset - Reset a CDX device
78 * Return: -errno on failure, 0 on success.
80 int cdx_dev_reset(struct device *dev)
82 struct cdx_device *cdx_dev = to_cdx_device(dev);
83 struct cdx_controller *cdx = cdx_dev->cdx;
84 struct cdx_device_config dev_config = {0};
85 struct cdx_driver *cdx_drv;
88 cdx_drv = to_cdx_driver(dev->driver);
89 /* Notify driver that device is being reset */
90 if (cdx_drv && cdx_drv->reset_prepare)
91 cdx_drv->reset_prepare(cdx_dev);
93 dev_config.type = CDX_DEV_RESET_CONF;
94 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
95 cdx_dev->dev_num, &dev_config);
97 dev_err(dev, "cdx device reset failed\n");
99 /* Notify driver that device reset is complete */
100 if (cdx_drv && cdx_drv->reset_done)
101 cdx_drv->reset_done(cdx_dev);
105 EXPORT_SYMBOL_GPL(cdx_dev_reset);
108 * cdx_unregister_device - Unregister a CDX device
110 * @data: This is always passed as NULL, and is not used in this API,
111 * but is required here as the bus_for_each_dev() API expects
112 * the passed function (cdx_unregister_device) to have this
115 * Return: 0 on success.
117 static int cdx_unregister_device(struct device *dev,
120 struct cdx_device *cdx_dev = to_cdx_device(dev);
122 kfree(cdx_dev->driver_override);
123 cdx_dev->driver_override = NULL;
125 * Do not free cdx_dev here as it would be freed in
126 * cdx_device_release() called from within put_device().
128 device_del(&cdx_dev->dev);
129 put_device(&cdx_dev->dev);
134 static void cdx_unregister_devices(struct bus_type *bus)
136 /* Reset all the devices attached to cdx bus */
137 bus_for_each_dev(bus, NULL, NULL, cdx_unregister_device);
141 * cdx_match_one_device - Tell if a CDX device structure has a matching
142 * CDX device id structure
143 * @id: single CDX device id structure to match
144 * @dev: the CDX device structure to match against
146 * Return: matching cdx_device_id structure or NULL if there is no match.
148 static inline const struct cdx_device_id *
149 cdx_match_one_device(const struct cdx_device_id *id,
150 const struct cdx_device *dev)
152 /* Use vendor ID and device ID for matching */
153 if ((id->vendor == CDX_ANY_ID || id->vendor == dev->vendor) &&
154 (id->device == CDX_ANY_ID || id->device == dev->device))
160 * cdx_match_id - See if a CDX device matches a given cdx_id table
161 * @ids: array of CDX device ID structures to search in
162 * @dev: the CDX device structure to match against.
164 * Used by a driver to check whether a CDX device is in its list of
165 * supported devices. Returns the matching cdx_device_id structure or
166 * NULL if there is no match.
168 * Return: matching cdx_device_id structure or NULL if there is no match.
170 static inline const struct cdx_device_id *
171 cdx_match_id(const struct cdx_device_id *ids, struct cdx_device *dev)
174 while (ids->vendor || ids->device) {
175 if (cdx_match_one_device(ids, dev))
184 * cdx_bus_match - device to driver matching callback
185 * @dev: the cdx device to match against
186 * @drv: the device driver to search for matching cdx device
189 * Return: true on success, false otherwise.
191 static int cdx_bus_match(struct device *dev, struct device_driver *drv)
193 struct cdx_device *cdx_dev = to_cdx_device(dev);
194 struct cdx_driver *cdx_drv = to_cdx_driver(drv);
195 const struct cdx_device_id *found_id = NULL;
196 const struct cdx_device_id *ids;
198 ids = cdx_drv->match_id_table;
200 /* When driver_override is set, only bind to the matching driver */
201 if (cdx_dev->driver_override && strcmp(cdx_dev->driver_override, drv->name))
204 found_id = cdx_match_id(ids, cdx_dev);
210 * In case override_only was set, enforce driver_override
213 if (!found_id->override_only)
215 if (cdx_dev->driver_override)
219 found_id = cdx_match_id(ids, cdx_dev);
225 static int cdx_probe(struct device *dev)
227 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
228 struct cdx_device *cdx_dev = to_cdx_device(dev);
231 error = cdx_drv->probe(cdx_dev);
233 dev_err_probe(dev, error, "%s failed\n", __func__);
240 static void cdx_remove(struct device *dev)
242 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
243 struct cdx_device *cdx_dev = to_cdx_device(dev);
245 if (cdx_drv && cdx_drv->remove)
246 cdx_drv->remove(cdx_dev);
249 static void cdx_shutdown(struct device *dev)
251 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
252 struct cdx_device *cdx_dev = to_cdx_device(dev);
254 if (cdx_drv && cdx_drv->shutdown)
255 cdx_drv->shutdown(cdx_dev);
258 static int cdx_dma_configure(struct device *dev)
260 struct cdx_device *cdx_dev = to_cdx_device(dev);
261 u32 input_id = cdx_dev->req_id;
264 ret = of_dma_configure_id(dev, dev->parent->of_node, 0, &input_id);
265 if (ret && ret != -EPROBE_DEFER) {
266 dev_err(dev, "of_dma_configure_id() failed\n");
273 /* show configuration fields */
274 #define cdx_config_attr(field, format_string) \
276 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
278 struct cdx_device *cdx_dev = to_cdx_device(dev); \
279 return sysfs_emit(buf, format_string, cdx_dev->field); \
281 static DEVICE_ATTR_RO(field)
283 cdx_config_attr(vendor, "0x%04x\n");
284 cdx_config_attr(device, "0x%04x\n");
286 static ssize_t remove_store(struct device *dev,
287 struct device_attribute *attr,
288 const char *buf, size_t count)
292 if (kstrtobool(buf, &val) < 0)
298 if (device_remove_file_self(dev, attr)) {
301 ret = cdx_unregister_device(dev, NULL);
308 static DEVICE_ATTR_WO(remove);
310 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
311 const char *buf, size_t count)
316 if (kstrtobool(buf, &val) < 0)
322 ret = cdx_dev_reset(dev);
328 static DEVICE_ATTR_WO(reset);
330 static ssize_t driver_override_store(struct device *dev,
331 struct device_attribute *attr,
332 const char *buf, size_t count)
334 struct cdx_device *cdx_dev = to_cdx_device(dev);
337 if (WARN_ON(dev->bus != &cdx_bus_type))
340 ret = driver_set_override(dev, &cdx_dev->driver_override, buf, count);
347 static ssize_t driver_override_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
350 struct cdx_device *cdx_dev = to_cdx_device(dev);
352 return sysfs_emit(buf, "%s\n", cdx_dev->driver_override);
354 static DEVICE_ATTR_RW(driver_override);
356 static struct attribute *cdx_dev_attrs[] = {
357 &dev_attr_remove.attr,
358 &dev_attr_reset.attr,
359 &dev_attr_vendor.attr,
360 &dev_attr_device.attr,
361 &dev_attr_driver_override.attr,
364 ATTRIBUTE_GROUPS(cdx_dev);
366 static ssize_t rescan_store(const struct bus_type *bus,
367 const char *buf, size_t count)
369 struct cdx_controller *cdx;
373 if (kstrtobool(buf, &val) < 0)
379 /* Unregister all the devices on the bus */
380 cdx_unregister_devices(&cdx_bus_type);
382 /* Rescan all the devices */
383 xa_for_each(&cdx_controllers, index, cdx) {
386 ret = cdx->ops->scan(cdx);
388 dev_err(cdx->dev, "cdx bus scanning failed\n");
393 static BUS_ATTR_WO(rescan);
395 static struct attribute *cdx_bus_attrs[] = {
396 &bus_attr_rescan.attr,
399 ATTRIBUTE_GROUPS(cdx_bus);
401 struct bus_type cdx_bus_type = {
403 .match = cdx_bus_match,
405 .remove = cdx_remove,
406 .shutdown = cdx_shutdown,
407 .dma_configure = cdx_dma_configure,
408 .bus_groups = cdx_bus_groups,
409 .dev_groups = cdx_dev_groups,
411 EXPORT_SYMBOL_GPL(cdx_bus_type);
413 int __cdx_driver_register(struct cdx_driver *cdx_driver,
414 struct module *owner)
418 cdx_driver->driver.owner = owner;
419 cdx_driver->driver.bus = &cdx_bus_type;
421 error = driver_register(&cdx_driver->driver);
423 pr_err("driver_register() failed for %s: %d\n",
424 cdx_driver->driver.name, error);
430 EXPORT_SYMBOL_GPL(__cdx_driver_register);
432 void cdx_driver_unregister(struct cdx_driver *cdx_driver)
434 driver_unregister(&cdx_driver->driver);
436 EXPORT_SYMBOL_GPL(cdx_driver_unregister);
438 static void cdx_device_release(struct device *dev)
440 struct cdx_device *cdx_dev = to_cdx_device(dev);
445 int cdx_device_add(struct cdx_dev_params *dev_params)
447 struct cdx_controller *cdx = dev_params->cdx;
448 struct device *parent = cdx->dev;
449 struct cdx_device *cdx_dev;
452 cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL);
456 /* Populate resource */
457 memcpy(cdx_dev->res, dev_params->res, sizeof(struct resource) *
458 dev_params->res_count);
459 cdx_dev->res_count = dev_params->res_count;
461 /* Populate CDX dev params */
462 cdx_dev->req_id = dev_params->req_id;
463 cdx_dev->vendor = dev_params->vendor;
464 cdx_dev->device = dev_params->device;
465 cdx_dev->bus_num = dev_params->bus_num;
466 cdx_dev->dev_num = dev_params->dev_num;
467 cdx_dev->cdx = dev_params->cdx;
468 cdx_dev->dma_mask = CDX_DEFAULT_DMA_MASK;
470 /* Initialize generic device */
471 device_initialize(&cdx_dev->dev);
472 cdx_dev->dev.parent = parent;
473 cdx_dev->dev.bus = &cdx_bus_type;
474 cdx_dev->dev.dma_mask = &cdx_dev->dma_mask;
475 cdx_dev->dev.release = cdx_device_release;
478 dev_set_name(&cdx_dev->dev, "cdx-%02x:%02x",
479 ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (cdx_dev->bus_num & CDX_BUS_NUM_MASK)),
482 ret = device_add(&cdx_dev->dev);
484 dev_err(&cdx_dev->dev,
485 "cdx device add failed: %d", ret);
492 * Do not free cdx_dev here as it would be freed in
493 * cdx_device_release() called from put_device().
495 put_device(&cdx_dev->dev);
499 EXPORT_SYMBOL_GPL(cdx_device_add);
501 int cdx_register_controller(struct cdx_controller *cdx)
505 ret = xa_alloc(&cdx_controllers, &cdx->id, cdx,
506 XA_LIMIT(0, MAX_CDX_CONTROLLERS - 1), GFP_KERNEL);
509 "No free index available. Maximum controllers already registered\n");
510 cdx->id = (u8)MAX_CDX_CONTROLLERS;
514 /* Scan all the devices */
519 EXPORT_SYMBOL_GPL(cdx_register_controller);
521 void cdx_unregister_controller(struct cdx_controller *cdx)
523 if (cdx->id >= MAX_CDX_CONTROLLERS)
526 device_for_each_child(cdx->dev, NULL, cdx_unregister_device);
527 xa_erase(&cdx_controllers, cdx->id);
529 EXPORT_SYMBOL_GPL(cdx_unregister_controller);
531 static int __init cdx_bus_init(void)
533 return bus_register(&cdx_bus_type);
535 postcore_initcall(cdx_bus_init);