Skip to contentSkip to conversation

XRT Alveo driver overview

PATCH Superseded Lizhi Hou 8 participants 87 messages Feb 18, 2021 – Mar 18, 2021 1 message not in the archive Page 1 of 3

Subsystems (2)

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Lizhi Hou

author
#0 lore raw

v3 cover XRT Alveo driver overview

Hello,

This is V3 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 https://www.xilinx.com/products/design-tools/vitis/vitis-platform.html
tools. 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 *xmgmt* 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. Unlike previous V1 and V2 version of patch series, V3 version does not require export of libfdt symbols.

TESTING AND VALIDATION

xmgmt 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 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 V1 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]/

Lizhi Hou (18):
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 platform driver manager
fpga: xrt: group platform driver
fpga: xrt: platform driver infrastructure
fpga: xrt: management physical function driver (root)
fpga: xrt: main platform driver for management function device
fpga: xrt: fpga-mgr and region implementation for xclbin download
fpga: xrt: VSEC platform driver
fpga: xrt: UCS platform driver
fpga: xrt: ICAP platform driver
fpga: xrt: devctl platform driver
fpga: xrt: clock platform driver
fpga: xrt: clock frequence counter platform driver
fpga: xrt: DDR calibration platform driver
fpga: xrt: partition isolation platform driver
fpga: xrt: Kconfig and Makefile updates for XRT drivers

 Documentation/fpga/index.rst             |   1 +
 Documentation/fpga/xrt.rst               | 842 ++++++++++++++++++++++
 MAINTAINERS                              |  11 +
 drivers/Makefile                         |   1 +
 drivers/fpga/Kconfig                     |   2 +
 drivers/fpga/Makefile                    |   4 +
 drivers/fpga/xrt/Kconfig                 |   8 +
 drivers/fpga/xrt/include/events.h        |  48 ++
 drivers/fpga/xrt/include/group.h         |  27 +
 drivers/fpga/xrt/include/metadata.h      | 229 ++++++
 drivers/fpga/xrt/include/subdev_id.h     |  43 ++
 drivers/fpga/xrt/include/xclbin-helper.h |  52 ++
 drivers/fpga/xrt/include/xleaf.h         | 276 +++++++
 drivers/fpga/xrt/include/xleaf/axigate.h |  25 +
 drivers/fpga/xrt/include/xleaf/calib.h   |  30 +
 drivers/fpga/xrt/include/xleaf/clkfreq.h |  23 +
 drivers/fpga/xrt/include/xleaf/clock.h   |  31 +
 drivers/fpga/xrt/include/xleaf/devctl.h  |  43 ++
 drivers/fpga/xrt/include/xleaf/icap.h    |  29 +
 drivers/fpga/xrt/include/xleaf/ucs.h     |  24 +
 drivers/fpga/xrt/include/xmgmt-main.h    |  37 +
 drivers/fpga/xrt/include/xroot.h         | 114 +++
 drivers/fpga/xrt/lib/Kconfig             |  16 +
 drivers/fpga/xrt/lib/Makefile            |  30 +
 drivers/fpga/xrt/lib/cdev.c              | 231 ++++++
 drivers/fpga/xrt/lib/group.c             | 265 +++++++
 drivers/fpga/xrt/lib/main.c              | 274 +++++++
 drivers/fpga/xrt/lib/main.h              |  17 +
 drivers/fpga/xrt/lib/subdev.c            | 871 +++++++++++++++++++++++
 drivers/fpga/xrt/lib/subdev_pool.h       |  53 ++
 drivers/fpga/xrt/lib/xclbin.c            | 394 ++++++++++
 drivers/fpga/xrt/lib/xleaf/axigate.c     | 298 ++++++++
 drivers/fpga/xrt/lib/xleaf/calib.c       | 226 ++++++
 drivers/fpga/xrt/lib/xleaf/clkfreq.c     | 221 ++++++
 drivers/fpga/xrt/lib/xleaf/clock.c       | 648 +++++++++++++++++
 drivers/fpga/xrt/lib/xleaf/devctl.c      | 206 ++++++
 drivers/fpga/xrt/lib/xleaf/icap.c        | 317 +++++++++
 drivers/fpga/xrt/lib/xleaf/ucs.c         | 235 ++++++
 drivers/fpga/xrt/lib/xleaf/vsec.c        | 359 ++++++++++
 drivers/fpga/xrt/lib/xroot.c             | 598 ++++++++++++++++
 drivers/fpga/xrt/metadata/Kconfig        |  12 +
 drivers/fpga/xrt/metadata/Makefile       |  16 +
 drivers/fpga/xrt/metadata/metadata.c     | 524 ++++++++++++++
 drivers/fpga/xrt/mgmt/Kconfig            |  15 +
 drivers/fpga/xrt/mgmt/Makefile           |  19 +
 drivers/fpga/xrt/mgmt/fmgr-drv.c         | 187 +++++
 drivers/fpga/xrt/mgmt/fmgr.h             |  28 +
 drivers/fpga/xrt/mgmt/main-impl.h        |  37 +
 drivers/fpga/xrt/mgmt/main-region.c      | 471 ++++++++++++
 drivers/fpga/xrt/mgmt/main.c             | 693 ++++++++++++++++++
 drivers/fpga/xrt/mgmt/root.c             | 342 +++++++++
 include/uapi/linux/xrt/xclbin.h          | 408 +++++++++++
 include/uapi/linux/xrt/xmgmt-ioctl.h     |  46 ++
 53 files changed, 9957 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/xleaf.h
 create mode 100644 drivers/fpga/xrt/include/xleaf/axigate.h
 create mode 100644 drivers/fpga/xrt/include/xleaf/calib.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/devctl.h
 create mode 100644 drivers/fpga/xrt/include/xleaf/icap.h
 create mode 100644 drivers/fpga/xrt/include/xleaf/ucs.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/main.c
 create mode 100644 drivers/fpga/xrt/lib/main.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/calib.c
 create mode 100644 drivers/fpga/xrt/lib/xleaf/clkfreq.c
 create mode 100644 drivers/fpga/xrt/lib/xleaf/clock.c
 create mode 100644 drivers/fpga/xrt/lib/xleaf/devctl.c
 create mode 100644 drivers/fpga/xrt/lib/xleaf/icap.c
 create mode 100644 drivers/fpga/xrt/lib/xleaf/ucs.c
 create mode 100644 drivers/fpga/xrt/lib/xleaf/vsec.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/fmgr-drv.c
 create mode 100644 drivers/fpga/xrt/mgmt/fmgr.h
 create mode 100644 drivers/fpga/xrt/mgmt/main-impl.h
 create mode 100644 drivers/fpga/xrt/mgmt/main-region.c
 create mode 100644 drivers/fpga/xrt/mgmt/main.c
 create mode 100644 drivers/fpga/xrt/mgmt/root.c
 create mode 100644 include/uapi/linux/xrt/xclbin.h
 create mode 100644 include/uapi/linux/xrt/xmgmt-ioctl.h
Signature · 1 line
2.18.4
author
#1 lore raw

PATCH v3 1/18 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   | 842 +++++++++++++++++++++++++++++++++++
 2 files changed, 843 insertions(+)
 create mode 100644 Documentation/fpga/xrt.rst
Documentation/fpga/index.rst +1 -0
index f80f95667ca2..30134357b70d 100644
@@ -8,6 +8,7 @@ fpga
     :maxdepth: 1
 
     dfl
+    xrt
 
 .. only::  subproject and html
 
Documentation/fpga/xrt.rst new file +842 -0

842 lines not on this page · raw message

Signature · 1 line
2.18.4
reviewer
#2 lore raw

Re: [PATCH V3 XRT Alveo 01/18] Documentation: fpga: Add a document describing XRT Alveo drivers

From the documentation, there are a couple of big questions and a bunch of word smithing.

pseudo-bus : do we need a bus ?

xrt-lib real platform devices that aren't fpga, do they need to move to another subsystem ?

Overall looks good, love the ascii art!

Quoted 37 lines · Lizhi Hou

Describe XRT driver architecture and provide basic overview of Xilinx Alveo platform.

---
 Documentation/fpga/index.rst |   1 +
 Documentation/fpga/xrt.rst   | 842 +++++++++++++++++++++++++++++++++++
 2 files changed, 843 insertions(+)
 create mode 100644 Documentation/fpga/xrt.rst
Documentation/fpga/index.rst +1 -0
index f80f95667ca2..30134357b70d 100644
@@ -8,6 +8,7 @@ fpga
     :maxdepth: 1
 
     dfl
+    xrt
 
 .. only::  subproject and html
 
Documentation/fpga/xrt.rst new file +7 -0
new file mode 100644
index 000000000000..9bc2d2785cb9
@@ -0,0 +1,842 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==================================
+XRTV2 Linux Kernel Driver Overview
+==================================
+
+Authors:

Documentation/fpga/xrt.rst line 17

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+
+* Sonal Santan <[email protected]>
+* Max Zhen <[email protected]>
+* Lizhi Hou <[email protected]>
+
+XRTV2 drivers are second generation `XRT <https://github.com/Xilinx/XRT>`_
+drivers which support `Alveo <https://www.xilinx.com/products/boards-and-kits/alveo.html>`_
+PCIe platforms from Xilinx.
+
+XRTV2 drivers support *subsystem* style data driven platforms where driver's

where the driver's

Documentation/fpga/xrt.rst line 23

Quoted 6 lines
Documentation/fpga/xrt.rst new file +6 -0
+configuration and behavior is determined by meta data provided by the platform
+(in *device tree* format). Primary management physical function (MPF) driver
+is called **xmgmt**. Primary user physical function (UPF) driver is called
+**xuser** and is under development. xrt driver framework and HW subsystem
+drivers are packaged into a library module called **xrt-lib**, which is
+shared by **xmgmt** and **xuser** (under development). The xrt driver framework

xuser still under development ?

Documentation/fpga/xrt.rst line 24

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+implements a pseudo-bus which is used to discover HW subsystems and facilitate

A pseudo-bus.

It would be good if this was close to what was done for dfl here

https://lore.kernel.org/linux-fpga/[email protected]/

Documentation/fpga/xrt.rst line 33

Quoted 9 lines
Documentation/fpga/xrt.rst new file +9 -0
+inter HW subsystem interaction.
+
+Driver Modules
+==============
+
+xrt-lib.ko
+----------
+
+Repository of all subsystem drivers and pure software modules that can potentially

subsystem drivers

drivers in fpga/ should be for managing just the fpganess of the fpga.

soft devices ex/ a soft tty should go to their respective subsystem location

Are there any in this patchset you think might move ?

Maybe we can defer reviewing those now.

Documentation/fpga/xrt.rst line 36

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+be shared between xmgmt and xuser. All these drivers are structured as Linux
+*platform driver* and are instantiated by xmgmt (or xuser under development) based
+on meta data associated with hardware. The metadata is in the form of device tree

with the hardware

form of a device tree

Documentation/fpga/xrt.rst line 44

Quoted 8 lines
Documentation/fpga/xrt.rst new file +8 -0
+as mentioned before. Each platform driver statically defines a subsystem node
+array by using node name or a string in its ``compatible`` property. And this
+array is eventually translated to IOMEM resources of the platform device.
+
+The xrt-lib core infrastructure provides hooks to platform drivers for device node
+management, user file operations and ioctl callbacks. The core also provides pseudo-bus
+functionality for platform driver registration, discovery and inter platform driver
+ioctl calls.

core infrastructure.

The interfaces to the infrastructure are not in include/linux/fpga/

Maybe this needs to change.

Documentation/fpga/xrt.rst line 56

Quoted 12 lines
Documentation/fpga/xrt.rst new file +12 -0
+
+.. note::
+   See code in ``include/xleaf.h``
+
+
+xmgmt.ko
+--------
+
+The xmgmt driver is a PCIe device driver driving MPF found on Xilinx's Alveo
+PCIE device. It consists of one *root* driver, one or more *group* drivers
+and one or more *xleaf* drivers. The root and MPF specific xleaf drivers are
+in xmgmt.ko. The group driver and other xleaf drivers are in xrt-lib.ko.

I am not sure if *.ko is correct, these will also be intree.

Documentation/fpga/xrt.rst line 58

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+
+The instantiation of specific group driver or xleaf driver is completely data

of a specific

Documentation/fpga/xrt.rst line 59

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+driven based on meta data (mostly in device tree format) found through VSEC

mostly ? what is the deviation from device tree ?

Documentation/fpga/xrt.rst line 61

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+capability and inside firmware files, such as platform xsabin or user xclbin file.
+The root driver manages life cycle of multiple group drivers, which, in turn,

the life cycle

Documentation/fpga/xrt.rst line 62

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+manages multiple xleaf drivers. This allows a single set of driver code to support

set of drivers

drop 'code'

Quoted 44 lines
Documentation/fpga/xrt.rst new file +44 -0
+all kinds of subsystems exposed by different shells. The difference among all
+these subsystems will be handled in xleaf drivers with root and group drivers
+being part of the infrastructure and provide common services for all leaves
+found on all platforms.
+
+The driver object model looks like the following::
+
+                    +-----------+
+                    |   xroot   |
+                    +-----+-----+
+                          |
+              +-----------+-----------+
+              |                       |
+              v                       v
+        +-----------+          +-----------+
+        |   group   |    ...   |   group   |
+        +-----+-----+          +------+----+
+              |                       |
+              |                       |
+        +-----+----+            +-----+----+
+        |          |            |          |
+        v          v            v          v
+    +-------+  +-------+    +-------+  +-------+
+    | xleaf |..| xleaf |    | xleaf |..| xleaf |
+    +-------+  +-------+    +-------+  +-------+
+
+As an example for Xilinx Alveo U50 before user xclbin download, the tree
+looks like the following::
+
+                                +-----------+
+                                |   xmgmt   |
+                                +-----+-----+
+                                      |
+            +-------------------------+--------------------+
+            |                         |                    |
+            v                         v                    v
+       +--------+                +--------+            +--------+
+       | group0 |                | group1 |            | group2 |
+       +----+---+                +----+---+            +---+----+
+            |                         |                    |
+            |                         |                    |
+      +-----+-----+        +----+-----+---+    +-----+-----+----+--------+
+      |           |        |    |         |    |     |          |        |
+      v           v        |    v         v    |     v          v        |

Documentation/fpga/xrt.rst line 116

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+ +------------+  +------+  | +------+ +------+ |  +------+ +-----------+ |
+ | xmgmt_main |  | VSEC |  | | GPIO | | QSPI | |  |  CMC | | AXI-GATE0 | |
+ +------------+  +------+  | +------+ +------+ |  +------+ +-----------+ |
+                           | +---------+       |  +------+ +-----------+ |
+                           +>| MAILBOX |       +->| ICAP | | AXI-GATE1 |<+
+                             +---------+       |  +------+ +-----------+
+                                               |  +-------+
+                                               +->| CALIB |
+                                                  +-------+
+

Nice ascii art!

Quoted 44 lines
Documentation/fpga/xrt.rst new file +44 -0
+After an xclbin is download, group3 will be added and the tree looks like the
+following::
+
+                                +-----------+
+                                |   xmgmt   |
+                                +-----+-----+
+                                      |
+            +-------------------------+--------------------+-----------------+
+            |                         |                    |                 |
+            v                         v                    v                 |
+       +--------+                +--------+            +--------+            |
+       | group0 |                | group1 |            | group2 |            |
+       +----+---+                +----+---+            +---+----+            |
+            |                         |                    |                 |
+            |                         |                    |                 |
+      +-----+-----+       +-----+-----+---+    +-----+-----+----+--------+   |
+      |           |       |     |         |    |     |          |        |   |
+      v           v       |     v         v    |     v          v        |   |
+ +------------+  +------+ | +------+ +------+  |  +------+ +-----------+ |   |
+ | xmgmt_main |  | VSEC | | | GPIO | | QSPI |  |  |  CMC | | AXI-GATE0 | |   |
+ +------------+  +------+ | +------+ +------+  |  +------+ +-----------+ |   |
+                          | +---------+        |  +------+ +-----------+ |   |
+                          +>| MAILBOX |        +->| ICAP | | AXI-GATE1 |<+   |
+                            +---------+        |  +------+ +-----------+     |
+                                               |  +-------+                  |
+                                               +->| CALIB |                  |
+                                                  +-------+                  |
+                      +---+----+                                             |
+                      | group3 |<--------------------------------------------+
+                      +--------+
+                          |
+                          |
+     +-------+--------+---+--+--------+------+-------+
+     |       |        |      |        |      |       |
+     v       |        v      |        v      |       v
+ +--------+  |   +--------+  |   +--------+  |    +-----+
+ | CLOCK0 |  |   | CLOCK1 |  |   | CLOCK2 |  |    | UCS |
+ +--------+  v   +--------+  v   +--------+  v    +-----+
+ +-------------+ +-------------+ +-------------+
+ | CLOCK-FREQ0 | | CLOCK-FREQ1 | | CLOCK-FREQ2 |
+ +-------------+ +-------------+ +-------------+
+
+
+xmgmt-root

Documentation/fpga/xrt.rst line 170

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+^^^^^^^^^^
+
+The xmgmt-root driver is a PCIe device driver attached to MPF. It's part of the
+infrastructure of the MPF driver and resides in xmgmt.ko. This driver
+
+* manages one or more group drivers
+* provides access to functionalities that requires pci_dev, such as PCIE config
+  space access, to other xleaf drivers through root calls
+* together with group driver, facilities event callbacks for other xleaf drivers
+* together with group driver, facilities inter-leaf driver calls for other xleaf

Maybe drop 'together with group driver'

Documentation/fpga/xrt.rst line 177

Quoted 7 lines
Documentation/fpga/xrt.rst new file +7 -0
+  drivers
+
+When root driver starts, it will explicitly create an initial group instance,
+which contains xleaf drivers that will trigger the creation of other group
+instances. The root driver will wait for all group and leaves to be created
+before it returns from it's probe routine and claim success of the
+initialization of the entire xmgmt driver.

What happens if there a failure in one leaf ? Does the whole board go down ?

Documentation/fpga/xrt.rst line 186

Quoted 9 lines
Documentation/fpga/xrt.rst new file +9 -0
+
+.. note::
+   See code in ``lib/xroot.c`` and ``mgmt/root.c``
+
+
+group
+^^^^^
+
+The group driver is a platform device driver whose life cycle is managed by

Maybe call this a 'pseudo device'

Documentation/fpga/xrt.rst line 191

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+root and does not have real IO mem or IRQ resources. It's part of the
+infrastructure of the MPF driver and resides in xrt-lib.ko. This driver
+
+* manages one or more xleaf drivers so that multiple leaves can be managed as a
+  group

can drop 'so that multiple leaves can be managed as a group' to me, this is the same as 'one or more'

Documentation/fpga/xrt.rst line 195

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+* provides access to root from leaves, so that root calls, event notifications
+  and inter-leaf calls can happen
+
+In xmgmt, an initial group driver instance will be created by root, which

by the root

Documentation/fpga/xrt.rst line 203

Quoted 8 lines
Documentation/fpga/xrt.rst new file +8 -0
+contains leaves that will trigger group instances to be created to manage
+groups of leaves found on different partitions on hardware, such as VSEC, Shell,
+and User.
+
+Every *fpga_region* has a group object associated with it. The group is
+created when xclbin image is loaded on the fpga_region. The existing group
+is destroyed when a new xclbin image is loaded. The fpga_region persists
+across xclbin downloads.

The connection of a 'group' node to a fpga region region is fairly important, maybe move this section earlier. 'group' as an fpganess thing would be kept in fpga/ subsystem.

Documentation/fpga/xrt.rst line 216

Quoted 13 lines
Documentation/fpga/xrt.rst new file +13 -0
+
+.. note::
+   See code in ``lib/group.c``
+
+
+xleaf
+^^^^^
+
+The xleaf driver is a platform device driver whose life cycle is managed by
+a group driver and may or may not have real IO mem or IRQ resources. They
+are the real meat of xmgmt and contains platform specific code to Shell and
+User found on a MPF.
+

Maybe a split is pseudo device leaves, those without real IO mem, stay in fpga/  others go ?

Documentation/fpga/xrt.rst line 220

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+A xleaf driver may not have real hardware resources when it merely acts as a
+driver that manages certain in-memory states for xmgmt. These in-memory states
+could be shared by multiple other leaves.
+

This implies locking and some message passing.

Documentation/fpga/xrt.rst line 221

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+Leaf drivers assigned to specific hardware resources drive specific subsystem in

drive a specific

Documentation/fpga/xrt.rst line 227

Quoted 6 lines
Documentation/fpga/xrt.rst new file +6 -0
+the device. To manipulate the subsystem or carry out a task, a xleaf driver may
+ask help from root via root calls and/or from other leaves via inter-leaf calls.
+
+A xleaf can also broadcast events through infrastructure code for other leaves
+to process. It can also receive event notification from infrastructure about
+certain events, such as post-creation or pre-exit of a particular xleaf.

I would like to see some examples of how the inter node communications work.

Documentation/fpga/xrt.rst line 239

Quoted 12 lines
Documentation/fpga/xrt.rst new file +12 -0
+
+.. note::
+   See code in ``lib/xleaf/*.c``
+
+
+FPGA Manager Interaction
+========================
+
+fpga_manager
+------------
+
+An instance of fpga_manager is created by xmgmt_main and is used for xclbin

for the xclbin

Documentation/fpga/xrt.rst line 241

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+image download. fpga_manager requires the full xclbin image before it can
+start programming the FPGA configuration engine via ICAP platform driver.

via the ICAP

what is ICAP ?

Documentation/fpga/xrt.rst line 246

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+
+fpga_region
+-----------
+
+For every interface exposed by currently loaded xclbin/xsabin in the *parent*

by the currently

Documentation/fpga/xrt.rst line 247

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+fpga_region a new instance of fpga_region is created like a *child* region.

fpga_region,

Documentation/fpga/xrt.rst line 249

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+The device tree of the *parent* fpga_region defines the
+resources for a new instance of fpga_bridge which isolates the parent from

and isolates

Documentation/fpga/xrt.rst line 253

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+child fpga_region. This new instance of fpga_bridge will be used when a
+xclbin image is loaded on the child fpga_region. After the xclbin image is
+downloaded to the fpga_region, an instance of group is created for the
+fpga_region using the device tree obtained as part of xclbin. If this device

of the xclbin

Documentation/fpga/xrt.rst line 254

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+tree defines any child interfaces then it can trigger the creation of

interfaces, then

Documentation/fpga/xrt.rst line 255

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+fpga_bridge and fpga_region for the next region in the chain.

a fpga_bridge and a fpga_region

Documentation/fpga/xrt.rst line 260

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+
+fpga_bridge
+-----------
+
+Like fpga_region, matching fpga_bridge is also created by walking the device

Like the fpga_region, a matchin

Quoted 6 lines
Documentation/fpga/xrt.rst new file +6 -0
+tree of the parent group.
+
+Driver Interfaces
+=================
+
+xmgmt Driver Ioctls

Documentation/fpga/xrt.rst line 276

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+-------------------
+
+Ioctls exposed by xmgmt driver to user space are enumerated in the following
+table:
+
+== ===================== ============================ ==========================
+#  Functionality         ioctl request code            data format
+== ===================== ============================ ==========================
+1  FPGA image download   XMGMT_IOCICAPDOWNLOAD_AXLF    xmgmt_ioc_bitstream_axlf
+== ===================== ============================ ==========================

This data format is described below, maybe swap this section with that so

folks will know what xmgmnt_ioc_bitstream_axlf is before this section.

Documentation/fpga/xrt.rst line 278

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+
+User xclbin can be downloaded by using xbmgmt tool from XRT open source suite. See

A user xclbin

using the xbmgmt

from the XRT

Quoted 20 lines
Documentation/fpga/xrt.rst new file +20 -0
+example usage below::
+
+  xbmgmt partition --program --path /lib/firmware/xilinx/862c7020a250293e32036f19956669e5/test/verify.xclbin --force
+
+xmgmt Driver Sysfs
+------------------
+
+xmgmt driver exposes a rich set of sysfs interfaces. Subsystem platform
+drivers export sysfs node for every platform instance.
+
+Every partition also exports its UUIDs. See below for examples::
+
+  /sys/bus/pci/devices/0000:06:00.0/xmgmt_main.0/interface_uuids
+  /sys/bus/pci/devices/0000:06:00.0/xmgmt_main.0/logic_uuids
+
+
+hwmon
+-----
+
+xmgmt driver exposes standard hwmon interface to report voltage, current,

Documentation/fpga/xrt.rst line 308

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+temperature, power, etc. These can easily be viewed using *sensors* command
+line utility.
+
+Alveo Platform Overview
+=======================
+
+Alveo platforms are architected as two physical FPGA partitions: *Shell* and
+*User*. The Shell provides basic infrastructure for the Alveo platform like
+PCIe connectivity, board management, Dynamic Function Exchange (DFX), sensors,
+clocking, reset, and security. User partition contains user compiled FPGA

the user compiled

Documentation/fpga/xrt.rst

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+binary which is loaded by a process called DFX also known as partial
+reconfiguration.
+
+Physical partitions require strict HW compatibility with each other for DFX to
+work properly. 

swap order

For DFX to work properly physical partitions ..

Documentation/fpga/xrt.rst line 316

Quoted 4 lines

Every physical partition has two interface UUIDs: *parent* UUID

Documentation/fpga/xrt.rst new file +3 -0
+and *child* UUID. For simple single stage platforms, Shell → User forms parent
+child relationship. For complex two stage platforms, Base → Shell → User forms
+the parent child relationship chain.

this bit is confusing. is this related to uuid?

Documentation/fpga/xrt.rst line 320

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+
+.. note::
+   Partition compatibility matching is key design component of Alveo platforms
+   and XRT. Partitions have child and parent relationship. A loaded partition

have a child

Quoted 1 line

+ exposes child partition UUID to advertise its compatibility requirement for

the child's

can drop 'for child partition'

Quoted 1 line

+ child partition. When loading a child partition the xmgmt management driver

When loading a child partition,

Documentation/fpga/xrt.rst

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+   matches parent UUID of the child partition against child UUID exported by
+   the parent. Parent and child partition UUIDs are stored in the *xclbin*
+   (for user) or *xsabin* (for base and shell).

this is confusing, is this part of the file image format ?

Maybe save/move till the image layout.

Documentation/fpga/xrt.rst line 327

Quoted 3 lines

Except for root UUID, VSEC,

Documentation/fpga/xrt.rst new file +2 -0
+   hardware itself does not know about UUIDs. UUIDs are stored in xsabin and
+   xclbin.

This is confusing too, not sure how to untangle.

Quoted 15 lines
Documentation/fpga/xrt.rst new file +15 -0
+
+
+The physical partitions and their loading is illustrated below::
+
+           SHELL                               USER
+        +-----------+                  +-------------------+
+        |           |                  |                   |
+        | VSEC UUID | CHILD     PARENT |    LOGIC UUID     |
+        |           o------->|<--------o                   |
+        |           | UUID       UUID  |                   |
+        +-----+-----+                  +--------+----------+
+              |                                 |
+              .                                 .
+              |                                 |
+          +---+---+                      +------+--------+

Documentation/fpga/xrt.rst line 352

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+          |  POR  |                      | USER COMPILED |
+          | FLASH |                      |    XCLBIN     |
+          +-------+                      +---------------+
+
+
+Loading Sequence
+----------------
+
+The Shell partition is loaded from flash at system boot time. It establishes the
+PCIe link and exposes two physical functions to the BIOS. After OS boot, xmgmt

the OS boots, the xmgmt

Documentation/fpga/xrt.rst line 354

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+driver attaches to PCIe physical function 0 exposed by the Shell and then looks
+for VSEC in PCIe extended configuration space. Using VSEC it determines the logic

the PCIe

The driver uses VSEC to determine the UUID of Shell.  The UUID is also used to load a matching ...

Documentation/fpga/xrt.rst line 357

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+UUID of Shell and uses the UUID to load matching *xsabin* file from Linux firmware
+directory. The xsabin file contains metadata to discover peripherals that are part
+of Shell and firmware(s) for any embedded soft processors in Shell.

the firmware needed for any ...

Documentation/fpga/xrt.rst line 359

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+
+The Shell exports child interface UUID which is used for compatibility check when

export a child

for a compatibility check

Documentation/fpga/xrt.rst line 361

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+loading user compiled xclbin over the User partition as part of DFX. When a user
+requests loading of a specific xclbin the xmgmt management driver reads the parent

xclbin, the

Documentation/fpga/xrt.rst line 374

Quoted 13 lines
Documentation/fpga/xrt.rst new file +13 -0
+interface UUID specified in the xclbin and matches it with child interface UUID
+exported by Shell to determine if xclbin is compatible with the Shell. If match
+fails loading of xclbin is denied.
+
+xclbin loading is requested using ICAP_DOWNLOAD_AXLF ioctl command. When loading
+xclbin, xmgmt driver performs the following *logical* operations:
+
+1. Copy xclbin from user to kernel memory
+2. Sanity check the xclbin contents
+3. Isolate the User partition
+4. Download the bitstream using the FPGA config engine (ICAP)
+5. De-isolate the User partition
+6. Program the clocks (ClockWiz) driving the User partition

maybe drop '(ClockWiz)'

Quoted 1 line

+7. Wait for memory controller (MIG) calibration

for the

Documentation/fpga/xrt.rst line 380

Quoted 5 lines

+8. Return the loading status back to the caller

Documentation/fpga/xrt.rst new file +4 -0
+
+`Platform Loading Overview <https://xilinx.github.io/XRT/master/html/platforms_partitions.html>`_
+provides more detailed information on platform loading.
+

the link works.

Documentation/fpga/xrt.rst line 385

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+
+xsabin
+------
+
+Each Alveo platform comes packaged with its own xsabin. The xsabin is trusted

is a trusted

Documentation/fpga/xrt.rst line 388

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+component of the platform. For format details refer to :ref:`xsabin_xclbin_container_format`
+below. xsabin contains basic information like UUIDs, platform name and metadata in the
+form of device tree. See :ref:`device_tree_usage` below for details and example.

of a device

Documentation/fpga/xrt.rst line 394

Quoted 6 lines
Documentation/fpga/xrt.rst new file +6 -0
+
+xclbin
+------
+
+xclbin is compiled by end user using
+`Vitis <https://www.xilinx.com/products/design-tools/vitis/vitis-platform.html>`_

this link works, seems reasonable landing

Documentation/fpga/xrt.rst line 397

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+tool set from Xilinx. The xclbin contains sections describing user compiled
+acceleration engines/kernels, memory subsystems, clocking information etc. It also
+contains bitstream for the user partition, UUIDs, platform name, etc. xclbin uses

bitstreams

Quoted 37 lines
Documentation/fpga/xrt.rst new file +37 -0
+the same container format as xsabin which is described below.
+
+
+.. _xsabin_xclbin_container_format:
+
+xsabin/xclbin Container Format
+------------------------------
+
+xclbin/xsabin is ELF-like binary container format. It is structured as series of
+sections. There is a file header followed by several section headers which is
+followed by sections. A section header points to an actual section. There is an
+optional signature at the end. The format is defined by header file ``xclbin.h``.
+The following figure illustrates a typical xclbin::
+
+
+           +---------------------+
+           |                     |
+           |       HEADER        |
+           +---------------------+
+           |   SECTION  HEADER   |
+           |                     |
+           +---------------------+
+           |         ...         |
+           |                     |
+           +---------------------+
+           |   SECTION  HEADER   |
+           |                     |
+           +---------------------+
+           |       SECTION       |
+           |                     |
+           +---------------------+
+           |         ...         |
+           |                     |
+           +---------------------+
+           |       SECTION       |
+           |                     |
+           +---------------------+

Documentation/fpga/xrt.rst line 444

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+           |      SIGNATURE      |
+           |      (OPTIONAL)     |
+           +---------------------+
+
+
+xclbin/xsabin files can be packaged, un-packaged and inspected using XRT utility
+called **xclbinutil**. xclbinutil is part of XRT open source software stack. The
+source code for xclbinutil can be found at
+https://github.com/Xilinx/XRT/tree/master/src/runtime_src/tools/xclbinutil
+

Works, but maybe the location of a manpage or doc would be better.

Quoted 314 lines
Documentation/fpga/xrt.rst new file +314 -0
+For example to enumerate the contents of a xclbin/xsabin use the *--info* switch
+as shown below::
+
+
+  xclbinutil --info --input /opt/xilinx/firmware/u50/gen3x16-xdma/blp/test/bandwidth.xclbin
+  xclbinutil --info --input /lib/firmware/xilinx/862c7020a250293e32036f19956669e5/partition.xsabin
+
+
+.. _device_tree_usage:
+
+Device Tree Usage
+-----------------
+
+As mentioned previously xsabin stores metadata which advertise HW subsystems present
+in a partition. The metadata is stored in device tree format with well defined schema.
+XRT management driver uses this information to bind *platform drivers* to the subsystem
+instantiations. The platform drivers are found in **xrt-lib.ko** kernel module defined
+later.
+
+Logic UUID
+^^^^^^^^^^
+A partition is identified uniquely through ``logic_uuid`` property::
+
+  /dts-v1/;
+  / {
+      logic_uuid = "0123456789abcdef0123456789abcdef";
+      ...
+    }
+
+Schema Version
+^^^^^^^^^^^^^^
+Schema version is defined through ``schema_version`` node. And it contains ``major``
+and ``minor`` properties as below::
+
+  /dts-v1/;
+  / {
+       schema_version {
+           major = <0x01>;
+           minor = <0x00>;
+       };
+       ...
+    }
+
+Partition UUIDs
+^^^^^^^^^^^^^^^
+As said earlier, each partition may have parent and child UUIDs. These UUIDs are
+defined by ``interfaces`` node and ``interface_uuid`` property::
+
+  /dts-v1/;
+  / {
+       interfaces {
+           @0 {
+                  interface_uuid = "0123456789abcdef0123456789abcdef";
+           };
+           @1 {
+                  interface_uuid = "fedcba9876543210fedcba9876543210";
+           };
+           ...
+        };
+       ...
+    }
+
+
+Subsystem Instantiations
+^^^^^^^^^^^^^^^^^^^^^^^^
+Subsystem instantiations are captured as children of ``addressable_endpoints``
+node::
+
+  /dts-v1/;
+  / {
+       addressable_endpoints {
+           abc {
+               ...
+           };
+           def {
+               ...
+           };
+           ...
+       }
+  }
+
+Subnode 'abc' and 'def' are the name of subsystem nodes
+
+Subsystem Node
+^^^^^^^^^^^^^^
+Each subsystem node and its properties define a hardware instance::
+
+
+  addressable_endpoints {
+      abc {
+          reg = <0xa 0xb>
+          pcie_physical_function = <0x0>;
+          pcie_bar_mapping = <0x2>;
+          compatible = "abc def";
+          firmware {
+              firmware_product_name = "abc"
+              firmware_branch_name = "def"
+              firmware_version_major = <1>
+              firmware_version_minor = <2>
+          };
+      }
+      ...
+  }
+
+:reg:
+ Property defines address range. '<0xa 0xb>' is BAR offset and length pair, both
+ are 64-bit integer.
+:pcie_physical_function:
+ Property specifies which PCIe physical function the subsystem node resides.
+:pcie_bar_mapping:
+ Property specifies which PCIe BAR the subsystem node resides. '<0x2>' is BAR
+ index and it is 0 if this property is not defined.
+:compatible:
+ Property is a list of strings. The first string in the list specifies the exact
+ subsystem node. The following strings represent other devices that the device
+ is compatible with.
+:firmware:
+ Subnode defines the firmware required by this subsystem node.
+
+Alveo U50 Platform Example
+^^^^^^^^^^^^^^^^^^^^^^^^^^
+::
+
+  /dts-v1/;
+
+  /{
+        logic_uuid = "f465b0a3ae8c64f619bc150384ace69b";
+
+        schema_version {
+                major = <0x01>;
+                minor = <0x00>;
+        };
+
+        interfaces {
+
+                @0 {
+                        interface_uuid = "862c7020a250293e32036f19956669e5";
+                };
+        };
+
+        addressable_endpoints {
+
+                ep_blp_rom_00 {
+                        reg = <0x00 0x1f04000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_bram_ctrl-1.0\0axi_bram_ctrl";
+                };
+
+                ep_card_flash_program_00 {
+                        reg = <0x00 0x1f06000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_quad_spi-1.0\0axi_quad_spi";
+                        interrupts = <0x03 0x03>;
+                };
+
+                ep_cmc_firmware_mem_00 {
+                        reg = <0x00 0x1e20000 0x00 0x20000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_bram_ctrl-1.0\0axi_bram_ctrl";
+
+                        firmware {
+                                firmware_product_name = "cmc";
+                                firmware_branch_name = "u50";
+                                firmware_version_major = <0x01>;
+                                firmware_version_minor = <0x00>;
+                        };
+                };
+
+                ep_cmc_intc_00 {
+                        reg = <0x00 0x1e03000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_intc-1.0\0axi_intc";
+                        interrupts = <0x04 0x04>;
+                };
+
+                ep_cmc_mutex_00 {
+                        reg = <0x00 0x1e02000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_gpio-1.0\0axi_gpio";
+                };
+
+                ep_cmc_regmap_00 {
+                        reg = <0x00 0x1e08000 0x00 0x2000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_bram_ctrl-1.0\0axi_bram_ctrl";
+
+                        firmware {
+                                firmware_product_name = "sc-fw";
+                                firmware_branch_name = "u50";
+                                firmware_version_major = <0x05>;
+                        };
+                };
+
+                ep_cmc_reset_00 {
+                        reg = <0x00 0x1e01000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_gpio-1.0\0axi_gpio";
+                };
+
+                ep_ddr_mem_calib_00 {
+                        reg = <0x00 0x63000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_gpio-1.0\0axi_gpio";
+                };
+
+                ep_debug_bscan_mgmt_00 {
+                        reg = <0x00 0x1e90000 0x00 0x10000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-debug_bridge-1.0\0debug_bridge";
+                };
+
+                ep_ert_base_address_00 {
+                        reg = <0x00 0x21000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_gpio-1.0\0axi_gpio";
+                };
+
+                ep_ert_command_queue_mgmt_00 {
+                        reg = <0x00 0x40000 0x00 0x10000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-ert_command_queue-1.0\0ert_command_queue";
+                };
+
+                ep_ert_command_queue_user_00 {
+                        reg = <0x00 0x40000 0x00 0x10000>;
+                        pcie_physical_function = <0x01>;
+                        compatible = "xilinx.com,reg_abs-ert_command_queue-1.0\0ert_command_queue";
+                };
+
+                ep_ert_firmware_mem_00 {
+                        reg = <0x00 0x30000 0x00 0x8000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_bram_ctrl-1.0\0axi_bram_ctrl";
+
+                        firmware {
+                                firmware_product_name = "ert";
+                                firmware_branch_name = "v20";
+                                firmware_version_major = <0x01>;
+                        };
+                };
+
+                ep_ert_intc_00 {
+                        reg = <0x00 0x23000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_intc-1.0\0axi_intc";
+                        interrupts = <0x05 0x05>;
+                };
+
+                ep_ert_reset_00 {
+                        reg = <0x00 0x22000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_gpio-1.0\0axi_gpio";
+                };
+
+                ep_ert_sched_00 {
+                        reg = <0x00 0x50000 0x00 0x1000>;
+                        pcie_physical_function = <0x01>;
+                        compatible = "xilinx.com,reg_abs-ert_sched-1.0\0ert_sched";
+                        interrupts = <0x09 0x0c>;
+                };
+
+                ep_fpga_configuration_00 {
+                        reg = <0x00 0x1e88000 0x00 0x8000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_hwicap-1.0\0axi_hwicap";
+                        interrupts = <0x02 0x02>;
+                };
+
+                ep_icap_reset_00 {
+                        reg = <0x00 0x1f07000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_gpio-1.0\0axi_gpio";
+                };
+
+                ep_msix_00 {
+                        reg = <0x00 0x00 0x00 0x20000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-msix-1.0\0msix";
+                        pcie_bar_mapping = <0x02>;
+                };
+
+                ep_pcie_link_mon_00 {
+                        reg = <0x00 0x1f05000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_gpio-1.0\0axi_gpio";
+                };
+
+                ep_pr_isolate_plp_00 {
+                        reg = <0x00 0x1f01000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_gpio-1.0\0axi_gpio";
+                };
+
+                ep_pr_isolate_ulp_00 {
+                        reg = <0x00 0x1000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_gpio-1.0\0axi_gpio";
+                };
+
+                ep_uuid_rom_00 {
+                        reg = <0x00 0x64000 0x00 0x1000>;
+                        pcie_physical_function = <0x00>;
+                        compatible = "xilinx.com,reg_abs-axi_bram_ctrl-1.0\0axi_bram_ctrl";
+                };
+
+                ep_xdma_00 {
+                        reg = <0x00 0x00 0x00 0x10000>;
+                        pcie_physical_function = <0x01>;
+                        compatible = "xilinx.com,reg_abs-xdma-1.0\0xdma";
+                        pcie_bar_mapping = <0x02>;
+                };
+        };
+
+  }

Documentation/fpga/xrt.rst line 768

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+
+
+
+Deployment Models
+=================
+
+Baremetal
+---------
+
+In bare-metal deployments both MPF and UPF are visible and accessible. xmgmt

In bare-meta deployments,

Quoted 24 lines
Documentation/fpga/xrt.rst new file +24 -0
+driver binds to MPF. xmgmt driver operations are privileged and available to
+system administrator. The full stack is illustrated below::
+
+                            HOST
+
+                 [XMGMT]            [XUSER]
+                    |                  |
+                    |                  |
+                 +-----+            +-----+
+                 | MPF |            | UPF |
+                 |     |            |     |
+                 | PF0 |            | PF1 |
+                 +--+--+            +--+--+
+          ......... ^................. ^..........
+                    |                  |
+                    |   PCIe DEVICE    |
+                    |                  |
+                 +--+------------------+--+
+                 |         SHELL          |
+                 |                        |
+                 +------------------------+
+                 |         USER           |
+                 |                        |
+                 |                        |

Documentation/fpga/xrt.rst line 802

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+                 |                        |
+                 |                        |
+                 +------------------------+
+
+
+
+Virtualized
+-----------
+
+In virtualized deployments privileged MPF is assigned to host but unprivileged

In virtualized deployments, the

Quoted 1 line

+UPF is assigned to guest VM via PCIe pass-through. xmgmt driver in host binds

in the host

Quoted 1 line

+to MPF. xmgmt driver operations are privileged and only accessible by hosting

to the MPF

Quoted 28 lines
Documentation/fpga/xrt.rst new file +28 -0
+service provider. The full stack is illustrated below::
+
+
+                                 .............
+                  HOST           .    VM     .
+                                 .           .
+                 [XMGMT]         .  [XUSER]  .
+                    |            .     |     .
+                    |            .     |     .
+                 +-----+         .  +-----+  .
+                 | MPF |         .  | UPF |  .
+                 |     |         .  |     |  .
+                 | PF0 |         .  | PF1 |  .
+                 +--+--+         .  +--+--+  .
+          ......... ^................. ^..........
+                    |                  |
+                    |   PCIe DEVICE    |
+                    |                  |
+                 +--+------------------+--+
+                 |         SHELL          |
+                 |                        |
+                 +------------------------+
+                 |         USER           |
+                 |                        |
+                 |                        |
+                 |                        |
+                 |                        |
+                 +------------------------+

Documentation/fpga/xrt.rst line 842

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+
+
+
+
+
+Platform Security Considerations
+================================
+
+`Security of Alveo Platform <https://xilinx.github.io/XRT/master/html/security.html>`_
+discusses the deployment options and security implications in great detail.

This link works and looks great.

Tom

#3 lore raw

RE: [PATCH V3 XRT Alveo 01/18] Documentation: fpga: Add a document describing XRT Alveo drivers

Hello Tom,

Quoted 12 lines

-----Original Message-----
From: Tom Rix <[email protected]>
Sent: Friday, February 19, 2021 2:26 PM
To: Lizhi Hou <[email protected]>; [email protected]
Cc: Lizhi Hou <[email protected]>; [email protected]; Max Zhen <[email protected]>; Sonal Santan <[email protected]>; Michal Simek <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]; [email protected]; [email protected]; Max Zhen <[email protected]>
Subject: Re: [PATCH V3 XRT Alveo 01/18] Documentation: fpga: Add a document describing XRT Alveo drivers

Quoted 4 lines

From the documentation, there are a couple of big questions and a bunch of word smithing.

pseudo-bus : do we need a bus ?

We are looking for guidance here.

Quoted 4 lines

xrt-lib real platform devices that aren't fpga, do they need to move to another subsystem ?

Drivers for the IPs that show up in the Alveo shell are not generic enough. They fit into the framework that XRT uses. Is the idea that that they can be used in a different context?

Quoted 67 lines

Overall looks good, love the ascii art!

Quoted 9 lines · Lizhi Hou

Describe XRT driver architecture and provide basic overview of Xilinx Alveo platform.

---
 Documentation/fpga/index.rst |   1 +
 Documentation/fpga/xrt.rst   | 842

+++++++++++++++++++++++++++++++++++

Quoted 29 lines
 2 files changed, 843 insertions(+)
 create mode 100644 Documentation/fpga/xrt.rst
(quoted diff) +0 -0

b/Documentation/fpga/index.rst index f80f95667ca2..30134357b70d 100644

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 +9 -0
new file mode 100644 index 000000000000..9bc2d2785cb9
@@ -0,0 +1,842 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==================================
+XRTV2 Linux Kernel Driver Overview
+==================================
+
+Authors:
+
+* Sonal Santan <[email protected]>

Documentation/fpga/xrt.rst line 19

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+* Max Zhen <[email protected]>
+* Lizhi Hou <[email protected]>
+
+XRTV2 drivers are second generation `XRT
+<https://github.com/Xilinx/XRT>`_ drivers which support `Alveo
+<https://www.xilinx.com/products/boards-and-kits/alveo.html>`_
+PCIe platforms from Xilinx.
+
+XRTV2 drivers support *subsystem* style data driven platforms where
+driver's

where the driver's

Documentation/fpga/xrt.rst line 27

Quoted 8 lines
Documentation/fpga/xrt.rst new file +8 -0
+configuration and behavior is determined by meta data provided by the
+platform (in *device tree* format). Primary management physical
+function (MPF) driver is called **xmgmt**. Primary user physical
+function (UPF) driver is called
+**xuser** and is under development. xrt driver framework and HW
+subsystem drivers are packaged into a library module called
+**xrt-lib**, which is shared by **xmgmt** and **xuser** (under
+development). The xrt driver framework

xuser still under development ?

Documentation/fpga/xrt.rst line 29

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+implements a pseudo-bus which is used to discover HW subsystems and
+facilitate

A pseudo-bus.

Quoted 5 lines

It would be good if this was close to what was done for dfl here

https://lore.kernel.org/linux-fpga/1605159759-3439-1-git-send-email-
[email protected]/

I am wondering if we can phase in the migration to formal bus architecture based on struct bus_type as a follow on set of patches?

Quoted 13 lines

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+inter HW subsystem interaction.
+
+Driver Modules
+==============
+
+xrt-lib.ko
+----------
+
+Repository of all subsystem drivers and pure software modules that
+can potentially

subsystem drivers

Quoted 5 lines

drivers in fpga/ should be for managing just the fpganess of the fpga.

soft devices ex/ a soft tty should go to their respective subsystem location

Are there any in this patchset you think might move ?

We have already shrunk the patch to only include FPGA centric pieces necessary to get the bitstream download implemented. Should we explore the question of subsystem drivers when we add support for more features of the Alveo shell?

Quoted 12 lines

Maybe we can defer reviewing those now.

Documentation/fpga/xrt.rst line 43

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+be shared between xmgmt and xuser. All these drivers are structured
+as Linux *platform driver* and are instantiated by xmgmt (or xuser
+under development) based on meta data associated with hardware. The
+metadata is in the form of device tree

with the hardware

form of a device tree

Will change

Quoted 4 lines

Documentation/fpga/xrt.rst line 46

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+as mentioned before. Each platform driver statically defines a
+subsystem node array by using node name or a string in its
+``compatible`` property. And this array is eventually translated to IOMEM

resources of the platform device.

Quoted 8 lines

Documentation/fpga/xrt.rst line 51

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+
+The xrt-lib core infrastructure provides hooks to platform drivers
+for device node management, user file operations and ioctl callbacks.
+The core also provides pseudo-bus functionality for platform driver
+registration, discovery and inter platform driver ioctl calls.

core infrastructure.

Will update.

Quoted 4 lines

The interfaces to the infrastructure are not in include/linux/fpga/

Maybe this needs to change.

Were you thinking of moving XRT infrastructure header files from drivers/fpga/xrt/include to include/linux/fpga?

Quoted 26 lines

Documentation/fpga/xrt.rst

Quoted 12 lines
Documentation/fpga/xrt.rst new file +12 -0
+
+.. note::
+   See code in ``include/xleaf.h``
+
+
+xmgmt.ko
+--------
+
+The xmgmt driver is a PCIe device driver driving MPF found on
+Xilinx's Alveo PCIE device. It consists of one *root* driver, one or
+more *group* drivers and one or more *xleaf* drivers. The root and
+MPF specific xleaf drivers are in xmgmt.ko. The group driver and other xleaf

drivers are in xrt-lib.ko.
I am not sure if *.ko is correct, these will also be intree.

Documentation/fpga/xrt.rst line 66

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+
+The instantiation of specific group driver or xleaf driver is
+completely data

of a specific

Documentation/fpga/xrt.rst line 68

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+driven based on meta data (mostly in device tree format) found
+through VSEC

mostly ? what is the deviation from device tree ?

Documentation/fpga/xrt.rst line 69

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+capability and inside firmware files, such as platform xsabin or user xclbin

file.

Documentation/fpga/xrt.rst line 71

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+The root driver manages life cycle of multiple group drivers, which,
+in turn,

the life cycle

Quoted 7 lines

Documentation/fpga/xrt.rst line 73

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+manages multiple xleaf drivers. This allows a single set of driver
+code to support

set of drivers

drop 'code'

Will update

Quoted 111 lines
Quoted 47 lines
Documentation/fpga/xrt.rst new file +47 -0
+all kinds of subsystems exposed by different shells. The difference
+among all these subsystems will be handled in xleaf drivers with root
+and group drivers being part of the infrastructure and provide common
+services for all leaves found on all platforms.
+
+The driver object model looks like the following::
+
+                    +-----------+
+                    |   xroot   |
+                    +-----+-----+
+                          |
+              +-----------+-----------+
+              |                       |
+              v                       v
+        +-----------+          +-----------+
+        |   group   |    ...   |   group   |
+        +-----+-----+          +------+----+
+              |                       |
+              |                       |
+        +-----+----+            +-----+----+
+        |          |            |          |
+        v          v            v          v
+    +-------+  +-------+    +-------+  +-------+
+    | xleaf |..| xleaf |    | xleaf |..| xleaf |
+    +-------+  +-------+    +-------+  +-------+
+
+As an example for Xilinx Alveo U50 before user xclbin download, the
+tree looks like the following::
+
+                                +-----------+
+                                |   xmgmt   |
+                                +-----+-----+
+                                      |
+            +-------------------------+--------------------+
+            |                         |                    |
+            v                         v                    v
+       +--------+                +--------+            +--------+
+       | group0 |                | group1 |            | group2 |
+       +----+---+                +----+---+            +---+----+
+            |                         |                    |
+            |                         |                    |
+      +-----+-----+        +----+-----+---+    +-----+-----+----+--------+
+      |           |        |    |         |    |     |          |        |
+      v           v        |    v         v    |     v          v        |
+ +------------+  +------+  | +------+ +------+ |  +------+
+ +------------+ +-----------+ |
+ | xmgmt_main |  | VSEC |  | | GPIO | | QSPI | |  |  CMC | |

Documentation/fpga/xrt.rst line 116

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+ | AXI-GATE0 | |
+ +------------+  +------+  | +------+ +------+ |  +------+
+ +------------+ +-----------+ |
+                           | +---------+       |  +------+ +-----------+ |
+                           +>| MAILBOX |       +->| ICAP | | AXI-GATE1 |<+
+                             +---------+       |  +------+ +-----------+
+                                               |  +-------+
+                                               +->| CALIB |
+                                                  +-------+
+

Nice ascii art!

Quoted 11 lines
Documentation/fpga/xrt.rst new file +11 -0
+After an xclbin is download, group3 will be added and the tree looks
+like the
+following::
+
+                                +-----------+
+                                |   xmgmt   |
+                                +-----+-----+
+                                      |
+            +-------------------------+--------------------+-----------------+
+            |                         |                    |                 |
+            v                         v                    v                 |

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+       +--------+                +--------+            +--------+            |
+       | group0 |                | group1 |            | group2 |            |
+       +----+---+                +----+---+            +---+----+            |
+            |                         |                    |                 |
+            |                         |                    |                 |
+      +-----+-----+       +-----+-----+---+    +-----+-----+----+--------+   |
+      |           |       |     |         |    |     |          |        |   |
+      v           v       |     v         v    |     v          v        |   |
+ +------------+  +------+ | +------+ +------+  |  +------+ +-----------+ |   |
+ | xmgmt_main |  | VSEC | | | GPIO | | QSPI |  |  |  CMC | | AXI-GATE0 | |

|

Quoted 31 lines
Documentation/fpga/xrt.rst new file +31 -0
+ +------------+  +------+ | +------+ +------+  |  +------+ +-----------+ |   |
+                          | +---------+        |  +------+ +-----------+ |   |
+                          +>| MAILBOX |        +->| ICAP | | AXI-GATE1 |<+   |
+                            +---------+        |  +------+ +-----------+     |
+                                               |  +-------+                  |
+                                               +->| CALIB |                  |
+                                                  +-------+                  |
+                      +---+----+                                             |
+                      | group3 |<--------------------------------------------+
+                      +--------+
+                          |
+                          |
+     +-------+--------+---+--+--------+------+-------+
+     |       |        |      |        |      |       |
+     v       |        v      |        v      |       v
+ +--------+  |   +--------+  |   +--------+  |    +-----+
+ | CLOCK0 |  |   | CLOCK1 |  |   | CLOCK2 |  |    | UCS |
+ +--------+  v   +--------+  v   +--------+  v    +-----+
+ +-------------+ +-------------+ +-------------+
+ | CLOCK-FREQ0 | | CLOCK-FREQ1 | | CLOCK-FREQ2 |
+ +-------------+ +-------------+ +-------------+
+
+
+xmgmt-root
+^^^^^^^^^^
+
+The xmgmt-root driver is a PCIe device driver attached to MPF. It's
+part of the infrastructure of the MPF driver and resides in xmgmt.ko.
+This driver
+
+* manages one or more group drivers
Quoted 8 lines

Documentation/fpga/xrt.rst line 189

Quoted 7 lines
Documentation/fpga/xrt.rst new file +7 -0
+* provides access to functionalities that requires pci_dev, such as
+PCIE config
+  space access, to other xleaf drivers through root calls
+* together with group driver, facilities event callbacks for other
+xleaf drivers
+* together with group driver, facilities inter-leaf driver calls for
+other xleaf

Maybe drop 'together with group driver'

Will update

Quoted 8 lines

Documentation/fpga/xrt.rst line 196

Quoted 7 lines
Documentation/fpga/xrt.rst new file +7 -0
+  drivers
+
+When root driver starts, it will explicitly create an initial group
+instance, which contains xleaf drivers that will trigger the creation
+of other group instances. The root driver will wait for all group and
+leaves to be created before it returns from it's probe routine and
+claim success of the initialization of the entire xmgmt driver.

What happens if there a failure in one leaf ? Does the whole board go down ?

Quoted 11 lines

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+
+.. note::
+   See code in ``lib/xroot.c`` and ``mgmt/root.c``
+
+
+group
+^^^^^
+
+The group driver is a platform device driver whose life cycle is
+managed by

Maybe call this a 'pseudo device'

Will update

Quoted 9 lines

Documentation/fpga/xrt.rst line 213

Quoted 7 lines
Documentation/fpga/xrt.rst new file +7 -0
+root and does not have real IO mem or IRQ resources. It's part of the
+infrastructure of the MPF driver and resides in xrt-lib.ko. This
+driver
+
+* manages one or more xleaf drivers so that multiple leaves can be
+managed as a
+  group

can drop 'so that multiple leaves can be managed as a group' to me, this is the same as 'one or more'

Will do

Quoted 7 lines

Documentation/fpga/xrt.rst line 219

Quoted 6 lines
Documentation/fpga/xrt.rst new file +6 -0
+* provides access to root from leaves, so that root calls, event
+notifications
+  and inter-leaf calls can happen
+
+In xmgmt, an initial group driver instance will be created by root,
+which

by the root

Quoted 11 lines

Documentation/fpga/xrt.rst line 227

Quoted 8 lines
Documentation/fpga/xrt.rst new file +8 -0
+contains leaves that will trigger group instances to be created to
+manage groups of leaves found on different partitions on hardware,
+such as VSEC, Shell, and User.
+
+Every *fpga_region* has a group object associated with it. The group
+is created when xclbin image is loaded on the fpga_region. The
+existing group is destroyed when a new xclbin image is loaded. The
+fpga_region persists across xclbin downloads.

The connection of a 'group' node to a fpga region region is fairly important, maybe move this section earlier. 'group' as an fpganess thing would be kept in fpga/ subsystem.

Will update

Quoted 8 lines
Quoted 8 lines
Documentation/fpga/xrt.rst new file +8 -0
+
+.. note::
+   See code in ``lib/group.c``
+
+
+xleaf
+^^^^^
+
Quoted 9 lines

Documentation/fpga/xrt.rst line 240

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+The xleaf driver is a platform device driver whose life cycle is
+managed by a group driver and may or may not have real IO mem or IRQ
+resources. They are the real meat of xmgmt and contains platform
+specific code to Shell and User found on a MPF.
+

Maybe a split is pseudo device leaves, those without real IO mem, stay in fpga/  others go ?

This goes back to the earlier question of what minimal set of platform drivers should stay in fpga subsystem. There are some like bridge or configuration engine (also called icap) which have their own IO mem but do not have a life outside of fpga subsystem.

Quoted 11 lines

Documentation/fpga/xrt.rst line 244

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+A xleaf driver may not have real hardware resources when it merely
+acts as a driver that manages certain in-memory states for xmgmt.
+These in-memory states could be shared by multiple other leaves.
+

This implies locking and some message passing.

Documentation/fpga/xrt.rst line 246

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+Leaf drivers assigned to specific hardware resources drive specific
+subsystem in

drive a specific

Documentation/fpga/xrt.rst line 248

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+the device. To manipulate the subsystem or carry out a task, a xleaf
+driver may ask help from root via root calls and/or from other leaves via

inter-leaf calls.

Quoted 6 lines

Documentation/fpga/xrt.rst line 252

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+
+A xleaf can also broadcast events through infrastructure code for
+other leaves to process. It can also receive event notification from
+infrastructure about certain events, such as post-creation or pre-exit of a

particular xleaf.
I would like to see some examples of how the inter node communications work.

Would update to show an example.

Quoted 14 lines

Documentation/fpga/xrt.rst

Quoted 13 lines
Documentation/fpga/xrt.rst new file +13 -0
+
+.. note::
+   See code in ``lib/xleaf/*.c``
+
+
+FPGA Manager Interaction
+========================
+
+fpga_manager
+------------
+
+An instance of fpga_manager is created by xmgmt_main and is used for
+xclbin

for the xclbin

Quoted 7 lines

Documentation/fpga/xrt.rst line 267

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+image download. fpga_manager requires the full xclbin image before it
+can start programming the FPGA configuration engine via ICAP platform

driver.

via the ICAP

what is ICAP ?

Will update. ICAP stands for Internal Configuration Access Port used for configuring the fpga.

Quoted 47 lines

Documentation/fpga/xrt.rst

Quoted 7 lines
Documentation/fpga/xrt.rst new file +6 -0
+
+fpga_region
+-----------
+
+For every interface exposed by currently loaded xclbin/xsabin in the
+*parent*

by the currently

Documentation/fpga/xrt.rst line 247

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+fpga_region a new instance of fpga_region is created like a *child* region.

fpga_region,

Documentation/fpga/xrt.rst line 276

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+The device tree of the *parent* fpga_region defines the resources for
+a new instance of fpga_bridge which isolates the parent from

and isolates

Documentation/fpga/xrt.rst line 281

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+child fpga_region. This new instance of fpga_bridge will be used when
+a xclbin image is loaded on the child fpga_region. After the xclbin
+image is downloaded to the fpga_region, an instance of group is
+created for the fpga_region using the device tree obtained as part of
+xclbin. If this device

of the xclbin

Documentation/fpga/xrt.rst line 254

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+tree defines any child interfaces then it can trigger the creation of

interfaces, then

Documentation/fpga/xrt.rst line 255

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+fpga_bridge and fpga_region for the next region in the chain.

a fpga_bridge and a fpga_region

Documentation/fpga/xrt.rst

Quoted 6 lines
Documentation/fpga/xrt.rst new file +6 -0
+
+fpga_bridge
+-----------
+
+Like fpga_region, matching fpga_bridge is also created by walking the
+device

Like the fpga_region, a matchin

Documentation/fpga/xrt.rst

Quoted 13 lines
Documentation/fpga/xrt.rst new file +13 -0
+tree of the parent group.
+
+Driver Interfaces
+=================
+
+xmgmt Driver Ioctls
+-------------------
+
+Ioctls exposed by xmgmt driver to user space are enumerated in the
+following
+table:
+
+== ===================== ============================

==========================

Documentation/fpga/xrt.rst line 304

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+#  Functionality         ioctl request code            data format
+== ===================== ============================

==========================

Quoted 9 lines

Documentation/fpga/xrt.rst line 305

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+1  FPGA image download   XMGMT_IOCICAPDOWNLOAD_AXLF

xmgmt_ioc_bitstream_axlf

Documentation/fpga/xrt.rst line 307

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+== ===================== ============================
+==========================

This data format is described below, maybe swap this section with that so

folks will know what xmgmnt_ioc_bitstream_axlf is before this section.

Will update.

Quoted 10 lines

Documentation/fpga/xrt.rst line 310

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+
+User xclbin can be downloaded by using xbmgmt tool from XRT open
+source suite. See

A user xclbin

using the xbmgmt

from the XRT

Will update

Quoted 42 lines
Quoted 23 lines
Documentation/fpga/xrt.rst new file +23 -0
+example usage below::
+
+  xbmgmt partition --program --path
+ /lib/firmware/xilinx/862c7020a250293e32036f19956669e5/test/verify.xc
+ lbin --force
+
+xmgmt Driver Sysfs
+------------------
+
+xmgmt driver exposes a rich set of sysfs interfaces. Subsystem
+platform drivers export sysfs node for every platform instance.
+
+Every partition also exports its UUIDs. See below for examples::
+
+  /sys/bus/pci/devices/0000:06:00.0/xmgmt_main.0/interface_uuids
+  /sys/bus/pci/devices/0000:06:00.0/xmgmt_main.0/logic_uuids
+
+
+hwmon
+-----
+
+xmgmt driver exposes standard hwmon interface to report voltage,
+current, temperature, power, etc. These can easily be viewed using

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+*sensors* command line utility.
+
+Alveo Platform Overview
+=======================
+
+Alveo platforms are architected as two physical FPGA partitions:
+*Shell* and *User*. The Shell provides basic infrastructure for the
+Alveo platform like PCIe connectivity, board management, Dynamic
+Function Exchange (DFX), sensors, clocking, reset, and security. User
+partition contains user compiled FPGA

the user compiled

Documentation/fpga/xrt.rst

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+binary which is loaded by a process called DFX also known as partial
+reconfiguration.
+
+Physical partitions require strict HW compatibility with each other
+for DFX to work properly.

swap order

Quoted 3 lines

For DFX to work properly physical partitions ..

Will update

Quoted 11 lines

Documentation/fpga/xrt.rst

Quoted 4 lines

Every physical partition has two interface UUIDs: *parent* UUID

Documentation/fpga/xrt.rst new file +3 -0
+and *child* UUID. For simple single stage platforms, Shell → User
+forms parent child relationship. For complex two stage platforms,
+Base → Shell → User forms the parent child relationship chain.

this bit is confusing. is this related to uuid?

Documentation/fpga/xrt.rst

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+
+.. note::
+   Partition compatibility matching is key design component of Alveo

platforms

Documentation/fpga/xrt.rst

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+   and XRT. Partitions have child and parent relationship. A loaded
+partition
Quoted 7 lines

have a child

Quoted 2 lines

+ exposes child partition UUID to advertise its compatibility
+ requirement for

the child's

can drop 'for child partition'

Will update

Quoted 11 lines
Quoted 3 lines

+ child partition. When loading a child partition the xmgmt
+ management driver

When loading a child partition,

Documentation/fpga/xrt.rst

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+   matches parent UUID of the child partition against child UUID exported by
+   the parent. Parent and child partition UUIDs are stored in the *xclbin*
+   (for user) or *xsabin* (for base and shell).

this is confusing, is this part of the file image format ?

Maybe save/move till the image layout.

Yes these IDs are stored in xclbin image format. Will move the sections around as suggested.

Quoted 6 lines
Quoted 3 lines

Except for root UUID, VSEC,
+ hardware itself does not know about UUIDs. UUIDs are stored in xsabin

and

Quoted 1 line

+ xclbin.

This is confusing too, not sure how to untangle.

Will reword.

Quoted 27 lines
Quoted 16 lines
Documentation/fpga/xrt.rst new file +16 -0
+
+
+The physical partitions and their loading is illustrated below::
+
+           SHELL                               USER
+        +-----------+                  +-------------------+
+        |           |                  |                   |
+        | VSEC UUID | CHILD     PARENT |    LOGIC UUID     |
+        |           o------->|<--------o                   |
+        |           | UUID       UUID  |                   |
+        +-----+-----+                  +--------+----------+
+              |                                 |
+              .                                 .
+              |                                 |
+          +---+---+                      +------+--------+
+          |  POR  |                      | USER COMPILED |

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+          | FLASH |                      |    XCLBIN     |
+          +-------+                      +---------------+
+
+
+Loading Sequence
+----------------
+
+The Shell partition is loaded from flash at system boot time. It
+establishes the PCIe link and exposes two physical functions to the
+BIOS. After OS boot, xmgmt

the OS boots, the xmgmt

Quoted 9 lines

Documentation/fpga/xrt.rst

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+driver attaches to PCIe physical function 0 exposed by the Shell and
+then looks for VSEC in PCIe extended configuration space. Using VSEC
+it determines the logic

the PCIe

2 lines not shown · raw message

551 lines not shown · raw message

Part of this message is not shown here. View the raw message.

maintainer
#4 lore raw

Re: [PATCH V3 XRT Alveo 01/18] Documentation: fpga: Add a document describing XRT Alveo drivers

Quoted 95 lines · Sonal Santan

Hello Tom,

Quoted 12 lines

-----Original Message-----
From: Tom Rix <[email protected]>
Sent: Friday, February 19, 2021 2:26 PM
To: Lizhi Hou <[email protected]>; [email protected]
Cc: Lizhi Hou <[email protected]>; [email protected]; Max Zhen <[email protected]>; Sonal Santan <[email protected]>; Michal Simek <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]; [email protected]; [email protected]; Max Zhen <[email protected]>
Subject: Re: [PATCH V3 XRT Alveo 01/18] Documentation: fpga: Add a document describing XRT Alveo drivers

Quoted 4 lines

From the documentation, there are a couple of big questions and a bunch of word smithing.

pseudo-bus : do we need a bus ?

We are looking for guidance here.

Quoted 4 lines

xrt-lib real platform devices that aren't fpga, do they need to move to another subsystem ?

Drivers for the IPs that show up in the Alveo shell are not generic enough. They fit into the framework that XRT uses. Is the idea that that they can be used in a different context?

Quoted 67 lines

Overall looks good, love the ascii art!

Quoted 9 lines · Lizhi Hou

Describe XRT driver architecture and provide basic overview of Xilinx Alveo platform.

---
 Documentation/fpga/index.rst |   1 +
 Documentation/fpga/xrt.rst   | 842

+++++++++++++++++++++++++++++++++++

Quoted 29 lines
 2 files changed, 843 insertions(+)
 create mode 100644 Documentation/fpga/xrt.rst
(quoted diff) +0 -0

b/Documentation/fpga/index.rst index f80f95667ca2..30134357b70d 100644

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 +9 -0
new file mode 100644 index 000000000000..9bc2d2785cb9
@@ -0,0 +1,842 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==================================
+XRTV2 Linux Kernel Driver Overview
+==================================
+
+Authors:
+
+* Sonal Santan <[email protected]>

Documentation/fpga/xrt.rst line 19

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+* Max Zhen <[email protected]>
+* Lizhi Hou <[email protected]>
+
+XRTV2 drivers are second generation `XRT
+<https://github.com/Xilinx/XRT>`_ drivers which support `Alveo
+<https://www.xilinx.com/products/boards-and-kits/alveo.html>`_
+PCIe platforms from Xilinx.
+
+XRTV2 drivers support *subsystem* style data driven platforms where
+driver's

where the driver's

Documentation/fpga/xrt.rst line 27

Quoted 8 lines
Documentation/fpga/xrt.rst new file +8 -0
+configuration and behavior is determined by meta data provided by the
+platform (in *device tree* format). Primary management physical
+function (MPF) driver is called **xmgmt**. Primary user physical
+function (UPF) driver is called
+**xuser** and is under development. xrt driver framework and HW
+subsystem drivers are packaged into a library module called
+**xrt-lib**, which is shared by **xmgmt** and **xuser** (under
+development). The xrt driver framework

xuser still under development ?

Documentation/fpga/xrt.rst line 29

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+implements a pseudo-bus which is used to discover HW subsystems and
+facilitate

A pseudo-bus.

Quoted 8 lines
Quoted 5 lines

It would be good if this was close to what was done for dfl here

https://lore.kernel.org/linux-fpga/1605159759-3439-1-git-send-email-
[email protected]/

I am wondering if we can phase in the migration to formal bus architecture based on struct bus_type as a follow on set of patches?

I'd rather have it done early on. If we know there's a better way of doing something and we don't the code should go into staging.

This gives us an out if it doesn't happen, otherwise the kernel community would depend on corporate goodwill to appropriately staff it.

Note, I'm not trying to say there's any ill will anywhere, Xilinx has been traditionally good about this :)

Quoted 1,096 lines
Quoted 14 lines

Documentation/fpga/xrt.rst

Quoted 11 lines
Documentation/fpga/xrt.rst new file +10 -0
+inter HW subsystem interaction.
+
+Driver Modules
+==============
+
+xrt-lib.ko
+----------
+
+Repository of all subsystem drivers and pure software modules that
+can potentially

subsystem drivers

Quoted 5 lines

drivers in fpga/ should be for managing just the fpganess of the fpga.

soft devices ex/ a soft tty should go to their respective subsystem location

Are there any in this patchset you think might move ?

We have already shrunk the patch to only include FPGA centric pieces necessary to get the bitstream download implemented. Should we explore the question of subsystem drivers when we add support for more features of the Alveo shell?

Quoted 12 lines

Maybe we can defer reviewing those now.

Documentation/fpga/xrt.rst line 43

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+be shared between xmgmt and xuser. All these drivers are structured
+as Linux *platform driver* and are instantiated by xmgmt (or xuser
+under development) based on meta data associated with hardware. The
+metadata is in the form of device tree

with the hardware

form of a device tree

Will change

Quoted 4 lines

Documentation/fpga/xrt.rst line 46

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+as mentioned before. Each platform driver statically defines a
+subsystem node array by using node name or a string in its
+``compatible`` property. And this array is eventually translated to IOMEM

resources of the platform device.

Quoted 8 lines

Documentation/fpga/xrt.rst line 51

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+
+The xrt-lib core infrastructure provides hooks to platform drivers
+for device node management, user file operations and ioctl callbacks.
+The core also provides pseudo-bus functionality for platform driver
+registration, discovery and inter platform driver ioctl calls.

core infrastructure.

Will update.

Quoted 4 lines

The interfaces to the infrastructure are not in include/linux/fpga/

Maybe this needs to change.

Were you thinking of moving XRT infrastructure header files from drivers/fpga/xrt/include to include/linux/fpga?

Quoted 26 lines

Documentation/fpga/xrt.rst

Quoted 12 lines
Documentation/fpga/xrt.rst new file +12 -0
+
+.. note::
+   See code in ``include/xleaf.h``
+
+
+xmgmt.ko
+--------
+
+The xmgmt driver is a PCIe device driver driving MPF found on
+Xilinx's Alveo PCIE device. It consists of one *root* driver, one or
+more *group* drivers and one or more *xleaf* drivers. The root and
+MPF specific xleaf drivers are in xmgmt.ko. The group driver and other xleaf

drivers are in xrt-lib.ko.
I am not sure if *.ko is correct, these will also be intree.

Documentation/fpga/xrt.rst line 66

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+
+The instantiation of specific group driver or xleaf driver is
+completely data

of a specific

Documentation/fpga/xrt.rst line 68

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+driven based on meta data (mostly in device tree format) found
+through VSEC

mostly ? what is the deviation from device tree ?

Documentation/fpga/xrt.rst line 69

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+capability and inside firmware files, such as platform xsabin or user xclbin

file.

Documentation/fpga/xrt.rst line 71

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+The root driver manages life cycle of multiple group drivers, which,
+in turn,

the life cycle

Quoted 7 lines

Documentation/fpga/xrt.rst line 73

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+manages multiple xleaf drivers. This allows a single set of driver
+code to support

set of drivers

drop 'code'

Will update

Quoted 111 lines
Quoted 47 lines
Documentation/fpga/xrt.rst new file +47 -0
+all kinds of subsystems exposed by different shells. The difference
+among all these subsystems will be handled in xleaf drivers with root
+and group drivers being part of the infrastructure and provide common
+services for all leaves found on all platforms.
+
+The driver object model looks like the following::
+
+                    +-----------+
+                    |   xroot   |
+                    +-----+-----+
+                          |
+              +-----------+-----------+
+              |                       |
+              v                       v
+        +-----------+          +-----------+
+        |   group   |    ...   |   group   |
+        +-----+-----+          +------+----+
+              |                       |
+              |                       |
+        +-----+----+            +-----+----+
+        |          |            |          |
+        v          v            v          v
+    +-------+  +-------+    +-------+  +-------+
+    | xleaf |..| xleaf |    | xleaf |..| xleaf |
+    +-------+  +-------+    +-------+  +-------+
+
+As an example for Xilinx Alveo U50 before user xclbin download, the
+tree looks like the following::
+
+                                +-----------+
+                                |   xmgmt   |
+                                +-----+-----+
+                                      |
+            +-------------------------+--------------------+
+            |                         |                    |
+            v                         v                    v
+       +--------+                +--------+            +--------+
+       | group0 |                | group1 |            | group2 |
+       +----+---+                +----+---+            +---+----+
+            |                         |                    |
+            |                         |                    |
+      +-----+-----+        +----+-----+---+    +-----+-----+----+--------+
+      |           |        |    |         |    |     |          |        |
+      v           v        |    v         v    |     v          v        |
+ +------------+  +------+  | +------+ +------+ |  +------+
+ +------------+ +-----------+ |
+ | xmgmt_main |  | VSEC |  | | GPIO | | QSPI | |  |  CMC | |

Documentation/fpga/xrt.rst line 116

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+ | AXI-GATE0 | |
+ +------------+  +------+  | +------+ +------+ |  +------+
+ +------------+ +-----------+ |
+                           | +---------+       |  +------+ +-----------+ |
+                           +>| MAILBOX |       +->| ICAP | | AXI-GATE1 |<+
+                             +---------+       |  +------+ +-----------+
+                                               |  +-------+
+                                               +->| CALIB |
+                                                  +-------+
+

Nice ascii art!

Quoted 11 lines
Documentation/fpga/xrt.rst new file +11 -0
+After an xclbin is download, group3 will be added and the tree looks
+like the
+following::
+
+                                +-----------+
+                                |   xmgmt   |
+                                +-----+-----+
+                                      |
+            +-------------------------+--------------------+-----------------+
+            |                         |                    |                 |
+            v                         v                    v                 |

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+       +--------+                +--------+            +--------+            |
+       | group0 |                | group1 |            | group2 |            |
+       +----+---+                +----+---+            +---+----+            |
+            |                         |                    |                 |
+            |                         |                    |                 |
+      +-----+-----+       +-----+-----+---+    +-----+-----+----+--------+   |
+      |           |       |     |         |    |     |          |        |   |
+      v           v       |     v         v    |     v          v        |   |
+ +------------+  +------+ | +------+ +------+  |  +------+ +-----------+ |   |
+ | xmgmt_main |  | VSEC | | | GPIO | | QSPI |  |  |  CMC | | AXI-GATE0 | |

|

Quoted 31 lines
Documentation/fpga/xrt.rst new file +31 -0
+ +------------+  +------+ | +------+ +------+  |  +------+ +-----------+ |   |
+                          | +---------+        |  +------+ +-----------+ |   |
+                          +>| MAILBOX |        +->| ICAP | | AXI-GATE1 |<+   |
+                            +---------+        |  +------+ +-----------+     |
+                                               |  +-------+                  |
+                                               +->| CALIB |                  |
+                                                  +-------+                  |
+                      +---+----+                                             |
+                      | group3 |<--------------------------------------------+
+                      +--------+
+                          |
+                          |
+     +-------+--------+---+--+--------+------+-------+
+     |       |        |      |        |      |       |
+     v       |        v      |        v      |       v
+ +--------+  |   +--------+  |   +--------+  |    +-----+
+ | CLOCK0 |  |   | CLOCK1 |  |   | CLOCK2 |  |    | UCS |
+ +--------+  v   +--------+  v   +--------+  v    +-----+
+ +-------------+ +-------------+ +-------------+
+ | CLOCK-FREQ0 | | CLOCK-FREQ1 | | CLOCK-FREQ2 |
+ +-------------+ +-------------+ +-------------+
+
+
+xmgmt-root
+^^^^^^^^^^
+
+The xmgmt-root driver is a PCIe device driver attached to MPF. It's
+part of the infrastructure of the MPF driver and resides in xmgmt.ko.
+This driver
+
+* manages one or more group drivers
Quoted 8 lines

Documentation/fpga/xrt.rst line 189

Quoted 7 lines
Documentation/fpga/xrt.rst new file +7 -0
+* provides access to functionalities that requires pci_dev, such as
+PCIE config
+  space access, to other xleaf drivers through root calls
+* together with group driver, facilities event callbacks for other
+xleaf drivers
+* together with group driver, facilities inter-leaf driver calls for
+other xleaf

Maybe drop 'together with group driver'

Will update

Quoted 8 lines

Documentation/fpga/xrt.rst line 196

Quoted 7 lines
Documentation/fpga/xrt.rst new file +7 -0
+  drivers
+
+When root driver starts, it will explicitly create an initial group
+instance, which contains xleaf drivers that will trigger the creation
+of other group instances. The root driver will wait for all group and
+leaves to be created before it returns from it's probe routine and
+claim success of the initialization of the entire xmgmt driver.

What happens if there a failure in one leaf ? Does the whole board go down ?

Quoted 11 lines

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+
+.. note::
+   See code in ``lib/xroot.c`` and ``mgmt/root.c``
+
+
+group
+^^^^^
+
+The group driver is a platform device driver whose life cycle is
+managed by

Maybe call this a 'pseudo device'

Will update

Quoted 9 lines

Documentation/fpga/xrt.rst line 213

Quoted 7 lines
Documentation/fpga/xrt.rst new file +7 -0
+root and does not have real IO mem or IRQ resources. It's part of the
+infrastructure of the MPF driver and resides in xrt-lib.ko. This
+driver
+
+* manages one or more xleaf drivers so that multiple leaves can be
+managed as a
+  group

can drop 'so that multiple leaves can be managed as a group' to me, this is the same as 'one or more'

Will do

Quoted 7 lines

Documentation/fpga/xrt.rst line 219

Quoted 6 lines
Documentation/fpga/xrt.rst new file +6 -0
+* provides access to root from leaves, so that root calls, event
+notifications
+  and inter-leaf calls can happen
+
+In xmgmt, an initial group driver instance will be created by root,
+which

by the root

Quoted 11 lines

Documentation/fpga/xrt.rst line 227

Quoted 8 lines
Documentation/fpga/xrt.rst new file +8 -0
+contains leaves that will trigger group instances to be created to
+manage groups of leaves found on different partitions on hardware,
+such as VSEC, Shell, and User.
+
+Every *fpga_region* has a group object associated with it. The group
+is created when xclbin image is loaded on the fpga_region. The
+existing group is destroyed when a new xclbin image is loaded. The
+fpga_region persists across xclbin downloads.

The connection of a 'group' node to a fpga region region is fairly important, maybe move this section earlier. 'group' as an fpganess thing would be kept in fpga/ subsystem.

Will update

Quoted 8 lines
Quoted 8 lines
Documentation/fpga/xrt.rst new file +8 -0
+
+.. note::
+   See code in ``lib/group.c``
+
+
+xleaf
+^^^^^
+
Quoted 9 lines

Documentation/fpga/xrt.rst line 240

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+The xleaf driver is a platform device driver whose life cycle is
+managed by a group driver and may or may not have real IO mem or IRQ
+resources. They are the real meat of xmgmt and contains platform
+specific code to Shell and User found on a MPF.
+

Maybe a split is pseudo device leaves, those without real IO mem, stay in fpga/  others go ?

This goes back to the earlier question of what minimal set of platform drivers should stay in fpga subsystem. There are some like bridge or configuration engine (also called icap) which have their own IO mem but do not have a life outside of fpga subsystem.

Quoted 11 lines

Documentation/fpga/xrt.rst line 244

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+A xleaf driver may not have real hardware resources when it merely
+acts as a driver that manages certain in-memory states for xmgmt.
+These in-memory states could be shared by multiple other leaves.
+

This implies locking and some message passing.

Documentation/fpga/xrt.rst line 246

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+Leaf drivers assigned to specific hardware resources drive specific
+subsystem in

drive a specific

Documentation/fpga/xrt.rst line 248

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+the device. To manipulate the subsystem or carry out a task, a xleaf
+driver may ask help from root via root calls and/or from other leaves via

inter-leaf calls.

Quoted 6 lines

Documentation/fpga/xrt.rst line 252

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+
+A xleaf can also broadcast events through infrastructure code for
+other leaves to process. It can also receive event notification from
+infrastructure about certain events, such as post-creation or pre-exit of a

particular xleaf.
I would like to see some examples of how the inter node communications work.

Would update to show an example.

Quoted 14 lines

Documentation/fpga/xrt.rst

Quoted 13 lines
Documentation/fpga/xrt.rst new file +13 -0
+
+.. note::
+   See code in ``lib/xleaf/*.c``
+
+
+FPGA Manager Interaction
+========================
+
+fpga_manager
+------------
+
+An instance of fpga_manager is created by xmgmt_main and is used for
+xclbin

for the xclbin

Quoted 7 lines

Documentation/fpga/xrt.rst line 267

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+image download. fpga_manager requires the full xclbin image before it
+can start programming the FPGA configuration engine via ICAP platform

driver.

via the ICAP

what is ICAP ?

Will update. ICAP stands for Internal Configuration Access Port used for configuring the fpga.

Quoted 47 lines

Documentation/fpga/xrt.rst

Quoted 7 lines
Documentation/fpga/xrt.rst new file +6 -0
+
+fpga_region
+-----------
+
+For every interface exposed by currently loaded xclbin/xsabin in the
+*parent*

by the currently

Documentation/fpga/xrt.rst line 247

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+fpga_region a new instance of fpga_region is created like a *child* region.

fpga_region,

Documentation/fpga/xrt.rst line 276

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+The device tree of the *parent* fpga_region defines the resources for
+a new instance of fpga_bridge which isolates the parent from

and isolates

Documentation/fpga/xrt.rst line 281

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+child fpga_region. This new instance of fpga_bridge will be used when
+a xclbin image is loaded on the child fpga_region. After the xclbin
+image is downloaded to the fpga_region, an instance of group is
+created for the fpga_region using the device tree obtained as part of
+xclbin. If this device

of the xclbin

Documentation/fpga/xrt.rst line 254

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+tree defines any child interfaces then it can trigger the creation of

interfaces, then

Documentation/fpga/xrt.rst line 255

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+fpga_bridge and fpga_region for the next region in the chain.

a fpga_bridge and a fpga_region

Documentation/fpga/xrt.rst

Quoted 6 lines
Documentation/fpga/xrt.rst new file +6 -0
+
+fpga_bridge
+-----------
+
+Like fpga_region, matching fpga_bridge is also created by walking the
+device

Like the fpga_region, a matchin

Documentation/fpga/xrt.rst

Quoted 13 lines
Documentation/fpga/xrt.rst new file +13 -0
+tree of the parent group.
+
+Driver Interfaces
+=================
+
+xmgmt Driver Ioctls
+-------------------
+
+Ioctls exposed by xmgmt driver to user space are enumerated in the
+following
+table:
+
+== ===================== ============================

==========================

Documentation/fpga/xrt.rst line 304

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+#  Functionality         ioctl request code            data format
+== ===================== ============================

==========================

Quoted 9 lines

Documentation/fpga/xrt.rst line 305

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+1  FPGA image download   XMGMT_IOCICAPDOWNLOAD_AXLF

xmgmt_ioc_bitstream_axlf

Documentation/fpga/xrt.rst line 307

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+== ===================== ============================
+==========================

This data format is described below, maybe swap this section with that so

folks will know what xmgmnt_ioc_bitstream_axlf is before this section.

Will update.

Quoted 10 lines

Documentation/fpga/xrt.rst line 310

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+
+User xclbin can be downloaded by using xbmgmt tool from XRT open
+source suite. See

A user xclbin

using the xbmgmt

from the XRT

Will update

Quoted 42 lines
Quoted 23 lines
Documentation/fpga/xrt.rst new file +23 -0
+example usage below::
+
+  xbmgmt partition --program --path
+ /lib/firmware/xilinx/862c7020a250293e32036f19956669e5/test/verify.xc
+ lbin --force
+
+xmgmt Driver Sysfs
+------------------
+
+xmgmt driver exposes a rich set of sysfs interfaces. Subsystem
+platform drivers export sysfs node for every platform instance.
+
+Every partition also exports its UUIDs. See below for examples::
+
+  /sys/bus/pci/devices/0000:06:00.0/xmgmt_main.0/interface_uuids
+  /sys/bus/pci/devices/0000:06:00.0/xmgmt_main.0/logic_uuids
+
+
+hwmon
+-----
+
+xmgmt driver exposes standard hwmon interface to report voltage,
+current, temperature, power, etc. These can easily be viewed using

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+*sensors* command line utility.
+
+Alveo Platform Overview
+=======================
+
+Alveo platforms are architected as two physical FPGA partitions:
+*Shell* and *User*. The Shell provides basic infrastructure for the
+Alveo platform like PCIe connectivity, board management, Dynamic
+Function Exchange (DFX), sensors, clocking, reset, and security. User
+partition contains user compiled FPGA

the user compiled

Documentation/fpga/xrt.rst

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+binary which is loaded by a process called DFX also known as partial
+reconfiguration.
+
+Physical partitions require strict HW compatibility with each other
+for DFX to work properly.

swap order

Quoted 3 lines

For DFX to work properly physical partitions ..

Will update

Quoted 11 lines

Documentation/fpga/xrt.rst

Quoted 4 lines

Every physical partition has two interface UUIDs: *parent* UUID

Documentation/fpga/xrt.rst new file +3 -0
+and *child* UUID. For simple single stage platforms, Shell → User
+forms parent child relationship. For complex two stage platforms,
+Base → Shell → User forms the parent child relationship chain.

this bit is confusing. is this related to uuid?

Documentation/fpga/xrt.rst

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+
+.. note::
+   Partition compatibility matching is key design component of Alveo

platforms

Documentation/fpga/xrt.rst

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+   and XRT. Partitions have child and parent relationship. A loaded
+partition
Quoted 7 lines

have a child

Quoted 2 lines

+ exposes child partition UUID to advertise its compatibility
+ requirement for

the child's

can drop 'for child partition'

Will update

Quoted 11 lines
Quoted 3 lines

+ child partition. When loading a child partition the xmgmt
+ management driver

When loading a child partition,

Documentation/fpga/xrt.rst

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+   matches parent UUID of the child partition against child UUID exported by
+   the parent. Parent and child partition UUIDs are stored in the *xclbin*
+   (for user) or *xsabin* (for base and shell).

this is confusing, is this part of the file image format ?

Maybe save/move till the image layout.

Yes these IDs are stored in xclbin image format. Will move the sections around as suggested.

Quoted 6 lines
Quoted 3 lines

Except for root UUID, VSEC,
+ hardware itself does not know about UUIDs. UUIDs are stored in xsabin

and

Quoted 1 line

+ xclbin.

This is confusing too, not sure how to untangle.

Will reword.

Quoted 27 lines
Quoted 16 lines
Documentation/fpga/xrt.rst new file +16 -0
+
+
+The physical partitions and their loading is illustrated below::
+
+           SHELL                               USER
+        +-----------+                  +-------------------+
+        |           |                  |                   |
+        | VSEC UUID | CHILD     PARENT |    LOGIC UUID     |
+        |           o------->|<--------o                   |
+        |           | UUID       UUID  |                   |
+        +-----+-----+                  +--------+----------+
+              |                                 |
+              .                                 .
+              |                                 |
+          +---+---+                      +------+--------+
+          |  POR  |                      | USER COMPILED |

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+          | FLASH |                      |    XCLBIN     |
+          +-------+                      +---------------+
+
+
+Loading Sequence
+----------------
+
+The Shell partition is loaded from flash at system boot time. It
+establishes the PCIe link and exposes two physical functions to the
+BIOS. After OS boot, xmgmt

the OS boots, the xmgmt

Quoted 9 lines

Documentation/fpga/xrt.rst

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+driver attaches to PCIe physical function 0 exposed by the Shell and
+then looks for VSEC in PCIe extended configuration space. Using VSEC
+it determines the logic

the PCIe

2 lines not shown · raw message

545 lines not shown · raw message

Quoted 8 lines

6 lines not shown · raw message

1 line not shown · raw message

Part of this message is not shown here. View the raw message.

#5 lore raw

RE: [PATCH V3 XRT Alveo 01/18] Documentation: fpga: Add a document describing XRT Alveo drivers

Hello Moritz,

Quoted 119 lines

-----Original Message-----
From: Moritz Fischer <[email protected]>
Sent: Saturday, March 6, 2021 9:19 AM
To: Sonal Santan <[email protected]>
Cc: Tom Rix <[email protected]>; Lizhi Hou <[email protected]>; linux- [email protected]; [email protected]; Max Zhen <[email protected]>; Michal Simek <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH V3 XRT Alveo 01/18] Documentation: fpga: Add a document describing XRT Alveo drivers

Quoted 97 lines · Sonal Santan

Hello Tom,

Quoted 12 lines

-----Original Message-----
From: Tom Rix <[email protected]>
Sent: Friday, February 19, 2021 2:26 PM
To: Lizhi Hou <[email protected]>; [email protected]
Cc: Lizhi Hou <[email protected]>; [email protected]; Max Zhen <[email protected]>; Sonal Santan <[email protected]>; Michal Simek <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]; [email protected]; [email protected]; Max Zhen <[email protected]>
Subject: Re: [PATCH V3 XRT Alveo 01/18] Documentation: fpga: Add a document describing XRT Alveo drivers

Quoted 4 lines

From the documentation, there are a couple of big questions and a bunch of word smithing.

pseudo-bus : do we need a bus ?

We are looking for guidance here.

Quoted 4 lines

xrt-lib real platform devices that aren't fpga, do they need to move to another subsystem ?

Drivers for the IPs that show up in the Alveo shell are not generic enough. They fit into the framework that XRT uses. Is the idea that that they can be used in a different context?

Quoted 69 lines

Overall looks good, love the ascii art!

Quoted 9 lines · Lizhi Hou

Describe XRT driver architecture and provide basic overview of Xilinx Alveo platform.

---
 Documentation/fpga/index.rst |   1 +
 Documentation/fpga/xrt.rst   | 842

+++++++++++++++++++++++++++++++++++

Quoted 31 lines
 2 files changed, 843 insertions(+)  create mode 100644

Documentation/fpga/xrt.rst

(quoted diff) +0 -0

b/Documentation/fpga/index.rst index f80f95667ca2..30134357b70d 100644

Documentation/fpga/index.rst +1 -0
@@ -8,6 +8,7 @@ fpga
     :maxdepth: 1
 
     dfl
+    xrt
 
 .. only::  subproject and html
 
(quoted diff) +0 -0

b/Documentation/fpga/xrt.rst new file mode 100644 index 000000000000..9bc2d2785cb9

Documentation/fpga/xrt.rst new file +9 -0
@@ -0,0 +1,842 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==================================
+XRTV2 Linux Kernel Driver Overview
+==================================
+
+Authors:
+
+* Sonal Santan <[email protected]>

Documentation/fpga/xrt.rst line 19

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+* Max Zhen <[email protected]>
+* Lizhi Hou <[email protected]>
+
+XRTV2 drivers are second generation `XRT
+<https://github.com/Xilinx/XRT>`_ drivers which support `Alveo
+<https://www.xilinx.com/products/boards-and-kits/alveo.html>`_
+PCIe platforms from Xilinx.
+
+XRTV2 drivers support *subsystem* style data driven platforms
+where driver's

where the driver's

Documentation/fpga/xrt.rst line 27

Quoted 8 lines
Documentation/fpga/xrt.rst new file +8 -0
+configuration and behavior is determined by meta data provided by
+the platform (in *device tree* format). Primary management
+physical function (MPF) driver is called **xmgmt**. Primary user
+physical function (UPF) driver is called
+**xuser** and is under development. xrt driver framework and HW
+subsystem drivers are packaged into a library module called
+**xrt-lib**, which is shared by **xmgmt** and **xuser** (under
+development). The xrt driver framework

xuser still under development ?

Documentation/fpga/xrt.rst line 29

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+implements a pseudo-bus which is used to discover HW subsystems
+and facilitate

A pseudo-bus.

Quoted 8 lines
Quoted 5 lines

It would be good if this was close to what was done for dfl here

https://lore.kernel.org/linux-fpga/1605159759-3439-1-git-send-email-
[email protected]/

I am wondering if we can phase in the migration to formal bus architecture based on struct bus_type as a follow on set of patches?

Quoted 8 lines

I'd rather have it done early on. If we know there's a better way of doing something and we don't the code should go into staging.

This gives us an out if it doesn't happen, otherwise the kernel community would depend on corporate goodwill to appropriately staff it.

Note, I'm not trying to say there's any ill will anywhere, Xilinx has been traditionally good about this :)

Thanks for the suggestion. Since the bus change is a bigger change we will incorporate it into V5 version of the patch series. Meanwhile we will post V4 version of patch series which will addresses other feedback we have received so far.

Quoted 1,151 lines
Quoted 41 lines
Quoted 14 lines

Documentation/fpga/xrt.rst

Quoted 11 lines
Documentation/fpga/xrt.rst new file +10 -0
+inter HW subsystem interaction.
+
+Driver Modules
+==============
+
+xrt-lib.ko
+----------
+
+Repository of all subsystem drivers and pure software modules
+that can potentially

subsystem drivers

Quoted 6 lines

drivers in fpga/ should be for managing just the fpganess of the fpga.

soft devices ex/ a soft tty should go to their respective subsystem location

Are there any in this patchset you think might move ?

We have already shrunk the patch to only include FPGA centric pieces necessary to get the bitstream download implemented. Should we explore the question of subsystem drivers when we add support for more features of the Alveo shell?

Quoted 12 lines

Maybe we can defer reviewing those now.

Documentation/fpga/xrt.rst line 43

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+be shared between xmgmt and xuser. All these drivers are
+structured as Linux *platform driver* and are instantiated by
+xmgmt (or xuser under development) based on meta data associated
+with hardware. The metadata is in the form of device tree

with the hardware

form of a device tree

Will change

Quoted 8 lines
Quoted 8 lines

Documentation/fpga/xrt.rst line 47

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+as mentioned before. Each platform driver statically defines a
+subsystem node array by using node name or a string in its
+``compatible`` property. And this array is eventually translated
+to IOMEM

resources of the platform device.

Documentation/fpga/xrt.rst line 50

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+
+The xrt-lib core infrastructure provides hooks to platform
+drivers for device node management, user file operations and ioctl

callbacks.

Quoted 52 lines
Quoted 5 lines

Documentation/fpga/xrt.rst line 52

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+The core also provides pseudo-bus functionality for platform
+driver registration, discovery and inter platform driver ioctl calls.

core infrastructure.

Will update.

Quoted 4 lines

The interfaces to the infrastructure are not in include/linux/fpga/

Maybe this needs to change.

Were you thinking of moving XRT infrastructure header files from drivers/fpga/xrt/include to include/linux/fpga?

Quoted 28 lines

Documentation/fpga/xrt.rst

Quoted 13 lines
Documentation/fpga/xrt.rst new file +13 -0
+
+.. note::
+   See code in ``include/xleaf.h``
+
+
+xmgmt.ko
+--------
+
+The xmgmt driver is a PCIe device driver driving MPF found on
+Xilinx's Alveo PCIE device. It consists of one *root* driver, one
+or more *group* drivers and one or more *xleaf* drivers. The root
+and MPF specific xleaf drivers are in xmgmt.ko. The group driver
+and other xleaf

drivers are in xrt-lib.ko.
I am not sure if *.ko is correct, these will also be intree.

Documentation/fpga/xrt.rst line 68

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+
+The instantiation of specific group driver or xleaf driver is
+completely data

of a specific

Documentation/fpga/xrt.rst line 70

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+driven based on meta data (mostly in device tree format) found
+through VSEC

mostly ? what is the deviation from device tree ?

Documentation/fpga/xrt.rst line 72

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+capability and inside firmware files, such as platform xsabin or
+user xclbin

file.

Documentation/fpga/xrt.rst line 74

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+The root driver manages life cycle of multiple group drivers,
+which, in turn,

the life cycle

Quoted 7 lines

Documentation/fpga/xrt.rst line 76

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+manages multiple xleaf drivers. This allows a single set of
+driver code to support

set of drivers

drop 'code'

Quoted 6 lines

Will update

Quoted 4 lines

Documentation/fpga/xrt.rst line 80

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+all kinds of subsystems exposed by different shells. The
+difference among all these subsystems will be handled in xleaf
+drivers with root and group drivers being part of the
+infrastructure and provide common services for all leaves found on all

platforms.

Quoted 119 lines
Quoted 108 lines
Quoted 43 lines
Documentation/fpga/xrt.rst new file +43 -0
+
+The driver object model looks like the following::
+
+                    +-----------+
+                    |   xroot   |
+                    +-----+-----+
+                          |
+              +-----------+-----------+
+              |                       |
+              v                       v
+        +-----------+          +-----------+
+        |   group   |    ...   |   group   |
+        +-----+-----+          +------+----+
+              |                       |
+              |                       |
+        +-----+----+            +-----+----+
+        |          |            |          |
+        v          v            v          v
+    +-------+  +-------+    +-------+  +-------+
+    | xleaf |..| xleaf |    | xleaf |..| xleaf |
+    +-------+  +-------+    +-------+  +-------+
+
+As an example for Xilinx Alveo U50 before user xclbin download,
+the tree looks like the following::
+
+                                +-----------+
+                                |   xmgmt   |
+                                +-----+-----+
+                                      |
+            +-------------------------+--------------------+
+            |                         |                    |
+            v                         v                    v
+       +--------+                +--------+            +--------+
+       | group0 |                | group1 |            | group2 |
+       +----+---+                +----+---+            +---+----+
+            |                         |                    |
+            |                         |                    |
+      +-----+-----+        +----+-----+---+    +-----+-----+----+--------+
+      |           |        |    |         |    |     |          |        |
+      v           v        |    v         v    |     v          v        |
+ +------------+  +------+  | +------+ +------+ |  +------+
+ +------------+ +-----------+ |
+ | xmgmt_main |  | VSEC |  | | GPIO | | QSPI | |  |  CMC | |

Documentation/fpga/xrt.rst line 116

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+ | AXI-GATE0 | |
+ +------------+  +------+  | +------+ +------+ |  +------+
+ +------------+ +-----------+ |
+                           | +---------+       |  +------+ +-----------+ |
+                           +>| MAILBOX |       +->| ICAP | | AXI-GATE1 |<+
+                             +---------+       |  +------+ +-----------+
+                                               |  +-------+
+                                               +->| CALIB |
+                                                  +-------+
+

Nice ascii art!

Quoted 12 lines
Documentation/fpga/xrt.rst new file +12 -0
+After an xclbin is download, group3 will be added and the tree
+looks like the
+following::
+
+                                +-----------+
+                                |   xmgmt   |
+                                +-----+-----+
+                                      |
+            +-------------------------+--------------------+-----------------+
+            |                         |                    |                 |
+            v                         v                    v                 |
+       +--------+                +--------+            +--------+            |

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+       | group0 |                | group1 |            | group2 |            |
+       +----+---+                +----+---+            +---+----+            |
+            |                         |                    |                 |
+            |                         |                    |                 |
+      +-----+-----+       +-----+-----+---+    +-----+-----+----+--------+   |
+      |           |       |     |         |    |     |          |        |   |
+      v           v       |     v         v    |     v          v        |   |
+ +------------+  +------+ | +------+ +------+  |  +------+ +-----------+ |   |
+ | xmgmt_main |  | VSEC | | | GPIO | | QSPI |  |  |  CMC | |
+ | AXI-GATE0 | |

|

Quoted 31 lines
Documentation/fpga/xrt.rst new file +31 -0
+ +------------+  +------+ | +------+ +------+  |  +------+ +-----------+ |   |
+                          | +---------+        |  +------+ +-----------+ |   |
+                          +>| MAILBOX |        +->| ICAP | | AXI-GATE1 |<+   |
+                            +---------+        |  +------+ +-----------+     |
+                                               |  +-------+                  |
+                                               +->| CALIB |                  |
+                                                  +-------+                  |
+                      +---+----+                                             |
+                      | group3 |<--------------------------------------------+
+                      +--------+
+                          |
+                          |
+     +-------+--------+---+--+--------+------+-------+
+     |       |        |      |        |      |       |
+     v       |        v      |        v      |       v
+ +--------+  |   +--------+  |   +--------+  |    +-----+
+ | CLOCK0 |  |   | CLOCK1 |  |   | CLOCK2 |  |    | UCS |
+ +--------+  v   +--------+  v   +--------+  v    +-----+
+ +-------------+ +-------------+ +-------------+
+ | CLOCK-FREQ0 | | CLOCK-FREQ1 | | CLOCK-FREQ2 |
+ +-------------+ +-------------+ +-------------+
+
+
+xmgmt-root
+^^^^^^^^^^
+
+The xmgmt-root driver is a PCIe device driver attached to MPF.
+It's part of the infrastructure of the MPF driver and resides in xmgmt.ko.
+This driver
+
+* manages one or more group drivers
Quoted 8 lines

Documentation/fpga/xrt.rst line 193

Quoted 7 lines
Documentation/fpga/xrt.rst new file +7 -0
+* provides access to functionalities that requires pci_dev, such
+as PCIE config
+  space access, to other xleaf drivers through root calls
+* together with group driver, facilities event callbacks for
+other xleaf drivers
+* together with group driver, facilities inter-leaf driver calls
+for other xleaf

Maybe drop 'together with group driver'

Will update

Quoted 7 lines
Quoted 7 lines

Documentation/fpga/xrt.rst line 200

Quoted 7 lines
Documentation/fpga/xrt.rst new file +7 -0
+  drivers
+
+When root driver starts, it will explicitly create an initial
+group instance, which contains xleaf drivers that will trigger
+the creation of other group instances. The root driver will wait
+for all group and leaves to be created before it returns from
+it's probe routine and claim success of the initialization of the entire

xmgmt driver.

Quoted 1 line
Quoted 1 line

What happens if there a failure in one leaf ? Does the whole board go down

?

Quoted 65 lines
Quoted 11 lines

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+
+.. note::
+   See code in ``lib/xroot.c`` and ``mgmt/root.c``
+
+
+group
+^^^^^
+
+The group driver is a platform device driver whose life cycle is
+managed by

Maybe call this a 'pseudo device'

Will update

Quoted 9 lines

Documentation/fpga/xrt.rst line 217

Quoted 7 lines
Documentation/fpga/xrt.rst new file +7 -0
+root and does not have real IO mem or IRQ resources. It's part of
+the infrastructure of the MPF driver and resides in xrt-lib.ko.
+This driver
+
+* manages one or more xleaf drivers so that multiple leaves can
+be managed as a
+  group

can drop 'so that multiple leaves can be managed as a group' to me, this is the same as 'one or more'

Will do

Quoted 7 lines

Documentation/fpga/xrt.rst line 223

Quoted 6 lines
Documentation/fpga/xrt.rst new file +6 -0
+* provides access to root from leaves, so that root calls, event
+notifications
+  and inter-leaf calls can happen
+
+In xmgmt, an initial group driver instance will be created by
+root, which

by the root

Quoted 11 lines

Documentation/fpga/xrt.rst line 231

Quoted 8 lines
Documentation/fpga/xrt.rst new file +8 -0
+contains leaves that will trigger group instances to be created
+to manage groups of leaves found on different partitions on
+hardware, such as VSEC, Shell, and User.
+
+Every *fpga_region* has a group object associated with it. The
+group is created when xclbin image is loaded on the fpga_region.
+The existing group is destroyed when a new xclbin image is
+loaded. The fpga_region persists across xclbin downloads.

The connection of a 'group' node to a fpga region region is fairly important, maybe move this section earlier. 'group' as an fpganess thing would be kept in fpga/ subsystem.

Will update

Quoted 8 lines
Quoted 8 lines
Documentation/fpga/xrt.rst new file +8 -0
+
+.. note::
+   See code in ``lib/group.c``
+
+
+xleaf
+^^^^^
+
Quoted 9 lines

Documentation/fpga/xrt.rst line 244

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+The xleaf driver is a platform device driver whose life cycle is
+managed by a group driver and may or may not have real IO mem or
+IRQ resources. They are the real meat of xmgmt and contains
+platform specific code to Shell and User found on a MPF.
+

Maybe a split is pseudo device leaves, those without real IO mem, stay in fpga/  others go ?

Quoted 7 lines

This goes back to the earlier question of what minimal set of platform drivers should stay in fpga subsystem. There are some like bridge or configuration engine (also called icap) which have their own IO mem but do not have a life outside of fpga subsystem.

Quoted 2 lines

Documentation/fpga/xrt.rst line 246

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+A xleaf driver may not have real hardware resources when it
+merely acts as a driver that manages certain in-memory states for

xmgmt.

Quoted 10 lines
Quoted 10 lines

Documentation/fpga/xrt.rst line 248

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+These in-memory states could be shared by multiple other leaves.
+

This implies locking and some message passing.

Documentation/fpga/xrt.rst line 250

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+Leaf drivers assigned to specific hardware resources drive
+specific subsystem in

drive a specific

Documentation/fpga/xrt.rst line 253

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+the device. To manipulate the subsystem or carry out a task, a
+xleaf driver may ask help from root via root calls and/or from
+other leaves via

inter-leaf calls.

Quoted 7 lines
Quoted 7 lines

Documentation/fpga/xrt.rst line 258

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+
+A xleaf can also broadcast events through infrastructure code for
+other leaves to process. It can also receive event notification
+from infrastructure about certain events, such as post-creation
+or pre-exit of a

particular xleaf.
I would like to see some examples of how the inter node communications

work.

Quoted 22 lines

Would update to show an example.

Quoted 19 lines

Documentation/fpga/xrt.rst

Quoted 13 lines
Documentation/fpga/xrt.rst new file +13 -0
+
+.. note::
+   See code in ``lib/xleaf/*.c``
+
+
+FPGA Manager Interaction
+========================
+
+fpga_manager
+------------
+
+An instance of fpga_manager is created by xmgmt_main and is used
+for xclbin

for the xclbin

Documentation/fpga/xrt.rst line 274

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+image download. fpga_manager requires the full xclbin image
+before it can start programming the FPGA configuration engine via
+ICAP platform

driver.

Quoted 17 lines
Quoted 3 lines

via the ICAP

what is ICAP ?

Will update. ICAP stands for Internal Configuration Access Port used for configuring the fpga.

Quoted 10 lines

Documentation/fpga/xrt.rst

Quoted 8 lines
Documentation/fpga/xrt.rst new file +7 -0
+
+fpga_region
+-----------
+
+For every interface exposed by currently loaded xclbin/xsabin in
+the
+*parent*

by the currently

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+fpga_region a new instance of fpga_region is created like a *child*

region.

Quoted 128 lines
Quoted 40 lines

fpga_region,

Documentation/fpga/xrt.rst line 284

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+The device tree of the *parent* fpga_region defines the resources
+for a new instance of fpga_bridge which isolates the parent from

and isolates

Documentation/fpga/xrt.rst line 289

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+child fpga_region. This new instance of fpga_bridge will be used
+when a xclbin image is loaded on the child fpga_region. After the
+xclbin image is downloaded to the fpga_region, an instance of
+group is created for the fpga_region using the device tree
+obtained as part of xclbin. If this device

of the xclbin

Documentation/fpga/xrt.rst line 291

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+tree defines any child interfaces then it can trigger the
+creation of

interfaces, then

Documentation/fpga/xrt.rst line 255

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+fpga_bridge and fpga_region for the next region in the chain.

a fpga_bridge and a fpga_region

Documentation/fpga/xrt.rst

Quoted 6 lines
Documentation/fpga/xrt.rst new file +6 -0
+
+fpga_bridge
+-----------
+
+Like fpga_region, matching fpga_bridge is also created by walking
+the device

Like the fpga_region, a matchin

Documentation/fpga/xrt.rst

Quoted 13 lines
Documentation/fpga/xrt.rst new file +13 -0
+tree of the parent group.
+
+Driver Interfaces
+=================
+
+xmgmt Driver Ioctls
+-------------------
+
+Ioctls exposed by xmgmt driver to user space are enumerated in
+the following
+table:
+
+== ===================== ============================

==========================

Documentation/fpga/xrt.rst line 313

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+#  Functionality         ioctl request code            data format
+== ===================== ============================

==========================

Documentation/fpga/xrt.rst line 314

Quoted 1 line
Documentation/fpga/xrt.rst new file +1 -0
+1  FPGA image download   XMGMT_IOCICAPDOWNLOAD_AXLF
Quoted 9 lines

xmgmt_ioc_bitstream_axlf

Documentation/fpga/xrt.rst line 316

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+== ===================== ============================
+==========================

This data format is described below, maybe swap this section with that so

folks will know what xmgmnt_ioc_bitstream_axlf is before this section.

Will update.

Quoted 10 lines

Documentation/fpga/xrt.rst line 319

Quoted 3 lines
Documentation/fpga/xrt.rst new file +3 -0
+
+User xclbin can be downloaded by using xbmgmt tool from XRT open
+source suite. See

A user xclbin

using the xbmgmt

from the XRT

Will update

Quoted 44 lines
Quoted 25 lines
Documentation/fpga/xrt.rst new file +25 -0
+example usage below::
+
+  xbmgmt partition --program --path
+ /lib/firmware/xilinx/862c7020a250293e32036f19956669e5/test/verif
+ y.xc
+ lbin --force
+
+xmgmt Driver Sysfs
+------------------
+
+xmgmt driver exposes a rich set of sysfs interfaces. Subsystem
+platform drivers export sysfs node for every platform instance.
+
+Every partition also exports its UUIDs. See below for examples::
+
+  /sys/bus/pci/devices/0000:06:00.0/xmgmt_main.0/interface_uuids
+  /sys/bus/pci/devices/0000:06:00.0/xmgmt_main.0/logic_uuids
+
+
+hwmon
+-----
+
+xmgmt driver exposes standard hwmon interface to report voltage,
+current, temperature, power, etc. These can easily be viewed
+using

Documentation/fpga/xrt.rst

Quoted 10 lines
Documentation/fpga/xrt.rst new file +10 -0
+*sensors* command line utility.
+
+Alveo Platform Overview
+=======================
+
+Alveo platforms are architected as two physical FPGA partitions:
+*Shell* and *User*. The Shell provides basic infrastructure for
+the Alveo platform like PCIe connectivity, board management,
+Dynamic Function Exchange (DFX), sensors, clocking, reset, and
+security. User partition contains user compiled FPGA

the user compiled

Documentation/fpga/xrt.rst line 359

Quoted 5 lines
Documentation/fpga/xrt.rst new file +5 -0
+binary which is loaded by a process called DFX also known as
+partial reconfiguration.
+
+Physical partitions require strict HW compatibility with each
+other for DFX to work properly.

swap order

Quoted 3 lines

For DFX to work properly physical partitions ..

Will update

Quoted 13 lines

Documentation/fpga/xrt.rst

Quoted 4 lines

Every physical partition has two interface UUIDs: *parent* UUID

Documentation/fpga/xrt.rst new file +3 -0
+and *child* UUID. For simple single stage platforms, Shell → User
+forms parent child relationship. For complex two stage platforms,
+Base → Shell → User forms the parent child relationship chain.

this bit is confusing. is this related to uuid?

Documentation/fpga/xrt.rst

Quoted 4 lines
Documentation/fpga/xrt.rst new file +4 -0
+
+.. note::
+   Partition compatibility matching is key design component of
+Alveo

platforms

Documentation/fpga/xrt.rst

Quoted 2 lines
Documentation/fpga/xrt.rst new file +2 -0
+   and XRT. Partitions have child and parent relationship. A
+loaded partition

have a child

Quoted 14 lines
Quoted 6 lines
Quoted 2 lines

+ exposes child partition UUID to advertise its compatibility
+ requirement for

the child's

can drop 'for child partition'

Will update

Quoted 5 lines
Quoted 3 lines

+ child partition. When loading a child partition the xmgmt
+ management driver

2 lines not shown · raw message

640 lines not shown · raw message

Quoted 8 lines

7 lines not shown · raw message

1 line not shown · raw message

Part of this message is not shown here. View the raw message.

author
#6 lore raw

PATCH v3 2/18 fpga: xrt: driver metadata helper functions

In reply to Lizhi Hou

XRT drivers use device tree as metadata format to discover HW subsystems behind PCIe BAR. Thus libfdt functions are called for driver to parse device tree blob.

---
 drivers/fpga/xrt/include/metadata.h  | 229 ++++++++++++
 drivers/fpga/xrt/metadata/metadata.c | 524 +++++++++++++++++++++++++++
 2 files changed, 753 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 +229 -0
new file mode 100644
index 000000000000..b929bc469b73
@@ -0,0 +1,229 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Header file for Xilinx Runtime (XRT) driver
+ *
+ * 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 (~0UL)
+
+#define XRT_MD_PROP_COMPATIBLE "compatible"
+#define XRT_MD_PROP_PF_NUM "pcie_physical_function"
+#define XRT_MD_PROP_BAR_IDX "pcie_bar_mapping"
+#define XRT_MD_PROP_IO_OFFSET "reg"
+#define XRT_MD_PROP_INTERRUPTS "interrupts"
+#define XRT_MD_PROP_INTERFACE_UUID "interface_uuid"
+#define XRT_MD_PROP_LOGIC_UUID "logic_uuid"
+#define XRT_MD_PROP_VERSION_MAJOR "firmware_version_major"
+
+#define XRT_MD_PROP_HWICAP "axi_hwicap"
+#define XRT_MD_PROP_PDI_CONFIG "pdi_config_mem"
+
+#define XRT_MD_NODE_ENDPOINTS "addressable_endpoints"
+#define XRT_MD_INTERFACES_PATH "/interfaces"
+
+#define XRT_MD_NODE_FIRMWARE "firmware"
+#define XRT_MD_NODE_INTERFACES "interfaces"
+#define XRT_MD_NODE_PARTITION_INFO "partition_info"
+
+#define XRT_MD_NODE_FLASH "ep_card_flash_program_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_SYSMON "ep_cmp_sysmon_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_MGMT "ep_firewall_ctrl_mgmt_00"
+#define XRT_MD_NODE_AF_CTRL_USER "ep_firewall_ctrl_user_00"
+#define XRT_MD_NODE_AF_CTRL_DEBUG "ep_firewall_ctrl_debug_00"
+#define XRT_MD_NODE_AF_DATA_H2C "ep_firewall_data_h2c_00"
+#define XRT_MD_NODE_AF_DATA_C2H "ep_firewall_data_c2h_00"
+#define XRT_MD_NODE_AF_DATA_P2P "ep_firewall_data_p2p_00"
+#define XRT_MD_NODE_AF_DATA_M2M "ep_firewall_data_m2m_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_CMC_MUTEX "ep_cmc_mutex_00"
+#define XRT_MD_NODE_CMC_FW_MEM "ep_cmc_firmware_mem_00"
+#define XRT_MD_NODE_ERT_FW_MEM "ep_ert_firmware_mem_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_MAILBOX_MGMT "ep_mailbox_mgmt_00"
+#define XRT_MD_NODE_MAILBOX_USER "ep_mailbox_user_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_PCIE_MON "ep_pcie_link_mon_00"
+#define XRT_MD_NODE_DDR_CALIB "ep_ddr_mem_calib_00"
+#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_KDMA_CTRL "ep_kdma_ctrl_00"
+#define XRT_MD_NODE_FPGA_CONFIG "ep_fpga_configuration_00"
+#define XRT_MD_NODE_ERT_SCHED "ep_ert_sched_00"
+#define XRT_MD_NODE_XDMA "ep_xdma_00"
+#define XRT_MD_NODE_MSIX "ep_msix_00"
+#define XRT_MD_NODE_QDMA "ep_qdma_00"
+#define XRT_MD_XRT_MD_NODE_QDMA4 "ep_qdma4_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_CLK_SHUTDOWN "ep_aclk_shutdown_00"
+#define XRT_MD_NODE_ERT_BASE "ep_ert_base_address_00"
+#define XRT_MD_NODE_ERT_RESET "ep_ert_reset_00"
+#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_CLKFREQ_HBM "ep_freq_cnt_aclk_hbm_00"
+#define XRT_MD_NODE_GAPPING "ep_gapping_demand_00"
+#define XRT_MD_NODE_UCS_CONTROL_STATUS "ep_ucs_control_status_00"
+#define XRT_MD_NODE_P2P "ep_p2p_00"
+#define XRT_MD_NODE_REMAP_P2P "ep_remap_p2p_00"
+#define XRT_MD_NODE_DDR4_RESET_GATE "ep_ddr_mem_srsr_gate_00"
+#define XRT_MD_NODE_ADDR_TRANSLATOR "ep_remap_data_c2h_00"
+#define XRT_MD_NODE_MAILBOX_XRT "ep_mailbox_user_to_ert_00"
+#define XRT_MD_NODE_PMC_INTR   "ep_pmc_intr_00"
+#define XRT_MD_NODE_PMC_MUX    "ep_pmc_mux_00"
+
+/* driver defined endpoints */
+#define XRT_MD_NODE_VSEC "drv_ep_vsec_00"
+#define XRT_MD_NODE_VSEC_GOLDEN "drv_ep_vsec_golden_00"
+#define XRT_MD_NODE_BLP_ROM "drv_ep_blp_rom_00"
+#define XRT_MD_NODE_MAILBOX_VSEC "ep_mailbox_vsec_00"
+#define XRT_MD_NODE_PLAT_INFO "drv_ep_platform_info_mgmt_00"
+#define XRT_MD_NODE_TEST "drv_ep_test_00"
+#define XRT_MD_NODE_MGMT_MAIN "drv_ep_mgmt_main_00"
+#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_XRT_MD_NODE_PARTITION_INFO_BLP "partition_info_0"
+#define XRT_MD_XRT_MD_NODE_PARTITION_INFO_PLP "partition_info_1"
+
+#define XRT_MD_NODE_DDR_SRSR "drv_ep_ddr_srsr"
+#define XRT_MD_REGMAP_DDR_SRSR "drv_ddr_srsr"
+
+#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;
+	long		bar_off;
+	ulong		size;
+	char		*regmap;
+	char		*regmap_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 (!strcmp(res_name, res_map->res_name))
+			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;
+}
+
+unsigned long xrt_md_size(struct device *dev, const char *blob);
+int xrt_md_create(struct device *dev, 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,
+			char *regmap_name);
+int xrt_md_get_prop(struct device *dev, const char *blob, const char *ep_name,
+		    const char *regmap_name, const char *prop,
+		    const void **val, int *size);
+int xrt_md_set_prop(struct device *dev, char *blob, const char *ep_name,
+		    const char *regmap_name, 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 *regmap_name,
+			 const char *new_ep_name);
+int xrt_md_get_next_endpoint(struct device *dev, const char *blob,
+			     const char *ep_name,  const char *regmap_name,
+			     char **next_ep, char **next_regmap);
+int xrt_md_get_compatible_endpoint(struct device *dev, const char *blob,
+				   const char *regmap_name, const char **ep_name);
+int xrt_md_find_endpoint(struct device *dev, const char *blob,
+			 const char *ep_name, const char *regmap_name,
+			 const char **epname);
+void xrt_md_pack(struct device *dev, char *blob);
+char *xrt_md_dup(struct device *dev, const char *blob);
+int xrt_md_get_intf_uuids(struct device *dev, const char *blob,
+			  u32 *num_uuids, uuid_t *intf_uuids);
+static inline int xrt_md_copy_all_endpoints(struct device *dev, char *blob, const char *src_blob)
+{
+	return xrt_md_copy_endpoint(dev, blob, src_blob, XRT_MD_NODE_ENDPOINTS,
+				    NULL, NULL);
+}
+
+/*
+ * Firmware provides 128 bit hash string as unque id of partition/interface.
+ * This string will be canonical textual representation in the future.
+ * Before that, introducing these two functions below to translate
+ * hash string to uuid_t for released hardware.
+ */
+static inline void xrt_md_trans_uuid2str(const uuid_t *uuid, char *uuidstr)
+{
+	int i, p;
+	u8 *u = (u8 *)uuid;
+
+	for (p = 0, i = sizeof(uuid_t) - 1; i >= 0; p++, i--)
+		(void)snprintf(&uuidstr[p * 2], 3, "%02x", u[i]);
+}
+
+static inline int xrt_md_trans_str2uuid(struct device *dev, const char *uuidstr, uuid_t *p_uuid)
+{
+	char *p;
+	const char *str;
+	char tmp[3] = { 0 };
+	int i, ret;
+
+	memset(p_uuid, 0, sizeof(*p_uuid));
+	p = (char *)p_uuid;
+	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);
+		if (ret)
+			return -EINVAL;
+		p++;
+		str -= 2;
+	}
+
+	return 0;
+}
+
+#endif
drivers/fpga/xrt/metadata/metadata.c new file +524 -0

524 lines not on this page · raw message

Signature · 1 line
2.18.4
reviewer
#7 lore raw

Re: [PATCH V3 XRT Alveo 02/18] fpga: xrt: driver metadata helper functions

Quoted 2 lines · Lizhi Hou

XRT drivers use device tree as metadata format to discover HW subsystems behind PCIe BAR. Thus libfdt functions are called for driver to parse

for the driver to parse the

Quoted 27 lines

device tree blob.

---
 drivers/fpga/xrt/include/metadata.h  | 229 ++++++++++++
 drivers/fpga/xrt/metadata/metadata.c | 524 +++++++++++++++++++++++++++
 2 files changed, 753 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 +9 -0
new file mode 100644
index 000000000000..b929bc469b73
@@ -0,0 +1,229 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Header file for Xilinx Runtime (XRT) driver
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *      Lizhi Hou <[email protected]>
+ */

drivers/fpga/xrt/include/metadata.h line 19

Quoted 10 lines
drivers/fpga/xrt/include/metadata.h new file +10 -0
+
+#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 (~0UL)
+

These #defines could be in alphabetical order

Some #define with embedded acronyms could be expanded

ex/ XRT_MD_NODE_CMC_REG , what is CMC ?

Quoted 94 lines
drivers/fpga/xrt/include/metadata.h new file +94 -0
+#define XRT_MD_PROP_COMPATIBLE "compatible"
+#define XRT_MD_PROP_PF_NUM "pcie_physical_function"
+#define XRT_MD_PROP_BAR_IDX "pcie_bar_mapping"
+#define XRT_MD_PROP_IO_OFFSET "reg"
+#define XRT_MD_PROP_INTERRUPTS "interrupts"
+#define XRT_MD_PROP_INTERFACE_UUID "interface_uuid"
+#define XRT_MD_PROP_LOGIC_UUID "logic_uuid"
+#define XRT_MD_PROP_VERSION_MAJOR "firmware_version_major"
+
+#define XRT_MD_PROP_HWICAP "axi_hwicap"
+#define XRT_MD_PROP_PDI_CONFIG "pdi_config_mem"
+
+#define XRT_MD_NODE_ENDPOINTS "addressable_endpoints"
+#define XRT_MD_INTERFACES_PATH "/interfaces"
+
+#define XRT_MD_NODE_FIRMWARE "firmware"
+#define XRT_MD_NODE_INTERFACES "interfaces"
+#define XRT_MD_NODE_PARTITION_INFO "partition_info"
+
+#define XRT_MD_NODE_FLASH "ep_card_flash_program_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_SYSMON "ep_cmp_sysmon_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_MGMT "ep_firewall_ctrl_mgmt_00"
+#define XRT_MD_NODE_AF_CTRL_USER "ep_firewall_ctrl_user_00"
+#define XRT_MD_NODE_AF_CTRL_DEBUG "ep_firewall_ctrl_debug_00"
+#define XRT_MD_NODE_AF_DATA_H2C "ep_firewall_data_h2c_00"
+#define XRT_MD_NODE_AF_DATA_C2H "ep_firewall_data_c2h_00"
+#define XRT_MD_NODE_AF_DATA_P2P "ep_firewall_data_p2p_00"
+#define XRT_MD_NODE_AF_DATA_M2M "ep_firewall_data_m2m_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_CMC_MUTEX "ep_cmc_mutex_00"
+#define XRT_MD_NODE_CMC_FW_MEM "ep_cmc_firmware_mem_00"
+#define XRT_MD_NODE_ERT_FW_MEM "ep_ert_firmware_mem_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_MAILBOX_MGMT "ep_mailbox_mgmt_00"
+#define XRT_MD_NODE_MAILBOX_USER "ep_mailbox_user_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_PCIE_MON "ep_pcie_link_mon_00"
+#define XRT_MD_NODE_DDR_CALIB "ep_ddr_mem_calib_00"
+#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_KDMA_CTRL "ep_kdma_ctrl_00"
+#define XRT_MD_NODE_FPGA_CONFIG "ep_fpga_configuration_00"
+#define XRT_MD_NODE_ERT_SCHED "ep_ert_sched_00"
+#define XRT_MD_NODE_XDMA "ep_xdma_00"
+#define XRT_MD_NODE_MSIX "ep_msix_00"
+#define XRT_MD_NODE_QDMA "ep_qdma_00"
+#define XRT_MD_XRT_MD_NODE_QDMA4 "ep_qdma4_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_CLK_SHUTDOWN "ep_aclk_shutdown_00"
+#define XRT_MD_NODE_ERT_BASE "ep_ert_base_address_00"
+#define XRT_MD_NODE_ERT_RESET "ep_ert_reset_00"
+#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_CLKFREQ_HBM "ep_freq_cnt_aclk_hbm_00"
+#define XRT_MD_NODE_GAPPING "ep_gapping_demand_00"
+#define XRT_MD_NODE_UCS_CONTROL_STATUS "ep_ucs_control_status_00"
+#define XRT_MD_NODE_P2P "ep_p2p_00"
+#define XRT_MD_NODE_REMAP_P2P "ep_remap_p2p_00"
+#define XRT_MD_NODE_DDR4_RESET_GATE "ep_ddr_mem_srsr_gate_00"
+#define XRT_MD_NODE_ADDR_TRANSLATOR "ep_remap_data_c2h_00"
+#define XRT_MD_NODE_MAILBOX_XRT "ep_mailbox_user_to_ert_00"
+#define XRT_MD_NODE_PMC_INTR   "ep_pmc_intr_00"
+#define XRT_MD_NODE_PMC_MUX    "ep_pmc_mux_00"
+
+/* driver defined endpoints */
+#define XRT_MD_NODE_VSEC "drv_ep_vsec_00"
+#define XRT_MD_NODE_VSEC_GOLDEN "drv_ep_vsec_golden_00"
+#define XRT_MD_NODE_BLP_ROM "drv_ep_blp_rom_00"
+#define XRT_MD_NODE_MAILBOX_VSEC "ep_mailbox_vsec_00"
+#define XRT_MD_NODE_PLAT_INFO "drv_ep_platform_info_mgmt_00"
+#define XRT_MD_NODE_TEST "drv_ep_test_00"
+#define XRT_MD_NODE_MGMT_MAIN "drv_ep_mgmt_main_00"
+#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_XRT_MD_NODE_PARTITION_INFO_BLP "partition_info_0"
+#define XRT_MD_XRT_MD_NODE_PARTITION_INFO_PLP "partition_info_1"
+
+#define XRT_MD_NODE_DDR_SRSR "drv_ep_ddr_srsr"
+#define XRT_MD_REGMAP_DDR_SRSR "drv_ddr_srsr"
+
+#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"

drivers/fpga/xrt/include/metadata.h line 123

Quoted 10 lines
drivers/fpga/xrt/include/metadata.h new file +10 -0
+#define XRT_MD_PROP_PARTITION_LEVEL "partition_level"
+
+struct xrt_md_endpoint {
+	const char	*ep_name;
+	u32		bar;
+	long		bar_off;
+	ulong		size;
+	char		*regmap;
+	char		*regmap_ver;
+};

could the existing 'struct regmap' be used here ?

Quoted 4 lines
drivers/fpga/xrt/include/metadata.h new file +4 -0
+
+/* Note: res_id is defined by leaf driver and must start with 0. */
+struct xrt_iores_map {
+	char		*res_name;

drivers/fpga/xrt/include/metadata.h line 137

Quoted 10 lines
drivers/fpga/xrt/include/metadata.h new file +10 -0
+	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 (!strcmp(res_name, res_map->res_name))

Use the 'n' variant strncmp for better safety.

Fix generally.

Quoted 40 lines
drivers/fpga/xrt/include/metadata.h new file +40 -0
+			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;
+}
+
+unsigned long xrt_md_size(struct device *dev, const char *blob);
+int xrt_md_create(struct device *dev, 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,
+			char *regmap_name);
+int xrt_md_get_prop(struct device *dev, const char *blob, const char *ep_name,
+		    const char *regmap_name, const char *prop,
+		    const void **val, int *size);
+int xrt_md_set_prop(struct device *dev, char *blob, const char *ep_name,
+		    const char *regmap_name, 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 *regmap_name,
+			 const char *new_ep_name);
+int xrt_md_get_next_endpoint(struct device *dev, const char *blob,
+			     const char *ep_name,  const char *regmap_name,
+			     char **next_ep, char **next_regmap);
+int xrt_md_get_compatible_endpoint(struct device *dev, const char *blob,
+				   const char *regmap_name, const char **ep_name);
+int xrt_md_find_endpoint(struct device *dev, const char *blob,

drivers/fpga/xrt/include/metadata.h line 187

Quoted 10 lines
drivers/fpga/xrt/include/metadata.h new file +10 -0
+			 const char *ep_name, const char *regmap_name,
+			 const char **epname);
+void xrt_md_pack(struct device *dev, char *blob);
+char *xrt_md_dup(struct device *dev, const char *blob);
+int xrt_md_get_intf_uuids(struct device *dev, const char *blob,
+			  u32 *num_uuids, uuid_t *intf_uuids);
+static inline int xrt_md_copy_all_endpoints(struct device *dev, char *blob, const char *src_blob)
+{
+	return xrt_md_copy_endpoint(dev, blob, src_blob, XRT_MD_NODE_ENDPOINTS,
+				    NULL, NULL);

A wrapping a single call seems like an unnecessary abstraction layer.

can this be reduced/removed ?

drivers/fpga/xrt/include/metadata.h line 191

Quoted 4 lines
drivers/fpga/xrt/include/metadata.h new file +4 -0
+}
+
+/*
+ * Firmware provides 128 bit hash string as unque id of partition/interface.

The firmware provides a 128 bit hash string as a unique id to the partition/interface.

Existing hw does not yet use the cononical form, so it is necessary to use a translation function.

drivers/fpga/xrt/include/metadata.h line 194

Quoted 3 lines
drivers/fpga/xrt/include/metadata.h new file +3 -0
+ * This string will be canonical textual representation in the future.
+ * Before that, introducing these two functions below to translate
+ * hash string to uuid_t for released hardware.

Is there an existing version string the new hw will use to check which way to go ?

drivers/fpga/xrt/include/metadata.h line 201

Quoted 7 lines
drivers/fpga/xrt/include/metadata.h new file +7 -0
+ */
+static inline void xrt_md_trans_uuid2str(const uuid_t *uuid, char *uuidstr)
+{
+	int i, p;
+	u8 *u = (u8 *)uuid;
+
+	for (p = 0, i = sizeof(uuid_t) - 1; i >= 0; p++, i--)

This loop needs to be improved.

Consider if sizeof(uuid_t) changed, accessing u[] would overflow.

drivers/fpga/xrt/include/metadata.h line 202

Quoted 1 line
drivers/fpga/xrt/include/metadata.h new file +1 -0
+		(void)snprintf(&uuidstr[p * 2], 3, "%02x", u[i]);

(void) cast isn't needed.

drivers/fpga/xrt/include/metadata.h line 213

Quoted 11 lines
drivers/fpga/xrt/include/metadata.h new file +11 -0
+}
+
+static inline int xrt_md_trans_str2uuid(struct device *dev, const char *uuidstr, uuid_t *p_uuid)
+{
+	char *p;
+	const char *str;
+	char tmp[3] = { 0 };
+	int i, ret;
+
+	memset(p_uuid, 0, sizeof(*p_uuid));
+	p = (char *)p_uuid;

access with p_uuid->b[] would remove use of 'p'

drivers/fpga/xrt/include/metadata.h line 216

Quoted 3 lines
drivers/fpga/xrt/include/metadata.h new file +3 -0
+	str = uuidstr + strlen(uuidstr) - 2;
+
+	for (i = 0; i < sizeof(*p_uuid) && str >= uuidstr; i++) {

is not filling p_uuid completely really ok ?

Quoted 32 lines
drivers/fpga/xrt/include/metadata.h new file +13 -0
+		tmp[0] = *str;
+		tmp[1] = *(str + 1);
+		ret = kstrtou8(tmp, 16, p);
+		if (ret)
+			return -EINVAL;
+		p++;
+		str -= 2;
+	}
+
+	return 0;
+}
+
+#endif
drivers/fpga/xrt/metadata/metadata.c new file +13 -0
new file mode 100644
index 000000000000..5d106396f438
@@ -0,0 +1,524 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Xilinx Alveo FPGA Metadata parse APIs
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *      Lizhi Hou <[email protected]>
+ */
+
+#include <linux/libfdt_env.h>
+#include "libfdt.h"
+#include "metadata.h"

drivers/fpga/xrt/metadata/metadata.c line 23

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+
+#define MAX_BLOB_SIZE	(4096 * 25)
+
+static int xrt_md_setprop(struct device *dev, char *blob, int offset,
+			  const char *prop, const void *val, int size);
+static int xrt_md_overlay(struct device *dev, char *blob, int target,
+			  const char *overlay_blob, int overlay_offset);
+static int xrt_md_get_endpoint(struct device *dev, const char *blob,
+			       const char *ep_name, const char *regmap_name,
+			       int *ep_offset);

consider reordering functions so these fwd decl's are not needed

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+
+unsigned long xrt_md_size(struct device *dev, const char *blob)
+{
+	unsigned long len = (long)fdt_totalsize(blob);
+
+	len = (len > MAX_BLOB_SIZE) ? XRT_MD_INVALID_LENGTH : len;
+	return len;

drivers/fpga/xrt/metadata/metadata.c line 40

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+}
+EXPORT_SYMBOL_GPL(xrt_md_size);
+
+int xrt_md_create(struct device *dev, char **blob)
+{
+	int ret = 0;
+
+	WARN_ON(!blob);
+
+	*blob = vmalloc(MAX_BLOB_SIZE);

why vmalloc instead of vzalloc ?

drivers/fpga/xrt/metadata/metadata.c line 50

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+	if (!*blob)
+		return -ENOMEM;
+
+	ret = fdt_create_empty_tree(*blob, MAX_BLOB_SIZE);
+	if (ret) {
+		dev_err(dev, "format blob failed, ret = %d", ret);
+		goto failed;
+	}
+
+	ret = fdt_next_node(*blob, -1, NULL);

A variable called 'offset' would make more sense here because it is used later in fdt_add_subnode as the parentoffset parameter.

drivers/fpga/xrt/metadata/metadata.c line 56

Quoted 6 lines
drivers/fpga/xrt/metadata/metadata.c new file +6 -0
+	if (ret < 0) {
+		dev_err(dev, "No Node, ret = %d", ret);
+		goto failed;
+	}
+
+	ret = fdt_add_subnode(*blob, ret, XRT_MD_NODE_ENDPOINTS);

why isn't the parentoffset '0' ?

Quoted 79 lines
drivers/fpga/xrt/metadata/metadata.c new file +79 -0
+	if (ret < 0) {
+		dev_err(dev, "add node failed, ret = %d", ret);
+		goto failed;
+	}
+
+	return 0;
+
+failed:
+	vfree(*blob);
+	*blob = NULL;
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_create);
+
+static int xrt_md_add_node(struct device *dev, char *blob, int parent_offset,
+			   const char *ep_name)
+{
+	int ret;
+
+	ret = fdt_add_subnode(blob, parent_offset, ep_name);
+	if (ret < 0 && ret != -FDT_ERR_EXISTS)
+		dev_err(dev, "failed to add node %s. %d", ep_name, ret);
+
+	return ret;
+}
+
+int xrt_md_del_endpoint(struct device *dev, char *blob, const char *ep_name,
+			char *regmap_name)
+{
+	int ret;
+	int ep_offset;
+
+	ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name, &ep_offset);
+	if (ret) {
+		dev_err(dev, "can not find ep %s", ep_name);
+		return -EINVAL;
+	}
+
+	ret = fdt_del_node(blob, ep_offset);
+	if (ret)
+		dev_err(dev, "delete node %s failed, ret %d", ep_name, ret);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_del_endpoint);
+
+static int __xrt_md_add_endpoint(struct device *dev, char *blob,
+				 struct xrt_md_endpoint *ep, int *offset, bool root)
+{
+	int ret = 0;
+	int ep_offset = 0;
+	u32 val, count = 0;
+	u64 io_range[2];
+	char comp[128];
+
+	if (!ep->ep_name) {
+		dev_err(dev, "empty name");
+		return -EINVAL;
+	}
+
+	if (!root) {
+		ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_ENDPOINTS, NULL,
+					  &ep_offset);
+		if (ret) {
+			dev_err(dev, "invalid blob, ret = %d", ret);
+			return -EINVAL;
+		}
+	}
+
+	ep_offset = xrt_md_add_node(dev, blob, ep_offset, ep->ep_name);
+	if (ep_offset < 0) {
+		dev_err(dev, "add endpoint failed, ret = %d", ret);
+		return -EINVAL;
+	}
+	if (offset)
+		*offset = ep_offset;
+
+	if (ep->size != 0) {

drivers/fpga/xrt/metadata/metadata.c line 145

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+		val = cpu_to_be32(ep->bar);
+		ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_BAR_IDX,
+				     &val, sizeof(u32));
+		if (ret) {
+			dev_err(dev, "set %s failed, ret %d",
+				XRT_MD_PROP_BAR_IDX, ret);
+			goto failed;
+		}
+		io_range[0] = cpu_to_be64((u64)ep->bar_off);
+		io_range[1] = cpu_to_be64((u64)ep->size);

is there a type mismatch between bar (u32) and size (u64) ?

Quoted 9 lines
drivers/fpga/xrt/metadata/metadata.c new file +9 -0
+		ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_IO_OFFSET,
+				     io_range, sizeof(io_range));
+		if (ret) {
+			dev_err(dev, "set %s failed, ret %d",
+				XRT_MD_PROP_IO_OFFSET, ret);
+			goto failed;
+		}
+	}
+

drivers/fpga/xrt/metadata/metadata.c line 164

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+	if (ep->regmap) {
+		if (ep->regmap_ver) {
+			count = snprintf(comp, sizeof(comp),
+					 "%s-%s", ep->regmap, ep->regmap_ver);
+			count++;
+		}
+
+		count += snprintf(comp + count, sizeof(comp) - count,
+				  "%s", ep->regmap);
+		count++;

unlikely, but overflow is not checked.

are multiple null's in this string ok ?

Quoted 11 lines
drivers/fpga/xrt/metadata/metadata.c new file +11 -0
+
+		ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_COMPATIBLE,
+				     comp, count);
+		if (ret) {
+			dev_err(dev, "set %s failed, ret %d",
+				XRT_MD_PROP_COMPATIBLE, ret);
+			goto failed;
+		}
+	}
+
+failed:

drivers/fpga/xrt/metadata/metadata.c line 185

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+	if (ret)
+		xrt_md_del_endpoint(dev, blob, ep->ep_name, NULL);
+
+	return ret;
+}
+
+int xrt_md_add_endpoint(struct device *dev, char *blob,
+			struct xrt_md_endpoint *ep)
+{
+	return __xrt_md_add_endpoint(dev, blob, ep, NULL, false);

ok, user doesn't add root's endpoint.

could an assert be added ?

Quoted 5 lines
drivers/fpga/xrt/metadata/metadata.c new file +5 -0
+}
+EXPORT_SYMBOL_GPL(xrt_md_add_endpoint);
+
+static int xrt_md_get_endpoint(struct device *dev, const char *blob,
+			       const char *ep_name, const char *regmap_name,

drivers/fpga/xrt/metadata/metadata.c line 200

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+			       int *ep_offset)
+{
+	int offset;
+	const char *name;
+
+	for (offset = fdt_next_node(blob, -1, NULL);
+	    offset >= 0;
+	    offset = fdt_next_node(blob, offset, NULL)) {
+		name = fdt_get_name(blob, offset, NULL);
+		if (!name || strncmp(name, ep_name, strlen(ep_name) + 1))

strlen() + 1 ?

Quoted 13 lines
drivers/fpga/xrt/metadata/metadata.c new file +13 -0
+			continue;
+		if (!regmap_name ||
+		    !fdt_node_check_compatible(blob, offset, regmap_name))
+			break;
+	}
+	if (offset < 0)
+		return -ENODEV;
+
+	*ep_offset = offset;
+
+	return 0;
+}
+

drivers/fpga/xrt/metadata/metadata.c line 223

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+int xrt_md_find_endpoint(struct device *dev, const char *blob,
+			 const char *ep_name, const char *regmap_name,
+			 const char **epname)
+{
+	int offset;
+	int ret;
+
+	ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+				  &offset);
+	if (!ret && epname && offset >= 0)

The offset >= 0 check isn't needed, !ret is enough

Quoted 5 lines
drivers/fpga/xrt/metadata/metadata.c new file +5 -0
+		*epname = fdt_get_name(blob, offset, NULL);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_find_endpoint);

drivers/fpga/xrt/metadata/metadata.c line 238

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+
+int xrt_md_get_prop(struct device *dev, const char *blob, const char *ep_name,
+		    const char *regmap_name, const char *prop,
+		    const void **val, int *size)
+{
+	int offset;
+	int ret;
+
+	if (val)
+		*val = NULL;

Seems like no point in making this call if !val

Return -EINVAL if !val and remove the if (val) check below.

Quoted 52 lines
drivers/fpga/xrt/metadata/metadata.c new file +52 -0
+	if (ep_name) {
+		ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+					  &offset);
+		if (ret) {
+			dev_err(dev, "cannot get ep %s, regmap %s, ret = %d",
+				ep_name, regmap_name, ret);
+			return -EINVAL;
+		}
+	} else {
+		offset = fdt_next_node(blob, -1, NULL);
+		if (offset < 0) {
+			dev_err(dev, "internal error, ret = %d", offset);
+			return -EINVAL;
+		}
+	}
+
+	if (val) {
+		*val = fdt_getprop(blob, offset, prop, size);
+		if (!*val) {
+			dev_dbg(dev, "get ep %s, prop %s failed", ep_name, prop);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_prop);
+
+static int xrt_md_setprop(struct device *dev, char *blob, int offset,
+			  const char *prop, const void *val, int size)
+{
+	int ret;
+
+	ret = fdt_setprop(blob, offset, prop, val, size);
+	if (ret)
+		dev_err(dev, "failed to set prop %d", ret);
+
+	return ret;
+}
+
+int xrt_md_set_prop(struct device *dev, char *blob,
+		    const char *ep_name, const char *regmap_name,
+		    const char *prop, const void *val, int size)
+{
+	int offset;
+	int ret;
+
+	if (ep_name) {
+		ret = xrt_md_get_endpoint(dev, blob, ep_name,
+					  regmap_name, &offset);
+		if (ret) {
+			dev_err(dev, "cannot get node %s, ret = %d",

drivers/fpga/xrt/metadata/metadata.c line 300

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+				ep_name, ret);
+			return -EINVAL;
+		}
+	} else {
+		offset = fdt_next_node(blob, -1, NULL);
+		if (offset < 0) {
+			dev_err(dev, "internal error, ret = %d", offset);
+			return -EINVAL;
+		}
+	}

This if-else block is a cut-n-paste from above.

It is good to convert common logic blocks to macros or inline functions.

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+
+	ret = xrt_md_setprop(dev, blob, offset, prop, val, size);
+	if (ret)
+		dev_err(dev, "set prop %s failed, ret = %d", prop, ret);
+
+	return ret;
+}

drivers/fpga/xrt/metadata/metadata.c line 317

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+EXPORT_SYMBOL_GPL(xrt_md_set_prop);
+
+int xrt_md_copy_endpoint(struct device *dev, char *blob, const char *src_blob,
+			 const char *ep_name, const char *regmap_name,
+			 const char *new_ep_name)
+{
+	int offset, target;
+	int ret;
+	struct xrt_md_endpoint ep = {0};
+	const char *newepnm = new_ep_name ? new_ep_name : ep_name;

How is this valid ?

The xrt_md_get_endpoint searches by ep_name and if there names are not unique the second one will never be found.

Quoted 22 lines
drivers/fpga/xrt/metadata/metadata.c new file +22 -0
+
+	ret = xrt_md_get_endpoint(dev, src_blob, ep_name, regmap_name,
+				  &offset);
+	if (ret)
+		return -EINVAL;
+
+	ret = xrt_md_get_endpoint(dev, blob, newepnm, regmap_name, &target);
+	if (ret) {
+		ep.ep_name = newepnm;
+		ret = __xrt_md_add_endpoint(dev, blob, &ep, &target,
+					    fdt_parent_offset(src_blob, offset) == 0);
+		if (ret)
+			return -EINVAL;
+	}
+
+	ret = xrt_md_overlay(dev, blob, target, src_blob, offset);
+	if (ret)
+		dev_err(dev, "overlay failed, ret = %d", ret);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_copy_endpoint);

drivers/fpga/xrt/metadata/metadata.c line 349

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+
+char *xrt_md_dup(struct device *dev, const char *blob)
+{
+	int ret;
+	char *dup_blob;
+
+	ret = xrt_md_create(dev, &dup_blob);
+	if (ret)
+		return NULL;
+	ret = xrt_md_overlay(dev, dup_blob, -1, blob, -1);

would memcpy-ing the blob work ?

Quoted 35 lines
drivers/fpga/xrt/metadata/metadata.c new file +35 -0
+	if (ret) {
+		vfree(dup_blob);
+		return NULL;
+	}
+
+	return dup_blob;
+}
+EXPORT_SYMBOL_GPL(xrt_md_dup);
+
+static int xrt_md_overlay(struct device *dev, char *blob, int target,
+			  const char *overlay_blob, int overlay_offset)
+{
+	int	property, subnode;
+	int	ret;
+
+	WARN_ON(!blob || !overlay_blob);
+
+	if (!blob) {
+		dev_err(dev, "blob is NULL");
+		return -EINVAL;
+	}
+
+	if (target < 0) {
+		target = fdt_next_node(blob, -1, NULL);
+		if (target < 0) {
+			dev_err(dev, "invalid target");
+			return -EINVAL;
+		}
+	}
+	if (overlay_offset < 0) {
+		overlay_offset = fdt_next_node(overlay_blob, -1, NULL);
+		if (overlay_offset < 0) {
+			dev_err(dev, "invalid overlay");
+			return -EINVAL;
+		}

drivers/fpga/xrt/metadata/metadata.c line 394

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+	}
+
+	fdt_for_each_property_offset(property, overlay_blob, overlay_offset) {
+		const char *name;
+		const void *prop;
+		int prop_len;
+
+		prop = fdt_getprop_by_offset(overlay_blob, property, &name,
+					     &prop_len);
+		if (!prop || prop_len >= MAX_BLOB_SIZE) {

could add prop_len < 0 as another sanity check

drivers/fpga/xrt/metadata/metadata.c line 403

Quoted 9 lines
drivers/fpga/xrt/metadata/metadata.c new file +9 -0
+			dev_err(dev, "internal error");
+			return -EINVAL;
+		}
+
+		ret = xrt_md_setprop(dev, blob, target, name, prop,
+				     prop_len);
+		if (ret) {
+			dev_err(dev, "setprop failed, ret = %d", ret);
+			return ret;

overlay_blob is half done, as an error handling shouldn't it be undone ?

Quoted 6 lines
drivers/fpga/xrt/metadata/metadata.c new file +6 -0
+		}
+	}
+
+	fdt_for_each_subnode(subnode, overlay_blob, overlay_offset) {
+		const char *name = fdt_get_name(overlay_blob, subnode, NULL);
+		int nnode;

drivers/fpga/xrt/metadata/metadata.c line 419

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+
+		nnode = xrt_md_add_node(dev, blob, target, name);
+		if (nnode == -FDT_ERR_EXISTS)
+			nnode = fdt_subnode_offset(blob, target, name);
+		if (nnode < 0) {
+			dev_err(dev, "add node failed, ret = %d", nnode);
+			return nnode;
+		}
+
+		ret = xrt_md_overlay(dev, blob, nnode, overlay_blob, subnode);

eek, recursion.

Any chance this will blow the stack ?

Quoted 14 lines
drivers/fpga/xrt/metadata/metadata.c new file +14 -0
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+int xrt_md_get_next_endpoint(struct device *dev, const char *blob,
+			     const char *ep_name, const char *regmap_name,
+			     char **next_ep, char **next_regmap)
+{
+	int offset, ret;
+
+	if (!ep_name) {

drivers/fpga/xrt/metadata/metadata.c line 443

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+		ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_ENDPOINTS, NULL,
+					  &offset);
+	} else {
+		ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+					  &offset);
+	}
+
+	if (ret) {
+		*next_ep = NULL;
+		*next_regmap = NULL;

could initialize next_ep and next_regmap to NULL outside the check.

Quoted 21 lines
drivers/fpga/xrt/metadata/metadata.c new file +21 -0
+		return -EINVAL;
+	}
+
+	offset = ep_name ? fdt_next_subnode(blob, offset) :
+		fdt_first_subnode(blob, offset);
+	if (offset < 0) {
+		*next_ep = NULL;
+		*next_regmap = NULL;
+		return -EINVAL;
+	}
+
+	*next_ep = (char *)fdt_get_name(blob, offset, NULL);
+	*next_regmap = (char *)fdt_stringlist_get(blob, offset, XRT_MD_PROP_COMPATIBLE,
+						  0, NULL);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_next_endpoint);
+
+int xrt_md_get_compatible_endpoint(struct device *dev, const char *blob,
+				   const char *regmap_name, const char **ep_name)

drivers/fpga/xrt/metadata/metadata.c line 474

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+{
+	int ep_offset;
+
+	ep_offset = fdt_node_offset_by_compatible(blob, -1, regmap_name);
+	if (ep_offset < 0) {
+		*ep_name = NULL;
+		return -ENOENT;
+	}
+
+	*ep_name = (char *)fdt_get_name(blob, ep_offset, NULL);

why the cast ?

drivers/fpga/xrt/metadata/metadata.c line 486

Quoted 12 lines
drivers/fpga/xrt/metadata/metadata.c new file +12 -0
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_compatible_endpoint);
+
+void xrt_md_pack(struct device *dev, char *blob)
+{
+	int ret;
+
+	ret = fdt_pack(blob);
+	if (ret)
+		dev_err(dev, "pack failed %d", ret);

maybe return int

drivers/fpga/xrt/metadata/metadata.c line 489

Quoted 3 lines
drivers/fpga/xrt/metadata/metadata.c new file +3 -0
+}
+EXPORT_SYMBOL_GPL(xrt_md_pack);
+

The input/output of num_uuids parameter is tricky add a comment

drivers/fpga/xrt/metadata/metadata.c line 491

Quoted 2 lines
drivers/fpga/xrt/metadata/metadata.c new file +2 -0
+int xrt_md_get_intf_uuids(struct device *dev, const char *blob,
+			  u32 *num_uuids, uuid_t *intf_uuids)

what is intf ? change to 'interface' ?

Quoted 16 lines
drivers/fpga/xrt/metadata/metadata.c new file +16 -0
+{
+	int offset, count = 0;
+	int ret;
+	const char *uuid_str;
+
+	ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_INTERFACES, NULL, &offset);
+	if (ret)
+		return -ENOENT;
+
+	for (offset = fdt_first_subnode(blob, offset);
+	    offset >= 0;
+	    offset = fdt_next_subnode(blob, offset)) {
+		uuid_str = fdt_getprop(blob, offset, XRT_MD_PROP_INTERFACE_UUID,
+				       NULL);
+		if (!uuid_str) {
+			dev_err(dev, "empty intf uuid node");

drivers/fpga/xrt/metadata/metadata.c line 517

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+			return -EINVAL;
+		}
+
+		if (intf_uuids && count < *num_uuids) {
+			ret = xrt_md_trans_str2uuid(dev, uuid_str,
+						    &intf_uuids[count]);
+			if (ret)
+				return -EINVAL;
+		}
+		count++;

keep going even when count > num_uuids ?

that seems like an error.

Tom

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+	}
+
+	*num_uuids = count;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_intf_uuids);
author
#8 lore raw

Re: [PATCH V3 XRT Alveo 02/18] fpga: xrt: driver metadata helper functions

Hi Tom,

Quoted 4 lines · Tom Rix
Quoted 2 lines · Lizhi Hou

XRT drivers use device tree as metadata format to discover HW subsystems behind PCIe BAR. Thus libfdt functions are called for driver to parse

for the driver to parse the

will fix.

Quoted 39 lines
Quoted 27 lines

device tree blob.

---
  drivers/fpga/xrt/include/metadata.h  | 229 ++++++++++++
  drivers/fpga/xrt/metadata/metadata.c | 524 +++++++++++++++++++++++++++
  2 files changed, 753 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 +9 -0
new file mode 100644
index 000000000000..b929bc469b73
@@ -0,0 +1,229 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Header file for Xilinx Runtime (XRT) driver
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *      Lizhi Hou <[email protected]>
+ */

drivers/fpga/xrt/include/metadata.h line 19

Quoted 10 lines
drivers/fpga/xrt/include/metadata.h new file +10 -0
+
+#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 (~0UL)
+

These #defines could be in alphabetical order

Quoted 3 lines

Some #define with embedded acronyms could be expanded

ex/ XRT_MD_NODE_CMC_REG , what is CMC ?

Will reorder. Expanding might make macro name too long. I will add comment as below:
/*
* IP nodes
* AF: AXI Firewall
* CMC: Card Management Controller
* ERT: Embedded Runtime
* PLP: Provider Reconfigurable Partition
* ULP: User Reconfigurable Partition
*/

Quoted 98 lines
Quoted 98 lines
drivers/fpga/xrt/include/metadata.h new file +97 -0
+#define XRT_MD_PROP_COMPATIBLE "compatible"
+#define XRT_MD_PROP_PF_NUM "pcie_physical_function"
+#define XRT_MD_PROP_BAR_IDX "pcie_bar_mapping"
+#define XRT_MD_PROP_IO_OFFSET "reg"
+#define XRT_MD_PROP_INTERRUPTS "interrupts"
+#define XRT_MD_PROP_INTERFACE_UUID "interface_uuid"
+#define XRT_MD_PROP_LOGIC_UUID "logic_uuid"
+#define XRT_MD_PROP_VERSION_MAJOR "firmware_version_major"
+
+#define XRT_MD_PROP_HWICAP "axi_hwicap"
+#define XRT_MD_PROP_PDI_CONFIG "pdi_config_mem"
+
+#define XRT_MD_NODE_ENDPOINTS "addressable_endpoints"
+#define XRT_MD_INTERFACES_PATH "/interfaces"
+
+#define XRT_MD_NODE_FIRMWARE "firmware"
+#define XRT_MD_NODE_INTERFACES "interfaces"
+#define XRT_MD_NODE_PARTITION_INFO "partition_info"
+
+#define XRT_MD_NODE_FLASH "ep_card_flash_program_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_SYSMON "ep_cmp_sysmon_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_MGMT "ep_firewall_ctrl_mgmt_00"
+#define XRT_MD_NODE_AF_CTRL_USER "ep_firewall_ctrl_user_00"
+#define XRT_MD_NODE_AF_CTRL_DEBUG "ep_firewall_ctrl_debug_00"
+#define XRT_MD_NODE_AF_DATA_H2C "ep_firewall_data_h2c_00"
+#define XRT_MD_NODE_AF_DATA_C2H "ep_firewall_data_c2h_00"
+#define XRT_MD_NODE_AF_DATA_P2P "ep_firewall_data_p2p_00"
+#define XRT_MD_NODE_AF_DATA_M2M "ep_firewall_data_m2m_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_CMC_MUTEX "ep_cmc_mutex_00"
+#define XRT_MD_NODE_CMC_FW_MEM "ep_cmc_firmware_mem_00"
+#define XRT_MD_NODE_ERT_FW_MEM "ep_ert_firmware_mem_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_MAILBOX_MGMT "ep_mailbox_mgmt_00"
+#define XRT_MD_NODE_MAILBOX_USER "ep_mailbox_user_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_PCIE_MON "ep_pcie_link_mon_00"
+#define XRT_MD_NODE_DDR_CALIB "ep_ddr_mem_calib_00"
+#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_KDMA_CTRL "ep_kdma_ctrl_00"
+#define XRT_MD_NODE_FPGA_CONFIG "ep_fpga_configuration_00"
+#define XRT_MD_NODE_ERT_SCHED "ep_ert_sched_00"
+#define XRT_MD_NODE_XDMA "ep_xdma_00"
+#define XRT_MD_NODE_MSIX "ep_msix_00"
+#define XRT_MD_NODE_QDMA "ep_qdma_00"
+#define XRT_MD_XRT_MD_NODE_QDMA4 "ep_qdma4_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_CLK_SHUTDOWN "ep_aclk_shutdown_00"
+#define XRT_MD_NODE_ERT_BASE "ep_ert_base_address_00"
+#define XRT_MD_NODE_ERT_RESET "ep_ert_reset_00"
+#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_CLKFREQ_HBM "ep_freq_cnt_aclk_hbm_00"
+#define XRT_MD_NODE_GAPPING "ep_gapping_demand_00"
+#define XRT_MD_NODE_UCS_CONTROL_STATUS "ep_ucs_control_status_00"
+#define XRT_MD_NODE_P2P "ep_p2p_00"
+#define XRT_MD_NODE_REMAP_P2P "ep_remap_p2p_00"
+#define XRT_MD_NODE_DDR4_RESET_GATE "ep_ddr_mem_srsr_gate_00"
+#define XRT_MD_NODE_ADDR_TRANSLATOR "ep_remap_data_c2h_00"
+#define XRT_MD_NODE_MAILBOX_XRT "ep_mailbox_user_to_ert_00"
+#define XRT_MD_NODE_PMC_INTR   "ep_pmc_intr_00"
+#define XRT_MD_NODE_PMC_MUX    "ep_pmc_mux_00"
+
+/* driver defined endpoints */
+#define XRT_MD_NODE_VSEC "drv_ep_vsec_00"
+#define XRT_MD_NODE_VSEC_GOLDEN "drv_ep_vsec_golden_00"
+#define XRT_MD_NODE_BLP_ROM "drv_ep_blp_rom_00"
+#define XRT_MD_NODE_MAILBOX_VSEC "ep_mailbox_vsec_00"
+#define XRT_MD_NODE_PLAT_INFO "drv_ep_platform_info_mgmt_00"
+#define XRT_MD_NODE_TEST "drv_ep_test_00"
+#define XRT_MD_NODE_MGMT_MAIN "drv_ep_mgmt_main_00"
+#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_XRT_MD_NODE_PARTITION_INFO_BLP "partition_info_0"
+#define XRT_MD_XRT_MD_NODE_PARTITION_INFO_PLP "partition_info_1"
+
+#define XRT_MD_NODE_DDR_SRSR "drv_ep_ddr_srsr"
+#define XRT_MD_REGMAP_DDR_SRSR "drv_ddr_srsr"
+
+#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 {
Quoted 8 lines

drivers/fpga/xrt/include/metadata.h line 123

Quoted 7 lines
drivers/fpga/xrt/include/metadata.h new file +7 -0
+     const char      *ep_name;
+     u32             bar;
+     long            bar_off;
+     ulong           size;
+     char            *regmap;
+     char            *regmap_ver;
+};

could the existing 'struct regmap' be used here ?

This structure is just a direct translation of firmware node. It is simple description of hardware endpoint.

Quoted 16 lines
Quoted 4 lines
drivers/fpga/xrt/include/metadata.h new file +4 -0
+
+/* Note: res_id is defined by leaf driver and must start with 0. */
+struct xrt_iores_map {
+     char            *res_name;

drivers/fpga/xrt/include/metadata.h line 137

Quoted 10 lines
drivers/fpga/xrt/include/metadata.h new file +10 -0
+     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 (!strcmp(res_name, res_map->res_name))

Use the 'n' variant strncmp for better safety.

Quoted 1 line

Fix generally.

Will replace all strcmp()

Quoted 53 lines
Quoted 41 lines
drivers/fpga/xrt/include/metadata.h new file +40 -0
+                     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;
+}
+
+unsigned long xrt_md_size(struct device *dev, const char *blob);
+int xrt_md_create(struct device *dev, 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,
+                     char *regmap_name);
+int xrt_md_get_prop(struct device *dev, const char *blob, const char *ep_name,
+                 const char *regmap_name, const char *prop,
+                 const void **val, int *size);
+int xrt_md_set_prop(struct device *dev, char *blob, const char *ep_name,
+                 const char *regmap_name, 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 *regmap_name,
+                      const char *new_ep_name);
+int xrt_md_get_next_endpoint(struct device *dev, const char *blob,
+                          const char *ep_name,  const char *regmap_name,
+                          char **next_ep, char **next_regmap);
+int xrt_md_get_compatible_endpoint(struct device *dev, const char *blob,
+                                const char *regmap_name, const char **ep_name);
+int xrt_md_find_endpoint(struct device *dev, const char *blob,

drivers/fpga/xrt/include/metadata.h line 187

Quoted 10 lines
drivers/fpga/xrt/include/metadata.h new file +10 -0
+                      const char *ep_name, const char *regmap_name,
+                      const char **epname);
+void xrt_md_pack(struct device *dev, char *blob);
+char *xrt_md_dup(struct device *dev, const char *blob);
+int xrt_md_get_intf_uuids(struct device *dev, const char *blob,
+                       u32 *num_uuids, uuid_t *intf_uuids);
+static inline int xrt_md_copy_all_endpoints(struct device *dev, char *blob, const char *src_blob)
+{
+     return xrt_md_copy_endpoint(dev, blob, src_blob, XRT_MD_NODE_ENDPOINTS,
+                                 NULL, NULL);

A wrapping a single call seems like an unnecessary abstraction layer.

Quoted 1 line

can this be reduced/removed ?

Will remove.

Quoted 8 lines

drivers/fpga/xrt/include/metadata.h line 191

Quoted 5 lines
drivers/fpga/xrt/include/metadata.h new file +4 -0
+}
+
+/*
+ * Firmware provides 128 bit hash string as unque id of partition/interface.

The firmware provides a 128 bit hash string as a unique id to the partition/interface.

Existing hw does not yet use the cononical form, so it is necessary to use a translation function.

Will fix

Quoted 5 lines

drivers/fpga/xrt/include/metadata.h line 194

Quoted 4 lines
drivers/fpga/xrt/include/metadata.h new file +3 -0
+ * This string will be canonical textual representation in the future.
+ * Before that, introducing these two functions below to translate
+ * hash string to uuid_t for released hardware.

Is there an existing version string the new hw will use to check which way to go ?

We do not have the new hw yet.

Quoted 10 lines

drivers/fpga/xrt/include/metadata.h line 201

Quoted 7 lines
drivers/fpga/xrt/include/metadata.h new file +7 -0
+ */
+static inline void xrt_md_trans_uuid2str(const uuid_t *uuid, char *uuidstr)
+{
+     int i, p;
+     u8 *u = (u8 *)uuid;
+
+     for (p = 0, i = sizeof(uuid_t) - 1; i >= 0; p++, i--)

This loop needs to be improved.

Consider if sizeof(uuid_t) changed, accessing u[] would overflow.

Will fix all the issues of uuid2str()/str2uuid() you pointed out. The rough idea is to use export_uuid() and import_uuid().

Quoted 54 lines

drivers/fpga/xrt/include/metadata.h line 202

Quoted 2 lines
drivers/fpga/xrt/include/metadata.h new file +1 -0
+             (void)snprintf(&uuidstr[p * 2], 3, "%02x", u[i]);

(void) cast isn't needed.

drivers/fpga/xrt/include/metadata.h line 213

Quoted 11 lines
drivers/fpga/xrt/include/metadata.h new file +11 -0
+}
+
+static inline int xrt_md_trans_str2uuid(struct device *dev, const char *uuidstr, uuid_t *p_uuid)
+{
+     char *p;
+     const char *str;
+     char tmp[3] = { 0 };
+     int i, ret;
+
+     memset(p_uuid, 0, sizeof(*p_uuid));
+     p = (char *)p_uuid;

access with p_uuid->b[] would remove use of 'p'

drivers/fpga/xrt/include/metadata.h line 216

Quoted 3 lines
drivers/fpga/xrt/include/metadata.h new file +3 -0
+     str = uuidstr + strlen(uuidstr) - 2;
+
+     for (i = 0; i < sizeof(*p_uuid) && str >= uuidstr; i++) {

is not filling p_uuid completely really ok ?

Quoted 35 lines
drivers/fpga/xrt/include/metadata.h new file +13 -0
+             tmp[0] = *str;
+             tmp[1] = *(str + 1);
+             ret = kstrtou8(tmp, 16, p);
+             if (ret)
+                     return -EINVAL;
+             p++;
+             str -= 2;
+     }
+
+     return 0;
+}
+
+#endif
drivers/fpga/xrt/metadata/metadata.c new file +16 -0
new file mode 100644
index 000000000000..5d106396f438
@@ -0,0 +1,524 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Xilinx Alveo FPGA Metadata parse APIs
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *      Lizhi Hou <[email protected]>
+ */
+
+#include <linux/libfdt_env.h>
+#include "libfdt.h"
+#include "metadata.h"
+
+#define MAX_BLOB_SIZE        (4096 * 25)
+
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 23

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+static int xrt_md_setprop(struct device *dev, char *blob, int offset,
+                       const char *prop, const void *val, int size);
+static int xrt_md_overlay(struct device *dev, char *blob, int target,
+                       const char *overlay_blob, int overlay_offset);
+static int xrt_md_get_endpoint(struct device *dev, const char *blob,
+                            const char *ep_name, const char *regmap_name,
+                            int *ep_offset);

consider reordering functions so these fwd decl's are not needed

Will remove decl's.

Quoted 10 lines
Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+
+unsigned long xrt_md_size(struct device *dev, const char *blob)
+{
+     unsigned long len = (long)fdt_totalsize(blob);
+
+     len = (len > MAX_BLOB_SIZE) ? XRT_MD_INVALID_LENGTH : len;
+     return len;
+}
+EXPORT_SYMBOL_GPL(xrt_md_size);
+
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 40

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+int xrt_md_create(struct device *dev, char **blob)
+{
+     int ret = 0;
+
+     WARN_ON(!blob);
+
+     *blob = vmalloc(MAX_BLOB_SIZE);

why vmalloc instead of vzalloc ?

Will use vzalloc.

Quoted 11 lines

drivers/fpga/xrt/metadata/metadata.c line 50

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+     if (!*blob)
+             return -ENOMEM;
+
+     ret = fdt_create_empty_tree(*blob, MAX_BLOB_SIZE);
+     if (ret) {
+             dev_err(dev, "format blob failed, ret = %d", ret);
+             goto failed;
+     }
+
+     ret = fdt_next_node(*blob, -1, NULL);

A variable called 'offset' would make more sense here because it is used later in fdt_add_subnode as the parentoffset parameter.

the following fdt_add_subnode should use 0 as the parentoffset parameter. So I will keep 'ret' here.

Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 56

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +6 -0
+     if (ret < 0) {
+             dev_err(dev, "No Node, ret = %d", ret);
+             goto failed;
+     }
+
+     ret = fdt_add_subnode(*blob, ret, XRT_MD_NODE_ENDPOINTS);

why isn't the parentoffset '0' ?

Yes, 0 should be used as parentoffset.

Quoted 84 lines
Quoted 84 lines
drivers/fpga/xrt/metadata/metadata.c new file +82 -0
+     if (ret < 0) {
+             dev_err(dev, "add node failed, ret = %d", ret);
+             goto failed;
+     }
+
+     return 0;
+
+failed:
+     vfree(*blob);
+     *blob = NULL;
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_create);
+
+static int xrt_md_add_node(struct device *dev, char *blob, int parent_offset,
+                        const char *ep_name)
+{
+     int ret;
+
+     ret = fdt_add_subnode(blob, parent_offset, ep_name);
+     if (ret < 0 && ret != -FDT_ERR_EXISTS)
+             dev_err(dev, "failed to add node %s. %d", ep_name, ret);
+
+     return ret;
+}
+
+int xrt_md_del_endpoint(struct device *dev, char *blob, const char *ep_name,
+                     char *regmap_name)
+{
+     int ret;
+     int ep_offset;
+
+     ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name, &ep_offset);
+     if (ret) {
+             dev_err(dev, "can not find ep %s", ep_name);
+             return -EINVAL;
+     }
+
+     ret = fdt_del_node(blob, ep_offset);
+     if (ret)
+             dev_err(dev, "delete node %s failed, ret %d", ep_name, ret);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_del_endpoint);
+
+static int __xrt_md_add_endpoint(struct device *dev, char *blob,
+                              struct xrt_md_endpoint *ep, int *offset, bool root)
+{
+     int ret = 0;
+     int ep_offset = 0;
+     u32 val, count = 0;
+     u64 io_range[2];
+     char comp[128];
+
+     if (!ep->ep_name) {
+             dev_err(dev, "empty name");
+             return -EINVAL;
+     }
+
+     if (!root) {
+             ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_ENDPOINTS, NULL,
+                                       &ep_offset);
+             if (ret) {
+                     dev_err(dev, "invalid blob, ret = %d", ret);
+                     return -EINVAL;
+             }
+     }
+
+     ep_offset = xrt_md_add_node(dev, blob, ep_offset, ep->ep_name);
+     if (ep_offset < 0) {
+             dev_err(dev, "add endpoint failed, ret = %d", ret);
+             return -EINVAL;
+     }
+     if (offset)
+             *offset = ep_offset;
+
+     if (ep->size != 0) {
+             val = cpu_to_be32(ep->bar);
+             ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_BAR_IDX,
+                                  &val, sizeof(u32));
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 145

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+             if (ret) {
+                     dev_err(dev, "set %s failed, ret %d",
+                             XRT_MD_PROP_BAR_IDX, ret);
+                     goto failed;
+             }
+             io_range[0] = cpu_to_be64((u64)ep->bar_off);
+             io_range[1] = cpu_to_be64((u64)ep->size);

is there a type mismatch between bar (u32) and size (u64) ?

Do you mean ep->bar? 'ep->bar' is bar index. So it does not have type mismatch issue.

Quoted 12 lines
Quoted 12 lines
drivers/fpga/xrt/metadata/metadata.c new file +12 -0
+             ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_IO_OFFSET,
+                                  io_range, sizeof(io_range));
+             if (ret) {
+                     dev_err(dev, "set %s failed, ret %d",
+                             XRT_MD_PROP_IO_OFFSET, ret);
+                     goto failed;
+             }
+     }
+
+     if (ep->regmap) {
+             if (ep->regmap_ver) {
+                     count = snprintf(comp, sizeof(comp),
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 164

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+                                      "%s-%s", ep->regmap, ep->regmap_ver);
+                     count++;
+             }
+
+             count += snprintf(comp + count, sizeof(comp) - count,
+                               "%s", ep->regmap);
+             count++;

unlikely, but overflow is not checked.

Will add overflow check.

Quoted 2 lines

are multiple null's in this string ok ?

yes.

Quoted 24 lines
Quoted 12 lines
drivers/fpga/xrt/metadata/metadata.c new file +11 -0
+
+             ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_COMPATIBLE,
+                                  comp, count);
+             if (ret) {
+                     dev_err(dev, "set %s failed, ret %d",
+                             XRT_MD_PROP_COMPATIBLE, ret);
+                     goto failed;
+             }
+     }
+
+failed:

drivers/fpga/xrt/metadata/metadata.c line 185

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+     if (ret)
+             xrt_md_del_endpoint(dev, blob, ep->ep_name, NULL);
+
+     return ret;
+}
+
+int xrt_md_add_endpoint(struct device *dev, char *blob,
+                     struct xrt_md_endpoint *ep)
+{
+     return __xrt_md_add_endpoint(dev, blob, ep, NULL, false);

ok, user doesn't add root's endpoint.

Quoted 1 line

could an assert be added ?

The word 'root' is misleading here. Will change this argument to 'char *parent'

Quoted 17 lines
Quoted 6 lines
drivers/fpga/xrt/metadata/metadata.c new file +5 -0
+}
+EXPORT_SYMBOL_GPL(xrt_md_add_endpoint);
+
+static int xrt_md_get_endpoint(struct device *dev, const char *blob,
+                            const char *ep_name, const char *regmap_name,

drivers/fpga/xrt/metadata/metadata.c line 200

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+                            int *ep_offset)
+{
+     int offset;
+     const char *name;
+
+     for (offset = fdt_next_node(blob, -1, NULL);
+         offset >= 0;
+         offset = fdt_next_node(blob, offset, NULL)) {
+             name = fdt_get_name(blob, offset, NULL);
+             if (!name || strncmp(name, ep_name, strlen(ep_name) + 1))

strlen() + 1 ?

This is to make sure "12345" and "123" are different.

Quoted 16 lines
Quoted 16 lines
drivers/fpga/xrt/metadata/metadata.c new file +16 -0
+                     continue;
+             if (!regmap_name ||
+                 !fdt_node_check_compatible(blob, offset, regmap_name))
+                     break;
+     }
+     if (offset < 0)
+             return -ENODEV;
+
+     *ep_offset = offset;
+
+     return 0;
+}
+
+int xrt_md_find_endpoint(struct device *dev, const char *blob,
+                      const char *ep_name, const char *regmap_name,
+                      const char **epname)
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 223

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+{
+     int offset;
+     int ret;
+
+     ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+                               &offset);
+     if (!ret && epname && offset >= 0)

The offset >= 0 check isn't needed, !ret is enough

Will fix.

Quoted 17 lines
Quoted 5 lines
drivers/fpga/xrt/metadata/metadata.c new file +5 -0
+             *epname = fdt_get_name(blob, offset, NULL);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_find_endpoint);

drivers/fpga/xrt/metadata/metadata.c line 238

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+
+int xrt_md_get_prop(struct device *dev, const char *blob, const char *ep_name,
+                 const char *regmap_name, const char *prop,
+                 const void **val, int *size)
+{
+     int offset;
+     int ret;
+
+     if (val)
+             *val = NULL;

Seems like no point in making this call if !val

Quoted 1 line

Return -EINVAL if !val and remove the if (val) check below.

Will fix

Quoted 65 lines
Quoted 53 lines
drivers/fpga/xrt/metadata/metadata.c new file +52 -0
+     if (ep_name) {
+             ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+                                       &offset);
+             if (ret) {
+                     dev_err(dev, "cannot get ep %s, regmap %s, ret = %d",
+                             ep_name, regmap_name, ret);
+                     return -EINVAL;
+             }
+     } else {
+             offset = fdt_next_node(blob, -1, NULL);
+             if (offset < 0) {
+                     dev_err(dev, "internal error, ret = %d", offset);
+                     return -EINVAL;
+             }
+     }
+
+     if (val) {
+             *val = fdt_getprop(blob, offset, prop, size);
+             if (!*val) {
+                     dev_dbg(dev, "get ep %s, prop %s failed", ep_name, prop);
+                     return -EINVAL;
+             }
+     }
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_prop);
+
+static int xrt_md_setprop(struct device *dev, char *blob, int offset,
+                       const char *prop, const void *val, int size)
+{
+     int ret;
+
+     ret = fdt_setprop(blob, offset, prop, val, size);
+     if (ret)
+             dev_err(dev, "failed to set prop %d", ret);
+
+     return ret;
+}
+
+int xrt_md_set_prop(struct device *dev, char *blob,
+                 const char *ep_name, const char *regmap_name,
+                 const char *prop, const void *val, int size)
+{
+     int offset;
+     int ret;
+
+     if (ep_name) {
+             ret = xrt_md_get_endpoint(dev, blob, ep_name,
+                                       regmap_name, &offset);
+             if (ret) {
+                     dev_err(dev, "cannot get node %s, ret = %d",

drivers/fpga/xrt/metadata/metadata.c line 300

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+                             ep_name, ret);
+                     return -EINVAL;
+             }
+     } else {
+             offset = fdt_next_node(blob, -1, NULL);
+             if (offset < 0) {
+                     dev_err(dev, "internal error, ret = %d", offset);
+                     return -EINVAL;
+             }
+     }

This if-else block is a cut-n-paste from above.

Quoted 1 line

It is good to convert common logic blocks to macros or inline functions.

Will define a in-line function.

Quoted 20 lines
Quoted 8 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+
+     ret = xrt_md_setprop(dev, blob, offset, prop, val, size);
+     if (ret)
+             dev_err(dev, "set prop %s failed, ret = %d", prop, ret);
+
+     return ret;
+}

drivers/fpga/xrt/metadata/metadata.c line 317

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+EXPORT_SYMBOL_GPL(xrt_md_set_prop);
+
+int xrt_md_copy_endpoint(struct device *dev, char *blob, const char *src_blob,
+                      const char *ep_name, const char *regmap_name,
+                      const char *new_ep_name)
+{
+     int offset, target;
+     int ret;
+     struct xrt_md_endpoint ep = {0};
+     const char *newepnm = new_ep_name ? new_ep_name : ep_name;

How is this valid ?

Quoted 1 line

The xrt_md_get_endpoint searches by ep_name and if there names are not unique the second one will never be found.

ep_name in the firmware will always be unique. It is documented in xrt.rst

Quoted 26 lines
Quoted 26 lines
drivers/fpga/xrt/metadata/metadata.c new file +25 -0
+
+     ret = xrt_md_get_endpoint(dev, src_blob, ep_name, regmap_name,
+                               &offset);
+     if (ret)
+             return -EINVAL;
+
+     ret = xrt_md_get_endpoint(dev, blob, newepnm, regmap_name, &target);
+     if (ret) {
+             ep.ep_name = newepnm;
+             ret = __xrt_md_add_endpoint(dev, blob, &ep, &target,
+                                         fdt_parent_offset(src_blob, offset) == 0);
+             if (ret)
+                     return -EINVAL;
+     }
+
+     ret = xrt_md_overlay(dev, blob, target, src_blob, offset);
+     if (ret)
+             dev_err(dev, "overlay failed, ret = %d", ret);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_copy_endpoint);
+
+char *xrt_md_dup(struct device *dev, const char *blob)
+{
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 349

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+     int ret;
+     char *dup_blob;
+
+     ret = xrt_md_create(dev, &dup_blob);
+     if (ret)
+             return NULL;
+     ret = xrt_md_overlay(dev, dup_blob, -1, blob, -1);

would memcpy-ing the blob work ?

Sure. Memcpy works. I will remove xrt_md_dup() and use memcpy.

Quoted 38 lines
Quoted 38 lines
drivers/fpga/xrt/metadata/metadata.c new file +38 -0
+     if (ret) {
+             vfree(dup_blob);
+             return NULL;
+     }
+
+     return dup_blob;
+}
+EXPORT_SYMBOL_GPL(xrt_md_dup);
+
+static int xrt_md_overlay(struct device *dev, char *blob, int target,
+                       const char *overlay_blob, int overlay_offset)
+{
+     int     property, subnode;
+     int     ret;
+
+     WARN_ON(!blob || !overlay_blob);
+
+     if (!blob) {
+             dev_err(dev, "blob is NULL");
+             return -EINVAL;
+     }
+
+     if (target < 0) {
+             target = fdt_next_node(blob, -1, NULL);
+             if (target < 0) {
+                     dev_err(dev, "invalid target");
+                     return -EINVAL;
+             }
+     }
+     if (overlay_offset < 0) {
+             overlay_offset = fdt_next_node(overlay_blob, -1, NULL);
+             if (overlay_offset < 0) {
+                     dev_err(dev, "invalid overlay");
+                     return -EINVAL;
+             }
+     }
+
+     fdt_for_each_property_offset(property, overlay_blob, overlay_offset) {
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 394

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+             const char *name;
+             const void *prop;
+             int prop_len;
+
+             prop = fdt_getprop_by_offset(overlay_blob, property, &name,
+                                          &prop_len);
+             if (!prop || prop_len >= MAX_BLOB_SIZE) {

could add prop_len < 0 as another sanity check

Will add.

Quoted 10 lines

drivers/fpga/xrt/metadata/metadata.c line 403

Quoted 9 lines
drivers/fpga/xrt/metadata/metadata.c new file +9 -0
+                     dev_err(dev, "internal error");
+                     return -EINVAL;
+             }
+
+             ret = xrt_md_setprop(dev, blob, target, name, prop,
+                                  prop_len);
+             if (ret) {
+                     dev_err(dev, "setprop failed, ret = %d", ret);
+                     return ret;

overlay_blob is half done, as an error handling shouldn't it be undone ?

The error will only cause a half done blob. Nothing need to be undone.

Quoted 18 lines
Quoted 6 lines
drivers/fpga/xrt/metadata/metadata.c new file +6 -0
+             }
+     }
+
+     fdt_for_each_subnode(subnode, overlay_blob, overlay_offset) {
+             const char *name = fdt_get_name(overlay_blob, subnode, NULL);
+             int nnode;

drivers/fpga/xrt/metadata/metadata.c line 419

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+
+             nnode = xrt_md_add_node(dev, blob, target, name);
+             if (nnode == -FDT_ERR_EXISTS)
+                     nnode = fdt_subnode_offset(blob, target, name);
+             if (nnode < 0) {
+                     dev_err(dev, "add node failed, ret = %d", nnode);
+                     return nnode;
+             }
+
+             ret = xrt_md_overlay(dev, blob, nnode, overlay_blob, subnode);

eek, recursion.

Quoted 1 line

Any chance this will blow the stack ?

Will add max recursive depth (5).

Quoted 18 lines
Quoted 18 lines
drivers/fpga/xrt/metadata/metadata.c new file +17 -0
+             if (ret)
+                     return ret;
+     }
+
+     return 0;
+}
+
+int xrt_md_get_next_endpoint(struct device *dev, const char *blob,
+                          const char *ep_name, const char *regmap_name,
+                          char **next_ep, char **next_regmap)
+{
+     int offset, ret;
+
+     if (!ep_name) {
+             ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_ENDPOINTS, NULL,
+                                       &offset);
+     } else {
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 443

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+             ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+                                       &offset);
+     }
+
+     if (ret) {
+             *next_ep = NULL;
+             *next_regmap = NULL;

could initialize next_ep and next_regmap to NULL outside the check.

Will move outside.

Quoted 24 lines
Quoted 24 lines
drivers/fpga/xrt/metadata/metadata.c new file +24 -0
+             return -EINVAL;
+     }
+
+     offset = ep_name ? fdt_next_subnode(blob, offset) :
+             fdt_first_subnode(blob, offset);
+     if (offset < 0) {
+             *next_ep = NULL;
+             *next_regmap = NULL;
+             return -EINVAL;
+     }
+
+     *next_ep = (char *)fdt_get_name(blob, offset, NULL);
+     *next_regmap = (char *)fdt_stringlist_get(blob, offset, XRT_MD_PROP_COMPATIBLE,
+                                               0, NULL);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_next_endpoint);
+
+int xrt_md_get_compatible_endpoint(struct device *dev, const char *blob,
+                                const char *regmap_name, const char **ep_name)
+{
+     int ep_offset;
+
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 474

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+     ep_offset = fdt_node_offset_by_compatible(blob, -1, regmap_name);
+     if (ep_offset < 0) {
+             *ep_name = NULL;
+             return -ENOENT;
+     }
+
+     *ep_name = (char *)fdt_get_name(blob, ep_offset, NULL);

why the cast ?

Will remove.

Quoted 5 lines
Quoted 5 lines
drivers/fpga/xrt/metadata/metadata.c new file +5 -0
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_compatible_endpoint);
+
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 486

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+void xrt_md_pack(struct device *dev, char *blob)
+{
+     int ret;
+
+     ret = fdt_pack(blob);
+     if (ret)
+             dev_err(dev, "pack failed %d", ret);

maybe return int

Will return int.

Quoted 4 lines

drivers/fpga/xrt/metadata/metadata.c line 489

Quoted 3 lines
drivers/fpga/xrt/metadata/metadata.c new file +3 -0
+}
+EXPORT_SYMBOL_GPL(xrt_md_pack);
+

The input/output of num_uuids parameter is tricky add a comment

Will modify to 'u32 num_uuids' and return the actual number

Quoted 3 lines

drivers/fpga/xrt/metadata/metadata.c line 491

Quoted 2 lines
drivers/fpga/xrt/metadata/metadata.c new file +2 -0
+int xrt_md_get_intf_uuids(struct device *dev, const char *blob,
+                       u32 *num_uuids, uuid_t *intf_uuids)

what is intf ? change to 'interface' ?

yes. And will change to 'interface'.

Quoted 28 lines
Quoted 16 lines
drivers/fpga/xrt/metadata/metadata.c new file +16 -0
+{
+     int offset, count = 0;
+     int ret;
+     const char *uuid_str;
+
+     ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_INTERFACES, NULL, &offset);
+     if (ret)
+             return -ENOENT;
+
+     for (offset = fdt_first_subnode(blob, offset);
+         offset >= 0;
+         offset = fdt_next_subnode(blob, offset)) {
+             uuid_str = fdt_getprop(blob, offset, XRT_MD_PROP_INTERFACE_UUID,
+                                    NULL);
+             if (!uuid_str) {
+                     dev_err(dev, "empty intf uuid node");

drivers/fpga/xrt/metadata/metadata.c line 517

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+                     return -EINVAL;
+             }
+
+             if (intf_uuids && count < *num_uuids) {
+                     ret = xrt_md_trans_str2uuid(dev, uuid_str,
+                                                 &intf_uuids[count]);
+                     if (ret)
+                             return -EINVAL;
+             }
+             count++;

keep going even when count > num_uuids ?

Quoted 1 line

that seems like an error.

Will add check.

Thanks,
Lizhi

Quoted 10 lines

Tom

Quoted 7 lines
drivers/fpga/xrt/metadata/metadata.c new file +7 -0
+     }
+
+     *num_uuids = count;
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_intf_uuids);
#9 lore raw

Re: [PATCH V3 XRT Alveo 02/18] fpga: xrt: driver metadata helper functions

In reply to Lizhi Hou
Quoted 107 lines · Lizhi Hou

XRT drivers use device tree as metadata format to discover HW subsystems behind PCIe BAR. Thus libfdt functions are called for driver to parse device tree blob.

---
 drivers/fpga/xrt/include/metadata.h  | 229 ++++++++++++
 drivers/fpga/xrt/metadata/metadata.c | 524 +++++++++++++++++++++++++++
 2 files changed, 753 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 +87 -0
new file mode 100644
index 000000000000..b929bc469b73
@@ -0,0 +1,229 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Header file for Xilinx Runtime (XRT) driver
+ *
+ * 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 (~0UL)
+
+#define XRT_MD_PROP_COMPATIBLE "compatible"
+#define XRT_MD_PROP_PF_NUM "pcie_physical_function"
+#define XRT_MD_PROP_BAR_IDX "pcie_bar_mapping"
+#define XRT_MD_PROP_IO_OFFSET "reg"
+#define XRT_MD_PROP_INTERRUPTS "interrupts"
+#define XRT_MD_PROP_INTERFACE_UUID "interface_uuid"
+#define XRT_MD_PROP_LOGIC_UUID "logic_uuid"
+#define XRT_MD_PROP_VERSION_MAJOR "firmware_version_major"
+
+#define XRT_MD_PROP_HWICAP "axi_hwicap"
+#define XRT_MD_PROP_PDI_CONFIG "pdi_config_mem"
+
+#define XRT_MD_NODE_ENDPOINTS "addressable_endpoints"
+#define XRT_MD_INTERFACES_PATH "/interfaces"
+
+#define XRT_MD_NODE_FIRMWARE "firmware"
+#define XRT_MD_NODE_INTERFACES "interfaces"
+#define XRT_MD_NODE_PARTITION_INFO "partition_info"
+
+#define XRT_MD_NODE_FLASH "ep_card_flash_program_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_SYSMON "ep_cmp_sysmon_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_MGMT "ep_firewall_ctrl_mgmt_00"
+#define XRT_MD_NODE_AF_CTRL_USER "ep_firewall_ctrl_user_00"
+#define XRT_MD_NODE_AF_CTRL_DEBUG "ep_firewall_ctrl_debug_00"
+#define XRT_MD_NODE_AF_DATA_H2C "ep_firewall_data_h2c_00"
+#define XRT_MD_NODE_AF_DATA_C2H "ep_firewall_data_c2h_00"
+#define XRT_MD_NODE_AF_DATA_P2P "ep_firewall_data_p2p_00"
+#define XRT_MD_NODE_AF_DATA_M2M "ep_firewall_data_m2m_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_CMC_MUTEX "ep_cmc_mutex_00"
+#define XRT_MD_NODE_CMC_FW_MEM "ep_cmc_firmware_mem_00"
+#define XRT_MD_NODE_ERT_FW_MEM "ep_ert_firmware_mem_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_MAILBOX_MGMT "ep_mailbox_mgmt_00"
+#define XRT_MD_NODE_MAILBOX_USER "ep_mailbox_user_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_PCIE_MON "ep_pcie_link_mon_00"
+#define XRT_MD_NODE_DDR_CALIB "ep_ddr_mem_calib_00"
+#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_KDMA_CTRL "ep_kdma_ctrl_00"
+#define XRT_MD_NODE_FPGA_CONFIG "ep_fpga_configuration_00"
+#define XRT_MD_NODE_ERT_SCHED "ep_ert_sched_00"
+#define XRT_MD_NODE_XDMA "ep_xdma_00"
+#define XRT_MD_NODE_MSIX "ep_msix_00"
+#define XRT_MD_NODE_QDMA "ep_qdma_00"
+#define XRT_MD_XRT_MD_NODE_QDMA4 "ep_qdma4_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_CLK_SHUTDOWN "ep_aclk_shutdown_00"
+#define XRT_MD_NODE_ERT_BASE "ep_ert_base_address_00"
+#define XRT_MD_NODE_ERT_RESET "ep_ert_reset_00"
+#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_CLKFREQ_HBM "ep_freq_cnt_aclk_hbm_00"
+#define XRT_MD_NODE_GAPPING "ep_gapping_demand_00"
+#define XRT_MD_NODE_UCS_CONTROL_STATUS "ep_ucs_control_status_00"
+#define XRT_MD_NODE_P2P "ep_p2p_00"
+#define XRT_MD_NODE_REMAP_P2P "ep_remap_p2p_00"
+#define XRT_MD_NODE_DDR4_RESET_GATE "ep_ddr_mem_srsr_gate_00"

drivers/fpga/xrt/include/metadata.h line 97

Quoted 10 lines
drivers/fpga/xrt/include/metadata.h new file +10 -0
+#define XRT_MD_NODE_ADDR_TRANSLATOR "ep_remap_data_c2h_00"
+#define XRT_MD_NODE_MAILBOX_XRT "ep_mailbox_user_to_ert_00"
+#define XRT_MD_NODE_PMC_INTR   "ep_pmc_intr_00"
+#define XRT_MD_NODE_PMC_MUX    "ep_pmc_mux_00"
+
+/* driver defined endpoints */
+#define XRT_MD_NODE_VSEC "drv_ep_vsec_00"
+#define XRT_MD_NODE_VSEC_GOLDEN "drv_ep_vsec_golden_00"
+#define XRT_MD_NODE_BLP_ROM "drv_ep_blp_rom_00"
+#define XRT_MD_NODE_MAILBOX_VSEC "ep_mailbox_vsec_00"

Should this be "drv_ep_mailbox_vsec_00"?

Quoted 157 lines
drivers/fpga/xrt/include/metadata.h new file +132 -0
+#define XRT_MD_NODE_PLAT_INFO "drv_ep_platform_info_mgmt_00"
+#define XRT_MD_NODE_TEST "drv_ep_test_00"
+#define XRT_MD_NODE_MGMT_MAIN "drv_ep_mgmt_main_00"
+#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_XRT_MD_NODE_PARTITION_INFO_BLP "partition_info_0"
+#define XRT_MD_XRT_MD_NODE_PARTITION_INFO_PLP "partition_info_1"
+
+#define XRT_MD_NODE_DDR_SRSR "drv_ep_ddr_srsr"
+#define XRT_MD_REGMAP_DDR_SRSR "drv_ddr_srsr"
+
+#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;
+	long		bar_off;
+	ulong		size;
+	char		*regmap;
+	char		*regmap_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 (!strcmp(res_name, res_map->res_name))
+			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;
+}
+
+unsigned long xrt_md_size(struct device *dev, const char *blob);
+int xrt_md_create(struct device *dev, 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,
+			char *regmap_name);
+int xrt_md_get_prop(struct device *dev, const char *blob, const char *ep_name,
+		    const char *regmap_name, const char *prop,
+		    const void **val, int *size);
+int xrt_md_set_prop(struct device *dev, char *blob, const char *ep_name,
+		    const char *regmap_name, 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 *regmap_name,
+			 const char *new_ep_name);
+int xrt_md_get_next_endpoint(struct device *dev, const char *blob,
+			     const char *ep_name,  const char *regmap_name,
+			     char **next_ep, char **next_regmap);
+int xrt_md_get_compatible_endpoint(struct device *dev, const char *blob,
+				   const char *regmap_name, const char **ep_name);
+int xrt_md_find_endpoint(struct device *dev, const char *blob,
+			 const char *ep_name, const char *regmap_name,
+			 const char **epname);
+void xrt_md_pack(struct device *dev, char *blob);
+char *xrt_md_dup(struct device *dev, const char *blob);
+int xrt_md_get_intf_uuids(struct device *dev, const char *blob,
+			  u32 *num_uuids, uuid_t *intf_uuids);
+static inline int xrt_md_copy_all_endpoints(struct device *dev, char *blob, const char *src_blob)
+{
+	return xrt_md_copy_endpoint(dev, blob, src_blob, XRT_MD_NODE_ENDPOINTS,
+				    NULL, NULL);
+}
+
+/*
+ * Firmware provides 128 bit hash string as unque id of partition/interface.
+ * This string will be canonical textual representation in the future.
+ * Before that, introducing these two functions below to translate
+ * hash string to uuid_t for released hardware.
+ */
+static inline void xrt_md_trans_uuid2str(const uuid_t *uuid, char *uuidstr)
+{
+	int i, p;
+	u8 *u = (u8 *)uuid;
+
+	for (p = 0, i = sizeof(uuid_t) - 1; i >= 0; p++, i--)
+		(void)snprintf(&uuidstr[p * 2], 3, "%02x", u[i]);
+}
+
+static inline int xrt_md_trans_str2uuid(struct device *dev, const char *uuidstr, uuid_t *p_uuid)
+{
+	char *p;
+	const char *str;
+	char tmp[3] = { 0 };
+	int i, ret;
+
+	memset(p_uuid, 0, sizeof(*p_uuid));
+	p = (char *)p_uuid;
+	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);
+		if (ret)
+			return -EINVAL;
+		p++;
+		str -= 2;
+	}
+
+	return 0;
+}
+
+#endif
drivers/fpga/xrt/metadata/metadata.c new file +19 -0
new file mode 100644
index 000000000000..5d106396f438
@@ -0,0 +1,524 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Xilinx Alveo FPGA Metadata parse APIs
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *      Lizhi Hou <[email protected]>
+ */
+
+#include <linux/libfdt_env.h>
+#include "libfdt.h"
+#include "metadata.h"
+
+#define MAX_BLOB_SIZE	(4096 * 25)
+
+static int xrt_md_setprop(struct device *dev, char *blob, int offset,
+			  const char *prop, const void *val, int size);
+static int xrt_md_overlay(struct device *dev, char *blob, int target,

drivers/fpga/xrt/metadata/metadata.c line 29

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+			  const char *overlay_blob, int overlay_offset);
+static int xrt_md_get_endpoint(struct device *dev, const char *blob,
+			       const char *ep_name, const char *regmap_name,
+			       int *ep_offset);
+
+unsigned long xrt_md_size(struct device *dev, const char *blob)
+{
+	unsigned long len = (long)fdt_totalsize(blob);
+
+	len = (len > MAX_BLOB_SIZE) ? XRT_MD_INVALID_LENGTH : len;

how about (avoiding len = len):

	if (len > MAX_BLOB_SIZE)
		return XRT_MD_INVALID_LENGTH;

	return len;

drivers/fpga/xrt/metadata/metadata.c line 38

Quoted 9 lines
drivers/fpga/xrt/metadata/metadata.c new file +9 -0
+	return len;
+}
+EXPORT_SYMBOL_GPL(xrt_md_size);
+
+int xrt_md_create(struct device *dev, char **blob)
+{
+	int ret = 0;
+
+	WARN_ON(!blob);

Shouldn't this be signaled to caller as a -EINVAL?

Quoted 79 lines
drivers/fpga/xrt/metadata/metadata.c new file +79 -0
+
+	*blob = vmalloc(MAX_BLOB_SIZE);
+	if (!*blob)
+		return -ENOMEM;
+
+	ret = fdt_create_empty_tree(*blob, MAX_BLOB_SIZE);
+	if (ret) {
+		dev_err(dev, "format blob failed, ret = %d", ret);
+		goto failed;
+	}
+
+	ret = fdt_next_node(*blob, -1, NULL);
+	if (ret < 0) {
+		dev_err(dev, "No Node, ret = %d", ret);
+		goto failed;
+	}
+
+	ret = fdt_add_subnode(*blob, ret, XRT_MD_NODE_ENDPOINTS);
+	if (ret < 0) {
+		dev_err(dev, "add node failed, ret = %d", ret);
+		goto failed;
+	}
+
+	return 0;
+
+failed:
+	vfree(*blob);
+	*blob = NULL;
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_create);
+
+static int xrt_md_add_node(struct device *dev, char *blob, int parent_offset,
+			   const char *ep_name)
+{
+	int ret;
+
+	ret = fdt_add_subnode(blob, parent_offset, ep_name);
+	if (ret < 0 && ret != -FDT_ERR_EXISTS)
+		dev_err(dev, "failed to add node %s. %d", ep_name, ret);
+
+	return ret;
+}
+
+int xrt_md_del_endpoint(struct device *dev, char *blob, const char *ep_name,
+			char *regmap_name)
+{
+	int ret;
+	int ep_offset;
+
+	ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name, &ep_offset);
+	if (ret) {
+		dev_err(dev, "can not find ep %s", ep_name);
+		return -EINVAL;
+	}
+
+	ret = fdt_del_node(blob, ep_offset);
+	if (ret)
+		dev_err(dev, "delete node %s failed, ret %d", ep_name, ret);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_del_endpoint);
+
+static int __xrt_md_add_endpoint(struct device *dev, char *blob,
+				 struct xrt_md_endpoint *ep, int *offset, bool root)
+{
+	int ret = 0;
+	int ep_offset = 0;
+	u32 val, count = 0;
+	u64 io_range[2];
+	char comp[128];
+
+	if (!ep->ep_name) {
+		dev_err(dev, "empty name");
+		return -EINVAL;
+	}
+

drivers/fpga/xrt/metadata/metadata.c line 127

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+	if (!root) {
+		ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_ENDPOINTS, NULL,
+					  &ep_offset);
+		if (ret) {
+			dev_err(dev, "invalid blob, ret = %d", ret);
+			return -EINVAL;
+		}
+	}
+
+	ep_offset = xrt_md_add_node(dev, blob, ep_offset, ep->ep_name);

A little hard to follow when you re-use ep_offset like this. Maybe parent_offset to match xrt_md_add_node parameter?

Quoted 228 lines
drivers/fpga/xrt/metadata/metadata.c new file +228 -0
+	if (ep_offset < 0) {
+		dev_err(dev, "add endpoint failed, ret = %d", ret);
+		return -EINVAL;
+	}
+	if (offset)
+		*offset = ep_offset;
+
+	if (ep->size != 0) {
+		val = cpu_to_be32(ep->bar);
+		ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_BAR_IDX,
+				     &val, sizeof(u32));
+		if (ret) {
+			dev_err(dev, "set %s failed, ret %d",
+				XRT_MD_PROP_BAR_IDX, ret);
+			goto failed;
+		}
+		io_range[0] = cpu_to_be64((u64)ep->bar_off);
+		io_range[1] = cpu_to_be64((u64)ep->size);
+		ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_IO_OFFSET,
+				     io_range, sizeof(io_range));
+		if (ret) {
+			dev_err(dev, "set %s failed, ret %d",
+				XRT_MD_PROP_IO_OFFSET, ret);
+			goto failed;
+		}
+	}
+
+	if (ep->regmap) {
+		if (ep->regmap_ver) {
+			count = snprintf(comp, sizeof(comp),
+					 "%s-%s", ep->regmap, ep->regmap_ver);
+			count++;
+		}
+
+		count += snprintf(comp + count, sizeof(comp) - count,
+				  "%s", ep->regmap);
+		count++;
+
+		ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_COMPATIBLE,
+				     comp, count);
+		if (ret) {
+			dev_err(dev, "set %s failed, ret %d",
+				XRT_MD_PROP_COMPATIBLE, ret);
+			goto failed;
+		}
+	}
+
+failed:
+	if (ret)
+		xrt_md_del_endpoint(dev, blob, ep->ep_name, NULL);
+
+	return ret;
+}
+
+int xrt_md_add_endpoint(struct device *dev, char *blob,
+			struct xrt_md_endpoint *ep)
+{
+	return __xrt_md_add_endpoint(dev, blob, ep, NULL, false);
+}
+EXPORT_SYMBOL_GPL(xrt_md_add_endpoint);
+
+static int xrt_md_get_endpoint(struct device *dev, const char *blob,
+			       const char *ep_name, const char *regmap_name,
+			       int *ep_offset)
+{
+	int offset;
+	const char *name;
+
+	for (offset = fdt_next_node(blob, -1, NULL);
+	    offset >= 0;
+	    offset = fdt_next_node(blob, offset, NULL)) {
+		name = fdt_get_name(blob, offset, NULL);
+		if (!name || strncmp(name, ep_name, strlen(ep_name) + 1))
+			continue;
+		if (!regmap_name ||
+		    !fdt_node_check_compatible(blob, offset, regmap_name))
+			break;
+	}
+	if (offset < 0)
+		return -ENODEV;
+
+	*ep_offset = offset;
+
+	return 0;
+}
+
+int xrt_md_find_endpoint(struct device *dev, const char *blob,
+			 const char *ep_name, const char *regmap_name,
+			 const char **epname)
+{
+	int offset;
+	int ret;
+
+	ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+				  &offset);
+	if (!ret && epname && offset >= 0)
+		*epname = fdt_get_name(blob, offset, NULL);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_find_endpoint);
+
+int xrt_md_get_prop(struct device *dev, const char *blob, const char *ep_name,
+		    const char *regmap_name, const char *prop,
+		    const void **val, int *size)
+{
+	int offset;
+	int ret;
+
+	if (val)
+		*val = NULL;
+	if (ep_name) {
+		ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+					  &offset);
+		if (ret) {
+			dev_err(dev, "cannot get ep %s, regmap %s, ret = %d",
+				ep_name, regmap_name, ret);
+			return -EINVAL;
+		}
+	} else {
+		offset = fdt_next_node(blob, -1, NULL);
+		if (offset < 0) {
+			dev_err(dev, "internal error, ret = %d", offset);
+			return -EINVAL;
+		}
+	}
+
+	if (val) {
+		*val = fdt_getprop(blob, offset, prop, size);
+		if (!*val) {
+			dev_dbg(dev, "get ep %s, prop %s failed", ep_name, prop);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_prop);
+
+static int xrt_md_setprop(struct device *dev, char *blob, int offset,
+			  const char *prop, const void *val, int size)
+{
+	int ret;
+
+	ret = fdt_setprop(blob, offset, prop, val, size);
+	if (ret)
+		dev_err(dev, "failed to set prop %d", ret);
+
+	return ret;
+}
+
+int xrt_md_set_prop(struct device *dev, char *blob,
+		    const char *ep_name, const char *regmap_name,
+		    const char *prop, const void *val, int size)
+{
+	int offset;
+	int ret;
+
+	if (ep_name) {
+		ret = xrt_md_get_endpoint(dev, blob, ep_name,
+					  regmap_name, &offset);
+		if (ret) {
+			dev_err(dev, "cannot get node %s, ret = %d",
+				ep_name, ret);
+			return -EINVAL;
+		}
+	} else {
+		offset = fdt_next_node(blob, -1, NULL);
+		if (offset < 0) {
+			dev_err(dev, "internal error, ret = %d", offset);
+			return -EINVAL;
+		}
+	}
+
+	ret = xrt_md_setprop(dev, blob, offset, prop, val, size);
+	if (ret)
+		dev_err(dev, "set prop %s failed, ret = %d", prop, ret);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_set_prop);
+
+int xrt_md_copy_endpoint(struct device *dev, char *blob, const char *src_blob,
+			 const char *ep_name, const char *regmap_name,
+			 const char *new_ep_name)
+{
+	int offset, target;
+	int ret;
+	struct xrt_md_endpoint ep = {0};
+	const char *newepnm = new_ep_name ? new_ep_name : ep_name;
+
+	ret = xrt_md_get_endpoint(dev, src_blob, ep_name, regmap_name,
+				  &offset);
+	if (ret)
+		return -EINVAL;
+
+	ret = xrt_md_get_endpoint(dev, blob, newepnm, regmap_name, &target);
+	if (ret) {
+		ep.ep_name = newepnm;
+		ret = __xrt_md_add_endpoint(dev, blob, &ep, &target,
+					    fdt_parent_offset(src_blob, offset) == 0);
+		if (ret)
+			return -EINVAL;
+	}
+
+	ret = xrt_md_overlay(dev, blob, target, src_blob, offset);
+	if (ret)
+		dev_err(dev, "overlay failed, ret = %d", ret);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_copy_endpoint);
+
+char *xrt_md_dup(struct device *dev, const char *blob)
+{
+	int ret;
+	char *dup_blob;
+
+	ret = xrt_md_create(dev, &dup_blob);
+	if (ret)
+		return NULL;
+	ret = xrt_md_overlay(dev, dup_blob, -1, blob, -1);
+	if (ret) {
+		vfree(dup_blob);
+		return NULL;
+	}
+
+	return dup_blob;

drivers/fpga/xrt/metadata/metadata.c line 365

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+}
+EXPORT_SYMBOL_GPL(xrt_md_dup);
+
+static int xrt_md_overlay(struct device *dev, char *blob, int target,
+			  const char *overlay_blob, int overlay_offset)
+{
+	int	property, subnode;
+	int	ret;
+
+	WARN_ON(!blob || !overlay_blob);

Why check !blob twice? Also, is a WARN_ON justified here (and in xrt_md_create)?

Fernando

Quoted 162 lines
drivers/fpga/xrt/metadata/metadata.c new file +159 -0
+
+	if (!blob) {
+		dev_err(dev, "blob is NULL");
+		return -EINVAL;
+	}
+
+	if (target < 0) {
+		target = fdt_next_node(blob, -1, NULL);
+		if (target < 0) {
+			dev_err(dev, "invalid target");
+			return -EINVAL;
+		}
+	}
+	if (overlay_offset < 0) {
+		overlay_offset = fdt_next_node(overlay_blob, -1, NULL);
+		if (overlay_offset < 0) {
+			dev_err(dev, "invalid overlay");
+			return -EINVAL;
+		}
+	}
+
+	fdt_for_each_property_offset(property, overlay_blob, overlay_offset) {
+		const char *name;
+		const void *prop;
+		int prop_len;
+
+		prop = fdt_getprop_by_offset(overlay_blob, property, &name,
+					     &prop_len);
+		if (!prop || prop_len >= MAX_BLOB_SIZE) {
+			dev_err(dev, "internal error");
+			return -EINVAL;
+		}
+
+		ret = xrt_md_setprop(dev, blob, target, name, prop,
+				     prop_len);
+		if (ret) {
+			dev_err(dev, "setprop failed, ret = %d", ret);
+			return ret;
+		}
+	}
+
+	fdt_for_each_subnode(subnode, overlay_blob, overlay_offset) {
+		const char *name = fdt_get_name(overlay_blob, subnode, NULL);
+		int nnode;
+
+		nnode = xrt_md_add_node(dev, blob, target, name);
+		if (nnode == -FDT_ERR_EXISTS)
+			nnode = fdt_subnode_offset(blob, target, name);
+		if (nnode < 0) {
+			dev_err(dev, "add node failed, ret = %d", nnode);
+			return nnode;
+		}
+
+		ret = xrt_md_overlay(dev, blob, nnode, overlay_blob, subnode);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+int xrt_md_get_next_endpoint(struct device *dev, const char *blob,
+			     const char *ep_name, const char *regmap_name,
+			     char **next_ep, char **next_regmap)
+{
+	int offset, ret;
+
+	if (!ep_name) {
+		ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_ENDPOINTS, NULL,
+					  &offset);
+	} else {
+		ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+					  &offset);
+	}
+
+	if (ret) {
+		*next_ep = NULL;
+		*next_regmap = NULL;
+		return -EINVAL;
+	}
+
+	offset = ep_name ? fdt_next_subnode(blob, offset) :
+		fdt_first_subnode(blob, offset);
+	if (offset < 0) {
+		*next_ep = NULL;
+		*next_regmap = NULL;
+		return -EINVAL;
+	}
+
+	*next_ep = (char *)fdt_get_name(blob, offset, NULL);
+	*next_regmap = (char *)fdt_stringlist_get(blob, offset, XRT_MD_PROP_COMPATIBLE,
+						  0, NULL);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_next_endpoint);
+
+int xrt_md_get_compatible_endpoint(struct device *dev, const char *blob,
+				   const char *regmap_name, const char **ep_name)
+{
+	int ep_offset;
+
+	ep_offset = fdt_node_offset_by_compatible(blob, -1, regmap_name);
+	if (ep_offset < 0) {
+		*ep_name = NULL;
+		return -ENOENT;
+	}
+
+	*ep_name = (char *)fdt_get_name(blob, ep_offset, NULL);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_compatible_endpoint);
+
+void xrt_md_pack(struct device *dev, char *blob)
+{
+	int ret;
+
+	ret = fdt_pack(blob);
+	if (ret)
+		dev_err(dev, "pack failed %d", ret);
+}
+EXPORT_SYMBOL_GPL(xrt_md_pack);
+
+int xrt_md_get_intf_uuids(struct device *dev, const char *blob,
+			  u32 *num_uuids, uuid_t *intf_uuids)
+{
+	int offset, count = 0;
+	int ret;
+	const char *uuid_str;
+
+	ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_INTERFACES, NULL, &offset);
+	if (ret)
+		return -ENOENT;
+
+	for (offset = fdt_first_subnode(blob, offset);
+	    offset >= 0;
+	    offset = fdt_next_subnode(blob, offset)) {
+		uuid_str = fdt_getprop(blob, offset, XRT_MD_PROP_INTERFACE_UUID,
+				       NULL);
+		if (!uuid_str) {
+			dev_err(dev, "empty intf uuid node");
+			return -EINVAL;
+		}
+
+		if (intf_uuids && count < *num_uuids) {
+			ret = xrt_md_trans_str2uuid(dev, uuid_str,
+						    &intf_uuids[count]);
+			if (ret)
+				return -EINVAL;
+		}
+		count++;
+	}
+
+	*num_uuids = count;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_intf_uuids);
Signature · 1 line
2.18.4
author
#10 lore raw

Re: [PATCH V3 XRT Alveo 02/18] fpga: xrt: driver metadata helper functions

Hi Fernando,

Quoted 111 lines · Fernando Pacheco
Quoted 110 lines · Lizhi Hou

XRT drivers use device tree as metadata format to discover HW subsystems behind PCIe BAR. Thus libfdt functions are called for driver to parse device tree blob.

---
  drivers/fpga/xrt/include/metadata.h  | 229 ++++++++++++
  drivers/fpga/xrt/metadata/metadata.c | 524 +++++++++++++++++++++++++++
  2 files changed, 753 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 +90 -0
new file mode 100644
index 000000000000..b929bc469b73
@@ -0,0 +1,229 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Header file for Xilinx Runtime (XRT) driver
+ *
+ * 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 (~0UL)
+
+#define XRT_MD_PROP_COMPATIBLE "compatible"
+#define XRT_MD_PROP_PF_NUM "pcie_physical_function"
+#define XRT_MD_PROP_BAR_IDX "pcie_bar_mapping"
+#define XRT_MD_PROP_IO_OFFSET "reg"
+#define XRT_MD_PROP_INTERRUPTS "interrupts"
+#define XRT_MD_PROP_INTERFACE_UUID "interface_uuid"
+#define XRT_MD_PROP_LOGIC_UUID "logic_uuid"
+#define XRT_MD_PROP_VERSION_MAJOR "firmware_version_major"
+
+#define XRT_MD_PROP_HWICAP "axi_hwicap"
+#define XRT_MD_PROP_PDI_CONFIG "pdi_config_mem"
+
+#define XRT_MD_NODE_ENDPOINTS "addressable_endpoints"
+#define XRT_MD_INTERFACES_PATH "/interfaces"
+
+#define XRT_MD_NODE_FIRMWARE "firmware"
+#define XRT_MD_NODE_INTERFACES "interfaces"
+#define XRT_MD_NODE_PARTITION_INFO "partition_info"
+
+#define XRT_MD_NODE_FLASH "ep_card_flash_program_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_SYSMON "ep_cmp_sysmon_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_MGMT "ep_firewall_ctrl_mgmt_00"
+#define XRT_MD_NODE_AF_CTRL_USER "ep_firewall_ctrl_user_00"
+#define XRT_MD_NODE_AF_CTRL_DEBUG "ep_firewall_ctrl_debug_00"
+#define XRT_MD_NODE_AF_DATA_H2C "ep_firewall_data_h2c_00"
+#define XRT_MD_NODE_AF_DATA_C2H "ep_firewall_data_c2h_00"
+#define XRT_MD_NODE_AF_DATA_P2P "ep_firewall_data_p2p_00"
+#define XRT_MD_NODE_AF_DATA_M2M "ep_firewall_data_m2m_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_CMC_MUTEX "ep_cmc_mutex_00"
+#define XRT_MD_NODE_CMC_FW_MEM "ep_cmc_firmware_mem_00"
+#define XRT_MD_NODE_ERT_FW_MEM "ep_ert_firmware_mem_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_MAILBOX_MGMT "ep_mailbox_mgmt_00"
+#define XRT_MD_NODE_MAILBOX_USER "ep_mailbox_user_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_PCIE_MON "ep_pcie_link_mon_00"
+#define XRT_MD_NODE_DDR_CALIB "ep_ddr_mem_calib_00"
+#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_KDMA_CTRL "ep_kdma_ctrl_00"
+#define XRT_MD_NODE_FPGA_CONFIG "ep_fpga_configuration_00"
+#define XRT_MD_NODE_ERT_SCHED "ep_ert_sched_00"
+#define XRT_MD_NODE_XDMA "ep_xdma_00"
+#define XRT_MD_NODE_MSIX "ep_msix_00"
+#define XRT_MD_NODE_QDMA "ep_qdma_00"
+#define XRT_MD_XRT_MD_NODE_QDMA4 "ep_qdma4_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_CLK_SHUTDOWN "ep_aclk_shutdown_00"
+#define XRT_MD_NODE_ERT_BASE "ep_ert_base_address_00"
+#define XRT_MD_NODE_ERT_RESET "ep_ert_reset_00"
+#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_CLKFREQ_HBM "ep_freq_cnt_aclk_hbm_00"
+#define XRT_MD_NODE_GAPPING "ep_gapping_demand_00"
+#define XRT_MD_NODE_UCS_CONTROL_STATUS "ep_ucs_control_status_00"
+#define XRT_MD_NODE_P2P "ep_p2p_00"
+#define XRT_MD_NODE_REMAP_P2P "ep_remap_p2p_00"
+#define XRT_MD_NODE_DDR4_RESET_GATE "ep_ddr_mem_srsr_gate_00"
+#define XRT_MD_NODE_ADDR_TRANSLATOR "ep_remap_data_c2h_00"
+#define XRT_MD_NODE_MAILBOX_XRT "ep_mailbox_user_to_ert_00"
+#define XRT_MD_NODE_PMC_INTR   "ep_pmc_intr_00"
Quoted 8 lines

drivers/fpga/xrt/include/metadata.h line 97

Quoted 7 lines
drivers/fpga/xrt/include/metadata.h new file +7 -0
+#define XRT_MD_NODE_PMC_MUX    "ep_pmc_mux_00"
+
+/* driver defined endpoints */
+#define XRT_MD_NODE_VSEC "drv_ep_vsec_00"
+#define XRT_MD_NODE_VSEC_GOLDEN "drv_ep_vsec_golden_00"
+#define XRT_MD_NODE_BLP_ROM "drv_ep_blp_rom_00"
+#define XRT_MD_NODE_MAILBOX_VSEC "ep_mailbox_vsec_00"

Should this be "drv_ep_mailbox_vsec_00"?

Will fix this.

Quoted 170 lines
Quoted 158 lines
drivers/fpga/xrt/include/metadata.h new file +132 -0
+#define XRT_MD_NODE_PLAT_INFO "drv_ep_platform_info_mgmt_00"
+#define XRT_MD_NODE_TEST "drv_ep_test_00"
+#define XRT_MD_NODE_MGMT_MAIN "drv_ep_mgmt_main_00"
+#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_XRT_MD_NODE_PARTITION_INFO_BLP "partition_info_0"
+#define XRT_MD_XRT_MD_NODE_PARTITION_INFO_PLP "partition_info_1"
+
+#define XRT_MD_NODE_DDR_SRSR "drv_ep_ddr_srsr"
+#define XRT_MD_REGMAP_DDR_SRSR "drv_ddr_srsr"
+
+#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;
+     long            bar_off;
+     ulong           size;
+     char            *regmap;
+     char            *regmap_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 (!strcmp(res_name, res_map->res_name))
+                     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;
+}
+
+unsigned long xrt_md_size(struct device *dev, const char *blob);
+int xrt_md_create(struct device *dev, 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,
+                     char *regmap_name);
+int xrt_md_get_prop(struct device *dev, const char *blob, const char *ep_name,
+                 const char *regmap_name, const char *prop,
+                 const void **val, int *size);
+int xrt_md_set_prop(struct device *dev, char *blob, const char *ep_name,
+                 const char *regmap_name, 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 *regmap_name,
+                      const char *new_ep_name);
+int xrt_md_get_next_endpoint(struct device *dev, const char *blob,
+                          const char *ep_name,  const char *regmap_name,
+                          char **next_ep, char **next_regmap);
+int xrt_md_get_compatible_endpoint(struct device *dev, const char *blob,
+                                const char *regmap_name, const char **ep_name);
+int xrt_md_find_endpoint(struct device *dev, const char *blob,
+                      const char *ep_name, const char *regmap_name,
+                      const char **epname);
+void xrt_md_pack(struct device *dev, char *blob);
+char *xrt_md_dup(struct device *dev, const char *blob);
+int xrt_md_get_intf_uuids(struct device *dev, const char *blob,
+                       u32 *num_uuids, uuid_t *intf_uuids);
+static inline int xrt_md_copy_all_endpoints(struct device *dev, char *blob, const char *src_blob)
+{
+     return xrt_md_copy_endpoint(dev, blob, src_blob, XRT_MD_NODE_ENDPOINTS,
+                                 NULL, NULL);
+}
+
+/*
+ * Firmware provides 128 bit hash string as unque id of partition/interface.
+ * This string will be canonical textual representation in the future.
+ * Before that, introducing these two functions below to translate
+ * hash string to uuid_t for released hardware.
+ */
+static inline void xrt_md_trans_uuid2str(const uuid_t *uuid, char *uuidstr)
+{
+     int i, p;
+     u8 *u = (u8 *)uuid;
+
+     for (p = 0, i = sizeof(uuid_t) - 1; i >= 0; p++, i--)
+             (void)snprintf(&uuidstr[p * 2], 3, "%02x", u[i]);
+}
+
+static inline int xrt_md_trans_str2uuid(struct device *dev, const char *uuidstr, uuid_t *p_uuid)
+{
+     char *p;
+     const char *str;
+     char tmp[3] = { 0 };
+     int i, ret;
+
+     memset(p_uuid, 0, sizeof(*p_uuid));
+     p = (char *)p_uuid;
+     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);
+             if (ret)
+                     return -EINVAL;
+             p++;
+             str -= 2;
+     }
+
+     return 0;
+}
+
+#endif
drivers/fpga/xrt/metadata/metadata.c new file +19 -0
new file mode 100644
index 000000000000..5d106396f438
@@ -0,0 +1,524 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Xilinx Alveo FPGA Metadata parse APIs
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *      Lizhi Hou <[email protected]>
+ */
+
+#include <linux/libfdt_env.h>
+#include "libfdt.h"
+#include "metadata.h"
+
+#define MAX_BLOB_SIZE        (4096 * 25)
+
+static int xrt_md_setprop(struct device *dev, char *blob, int offset,
+                       const char *prop, const void *val, int size);
+static int xrt_md_overlay(struct device *dev, char *blob, int target,

drivers/fpga/xrt/metadata/metadata.c line 29

Quoted 10 lines
drivers/fpga/xrt/metadata/metadata.c new file +10 -0
+                       const char *overlay_blob, int overlay_offset);
+static int xrt_md_get_endpoint(struct device *dev, const char *blob,
+                            const char *ep_name, const char *regmap_name,
+                            int *ep_offset);
+
+unsigned long xrt_md_size(struct device *dev, const char *blob)
+{
+     unsigned long len = (long)fdt_totalsize(blob);
+
+     len = (len > MAX_BLOB_SIZE) ? XRT_MD_INVALID_LENGTH : len;

how about (avoiding len = len):

Quoted 4 lines
         if (len > MAX_BLOB_SIZE)
                 return XRT_MD_INVALID_LENGTH;

         return len;

Agree. Will fix.

Quoted 10 lines

drivers/fpga/xrt/metadata/metadata.c line 38

Quoted 9 lines
drivers/fpga/xrt/metadata/metadata.c new file +9 -0
+     return len;
+}
+EXPORT_SYMBOL_GPL(xrt_md_size);
+
+int xrt_md_create(struct device *dev, char **blob)
+{
+     int ret = 0;
+
+     WARN_ON(!blob);

Shouldn't this be signaled to caller as a -EINVAL?

Agree. Will fix it.

Quoted 84 lines
Quoted 84 lines
drivers/fpga/xrt/metadata/metadata.c new file +83 -0
+
+     *blob = vmalloc(MAX_BLOB_SIZE);
+     if (!*blob)
+             return -ENOMEM;
+
+     ret = fdt_create_empty_tree(*blob, MAX_BLOB_SIZE);
+     if (ret) {
+             dev_err(dev, "format blob failed, ret = %d", ret);
+             goto failed;
+     }
+
+     ret = fdt_next_node(*blob, -1, NULL);
+     if (ret < 0) {
+             dev_err(dev, "No Node, ret = %d", ret);
+             goto failed;
+     }
+
+     ret = fdt_add_subnode(*blob, ret, XRT_MD_NODE_ENDPOINTS);
+     if (ret < 0) {
+             dev_err(dev, "add node failed, ret = %d", ret);
+             goto failed;
+     }
+
+     return 0;
+
+failed:
+     vfree(*blob);
+     *blob = NULL;
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_create);
+
+static int xrt_md_add_node(struct device *dev, char *blob, int parent_offset,
+                        const char *ep_name)
+{
+     int ret;
+
+     ret = fdt_add_subnode(blob, parent_offset, ep_name);
+     if (ret < 0 && ret != -FDT_ERR_EXISTS)
+             dev_err(dev, "failed to add node %s. %d", ep_name, ret);
+
+     return ret;
+}
+
+int xrt_md_del_endpoint(struct device *dev, char *blob, const char *ep_name,
+                     char *regmap_name)
+{
+     int ret;
+     int ep_offset;
+
+     ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name, &ep_offset);
+     if (ret) {
+             dev_err(dev, "can not find ep %s", ep_name);
+             return -EINVAL;
+     }
+
+     ret = fdt_del_node(blob, ep_offset);
+     if (ret)
+             dev_err(dev, "delete node %s failed, ret %d", ep_name, ret);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_del_endpoint);
+
+static int __xrt_md_add_endpoint(struct device *dev, char *blob,
+                              struct xrt_md_endpoint *ep, int *offset, bool root)
+{
+     int ret = 0;
+     int ep_offset = 0;
+     u32 val, count = 0;
+     u64 io_range[2];
+     char comp[128];
+
+     if (!ep->ep_name) {
+             dev_err(dev, "empty name");
+             return -EINVAL;
+     }
+
+     if (!root) {
+             ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_ENDPOINTS, NULL,
+                                       &ep_offset);
+             if (ret) {
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 127

Quoted 6 lines
drivers/fpga/xrt/metadata/metadata.c new file +6 -0
+                     dev_err(dev, "invalid blob, ret = %d", ret);
+                     return -EINVAL;
+             }
+     }
+
+     ep_offset = xrt_md_add_node(dev, blob, ep_offset, ep->ep_name);

A little hard to follow when you re-use ep_offset like this. Maybe parent_offset to match xrt_md_add_node parameter?

Will add parent_offset.

Quoted 233 lines
Quoted 233 lines
drivers/fpga/xrt/metadata/metadata.c new file +232 -0
+     if (ep_offset < 0) {
+             dev_err(dev, "add endpoint failed, ret = %d", ret);
+             return -EINVAL;
+     }
+     if (offset)
+             *offset = ep_offset;
+
+     if (ep->size != 0) {
+             val = cpu_to_be32(ep->bar);
+             ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_BAR_IDX,
+                                  &val, sizeof(u32));
+             if (ret) {
+                     dev_err(dev, "set %s failed, ret %d",
+                             XRT_MD_PROP_BAR_IDX, ret);
+                     goto failed;
+             }
+             io_range[0] = cpu_to_be64((u64)ep->bar_off);
+             io_range[1] = cpu_to_be64((u64)ep->size);
+             ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_IO_OFFSET,
+                                  io_range, sizeof(io_range));
+             if (ret) {
+                     dev_err(dev, "set %s failed, ret %d",
+                             XRT_MD_PROP_IO_OFFSET, ret);
+                     goto failed;
+             }
+     }
+
+     if (ep->regmap) {
+             if (ep->regmap_ver) {
+                     count = snprintf(comp, sizeof(comp),
+                                      "%s-%s", ep->regmap, ep->regmap_ver);
+                     count++;
+             }
+
+             count += snprintf(comp + count, sizeof(comp) - count,
+                               "%s", ep->regmap);
+             count++;
+
+             ret = xrt_md_setprop(dev, blob, ep_offset, XRT_MD_PROP_COMPATIBLE,
+                                  comp, count);
+             if (ret) {
+                     dev_err(dev, "set %s failed, ret %d",
+                             XRT_MD_PROP_COMPATIBLE, ret);
+                     goto failed;
+             }
+     }
+
+failed:
+     if (ret)
+             xrt_md_del_endpoint(dev, blob, ep->ep_name, NULL);
+
+     return ret;
+}
+
+int xrt_md_add_endpoint(struct device *dev, char *blob,
+                     struct xrt_md_endpoint *ep)
+{
+     return __xrt_md_add_endpoint(dev, blob, ep, NULL, false);
+}
+EXPORT_SYMBOL_GPL(xrt_md_add_endpoint);
+
+static int xrt_md_get_endpoint(struct device *dev, const char *blob,
+                            const char *ep_name, const char *regmap_name,
+                            int *ep_offset)
+{
+     int offset;
+     const char *name;
+
+     for (offset = fdt_next_node(blob, -1, NULL);
+         offset >= 0;
+         offset = fdt_next_node(blob, offset, NULL)) {
+             name = fdt_get_name(blob, offset, NULL);
+             if (!name || strncmp(name, ep_name, strlen(ep_name) + 1))
+                     continue;
+             if (!regmap_name ||
+                 !fdt_node_check_compatible(blob, offset, regmap_name))
+                     break;
+     }
+     if (offset < 0)
+             return -ENODEV;
+
+     *ep_offset = offset;
+
+     return 0;
+}
+
+int xrt_md_find_endpoint(struct device *dev, const char *blob,
+                      const char *ep_name, const char *regmap_name,
+                      const char **epname)
+{
+     int offset;
+     int ret;
+
+     ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+                               &offset);
+     if (!ret && epname && offset >= 0)
+             *epname = fdt_get_name(blob, offset, NULL);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_find_endpoint);
+
+int xrt_md_get_prop(struct device *dev, const char *blob, const char *ep_name,
+                 const char *regmap_name, const char *prop,
+                 const void **val, int *size)
+{
+     int offset;
+     int ret;
+
+     if (val)
+             *val = NULL;
+     if (ep_name) {
+             ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+                                       &offset);
+             if (ret) {
+                     dev_err(dev, "cannot get ep %s, regmap %s, ret = %d",
+                             ep_name, regmap_name, ret);
+                     return -EINVAL;
+             }
+     } else {
+             offset = fdt_next_node(blob, -1, NULL);
+             if (offset < 0) {
+                     dev_err(dev, "internal error, ret = %d", offset);
+                     return -EINVAL;
+             }
+     }
+
+     if (val) {
+             *val = fdt_getprop(blob, offset, prop, size);
+             if (!*val) {
+                     dev_dbg(dev, "get ep %s, prop %s failed", ep_name, prop);
+                     return -EINVAL;
+             }
+     }
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_prop);
+
+static int xrt_md_setprop(struct device *dev, char *blob, int offset,
+                       const char *prop, const void *val, int size)
+{
+     int ret;
+
+     ret = fdt_setprop(blob, offset, prop, val, size);
+     if (ret)
+             dev_err(dev, "failed to set prop %d", ret);
+
+     return ret;
+}
+
+int xrt_md_set_prop(struct device *dev, char *blob,
+                 const char *ep_name, const char *regmap_name,
+                 const char *prop, const void *val, int size)
+{
+     int offset;
+     int ret;
+
+     if (ep_name) {
+             ret = xrt_md_get_endpoint(dev, blob, ep_name,
+                                       regmap_name, &offset);
+             if (ret) {
+                     dev_err(dev, "cannot get node %s, ret = %d",
+                             ep_name, ret);
+                     return -EINVAL;
+             }
+     } else {
+             offset = fdt_next_node(blob, -1, NULL);
+             if (offset < 0) {
+                     dev_err(dev, "internal error, ret = %d", offset);
+                     return -EINVAL;
+             }
+     }
+
+     ret = xrt_md_setprop(dev, blob, offset, prop, val, size);
+     if (ret)
+             dev_err(dev, "set prop %s failed, ret = %d", prop, ret);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_set_prop);
+
+int xrt_md_copy_endpoint(struct device *dev, char *blob, const char *src_blob,
+                      const char *ep_name, const char *regmap_name,
+                      const char *new_ep_name)
+{
+     int offset, target;
+     int ret;
+     struct xrt_md_endpoint ep = {0};
+     const char *newepnm = new_ep_name ? new_ep_name : ep_name;
+
+     ret = xrt_md_get_endpoint(dev, src_blob, ep_name, regmap_name,
+                               &offset);
+     if (ret)
+             return -EINVAL;
+
+     ret = xrt_md_get_endpoint(dev, blob, newepnm, regmap_name, &target);
+     if (ret) {
+             ep.ep_name = newepnm;
+             ret = __xrt_md_add_endpoint(dev, blob, &ep, &target,
+                                         fdt_parent_offset(src_blob, offset) == 0);
+             if (ret)
+                     return -EINVAL;
+     }
+
+     ret = xrt_md_overlay(dev, blob, target, src_blob, offset);
+     if (ret)
+             dev_err(dev, "overlay failed, ret = %d", ret);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(xrt_md_copy_endpoint);
+
+char *xrt_md_dup(struct device *dev, const char *blob)
+{
+     int ret;
+     char *dup_blob;
+
+     ret = xrt_md_create(dev, &dup_blob);
+     if (ret)
+             return NULL;
+     ret = xrt_md_overlay(dev, dup_blob, -1, blob, -1);
+     if (ret) {
+             vfree(dup_blob);
+             return NULL;
+     }
+
+     return dup_blob;
+}
+EXPORT_SYMBOL_GPL(xrt_md_dup);
+
+static int xrt_md_overlay(struct device *dev, char *blob, int target,
Quoted 8 lines

drivers/fpga/xrt/metadata/metadata.c line 365

Quoted 6 lines
drivers/fpga/xrt/metadata/metadata.c new file +6 -0
+                       const char *overlay_blob, int overlay_offset)
+{
+     int     property, subnode;
+     int     ret;
+
+     WARN_ON(!blob || !overlay_blob);

Why check !blob twice? Also, is a WARN_ON justified here (and in xrt_md_create)?

Using 'if' statement to check is good enough and I will remove WARN_ON from both places.

Thanks,
Lizhi

Quoted 165 lines

Fernando

Quoted 162 lines
drivers/fpga/xrt/metadata/metadata.c new file +159 -1
+
+     if (!blob) {
+             dev_err(dev, "blob is NULL");
+             return -EINVAL;
+     }
+
+     if (target < 0) {
+             target = fdt_next_node(blob, -1, NULL);
+             if (target < 0) {
+                     dev_err(dev, "invalid target");
+                     return -EINVAL;
+             }
+     }
+     if (overlay_offset < 0) {
+             overlay_offset = fdt_next_node(overlay_blob, -1, NULL);
+             if (overlay_offset < 0) {
+                     dev_err(dev, "invalid overlay");
+                     return -EINVAL;
+             }
+     }
+
+     fdt_for_each_property_offset(property, overlay_blob, overlay_offset) {
+             const char *name;
+             const void *prop;
+             int prop_len;
+
+             prop = fdt_getprop_by_offset(overlay_blob, property, &name,
+                                          &prop_len);
+             if (!prop || prop_len >= MAX_BLOB_SIZE) {
+                     dev_err(dev, "internal error");
+                     return -EINVAL;
+             }
+
+             ret = xrt_md_setprop(dev, blob, target, name, prop,
+                                  prop_len);
+             if (ret) {
+                     dev_err(dev, "setprop failed, ret = %d", ret);
+                     return ret;
+             }
+     }
+
+     fdt_for_each_subnode(subnode, overlay_blob, overlay_offset) {
+             const char *name = fdt_get_name(overlay_blob, subnode, NULL);
+             int nnode;
+
+             nnode = xrt_md_add_node(dev, blob, target, name);
+             if (nnode == -FDT_ERR_EXISTS)
+                     nnode = fdt_subnode_offset(blob, target, name);
+             if (nnode < 0) {
+                     dev_err(dev, "add node failed, ret = %d", nnode);
+                     return nnode;
+             }
+
+             ret = xrt_md_overlay(dev, blob, nnode, overlay_blob, subnode);
+             if (ret)
+                     return ret;
+     }
+
+     return 0;
+}
+
+int xrt_md_get_next_endpoint(struct device *dev, const char *blob,
+                          const char *ep_name, const char *regmap_name,
+                          char **next_ep, char **next_regmap)
+{
+     int offset, ret;
+
+     if (!ep_name) {
+             ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_ENDPOINTS, NULL,
+                                       &offset);
+     } else {
+             ret = xrt_md_get_endpoint(dev, blob, ep_name, regmap_name,
+                                       &offset);
+     }
+
+     if (ret) {
+             *next_ep = NULL;
+             *next_regmap = NULL;
+             return -EINVAL;
+     }
+
+     offset = ep_name ? fdt_next_subnode(blob, offset) :
+             fdt_first_subnode(blob, offset);
+     if (offset < 0) {
+             *next_ep = NULL;
+             *next_regmap = NULL;
+             return -EINVAL;
+     }
+
+     *next_ep = (char *)fdt_get_name(blob, offset, NULL);
+     *next_regmap = (char *)fdt_stringlist_get(blob, offset, XRT_MD_PROP_COMPATIBLE,
+                                               0, NULL);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_next_endpoint);
+
+int xrt_md_get_compatible_endpoint(struct device *dev, const char *blob,
+                                const char *regmap_name, const char **ep_name)
+{
+     int ep_offset;
+
+     ep_offset = fdt_node_offset_by_compatible(blob, -1, regmap_name);
+     if (ep_offset < 0) {
+             *ep_name = NULL;
+             return -ENOENT;
+     }
+
+     *ep_name = (char *)fdt_get_name(blob, ep_offset, NULL);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_compatible_endpoint);
+
+void xrt_md_pack(struct device *dev, char *blob)
+{
+     int ret;
+
+     ret = fdt_pack(blob);
+     if (ret)
+             dev_err(dev, "pack failed %d", ret);
+}
+EXPORT_SYMBOL_GPL(xrt_md_pack);
+
+int xrt_md_get_intf_uuids(struct device *dev, const char *blob,
+                       u32 *num_uuids, uuid_t *intf_uuids)
+{
+     int offset, count = 0;
+     int ret;
+     const char *uuid_str;
+
+     ret = xrt_md_get_endpoint(dev, blob, XRT_MD_NODE_INTERFACES, NULL, &offset);
+     if (ret)
+             return -ENOENT;
+
+     for (offset = fdt_first_subnode(blob, offset);
+         offset >= 0;
+         offset = fdt_next_subnode(blob, offset)) {
+             uuid_str = fdt_getprop(blob, offset, XRT_MD_PROP_INTERFACE_UUID,
+                                    NULL);
+             if (!uuid_str) {
+                     dev_err(dev, "empty intf uuid node");
+                     return -EINVAL;
+             }
+
+             if (intf_uuids && count < *num_uuids) {
+                     ret = xrt_md_trans_str2uuid(dev, uuid_str,
+                                                 &intf_uuids[count]);
+                     if (ret)
+                             return -EINVAL;
+             }
+             count++;
+     }
+
+     *num_uuids = count;
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_md_get_intf_uuids);
--

2.18.4

author
#11 lore raw

PATCH v3 3/18 fpga: xrt: xclbin file helper functions

In reply to Lizhi Hou

Alveo FPGA firmware and partial reconfigure file are in xclbin format. Add code to enumerate and extract sections from xclbin files. xclbin.h is cross platform and used across all platforms and OS

---
 drivers/fpga/xrt/include/xclbin-helper.h |  52 +++
 drivers/fpga/xrt/lib/xclbin.c            | 394 ++++++++++++++++++++++
 include/uapi/linux/xrt/xclbin.h          | 408 +++++++++++++++++++++++
 3 files changed, 854 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/xrt/xclbin.h
drivers/fpga/xrt/include/xclbin-helper.h new file +52 -0
new file mode 100644
index 000000000000..68218efc9d0b
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Header file for Xilinx Runtime (XRT) driver
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *    David Zhang <[email protected]>
+ *    Sonal Santan <[email protected]>
+ */
+
+#ifndef _XRT_XCLBIN_H
+#define _XRT_XCLBIN_H
+
+#include <linux/types.h>
+#include <linux/device.h>
+#include <linux/xrt/xclbin.h>
+
+#define ICAP_XCLBIN_V2	"xclbin2"
+#define DMA_HWICAP_BITFILE_BUFFER_SIZE 1024
+#define MAX_XCLBIN_SIZE (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 hw_icap_bit_header {
+	unsigned int header_length;     /* Length of header in 32 bit words */
+	unsigned int bitstream_length;  /* Length of bitstream to read in bytes*/
+	unsigned char *design_name;     /* Design name get from bitstream */
+	unsigned char *part_name;       /* Part name read from bitstream */
+	unsigned char *date;           /* Date read from bitstream header */
+	unsigned char *time;           /* Bitstream creation time */
+	unsigned int magic_length;      /* Length of the magic numbers */
+	unsigned char *version;		/* Version string */
+};
+
+const char *xrt_xclbin_kind_to_string(enum axlf_section_kind kind);
+int xrt_xclbin_get_section(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(const unsigned char *data,
+				      unsigned int size,
+				      struct hw_icap_bit_header *header);
+void xrt_xclbin_free_header(struct hw_icap_bit_header *header);
+const char *xrt_clock_type2epname(enum CLOCK_TYPE type);
+
+#endif /* _XRT_XCLBIN_H */
drivers/fpga/xrt/lib/xclbin.c new file +394 -0

394 lines not on this page · raw message

include/uapi/linux/xrt/xclbin.h new file +408 -0

408 lines not on this page · raw message

Signature · 1 line
2.18.4
reviewer
#12 lore raw

Re: [PATCH V3 XRT Alveo 03/18] fpga: xrt: xclbin file helper functions

Quoted 1 line · Lizhi Hou

Alveo FPGA firmware and partial reconfigure file are in xclbin format.

This code enumerates and extracts

Quoted 25 lines

Add
code to enumerate and extract sections from xclbin files. xclbin.h is cross platform and used across all platforms and OS

---
 drivers/fpga/xrt/include/xclbin-helper.h |  52 +++
 drivers/fpga/xrt/lib/xclbin.c            | 394 ++++++++++++++++++++++
 include/uapi/linux/xrt/xclbin.h          | 408 +++++++++++++++++++++++
 3 files changed, 854 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/xrt/xclbin.h
drivers/fpga/xrt/include/xclbin-helper.h new file +3 -0
new file mode 100644
index 000000000000..68218efc9d0b
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Header file for Xilinx Runtime (XRT) driver

drivers/fpga/xrt/include/xclbin-helper.h line 13

Quoted 10 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +10 -0
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *    David Zhang <[email protected]>
+ *    Sonal Santan <[email protected]>
+ */
+
+#ifndef _XRT_XCLBIN_H
+#define _XRT_XCLBIN_H

The header guard should match the filename.

drivers/fpga/xrt/include/xclbin-helper.h line 21

Quoted 8 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +8 -0
+
+#include <linux/types.h>
+#include <linux/device.h>
+#include <linux/xrt/xclbin.h>
+
+#define ICAP_XCLBIN_V2	"xclbin2"
+#define DMA_HWICAP_BITFILE_BUFFER_SIZE 1024
+#define MAX_XCLBIN_SIZE (1024 * 1024 * 1024) /* Assuming xclbin <= 1G, always */

#defines should have a prefix, maybe XRT_ or XCLBIN_

drivers/fpga/xrt/include/xclbin-helper.h line 30

Quoted 9 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +9 -0
+
+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 hw_icap_bit_header {

File headers usually have fixed length fields like uint32_t

Is this a structure the real header is converted into ?

drivers/fpga/xrt/include/xclbin-helper.h line 41

Quoted 11 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +11 -0
+	unsigned int header_length;     /* Length of header in 32 bit words */
+	unsigned int bitstream_length;  /* Length of bitstream to read in bytes*/
+	unsigned char *design_name;     /* Design name get from bitstream */
+	unsigned char *part_name;       /* Part name read from bitstream */
+	unsigned char *date;           /* Date read from bitstream header */
+	unsigned char *time;           /* Bitstream creation time */
+	unsigned int magic_length;      /* Length of the magic numbers */
+	unsigned char *version;		/* Version string */
+};
+
+const char *xrt_xclbin_kind_to_string(enum axlf_section_kind kind);

Only add decl's that are using in multiple files.

This is only defined in xclbin.c, why does it need to be in the header ?

drivers/fpga/xrt/include/xclbin-helper.h line 50

Quoted 9 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +9 -0
+int xrt_xclbin_get_section(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(const unsigned char *data,
+				      unsigned int size,
+				      struct hw_icap_bit_header *header);
+void xrt_xclbin_free_header(struct hw_icap_bit_header *header);
+const char *xrt_clock_type2epname(enum CLOCK_TYPE type);

CLOCK_TYPE needs a prefix, something like XCLBIN_CLOCK_TYPE

Quoted 13 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +2 -0
+
+#endif /* _XRT_XCLBIN_H */
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
new file mode 100644
index 000000000000..47dc6ca25c1b
@@ -0,0 +1,394 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Xilinx Alveo FPGA Driver XCLBIN parser
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.

drivers/fpga/xrt/lib/xclbin.c line 15

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+ *
+ * Authors: David Zhang <[email protected]>
+ */
+
+#include <asm/errno.h>
+#include <linux/vmalloc.h>
+#include <linux/device.h>
+#include "xclbin-helper.h"
+#include "metadata.h"
+

What is XHI ?  Maybe expand this, at the lease should comment

drivers/fpga/xrt/lib/xclbin.c line 27

Quoted 12 lines
drivers/fpga/xrt/lib/xclbin.c new file +12 -0
+/* Used for parsing bitstream header */
+#define XHI_EVEN_MAGIC_BYTE     0x0f
+#define XHI_ODD_MAGIC_BYTE      0xf0
+
+/* Extra mode for IDLE */
+#define XHI_OP_IDLE  -1
+#define XHI_BIT_HEADER_FAILURE -1
+
+/* The imaginary module length register */
+#define XHI_MLR                  15
+
+static inline unsigned char xhi_data_and_inc(const unsigned char *d, int *i, int sz)

could move to the *.h

drivers/fpga/xrt/lib/xclbin.c line 32

Quoted 5 lines
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
+{_
+	unsigned char data;
+
+	if (*i >= sz)
+		return -1;

The return value of this funtion is not always checked, at the least add a dev_err here

Quoted 18 lines
drivers/fpga/xrt/lib/xclbin.c new file +18 -0
+
+	data = d[*i];
+	(*i)++;
+
+	return data;
+}
+
+static const struct axlf_section_header *
+xrt_xclbin_get_section_hdr(const struct axlf *xclbin,
+			   enum axlf_section_kind kind)
+{
+	int i = 0;
+
+	for (i = 0; i < xclbin->m_header.m_numSections; i++) {
+		if (xclbin->m_sections[i].m_sectionKind == kind)
+			return &xclbin->m_sections[i];
+	}
+

drivers/fpga/xrt/lib/xclbin.c line 60

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+	return NULL;
+}
+
+static int
+xrt_xclbin_check_section_hdr(const struct axlf_section_header *header,
+			     u64 xclbin_len)
+{
+	int ret;
+
+	ret = (header->m_sectionOffset + header->m_sectionSize) > xclbin_len ? -EINVAL : 0;

Tristate is harder to read, consider replacing with if()

int ret = 0

if ()

  ret =

Quoted 9 lines
drivers/fpga/xrt/lib/xclbin.c new file +9 -0
+
+	return ret;
+}
+
+static int xrt_xclbin_section_info(const struct axlf *xclbin,
+				   enum axlf_section_kind kind,
+				   u64 *offset, u64 *size)
+{
+	const struct axlf_section_header *mem_header = NULL;

drivers/fpga/xrt/lib/xclbin.c line 79

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+	u64 xclbin_len;
+	int err = 0;
+
+	mem_header = xrt_xclbin_get_section_hdr(xclbin, kind);
+	if (!mem_header)
+		return -EINVAL;
+
+	xclbin_len = xclbin->m_header.m_length;
+	if (xclbin_len > MAX_XCLBIN_SIZE)
+		return -EINVAL;

This check can be added to the function call..

or the sanity checking added to the earier call to *get_section_hdr

There a number of small functions that can be combined.

drivers/fpga/xrt/lib/xclbin.c line 91

Quoted 12 lines
drivers/fpga/xrt/lib/xclbin.c new file +12 -0
+
+	err = xrt_xclbin_check_section_hdr(mem_header, xclbin_len);
+	if (err)
+		return err;
+
+	*offset = mem_header->m_sectionOffset;
+	*size = mem_header->m_sectionSize;
+
+	return 0;
+}
+
+/* caller should free the allocated memory for **data */

must free

This comment also needs to be with the *.h decl

Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+int xrt_xclbin_get_section(const struct axlf *buf,
+			   enum axlf_section_kind kind,
+			   void **data, u64 *len)
+{
+	const struct axlf *xclbin = (const struct axlf *)buf;
+	void *section = NULL;
+	int err = 0;
+	u64 offset = 0;
+	u64 size = 0;
+
+	err = xrt_xclbin_section_info(xclbin, kind, &offset, &size);

drivers/fpga/xrt/lib/xclbin.c line 112

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+	if (err)
+		return err;
+
+	section = vmalloc(size);
+	if (!section)
+		return -ENOMEM;
+
+	memcpy(section, ((const char *)xclbin) + offset, size);
+
+	*data = section;

a general comment

for exported function checking the validity of the inputs in more important.

here you assume **data is valid, really you should check.

drivers/fpga/xrt/lib/xclbin.c line 114

Quoted 2 lines
drivers/fpga/xrt/lib/xclbin.c new file +2 -0
+	if (len)
+		*len = size;

len setting being optional, needs to be in the *.h comment

drivers/fpga/xrt/lib/xclbin.c line 119

Quoted 5 lines
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_get_section);
+

Instead of allocating new memory and making copies of bits of *data

why not have the points reference data ?

The size operations look like translating big endian data to little endian.

This will break on a big endian host.

Quoted 6 lines
drivers/fpga/xrt/lib/xclbin.c new file +6 -0
+/* parse bitstream header */
+int xrt_xclbin_parse_bitstream_header(const unsigned char *data,
+				      unsigned int size,
+				      struct hw_icap_bit_header *header)
+{
+	unsigned int index;

drivers/fpga/xrt/lib/xclbin.c line 135

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+	unsigned int len;
+	unsigned int tmp;
+	unsigned int i;
+
+	memset(header, 0, sizeof(*header));
+	/* Start Index at start of bitstream */
+	index = 0;
+
+	/* Initialize HeaderLength.  If header returned early inidicates
+	 * failure.

This side effect should be documented in the *.h comment.

Also the multi line comment is a bit weird, not sure if it is ok

Quoted 82 lines
drivers/fpga/xrt/lib/xclbin.c new file +82 -0
+	 */
+	header->header_length = XHI_BIT_HEADER_FAILURE;
+
+	/* Get "Magic" length */
+	header->magic_length = xhi_data_and_inc(data, &index, size);
+	header->magic_length = (header->magic_length << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* Read in "magic" */
+	for (i = 0; i < header->magic_length - 1; i++) {
+		tmp = xhi_data_and_inc(data, &index, size);
+		if (i % 2 == 0 && tmp != XHI_EVEN_MAGIC_BYTE)
+			return -1;	/* INVALID_FILE_HEADER_ERROR */
+
+		if (i % 2 == 1 && tmp != XHI_ODD_MAGIC_BYTE)
+			return -1;	/* INVALID_FILE_HEADER_ERROR */
+	}
+
+	/* Read null end of magic data. */
+	tmp = xhi_data_and_inc(data, &index, size);
+
+	/* Read 0x01 (short) */
+	tmp = xhi_data_and_inc(data, &index, size);
+	tmp = (tmp << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* Check the "0x01" half word */
+	if (tmp != 0x01)
+		return -1;	/* INVALID_FILE_HEADER_ERROR */
+
+	/* Read 'a' */
+	tmp = xhi_data_and_inc(data, &index, size);
+	if (tmp != 'a')
+		return -1;	/* INVALID_FILE_HEADER_ERROR	*/
+
+	/* Get Design Name length */
+	len = xhi_data_and_inc(data, &index, size);
+	len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* allocate space for design name and final null character. */
+	header->design_name = vmalloc(len);
+	if (!header->design_name)
+		return -ENOMEM;
+
+	/* Read in Design Name */
+	for (i = 0; i < len; i++)
+		header->design_name[i] = xhi_data_and_inc(data, &index, size);
+
+	if (header->design_name[len - 1] != '\0')
+		return -1;
+
+	header->version = strstr(header->design_name, "Version=") + strlen("Version=");
+
+	/* Read 'b' */
+	tmp = xhi_data_and_inc(data, &index, size);
+	if (tmp != 'b')
+		return -1;	/* INVALID_FILE_HEADER_ERROR */
+
+	/* Get Part Name length */
+	len = xhi_data_and_inc(data, &index, size);
+	len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* allocate space for part name and final null character. */
+	header->part_name = vmalloc(len);
+	if (!header->part_name)
+		return -ENOMEM;
+
+	/* Read in part name */
+	for (i = 0; i < len; i++)
+		header->part_name[i] = xhi_data_and_inc(data, &index, size);
+
+	if (header->part_name[len - 1] != '\0')
+		return -1;
+
+	/* Read 'c' */
+	tmp = xhi_data_and_inc(data, &index, size);
+	if (tmp != 'c')
+		return -1;	/* INVALID_FILE_HEADER_ERROR */
+
+	/* Get date length */
+	len = xhi_data_and_inc(data, &index, size);
+	len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* allocate space for date and final null character. */

drivers/fpga/xrt/lib/xclbin.c line 227

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+	header->date = vmalloc(len);
+	if (!header->date)
+		return -ENOMEM;
+
+	/* Read in date name */
+	for (i = 0; i < len; i++)
+		header->date[i] = xhi_data_and_inc(data, &index, size);
+
+	if (header->date[len - 1] != '\0')
+		return -1;

generally -EINVAL is more meaningful than -1

Quoted 25 lines
drivers/fpga/xrt/lib/xclbin.c new file +25 -0
+
+	/* Read 'd' */
+	tmp = xhi_data_and_inc(data, &index, size);
+	if (tmp != 'd')
+		return -1;	/* INVALID_FILE_HEADER_ERROR  */
+
+	/* Get time length */
+	len = xhi_data_and_inc(data, &index, size);
+	len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* allocate space for time and final null character. */
+	header->time = vmalloc(len);
+	if (!header->time)
+		return -ENOMEM;
+
+	/* Read in time name */
+	for (i = 0; i < len; i++)
+		header->time[i] = xhi_data_and_inc(data, &index, size);
+
+	if (header->time[len - 1] != '\0')
+		return -1;
+
+	/* Read 'e' */
+	tmp = xhi_data_and_inc(data, &index, size);
+	if (tmp != 'e')

drivers/fpga/xrt/lib/xclbin.c line 262

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+		return -1;	/* INVALID_FILE_HEADER_ERROR */
+
+	/* Get byte length of bitstream */
+	header->bitstream_length = xhi_data_and_inc(data, &index, size);
+	header->bitstream_length = (header->bitstream_length << 8) |
+		xhi_data_and_inc(data, &index, size);
+	header->bitstream_length = (header->bitstream_length << 8) |
+		xhi_data_and_inc(data, &index, size);
+	header->bitstream_length = (header->bitstream_length << 8) |
+		xhi_data_and_inc(data, &index, size);

generally a problem

This is confusing, collect the bytes in a temp[] and construct the header->bitstream_length in on statement.

This is a case where xhi_data_and_inc return is not checked and if it failed could blow up later.

drivers/fpga/xrt/lib/xclbin.c line 264

Quoted 2 lines
drivers/fpga/xrt/lib/xclbin.c new file +2 -0
+
+	header->header_length = index;

index is not a good variable name if it going to be stored as a length.

consider changing it to something like current_length.

drivers/fpga/xrt/lib/xclbin.c line 275

Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_parse_bitstream_header);
+
+void xrt_xclbin_free_header(struct hw_icap_bit_header *header)
+{
+	vfree(header->design_name);
+	vfree(header->part_name);
+	vfree(header->date);
+	vfree(header->time);

missing header->version

Quoted 26 lines
drivers/fpga/xrt/lib/xclbin.c new file +26 -0
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_free_header);
+
+struct xrt_clock_desc {
+	char	*clock_ep_name;
+	u32	clock_xclbin_type;
+	char	*clkfreq_ep_name;
+} clock_desc[] = {
+	{
+		.clock_ep_name = XRT_MD_NODE_CLK_KERNEL1,
+		.clock_xclbin_type = CT_DATA,
+		.clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_K1,
+	},
+	{
+		.clock_ep_name = XRT_MD_NODE_CLK_KERNEL2,
+		.clock_xclbin_type = CT_KERNEL,
+		.clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_K2,
+	},
+	{
+		.clock_ep_name = XRT_MD_NODE_CLK_KERNEL3,
+		.clock_xclbin_type = CT_SYSTEM,
+		.clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_HBM,
+	},
+};
+
+const char *xrt_clock_type2epname(enum CLOCK_TYPE type)

drivers/fpga/xrt/lib/xclbin.c line 311

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(clock_desc); i++) {
+		if (clock_desc[i].clock_xclbin_type == type)
+			return clock_desc[i].clock_ep_name;
+	}
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(xrt_clock_type2epname);

What is clock stuff doing in xclbin ?

I think clock needs its own file

Quoted 14 lines
drivers/fpga/xrt/lib/xclbin.c new file +14 -0
+
+static const char *clock_type2clkfreq_name(u32 type)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(clock_desc); i++) {
+		if (clock_desc[i].clock_xclbin_type == type)
+			return clock_desc[i].clkfreq_ep_name;
+	}
+	return NULL;
+}
+
+static int xrt_xclbin_add_clock_metadata(struct device *dev,
+					 const struct axlf *xclbin,

drivers/fpga/xrt/lib/xclbin.c line 335

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+					 char *dtb)
+{
+	int i;
+	u16 freq;
+	struct clock_freq_topology *clock_topo;
+	int rc = xrt_xclbin_get_section(xclbin, CLOCK_FREQ_TOPOLOGY,
+					(void **)&clock_topo, NULL);
+
+	if (rc)
+		return 0;

failing is ok ?

Quoted 9 lines
drivers/fpga/xrt/lib/xclbin.c new file +9 -0
+
+	for (i = 0; i < clock_topo->m_count; i++) {
+		u8 type = clock_topo->m_clock_freq[i].m_type;
+		const char *ep_name = xrt_clock_type2epname(type);
+		const char *counter_name = clock_type2clkfreq_name(type);
+
+		if (!ep_name || !counter_name)
+			continue;
+

drivers/fpga/xrt/lib/xclbin.c line 354

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+		freq = cpu_to_be16(clock_topo->m_clock_freq[i].m_freq_Mhz);
+		rc = xrt_md_set_prop(dev, dtb, ep_name, NULL, XRT_MD_PROP_CLK_FREQ,
+				     &freq, sizeof(freq));
+		if (rc)
+			break;
+
+		rc = xrt_md_set_prop(dev, dtb, ep_name, NULL, XRT_MD_PROP_CLK_CNT,
+				     counter_name, strlen(counter_name) + 1);
+		if (rc)
+			break;

Failing in a loop, why isn't there some cleanup of the *set_prop() calls ?

drivers/fpga/xrt/lib/xclbin.c line 365

Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+	}
+
+	vfree(clock_topo);
+
+	return rc;
+}
+
+int xrt_xclbin_get_metadata(struct device *dev, const struct axlf *xclbin, char **dtb)
+{
+	char *md = NULL, *newmd = NULL;
+	u64 len;

*dtb = NULL;

Quoted 9 lines
drivers/fpga/xrt/lib/xclbin.c new file +9 -0
+	int rc = xrt_xclbin_get_section(xclbin, PARTITION_METADATA,
+					(void **)&md, &len);
+
+	if (rc)
+		goto done;
+
+	/* Sanity check the dtb section. */
+	if (xrt_md_size(dev, md) > len) {
+		rc = -EINVAL;

drivers/fpga/xrt/lib/xclbin.c line 384

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+		goto done;
+	}
+
+	newmd = xrt_md_dup(dev, md);
+	if (!newmd) {
+		rc = -EFAULT;
+		goto done;
+	}
+	/* Convert various needed xclbin sections into dtb. */
+	rc = xrt_xclbin_add_clock_metadata(dev, xclbin, newmd);

newmd is only valid here, but the above error handling jump here. change this to

if (!rc)

  *dtb = newmd

else

   vfree(newmd)

done:

  vfree(md)

  return rc;

Quoted 17 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+
+done:
+	if (rc == 0)
+		*dtb = newmd;
+	else
+		vfree(newmd);
+	vfree(md);
+	return rc;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_get_metadata);
include/uapi/linux/xrt/xclbin.h new file +1 -0
new file mode 100644
index 000000000000..53f140123ef1
@@ -0,0 +1,408 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */

include/uapi/linux/xrt/xclbin.h line 11

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+/*
+ *  Xilinx FPGA compiled binary container format
+ *
+ *  Copyright (C) 2015-2021, Xilinx Inc
+ */
+
+#ifndef _XCLBIN_H_
+#define _XCLBIN_H_
+
+#ifdef _WIN32

WIN32 ?

Only 1 other header has this ifdef

include/uapi/linux/xrt/xclbin.h line 13

Quoted 2 lines
include/uapi/linux/xrt/xclbin.h new file +2 -0
+  #include <cstdint>
+  #include <algorithm>

c++ is being assumed for windows

include/uapi/linux/xrt/xclbin.h line 14

Quoted 1 line
include/uapi/linux/xrt/xclbin.h new file +1 -0
+  #include "windows/uuid.h"

thank you for not including windows.h ;)

Quoted 7 lines
include/uapi/linux/xrt/xclbin.h new file +7 -0
+#else
+  #if defined(__KERNEL__)
+    #include <linux/types.h>
+    #include <linux/uuid.h>
+    #include <linux/version.h>
+  #elif defined(__cplusplus)
+    #include <cstdlib>

include/uapi/linux/xrt/xclbin.h line 31

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+    #include <cstdint>
+    #include <algorithm>
+    #include <uuid/uuid.h>
+  #else
+    #include <stdlib.h>
+    #include <stdint.h>
+    #include <uuid/uuid.h>
+  #endif
+#endif
+

Review these includes, some could be convenience includes.

ex/ linux/version.h with no obvious use of version macros.

struct axlf_header {
+ uint64_t m_length; /* Total size of the xclbin file */
.. snip ..
+ union {
+ char m_next_axlf[16]; /* Name of next xclbin file */
+ /* in the daisy chain */
+ uuid_t uuid; /* uuid of this xclbin*/
+ };

As mentioned in an earlier patch, if uuid_t is larger than 16 bytes, axlf_header breaks. while it is convenient to have this type here, it would be better this access was handled in another way. Maybe a host specific function.

I also do not see a pragma pack, usually this is set of 1 so the compiler does not shuffle elements, increase size etc.

include/uapi/linux/xrt/xclbin.h line 39

Quoted 8 lines
include/uapi/linux/xrt/xclbin.h new file +8 -0
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * DOC: Container format for Xilinx FPGA images
+ * The container stores bitstreams, metadata and firmware images.
+ * xclbin/xsabin is ELF-like binary container format. It is structured

is an ELF-like file format.  It is a structured

Quoted 20 lines
include/uapi/linux/xrt/xclbin.h new file +20 -0
+ * series of sections. There is a file header followed by several section
+ * headers which is followed by sections. A section header points to an
+ * actual section. There is an optional signature at the end. The
+ * following figure illustrates a typical xclbin:
+ *
+ *     +---------------------+
+ *     |		     |
+ *     |       HEADER	     |
+ *     +---------------------+
+ *     |   SECTION  HEADER   |
+ *     |		     |
+ *     +---------------------+
+ *     |	 ...	     |
+ *     |		     |
+ *     +---------------------+
+ *     |   SECTION  HEADER   |
+ *     |		     |
+ *     +---------------------+
+ *     |       SECTION	     |
+ *     |		     |

include/uapi/linux/xrt/xclbin.h line 69

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+ *     +---------------------+
+ *     |	 ...	     |
+ *     |		     |
+ *     +---------------------+
+ *     |       SECTION	     |
+ *     |		     |
+ *     +---------------------+
+ *     |      SIGNATURE	     |
+ *     |      (OPTIONAL)     |
+ *     +---------------------+

This ascii art is a mixture of tabs and spaces, for someone with tab = 2 spaces, this will look messed up.

convert the tabs to spaces

include/uapi/linux/xrt/xclbin.h line 73

Quoted 4 lines
include/uapi/linux/xrt/xclbin.h new file +4 -0
+ */
+
+enum XCLBIN_MODE {
+	XCLBIN_FLAT,

generally

all enums used in a file format should be initialized.

This likely should be

XCLBIN_FLAT = 0,

Quoted 67 lines
include/uapi/linux/xrt/xclbin.h new file +67 -0
+	XCLBIN_PR,
+	XCLBIN_TANDEM_STAGE2,
+	XCLBIN_TANDEM_STAGE2_WITH_PR,
+	XCLBIN_HW_EMU,
+	XCLBIN_SW_EMU,
+	XCLBIN_MODE_MAX
+};
+
+enum axlf_section_kind {
+	BITSTREAM = 0,
+	CLEARING_BITSTREAM,
+	EMBEDDED_METADATA,
+	FIRMWARE,
+	DEBUG_DATA,
+	SCHED_FIRMWARE,
+	MEM_TOPOLOGY,
+	CONNECTIVITY,
+	IP_LAYOUT,
+	DEBUG_IP_LAYOUT,
+	DESIGN_CHECK_POINT,
+	CLOCK_FREQ_TOPOLOGY,
+	MCS,
+	BMC,
+	BUILD_METADATA,
+	KEYVALUE_METADATA,
+	USER_METADATA,
+	DNA_CERTIFICATE,
+	PDI,
+	BITSTREAM_PARTIAL_PDI,
+	PARTITION_METADATA,
+	EMULATION_DATA,
+	SYSTEM_METADATA,
+	SOFT_KERNEL,
+	ASK_FLASH,
+	AIE_METADATA,
+	ASK_GROUP_TOPOLOGY,
+	ASK_GROUP_CONNECTIVITY
+};
+
+enum MEM_TYPE {
+	MEM_DDR3,
+	MEM_DDR4,
+	MEM_DRAM,
+	MEM_STREAMING,
+	MEM_PREALLOCATED_GLOB,
+	MEM_ARE,
+	MEM_HBM,
+	MEM_BRAM,
+	MEM_URAM,
+	MEM_STREAMING_CONNECTION
+};
+
+enum IP_TYPE {
+	IP_MB = 0,
+	IP_KERNEL,
+	IP_DNASC,
+	IP_DDR4_CONTROLLER,
+	IP_MEM_DDR4,
+	IP_MEM_HBM
+};
+
+struct axlf_section_header {
+	uint32_t m_sectionKind;		    /* Section type */
+	char m_sectionName[16];		    /* Examples: "stage2", "clear1", */
+					    /* "clear2", "ocl1", "ocl2, */
+					    /* "ublaze", "sched" */
+	uint64_t m_sectionOffset;	    /* File offset of section data */

include/uapi/linux/xrt/xclbin.h line 150

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+	uint64_t m_sectionSize;		    /* Size of section data */
+};
+
+struct axlf_header {
+	uint64_t m_length;		    /* Total size of the xclbin file */
+	uint64_t m_timeStamp;		    /* Number of seconds since epoch */
+					    /* when xclbin was created */
+	uint64_t m_featureRomTimeStamp;	    /* TimeSinceEpoch of the featureRom */
+	uint16_t m_versionPatch;	    /* Patch Version */
+	uint8_t m_versionMajor;		    /* Major Version - Version: 2.1.0*/

i did not see the version checked earlier, which one is expected ?

include/uapi/linux/xrt/xclbin.h line 162

Quoted 12 lines
include/uapi/linux/xrt/xclbin.h new file +12 -0
+	uint8_t m_versionMinor;		    /* Minor Version */
+	uint32_t m_mode;		    /* XCLBIN_MODE */
+	union {
+		struct {
+			uint64_t m_platformId;	/* 64 bit platform ID: */
+					/* vendor-device-subvendor-subdev */
+			uint64_t m_featureId;	/* 64 bit feature id */
+		} rom;
+		unsigned char rom_uuid[16];	/* feature ROM UUID for which */
+						/* this xclbin was generated */
+	};
+	unsigned char m_platformVBNV[64];	/* e.g. */

what is VBNV?

Quoted 45 lines
include/uapi/linux/xrt/xclbin.h new file +45 -0
+		/* xilinx:xil-accel-rd-ku115:4ddr-xpr:3.4: null terminated */
+	union {
+		char m_next_axlf[16];		/* Name of next xclbin file */
+						/* in the daisy chain */
+		uuid_t uuid;			/* uuid of this xclbin*/
+	};
+	char m_debug_bin[16];			/* Name of binary with debug */
+						/* information */
+	uint32_t m_numSections;			/* Number of section headers */
+};
+
+struct axlf {
+	char m_magic[8];			/* Should be "xclbin2\0"  */
+	int32_t m_signature_length;		/* Length of the signature. */
+						/* -1 indicates no signature */
+	unsigned char reserved[28];		/* Note: Initialized to 0xFFs */
+
+	unsigned char m_keyBlock[256];		/* Signature for validation */
+						/* of binary */
+	uint64_t m_uniqueId;			/* axlf's uniqueId, use it to */
+						/* skip redownload etc */
+	struct axlf_header m_header;		/* Inline header */
+	struct axlf_section_header m_sections[1];   /* One or more section */
+						    /* headers follow */
+};
+
+/* bitstream information */
+struct xlnx_bitstream {
+	uint8_t m_freq[8];
+	char bits[1];
+};
+
+/****	MEMORY TOPOLOGY SECTION ****/
+struct mem_data {
+	uint8_t m_type; /* enum corresponding to mem_type. */
+	uint8_t m_used; /* if 0 this bank is not present */
+	union {
+		uint64_t m_size; /* if mem_type DDR, then size in KB; */
+		uint64_t route_id; /* if streaming then "route_id" */
+	};
+	union {
+		uint64_t m_base_address;/* if DDR then the base address; */
+		uint64_t flow_id; /* if streaming then "flow id" */
+	};
+	unsigned char m_tag[16]; /* DDR: BANK0,1,2,3, has to be null */

include/uapi/linux/xrt/xclbin.h line 217

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+			/* terminated; if streaming then stream0, 1 etc */
+};
+
+struct mem_topology {
+	int32_t m_count; /* Number of mem_data */
+	struct mem_data m_mem_data[1]; /* Should be sorted on mem_type */
+};
+
+/****	CONNECTIVITY SECTION ****/
+/* Connectivity of each argument of Kernel. It will be in terms of argument

This section does not make sense.

Likely you mean some algorithm kernel, rather than the linux kernel.

Quoted 24 lines
include/uapi/linux/xrt/xclbin.h new file +24 -0
+ * index associated. For associating kernel instances with arguments and
+ * banks, start at the connectivity section. Using the m_ip_layout_index
+ * access the ip_data.m_name. Now we can associate this kernel instance
+ * with its original kernel name and get the connectivity as well. This
+ * enables us to form related groups of kernel instances.
+ */
+
+struct connection {
+	int32_t arg_index; /* From 0 to n, may not be contiguous as scalars */
+			   /* skipped */
+	int32_t m_ip_layout_index; /* index into the ip_layout section. */
+			   /* ip_layout.m_ip_data[index].m_type == IP_KERNEL */
+	int32_t mem_data_index; /* index of the m_mem_data . Flag error is */
+				/* m_used false. */
+};
+
+struct connectivity {
+	int32_t m_count;
+	struct connection m_connection[1];
+};
+
+/****	IP_LAYOUT SECTION ****/
+
+/* IP Kernel */

include/uapi/linux/xrt/xclbin.h line 251

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+#define IP_INT_ENABLE_MASK	  0x0001
+#define IP_INTERRUPT_ID_MASK  0x00FE
+#define IP_INTERRUPT_ID_SHIFT 0x1
+
+enum IP_CONTROL {
+	AP_CTRL_HS = 0,
+	AP_CTRL_CHAIN = 1,
+	AP_CTRL_NONE = 2,
+	AP_CTRL_ME = 3,
+	ACCEL_ADAPTER = 4

assigning beyond the first is not necessary unless there are dups or gaps

Quoted 21 lines
include/uapi/linux/xrt/xclbin.h new file +21 -0
+};
+
+#define IP_CONTROL_MASK	 0xFF00
+#define IP_CONTROL_SHIFT 0x8
+
+/* IPs on AXI lite - their types, names, and base addresses.*/
+struct ip_data {
+	uint32_t m_type; /* map to IP_TYPE enum */
+	union {
+		uint32_t properties; /* Default: 32-bits to indicate ip */
+				     /* specific property. */
+		/* m_type: IP_KERNEL
+		 *	    m_int_enable   : Bit  - 0x0000_0001;
+		 *	    m_interrupt_id : Bits - 0x0000_00FE;
+		 *	    m_ip_control   : Bits = 0x0000_FF00;
+		 */
+		struct {		 /* m_type: IP_MEM_* */
+			uint16_t m_index;
+			uint8_t m_pc_index;
+			uint8_t unused;
+		} indices;

include/uapi/linux/xrt/xclbin.h line 282

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+	};
+	uint64_t m_base_address;
+	uint8_t m_name[64]; /* eg Kernel name corresponding to KERNEL */
+			    /* instance, can embed CU name in future. */
+};
+
+struct ip_layout {
+	int32_t m_count;
+	struct ip_data m_ip_data[1]; /* All the ip_data needs to be sorted */
+				     /* by m_base_address. */

general

doing the bla[1] for c++ ?

Tom

Quoted 126 lines
include/uapi/linux/xrt/xclbin.h new file +126 -0
+};
+
+/*** Debug IP section layout ****/
+enum DEBUG_IP_TYPE {
+	UNDEFINED = 0,
+	LAPC,
+	ILA,
+	AXI_MM_MONITOR,
+	AXI_TRACE_FUNNEL,
+	AXI_MONITOR_FIFO_LITE,
+	AXI_MONITOR_FIFO_FULL,
+	ACCEL_MONITOR,
+	AXI_STREAM_MONITOR,
+	AXI_STREAM_PROTOCOL_CHECKER,
+	TRACE_S2MM,
+	AXI_DMA,
+	TRACE_S2MM_FULL
+};
+
+struct debug_ip_data {
+	uint8_t m_type; /* type of enum DEBUG_IP_TYPE */
+	uint8_t m_index_lowbyte;
+	uint8_t m_properties;
+	uint8_t m_major;
+	uint8_t m_minor;
+	uint8_t m_index_highbyte;
+	uint8_t m_reserved[2];
+	uint64_t m_base_address;
+	char	m_name[128];
+};
+
+struct debug_ip_layout {
+	uint16_t m_count;
+	struct debug_ip_data m_debug_ip_data[1];
+};
+
+/* Supported clock frequency types */
+enum CLOCK_TYPE {
+	CT_UNUSED = 0,			   /* Initialized value */
+	CT_DATA	  = 1,			   /* Data clock */
+	CT_KERNEL = 2,			   /* Kernel clock */
+	CT_SYSTEM = 3			   /* System Clock */
+};
+
+/* Clock Frequency Entry */
+struct clock_freq {
+	uint16_t m_freq_Mhz;		   /* Frequency in MHz */
+	uint8_t m_type;			   /* Clock type (enum CLOCK_TYPE) */
+	uint8_t m_unused[5];		   /* Not used - padding */
+	char m_name[128];		   /* Clock Name */
+};
+
+/* Clock frequency section */
+struct clock_freq_topology {
+	int16_t m_count;		   /* Number of entries */
+	struct clock_freq m_clock_freq[1]; /* Clock array */
+};
+
+/* Supported MCS file types */
+enum MCS_TYPE {
+	MCS_UNKNOWN = 0,		   /* Initialized value */
+	MCS_PRIMARY = 1,		   /* The primary mcs file data */
+	MCS_SECONDARY = 2,		   /* The secondary mcs file data */
+};
+
+/* One chunk of MCS data */
+struct mcs_chunk {
+	uint8_t m_type;			   /* MCS data type */
+	uint8_t m_unused[7];		   /* padding */
+	uint64_t m_offset;		   /* data offset from the start of */
+					   /* the section */
+	uint64_t m_size;		   /* data size */
+};
+
+/* MCS data section */
+struct mcs {
+	int8_t m_count;			   /* Number of chunks */
+	int8_t m_unused[7];		   /* padding */
+	struct mcs_chunk m_chunk[1];	   /* MCS chunks followed by data */
+};
+
+/* bmc data section */
+struct bmc {
+	uint64_t m_offset;		   /* data offset from the start of */
+					   /* the section */
+	uint64_t m_size;		   /* data size (bytes) */
+	char m_image_name[64];		   /* Name of the image */
+					   /* (e.g., MSP432P401R) */
+	char m_device_name[64];		   /* Device ID	(e.g., VCU1525)	 */
+	char m_version[64];
+	char m_md5value[33];		   /* MD5 Expected Value */
+				/* (e.g., 56027182079c0bd621761b7dab5a27ca)*/
+	char m_padding[7];		   /* Padding */
+};
+
+/* soft kernel data section, used by classic driver */
+struct soft_kernel {
+	/** Prefix Syntax:
+	 *  mpo - member, pointer, offset
+	 *  This variable represents a zero terminated string
+	 *  that is offseted from the beginning of the section.
+	 *  The pointer to access the string is initialized as follows:
+	 *  char * pCharString = (address_of_section) + (mpo value)
+	 */
+	uint32_t mpo_name;	   /* Name of the soft kernel */
+	uint32_t m_image_offset;   /* Image offset */
+	uint32_t m_image_size;	   /* Image size */
+	uint32_t mpo_version;	   /* Version */
+	uint32_t mpo_md5_value;	   /* MD5 checksum */
+	uint32_t mpo_symbol_name;  /* Symbol name */
+	uint32_t m_num_instances;  /* Number of instances */
+	uint8_t padding[36];	   /* Reserved for future use */
+	uint8_t reservedExt[16];   /* Reserved for future extended data */
+};
+
+enum CHECKSUM_TYPE {
+	CST_UNKNOWN = 0,
+	CST_SDBM = 1,
+	CST_LAST
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
maintainer
#13 lore raw

Re: [PATCH V3 XRT Alveo 03/18] fpga: xrt: xclbin file helper functions

Quoted 165 lines · Tom Rix
Quoted 1 line · Lizhi Hou

Alveo FPGA firmware and partial reconfigure file are in xclbin format.

This code enumerates and extracts

Quoted 25 lines

Add
code to enumerate and extract sections from xclbin files. xclbin.h is cross platform and used across all platforms and OS

---
 drivers/fpga/xrt/include/xclbin-helper.h |  52 +++
 drivers/fpga/xrt/lib/xclbin.c            | 394 ++++++++++++++++++++++
 include/uapi/linux/xrt/xclbin.h          | 408 +++++++++++++++++++++++
 3 files changed, 854 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/xrt/xclbin.h
drivers/fpga/xrt/include/xclbin-helper.h new file +3 -0
new file mode 100644
index 000000000000..68218efc9d0b
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Header file for Xilinx Runtime (XRT) driver

drivers/fpga/xrt/include/xclbin-helper.h line 13

Quoted 10 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +10 -0
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *    David Zhang <[email protected]>
+ *    Sonal Santan <[email protected]>
+ */
+
+#ifndef _XRT_XCLBIN_H
+#define _XRT_XCLBIN_H

The header guard should match the filename.

drivers/fpga/xrt/include/xclbin-helper.h line 21

Quoted 8 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +8 -0
+
+#include <linux/types.h>
+#include <linux/device.h>
+#include <linux/xrt/xclbin.h>
+
+#define ICAP_XCLBIN_V2	"xclbin2"
+#define DMA_HWICAP_BITFILE_BUFFER_SIZE 1024
+#define MAX_XCLBIN_SIZE (1024 * 1024 * 1024) /* Assuming xclbin <= 1G, always */

#defines should have a prefix, maybe XRT_ or XCLBIN_

drivers/fpga/xrt/include/xclbin-helper.h line 30

Quoted 9 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +9 -0
+
+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 hw_icap_bit_header {

File headers usually have fixed length fields like uint32_t

Is this a structure the real header is converted into ?

drivers/fpga/xrt/include/xclbin-helper.h line 41

Quoted 11 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +11 -0
+	unsigned int header_length;     /* Length of header in 32 bit words */
+	unsigned int bitstream_length;  /* Length of bitstream to read in bytes*/
+	unsigned char *design_name;     /* Design name get from bitstream */
+	unsigned char *part_name;       /* Part name read from bitstream */
+	unsigned char *date;           /* Date read from bitstream header */
+	unsigned char *time;           /* Bitstream creation time */
+	unsigned int magic_length;      /* Length of the magic numbers */
+	unsigned char *version;		/* Version string */
+};
+
+const char *xrt_xclbin_kind_to_string(enum axlf_section_kind kind);

Only add decl's that are using in multiple files.

This is only defined in xclbin.c, why does it need to be in the header ?

drivers/fpga/xrt/include/xclbin-helper.h line 50

Quoted 9 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +9 -0
+int xrt_xclbin_get_section(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(const unsigned char *data,
+				      unsigned int size,
+				      struct hw_icap_bit_header *header);
+void xrt_xclbin_free_header(struct hw_icap_bit_header *header);
+const char *xrt_clock_type2epname(enum CLOCK_TYPE type);

CLOCK_TYPE needs a prefix, something like XCLBIN_CLOCK_TYPE

Quoted 13 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +2 -0
+
+#endif /* _XRT_XCLBIN_H */
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
new file mode 100644
index 000000000000..47dc6ca25c1b
@@ -0,0 +1,394 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Xilinx Alveo FPGA Driver XCLBIN parser
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.

drivers/fpga/xrt/lib/xclbin.c line 15

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+ *
+ * Authors: David Zhang <[email protected]>
+ */
+
+#include <asm/errno.h>
+#include <linux/vmalloc.h>
+#include <linux/device.h>
+#include "xclbin-helper.h"
+#include "metadata.h"
+

What is XHI ?  Maybe expand this, at the lease should comment

drivers/fpga/xrt/lib/xclbin.c line 27

Quoted 12 lines
drivers/fpga/xrt/lib/xclbin.c new file +12 -0
+/* Used for parsing bitstream header */
+#define XHI_EVEN_MAGIC_BYTE     0x0f
+#define XHI_ODD_MAGIC_BYTE      0xf0
+
+/* Extra mode for IDLE */
+#define XHI_OP_IDLE  -1
+#define XHI_BIT_HEADER_FAILURE -1
+
+/* The imaginary module length register */
+#define XHI_MLR                  15
+
+static inline unsigned char xhi_data_and_inc(const unsigned char *d, int *i, int sz)

could move to the *.h

drivers/fpga/xrt/lib/xclbin.c line 32

Quoted 5 lines
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
+{_
+	unsigned char data;
+
+	if (*i >= sz)
+		return -1;

The return value of this funtion is not always checked, at the least add a dev_err here

Quoted 18 lines
drivers/fpga/xrt/lib/xclbin.c new file +18 -0
+
+	data = d[*i];
+	(*i)++;
+
+	return data;
+}
+
+static const struct axlf_section_header *
+xrt_xclbin_get_section_hdr(const struct axlf *xclbin,
+			   enum axlf_section_kind kind)
+{
+	int i = 0;
+
+	for (i = 0; i < xclbin->m_header.m_numSections; i++) {
+		if (xclbin->m_sections[i].m_sectionKind == kind)
+			return &xclbin->m_sections[i];
+	}
+

drivers/fpga/xrt/lib/xclbin.c line 60

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+	return NULL;
+}
+
+static int
+xrt_xclbin_check_section_hdr(const struct axlf_section_header *header,
+			     u64 xclbin_len)
+{
+	int ret;
+
+	ret = (header->m_sectionOffset + header->m_sectionSize) > xclbin_len ? -EINVAL : 0;

Tristate is harder to read, consider replacing with if()

Quoted 5 lines

int ret = 0

if ()

  ret =

Why not just:

if (header->m_section_offset + header->m_section_size)

	return -EINVAL;

return 0;

Also please fix the camelCase throughout the entire patchset.

Quoted 110 lines
Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +9 -0
+
+	return ret;
+}
+
+static int xrt_xclbin_section_info(const struct axlf *xclbin,
+				   enum axlf_section_kind kind,
+				   u64 *offset, u64 *size)
+{
+	const struct axlf_section_header *mem_header = NULL;

drivers/fpga/xrt/lib/xclbin.c line 79

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+	u64 xclbin_len;
+	int err = 0;
+
+	mem_header = xrt_xclbin_get_section_hdr(xclbin, kind);
+	if (!mem_header)
+		return -EINVAL;
+
+	xclbin_len = xclbin->m_header.m_length;
+	if (xclbin_len > MAX_XCLBIN_SIZE)
+		return -EINVAL;

This check can be added to the function call..

or the sanity checking added to the earier call to *get_section_hdr

There a number of small functions that can be combined.

drivers/fpga/xrt/lib/xclbin.c line 91

Quoted 12 lines
drivers/fpga/xrt/lib/xclbin.c new file +12 -0
+
+	err = xrt_xclbin_check_section_hdr(mem_header, xclbin_len);
+	if (err)
+		return err;
+
+	*offset = mem_header->m_sectionOffset;
+	*size = mem_header->m_sectionSize;
+
+	return 0;
+}
+
+/* caller should free the allocated memory for **data */

must free

This comment also needs to be with the *.h decl

Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+int xrt_xclbin_get_section(const struct axlf *buf,
+			   enum axlf_section_kind kind,
+			   void **data, u64 *len)
+{
+	const struct axlf *xclbin = (const struct axlf *)buf;
+	void *section = NULL;
+	int err = 0;
+	u64 offset = 0;
+	u64 size = 0;
+
+	err = xrt_xclbin_section_info(xclbin, kind, &offset, &size);

drivers/fpga/xrt/lib/xclbin.c line 112

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+	if (err)
+		return err;
+
+	section = vmalloc(size);
+	if (!section)
+		return -ENOMEM;
+
+	memcpy(section, ((const char *)xclbin) + offset, size);
+
+	*data = section;

a general comment

for exported function checking the validity of the inputs in more important.

here you assume **data is valid, really you should check.

drivers/fpga/xrt/lib/xclbin.c line 114

Quoted 2 lines
drivers/fpga/xrt/lib/xclbin.c new file +2 -0
+	if (len)
+		*len = size;

len setting being optional, needs to be in the *.h comment

drivers/fpga/xrt/lib/xclbin.c line 119

Quoted 5 lines
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_get_section);
+

Instead of allocating new memory and making copies of bits of *data

why not have the points reference data ?

The size operations look like translating big endian data to little endian.

This will break on a big endian host.

Quoted 6 lines
drivers/fpga/xrt/lib/xclbin.c new file +6 -0
+/* parse bitstream header */
+int xrt_xclbin_parse_bitstream_header(const unsigned char *data,
+				      unsigned int size,
+				      struct hw_icap_bit_header *header)
+{
+	unsigned int index;

drivers/fpga/xrt/lib/xclbin.c line 135

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+	unsigned int len;
+	unsigned int tmp;
+	unsigned int i;
+
+	memset(header, 0, sizeof(*header));
+	/* Start Index at start of bitstream */
+	index = 0;
+
+	/* Initialize HeaderLength.  If header returned early inidicates
+	 * failure.

This side effect should be documented in the *.h comment.

Also the multi line comment is a bit weird, not sure if it is ok

Quoted 11 lines

drivers/fpga/xrt/lib/xclbin.c line 146

Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+	 */
+	header->header_length = XHI_BIT_HEADER_FAILURE;
+
+	/* Get "Magic" length */
+	header->magic_length = xhi_data_and_inc(data, &index, size);
+	header->magic_length = (header->magic_length << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* Read in "magic" */
+	for (i = 0; i < header->magic_length - 1; i++) {
+		tmp = xhi_data_and_inc(data, &index, size);
+		if (i % 2 == 0 && tmp != XHI_EVEN_MAGIC_BYTE)

if !(i % 2) ...

Quoted 314 lines
Quoted 71 lines
drivers/fpga/xrt/lib/xclbin.c new file +71 -0
+			return -1;	/* INVALID_FILE_HEADER_ERROR */
+
+		if (i % 2 == 1 && tmp != XHI_ODD_MAGIC_BYTE)
+			return -1;	/* INVALID_FILE_HEADER_ERROR */
+	}
+
+	/* Read null end of magic data. */
+	tmp = xhi_data_and_inc(data, &index, size);
+
+	/* Read 0x01 (short) */
+	tmp = xhi_data_and_inc(data, &index, size);
+	tmp = (tmp << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* Check the "0x01" half word */
+	if (tmp != 0x01)
+		return -1;	/* INVALID_FILE_HEADER_ERROR */
+
+	/* Read 'a' */
+	tmp = xhi_data_and_inc(data, &index, size);
+	if (tmp != 'a')
+		return -1;	/* INVALID_FILE_HEADER_ERROR	*/
+
+	/* Get Design Name length */
+	len = xhi_data_and_inc(data, &index, size);
+	len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* allocate space for design name and final null character. */
+	header->design_name = vmalloc(len);
+	if (!header->design_name)
+		return -ENOMEM;
+
+	/* Read in Design Name */
+	for (i = 0; i < len; i++)
+		header->design_name[i] = xhi_data_and_inc(data, &index, size);
+
+	if (header->design_name[len - 1] != '\0')
+		return -1;
+
+	header->version = strstr(header->design_name, "Version=") + strlen("Version=");
+
+	/* Read 'b' */
+	tmp = xhi_data_and_inc(data, &index, size);
+	if (tmp != 'b')
+		return -1;	/* INVALID_FILE_HEADER_ERROR */
+
+	/* Get Part Name length */
+	len = xhi_data_and_inc(data, &index, size);
+	len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* allocate space for part name and final null character. */
+	header->part_name = vmalloc(len);
+	if (!header->part_name)
+		return -ENOMEM;
+
+	/* Read in part name */
+	for (i = 0; i < len; i++)
+		header->part_name[i] = xhi_data_and_inc(data, &index, size);
+
+	if (header->part_name[len - 1] != '\0')
+		return -1;
+
+	/* Read 'c' */
+	tmp = xhi_data_and_inc(data, &index, size);
+	if (tmp != 'c')
+		return -1;	/* INVALID_FILE_HEADER_ERROR */
+
+	/* Get date length */
+	len = xhi_data_and_inc(data, &index, size);
+	len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* allocate space for date and final null character. */

drivers/fpga/xrt/lib/xclbin.c line 227

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+	header->date = vmalloc(len);
+	if (!header->date)
+		return -ENOMEM;
+
+	/* Read in date name */
+	for (i = 0; i < len; i++)
+		header->date[i] = xhi_data_and_inc(data, &index, size);
+
+	if (header->date[len - 1] != '\0')
+		return -1;

generally -EINVAL is more meaningful than -1

Quoted 25 lines
drivers/fpga/xrt/lib/xclbin.c new file +25 -0
+
+	/* Read 'd' */
+	tmp = xhi_data_and_inc(data, &index, size);
+	if (tmp != 'd')
+		return -1;	/* INVALID_FILE_HEADER_ERROR  */
+
+	/* Get time length */
+	len = xhi_data_and_inc(data, &index, size);
+	len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+	/* allocate space for time and final null character. */
+	header->time = vmalloc(len);
+	if (!header->time)
+		return -ENOMEM;
+
+	/* Read in time name */
+	for (i = 0; i < len; i++)
+		header->time[i] = xhi_data_and_inc(data, &index, size);
+
+	if (header->time[len - 1] != '\0')
+		return -1;
+
+	/* Read 'e' */
+	tmp = xhi_data_and_inc(data, &index, size);
+	if (tmp != 'e')

drivers/fpga/xrt/lib/xclbin.c line 262

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+		return -1;	/* INVALID_FILE_HEADER_ERROR */
+
+	/* Get byte length of bitstream */
+	header->bitstream_length = xhi_data_and_inc(data, &index, size);
+	header->bitstream_length = (header->bitstream_length << 8) |
+		xhi_data_and_inc(data, &index, size);
+	header->bitstream_length = (header->bitstream_length << 8) |
+		xhi_data_and_inc(data, &index, size);
+	header->bitstream_length = (header->bitstream_length << 8) |
+		xhi_data_and_inc(data, &index, size);

generally a problem

This is confusing, collect the bytes in a temp[] and construct the header->bitstream_length in on statement.

This is a case where xhi_data_and_inc return is not checked and if it failed could blow up later.

drivers/fpga/xrt/lib/xclbin.c line 264

Quoted 2 lines
drivers/fpga/xrt/lib/xclbin.c new file +2 -0
+
+	header->header_length = index;

index is not a good variable name if it going to be stored as a length.

consider changing it to something like current_length.

drivers/fpga/xrt/lib/xclbin.c line 275

Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_parse_bitstream_header);
+
+void xrt_xclbin_free_header(struct hw_icap_bit_header *header)
+{
+	vfree(header->design_name);
+	vfree(header->part_name);
+	vfree(header->date);
+	vfree(header->time);

missing header->version

Quoted 26 lines
drivers/fpga/xrt/lib/xclbin.c new file +26 -0
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_free_header);
+
+struct xrt_clock_desc {
+	char	*clock_ep_name;
+	u32	clock_xclbin_type;
+	char	*clkfreq_ep_name;
+} clock_desc[] = {
+	{
+		.clock_ep_name = XRT_MD_NODE_CLK_KERNEL1,
+		.clock_xclbin_type = CT_DATA,
+		.clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_K1,
+	},
+	{
+		.clock_ep_name = XRT_MD_NODE_CLK_KERNEL2,
+		.clock_xclbin_type = CT_KERNEL,
+		.clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_K2,
+	},
+	{
+		.clock_ep_name = XRT_MD_NODE_CLK_KERNEL3,
+		.clock_xclbin_type = CT_SYSTEM,
+		.clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_HBM,
+	},
+};
+
+const char *xrt_clock_type2epname(enum CLOCK_TYPE type)

drivers/fpga/xrt/lib/xclbin.c line 311

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(clock_desc); i++) {
+		if (clock_desc[i].clock_xclbin_type == type)
+			return clock_desc[i].clock_ep_name;
+	}
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(xrt_clock_type2epname);

What is clock stuff doing in xclbin ?

I think clock needs its own file

Quoted 14 lines
drivers/fpga/xrt/lib/xclbin.c new file +14 -0
+
+static const char *clock_type2clkfreq_name(u32 type)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(clock_desc); i++) {
+		if (clock_desc[i].clock_xclbin_type == type)
+			return clock_desc[i].clkfreq_ep_name;
+	}
+	return NULL;
+}
+
+static int xrt_xclbin_add_clock_metadata(struct device *dev,
+					 const struct axlf *xclbin,

drivers/fpga/xrt/lib/xclbin.c line 335

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+					 char *dtb)
+{
+	int i;
+	u16 freq;
+	struct clock_freq_topology *clock_topo;
+	int rc = xrt_xclbin_get_section(xclbin, CLOCK_FREQ_TOPOLOGY,
+					(void **)&clock_topo, NULL);
+
+	if (rc)
+		return 0;

failing is ok ?

Quoted 9 lines
drivers/fpga/xrt/lib/xclbin.c new file +9 -0
+
+	for (i = 0; i < clock_topo->m_count; i++) {
+		u8 type = clock_topo->m_clock_freq[i].m_type;
+		const char *ep_name = xrt_clock_type2epname(type);
+		const char *counter_name = clock_type2clkfreq_name(type);
+
+		if (!ep_name || !counter_name)
+			continue;
+

drivers/fpga/xrt/lib/xclbin.c line 354

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+		freq = cpu_to_be16(clock_topo->m_clock_freq[i].m_freq_Mhz);
+		rc = xrt_md_set_prop(dev, dtb, ep_name, NULL, XRT_MD_PROP_CLK_FREQ,
+				     &freq, sizeof(freq));
+		if (rc)
+			break;
+
+		rc = xrt_md_set_prop(dev, dtb, ep_name, NULL, XRT_MD_PROP_CLK_CNT,
+				     counter_name, strlen(counter_name) + 1);
+		if (rc)
+			break;

Failing in a loop, why isn't there some cleanup of the *set_prop() calls ?

drivers/fpga/xrt/lib/xclbin.c line 365

Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+	}
+
+	vfree(clock_topo);
+
+	return rc;
+}
+
+int xrt_xclbin_get_metadata(struct device *dev, const struct axlf *xclbin, char **dtb)
+{
+	char *md = NULL, *newmd = NULL;
+	u64 len;

*dtb = NULL;

Quoted 9 lines
drivers/fpga/xrt/lib/xclbin.c new file +9 -0
+	int rc = xrt_xclbin_get_section(xclbin, PARTITION_METADATA,
+					(void **)&md, &len);
+
+	if (rc)
+		goto done;
+
+	/* Sanity check the dtb section. */
+	if (xrt_md_size(dev, md) > len) {
+		rc = -EINVAL;

drivers/fpga/xrt/lib/xclbin.c line 384

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+		goto done;
+	}
+
+	newmd = xrt_md_dup(dev, md);
+	if (!newmd) {
+		rc = -EFAULT;
+		goto done;
+	}
+	/* Convert various needed xclbin sections into dtb. */
+	rc = xrt_xclbin_add_clock_metadata(dev, xclbin, newmd);

newmd is only valid here, but the above error handling jump here. change this to

if (!rc)

  *dtb = newmd

else

   vfree(newmd)

done:

  vfree(md)

  return rc;

Quoted 17 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+
+done:
+	if (rc == 0)
+		*dtb = newmd;
+	else
+		vfree(newmd);
+	vfree(md);
+	return rc;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_get_metadata);
include/uapi/linux/xrt/xclbin.h new file +1 -0
new file mode 100644
index 000000000000..53f140123ef1
@@ -0,0 +1,408 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */

include/uapi/linux/xrt/xclbin.h line 11

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+/*
+ *  Xilinx FPGA compiled binary container format
+ *
+ *  Copyright (C) 2015-2021, Xilinx Inc
+ */
+
+#ifndef _XCLBIN_H_
+#define _XCLBIN_H_
+
+#ifdef _WIN32

WIN32 ?

Only 1 other header has this ifdef

include/uapi/linux/xrt/xclbin.h line 13

Quoted 2 lines
include/uapi/linux/xrt/xclbin.h new file +2 -0
+  #include <cstdint>
+  #include <algorithm>

c++ is being assumed for windows

include/uapi/linux/xrt/xclbin.h line 14

Quoted 1 line
include/uapi/linux/xrt/xclbin.h new file +1 -0
+  #include "windows/uuid.h"

thank you for not including windows.h ;)

Quoted 16 lines
Quoted 6 lines
include/uapi/linux/xrt/xclbin.h new file +6 -0
+#else
+  #if defined(__KERNEL__)
+    #include <linux/types.h>
+    #include <linux/uuid.h>
+    #include <linux/version.h>
+  #elif defined(__cplusplus)

include/uapi/linux/xrt/xclbin.h line 30

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+    #include <cstdlib>
+    #include <cstdint>
+    #include <algorithm>
+    #include <uuid/uuid.h>
+  #else
+    #include <stdlib.h>
+    #include <stdint.h>
+    #include <uuid/uuid.h>
+  #endif
+#endif

Tbh, not a big fan of this ...

Quoted 362 lines

include/uapi/linux/xrt/xclbin.h line 31

Quoted 1 line
include/uapi/linux/xrt/xclbin.h new file +1 -0
+

Review these includes, some could be convenience includes.

ex/ linux/version.h with no obvious use of version macros.

struct axlf_header {

include/uapi/linux/xrt/xclbin.h new file +6 -0
+	uint64_t m_length;		    /* Total size of the xclbin file */
.. snip ..
+	union {
+		char m_next_axlf[16];		/* Name of next xclbin file */
+						/* in the daisy chain */
+		uuid_t uuid;			/* uuid of this xclbin*/
+	};

As mentioned in an earlier patch, if uuid_t is larger than 16 bytes, axlf_header breaks. while it is convenient to have this type here, it would be better this access was handled in another way. Maybe a host specific function.

I also do not see a pragma pack, usually this is set of 1 so the compiler does not shuffle elements, increase size etc.

include/uapi/linux/xrt/xclbin.h line 39

Quoted 8 lines
include/uapi/linux/xrt/xclbin.h new file +8 -0
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * DOC: Container format for Xilinx FPGA images
+ * The container stores bitstreams, metadata and firmware images.
+ * xclbin/xsabin is ELF-like binary container format. It is structured

is an ELF-like file format.  It is a structured

Quoted 20 lines
include/uapi/linux/xrt/xclbin.h new file +20 -0
+ * series of sections. There is a file header followed by several section
+ * headers which is followed by sections. A section header points to an
+ * actual section. There is an optional signature at the end. The
+ * following figure illustrates a typical xclbin:
+ *
+ *     +---------------------+
+ *     |		     |
+ *     |       HEADER	     |
+ *     +---------------------+
+ *     |   SECTION  HEADER   |
+ *     |		     |
+ *     +---------------------+
+ *     |	 ...	     |
+ *     |		     |
+ *     +---------------------+
+ *     |   SECTION  HEADER   |
+ *     |		     |
+ *     +---------------------+
+ *     |       SECTION	     |
+ *     |		     |

include/uapi/linux/xrt/xclbin.h line 69

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+ *     +---------------------+
+ *     |	 ...	     |
+ *     |		     |
+ *     +---------------------+
+ *     |       SECTION	     |
+ *     |		     |
+ *     +---------------------+
+ *     |      SIGNATURE	     |
+ *     |      (OPTIONAL)     |
+ *     +---------------------+

This ascii art is a mixture of tabs and spaces, for someone with tab = 2 spaces, this will look messed up.

convert the tabs to spaces

include/uapi/linux/xrt/xclbin.h line 73

Quoted 4 lines
include/uapi/linux/xrt/xclbin.h new file +4 -0
+ */
+
+enum XCLBIN_MODE {
+	XCLBIN_FLAT,

generally

all enums used in a file format should be initialized.

This likely should be

XCLBIN_FLAT = 0,

Quoted 67 lines
include/uapi/linux/xrt/xclbin.h new file +67 -0
+	XCLBIN_PR,
+	XCLBIN_TANDEM_STAGE2,
+	XCLBIN_TANDEM_STAGE2_WITH_PR,
+	XCLBIN_HW_EMU,
+	XCLBIN_SW_EMU,
+	XCLBIN_MODE_MAX
+};
+
+enum axlf_section_kind {
+	BITSTREAM = 0,
+	CLEARING_BITSTREAM,
+	EMBEDDED_METADATA,
+	FIRMWARE,
+	DEBUG_DATA,
+	SCHED_FIRMWARE,
+	MEM_TOPOLOGY,
+	CONNECTIVITY,
+	IP_LAYOUT,
+	DEBUG_IP_LAYOUT,
+	DESIGN_CHECK_POINT,
+	CLOCK_FREQ_TOPOLOGY,
+	MCS,
+	BMC,
+	BUILD_METADATA,
+	KEYVALUE_METADATA,
+	USER_METADATA,
+	DNA_CERTIFICATE,
+	PDI,
+	BITSTREAM_PARTIAL_PDI,
+	PARTITION_METADATA,
+	EMULATION_DATA,
+	SYSTEM_METADATA,
+	SOFT_KERNEL,
+	ASK_FLASH,
+	AIE_METADATA,
+	ASK_GROUP_TOPOLOGY,
+	ASK_GROUP_CONNECTIVITY
+};
+
+enum MEM_TYPE {
+	MEM_DDR3,
+	MEM_DDR4,
+	MEM_DRAM,
+	MEM_STREAMING,
+	MEM_PREALLOCATED_GLOB,
+	MEM_ARE,
+	MEM_HBM,
+	MEM_BRAM,
+	MEM_URAM,
+	MEM_STREAMING_CONNECTION
+};
+
+enum IP_TYPE {
+	IP_MB = 0,
+	IP_KERNEL,
+	IP_DNASC,
+	IP_DDR4_CONTROLLER,
+	IP_MEM_DDR4,
+	IP_MEM_HBM
+};
+
+struct axlf_section_header {
+	uint32_t m_sectionKind;		    /* Section type */
+	char m_sectionName[16];		    /* Examples: "stage2", "clear1", */
+					    /* "clear2", "ocl1", "ocl2, */
+					    /* "ublaze", "sched" */
+	uint64_t m_sectionOffset;	    /* File offset of section data */

include/uapi/linux/xrt/xclbin.h line 150

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+	uint64_t m_sectionSize;		    /* Size of section data */
+};
+
+struct axlf_header {
+	uint64_t m_length;		    /* Total size of the xclbin file */
+	uint64_t m_timeStamp;		    /* Number of seconds since epoch */
+					    /* when xclbin was created */
+	uint64_t m_featureRomTimeStamp;	    /* TimeSinceEpoch of the featureRom */
+	uint16_t m_versionPatch;	    /* Patch Version */
+	uint8_t m_versionMajor;		    /* Major Version - Version: 2.1.0*/

i did not see the version checked earlier, which one is expected ?

include/uapi/linux/xrt/xclbin.h line 162

Quoted 12 lines
include/uapi/linux/xrt/xclbin.h new file +12 -0
+	uint8_t m_versionMinor;		    /* Minor Version */
+	uint32_t m_mode;		    /* XCLBIN_MODE */
+	union {
+		struct {
+			uint64_t m_platformId;	/* 64 bit platform ID: */
+					/* vendor-device-subvendor-subdev */
+			uint64_t m_featureId;	/* 64 bit feature id */
+		} rom;
+		unsigned char rom_uuid[16];	/* feature ROM UUID for which */
+						/* this xclbin was generated */
+	};
+	unsigned char m_platformVBNV[64];	/* e.g. */

what is VBNV?

Quoted 45 lines
include/uapi/linux/xrt/xclbin.h new file +45 -0
+		/* xilinx:xil-accel-rd-ku115:4ddr-xpr:3.4: null terminated */
+	union {
+		char m_next_axlf[16];		/* Name of next xclbin file */
+						/* in the daisy chain */
+		uuid_t uuid;			/* uuid of this xclbin*/
+	};
+	char m_debug_bin[16];			/* Name of binary with debug */
+						/* information */
+	uint32_t m_numSections;			/* Number of section headers */
+};
+
+struct axlf {
+	char m_magic[8];			/* Should be "xclbin2\0"  */
+	int32_t m_signature_length;		/* Length of the signature. */
+						/* -1 indicates no signature */
+	unsigned char reserved[28];		/* Note: Initialized to 0xFFs */
+
+	unsigned char m_keyBlock[256];		/* Signature for validation */
+						/* of binary */
+	uint64_t m_uniqueId;			/* axlf's uniqueId, use it to */
+						/* skip redownload etc */
+	struct axlf_header m_header;		/* Inline header */
+	struct axlf_section_header m_sections[1];   /* One or more section */
+						    /* headers follow */
+};
+
+/* bitstream information */
+struct xlnx_bitstream {
+	uint8_t m_freq[8];
+	char bits[1];
+};
+
+/****	MEMORY TOPOLOGY SECTION ****/
+struct mem_data {
+	uint8_t m_type; /* enum corresponding to mem_type. */
+	uint8_t m_used; /* if 0 this bank is not present */
+	union {
+		uint64_t m_size; /* if mem_type DDR, then size in KB; */
+		uint64_t route_id; /* if streaming then "route_id" */
+	};
+	union {
+		uint64_t m_base_address;/* if DDR then the base address; */
+		uint64_t flow_id; /* if streaming then "flow id" */
+	};
+	unsigned char m_tag[16]; /* DDR: BANK0,1,2,3, has to be null */

include/uapi/linux/xrt/xclbin.h line 217

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+			/* terminated; if streaming then stream0, 1 etc */
+};
+
+struct mem_topology {
+	int32_t m_count; /* Number of mem_data */
+	struct mem_data m_mem_data[1]; /* Should be sorted on mem_type */
+};
+
+/****	CONNECTIVITY SECTION ****/
+/* Connectivity of each argument of Kernel. It will be in terms of argument

This section does not make sense.

Likely you mean some algorithm kernel, rather than the linux kernel.

Quoted 24 lines
include/uapi/linux/xrt/xclbin.h new file +24 -0
+ * index associated. For associating kernel instances with arguments and
+ * banks, start at the connectivity section. Using the m_ip_layout_index
+ * access the ip_data.m_name. Now we can associate this kernel instance
+ * with its original kernel name and get the connectivity as well. This
+ * enables us to form related groups of kernel instances.
+ */
+
+struct connection {
+	int32_t arg_index; /* From 0 to n, may not be contiguous as scalars */
+			   /* skipped */
+	int32_t m_ip_layout_index; /* index into the ip_layout section. */
+			   /* ip_layout.m_ip_data[index].m_type == IP_KERNEL */
+	int32_t mem_data_index; /* index of the m_mem_data . Flag error is */
+				/* m_used false. */
+};
+
+struct connectivity {
+	int32_t m_count;
+	struct connection m_connection[1];
+};
+
+/****	IP_LAYOUT SECTION ****/
+
+/* IP Kernel */

include/uapi/linux/xrt/xclbin.h line 251

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+#define IP_INT_ENABLE_MASK	  0x0001
+#define IP_INTERRUPT_ID_MASK  0x00FE
+#define IP_INTERRUPT_ID_SHIFT 0x1
+
+enum IP_CONTROL {
+	AP_CTRL_HS = 0,
+	AP_CTRL_CHAIN = 1,
+	AP_CTRL_NONE = 2,
+	AP_CTRL_ME = 3,
+	ACCEL_ADAPTER = 4

assigning beyond the first is not necessary unless there are dups or gaps

Quoted 21 lines
include/uapi/linux/xrt/xclbin.h new file +21 -0
+};
+
+#define IP_CONTROL_MASK	 0xFF00
+#define IP_CONTROL_SHIFT 0x8
+
+/* IPs on AXI lite - their types, names, and base addresses.*/
+struct ip_data {
+	uint32_t m_type; /* map to IP_TYPE enum */
+	union {
+		uint32_t properties; /* Default: 32-bits to indicate ip */
+				     /* specific property. */
+		/* m_type: IP_KERNEL
+		 *	    m_int_enable   : Bit  - 0x0000_0001;
+		 *	    m_interrupt_id : Bits - 0x0000_00FE;
+		 *	    m_ip_control   : Bits = 0x0000_FF00;
+		 */
+		struct {		 /* m_type: IP_MEM_* */
+			uint16_t m_index;
+			uint8_t m_pc_index;
+			uint8_t unused;
+		} indices;

include/uapi/linux/xrt/xclbin.h line 282

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+	};
+	uint64_t m_base_address;
+	uint8_t m_name[64]; /* eg Kernel name corresponding to KERNEL */
+			    /* instance, can embed CU name in future. */
+};
+
+struct ip_layout {
+	int32_t m_count;
+	struct ip_data m_ip_data[1]; /* All the ip_data needs to be sorted */
+				     /* by m_base_address. */

general

doing the bla[1] for c++ ?

Tom

Quoted 60 lines
include/uapi/linux/xrt/xclbin.h new file +60 -0
+};
+
+/*** Debug IP section layout ****/
+enum DEBUG_IP_TYPE {
+	UNDEFINED = 0,
+	LAPC,
+	ILA,
+	AXI_MM_MONITOR,
+	AXI_TRACE_FUNNEL,
+	AXI_MONITOR_FIFO_LITE,
+	AXI_MONITOR_FIFO_FULL,
+	ACCEL_MONITOR,
+	AXI_STREAM_MONITOR,
+	AXI_STREAM_PROTOCOL_CHECKER,
+	TRACE_S2MM,
+	AXI_DMA,
+	TRACE_S2MM_FULL
+};
+
+struct debug_ip_data {
+	uint8_t m_type; /* type of enum DEBUG_IP_TYPE */
+	uint8_t m_index_lowbyte;
+	uint8_t m_properties;
+	uint8_t m_major;
+	uint8_t m_minor;
+	uint8_t m_index_highbyte;
+	uint8_t m_reserved[2];
+	uint64_t m_base_address;
+	char	m_name[128];
+};
+
+struct debug_ip_layout {
+	uint16_t m_count;
+	struct debug_ip_data m_debug_ip_data[1];
+};
+
+/* Supported clock frequency types */
+enum CLOCK_TYPE {
+	CT_UNUSED = 0,			   /* Initialized value */
+	CT_DATA	  = 1,			   /* Data clock */
+	CT_KERNEL = 2,			   /* Kernel clock */
+	CT_SYSTEM = 3			   /* System Clock */
+};
+
+/* Clock Frequency Entry */
+struct clock_freq {
+	uint16_t m_freq_Mhz;		   /* Frequency in MHz */
+	uint8_t m_type;			   /* Clock type (enum CLOCK_TYPE) */
+	uint8_t m_unused[5];		   /* Not used - padding */
+	char m_name[128];		   /* Clock Name */
+};
+
+/* Clock frequency section */
+struct clock_freq_topology {
+	int16_t m_count;		   /* Number of entries */
+	struct clock_freq m_clock_freq[1]; /* Clock array */
+};
+
+/* Supported MCS file types */
+enum MCS_TYPE {
Quoted 8 lines

include/uapi/linux/xrt/xclbin.h line 350

Quoted 8 lines
include/uapi/linux/xrt/xclbin.h new file +8 -0
+	MCS_UNKNOWN = 0,		   /* Initialized value */
+	MCS_PRIMARY = 1,		   /* The primary mcs file data */
+	MCS_SECONDARY = 2,		   /* The secondary mcs file data */
+};
+
+/* One chunk of MCS data */
+struct mcs_chunk {
+	uint8_t m_type;			   /* MCS data type */

just call them type, unused, offset. Drop the m_*

Quoted 50 lines
Quoted 50 lines
include/uapi/linux/xrt/xclbin.h new file +50 -0
+	uint8_t m_unused[7];		   /* padding */
+	uint64_t m_offset;		   /* data offset from the start of */
+					   /* the section */
+	uint64_t m_size;		   /* data size */
+};
+
+/* MCS data section */
+struct mcs {
+	int8_t m_count;			   /* Number of chunks */
+	int8_t m_unused[7];		   /* padding */
+	struct mcs_chunk m_chunk[1];	   /* MCS chunks followed by data */
+};
+
+/* bmc data section */
+struct bmc {
+	uint64_t m_offset;		   /* data offset from the start of */
+					   /* the section */
+	uint64_t m_size;		   /* data size (bytes) */
+	char m_image_name[64];		   /* Name of the image */
+					   /* (e.g., MSP432P401R) */
+	char m_device_name[64];		   /* Device ID	(e.g., VCU1525)	 */
+	char m_version[64];
+	char m_md5value[33];		   /* MD5 Expected Value */
+				/* (e.g., 56027182079c0bd621761b7dab5a27ca)*/
+	char m_padding[7];		   /* Padding */
+};
+
+/* soft kernel data section, used by classic driver */
+struct soft_kernel {
+	/** Prefix Syntax:
+	 *  mpo - member, pointer, offset
+	 *  This variable represents a zero terminated string
+	 *  that is offseted from the beginning of the section.
+	 *  The pointer to access the string is initialized as follows:
+	 *  char * pCharString = (address_of_section) + (mpo value)
+	 */
+	uint32_t mpo_name;	   /* Name of the soft kernel */
+	uint32_t m_image_offset;   /* Image offset */
+	uint32_t m_image_size;	   /* Image size */
+	uint32_t mpo_version;	   /* Version */
+	uint32_t mpo_md5_value;	   /* MD5 checksum */
+	uint32_t mpo_symbol_name;  /* Symbol name */
+	uint32_t m_num_instances;  /* Number of instances */
+	uint8_t padding[36];	   /* Reserved for future use */
+	uint8_t reservedExt[16];   /* Reserved for future extended data */
+};
+
+enum CHECKSUM_TYPE {
+	CST_UNKNOWN = 0,
+	CST_SDBM = 1,
Quoted 9 lines

include/uapi/linux/xrt/xclbin.h line 408

Quoted 8 lines
include/uapi/linux/xrt/xclbin.h new file +8 -0
+	CST_LAST
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

I'll take a closer look, these were just random things I bumped into.

- Moritz

author
#14 lore raw

Re: [PATCH V3 XRT Alveo 03/18] fpga: xrt: xclbin file helper functions

Hi Moritz,

Quoted 166 lines · Moritz Fischer
Quoted 160 lines · Tom Rix
Quoted 1 line · Lizhi Hou

Alveo FPGA firmware and partial reconfigure file are in xclbin format.

This code enumerates and extracts

Quoted 25 lines

Add
code to enumerate and extract sections from xclbin files. xclbin.h is cross platform and used across all platforms and OS

---
  drivers/fpga/xrt/include/xclbin-helper.h |  52 +++
  drivers/fpga/xrt/lib/xclbin.c            | 394 ++++++++++++++++++++++
  include/uapi/linux/xrt/xclbin.h          | 408 +++++++++++++++++++++++
  3 files changed, 854 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/xrt/xclbin.h

drivers/fpga/xrt/include/xclbin-helper.h new file +3 -0
new file mode 100644
index 000000000000..68218efc9d0b
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Header file for Xilinx Runtime (XRT) driver

drivers/fpga/xrt/include/xclbin-helper.h line 13

Quoted 10 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +10 -0
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *    David Zhang <[email protected]>
+ *    Sonal Santan <[email protected]>
+ */
+
+#ifndef _XRT_XCLBIN_H
+#define _XRT_XCLBIN_H

The header guard should match the filename.

drivers/fpga/xrt/include/xclbin-helper.h line 21

Quoted 8 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +8 -0
+
+#include <linux/types.h>
+#include <linux/device.h>
+#include <linux/xrt/xclbin.h>
+
+#define ICAP_XCLBIN_V2     "xclbin2"
+#define DMA_HWICAP_BITFILE_BUFFER_SIZE 1024
+#define MAX_XCLBIN_SIZE (1024 * 1024 * 1024) /* Assuming xclbin <= 1G, always */

#defines should have a prefix, maybe XRT_ or XCLBIN_

drivers/fpga/xrt/include/xclbin-helper.h line 30

Quoted 9 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +9 -0
+
+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 hw_icap_bit_header {

File headers usually have fixed length fields like uint32_t

Is this a structure the real header is converted into ?

drivers/fpga/xrt/include/xclbin-helper.h line 41

Quoted 11 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +11 -0
+   unsigned int header_length;     /* Length of header in 32 bit words */
+   unsigned int bitstream_length;  /* Length of bitstream to read in bytes*/
+   unsigned char *design_name;     /* Design name get from bitstream */
+   unsigned char *part_name;       /* Part name read from bitstream */
+   unsigned char *date;           /* Date read from bitstream header */
+   unsigned char *time;           /* Bitstream creation time */
+   unsigned int magic_length;      /* Length of the magic numbers */
+   unsigned char *version;         /* Version string */
+};
+
+const char *xrt_xclbin_kind_to_string(enum axlf_section_kind kind);

Only add decl's that are using in multiple files.

This is only defined in xclbin.c, why does it need to be in the header ?

drivers/fpga/xrt/include/xclbin-helper.h line 50

Quoted 9 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +9 -0
+int xrt_xclbin_get_section(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(const unsigned char *data,
+                                 unsigned int size,
+                                 struct hw_icap_bit_header *header);
+void xrt_xclbin_free_header(struct hw_icap_bit_header *header);
+const char *xrt_clock_type2epname(enum CLOCK_TYPE type);

CLOCK_TYPE needs a prefix, something like XCLBIN_CLOCK_TYPE

Quoted 13 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +2 -0
+
+#endif /* _XRT_XCLBIN_H */
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
new file mode 100644
index 000000000000..47dc6ca25c1b
@@ -0,0 +1,394 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Xilinx Alveo FPGA Driver XCLBIN parser
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.

drivers/fpga/xrt/lib/xclbin.c line 15

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+ *
+ * Authors: David Zhang <[email protected]>
+ */
+
+#include <asm/errno.h>
+#include <linux/vmalloc.h>
+#include <linux/device.h>
+#include "xclbin-helper.h"
+#include "metadata.h"
+

What is XHI ? Maybe expand this, at the lease should comment

drivers/fpga/xrt/lib/xclbin.c line 27

Quoted 12 lines
drivers/fpga/xrt/lib/xclbin.c new file +12 -0
+/* Used for parsing bitstream header */
+#define XHI_EVEN_MAGIC_BYTE     0x0f
+#define XHI_ODD_MAGIC_BYTE      0xf0
+
+/* Extra mode for IDLE */
+#define XHI_OP_IDLE  -1
+#define XHI_BIT_HEADER_FAILURE -1
+
+/* The imaginary module length register */
+#define XHI_MLR                  15
+
+static inline unsigned char xhi_data_and_inc(const unsigned char *d, int *i, int sz)

could move to the *.h

drivers/fpga/xrt/lib/xclbin.c line 32

Quoted 5 lines
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
+{_
+   unsigned char data;
+
+   if (*i >= sz)
+           return -1;

The return value of this funtion is not always checked, at the least add a dev_err here

Quoted 18 lines
drivers/fpga/xrt/lib/xclbin.c new file +18 -0
+
+   data = d[*i];
+   (*i)++;
+
+   return data;
+}
+
+static const struct axlf_section_header *
+xrt_xclbin_get_section_hdr(const struct axlf *xclbin,
+                      enum axlf_section_kind kind)
+{
+   int i = 0;
+
+   for (i = 0; i < xclbin->m_header.m_numSections; i++) {
+           if (xclbin->m_sections[i].m_sectionKind == kind)
+                   return &xclbin->m_sections[i];
+   }
+

drivers/fpga/xrt/lib/xclbin.c line 60

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+   return NULL;
+}
+
+static int
+xrt_xclbin_check_section_hdr(const struct axlf_section_header *header,
+                        u64 xclbin_len)
+{
+   int ret;
+
+   ret = (header->m_sectionOffset + header->m_sectionSize) > xclbin_len ? -EINVAL : 0;

Tristate is harder to read, consider replacing with if()

Quoted 5 lines

int ret = 0

if ()

ret =

Quoted 8 lines

Why not just:

if (header->m_section_offset + header->m_section_size)

         return -EINVAL;

return 0;

Also please fix the camelCase throughout the entire patchset.

Will fix both.

Quoted 105 lines
Quoted 105 lines
Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +9 -0
+
+   return ret;
+}
+
+static int xrt_xclbin_section_info(const struct axlf *xclbin,
+                              enum axlf_section_kind kind,
+                              u64 *offset, u64 *size)
+{
+   const struct axlf_section_header *mem_header = NULL;

drivers/fpga/xrt/lib/xclbin.c line 79

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+   u64 xclbin_len;
+   int err = 0;
+
+   mem_header = xrt_xclbin_get_section_hdr(xclbin, kind);
+   if (!mem_header)
+           return -EINVAL;
+
+   xclbin_len = xclbin->m_header.m_length;
+   if (xclbin_len > MAX_XCLBIN_SIZE)
+           return -EINVAL;

This check can be added to the function call..

or the sanity checking added to the earier call to *get_section_hdr

There a number of small functions that can be combined.

drivers/fpga/xrt/lib/xclbin.c line 91

Quoted 12 lines
drivers/fpga/xrt/lib/xclbin.c new file +12 -0
+
+   err = xrt_xclbin_check_section_hdr(mem_header, xclbin_len);
+   if (err)
+           return err;
+
+   *offset = mem_header->m_sectionOffset;
+   *size = mem_header->m_sectionSize;
+
+   return 0;
+}
+
+/* caller should free the allocated memory for **data */

must free

This comment also needs to be with the *.h decl

Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+int xrt_xclbin_get_section(const struct axlf *buf,
+                      enum axlf_section_kind kind,
+                      void **data, u64 *len)
+{
+   const struct axlf *xclbin = (const struct axlf *)buf;
+   void *section = NULL;
+   int err = 0;
+   u64 offset = 0;
+   u64 size = 0;
+
+   err = xrt_xclbin_section_info(xclbin, kind, &offset, &size);

drivers/fpga/xrt/lib/xclbin.c line 112

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+   if (err)
+           return err;
+
+   section = vmalloc(size);
+   if (!section)
+           return -ENOMEM;
+
+   memcpy(section, ((const char *)xclbin) + offset, size);
+
+   *data = section;

a general comment

for exported function checking the validity of the inputs in more important.

here you assume **data is valid, really you should check.

drivers/fpga/xrt/lib/xclbin.c line 114

Quoted 2 lines
drivers/fpga/xrt/lib/xclbin.c new file +2 -0
+   if (len)
+           *len = size;

len setting being optional, needs to be in the *.h comment

drivers/fpga/xrt/lib/xclbin.c line 119

Quoted 5 lines
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
+
+   return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_get_section);
+

Instead of allocating new memory and making copies of bits of *data

why not have the points reference data ?

The size operations look like translating big endian data to little endian.

This will break on a big endian host.

Quoted 6 lines
drivers/fpga/xrt/lib/xclbin.c new file +6 -0
+/* parse bitstream header */
+int xrt_xclbin_parse_bitstream_header(const unsigned char *data,
+                                 unsigned int size,
+                                 struct hw_icap_bit_header *header)
+{
+   unsigned int index;

drivers/fpga/xrt/lib/xclbin.c line 135

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+   unsigned int len;
+   unsigned int tmp;
+   unsigned int i;
+
+   memset(header, 0, sizeof(*header));
+   /* Start Index at start of bitstream */
+   index = 0;
+
+   /* Initialize HeaderLength.  If header returned early inidicates
+    * failure.

This side effect should be documented in the *.h comment.

Also the multi line comment is a bit weird, not sure if it is ok

Quoted 12 lines
Quoted 11 lines

drivers/fpga/xrt/lib/xclbin.c line 146

Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+    */
+   header->header_length = XHI_BIT_HEADER_FAILURE;
+
+   /* Get "Magic" length */
+   header->magic_length = xhi_data_and_inc(data, &index, size);
+   header->magic_length = (header->magic_length << 8) | xhi_data_and_inc(data, &index, size);
+
+   /* Read in "magic" */
+   for (i = 0; i < header->magic_length - 1; i++) {
+           tmp = xhi_data_and_inc(data, &index, size);
+           if (i % 2 == 0 && tmp != XHI_EVEN_MAGIC_BYTE)

if !(i % 2) ...

Will change it.

Quoted 318 lines
Quoted 318 lines
Quoted 71 lines
drivers/fpga/xrt/lib/xclbin.c new file +71 -0
+                   return -1;      /* INVALID_FILE_HEADER_ERROR */
+
+           if (i % 2 == 1 && tmp != XHI_ODD_MAGIC_BYTE)
+                   return -1;      /* INVALID_FILE_HEADER_ERROR */
+   }
+
+   /* Read null end of magic data. */
+   tmp = xhi_data_and_inc(data, &index, size);
+
+   /* Read 0x01 (short) */
+   tmp = xhi_data_and_inc(data, &index, size);
+   tmp = (tmp << 8) | xhi_data_and_inc(data, &index, size);
+
+   /* Check the "0x01" half word */
+   if (tmp != 0x01)
+           return -1;      /* INVALID_FILE_HEADER_ERROR */
+
+   /* Read 'a' */
+   tmp = xhi_data_and_inc(data, &index, size);
+   if (tmp != 'a')
+           return -1;      /* INVALID_FILE_HEADER_ERROR    */
+
+   /* Get Design Name length */
+   len = xhi_data_and_inc(data, &index, size);
+   len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+   /* allocate space for design name and final null character. */
+   header->design_name = vmalloc(len);
+   if (!header->design_name)
+           return -ENOMEM;
+
+   /* Read in Design Name */
+   for (i = 0; i < len; i++)
+           header->design_name[i] = xhi_data_and_inc(data, &index, size);
+
+   if (header->design_name[len - 1] != '\0')
+           return -1;
+
+   header->version = strstr(header->design_name, "Version=") + strlen("Version=");
+
+   /* Read 'b' */
+   tmp = xhi_data_and_inc(data, &index, size);
+   if (tmp != 'b')
+           return -1;      /* INVALID_FILE_HEADER_ERROR */
+
+   /* Get Part Name length */
+   len = xhi_data_and_inc(data, &index, size);
+   len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+   /* allocate space for part name and final null character. */
+   header->part_name = vmalloc(len);
+   if (!header->part_name)
+           return -ENOMEM;
+
+   /* Read in part name */
+   for (i = 0; i < len; i++)
+           header->part_name[i] = xhi_data_and_inc(data, &index, size);
+
+   if (header->part_name[len - 1] != '\0')
+           return -1;
+
+   /* Read 'c' */
+   tmp = xhi_data_and_inc(data, &index, size);
+   if (tmp != 'c')
+           return -1;      /* INVALID_FILE_HEADER_ERROR */
+
+   /* Get date length */
+   len = xhi_data_and_inc(data, &index, size);
+   len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+   /* allocate space for date and final null character. */

drivers/fpga/xrt/lib/xclbin.c line 227

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+   header->date = vmalloc(len);
+   if (!header->date)
+           return -ENOMEM;
+
+   /* Read in date name */
+   for (i = 0; i < len; i++)
+           header->date[i] = xhi_data_and_inc(data, &index, size);
+
+   if (header->date[len - 1] != '\0')
+           return -1;

generally -EINVAL is more meaningful than -1

Quoted 25 lines
drivers/fpga/xrt/lib/xclbin.c new file +25 -0
+
+   /* Read 'd' */
+   tmp = xhi_data_and_inc(data, &index, size);
+   if (tmp != 'd')
+           return -1;      /* INVALID_FILE_HEADER_ERROR  */
+
+   /* Get time length */
+   len = xhi_data_and_inc(data, &index, size);
+   len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+   /* allocate space for time and final null character. */
+   header->time = vmalloc(len);
+   if (!header->time)
+           return -ENOMEM;
+
+   /* Read in time name */
+   for (i = 0; i < len; i++)
+           header->time[i] = xhi_data_and_inc(data, &index, size);
+
+   if (header->time[len - 1] != '\0')
+           return -1;
+
+   /* Read 'e' */
+   tmp = xhi_data_and_inc(data, &index, size);
+   if (tmp != 'e')

drivers/fpga/xrt/lib/xclbin.c line 262

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+           return -1;      /* INVALID_FILE_HEADER_ERROR */
+
+   /* Get byte length of bitstream */
+   header->bitstream_length = xhi_data_and_inc(data, &index, size);
+   header->bitstream_length = (header->bitstream_length << 8) |
+           xhi_data_and_inc(data, &index, size);
+   header->bitstream_length = (header->bitstream_length << 8) |
+           xhi_data_and_inc(data, &index, size);
+   header->bitstream_length = (header->bitstream_length << 8) |
+           xhi_data_and_inc(data, &index, size);

generally a problem

This is confusing, collect the bytes in a temp[] and construct the header->bitstream_length in on statement.

This is a case where xhi_data_and_inc return is not checked and if it failed could blow up later.

drivers/fpga/xrt/lib/xclbin.c line 264

Quoted 2 lines
drivers/fpga/xrt/lib/xclbin.c new file +2 -0
+
+   header->header_length = index;

index is not a good variable name if it going to be stored as a length.

consider changing it to something like current_length.

drivers/fpga/xrt/lib/xclbin.c line 275

Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+
+   return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_parse_bitstream_header);
+
+void xrt_xclbin_free_header(struct hw_icap_bit_header *header)
+{
+   vfree(header->design_name);
+   vfree(header->part_name);
+   vfree(header->date);
+   vfree(header->time);

missing header->version

Quoted 26 lines
drivers/fpga/xrt/lib/xclbin.c new file +26 -0
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_free_header);
+
+struct xrt_clock_desc {
+   char    *clock_ep_name;
+   u32     clock_xclbin_type;
+   char    *clkfreq_ep_name;
+} clock_desc[] = {
+   {
+           .clock_ep_name = XRT_MD_NODE_CLK_KERNEL1,
+           .clock_xclbin_type = CT_DATA,
+           .clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_K1,
+   },
+   {
+           .clock_ep_name = XRT_MD_NODE_CLK_KERNEL2,
+           .clock_xclbin_type = CT_KERNEL,
+           .clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_K2,
+   },
+   {
+           .clock_ep_name = XRT_MD_NODE_CLK_KERNEL3,
+           .clock_xclbin_type = CT_SYSTEM,
+           .clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_HBM,
+   },
+};
+
+const char *xrt_clock_type2epname(enum CLOCK_TYPE type)

drivers/fpga/xrt/lib/xclbin.c line 311

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+{
+   int i;
+
+   for (i = 0; i < ARRAY_SIZE(clock_desc); i++) {
+           if (clock_desc[i].clock_xclbin_type == type)
+                   return clock_desc[i].clock_ep_name;
+   }
+   return NULL;
+}
+EXPORT_SYMBOL_GPL(xrt_clock_type2epname);

What is clock stuff doing in xclbin ?

I think clock needs its own file

Quoted 14 lines
drivers/fpga/xrt/lib/xclbin.c new file +14 -0
+
+static const char *clock_type2clkfreq_name(u32 type)
+{
+   int i;
+
+   for (i = 0; i < ARRAY_SIZE(clock_desc); i++) {
+           if (clock_desc[i].clock_xclbin_type == type)
+                   return clock_desc[i].clkfreq_ep_name;
+   }
+   return NULL;
+}
+
+static int xrt_xclbin_add_clock_metadata(struct device *dev,
+                                    const struct axlf *xclbin,

drivers/fpga/xrt/lib/xclbin.c line 335

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+                                    char *dtb)
+{
+   int i;
+   u16 freq;
+   struct clock_freq_topology *clock_topo;
+   int rc = xrt_xclbin_get_section(xclbin, CLOCK_FREQ_TOPOLOGY,
+                                   (void **)&clock_topo, NULL);
+
+   if (rc)
+           return 0;

failing is ok ?

Quoted 9 lines
drivers/fpga/xrt/lib/xclbin.c new file +9 -0
+
+   for (i = 0; i < clock_topo->m_count; i++) {
+           u8 type = clock_topo->m_clock_freq[i].m_type;
+           const char *ep_name = xrt_clock_type2epname(type);
+           const char *counter_name = clock_type2clkfreq_name(type);
+
+           if (!ep_name || !counter_name)
+                   continue;
+

drivers/fpga/xrt/lib/xclbin.c line 354

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+           freq = cpu_to_be16(clock_topo->m_clock_freq[i].m_freq_Mhz);
+           rc = xrt_md_set_prop(dev, dtb, ep_name, NULL, XRT_MD_PROP_CLK_FREQ,
+                                &freq, sizeof(freq));
+           if (rc)
+                   break;
+
+           rc = xrt_md_set_prop(dev, dtb, ep_name, NULL, XRT_MD_PROP_CLK_CNT,
+                                counter_name, strlen(counter_name) + 1);
+           if (rc)
+                   break;

Failing in a loop, why isn't there some cleanup of the *set_prop() calls ?

drivers/fpga/xrt/lib/xclbin.c line 365

Quoted 11 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+   }
+
+   vfree(clock_topo);
+
+   return rc;
+}
+
+int xrt_xclbin_get_metadata(struct device *dev, const struct axlf *xclbin, char **dtb)
+{
+   char *md = NULL, *newmd = NULL;
+   u64 len;

*dtb = NULL;

Quoted 9 lines
drivers/fpga/xrt/lib/xclbin.c new file +9 -0
+   int rc = xrt_xclbin_get_section(xclbin, PARTITION_METADATA,
+                                   (void **)&md, &len);
+
+   if (rc)
+           goto done;
+
+   /* Sanity check the dtb section. */
+   if (xrt_md_size(dev, md) > len) {
+           rc = -EINVAL;

drivers/fpga/xrt/lib/xclbin.c line 384

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+           goto done;
+   }
+
+   newmd = xrt_md_dup(dev, md);
+   if (!newmd) {
+           rc = -EFAULT;
+           goto done;
+   }
+   /* Convert various needed xclbin sections into dtb. */
+   rc = xrt_xclbin_add_clock_metadata(dev, xclbin, newmd);

newmd is only valid here, but the above error handling jump here. change this to

if (!rc)

*dtb = newmd

else

vfree(newmd)

done:

   vfree(md)

   return rc;
Quoted 17 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+
+done:
+   if (rc == 0)
+           *dtb = newmd;
+   else
+           vfree(newmd);
+   vfree(md);
+   return rc;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_get_metadata);
include/uapi/linux/xrt/xclbin.h new file +1 -0
new file mode 100644
index 000000000000..53f140123ef1
@@ -0,0 +1,408 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */

include/uapi/linux/xrt/xclbin.h line 11

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+/*
+ *  Xilinx FPGA compiled binary container format
+ *
+ *  Copyright (C) 2015-2021, Xilinx Inc
+ */
+
+#ifndef _XCLBIN_H_
+#define _XCLBIN_H_
+
+#ifdef _WIN32

WIN32 ?

Only 1 other header has this ifdef

include/uapi/linux/xrt/xclbin.h line 13

Quoted 2 lines
include/uapi/linux/xrt/xclbin.h new file +2 -0
+  #include <cstdint>
+  #include <algorithm>

c++ is being assumed for windows

include/uapi/linux/xrt/xclbin.h line 14

Quoted 1 line
include/uapi/linux/xrt/xclbin.h new file +1 -0
+  #include "windows/uuid.h"

thank you for not including windows.h ;)

Quoted 9 lines
include/uapi/linux/xrt/xclbin.h new file +9 -0
+#else
+  #if defined(__KERNEL__)
+    #include <linux/types.h>
+    #include <linux/uuid.h>
+    #include <linux/version.h>
+  #elif defined(__cplusplus)
+    #include <cstdlib>
+    #include <cstdint>
+    #include <algorithm>
Quoted 8 lines
Quoted 7 lines

include/uapi/linux/xrt/xclbin.h line 30

Quoted 7 lines
include/uapi/linux/xrt/xclbin.h new file +7 -0
+    #include <uuid/uuid.h>
+  #else
+    #include <stdlib.h>
+    #include <stdint.h>
+    #include <uuid/uuid.h>
+  #endif
+#endif

Tbh, not a big fan of this ...

We will remove WIN32 and keep __cplusplus

Quoted 358 lines
Quoted 358 lines

include/uapi/linux/xrt/xclbin.h line 31

Quoted 1 line
include/uapi/linux/xrt/xclbin.h new file +1 -0
+

Review these includes, some could be convenience includes.

ex/ linux/version.h with no obvious use of version macros.

struct axlf_header {

include/uapi/linux/xrt/xclbin.h new file +6 -0
+     uint64_t m_length;                  /* Total size of the xclbin file */
.. snip ..
+     union {
+             char m_next_axlf[16];           /* Name of next xclbin file */
+                                             /* in the daisy chain */
+             uuid_t uuid;                    /* uuid of this xclbin*/
+     };

As mentioned in an earlier patch, if uuid_t is larger than 16 bytes, axlf_header breaks. while it is convenient to have this type here, it would be better this access was handled in another way. Maybe a host specific function.

I also do not see a pragma pack, usually this is set of 1 so the compiler does not shuffle elements, increase size etc.

include/uapi/linux/xrt/xclbin.h line 39

Quoted 8 lines
include/uapi/linux/xrt/xclbin.h new file +8 -0
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * DOC: Container format for Xilinx FPGA images
+ * The container stores bitstreams, metadata and firmware images.
+ * xclbin/xsabin is ELF-like binary container format. It is structured

is an ELF-like file format. It is a structured

Quoted 20 lines
include/uapi/linux/xrt/xclbin.h new file +20 -0
+ * series of sections. There is a file header followed by several section
+ * headers which is followed by sections. A section header points to an
+ * actual section. There is an optional signature at the end. The
+ * following figure illustrates a typical xclbin:
+ *
+ *     +---------------------+
+ *     |                |
+ *     |       HEADER           |
+ *     +---------------------+
+ *     |   SECTION  HEADER   |
+ *     |                |
+ *     +---------------------+
+ *     |    ...         |
+ *     |                |
+ *     +---------------------+
+ *     |   SECTION  HEADER   |
+ *     |                |
+ *     +---------------------+
+ *     |       SECTION          |
+ *     |                |

include/uapi/linux/xrt/xclbin.h line 69

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+ *     +---------------------+
+ *     |    ...         |
+ *     |                |
+ *     +---------------------+
+ *     |       SECTION          |
+ *     |                |
+ *     +---------------------+
+ *     |      SIGNATURE         |
+ *     |      (OPTIONAL)     |
+ *     +---------------------+

This ascii art is a mixture of tabs and spaces, for someone with tab = 2 spaces, this will look messed up.

convert the tabs to spaces

include/uapi/linux/xrt/xclbin.h line 73

Quoted 4 lines
include/uapi/linux/xrt/xclbin.h new file +4 -0
+ */
+
+enum XCLBIN_MODE {
+   XCLBIN_FLAT,

generally

all enums used in a file format should be initialized.

This likely should be

XCLBIN_FLAT = 0,

Quoted 67 lines
include/uapi/linux/xrt/xclbin.h new file +67 -0
+   XCLBIN_PR,
+   XCLBIN_TANDEM_STAGE2,
+   XCLBIN_TANDEM_STAGE2_WITH_PR,
+   XCLBIN_HW_EMU,
+   XCLBIN_SW_EMU,
+   XCLBIN_MODE_MAX
+};
+
+enum axlf_section_kind {
+   BITSTREAM = 0,
+   CLEARING_BITSTREAM,
+   EMBEDDED_METADATA,
+   FIRMWARE,
+   DEBUG_DATA,
+   SCHED_FIRMWARE,
+   MEM_TOPOLOGY,
+   CONNECTIVITY,
+   IP_LAYOUT,
+   DEBUG_IP_LAYOUT,
+   DESIGN_CHECK_POINT,
+   CLOCK_FREQ_TOPOLOGY,
+   MCS,
+   BMC,
+   BUILD_METADATA,
+   KEYVALUE_METADATA,
+   USER_METADATA,
+   DNA_CERTIFICATE,
+   PDI,
+   BITSTREAM_PARTIAL_PDI,
+   PARTITION_METADATA,
+   EMULATION_DATA,
+   SYSTEM_METADATA,
+   SOFT_KERNEL,
+   ASK_FLASH,
+   AIE_METADATA,
+   ASK_GROUP_TOPOLOGY,
+   ASK_GROUP_CONNECTIVITY
+};
+
+enum MEM_TYPE {
+   MEM_DDR3,
+   MEM_DDR4,
+   MEM_DRAM,
+   MEM_STREAMING,
+   MEM_PREALLOCATED_GLOB,
+   MEM_ARE,
+   MEM_HBM,
+   MEM_BRAM,
+   MEM_URAM,
+   MEM_STREAMING_CONNECTION
+};
+
+enum IP_TYPE {
+   IP_MB = 0,
+   IP_KERNEL,
+   IP_DNASC,
+   IP_DDR4_CONTROLLER,
+   IP_MEM_DDR4,
+   IP_MEM_HBM
+};
+
+struct axlf_section_header {
+   uint32_t m_sectionKind;             /* Section type */
+   char m_sectionName[16];             /* Examples: "stage2", "clear1", */
+                                       /* "clear2", "ocl1", "ocl2, */
+                                       /* "ublaze", "sched" */
+   uint64_t m_sectionOffset;           /* File offset of section data */

include/uapi/linux/xrt/xclbin.h line 150

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+   uint64_t m_sectionSize;             /* Size of section data */
+};
+
+struct axlf_header {
+   uint64_t m_length;                  /* Total size of the xclbin file */
+   uint64_t m_timeStamp;               /* Number of seconds since epoch */
+                                       /* when xclbin was created */
+   uint64_t m_featureRomTimeStamp;     /* TimeSinceEpoch of the featureRom */
+   uint16_t m_versionPatch;            /* Patch Version */
+   uint8_t m_versionMajor;             /* Major Version - Version: 2.1.0*/

i did not see the version checked earlier, which one is expected ?

include/uapi/linux/xrt/xclbin.h line 162

Quoted 12 lines
include/uapi/linux/xrt/xclbin.h new file +12 -0
+   uint8_t m_versionMinor;             /* Minor Version */
+   uint32_t m_mode;                    /* XCLBIN_MODE */
+   union {
+           struct {
+                   uint64_t m_platformId;  /* 64 bit platform ID: */
+                                   /* vendor-device-subvendor-subdev */
+                   uint64_t m_featureId;   /* 64 bit feature id */
+           } rom;
+           unsigned char rom_uuid[16];     /* feature ROM UUID for which */
+                                           /* this xclbin was generated */
+   };
+   unsigned char m_platformVBNV[64];       /* e.g. */

what is VBNV?

Quoted 45 lines
include/uapi/linux/xrt/xclbin.h new file +45 -0
+           /* xilinx:xil-accel-rd-ku115:4ddr-xpr:3.4: null terminated */
+   union {
+           char m_next_axlf[16];           /* Name of next xclbin file */
+                                           /* in the daisy chain */
+           uuid_t uuid;                    /* uuid of this xclbin*/
+   };
+   char m_debug_bin[16];                   /* Name of binary with debug */
+                                           /* information */
+   uint32_t m_numSections;                 /* Number of section headers */
+};
+
+struct axlf {
+   char m_magic[8];                        /* Should be "xclbin2\0"  */
+   int32_t m_signature_length;             /* Length of the signature. */
+                                           /* -1 indicates no signature */
+   unsigned char reserved[28];             /* Note: Initialized to 0xFFs */
+
+   unsigned char m_keyBlock[256];          /* Signature for validation */
+                                           /* of binary */
+   uint64_t m_uniqueId;                    /* axlf's uniqueId, use it to */
+                                           /* skip redownload etc */
+   struct axlf_header m_header;            /* Inline header */
+   struct axlf_section_header m_sections[1];   /* One or more section */
+                                               /* headers follow */
+};
+
+/* bitstream information */
+struct xlnx_bitstream {
+   uint8_t m_freq[8];
+   char bits[1];
+};
+
+/****      MEMORY TOPOLOGY SECTION ****/
+struct mem_data {
+   uint8_t m_type; /* enum corresponding to mem_type. */
+   uint8_t m_used; /* if 0 this bank is not present */
+   union {
+           uint64_t m_size; /* if mem_type DDR, then size in KB; */
+           uint64_t route_id; /* if streaming then "route_id" */
+   };
+   union {
+           uint64_t m_base_address;/* if DDR then the base address; */
+           uint64_t flow_id; /* if streaming then "flow id" */
+   };
+   unsigned char m_tag[16]; /* DDR: BANK0,1,2,3, has to be null */

include/uapi/linux/xrt/xclbin.h line 217

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+                   /* terminated; if streaming then stream0, 1 etc */
+};
+
+struct mem_topology {
+   int32_t m_count; /* Number of mem_data */
+   struct mem_data m_mem_data[1]; /* Should be sorted on mem_type */
+};
+
+/****      CONNECTIVITY SECTION ****/
+/* Connectivity of each argument of Kernel. It will be in terms of argument

This section does not make sense.

Likely you mean some algorithm kernel, rather than the linux kernel.

Quoted 24 lines
include/uapi/linux/xrt/xclbin.h new file +24 -0
+ * index associated. For associating kernel instances with arguments and
+ * banks, start at the connectivity section. Using the m_ip_layout_index
+ * access the ip_data.m_name. Now we can associate this kernel instance
+ * with its original kernel name and get the connectivity as well. This
+ * enables us to form related groups of kernel instances.
+ */
+
+struct connection {
+   int32_t arg_index; /* From 0 to n, may not be contiguous as scalars */
+                      /* skipped */
+   int32_t m_ip_layout_index; /* index into the ip_layout section. */
+                      /* ip_layout.m_ip_data[index].m_type == IP_KERNEL */
+   int32_t mem_data_index; /* index of the m_mem_data . Flag error is */
+                           /* m_used false. */
+};
+
+struct connectivity {
+   int32_t m_count;
+   struct connection m_connection[1];
+};
+
+/****      IP_LAYOUT SECTION ****/
+
+/* IP Kernel */

include/uapi/linux/xrt/xclbin.h line 251

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+#define IP_INT_ENABLE_MASK   0x0001
+#define IP_INTERRUPT_ID_MASK  0x00FE
+#define IP_INTERRUPT_ID_SHIFT 0x1
+
+enum IP_CONTROL {
+   AP_CTRL_HS = 0,
+   AP_CTRL_CHAIN = 1,
+   AP_CTRL_NONE = 2,
+   AP_CTRL_ME = 3,
+   ACCEL_ADAPTER = 4

assigning beyond the first is not necessary unless there are dups or gaps

Quoted 21 lines
include/uapi/linux/xrt/xclbin.h new file +21 -0
+};
+
+#define IP_CONTROL_MASK     0xFF00
+#define IP_CONTROL_SHIFT 0x8
+
+/* IPs on AXI lite - their types, names, and base addresses.*/
+struct ip_data {
+   uint32_t m_type; /* map to IP_TYPE enum */
+   union {
+           uint32_t properties; /* Default: 32-bits to indicate ip */
+                                /* specific property. */
+           /* m_type: IP_KERNEL
+            *          m_int_enable   : Bit  - 0x0000_0001;
+            *          m_interrupt_id : Bits - 0x0000_00FE;
+            *          m_ip_control   : Bits = 0x0000_FF00;
+            */
+           struct {                 /* m_type: IP_MEM_* */
+                   uint16_t m_index;
+                   uint8_t m_pc_index;
+                   uint8_t unused;
+           } indices;

include/uapi/linux/xrt/xclbin.h line 282

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+   };
+   uint64_t m_base_address;
+   uint8_t m_name[64]; /* eg Kernel name corresponding to KERNEL */
+                       /* instance, can embed CU name in future. */
+};
+
+struct ip_layout {
+   int32_t m_count;
+   struct ip_data m_ip_data[1]; /* All the ip_data needs to be sorted */
+                                /* by m_base_address. */

general

doing the bla[1] for c++ ?

Tom

Quoted 61 lines
include/uapi/linux/xrt/xclbin.h new file +61 -0
+};
+
+/*** Debug IP section layout ****/
+enum DEBUG_IP_TYPE {
+   UNDEFINED = 0,
+   LAPC,
+   ILA,
+   AXI_MM_MONITOR,
+   AXI_TRACE_FUNNEL,
+   AXI_MONITOR_FIFO_LITE,
+   AXI_MONITOR_FIFO_FULL,
+   ACCEL_MONITOR,
+   AXI_STREAM_MONITOR,
+   AXI_STREAM_PROTOCOL_CHECKER,
+   TRACE_S2MM,
+   AXI_DMA,
+   TRACE_S2MM_FULL
+};
+
+struct debug_ip_data {
+   uint8_t m_type; /* type of enum DEBUG_IP_TYPE */
+   uint8_t m_index_lowbyte;
+   uint8_t m_properties;
+   uint8_t m_major;
+   uint8_t m_minor;
+   uint8_t m_index_highbyte;
+   uint8_t m_reserved[2];
+   uint64_t m_base_address;
+   char    m_name[128];
+};
+
+struct debug_ip_layout {
+   uint16_t m_count;
+   struct debug_ip_data m_debug_ip_data[1];
+};
+
+/* Supported clock frequency types */
+enum CLOCK_TYPE {
+   CT_UNUSED = 0,                     /* Initialized value */
+   CT_DATA   = 1,                     /* Data clock */
+   CT_KERNEL = 2,                     /* Kernel clock */
+   CT_SYSTEM = 3                      /* System Clock */
+};
+
+/* Clock Frequency Entry */
+struct clock_freq {
+   uint16_t m_freq_Mhz;               /* Frequency in MHz */
+   uint8_t m_type;                    /* Clock type (enum CLOCK_TYPE) */
+   uint8_t m_unused[5];               /* Not used - padding */
+   char m_name[128];                  /* Clock Name */
+};
+
+/* Clock frequency section */
+struct clock_freq_topology {
+   int16_t m_count;                   /* Number of entries */
+   struct clock_freq m_clock_freq[1]; /* Clock array */
+};
+
+/* Supported MCS file types */
+enum MCS_TYPE {
+   MCS_UNKNOWN = 0,                   /* Initialized value */
Quoted 8 lines
Quoted 7 lines

include/uapi/linux/xrt/xclbin.h line 350

Quoted 7 lines
include/uapi/linux/xrt/xclbin.h new file +7 -0
+   MCS_PRIMARY = 1,                   /* The primary mcs file data */
+   MCS_SECONDARY = 2,                 /* The secondary mcs file data */
+};
+
+/* One chunk of MCS data */
+struct mcs_chunk {
+   uint8_t m_type;                    /* MCS data type */

just call them type, unused, offset. Drop the m_*

Will remove all 'm_'

Thanks,
Lizhi

Quoted 61 lines
Quoted 50 lines
Quoted 50 lines
include/uapi/linux/xrt/xclbin.h new file +50 -0
+   uint8_t m_unused[7];               /* padding */
+   uint64_t m_offset;                 /* data offset from the start of */
+                                      /* the section */
+   uint64_t m_size;                   /* data size */
+};
+
+/* MCS data section */
+struct mcs {
+   int8_t m_count;                    /* Number of chunks */
+   int8_t m_unused[7];                /* padding */
+   struct mcs_chunk m_chunk[1];       /* MCS chunks followed by data */
+};
+
+/* bmc data section */
+struct bmc {
+   uint64_t m_offset;                 /* data offset from the start of */
+                                      /* the section */
+   uint64_t m_size;                   /* data size (bytes) */
+   char m_image_name[64];             /* Name of the image */
+                                      /* (e.g., MSP432P401R) */
+   char m_device_name[64];            /* Device ID (e.g., VCU1525)  */
+   char m_version[64];
+   char m_md5value[33];               /* MD5 Expected Value */
+                           /* (e.g., 56027182079c0bd621761b7dab5a27ca)*/
+   char m_padding[7];                 /* Padding */
+};
+
+/* soft kernel data section, used by classic driver */
+struct soft_kernel {
+   /** Prefix Syntax:
+    *  mpo - member, pointer, offset
+    *  This variable represents a zero terminated string
+    *  that is offseted from the beginning of the section.
+    *  The pointer to access the string is initialized as follows:
+    *  char * pCharString = (address_of_section) + (mpo value)
+    */
+   uint32_t mpo_name;         /* Name of the soft kernel */
+   uint32_t m_image_offset;   /* Image offset */
+   uint32_t m_image_size;     /* Image size */
+   uint32_t mpo_version;      /* Version */
+   uint32_t mpo_md5_value;    /* MD5 checksum */
+   uint32_t mpo_symbol_name;  /* Symbol name */
+   uint32_t m_num_instances;  /* Number of instances */
+   uint8_t padding[36];       /* Reserved for future use */
+   uint8_t reservedExt[16];   /* Reserved for future extended data */
+};
+
+enum CHECKSUM_TYPE {
+   CST_UNKNOWN = 0,
+   CST_SDBM = 1,
Quoted 8 lines

include/uapi/linux/xrt/xclbin.h line 408

Quoted 8 lines
include/uapi/linux/xrt/xclbin.h new file +8 -0
+   CST_LAST
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

I'll take a closer look, these were just random things I bumped into.

- Moritz

author
#15 lore raw

Re: [PATCH V3 XRT Alveo 03/18] fpga: xrt: xclbin file helper functions

In reply to Tom Rix

Hi Tom,

Quoted 3 lines · Tom Rix
Quoted 1 line · Lizhi Hou

Alveo FPGA firmware and partial reconfigure file are in xclbin format.

This code enumerates and extracts

Will change this to

Alveo 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.

Quoted 28 lines
Quoted 28 lines

Add
code to enumerate and extract sections from xclbin files. xclbin.h is cross platform and used across all platforms and OS

---
  drivers/fpga/xrt/include/xclbin-helper.h |  52 +++
  drivers/fpga/xrt/lib/xclbin.c            | 394 ++++++++++++++++++++++
  include/uapi/linux/xrt/xclbin.h          | 408 +++++++++++++++++++++++
  3 files changed, 854 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/xrt/xclbin.h

drivers/fpga/xrt/include/xclbin-helper.h new file +6 -0
new file mode 100644
index 000000000000..68218efc9d0b
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Header file for Xilinx Runtime (XRT) driver
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
Quoted 8 lines

drivers/fpga/xrt/include/xclbin-helper.h line 13

Quoted 7 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +7 -0
+ * Authors:
+ *    David Zhang <[email protected]>
+ *    Sonal Santan <[email protected]>
+ */
+
+#ifndef _XRT_XCLBIN_H
+#define _XRT_XCLBIN_H

The header guard should match the filename.

will fix this.

Quoted 10 lines

drivers/fpga/xrt/include/xclbin-helper.h line 21

Quoted 9 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +8 -0
+
+#include <linux/types.h>
+#include <linux/device.h>
+#include <linux/xrt/xclbin.h>
+
+#define ICAP_XCLBIN_V2       "xclbin2"
+#define DMA_HWICAP_BITFILE_BUFFER_SIZE 1024
+#define MAX_XCLBIN_SIZE (1024 * 1024 * 1024) /* Assuming xclbin <= 1G, always */

#defines should have a prefix, maybe XRT_ or XCLBIN_

Will add prefix XCLBIN_

Quoted 11 lines

drivers/fpga/xrt/include/xclbin-helper.h line 30

Quoted 9 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +9 -0
+
+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 hw_icap_bit_header {

File headers usually have fixed length fields like uint32_t

Quoted 1 line

Is this a structure the real header is converted into ?

This is not real header. This structure saves the information extracted from bitstream header.

Quoted 14 lines

drivers/fpga/xrt/include/xclbin-helper.h line 41

Quoted 12 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +11 -0
+     unsigned int header_length;     /* Length of header in 32 bit words */
+     unsigned int bitstream_length;  /* Length of bitstream to read in bytes*/
+     unsigned char *design_name;     /* Design name get from bitstream */
+     unsigned char *part_name;       /* Part name read from bitstream */
+     unsigned char *date;           /* Date read from bitstream header */
+     unsigned char *time;           /* Bitstream creation time */
+     unsigned int magic_length;      /* Length of the magic numbers */
+     unsigned char *version;         /* Version string */
+};
+
+const char *xrt_xclbin_kind_to_string(enum axlf_section_kind kind);

Only add decl's that are using in multiple files.

Quoted 1 line

This is only defined in xclbin.c, why does it need to be in the header ?

Will remove this.

Quoted 11 lines

drivers/fpga/xrt/include/xclbin-helper.h line 50

Quoted 10 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +9 -0
+int xrt_xclbin_get_section(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(const unsigned char *data,
+                                   unsigned int size,
+                                   struct hw_icap_bit_header *header);
+void xrt_xclbin_free_header(struct hw_icap_bit_header *header);
+const char *xrt_clock_type2epname(enum CLOCK_TYPE type);

CLOCK_TYPE needs a prefix, something like XCLBIN_CLOCK_TYPE

Will change to XCLBIN_CLOCK_TYPE.

Quoted 16 lines
Quoted 16 lines
drivers/fpga/xrt/include/xclbin-helper.h new file +2 -0
+
+#endif /* _XRT_XCLBIN_H */
drivers/fpga/xrt/lib/xclbin.c new file +8 -0
new file mode 100644
index 000000000000..47dc6ca25c1b
@@ -0,0 +1,394 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Xilinx Alveo FPGA Driver XCLBIN parser
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors: David Zhang <[email protected]>
+ */
Quoted 8 lines

drivers/fpga/xrt/lib/xclbin.c line 15

Quoted 7 lines
drivers/fpga/xrt/lib/xclbin.c new file +7 -0
+
+#include <asm/errno.h>
+#include <linux/vmalloc.h>
+#include <linux/device.h>
+#include "xclbin-helper.h"
+#include "metadata.h"
+

What is XHI ? Maybe expand this, at the lease should comment

Will use BITSTREAM_ instead.

Quoted 5 lines
Quoted 5 lines
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
+/* Used for parsing bitstream header */
+#define XHI_EVEN_MAGIC_BYTE     0x0f
+#define XHI_ODD_MAGIC_BYTE      0xf0
+
+/* Extra mode for IDLE */
Quoted 8 lines

drivers/fpga/xrt/lib/xclbin.c line 27

Quoted 7 lines
drivers/fpga/xrt/lib/xclbin.c new file +7 -0
+#define XHI_OP_IDLE  -1
+#define XHI_BIT_HEADER_FAILURE -1
+
+/* The imaginary module length register */
+#define XHI_MLR                  15
+
+static inline unsigned char xhi_data_and_inc(const unsigned char *d, int *i, int sz)

could move to the *.h

I will restructure caller function xrt_xclbin_parse_bitstream_header() and remove xhi_data_and_inc().

Quoted 36 lines

drivers/fpga/xrt/lib/xclbin.c line 32

Quoted 5 lines
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
+{_
+     unsigned char data;
+
+     if (*i >= sz)
+             return -1;

The return value of this funtion is not always checked, at the least add a dev_err here

Quoted 18 lines
drivers/fpga/xrt/lib/xclbin.c new file +18 -0
+
+     data = d[*i];
+     (*i)++;
+
+     return data;
+}
+
+static const struct axlf_section_header *
+xrt_xclbin_get_section_hdr(const struct axlf *xclbin,
+                        enum axlf_section_kind kind)
+{
+     int i = 0;
+
+     for (i = 0; i < xclbin->m_header.m_numSections; i++) {
+             if (xclbin->m_sections[i].m_sectionKind == kind)
+                     return &xclbin->m_sections[i];
+     }
+

drivers/fpga/xrt/lib/xclbin.c line 60

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+     return NULL;
+}
+
+static int
+xrt_xclbin_check_section_hdr(const struct axlf_section_header *header,
+                          u64 xclbin_len)
+{
+     int ret;
+
+     ret = (header->m_sectionOffset + header->m_sectionSize) > xclbin_len ? -EINVAL : 0;

Tristate is harder to read, consider replacing with if()

Quoted 5 lines

int ret = 0

if ()

ret =

Will change to if statement.

Quoted 22 lines
Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +9 -0
+
+     return ret;
+}
+
+static int xrt_xclbin_section_info(const struct axlf *xclbin,
+                                enum axlf_section_kind kind,
+                                u64 *offset, u64 *size)
+{
+     const struct axlf_section_header *mem_header = NULL;

drivers/fpga/xrt/lib/xclbin.c line 79

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+     u64 xclbin_len;
+     int err = 0;
+
+     mem_header = xrt_xclbin_get_section_hdr(xclbin, kind);
+     if (!mem_header)
+             return -EINVAL;
+
+     xclbin_len = xclbin->m_header.m_length;
+     if (xclbin_len > MAX_XCLBIN_SIZE)
+             return -EINVAL;

This check can be added to the function call..

Quoted 3 lines

or the sanity checking added to the earier call to *get_section_hdr

There a number of small functions that can be combined.

Will change this and combine the small functions.

Quoted 15 lines

drivers/fpga/xrt/lib/xclbin.c line 91

Quoted 13 lines
drivers/fpga/xrt/lib/xclbin.c new file +12 -0
+
+     err = xrt_xclbin_check_section_hdr(mem_header, xclbin_len);
+     if (err)
+             return err;
+
+     *offset = mem_header->m_sectionOffset;
+     *size = mem_header->m_sectionSize;
+
+     return 0;
+}
+
+/* caller should free the allocated memory for **data */

must free

Quoted 1 line

This comment also needs to be with the *.h decl

Will change this and add the same comment to .h

Quoted 24 lines
Quoted 12 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+int xrt_xclbin_get_section(const struct axlf *buf,
+                        enum axlf_section_kind kind,
+                        void **data, u64 *len)
+{
+     const struct axlf *xclbin = (const struct axlf *)buf;
+     void *section = NULL;
+     int err = 0;
+     u64 offset = 0;
+     u64 size = 0;
+
+     err = xrt_xclbin_section_info(xclbin, kind, &offset, &size);

drivers/fpga/xrt/lib/xclbin.c line 112

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+     if (err)
+             return err;
+
+     section = vmalloc(size);
+     if (!section)
+             return -ENOMEM;
+
+     memcpy(section, ((const char *)xclbin) + offset, size);
+
+     *data = section;

a general comment

Quoted 3 lines

for exported function checking the validity of the inputs in more important.

here you assume **data is valid, really you should check.

Will add check for exported functions.

Quoted 4 lines

drivers/fpga/xrt/lib/xclbin.c line 114

Quoted 3 lines
drivers/fpga/xrt/lib/xclbin.c new file +2 -0
+     if (len)
+             *len = size;

len setting being optional, needs to be in the *.h comment

Will add comment.

Quoted 8 lines

drivers/fpga/xrt/lib/xclbin.c line 119

Quoted 5 lines
drivers/fpga/xrt/lib/xclbin.c new file +5 -0
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_get_section);
+

Instead of allocating new memory and making copies of bits of *data

why not have the points reference data ?

Agree. I will change to use this points reference data.

Quoted 4 lines

The size operations look like translating big endian data to little endian.

This will break on a big endian host.

It does not translate to little endian. It converts to host endian. We can not use existing function because of the alignment.

Quoted 19 lines
Quoted 7 lines
drivers/fpga/xrt/lib/xclbin.c new file +6 -0
+/* parse bitstream header */
+int xrt_xclbin_parse_bitstream_header(const unsigned char *data,
+                                   unsigned int size,
+                                   struct hw_icap_bit_header *header)
+{
+     unsigned int index;

drivers/fpga/xrt/lib/xclbin.c line 135

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+     unsigned int len;
+     unsigned int tmp;
+     unsigned int i;
+
+     memset(header, 0, sizeof(*header));
+     /* Start Index at start of bitstream */
+     index = 0;
+
+     /* Initialize HeaderLength.  If header returned early inidicates
+      * failure.

This side effect should be documented in the *.h comment.

Quoted 1 line

Also the multi line comment is a bit weird, not sure if it is ok

Will remove this. The function will return -EINVAL for failure cases.

Quoted 86 lines
Quoted 86 lines
drivers/fpga/xrt/lib/xclbin.c new file +85 -0
+      */
+     header->header_length = XHI_BIT_HEADER_FAILURE;
+
+     /* Get "Magic" length */
+     header->magic_length = xhi_data_and_inc(data, &index, size);
+     header->magic_length = (header->magic_length << 8) | xhi_data_and_inc(data, &index, size);
+
+     /* Read in "magic" */
+     for (i = 0; i < header->magic_length - 1; i++) {
+             tmp = xhi_data_and_inc(data, &index, size);
+             if (i % 2 == 0 && tmp != XHI_EVEN_MAGIC_BYTE)
+                     return -1;      /* INVALID_FILE_HEADER_ERROR */
+
+             if (i % 2 == 1 && tmp != XHI_ODD_MAGIC_BYTE)
+                     return -1;      /* INVALID_FILE_HEADER_ERROR */
+     }
+
+     /* Read null end of magic data. */
+     tmp = xhi_data_and_inc(data, &index, size);
+
+     /* Read 0x01 (short) */
+     tmp = xhi_data_and_inc(data, &index, size);
+     tmp = (tmp << 8) | xhi_data_and_inc(data, &index, size);
+
+     /* Check the "0x01" half word */
+     if (tmp != 0x01)
+             return -1;      /* INVALID_FILE_HEADER_ERROR */
+
+     /* Read 'a' */
+     tmp = xhi_data_and_inc(data, &index, size);
+     if (tmp != 'a')
+             return -1;      /* INVALID_FILE_HEADER_ERROR    */
+
+     /* Get Design Name length */
+     len = xhi_data_and_inc(data, &index, size);
+     len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+     /* allocate space for design name and final null character. */
+     header->design_name = vmalloc(len);
+     if (!header->design_name)
+             return -ENOMEM;
+
+     /* Read in Design Name */
+     for (i = 0; i < len; i++)
+             header->design_name[i] = xhi_data_and_inc(data, &index, size);
+
+     if (header->design_name[len - 1] != '\0')
+             return -1;
+
+     header->version = strstr(header->design_name, "Version=") + strlen("Version=");
+
+     /* Read 'b' */
+     tmp = xhi_data_and_inc(data, &index, size);
+     if (tmp != 'b')
+             return -1;      /* INVALID_FILE_HEADER_ERROR */
+
+     /* Get Part Name length */
+     len = xhi_data_and_inc(data, &index, size);
+     len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+     /* allocate space for part name and final null character. */
+     header->part_name = vmalloc(len);
+     if (!header->part_name)
+             return -ENOMEM;
+
+     /* Read in part name */
+     for (i = 0; i < len; i++)
+             header->part_name[i] = xhi_data_and_inc(data, &index, size);
+
+     if (header->part_name[len - 1] != '\0')
+             return -1;
+
+     /* Read 'c' */
+     tmp = xhi_data_and_inc(data, &index, size);
+     if (tmp != 'c')
+             return -1;      /* INVALID_FILE_HEADER_ERROR */
+
+     /* Get date length */
+     len = xhi_data_and_inc(data, &index, size);
+     len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+     /* allocate space for date and final null character. */
+     header->date = vmalloc(len);
+     if (!header->date)
+             return -ENOMEM;
Quoted 8 lines

drivers/fpga/xrt/lib/xclbin.c line 227

Quoted 7 lines
drivers/fpga/xrt/lib/xclbin.c new file +7 -0
+
+     /* Read in date name */
+     for (i = 0; i < len; i++)
+             header->date[i] = xhi_data_and_inc(data, &index, size);
+
+     if (header->date[len - 1] != '\0')
+             return -1;

generally -EINVAL is more meaningful than -1

Will fix this.

Quoted 37 lines
Quoted 25 lines
drivers/fpga/xrt/lib/xclbin.c new file +25 -0
+
+     /* Read 'd' */
+     tmp = xhi_data_and_inc(data, &index, size);
+     if (tmp != 'd')
+             return -1;      /* INVALID_FILE_HEADER_ERROR  */
+
+     /* Get time length */
+     len = xhi_data_and_inc(data, &index, size);
+     len = (len << 8) | xhi_data_and_inc(data, &index, size);
+
+     /* allocate space for time and final null character. */
+     header->time = vmalloc(len);
+     if (!header->time)
+             return -ENOMEM;
+
+     /* Read in time name */
+     for (i = 0; i < len; i++)
+             header->time[i] = xhi_data_and_inc(data, &index, size);
+
+     if (header->time[len - 1] != '\0')
+             return -1;
+
+     /* Read 'e' */
+     tmp = xhi_data_and_inc(data, &index, size);
+     if (tmp != 'e')

drivers/fpga/xrt/lib/xclbin.c line 262

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+             return -1;      /* INVALID_FILE_HEADER_ERROR */
+
+     /* Get byte length of bitstream */
+     header->bitstream_length = xhi_data_and_inc(data, &index, size);
+     header->bitstream_length = (header->bitstream_length << 8) |
+             xhi_data_and_inc(data, &index, size);
+     header->bitstream_length = (header->bitstream_length << 8) |
+             xhi_data_and_inc(data, &index, size);
+     header->bitstream_length = (header->bitstream_length << 8) |
+             xhi_data_and_inc(data, &index, size);

generally a problem

Quoted 3 lines

This is confusing, collect the bytes in a temp[] and construct the header->bitstream_length in on statement.

This is a case where xhi_data_and_inc return is not checked and if it failed could blow up later.

Will restructure this function and check the possible overflow.

Quoted 6 lines

drivers/fpga/xrt/lib/xclbin.c line 264

Quoted 3 lines
drivers/fpga/xrt/lib/xclbin.c new file +2 -0
+
+     header->header_length = index;

index is not a good variable name if it going to be stored as a length.

consider changing it to something like current_length.

Will change it to offset.

Quoted 13 lines

drivers/fpga/xrt/lib/xclbin.c line 275

Quoted 12 lines
drivers/fpga/xrt/lib/xclbin.c new file +11 -0
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_parse_bitstream_header);
+
+void xrt_xclbin_free_header(struct hw_icap_bit_header *header)
+{
+     vfree(header->design_name);
+     vfree(header->part_name);
+     vfree(header->date);
+     vfree(header->time);

missing header->version

Will remove xrt_xclbin_free_header(). All the pointers will point to reference data instead of allocation.

Quoted 38 lines
Quoted 26 lines
drivers/fpga/xrt/lib/xclbin.c new file +26 -0
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_free_header);
+
+struct xrt_clock_desc {
+     char    *clock_ep_name;
+     u32     clock_xclbin_type;
+     char    *clkfreq_ep_name;
+} clock_desc[] = {
+     {
+             .clock_ep_name = XRT_MD_NODE_CLK_KERNEL1,
+             .clock_xclbin_type = CT_DATA,
+             .clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_K1,
+     },
+     {
+             .clock_ep_name = XRT_MD_NODE_CLK_KERNEL2,
+             .clock_xclbin_type = CT_KERNEL,
+             .clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_K2,
+     },
+     {
+             .clock_ep_name = XRT_MD_NODE_CLK_KERNEL3,
+             .clock_xclbin_type = CT_SYSTEM,
+             .clkfreq_ep_name = XRT_MD_NODE_CLKFREQ_HBM,
+     },
+};
+
+const char *xrt_clock_type2epname(enum CLOCK_TYPE type)

drivers/fpga/xrt/lib/xclbin.c line 311

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+{
+     int i;
+
+     for (i = 0; i < ARRAY_SIZE(clock_desc); i++) {
+             if (clock_desc[i].clock_xclbin_type == type)
+                     return clock_desc[i].clock_ep_name;
+     }
+     return NULL;
+}
+EXPORT_SYMBOL_GPL(xrt_clock_type2epname);

What is clock stuff doing in xclbin ?

Quoted 1 line

I think clock needs its own file

xclbin file contains a section which describes the clock settings to running this xclbin on hardware. The _clock_ functions convert clock section to XRT driver metadata which is in device tree format.

Quoted 18 lines
Quoted 18 lines
drivers/fpga/xrt/lib/xclbin.c new file +17 -0
+
+static const char *clock_type2clkfreq_name(u32 type)
+{
+     int i;
+
+     for (i = 0; i < ARRAY_SIZE(clock_desc); i++) {
+             if (clock_desc[i].clock_xclbin_type == type)
+                     return clock_desc[i].clkfreq_ep_name;
+     }
+     return NULL;
+}
+
+static int xrt_xclbin_add_clock_metadata(struct device *dev,
+                                      const struct axlf *xclbin,
+                                      char *dtb)
+{
+     int i;
Quoted 8 lines

drivers/fpga/xrt/lib/xclbin.c line 335

Quoted 7 lines
drivers/fpga/xrt/lib/xclbin.c new file +7 -0
+     u16 freq;
+     struct clock_freq_topology *clock_topo;
+     int rc = xrt_xclbin_get_section(xclbin, CLOCK_FREQ_TOPOLOGY,
+                                     (void **)&clock_topo, NULL);
+
+     if (rc)
+             return 0;

failing is ok ?

If clock section does not exist in xclbin, that means the xclbin uses default clock. And this is a valid case. I will add comment /* if clock section does not exist, add nothing and return success */

Quoted 12 lines
Quoted 12 lines
drivers/fpga/xrt/lib/xclbin.c new file +12 -0
+
+     for (i = 0; i < clock_topo->m_count; i++) {
+             u8 type = clock_topo->m_clock_freq[i].m_type;
+             const char *ep_name = xrt_clock_type2epname(type);
+             const char *counter_name = clock_type2clkfreq_name(type);
+
+             if (!ep_name || !counter_name)
+                     continue;
+
+             freq = cpu_to_be16(clock_topo->m_clock_freq[i].m_freq_Mhz);
+             rc = xrt_md_set_prop(dev, dtb, ep_name, NULL, XRT_MD_PROP_CLK_FREQ,
+                                  &freq, sizeof(freq));
Quoted 8 lines

drivers/fpga/xrt/lib/xclbin.c line 354

Quoted 7 lines
drivers/fpga/xrt/lib/xclbin.c new file +7 -0
+             if (rc)
+                     break;
+
+             rc = xrt_md_set_prop(dev, dtb, ep_name, NULL, XRT_MD_PROP_CLK_CNT,
+                                  counter_name, strlen(counter_name) + 1);
+             if (rc)
+                     break;

Failing in a loop, why isn't there some cleanup of the *set_prop() calls ?

The entire blob will be freed outside if this function return failure.

Quoted 4 lines
Quoted 4 lines
drivers/fpga/xrt/lib/xclbin.c new file +4 -0
+     }
+
+     vfree(clock_topo);
+
Quoted 8 lines

drivers/fpga/xrt/lib/xclbin.c line 365

Quoted 7 lines
drivers/fpga/xrt/lib/xclbin.c new file +7 -0
+     return rc;
+}
+
+int xrt_xclbin_get_metadata(struct device *dev, const struct axlf *xclbin, char **dtb)
+{
+     char *md = NULL, *newmd = NULL;
+     u64 len;

*dtb = NULL;

Will fix this.

Quoted 27 lines
Quoted 9 lines
drivers/fpga/xrt/lib/xclbin.c new file +9 -0
+     int rc = xrt_xclbin_get_section(xclbin, PARTITION_METADATA,
+                                     (void **)&md, &len);
+
+     if (rc)
+             goto done;
+
+     /* Sanity check the dtb section. */
+     if (xrt_md_size(dev, md) > len) {
+             rc = -EINVAL;

drivers/fpga/xrt/lib/xclbin.c line 384

Quoted 10 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+             goto done;
+     }
+
+     newmd = xrt_md_dup(dev, md);
+     if (!newmd) {
+             rc = -EFAULT;
+             goto done;
+     }
+     /* Convert various needed xclbin sections into dtb. */
+     rc = xrt_xclbin_add_clock_metadata(dev, xclbin, newmd);

newmd is only valid here, but the above error handling jump here. change this to

if (!rc)

*dtb = newmd

else

Quoted 7 lines

vfree(newmd)

done:

   vfree(md)

   return rc;

Will change it.

Quoted 46 lines
Quoted 17 lines
drivers/fpga/xrt/lib/xclbin.c new file +10 -0
+
+done:
+     if (rc == 0)
+             *dtb = newmd;
+     else
+             vfree(newmd);
+     vfree(md);
+     return rc;
+}
+EXPORT_SYMBOL_GPL(xrt_xclbin_get_metadata);
include/uapi/linux/xrt/xclbin.h new file +1 -0
new file mode 100644
index 000000000000..53f140123ef1
@@ -0,0 +1,408 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */

include/uapi/linux/xrt/xclbin.h line 11

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+/*
+ *  Xilinx FPGA compiled binary container format
+ *
+ *  Copyright (C) 2015-2021, Xilinx Inc
+ */
+
+#ifndef _XCLBIN_H_
+#define _XCLBIN_H_
+
+#ifdef _WIN32

WIN32 ?

Only 1 other header has this ifdef

include/uapi/linux/xrt/xclbin.h line 13

Quoted 2 lines
include/uapi/linux/xrt/xclbin.h new file +2 -0
+  #include <cstdint>
+  #include <algorithm>

c++ is being assumed for windows

include/uapi/linux/xrt/xclbin.h line 14

Quoted 1 line
include/uapi/linux/xrt/xclbin.h new file +1 -0
+  #include "windows/uuid.h"

thank you for not including windows.h ;)

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+#else
+  #if defined(__KERNEL__)
+    #include <linux/types.h>
+    #include <linux/uuid.h>
+    #include <linux/version.h>
+  #elif defined(__cplusplus)
+    #include <cstdlib>
+    #include <cstdint>
+    #include <algorithm>
+    #include <uuid/uuid.h>
Quoted 8 lines

include/uapi/linux/xrt/xclbin.h line 31

Quoted 7 lines
include/uapi/linux/xrt/xclbin.h new file +7 -0
+  #else
+    #include <stdlib.h>
+    #include <stdint.h>
+    #include <uuid/uuid.h>
+  #endif
+#endif
+

Review these includes, some could be convenience includes.

removed uuid.h and version.h.

Quoted 12 lines

ex/ linux/version.h with no obvious use of version macros.

struct axlf_header {

include/uapi/linux/xrt/xclbin.h new file +6 -0
+       uint64_t m_length;                  /* Total size of the xclbin file */
.. snip ..
+       union {
+               char m_next_axlf[16];           /* Name of next xclbin file */
+                                               /* in the daisy chain */
+               uuid_t uuid;                    /* uuid of this xclbin*/
+       };
Quoted 3 lines

As mentioned in an earlier patch, if uuid_t is larger than 16 bytes, axlf_header breaks. while it is convenient to have this type here, it would be better this access was handled in another way. Maybe a host specific function.

Agree. will change to "unsigned char uuid[16]".

Quoted 2 lines

I also do not see a pragma pack, usually this is set of 1 so the compiler does not shuffle elements, increase size etc.

This data structure is shared with other tools. And the structure is well defined with reasonable alignment. It is compatible with all compilers we have tested. So pragma pack is not necessary.

Quoted 10 lines

include/uapi/linux/xrt/xclbin.h line 39

Quoted 9 lines
include/uapi/linux/xrt/xclbin.h new file +8 -0
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * DOC: Container format for Xilinx FPGA images
+ * The container stores bitstreams, metadata and firmware images.
+ * xclbin/xsabin is ELF-like binary container format. It is structured

is an ELF-like file format. It is a structured

Will fix this.

Quoted 30 lines
Quoted 20 lines
include/uapi/linux/xrt/xclbin.h new file +20 -0
+ * series of sections. There is a file header followed by several section
+ * headers which is followed by sections. A section header points to an
+ * actual section. There is an optional signature at the end. The
+ * following figure illustrates a typical xclbin:
+ *
+ *     +---------------------+
+ *     |                  |
+ *     |       HEADER             |
+ *     +---------------------+
+ *     |   SECTION  HEADER   |
+ *     |                  |
+ *     +---------------------+
+ *     |      ...         |
+ *     |                  |
+ *     +---------------------+
+ *     |   SECTION  HEADER   |
+ *     |                  |
+ *     +---------------------+
+ *     |       SECTION            |
+ *     |                  |

include/uapi/linux/xrt/xclbin.h line 69

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+ *     +---------------------+
+ *     |      ...         |
+ *     |                  |
+ *     +---------------------+
+ *     |       SECTION            |
+ *     |                  |
+ *     +---------------------+
+ *     |      SIGNATURE           |
+ *     |      (OPTIONAL)     |
+ *     +---------------------+
Quoted 3 lines

This ascii art is a mixture of tabs and spaces, for someone with tab = 2 spaces, this will look messed up.

convert the tabs to spaces

Will fix this.

Quoted 7 lines

include/uapi/linux/xrt/xclbin.h line 73

Quoted 5 lines
include/uapi/linux/xrt/xclbin.h new file +4 -0
+ */
+
+enum XCLBIN_MODE {
+     XCLBIN_FLAT,

generally

Quoted 5 lines

all enums used in a file format should be initialized.

This likely should be

XCLBIN_FLAT = 0,

Will fix this.

Quoted 71 lines
Quoted 71 lines
include/uapi/linux/xrt/xclbin.h new file +70 -0
+     XCLBIN_PR,
+     XCLBIN_TANDEM_STAGE2,
+     XCLBIN_TANDEM_STAGE2_WITH_PR,
+     XCLBIN_HW_EMU,
+     XCLBIN_SW_EMU,
+     XCLBIN_MODE_MAX
+};
+
+enum axlf_section_kind {
+     BITSTREAM = 0,
+     CLEARING_BITSTREAM,
+     EMBEDDED_METADATA,
+     FIRMWARE,
+     DEBUG_DATA,
+     SCHED_FIRMWARE,
+     MEM_TOPOLOGY,
+     CONNECTIVITY,
+     IP_LAYOUT,
+     DEBUG_IP_LAYOUT,
+     DESIGN_CHECK_POINT,
+     CLOCK_FREQ_TOPOLOGY,
+     MCS,
+     BMC,
+     BUILD_METADATA,
+     KEYVALUE_METADATA,
+     USER_METADATA,
+     DNA_CERTIFICATE,
+     PDI,
+     BITSTREAM_PARTIAL_PDI,
+     PARTITION_METADATA,
+     EMULATION_DATA,
+     SYSTEM_METADATA,
+     SOFT_KERNEL,
+     ASK_FLASH,
+     AIE_METADATA,
+     ASK_GROUP_TOPOLOGY,
+     ASK_GROUP_CONNECTIVITY
+};
+
+enum MEM_TYPE {
+     MEM_DDR3,
+     MEM_DDR4,
+     MEM_DRAM,
+     MEM_STREAMING,
+     MEM_PREALLOCATED_GLOB,
+     MEM_ARE,
+     MEM_HBM,
+     MEM_BRAM,
+     MEM_URAM,
+     MEM_STREAMING_CONNECTION
+};
+
+enum IP_TYPE {
+     IP_MB = 0,
+     IP_KERNEL,
+     IP_DNASC,
+     IP_DDR4_CONTROLLER,
+     IP_MEM_DDR4,
+     IP_MEM_HBM
+};
+
+struct axlf_section_header {
+     uint32_t m_sectionKind;             /* Section type */
+     char m_sectionName[16];             /* Examples: "stage2", "clear1", */
+                                         /* "clear2", "ocl1", "ocl2, */
+                                         /* "ublaze", "sched" */
+     uint64_t m_sectionOffset;           /* File offset of section data */
+     uint64_t m_sectionSize;             /* Size of section data */
+};
+
Quoted 8 lines

include/uapi/linux/xrt/xclbin.h line 150

Quoted 7 lines
include/uapi/linux/xrt/xclbin.h new file +7 -0
+struct axlf_header {
+     uint64_t m_length;                  /* Total size of the xclbin file */
+     uint64_t m_timeStamp;               /* Number of seconds since epoch */
+                                         /* when xclbin was created */
+     uint64_t m_featureRomTimeStamp;     /* TimeSinceEpoch of the featureRom */
+     uint16_t m_versionPatch;            /* Patch Version */
+     uint8_t m_versionMajor;             /* Major Version - Version: 2.1.0*/

i did not see the version checked earlier, which one is expected ?

Will check Major.

Quoted 12 lines

include/uapi/linux/xrt/xclbin.h line 162

Quoted 12 lines
include/uapi/linux/xrt/xclbin.h new file +12 -0
+     uint8_t m_versionMinor;             /* Minor Version */
+     uint32_t m_mode;                    /* XCLBIN_MODE */
+     union {
+             struct {
+                     uint64_t m_platformId;  /* 64 bit platform ID: */
+                                     /* vendor-device-subvendor-subdev */
+                     uint64_t m_featureId;   /* 64 bit feature id */
+             } rom;
+             unsigned char rom_uuid[16];     /* feature ROM UUID for which */
+                                             /* this xclbin was generated */
+     };
+     unsigned char m_platformVBNV[64];       /* e.g. */
Quoted 1 line

what is VBNV?

VBNV stands for Vendor, BoardID, Name, Version. It is a string like <Vendor>:<BoardID>:<Name>:<Version> or <Vendor>_<BoardID>_<Name>_<Version>

Quoted 57 lines
Quoted 45 lines
include/uapi/linux/xrt/xclbin.h new file +45 -0
+             /* xilinx:xil-accel-rd-ku115:4ddr-xpr:3.4: null terminated */
+     union {
+             char m_next_axlf[16];           /* Name of next xclbin file */
+                                             /* in the daisy chain */
+             uuid_t uuid;                    /* uuid of this xclbin*/
+     };
+     char m_debug_bin[16];                   /* Name of binary with debug */
+                                             /* information */
+     uint32_t m_numSections;                 /* Number of section headers */
+};
+
+struct axlf {
+     char m_magic[8];                        /* Should be "xclbin2\0"  */
+     int32_t m_signature_length;             /* Length of the signature. */
+                                             /* -1 indicates no signature */
+     unsigned char reserved[28];             /* Note: Initialized to 0xFFs */
+
+     unsigned char m_keyBlock[256];          /* Signature for validation */
+                                             /* of binary */
+     uint64_t m_uniqueId;                    /* axlf's uniqueId, use it to */
+                                             /* skip redownload etc */
+     struct axlf_header m_header;            /* Inline header */
+     struct axlf_section_header m_sections[1];   /* One or more section */
+                                                 /* headers follow */
+};
+
+/* bitstream information */
+struct xlnx_bitstream {
+     uint8_t m_freq[8];
+     char bits[1];
+};
+
+/****        MEMORY TOPOLOGY SECTION ****/
+struct mem_data {
+     uint8_t m_type; /* enum corresponding to mem_type. */
+     uint8_t m_used; /* if 0 this bank is not present */
+     union {
+             uint64_t m_size; /* if mem_type DDR, then size in KB; */
+             uint64_t route_id; /* if streaming then "route_id" */
+     };
+     union {
+             uint64_t m_base_address;/* if DDR then the base address; */
+             uint64_t flow_id; /* if streaming then "flow id" */
+     };
+     unsigned char m_tag[16]; /* DDR: BANK0,1,2,3, has to be null */

include/uapi/linux/xrt/xclbin.h line 217

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+                     /* terminated; if streaming then stream0, 1 etc */
+};
+
+struct mem_topology {
+     int32_t m_count; /* Number of mem_data */
+     struct mem_data m_mem_data[1]; /* Should be sorted on mem_type */
+};
+
+/****        CONNECTIVITY SECTION ****/
+/* Connectivity of each argument of Kernel. It will be in terms of argument

This section does not make sense.

Quoted 1 line

Likely you mean some algorithm kernel, rather than the linux kernel.

The kernel here means hardware IP kernel. I will change the term to CU (Compute Unit).

Quoted 28 lines
Quoted 28 lines
include/uapi/linux/xrt/xclbin.h new file +27 -0
+ * index associated. For associating kernel instances with arguments and
+ * banks, start at the connectivity section. Using the m_ip_layout_index
+ * access the ip_data.m_name. Now we can associate this kernel instance
+ * with its original kernel name and get the connectivity as well. This
+ * enables us to form related groups of kernel instances.
+ */
+
+struct connection {
+     int32_t arg_index; /* From 0 to n, may not be contiguous as scalars */
+                        /* skipped */
+     int32_t m_ip_layout_index; /* index into the ip_layout section. */
+                        /* ip_layout.m_ip_data[index].m_type == IP_KERNEL */
+     int32_t mem_data_index; /* index of the m_mem_data . Flag error is */
+                             /* m_used false. */
+};
+
+struct connectivity {
+     int32_t m_count;
+     struct connection m_connection[1];
+};
+
+/****        IP_LAYOUT SECTION ****/
+
+/* IP Kernel */
+#define IP_INT_ENABLE_MASK     0x0001
+#define IP_INTERRUPT_ID_MASK  0x00FE
+#define IP_INTERRUPT_ID_SHIFT 0x1
Quoted 8 lines

include/uapi/linux/xrt/xclbin.h line 251

Quoted 7 lines
include/uapi/linux/xrt/xclbin.h new file +7 -0
+
+enum IP_CONTROL {
+     AP_CTRL_HS = 0,
+     AP_CTRL_CHAIN = 1,
+     AP_CTRL_NONE = 2,
+     AP_CTRL_ME = 3,
+     ACCEL_ADAPTER = 4

assigning beyond the first is not necessary unless there are dups or gaps

Will fix this.

Quoted 33 lines
Quoted 21 lines
include/uapi/linux/xrt/xclbin.h new file +21 -0
+};
+
+#define IP_CONTROL_MASK       0xFF00
+#define IP_CONTROL_SHIFT 0x8
+
+/* IPs on AXI lite - their types, names, and base addresses.*/
+struct ip_data {
+     uint32_t m_type; /* map to IP_TYPE enum */
+     union {
+             uint32_t properties; /* Default: 32-bits to indicate ip */
+                                  /* specific property. */
+             /* m_type: IP_KERNEL
+              *          m_int_enable   : Bit  - 0x0000_0001;
+              *          m_interrupt_id : Bits - 0x0000_00FE;
+              *          m_ip_control   : Bits = 0x0000_FF00;
+              */
+             struct {                 /* m_type: IP_MEM_* */
+                     uint16_t m_index;
+                     uint8_t m_pc_index;
+                     uint8_t unused;
+             } indices;

include/uapi/linux/xrt/xclbin.h line 282

Quoted 10 lines
include/uapi/linux/xrt/xclbin.h new file +10 -0
+     };
+     uint64_t m_base_address;
+     uint8_t m_name[64]; /* eg Kernel name corresponding to KERNEL */
+                         /* instance, can embed CU name in future. */
+};
+
+struct ip_layout {
+     int32_t m_count;
+     struct ip_data m_ip_data[1]; /* All the ip_data needs to be sorted */
+                                  /* by m_base_address. */

general

Quoted 1 line

doing the bla[1] for c++ ?

This file is cross platform and having an array with size 0 chokes certain compilers.

Thanks,
Lizhi

Quoted 129 lines

Tom

Quoted 126 lines
include/uapi/linux/xrt/xclbin.h new file +126 -0
+};
+
+/*** Debug IP section layout ****/
+enum DEBUG_IP_TYPE {
+     UNDEFINED = 0,
+     LAPC,
+     ILA,
+     AXI_MM_MONITOR,
+     AXI_TRACE_FUNNEL,
+     AXI_MONITOR_FIFO_LITE,
+     AXI_MONITOR_FIFO_FULL,
+     ACCEL_MONITOR,
+     AXI_STREAM_MONITOR,
+     AXI_STREAM_PROTOCOL_CHECKER,
+     TRACE_S2MM,
+     AXI_DMA,
+     TRACE_S2MM_FULL
+};
+
+struct debug_ip_data {
+     uint8_t m_type; /* type of enum DEBUG_IP_TYPE */
+     uint8_t m_index_lowbyte;
+     uint8_t m_properties;
+     uint8_t m_major;
+     uint8_t m_minor;
+     uint8_t m_index_highbyte;
+     uint8_t m_reserved[2];
+     uint64_t m_base_address;
+     char    m_name[128];
+};
+
+struct debug_ip_layout {
+     uint16_t m_count;
+     struct debug_ip_data m_debug_ip_data[1];
+};
+
+/* Supported clock frequency types */
+enum CLOCK_TYPE {
+     CT_UNUSED = 0,                     /* Initialized value */
+     CT_DATA   = 1,                     /* Data clock */
+     CT_KERNEL = 2,                     /* Kernel clock */
+     CT_SYSTEM = 3                      /* System Clock */
+};
+
+/* Clock Frequency Entry */
+struct clock_freq {
+     uint16_t m_freq_Mhz;               /* Frequency in MHz */
+     uint8_t m_type;                    /* Clock type (enum CLOCK_TYPE) */
+     uint8_t m_unused[5];               /* Not used - padding */
+     char m_name[128];                  /* Clock Name */
+};
+
+/* Clock frequency section */
+struct clock_freq_topology {
+     int16_t m_count;                   /* Number of entries */
+     struct clock_freq m_clock_freq[1]; /* Clock array */
+};
+
+/* Supported MCS file types */
+enum MCS_TYPE {
+     MCS_UNKNOWN = 0,                   /* Initialized value */
+     MCS_PRIMARY = 1,                   /* The primary mcs file data */
+     MCS_SECONDARY = 2,                 /* The secondary mcs file data */
+};
+
+/* One chunk of MCS data */
+struct mcs_chunk {
+     uint8_t m_type;                    /* MCS data type */
+     uint8_t m_unused[7];               /* padding */
+     uint64_t m_offset;                 /* data offset from the start of */
+                                        /* the section */
+     uint64_t m_size;                   /* data size */
+};
+
+/* MCS data section */
+struct mcs {
+     int8_t m_count;                    /* Number of chunks */
+     int8_t m_unused[7];                /* padding */
+     struct mcs_chunk m_chunk[1];       /* MCS chunks followed by data */
+};
+
+/* bmc data section */
+struct bmc {
+     uint64_t m_offset;                 /* data offset from the start of */
+                                        /* the section */
+     uint64_t m_size;                   /* data size (bytes) */
+     char m_image_name[64];             /* Name of the image */
+                                        /* (e.g., MSP432P401R) */
+     char m_device_name[64];            /* Device ID (e.g., VCU1525)  */
+     char m_version[64];
+     char m_md5value[33];               /* MD5 Expected Value */
+                             /* (e.g., 56027182079c0bd621761b7dab5a27ca)*/
+     char m_padding[7];                 /* Padding */
+};
+
+/* soft kernel data section, used by classic driver */
+struct soft_kernel {
+     /** Prefix Syntax:
+      *  mpo - member, pointer, offset
+      *  This variable represents a zero terminated string
+      *  that is offseted from the beginning of the section.
+      *  The pointer to access the string is initialized as follows:
+      *  char * pCharString = (address_of_section) + (mpo value)
+      */
+     uint32_t mpo_name;         /* Name of the soft kernel */
+     uint32_t m_image_offset;   /* Image offset */
+     uint32_t m_image_size;     /* Image size */
+     uint32_t mpo_version;      /* Version */
+     uint32_t mpo_md5_value;    /* MD5 checksum */
+     uint32_t mpo_symbol_name;  /* Symbol name */
+     uint32_t m_num_instances;  /* Number of instances */
+     uint8_t padding[36];       /* Reserved for future use */
+     uint8_t reservedExt[16];   /* Reserved for future extended data */
+};
+
+enum CHECKSUM_TYPE {
+     CST_UNKNOWN = 0,
+     CST_SDBM = 1,
+     CST_LAST
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
reviewer
#16 lore raw

Re: [PATCH V3 XRT Alveo 03/18] fpga: xrt: xclbin file helper functions

Quoted 3 lines · Lizhi Hou

Hi Tom,

snip

Quoted 3 lines
Quoted 2 lines

I also do not see a pragma pack, usually this is set of 1 so the compiler does not shuffle elements, increase size etc.

This data structure is shared with other tools. And the structure is well defined with reasonable alignment. It is compatible with all compilers we have tested. So pragma pack is not necessary.

You can not have possibly tested all the configurations since the kernel supports many arches and compilers.

If the tested existing alignment is ok, pragma pack should be a noop on your tested configurations.

And help cover the untested configurations.

Tom

author
#17 lore raw

Re: [PATCH V3 XRT Alveo 03/18] fpga: xrt: xclbin file helper functions

Hi Tom,

Quoted 9 lines · Tom Rix

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

Quoted 3 lines · Lizhi Hou

Hi Tom,

snip

Quoted 7 lines
Quoted 2 lines
Quoted 1 line

I also do not see a pragma pack, usually this is set of 1 so the compiler does not shuffle elements, increase size etc.

This data structure is shared with other tools. And the structure is well defined with reasonable alignment. It is compatible with all compilers we have tested. So pragma pack is not necessary.

You can not have possibly tested all the configurations since the kernel supports many arches and compilers.

If the tested existing alignment is ok, pragma pack should be a noop on your tested configurations.

And help cover the untested configurations.

Got it. I will add pragma pack(1).

Lizhi

Quoted 3 lines

Tom

maintainer
#18 lore raw

Re: [PATCH V3 XRT Alveo 03/18] fpga: xrt: xclbin file helper functions

Quoted 13 lines · Lizhi Hou

Hi Tom,

Quoted 9 lines · Tom Rix

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

Quoted 3 lines · Lizhi Hou

Hi Tom,

snip

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines
Quoted 1 line

I also do not see a pragma pack, usually this is set of 1 so the compiler does not shuffle elements, increase size etc.

This data structure is shared with other tools. And the structure is well defined with reasonable alignment. It is compatible with all compilers we have tested. So pragma pack is not necessary.

You can not have possibly tested all the configurations since the kernel supports many arches and compilers.

If the tested existing alignment is ok, pragma pack should be a noop on your tested configurations.

And help cover the untested configurations.

Got it. I will add pragma pack(1).

Please do not use pragma pack(), add __packed to the structs in question.

- Moritz

author
#19 lore raw

Re: [PATCH V3 XRT Alveo 03/18] fpga: xrt: xclbin file helper functions

Hi Moritz,

Quoted 17 lines · Moritz Fischer
Quoted 15 lines · Lizhi Hou

Hi Tom,

Quoted 11 lines · Tom Rix

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

Quoted 3 lines · Lizhi Hou

Hi Tom,

snip

Quoted 2 lines
Quoted 1 line

I also do not see a pragma pack, usually this is set of 1 so the compiler does not shuffle elements, increase size etc.

This data structure is shared with other tools. And the structure is well defined with reasonable alignment. It is compatible with all compilers we have tested. So pragma pack is not necessary.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

You can not have possibly tested all the configurations since the kernel supports many arches and compilers.

If the tested existing alignment is ok, pragma pack should be a noop on your tested configurations.

And help cover the untested configurations.

Got it. I will add pragma pack(1).

Please do not use pragma pack(), add __packed to the structs in question.

Ok, I will use __packed.

Thanks,
Lizhi

Quoted 2 lines

- Moritz

author
#20 lore raw

PATCH v3 4/18 fpga: xrt: xrt-lib platform driver manager

In reply to Lizhi Hou

xrt-lib kernel module infrastructure code to register and manage all leaf driver modules.

---
 drivers/fpga/xrt/lib/main.c | 274 ++++++++++++++++++++++++++++++++++++
 drivers/fpga/xrt/lib/main.h |  17 +++
 2 files changed, 291 insertions(+)
 create mode 100644 drivers/fpga/xrt/lib/main.c
 create mode 100644 drivers/fpga/xrt/lib/main.h
drivers/fpga/xrt/lib/main.c new file +274 -0
new file mode 100644
index 000000000000..36fb62710843
@@ -0,0 +1,274 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Xilinx Alveo FPGA Support
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *	Cheng Zhen <[email protected]>
+ */
+
+#include <linux/module.h>
+#include "xleaf.h"
+#include "xroot.h"
+#include "main.h"
+
+#define XRT_IPLIB_MODULE_NAME		"xrt-lib"
+#define XRT_IPLIB_MODULE_VERSION	"4.0.0"
+#define XRT_MAX_DEVICE_NODES		128
+#define XRT_DRVNAME(drv)		((drv)->driver.name)
+
+/*
+ * Subdev driver is known by ID to others. We map the ID to it's
+ * struct platform_driver, which contains it's binding name and driver/file ops.
+ * We also map it to the endpoint name in DTB as well, if it's different
+ * than the driver's binding name.
+ */
+struct xrt_drv_map {
+	struct list_head list;
+	enum xrt_subdev_id id;
+	struct platform_driver *drv;
+	struct xrt_subdev_endpoints *eps;
+	struct ida ida; /* manage driver instance and char dev minor */
+};
+
+static DEFINE_MUTEX(xrt_lib_lock); /* global lock protecting xrt_drv_maps list */
+static LIST_HEAD(xrt_drv_maps);
+struct class *xrt_class;
+
+static inline struct xrt_subdev_drvdata *
+xrt_drv_map2drvdata(struct xrt_drv_map *map)
+{
+	return (struct xrt_subdev_drvdata *)map->drv->id_table[0].driver_data;
+}
+
+static struct xrt_drv_map *
+xrt_drv_find_map_by_id_nolock(enum xrt_subdev_id id)
+{
+	const struct list_head *ptr;
+
+	list_for_each(ptr, &xrt_drv_maps) {
+		struct xrt_drv_map *tmap = list_entry(ptr, struct xrt_drv_map, list);
+
+		if (tmap->id == id)
+			return tmap;
+	}
+	return NULL;
+}
+
+static struct xrt_drv_map *
+xrt_drv_find_map_by_id(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);
+	map = xrt_drv_find_map_by_id_nolock(id);
+	mutex_unlock(&xrt_lib_lock);
+	/*
+	 * map should remain valid even after lock is dropped since a registered
+	 * driver should only be unregistered when driver module is being unloaded,
+	 * which means that the driver should not be used by then.
+	 */
+	return map;
+}
+
+static int xrt_drv_register_driver(struct xrt_drv_map *map)
+{
+	struct xrt_subdev_drvdata *drvdata;
+	int rc = 0;
+	const char *drvname = XRT_DRVNAME(map->drv);
+
+	rc = platform_driver_register(map->drv);
+	if (rc) {
+		pr_err("register %s platform driver failed\n", drvname);
+		return rc;
+	}
+
+	drvdata = xrt_drv_map2drvdata(map);
+	if (drvdata) {
+		/* Initialize dev_t for char dev node. */
+		if (xleaf_devnode_enabled(drvdata)) {
+			rc = alloc_chrdev_region(&drvdata->xsd_file_ops.xsf_dev_t, 0,
+						 XRT_MAX_DEVICE_NODES, drvname);
+			if (rc) {
+				platform_driver_unregister(map->drv);
+				pr_err("failed to alloc dev minor for %s: %d\n", drvname, rc);
+				return rc;
+			}
+		} else {
+			drvdata->xsd_file_ops.xsf_dev_t = (dev_t)-1;
+		}
+	}
+
+	ida_init(&map->ida);
+
+	pr_info("%s registered successfully\n", drvname);
+
+	return 0;
+}
+
+static void xrt_drv_unregister_driver(struct xrt_drv_map *map)
+{
+	const char *drvname = XRT_DRVNAME(map->drv);
+	struct xrt_subdev_drvdata *drvdata;
+
+	ida_destroy(&map->ida);
+
+	drvdata = xrt_drv_map2drvdata(map);
+	if (drvdata && drvdata->xsd_file_ops.xsf_dev_t != (dev_t)-1) {
+		unregister_chrdev_region(drvdata->xsd_file_ops.xsf_dev_t,
+					 XRT_MAX_DEVICE_NODES);
+	}
+
+	platform_driver_unregister(map->drv);
+
+	pr_info("%s unregistered successfully\n", drvname);
+}
+
+int xleaf_register_driver(enum xrt_subdev_id id,
+			  struct platform_driver *drv,
+			  struct xrt_subdev_endpoints *eps)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);
+
+	map = xrt_drv_find_map_by_id_nolock(id);
+	if (map) {
+		mutex_unlock(&xrt_lib_lock);
+		pr_err("Id %d already has a registered driver, 0x%p\n",
+		       id, map->drv);
+		return -EEXIST;
+	}
+
+	map = vzalloc(sizeof(*map));
+	if (!map) {
+		mutex_unlock(&xrt_lib_lock);
+		return -ENOMEM;
+	}
+	map->id = id;
+	map->drv = drv;
+	map->eps = eps;
+
+	xrt_drv_register_driver(map);
+
+	list_add(&map->list, &xrt_drv_maps);
+
+	mutex_unlock(&xrt_lib_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xleaf_register_driver);
+
+void xleaf_unregister_driver(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);
+
+	map = xrt_drv_find_map_by_id_nolock(id);
+	if (!map) {
+		mutex_unlock(&xrt_lib_lock);
+		pr_err("Id %d has no registered driver\n", id);
+		return;
+	}
+
+	list_del(&map->list);
+
+	mutex_unlock(&xrt_lib_lock);
+
+	xrt_drv_unregister_driver(map);
+	vfree(map);
+}
+EXPORT_SYMBOL_GPL(xleaf_unregister_driver);
+
+const char *xrt_drv_name(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	if (map)
+		return XRT_DRVNAME(map->drv);
+	return NULL;
+}
+
+int xrt_drv_get_instance(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	return ida_alloc_range(&map->ida, 0, XRT_MAX_DEVICE_NODES, GFP_KERNEL);
+}
+
+void xrt_drv_put_instance(enum xrt_subdev_id id, int instance)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	ida_free(&map->ida, instance);
+}
+
+struct xrt_subdev_endpoints *xrt_drv_get_endpoints(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+	struct xrt_subdev_endpoints *eps;
+
+	eps = map ? map->eps : NULL;
+	return eps;
+}
+
+/* Leaf driver's module init/fini callbacks. */
+static void (*leaf_init_fini_cbs[])(bool) = {
+	group_leaf_init_fini,
+	vsec_leaf_init_fini,
+	devctl_leaf_init_fini,
+	axigate_leaf_init_fini,
+	icap_leaf_init_fini,
+	calib_leaf_init_fini,
+	clkfreq_leaf_init_fini,
+	clock_leaf_init_fini,
+	ucs_leaf_init_fini,
+};
+
+static __init int xrt_lib_init(void)
+{
+	int i;
+
+	xrt_class = class_create(THIS_MODULE, XRT_IPLIB_MODULE_NAME);
+	if (IS_ERR(xrt_class))
+		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)
+{
+	struct xrt_drv_map *map;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(leaf_init_fini_cbs); i++)
+		leaf_init_fini_cbs[i](false);
+
+	mutex_lock(&xrt_lib_lock);
+
+	while (!list_empty(&xrt_drv_maps)) {
+		map = list_first_entry_or_null(&xrt_drv_maps, struct xrt_drv_map, list);
+		pr_err("Unloading module with %s still registered\n", XRT_DRVNAME(map->drv));
+		list_del(&map->list);
+		mutex_unlock(&xrt_lib_lock);
+		xrt_drv_unregister_driver(map);
+		vfree(map);
+		mutex_lock(&xrt_lib_lock);
+	}
+
+	mutex_unlock(&xrt_lib_lock);
+
+	class_destroy(xrt_class);
+}
+
+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/main.h new file +17 -0
new file mode 100644
index 000000000000..f3bfc87ee614
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *	Cheng Zhen <[email protected]>
+ */
+
+#ifndef _XRT_MAIN_H_
+#define _XRT_MAIN_H_
+
+const char *xrt_drv_name(enum xrt_subdev_id id);
+int xrt_drv_get_instance(enum xrt_subdev_id id);
+void xrt_drv_put_instance(enum xrt_subdev_id id, int instance);
+struct xrt_subdev_endpoints *xrt_drv_get_endpoints(enum xrt_subdev_id id);
+
+#endif	/* _XRT_MAIN_H_ */
Signature · 1 line
2.18.4
maintainer
#21 lore raw

Re: [PATCH V3 XRT Alveo 04/18] fpga: xrt: xrt-lib platform driver manager

Lizhi,

Quoted 60 lines · Lizhi Hou

xrt-lib kernel module infrastructure code to register and manage all leaf driver modules.

---
 drivers/fpga/xrt/lib/main.c | 274 ++++++++++++++++++++++++++++++++++++
 drivers/fpga/xrt/lib/main.h |  17 +++
 2 files changed, 291 insertions(+)
 create mode 100644 drivers/fpga/xrt/lib/main.c
 create mode 100644 drivers/fpga/xrt/lib/main.h
drivers/fpga/xrt/lib/main.c new file +41 -0
new file mode 100644
index 000000000000..36fb62710843
@@ -0,0 +1,274 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Xilinx Alveo FPGA Support
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *	Cheng Zhen <[email protected]>
+ */
+
+#include <linux/module.h>
+#include "xleaf.h"
+#include "xroot.h"
+#include "main.h"
+
+#define XRT_IPLIB_MODULE_NAME		"xrt-lib"
+#define XRT_IPLIB_MODULE_VERSION	"4.0.0"
+#define XRT_MAX_DEVICE_NODES		128
+#define XRT_DRVNAME(drv)		((drv)->driver.name)
+
+/*
+ * Subdev driver is known by ID to others. We map the ID to it's
+ * struct platform_driver, which contains it's binding name and driver/file ops.
+ * We also map it to the endpoint name in DTB as well, if it's different
+ * than the driver's binding name.
+ */
+struct xrt_drv_map {
+	struct list_head list;
+	enum xrt_subdev_id id;
+	struct platform_driver *drv;
+	struct xrt_subdev_endpoints *eps;
+	struct ida ida; /* manage driver instance and char dev minor */
+};
+
+static DEFINE_MUTEX(xrt_lib_lock); /* global lock protecting xrt_drv_maps list */
+static LIST_HEAD(xrt_drv_maps);
+struct class *xrt_class;
+
+static inline struct xrt_subdev_drvdata *
+xrt_drv_map2drvdata(struct xrt_drv_map *map)
+{

drivers/fpga/xrt/lib/main.c line 51

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+	return (struct xrt_subdev_drvdata *)map->drv->id_table[0].driver_data;
+}
+
+static struct xrt_drv_map *
+xrt_drv_find_map_by_id_nolock(enum xrt_subdev_id id)
+{
+	const struct list_head *ptr;
+
+	list_for_each(ptr, &xrt_drv_maps) {
+		struct xrt_drv_map *tmap = list_entry(ptr, struct xrt_drv_map, list);

list_for_each_entry, maybe?

Quoted 44 lines
drivers/fpga/xrt/lib/main.c new file +44 -0
+
+		if (tmap->id == id)
+			return tmap;
+	}
+	return NULL;
+}
+
+static struct xrt_drv_map *
+xrt_drv_find_map_by_id(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);
+	map = xrt_drv_find_map_by_id_nolock(id);
+	mutex_unlock(&xrt_lib_lock);
+	/*
+	 * map should remain valid even after lock is dropped since a registered
+	 * driver should only be unregistered when driver module is being unloaded,
+	 * which means that the driver should not be used by then.
+	 */
+	return map;
+}
+
+static int xrt_drv_register_driver(struct xrt_drv_map *map)
+{
+	struct xrt_subdev_drvdata *drvdata;
+	int rc = 0;
+	const char *drvname = XRT_DRVNAME(map->drv);
+
+	rc = platform_driver_register(map->drv);
+	if (rc) {
+		pr_err("register %s platform driver failed\n", drvname);
+		return rc;
+	}
+
+	drvdata = xrt_drv_map2drvdata(map);
+	if (drvdata) {
+		/* Initialize dev_t for char dev node. */
+		if (xleaf_devnode_enabled(drvdata)) {
+			rc = alloc_chrdev_region(&drvdata->xsd_file_ops.xsf_dev_t, 0,
+						 XRT_MAX_DEVICE_NODES, drvname);
+			if (rc) {
+				platform_driver_unregister(map->drv);
+				pr_err("failed to alloc dev minor for %s: %d\n", drvname, rc);

drivers/fpga/xrt/lib/main.c line 105

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+				return rc;
+			}
+		} else {
+			drvdata->xsd_file_ops.xsf_dev_t = (dev_t)-1;
+		}
+	}
+
+	ida_init(&map->ida);
+
+	pr_info("%s registered successfully\n", drvname);

Is this not xrt_info() then?

Quoted 195 lines
drivers/fpga/xrt/lib/main.c new file +169 -0
+
+	return 0;
+}
+
+static void xrt_drv_unregister_driver(struct xrt_drv_map *map)
+{
+	const char *drvname = XRT_DRVNAME(map->drv);
+	struct xrt_subdev_drvdata *drvdata;
+
+	ida_destroy(&map->ida);
+
+	drvdata = xrt_drv_map2drvdata(map);
+	if (drvdata && drvdata->xsd_file_ops.xsf_dev_t != (dev_t)-1) {
+		unregister_chrdev_region(drvdata->xsd_file_ops.xsf_dev_t,
+					 XRT_MAX_DEVICE_NODES);
+	}
+
+	platform_driver_unregister(map->drv);
+
+	pr_info("%s unregistered successfully\n", drvname);
+}
+
+int xleaf_register_driver(enum xrt_subdev_id id,
+			  struct platform_driver *drv,
+			  struct xrt_subdev_endpoints *eps)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);
+
+	map = xrt_drv_find_map_by_id_nolock(id);
+	if (map) {
+		mutex_unlock(&xrt_lib_lock);
+		pr_err("Id %d already has a registered driver, 0x%p\n",
+		       id, map->drv);
+		return -EEXIST;
+	}
+
+	map = vzalloc(sizeof(*map));
+	if (!map) {
+		mutex_unlock(&xrt_lib_lock);
+		return -ENOMEM;
+	}
+	map->id = id;
+	map->drv = drv;
+	map->eps = eps;
+
+	xrt_drv_register_driver(map);
+
+	list_add(&map->list, &xrt_drv_maps);
+
+	mutex_unlock(&xrt_lib_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xleaf_register_driver);
+
+void xleaf_unregister_driver(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);
+
+	map = xrt_drv_find_map_by_id_nolock(id);
+	if (!map) {
+		mutex_unlock(&xrt_lib_lock);
+		pr_err("Id %d has no registered driver\n", id);
+		return;
+	}
+
+	list_del(&map->list);
+
+	mutex_unlock(&xrt_lib_lock);
+
+	xrt_drv_unregister_driver(map);
+	vfree(map);
+}
+EXPORT_SYMBOL_GPL(xleaf_unregister_driver);
+
+const char *xrt_drv_name(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	if (map)
+		return XRT_DRVNAME(map->drv);
+	return NULL;
+}
+
+int xrt_drv_get_instance(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	return ida_alloc_range(&map->ida, 0, XRT_MAX_DEVICE_NODES, GFP_KERNEL);
+}
+
+void xrt_drv_put_instance(enum xrt_subdev_id id, int instance)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	ida_free(&map->ida, instance);
+}
+
+struct xrt_subdev_endpoints *xrt_drv_get_endpoints(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+	struct xrt_subdev_endpoints *eps;
+
+	eps = map ? map->eps : NULL;
+	return eps;
+}
+
+/* Leaf driver's module init/fini callbacks. */
+static void (*leaf_init_fini_cbs[])(bool) = {
+	group_leaf_init_fini,
+	vsec_leaf_init_fini,
+	devctl_leaf_init_fini,
+	axigate_leaf_init_fini,
+	icap_leaf_init_fini,
+	calib_leaf_init_fini,
+	clkfreq_leaf_init_fini,
+	clock_leaf_init_fini,
+	ucs_leaf_init_fini,
+};
+
+static __init int xrt_lib_init(void)
+{
+	int i;
+
+	xrt_class = class_create(THIS_MODULE, XRT_IPLIB_MODULE_NAME);
+	if (IS_ERR(xrt_class))
+		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)
+{
+	struct xrt_drv_map *map;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(leaf_init_fini_cbs); i++)
+		leaf_init_fini_cbs[i](false);
+
+	mutex_lock(&xrt_lib_lock);
+
+	while (!list_empty(&xrt_drv_maps)) {
+		map = list_first_entry_or_null(&xrt_drv_maps, struct xrt_drv_map, list);
+		pr_err("Unloading module with %s still registered\n", XRT_DRVNAME(map->drv));
+		list_del(&map->list);
+		mutex_unlock(&xrt_lib_lock);
+		xrt_drv_unregister_driver(map);
+		vfree(map);
+		mutex_lock(&xrt_lib_lock);
+	}
+
+	mutex_unlock(&xrt_lib_lock);
+
+	class_destroy(xrt_class);
+}
+
+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/main.h new file +17 -0
new file mode 100644
index 000000000000..f3bfc87ee614
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *	Cheng Zhen <[email protected]>
+ */
+
+#ifndef _XRT_MAIN_H_
+#define _XRT_MAIN_H_
+
+const char *xrt_drv_name(enum xrt_subdev_id id);
+int xrt_drv_get_instance(enum xrt_subdev_id id);
+void xrt_drv_put_instance(enum xrt_subdev_id id, int instance);
+struct xrt_subdev_endpoints *xrt_drv_get_endpoints(enum xrt_subdev_id id);
+
+#endif	/* _XRT_MAIN_H_ */
Signature · 1 line
2.18.4

This smells like it should be a bus to me...

- Moritz

#22 lore raw

Re: [PATCH V3 XRT Alveo 04/18] fpga: xrt: xrt-lib platform driver manager

Hi Moritz,

Quoted 69 lines · Moritz Fischer

CAUTION: This message has originated from an External Source. Please use proper judgment and caution when opening attachments, clicking links, or responding to this email.

Lizhi,

Quoted 63 lines · Lizhi Hou

xrt-lib kernel module infrastructure code to register and manage all leaf driver modules.

---
  drivers/fpga/xrt/lib/main.c | 274 ++++++++++++++++++++++++++++++++++++
  drivers/fpga/xrt/lib/main.h |  17 +++
  2 files changed, 291 insertions(+)

create mode 100644 drivers/fpga/xrt/lib/main.c
create mode 100644 drivers/fpga/xrt/lib/main.h

drivers/fpga/xrt/lib/main.c new file +44 -0
new file mode 100644
index 000000000000..36fb62710843
@@ -0,0 +1,274 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Xilinx Alveo FPGA Support
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *   Cheng Zhen <[email protected]>
+ */
+
+#include <linux/module.h>
+#include "xleaf.h"
+#include "xroot.h"
+#include "main.h"
+
+#define XRT_IPLIB_MODULE_NAME                "xrt-lib"
+#define XRT_IPLIB_MODULE_VERSION     "4.0.0"
+#define XRT_MAX_DEVICE_NODES         128
+#define XRT_DRVNAME(drv)             ((drv)->driver.name)
+
+/*
+ * Subdev driver is known by ID to others. We map the ID to it's
+ * struct platform_driver, which contains it's binding name and driver/file ops.
+ * We also map it to the endpoint name in DTB as well, if it's different
+ * than the driver's binding name.
+ */
+struct xrt_drv_map {
+     struct list_head list;
+     enum xrt_subdev_id id;
+     struct platform_driver *drv;
+     struct xrt_subdev_endpoints *eps;
+     struct ida ida; /* manage driver instance and char dev minor */
+};
+
+static DEFINE_MUTEX(xrt_lib_lock); /* global lock protecting xrt_drv_maps list */
+static LIST_HEAD(xrt_drv_maps);
+struct class *xrt_class;
+
+static inline struct xrt_subdev_drvdata *
+xrt_drv_map2drvdata(struct xrt_drv_map *map)
+{
+     return (struct xrt_subdev_drvdata *)map->drv->id_table[0].driver_data;
+}
+
Quoted 8 lines

drivers/fpga/xrt/lib/main.c line 51

Quoted 7 lines
drivers/fpga/xrt/lib/main.c new file +7 -0
+static struct xrt_drv_map *
+xrt_drv_find_map_by_id_nolock(enum xrt_subdev_id id)
+{
+     const struct list_head *ptr;
+
+     list_for_each(ptr, &xrt_drv_maps) {
+             struct xrt_drv_map *tmap = list_entry(ptr, struct xrt_drv_map, list);

list_for_each_entry, maybe?

Yes!

Quoted 47 lines
Quoted 47 lines
drivers/fpga/xrt/lib/main.c new file +47 -0
+
+             if (tmap->id == id)
+                     return tmap;
+     }
+     return NULL;
+}
+
+static struct xrt_drv_map *
+xrt_drv_find_map_by_id(enum xrt_subdev_id id)
+{
+     struct xrt_drv_map *map;
+
+     mutex_lock(&xrt_lib_lock);
+     map = xrt_drv_find_map_by_id_nolock(id);
+     mutex_unlock(&xrt_lib_lock);
+     /*
+      * map should remain valid even after lock is dropped since a registered
+      * driver should only be unregistered when driver module is being unloaded,
+      * which means that the driver should not be used by then.
+      */
+     return map;
+}
+
+static int xrt_drv_register_driver(struct xrt_drv_map *map)
+{
+     struct xrt_subdev_drvdata *drvdata;
+     int rc = 0;
+     const char *drvname = XRT_DRVNAME(map->drv);
+
+     rc = platform_driver_register(map->drv);
+     if (rc) {
+             pr_err("register %s platform driver failed\n", drvname);
+             return rc;
+     }
+
+     drvdata = xrt_drv_map2drvdata(map);
+     if (drvdata) {
+             /* Initialize dev_t for char dev node. */
+             if (xleaf_devnode_enabled(drvdata)) {
+                     rc = alloc_chrdev_region(&drvdata->xsd_file_ops.xsf_dev_t, 0,
+                                              XRT_MAX_DEVICE_NODES, drvname);
+                     if (rc) {
+                             platform_driver_unregister(map->drv);
+                             pr_err("failed to alloc dev minor for %s: %d\n", drvname, rc);
+                             return rc;
+                     }
+             } else {
Quoted 8 lines

drivers/fpga/xrt/lib/main.c line 105

Quoted 7 lines
drivers/fpga/xrt/lib/main.c new file +7 -0
+                     drvdata->xsd_file_ops.xsf_dev_t = (dev_t)-1;
+             }
+     }
+
+     ida_init(&map->ida);
+
+     pr_info("%s registered successfully\n", drvname);

Is this not xrt_info() then?

xrt_info() is meant for leaf driver to call where struct platform_device is available. We are initializing the drivers here, so can't call xrt_info().

Quoted 188 lines
Quoted 188 lines
drivers/fpga/xrt/lib/main.c new file +169 -0
+
+     return 0;
+}
+
+static void xrt_drv_unregister_driver(struct xrt_drv_map *map)
+{
+     const char *drvname = XRT_DRVNAME(map->drv);
+     struct xrt_subdev_drvdata *drvdata;
+
+     ida_destroy(&map->ida);
+
+     drvdata = xrt_drv_map2drvdata(map);
+     if (drvdata && drvdata->xsd_file_ops.xsf_dev_t != (dev_t)-1) {
+             unregister_chrdev_region(drvdata->xsd_file_ops.xsf_dev_t,
+                                      XRT_MAX_DEVICE_NODES);
+     }
+
+     platform_driver_unregister(map->drv);
+
+     pr_info("%s unregistered successfully\n", drvname);
+}
+
+int xleaf_register_driver(enum xrt_subdev_id id,
+                       struct platform_driver *drv,
+                       struct xrt_subdev_endpoints *eps)
+{
+     struct xrt_drv_map *map;
+
+     mutex_lock(&xrt_lib_lock);
+
+     map = xrt_drv_find_map_by_id_nolock(id);
+     if (map) {
+             mutex_unlock(&xrt_lib_lock);
+             pr_err("Id %d already has a registered driver, 0x%p\n",
+                    id, map->drv);
+             return -EEXIST;
+     }
+
+     map = vzalloc(sizeof(*map));
+     if (!map) {
+             mutex_unlock(&xrt_lib_lock);
+             return -ENOMEM;
+     }
+     map->id = id;
+     map->drv = drv;
+     map->eps = eps;
+
+     xrt_drv_register_driver(map);
+
+     list_add(&map->list, &xrt_drv_maps);
+
+     mutex_unlock(&xrt_lib_lock);
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(xleaf_register_driver);
+
+void xleaf_unregister_driver(enum xrt_subdev_id id)
+{
+     struct xrt_drv_map *map;
+
+     mutex_lock(&xrt_lib_lock);
+
+     map = xrt_drv_find_map_by_id_nolock(id);
+     if (!map) {
+             mutex_unlock(&xrt_lib_lock);
+             pr_err("Id %d has no registered driver\n", id);
+             return;
+     }
+
+     list_del(&map->list);
+
+     mutex_unlock(&xrt_lib_lock);
+
+     xrt_drv_unregister_driver(map);
+     vfree(map);
+}
+EXPORT_SYMBOL_GPL(xleaf_unregister_driver);
+
+const char *xrt_drv_name(enum xrt_subdev_id id)
+{
+     struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+     if (map)
+             return XRT_DRVNAME(map->drv);
+     return NULL;
+}
+
+int xrt_drv_get_instance(enum xrt_subdev_id id)
+{
+     struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+     return ida_alloc_range(&map->ida, 0, XRT_MAX_DEVICE_NODES, GFP_KERNEL);
+}
+
+void xrt_drv_put_instance(enum xrt_subdev_id id, int instance)
+{
+     struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+     ida_free(&map->ida, instance);
+}
+
+struct xrt_subdev_endpoints *xrt_drv_get_endpoints(enum xrt_subdev_id id)
+{
+     struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+     struct xrt_subdev_endpoints *eps;
+
+     eps = map ? map->eps : NULL;
+     return eps;
+}
+
+/* Leaf driver's module init/fini callbacks. */
+static void (*leaf_init_fini_cbs[])(bool) = {
+     group_leaf_init_fini,
+     vsec_leaf_init_fini,
+     devctl_leaf_init_fini,
+     axigate_leaf_init_fini,
+     icap_leaf_init_fini,
+     calib_leaf_init_fini,
+     clkfreq_leaf_init_fini,
+     clock_leaf_init_fini,
+     ucs_leaf_init_fini,
+};
+
+static __init int xrt_lib_init(void)
+{
+     int i;
+
+     xrt_class = class_create(THIS_MODULE, XRT_IPLIB_MODULE_NAME);
+     if (IS_ERR(xrt_class))
+             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)
+{
+     struct xrt_drv_map *map;
+     int i;
+
+     for (i = 0; i < ARRAY_SIZE(leaf_init_fini_cbs); i++)
+             leaf_init_fini_cbs[i](false);
+
+     mutex_lock(&xrt_lib_lock);
+
+     while (!list_empty(&xrt_drv_maps)) {
+             map = list_first_entry_or_null(&xrt_drv_maps, struct xrt_drv_map, list);
+             pr_err("Unloading module with %s still registered\n", XRT_DRVNAME(map->drv));
+             list_del(&map->list);
+             mutex_unlock(&xrt_lib_lock);
+             xrt_drv_unregister_driver(map);
+             vfree(map);
+             mutex_lock(&xrt_lib_lock);
+     }
+
+     mutex_unlock(&xrt_lib_lock);
+
+     class_destroy(xrt_class);
+}
+
+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/main.h new file +13 -0
new file mode 100644
index 000000000000..f3bfc87ee614
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *   Cheng Zhen <[email protected]>
+ */
+
+#ifndef _XRT_MAIN_H_
+#define _XRT_MAIN_H_
+
+const char *xrt_drv_name(enum xrt_subdev_id id);
+int xrt_drv_get_instance(enum xrt_subdev_id id);
Quoted 8 lines
Quoted 7 lines
drivers/fpga/xrt/lib/main.h new file +4 -1
+void xrt_drv_put_instance(enum xrt_subdev_id id, int instance);
+struct xrt_subdev_endpoints *xrt_drv_get_endpoints(enum xrt_subdev_id id);
+
+#endif       /* _XRT_MAIN_H_ */
--

2.18.4

This smells like it should be a bus to me...

This is discussed in another email thread. I will not add more comment here.

Thanks,

Max

Quoted 2 lines

- Moritz

reviewer
#23 lore raw

Re: [PATCH V3 XRT Alveo 04/18] fpga: xrt: xrt-lib platform driver manager

In reply to Lizhi Hou
Quoted 6 lines · Lizhi Hou

xrt-lib kernel module infrastructure code to register and manage all leaf driver modules.

Quoted 6 lines
---
 drivers/fpga/xrt/lib/main.c | 274 ++++++++++++++++++++++++++++++++++++
 drivers/fpga/xrt/lib/main.h |  17 +++
 2 files changed, 291 insertions(+)
 create mode 100644 drivers/fpga/xrt/lib/main.c
 create mode 100644 drivers/fpga/xrt/lib/main.h

Not sure if 'main' is a good base name for something going into a lib.

Quoted 19 lines
drivers/fpga/xrt/lib/main.c new file +12 -0
new file mode 100644
index 000000000000..36fb62710843
@@ -0,0 +1,274 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Xilinx Alveo FPGA Support
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *	Cheng Zhen <[email protected]>
+ */
+
+#include <linux/module.h>
+#include "xleaf.h"

drivers/fpga/xrt/lib/main.c line 22

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+#include "xroot.h"
+#include "main.h"
+
+#define XRT_IPLIB_MODULE_NAME		"xrt-lib"
+#define XRT_IPLIB_MODULE_VERSION	"4.0.0"
+#define XRT_MAX_DEVICE_NODES		128
+#define XRT_DRVNAME(drv)		((drv)->driver.name)
+
+/*
+ * Subdev driver is known by ID to others. We map the ID to it's

by it's ID

Quoted 14 lines
drivers/fpga/xrt/lib/main.c new file +14 -0
+ * struct platform_driver, which contains it's binding name and driver/file ops.
+ * We also map it to the endpoint name in DTB as well, if it's different
+ * than the driver's binding name.
+ */
+struct xrt_drv_map {
+	struct list_head list;
+	enum xrt_subdev_id id;
+	struct platform_driver *drv;
+	struct xrt_subdev_endpoints *eps;
+	struct ida ida; /* manage driver instance and char dev minor */
+};
+
+static DEFINE_MUTEX(xrt_lib_lock); /* global lock protecting xrt_drv_maps list */
+static LIST_HEAD(xrt_drv_maps);

drivers/fpga/xrt/lib/main.c line 46

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+struct class *xrt_class;
+
+static inline struct xrt_subdev_drvdata *
+xrt_drv_map2drvdata(struct xrt_drv_map *map)
+{
+	return (struct xrt_subdev_drvdata *)map->drv->id_table[0].driver_data;
+}
+
+static struct xrt_drv_map *
+xrt_drv_find_map_by_id_nolock(enum xrt_subdev_id id)

name could be by convention

__xrt_drv_find_map_id

Quoted 12 lines
drivers/fpga/xrt/lib/main.c new file +12 -0
+{
+	const struct list_head *ptr;
+
+	list_for_each(ptr, &xrt_drv_maps) {
+		struct xrt_drv_map *tmap = list_entry(ptr, struct xrt_drv_map, list);
+
+		if (tmap->id == id)
+			return tmap;
+	}
+	return NULL;
+}
+

drivers/fpga/xrt/lib/main.c line 68

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+static struct xrt_drv_map *
+xrt_drv_find_map_by_id(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);
+	map = xrt_drv_find_map_by_id_nolock(id);
+	mutex_unlock(&xrt_lib_lock);
+	/*
+	 * map should remain valid even after lock is dropped since a registered

even after the lock

Quoted 56 lines
drivers/fpga/xrt/lib/main.c new file +56 -0
+	 * driver should only be unregistered when driver module is being unloaded,
+	 * which means that the driver should not be used by then.
+	 */
+	return map;
+}
+
+static int xrt_drv_register_driver(struct xrt_drv_map *map)
+{
+	struct xrt_subdev_drvdata *drvdata;
+	int rc = 0;
+	const char *drvname = XRT_DRVNAME(map->drv);
+
+	rc = platform_driver_register(map->drv);
+	if (rc) {
+		pr_err("register %s platform driver failed\n", drvname);
+		return rc;
+	}
+
+	drvdata = xrt_drv_map2drvdata(map);
+	if (drvdata) {
+		/* Initialize dev_t for char dev node. */
+		if (xleaf_devnode_enabled(drvdata)) {
+			rc = alloc_chrdev_region(&drvdata->xsd_file_ops.xsf_dev_t, 0,
+						 XRT_MAX_DEVICE_NODES, drvname);
+			if (rc) {
+				platform_driver_unregister(map->drv);
+				pr_err("failed to alloc dev minor for %s: %d\n", drvname, rc);
+				return rc;
+			}
+		} else {
+			drvdata->xsd_file_ops.xsf_dev_t = (dev_t)-1;
+		}
+	}
+
+	ida_init(&map->ida);
+
+	pr_info("%s registered successfully\n", drvname);
+
+	return 0;
+}
+
+static void xrt_drv_unregister_driver(struct xrt_drv_map *map)
+{
+	const char *drvname = XRT_DRVNAME(map->drv);
+	struct xrt_subdev_drvdata *drvdata;
+
+	ida_destroy(&map->ida);
+
+	drvdata = xrt_drv_map2drvdata(map);
+	if (drvdata && drvdata->xsd_file_ops.xsf_dev_t != (dev_t)-1) {
+		unregister_chrdev_region(drvdata->xsd_file_ops.xsf_dev_t,
+					 XRT_MAX_DEVICE_NODES);
+	}
+
+	platform_driver_unregister(map->drv);
+

drivers/fpga/xrt/lib/main.c line 134

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+	pr_info("%s unregistered successfully\n", drvname);
+}
+
+int xleaf_register_driver(enum xrt_subdev_id id,
+			  struct platform_driver *drv,
+			  struct xrt_subdev_endpoints *eps)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);

Trying to minimize length of lock being held.

Could holding this lock be split or the alloc moved above ?

drivers/fpga/xrt/lib/main.c line 144

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+
+	map = xrt_drv_find_map_by_id_nolock(id);
+	if (map) {
+		mutex_unlock(&xrt_lib_lock);
+		pr_err("Id %d already has a registered driver, 0x%p\n",
+		       id, map->drv);
+		return -EEXIST;
+	}
+
+	map = vzalloc(sizeof(*map));

general issue

map is small, so kzalloc

drivers/fpga/xrt/lib/main.c line 153

Quoted 9 lines
drivers/fpga/xrt/lib/main.c new file +9 -0
+	if (!map) {
+		mutex_unlock(&xrt_lib_lock);
+		return -ENOMEM;
+	}
+	map->id = id;
+	map->drv = drv;
+	map->eps = eps;
+
+	xrt_drv_register_driver(map);

xrt_drv_register_driver failure is unhandled.

This is the only time xrt_drv_register_driver is called, consider expanding the function here and removing the call.

Quoted 54 lines
drivers/fpga/xrt/lib/main.c new file +54 -0
+
+	list_add(&map->list, &xrt_drv_maps);
+
+	mutex_unlock(&xrt_lib_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xleaf_register_driver);
+
+void xleaf_unregister_driver(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);
+
+	map = xrt_drv_find_map_by_id_nolock(id);
+	if (!map) {
+		mutex_unlock(&xrt_lib_lock);
+		pr_err("Id %d has no registered driver\n", id);
+		return;
+	}
+
+	list_del(&map->list);
+
+	mutex_unlock(&xrt_lib_lock);
+
+	xrt_drv_unregister_driver(map);
+	vfree(map);
+}
+EXPORT_SYMBOL_GPL(xleaf_unregister_driver);
+
+const char *xrt_drv_name(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	if (map)
+		return XRT_DRVNAME(map->drv);
+	return NULL;
+}
+
+int xrt_drv_get_instance(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	return ida_alloc_range(&map->ida, 0, XRT_MAX_DEVICE_NODES, GFP_KERNEL);
+}
+
+void xrt_drv_put_instance(enum xrt_subdev_id id, int instance)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	ida_free(&map->ida, instance);
+}
+

drivers/fpga/xrt/lib/main.c line 217

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+struct xrt_subdev_endpoints *xrt_drv_get_endpoints(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+	struct xrt_subdev_endpoints *eps;
+
+	eps = map ? map->eps : NULL;
+	return eps;
+}
+
+/* Leaf driver's module init/fini callbacks. */

These constructor/destructor calls needs to be more dynamic.

calls are made even if there are no subdevices to go with the id's.

Also this list can not grow.  How would a new id be added by a module ?

Quoted 65 lines
drivers/fpga/xrt/lib/main.c new file +57 -0
+static void (*leaf_init_fini_cbs[])(bool) = {
+	group_leaf_init_fini,
+	vsec_leaf_init_fini,
+	devctl_leaf_init_fini,
+	axigate_leaf_init_fini,
+	icap_leaf_init_fini,
+	calib_leaf_init_fini,
+	clkfreq_leaf_init_fini,
+	clock_leaf_init_fini,
+	ucs_leaf_init_fini,
+};
+
+static __init int xrt_lib_init(void)
+{
+	int i;
+
+	xrt_class = class_create(THIS_MODULE, XRT_IPLIB_MODULE_NAME);
+	if (IS_ERR(xrt_class))
+		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)
+{
+	struct xrt_drv_map *map;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(leaf_init_fini_cbs); i++)
+		leaf_init_fini_cbs[i](false);
+
+	mutex_lock(&xrt_lib_lock);
+
+	while (!list_empty(&xrt_drv_maps)) {
+		map = list_first_entry_or_null(&xrt_drv_maps, struct xrt_drv_map, list);
+		pr_err("Unloading module with %s still registered\n", XRT_DRVNAME(map->drv));
+		list_del(&map->list);
+		mutex_unlock(&xrt_lib_lock);
+		xrt_drv_unregister_driver(map);
+		vfree(map);
+		mutex_lock(&xrt_lib_lock);
+	}
+
+	mutex_unlock(&xrt_lib_lock);
+
+	class_destroy(xrt_class);
+}
+
+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/main.h new file +2 -0
new file mode 100644
index 000000000000..f3bfc87ee614
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*

drivers/fpga/xrt/lib/main.h line 12

Quoted 10 lines
drivers/fpga/xrt/lib/main.h new file +10 -0
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *	Cheng Zhen <[email protected]>
+ */
+
+#ifndef _XRT_MAIN_H_
+#define _XRT_MAIN_H_
+
+const char *xrt_drv_name(enum xrt_subdev_id id);

To be self contained, the header defining enum xrt_subdev_id should be included.

This is subdev_id.h which comes in with patch 6

A dependency on a future patch breaks bisectablity.

It may make sense to collect these small headers into a single large header for the ip infra lib and bring them all in this patch.

Tom

Quoted 5 lines
drivers/fpga/xrt/lib/main.h new file +5 -0
+int xrt_drv_get_instance(enum xrt_subdev_id id);
+void xrt_drv_put_instance(enum xrt_subdev_id id, int instance);
+struct xrt_subdev_endpoints *xrt_drv_get_endpoints(enum xrt_subdev_id id);
+
+#endif	/* _XRT_MAIN_H_ */
maintainer
#24 lore raw

Re: [PATCH V3 XRT Alveo 04/18] fpga: xrt: xrt-lib platform driver manager

Quoted 348 lines · Tom Rix
Quoted 6 lines · Lizhi Hou

xrt-lib kernel module infrastructure code to register and manage all leaf driver modules.

Quoted 6 lines
---
 drivers/fpga/xrt/lib/main.c | 274 ++++++++++++++++++++++++++++++++++++
 drivers/fpga/xrt/lib/main.h |  17 +++
 2 files changed, 291 insertions(+)
 create mode 100644 drivers/fpga/xrt/lib/main.c
 create mode 100644 drivers/fpga/xrt/lib/main.h

Not sure if 'main' is a good base name for something going into a lib.

Quoted 19 lines
drivers/fpga/xrt/lib/main.c new file +12 -0
new file mode 100644
index 000000000000..36fb62710843
@@ -0,0 +1,274 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Xilinx Alveo FPGA Support
+ *
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *	Cheng Zhen <[email protected]>
+ */
+
+#include <linux/module.h>
+#include "xleaf.h"

drivers/fpga/xrt/lib/main.c line 22

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+#include "xroot.h"
+#include "main.h"
+
+#define XRT_IPLIB_MODULE_NAME		"xrt-lib"
+#define XRT_IPLIB_MODULE_VERSION	"4.0.0"
+#define XRT_MAX_DEVICE_NODES		128
+#define XRT_DRVNAME(drv)		((drv)->driver.name)
+
+/*
+ * Subdev driver is known by ID to others. We map the ID to it's

by it's ID

Quoted 14 lines
drivers/fpga/xrt/lib/main.c new file +14 -0
+ * struct platform_driver, which contains it's binding name and driver/file ops.
+ * We also map it to the endpoint name in DTB as well, if it's different
+ * than the driver's binding name.
+ */
+struct xrt_drv_map {
+	struct list_head list;
+	enum xrt_subdev_id id;
+	struct platform_driver *drv;
+	struct xrt_subdev_endpoints *eps;
+	struct ida ida; /* manage driver instance and char dev minor */
+};
+
+static DEFINE_MUTEX(xrt_lib_lock); /* global lock protecting xrt_drv_maps list */
+static LIST_HEAD(xrt_drv_maps);

drivers/fpga/xrt/lib/main.c line 46

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+struct class *xrt_class;
+
+static inline struct xrt_subdev_drvdata *
+xrt_drv_map2drvdata(struct xrt_drv_map *map)
+{
+	return (struct xrt_subdev_drvdata *)map->drv->id_table[0].driver_data;
+}
+
+static struct xrt_drv_map *
+xrt_drv_find_map_by_id_nolock(enum xrt_subdev_id id)

name could be by convention

__xrt_drv_find_map_id

Quoted 12 lines
drivers/fpga/xrt/lib/main.c new file +12 -0
+{
+	const struct list_head *ptr;
+
+	list_for_each(ptr, &xrt_drv_maps) {
+		struct xrt_drv_map *tmap = list_entry(ptr, struct xrt_drv_map, list);
+
+		if (tmap->id == id)
+			return tmap;
+	}
+	return NULL;
+}
+

drivers/fpga/xrt/lib/main.c line 68

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+static struct xrt_drv_map *
+xrt_drv_find_map_by_id(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);
+	map = xrt_drv_find_map_by_id_nolock(id);
+	mutex_unlock(&xrt_lib_lock);
+	/*
+	 * map should remain valid even after lock is dropped since a registered

even after the lock

Quoted 56 lines
drivers/fpga/xrt/lib/main.c new file +56 -0
+	 * driver should only be unregistered when driver module is being unloaded,
+	 * which means that the driver should not be used by then.
+	 */
+	return map;
+}
+
+static int xrt_drv_register_driver(struct xrt_drv_map *map)
+{
+	struct xrt_subdev_drvdata *drvdata;
+	int rc = 0;
+	const char *drvname = XRT_DRVNAME(map->drv);
+
+	rc = platform_driver_register(map->drv);
+	if (rc) {
+		pr_err("register %s platform driver failed\n", drvname);
+		return rc;
+	}
+
+	drvdata = xrt_drv_map2drvdata(map);
+	if (drvdata) {
+		/* Initialize dev_t for char dev node. */
+		if (xleaf_devnode_enabled(drvdata)) {
+			rc = alloc_chrdev_region(&drvdata->xsd_file_ops.xsf_dev_t, 0,
+						 XRT_MAX_DEVICE_NODES, drvname);
+			if (rc) {
+				platform_driver_unregister(map->drv);
+				pr_err("failed to alloc dev minor for %s: %d\n", drvname, rc);
+				return rc;
+			}
+		} else {
+			drvdata->xsd_file_ops.xsf_dev_t = (dev_t)-1;
+		}
+	}
+
+	ida_init(&map->ida);
+
+	pr_info("%s registered successfully\n", drvname);
+
+	return 0;
+}
+
+static void xrt_drv_unregister_driver(struct xrt_drv_map *map)
+{
+	const char *drvname = XRT_DRVNAME(map->drv);
+	struct xrt_subdev_drvdata *drvdata;
+
+	ida_destroy(&map->ida);
+
+	drvdata = xrt_drv_map2drvdata(map);
+	if (drvdata && drvdata->xsd_file_ops.xsf_dev_t != (dev_t)-1) {
+		unregister_chrdev_region(drvdata->xsd_file_ops.xsf_dev_t,
+					 XRT_MAX_DEVICE_NODES);
+	}
+
+	platform_driver_unregister(map->drv);
+

drivers/fpga/xrt/lib/main.c line 134

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+	pr_info("%s unregistered successfully\n", drvname);
+}
+
+int xleaf_register_driver(enum xrt_subdev_id id,
+			  struct platform_driver *drv,
+			  struct xrt_subdev_endpoints *eps)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);

Trying to minimize length of lock being held.

Could holding this lock be split or the alloc moved above ?

drivers/fpga/xrt/lib/main.c line 144

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+
+	map = xrt_drv_find_map_by_id_nolock(id);
+	if (map) {
+		mutex_unlock(&xrt_lib_lock);
+		pr_err("Id %d already has a registered driver, 0x%p\n",
+		       id, map->drv);
+		return -EEXIST;
+	}
+
+	map = vzalloc(sizeof(*map));

general issue

map is small, so kzalloc

drivers/fpga/xrt/lib/main.c line 153

Quoted 9 lines
drivers/fpga/xrt/lib/main.c new file +9 -0
+	if (!map) {
+		mutex_unlock(&xrt_lib_lock);
+		return -ENOMEM;
+	}
+	map->id = id;
+	map->drv = drv;
+	map->eps = eps;
+
+	xrt_drv_register_driver(map);

xrt_drv_register_driver failure is unhandled.

This is the only time xrt_drv_register_driver is called, consider expanding the function here and removing the call.

Quoted 54 lines
drivers/fpga/xrt/lib/main.c new file +54 -0
+
+	list_add(&map->list, &xrt_drv_maps);
+
+	mutex_unlock(&xrt_lib_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xleaf_register_driver);
+
+void xleaf_unregister_driver(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map;
+
+	mutex_lock(&xrt_lib_lock);
+
+	map = xrt_drv_find_map_by_id_nolock(id);
+	if (!map) {
+		mutex_unlock(&xrt_lib_lock);
+		pr_err("Id %d has no registered driver\n", id);
+		return;
+	}
+
+	list_del(&map->list);
+
+	mutex_unlock(&xrt_lib_lock);
+
+	xrt_drv_unregister_driver(map);
+	vfree(map);
+}
+EXPORT_SYMBOL_GPL(xleaf_unregister_driver);
+
+const char *xrt_drv_name(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	if (map)
+		return XRT_DRVNAME(map->drv);
+	return NULL;
+}
+
+int xrt_drv_get_instance(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	return ida_alloc_range(&map->ida, 0, XRT_MAX_DEVICE_NODES, GFP_KERNEL);
+}
+
+void xrt_drv_put_instance(enum xrt_subdev_id id, int instance)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+
+	ida_free(&map->ida, instance);
+}
+

drivers/fpga/xrt/lib/main.c line 217

Quoted 10 lines
drivers/fpga/xrt/lib/main.c new file +10 -0
+struct xrt_subdev_endpoints *xrt_drv_get_endpoints(enum xrt_subdev_id id)
+{
+	struct xrt_drv_map *map = xrt_drv_find_map_by_id(id);
+	struct xrt_subdev_endpoints *eps;
+
+	eps = map ? map->eps : NULL;
+	return eps;
+}
+
+/* Leaf driver's module init/fini callbacks. */

These constructor/destructor calls needs to be more dynamic.

calls are made even if there are no subdevices to go with the id's.

Also this list can not grow.  How would a new id be added by a module ?

Quoted 65 lines
drivers/fpga/xrt/lib/main.c new file +57 -0
+static void (*leaf_init_fini_cbs[])(bool) = {
+	group_leaf_init_fini,
+	vsec_leaf_init_fini,
+	devctl_leaf_init_fini,
+	axigate_leaf_init_fini,
+	icap_leaf_init_fini,
+	calib_leaf_init_fini,
+	clkfreq_leaf_init_fini,
+	clock_leaf_init_fini,
+	ucs_leaf_init_fini,
+};
+
+static __init int xrt_lib_init(void)
+{
+	int i;
+
+	xrt_class = class_create(THIS_MODULE, XRT_IPLIB_MODULE_NAME);
+	if (IS_ERR(xrt_class))
+		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)
+{
+	struct xrt_drv_map *map;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(leaf_init_fini_cbs); i++)
+		leaf_init_fini_cbs[i](false);
+
+	mutex_lock(&xrt_lib_lock);
+
+	while (!list_empty(&xrt_drv_maps)) {
+		map = list_first_entry_or_null(&xrt_drv_maps, struct xrt_drv_map, list);
+		pr_err("Unloading module with %s still registered\n", XRT_DRVNAME(map->drv));
+		list_del(&map->list);
+		mutex_unlock(&xrt_lib_lock);
+		xrt_drv_unregister_driver(map);
+		vfree(map);
+		mutex_lock(&xrt_lib_lock);
+	}
+
+	mutex_unlock(&xrt_lib_lock);
+
+	class_destroy(xrt_class);
+}
+
+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/main.h new file +2 -0
new file mode 100644
index 000000000000..f3bfc87ee614
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*

drivers/fpga/xrt/lib/main.h line 12

Quoted 10 lines
drivers/fpga/xrt/lib/main.h new file +10 -0
+ * Copyright (C) 2020-2021 Xilinx, Inc.
+ *
+ * Authors:
+ *	Cheng Zhen <[email protected]>
+ */
+
+#ifndef _XRT_MAIN_H_
+#define _XRT_MAIN_H_
+
+const char *xrt_drv_name(enum xrt_subdev_id id);

To be self contained, the header defining enum xrt_subdev_id should be included.

Quoted 5 lines

This is subdev_id.h which comes in with patch 6

A dependency on a future patch breaks bisectablity.

It may make sense to collect these small headers into a single large header for the ip infra lib and bring them all in this patch.

Please add the headers when you use them, do *not* do header only commits.

It's perfectly fine (and desirable) to add things over time to a header as you use them.

Note *each* commit must compile and work standing on its own, so yes as Tom pointed out, do not depend on future (later commit) files.

Quoted 9 lines

Tom

drivers/fpga/xrt/lib/main.h line 17

Quoted 5 lines
drivers/fpga/xrt/lib/main.h new file +5 -0
+int xrt_drv_get_instance(enum xrt_subdev_id id);
+void xrt_drv_put_instance(enum xrt_subdev_id id, int instance);
+struct xrt_subdev_endpoints *xrt_drv_get_endpoints(enum xrt_subdev_id id);
+
+#endif	/* _XRT_MAIN_H_ */

- Moritz