XRT Alveo driver overview
Subsystems (2)
v8 cover XRT Alveo driver overview
Hello,
This is V8 of patch series which adds management physical function driver for Xilinx Alveo PCIe accelerator cards.
https://www.xilinx.com/products/boards-and-kits/alveo.html
This driver is part of Xilinx Runtime (XRT) open source stack.
XILINX ALVEO PLATFORM ARCHITECTURE
Alveo PCIe FPGA based platforms have a static *shell* partition and a partial re-configurable *user* partition. The shell partition is automatically loaded from flash when host is booted and PCIe is enumerated by BIOS. Shell cannot be changed till the next cold reboot. The shell exposes two PCIe physical functions:
1. management physical function
2. user physical function
The patch series includes Documentation/xrt.rst which describes Alveo platform, XRT driver architecture and deployment model in more detail.
Users compile their high level design in C/C++/OpenCL or RTL into FPGA image using Vitis tools.
https://www.xilinx.com/products/design-tools/vitis/vitis-platform.html
The compiled image is packaged as xclbin which contains partial bitstream for the user partition and necessary metadata. Users can dynamically swap the image running on the user partition in order to switch between different workloads by loading different xclbins.
XRT DRIVERS FOR XILINX ALVEO
XRT Linux kernel driver *xrt-mgmt* binds to management physical function of Alveo platform. The modular driver framework is organized into several platform drivers which primarily handle the following functionality:
1. Loading firmware container also called xsabin at driver attach time 2. Loading of user compiled xclbin with FPGA Manager integration 3. Clock scaling of image running on user partition 4. In-band sensors: temp, voltage, power, etc. 5. Device reset and rescan
The platform drivers are packaged into *xrt-lib* helper module with well defined interfaces. The module provides a pseudo-bus implementation for the platform drivers. More details on the driver model can be found in Documentation/xrt.rst.
User physical function driver is not included in this patch series.
LIBFDT REQUIREMENT
XRT driver infrastructure uses Device Tree as a metadata format to discover HW subsystems in the Alveo PCIe device. The Device Tree schema used by XRT is documented in Documentation/xrt.rst.
TESTING AND VALIDATION
xrt-mgmt driver can be tested with full XRT open source stack which includes user space libraries, board utilities and (out of tree) first generation user physical function driver xocl. XRT open source runtime stack is available at https://github.com/Xilinx/XRT
Complete documentation for XRT open source stack including sections on Alveo/XRT security and platform architecture can be found here:
https://xilinx.github.io/XRT/master/html/index.html
https://xilinx.github.io/XRT/master/html/security.html
https://xilinx.github.io/XRT/master/html/platforms_partitions.html
Changes since v7:
- Followed review comment to remove non fpga subdevices:
clock, clkfrq, ucs, ddr_calibaration, devctl and vsec
- Collapsed include/uapi/linux/xrt/*.h into include/uapi/linux/fpga-xrt.h
- Cleaned up comments in fpga-xrt.h
- Fixed spelling errors in xrt.rst
Changes since v6:
- Resolved grammatical errors and cleaned up taxonomy in xrt.rst documentation.
- Fixed clang warnings.
- Updated code base to include v6 code review comments.
Changes since v5:
- Revert all changes 'mgnt/MGNT' back to 'mgmt/MGMT'
- Updated code base to include v5 code review comments. xrt.rst: address grammar and taxonomy subdev_id.h: defines XRT_SUBDEV_INVALID = 0 xclbin.c: change shift operation to be_to_cpu
- Resolved kernel test robot errors.
Changes since v4:
- Added xrt_bus_type and xrt_device. All sub devices were changed from platform_bus_type/platform_device to xrt_bus_type/xrt_device.
- Renamed xrt-mgmt driver to xrt-mgnt driver.
- Replaced 'MGMT' with 'MGNT' and 'mgmt' with 'mgnt' in code and file names
- Moved pci function calls from infrastructure to xrt-mgnt driver.
- Renamed files: mgmt/main.c -> mgnt/xmgnt-main.c
mgmt/main-region.c -> mgnt/xmgnt-main-region.c
include/xmgmt-main.h -> include/xmgnt-main.h
mgmt/fmgr-drv.c -> mgnt/xrt-mgr.c
mgmt/fmgr.h -> mgnt/xrt-mgr.h- Updated code base to include v4 code review comments.
Changes since v3:
- Leaf drivers use regmap-mmio to access hardware registers.
- Renamed driver module: xmgmt.ko -> xrt-mgmt.ko
- Renamed files: calib.[c|h] -> ddr_calibration.[c|h], lib/main.[c|h] -> lib/lib-drv.[c|h], mgmt/main-impl.h - > mgmt/xmgnt.h
- Updated code base to include v3 code review comments.
Changes since v2:
- Streamlined the driver framework into *xleaf*, *group* and *xroot*
- Updated documentation to show the driver model with examples
- Addressed kernel test robot errors
- Added a selftest for basic driver framework
- Documented device tree schema
- Removed need to export libfdt symbols
Changes since v1:
- Updated the driver to use fpga_region and fpga_bridge for FPGA programming
- Dropped platform drivers not related to PR programming to focus on XRT core framework
- Updated Documentation/fpga/xrt.rst with information on XRT core framework
- Addressed checkpatch issues
- Dropped xrt- prefix from some header files
For reference V7 version of patch series can be found here:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
Lizhi Hou (14):
Documentation: fpga: Add a document describing XRT Alveo drivers
fpga: xrt: driver metadata helper functions
fpga: xrt: xclbin file helper functions
fpga: xrt: xrt-lib driver manager
fpga: xrt: group driver
fpga: xrt: char dev node helper functions
fpga: xrt: root driver infrastructure
fpga: xrt: driver infrastructure
fpga: xrt: management physical function driver (root)
fpga: xrt: main driver for management function device
fpga: xrt: fpga-mgr and region implementation for xclbin download
fpga: xrt: ICAP driver
fpga: xrt: partition isolation driver
fpga: xrt: Kconfig and Makefile updates for XRT drivers
Documentation/fpga/index.rst | 1 + Documentation/fpga/xrt.rst | 870 ++++++++++++++++++ MAINTAINERS | 11 + drivers/Makefile | 1 + drivers/fpga/Kconfig | 2 + drivers/fpga/Makefile | 5 + drivers/fpga/xrt/Kconfig | 8 + drivers/fpga/xrt/include/events.h | 45 + drivers/fpga/xrt/include/group.h | 25 + drivers/fpga/xrt/include/metadata.h | 236 +++++ drivers/fpga/xrt/include/subdev_id.h | 39 + drivers/fpga/xrt/include/xclbin-helper.h | 48 + drivers/fpga/xrt/include/xdevice.h | 131 +++ drivers/fpga/xrt/include/xleaf.h | 205 +++++ drivers/fpga/xrt/include/xleaf/axigate.h | 23 + drivers/fpga/xrt/include/xleaf/clkfreq.h | 21 + drivers/fpga/xrt/include/xleaf/clock.h | 29 + .../fpga/xrt/include/xleaf/ddr_calibration.h | 28 + drivers/fpga/xrt/include/xleaf/devctl.h | 40 + drivers/fpga/xrt/include/xleaf/icap.h | 27 + drivers/fpga/xrt/include/xmgmt-main.h | 34 + drivers/fpga/xrt/include/xroot.h | 117 +++ drivers/fpga/xrt/lib/Kconfig | 17 + drivers/fpga/xrt/lib/Makefile | 24 + drivers/fpga/xrt/lib/cdev.c | 209 +++++ drivers/fpga/xrt/lib/group.c | 278 ++++++ drivers/fpga/xrt/lib/lib-drv.c | 322 +++++++ drivers/fpga/xrt/lib/lib-drv.h | 21 + drivers/fpga/xrt/lib/subdev.c | 859 +++++++++++++++++ drivers/fpga/xrt/lib/subdev_pool.h | 53 ++ drivers/fpga/xrt/lib/xclbin.c | 381 ++++++++ drivers/fpga/xrt/lib/xleaf/axigate.c | 325 +++++++ drivers/fpga/xrt/lib/xleaf/icap.c | 328 +++++++ drivers/fpga/xrt/lib/xroot.c | 536 +++++++++++ drivers/fpga/xrt/metadata/Kconfig | 12 + drivers/fpga/xrt/metadata/Makefile | 16 + drivers/fpga/xrt/metadata/metadata.c | 578 ++++++++++++ drivers/fpga/xrt/mgmt/Kconfig | 15 + drivers/fpga/xrt/mgmt/Makefile | 19 + drivers/fpga/xrt/mgmt/root.c | 420 +++++++++ drivers/fpga/xrt/mgmt/xmgmt-main-region.c | 483 ++++++++++ drivers/fpga/xrt/mgmt/xmgmt-main.c | 662 +++++++++++++ drivers/fpga/xrt/mgmt/xmgmt.h | 33 + drivers/fpga/xrt/mgmt/xrt-mgr.c | 190 ++++ drivers/fpga/xrt/mgmt/xrt-mgr.h | 16 + include/uapi/linux/fpga-xrt.h | 428 +++++++++ 46 files changed, 8171 insertions(+) create mode 100644 Documentation/fpga/xrt.rst create mode 100644 drivers/fpga/xrt/Kconfig create mode 100644 drivers/fpga/xrt/include/events.h create mode 100644 drivers/fpga/xrt/include/group.h create mode 100644 drivers/fpga/xrt/include/metadata.h create mode 100644 drivers/fpga/xrt/include/subdev_id.h create mode 100644 drivers/fpga/xrt/include/xclbin-helper.h create mode 100644 drivers/fpga/xrt/include/xdevice.h create mode 100644 drivers/fpga/xrt/include/xleaf.h create mode 100644 drivers/fpga/xrt/include/xleaf/axigate.h create mode 100644 drivers/fpga/xrt/include/xleaf/clkfreq.h create mode 100644 drivers/fpga/xrt/include/xleaf/clock.h create mode 100644 drivers/fpga/xrt/include/xleaf/ddr_calibration.h create mode 100644 drivers/fpga/xrt/include/xleaf/devctl.h create mode 100644 drivers/fpga/xrt/include/xleaf/icap.h create mode 100644 drivers/fpga/xrt/include/xmgmt-main.h create mode 100644 drivers/fpga/xrt/include/xroot.h create mode 100644 drivers/fpga/xrt/lib/Kconfig create mode 100644 drivers/fpga/xrt/lib/Makefile create mode 100644 drivers/fpga/xrt/lib/cdev.c create mode 100644 drivers/fpga/xrt/lib/group.c create mode 100644 drivers/fpga/xrt/lib/lib-drv.c create mode 100644 drivers/fpga/xrt/lib/lib-drv.h create mode 100644 drivers/fpga/xrt/lib/subdev.c create mode 100644 drivers/fpga/xrt/lib/subdev_pool.h create mode 100644 drivers/fpga/xrt/lib/xclbin.c create mode 100644 drivers/fpga/xrt/lib/xleaf/axigate.c create mode 100644 drivers/fpga/xrt/lib/xleaf/icap.c create mode 100644 drivers/fpga/xrt/lib/xroot.c create mode 100644 drivers/fpga/xrt/metadata/Kconfig create mode 100644 drivers/fpga/xrt/metadata/Makefile create mode 100644 drivers/fpga/xrt/metadata/metadata.c create mode 100644 drivers/fpga/xrt/mgmt/Kconfig create mode 100644 drivers/fpga/xrt/mgmt/Makefile create mode 100644 drivers/fpga/xrt/mgmt/root.c create mode 100644 drivers/fpga/xrt/mgmt/xmgmt-main-region.c create mode 100644 drivers/fpga/xrt/mgmt/xmgmt-main.c create mode 100644 drivers/fpga/xrt/mgmt/xmgmt.h create mode 100644 drivers/fpga/xrt/mgmt/xrt-mgr.c create mode 100644 drivers/fpga/xrt/mgmt/xrt-mgr.h create mode 100644 include/uapi/linux/fpga-xrt.h
Signature · 1 line
2.27.0
PATCH v8 1/14 Documentation: fpga: Add a document describing XRT Alveo drivers
Describe XRT driver architecture and provide basic overview of Xilinx Alveo platform.
--- Documentation/fpga/index.rst | 1 + Documentation/fpga/xrt.rst | 870 +++++++++++++++++++++++++++++++++++ MAINTAINERS | 11 + 3 files changed, 882 insertions(+) create mode 100644 Documentation/fpga/xrt.rst
Documentation/fpga/index.rst +1 -0
@@ -8,6 +8,7 @@ fpga :maxdepth: 1 dfl + xrt .. only:: subproject and html
Documentation/fpga/xrt.rst new file +870 -0
870 lines not on this page · raw message
MAINTAINERS +11 -0
@@ -7273,6 +7273,17 @@ F: Documentation/fpga/ F: drivers/fpga/ F: include/linux/fpga/ +FPGA XRT DRIVERS +M: Lizhi Hou <[email protected]> +R: Max Zhen <[email protected]> +R: Sonal Santan <[email protected]> +L: [email protected] +S: Supported +W: https://github.com/Xilinx/XRT +F: Documentation/fpga/xrt.rst +F: drivers/fpga/xrt/ +F: include/uapi/linux/xrt/ + FPU EMULATOR M: Bill Metzenthen <[email protected]> S: Maintained
Signature · 1 line
2.27.0
PATCH v8 2/14 fpga: xrt: driver metadata helper functions
In reply to Lizhi HouXRT drivers use device tree as metadata format to discover HW subsystems behind PCIe BAR. Thus libfdt functions are called for the driver to parse the device tree blob.
--- drivers/fpga/xrt/include/metadata.h | 236 +++++++++++ drivers/fpga/xrt/metadata/metadata.c | 578 +++++++++++++++++++++++++++ 2 files changed, 814 insertions(+) create mode 100644 drivers/fpga/xrt/include/metadata.h create mode 100644 drivers/fpga/xrt/metadata/metadata.c
drivers/fpga/xrt/include/metadata.h new file +236 -0
@@ -0,0 +1,236 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_METADATA_H +#define _XRT_METADATA_H + +#include <linux/device.h> +#include <linux/vmalloc.h> +#include <linux/uuid.h> + +#define XRT_MD_INVALID_LENGTH (~0U) + +/* metadata properties */ +#define XRT_MD_PROP_BAR_IDX "pcie_bar_mapping" +#define XRT_MD_PROP_COMPATIBLE "compatible" +#define XRT_MD_PROP_HWICAP "axi_hwicap" +#define XRT_MD_PROP_INTERFACE_UUID "interface_uuid" +#define XRT_MD_PROP_INTERRUPTS "interrupts" +#define XRT_MD_PROP_IO_OFFSET "reg" +#define XRT_MD_PROP_LOGIC_UUID "logic_uuid" +#define XRT_MD_PROP_PDI_CONFIG "pdi_config_mem" +#define XRT_MD_PROP_PF_NUM "pcie_physical_function" +#define XRT_MD_PROP_VERSION_MAJOR "firmware_version_major" + +/* non IP nodes */ +#define XRT_MD_NODE_ENDPOINTS "addressable_endpoints" +#define XRT_MD_NODE_FIRMWARE "firmware" +#define XRT_MD_NODE_INTERFACES "interfaces" +#define XRT_MD_NODE_PARTITION_INFO "partition_info" + +/* +* IP nodes +* AF: AXI Firewall +* CMC: Card Management Controller +* ERT: Embedded Runtime +* EP: End Point +* PLP: Provider Reconfigurable Partition +* ULP: User Reconfigurable Partition +*/ +#define XRT_MD_NODE_ADDR_TRANSLATOR "ep_remap_data_c2h_00" +#define XRT_MD_NODE_AF_BLP_CTRL_MGMT "ep_firewall_blp_ctrl_mgmt_00" +#define XRT_MD_NODE_AF_BLP_CTRL_USER "ep_firewall_blp_ctrl_user_00" +#define XRT_MD_NODE_AF_CTRL_DEBUG "ep_firewall_ctrl_debug_00" +#define XRT_MD_NODE_AF_CTRL_MGMT "ep_firewall_ctrl_mgmt_00" +#define XRT_MD_NODE_AF_CTRL_USER "ep_firewall_ctrl_user_00" +#define XRT_MD_NODE_AF_DATA_C2H "ep_firewall_data_c2h_00"/* c2h: card to host */ +#define XRT_MD_NODE_AF_DATA_H2C "ep_firewall_data_h2c_00"/* h2c: host to card */ +#define XRT_MD_NODE_AF_DATA_M2M "ep_firewall_data_m2m_00" +#define XRT_MD_NODE_AF_DATA_P2P "ep_firewall_data_p2p_00" +#define XRT_MD_NODE_CLKFREQ_HBM "ep_freq_cnt_aclk_hbm_00"/* hbm: High Bandwidth Memory */ +#define XRT_MD_NODE_CLKFREQ_K1 "ep_freq_cnt_aclk_kernel_00" +#define XRT_MD_NODE_CLKFREQ_K2 "ep_freq_cnt_aclk_kernel_01" +#define XRT_MD_NODE_CLK_KERNEL1 "ep_aclk_kernel_00" +#define XRT_MD_NODE_CLK_KERNEL2 "ep_aclk_kernel_01" +#define XRT_MD_NODE_CLK_KERNEL3 "ep_aclk_hbm_00" +#define XRT_MD_NODE_CLK_SHUTDOWN "ep_aclk_shutdown_00" +#define XRT_MD_NODE_CMC_FW_MEM "ep_cmc_firmware_mem_00" +#define XRT_MD_NODE_CMC_MUTEX "ep_cmc_mutex_00" +#define XRT_MD_NODE_CMC_REG "ep_cmc_regmap_00" +#define XRT_MD_NODE_CMC_RESET "ep_cmc_reset_00" +#define XRT_MD_NODE_DDR_CALIB "ep_ddr_mem_calib_00" +#define XRT_MD_NODE_DDR4_RESET_GATE "ep_ddr_mem_srsr_gate_00" +#define XRT_MD_NODE_ERT_BASE "ep_ert_base_address_00" +#define XRT_MD_NODE_ERT_CQ_MGMT "ep_ert_command_queue_mgmt_00" +#define XRT_MD_NODE_ERT_CQ_USER "ep_ert_command_queue_user_00" +#define XRT_MD_NODE_ERT_FW_MEM "ep_ert_firmware_mem_00" +#define XRT_MD_NODE_ERT_RESET "ep_ert_reset_00" +#define XRT_MD_NODE_ERT_SCHED "ep_ert_sched_00" +#define XRT_MD_NODE_FLASH "ep_card_flash_program_00" +#define XRT_MD_NODE_FPGA_CONFIG "ep_fpga_configuration_00" +#define XRT_MD_NODE_GAPPING "ep_gapping_demand_00" +#define XRT_MD_NODE_GATE_PLP "ep_pr_isolate_plp_00" +#define XRT_MD_NODE_GATE_ULP "ep_pr_isolate_ulp_00" +#define XRT_MD_NODE_KDMA_CTRL "ep_kdma_ctrl_00" +#define XRT_MD_NODE_MAILBOX_MGMT "ep_mailbox_mgmt_00" +#define XRT_MD_NODE_MAILBOX_USER "ep_mailbox_user_00" +#define XRT_MD_NODE_MAILBOX_XRT "ep_mailbox_user_to_ert_00" +#define XRT_MD_NODE_MSIX "ep_msix_00" +#define XRT_MD_NODE_P2P "ep_p2p_00" +#define XRT_MD_NODE_PCIE_MON "ep_pcie_link_mon_00" +#define XRT_MD_NODE_PMC_INTR "ep_pmc_intr_00" +#define XRT_MD_NODE_PMC_MUX "ep_pmc_mux_00" +#define XRT_MD_NODE_QDMA "ep_qdma_00" +#define XRT_MD_NODE_QDMA4 "ep_qdma4_00" +#define XRT_MD_NODE_REMAP_P2P "ep_remap_p2p_00" +#define XRT_MD_NODE_STM "ep_stream_traffic_manager_00" +#define XRT_MD_NODE_STM4 "ep_stream_traffic_manager4_00" +#define XRT_MD_NODE_SYSMON "ep_cmp_sysmon_00" +#define XRT_MD_NODE_XDMA "ep_xdma_00" +#define XRT_MD_NODE_XVC_PUB "ep_debug_bscan_user_00" +#define XRT_MD_NODE_XVC_PRI "ep_debug_bscan_mgmt_00" +#define XRT_MD_NODE_UCS_CONTROL_STATUS "ep_ucs_control_status_00" + +/* endpoint compatible string */ +#define XRT_MD_COMPAT_DDR_SRSR "drv_ddr_srsr" +#define XRT_MD_COMPAT_CLKFREQ "freq_cnt" + +/* driver defined endpoints */ +#define XRT_MD_NODE_BLP_ROM "drv_ep_blp_rom_00" +#define XRT_MD_NODE_DDR_SRSR "drv_ep_ddr_srsr" +#define XRT_MD_NODE_FLASH_VSEC "drv_ep_card_flash_program_00" +#define XRT_MD_NODE_GOLDEN_VER "drv_ep_golden_ver_00" +#define XRT_MD_NODE_MAILBOX_VSEC "drv_ep_mailbox_vsec_00" +#define XRT_MD_NODE_MGMT_MAIN "drv_ep_mgmt_main_00" +#define XRT_MD_NODE_PLAT_INFO "drv_ep_platform_info_mgmt_00" +#define XRT_MD_NODE_PARTITION_INFO_BLP "partition_info_0" +#define XRT_MD_NODE_PARTITION_INFO_PLP "partition_info_1" +#define XRT_MD_NODE_TEST "drv_ep_test_00" +#define XRT_MD_NODE_VSEC "drv_ep_vsec_00" +#define XRT_MD_NODE_VSEC_GOLDEN "drv_ep_vsec_golden_00" + +/* driver defined properties */ +#define XRT_MD_PROP_OFFSET "drv_offset" +#define XRT_MD_PROP_CLK_FREQ "drv_clock_frequency" +#define XRT_MD_PROP_CLK_CNT "drv_clock_frequency_counter" +#define XRT_MD_PROP_VBNV "vbnv" +#define XRT_MD_PROP_VROM "vrom" +#define XRT_MD_PROP_PARTITION_LEVEL "partition_level" + +struct xrt_md_endpoint { +const char *ep_name; +u32 bar_index; +u64 bar_off; +u64 size; +char *compat; +char *compat_ver; +}; + +/* Note: res_id is defined by leaf driver and must start with 0. */ +struct xrt_iores_map { +char *res_name; +int res_id; +}; + +static inline int xrt_md_res_name2id (const struct xrt_iores_map *res_map, +int entry_num,const char *res_name) +{ +int i; + +for (i =0 ; i < entry_num; i++) { +if (!strncmp (res_name, res_map->res_name,strlen (res_map->res_name) +1 )) +return res_map->res_id; + res_map++; + } +return -1 ; +} + +static inline const char * +xrt_md_res_id2name (const struct xrt_iores_map *res_map,int entry_num,int id) +{ +int i; + +for (i =0 ; i < entry_num; i++) { +if (res_map->res_id == id) +return res_map->res_name; + res_map++; + } +return NULL ; +} + +u32 xrt_md_size (struct device *dev,const char *blob); +int xrt_md_create (struct device *dev,char **blob); +char *xrt_md_dup (struct device *dev,const char *blob); +int xrt_md_add_endpoint (struct device *dev,char *blob, +struct xrt_md_endpoint *ep); +int xrt_md_del_endpoint (struct device *dev,char *blob,const char *ep_name, +const char *compat); +int xrt_md_get_prop (struct device *dev,const char *blob,const char *ep_name, +const char *compat,const char *prop, +const void **val,int *size); +int xrt_md_set_prop (struct device *dev,char *blob,const char *ep_name, +const char *compat,const char *prop, +const void *val,int size); +int xrt_md_copy_endpoint (struct device *dev,char *blob,const char *src_blob, +const char *ep_name,const char *compat, +const char *new_ep_name); +int xrt_md_get_next_endpoint (struct device *dev,const char *blob, +const char *ep_name,const char *compat, +char **next_ep,char **next_compat); +int xrt_md_get_compatible_endpoint (struct device *dev,const char *blob, +const char *compat,const char **ep_name); +int xrt_md_find_endpoint (struct device *dev,const char *blob, +const char *ep_name,const char *compat, +const char **epname); +int xrt_md_pack (struct device *dev,char *blob); +int xrt_md_get_interface_uuids (struct device *dev,const char *blob, +u32 num_uuids,uuid_t *intf_uuids); + +/* +* The firmware provides a 128 bit hash string as a unique id to the +* partition/interface. +* Existing hw does not yet use the canonical form, so it is necessary to +* use a translation function. +*/ +static inline void xrt_md_trans_uuid2str (const uuid_t *uuid,char *uuidstr) +{ +int i, p; +u8 tmp[UUID_SIZE ]; + +BUILD_BUG_ON (UUID_SIZE !=16 ); +export_uuid (tmp, uuid); +for (p =0 , i =UUID_SIZE -1 ; i >=0 ; p++, i--) +snprintf (&uuidstr[p *2 ],3 ,"%02x" , tmp[i]); +} + +static inline int xrt_md_trans_str2uuid (struct device *dev,const char *uuidstr,uuid_t *p_uuid) +{ +u8 p[UUID_SIZE ]; +const char *str; +char tmp[3 ] = {0 }; +int i, ret; + +if (strlen (uuidstr) !=UUID_SIZE *2 ) +return -EINVAL ; + + str = uuidstr +strlen (uuidstr) -2 ; + +for (i =0 ; i <sizeof (*p_uuid) && str >= uuidstr; i++) { + tmp[0 ] = *str; + tmp[1 ] = *(str +1 ); + ret =kstrtou8 (tmp,16 , &p[i]); +if (ret) +return -EINVAL ; + str -=2 ; + } +import_uuid (p_uuid, p); + +return 0 ; +} + +#endif
drivers/fpga/xrt/metadata/metadata.c new file +578 -0
578 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v8 3/14 fpga: xrt: xclbin file helper functions
In reply to Lizhi HouAlveo FPGA firmware and partial reconfigure file are in xclbin format. This code enumerates and extracts sections from xclbin files. xclbin.h is cross platform and used across all platforms and OS.
--- drivers/fpga/xrt/include/xclbin-helper.h | 48 +++ drivers/fpga/xrt/lib/xclbin.c | 381 ++++++++++++++++++++ include/uapi/linux/fpga-xrt.h | 428 +++++++++++++++++++++++ 3 files changed, 857 insertions(+) create mode 100644 drivers/fpga/xrt/include/xclbin-helper.h create mode 100644 drivers/fpga/xrt/lib/xclbin.c create mode 100644 include/uapi/linux/fpga-xrt.h
drivers/fpga/xrt/include/xclbin-helper.h new file +48 -0
@@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* David Zhang <[email protected]> +* Sonal Santan <[email protected]> +*/ + +#ifndef _XCLBIN_HELPER_H_ +#define _XCLBIN_HELPER_H_ + +#include <linux/types.h> +#include <linux/device.h> +#include <linux/fpga-xrt.h> + +#define XCLBIN_VERSION2 "xclbin2" +#define XCLBIN_HWICAP_BITFILE_BUF_SZ 1024 +#define XCLBIN_MAX_SZ_1G (1024 * 1024 * 1024)/* Assuming xclbin <= 1G, always */ + +enum axlf_section_kind ; +struct axlf ; + +/** +* Bitstream header information as defined by Xilinx tools. +* Please note that this struct definition is not owned by the driver. +*/ +struct xclbin_bit_head_info { +u32 header_length;/* Length of header in 32 bit words */ +u32 bitstream_length;/* Length of bitstream to read in bytes */ +const unchar *design_name;/* Design name get from bitstream */ +const unchar *part_name;/* Part name read from bitstream */ +const unchar *date;/* Date read from bitstream header */ +const unchar *time;/* Bitstream creation time */ +u32 magic_length;/* Length of the magic numbers */ +const unchar *version;/* Version string */ +}; + +/* caller must free the allocated memory for **data. len could be NULL. */ +int xrt_xclbin_get_section (struct device *dev,const struct axlf *xclbin, +enum axlf_section_kind kind,void **data, +uint64_t *len); +int xrt_xclbin_get_metadata (struct device *dev,const struct axlf *xclbin,char **dtb); +int xrt_xclbin_parse_bitstream_header (struct device *dev,const unchar *data, +u32 size,struct xclbin_bit_head_info *head_info); +const char *xrt_clock_type2epname (enum XCLBIN_CLOCK_TYPE type); + +#endif /* _XCLBIN_HELPER_H_ */
drivers/fpga/xrt/lib/xclbin.c new file +381 -0
381 lines not on this page · raw message
include/uapi/linux/fpga-xrt.h new file +428 -0
428 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v8 4/14 fpga: xrt: xrt-lib driver manager
In reply to Lizhi Houxrt-lib kernel module infrastructure code to register and manage all leaf driver modules.
--- drivers/fpga/xrt/include/subdev_id.h | 39 +++ drivers/fpga/xrt/include/xdevice.h | 131 +++++++ drivers/fpga/xrt/include/xleaf.h | 205 +++++++++++ drivers/fpga/xrt/include/xleaf/clkfreq.h | 21 ++ drivers/fpga/xrt/include/xleaf/clock.h | 29 ++ .../fpga/xrt/include/xleaf/ddr_calibration.h | 28 ++ drivers/fpga/xrt/include/xleaf/devctl.h | 40 +++ drivers/fpga/xrt/lib/lib-drv.c | 322 ++++++++++++++++++ drivers/fpga/xrt/lib/lib-drv.h | 21 ++ 9 files changed, 836 insertions(+) create mode 100644 drivers/fpga/xrt/include/subdev_id.h create mode 100644 drivers/fpga/xrt/include/xdevice.h create mode 100644 drivers/fpga/xrt/include/xleaf.h create mode 100644 drivers/fpga/xrt/include/xleaf/clkfreq.h create mode 100644 drivers/fpga/xrt/include/xleaf/clock.h create mode 100644 drivers/fpga/xrt/include/xleaf/ddr_calibration.h create mode 100644 drivers/fpga/xrt/include/xleaf/devctl.h create mode 100644 drivers/fpga/xrt/lib/lib-drv.c create mode 100644 drivers/fpga/xrt/lib/lib-drv.h
drivers/fpga/xrt/include/subdev_id.h new file +39 -0
@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XRT_SUBDEV_ID_H_ +#define _XRT_SUBDEV_ID_H_ + +/* +* Every subdev driver has an ID for others to refer to it. There can be multiple number of +* instances of a subdev driver. A <subdev_id, subdev_instance> tuple is a unique identification +* of a specific instance of a subdev driver. +*/ +enum xrt_subdev_id { +XRT_SUBDEV_INVALID =0 , +XRT_SUBDEV_GRP , +XRT_SUBDEV_VSEC , +XRT_SUBDEV_VSEC_GOLDEN , +XRT_SUBDEV_DEVCTL , +XRT_SUBDEV_AXIGATE , +XRT_SUBDEV_ICAP , +XRT_SUBDEV_TEST , +XRT_SUBDEV_MGMT_MAIN , +XRT_SUBDEV_QSPI , +XRT_SUBDEV_MAILBOX , +XRT_SUBDEV_CMC , +XRT_SUBDEV_CALIB , +XRT_SUBDEV_CLKFREQ , +XRT_SUBDEV_CLOCK , +XRT_SUBDEV_SRSR , +XRT_SUBDEV_UCS , +XRT_SUBDEV_NUM ,/* Total number of subdevs. */ +XRT_ROOT =-1 ,/* Special ID for root driver. */ +}; + +#endif /* _XRT_SUBDEV_ID_H_ */
drivers/fpga/xrt/include/xdevice.h new file +131 -0
131 lines not on this page · raw message
drivers/fpga/xrt/include/xleaf.h new file +205 -0
205 lines not on this page · raw message
drivers/fpga/xrt/include/xleaf/clkfreq.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_CLKFREQ_H_ +#define _XRT_CLKFREQ_H_ + +#include "xleaf.h" + +/* +* CLKFREQ driver leaf calls. +*/ +enum xrt_clkfreq_leaf_cmd { +XRT_CLKFREQ_READ =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +}; + +#endif /* _XRT_CLKFREQ_H_ */
drivers/fpga/xrt/include/xleaf/clock.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_CLOCK_H_ +#define _XRT_CLOCK_H_ + +#include "xleaf.h" +#include <linux/fpga_xrt.h> + +/* +* CLOCK driver leaf calls. +*/ +enum xrt_clock_leaf_cmd { +XRT_CLOCK_SET =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +XRT_CLOCK_GET , +XRT_CLOCK_VERIFY , +}; + +struct xrt_clock_get { +u16 freq; +u32 freq_cnter; +}; + +#endif /* _XRT_CLOCK_H_ */
drivers/fpga/xrt/include/xleaf/ddr_calibration.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XRT_DDR_CALIBRATION_H_ +#define _XRT_DDR_CALIBRATION_H_ + +#include "xleaf.h" +#include <linux/fpga-xrt.h> + +/* +* Memory calibration driver leaf calls. +*/ +enum xrt_calib_results { +XRT_CALIB_UNKNOWN =0 , +XRT_CALIB_SUCCEEDED , +XRT_CALIB_FAILED , +}; + +enum xrt_calib_leaf_cmd { +XRT_CALIB_RESULT =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +}; + +#endif /* _XRT_DDR_CALIBRATION_H_ */
drivers/fpga/xrt/include/xleaf/devctl.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_DEVCTL_H_ +#define _XRT_DEVCTL_H_ + +#include "xleaf.h" + +/* +* DEVCTL driver leaf calls. +*/ +enum xrt_devctl_leaf_cmd { +XRT_DEVCTL_READ =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +}; + +enum xrt_devctl_id { +XRT_DEVCTL_ROM_UUID =0 , +XRT_DEVCTL_DDR_CALIB , +XRT_DEVCTL_GOLDEN_VER , +XRT_DEVCTL_MAX +}; + +struct xrt_devctl_rw { +u32 xdr_id; +void *xdr_buf; +u32 xdr_len; +u32 xdr_offset; +}; + +struct xrt_devctl_intf_uuid { +u32 uuid_num; +uuid_t *uuids; +}; + +#endif /* _XRT_DEVCTL_H_ */
drivers/fpga/xrt/lib/lib-drv.c new file +322 -0
322 lines not on this page · raw message
drivers/fpga/xrt/lib/lib-drv.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _LIB_DRV_H_ +#define _LIB_DRV_H_ + +#include <linux/device/class.h> +#include <linux/device/bus.h> + +extern struct class *xrt_class; +extern struct bus_type xrt_bus_type; + +const char *xrt_drv_name (enum xrt_subdev_id id); +struct xrt_dev_endpoints *xrt_drv_get_endpoints (enum xrt_subdev_id id); + +#endif /* _LIB_DRV_H_ */
Signature · 1 line
2.27.0
Moritz Fischer
maintainerRe: [PATCH V8 XRT Alveo 04/14] fpga: xrt: xrt-lib driver manager
Quoted 96 lines · Lizhi Hou
xrt-lib kernel module infrastructure code to register and manage all leaf driver modules.
Signed-off-by Sonal Santan <[email protected]>Signed-off-by Max Zhen <[email protected]>Signed-off-by Lizhi Hou <[email protected]>Reviewed-by Tom Rix <[email protected]>--- drivers/fpga/xrt/include/subdev_id.h | 39 +++ drivers/fpga/xrt/include/xdevice.h | 131 +++++++ drivers/fpga/xrt/include/xleaf.h | 205 +++++++++++ drivers/fpga/xrt/include/xleaf/clkfreq.h | 21 ++ drivers/fpga/xrt/include/xleaf/clock.h | 29 ++ .../fpga/xrt/include/xleaf/ddr_calibration.h | 28 ++ drivers/fpga/xrt/include/xleaf/devctl.h | 40 +++ drivers/fpga/xrt/lib/lib-drv.c | 322 ++++++++++++++++++ drivers/fpga/xrt/lib/lib-drv.h | 21 ++ 9 files changed, 836 insertions(+) create mode 100644 drivers/fpga/xrt/include/subdev_id.h create mode 100644 drivers/fpga/xrt/include/xdevice.h create mode 100644 drivers/fpga/xrt/include/xleaf.h create mode 100644 drivers/fpga/xrt/include/xleaf/clkfreq.h create mode 100644 drivers/fpga/xrt/include/xleaf/clock.h create mode 100644 drivers/fpga/xrt/include/xleaf/ddr_calibration.h create mode 100644 drivers/fpga/xrt/include/xleaf/devctl.h create mode 100644 drivers/fpga/xrt/lib/lib-drv.c create mode 100644 drivers/fpga/xrt/lib/lib-drv.hdrivers/fpga/xrt/include/subdev_id.h new file +39
-0@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XRT_SUBDEV_ID_H_ +#define _XRT_SUBDEV_ID_H_ + +/* +* Every subdev driver has an ID for others to refer to it. There can be multiple number of +* instances of a subdev driver. A <subdev_id, subdev_instance> tuple is a unique identification +* of a specific instance of a subdev driver. +*/ +enum xrt_subdev_id { +XRT_SUBDEV_INVALID =0 , +XRT_SUBDEV_GRP , +XRT_SUBDEV_VSEC , +XRT_SUBDEV_VSEC_GOLDEN , +XRT_SUBDEV_DEVCTL , +XRT_SUBDEV_AXIGATE , +XRT_SUBDEV_ICAP , +XRT_SUBDEV_TEST , +XRT_SUBDEV_MGMT_MAIN , +XRT_SUBDEV_QSPI , +XRT_SUBDEV_MAILBOX , +XRT_SUBDEV_CMC , +XRT_SUBDEV_CALIB , +XRT_SUBDEV_CLKFREQ , +XRT_SUBDEV_CLOCK , +XRT_SUBDEV_SRSR , +XRT_SUBDEV_UCS , +XRT_SUBDEV_NUM ,/* Total number of subdevs. */ +XRT_ROOT =-1 ,/* Special ID for root driver. */ +}; + +#endif /* _XRT_SUBDEV_ID_H_ */ drivers/fpga/xrt/include/xdevice.h new file +17
-0@@ -0,0 +1,131 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_DEVICE_H_ +#define _XRT_DEVICE_H_ + +#include <linux/fs.h> +#include <linux/cdev.h> + +#define XRT_MAX_DEVICE_NODES 128 +#define XRT_INVALID_DEVICE_INST (XRT_MAX_DEVICE_NODES + 1) +
drivers/fpga/xrt/include/xdevice.h line 27
Quoted 10 lines
drivers/fpga/xrt/include/xdevice.h new file +10
-0+enum { +XRT_DEVICE_STATE_NONE =0 , +XRT_DEVICE_STATE_ADDED +}; + +/* +* struct xrt_device - represent an xrt device on xrt bus +* +* dev: generic device interface. +* id: id of the xrt device.
Shouldn't this document the rest of the members?
Quoted 25 lines
drivers/fpga/xrt/include/xdevice.h new file +25
-0+*/ +struct xrt_device { +struct device dev; +u32 subdev_id; +const char *name; +u32 instance; +u32 state; +u32 num_resources; +struct resource *resource; +void *sdev_data; +}; + +/* +* If populated by xrt device driver, infra will handle the mechanics of +* char device (un)registration. +*/ +enum xrt_dev_file_mode { +/* Infra create cdev, default file name */ +XRT_DEV_FILE_DEFAULT =0 , +/* Infra create cdev, need to encode inst num in file name */ +XRT_DEV_FILE_MULTI_INST , +/* No auto creation of cdev by infra, leaf handles it by itself */ +XRT_DEV_FILE_NO_AUTO , +}; +
drivers/fpga/xrt/include/xdevice.h line 62
Quoted 10 lines
drivers/fpga/xrt/include/xdevice.h new file +10
-0+struct xrt_dev_file_ops { +const struct file_operations xsf_ops; +dev_t xsf_dev_t; +const char *xsf_dev_name; +enum xrt_dev_file_mode xsf_mode; +}; + +/* +* this struct define the endpoints belong to the same xrt device +*/
ep_name: ... etc?
Quoted 5 lines
drivers/fpga/xrt/include/xdevice.h new file +5
-0+struct xrt_dev_ep_names { +const char *ep_name; +const char *compat; +}; +
drivers/fpga/xrt/include/xdevice.h line 77
Quoted 10 lines
drivers/fpga/xrt/include/xdevice.h new file +10
-0+struct xrt_dev_endpoints { +struct xrt_dev_ep_names *xse_names; +/* minimum number of endpoints to support the subdevice */ +u32 xse_min_ep; +}; + +/* +* struct xrt_driver - represent a xrt device driver +* +* drv: driver model structure.
drv vs driver?
drivers/fpga/xrt/include/xdevice.h line 79
Quoted 2 lines
drivers/fpga/xrt/include/xdevice.h new file +2
-0+* id_table: pointer to table of device IDs the driver is interested in. +* { } member terminated.
?file_ops?
Quoted 419 lines
drivers/fpga/xrt/include/xdevice.h new file +52
-0+* probe: mandatory callback for device binding. +* remove: callback for device unbinding. +*/ +struct xrt_driver { +struct device_driver driver; +u32 subdev_id; +struct xrt_dev_file_ops file_ops; +struct xrt_dev_endpoints *endpoints; + +/* +* Subdev driver callbacks populated by subdev driver. +*/ +int (*probe)(struct xrt_device *xrt_dev); +void (*remove)(struct xrt_device *xrt_dev); +/* +* If leaf_call is defined, these are called by other leaf drivers. +* Note that root driver may call into leaf_call of a group driver. +*/ +int (*leaf_call)(struct xrt_device *xrt_dev,u32 cmd,void *arg); +}; + +#define to_xrt_dev (d) container_of(d, struct xrt_device, dev) +#define to_xrt_drv (d) container_of(d, struct xrt_driver, driver) + +static inline void *xrt_get_drvdata (const struct xrt_device *xdev) +{ +return dev_get_drvdata (&xdev->dev); +} + +static inline void xrt_set_drvdata (struct xrt_device *xdev,void *data) +{ +dev_set_drvdata (&xdev->dev, data); +} + +static inline void *xrt_get_xdev_data (struct device *dev) +{ +struct xrt_device *xdev =to_xrt_dev (dev); + +return xdev->sdev_data; +} + +struct xrt_device * +xrt_device_register (struct device *parent,u32 id, +struct resource *res,u32 res_num, +void *pdata,size_t data_sz); +void xrt_device_unregister (struct xrt_device *xdev); +int xrt_register_driver (struct xrt_driver *drv); +void xrt_unregister_driver (struct xrt_driver *drv); +void *xrt_get_xdev_data (struct device *dev); +struct resource *xrt_get_resource (struct xrt_device *xdev,u32 type,u32 num); + +#endif /* _XRT_DEVICE_H_ */ drivers/fpga/xrt/include/xleaf.h new file +205
-0@@ -0,0 +1,205 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +* Sonal Santan <[email protected]> +*/ + +#ifndef _XRT_XLEAF_H_ +#define _XRT_XLEAF_H_ + +#include <linux/mod_devicetable.h> +#include "xdevice.h" +#include "subdev_id.h" +#include "xroot.h" +#include "events.h" + +/* All subdev drivers should use below common routines to print out msg. */ +#define DEV (xdev) (&(xdev)->dev) +#define DEV_PDATA (xdev) \ + ((struct xrt_subdev_platdata *)xrt_get_xdev_data(DEV(xdev))) +#define DEV_FILE_OPS (xdev) \ + (&(to_xrt_drv((xdev)->dev.driver))->file_ops) +#define FMT_PRT(prt_fn, xdev, fmt, args...) \ + ({typeof(xdev) (_xdev) = (xdev); \ + prt_fn(DEV(_xdev), "%s %s: " fmt, \ + DEV_PDATA(_xdev)->xsp_root_name, __func__, ##args); }) +#define xrt_err(xdev, fmt, args...) FMT_PRT(dev_err, xdev, fmt, ##args) +#define xrt_warn(xdev, fmt, args...) FMT_PRT(dev_warn, xdev, fmt, ##args) +#define xrt_info(xdev, fmt, args...) FMT_PRT(dev_info, xdev, fmt, ##args) +#define xrt_dbg(xdev, fmt, args...) FMT_PRT(dev_dbg, xdev, fmt, ##args) + +#define XRT_DEFINE_REGMAP_CONFIG (config_name) \ + static const struct regmap_config config_name = { \ + .reg_bits = 32, \ + .val_bits = 32, \ + .reg_stride = 4, \ + .max_register = 0x1000, \ + } + +enum { +/* Starting cmd for common leaf cmd implemented by all leaves. */ +XRT_XLEAF_COMMON_BASE =0 , +/* Starting cmd for leaves' specific leaf cmds. */ +XRT_XLEAF_CUSTOM_BASE =64 , +}; + +enum xrt_xleaf_common_leaf_cmd { +XRT_XLEAF_EVENT =XRT_XLEAF_COMMON_BASE , +}; + +/* +* Partially initialized by the parent driver, then, passed in as subdev driver's +* platform data when creating subdev driver instance by calling platform +* device register API (xrt_device_register_data() or the likes). +* +* Once device register API returns, platform driver framework makes a copy of +* this buffer and maintains its life cycle. The content of the buffer is +* completely owned by subdev driver. +* +* Thus, parent driver should be very careful when it touches this buffer +* again once it's handed over to subdev driver. And the data structure +* should not contain pointers pointing to buffers that is managed by +* other or parent drivers since it could have been freed before platform +* data buffer is freed by platform driver framework. +*/ +struct xrt_subdev_platdata { +/* +* Per driver instance callback. The xdev points to the instance. +* Should always be defined for subdev driver to get service from root. +*/ +xrt_subdev_root_cb_t xsp_root_cb; +void *xsp_root_cb_arg; + +/* Something to associate w/ root for msg printing. */ +const char *xsp_root_name; + +/* +* Char dev support for this subdev instance. +* Initialized by subdev driver. +*/ +struct cdev xsp_cdev; +struct device *xsp_sysdev; +struct mutex xsp_devnode_lock;/* devnode lock */ +struct completion xsp_devnode_comp; +int xsp_devnode_ref; +bool xsp_devnode_online; +bool xsp_devnode_excl; + +/* +* Subdev driver specific init data. The buffer should be embedded +* in this data structure buffer after dtb, so that it can be freed +* together with platform data. +*/ +loff_t xsp_priv_off;/* Offset into this platform data buffer. */ +size_t xsp_priv_len; + +/* +* Populated by parent driver to describe the device tree for +* the subdev driver to handle. Should always be last one since it's +* of variable length. +*/ +bool xsp_dtb_valid; +char xsp_dtb[0 ]; +}; + +struct subdev_match_arg { +enum xrt_subdev_id id; +int instance; +}; + +bool xleaf_has_endpoint (struct xrt_device *xdev,const char *endpoint_name); +struct xrt_device *xleaf_get_leaf (struct xrt_device *xdev, +xrt_subdev_match_t cb,void *arg); + +static inline bool subdev_match (enum xrt_subdev_id id,struct xrt_device *xdev,void *arg) +{ +const struct subdev_match_arg *a = (struct subdev_match_arg *)arg; +int instance = a->instance; + +if (id != a->id) +return false; +if (instance != xdev->instance && instance !=XRT_INVALID_DEVICE_INST ) +return false; +return true; +} + +static inline bool xrt_subdev_match_epname (enum xrt_subdev_id id, +struct xrt_device *xdev,void *arg) +{ +return xleaf_has_endpoint (xdev, arg); +} + +static inline struct xrt_device * +xleaf_get_leaf_by_id (struct xrt_device *xdev, +enum xrt_subdev_id id,int instance) +{ +struct subdev_match_arg arg = { id, instance }; + +return xleaf_get_leaf (xdev, subdev_match, &arg); +} + +static inline struct xrt_device * +xleaf_get_leaf_by_epname (struct xrt_device *xdev,const char *name) +{ +return xleaf_get_leaf (xdev, xrt_subdev_match_epname, (void *)name); +} + +static inline int xleaf_call (struct xrt_device *tgt,u32 cmd,void *arg) +{ +return (to_xrt_drv (tgt->dev.driver)->leaf_call)(tgt, cmd, arg); +} + +int xleaf_broadcast_event (struct xrt_device *xdev,enum xrt_events evt,bool async); +int xleaf_create_group (struct xrt_device *xdev,char *dtb); +int xleaf_destroy_group (struct xrt_device *xdev,int instance); +void xleaf_get_root_res (struct xrt_device *xdev,u32 region_id,struct resource **res); +void xleaf_get_root_id (struct xrt_device *xdev,unsigned short *vendor,unsigned short *device, +unsigned short *subvendor,unsigned short *subdevice); +void xleaf_hot_reset (struct xrt_device *xdev); +int xleaf_put_leaf (struct xrt_device *xdev,struct xrt_device *leaf); +struct device *xleaf_register_hwmon (struct xrt_device *xdev,const char *name,void *drvdata, +const struct attribute_group **grps); +void xleaf_unregister_hwmon (struct xrt_device *xdev,struct device *hwmon); +int xleaf_wait_for_group_bringup (struct xrt_device *xdev); + +/* +* Character device helper APIs for use by leaf drivers +*/ +static inline bool xleaf_devnode_enabled (struct xrt_device *xdev) +{ +return DEV_FILE_OPS (xdev)->xsf_ops.open; +} + +int xleaf_devnode_create (struct xrt_device *xdev, +const char *file_name,const char *inst_name); +void xleaf_devnode_destroy (struct xrt_device *xdev); + +struct xrt_device *xleaf_devnode_open_excl (struct inode *inode); +struct xrt_device *xleaf_devnode_open (struct inode *inode); +void xleaf_devnode_close (struct inode *inode); + +/* Module's init/fini routines for leaf driver in xrt-lib module */ +#define XRT_LEAF_INIT_FINI_FUNC (name) \ +void name##_leaf_init_fini(bool init) \ +{ \ + if (init) \ + xrt_register_driver(&xrt_##name##_driver); \ + else \ + xrt_unregister_driver(&xrt_##name##_driver); \ +} + +/* Module's init/fini routines for leaf driver in xrt-lib module */ +void group_leaf_init_fini (bool init); +void vsec_leaf_init_fini (bool init); +void devctl_leaf_init_fini (bool init); +void axigate_leaf_init_fini (bool init); +void icap_leaf_init_fini (bool init); +void calib_leaf_init_fini (bool init); +void clkfreq_leaf_init_fini (bool init); +void clock_leaf_init_fini (bool init); +void ucs_leaf_init_fini (bool init); + +#endif /* _XRT_LEAF_H_ */ drivers/fpga/xrt/include/xleaf/clkfreq.h new file +21
-0@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_CLKFREQ_H_ +#define _XRT_CLKFREQ_H_ + +#include "xleaf.h" + +/* +* CLKFREQ driver leaf calls. +*/ +enum xrt_clkfreq_leaf_cmd { +XRT_CLKFREQ_READ =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +}; + +#endif /* _XRT_CLKFREQ_H_ */ drivers/fpga/xrt/include/xleaf/clock.h new file +29
-0@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_CLOCK_H_ +#define _XRT_CLOCK_H_ + +#include "xleaf.h" +#include <linux/fpga_xrt.h> + +/* +* CLOCK driver leaf calls. +*/ +enum xrt_clock_leaf_cmd { +XRT_CLOCK_SET =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +XRT_CLOCK_GET , +XRT_CLOCK_VERIFY , +}; + +struct xrt_clock_get { +u16 freq; +u32 freq_cnter; +}; + +#endif /* _XRT_CLOCK_H_ */ drivers/fpga/xrt/include/xleaf/ddr_calibration.h new file +28
-0@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XRT_DDR_CALIBRATION_H_ +#define _XRT_DDR_CALIBRATION_H_ + +#include "xleaf.h" +#include <linux/fpga-xrt.h> + +/* +* Memory calibration driver leaf calls. +*/ +enum xrt_calib_results { +XRT_CALIB_UNKNOWN =0 , +XRT_CALIB_SUCCEEDED , +XRT_CALIB_FAILED , +}; + +enum xrt_calib_leaf_cmd { +XRT_CALIB_RESULT =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +}; + +#endif /* _XRT_DDR_CALIBRATION_H_ */ drivers/fpga/xrt/include/xleaf/devctl.h new file +40
-0@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_DEVCTL_H_ +#define _XRT_DEVCTL_H_ + +#include "xleaf.h" + +/* +* DEVCTL driver leaf calls. +*/ +enum xrt_devctl_leaf_cmd { +XRT_DEVCTL_READ =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +}; + +enum xrt_devctl_id { +XRT_DEVCTL_ROM_UUID =0 , +XRT_DEVCTL_DDR_CALIB , +XRT_DEVCTL_GOLDEN_VER , +XRT_DEVCTL_MAX +}; + +struct xrt_devctl_rw { +u32 xdr_id; +void *xdr_buf; +u32 xdr_len; +u32 xdr_offset; +}; + +struct xrt_devctl_intf_uuid { +u32 uuid_num; +uuid_t *uuids; +}; + +#endif /* _XRT_DEVCTL_H_ */ drivers/fpga/xrt/lib/lib-drv.c new file +8
-0@@ -0,0 +1,322 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +* Lizhi Hou <[email protected]> +*/
drivers/fpga/xrt/lib/lib-drv.c line 18
Quoted 10 lines
drivers/fpga/xrt/lib/lib-drv.c new file +10
-0+ +#include <linux/module.h> +#include <linux/vmalloc.h> +#include <linux/slab.h> +#include "xleaf.h" +#include "xroot.h" +#include "lib-drv.h" + +#define XRT_IPLIB_MODULE_NAME "xrt-lib" +#define XRT_IPLIB_MODULE_VERSION "4.0.0"
No driver versions ...
Quoted 11 lines
drivers/fpga/xrt/lib/lib-drv.c new file +11
-0+#define XRT_DRVNAME (drv) ((drv)->driver.name) + +#define XRT_SUBDEV_ID_SHIFT 16 +#define XRT_SUBDEV_ID_MASK ((1 << XRT_SUBDEV_ID_SHIFT) - 1) + +struct xrt_find_drv_data { +enum xrt_subdev_id id; +struct xrt_driver *xdrv; +}; + +struct class *xrt_class;
drivers/fpga/xrt/lib/lib-drv.c line 39
Quoted 10 lines
drivers/fpga/xrt/lib/lib-drv.c new file +10
-0+static DEFINE_IDA (xrt_device_ida); + +static inline u32 xrt_instance_to_id (enum xrt_subdev_id id,u32 instance) +{ +return (id <<XRT_SUBDEV_ID_SHIFT ) | instance; +} + +static inline u32 xrt_id_to_instance (u32 id) +{ +return (id &XRT_SUBDEV_ID_MASK );
Don't need braces?
Quoted 73 lines
drivers/fpga/xrt/lib/lib-drv.c new file +73
-0+} + +static int xrt_bus_match (struct device *dev,struct device_driver *drv) +{ +struct xrt_device *xdev =to_xrt_dev (dev); +struct xrt_driver *xdrv =to_xrt_drv (drv); + +if (xdev->subdev_id == xdrv->subdev_id) +return 1 ; + +return 0 ; +} + +static int xrt_bus_probe (struct device *dev) +{ +struct xrt_driver *xdrv =to_xrt_drv (dev->driver); +struct xrt_device *xdev =to_xrt_dev (dev); + +return xdrv->probe (xdev); +} + +static int xrt_bus_remove (struct device *dev) +{ +struct xrt_driver *xdrv =to_xrt_drv (dev->driver); +struct xrt_device *xdev =to_xrt_dev (dev); + +if (xdrv->remove) + xdrv->remove (xdev); + +return 0 ; +} + +struct bus_type xrt_bus_type = { + .name ="xrt" , + .match = xrt_bus_match, + .probe = xrt_bus_probe, + .remove = xrt_bus_remove, +}; + +int xrt_register_driver (struct xrt_driver *drv) +{ +const char *drvname =XRT_DRVNAME (drv); +int rc =0 ; + +/* Initialize dev_t for char dev node. */ +if (drv->file_ops.xsf_ops.open) { + rc =alloc_chrdev_region (&drv->file_ops.xsf_dev_t,0 , +XRT_MAX_DEVICE_NODES , drvname); +if (rc) { +pr_err ("failed to alloc dev minor for %s: %d\n" , drvname, rc); +return rc; + } + }else { + drv->file_ops.xsf_dev_t = (dev_t)-1 ; + } + + drv->driver.owner =THIS_MODULE ; + drv->driver.bus = &xrt_bus_type; + + rc =driver_register (&drv->driver); +if (rc) { +pr_err ("register %s xrt driver failed\n" , drvname); +if (drv->file_ops.xsf_dev_t != (dev_t)-1 ) { +unregister_chrdev_region (drv->file_ops.xsf_dev_t, +XRT_MAX_DEVICE_NODES ); + } +return rc; + } + +pr_info ("%s registered successfully\n" , drvname); + +return 0 ; +}
drivers/fpga/xrt/lib/lib-drv.c line 122
Quoted 10 lines
drivers/fpga/xrt/lib/lib-drv.c new file +10
-0+EXPORT_SYMBOL_GPL (xrt_register_driver); + +void xrt_unregister_driver (struct xrt_driver *drv) +{ +driver_unregister (&drv->driver); + +if (drv->file_ops.xsf_dev_t != (dev_t)-1 ) +unregister_chrdev_region (drv->file_ops.xsf_dev_t,XRT_MAX_DEVICE_NODES ); + +pr_info ("%s unregistered successfully\n" ,XRT_DRVNAME (drv));
Drop this.
Quoted 83 lines
drivers/fpga/xrt/lib/lib-drv.c new file +83
-0+} +EXPORT_SYMBOL_GPL (xrt_unregister_driver); + +static int __find_driver (struct device_driver *drv,void *_data) +{ +struct xrt_driver *xdrv =to_xrt_drv (drv); +struct xrt_find_drv_data *data = _data; + +if (xdrv->subdev_id == data->id) { + data->xdrv = xdrv; +return 1 ; + } + +return 0 ; +} + +const char *xrt_drv_name (enum xrt_subdev_id id) +{ +struct xrt_find_drv_data data = {0 }; + + data.id = id; +bus_for_each_drv (&xrt_bus_type,NULL , &data, __find_driver); + +if (data.xdrv) +return XRT_DRVNAME (data.xdrv); + +return NULL ; +} + +static int xrt_drv_get_instance (enum xrt_subdev_id id) +{ +int ret; + + ret =ida_alloc_range (&xrt_device_ida,xrt_instance_to_id (id,0 ), +xrt_instance_to_id (id,XRT_MAX_DEVICE_NODES ), +GFP_KERNEL ); +if (ret <0 ) +return ret; + +return xrt_id_to_instance ((u32 )ret); +} + +static void xrt_drv_put_instance (enum xrt_subdev_id id,int instance) +{ +ida_free (&xrt_device_ida,xrt_instance_to_id (id, instance)); +} + +struct xrt_dev_endpoints *xrt_drv_get_endpoints (enum xrt_subdev_id id) +{ +struct xrt_find_drv_data data = {0 }; + + data.id = id; +bus_for_each_drv (&xrt_bus_type,NULL , &data, __find_driver); + +if (data.xdrv) +return data.xdrv->endpoints; + +return NULL ; +} + +static void xrt_device_release (struct device *dev) +{ + struct xrt_device *xdev = container_of(dev, struct xrt_device, dev); + +kfree (xdev); +} + +void xrt_device_unregister (struct xrt_device *xdev) +{ +if (xdev->state ==XRT_DEVICE_STATE_ADDED ) +device_del (&xdev->dev); + +vfree (xdev->sdev_data); +kfree (xdev->resource); + +if (xdev->instance !=XRT_INVALID_DEVICE_INST ) +xrt_drv_put_instance (xdev->subdev_id, xdev->instance); + +if (xdev->dev.release == xrt_device_release) +put_device (&xdev->dev); +} + +struct xrt_device *
drivers/fpga/xrt/lib/lib-drv.c line 215
Quoted 10 lines
drivers/fpga/xrt/lib/lib-drv.c new file +10
-0+xrt_device_register(struct device *parent, u32 id, + struct resource *res, u32 res_num, + void *pdata, size_t data_sz) +{ + struct xrt_device *xdev = NULL; +int ret; + + xdev =kzalloc (sizeof (*xdev),GFP_KERNEL ); +if (!xdev) +return NULL ;
Nit: return xdev
Quoted 17 lines
drivers/fpga/xrt/lib/lib-drv.c new file +17
-0+ xdev->instance =XRT_INVALID_DEVICE_INST ; + +/* Obtain dev instance number. */ + ret =xrt_drv_get_instance (id); +if (ret <0 ) { +dev_err (parent,"failed get instance, ret %d" , ret); + goto fail; + } + + xdev->instance = ret; + xdev->name =xrt_drv_name (id); + xdev->subdev_id = id; +device_initialize (&xdev->dev); + xdev->dev.release = xrt_device_release; + xdev->dev.parent = parent; + + xdev->dev.bus = &xrt_bus_type;
drivers/fpga/xrt/lib/lib-drv.c line 242
Quoted 10 lines
drivers/fpga/xrt/lib/lib-drv.c new file +10
-0+dev_set_name (&xdev->dev,"%s.%d" , xdev->name, xdev->instance); + + xdev->num_resources = res_num; + xdev->resource =kmemdup (res,sizeof (*res) * res_num,GFP_KERNEL ); +if (!xdev->resource) + goto fail; + + xdev->sdev_data =vzalloc (data_sz); +if (!xdev->sdev_data) + goto fail;
Do you need another label here? I don't see this getting cleaned up if device_add() fails below ...?
Quoted 110 lines
drivers/fpga/xrt/lib/lib-drv.c new file +80
-0+ +memcpy (xdev->sdev_data, pdata, data_sz); + + ret =device_add (&xdev->dev); +if (ret) { +dev_err (parent,"failed add device, ret %d" , ret); + goto fail; + } + xdev->state =XRT_DEVICE_STATE_ADDED ; + +return xdev; + +fail: +xrt_device_unregister (xdev); +kfree (xdev); + +return NULL ; +} + +struct resource *xrt_get_resource (struct xrt_device *xdev,u32 type,u32 num) +{ +u32 i; + +for (i =0 ; i < xdev->num_resources; i++) { +struct resource *r = &xdev->resource[i]; + +if (type ==resource_type (r) && num-- ==0 ) +return r; + } +return NULL ; +} + +/* +* Leaf driver's module init/fini callbacks. This is not a open infrastructure for dynamic +* plugging in drivers. All drivers should be statically added. +*/ +static void (*leaf_init_fini_cbs[])(bool ) = { + group_leaf_init_fini, + axigate_leaf_init_fini, + icap_leaf_init_fini, +}; + +static __init int xrt_lib_init (void ) +{ +int ret; +int i; + + ret =bus_register (&xrt_bus_type); +if (ret) +return ret; + + xrt_class =class_create (THIS_MODULE ,XRT_IPLIB_MODULE_NAME ); +if (IS_ERR (xrt_class)) { +bus_unregister (&xrt_bus_type); +return PTR_ERR (xrt_class); + } + +for (i =0 ; i <ARRAY_SIZE (leaf_init_fini_cbs); i++) + leaf_init_fini_cbs[i](true); +return 0 ; +} + +static __exit void xrt_lib_fini(void) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (leaf_init_fini_cbs); i++) + leaf_init_fini_cbs[i](false); + +class_destroy (xrt_class); +bus_unregister (&xrt_bus_type); +} + +module_init (xrt_lib_init); +module_exit (xrt_lib_fini); + +MODULE_VERSION (XRT_IPLIB_MODULE_VERSION ); +MODULE_AUTHOR ("XRT Team <[email protected]>" ); +MODULE_DESCRIPTION ("Xilinx Alveo IP Lib driver" ); +MODULE_LICENSE ("GPL v2" );drivers/fpga/xrt/lib/lib-drv.h new file +21
-0@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _LIB_DRV_H_ +#define _LIB_DRV_H_ + +#include <linux/device/class.h> +#include <linux/device/bus.h> + +extern struct class *xrt_class; +extern struct bus_type xrt_bus_type; + +const char *xrt_drv_name (enum xrt_subdev_id id); +struct xrt_dev_endpoints *xrt_drv_get_endpoints (enum xrt_subdev_id id); + +#endif /* _LIB_DRV_H_ */ Signature · 1 line
2.27.0
Re: [PATCH V8 XRT Alveo 04/14] fpga: xrt: xrt-lib driver manager
Hi Moritz,
Quoted 100 lines · Moritz Fischer
Quoted 99 lines · Lizhi Hou
xrt-lib kernel module infrastructure code to register and manage all leaf driver modules.
Signed-off-by Sonal Santan <[email protected]>Signed-off-by Max Zhen <[email protected]>Signed-off-by Lizhi Hou <[email protected]>Reviewed-by Tom Rix <[email protected]>--- drivers/fpga/xrt/include/subdev_id.h | 39 +++ drivers/fpga/xrt/include/xdevice.h | 131 +++++++ drivers/fpga/xrt/include/xleaf.h | 205 +++++++++++ drivers/fpga/xrt/include/xleaf/clkfreq.h | 21 ++ drivers/fpga/xrt/include/xleaf/clock.h | 29 ++ .../fpga/xrt/include/xleaf/ddr_calibration.h | 28 ++ drivers/fpga/xrt/include/xleaf/devctl.h | 40 +++ drivers/fpga/xrt/lib/lib-drv.c | 322 ++++++++++++++++++ drivers/fpga/xrt/lib/lib-drv.h | 21 ++ 9 files changed, 836 insertions(+)create mode 100644
drivers/fpga/xrt/include/subdev_id.hcreate mode 100644drivers/fpga/xrt/include/xdevice.hcreate mode 100644drivers/fpga/xrt/include/xleaf.hcreate mode 100644drivers/fpga/xrt/include/xleaf/clkfreq.hcreate mode 100644drivers/fpga/xrt/include/xleaf/clock.hcreate mode 100644drivers/fpga/xrt/include/xleaf/ddr_calibration.hcreate mode 100644drivers/fpga/xrt/include/xleaf/devctl.hcreate mode 100644drivers/fpga/xrt/lib/lib-drv.c
create mode 100644drivers/fpga/xrt/lib/lib-drv.hdrivers/fpga/xrt/include/subdev_id.h new file +39
-0@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XRT_SUBDEV_ID_H_ +#define _XRT_SUBDEV_ID_H_ + +/* +* Every subdev driver has an ID for others to refer to it. There can be multiple number of +* instances of a subdev driver. A <subdev_id, subdev_instance> tuple is a unique identification +* of a specific instance of a subdev driver. +*/ +enum xrt_subdev_id { +XRT_SUBDEV_INVALID =0 , +XRT_SUBDEV_GRP , +XRT_SUBDEV_VSEC , +XRT_SUBDEV_VSEC_GOLDEN , +XRT_SUBDEV_DEVCTL , +XRT_SUBDEV_AXIGATE , +XRT_SUBDEV_ICAP , +XRT_SUBDEV_TEST , +XRT_SUBDEV_MGMT_MAIN , +XRT_SUBDEV_QSPI , +XRT_SUBDEV_MAILBOX , +XRT_SUBDEV_CMC , +XRT_SUBDEV_CALIB , +XRT_SUBDEV_CLKFREQ , +XRT_SUBDEV_CLOCK , +XRT_SUBDEV_SRSR , +XRT_SUBDEV_UCS , +XRT_SUBDEV_NUM ,/* Total number of subdevs. */ +XRT_ROOT =-1 ,/* Special ID for root driver. */ +}; + +#endif /* _XRT_SUBDEV_ID_H_ */ drivers/fpga/xrt/include/xdevice.h new file +20
-0@@ -0,0 +1,131 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_DEVICE_H_ +#define _XRT_DEVICE_H_ + +#include <linux/fs.h> +#include <linux/cdev.h> + +#define XRT_MAX_DEVICE_NODES 128 +#define XRT_INVALID_DEVICE_INST (XRT_MAX_DEVICE_NODES + 1) + +enum { +XRT_DEVICE_STATE_NONE =0 , + XRT_DEVICE_STATE_ADDED
Quoted 8 lines
drivers/fpga/xrt/include/xdevice.h line 27
Quoted 7 lines
drivers/fpga/xrt/include/xdevice.h new file +7
-0+}; + +/* +* struct xrt_device - represent an xrt device on xrt bus +* +* dev: generic device interface. +* id: id of the xrt device. Shouldn't this document the rest of the members?
Sure. I will add comments as below.
* dev: generic device interface.
* subdev_id: id of the xrt device. See enum xrt_subdev_id.
* name: name of the xrt device.
* instance: instance of the xrt device. The xrt device with same id
can have
* more than 1 instances.
* state: current state of the xrt device.
* num_resources: The total number of resource for the xrt device.
* resource: point to the xrt device resource array.
* sdev_data: private data pointer.
Quoted 29 lines
Quoted 29 lines
drivers/fpga/xrt/include/xdevice.h new file +28
-0+*/ +struct xrt_device { +struct device dev; +u32 subdev_id; +const char *name; +u32 instance; +u32 state; +u32 num_resources; +struct resource *resource; +void *sdev_data; +}; + +/* +* If populated by xrt device driver, infra will handle the mechanics of +* char device (un)registration. +*/ +enum xrt_dev_file_mode { +/* Infra create cdev, default file name */ +XRT_DEV_FILE_DEFAULT =0 , +/* Infra create cdev, need to encode inst num in file name */ +XRT_DEV_FILE_MULTI_INST , +/* No auto creation of cdev by infra, leaf handles it by itself */ +XRT_DEV_FILE_NO_AUTO , +}; + +struct xrt_dev_file_ops { +const struct file_operations xsf_ops; +dev_t xsf_dev_t;
Quoted 8 lines
drivers/fpga/xrt/include/xdevice.h line 62
Quoted 7 lines
drivers/fpga/xrt/include/xdevice.h new file +7
-0+const char *xsf_dev_name; +enum xrt_dev_file_mode xsf_mode; +}; + +/* +* this struct define the endpoints belong to the same xrt device +*/
ep_name: ... etc?
I will add comment as below.
* ep_name: endpoint name
* compat: compatible string
Quoted 8 lines
Quoted 8 lines
drivers/fpga/xrt/include/xdevice.h new file +8
-0+struct xrt_dev_ep_names { +const char *ep_name; +const char *compat; +}; + +struct xrt_dev_endpoints { +struct xrt_dev_ep_names *xse_names; +/* minimum number of endpoints to support the subdevice */
Quoted 8 lines
drivers/fpga/xrt/include/xdevice.h line 77
Quoted 7 lines
drivers/fpga/xrt/include/xdevice.h new file +7
-0+u32 xse_min_ep; +}; + +/* +* struct xrt_driver - represent a xrt device driver +* +* drv: driver model structure. drv vs driver?
will fix this.
Quoted 4 lines
drivers/fpga/xrt/include/xdevice.h line 79
Quoted 2 lines
drivers/fpga/xrt/include/xdevice.h new file +2
-0+* id_table: pointer to table of device IDs the driver is interested in. +* { } member terminated. ?
file_ops?
I will remove 'id_table' and add comment as below:
* driver: driver model structure.
* subdev_id: id of the xrt device. See enum xrt_subdev_id.
* file_ops: character device name and callbacks.
* probe: mandatory callback for device binding.
* remove: callback for device unbinding.
* leaf_call: callback for servicing other leaf drivers.
Quoted 422 lines
Quoted 422 lines
drivers/fpga/xrt/include/xdevice.h new file +52
-0+* probe: mandatory callback for device binding. +* remove: callback for device unbinding. +*/ +struct xrt_driver { +struct device_driver driver; +u32 subdev_id; +struct xrt_dev_file_ops file_ops; +struct xrt_dev_endpoints *endpoints; + +/* +* Subdev driver callbacks populated by subdev driver. +*/ +int (*probe)(struct xrt_device *xrt_dev); +void (*remove)(struct xrt_device *xrt_dev); +/* +* If leaf_call is defined, these are called by other leaf drivers. +* Note that root driver may call into leaf_call of a group driver. +*/ +int (*leaf_call)(struct xrt_device *xrt_dev,u32 cmd,void *arg); +}; + +#define to_xrt_dev (d) container_of(d, struct xrt_device, dev) +#define to_xrt_drv (d) container_of(d, struct xrt_driver, driver) + +static inline void *xrt_get_drvdata (const struct xrt_device *xdev) +{ +return dev_get_drvdata (&xdev->dev); +} + +static inline void xrt_set_drvdata (struct xrt_device *xdev,void *data) +{ +dev_set_drvdata (&xdev->dev, data); +} + +static inline void *xrt_get_xdev_data (struct device *dev) +{ +struct xrt_device *xdev =to_xrt_dev (dev); + +return xdev->sdev_data; +} + +struct xrt_device * +xrt_device_register (struct device *parent,u32 id, +struct resource *res,u32 res_num, +void *pdata,size_t data_sz); +void xrt_device_unregister (struct xrt_device *xdev); +int xrt_register_driver (struct xrt_driver *drv); +void xrt_unregister_driver (struct xrt_driver *drv); +void *xrt_get_xdev_data (struct device *dev); +struct resource *xrt_get_resource (struct xrt_device *xdev,u32 type,u32 num); + +#endif /* _XRT_DEVICE_H_ */ drivers/fpga/xrt/include/xleaf.h new file +205
-0@@ -0,0 +1,205 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +* Sonal Santan <[email protected]> +*/ + +#ifndef _XRT_XLEAF_H_ +#define _XRT_XLEAF_H_ + +#include <linux/mod_devicetable.h> +#include "xdevice.h" +#include "subdev_id.h" +#include "xroot.h" +#include "events.h" + +/* All subdev drivers should use below common routines to print out msg. */ +#define DEV (xdev) (&(xdev)->dev) +#define DEV_PDATA (xdev) \ + ((struct xrt_subdev_platdata *)xrt_get_xdev_data(DEV(xdev))) +#define DEV_FILE_OPS (xdev) \ + (&(to_xrt_drv((xdev)->dev.driver))->file_ops) +#define FMT_PRT(prt_fn, xdev, fmt, args...) \ + ({typeof(xdev) (_xdev) = (xdev); \ + prt_fn(DEV(_xdev), "%s %s: " fmt, \ + DEV_PDATA(_xdev)->xsp_root_name, __func__, ##args); }) +#define xrt_err(xdev, fmt, args...) FMT_PRT(dev_err, xdev, fmt, ##args) +#define xrt_warn(xdev, fmt, args...) FMT_PRT(dev_warn, xdev, fmt, ##args) +#define xrt_info(xdev, fmt, args...) FMT_PRT(dev_info, xdev, fmt, ##args) +#define xrt_dbg(xdev, fmt, args...) FMT_PRT(dev_dbg, xdev, fmt, ##args) + +#define XRT_DEFINE_REGMAP_CONFIG (config_name) \ + static const struct regmap_config config_name = { \ + .reg_bits = 32, \ + .val_bits = 32, \ + .reg_stride = 4, \ + .max_register = 0x1000, \ + } + +enum { +/* Starting cmd for common leaf cmd implemented by all leaves. */ +XRT_XLEAF_COMMON_BASE =0 , +/* Starting cmd for leaves' specific leaf cmds. */ +XRT_XLEAF_CUSTOM_BASE =64 , +}; + +enum xrt_xleaf_common_leaf_cmd { +XRT_XLEAF_EVENT =XRT_XLEAF_COMMON_BASE , +}; + +/* +* Partially initialized by the parent driver, then, passed in as subdev driver's +* platform data when creating subdev driver instance by calling platform +* device register API (xrt_device_register_data() or the likes). +* +* Once device register API returns, platform driver framework makes a copy of +* this buffer and maintains its life cycle. The content of the buffer is +* completely owned by subdev driver. +* +* Thus, parent driver should be very careful when it touches this buffer +* again once it's handed over to subdev driver. And the data structure +* should not contain pointers pointing to buffers that is managed by +* other or parent drivers since it could have been freed before platform +* data buffer is freed by platform driver framework. +*/ +struct xrt_subdev_platdata { +/* +* Per driver instance callback. The xdev points to the instance. +* Should always be defined for subdev driver to get service from root. +*/ +xrt_subdev_root_cb_t xsp_root_cb; +void *xsp_root_cb_arg; + +/* Something to associate w/ root for msg printing. */ +const char *xsp_root_name; + +/* +* Char dev support for this subdev instance. +* Initialized by subdev driver. +*/ +struct cdev xsp_cdev; +struct device *xsp_sysdev; +struct mutex xsp_devnode_lock;/* devnode lock */ +struct completion xsp_devnode_comp; +int xsp_devnode_ref; +bool xsp_devnode_online; +bool xsp_devnode_excl; + +/* +* Subdev driver specific init data. The buffer should be embedded +* in this data structure buffer after dtb, so that it can be freed +* together with platform data. +*/ +loff_t xsp_priv_off;/* Offset into this platform data buffer. */ +size_t xsp_priv_len; + +/* +* Populated by parent driver to describe the device tree for +* the subdev driver to handle. Should always be last one since it's +* of variable length. +*/ +bool xsp_dtb_valid; +char xsp_dtb[0 ]; +}; + +struct subdev_match_arg { +enum xrt_subdev_id id; +int instance; +}; + +bool xleaf_has_endpoint (struct xrt_device *xdev,const char *endpoint_name); +struct xrt_device *xleaf_get_leaf (struct xrt_device *xdev, +xrt_subdev_match_t cb,void *arg); + +static inline bool subdev_match (enum xrt_subdev_id id,struct xrt_device *xdev,void *arg) +{ +const struct subdev_match_arg *a = (struct subdev_match_arg *)arg; +int instance = a->instance; + +if (id != a->id) +return false; +if (instance != xdev->instance && instance !=XRT_INVALID_DEVICE_INST ) +return false; +return true; +} + +static inline bool xrt_subdev_match_epname (enum xrt_subdev_id id, +struct xrt_device *xdev,void *arg) +{ +return xleaf_has_endpoint (xdev, arg); +} + +static inline struct xrt_device * +xleaf_get_leaf_by_id (struct xrt_device *xdev, +enum xrt_subdev_id id,int instance) +{ +struct subdev_match_arg arg = { id, instance }; + +return xleaf_get_leaf (xdev, subdev_match, &arg); +} + +static inline struct xrt_device * +xleaf_get_leaf_by_epname (struct xrt_device *xdev,const char *name) +{ +return xleaf_get_leaf (xdev, xrt_subdev_match_epname, (void *)name); +} + +static inline int xleaf_call (struct xrt_device *tgt,u32 cmd,void *arg) +{ +return (to_xrt_drv (tgt->dev.driver)->leaf_call)(tgt, cmd, arg); +} + +int xleaf_broadcast_event (struct xrt_device *xdev,enum xrt_events evt,bool async); +int xleaf_create_group (struct xrt_device *xdev,char *dtb); +int xleaf_destroy_group (struct xrt_device *xdev,int instance); +void xleaf_get_root_res (struct xrt_device *xdev,u32 region_id,struct resource **res); +void xleaf_get_root_id (struct xrt_device *xdev,unsigned short *vendor,unsigned short *device, +unsigned short *subvendor,unsigned short *subdevice); +void xleaf_hot_reset (struct xrt_device *xdev); +int xleaf_put_leaf (struct xrt_device *xdev,struct xrt_device *leaf); +struct device *xleaf_register_hwmon (struct xrt_device *xdev,const char *name,void *drvdata, +const struct attribute_group **grps); +void xleaf_unregister_hwmon (struct xrt_device *xdev,struct device *hwmon); +int xleaf_wait_for_group_bringup (struct xrt_device *xdev); + +/* +* Character device helper APIs for use by leaf drivers +*/ +static inline bool xleaf_devnode_enabled (struct xrt_device *xdev) +{ +return DEV_FILE_OPS (xdev)->xsf_ops.open; +} + +int xleaf_devnode_create (struct xrt_device *xdev, +const char *file_name,const char *inst_name); +void xleaf_devnode_destroy (struct xrt_device *xdev); + +struct xrt_device *xleaf_devnode_open_excl (struct inode *inode); +struct xrt_device *xleaf_devnode_open (struct inode *inode); +void xleaf_devnode_close (struct inode *inode); + +/* Module's init/fini routines for leaf driver in xrt-lib module */ +#define XRT_LEAF_INIT_FINI_FUNC (name) \ +void name##_leaf_init_fini(bool init) \ +{ \ + if (init) \ + xrt_register_driver(&xrt_##name##_driver); \ + else \ + xrt_unregister_driver(&xrt_##name##_driver); \ +} + +/* Module's init/fini routines for leaf driver in xrt-lib module */ +void group_leaf_init_fini (bool init); +void vsec_leaf_init_fini (bool init); +void devctl_leaf_init_fini (bool init); +void axigate_leaf_init_fini (bool init); +void icap_leaf_init_fini (bool init); +void calib_leaf_init_fini (bool init); +void clkfreq_leaf_init_fini (bool init); +void clock_leaf_init_fini (bool init); +void ucs_leaf_init_fini (bool init); + +#endif /* _XRT_LEAF_H_ */ drivers/fpga/xrt/include/xleaf/clkfreq.h new file +21
-0@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_CLKFREQ_H_ +#define _XRT_CLKFREQ_H_ + +#include "xleaf.h" + +/* +* CLKFREQ driver leaf calls. +*/ +enum xrt_clkfreq_leaf_cmd { +XRT_CLKFREQ_READ =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +}; + +#endif /* _XRT_CLKFREQ_H_ */ drivers/fpga/xrt/include/xleaf/clock.h new file +29
-0@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_CLOCK_H_ +#define _XRT_CLOCK_H_ + +#include "xleaf.h" +#include <linux/fpga_xrt.h> + +/* +* CLOCK driver leaf calls. +*/ +enum xrt_clock_leaf_cmd { +XRT_CLOCK_SET =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +XRT_CLOCK_GET , +XRT_CLOCK_VERIFY , +}; + +struct xrt_clock_get { +u16 freq; +u32 freq_cnter; +}; + +#endif /* _XRT_CLOCK_H_ */ drivers/fpga/xrt/include/xleaf/ddr_calibration.h new file +28
-0@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XRT_DDR_CALIBRATION_H_ +#define _XRT_DDR_CALIBRATION_H_ + +#include "xleaf.h" +#include <linux/fpga-xrt.h> + +/* +* Memory calibration driver leaf calls. +*/ +enum xrt_calib_results { +XRT_CALIB_UNKNOWN =0 , +XRT_CALIB_SUCCEEDED , +XRT_CALIB_FAILED , +}; + +enum xrt_calib_leaf_cmd { +XRT_CALIB_RESULT =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +}; + +#endif /* _XRT_DDR_CALIBRATION_H_ */ drivers/fpga/xrt/include/xleaf/devctl.h new file +40
-0@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_DEVCTL_H_ +#define _XRT_DEVCTL_H_ + +#include "xleaf.h" + +/* +* DEVCTL driver leaf calls. +*/ +enum xrt_devctl_leaf_cmd { +XRT_DEVCTL_READ =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +}; + +enum xrt_devctl_id { +XRT_DEVCTL_ROM_UUID =0 , +XRT_DEVCTL_DDR_CALIB , +XRT_DEVCTL_GOLDEN_VER , +XRT_DEVCTL_MAX +}; + +struct xrt_devctl_rw { +u32 xdr_id; +void *xdr_buf; +u32 xdr_len; +u32 xdr_offset; +}; + +struct xrt_devctl_intf_uuid { +u32 uuid_num; +uuid_t *uuids; +}; + +#endif /* _XRT_DEVCTL_H_ */ drivers/fpga/xrt/lib/lib-drv.c new file +11
-0@@ -0,0 +1,322 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +* Lizhi Hou <[email protected]> +*/ + +#include <linux/module.h> +#include <linux/vmalloc.h>
Quoted 8 lines
drivers/fpga/xrt/lib/lib-drv.c line 18
Quoted 7 lines
drivers/fpga/xrt/lib/lib-drv.c new file +7
-0+#include <linux/slab.h> +#include "xleaf.h" +#include "xroot.h" +#include "lib-drv.h" + +#define XRT_IPLIB_MODULE_NAME "xrt-lib" +#define XRT_IPLIB_MODULE_VERSION "4.0.0"No driver versions ...
I will remove the version.
Quoted 14 lines
Quoted 14 lines
drivers/fpga/xrt/lib/lib-drv.c new file +14
-0+#define XRT_DRVNAME (drv) ((drv)->driver.name) + +#define XRT_SUBDEV_ID_SHIFT 16 +#define XRT_SUBDEV_ID_MASK ((1 << XRT_SUBDEV_ID_SHIFT) - 1) + +struct xrt_find_drv_data { +enum xrt_subdev_id id; +struct xrt_driver *xdrv; +}; + +struct class *xrt_class; +static DEFINE_IDA (xrt_device_ida); + +static inline u32 xrt_instance_to_id (enum xrt_subdev_id id,u32 instance)
Quoted 8 lines
drivers/fpga/xrt/lib/lib-drv.c line 39
Quoted 7 lines
drivers/fpga/xrt/lib/lib-drv.c new file +7
-0+{ +return (id <<XRT_SUBDEV_ID_SHIFT ) | instance; +} + +static inline u32 xrt_id_to_instance (u32 id) +{ +return (id &XRT_SUBDEV_ID_MASK );Don't need braces?
I will remove the braces.
Quoted 76 lines
Quoted 76 lines
drivers/fpga/xrt/lib/lib-drv.c new file +76
-0+} + +static int xrt_bus_match (struct device *dev,struct device_driver *drv) +{ +struct xrt_device *xdev =to_xrt_dev (dev); +struct xrt_driver *xdrv =to_xrt_drv (drv); + +if (xdev->subdev_id == xdrv->subdev_id) +return 1 ; + +return 0 ; +} + +static int xrt_bus_probe (struct device *dev) +{ +struct xrt_driver *xdrv =to_xrt_drv (dev->driver); +struct xrt_device *xdev =to_xrt_dev (dev); + +return xdrv->probe (xdev); +} + +static int xrt_bus_remove (struct device *dev) +{ +struct xrt_driver *xdrv =to_xrt_drv (dev->driver); +struct xrt_device *xdev =to_xrt_dev (dev); + +if (xdrv->remove) + xdrv->remove (xdev); + +return 0 ; +} + +struct bus_type xrt_bus_type = { + .name ="xrt" , + .match = xrt_bus_match, + .probe = xrt_bus_probe, + .remove = xrt_bus_remove, +}; + +int xrt_register_driver (struct xrt_driver *drv) +{ +const char *drvname =XRT_DRVNAME (drv); +int rc =0 ; + +/* Initialize dev_t for char dev node. */ +if (drv->file_ops.xsf_ops.open) { + rc =alloc_chrdev_region (&drv->file_ops.xsf_dev_t,0 , +XRT_MAX_DEVICE_NODES , drvname); +if (rc) { +pr_err ("failed to alloc dev minor for %s: %d\n" , drvname, rc); +return rc; + } + }else { + drv->file_ops.xsf_dev_t = (dev_t)-1 ; + } + + drv->driver.owner =THIS_MODULE ; + drv->driver.bus = &xrt_bus_type; + + rc =driver_register (&drv->driver); +if (rc) { +pr_err ("register %s xrt driver failed\n" , drvname); +if (drv->file_ops.xsf_dev_t != (dev_t)-1 ) { +unregister_chrdev_region (drv->file_ops.xsf_dev_t, +XRT_MAX_DEVICE_NODES ); + } +return rc; + } + +pr_info ("%s registered successfully\n" , drvname); + +return 0 ; +} +EXPORT_SYMBOL_GPL (xrt_register_driver); + +void xrt_unregister_driver (struct xrt_driver *drv)
Quoted 8 lines
drivers/fpga/xrt/lib/lib-drv.c line 122
Quoted 7 lines
drivers/fpga/xrt/lib/lib-drv.c new file +7
-0+{ +driver_unregister (&drv->driver); + +if (drv->file_ops.xsf_dev_t != (dev_t)-1 ) +unregister_chrdev_region (drv->file_ops.xsf_dev_t,XRT_MAX_DEVICE_NODES ); + +pr_info ("%s unregistered successfully\n" ,XRT_DRVNAME (drv));Drop this.
Sure.
Quoted 86 lines
Quoted 86 lines
drivers/fpga/xrt/lib/lib-drv.c new file +86
-0+} +EXPORT_SYMBOL_GPL (xrt_unregister_driver); + +static int __find_driver (struct device_driver *drv,void *_data) +{ +struct xrt_driver *xdrv =to_xrt_drv (drv); +struct xrt_find_drv_data *data = _data; + +if (xdrv->subdev_id == data->id) { + data->xdrv = xdrv; +return 1 ; + } + +return 0 ; +} + +const char *xrt_drv_name (enum xrt_subdev_id id) +{ +struct xrt_find_drv_data data = {0 }; + + data.id = id; +bus_for_each_drv (&xrt_bus_type,NULL , &data, __find_driver); + +if (data.xdrv) +return XRT_DRVNAME (data.xdrv); + +return NULL ; +} + +static int xrt_drv_get_instance (enum xrt_subdev_id id) +{ +int ret; + + ret =ida_alloc_range (&xrt_device_ida,xrt_instance_to_id (id,0 ), +xrt_instance_to_id (id,XRT_MAX_DEVICE_NODES ), +GFP_KERNEL ); +if (ret <0 ) +return ret; + +return xrt_id_to_instance ((u32 )ret); +} + +static void xrt_drv_put_instance (enum xrt_subdev_id id,int instance) +{ +ida_free (&xrt_device_ida,xrt_instance_to_id (id, instance)); +} + +struct xrt_dev_endpoints *xrt_drv_get_endpoints (enum xrt_subdev_id id) +{ +struct xrt_find_drv_data data = {0 }; + + data.id = id; +bus_for_each_drv (&xrt_bus_type,NULL , &data, __find_driver); + +if (data.xdrv) +return data.xdrv->endpoints; + +return NULL ; +} + +static void xrt_device_release (struct device *dev) +{ + struct xrt_device *xdev = container_of(dev, struct xrt_device, dev); + +kfree (xdev); +} + +void xrt_device_unregister (struct xrt_device *xdev) +{ +if (xdev->state ==XRT_DEVICE_STATE_ADDED ) +device_del (&xdev->dev); + +vfree (xdev->sdev_data); +kfree (xdev->resource); + +if (xdev->instance !=XRT_INVALID_DEVICE_INST ) +xrt_drv_put_instance (xdev->subdev_id, xdev->instance); + +if (xdev->dev.release == xrt_device_release) +put_device (&xdev->dev); +} + +struct xrt_device * +xrt_device_register (struct device *parent,u32 id, +struct resource *res,u32 res_num, +void *pdata,size_t data_sz)
Quoted 8 lines
drivers/fpga/xrt/lib/lib-drv.c line 215
Quoted 7 lines
drivers/fpga/xrt/lib/lib-drv.c new file +7
-0+{ +struct xrt_device *xdev =NULL ; +int ret; + + xdev =kzalloc (sizeof (*xdev),GFP_KERNEL ); +if (!xdev) +return NULL ;Nit: return xdev
Sure.
Quoted 21 lines
Quoted 21 lines
drivers/fpga/xrt/lib/lib-drv.c new file +21
-0+ xdev->instance =XRT_INVALID_DEVICE_INST ; + +/* Obtain dev instance number. */ + ret =xrt_drv_get_instance (id); +if (ret <0 ) { +dev_err (parent,"failed get instance, ret %d" , ret); + goto fail; + } + + xdev->instance = ret; + xdev->name =xrt_drv_name (id); + xdev->subdev_id = id; +device_initialize (&xdev->dev); + xdev->dev.release = xrt_device_release; + xdev->dev.parent = parent; + + xdev->dev.bus = &xrt_bus_type; +dev_set_name (&xdev->dev,"%s.%d" , xdev->name, xdev->instance); + + xdev->num_resources = res_num; + xdev->resource =kmemdup (res,sizeof (*res) * res_num,GFP_KERNEL );
Quoted 8 lines
drivers/fpga/xrt/lib/lib-drv.c line 242
Quoted 6 lines
drivers/fpga/xrt/lib/lib-drv.c new file +6
-0+if (!xdev->resource) + goto fail; + + xdev->sdev_data =vzalloc (data_sz); +if (!xdev->sdev_data) + goto fail;Do you need another label here? I don't see this getting cleaned up if
device_add()fails below ...?
sdev_data will be cleaned up in xrt_device_unregister().
Thanks,
Lizhi
Quoted 110 lines
Quoted 110 lines
drivers/fpga/xrt/lib/lib-drv.c new file +80
-0+ +memcpy (xdev->sdev_data, pdata, data_sz); + + ret =device_add (&xdev->dev); +if (ret) { +dev_err (parent,"failed add device, ret %d" , ret); + goto fail; + } + xdev->state =XRT_DEVICE_STATE_ADDED ; + +return xdev; + +fail: +xrt_device_unregister (xdev); +kfree (xdev); + +return NULL ; +} + +struct resource *xrt_get_resource (struct xrt_device *xdev,u32 type,u32 num) +{ +u32 i; + +for (i =0 ; i < xdev->num_resources; i++) { +struct resource *r = &xdev->resource[i]; + +if (type ==resource_type (r) && num-- ==0 ) +return r; + } +return NULL ; +} + +/* +* Leaf driver's module init/fini callbacks. This is not a open infrastructure for dynamic +* plugging in drivers. All drivers should be statically added. +*/ +static void (*leaf_init_fini_cbs[])(bool ) = { + group_leaf_init_fini, + axigate_leaf_init_fini, + icap_leaf_init_fini, +}; + +static __init int xrt_lib_init (void ) +{ +int ret; +int i; + + ret =bus_register (&xrt_bus_type); +if (ret) +return ret; + + xrt_class =class_create (THIS_MODULE ,XRT_IPLIB_MODULE_NAME ); +if (IS_ERR (xrt_class)) { +bus_unregister (&xrt_bus_type); +return PTR_ERR (xrt_class); + } + +for (i =0 ; i <ARRAY_SIZE (leaf_init_fini_cbs); i++) + leaf_init_fini_cbs[i](true); +return 0 ; +} + +static __exit void xrt_lib_fini(void) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (leaf_init_fini_cbs); i++) + leaf_init_fini_cbs[i](false); + +class_destroy (xrt_class); +bus_unregister (&xrt_bus_type); +} + +module_init (xrt_lib_init); +module_exit (xrt_lib_fini); + +MODULE_VERSION (XRT_IPLIB_MODULE_VERSION ); +MODULE_AUTHOR ("XRT Team <[email protected]>" ); +MODULE_DESCRIPTION ("Xilinx Alveo IP Lib driver" ); +MODULE_LICENSE ("GPL v2" );drivers/fpga/xrt/lib/lib-drv.h new file +21
-1@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _LIB_DRV_H_ +#define _LIB_DRV_H_ + +#include <linux/device/class.h> +#include <linux/device/bus.h> + +extern struct class *xrt_class; +extern struct bus_type xrt_bus_type; + +const char *xrt_drv_name (enum xrt_subdev_id id); +struct xrt_dev_endpoints *xrt_drv_get_endpoints (enum xrt_subdev_id id); + +#endif /* _LIB_DRV_H_ */ --2.27.0
Re: [PATCH V8 XRT Alveo 04/14] fpga: xrt: xrt-lib driver manager
Hi Moritz,
Did you get a chance to review XRT V9 patchset which has fixes you suggested? Do you have more comments for XRT patches?
Thanks,
Lizhi
Quoted 1,028 lines · Lizhi Hou
Hi Moritz,
Quoted 103 lines · Moritz Fischer
Quoted 102 lines · Lizhi Hou
xrt-lib kernel module infrastructure code to register and manage all leaf driver modules.
Signed-off-by Sonal Santan <[email protected]>Signed-off-by Max Zhen <[email protected]>Signed-off-by Lizhi Hou <[email protected]>Reviewed-by Tom Rix <[email protected]>---
drivers/fpga/xrt/include/subdev_id.h | 39 +++ drivers/fpga/xrt/include/xdevice.h | 131 +++++++ drivers/fpga/xrt/include/xleaf.h | 205 +++++++++++ drivers/fpga/xrt/include/xleaf/clkfreq.h | 21 ++ drivers/fpga/xrt/include/xleaf/clock.h | 29 ++ .../fpga/xrt/include/xleaf/ddr_calibration.h | 28 ++ drivers/fpga/xrt/include/xleaf/devctl.h | 40 +++ drivers/fpga/xrt/lib/lib-drv.c | 322
++++++++++++++++++
drivers/fpga/xrt/lib/lib-drv.h | 21 ++9 files changed, 836 insertions(+)create mode 100644
drivers/fpga/xrt/include/subdev_id.hcreate mode 100644drivers/fpga/xrt/include/xdevice.hcreate mode 100644drivers/fpga/xrt/include/xleaf.hcreate mode 100644drivers/fpga/xrt/include/xleaf/clkfreq.hcreate mode 100644drivers/fpga/xrt/include/xleaf/clock.hcreate mode 100644drivers/fpga/xrt/include/xleaf/ddr_calibration.hcreate mode 100644drivers/fpga/xrt/include/xleaf/devctl.hcreate mode 100644drivers/fpga/xrt/lib/lib-drv.c
create mode 100644drivers/fpga/xrt/lib/lib-drv.hdrivers/fpga/xrt/include/subdev_id.h new file +13
-0@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XRT_SUBDEV_ID_H_ +#define _XRT_SUBDEV_ID_H_ + +/* +* Every subdev driver has an ID for others to refer to it. There can be multiple number of
+ * instances of a subdev driver. A <subdev_id,subdev_instance> tuple is a unique identification
+ * of a specific instance of a subdev driver.
+ */drivers/fpga/xrt/include/subdev_id.h new file +23
-0+enum xrt_subdev_id { + XRT_SUBDEV_INVALID = 0, + XRT_SUBDEV_GRP, + XRT_SUBDEV_VSEC, + XRT_SUBDEV_VSEC_GOLDEN, + XRT_SUBDEV_DEVCTL, + XRT_SUBDEV_AXIGATE, + XRT_SUBDEV_ICAP, + XRT_SUBDEV_TEST, + XRT_SUBDEV_MGMT_MAIN, + XRT_SUBDEV_QSPI, + XRT_SUBDEV_MAILBOX, + XRT_SUBDEV_CMC, + XRT_SUBDEV_CALIB, + XRT_SUBDEV_CLKFREQ, + XRT_SUBDEV_CLOCK, + XRT_SUBDEV_SRSR, + XRT_SUBDEV_UCS, + XRT_SUBDEV_NUM, /* Total number of subdevs. */ + XRT_ROOT = -1, /* Special ID for root driver. */ +}; + +#endif /* _XRT_SUBDEV_ID_H_ */drivers/fpga/xrt/include/xdevice.h new file +20
-0@@ -0,0 +1,131 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_DEVICE_H_ +#define _XRT_DEVICE_H_ + +#include <linux/fs.h> +#include <linux/cdev.h> + +#define XRT_MAX_DEVICE_NODES 128 +#define XRT_INVALID_DEVICE_INST (XRT_MAX_DEVICE_NODES + 1) + +enum { + XRT_DEVICE_STATE_NONE = 0, + XRT_DEVICE_STATE_ADDEDQuoted 8 lines
drivers/fpga/xrt/include/xdevice.h line 27
Quoted 7 lines
drivers/fpga/xrt/include/xdevice.h new file +7
-0+}; + +/* +* struct xrt_device - represent an xrt device on xrt bus +* +* dev: generic device interface. +* id: id of the xrt device. Shouldn't this document the rest of the members?
Sure. I will add comments as below.
* dev: generic device interface.
*subdev_id: id of the xrt device. See enumxrt_subdev_id.
* name: name of the xrt device.
* instance: instance of the xrt device. The xrt device with same id can have * more than 1 instances.
* state: current state of the xrt device.
*num_resources: The total number of resource for the xrt device.
* resource: point to the xrt device resource array.
*sdev_data: private data pointer.Quoted 31 lines
Quoted 31 lines
drivers/fpga/xrt/include/xdevice.h new file +14
-0+*/ +struct xrt_device { + struct device dev; + u32 subdev_id; + const char *name; + u32 instance; + u32 state; + u32 num_resources; + struct resource *resource; + void *sdev_data; +}; + +/* +* If populated by xrt device driver, infra will handle the mechanics of
+ * char device (un)registration.
+ */
+enumxrt_dev_file_mode{
+ /* Infra create cdev, default file name */
+XRT_DEV_FILE_DEFAULT= 0,
+ /* Infra create cdev, need to encode inst num in file name */ +XRT_DEV_FILE_MULTI_INST,
+ /* No auto creation of cdev by infra, leaf handles it by itself */
+XRT_DEV_FILE_NO_AUTO,drivers/fpga/xrt/include/xdevice.h new file +5
-0+}; + +struct xrt_dev_file_ops { + const struct file_operations xsf_ops; + dev_t xsf_dev_t;Quoted 8 lines
drivers/fpga/xrt/include/xdevice.h line 62
Quoted 7 lines
+ const char
*xsf_dev_name;
+ enumxrt_dev_file_modexsf_mode;drivers/fpga/xrt/include/xdevice.h new file +5
-0+}; + +/* +* this struct define the endpoints belong to the same xrt device +*/
ep_name: ... etc?I will add comment as below.
*
ep_name: endpoint name
* compat: compatible stringQuoted 8 lines
Quoted 8 lines
drivers/fpga/xrt/include/xdevice.h new file +8
-0+struct xrt_dev_ep_names { + const char *ep_name; + const char *compat; +}; + +struct xrt_dev_endpoints { + struct xrt_dev_ep_names *xse_names; + /* minimum number of endpoints to support the subdevice */Quoted 8 lines
drivers/fpga/xrt/include/xdevice.h line 77
Quoted 7 lines
+ u32
xse_min_ep;drivers/fpga/xrt/include/xdevice.h new file +6
-0+}; + +/* +* struct xrt_driver - represent a xrt device driver +* +* drv: driver model structure. drv vs driver?
will fix this.
Quoted 5 lines
Quoted 3 lines
+ *
id_table: pointer to table of device IDs the driver is interested in.
+ * { } member terminated.?
file_ops?I will remove '
id_table' and add comment as below:* driver: driver model structure.
*subdev_id: id of the xrt device. See enumxrt_subdev_id.
*file_ops: character device name and callbacks.
* probe: mandatory callback for device binding.
* remove: callback for device unbinding.
*leaf_call: callback for servicing other leaf drivers.Quoted 460 lines
Quoted 460 lines
+ * probe: mandatory callback for device binding.
+ * remove: callback for device unbinding.
+ */drivers/fpga/xrt/include/xdevice.h new file +13
-0+struct xrt_driver { + struct device_driver driver; + u32 subdev_id; + struct xrt_dev_file_ops file_ops; + struct xrt_dev_endpoints *endpoints; + + /* + * Subdev driver callbacks populated by subdev driver. + */ + int (*probe)(struct xrt_device *xrt_dev); + void (*remove)(struct xrt_device *xrt_dev); + /* + * If leaf_call is defined, these are called by other leafdrivers.
+ * Note that root driver may call intoleaf_callof a group driver.
+ */
+ int (*leaf_call)(structxrt_device*xrt_dev, u32 cmd, void *arg);drivers/fpga/xrt/include/xdevice.h new file +11
-0+}; + +#define to_xrt_dev (d) container_of(d, struct xrt_device, dev) +#define to_xrt_drv (d) container_of(d, struct xrt_driver, driver) + +static inline void *xrt_get_drvdata (const struct xrt_device *xdev) +{ + return dev_get_drvdata(&xdev->dev); +} + +static inline void xrt_set_drvdata(struct xrt_device *xdev, void*data)
drivers/fpga/xrt/include/xdevice.h new file +20
-0+{ + dev_set_drvdata(&xdev->dev, data); +} + +static inline void *xrt_get_xdev_data (struct device *dev) +{ + struct xrt_device *xdev = to_xrt_dev(dev); + + return xdev->sdev_data; +} + +struct xrt_device * +xrt_device_register (struct device *parent,u32 id, + struct resource *res, u32 res_num, + void *pdata, size_t data_sz); +void xrt_device_unregister (struct xrt_device *xdev); +int xrt_register_driver (struct xrt_driver *drv); +void xrt_unregister_driver (struct xrt_driver *drv); +void *xrt_get_xdev_data (struct device *dev); +struct resource *xrt_get_resource(struct xrt_device *xdev, u32type, u32 num);
drivers/fpga/xrt/include/xdevice.h new file +2
-0+ +#endif /* _XRT_DEVICE_H_ */ drivers/fpga/xrt/include/xleaf.h new file +19
-0@@ -0,0 +1,205 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +* Sonal Santan <[email protected]> +*/ + +#ifndef _XRT_XLEAF_H_ +#define _XRT_XLEAF_H_ + +#include <linux/mod_devicetable.h> +#include "xdevice.h" +#include "subdev_id.h" +#include "xroot.h" +#include "events.h" + +/* All subdev drivers should use below common routines to print out msg. */
drivers/fpga/xrt/include/xleaf.h new file +10
-0+#define DEV (xdev) (&(xdev)->dev) +#define DEV_PDATA (xdev) \ + ((struct xrt_subdev_platdata *)xrt_get_xdev_data(DEV(xdev))) +#define DEV_FILE_OPS (xdev) \ + (&(to_xrt_drv((xdev)->dev.driver))->file_ops) +#define FMT_PRT(prt_fn, xdev, fmt, args...) \ + ({typeof(xdev) (_xdev) = (xdev); \ + prt_fn(DEV(_xdev), "%s %s: " fmt, \ + DEV_PDATA(_xdev)->xsp_root_name, __func__, ##args); }) +#define xrt_err(xdev, fmt, args...) FMT_PRT(dev_err, xdev, fmt,##args)
+#definexrt_warn(xdev, fmt, args...)FMT_PRT(dev_warn, xdev, fmt, ##args)
+#definexrt_info(xdev, fmt, args...)FMT_PRT(dev_info, xdev, fmt, ##args)
+#definexrt_dbg(xdev, fmt, args...)FMT_PRT(dev_dbg, xdev, fmt, ##args)drivers/fpga/xrt/include/xleaf.h new file +22
-0+ +#define XRT_DEFINE_REGMAP_CONFIG (config_name) \ + static const struct regmap_config config_name = { \ + .reg_bits = 32, \ + .val_bits = 32, \ + .reg_stride = 4, \ + .max_register = 0x1000, \ + } + +enum { + /* Starting cmd for common leaf cmd implemented by all leaves. */ + XRT_XLEAF_COMMON_BASE = 0, + /* Starting cmd for leaves' specific leaf cmds. */ + XRT_XLEAF_CUSTOM_BASE = 64, +}; + +enum xrt_xleaf_common_leaf_cmd { + XRT_XLEAF_EVENT = XRT_XLEAF_COMMON_BASE, +}; + +/* +* Partially initialized by the parent driver, then, passed in as subdev driver's
+ * platform data when creating subdev driver instance by calling platform
+ * device register API (xrt_device_register_data()or the likes).
+ *
+ * Once device register API returns, platform driver framework makes a copy of
+ * this buffer and maintains its life cycle. The content of the buffer is
+ * completely owned by subdev driver.
+ *
+ * Thus, parent driver should be very careful when it touches this buffer
+ * again once it's handed over to subdev driver. And the data structure
+ * should not contain pointers pointing to buffers that is managed by
+ * other or parent drivers since it could have been freed before platform
+ * data buffer is freed by platform driver framework.
+ */
+structxrt_subdev_platdata{
+ /*
+ * Per driver instance callback. The xdev points to the instance. + * Should always be defined for subdev driver to get service from root.
+ */
+xrt_subdev_root_cb_txsp_root_cb;
+ void*xsp_root_cb_arg;drivers/fpga/xrt/include/xleaf.h new file +18
-0+ + /* Something to associate w/ root for msg printing. */ + const char *xsp_root_name; + + /* + * Char dev support for this subdev instance. + * Initialized by subdev driver. + */ + struct cdev xsp_cdev; + struct device *xsp_sysdev; + struct mutex xsp_devnode_lock; /* devnode lock */ + struct completion xsp_devnode_comp; + int xsp_devnode_ref; + bool xsp_devnode_online; + bool xsp_devnode_excl; + + /* +* Subdev driver specific init data. The buffer should be embedded
+ * in this data structure buffer after dtb, so that it can be freed
+ * together with platform data.
+ */
+loff_txsp_priv_off; /* Offset into this platform data buffer. */ +size_txsp_priv_len;
+
+ /*
+ * Populated by parent driver to describe the device tree for + * the subdev driver to handle. Should always be last one since it's
+ * of variable length.
+ */
+ boolxsp_dtb_valid;
+ char xsp_dtb[0];drivers/fpga/xrt/include/xleaf.h new file +8
-0+}; + +struct subdev_match_arg { + enum xrt_subdev_id id; + int instance; +}; + +bool xleaf_has_endpoint(struct xrt_device *xdev, const char
*endpoint_name);drivers/fpga/xrt/include/xleaf.h new file +4
-0+struct xrt_device *xleaf_get_leaf (struct xrt_device *xdev, + xrt_subdev_match_t cb, void *arg); + +static inline bool subdev_match(enum xrt_subdev_id id, struct
xrt_device*xdev, void *arg)
+{
+ const structsubdev_match_arg*a = (structsubdev_match_arg*)arg;
+ int instance =a->instance;
+
+ if (id !=a->id)
+ return false;
+ if (instance !=xdev->instance&& instance !=XRT_INVALID_DEVICE_INST)
+ return false;
+ return true;drivers/fpga/xrt/include/xleaf.h new file +4
-0+} + +static inline bool xrt_subdev_match_epname(enum xrt_subdev_id id, + struct xrt_device *xdev,void *arg)
drivers/fpga/xrt/include/xleaf.h new file +17
-0+{ + return xleaf_has_endpoint(xdev, arg); +} + +static inline struct xrt_device * +xleaf_get_leaf_by_id (struct xrt_device *xdev, + enum xrt_subdev_id id, int instance) +{ + struct subdev_match_arg arg = { id, instance }; + + return xleaf_get_leaf(xdev, subdev_match, &arg); +} + +static inline struct xrt_device * +xleaf_get_leaf_by_epname (struct xrt_device *xdev,const char *name) +{ + return xleaf_get_leaf(xdev, xrt_subdev_match_epname, (void*)name);
drivers/fpga/xrt/include/xleaf.h new file +3
-0+} + +static inline int xleaf_call(struct xrt_device *tgt, u32 cmd, void*arg)
drivers/fpga/xrt/include/xleaf.h new file +5
-0+{ + return (to_xrt_drv(tgt->dev.driver)->leaf_call)(tgt, cmd, arg); +} + +int xleaf_broadcast_event(struct xrt_device *xdev, enum xrt_eventsevt, bool async);
drivers/fpga/xrt/include/xleaf.h new file +3
-0+int xleaf_create_group (struct xrt_device *xdev,char *dtb); +int xleaf_destroy_group (struct xrt_device *xdev,int instance); +void xleaf_get_root_res(struct xrt_device *xdev, u32 region_id,struct resource **res);
+void xleaf_get_root_id(structxrt_device*xdev, unsigned short *vendor, unsigned short *device,
+ unsigned short *subvendor, unsigned short *subdevice);drivers/fpga/xrt/include/xleaf.h new file +3
-0+void xleaf_hot_reset (struct xrt_device *xdev); +int xleaf_put_leaf (struct xrt_device *xdev,struct xrt_device *leaf); +struct device *xleaf_register_hwmon(struct xrt_device *xdev, constchar *name, void *drvdata,
+ const structattribute_group**grps); +voidxleaf_unregister_hwmon(struct xrt_device *xdev, struct device *hwmon);drivers/fpga/xrt/include/xleaf.h new file +21
-0+int xleaf_wait_for_group_bringup (struct xrt_device *xdev); + +/* +* Character device helper APIs for use by leaf drivers +*/ +static inline bool xleaf_devnode_enabled (struct xrt_device *xdev) +{ + return DEV_FILE_OPS(xdev)->xsf_ops.open; +} + +int xleaf_devnode_create (struct xrt_device *xdev, + const char *file_name, const char *inst_name); +void xleaf_devnode_destroy (struct xrt_device *xdev); + +struct xrt_device *xleaf_devnode_open_excl (struct inode *inode); +struct xrt_device *xleaf_devnode_open (struct inode *inode); +void xleaf_devnode_close (struct inode *inode); + +/* Module's init/fini routines for leaf driver in xrt-lib module */ +#define XRT_LEAF_INIT_FINI_FUNC (name) \ +void name##_leaf_init_fini(boolinit) \
drivers/fpga/xrt/include/xleaf.h new file +19
-0+{ \ + if (init) \ + xrt_register_driver(&xrt_##name##_driver); \ + else \ + xrt_unregister_driver(&xrt_##name##_driver); \ +} + +/* Module's init/fini routines for leaf driver in xrt-lib module */ +void group_leaf_init_fini(bool init); +void vsec_leaf_init_fini(bool init); +void devctl_leaf_init_fini(bool init); +void axigate_leaf_init_fini(bool init); +void icap_leaf_init_fini(bool init); +void calib_leaf_init_fini(bool init); +void clkfreq_leaf_init_fini(bool init); +void clock_leaf_init_fini(bool init); +void ucs_leaf_init_fini(bool init); + +#endif /* _XRT_LEAF_H_ */drivers/fpga/xrt/include/xleaf/clkfreq.h new file +18
-0@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_CLKFREQ_H_ +#define _XRT_CLKFREQ_H_ + +#include "xleaf.h" + +/* +* CLKFREQ driver leaf calls. +*/ +enum xrt_clkfreq_leaf_cmd { + XRT_CLKFREQ_READ = XRT_XLEAF_CUSTOM_BASE, /* See comments in
xleaf.h*/drivers/fpga/xrt/include/xleaf/clkfreq.h new file +3
-0+}; + +#endif /* _XRT_CLKFREQ_H_ */ drivers/fpga/xrt/include/xleaf/clock.h new file +19
-0@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_CLOCK_H_ +#define _XRT_CLOCK_H_ + +#include "xleaf.h" +#include <linux/fpga_xrt.h> + +/* +* CLOCK driver leaf calls. +*/ +enum xrt_clock_leaf_cmd { + XRT_CLOCK_SET = XRT_XLEAF_CUSTOM_BASE, /* See comments in
xleaf.h*/
+XRT_CLOCK_GET,
+XRT_CLOCK_VERIFY,drivers/fpga/xrt/include/xleaf/clock.h new file +8
-0+}; + +struct xrt_clock_get { + u16 freq; + u32 freq_cnter; +}; + +#endif /* _XRT_CLOCK_H_ */drivers/fpga/xrt/include/xleaf/ddr_calibration.h new file +25
-0@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XRT_DDR_CALIBRATION_H_ +#define _XRT_DDR_CALIBRATION_H_ + +#include "xleaf.h" +#include <linux/fpga-xrt.h> + +/* +* Memory calibration driver leaf calls. +*/ +enum xrt_calib_results { + XRT_CALIB_UNKNOWN = 0, + XRT_CALIB_SUCCEEDED, + XRT_CALIB_FAILED, +}; + +enum xrt_calib_leaf_cmd { + XRT_CALIB_RESULT = XRT_XLEAF_CUSTOM_BASE, /* See comments in
xleaf.h*/drivers/fpga/xrt/include/xleaf/ddr_calibration.h new file +3
-0+}; + +#endif /* _XRT_DDR_CALIBRATION_H_ */ drivers/fpga/xrt/include/xleaf/devctl.h new file +18
-0@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_DEVCTL_H_ +#define _XRT_DEVCTL_H_ + +#include "xleaf.h" + +/* +* DEVCTL driver leaf calls. +*/ +enum xrt_devctl_leaf_cmd { + XRT_DEVCTL_READ = XRT_XLEAF_CUSTOM_BASE, /* See comments in
xleaf.h*/drivers/fpga/xrt/include/xleaf/devctl.h new file +22
-0+}; + +enum xrt_devctl_id { + XRT_DEVCTL_ROM_UUID = 0, + XRT_DEVCTL_DDR_CALIB, + XRT_DEVCTL_GOLDEN_VER, + XRT_DEVCTL_MAX +}; + +struct xrt_devctl_rw { + u32 xdr_id; + void *xdr_buf; + u32 xdr_len; + u32 xdr_offset; +}; + +struct xrt_devctl_intf_uuid { + u32 uuid_num; + uuid_t *uuids; +}; + +#endif /* _XRT_DEVCTL_H_ */drivers/fpga/xrt/lib/lib-drv.c new file +11
-0@@ -0,0 +1,322 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +* Lizhi Hou <[email protected]> +*/ + +#include <linux/module.h> +#include <linux/vmalloc.h> Quoted 8 lines
drivers/fpga/xrt/lib/lib-drv.c line 18
Quoted 7 lines
drivers/fpga/xrt/lib/lib-drv.c new file +7
-0+#include <linux/slab.h> +#include "xleaf.h" +#include "xroot.h" +#include "lib-drv.h" + +#define XRT_IPLIB_MODULE_NAME "xrt-lib" +#define XRT_IPLIB_MODULE_VERSION "4.0.0"No driver versions ...
I will remove the version.
Quoted 15 lines
Quoted 15 lines
drivers/fpga/xrt/lib/lib-drv.c new file +14
-0+#define XRT_DRVNAME (drv) ((drv)->driver.name) + +#define XRT_SUBDEV_ID_SHIFT 16 +#define XRT_SUBDEV_ID_MASK ((1 << XRT_SUBDEV_ID_SHIFT) - 1) + +struct xrt_find_drv_data { + enum xrt_subdev_id id; + struct xrt_driver *xdrv; +}; + +struct class *xrt_class; +static DEFINE_IDA (xrt_device_ida); + +static inline u32 xrt_instance_to_id(enum xrt_subdev_id id, u32instance)
Quoted 8 lines
drivers/fpga/xrt/lib/lib-drv.c line 39
Quoted 7 lines
drivers/fpga/xrt/lib/lib-drv.c new file +7
-0+{ + return (id << XRT_SUBDEV_ID_SHIFT) | instance; +} + +static inline u32 xrt_id_to_instance (u32 id) +{ + return (id & XRT_SUBDEV_ID_MASK);Don't need braces?
I will remove the braces.
Quoted 79 lines
Quoted 79 lines
drivers/fpga/xrt/lib/lib-drv.c new file +3
-0+} + +static int xrt_bus_match(struct device *dev, struct device_driver*drv)
drivers/fpga/xrt/lib/lib-drv.c new file +47
-0+{ + struct xrt_device *xdev = to_xrt_dev(dev); + struct xrt_driver *xdrv = to_xrt_drv(drv); + + if (xdev->subdev_id == xdrv->subdev_id) + return 1; + + return 0; +} + +static int xrt_bus_probe (struct device *dev) +{ + struct xrt_driver *xdrv = to_xrt_drv(dev->driver); + struct xrt_device *xdev = to_xrt_dev(dev); + + return xdrv->probe(xdev); +} + +static int xrt_bus_remove (struct device *dev) +{ + struct xrt_driver *xdrv = to_xrt_drv(dev->driver); + struct xrt_device *xdev = to_xrt_dev(dev); + + if (xdrv->remove) + xdrv->remove(xdev); + + return 0; +} + +struct bus_type xrt_bus_type = { + .name = "xrt", + .match = xrt_bus_match, + .probe = xrt_bus_probe, + .remove = xrt_bus_remove, +}; + +int xrt_register_driver (struct xrt_driver *drv) +{ + const char *drvname = XRT_DRVNAME(drv); + int rc = 0; + + /* Initialize dev_t for char dev node. */ + if (drv->file_ops.xsf_ops.open) { + rc = alloc_chrdev_region(&drv->file_ops.xsf_dev_t, 0, + XRT_MAX_DEVICE_NODES, drvname); + if (rc) { + pr_err("failed to alloc dev minor for %s:%d\n", drvname, rc);
+ return rc;
+ }
+ } else {
+drv->file_ops.xsf_dev_t= (dev_t)-1; + }drivers/fpga/xrt/lib/lib-drv.c new file +22
-0+ + drv->driver.owner = THIS_MODULE; + drv->driver.bus = &xrt_bus_type; + + rc = driver_register(&drv->driver); + if (rc) { + pr_err("register %s xrt driver failed\n", drvname); + if (drv->file_ops.xsf_dev_t != (dev_t)-1) { +unregister_chrdev_region (drv->file_ops.xsf_dev_t, +XRT_MAX_DEVICE_NODES ); + } + return rc; + } + + pr_info("%s registered successfully\n", drvname); + + return 0; +} +EXPORT_SYMBOL_GPL (xrt_register_driver); + +void xrt_unregister_driver (struct xrt_driver *drv) +{Quoted 8 lines
Quoted 7 lines
+
driver_unregister(&drv->driver);
+
+ if (drv->file_ops.xsf_dev_t!= (dev_t)-1)
+unregister_chrdev_region(drv->file_ops.xsf_dev_t, XRT_MAX_DEVICE_NODES);
+
+pr_info("%s unregistered successfully\n", XRT_DRVNAME(drv));Drop this.
Sure.
Quoted 88 lines
Quoted 88 lines
drivers/fpga/xrt/lib/lib-drv.c new file +35
-0+} +EXPORT_SYMBOL_GPL (xrt_unregister_driver); + +static int __find_driver (struct device_driver *drv,void *_data) +{ + struct xrt_driver *xdrv = to_xrt_drv(drv); + struct xrt_find_drv_data *data = _data; + + if (xdrv->subdev_id == data->id) { + data->xdrv = xdrv; + return 1; + } + + return 0; +} + +const char *xrt_drv_name (enum xrt_subdev_id id) +{ + struct xrt_find_drv_data data = { 0 }; + + data.id = id; + bus_for_each_drv(&xrt_bus_type, NULL, &data, __find_driver); + + if (data.xdrv) + return XRT_DRVNAME(data.xdrv); + + return NULL; +} + +static int xrt_drv_get_instance (enum xrt_subdev_id id) +{ + int ret; + + ret = ida_alloc_range(&xrt_device_ida, xrt_instance_to_id(id, 0), + xrt_instance_to_id(id,
XRT_MAX_DEVICE_NODES),
+GFP_KERNEL);
+ if (ret < 0)
+ return ret;drivers/fpga/xrt/lib/lib-drv.c new file +25
-0+ + return xrt_id_to_instance((u32)ret); +} + +static void xrt_drv_put_instance (enum xrt_subdev_id id,int instance) +{ + ida_free(&xrt_device_ida, xrt_instance_to_id(id, instance)); +} + +struct xrt_dev_endpoints *xrt_drv_get_endpoints (enum xrt_subdev_id id) +{ + struct xrt_find_drv_data data = { 0 }; + + data.id = id; + bus_for_each_drv(&xrt_bus_type, NULL, &data, __find_driver); + + if (data.xdrv) + return data.xdrv->endpoints; + + return NULL; +} + +static void xrt_device_release (struct device *dev) +{ + struct xrt_device *xdev = container_of(dev, struct xrt_device,dev);
drivers/fpga/xrt/lib/lib-drv.c new file +23
-0+ + kfree(xdev); +} + +void xrt_device_unregister (struct xrt_device *xdev) +{ + if (xdev->state == XRT_DEVICE_STATE_ADDED) + device_del(&xdev->dev); + + vfree(xdev->sdev_data); + kfree(xdev->resource); + + if (xdev->instance != XRT_INVALID_DEVICE_INST) + xrt_drv_put_instance(xdev->subdev_id, xdev->instance); + + if (xdev->dev.release == xrt_device_release) + put_device(&xdev->dev); +} + +struct xrt_device * +xrt_device_register (struct device *parent,u32 id, + struct resource *res, u32 res_num, + void *pdata, size_t data_sz)Quoted 8 lines
drivers/fpga/xrt/lib/lib-drv.c line 215
Quoted 7 lines
drivers/fpga/xrt/lib/lib-drv.c new file +7
-0+{ + struct xrt_device *xdev = NULL; + int ret; + + xdev = kzalloc(sizeof(*xdev), GFP_KERNEL); + if (!xdev) + return NULL;Nit: return xdev
Sure.
Quoted 22 lines
Quoted 22 lines
+
xdev->instance=XRT_INVALID_DEVICE_INST;drivers/fpga/xrt/lib/lib-drv.c new file +20
-0+ + /* Obtain dev instance number. */ + ret = xrt_drv_get_instance(id); + if (ret < 0) { + dev_err(parent, "failed get instance, ret %d", ret); + goto fail; + } + + xdev->instance = ret; + xdev->name = xrt_drv_name(id); + xdev->subdev_id = id; + device_initialize(&xdev->dev); + xdev->dev.release = xrt_device_release; + xdev->dev.parent = parent; + + xdev->dev.bus = &xrt_bus_type; + dev_set_name(&xdev->dev, "%s.%d", xdev->name, xdev->instance); + + xdev->num_resources = res_num; + xdev->resource = kmemdup(res, sizeof(*res) * res_num,
GFP_KERNEL);Quoted 8 lines
Quoted 6 lines
+ if (!xdev->resource)
+ goto fail;
+
+xdev->sdev_data=vzalloc(data_sz);
+ if (!xdev->sdev_data)
+ goto fail;Do you need another label here? I don't see this getting cleaned up if
device_add()fails below ...?
sdev_datawill be cleaned up inxrt_device_unregister().Thanks,
LizhiQuoted 113 lines
Quoted 113 lines
drivers/fpga/xrt/lib/lib-drv.c new file +20
-0+ + memcpy(xdev->sdev_data, pdata, data_sz); + + ret = device_add(&xdev->dev); + if (ret) { + dev_err(parent, "failed add device, ret %d", ret); + goto fail; + } + xdev->state = XRT_DEVICE_STATE_ADDED; + + return xdev; + +fail: + xrt_device_unregister(xdev); + kfree(xdev); + + return NULL; +} + +struct resource *xrt_get_resource(struct xrt_device *xdev, u32type, u32 num)
drivers/fpga/xrt/lib/lib-drv.c new file +14
-0+{ + u32 i; + + for (i = 0; i < xdev->num_resources; i++) { + struct resource *r = &xdev->resource[i]; + + if (type == resource_type(r) && num-- == 0) + return r; + } + return NULL; +} + +/* +* Leaf driver's module init/fini callbacks. This is not a open infrastructure for dynamic
+ * plugging in drivers. All drivers should be statically added.
+ */drivers/fpga/xrt/lib/lib-drv.c new file +44
-0+static void (*leaf_init_fini_cbs[])(bool ) = { + group_leaf_init_fini, + axigate_leaf_init_fini, + icap_leaf_init_fini, +}; + +static __init int xrt_lib_init (void ) +{ + int ret; + int i; + + ret = bus_register(&xrt_bus_type); + if (ret) + return ret; + + xrt_class = class_create(THIS_MODULE, XRT_IPLIB_MODULE_NAME); + if (IS_ERR(xrt_class)) { + bus_unregister(&xrt_bus_type); + return PTR_ERR(xrt_class); + } + + for (i = 0; i < ARRAY_SIZE(leaf_init_fini_cbs); i++) + leaf_init_fini_cbs[i](true); + return 0; +} + +static __exit void xrt_lib_fini(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(leaf_init_fini_cbs); i++) + leaf_init_fini_cbs[i](false); + + class_destroy(xrt_class); + bus_unregister(&xrt_bus_type); +} + +module_init (xrt_lib_init); +module_exit (xrt_lib_fini); + +MODULE_VERSION (XRT_IPLIB_MODULE_VERSION ); +MODULE_AUTHOR ("XRT Team <[email protected]>" ); +MODULE_DESCRIPTION ("Xilinx Alveo IP Lib driver" ); +MODULE_LICENSE ("GPL v2" );drivers/fpga/xrt/lib/lib-drv.h new file +19
-0@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _LIB_DRV_H_ +#define _LIB_DRV_H_ + +#include <linux/device/class.h> +#include <linux/device/bus.h> + +extern struct class *xrt_class; +extern struct bus_type xrt_bus_type; + +const char *xrt_drv_name (enum xrt_subdev_id id); +struct xrt_dev_endpoints *xrt_drv_get_endpoints(enum xrt_subdev_idid);
drivers/fpga/xrt/lib/lib-drv.h new file +2
-0+ +#endif /* _LIB_DRV_H_ */Signature · 1 line
2.27.0
PATCH v8 5/14 fpga: xrt: group driver
In reply to Lizhi Hougroup driver that manages life cycle of a bunch of leaf driver instances and bridges them with root.
--- drivers/fpga/xrt/include/group.h | 25 +++ drivers/fpga/xrt/lib/group.c | 278 +++++++++++++++++++++++++++++++ 2 files changed, 303 insertions(+) create mode 100644 drivers/fpga/xrt/include/group.h create mode 100644 drivers/fpga/xrt/lib/group.c
drivers/fpga/xrt/include/group.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XRT_GROUP_H_ +#define _XRT_GROUP_H_ + +#include "xleaf.h" + +/* +* Group driver leaf calls. +*/ +enum xrt_group_leaf_cmd { +XRT_GROUP_GET_LEAF =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +XRT_GROUP_PUT_LEAF , +XRT_GROUP_INIT_CHILDREN , +XRT_GROUP_FINI_CHILDREN , +XRT_GROUP_TRIGGER_EVENT , +}; + +#endif /* _XRT_GROUP_H_ */
drivers/fpga/xrt/lib/group.c new file +278 -0
278 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v8 6/14 fpga: xrt: char dev node helper functions
In reply to Lizhi HouHelper functions for char device node creation / removal for xrt drivers. This is part of xrt driver infrastructure.
--- drivers/fpga/xrt/lib/cdev.c | 209 ++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 drivers/fpga/xrt/lib/cdev.c
drivers/fpga/xrt/lib/cdev.c new file +209 -0
@@ -0,0 +1,209 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Xilinx Alveo FPGA device node helper functions. +* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#include "xleaf.h" +#include "lib-drv.h" + +#define XRT_CDEV_DIR "xrt" +#define INODE2PDATA (inode) \ + container_of((inode)->i_cdev, struct xrt_subdev_platdata, xsp_cdev) +#define INODE2PDEV (inode) \ + to_xrt_dev(kobj_to_dev((inode)->i_cdev->kobj.parent)) +#define CDEV_NAME (sysdev) (strchr((sysdev)->kobj.name, '!') + 1) + +/* Allow it to be accessed from cdev. */ +static void xleaf_devnode_allowed (struct xrt_device *xdev) +{ +struct xrt_subdev_platdata *pdata =DEV_PDATA (xdev); + +/* Allow new opens. */ +mutex_lock (&pdata->xsp_devnode_lock); + pdata->xsp_devnode_online = true; +mutex_unlock (&pdata->xsp_devnode_lock); +} + +/* Turn off access from cdev and wait for all existing user to go away. */ +static void xleaf_devnode_disallowed (struct xrt_device *xdev) +{ +struct xrt_subdev_platdata *pdata =DEV_PDATA (xdev); + +mutex_lock (&pdata->xsp_devnode_lock); + +/* Prevent new opens. */ + pdata->xsp_devnode_online = false; +/* Wait for existing user to close. */ +while (pdata->xsp_devnode_ref) { +mutex_unlock (&pdata->xsp_devnode_lock); +wait_for_completion (&pdata->xsp_devnode_comp); +mutex_lock (&pdata->xsp_devnode_lock); + } + +mutex_unlock (&pdata->xsp_devnode_lock); +} + +static struct xrt_device * +__xleaf_devnode_open (struct inode *inode,bool excl) +{ +struct xrt_subdev_platdata *pdata =INODE2PDATA (inode); +struct xrt_device *xdev =INODE2PDEV (inode); +bool opened = false; + +mutex_lock (&pdata->xsp_devnode_lock); + +if (pdata->xsp_devnode_online) { +if (excl && pdata->xsp_devnode_ref) { +xrt_err (xdev,"%s has already been opened exclusively" , +CDEV_NAME (pdata->xsp_sysdev)); + }else if (!excl && pdata->xsp_devnode_excl) { +xrt_err (xdev,"%s has been opened exclusively" , +CDEV_NAME (pdata->xsp_sysdev)); + }else { + pdata->xsp_devnode_ref++; + pdata->xsp_devnode_excl = excl; + opened = true; +xrt_info (xdev,"opened %s, ref=%d" , +CDEV_NAME (pdata->xsp_sysdev), + pdata->xsp_devnode_ref); + } + }else { +xrt_err (xdev,"%s is offline" ,CDEV_NAME (pdata->xsp_sysdev)); + } + +mutex_unlock (&pdata->xsp_devnode_lock); + + xdev = opened ? xdev :NULL ; +return xdev; +} + +struct xrt_device * +xleaf_devnode_open_excl (struct inode *inode) +{ +return __xleaf_devnode_open (inode, true); +} + +struct xrt_device * +xleaf_devnode_open (struct inode *inode) +{ +return __xleaf_devnode_open (inode, false); +} +EXPORT_SYMBOL_GPL (xleaf_devnode_open); + +void xleaf_devnode_close (struct inode *inode) +{ +struct xrt_subdev_platdata *pdata =INODE2PDATA (inode); +struct xrt_device *xdev =INODE2PDEV (inode); +bool notify = false; + +mutex_lock (&pdata->xsp_devnode_lock); + +WARN_ON (pdata->xsp_devnode_ref ==0 ); + pdata->xsp_devnode_ref--; +if (pdata->xsp_devnode_ref ==0 ) { + pdata->xsp_devnode_excl = false; + notify = true; + } +if (notify) +xrt_info (xdev,"closed %s" ,CDEV_NAME (pdata->xsp_sysdev)); +else +xrt_info (xdev,"closed %s, notifying waiter" ,CDEV_NAME (pdata->xsp_sysdev)); + +mutex_unlock (&pdata->xsp_devnode_lock); + +if (notify) +complete (&pdata->xsp_devnode_comp); +} +EXPORT_SYMBOL_GPL (xleaf_devnode_close); + +static inline enum xrt_dev_file_mode +devnode_mode (struct xrt_device *xdev) +{ +return DEV_FILE_OPS (xdev)->xsf_mode; +} + +int xleaf_devnode_create (struct xrt_device *xdev,const char *file_name, +const char *inst_name) +{ +struct xrt_subdev_platdata *pdata =DEV_PDATA (xdev); +struct xrt_dev_file_ops *fops =DEV_FILE_OPS (xdev); +struct cdev *cdevp; +struct device *sysdev; +int ret =0 ; +char fname[256 ]; + +mutex_init (&pdata->xsp_devnode_lock); +init_completion (&pdata->xsp_devnode_comp); + + cdevp = &DEV_PDATA (xdev)->xsp_cdev; +cdev_init (cdevp, &fops->xsf_ops); + cdevp->owner = fops->xsf_ops.owner; + cdevp->dev =MKDEV (MAJOR (fops->xsf_dev_t), xdev->instance); + +/* +* Set xdev as parent of cdev so that when xdev (and its platform +* data) will not be freed when cdev is not freed. +*/ +cdev_set_parent (cdevp, &DEV (xdev)->kobj); + + ret =cdev_add (cdevp, cdevp->dev,1 ); +if (ret) { +xrt_err (xdev,"failed to add cdev: %d" , ret); + goto failed; + } +if (!file_name) + file_name = xdev->name; +if (!inst_name) { +if (devnode_mode (xdev) ==XRT_DEV_FILE_MULTI_INST ) { +snprintf (fname,sizeof (fname),"%s/%s/%s.%u" , +XRT_CDEV_DIR ,DEV_PDATA (xdev)->xsp_root_name, + file_name, xdev->instance); + }else { +snprintf (fname,sizeof (fname),"%s/%s/%s" , +XRT_CDEV_DIR ,DEV_PDATA (xdev)->xsp_root_name, + file_name); + } + }else { +snprintf (fname,sizeof (fname),"%s/%s/%s.%s" ,XRT_CDEV_DIR , +DEV_PDATA (xdev)->xsp_root_name, file_name, inst_name); + } + sysdev =device_create (xrt_class,NULL , cdevp->dev,NULL ,"%s" , fname); +if (IS_ERR (sysdev)) { + ret =PTR_ERR (sysdev); +xrt_err (xdev,"failed to create device node: %d" , ret); + goto failed_cdev_add; + } + pdata->xsp_sysdev = sysdev; + +xleaf_devnode_allowed (xdev); + +xrt_info (xdev,"created (%d, %d): /dev/%s" , +MAJOR (cdevp->dev), xdev->instance, fname); +return 0 ; + +failed_cdev_add: +cdev_del (cdevp); +failed: + cdevp->owner =NULL ; +return ret; +} + +void xleaf_devnode_destroy (struct xrt_device *xdev) +{ +struct xrt_subdev_platdata *pdata =DEV_PDATA (xdev); +struct cdev *cdevp = &pdata->xsp_cdev; +dev_t dev = cdevp->dev; + +xleaf_devnode_disallowed (xdev); + +xrt_info (xdev,"removed (%d, %d): /dev/%s/%s" ,MAJOR (dev),MINOR (dev), +XRT_CDEV_DIR ,CDEV_NAME (pdata->xsp_sysdev)); +device_destroy (xrt_class, cdevp->dev); + pdata->xsp_sysdev =NULL ; +cdev_del (cdevp); +}
Signature · 1 line
2.27.0
PATCH v8 7/14 fpga: xrt: root driver infrastructure
In reply to Lizhi HouContains common code for all root drivers and handles root calls from xrt drivers. This is part of root driver infrastructure.
--- drivers/fpga/xrt/include/events.h | 45 +++ drivers/fpga/xrt/include/xroot.h | 117 +++++++ drivers/fpga/xrt/lib/subdev_pool.h | 53 +++ drivers/fpga/xrt/lib/xroot.c | 536 +++++++++++++++++++++++++++++ 4 files changed, 751 insertions(+) create mode 100644 drivers/fpga/xrt/include/events.h create mode 100644 drivers/fpga/xrt/include/xroot.h create mode 100644 drivers/fpga/xrt/lib/subdev_pool.h create mode 100644 drivers/fpga/xrt/lib/xroot.c
drivers/fpga/xrt/include/events.h new file +45 -0
@@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XRT_EVENTS_H_ +#define _XRT_EVENTS_H_ + +#include "subdev_id.h" + +/* +* Event notification. +*/ +enum xrt_events { +XRT_EVENT_TEST =0 ,/* for testing */ +/* +* Events related to specific subdev +* Callback arg: struct xrt_event_arg_subdev +*/ +XRT_EVENT_POST_CREATION , +XRT_EVENT_PRE_REMOVAL , +/* +* Events related to change of the whole board +* Callback arg: <none> +*/ +XRT_EVENT_PRE_HOT_RESET , +XRT_EVENT_POST_HOT_RESET , +XRT_EVENT_PRE_GATE_CLOSE , +XRT_EVENT_POST_GATE_OPEN , +}; + +struct xrt_event_arg_subdev { +enum xrt_subdev_id xevt_subdev_id; +int xevt_subdev_instance; +}; + +struct xrt_event { +enum xrt_events xe_evt; +struct xrt_event_arg_subdev xe_subdev; +}; + +#endif /* _XRT_EVENTS_H_ */
drivers/fpga/xrt/include/xroot.h new file +117 -0
117 lines not on this page · raw message
drivers/fpga/xrt/lib/subdev_pool.h new file +53 -0
53 lines not on this page · raw message
drivers/fpga/xrt/lib/xroot.c new file +536 -0
536 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v8 8/14 fpga: xrt: driver infrastructure
In reply to Lizhi HouInfrastructure code providing APIs for managing leaf driver instance groups, facilitating inter-leaf driver calls and root calls.
--- drivers/fpga/xrt/lib/subdev.c | 859 ++++++++++++++++++++++++++++++++++ 1 file changed, 859 insertions(+) create mode 100644 drivers/fpga/xrt/lib/subdev.c
drivers/fpga/xrt/lib/subdev.c new file +859 -0
@@ -0,0 +1,859 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#include <linux/vmalloc.h> +#include <linux/slab.h> +#include "xleaf.h" +#include "subdev_pool.h" +#include "lib-drv.h" +#include "metadata.h" + +extern struct bus_type xrt_bus_type; + +#define IS_ROOT_DEV (dev) ((dev)->bus != &xrt_bus_type) +#define XRT_HOLDER_BUF_SZ 1024 + +static inline struct device *find_root (struct xrt_device *xdev) +{ +struct device *d =DEV (xdev); + +while (!IS_ROOT_DEV (d)) + d = d->parent; +return d; +} + +/* +* It represents a holder of a subdev. One holder can repeatedly hold a subdev +* as long as there is a unhold corresponding to a hold. +*/ +struct xrt_subdev_holder { +struct list_head xsh_holder_list; +struct device *xsh_holder; +int xsh_count; +struct kref xsh_kref; +}; + +/* +* It represents a specific instance of platform driver for a subdev, which +* provides services to its clients (another subdev driver or root driver). +*/ +struct xrt_subdev { +struct list_head xs_dev_list; +struct list_head xs_holder_list; +enum xrt_subdev_id xs_id;/* type of subdev */ +struct xrt_device *xs_xdev; +struct completion xs_holder_comp; +}; + +static struct xrt_subdev *xrt_subdev_alloc (void ) +{ +struct xrt_subdev *sdev =kzalloc (sizeof (*sdev),GFP_KERNEL ); + +if (!sdev) +return NULL ; + +INIT_LIST_HEAD (&sdev->xs_dev_list); +INIT_LIST_HEAD (&sdev->xs_holder_list); +init_completion (&sdev->xs_holder_comp); +return sdev; +} + +int xrt_subdev_root_request (struct xrt_device *self,u32 cmd,void *arg) +{ +struct device *dev =DEV (self); +struct xrt_subdev_platdata *pdata =DEV_PDATA (self); + +if (!pdata->xsp_root_cb) { +dev_err (dev,"invalid root callback" ); +return -EINVAL ; + } +return (*pdata->xsp_root_cb)(dev->parent, pdata->xsp_root_cb_arg, cmd, arg); +} + +/* +* Subdev common sysfs nodes. +*/ +static ssize_t holders_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +ssize_t len; +struct xrt_device *xdev =to_xrt_dev (dev); +struct xrt_root_get_holders holders = { xdev, buf,XRT_HOLDER_BUF_SZ }; + + len =xrt_subdev_root_request (xdev,XRT_ROOT_GET_LEAF_HOLDERS , &holders); +if (len >= holders.xpigh_holder_buf_len) +return len; + buf[len] ='\n' ; +return len +1 ; +} +static DEVICE_ATTR_RO (holders); + +static struct attribute *xrt_subdev_attrs[] = { + &dev_attr_holders.attr, +NULL , +}; + +static ssize_t metadata_output (struct file *filp,struct kobject *kobj, +struct bin_attribute *attr,char *buf,loff_t off,size_t count) +{ +struct device *dev =kobj_to_dev (kobj); +struct xrt_device *xdev =to_xrt_dev (dev); +struct xrt_subdev_platdata *pdata =DEV_PDATA (xdev); +unsigned char *blob; +unsigned long size; +ssize_t ret =0 ; + + blob = pdata->xsp_dtb; + size =xrt_md_size (dev, blob); +if (size ==XRT_MD_INVALID_LENGTH ) { + ret = -EINVAL ; + goto failed; + } + +if (off >= size) { +dev_dbg (dev,"offset (%lld) beyond total size: %ld\n" , off, size); + goto failed; + } + +if (off + count > size) { +dev_dbg (dev,"count (%ld) beyond left bytes: %lld\n" , + (unsigned long )count, size - off); + count = size - off; + } +memcpy (buf, blob + off, count); + + ret = count; +failed: +return ret; +} + +static struct bin_attribute meta_data_attr = { + .attr = { + .name ="metadata" , + .mode =0400 + }, + .read = metadata_output, + .size =0 +}; + +static struct bin_attribute *xrt_subdev_bin_attrs[] = { + &meta_data_attr, +NULL , +}; + +static const struct attribute_group xrt_subdev_attrgroup = { + .attrs = xrt_subdev_attrs, + .bin_attrs = xrt_subdev_bin_attrs, +}; + +/* +* Given the device metadata, parse it to get IO ranges and construct +* resource array. +*/ +static int +xrt_subdev_getres (struct device *parent,enum xrt_subdev_id id, +char *dtb,struct resource **res,int *res_num) +{ +struct xrt_subdev_platdata *pdata; +struct resource *pci_res =NULL ; +const __be64 *bar_range; +const __be32 *bar_idx; +char *ep_name =NULL , *compat =NULL ; +uint bar; +int count1 =0 , count2 =0 , ret; + +if (!dtb) +return -EINVAL ; + + pdata =DEV_PDATA (to_xrt_dev (parent)); + +/* go through metadata and count endpoints in it */ +xrt_md_get_next_endpoint (parent, dtb,NULL ,NULL , &ep_name, &compat); +while (ep_name) { + ret =xrt_md_get_prop (parent, dtb, ep_name, compat, +XRT_MD_PROP_IO_OFFSET , (const void **)&bar_range,NULL ); +if (!ret) + count1++; +xrt_md_get_next_endpoint (parent, dtb, ep_name, compat, &ep_name, &compat); + } +if (!count1) +return 0 ; + +/* allocate resource array for all endpoints been found in metadata */ + *res =vzalloc (sizeof (**res) * count1); + +/* go through all endpoints again and get IO range for each endpoint */ + ep_name =NULL ; +xrt_md_get_next_endpoint (parent, dtb,NULL ,NULL , &ep_name, &compat); +while (ep_name) { + ret =xrt_md_get_prop (parent, dtb, ep_name, compat, +XRT_MD_PROP_IO_OFFSET , (const void **)&bar_range,NULL ); +if (ret) +continue ; +xrt_md_get_prop (parent, dtb, ep_name, compat, +XRT_MD_PROP_BAR_IDX , (const void **)&bar_idx,NULL ); + bar = bar_idx ?be32_to_cpu (*bar_idx) :0 ; +xleaf_get_root_res (to_xrt_dev (parent), bar, &pci_res); +if (!pci_res) { +dev_err (parent,"Invalid bar defined %d" , bar); + ret = -EINVAL ; + goto failed; + } + (*res)[count2].start = pci_res->start +be64_to_cpu (bar_range[0 ]); + (*res)[count2].end = pci_res->start +be64_to_cpu (bar_range[0 ]) + +be64_to_cpu (bar_range[1 ]) -1 ; + (*res)[count2].flags =IORESOURCE_MEM ; +/* check if there is conflicted resource */ + ret =request_resource (pci_res, *res + count2); +if (ret) { +dev_err (parent,"Conflict resource %pR\n" , *res + count2); + goto failed; + } +release_resource (*res + count2); + + (*res)[count2].parent = pci_res; + +xrt_md_find_endpoint (parent, pdata->xsp_dtb, ep_name, + compat, &(*res)[count2].name); + + count2++; +xrt_md_get_next_endpoint (parent, dtb, ep_name, compat, &ep_name, &compat); + } + +WARN_ON (count1 != count2); + *res_num = count2; + +return 0 ; + +failed: +vfree (*res); + *res_num =0 ; + *res =NULL ; +return ret; +} + +static inline enum xrt_dev_file_mode +xleaf_devnode_mode (struct xrt_device *xdev) +{ +return DEV_FILE_OPS (xdev)->xsf_mode; +} + +static bool xrt_subdev_cdev_auto_creation (struct xrt_device *xdev) +{ +enum xrt_dev_file_mode mode =xleaf_devnode_mode (xdev); + +if (!xleaf_devnode_enabled (xdev)) +return false; + +return (mode ==XRT_DEV_FILE_DEFAULT || mode ==XRT_DEV_FILE_MULTI_INST ); +} + +static struct xrt_subdev * +xrt_subdev_create (struct device *parent,enum xrt_subdev_id id, +xrt_subdev_root_cb_t pcb,void *pcb_arg,char *dtb) +{ +struct xrt_subdev_platdata *pdata =NULL ; +struct xrt_subdev *sdev =NULL ; +struct xrt_device *xdev =NULL ; +struct resource *res =NULL ; +unsigned long dtb_len =0 ; +bool dtb_alloced = false; +int res_num =0 ; +size_t pdata_sz; +int ret; + + sdev =xrt_subdev_alloc (); +if (!sdev) { +dev_err (parent,"failed to alloc subdev for ID %d" , id); +return NULL ; + } + sdev->xs_id = id; + +if (!dtb) { + ret =xrt_md_create (parent, &dtb); +if (ret) { +dev_err (parent,"can't create empty dtb: %d" , ret); + goto fail; + } + dtb_alloced = true; + } +xrt_md_pack (parent, dtb); + dtb_len =xrt_md_size (parent, dtb); +if (dtb_len ==XRT_MD_INVALID_LENGTH ) { +dev_err (parent,"invalid metadata len %ld" , dtb_len); + goto fail1; + } + pdata_sz =sizeof (struct xrt_subdev_platdata ) + dtb_len; + +/* Prepare platform data passed to subdev. */ + pdata =vzalloc (pdata_sz); +if (!pdata) + goto fail1; + + pdata->xsp_root_cb = pcb; + pdata->xsp_root_cb_arg = pcb_arg; +memcpy (pdata->xsp_dtb, dtb, dtb_len); +if (id ==XRT_SUBDEV_GRP ) { +/* Group can only be created by root driver. */ + pdata->xsp_root_name =dev_name (parent); + }else { +struct xrt_device *grp =to_xrt_dev (parent); + +/* Leaf can only be created by group driver. */ +WARN_ON (to_xrt_drv (parent->driver)->subdev_id !=XRT_SUBDEV_GRP ); + pdata->xsp_root_name =DEV_PDATA (grp)->xsp_root_name; + } + +/* Create subdev. */ +if (id !=XRT_SUBDEV_GRP ) { +int rc =xrt_subdev_getres (parent, id, dtb, &res, &res_num); + +if (rc) { +dev_err (parent,"failed to get resource for %s: %d" , +xrt_drv_name (id), rc); + goto fail2; + } + } + xdev =xrt_device_register (parent, id, res, res_num, pdata, pdata_sz); +vfree (res); +if (!xdev) { +dev_err (parent,"failed to create subdev for %s" ,xrt_drv_name (id)); + goto fail2; + } + sdev->xs_xdev = xdev; + +if (device_attach (DEV (xdev)) !=1 ) { +xrt_err (xdev,"failed to attach" ); + goto fail3; + } + +if (sysfs_create_group (&DEV (xdev)->kobj, &xrt_subdev_attrgroup)) +xrt_err (xdev,"failed to create sysfs group" ); + +/* +* Create sysfs sym link under root for leaves +* under random groups for easy access to them. +*/ +if (id !=XRT_SUBDEV_GRP ) { +if (sysfs_create_link (&find_root (xdev)->kobj, + &DEV (xdev)->kobj,dev_name (DEV (xdev)))) { +xrt_err (xdev,"failed to create sysfs link" ); + } + } + +/* All done, ready to handle req thru cdev. */ +if (xrt_subdev_cdev_auto_creation (xdev)) +xleaf_devnode_create (xdev,DEV_FILE_OPS (xdev)->xsf_dev_name,NULL ); + +vfree (pdata); +return sdev; + +fail3: +xrt_device_unregister (sdev->xs_xdev); +fail2: +vfree (pdata); +fail1: +if (dtb_alloced) +vfree (dtb); +fail: +kfree (sdev); +return NULL ; +} + +static void xrt_subdev_destroy (struct xrt_subdev *sdev) +{ +struct xrt_device *xdev = sdev->xs_xdev; +struct device *dev =DEV (xdev); + +/* Take down the device node */ +if (xrt_subdev_cdev_auto_creation (xdev)) +xleaf_devnode_destroy (xdev); +if (sdev->xs_id !=XRT_SUBDEV_GRP ) +sysfs_remove_link (&find_root (xdev)->kobj,dev_name (dev)); +sysfs_remove_group (&dev->kobj, &xrt_subdev_attrgroup); +xrt_device_unregister (xdev); +kfree (sdev); +} + +struct xrt_device * +xleaf_get_leaf (struct xrt_device *xdev,xrt_subdev_match_t match_cb,void *match_arg) +{ +int rc; +struct xrt_root_get_leaf get_leaf = { + xdev, match_cb, match_arg, }; + + rc =xrt_subdev_root_request (xdev,XRT_ROOT_GET_LEAF , &get_leaf); +if (rc) +return NULL ; +return get_leaf.xpigl_tgt_xdev; +} +EXPORT_SYMBOL_GPL (xleaf_get_leaf); + +bool xleaf_has_endpoint (struct xrt_device *xdev,const char *endpoint_name) +{ +struct resource *res; +int i =0 ; + +do { + res =xrt_get_resource (xdev,IORESOURCE_MEM , i); +if (res && !strncmp (res->name, endpoint_name,strlen (res->name) +1 )) +return true; + ++i; + }while (res); + +return false; +} +EXPORT_SYMBOL_GPL (xleaf_has_endpoint); + +int xleaf_put_leaf (struct xrt_device *xdev,struct xrt_device *leaf) +{ +struct xrt_root_put_leaf put_leaf = { xdev, leaf }; + +return xrt_subdev_root_request (xdev,XRT_ROOT_PUT_LEAF , &put_leaf); +} +EXPORT_SYMBOL_GPL (xleaf_put_leaf); + +int xleaf_create_group (struct xrt_device *xdev,char *dtb) +{ +return xrt_subdev_root_request (xdev,XRT_ROOT_CREATE_GROUP , dtb); +} +EXPORT_SYMBOL_GPL (xleaf_create_group); + +int xleaf_destroy_group (struct xrt_device *xdev,int instance) +{ +return xrt_subdev_root_request (xdev,XRT_ROOT_REMOVE_GROUP , (void *)(uintptr_t )instance); +} +EXPORT_SYMBOL_GPL (xleaf_destroy_group); + +int xleaf_wait_for_group_bringup (struct xrt_device *xdev) +{ +return xrt_subdev_root_request (xdev,XRT_ROOT_WAIT_GROUP_BRINGUP ,NULL ); +} +EXPORT_SYMBOL_GPL (xleaf_wait_for_group_bringup); + +static ssize_t +xrt_subdev_get_holders (struct xrt_subdev *sdev,char *buf,size_t len) +{ +const struct list_head *ptr; +struct xrt_subdev_holder *h; +ssize_t n =0 ; + +list_for_each (ptr, &sdev->xs_holder_list) { + h = list_entry(ptr, struct xrt_subdev_holder, xsh_holder_list); + n +=snprintf (buf + n, len - n,"%s:%d " , +dev_name (h->xsh_holder),kref_read (&h->xsh_kref)); +/* Truncation is fine here. Buffer content is only for debugging. */ +if (n >= (len -1 )) +break ; + } +return n; +} + +void xrt_subdev_pool_init (struct device *dev,struct xrt_subdev_pool *spool) +{ +INIT_LIST_HEAD (&spool->xsp_dev_list); + spool->xsp_owner = dev; +mutex_init (&spool->xsp_lock); + spool->xsp_closing = false; +} + +static void xrt_subdev_free_holder (struct xrt_subdev_holder *holder) +{ +list_del (&holder->xsh_holder_list); +vfree (holder); +} + +static void xrt_subdev_pool_wait_for_holders (struct xrt_subdev_pool *spool,struct xrt_subdev *sdev) +{ +const struct list_head *ptr, *next; +char holders[128 ]; +struct xrt_subdev_holder *holder; +struct mutex *lk = &spool->xsp_lock; + +while (!list_empty (&sdev->xs_holder_list)) { +int rc; + +/* It's most likely a bug if we ever enters this loop. */ +xrt_subdev_get_holders (sdev, holders,sizeof (holders)); +xrt_err (sdev->xs_xdev,"awaits holders: %s" , holders); +mutex_unlock (lk); + rc =wait_for_completion_killable (&sdev->xs_holder_comp); +mutex_lock (lk); +if (rc == -ERESTARTSYS ) { +xrt_err (sdev->xs_xdev,"give up on waiting for holders, clean up now" ); +list_for_each_safe (ptr, next, &sdev->xs_holder_list) { + holder = list_entry(ptr, struct xrt_subdev_holder, xsh_holder_list); +xrt_subdev_free_holder (holder); + } + } + } +} + +void xrt_subdev_pool_fini (struct xrt_subdev_pool *spool) +{ +struct list_head *dl = &spool->xsp_dev_list; +struct mutex *lk = &spool->xsp_lock; + +mutex_lock (lk); +if (spool->xsp_closing) { +mutex_unlock (lk); +return ; + } + spool->xsp_closing = true; +mutex_unlock (lk); + +/* Remove subdev in the reverse order of added. */ +while (!list_empty (dl)) { + struct xrt_subdev *sdev = list_first_entry(dl, struct xrt_subdev, xs_dev_list); + +xrt_subdev_pool_wait_for_holders (spool, sdev); +list_del (&sdev->xs_dev_list); +xrt_subdev_destroy (sdev); + } +} + +static struct xrt_subdev_holder *xrt_subdev_find_holder (struct xrt_subdev *sdev, +struct device *holder_dev) +{ +struct list_head *hl = &sdev->xs_holder_list; +struct xrt_subdev_holder *holder; +const struct list_head *ptr; + +list_for_each (ptr, hl) { + holder = list_entry(ptr, struct xrt_subdev_holder, xsh_holder_list); +if (holder->xsh_holder == holder_dev) +return holder; + } +return NULL ; +} + +static int xrt_subdev_hold (struct xrt_subdev *sdev,struct device *holder_dev) +{ +struct xrt_subdev_holder *holder =xrt_subdev_find_holder (sdev, holder_dev); +struct list_head *hl = &sdev->xs_holder_list; + +if (!holder) { + holder =vzalloc (sizeof (*holder)); +if (!holder) +return -ENOMEM ; + holder->xsh_holder = holder_dev; +kref_init (&holder->xsh_kref); +list_add_tail (&holder->xsh_holder_list, hl); + }else { +kref_get (&holder->xsh_kref); + } + +return 0 ; +} + +static void xrt_subdev_free_holder_kref (struct kref *kref) +{ + struct xrt_subdev_holder *holder = container_of(kref, struct xrt_subdev_holder, xsh_kref); + +xrt_subdev_free_holder (holder); +} + +static int +xrt_subdev_release (struct xrt_subdev *sdev,struct device *holder_dev) +{ +struct xrt_subdev_holder *holder =xrt_subdev_find_holder (sdev, holder_dev); +struct list_head *hl = &sdev->xs_holder_list; + +if (!holder) { +dev_err (holder_dev,"can't release, %s did not hold %s" , +dev_name (holder_dev),dev_name (DEV (sdev->xs_xdev))); +return -EINVAL ; + } +kref_put (&holder->xsh_kref, xrt_subdev_free_holder_kref); + +/* kref_put above may remove holder from list. */ +if (list_empty (hl)) +complete (&sdev->xs_holder_comp); +return 0 ; +} + +int xrt_subdev_pool_add (struct xrt_subdev_pool *spool,enum xrt_subdev_id id, +xrt_subdev_root_cb_t pcb,void *pcb_arg,char *dtb) +{ +struct mutex *lk = &spool->xsp_lock; +struct list_head *dl = &spool->xsp_dev_list; +struct xrt_subdev *sdev; +int ret =0 ; + + sdev =xrt_subdev_create (spool->xsp_owner, id, pcb, pcb_arg, dtb); +if (sdev) { +mutex_lock (lk); +if (spool->xsp_closing) { +/* No new subdev when pool is going away. */ +xrt_err (sdev->xs_xdev,"pool is closing" ); + ret = -ENODEV ; + }else { +list_add (&sdev->xs_dev_list, dl); + } +mutex_unlock (lk); +if (ret) +xrt_subdev_destroy (sdev); + }else { + ret = -EINVAL ; + } + + ret = ret ? ret : sdev->xs_xdev->instance; +return ret; +} + +int xrt_subdev_pool_del (struct xrt_subdev_pool *spool,enum xrt_subdev_id id,int instance) +{ +const struct list_head *ptr; +struct mutex *lk = &spool->xsp_lock; +struct list_head *dl = &spool->xsp_dev_list; +struct xrt_subdev *sdev; +int ret = -ENOENT ; + +mutex_lock (lk); +if (spool->xsp_closing) { +/* Pool is going away, all subdevs will be gone. */ +mutex_unlock (lk); +return 0 ; + } +list_for_each (ptr, dl) { + sdev = list_entry(ptr, struct xrt_subdev, xs_dev_list); +if (sdev->xs_id != id || sdev->xs_xdev->instance != instance) +continue ; +xrt_subdev_pool_wait_for_holders (spool, sdev); +list_del (&sdev->xs_dev_list); + ret =0 ; +break ; + } +mutex_unlock (lk); +if (ret) +return ret; + +xrt_subdev_destroy (sdev); +return 0 ; +} + +static int xrt_subdev_pool_get_impl (struct xrt_subdev_pool *spool,xrt_subdev_match_t match, +void *arg,struct device *holder_dev,struct xrt_subdev **sdevp) +{ +struct xrt_device *xdev = (struct xrt_device *)arg; +struct list_head *dl = &spool->xsp_dev_list; +struct mutex *lk = &spool->xsp_lock; +struct xrt_subdev *sdev =NULL ; +const struct list_head *ptr; +struct xrt_subdev *d =NULL ; +int ret = -ENOENT ; + +mutex_lock (lk); + +if (!xdev) { +if (match ==XRT_SUBDEV_MATCH_PREV ) { + sdev =list_empty (dl) ?NULL : + list_last_entry(dl, struct xrt_subdev, xs_dev_list); + }else if (match ==XRT_SUBDEV_MATCH_NEXT ) { + sdev = list_first_entry_or_null(dl, struct xrt_subdev, xs_dev_list); + } + } + +list_for_each (ptr, dl) { + d = list_entry(ptr, struct xrt_subdev, xs_dev_list); +if (match ==XRT_SUBDEV_MATCH_PREV || match ==XRT_SUBDEV_MATCH_NEXT ) { +if (d->xs_xdev != xdev) +continue ; + }else { +if (!match (d->xs_id, d->xs_xdev, arg)) +continue ; + } + +if (match ==XRT_SUBDEV_MATCH_PREV ) + sdev = !list_is_first (ptr, dl) ?list_prev_entry (d, xs_dev_list) :NULL ; +else if (match ==XRT_SUBDEV_MATCH_NEXT ) + sdev = !list_is_last (ptr, dl) ?list_next_entry (d, xs_dev_list) :NULL ; +else + sdev = d; + } + +if (sdev) + ret =xrt_subdev_hold (sdev, holder_dev); + +mutex_unlock (lk); + +if (!ret) + *sdevp = sdev; +return ret; +} + +int xrt_subdev_pool_get (struct xrt_subdev_pool *spool,xrt_subdev_match_t match,void *arg, +struct device *holder_dev,struct xrt_device **xdevp) +{ +int rc; +struct xrt_subdev *sdev; + + rc =xrt_subdev_pool_get_impl (spool, match, arg, holder_dev, &sdev); +if (rc) { +if (rc != -ENOENT ) +dev_err (holder_dev,"failed to hold device: %d" , rc); +return rc; + } + +if (!IS_ROOT_DEV (holder_dev)) { +xrt_dbg (to_xrt_dev (holder_dev),"%s <<==== %s" , +dev_name (holder_dev),dev_name (DEV (sdev->xs_xdev))); + } + + *xdevp = sdev->xs_xdev; +return 0 ; +} + +static int xrt_subdev_pool_put_impl (struct xrt_subdev_pool *spool,struct xrt_device *xdev, +struct device *holder_dev) +{ +const struct list_head *ptr; +struct mutex *lk = &spool->xsp_lock; +struct list_head *dl = &spool->xsp_dev_list; +struct xrt_subdev *sdev; +int ret = -ENOENT ; + +mutex_lock (lk); +list_for_each (ptr, dl) { + sdev = list_entry(ptr, struct xrt_subdev, xs_dev_list); +if (sdev->xs_xdev != xdev) +continue ; + ret =xrt_subdev_release (sdev, holder_dev); +break ; + } +mutex_unlock (lk); + +return ret; +} + +int xrt_subdev_pool_put (struct xrt_subdev_pool *spool,struct xrt_device *xdev, +struct device *holder_dev) +{ +int ret =xrt_subdev_pool_put_impl (spool, xdev, holder_dev); + +if (ret) +return ret; + +if (!IS_ROOT_DEV (holder_dev)) { +xrt_dbg (to_xrt_dev (holder_dev),"%s <<==X== %s" , +dev_name (holder_dev),dev_name (DEV (xdev))); + } +return 0 ; +} + +void xrt_subdev_pool_trigger_event (struct xrt_subdev_pool *spool,enum xrt_events e) +{ +struct xrt_device *tgt =NULL ; +struct xrt_subdev *sdev =NULL ; +struct xrt_event evt; + +while (!xrt_subdev_pool_get_impl (spool,XRT_SUBDEV_MATCH_NEXT , + tgt, spool->xsp_owner, &sdev)) { + tgt = sdev->xs_xdev; + evt.xe_evt = e; + evt.xe_subdev.xevt_subdev_id = sdev->xs_id; + evt.xe_subdev.xevt_subdev_instance = tgt->instance; +xrt_subdev_root_request (tgt,XRT_ROOT_EVENT_SYNC , &evt); +xrt_subdev_pool_put_impl (spool, tgt, spool->xsp_owner); + } +} + +void xrt_subdev_pool_handle_event (struct xrt_subdev_pool *spool,struct xrt_event *evt) +{ +struct xrt_device *tgt =NULL ; +struct xrt_subdev *sdev =NULL ; + +while (!xrt_subdev_pool_get_impl (spool,XRT_SUBDEV_MATCH_NEXT , + tgt, spool->xsp_owner, &sdev)) { + tgt = sdev->xs_xdev; +xleaf_call (tgt,XRT_XLEAF_EVENT , evt); +xrt_subdev_pool_put_impl (spool, tgt, spool->xsp_owner); + } +} + +ssize_t xrt_subdev_pool_get_holders (struct xrt_subdev_pool *spool, +struct xrt_device *xdev,char *buf,size_t len) +{ +const struct list_head *ptr; +struct mutex *lk = &spool->xsp_lock; +struct list_head *dl = &spool->xsp_dev_list; +struct xrt_subdev *sdev; +ssize_t ret =0 ; + +mutex_lock (lk); +list_for_each (ptr, dl) { + sdev = list_entry(ptr, struct xrt_subdev, xs_dev_list); +if (sdev->xs_xdev != xdev) +continue ; + ret =xrt_subdev_get_holders (sdev, buf, len); +break ; + } +mutex_unlock (lk); + +return ret; +} +EXPORT_SYMBOL_GPL (xrt_subdev_pool_get_holders); + +int xleaf_broadcast_event (struct xrt_device *xdev,enum xrt_events evt,bool async) +{ +struct xrt_event e = { evt, }; +enum xrt_root_cmd cmd = async ?XRT_ROOT_EVENT_ASYNC :XRT_ROOT_EVENT_SYNC ; + +WARN_ON (evt ==XRT_EVENT_POST_CREATION || evt ==XRT_EVENT_PRE_REMOVAL ); +return xrt_subdev_root_request (xdev, cmd, &e); +} +EXPORT_SYMBOL_GPL (xleaf_broadcast_event); + +void xleaf_hot_reset (struct xrt_device *xdev) +{ +xrt_subdev_root_request (xdev,XRT_ROOT_HOT_RESET ,NULL ); +} +EXPORT_SYMBOL_GPL (xleaf_hot_reset); + +void xleaf_get_root_res (struct xrt_device *xdev,u32 region_id,struct resource **res) +{ +struct xrt_root_get_res arg = {0 }; + + arg.xpigr_region_id = region_id; +xrt_subdev_root_request (xdev,XRT_ROOT_GET_RESOURCE , &arg); + *res = arg.xpigr_res; +} + +void xleaf_get_root_id (struct xrt_device *xdev,unsigned short *vendor,unsigned short *device, +unsigned short *subvendor,unsigned short *subdevice) +{ +struct xrt_root_get_id id = {0 }; + +WARN_ON (!vendor && !device && !subvendor && !subdevice); + +xrt_subdev_root_request (xdev,XRT_ROOT_GET_ID , (void *)&id); +if (vendor) + *vendor = id.xpigi_vendor_id; +if (device) + *device = id.xpigi_device_id; +if (subvendor) + *subvendor = id.xpigi_sub_vendor_id; +if (subdevice) + *subdevice = id.xpigi_sub_device_id; +} + +struct device *xleaf_register_hwmon (struct xrt_device *xdev,const char *name,void *drvdata, +const struct attribute_group **grps) +{ +struct xrt_root_hwmon hm = { true, name, drvdata, grps, }; + +xrt_subdev_root_request (xdev,XRT_ROOT_HWMON , (void *)&hm); +return hm.xpih_hwmon_dev; +} + +void xleaf_unregister_hwmon (struct xrt_device *xdev,struct device *hwmon) +{ +struct xrt_root_hwmon hm = { false, }; + + hm.xpih_hwmon_dev = hwmon; +xrt_subdev_root_request (xdev,XRT_ROOT_HWMON , (void *)&hm); +}
Signature · 1 line
2.27.0
PATCH v8 9/14 fpga: xrt: management physical function driver (root)
In reply to Lizhi HouThe PCIE device driver which attaches to management function on Alveo devices. It instantiates one or more group drivers which, in turn, instantiate xrt drivers. The instantiation of group and xrt drivers is completely dtb driven.
--- drivers/fpga/xrt/mgmt/root.c | 420 +++++++++++++++++++++++++++++++++++ 1 file changed, 420 insertions(+) create mode 100644 drivers/fpga/xrt/mgmt/root.c
drivers/fpga/xrt/mgmt/root.c new file +420 -0
@@ -0,0 +1,420 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Xilinx Alveo Management Function Driver +* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#include <linux/module.h> +#include <linux/pci.h> +#include <linux/aer.h> +#include <linux/vmalloc.h> +#include <linux/delay.h> + +#include "xroot.h" +#include "xmgmt.h" +#include "metadata.h" + +#define XMGMT_MODULE_NAME "xrt-mgmt" +#define XMGMT_DRIVER_VERSION "4.0.0" + +#define XMGMT_PDEV (xm) ((xm)->pdev) +#define XMGMT_DEV (xm) (&(XMGMT_PDEV(xm)->dev)) +#define xmgmt_err(xm, fmt, args...) \ + dev_err(XMGMT_DEV(xm), "%s: " fmt, __func__, ##args) +#define xmgmt_warn(xm, fmt, args...) \ + dev_warn(XMGMT_DEV(xm), "%s: " fmt, __func__, ##args) +#define xmgmt_info(xm, fmt, args...) \ + dev_info(XMGMT_DEV(xm), "%s: " fmt, __func__, ##args) +#define xmgmt_dbg(xm, fmt, args...) \ + dev_dbg(XMGMT_DEV(xm), "%s: " fmt, __func__, ##args) +#define XMGMT_DEV_ID (_pcidev) \ + ({ typeof(_pcidev) (pcidev) = (_pcidev); \ + ((pci_domain_nr((pcidev)->bus) << 16) | \ + PCI_DEVID((pcidev)->bus->number, 0)); }) +#define XRT_VSEC_ID 0x20 +#define XRT_MAX_READRQ 512 + +static struct class *xmgmt_class; + +/* PCI Device IDs */ +/* +* Golden image is preloaded on the device when it is shipped to customer. +* Then, customer can load other shells (from Xilinx or some other vendor). +* If something goes wrong with the shell, customer can always go back to +* golden and start over again. +*/ +#define PCI_DEVICE_ID_U50_GOLDEN 0xD020 +#define PCI_DEVICE_ID_U50 0x5020 +static const struct pci_device_id xmgmt_pci_ids[] = { + {PCI_DEVICE (PCI_VENDOR_ID_XILINX ,PCI_DEVICE_ID_U50_GOLDEN ), },/* Alveo U50 (golden) */ + {PCI_DEVICE (PCI_VENDOR_ID_XILINX ,PCI_DEVICE_ID_U50 ), },/* Alveo U50 */ + {0 , } +}; + +struct xmgmt { +struct pci_dev *pdev; +void *root; + +bool ready; +}; + +static int xmgmt_config_pci (struct xmgmt *xm) +{ +struct pci_dev *pdev =XMGMT_PDEV (xm); +int rc; + + rc =pcim_enable_device (pdev); +if (rc <0 ) { +xmgmt_err (xm,"failed to enable device: %d" , rc); +return rc; + } + + rc =pci_enable_pcie_error_reporting (pdev); +if (rc) +xmgmt_warn (xm,"failed to enable AER: %d" , rc); + +pci_set_master (pdev); + + rc =pcie_get_readrq (pdev); +if (rc >XRT_MAX_READRQ ) +pcie_set_readrq (pdev,XRT_MAX_READRQ ); +return 0 ; +} + +static int xmgmt_match_slot_and_save (struct device *dev,void *data) +{ +struct xmgmt *xm = data; +struct pci_dev *pdev =to_pci_dev (dev); + +if (XMGMT_DEV_ID (pdev) ==XMGMT_DEV_ID (xm->pdev)) { +pci_cfg_access_lock (pdev); +pci_save_state (pdev); + } + +return 0 ; +} + +static void xmgmt_pci_save_config_all (struct xmgmt *xm) +{ +bus_for_each_dev (&pci_bus_type,NULL , xm, xmgmt_match_slot_and_save); +} + +static int xmgmt_match_slot_and_restore (struct device *dev,void *data) +{ +struct xmgmt *xm = data; +struct pci_dev *pdev =to_pci_dev (dev); + +if (XMGMT_DEV_ID (pdev) ==XMGMT_DEV_ID (xm->pdev)) { +pci_restore_state (pdev); +pci_cfg_access_unlock (pdev); + } + +return 0 ; +} + +static void xmgmt_pci_restore_config_all (struct xmgmt *xm) +{ +bus_for_each_dev (&pci_bus_type,NULL , xm, xmgmt_match_slot_and_restore); +} + +static void xmgmt_root_hot_reset (struct device *dev) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct pci_bus *bus; +u16 pci_cmd, devctl; +struct xmgmt *xm; +u8 pci_bctl; +int i, ret; + + xm =pci_get_drvdata (pdev); +xmgmt_info (xm,"hot reset start" ); +xmgmt_pci_save_config_all (xm); +pci_disable_device (pdev); + bus = pdev->bus; + +/* +* When flipping the SBR bit, device can fall off the bus. This is +* usually no problem at all so long as drivers are working properly +* after SBR. However, some systems complain bitterly when the device +* falls off the bus. +* The quick solution is to temporarily disable the SERR reporting of +* switch port during SBR. +*/ + +pci_read_config_word (bus->self,PCI_COMMAND , &pci_cmd); +pci_write_config_word (bus->self,PCI_COMMAND , (pci_cmd & ~PCI_COMMAND_SERR )); +pcie_capability_read_word (bus->self,PCI_EXP_DEVCTL , &devctl); +pcie_capability_write_word (bus->self,PCI_EXP_DEVCTL , (devctl & ~PCI_EXP_DEVCTL_FERE )); +pci_read_config_byte (bus->self,PCI_BRIDGE_CONTROL , &pci_bctl); +pci_write_config_byte (bus->self,PCI_BRIDGE_CONTROL , pci_bctl |PCI_BRIDGE_CTL_BUS_RESET ); +msleep (100 ); +pci_write_config_byte (bus->self,PCI_BRIDGE_CONTROL , pci_bctl); +ssleep (1 ); + +pcie_capability_write_word (bus->self,PCI_EXP_DEVCTL , devctl); +pci_write_config_word (bus->self,PCI_COMMAND , pci_cmd); + + ret =pci_enable_device (pdev); +if (ret) +xmgmt_err (xm,"failed to enable device, ret %d" , ret); + +for (i =0 ; i <300 ; i++) { +pci_read_config_word (pdev,PCI_COMMAND , &pci_cmd); +if (pci_cmd !=0xffff ) +break ; +msleep (20 ); + } +if (i ==300 ) +xmgmt_err (xm,"timed out waiting for device to be online after reset" ); + +xmgmt_info (xm,"waiting for %d ms" , i *20 ); +xmgmt_pci_restore_config_all (xm); +xmgmt_config_pci (xm); +} + +static int xmgmt_add_vsec_node (struct xmgmt *xm,char *dtb) +{ +struct pci_dev *pdev =XMGMT_PDEV (xm); +struct xrt_md_endpoint ep = {0 }; +struct device *dev =DEV (pdev); +u32 off_low, off_high, header; +int cap =0 , ret =0 ; +__be32 vsec_bar; +__be64 vsec_off; + +while ((cap =pci_find_next_ext_capability (pdev, cap,PCI_EXT_CAP_ID_VNDR ))) { +pci_read_config_dword (pdev, cap +PCI_VNDR_HEADER , &header); +if (PCI_VNDR_HEADER_ID (header) ==XRT_VSEC_ID ) +break ; + } +if (!cap) { +xmgmt_info (xm,"No Vendor Specific Capability." ); +return -ENOENT ; + } + +if (pci_read_config_dword (pdev, cap +8 , &off_low) || +pci_read_config_dword (pdev, cap +12 , &off_high)) { +xmgmt_err (xm,"pci_read vendor specific failed." ); +return -EINVAL ; + } + + ep.ep_name =XRT_MD_NODE_VSEC ; + ret =xrt_md_add_endpoint (dev, dtb, &ep); +if (ret) { +xmgmt_err (xm,"add vsec metadata failed, ret %d" , ret); + goto failed; + } + + vsec_bar =cpu_to_be32 (off_low &0xf ); + ret =xrt_md_set_prop (dev, dtb,XRT_MD_NODE_VSEC ,NULL , +XRT_MD_PROP_BAR_IDX , &vsec_bar,sizeof (vsec_bar)); +if (ret) { +xmgmt_err (xm,"add vsec bar idx failed, ret %d" , ret); + goto failed; + } + + vsec_off =cpu_to_be64 (((u64 )off_high <<32 ) | (off_low & ~0xfU )); + ret =xrt_md_set_prop (dev, dtb,XRT_MD_NODE_VSEC ,NULL , +XRT_MD_PROP_OFFSET , &vsec_off,sizeof (vsec_off)); +if (ret) { +xmgmt_err (xm,"add vsec offset failed, ret %d" , ret); + goto failed; + } + +failed: +return ret; +} + +static int xmgmt_create_root_metadata (struct xmgmt *xm,char **root_dtb) +{ +char *dtb =NULL ; +int ret; + + ret =xrt_md_create (XMGMT_DEV (xm), &dtb); +if (ret) { +xmgmt_err (xm,"create metadata failed, ret %d" , ret); + goto failed; + } + + ret =xmgmt_add_vsec_node (xm, dtb); +if (ret == -ENOENT ) { +/* +* We may be dealing with a MFG board. +* Try vsec-golden which will bring up all hard-coded leaves +* at hard-coded offsets. +*/ + ret =xroot_add_simple_node (xm->root, dtb,XRT_MD_NODE_VSEC_GOLDEN ); + }else if (ret ==0 ) { + ret =xroot_add_simple_node (xm->root, dtb,XRT_MD_NODE_MGMT_MAIN ); + } +if (ret) + goto failed; + + *root_dtb = dtb; +return 0 ; + +failed: +vfree (dtb); +return ret; +} + +static ssize_t ready_show (struct device *dev, +struct device_attribute *da, +char *buf) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct xmgmt *xm =pci_get_drvdata (pdev); + +return sprintf (buf,"%d\n" , xm->ready); +} +static DEVICE_ATTR_RO (ready); + +static struct attribute *xmgmt_root_attrs[] = { + &dev_attr_ready.attr, +NULL +}; + +static struct attribute_group xmgmt_root_attr_group = { + .attrs = xmgmt_root_attrs, +}; + +static void xmgmt_root_get_id (struct device *dev,struct xrt_root_get_id *rid) +{ +struct pci_dev *pdev =to_pci_dev (dev); + + rid->xpigi_vendor_id = pdev->vendor; + rid->xpigi_device_id = pdev->device; + rid->xpigi_sub_vendor_id = pdev->subsystem_vendor; + rid->xpigi_sub_device_id = pdev->subsystem_device; +} + +static int xmgmt_root_get_resource (struct device *dev,struct xrt_root_get_res *res) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct xmgmt *xm; + + xm =pci_get_drvdata (pdev); +if (res->xpigr_region_id >PCI_STD_RESOURCE_END ) { +xmgmt_err (xm,"Invalid bar idx %d" , res->xpigr_region_id); +return -EINVAL ; + } + + res->xpigr_res = &pdev->resource[res->xpigr_region_id]; +return 0 ; +} + +static struct xroot_physical_function_callback xmgmt_xroot_pf_cb = { + .xpc_get_id = xmgmt_root_get_id, + .xpc_get_resource = xmgmt_root_get_resource, + .xpc_hot_reset = xmgmt_root_hot_reset, +}; + +static int xmgmt_probe (struct pci_dev *pdev,const struct pci_device_id *id) +{ +int ret; +struct device *dev = &pdev->dev; +struct xmgmt *xm =devm_kzalloc (dev,sizeof (*xm),GFP_KERNEL ); +char *dtb =NULL ; + +if (!xm) +return -ENOMEM ; + xm->pdev = pdev; +pci_set_drvdata (pdev, xm); + + ret =xmgmt_config_pci (xm); +if (ret) + goto failed; + + ret =xroot_probe (&pdev->dev, &xmgmt_xroot_pf_cb, &xm->root); +if (ret) + goto failed; + + ret =xmgmt_create_root_metadata (xm, &dtb); +if (ret) + goto failed_metadata; + + ret =xroot_create_group (xm->root, dtb); +vfree (dtb); +if (ret) +xmgmt_err (xm,"failed to create root group: %d" , ret); + +if (!xroot_wait_for_bringup (xm->root)) +xmgmt_err (xm,"failed to bringup all groups" ); +else + xm->ready = true; + + ret =sysfs_create_group (&pdev->dev.kobj, &xmgmt_root_attr_group); +if (ret) { +/* Warning instead of failing the probe. */ +xmgmt_warn (xm,"create xmgmt root attrs failed: %d" , ret); + } + +xroot_broadcast (xm->root,XRT_EVENT_POST_CREATION ); +xmgmt_info (xm,"%s started successfully" ,XMGMT_MODULE_NAME ); +return 0 ; + +failed_metadata: +xroot_remove (xm->root); +failed: +pci_set_drvdata (pdev,NULL ); +return ret; +} + +static void xmgmt_remove (struct pci_dev *pdev) +{ +struct xmgmt *xm =pci_get_drvdata (pdev); + +xroot_broadcast (xm->root,XRT_EVENT_PRE_REMOVAL ); +sysfs_remove_group (&pdev->dev.kobj, &xmgmt_root_attr_group); +xroot_remove (xm->root); +pci_disable_pcie_error_reporting (xm->pdev); +xmgmt_info (xm,"%s cleaned up successfully" ,XMGMT_MODULE_NAME ); +} + +static struct pci_driver xmgmt_driver = { + .name =XMGMT_MODULE_NAME , + .id_table = xmgmt_pci_ids, + .probe = xmgmt_probe, + .remove = xmgmt_remove, +}; + +static int __init xmgmt_init (void ) +{ +int res =0 ; + + res =xmgmt_register_leaf (); +if (res) +return res; + + xmgmt_class =class_create (THIS_MODULE ,XMGMT_MODULE_NAME ); +if (IS_ERR (xmgmt_class)) +return PTR_ERR (xmgmt_class); + + res =pci_register_driver (&xmgmt_driver); +if (res) { +class_destroy (xmgmt_class); +return res; + } + +return 0 ; +} + +static __exit void xmgmt_exit(void) +{ +pci_unregister_driver (&xmgmt_driver); +class_destroy (xmgmt_class); +xmgmt_unregister_leaf (); +} + +module_init (xmgmt_init); +module_exit (xmgmt_exit); + +MODULE_DEVICE_TABLE (pci, xmgmt_pci_ids); +MODULE_VERSION (XMGMT_DRIVER_VERSION ); +MODULE_AUTHOR ("XRT Team <[email protected]>" ); +MODULE_DESCRIPTION ("Xilinx Alveo management function driver" ); +MODULE_LICENSE ("GPL v2" );
Signature · 1 line
2.27.0
PATCH v8 10/14 fpga: xrt: main driver for management function device
In reply to Lizhi Houxrt driver that handles IOCTLs, such as hot reset and xclbin download.
--- drivers/fpga/xrt/include/xmgmt-main.h | 34 ++ drivers/fpga/xrt/mgmt/xmgmt-main.c | 662 ++++++++++++++++++++++++++ drivers/fpga/xrt/mgmt/xmgmt.h | 33 ++ 3 files changed, 729 insertions(+) create mode 100644 drivers/fpga/xrt/include/xmgmt-main.h create mode 100644 drivers/fpga/xrt/mgmt/xmgmt-main.c create mode 100644 drivers/fpga/xrt/mgmt/xmgmt.h
drivers/fpga/xrt/include/xmgmt-main.h new file +34 -0
@@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XMGMT_MAIN_H_ +#define _XMGMT_MAIN_H_ + +#include <linux/fpga-xrt.h> +#include "xleaf.h" + +enum xrt_mgmt_main_leaf_cmd { +XRT_MGMT_MAIN_GET_AXLF_SECTION =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +XRT_MGMT_MAIN_GET_VBNV , +}; + +/* There are three kind of partitions. Each of them is programmed independently. */ +enum provider_kind { +XMGMT_BLP ,/* Base Logic Partition */ +XMGMT_PLP ,/* Provider Logic Partition */ +XMGMT_ULP ,/* User Logic Partition */ +}; + +struct xrt_mgmt_main_get_axlf_section { +enum provider_kind xmmigas_axlf_kind; +enum axlf_section_kind xmmigas_section_kind; +void *xmmigas_section; +u64 xmmigas_section_size; +}; + +#endif /* _XMGMT_MAIN_H_ */
drivers/fpga/xrt/mgmt/xmgmt-main.c new file +662 -0
662 lines not on this page · raw message
drivers/fpga/xrt/mgmt/xmgmt.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +* Cheng Zhen <[email protected]> +*/ + +#ifndef _XMGMT_H_ +#define _XMGMT_H_ + +#include "xmgmt-main.h" + +struct fpga_manager ; +int xmgmt_process_xclbin (struct xrt_device *xdev, +struct fpga_manager *fmgr, +const struct axlf *xclbin, +enum provider_kind kind); +void xmgmt_region_cleanup_all (struct xrt_device *xdev); + +int xmgmt_hot_reset (struct xrt_device *xdev); + +/* Getting dtb for specified group. Caller should vfree returned dtb. */ +char *xmgmt_get_dtb (struct xrt_device *xdev,enum provider_kind kind); +char *xmgmt_get_vbnv (struct xrt_device *xdev); +int xmgmt_get_provider_uuid (struct xrt_device *xdev, +enum provider_kind kind,uuid_t *uuid); + +int xmgmt_register_leaf (void ); +void xmgmt_unregister_leaf (void ); + +#endif /* _XMGMT_H_ */
Signature · 1 line
2.27.0
PATCH v8 11/14 fpga: xrt: fpga-mgr and region implementation for xclbin download
In reply to Lizhi Houfpga-mgr and region implementation for xclbin download which will be called from main xrt driver
--- drivers/fpga/xrt/mgmt/xmgmt-main-region.c | 483 ++++++++++++++++++++++ drivers/fpga/xrt/mgmt/xrt-mgr.c | 190 +++++++++ drivers/fpga/xrt/mgmt/xrt-mgr.h | 16 + 3 files changed, 689 insertions(+) create mode 100644 drivers/fpga/xrt/mgmt/xmgmt-main-region.c create mode 100644 drivers/fpga/xrt/mgmt/xrt-mgr.c create mode 100644 drivers/fpga/xrt/mgmt/xrt-mgr.h
drivers/fpga/xrt/mgmt/xmgmt-main-region.c new file +483 -0
@@ -0,0 +1,483 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* FPGA Region Support for Xilinx Alveo +* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: [email protected] +*/ + +#include <linux/uuid.h> +#include <linux/fpga/fpga-bridge.h> +#include <linux/fpga/fpga-region.h> +#include <linux/slab.h> +#include "metadata.h" +#include "xleaf.h" +#include "xleaf/axigate.h" +#include "xclbin-helper.h" +#include "xmgmt.h" + +struct xmgmt_bridge { +struct xrt_device *xdev; +const char *bridge_name; +}; + +struct xmgmt_region { +struct xrt_device *xdev; +struct fpga_region *region; +struct fpga_compat_id compat_id; +uuid_t interface_uuid; +struct fpga_bridge *bridge; +int group_instance; +uuid_t depend_uuid; +struct list_head list; +}; + +struct xmgmt_region_match_arg { +struct xrt_device *xdev; +uuid_t *uuids; +u32 uuid_num; +}; + +static int xmgmt_br_enable_set (struct fpga_bridge *bridge,bool enable) +{ +struct xmgmt_bridge *br_data = (struct xmgmt_bridge *)bridge->priv; +struct xrt_device *axigate_leaf; +int rc; + + axigate_leaf =xleaf_get_leaf_by_epname (br_data->xdev, br_data->bridge_name); +if (!axigate_leaf) { +xrt_err (br_data->xdev,"failed to get leaf %s" , + br_data->bridge_name); +return -ENOENT ; + } + +if (enable) + rc =xleaf_call (axigate_leaf,XRT_AXIGATE_OPEN ,NULL ); +else + rc =xleaf_call (axigate_leaf,XRT_AXIGATE_CLOSE ,NULL ); + +if (rc) { +xrt_err (br_data->xdev,"failed to %s gate %s, rc %d" , + (enable ?"free" :"freeze" ), br_data->bridge_name, + rc); + } + +xleaf_put_leaf (br_data->xdev, axigate_leaf); + +return rc; +} + +static const struct fpga_bridge_ops xmgmt_bridge_ops = { + .enable_set = xmgmt_br_enable_set +}; + +static void xmgmt_destroy_bridge (struct fpga_bridge *br) +{ +struct xmgmt_bridge *br_data = br->priv; + +if (!br_data) +return ; + +xrt_info (br_data->xdev,"destroy fpga bridge %s" , br_data->bridge_name); +fpga_bridge_unregister (br); + +devm_kfree (DEV (br_data->xdev), br_data); + +fpga_bridge_free (br); +} + +static struct fpga_bridge *xmgmt_create_bridge (struct xrt_device *xdev, +char *dtb) +{ +struct fpga_bridge *br =NULL ; +struct xmgmt_bridge *br_data; +const char *gate; +int rc; + + br_data =devm_kzalloc (DEV (xdev),sizeof (*br_data),GFP_KERNEL ); +if (!br_data) +return NULL ; + br_data->xdev = xdev; + + br_data->bridge_name =XRT_MD_NODE_GATE_ULP ; + rc =xrt_md_find_endpoint (&xdev->dev, dtb,XRT_MD_NODE_GATE_ULP , +NULL , &gate); +if (rc) { + br_data->bridge_name =XRT_MD_NODE_GATE_PLP ; + rc =xrt_md_find_endpoint (&xdev->dev, dtb,XRT_MD_NODE_GATE_PLP , +NULL , &gate); + } +if (rc) { +xrt_err (xdev,"failed to get axigate, rc %d" , rc); + goto failed; + } + + br =fpga_bridge_create (DEV (xdev), br_data->bridge_name, + &xmgmt_bridge_ops, br_data); +if (!br) { +xrt_err (xdev,"failed to create bridge" ); + goto failed; + } + + rc =fpga_bridge_register (br); +if (rc) { +xrt_err (xdev,"failed to register bridge, rc %d" , rc); + goto failed; + } + +xrt_info (xdev,"created fpga bridge %s" , br_data->bridge_name); + +return br; + +failed: +if (br) +fpga_bridge_free (br); +if (br_data) +devm_kfree (DEV (xdev), br_data); + +return NULL ; +} + +static void xmgmt_destroy_region (struct fpga_region *region) +{ +struct xmgmt_region *r_data = region->priv; + +xrt_info (r_data->xdev,"destroy fpga region %llx.%llx" , + region->compat_id->id_h, region->compat_id->id_l); + +fpga_region_unregister (region); + +if (r_data->group_instance >0 ) +xleaf_destroy_group (r_data->xdev, r_data->group_instance); + +if (r_data->bridge) +xmgmt_destroy_bridge (r_data->bridge); + +if (r_data->region->info) { +fpga_image_info_free (r_data->region->info); + r_data->region->info =NULL ; + } + +fpga_region_free (region); + +devm_kfree (DEV (r_data->xdev), r_data); +} + +static int xmgmt_region_match (struct device *dev,const void *data) +{ +const struct xmgmt_region_match_arg *arg = data; +const struct fpga_region *match_region; +uuid_t compat_uuid; +int i; + +if (dev->parent != &arg->xdev->dev) +return false; + + match_region =to_fpga_region (dev); +/* +* The device tree provides both parent and child uuids for an +* xclbin in one array. Here we try both uuids to see if it matches +* with target region's compat_id. Strictly speaking we should +* only match xclbin's parent uuid with target region's compat_id +* but given the uuids by design are unique comparing with both +* does not hurt. +*/ +import_uuid (&compat_uuid, (const char *)match_region->compat_id); +for (i =0 ; i < arg->uuid_num; i++) { +if (uuid_equal (&compat_uuid, &arg->uuids[i])) +return true; + } + +return false; +} + +static int xmgmt_region_match_base (struct device *dev,const void *data) +{ +const struct xmgmt_region_match_arg *arg = data; +const struct fpga_region *match_region; +const struct xmgmt_region *r_data; + +if (dev->parent != &arg->xdev->dev) +return false; + + match_region =to_fpga_region (dev); + r_data = match_region->priv; +if (uuid_is_null (&r_data->depend_uuid)) +return true; + +return false; +} + +static int xmgmt_region_match_by_uuid (struct device *dev,const void *data) +{ +const struct xmgmt_region_match_arg *arg = data; +const struct fpga_region *match_region; +const struct xmgmt_region *r_data; + +if (dev->parent != &arg->xdev->dev) +return false; + +if (arg->uuid_num !=1 ) +return false; + + match_region =to_fpga_region (dev); + r_data = match_region->priv; +if (uuid_equal (&r_data->depend_uuid, arg->uuids)) +return true; + +return false; +} + +static void xmgmt_region_cleanup (struct fpga_region *region) +{ +struct xmgmt_region *r_data = region->priv, *pdata, *temp; +struct xrt_device *xdev = r_data->xdev; +struct xmgmt_region_match_arg arg = {0 }; +struct fpga_region *match_region =NULL ; +struct device *start_dev =NULL ; +LIST_HEAD (free_list); +uuid_t compat_uuid; + +list_add_tail (&r_data->list, &free_list); + arg.xdev = xdev; + arg.uuid_num =1 ; + arg.uuids = &compat_uuid; + +/* find all regions depending on this region */ +list_for_each_entry_safe (pdata, temp, &free_list, list) { +import_uuid (arg.uuids, (const char *)pdata->region->compat_id); + start_dev =NULL ; +while ((match_region =fpga_region_class_find (start_dev, &arg, + xmgmt_region_match_by_uuid))) { + pdata = match_region->priv; +list_add_tail (&pdata->list, &free_list); + start_dev = &match_region->dev; +put_device (&match_region->dev); + } + } + +list_del (&r_data->list); + +list_for_each_entry_safe_reverse (pdata, temp, &free_list, list) +xmgmt_destroy_region (pdata->region); + +if (r_data->group_instance >0 ) { +xleaf_destroy_group (xdev, r_data->group_instance); + r_data->group_instance =-1 ; + } +if (r_data->region->info) { +fpga_image_info_free (r_data->region->info); + r_data->region->info =NULL ; + } +} + +void xmgmt_region_cleanup_all (struct xrt_device *xdev) +{ +struct xmgmt_region_match_arg arg = {0 }; +struct fpga_region *base_region; + + arg.xdev = xdev; + +while ((base_region =fpga_region_class_find (NULL , &arg, xmgmt_region_match_base))) { +put_device (&base_region->dev); + +xmgmt_region_cleanup (base_region); +xmgmt_destroy_region (base_region); + } +} + +/* +* Program a region with a xclbin image. Bring up the subdevs and the +* group object to contain the subdevs. +*/ +static int xmgmt_region_program (struct fpga_region *region,const void *xclbin,char *dtb) +{ +const struct axlf *xclbin_obj = xclbin; +struct fpga_image_info *info; +struct xrt_device *xdev; +struct xmgmt_region *r_data; +int rc; + + r_data = region->priv; + xdev = r_data->xdev; + + info =fpga_image_info_alloc (&xdev->dev); +if (!info) +return -ENOMEM ; + + info->buf = xclbin; + info->count = xclbin_obj->header.length; + info->flags |=FPGA_MGR_PARTIAL_RECONFIG ; + region->info = info; + rc =fpga_region_program_fpga (region); +if (rc) { +xrt_err (xdev,"programming xclbin failed, rc %d" , rc); +return rc; + } + +/* free bridges to allow reprogram */ +if (region->get_bridges) +fpga_bridges_put (®ion->bridge_list); + +/* +* Next bringup the subdevs for this region which will be managed by +* its own group object. +*/ + r_data->group_instance =xleaf_create_group (xdev, dtb); +if (r_data->group_instance <0 ) { +xrt_err (xdev,"failed to create group, rc %d" , + r_data->group_instance); + rc = r_data->group_instance; +return rc; + } + + rc =xleaf_wait_for_group_bringup (xdev); +if (rc) +xrt_err (xdev,"group bringup failed, rc %d" , rc); +return rc; +} + +static int xmgmt_get_bridges (struct fpga_region *region) +{ +struct xmgmt_region *r_data = region->priv; +struct device *dev = &r_data->xdev->dev; + +return fpga_bridge_get_to_list (dev, region->info, ®ion->bridge_list); +} + +/* +* Program/create FPGA regions based on input xclbin file. +* 1. Identify a matching existing region for this xclbin +* 2. Tear down any previous objects for the found region +* 3. Program this region with input xclbin +* 4. Iterate over this region's interface uuids to determine if it defines any +* child region. Create fpga_region for the child region. +*/ +int xmgmt_process_xclbin (struct xrt_device *xdev, +struct fpga_manager *fmgr, +const struct axlf *xclbin, +enum provider_kind kind) +{ +struct fpga_region *region, *compat_region =NULL ; +struct xmgmt_region_match_arg arg = {0 }; +struct xmgmt_region *r_data; +uuid_t compat_uuid; +char *dtb =NULL ; +int rc, i; + + rc =xrt_xclbin_get_metadata (DEV (xdev), xclbin, &dtb); +if (rc) { +xrt_err (xdev,"failed to get dtb: %d" , rc); + goto failed; + } + + rc =xrt_md_get_interface_uuids (DEV (xdev), dtb,0 ,NULL ); +if (rc <0 ) { +xrt_err (xdev,"failed to get intf uuid" ); + rc = -EINVAL ; + goto failed; + } + arg.uuid_num = rc; + arg.uuids =kcalloc (arg.uuid_num,sizeof (uuid_t),GFP_KERNEL ); +if (!arg.uuids) { + rc = -ENOMEM ; + goto failed; + } + arg.xdev = xdev; + + rc =xrt_md_get_interface_uuids (DEV (xdev), dtb, arg.uuid_num, arg.uuids); +if (rc != arg.uuid_num) { +xrt_err (xdev,"only get %d uuids, expect %d" , rc, arg.uuid_num); + rc = -EINVAL ; + goto failed; + } + +/* if this is not base firmware, search for a compatible region */ +if (kind !=XMGMT_BLP ) { + compat_region =fpga_region_class_find (NULL , &arg, xmgmt_region_match); +if (!compat_region) { +xrt_err (xdev,"failed to get compatible region" ); + rc = -ENOENT ; + goto failed; + } + +xmgmt_region_cleanup (compat_region); + + rc =xmgmt_region_program (compat_region, xclbin, dtb); +if (rc) { +xrt_err (xdev,"failed to program region" ); + goto failed; + } + } + +if (compat_region) +import_uuid (&compat_uuid, (const char *)compat_region->compat_id); + +/* create all the new regions contained in this xclbin */ +for (i =0 ; i < arg.uuid_num; i++) { +if (compat_region &&uuid_equal (&compat_uuid, &arg.uuids[i])) { +/* region for this interface already exists */ +continue ; + } + + region =fpga_region_create (DEV (xdev), fmgr, xmgmt_get_bridges); +if (!region) { +xrt_err (xdev,"failed to create fpga region" ); + rc = -EFAULT ; + goto failed; + } + r_data =devm_kzalloc (DEV (xdev),sizeof (*r_data),GFP_KERNEL ); +if (!r_data) { + rc = -ENOMEM ; +fpga_region_free (region); + goto failed; + } + r_data->xdev = xdev; + r_data->region = region; + r_data->group_instance =-1 ; +uuid_copy (&r_data->interface_uuid, &arg.uuids[i]); +if (compat_region) +import_uuid (&r_data->depend_uuid, (const char *)compat_region->compat_id); + r_data->bridge =xmgmt_create_bridge (xdev, dtb); +if (!r_data->bridge) { +xrt_err (xdev,"failed to create fpga bridge" ); + rc = -EFAULT ; +devm_kfree (DEV (xdev), r_data); +fpga_region_free (region); + goto failed; + } + + region->compat_id = &r_data->compat_id; +export_uuid ((char *)region->compat_id, &r_data->interface_uuid); + region->priv = r_data; + + rc =fpga_region_register (region); +if (rc) { +xrt_err (xdev,"failed to register fpga region" ); +xmgmt_destroy_bridge (r_data->bridge); +fpga_region_free (region); +devm_kfree (DEV (xdev), r_data); + goto failed; + } + +xrt_info (xdev,"created fpga region %llx.%llx" , + region->compat_id->id_h, region->compat_id->id_l); + } + +if (compat_region) +put_device (&compat_region->dev); +vfree (dtb); +kfree (arg.uuids); +return 0 ; + +failed: +if (compat_region) { +put_device (&compat_region->dev); +xmgmt_region_cleanup (compat_region); + } + +vfree (dtb); +kfree (arg.uuids); +return rc; +}
drivers/fpga/xrt/mgmt/xrt-mgr.c new file +190 -0
190 lines not on this page · raw message
drivers/fpga/xrt/mgmt/xrt-mgr.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: [email protected] +*/ + +#ifndef _XRT_MGR_H_ +#define _XRT_MGR_H_ + +#include <linux/fpga/fpga-mgr.h> + +struct fpga_manager *xmgmt_fmgr_probe (struct xrt_device *xdev); +int xmgmt_fmgr_remove (struct fpga_manager *fmgr); + +#endif /* _XRT_MGR_H_ */
Signature · 1 line
2.27.0
PATCH v8 12/14 fpga: xrt: ICAP driver
In reply to Lizhi HouICAP stands for Hardware Internal Configuration Access Port. ICAP is discovered by walking the firmware metadata. A xrt device node will be created for it. FPGA bitstream is written to hardware through ICAP.
--- drivers/fpga/xrt/include/xleaf/icap.h | 27 +++ drivers/fpga/xrt/lib/xleaf/icap.c | 328 ++++++++++++++++++++++++++ 2 files changed, 355 insertions(+) create mode 100644 drivers/fpga/xrt/include/xleaf/icap.h create mode 100644 drivers/fpga/xrt/lib/xleaf/icap.c
drivers/fpga/xrt/include/xleaf/icap.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_ICAP_H_ +#define _XRT_ICAP_H_ + +#include "xleaf.h" + +/* +* ICAP driver leaf calls. +*/ +enum xrt_icap_leaf_cmd { +XRT_ICAP_WRITE =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +XRT_ICAP_GET_IDCODE , +}; + +struct xrt_icap_wr { +void *xiiw_bit_data; +u32 xiiw_data_len; +}; + +#endif /* _XRT_ICAP_H_ */
drivers/fpga/xrt/lib/xleaf/icap.c new file +328 -0
328 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v8 13/14 fpga: xrt: partition isolation driver
In reply to Lizhi HouAdd partition isolation xrt driver. partition isolation is a hardware function discovered by walking firmware metadata. A xrt device node will be created for it. Partition isolation function isolate the different fpga regions
--- drivers/fpga/xrt/include/xleaf/axigate.h | 23 ++ drivers/fpga/xrt/lib/xleaf/axigate.c | 325 +++++++++++++++++++++++ 2 files changed, 348 insertions(+) create mode 100644 drivers/fpga/xrt/include/xleaf/axigate.h create mode 100644 drivers/fpga/xrt/lib/xleaf/axigate.c
drivers/fpga/xrt/include/xleaf/axigate.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2020-2021 Xilinx, Inc. +* +* Authors: +* Lizhi Hou <[email protected]> +*/ + +#ifndef _XRT_AXIGATE_H_ +#define _XRT_AXIGATE_H_ + +#include "xleaf.h" +#include "metadata.h" + +/* +* AXIGATE driver leaf calls. +*/ +enum xrt_axigate_leaf_cmd { +XRT_AXIGATE_CLOSE =XRT_XLEAF_CUSTOM_BASE ,/* See comments in xleaf.h */ +XRT_AXIGATE_OPEN , +}; + +#endif /* _XRT_AXIGATE_H_ */
drivers/fpga/xrt/lib/xleaf/axigate.c new file +325 -0
325 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v8 14/14 fpga: xrt: Kconfig and Makefile updates for XRT drivers
In reply to Lizhi HouUpdate fpga Kconfig/Makefile and add Kconfig/Makefile for new drivers.
--- drivers/Makefile | 1 + drivers/fpga/Kconfig | 2 ++ drivers/fpga/Makefile | 5 +++++ drivers/fpga/xrt/Kconfig | 8 ++++++++ drivers/fpga/xrt/lib/Kconfig | 17 +++++++++++++++++ drivers/fpga/xrt/lib/Makefile | 24 ++++++++++++++++++++++++ drivers/fpga/xrt/metadata/Kconfig | 12 ++++++++++++ drivers/fpga/xrt/metadata/Makefile | 16 ++++++++++++++++ drivers/fpga/xrt/mgmt/Kconfig | 15 +++++++++++++++ drivers/fpga/xrt/mgmt/Makefile | 19 +++++++++++++++++++ 10 files changed, 119 insertions(+) create mode 100644 drivers/fpga/xrt/Kconfig create mode 100644 drivers/fpga/xrt/lib/Kconfig create mode 100644 drivers/fpga/xrt/lib/Makefile create mode 100644 drivers/fpga/xrt/metadata/Kconfig create mode 100644 drivers/fpga/xrt/metadata/Makefile create mode 100644 drivers/fpga/xrt/mgmt/Kconfig create mode 100644 drivers/fpga/xrt/mgmt/Makefile
drivers/Makefile +1 -0
@@ -180,6 +180,7 @@ obj-$(CONFIG_STM) += hwtracing/stm/ obj-$(CONFIG_ANDROID) += android/ obj-$(CONFIG_NVMEM) += nvmem/ obj-$(CONFIG_FPGA) += fpga/ +obj-$(CONFIG_FPGA_XRT_METADATA) += fpga/ obj-$(CONFIG_FSI) += fsi/ obj-$(CONFIG_TEE) += tee/ obj-$(CONFIG_MULTIPLEXER) += mux/
drivers/fpga/Kconfig +2 -0
@@ -234,4 +234,6 @@ config FPGA_MGR_ZYNQMP_FPGA to configure the programmable logic(PL) through PS on ZynqMP SoC. +source "drivers/fpga/xrt/Kconfig" + endif # FPGA
drivers/fpga/Makefile +5 -0
@@ -48,3 +48,8 @@ obj-$(CONFIG_FPGA_DFL_NIOS_INTEL_PAC_N3000) += dfl-n3000-nios.o # Drivers for FPGAs which implement DFL obj-$(CONFIG_FPGA_DFL_PCI) += dfl-pci.o + +# XRT drivers for Alveo +obj-$(CONFIG_FPGA_XRT_METADATA) += xrt/metadata/ +obj-$(CONFIG_FPGA_XRT_LIB) += xrt/lib/ +obj-$(CONFIG_FPGA_XRT_XMGMT) += xrt/mgmt/
drivers/fpga/xrt/Kconfig new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Xilinx Alveo FPGA device configuration +# + +source "drivers/fpga/xrt/metadata/Kconfig" +source "drivers/fpga/xrt/lib/Kconfig" +source "drivers/fpga/xrt/mgmt/Kconfig"
drivers/fpga/xrt/lib/Kconfig new file +17 -0
@@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# XRT Alveo FPGA device configuration +# + +config FPGA_XRT_LIB + tristate "XRT Alveo Driver Library" + depends on HWMON && PCI && HAS_IOMEM + select FPGA_XRT_METADATA + select REGMAP_MMIO + help + Select this option to enable Xilinx XRT Alveo driver library. This + library is core infrastructure of XRT Alveo FPGA drivers which + provides functions for working with device nodes, iteration and + lookup of platform devices, common interfaces for platform devices, + plumbing of function call and ioctls between platform devices and + parent partitions.
drivers/fpga/xrt/lib/Makefile new file +24 -0
@@ -0,0 +1,24 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020-2021 Xilinx, Inc. All rights reserved. +# +# Authors: [email protected] +# + +FULL_XRT_PATH=$(srctree)/$(src)/.. +FULL_DTC_PATH=$(srctree)/scripts/dtc/libfdt + +obj-$(CONFIG_FPGA_XRT_LIB) += xrt-lib.o + +xrt-lib-objs := \ + lib-drv.o \ + xroot.o \ + xclbin.o \ + subdev.o \ + cdev.o \ + group.o \ + xleaf/axigate.o \ + xleaf/icap.o + +ccflags-y := -I$(FULL_XRT_PATH)/include \ + -I$(FULL_DTC_PATH)
drivers/fpga/xrt/metadata/Kconfig new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# XRT Alveo FPGA device configuration +# + +config FPGA_XRT_METADATA + bool "XRT Alveo Driver Metadata Parser" + select LIBFDT + help + This option provides helper functions to parse Xilinx Alveo FPGA + firmware metadata. The metadata is in device tree format and the + XRT driver uses it to discover the HW subsystems behind PCIe BAR.
drivers/fpga/xrt/metadata/Makefile new file +16 -0
@@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020-2021 Xilinx, Inc. All rights reserved. +# +# Authors: [email protected] +# + +FULL_XRT_PATH=$(srctree)/$(src)/.. +FULL_DTC_PATH=$(srctree)/scripts/dtc/libfdt + +obj-$(CONFIG_FPGA_XRT_METADATA) += xrt-md.o + +xrt-md-objs := metadata.o + +ccflags-y := -I$(FULL_XRT_PATH)/include \ + -I$(FULL_DTC_PATH)
drivers/fpga/xrt/mgmt/Kconfig new file +15 -0
@@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Xilinx XRT FPGA device configuration +# + +config FPGA_XRT_XMGMT + tristate "Xilinx Alveo Management Driver" + depends on FPGA_XRT_LIB + select FPGA_XRT_METADATA + select FPGA_BRIDGE + select FPGA_REGION + help + Select this option to enable XRT PCIe driver for Xilinx Alveo FPGA. + This driver provides interfaces for userspace application to access + Alveo FPGA device.
drivers/fpga/xrt/mgmt/Makefile new file +19 -0
@@ -0,0 +1,19 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2020-2021 Xilinx, Inc. All rights reserved. +# +# Authors: [email protected] +# + +FULL_XRT_PATH=$(srctree)/$(src)/.. +FULL_DTC_PATH=$(srctree)/scripts/dtc/libfdt + +obj-$(CONFIG_FPGA_XRT_XMGMT) += xrt-mgmt.o + +xrt-mgmt-objs := root.o \ + xmgmt-main.o \ + xrt-mgr.o \ + xmgmt-main-region.o + +ccflags-y := -I$(FULL_XRT_PATH)/include \ + -I$(FULL_DTC_PATH)
Signature · 1 line
2.27.0
Tom Rix
reviewerQuoted 70 lines · Lizhi Hou
Hello,
This is V8 of patch series which adds management physical function driver for Xilinx Alveo PCIe accelerator cards.
https://www.xilinx.com/products/boards-and-kits/alveo.htmlThis driver is part of Xilinx Runtime (XRT) open source stack.
XILINX ALVEO PLATFORM ARCHITECTURE
Alveo PCIe FPGA based platforms have a static *shell* partition and a partial re-configurable *user* partition. The shell partition is automatically loaded from flash when host is booted and PCIe is enumerated by BIOS. Shell cannot be changed till the next cold reboot. The shell exposes two PCIe physical functions:
1. management physical function
2. user physical functionThe patch series includes
Documentation/xrt.rstwhich describes Alveo platform, XRT driver architecture and deployment model in more detail.Users compile their high level design in C/C++/OpenCL or RTL into FPGA image using Vitis tools.
https://www.xilinx.com/products/design-tools/vitis/vitis-platform.htmlThe compiled image is packaged as xclbin which contains partial bitstream for the user partition and necessary metadata. Users can dynamically swap the image running on the user partition in order to switch between different workloads by loading different xclbins.
XRT DRIVERS FOR XILINX ALVEO
XRT Linux kernel driver *xrt-mgmt* binds to management physical function of Alveo platform. The modular driver framework is organized into several platform drivers which primarily handle the following functionality:
1. Loading firmware container also called xsabin at driver attach time 2. Loading of user compiled xclbin with FPGA Manager integration 3. Clock scaling of image running on user partition 4. In-band sensors: temp, voltage, power, etc. 5. Device reset and rescanThe platform drivers are packaged into *xrt-lib* helper module with well defined interfaces. The module provides a pseudo-bus implementation for the platform drivers. More details on the driver model can be found in
Documentation/xrt.rst.User physical function driver is not included in this patch series.
LIBFDT REQUIREMENT
XRT driver infrastructure uses Device Tree as a metadata format to discover HW subsystems in the Alveo PCIe device. The Device Tree schema used by XRT is documented in
Documentation/xrt.rst.TESTING AND VALIDATION
xrt-mgmt driver can be tested with full XRT open source stack which includes user space libraries, board utilities and (out of tree) first generation user physical function driver xocl. XRT open source runtime stack is available at https://github.com/Xilinx/XRT
Complete documentation for XRT open source stack including sections on Alveo/XRT security and platform architecture can be found here:
https://xilinx.github.io/XRT/master/html/index.html
https://xilinx.github.io/XRT/master/html/security.html
https://xilinx.github.io/XRT/master/html/platforms_partitions.html
Quoted 6 lines
Changes since v7:
- Followed review comment to remove non fpga subdevices:
clock, clkfrq, ucs,ddr_calibaration, devctl and vsec
- Collapsedinclude/uapi/linux/xrt/*.hintoinclude/uapi/linux/fpga-xrt.h
- Cleaned up comments infpga-xrt.h
- Fixed spelling errors inxrt.rst
Lizhi,
Thanks, the changes look good.
Moritz,
I have no outstanding issues and I do not believe there are any from anyone else.
Can this set be included in fpga-next ?
Tom
Quoted 180 lines
Changes since v6:
- Resolved grammatical errors and cleaned up taxonomy inxrt.rstdocumentation.
- Fixed clang warnings.
- Updated code base to include v6 code review comments.Changes since v5:
- Revert all changes 'mgnt/MGNT' back to 'mgmt/MGMT'
- Updated code base to include v5 code review comments.xrt.rst: address grammar and taxonomysubdev_id.h: definesXRT_SUBDEV_INVALID= 0xclbin.c: change shift operation tobe_to_cpu
- Resolved kernel test robot errors.Changes since v4:
- Addedxrt_bus_typeandxrt_device. All sub devices were changed from platform_bus_type/platform_device to xrt_bus_type/xrt_device.
- Renamed xrt-mgmt driver to xrt-mgnt driver.
- Replaced 'MGMT' with 'MGNT' and 'mgmt' with 'mgnt' in code and file names
- Moved pci function calls from infrastructure to xrt-mgnt driver.
- Renamed files:mgmt/main.c->mgnt/xmgnt-main.cmgmt/main-region.c -> mgnt/xmgnt-main-region.c include/xmgmt-main.h -> include/xmgnt-main.h mgmt/fmgr-drv.c -> mgnt/xrt-mgr.c mgmt/fmgr.h -> mgnt/xrt-mgr.h- Updated code base to include v4 code review comments.
Changes since v3:
- Leaf drivers use regmap-mmio to access hardware registers.
- Renamed driver module: xmgmt.ko -> xrt-mgmt.ko
- Renamed files: calib.[c|h] -> ddr_calibration.[c|h], lib/main.[c|h] -> lib/lib-drv.[c|h],mgmt/main-impl.h- >mgmt/xmgnt.h
- Updated code base to include v3 code review comments.Changes since v2:
- Streamlined the driver framework into *xleaf*, *group* and *xroot*
- Updated documentation to show the driver model with examples
- Addressed kernel test robot errors
- Added a selftest for basic driver framework
- Documented device tree schema
- Removed need to export libfdt symbolsChanges since v1:
- Updated the driver to usefpga_regionandfpga_bridgefor FPGA programming
- Dropped platform drivers not related to PR programming to focus on XRT core framework
- UpdatedDocumentation/fpga/xrt.rstwith information on XRT core framework
- Addressed checkpatch issues
- Dropped xrt- prefix from some header filesFor reference V7 version of patch series can be found here:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/Lizhi Hou (14):
Documentation: fpga: Add a document describing XRT Alveo drivers
fpga: xrt: driver metadata helper functions
fpga: xrt: xclbin file helper functions
fpga: xrt: xrt-lib driver manager
fpga: xrt: group driver
fpga: xrt: char dev node helper functions
fpga: xrt: root driver infrastructure
fpga: xrt: driver infrastructure
fpga: xrt: management physical function driver (root)
fpga: xrt: main driver for management function device
fpga: xrt: fpga-mgr and region implementation for xclbin download
fpga: xrt: ICAP driver
fpga: xrt: partition isolation driver
fpga: xrt:KconfigandMakefileupdates for XRT driversDocumentation/fpga/index.rst | 1 + Documentation/fpga/xrt.rst | 870 ++++++++++++++++++ MAINTAINERS | 11 + drivers/Makefile | 1 + drivers/fpga/Kconfig | 2 + drivers/fpga/Makefile | 5 + drivers/fpga/xrt/Kconfig | 8 + drivers/fpga/xrt/include/events.h | 45 + drivers/fpga/xrt/include/group.h | 25 + drivers/fpga/xrt/include/metadata.h | 236 +++++ drivers/fpga/xrt/include/subdev_id.h | 39 + drivers/fpga/xrt/include/xclbin-helper.h | 48 + drivers/fpga/xrt/include/xdevice.h | 131 +++ drivers/fpga/xrt/include/xleaf.h | 205 +++++ drivers/fpga/xrt/include/xleaf/axigate.h | 23 + drivers/fpga/xrt/include/xleaf/clkfreq.h | 21 + drivers/fpga/xrt/include/xleaf/clock.h | 29 + .../fpga/xrt/include/xleaf/ddr_calibration.h | 28 + drivers/fpga/xrt/include/xleaf/devctl.h | 40 + drivers/fpga/xrt/include/xleaf/icap.h | 27 + drivers/fpga/xrt/include/xmgmt-main.h | 34 + drivers/fpga/xrt/include/xroot.h | 117 +++ drivers/fpga/xrt/lib/Kconfig | 17 + drivers/fpga/xrt/lib/Makefile | 24 + drivers/fpga/xrt/lib/cdev.c | 209 +++++ drivers/fpga/xrt/lib/group.c | 278 ++++++ drivers/fpga/xrt/lib/lib-drv.c | 322 +++++++ drivers/fpga/xrt/lib/lib-drv.h | 21 + drivers/fpga/xrt/lib/subdev.c | 859 +++++++++++++++++ drivers/fpga/xrt/lib/subdev_pool.h | 53 ++ drivers/fpga/xrt/lib/xclbin.c | 381 ++++++++ drivers/fpga/xrt/lib/xleaf/axigate.c | 325 +++++++ drivers/fpga/xrt/lib/xleaf/icap.c | 328 +++++++ drivers/fpga/xrt/lib/xroot.c | 536 +++++++++++ drivers/fpga/xrt/metadata/Kconfig | 12 + drivers/fpga/xrt/metadata/Makefile | 16 + drivers/fpga/xrt/metadata/metadata.c | 578 ++++++++++++ drivers/fpga/xrt/mgmt/Kconfig | 15 + drivers/fpga/xrt/mgmt/Makefile | 19 + drivers/fpga/xrt/mgmt/root.c | 420 +++++++++ drivers/fpga/xrt/mgmt/xmgmt-main-region.c | 483 ++++++++++ drivers/fpga/xrt/mgmt/xmgmt-main.c | 662 +++++++++++++ drivers/fpga/xrt/mgmt/xmgmt.h | 33 + drivers/fpga/xrt/mgmt/xrt-mgr.c | 190 ++++ drivers/fpga/xrt/mgmt/xrt-mgr.h | 16 + include/uapi/linux/fpga-xrt.h | 428 +++++++++ 46 files changed, 8171 insertions(+)create mode 100644
Documentation/fpga/xrt.rst
create mode 100644drivers/fpga/xrt/Kconfig
create mode 100644drivers/fpga/xrt/include/events.hcreate mode 100644drivers/fpga/xrt/include/group.hcreate mode 100644drivers/fpga/xrt/include/metadata.hcreate mode 100644drivers/fpga/xrt/include/subdev_id.hcreate mode 100644drivers/fpga/xrt/include/xclbin-helper.hcreate mode 100644drivers/fpga/xrt/include/xdevice.hcreate mode 100644drivers/fpga/xrt/include/xleaf.hcreate mode 100644drivers/fpga/xrt/include/xleaf/axigate.hcreate mode 100644drivers/fpga/xrt/include/xleaf/clkfreq.hcreate mode 100644drivers/fpga/xrt/include/xleaf/clock.hcreate mode 100644drivers/fpga/xrt/include/xleaf/ddr_calibration.hcreate mode 100644drivers/fpga/xrt/include/xleaf/devctl.hcreate mode 100644drivers/fpga/xrt/include/xleaf/icap.hcreate mode 100644drivers/fpga/xrt/include/xmgmt-main.hcreate mode 100644drivers/fpga/xrt/include/xroot.hcreate mode 100644drivers/fpga/xrt/lib/Kconfig
create mode 100644drivers/fpga/xrt/lib/Makefile
create mode 100644drivers/fpga/xrt/lib/cdev.c
create mode 100644drivers/fpga/xrt/lib/group.c
create mode 100644drivers/fpga/xrt/lib/lib-drv.c
create mode 100644drivers/fpga/xrt/lib/lib-drv.h
create mode 100644drivers/fpga/xrt/lib/subdev.c
create mode 100644drivers/fpga/xrt/lib/subdev_pool.hcreate mode 100644drivers/fpga/xrt/lib/xclbin.c
create mode 100644drivers/fpga/xrt/lib/xleaf/axigate.ccreate mode 100644drivers/fpga/xrt/lib/xleaf/icap.ccreate mode 100644drivers/fpga/xrt/lib/xroot.c
create mode 100644drivers/fpga/xrt/metadata/Kconfigcreate mode 100644drivers/fpga/xrt/metadata/Makefilecreate mode 100644drivers/fpga/xrt/metadata/metadata.ccreate mode 100644drivers/fpga/xrt/mgmt/Kconfig
create mode 100644drivers/fpga/xrt/mgmt/Makefile
create mode 100644drivers/fpga/xrt/mgmt/root.c
create mode 100644drivers/fpga/xrt/mgmt/xmgmt-main-region.ccreate mode 100644drivers/fpga/xrt/mgmt/xmgmt-main.ccreate mode 100644drivers/fpga/xrt/mgmt/xmgmt.h
create mode 100644drivers/fpga/xrt/mgmt/xrt-mgr.ccreate mode 100644drivers/fpga/xrt/mgmt/xrt-mgr.hcreate mode 100644include/uapi/linux/fpga-xrt.h
Moritz Fischer
maintainerHi all,
Quoted 81 lines · Tom Rix
Quoted 70 lines · Lizhi Hou
Hello,
This is V8 of patch series which adds management physical function driver for Xilinx Alveo PCIe accelerator cards.
https://www.xilinx.com/products/boards-and-kits/alveo.htmlThis driver is part of Xilinx Runtime (XRT) open source stack.
XILINX ALVEO PLATFORM ARCHITECTURE
Alveo PCIe FPGA based platforms have a static *shell* partition and a partial re-configurable *user* partition. The shell partition is automatically loaded from flash when host is booted and PCIe is enumerated by BIOS. Shell cannot be changed till the next cold reboot. The shell exposes two PCIe physical functions:
1. management physical function
2. user physical functionThe patch series includes
Documentation/xrt.rstwhich describes Alveo platform, XRT driver architecture and deployment model in more detail.Users compile their high level design in C/C++/OpenCL or RTL into FPGA image using Vitis tools.
https://www.xilinx.com/products/design-tools/vitis/vitis-platform.htmlThe compiled image is packaged as xclbin which contains partial bitstream for the user partition and necessary metadata. Users can dynamically swap the image running on the user partition in order to switch between different workloads by loading different xclbins.
XRT DRIVERS FOR XILINX ALVEO
XRT Linux kernel driver *xrt-mgmt* binds to management physical function of Alveo platform. The modular driver framework is organized into several platform drivers which primarily handle the following functionality:
1. Loading firmware container also called xsabin at driver attach time 2. Loading of user compiled xclbin with FPGA Manager integration 3. Clock scaling of image running on user partition 4. In-band sensors: temp, voltage, power, etc. 5. Device reset and rescanThe platform drivers are packaged into *xrt-lib* helper module with well defined interfaces. The module provides a pseudo-bus implementation for the platform drivers. More details on the driver model can be found in
Documentation/xrt.rst.User physical function driver is not included in this patch series.
LIBFDT REQUIREMENT
XRT driver infrastructure uses Device Tree as a metadata format to discover HW subsystems in the Alveo PCIe device. The Device Tree schema used by XRT is documented in
Documentation/xrt.rst.TESTING AND VALIDATION
xrt-mgmt driver can be tested with full XRT open source stack which includes user space libraries, board utilities and (out of tree) first generation user physical function driver xocl. XRT open source runtime stack is available at https://github.com/Xilinx/XRT
Complete documentation for XRT open source stack including sections on Alveo/XRT security and platform architecture can be found here:
https://xilinx.github.io/XRT/master/html/index.html
https://xilinx.github.io/XRT/master/html/security.html
https://xilinx.github.io/XRT/master/html/platforms_partitions.htmlQuoted 6 lines
Changes since v7:
- Followed review comment to remove non fpga subdevices:
clock, clkfrq, ucs,ddr_calibaration, devctl and vsec
- Collapsedinclude/uapi/linux/xrt/*.hintoinclude/uapi/linux/fpga-xrt.h
- Cleaned up comments infpga-xrt.h
- Fixed spelling errors inxrt.rstLizhi,
Quoted 8 lines
Thanks, the changes look good.
Moritz,
I have no outstanding issues and I do not believe there are any from anyone else.
Can this set be included in fpga-next ?
I'll get to it this weekend, apologies for the delay guys.
- Moritz