// SPDX-License-Identifier: GPL-2.0 /* * CDX bus driver. * * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
*/
/* * Architecture Overview * ===================== * CDX is a Hardware Architecture designed for AMD FPGA devices. It * consists of sophisticated mechanism for interaction between FPGA, * Firmware and the APUs (Application CPUs). * * Firmware resides on RPU (Realtime CPUs) which interacts with * the FPGA program manager and the APUs. The RPU provides memory-mapped * interface (RPU if) which is used to communicate with APUs. * * The diagram below shows an overview of the CDX architecture: * * +--------------------------------------+ * | Application CPUs (APU) | * | | * | CDX device drivers| * | Linux OS | | * | CDX bus | * | | | * | CDX controller | * | | | * +-----------------------------|--------+ * | (discover, config, * | reset, rescan) * | * +------------------------| RPU if |----+ * | | | * | V | * | Realtime CPUs (RPU) | * | | * +--------------------------------------+ * | * +---------------------|----------------+ * | FPGA | | * | +-----------------------+ | * | | | | | * | +-------+ +-------+ +-------+ | * | | dev 1 | | dev 2 | | dev 3 | | * | +-------+ +-------+ +-------+ | * +--------------------------------------+ * * The RPU firmware extracts the device information from the loaded FPGA * image and implements a mechanism that allows the APU drivers to * enumerate such devices (device personality and resource details) via * a dedicated communication channel. RPU mediates operations such as * discover, reset and rescan of the FPGA devices for the APU. This is * done using memory mapped interface provided by the RPU to APU.
*/
/* Default DMA mask for devices on a CDX bus */ #define CDX_DEFAULT_DMA_MASK (~0ULL) #define MAX_CDX_CONTROLLERS 16
/* IDA for CDX controllers registered with the CDX bus */ static DEFINE_IDA(cdx_controller_ida); /* Lock to protect controller ops */ static DEFINE_MUTEX(cdx_controller_lock); /* Debugfs dir for cdx bus */ staticstruct dentry *cdx_debugfs_dir;
staticvoid cdx_destroy_res_attr(struct cdx_device *cdx_dev, int num);
/** * cdx_dev_reset - Reset a CDX device * @dev: CDX device * * Return: -errno on failure, 0 on success.
*/ int cdx_dev_reset(struct device *dev)
{ struct cdx_device *cdx_dev = to_cdx_device(dev); struct cdx_controller *cdx = cdx_dev->cdx; struct cdx_device_config dev_config = {0}; struct cdx_driver *cdx_drv; int ret;
cdx_drv = to_cdx_driver(dev->driver); /* Notify driver that device is being reset */ if (cdx_drv && cdx_drv->reset_prepare)
cdx_drv->reset_prepare(cdx_dev);
dev_config.type = CDX_DEV_RESET_CONF;
ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
cdx_dev->dev_num, &dev_config); if (ret)
dev_err(dev, "cdx device reset failed\n");
/* Notify driver that device reset is complete */ if (cdx_drv && cdx_drv->reset_done)
cdx_drv->reset_done(cdx_dev);
return ret;
}
EXPORT_SYMBOL_GPL(cdx_dev_reset);
/** * reset_cdx_device - Reset a CDX device * @dev: CDX device * @data: This is always passed as NULL, and is not used in this API, * but is required here as the device_for_each_child() API expects * the passed function to have this as an argument. * * Return: -errno on failure, 0 on success.
*/ staticint reset_cdx_device(struct device *dev, void *data)
{ return cdx_dev_reset(dev);
}
/** * cdx_unregister_device - Unregister a CDX device * @dev: CDX device * @data: This is always passed as NULL, and is not used in this API, * but is required here as the bus_for_each_dev() API expects * the passed function (cdx_unregister_device) to have this * as an argument. * * Return: 0 on success.
*/ staticint cdx_unregister_device(struct device *dev, void *data)
{ struct cdx_device *cdx_dev = to_cdx_device(dev); struct cdx_controller *cdx = cdx_dev->cdx;
/* * Do not free cdx_dev here as it would be freed in * cdx_device_release() called from within put_device().
*/
device_del(&cdx_dev->dev);
put_device(&cdx_dev->dev);
return 0;
}
staticvoid cdx_unregister_devices(struct bus_type *bus)
{ /* Reset all the devices attached to cdx bus */
bus_for_each_dev(bus, NULL, NULL, cdx_unregister_device);
}
/** * cdx_match_one_device - Tell if a CDX device structure has a matching * CDX device id structure * @id: single CDX device id structure to match * @dev: the CDX device structure to match against * * Return: matching cdx_device_id structure or NULL if there is no match.
*/ staticinlineconststruct cdx_device_id *
cdx_match_one_device(conststruct cdx_device_id *id, conststruct cdx_device *dev)
{ /* Use vendor ID and device ID for matching */ if ((id->vendor == CDX_ANY_ID || id->vendor == dev->vendor) &&
(id->device == CDX_ANY_ID || id->device == dev->device) &&
(id->subvendor == CDX_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
(id->subdevice == CDX_ANY_ID || id->subdevice == dev->subsystem_device) &&
!((id->class ^ dev->class) & id->class_mask)) return id; return NULL;
}
/** * cdx_match_id - See if a CDX device matches a given cdx_id table * @ids: array of CDX device ID structures to search in * @dev: the CDX device structure to match against. * * Used by a driver to check whether a CDX device is in its list of * supported devices. Returns the matching cdx_device_id structure or * NULL if there is no match. * * Return: matching cdx_device_id structure or NULL if there is no match.
*/ staticinlineconststruct cdx_device_id *
cdx_match_id(conststruct cdx_device_id *ids, struct cdx_device *dev)
{ if (ids) { while (ids->vendor || ids->device) { if (cdx_match_one_device(ids, dev)) return ids;
ids++;
}
} return NULL;
}
int cdx_set_master(struct cdx_device *cdx_dev)
{ struct cdx_controller *cdx = cdx_dev->cdx; struct cdx_device_config dev_config; int ret = -EOPNOTSUPP;
dev_config.type = CDX_DEV_BUS_MASTER_CONF;
dev_config.bus_master_enable = true; if (cdx->ops->dev_configure)
ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
cdx_dev->dev_num, &dev_config);
return ret;
}
EXPORT_SYMBOL_GPL(cdx_set_master);
int cdx_clear_master(struct cdx_device *cdx_dev)
{ struct cdx_controller *cdx = cdx_dev->cdx; struct cdx_device_config dev_config; int ret = -EOPNOTSUPP;
dev_config.type = CDX_DEV_BUS_MASTER_CONF;
dev_config.bus_master_enable = false; if (cdx->ops->dev_configure)
ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
cdx_dev->dev_num, &dev_config);
/** * cdx_bus_match - device to driver matching callback * @dev: the cdx device to match against * @drv: the device driver to search for matching cdx device * structures * * Return: true on success, false otherwise.
*/ staticint cdx_bus_match(struct device *dev, conststruct device_driver *drv)
{ struct cdx_device *cdx_dev = to_cdx_device(dev); conststruct cdx_driver *cdx_drv = to_cdx_driver(drv); conststruct cdx_device_id *found_id = NULL; conststruct cdx_device_id *ids;
if (cdx_dev->is_bus) returnfalse;
ids = cdx_drv->match_id_table;
/* When driver_override is set, only bind to the matching driver */ if (cdx_dev->driver_override && strcmp(cdx_dev->driver_override, drv->name)) returnfalse;
found_id = cdx_match_id(ids, cdx_dev); if (!found_id) returnfalse;
do { /* * In case override_only was set, enforce driver_override * matching.
*/ if (!found_id->override_only) returntrue; if (cdx_dev->driver_override) returntrue;
/* * Setup MSI device data so that generic MSI alloc/free can * be used by the device driver.
*/ if (IS_ENABLED(CONFIG_GENERIC_MSI_IRQ) && cdx->msi_domain) {
error = msi_setup_device_data(&cdx_dev->dev); if (error) return error;
}
ret = of_dma_configure_id(dev, cdx->dev->of_node, 0, &input_id); if (ret && ret != -EPROBE_DEFER) {
dev_err(dev, "of_dma_configure_id() failed\n"); return ret;
}
/* @cdx_drv may not be valid when we're called from the IOMMU layer */ if (!ret && dev->driver && !cdx_drv->driver_managed_dma) {
ret = iommu_device_use_default_domain(dev); if (ret)
arch_teardown_dma_ops(dev);
}
if (cdx_dev->is_bus) /* Reset all the devices attached to cdx bus */
ret = device_for_each_child(dev, NULL, reset_cdx_device); else
ret = cdx_dev_reset(dev);
return ret < 0 ? ret : count;
} static DEVICE_ATTR_WO(reset);
if (enable && cdx->ops->bus_enable)
ret = cdx->ops->bus_enable(cdx, cdx_dev->bus_num); elseif (!enable && cdx->ops->bus_disable)
ret = cdx->ops->bus_disable(cdx, cdx_dev->bus_num); else
ret = -EOPNOTSUPP;
/** * cdx_mmap_resource - map a CDX resource into user memory space * @fp: File pointer. Not used in this function, but required where * this API is registered as a callback. * @kobj: kobject for mapping * @attr: struct bin_attribute for the file being mapped * @vma: struct vm_area_struct passed into the mmap * * Use the regular CDX mapping routines to map a CDX resource into userspace. * * Return: true on success, false otherwise.
*/ staticint cdx_mmap_resource(struct file *fp, struct kobject *kobj, conststruct bin_attribute *attr, struct vm_area_struct *vma)
{ struct cdx_device *cdx_dev = to_cdx_device(kobj_to_dev(kobj)); int num = (unsignedlong)attr->private; struct resource *res; unsignedlong size;
res = &cdx_dev->res[num]; if (iomem_is_exclusive(res->start)) return -EINVAL;
/* Make sure the caller is mapping a valid resource for this device */
size = ((cdx_resource_len(cdx_dev, num) - 1) >> PAGE_SHIFT) + 1; if (vma->vm_pgoff + vma_pages(vma) > size) return -EINVAL;
/* * Map memory region and vm->vm_pgoff is expected to be an * offset within that region.
*/
vma->vm_page_prot = pgprot_device(vma->vm_page_prot);
vma->vm_pgoff += (cdx_resource_start(cdx_dev, num) >> PAGE_SHIFT);
vma->vm_ops = &cdx_phys_vm_ops; return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
vma->vm_end - vma->vm_start,
vma->vm_page_prot);
}
staticvoid cdx_destroy_res_attr(struct cdx_device *cdx_dev, int num)
{ int i;
/* removing the bin attributes */ for (i = 0; i < num; i++) { struct bin_attribute *res_attr;
ret = device_add(&cdx_dev->dev); if (ret) {
dev_err(&cdx_dev->dev, "cdx device add failed: %d", ret); goto fail;
}
/* Create resource<N> attributes */ for (i = 0; i < MAX_CDX_DEV_RESOURCES; i++) { if (cdx_resource_flags(cdx_dev, i) & IORESOURCE_MEM) { /* skip empty resources */ if (!cdx_resource_len(cdx_dev, i)) continue;
ret = cdx_create_res_attr(cdx_dev, i); if (ret != 0) {
dev_err(&cdx_dev->dev, "cdx device resource<%d> file creation failed: %d", i, ret); goto resource_create_fail;
}
}
}
cdx_device_debugfs_init(cdx_dev);
return 0;
resource_create_fail:
cdx_destroy_res_attr(cdx_dev, i);
device_del(&cdx_dev->dev);
fail: /* * Do not free cdx_dev here as it would be freed in * cdx_device_release() called from put_device().
*/
put_device(&cdx_dev->dev);
ret = device_add(&cdx_dev->dev); if (ret) {
dev_err(&cdx_dev->dev, "cdx bus device add failed: %d\n", ret); goto device_add_fail;
}
if (cdx->ops->bus_enable) {
ret = cdx->ops->bus_enable(cdx, bus_num); if (ret && ret != -EALREADY) {
dev_err(cdx->dev, "cdx bus enable failed: %d\n", ret); goto bus_enable_fail;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.