Skip to contentSkip to conversation

Intel FPGA Device Drivers

PATCH Superseded Wu Hao 7 participants 94 messages Jun 26, 2017 – Sep 28, 2017 Page 1 of 3

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wu Hao

author
#0 lore raw

v2 cover Intel FPGA Device Drivers

Hi All,

Here is v2 patch-series adding drivers for Intel FPGA devices.

The Intel FPGA driver provides interfaces for userspace applications to configure, enumerate, open, and access FPGA accelerators on platforms equipped with Intel(R) PCIe based FPGA solutions and enables system level management functions such as FPGA partial reconfiguration, power management and virtualization.

This patch series only adds the basic functions for FPGA accelerators and partial reconfiguration. Patches for more functions, e.g power management and virtualization, will be submitted after this series gets reviewed.

This patch series depends on Alan Tull's patch set v2 on FPGA region support w/o device tree (see [1]).

Patch 1: add a document for Intel FPGA driver overview, including the HW architecture, driver organization, device enumeration, virtualization and opens.

Patch 2: introduce a fpga-dev class. It's used in below Intel FPGA PCIe device driver, to represent a FPGA device on the system, and all actual feature devices should be registered as child nodes of this container fpga-dev device.

Patch 3: remove OF dependency for fpga bridge. allow fpga bridge class to be used in non device tree case.

Patch 4: add region_id for fpga_image_info data structure, which allows driver to pass region id information to fpga-mgr for FPGA reconfiguration function.

Patch 5: add a 'status' sysfs interface to fpga-mgr class, it reflects the status of the fpga-mgr including reconfiguration errors.

Patch 6-10: implement Intel FPGA PCIe device driver. It walks through the 'Device Feature List' in the PCI Bar, creates the container fpga-dev as parent and platform devices as children for the feature devices it found.

Patch 11-14: implement Intel FPGA Management Engine (FME) driver. It's a platform driver matching with the FME platform device created by above PCIe driver. Sysfs and device file ioctls are exposed as user interfaces to allow partial reconfiguration to Accelerated Function Units (AFUs) from user space applications.

Patch 15-17: implement FPGA manager/bridge/region platform drivers for Intel FPGA Management Engine (FME). These platform drivers match with platform devices created by above FME driver, they use the generic fpga-mgr/bridge/region class infrastructure to implement FPGA partial reconfiguration function.

Patch 18-22: implement Intel FPGA Accelerated Function Unit (AFU) driver. It's a platform driver matching with AFU platform device created by above PCIe driver. It provides user interfaces to expose the AFU MMIO region, map/unmap dma buffer, and control the port which AFU connects to.

Changes from v1:

- Use GPLv2 license instead of Dual BSD/GPL.
- Move the code to drivers/fpga folder.
- Update the intel-fpga.txt documentation for new driver organization.
- Add documentation for new sysfs interfacesa.
- Switch to use common fpga-region interface for partial reconfiguration (PR) function in FME. It creates fpga-region/fpga-mgr/fpga-bridge platform devices and leave the implementation to their platform drivers.
- Add platform drivers for FME fpga-mgr/bridge/region platform devices.
- Fix kbuild warnings, typos and other comments.

[1] http://marc.info/?l=linux-kernel&m=149269778606030&w=2

Kang Luwei (3):
fpga: intel: add FPGA Management Engine driver basic framework
fpga: intel: fme: add header sub feature support
fpga: intel: fme: add partial reconfiguration sub feature support

Wu Hao (14):
docs: fpga: add a document for Intel FPGA driver overview
fpga: add FPGA device framework
fpga: bridge: remove OF dependency for fpga-bridge
fpga: mgr: add region_id to fpga_image_info
fpga: mgr: add status for fpga-mgr
fpga: intel: pcie: adds fpga_for_each_port callback for fme device
fpga: intel: fme: add FPGA_GET_API_VERSION/CHECK_EXTENSION ioctls
support
fpga: intel: add fpga manager platform driver for FME
fpga: intel: add fpga bridge platform driver for FME
fpga: intel: add fpga region platform driver for FME
fpga: intel: add FPGA Accelerated Function Unit driver basic framework
fpga: intel: afu: add header sub feature support
fpga: intel: afu add FPGA_GET_API_VERSION/CHECK_EXTENSION ioctls
support
fpga: intel: afu: add FPGA_PORT_DMA_MAP/UNMAP ioctls support

Xiao Guangrong (4):
fpga: intel: pcie: parse feature list and create platform device for
features.
fpga: intel: pcie: add chardev support for feature devices
fpga: intel: add feature device infrastructure
fpga: intel: afu: add user afu sub feature support

Zhang Yi (1):
fpga: intel: add FPGA PCIe device driver

 Documentation/ABI/testing/sysfs-class-fpga-dev     |    5 +
 Documentation/ABI/testing/sysfs-class-fpga-manager |   10 +
 .../ABI/testing/sysfs-platform-intel-fpga-afu      |   16 +
 .../ABI/testing/sysfs-platform-intel-fpga-fme      |   19 +
 .../ABI/testing/sysfs-platform-intel-fpga-fme-mgr  |    8 +
 Documentation/fpga/intel-fpga.txt                  |  256 +++++
 Documentation/ioctl/ioctl-number.txt               |    1 +
 drivers/fpga/Kconfig                               |   73 +-
 drivers/fpga/Makefile                              |   16 +
 drivers/fpga/fpga-dev.c                            |  118 +++
 drivers/fpga/fpga-mgr.c                            |   24 +
 drivers/fpga/intel-afu-dma-region.c                |  372 ++++++++
 drivers/fpga/intel-afu-main.c                      |  469 +++++++++
 drivers/fpga/intel-afu-region.c                    |  127 +++
 drivers/fpga/intel-afu.h                           |   70 ++
 drivers/fpga/intel-feature-dev.c                   |  272 ++++++
 drivers/fpga/intel-feature-dev.h                   |  515 ++++++++++
 drivers/fpga/intel-fme-main.c                      |  265 ++++++
 drivers/fpga/intel-fme-pr.c                        |  494 ++++++++++
 drivers/fpga/intel-fme.h                           |   57 ++
 drivers/fpga/intel-fpga-fme-br.c                   |   77 ++
 drivers/fpga/intel-fpga-fme-mgr.c                  |  307 ++++++
 drivers/fpga/intel-fpga-fme-region.c               |   91 ++
 drivers/fpga/intel-pcie.c                          | 1003 ++++++++++++++++++++
 include/linux/fpga/fpga-dev.h                      |   31 +
 include/linux/fpga/fpga-mgr.h                      |   10 +
 include/uapi/linux/intel-fpga.h                    |  175 ++++
 27 files changed, 4880 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-dev
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-afu
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-fme
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-fme-mgr
 create mode 100644 Documentation/fpga/intel-fpga.txt
 create mode 100644 drivers/fpga/fpga-dev.c
 create mode 100644 drivers/fpga/intel-afu-dma-region.c
 create mode 100644 drivers/fpga/intel-afu-main.c
 create mode 100644 drivers/fpga/intel-afu-region.c
 create mode 100644 drivers/fpga/intel-afu.h
 create mode 100644 drivers/fpga/intel-feature-dev.c
 create mode 100644 drivers/fpga/intel-feature-dev.h
 create mode 100644 drivers/fpga/intel-fme-main.c
 create mode 100644 drivers/fpga/intel-fme-pr.c
 create mode 100644 drivers/fpga/intel-fme.h
 create mode 100644 drivers/fpga/intel-fpga-fme-br.c
 create mode 100644 drivers/fpga/intel-fpga-fme-mgr.c
 create mode 100644 drivers/fpga/intel-fpga-fme-region.c
 create mode 100644 drivers/fpga/intel-pcie.c
 create mode 100644 include/linux/fpga/fpga-dev.h
 create mode 100644 include/uapi/linux/intel-fpga.h
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH v2 1/22 docs: fpga: add a document for Intel FPGA driver overview

Add a document for Intel FPGA driver overview.

----
v2: added FME fpga-mgr/bridge/region platform driver to driver organization.
updated open discussion per current implementation.
fixed some typos.

---
 Documentation/fpga/intel-fpga.txt | 256 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 256 insertions(+)
 create mode 100644 Documentation/fpga/intel-fpga.txt
Documentation/fpga/intel-fpga.txt new file +256 -0
new file mode 100644
index 0000000..4a29470
@@ -0,0 +1,256 @@
+===============================================================================
+                    Intel FPGA driver Overview
+-------------------------------------------------------------------------------
+                Enno Luebbers <[email protected]>
+                Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>
+                Wu Hao <[email protected]>
+
+The Intel FPGA driver provides interfaces for userspace applications to
+configure, enumerate, open, and access FPGA accelerators on platforms equipped
+with Intel(R) FPGA PCIe based solutions and enables system level management
+functions such as FPGA reconfiguration, power management, and virtualization.
+
+HW Architecture
+===============
+From the OS's point of view, the FPGA hardware appears as a regular PCIe device.
+The FPGA device memory is organized using a predefined data structure (Device
+Feature List). Features supported by the particular FPGA device are exposed
+through these data structures, as illustrated below:
+
+  +-------------------------------+  +-------------+
+  |              PF               |  |     VF      |
+  +-------------------------------+  +-------------+
+      ^            ^         ^              ^
+      |            |         |              |
++-----|------------|---------|--------------|-------+
+|     |            |         |              |       |
+|  +-----+     +-------+ +-------+      +-------+   |
+|  | FME |     | Port0 | | Port1 |      | Port2 |   |
+|  +-----+     +-------+ +-------+      +-------+   |
+|                  ^         ^              ^       |
+|                  |         |              |       |
+|              +-------+ +------+       +-------+   |
+|              |  AFU  | |  AFU |       |  AFU  |   |
+|              +-------+ +------+       +-------+   |
+|                                                   |
+|                 FPGA PCIe Device                  |
++---------------------------------------------------+
+
+The driver supports PCIe SR-IOV to create virtual functions (VFs) which can be
+used to assign individual accelerators to virtual machines.
+
+FME (FPGA Management Engine)
+============================
+The FPGA Management Engine performs power and thermal management, error
+reporting, reconfiguration, performance reporting, and other infrastructure
+functions. Each FPGA has one FME, which is always accessed through the physical
+function (PF).
+
+User-space applications can acquire exclusive access to the FME using open(),
+and release it using close().
+
+The following functions are exposed through ioctls:
+
+	Get driver API version (FPGA_GET_API_VERSION)
+	Check for extensions (FPGA_CHECK_EXTENSION)
+	Assign port to PF (FPGA_FME_PORT_ASSIGN)
+	Release port from PF (FPGA_FME_PORT_RELEASE)
+	Program bitstream (FPGA_FME_PORT_PR)
+
+More functions are exposed through sysfs
+(/sys/class/fpga/fpga.n/intel-fpga-fme.n/):
+
+	Read bitstream ID (bitstream_id)
+	Read bitstream metadata (bitstream_metadata)
+	Read number of ports (ports_num)
+	Read socket ID (socket_id)
+	Read performance counters (perf/)
+	Power management (power_mgmt/)
+	Thermal management (thermal_mgmt/)
+	Error reporting (errors/)
+
+PORT
+====
+A port represents the interface between the static FPGA fabric (the "blue
+bitstream") and a partially reconfigurable region containing an AFU (the "green
+bitstream"). It controls the communication from SW to the accelerator and
+exposes features such as reset and debug.
+
+A PCIe device may have several ports and each port can be released from PF by
+FPGA_FME_PORT_RELEASE ioctl on FME, and exposed through a VF via PCIe sriov
+sysfs interface.
+
+AFU
+===
+An AFU is attached to a port and exposes a 256k MMIO region to be used for
+accelerator-specific control registers.
+
+User-space applications can acquire exclusive access to an AFU attached to a
+port by using open() on the port device node, and release it using close().
+
+The following functions are exposed through ioctls:
+
+	Get driver API version (FPGA_GET_API_VERSION)
+	Check for extensions (FPGA_CHECK_EXTENSION)
+	Get port info (FPGA_PORT_GET_INFO)
+	Get MMIO region info (FPGA_PORT_GET_REGION_INFO)
+	Map DMA buffer (FPGA_PORT_DMA_MAP)
+	Unmap DMA buffer (FPGA_PORT_DMA_UNMAP)
+	Reset AFU (FPGA_PORT_RESET)
+	Enable UMsg (FPGA_PORT_UMSG_ENABLE)
+	Disable UMsg (FPGA_PORT_UMSG_DISABLE)
+	Set UMsg mode (FPGA_PORT_UMSG_SET_MODE)
+	Set UMsg base address (FPGA_PORT_UMSG_SET_BASE_ADDR)
+
+User-space applications can also mmap() accelerator MMIO regions.
+
+More functions are exposed through sysfs:
+(/sys/class/fpga/fpga.n/intel-fpga-port.m/):
+
+	Read Accelerator GUID (afu_id)
+	Error reporting (errors/)
+
+Partial Reconfiguration
+=======================
+As mentioned above, accelerators can be reconfigured through partial
+reconfiguration of a green bitstream file (GBS). The green bitstream must have
+been generated for the exact blue bitstream and targeted reconfigurable region
+(port) of the FPGA; otherwise, the reconfiguration operation will fail and
+possibly cause system instability. This compatibility can be checked by
+comparing the interface ID noted in the GBS header against the interface ID
+exposed by the FME through sysfs (see above). This check is usually done by
+user-space before calling the reconfiguration IOCTL.
+
+FPGA virtualization
+===================
+To enable accessing an accelerator from applications running in a VM, the
+respective AFU's port needs to be assigned to a VF using the following steps:
+
+ a) The PF owns all AFU ports by default. Any port that needs to be reassigned
+ to a VF must be released from PF firstly through the FPGA_FME_PORT_RELEASE
+ ioctl on the FME device.
+
+ b) Once N ports are released from PF, then user can use below command to
+ enable SRIOV and VFs. Each VF owns only one Port with AFU.
+
+ echo N > $PCI_DEVICE_PATH/sriov_numvfs
+
+ c) Pass through the VFs to VMs
+
+ d) The AFU under VF is accessible from applications in VM (using the same
+ driver inside the VF).
+
+Note the an FME can't be assigned to a VF, thus PR and other management
+functions are only available via the PF.
+
+
+Driver organization
+===================
+
+  +-------++------++------+             |
+  | FME   || FME  || FME  |             |
+  | FPGA  || FPGA || FPGA |             |
+  |Manager||Bridge||Region|             |
+  +-------++------++------+             |
+  +-----------------------+  +--------+ |             +--------+
+  |          FME          |  |  AFU   | |             |  AFU   |
+  |         Module        |  | Module | |             | Module |
+  +-----------------------+  +--------+ |             +--------+
+        +-----------------------+       |      +-----------------------+
+        | FPGA Container Device |       |      | FPGA Container Device |
+        +-----------------------+       |      +-----------------------+
+          +------------------+          |         +------------------+
+          | FPGA PCIE Module |          | Virtual | FPGA PCIE Module |
+          +------------------+   Host   | Machine +------------------+
+ -------------------------------------- | ------------------------------
+           +---------------+            |          +---------------+
+           | PCI PF Device |            |          | PCI VF Device |
+           +---------------+            |          +---------------+
+
+The FPGA devices appear as regular PCIe devices; thus, the FPGA PCIe device
+driver is always loaded first once a FPGA PCIE PF or VF device is detected. This
+driver plays an infrastructural role in the driver architecture.  It:
+
+	a) creates FPGA container device as parent of the feature devices.
+	b) walks through the Device Feature List, which is implemented in PCIE
+	   device BAR memory, to discover feature devices and their sub features
+	   and create platform device for them under the container device.
+	c) supports SRIOV.
+	d) introduces the feature device infrastructure, which abstracts
+	   operations for sub features and exposes common functions to feature
+	   device drivers.
+
+The FPGA Management Engine (FME) driver is a platform driver which is loaded
+automatically after FME platform device creation from the PCIE driver. It
+provides the key features for FPGA management, including:
+
+	a) Power and thermal management, error reporting, performance reporting
+	   and other infrastructure functions. Users can access these functions
+	   via sysfs interfaces exposed by FME driver.
+	b) Partial Reconfiguration. The FME driver creates platform devices
+	   for FPGA manager, FPGA bridges and FPGA regions during PR sub
+	   feature initialization; Once it receives an FPGA_FME_PORT_PR ioctl
+	   from user, it invokes the common interface function from FPGA Region
+	   to complete the partial reconfiguration of the bitstream to the given
+	   port.
+	c) Port management for virtualization. The FME driver introduces two
+	   ioctls, FPGA_FME_PORT_RELEASE (releases given port from PF) and
+	   FPGA_FME_PORT_ASSIGN (assigns the port back to PF). Once the port is
+	   released from the PF, it can be assigned to the VF through the SRIOV
+	   interfaces provided by PCIE driver. (Refer to "FPGA virtualization"
+	   for more details).
+
+Similar to the the FME driver, the FPGA Accelerated Function Unit (AFU) driver
+is probed once the AFU platform device is created. The main function of this
+module is to provide an interface for userspace applications to access the
+individual accelerators, including basic reset control on port, AFU MMIO region
+export, dma buffer mapping service, UMsg notification, and remote debug
+functions (see above).
+
+
+Device enumeration
+==================
+This section introduces how applications enumerate the fpga device from
+the sysfs hierarchy under /sys/class/fpga.
+
+In the example below, two Intel(R) FPGA devices are installed in the host. Each
+fpga device has one FME and two ports (AFUs).
+
+For each FPGA device, a device director is created under /sys/class/fpga/:
+
+	/sys/class/fpga/fpga.0
+	/sys/class/fpga/fpga.1
+
+The Intel(R) FPGA device driver exposes "intel-fpga-dev" as the FPGA's name.
+Application can retrieve name information via the sysfs interface:
+
+	/sys/class/fpga/fpga.0/name
+
+Each node has one FME and two ports (AFUs) as child devices:
+
+	/sys/class/fpga/fpga.0/intel-fpga-fme.0
+	/sys/class/fpga/fpga.0/intel-fpga-port.0
+	/sys/class/fpga/fpga.0/intel-fpga-port.1
+
+	/sys/class/fpga/fpga.1/intel-fpga-fme.1
+	/sys/class/fpga/fpga.1/intel-fpga-port.2
+	/sys/class/fpga/fpga.1/intel-fpga-port.3
+
+In general, the FME/AFU sysfs interfaces are named as follows:
+
+	/sys/class/fpga/<fpga.n>/<intel-fpga-fme.n>/
+	/sys/class/fpga/<fpga.n>/<intel-fpga-port.m>/
+
+with 'n' consecutively numbering all FMEs and 'm' consecutively numbering all
+ports.
+
+The device nodes used for ioctl() or mmap() can be referenced through:
+
+	/sys/class/fpga/<fpga.n>/<intel-fpga-port.n>/dev
+	/sys/class/fpga/<fpga.n>/<intel-fpga-fme.n>/dev
+
+Open discussion
+===============
+FME driver exports one ioctl (FPGA_FME_PORT_PR) for partial reconfiguration to
+user now. In the future, if unified user interfaces for reconfiguration are
+added, FME driver should switch to them from ioctl interface.
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH v2 01/22] docs: fpga: add a document for Intel FPGA driver overview

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

Quoted 69 lines

Add a document for Intel FPGA driver overview.

----
v2: added FME fpga-mgr/bridge/region platform driver to driver organization.
updated open discussion per current implementation.
fixed some typos.

---
 Documentation/fpga/intel-fpga.txt | 256 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 256 insertions(+)
 create mode 100644 Documentation/fpga/intel-fpga.txt
Documentation/fpga/intel-fpga.txt new file +49 -0
new file mode 100644
index 0000000..4a29470
@@ -0,0 +1,256 @@
+===============================================================================
+                    Intel FPGA driver Overview
+-------------------------------------------------------------------------------
+                Enno Luebbers <[email protected]>
+                Xiao Guangrong <[email protected]>
+                Wu Hao <[email protected]>
+
+The Intel FPGA driver provides interfaces for userspace applications to
+configure, enumerate, open, and access FPGA accelerators on platforms equipped
+with Intel(R) FPGA PCIe based solutions and enables system level management
+functions such as FPGA reconfiguration, power management, and virtualization.
+
+HW Architecture
+===============
+From the OS's point of view, the FPGA hardware appears as a regular PCIe device.
+The FPGA device memory is organized using a predefined data structure (Device
+Feature List). Features supported by the particular FPGA device are exposed
+through these data structures, as illustrated below:
+
+  +-------------------------------+  +-------------+
+  |              PF               |  |     VF      |
+  +-------------------------------+  +-------------+
+      ^            ^         ^              ^
+      |            |         |              |
++-----|------------|---------|--------------|-------+
+|     |            |         |              |       |
+|  +-----+     +-------+ +-------+      +-------+   |
+|  | FME |     | Port0 | | Port1 |      | Port2 |   |
+|  +-----+     +-------+ +-------+      +-------+   |
+|                  ^         ^              ^       |
+|                  |         |              |       |
+|              +-------+ +------+       +-------+   |
+|              |  AFU  | |  AFU |       |  AFU  |   |
+|              +-------+ +------+       +-------+   |
+|                                                   |
+|                 FPGA PCIe Device                  |
++---------------------------------------------------+
+
+The driver supports PCIe SR-IOV to create virtual functions (VFs) which can be
+used to assign individual accelerators to virtual machines.
+
+FME (FPGA Management Engine)
+============================
+The FPGA Management Engine performs power and thermal management, error
+reporting, reconfiguration, performance reporting, and other infrastructure
+functions. Each FPGA has one FME, which is always accessed through the physical
+function (PF).
+
+User-space applications can acquire exclusive access to the FME using open(),

Documentation/fpga/intel-fpga.txt line 59

Quoted 10 lines
Documentation/fpga/intel-fpga.txt new file +10 -0
+and release it using close().
+
+The following functions are exposed through ioctls:
+
+       Get driver API version (FPGA_GET_API_VERSION)
+       Check for extensions (FPGA_CHECK_EXTENSION)
+       Assign port to PF (FPGA_FME_PORT_ASSIGN)
+       Release port from PF (FPGA_FME_PORT_RELEASE)
+       Program bitstream (FPGA_FME_PORT_PR)
+

I was hoping the API mailing list might have an opinion about this, but I think adding ioctls to the kernel is discouraged. Could these be sysfs?

Quoted 187 lines
Documentation/fpga/intel-fpga.txt new file +187 -0
+More functions are exposed through sysfs
+(/sys/class/fpga/fpga.n/intel-fpga-fme.n/):
+
+       Read bitstream ID (bitstream_id)
+       Read bitstream metadata (bitstream_metadata)
+       Read number of ports (ports_num)
+       Read socket ID (socket_id)
+       Read performance counters (perf/)
+       Power management (power_mgmt/)
+       Thermal management (thermal_mgmt/)
+       Error reporting (errors/)
+
+PORT
+====
+A port represents the interface between the static FPGA fabric (the "blue
+bitstream") and a partially reconfigurable region containing an AFU (the "green
+bitstream"). It controls the communication from SW to the accelerator and
+exposes features such as reset and debug.
+
+A PCIe device may have several ports and each port can be released from PF by
+FPGA_FME_PORT_RELEASE ioctl on FME, and exposed through a VF via PCIe sriov
+sysfs interface.
+
+AFU
+===
+An AFU is attached to a port and exposes a 256k MMIO region to be used for
+accelerator-specific control registers.
+
+User-space applications can acquire exclusive access to an AFU attached to a
+port by using open() on the port device node, and release it using close().
+
+The following functions are exposed through ioctls:
+
+       Get driver API version (FPGA_GET_API_VERSION)
+       Check for extensions (FPGA_CHECK_EXTENSION)
+       Get port info (FPGA_PORT_GET_INFO)
+       Get MMIO region info (FPGA_PORT_GET_REGION_INFO)
+       Map DMA buffer (FPGA_PORT_DMA_MAP)
+       Unmap DMA buffer (FPGA_PORT_DMA_UNMAP)
+       Reset AFU (FPGA_PORT_RESET)
+       Enable UMsg (FPGA_PORT_UMSG_ENABLE)
+       Disable UMsg (FPGA_PORT_UMSG_DISABLE)
+       Set UMsg mode (FPGA_PORT_UMSG_SET_MODE)
+       Set UMsg base address (FPGA_PORT_UMSG_SET_BASE_ADDR)
+
+User-space applications can also mmap() accelerator MMIO regions.
+
+More functions are exposed through sysfs:
+(/sys/class/fpga/fpga.n/intel-fpga-port.m/):
+
+       Read Accelerator GUID (afu_id)
+       Error reporting (errors/)
+
+Partial Reconfiguration
+=======================
+As mentioned above, accelerators can be reconfigured through partial
+reconfiguration of a green bitstream file (GBS). The green bitstream must have
+been generated for the exact blue bitstream and targeted reconfigurable region
+(port) of the FPGA; otherwise, the reconfiguration operation will fail and
+possibly cause system instability. This compatibility can be checked by
+comparing the interface ID noted in the GBS header against the interface ID
+exposed by the FME through sysfs (see above). This check is usually done by
+user-space before calling the reconfiguration IOCTL.
+
+FPGA virtualization
+===================
+To enable accessing an accelerator from applications running in a VM, the
+respective AFU's port needs to be assigned to a VF using the following steps:
+
+ a) The PF owns all AFU ports by default. Any port that needs to be reassigned
+ to a VF must be released from PF firstly through the FPGA_FME_PORT_RELEASE
+ ioctl on the FME device.
+
+ b) Once N ports are released from PF, then user can use below command to
+ enable SRIOV and VFs. Each VF owns only one Port with AFU.
+
+ echo N > $PCI_DEVICE_PATH/sriov_numvfs
+
+ c) Pass through the VFs to VMs
+
+ d) The AFU under VF is accessible from applications in VM (using the same
+ driver inside the VF).
+
+Note the an FME can't be assigned to a VF, thus PR and other management
+functions are only available via the PF.
+
+
+Driver organization
+===================
+
+  +-------++------++------+             |
+  | FME   || FME  || FME  |             |
+  | FPGA  || FPGA || FPGA |             |
+  |Manager||Bridge||Region|             |
+  +-------++------++------+             |
+  +-----------------------+  +--------+ |             +--------+
+  |          FME          |  |  AFU   | |             |  AFU   |
+  |         Module        |  | Module | |             | Module |
+  +-----------------------+  +--------+ |             +--------+
+        +-----------------------+       |      +-----------------------+
+        | FPGA Container Device |       |      | FPGA Container Device |
+        +-----------------------+       |      +-----------------------+
+          +------------------+          |         +------------------+
+          | FPGA PCIE Module |          | Virtual | FPGA PCIE Module |
+          +------------------+   Host   | Machine +------------------+
+ -------------------------------------- | ------------------------------
+           +---------------+            |          +---------------+
+           | PCI PF Device |            |          | PCI VF Device |
+           +---------------+            |          +---------------+
+
+The FPGA devices appear as regular PCIe devices; thus, the FPGA PCIe device
+driver is always loaded first once a FPGA PCIE PF or VF device is detected. This
+driver plays an infrastructural role in the driver architecture.  It:
+
+       a) creates FPGA container device as parent of the feature devices.
+       b) walks through the Device Feature List, which is implemented in PCIE
+          device BAR memory, to discover feature devices and their sub features
+          and create platform device for them under the container device.
+       c) supports SRIOV.
+       d) introduces the feature device infrastructure, which abstracts
+          operations for sub features and exposes common functions to feature
+          device drivers.
+
+The FPGA Management Engine (FME) driver is a platform driver which is loaded
+automatically after FME platform device creation from the PCIE driver. It
+provides the key features for FPGA management, including:
+
+       a) Power and thermal management, error reporting, performance reporting
+          and other infrastructure functions. Users can access these functions
+          via sysfs interfaces exposed by FME driver.
+       b) Partial Reconfiguration. The FME driver creates platform devices
+          for FPGA manager, FPGA bridges and FPGA regions during PR sub
+          feature initialization; Once it receives an FPGA_FME_PORT_PR ioctl
+          from user, it invokes the common interface function from FPGA Region
+          to complete the partial reconfiguration of the bitstream to the given
+          port.
+       c) Port management for virtualization. The FME driver introduces two
+          ioctls, FPGA_FME_PORT_RELEASE (releases given port from PF) and
+          FPGA_FME_PORT_ASSIGN (assigns the port back to PF). Once the port is
+          released from the PF, it can be assigned to the VF through the SRIOV
+          interfaces provided by PCIE driver. (Refer to "FPGA virtualization"
+          for more details).
+
+Similar to the the FME driver, the FPGA Accelerated Function Unit (AFU) driver
+is probed once the AFU platform device is created. The main function of this
+module is to provide an interface for userspace applications to access the
+individual accelerators, including basic reset control on port, AFU MMIO region
+export, dma buffer mapping service, UMsg notification, and remote debug
+functions (see above).
+
+
+Device enumeration
+==================
+This section introduces how applications enumerate the fpga device from
+the sysfs hierarchy under /sys/class/fpga.
+
+In the example below, two Intel(R) FPGA devices are installed in the host. Each
+fpga device has one FME and two ports (AFUs).
+
+For each FPGA device, a device director is created under /sys/class/fpga/:
+
+       /sys/class/fpga/fpga.0
+       /sys/class/fpga/fpga.1
+
+The Intel(R) FPGA device driver exposes "intel-fpga-dev" as the FPGA's name.
+Application can retrieve name information via the sysfs interface:
+
+       /sys/class/fpga/fpga.0/name
+
+Each node has one FME and two ports (AFUs) as child devices:
+
+       /sys/class/fpga/fpga.0/intel-fpga-fme.0
+       /sys/class/fpga/fpga.0/intel-fpga-port.0
+       /sys/class/fpga/fpga.0/intel-fpga-port.1
+
+       /sys/class/fpga/fpga.1/intel-fpga-fme.1
+       /sys/class/fpga/fpga.1/intel-fpga-port.2
+       /sys/class/fpga/fpga.1/intel-fpga-port.3
+
+In general, the FME/AFU sysfs interfaces are named as follows:
+
+       /sys/class/fpga/<fpga.n>/<intel-fpga-fme.n>/
+       /sys/class/fpga/<fpga.n>/<intel-fpga-port.m>/
+
+with 'n' consecutively numbering all FMEs and 'm' consecutively numbering all
+ports.
+

Documentation/fpga/intel-fpga.txt line 256

Quoted 10 lines
Documentation/fpga/intel-fpga.txt new file +10 -0
+The device nodes used for ioctl() or mmap() can be referenced through:
+
+       /sys/class/fpga/<fpga.n>/<intel-fpga-port.n>/dev
+       /sys/class/fpga/<fpga.n>/<intel-fpga-fme.n>/dev
+
+Open discussion
+===============
+FME driver exports one ioctl (FPGA_FME_PORT_PR) for partial reconfiguration to
+user now. In the future, if unified user interfaces for reconfiguration are
+added, FME driver should switch to them from ioctl interface.

Adding an ioctl that will go away in the future?

I think we are going to have to find a different interface for doing partial reconfiguration. There was a recent conversation on the mailing list regarding adding a header to the raw bitstream and probably using sysfs to present that to the kernel. I'm working on getting an RFC together for that.

Alan Tull

author
#3 lore raw

Re: [PATCH v2 01/22] docs: fpga: add a document for Intel FPGA driver overview

Quoted 79 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

Quoted 75 lines

Add a document for Intel FPGA driver overview.

----
v2: added FME fpga-mgr/bridge/region platform driver to driver organization.
updated open discussion per current implementation.
fixed some typos.

---
 Documentation/fpga/intel-fpga.txt | 256 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 256 insertions(+)
 create mode 100644 Documentation/fpga/intel-fpga.txt
Documentation/fpga/intel-fpga.txt new file +55 -0
new file mode 100644
index 0000000..4a29470
@@ -0,0 +1,256 @@
+===============================================================================
+                    Intel FPGA driver Overview
+-------------------------------------------------------------------------------
+                Enno Luebbers <[email protected]>
+                Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>
+                Wu Hao <[email protected]>
+
+The Intel FPGA driver provides interfaces for userspace applications to
+configure, enumerate, open, and access FPGA accelerators on platforms equipped
+with Intel(R) FPGA PCIe based solutions and enables system level management
+functions such as FPGA reconfiguration, power management, and virtualization.
+
+HW Architecture
+===============
+From the OS's point of view, the FPGA hardware appears as a regular PCIe device.
+The FPGA device memory is organized using a predefined data structure (Device
+Feature List). Features supported by the particular FPGA device are exposed
+through these data structures, as illustrated below:
+
+  +-------------------------------+  +-------------+
+  |              PF               |  |     VF      |
+  +-------------------------------+  +-------------+
+      ^            ^         ^              ^
+      |            |         |              |
++-----|------------|---------|--------------|-------+
+|     |            |         |              |       |
+|  +-----+     +-------+ +-------+      +-------+   |
+|  | FME |     | Port0 | | Port1 |      | Port2 |   |
+|  +-----+     +-------+ +-------+      +-------+   |
+|                  ^         ^              ^       |
+|                  |         |              |       |
+|              +-------+ +------+       +-------+   |
+|              |  AFU  | |  AFU |       |  AFU  |   |
+|              +-------+ +------+       +-------+   |
+|                                                   |
+|                 FPGA PCIe Device                  |
++---------------------------------------------------+
+
+The driver supports PCIe SR-IOV to create virtual functions (VFs) which can be
+used to assign individual accelerators to virtual machines.
+
+FME (FPGA Management Engine)
+============================
+The FPGA Management Engine performs power and thermal management, error
+reporting, reconfiguration, performance reporting, and other infrastructure
+functions. Each FPGA has one FME, which is always accessed through the physical
+function (PF).
+
+User-space applications can acquire exclusive access to the FME using open(),
+and release it using close().
+
+The following functions are exposed through ioctls:
+
+       Get driver API version (FPGA_GET_API_VERSION)
+       Check for extensions (FPGA_CHECK_EXTENSION)
Quoted 8 lines

Documentation/fpga/intel-fpga.txt line 59

Quoted 4 lines
Documentation/fpga/intel-fpga.txt new file +4 -0
+       Assign port to PF (FPGA_FME_PORT_ASSIGN)
+       Release port from PF (FPGA_FME_PORT_RELEASE)
+       Program bitstream (FPGA_FME_PORT_PR)
+

I was hoping the API mailing list might have an opinion about this, but I think adding ioctls to the kernel is discouraged. Could these be sysfs?

Hi Alan,

As you see below, we have defined a lot of sysfs interface for device info, attributes and simple control operations. But for some actions which requires complex inputs/outputs parameters (e.g a struct with multiple items) with userspace, ioctls are used. I feel in such cases, ioctls seem more suitable than sysfs.

Quoted 192 lines
Quoted 192 lines
Documentation/fpga/intel-fpga.txt new file +191 -0
+More functions are exposed through sysfs
+(/sys/class/fpga/fpga.n/intel-fpga-fme.n/):
+
+       Read bitstream ID (bitstream_id)
+       Read bitstream metadata (bitstream_metadata)
+       Read number of ports (ports_num)
+       Read socket ID (socket_id)
+       Read performance counters (perf/)
+       Power management (power_mgmt/)
+       Thermal management (thermal_mgmt/)
+       Error reporting (errors/)
+
+PORT
+====
+A port represents the interface between the static FPGA fabric (the "blue
+bitstream") and a partially reconfigurable region containing an AFU (the "green
+bitstream"). It controls the communication from SW to the accelerator and
+exposes features such as reset and debug.
+
+A PCIe device may have several ports and each port can be released from PF by
+FPGA_FME_PORT_RELEASE ioctl on FME, and exposed through a VF via PCIe sriov
+sysfs interface.
+
+AFU
+===
+An AFU is attached to a port and exposes a 256k MMIO region to be used for
+accelerator-specific control registers.
+
+User-space applications can acquire exclusive access to an AFU attached to a
+port by using open() on the port device node, and release it using close().
+
+The following functions are exposed through ioctls:
+
+       Get driver API version (FPGA_GET_API_VERSION)
+       Check for extensions (FPGA_CHECK_EXTENSION)
+       Get port info (FPGA_PORT_GET_INFO)
+       Get MMIO region info (FPGA_PORT_GET_REGION_INFO)
+       Map DMA buffer (FPGA_PORT_DMA_MAP)
+       Unmap DMA buffer (FPGA_PORT_DMA_UNMAP)
+       Reset AFU (FPGA_PORT_RESET)
+       Enable UMsg (FPGA_PORT_UMSG_ENABLE)
+       Disable UMsg (FPGA_PORT_UMSG_DISABLE)
+       Set UMsg mode (FPGA_PORT_UMSG_SET_MODE)
+       Set UMsg base address (FPGA_PORT_UMSG_SET_BASE_ADDR)
+
+User-space applications can also mmap() accelerator MMIO regions.
+
+More functions are exposed through sysfs:
+(/sys/class/fpga/fpga.n/intel-fpga-port.m/):
+
+       Read Accelerator GUID (afu_id)
+       Error reporting (errors/)
+
+Partial Reconfiguration
+=======================
+As mentioned above, accelerators can be reconfigured through partial
+reconfiguration of a green bitstream file (GBS). The green bitstream must have
+been generated for the exact blue bitstream and targeted reconfigurable region
+(port) of the FPGA; otherwise, the reconfiguration operation will fail and
+possibly cause system instability. This compatibility can be checked by
+comparing the interface ID noted in the GBS header against the interface ID
+exposed by the FME through sysfs (see above). This check is usually done by
+user-space before calling the reconfiguration IOCTL.
+
+FPGA virtualization
+===================
+To enable accessing an accelerator from applications running in a VM, the
+respective AFU's port needs to be assigned to a VF using the following steps:
+
+ a) The PF owns all AFU ports by default. Any port that needs to be reassigned
+ to a VF must be released from PF firstly through the FPGA_FME_PORT_RELEASE
+ ioctl on the FME device.
+
+ b) Once N ports are released from PF, then user can use below command to
+ enable SRIOV and VFs. Each VF owns only one Port with AFU.
+
+ echo N > $PCI_DEVICE_PATH/sriov_numvfs
+
+ c) Pass through the VFs to VMs
+
+ d) The AFU under VF is accessible from applications in VM (using the same
+ driver inside the VF).
+
+Note the an FME can't be assigned to a VF, thus PR and other management
+functions are only available via the PF.
+
+
+Driver organization
+===================
+
+  +-------++------++------+             |
+  | FME   || FME  || FME  |             |
+  | FPGA  || FPGA || FPGA |             |
+  |Manager||Bridge||Region|             |
+  +-------++------++------+             |
+  +-----------------------+  +--------+ |             +--------+
+  |          FME          |  |  AFU   | |             |  AFU   |
+  |         Module        |  | Module | |             | Module |
+  +-----------------------+  +--------+ |             +--------+
+        +-----------------------+       |      +-----------------------+
+        | FPGA Container Device |       |      | FPGA Container Device |
+        +-----------------------+       |      +-----------------------+
+          +------------------+          |         +------------------+
+          | FPGA PCIE Module |          | Virtual | FPGA PCIE Module |
+          +------------------+   Host   | Machine +------------------+
+ -------------------------------------- | ------------------------------
+           +---------------+            |          +---------------+
+           | PCI PF Device |            |          | PCI VF Device |
+           +---------------+            |          +---------------+
+
+The FPGA devices appear as regular PCIe devices; thus, the FPGA PCIe device
+driver is always loaded first once a FPGA PCIE PF or VF device is detected. This
+driver plays an infrastructural role in the driver architecture.  It:
+
+       a) creates FPGA container device as parent of the feature devices.
+       b) walks through the Device Feature List, which is implemented in PCIE
+          device BAR memory, to discover feature devices and their sub features
+          and create platform device for them under the container device.
+       c) supports SRIOV.
+       d) introduces the feature device infrastructure, which abstracts
+          operations for sub features and exposes common functions to feature
+          device drivers.
+
+The FPGA Management Engine (FME) driver is a platform driver which is loaded
+automatically after FME platform device creation from the PCIE driver. It
+provides the key features for FPGA management, including:
+
+       a) Power and thermal management, error reporting, performance reporting
+          and other infrastructure functions. Users can access these functions
+          via sysfs interfaces exposed by FME driver.
+       b) Partial Reconfiguration. The FME driver creates platform devices
+          for FPGA manager, FPGA bridges and FPGA regions during PR sub
+          feature initialization; Once it receives an FPGA_FME_PORT_PR ioctl
+          from user, it invokes the common interface function from FPGA Region
+          to complete the partial reconfiguration of the bitstream to the given
+          port.
+       c) Port management for virtualization. The FME driver introduces two
+          ioctls, FPGA_FME_PORT_RELEASE (releases given port from PF) and
+          FPGA_FME_PORT_ASSIGN (assigns the port back to PF). Once the port is
+          released from the PF, it can be assigned to the VF through the SRIOV
+          interfaces provided by PCIE driver. (Refer to "FPGA virtualization"
+          for more details).
+
+Similar to the the FME driver, the FPGA Accelerated Function Unit (AFU) driver
+is probed once the AFU platform device is created. The main function of this
+module is to provide an interface for userspace applications to access the
+individual accelerators, including basic reset control on port, AFU MMIO region
+export, dma buffer mapping service, UMsg notification, and remote debug
+functions (see above).
+
+
+Device enumeration
+==================
+This section introduces how applications enumerate the fpga device from
+the sysfs hierarchy under /sys/class/fpga.
+
+In the example below, two Intel(R) FPGA devices are installed in the host. Each
+fpga device has one FME and two ports (AFUs).
+
+For each FPGA device, a device director is created under /sys/class/fpga/:
+
+       /sys/class/fpga/fpga.0
+       /sys/class/fpga/fpga.1
+
+The Intel(R) FPGA device driver exposes "intel-fpga-dev" as the FPGA's name.
+Application can retrieve name information via the sysfs interface:
+
+       /sys/class/fpga/fpga.0/name
+
+Each node has one FME and two ports (AFUs) as child devices:
+
+       /sys/class/fpga/fpga.0/intel-fpga-fme.0
+       /sys/class/fpga/fpga.0/intel-fpga-port.0
+       /sys/class/fpga/fpga.0/intel-fpga-port.1
+
+       /sys/class/fpga/fpga.1/intel-fpga-fme.1
+       /sys/class/fpga/fpga.1/intel-fpga-port.2
+       /sys/class/fpga/fpga.1/intel-fpga-port.3
+
+In general, the FME/AFU sysfs interfaces are named as follows:
+
+       /sys/class/fpga/<fpga.n>/<intel-fpga-fme.n>/
+       /sys/class/fpga/<fpga.n>/<intel-fpga-port.m>/
+
+with 'n' consecutively numbering all FMEs and 'm' consecutively numbering all
+ports.
+
+The device nodes used for ioctl() or mmap() can be referenced through:
+
+       /sys/class/fpga/<fpga.n>/<intel-fpga-port.n>/dev
+       /sys/class/fpga/<fpga.n>/<intel-fpga-fme.n>/dev
Quoted 8 lines

Documentation/fpga/intel-fpga.txt line 256

Quoted 6 lines
Documentation/fpga/intel-fpga.txt new file +6 -0
+
+Open discussion
+===============
+FME driver exports one ioctl (FPGA_FME_PORT_PR) for partial reconfiguration to
+user now. In the future, if unified user interfaces for reconfiguration are
+added, FME driver should switch to them from ioctl interface.

Adding an ioctl that will go away in the future?

Currently there is no common userspace interfaces for PR, so Intel FPGA device driver implemented its own solution by adding one ioctl for now. Do you think if this is acceptable for now?

As mentioned in 'open discussion', if common interfaces are introduced in the future, we can adpat this driver for that, and encourage users to use common interfaces instead. But if ioctl is used widely at that time, then we may need to keep this ioctl for compatibility.

Quoted 5 lines

I think we are going to have to find a different interface for doing partial reconfiguration. There was a recent conversation on the mailing list regarding adding a header to the raw bitstream and probably using sysfs to present that to the kernel. I'm working on getting an RFC together for that.

Great to know this. Looking forward to see that. : )

Thanks
Hao

#4 lore raw

Re: [PATCH v2 01/22] docs: fpga: add a document for Intel FPGA driver overview

Quoted 89 lines · Wu Hao
Quoted 79 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

Quoted 75 lines

Add a document for Intel FPGA driver overview.

----
v2: added FME fpga-mgr/bridge/region platform driver to driver organization.
updated open discussion per current implementation.
fixed some typos.

---
 Documentation/fpga/intel-fpga.txt | 256 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 256 insertions(+)
 create mode 100644 Documentation/fpga/intel-fpga.txt
Documentation/fpga/intel-fpga.txt new file +55 -0
new file mode 100644
index 0000000..4a29470
@@ -0,0 +1,256 @@
+===============================================================================
+                    Intel FPGA driver Overview
+-------------------------------------------------------------------------------
+                Enno Luebbers <[email protected]>
+                Xiao Guangrong <[email protected]>
+                Wu Hao <[email protected]>
+
+The Intel FPGA driver provides interfaces for userspace applications to
+configure, enumerate, open, and access FPGA accelerators on platforms equipped
+with Intel(R) FPGA PCIe based solutions and enables system level management
+functions such as FPGA reconfiguration, power management, and virtualization.
+
+HW Architecture
+===============
+From the OS's point of view, the FPGA hardware appears as a regular PCIe device.
+The FPGA device memory is organized using a predefined data structure (Device
+Feature List). Features supported by the particular FPGA device are exposed
+through these data structures, as illustrated below:
+
+  +-------------------------------+  +-------------+
+  |              PF               |  |     VF      |
+  +-------------------------------+  +-------------+
+      ^            ^         ^              ^
+      |            |         |              |
++-----|------------|---------|--------------|-------+
+|     |            |         |              |       |
+|  +-----+     +-------+ +-------+      +-------+   |
+|  | FME |     | Port0 | | Port1 |      | Port2 |   |
+|  +-----+     +-------+ +-------+      +-------+   |
+|                  ^         ^              ^       |
+|                  |         |              |       |
+|              +-------+ +------+       +-------+   |
+|              |  AFU  | |  AFU |       |  AFU  |   |
+|              +-------+ +------+       +-------+   |
+|                                                   |
+|                 FPGA PCIe Device                  |
++---------------------------------------------------+
+
+The driver supports PCIe SR-IOV to create virtual functions (VFs) which can be
+used to assign individual accelerators to virtual machines.
+
+FME (FPGA Management Engine)
+============================
+The FPGA Management Engine performs power and thermal management, error
+reporting, reconfiguration, performance reporting, and other infrastructure
+functions. Each FPGA has one FME, which is always accessed through the physical
+function (PF).
+
+User-space applications can acquire exclusive access to the FME using open(),
+and release it using close().
+
+The following functions are exposed through ioctls:
+
+       Get driver API version (FPGA_GET_API_VERSION)
+       Check for extensions (FPGA_CHECK_EXTENSION)
Quoted 8 lines

Documentation/fpga/intel-fpga.txt line 59

Quoted 4 lines
Documentation/fpga/intel-fpga.txt new file +4 -0
+       Assign port to PF (FPGA_FME_PORT_ASSIGN)
+       Release port from PF (FPGA_FME_PORT_RELEASE)
+       Program bitstream (FPGA_FME_PORT_PR)
+

I was hoping the API mailing list might have an opinion about this, but I think adding ioctls to the kernel is discouraged. Could these be sysfs?

Quoted 8 lines

Hi Alan,

As you see below, we have defined a lot of sysfs interface for device info, attributes and simple control operations. But for some actions which requires complex inputs/outputs parameters (e.g a struct with multiple items) with userspace, ioctls are used. I feel in such cases, ioctls seem more suitable than sysfs.

Also, we're thinking that some operations require that you first "acquire ownership" of the respective device, which I believe maps more easily to open() and ioctls than sysfs.

Thanks
- Enno

#5 lore raw

Re: [PATCH v2 01/22] docs: fpga: add a document for Intel FPGA driver overview

Quoted 99 lines · Enno
Quoted 89 lines · Wu Hao
Quoted 79 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

Quoted 75 lines

Add a document for Intel FPGA driver overview.

----
v2: added FME fpga-mgr/bridge/region platform driver to driver organization.
updated open discussion per current implementation.
fixed some typos.

---
 Documentation/fpga/intel-fpga.txt | 256 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 256 insertions(+)
 create mode 100644 Documentation/fpga/intel-fpga.txt
Documentation/fpga/intel-fpga.txt new file +55 -0
new file mode 100644
index 0000000..4a29470
@@ -0,0 +1,256 @@
+===============================================================================
+                    Intel FPGA driver Overview
+-------------------------------------------------------------------------------
+                Enno Luebbers <[email protected]>
+                Xiao Guangrong <[email protected]>
+                Wu Hao <[email protected]>
+
+The Intel FPGA driver provides interfaces for userspace applications to
+configure, enumerate, open, and access FPGA accelerators on platforms equipped
+with Intel(R) FPGA PCIe based solutions and enables system level management
+functions such as FPGA reconfiguration, power management, and virtualization.
+
+HW Architecture
+===============
+From the OS's point of view, the FPGA hardware appears as a regular PCIe device.
+The FPGA device memory is organized using a predefined data structure (Device
+Feature List). Features supported by the particular FPGA device are exposed
+through these data structures, as illustrated below:
+
+  +-------------------------------+  +-------------+
+  |              PF               |  |     VF      |
+  +-------------------------------+  +-------------+
+      ^            ^         ^              ^
+      |            |         |              |
++-----|------------|---------|--------------|-------+
+|     |            |         |              |       |
+|  +-----+     +-------+ +-------+      +-------+   |
+|  | FME |     | Port0 | | Port1 |      | Port2 |   |
+|  +-----+     +-------+ +-------+      +-------+   |
+|                  ^         ^              ^       |
+|                  |         |              |       |
+|              +-------+ +------+       +-------+   |
+|              |  AFU  | |  AFU |       |  AFU  |   |
+|              +-------+ +------+       +-------+   |
+|                                                   |
+|                 FPGA PCIe Device                  |
++---------------------------------------------------+
+
+The driver supports PCIe SR-IOV to create virtual functions (VFs) which can be
+used to assign individual accelerators to virtual machines.
+
+FME (FPGA Management Engine)
+============================
+The FPGA Management Engine performs power and thermal management, error
+reporting, reconfiguration, performance reporting, and other infrastructure
+functions. Each FPGA has one FME, which is always accessed through the physical
+function (PF).
+
+User-space applications can acquire exclusive access to the FME using open(),
+and release it using close().
+
+The following functions are exposed through ioctls:
+
+       Get driver API version (FPGA_GET_API_VERSION)
+       Check for extensions (FPGA_CHECK_EXTENSION)
Quoted 8 lines

Documentation/fpga/intel-fpga.txt line 59

Quoted 4 lines
Documentation/fpga/intel-fpga.txt new file +4 -0
+       Assign port to PF (FPGA_FME_PORT_ASSIGN)
+       Release port from PF (FPGA_FME_PORT_RELEASE)
+       Program bitstream (FPGA_FME_PORT_PR)
+

I was hoping the API mailing list might have an opinion about this, but I think adding ioctls to the kernel is discouraged. Could these be sysfs?

Quoted 8 lines

Hi Alan,

As you see below, we have defined a lot of sysfs interface for device info, attributes and simple control operations. But for some actions which requires complex inputs/outputs parameters (e.g a struct with multiple items) with userspace, ioctls are used. I feel in such cases, ioctls seem more suitable than sysfs.

Quoted 6 lines

Also, we're thinking that some operations require that you first "acquire ownership" of the respective device, which I believe maps more easily to open() and ioctls than sysfs.

Thanks
- Enno

Yes, sysfs doesn't implement open/close so there's no clean way to have one thing in userspace that owns the interface. It's a shame.

Alan

#6 lore raw

Re: [PATCH v2 01/22] docs: fpga: add a document for Intel FPGA driver overview

Quoted 9 lines · Alan Tull
Quoted 6 lines

Also, we're thinking that some operations require that you first "acquire ownership" of the respective device, which I believe maps more easily to open() and ioctls than sysfs.

Thanks
- Enno

Yes, sysfs doesn't implement open/close so there's no clean way to have one thing in userspace that owns the interface. It's a shame.

No, that was by design :)

#7 lore raw

Re: [PATCH v2 01/22] docs: fpga: add a document for Intel FPGA driver overview

Quoted 5 lines · Greg KH
Quoted 4 lines · Alan Tull
Quoted 4 lines

Also, we're thinking that some operations require that you first "acquire ownership" of the respective device, which I believe maps more easily to open() and ioctls than sysfs.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Thanks
- Enno

Yes, sysfs doesn't implement open/close so there's no clean way to have one thing in userspace that owns the interface. It's a shame.

No, that was by design :)

Well that's good to know! linux-api is copied on this patchset. I'm wondering if there is another mailing list that can weigh in on this new ioctl that is being added to the kernel or if that's the one.

Alan Tull

author
#8 lore raw

PATCH v2 2/22 fpga: add FPGA device framework

In reply to Wu Hao

During FPGA device (e.g PCI-based) discovery, platform devices are registered for different FPGA function units. But the device node path isn't quite friendly to applications.

Consider this case, applications want to access child device's sysfs file for some information.

1) Access using bus-based path (e.g PCI)

/sys/bus/pci/devices/xxxxx/fpga_func_a.0/sysfs_file

From the path, it's clear which PCI device is the parent, but not perfect solution for applications. PCI device BDF is not fixed, application may need to search all PCI device to find the actual FPGA Device.

2) Or access using platform device path

/sys/bus/platform/devices/fpga_func_a.0/sysfs_file

Applications find the actual function by name easily, but no information about which fpga device it belongs to. It's quite confusing if multiple FPGA devices are in one system.

'FPGA Device' class is introduced to resolve this problem. Each node under this class represents a fpga device, which may have one or more child devices. Applications only need to search under this FPGA Device class folder to find the child device node it needs.

For example, for the platform has 2 fpga devices, each fpga device has 3 child devices, the hierarchy looks like this.

Two nodes are under /sys/class/fpga/:
/sys/class/fpga/fpga.0
/sys/class/fpga/fpga.1

Each node has 1 function A device and 2 function B devices:
/sys/class/fpga/fpga.0/func_a.0 /sys/class/fpga/fpga.0/func_b.0 /sys/class/fpga/fpga.0/func_b.1

/sys/class/fpga/fpga.1/func_a.1
/sys/class/fpga/fpga.1/func_b.2
/sys/class/fpga/fpga.1/func_b.3

This following APIs are provided by FPGA device framework:
* fpga_dev_create
Create fpga device under the given parent device.
* fpga_dev_destroy
Destroy fpga device

The following sysfs files are created:
* /sys/class/fpga/<fpga.x>/name
Name of the fpga device.

---
v2: added sysfs documentation for fpga-dev class.
switched to GPLv2 license.

---
 Documentation/ABI/testing/sysfs-class-fpga-dev |   5 ++
 drivers/fpga/Kconfig                           |   6 ++
 drivers/fpga/Makefile                          |   3 +
 drivers/fpga/fpga-dev.c                        | 118 +++++++++++++++++++++++++
 include/linux/fpga/fpga-dev.h                  |  31 +++++++
 5 files changed, 163 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-dev
 create mode 100644 drivers/fpga/fpga-dev.c
 create mode 100644 include/linux/fpga/fpga-dev.h
Documentation/ABI/testing/sysfs-class-fpga-dev new file +5 -0
new file mode 100644
index 0000000..ce162df
@@ -0,0 +1,5 @@
+What:		/sys/class/fpga/<device>/name
+Date:		June 2017
+KernelVersion:	4.12
+Contact:	Wu Hao <[email protected]>
+Description:	Name of FPGA device
drivers/fpga/Kconfig +6 -0
index 394c141..ed600d5 100644
@@ -12,6 +12,12 @@ config FPGA
 	  manager drivers.
 
 if FPGA
+config FPGA_DEVICE
+	tristate "FPGA Device Framework"
+	help
+	  Say Y here if you want support for FPGA Devices from the kernel.
+	  The FPGA Device Framework adds a FPGA device class and provide
+	  interfaces to create FPGA devices.
 
 config FPGA_REGION
 	tristate "FPGA Region"
drivers/fpga/Makefile +3 -0
index 85b45d0..8950a8f 100644
@@ -5,6 +5,9 @@
 # Core FPGA Manager Framework
 obj-$(CONFIG_FPGA)			+= fpga-mgr.o
 
+# FPGA Device Framework
+obj-$(CONFIG_FPGA_DEVICE)		+= fpga-dev.o
+
 # FPGA Manager Drivers
 obj-$(CONFIG_FPGA_MGR_ICE40_SPI)	+= ice40-spi.o
 obj-$(CONFIG_FPGA_MGR_SOCFPGA)		+= socfpga.o
drivers/fpga/fpga-dev.c new file +118 -0

118 lines not on this page · raw message

include/linux/fpga/fpga-dev.h new file +31 -0
new file mode 100644
index 0000000..ee693cc
@@ -0,0 +1,31 @@
+/*
+ * FPGA Device Driver Header
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+#ifndef _LINUX_FPGA_DEV_H
+#define _LINUX_FPGA_DEV_H
+
+/**
+ * struct fpga_dev - fpga device structure
+ * @name: name of fpga device
+ * @dev: fpga device
+ */
+struct fpga_dev {
+	const char *name;
+	struct device dev;
+};
+
+#define to_fpga_dev(d) container_of(d, struct fpga_dev, dev)
+
+struct fpga_dev *fpga_dev_create(struct device *parent, const char *name);
+
+static inline void fpga_dev_destroy(struct fpga_dev *fdev)
+{
+	device_unregister(&fdev->dev);
+}
+
+#endif
Signature · 1 line
1.8.3.1
#9 lore raw

Re: [PATCH v2 02/22] fpga: add FPGA device framework

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Rob,

I was hoping to pick your brain a bit on a DT question.

Quoted 15 lines

During FPGA device (e.g PCI-based) discovery, platform devices are registered for different FPGA function units. But the device node path isn't quite friendly to applications.

Consider this case, applications want to access child device's sysfs file for some information.

1) Access using bus-based path (e.g PCI)

/sys/bus/pci/devices/xxxxx/fpga_func_a.0/sysfs_file

From the path, it's clear which PCI device is the parent, but not perfect solution for applications. PCI device BDF is not fixed, application may need to search all PCI device to find the actual FPGA Device.

Quoted 7 lines

2) Or access using platform device path

/sys/bus/platform/devices/fpga_func_a.0/sysfs_file

Applications find the actual function by name easily, but no information about which fpga device it belongs to. It's quite confusing if multiple FPGA devices are in one system.

There's a proposal for adding sysfs nodes that correspond to each FPGA device., with the devices located on each FPGA under them. It makes it easier to see which device is on which FPGA.

Quoted 13 lines

'FPGA Device' class is introduced to resolve this problem. Each node under this class represents a fpga device, which may have one or more child devices. Applications only need to search under this FPGA Device class folder to find the child device node it needs.

For example, for the platform has 2 fpga devices, each fpga device has 3 child devices, the hierarchy looks like this.

Two nodes are under /sys/class/fpga/:
/sys/class/fpga/fpga.0
/sys/class/fpga/fpga.1

Quoted 8 lines

Each node has 1 function A device and 2 function B devices:
/sys/class/fpga/fpga.0/func_a.0 /sys/class/fpga/fpga.0/func_b.0 /sys/class/fpga/fpga.0/func_b.1

/sys/class/fpga/fpga.1/func_a.1
/sys/class/fpga/fpga.1/func_b.2
/sys/class/fpga/fpga.1/func_b.3

I can see the value of having sysfs nodes that correspond to fpga devices and being able to find devices under them. I'm thinking what that would mean for Device Tree when fpga-dev is used on DT enabled systems. In Device Tree, what is a fpga-dev?

Currently the DT would have a FPGA bridge corresponding to each FPGA's hardware bridge and a heirarchy of bridges, regions and devices under it. On systems that don't support partial reconfiguration under the OS (so not main bridge that was controlled by the OS), there would be a FPGA region, then its child regions, bridges, and devices.

Alan

Quoted 237 lines

This following APIs are provided by FPGA device framework:
* fpga_dev_create
Create fpga device under the given parent device.
* fpga_dev_destroy
Destroy fpga device

The following sysfs files are created:
* /sys/class/fpga/<fpga.x>/name
Name of the fpga device.

---
v2: added sysfs documentation for fpga-dev class.
switched to GPLv2 license.

---
 Documentation/ABI/testing/sysfs-class-fpga-dev |   5 ++
 drivers/fpga/Kconfig                           |   6 ++
 drivers/fpga/Makefile                          |   3 +
 drivers/fpga/fpga-dev.c                        | 118 +++++++++++++++++++++++++
 include/linux/fpga/fpga-dev.h                  |  31 +++++++
 5 files changed, 163 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-dev
 create mode 100644 drivers/fpga/fpga-dev.c
 create mode 100644 include/linux/fpga/fpga-dev.h
Documentation/ABI/testing/sysfs-class-fpga-dev new file +5 -0
new file mode 100644
index 0000000..ce162df
@@ -0,0 +1,5 @@
+What:          /sys/class/fpga/<device>/name
+Date:          June 2017
+KernelVersion: 4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Name of FPGA device
drivers/fpga/Kconfig +6 -0
index 394c141..ed600d5 100644
@@ -12,6 +12,12 @@ config FPGA
          manager drivers.
 
 if FPGA
+config FPGA_DEVICE
+       tristate "FPGA Device Framework"
+       help
+         Say Y here if you want support for FPGA Devices from the kernel.
+         The FPGA Device Framework adds a FPGA device class and provide
+         interfaces to create FPGA devices.
 
 config FPGA_REGION
        tristate "FPGA Region"
drivers/fpga/Makefile +3 -0
index 85b45d0..8950a8f 100644
@@ -5,6 +5,9 @@
 # Core FPGA Manager Framework
 obj-$(CONFIG_FPGA)                     += fpga-mgr.o
 
+# FPGA Device Framework
+obj-$(CONFIG_FPGA_DEVICE)              += fpga-dev.o
+
 # FPGA Manager Drivers
 obj-$(CONFIG_FPGA_MGR_ICE40_SPI)       += ice40-spi.o
 obj-$(CONFIG_FPGA_MGR_SOCFPGA)         += socfpga.o
drivers/fpga/fpga-dev.c new file +118 -0
new file mode 100644
index 0000000..5778ebc
@@ -0,0 +1,118 @@
+/*
+ * FPGA Device Framework Driver
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/fpga/fpga-dev.h>
+
+static DEFINE_IDA(fpga_dev_ida);
+static struct class *fpga_dev_class;
+
+static ssize_t name_show(struct device *dev,
+                        struct device_attribute *attr, char *buf)
+{
+       struct fpga_dev *fdev = to_fpga_dev(dev);
+
+       return sprintf(buf, "%s\n", fdev->name);
+}
+static DEVICE_ATTR_RO(name);
+
+static struct attribute *fpga_dev_attrs[] = {
+       &dev_attr_name.attr,
+       NULL,
+};
+ATTRIBUTE_GROUPS(fpga_dev);
+
+/**
+ * fpga_dev_create - create a fpga device
+ * @parent: parent device
+ * @name: fpga device name
+ *
+ * Return fpga_dev struct for success, error code otherwise.
+ */
+struct fpga_dev *fpga_dev_create(struct device *parent, const char *name)
+{
+       struct fpga_dev *fdev;
+       int id, ret = 0;
+
+       if (!name || !strlen(name)) {
+               dev_err(parent, "Attempt to register with no name!\n");
+               return ERR_PTR(-EINVAL);
+       }
+
+       fdev = kzalloc(sizeof(*fdev), GFP_KERNEL);
+       if (!fdev)
+               return ERR_PTR(-ENOMEM);
+
+       id = ida_simple_get(&fpga_dev_ida, 0, 0, GFP_KERNEL);
+       if (id < 0) {
+               ret = id;
+               goto error_kfree;
+       }
+
+       fdev->name = name;
+
+       device_initialize(&fdev->dev);
+       fdev->dev.class = fpga_dev_class;
+       fdev->dev.parent = parent;
+       fdev->dev.id = id;
+
+       ret = dev_set_name(&fdev->dev, "fpga.%d", id);
+       if (ret)
+               goto error_device;
+
+       ret = device_add(&fdev->dev);
+       if (ret)
+               goto error_device;
+
+       dev_dbg(fdev->dev.parent, "fpga device [%s] created\n", fdev->name);
+
+       return fdev;
+
+error_device:
+       ida_simple_remove(&fpga_dev_ida, id);
+error_kfree:
+       kfree(fdev);
+
+       return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(fpga_dev_create);
+
+static void fpga_dev_release(struct device *dev)
+{
+       struct fpga_dev *fdev = to_fpga_dev(dev);
+
+       ida_simple_remove(&fpga_dev_ida, fdev->dev.id);
+       kfree(fdev);
+}
+
+static int __init fpga_dev_class_init(void)
+{
+       pr_info("FPGA Device framework\n");
+
+       fpga_dev_class = class_create(THIS_MODULE, "fpga");
+       if (IS_ERR(fpga_dev_class))
+               return PTR_ERR(fpga_dev_class);
+
+       fpga_dev_class->dev_groups = fpga_dev_groups;
+       fpga_dev_class->dev_release = fpga_dev_release;
+
+       return 0;
+}
+
+static void __exit fpga_dev_class_exit(void)
+{
+       class_destroy(fpga_dev_class);
+}
+
+MODULE_DESCRIPTION("FPGA Device framework");
+MODULE_LICENSE("GPL v2");
+
+subsys_initcall(fpga_dev_class_init);
+module_exit(fpga_dev_class_exit);
include/linux/fpga/fpga-dev.h new file +31 -1
new file mode 100644
index 0000000..ee693cc
@@ -0,0 +1,31 @@
+/*
+ * FPGA Device Driver Header
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+#ifndef _LINUX_FPGA_DEV_H
+#define _LINUX_FPGA_DEV_H
+
+/**
+ * struct fpga_dev - fpga device structure
+ * @name: name of fpga device
+ * @dev: fpga device
+ */
+struct fpga_dev {
+       const char *name;
+       struct device dev;
+};
+
+#define to_fpga_dev(d) container_of(d, struct fpga_dev, dev)
+
+struct fpga_dev *fpga_dev_create(struct device *parent, const char *name);
+
+static inline void fpga_dev_destroy(struct fpga_dev *fdev)
+{
+       device_unregister(&fdev->dev);
+}
+
+#endif
--

1.8.3.1

#10 lore raw

Re: [PATCH v2 02/22] fpga: add FPGA device framework

Quoted 25 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Rob,

I was hoping to pick your brain a bit on a DT question.

Quoted 19 lines

During FPGA device (e.g PCI-based) discovery, platform devices are registered for different FPGA function units. But the device node path isn't quite friendly to applications.

Consider this case, applications want to access child device's sysfs file for some information.

1) Access using bus-based path (e.g PCI)

/sys/bus/pci/devices/xxxxx/fpga_func_a.0/sysfs_file

From the path, it's clear which PCI device is the parent, but not perfect solution for applications. PCI device BDF is not fixed, application may need to search all PCI device to find the actual FPGA Device.

2) Or access using platform device path

/sys/bus/platform/devices/fpga_func_a.0/sysfs_file

Quoted 7 lines
Quoted 3 lines

Applications find the actual function by name easily, but no information about which fpga device it belongs to. It's quite confusing if multiple FPGA devices are in one system.

There's a proposal for adding sysfs nodes that correspond to each FPGA device., with the devices located on each FPGA under them. It makes it easier to see which device is on which FPGA.

Makes sense.

Quoted 12 lines
Quoted 12 lines

'FPGA Device' class is introduced to resolve this problem. Each node under this class represents a fpga device, which may have one or more child devices. Applications only need to search under this FPGA Device class folder to find the child device node it needs.

For example, for the platform has 2 fpga devices, each fpga device has 3 child devices, the hierarchy looks like this.

Two nodes are under /sys/class/fpga/:
/sys/class/fpga/fpga.0
/sys/class/fpga/fpga.1

Quoted 8 lines
Quoted 8 lines

Each node has 1 function A device and 2 function B devices:
/sys/class/fpga/fpga.0/func_a.0 /sys/class/fpga/fpga.0/func_b.0 /sys/class/fpga/fpga.0/func_b.1

/sys/class/fpga/fpga.1/func_a.1
/sys/class/fpga/fpga.1/func_b.2
/sys/class/fpga/fpga.1/func_b.3

A class is generally what is the function of the device, not how it is attached. Seems like what you want here is a new bus type if the existing PCI and platform bus types don't work.

Quoted 5 lines

I can see the value of having sysfs nodes that correspond to fpga devices and being able to find devices under them. I'm thinking what that would mean for Device Tree when fpga-dev is used on DT enabled systems. In Device Tree, what is a fpga-dev?

Just properly setting the parent struct device on the functions should be enough to figure out which function is in which fpga. I don't see why a new class is needed.

Quoted 5 lines

Currently the DT would have a FPGA bridge corresponding to each FPGA's hardware bridge and a heirarchy of bridges, regions and devices under it. On systems that don't support partial reconfiguration under the OS (so not main bridge that was controlled by the OS), there would be a FPGA region, then its child regions, bridges, and devices.

The FPGA bridges could instantiate fpga bus type devices instead of platform devices. That's really up to Linux and outside the scope of the bindings.

Rob
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#11 lore raw

Re: [PATCH v2 02/22] fpga: add FPGA device framework

Quoted 71 lines · Rob Herring
Quoted 25 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Rob,

I was hoping to pick your brain a bit on a DT question.

Quoted 19 lines

During FPGA device (e.g PCI-based) discovery, platform devices are registered for different FPGA function units. But the device node path isn't quite friendly to applications.

Consider this case, applications want to access child device's sysfs file for some information.

1) Access using bus-based path (e.g PCI)

/sys/bus/pci/devices/xxxxx/fpga_func_a.0/sysfs_file

From the path, it's clear which PCI device is the parent, but not perfect solution for applications. PCI device BDF is not fixed, application may need to search all PCI device to find the actual FPGA Device.

2) Or access using platform device path

/sys/bus/platform/devices/fpga_func_a.0/sysfs_file

Quoted 7 lines
Quoted 3 lines

Applications find the actual function by name easily, but no information about which fpga device it belongs to. It's quite confusing if multiple FPGA devices are in one system.

There's a proposal for adding sysfs nodes that correspond to each FPGA device., with the devices located on each FPGA under them. It makes it easier to see which device is on which FPGA.

Makes sense.

Quoted 12 lines
Quoted 12 lines

'FPGA Device' class is introduced to resolve this problem. Each node under this class represents a fpga device, which may have one or more child devices. Applications only need to search under this FPGA Device class folder to find the child device node it needs.

For example, for the platform has 2 fpga devices, each fpga device has 3 child devices, the hierarchy looks like this.

Two nodes are under /sys/class/fpga/:
/sys/class/fpga/fpga.0
/sys/class/fpga/fpga.1

Quoted 8 lines
Quoted 8 lines

Each node has 1 function A device and 2 function B devices:
/sys/class/fpga/fpga.0/func_a.0 /sys/class/fpga/fpga.0/func_b.0 /sys/class/fpga/fpga.0/func_b.1

/sys/class/fpga/fpga.1/func_a.1
/sys/class/fpga/fpga.1/func_b.2
/sys/class/fpga/fpga.1/func_b.3

A class is generally what is the function of the device, not how it is attached. Seems like what you want here is a new bus type if the existing PCI and platform bus types don't work.

Quoted 5 lines

I can see the value of having sysfs nodes that correspond to fpga devices and being able to find devices under them. I'm thinking what that would mean for Device Tree when fpga-dev is used on DT enabled systems. In Device Tree, what is a fpga-dev?

Just properly setting the parent struct device on the functions should be enough to figure out which function is in which fpga. I don't see why a new class is needed.

Quoted 8 lines
Quoted 5 lines

Currently the DT would have a FPGA bridge corresponding to each FPGA's hardware bridge and a heirarchy of bridges, regions and devices under it. On systems that don't support partial reconfiguration under the OS (so not main bridge that was controlled by the OS), there would be a FPGA region, then its child regions, bridges, and devices.

The FPGA bridges could instantiate fpga bus type devices instead of platform devices.

Yes

Some FPGA use cases already have a base bridge per FPGA that could serve as this bus. But this use case has a static FPGA image + reprogrammable child fpga regions. There's no base bridge under Linux since the FPGA was programmed and the bridge enabled before Linux
boots. An added base bridge that doesn't touch hardware will be
required for this type of use.

Quoted 2 lines

That's really up to Linux and outside the scope of the bindings.

Thanks for the feedback.

Alan Tull

Quoted 2 lines

Rob

author
#12 lore raw

Re: [PATCH v2 02/22] fpga: add FPGA device framework

Quoted 81 lines · Alan Tull
Quoted 71 lines · Rob Herring
Quoted 25 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Rob,

I was hoping to pick your brain a bit on a DT question.

Quoted 19 lines

During FPGA device (e.g PCI-based) discovery, platform devices are registered for different FPGA function units. But the device node path isn't quite friendly to applications.

Consider this case, applications want to access child device's sysfs file for some information.

1) Access using bus-based path (e.g PCI)

/sys/bus/pci/devices/xxxxx/fpga_func_a.0/sysfs_file

From the path, it's clear which PCI device is the parent, but not perfect solution for applications. PCI device BDF is not fixed, application may need to search all PCI device to find the actual FPGA Device.

2) Or access using platform device path

/sys/bus/platform/devices/fpga_func_a.0/sysfs_file

Quoted 7 lines
Quoted 3 lines

Applications find the actual function by name easily, but no information about which fpga device it belongs to. It's quite confusing if multiple FPGA devices are in one system.

There's a proposal for adding sysfs nodes that correspond to each FPGA device., with the devices located on each FPGA under them. It makes it easier to see which device is on which FPGA.

Makes sense.

Quoted 12 lines
Quoted 12 lines

'FPGA Device' class is introduced to resolve this problem. Each node under this class represents a fpga device, which may have one or more child devices. Applications only need to search under this FPGA Device class folder to find the child device node it needs.

For example, for the platform has 2 fpga devices, each fpga device has 3 child devices, the hierarchy looks like this.

Two nodes are under /sys/class/fpga/:
/sys/class/fpga/fpga.0
/sys/class/fpga/fpga.1

Quoted 8 lines
Quoted 8 lines

Each node has 1 function A device and 2 function B devices:
/sys/class/fpga/fpga.0/func_a.0 /sys/class/fpga/fpga.0/func_b.0 /sys/class/fpga/fpga.0/func_b.1

/sys/class/fpga/fpga.1/func_a.1
/sys/class/fpga/fpga.1/func_b.2
/sys/class/fpga/fpga.1/func_b.3

A class is generally what is the function of the device, not how it is attached. Seems like what you want here is a new bus type if the existing PCI and platform bus types don't work.

Quoted 5 lines

I can see the value of having sysfs nodes that correspond to fpga devices and being able to find devices under them. I'm thinking what that would mean for Device Tree when fpga-dev is used on DT enabled systems. In Device Tree, what is a fpga-dev?

Just properly setting the parent struct device on the functions should be enough to figure out which function is in which fpga. I don't see why a new class is needed.

Quoted 8 lines
Quoted 5 lines

Currently the DT would have a FPGA bridge corresponding to each FPGA's hardware bridge and a heirarchy of bridges, regions and devices under it. On systems that don't support partial reconfiguration under the OS (so not main bridge that was controlled by the OS), there would be a FPGA region, then its child regions, bridges, and devices.

The FPGA bridges could instantiate fpga bus type devices instead of platform devices.

Quoted 8 lines

Yes

Some FPGA use cases already have a base bridge per FPGA that could serve as this bus. But this use case has a static FPGA image + reprogrammable child fpga regions. There's no base bridge under Linux since the FPGA was programmed and the bridge enabled before Linux
boots. An added base bridge that doesn't touch hardware will be
required for this type of use.

Hi Alan

Does 'base bridge' mentioned above mean a hardware bridge just like PCIe or USB?

I tried to use fpga bus type device instead of fpga-dev class today, it works for me, e.g Intel FPGA device PCIe driver could create a fpga bus type dev as a child of PCIe device and its sysfs path will be changed to /sys/bus/fpga/devices/fpga.x/ from /sys/class/fpga/fpga.x/. For now, this fpga bus type device is only used as container device, so no driver needed for it.

Do you have any concern on this? I see fpga bus type works fine, but I didn't see other advantages for this case, as we only use it as a container device to represent a FPGA device in sysfs hierarchy. :)

Thanks
Hao

Quoted 10 lines
Quoted 3 lines

That's really up to Linux and outside the scope of the bindings.

Thanks for the feedback.

Alan Tull

Quoted 2 lines

Rob

#13 lore raw

Re: [PATCH v2 02/22] fpga: add FPGA device framework

Quoted 91 lines · Wu Hao
Quoted 81 lines · Alan Tull
Quoted 71 lines · Rob Herring
Quoted 25 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Rob,

I was hoping to pick your brain a bit on a DT question.

Quoted 19 lines

During FPGA device (e.g PCI-based) discovery, platform devices are registered for different FPGA function units. But the device node path isn't quite friendly to applications.

Consider this case, applications want to access child device's sysfs file for some information.

1) Access using bus-based path (e.g PCI)

/sys/bus/pci/devices/xxxxx/fpga_func_a.0/sysfs_file

From the path, it's clear which PCI device is the parent, but not perfect solution for applications. PCI device BDF is not fixed, application may need to search all PCI device to find the actual FPGA Device.

2) Or access using platform device path

/sys/bus/platform/devices/fpga_func_a.0/sysfs_file

Quoted 7 lines
Quoted 3 lines

Applications find the actual function by name easily, but no information about which fpga device it belongs to. It's quite confusing if multiple FPGA devices are in one system.

There's a proposal for adding sysfs nodes that correspond to each FPGA device., with the devices located on each FPGA under them. It makes it easier to see which device is on which FPGA.

Makes sense.

Quoted 12 lines
Quoted 12 lines

'FPGA Device' class is introduced to resolve this problem. Each node under this class represents a fpga device, which may have one or more child devices. Applications only need to search under this FPGA Device class folder to find the child device node it needs.

For example, for the platform has 2 fpga devices, each fpga device has 3 child devices, the hierarchy looks like this.

Two nodes are under /sys/class/fpga/:
/sys/class/fpga/fpga.0
/sys/class/fpga/fpga.1

Quoted 8 lines
Quoted 8 lines

Each node has 1 function A device and 2 function B devices:
/sys/class/fpga/fpga.0/func_a.0 /sys/class/fpga/fpga.0/func_b.0 /sys/class/fpga/fpga.0/func_b.1

/sys/class/fpga/fpga.1/func_a.1
/sys/class/fpga/fpga.1/func_b.2
/sys/class/fpga/fpga.1/func_b.3

A class is generally what is the function of the device, not how it is attached. Seems like what you want here is a new bus type if the existing PCI and platform bus types don't work.

Quoted 5 lines

I can see the value of having sysfs nodes that correspond to fpga devices and being able to find devices under them. I'm thinking what that would mean for Device Tree when fpga-dev is used on DT enabled systems. In Device Tree, what is a fpga-dev?

Just properly setting the parent struct device on the functions should be enough to figure out which function is in which fpga. I don't see why a new class is needed.

Quoted 8 lines
Quoted 5 lines

Currently the DT would have a FPGA bridge corresponding to each FPGA's hardware bridge and a heirarchy of bridges, regions and devices under it. On systems that don't support partial reconfiguration under the OS (so not main bridge that was controlled by the OS), there would be a FPGA region, then its child regions, bridges, and devices.

The FPGA bridges could instantiate fpga bus type devices instead of platform devices.

Quoted 8 lines

Yes

Some FPGA use cases already have a base bridge per FPGA that could serve as this bus. But this use case has a static FPGA image + reprogrammable child fpga regions. There's no base bridge under Linux since the FPGA was programmed and the bridge enabled before Linux
boots. An added base bridge that doesn't touch hardware will be
required for this type of use.

Quoted 4 lines

Hi Alan

Does 'base bridge' mentioned above mean a hardware bridge just like PCIe or USB?

Whatever connects each FPGA to the CPU. One base bridge per FPGA device to create the fpga bus type devices. Each PR region's bridge would also be a bus.

Quoted 7 lines

I tried to use fpga bus type device instead of fpga-dev class today, it works for me, e.g Intel FPGA device PCIe driver could create a fpga bus type dev as a child of PCIe device and its sysfs path will be changed to /sys/bus/fpga/devices/fpga.x/ from /sys/class/fpga/fpga.x/. For now, this fpga bus type device is only used as container device, so no driver needed for it.

That's great! I'd like to see the code to try it out with device tree. Is it part of fpga-bridge or something separate for now?

Quoted 4 lines

Do you have any concern on this? I see fpga bus type works fine, but I didn't see other advantages for this case, as we only use it as a container device to represent a FPGA device in sysfs hierarchy. :)

I could not see a way to make the fpga-dev class compatible with the FPGA Device Tree bindings. This was a red flag. That's why I asked Rob's opinion. Sysfs classes collect devices of a specific type together; busses describe topology. I think the goal of fpga-dev was to describe topology. It's more correct to define this as a bus, not a class. If it's done right, it can work for device tree also.

Alan

Quoted 14 lines

Thanks
Hao

Quoted 10 lines
Quoted 3 lines

That's really up to Linux and outside the scope of the bindings.

Thanks for the feedback.

Alan Tull

Quoted 2 lines

Rob

author
#14 lore raw

Re: [PATCH v2 02/22] fpga: add FPGA device framework

Quoted 109 lines · Alan Tull
Quoted 91 lines · Wu Hao
Quoted 81 lines · Alan Tull
Quoted 71 lines · Rob Herring
Quoted 25 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Rob,

I was hoping to pick your brain a bit on a DT question.

Quoted 19 lines

During FPGA device (e.g PCI-based) discovery, platform devices are registered for different FPGA function units. But the device node path isn't quite friendly to applications.

Consider this case, applications want to access child device's sysfs file for some information.

1) Access using bus-based path (e.g PCI)

/sys/bus/pci/devices/xxxxx/fpga_func_a.0/sysfs_file

From the path, it's clear which PCI device is the parent, but not perfect solution for applications. PCI device BDF is not fixed, application may need to search all PCI device to find the actual FPGA Device.

2) Or access using platform device path

/sys/bus/platform/devices/fpga_func_a.0/sysfs_file

Quoted 7 lines
Quoted 3 lines

Applications find the actual function by name easily, but no information about which fpga device it belongs to. It's quite confusing if multiple FPGA devices are in one system.

There's a proposal for adding sysfs nodes that correspond to each FPGA device., with the devices located on each FPGA under them. It makes it easier to see which device is on which FPGA.

Makes sense.

Quoted 12 lines
Quoted 12 lines

'FPGA Device' class is introduced to resolve this problem. Each node under this class represents a fpga device, which may have one or more child devices. Applications only need to search under this FPGA Device class folder to find the child device node it needs.

For example, for the platform has 2 fpga devices, each fpga device has 3 child devices, the hierarchy looks like this.

Two nodes are under /sys/class/fpga/:
/sys/class/fpga/fpga.0
/sys/class/fpga/fpga.1

Quoted 8 lines
Quoted 8 lines

Each node has 1 function A device and 2 function B devices:
/sys/class/fpga/fpga.0/func_a.0 /sys/class/fpga/fpga.0/func_b.0 /sys/class/fpga/fpga.0/func_b.1

/sys/class/fpga/fpga.1/func_a.1
/sys/class/fpga/fpga.1/func_b.2
/sys/class/fpga/fpga.1/func_b.3

A class is generally what is the function of the device, not how it is attached. Seems like what you want here is a new bus type if the existing PCI and platform bus types don't work.

Quoted 5 lines

I can see the value of having sysfs nodes that correspond to fpga devices and being able to find devices under them. I'm thinking what that would mean for Device Tree when fpga-dev is used on DT enabled systems. In Device Tree, what is a fpga-dev?

Just properly setting the parent struct device on the functions should be enough to figure out which function is in which fpga. I don't see why a new class is needed.

Quoted 8 lines
Quoted 5 lines

Currently the DT would have a FPGA bridge corresponding to each FPGA's hardware bridge and a heirarchy of bridges, regions and devices under it. On systems that don't support partial reconfiguration under the OS (so not main bridge that was controlled by the OS), there would be a FPGA region, then its child regions, bridges, and devices.

The FPGA bridges could instantiate fpga bus type devices instead of platform devices.

Quoted 8 lines

Yes

Some FPGA use cases already have a base bridge per FPGA that could serve as this bus. But this use case has a static FPGA image + reprogrammable child fpga regions. There's no base bridge under Linux since the FPGA was programmed and the bridge enabled before Linux
boots. An added base bridge that doesn't touch hardware will be
required for this type of use.

Quoted 4 lines

Hi Alan

Does 'base bridge' mentioned above mean a hardware bridge just like PCIe or USB?

Whatever connects each FPGA to the CPU. One base bridge per FPGA device to create the fpga bus type devices. Each PR region's bridge would also be a bus.

Quoted 7 lines

I tried to use fpga bus type device instead of fpga-dev class today, it works for me, e.g Intel FPGA device PCIe driver could create a fpga bus type dev as a child of PCIe device and its sysfs path will be changed to /sys/bus/fpga/devices/fpga.x/ from /sys/class/fpga/fpga.x/. For now, this fpga bus type device is only used as container device, so no driver needed for it.

Quoted 3 lines

That's great! I'd like to see the code to try it out with device tree. Is it part of fpga-bridge or something separate for now?

Hi Alan

I just sent the patch I did as a RFC Patch[1] to the mailing list. Please take a look. I only replaced the original fpga-dev class with new 'fpga' bus type, and keep the original interface not changed.

[1] http://marc.info/?l=linux-fpga&m=150167682312708&w=2

Quoted 11 lines
Quoted 4 lines

Do you have any concern on this? I see fpga bus type works fine, but I didn't see other advantages for this case, as we only use it as a container device to represent a FPGA device in sysfs hierarchy. :)

I could not see a way to make the fpga-dev class compatible with the FPGA Device Tree bindings. This was a red flag. That's why I asked Rob's opinion. Sysfs classes collect devices of a specific type together; busses describe topology. I think the goal of fpga-dev was to describe topology. It's more correct to define this as a bus, not a class. If it's done right, it can work for device tree also.

Got it. Thanks. :)

Hao

Quoted 17 lines

Alan

Quoted 14 lines

Thanks
Hao

Quoted 10 lines
Quoted 3 lines

That's really up to Linux and outside the scope of the bindings.

Thanks for the feedback.

Alan Tull

Quoted 2 lines

Rob

#15 lore raw

Re: [PATCH v2 02/22] fpga: add FPGA device framework

Quoted 93 lines · Wu Hao
Quoted 92 lines · Alan Tull
Quoted 91 lines · Wu Hao
Quoted 89 lines · Alan Tull
Quoted 71 lines · Rob Herring
Quoted 25 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Rob,

I was hoping to pick your brain a bit on a DT question.

Quoted 19 lines

During FPGA device (e.g PCI-based) discovery, platform devices are registered for different FPGA function units. But the device node path isn't quite friendly to applications.

Consider this case, applications want to access child device's sysfs file for some information.

1) Access using bus-based path (e.g PCI)

/sys/bus/pci/devices/xxxxx/fpga_func_a.0/sysfs_file

From the path, it's clear which PCI device is the parent, but not perfect solution for applications. PCI device BDF is not fixed, application may need to search all PCI device to find the actual FPGA Device.

2) Or access using platform device path

/sys/bus/platform/devices/fpga_func_a.0/sysfs_file

Quoted 7 lines
Quoted 3 lines

Applications find the actual function by name easily, but no information about which fpga device it belongs to. It's quite confusing if multiple FPGA devices are in one system.

There's a proposal for adding sysfs nodes that correspond to each FPGA device., with the devices located on each FPGA under them. It makes it easier to see which device is on which FPGA.

Makes sense.

Quoted 12 lines
Quoted 12 lines

'FPGA Device' class is introduced to resolve this problem. Each node under this class represents a fpga device, which may have one or more child devices. Applications only need to search under this FPGA Device class folder to find the child device node it needs.

For example, for the platform has 2 fpga devices, each fpga device has 3 child devices, the hierarchy looks like this.

Two nodes are under /sys/class/fpga/:
/sys/class/fpga/fpga.0
/sys/class/fpga/fpga.1

Quoted 8 lines
Quoted 8 lines

Each node has 1 function A device and 2 function B devices:
/sys/class/fpga/fpga.0/func_a.0 /sys/class/fpga/fpga.0/func_b.0 /sys/class/fpga/fpga.0/func_b.1

/sys/class/fpga/fpga.1/func_a.1
/sys/class/fpga/fpga.1/func_b.2
/sys/class/fpga/fpga.1/func_b.3

A class is generally what is the function of the device, not how it is attached. Seems like what you want here is a new bus type if the existing PCI and platform bus types don't work.

Quoted 5 lines

I can see the value of having sysfs nodes that correspond to fpga devices and being able to find devices under them. I'm thinking what that would mean for Device Tree when fpga-dev is used on DT enabled systems. In Device Tree, what is a fpga-dev?

Just properly setting the parent struct device on the functions should be enough to figure out which function is in which fpga. I don't see why a new class is needed.

Quoted 8 lines
Quoted 5 lines

Currently the DT would have a FPGA bridge corresponding to each FPGA's hardware bridge and a heirarchy of bridges, regions and devices under it. On systems that don't support partial reconfiguration under the OS (so not main bridge that was controlled by the OS), there would be a FPGA region, then its child regions, bridges, and devices.

The FPGA bridges could instantiate fpga bus type devices instead of platform devices.

Yes

Some FPGA use cases already have a base bridge per FPGA that could serve as this bus. But this use case has a static FPGA image + reprogrammable child fpga regions. There's no base bridge under Linux since the FPGA was programmed and the bridge enabled before Linux
boots. An added base bridge that doesn't touch hardware will be
required for this type of use.

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Hi Alan

Does 'base bridge' mentioned above mean a hardware bridge just like PCIe or USB?

Whatever connects each FPGA to the CPU. One base bridge per FPGA device to create the fpga bus type devices. Each PR region's bridge would also be a bus.

device.h [1] says it better than me:

* A bus is a channel between the processor and one or more devices.
For the
* purposes of the device model, all devices are connected via a bus,
even if
* it is an internal, virtual, "platform" bus. Buses can plug into
each other.
* A USB controller is usually a PCI device, for example. The device
model
* represents the actual connections between buses and the devices
they control.

A fpga-bridge could be the bus even if the FPGA is on PCIe. The advantage of doing it that way is that this bus will be usable for both embedded and PCIe FPGAs.

Alan

[1] https://github.com/torvalds/linux/blob/master/include/linux/device.h

Quoted 33 lines
Quoted 9 lines
Quoted 8 lines

I tried to use fpga bus type device instead of fpga-dev class today, it works for me, e.g Intel FPGA device PCIe driver could create a fpga bus type dev as a child of PCIe device and its sysfs path will be changed to /sys/bus/fpga/devices/fpga.x/ from /sys/class/fpga/fpga.x/. For now, this fpga bus type device is only used as container device, so no driver needed for it.

Quoted 3 lines

That's great! I'd like to see the code to try it out with device tree. Is it part of fpga-bridge or something separate for now?

Hi Alan

I just sent the patch I did as a RFC Patch[1] to the mailing list. Please take a look. I only replaced the original fpga-dev class with new 'fpga' bus type, and keep the original interface not changed.

[1] http://marc.info/?l=linux-fpga&m=150167682312708&w=2

Quoted 11 lines
Quoted 4 lines

Do you have any concern on this? I see fpga bus type works fine, but I didn't see other advantages for this case, as we only use it as a container device to represent a FPGA device in sysfs hierarchy. :)

I could not see a way to make the fpga-dev class compatible with the FPGA Device Tree bindings. This was a red flag. That's why I asked Rob's opinion. Sysfs classes collect devices of a specific type together; busses describe topology. I think the goal of fpga-dev was to describe topology. It's more correct to define this as a bus, not a class. If it's done right, it can work for device tree also.

Quoted 21 lines

Got it. Thanks. :)

Hao

Quoted 17 lines

Alan

Quoted 14 lines

Thanks
Hao

Quoted 10 lines
Quoted 3 lines

That's really up to Linux and outside the scope of the bindings.

Thanks for the feedback.

Alan Tull

Quoted 2 lines

Rob

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#16 lore raw

Re: [PATCH v2 02/22] fpga: add FPGA device framework

In reply to Rob Herring
Quoted 71 lines · Rob Herring
Quoted 25 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:51 PM, Wu Hao <[email protected]> wrote:

Hi Rob,

I was hoping to pick your brain a bit on a DT question.

Quoted 19 lines

During FPGA device (e.g PCI-based) discovery, platform devices are registered for different FPGA function units. But the device node path isn't quite friendly to applications.

Consider this case, applications want to access child device's sysfs file for some information.

1) Access using bus-based path (e.g PCI)

/sys/bus/pci/devices/xxxxx/fpga_func_a.0/sysfs_file

From the path, it's clear which PCI device is the parent, but not perfect solution for applications. PCI device BDF is not fixed, application may need to search all PCI device to find the actual FPGA Device.

2) Or access using platform device path

/sys/bus/platform/devices/fpga_func_a.0/sysfs_file

Quoted 7 lines
Quoted 3 lines

Applications find the actual function by name easily, but no information about which fpga device it belongs to. It's quite confusing if multiple FPGA devices are in one system.

There's a proposal for adding sysfs nodes that correspond to each FPGA device., with the devices located on each FPGA under them. It makes it easier to see which device is on which FPGA.

Makes sense.

Quoted 12 lines
Quoted 12 lines

'FPGA Device' class is introduced to resolve this problem. Each node under this class represents a fpga device, which may have one or more child devices. Applications only need to search under this FPGA Device class folder to find the child device node it needs.

For example, for the platform has 2 fpga devices, each fpga device has 3 child devices, the hierarchy looks like this.

Two nodes are under /sys/class/fpga/:
/sys/class/fpga/fpga.0
/sys/class/fpga/fpga.1

Quoted 8 lines
Quoted 8 lines

Each node has 1 function A device and 2 function B devices:
/sys/class/fpga/fpga.0/func_a.0 /sys/class/fpga/fpga.0/func_b.0 /sys/class/fpga/fpga.0/func_b.1

/sys/class/fpga/fpga.1/func_a.1
/sys/class/fpga/fpga.1/func_b.2
/sys/class/fpga/fpga.1/func_b.3

A class is generally what is the function of the device, not how it is attached. Seems like what you want here is a new bus type if the existing PCI and platform bus types don't work.

Quoted 5 lines

I can see the value of having sysfs nodes that correspond to fpga devices and being able to find devices under them. I'm thinking what that would mean for Device Tree when fpga-dev is used on DT enabled systems. In Device Tree, what is a fpga-dev?

Just properly setting the parent struct device on the functions should be enough to figure out which function is in which fpga. I don't see why a new class is needed.

Quoted 8 lines
Quoted 5 lines

Currently the DT would have a FPGA bridge corresponding to each FPGA's hardware bridge and a heirarchy of bridges, regions and devices under it. On systems that don't support partial reconfiguration under the OS (so not main bridge that was controlled by the OS), there would be a FPGA region, then its child regions, bridges, and devices.

The FPGA bridges could instantiate fpga bus type devices instead of platform devices.

Seems like of_platform_bus_create() would have to be expanded to support FPGA bus devices, right? Or is it acceptable to create platform devices under the FPGA bus? I'm still pondering this. Currently this patchset is creating platform devices under a PCIe bus.

Alan

Quoted 4 lines

That's really up to Linux and outside the scope of the bindings.

Rob

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#17 lore raw

Re: [PATCH v2 02/22] fpga: add FPGA device framework

In reply to Wu Hao
Quoted 4 lines · Wu Hao

During FPGA device (e.g PCI-based) discovery, platform devices are registered for different FPGA function units. But the device node path isn't quite friendly to applications.

Hi Hao,

Please add a document for fpga-dev under Documentation/fpga to help future users. The header of this patch explains things pretty well and could be used with a little formatting for the document.

Alan

author
#18 lore raw

Re: [PATCH v2 02/22] fpga: add FPGA device framework

Quoted 11 lines · Alan Tull
Quoted 4 lines · Wu Hao

During FPGA device (e.g PCI-based) discovery, platform devices are registered for different FPGA function units. But the device node path isn't quite friendly to applications.

Hi Hao,

Please add a document for fpga-dev under Documentation/fpga to help future users. The header of this patch explains things pretty well and could be used with a little formatting for the document.

Sure, will include one document for fpga-dev in the next version patch set.

Thanks
Hao

Quoted 2 lines

Alan

author
#19 lore raw

PATCH v2 3/22 fpga: bridge: remove OF dependency for fpga-bridge

In reply to Wu Hao

This patch removes OF dependency of fpga-bridge, it allows drivers to use fpga-bridge class without device tree support.

---
 drivers/fpga/Kconfig | 1 -
 1 file changed, 1 deletion(-)
drivers/fpga/Kconfig +0 -1
index ed600d5..c1d8f41 100644
@@ -75,7 +75,6 @@ config FPGA_MGR_ZYNQ_FPGA
 
 config FPGA_BRIDGE
 	tristate "FPGA Bridge Framework"
-	depends on OF
 	help
 	  Say Y here if you want to support bridges connected between host
 	  processors and FPGAs or between FPGAs.
Signature · 1 line
1.8.3.1
maintainer
#22 lore raw

Re: [PATCH v2 03/22] fpga: bridge: remove OF dependency for fpga-bridge

In reply to Wu Hao

Hi,

Quoted 13 lines · Wu Hao

This patch removes OF dependency of fpga-bridge, it allows drivers to use fpga-bridge class without device tree support.

---
 drivers/fpga/Kconfig | 1 -
 1 file changed, 1 deletion(-)
drivers/fpga/Kconfig +0 -0
index ed600d5..c1d8f41 100644

drivers/fpga/Kconfig config FPGA_MGR_ZYNQ_FPGA · line 78

Quoted 4 lines
drivers/fpga/Kconfig +0 -1
@@ -75,7 +75,6 @@ config FPGA_MGR_ZYNQ_FPGA
 
 config FPGA_BRIDGE
 	tristate "FPGA Bridge Framework"
-	depends on OF

Shouldn't that dependency be removed in the patch that removes the dependency, or was the dependency never there in the first place?

Quoted 6 lines
drivers/fpga/Kconfig +0 -0
 	help
 	  Say Y here if you want to support bridges connected between host
 	  processors and FPGAs or between FPGAs.
Signature · 1 line
1.8.3.1

Thanks,

Moritz

#23 lore raw

Re: [PATCH v2 03/22] fpga: bridge: remove OF dependency for fpga-bridge

On Thu, Sep 21, 2017 at 2:11 PM, Moritz Fischer <[email protected]> wrote:

Hi Moritz,

Quoted 17 lines

Hi,

Quoted 14 lines · Wu Hao

This patch removes OF dependency of fpga-bridge, it allows drivers to use fpga-bridge class without device tree support.

---
 drivers/fpga/Kconfig | 1 -
 1 file changed, 1 deletion(-)
drivers/fpga/Kconfig +0 -0
index ed600d5..c1d8f41 100644
@@ -75,7 +75,6 @@ config FPGA_MGR_ZYNQ_FPGA
 
Quoted 6 lines

drivers/fpga/Kconfig config FPGA_MGR_ZYNQ_FPGA · line 78

Quoted 3 lines
drivers/fpga/Kconfig +0 -1
 config FPGA_BRIDGE
      tristate "FPGA Bridge Framework"
-     depends on OF

Shouldn't that dependency be removed in the patch that removes the dependency, or was the dependency never there in the first place?

It wasn't dependent on OF in the first place.

Thanks,
Alan

Quoted 11 lines
Quoted 7 lines
drivers/fpga/Kconfig +0 -1
      help
        Say Y here if you want to support bridges connected between host
        processors and FPGAs or between FPGAs.
--

1.8.3.1

Thanks,

Moritz

author
#24 lore raw

Re: [PATCH v2 03/22] fpga: bridge: remove OF dependency for fpga-bridge

Quoted 21 lines · Alan Tull

On Thu, Sep 21, 2017 at 2:11 PM, Moritz Fischer <[email protected]> wrote:

Hi Moritz,

Quoted 17 lines

Hi,

Quoted 14 lines · Wu Hao

This patch removes OF dependency of fpga-bridge, it allows drivers to use fpga-bridge class without device tree support.

---
 drivers/fpga/Kconfig | 1 -
 1 file changed, 1 deletion(-)
drivers/fpga/Kconfig +0 -0
index ed600d5..c1d8f41 100644
@@ -75,7 +75,6 @@ config FPGA_MGR_ZYNQ_FPGA
 
Quoted 8 lines
Quoted 6 lines

drivers/fpga/Kconfig config FPGA_MGR_ZYNQ_FPGA · line 78

Quoted 3 lines
drivers/fpga/Kconfig +0 -1
 config FPGA_BRIDGE
      tristate "FPGA Bridge Framework"
-     depends on OF

Shouldn't that dependency be removed in the patch that removes the dependency, or was the dependency never there in the first place?

It wasn't dependent on OF in the first place.

Hi Alan / Moritz

I see this change has been integrated into below patch from Alan.

[PATCH v4 15/18] fpga: region: move device tree support to of-fpga-region.c

so I think I can drop this patch in my next version patch set. : )

Thanks
Hao

Quoted 19 lines

Thanks,
Alan

Quoted 11 lines
Quoted 7 lines
drivers/fpga/Kconfig +0 -1
      help
        Say Y here if you want to support bridges connected between host
        processors and FPGAs or between FPGAs.
--

1.8.3.1

Thanks,

Moritz

--
To unsubscribe from this list: send the line "unsubscribe linux-fpga" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#25 lore raw

Re: [PATCH v2 03/22] fpga: bridge: remove OF dependency for fpga-bridge

Quoted 31 lines · Wu Hao
Quoted 21 lines · Alan Tull

On Thu, Sep 21, 2017 at 2:11 PM, Moritz Fischer <[email protected]> wrote:

Hi Moritz,

Quoted 17 lines

Hi,

Quoted 14 lines · Wu Hao

This patch removes OF dependency of fpga-bridge, it allows drivers to use fpga-bridge class without device tree support.

---
 drivers/fpga/Kconfig | 1 -
 1 file changed, 1 deletion(-)
drivers/fpga/Kconfig +0 -0
index ed600d5..c1d8f41 100644
@@ -75,7 +75,6 @@ config FPGA_MGR_ZYNQ_FPGA
 
Quoted 8 lines
Quoted 6 lines

drivers/fpga/Kconfig config FPGA_MGR_ZYNQ_FPGA · line 78

Quoted 3 lines
drivers/fpga/Kconfig +0 -1
 config FPGA_BRIDGE
      tristate "FPGA Bridge Framework"
-     depends on OF

Shouldn't that dependency be removed in the patch that removes the dependency, or was the dependency never there in the first place?

It wasn't dependent on OF in the first place.

Quoted 7 lines

Hi Alan / Moritz

I see this change has been integrated into below patch from Alan.

[PATCH v4 15/18] fpga: region: move device tree support to of-fpga-region.c

so I think I can drop this patch in my next version patch set. : )

Hi Hao,

Yes, I went ahead and pulled it into my patchset so you can drop it.

Thanks for the patch!

Alan

Quoted 23 lines

Thanks
Hao

Quoted 19 lines

Thanks,
Alan

Quoted 11 lines
Quoted 7 lines
drivers/fpga/Kconfig +0 -1
      help
        Say Y here if you want to support bridges connected between host
        processors and FPGAs or between FPGAs.
--

1.8.3.1

Thanks,

Moritz

--
To unsubscribe from this list: send the line "unsubscribe linux-fpga" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#26 lore raw

PATCH v2 4/22 fpga: mgr: add region_id to fpga_image_info

In reply to Wu Hao

This patch adds region_id to fpga_image_info data structure, it allows driver to pass region id information to fpga-mgr via fpga_image_info for fpga reconfiguration function.

---
 include/linux/fpga/fpga-mgr.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/fpga/fpga-mgr.h +1 -0
index 66d0e32..b222a57 100644
@@ -95,6 +95,7 @@ struct fpga_image_info {
 	struct sg_table *sgt;
 	const char *buf;
 	size_t count;
+	int region_id;
 #ifdef CONFIG_OF
 	struct device_node *overlay;
 #endif
Signature · 1 line
1.8.3.1
#27 lore raw

Re: [PATCH v2 04/22] fpga: mgr: add region_id to fpga_image_info

On Sun, Jun 25, 2017 at 8:52 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

Quoted 14 lines

This patch adds region_id to fpga_image_info data structure, it allows driver to pass region id information to fpga-mgr via fpga_image_info for fpga reconfiguration function.

---
 include/linux/fpga/fpga-mgr.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/fpga/fpga-mgr.h +0 -0
index 66d0e32..b222a57 100644

include/linux/fpga/fpga-mgr.h struct fpga_image_info { · line 98

Quoted 4 lines
include/linux/fpga/fpga-mgr.h +1 -0
@@ -95,6 +95,7 @@ struct fpga_image_info {
        struct sg_table *sgt;
        const char *buf;
        size_t count;
+       int region_id;

Please add a line to document this region_id above where all the other struct members are documented.

Alan

Quoted 6 lines
include/linux/fpga/fpga-mgr.h +0 -1
 #ifdef CONFIG_OF
        struct device_node *overlay;
 #endif
--

1.8.3.1

author
#28 lore raw

Re: [PATCH v2 04/22] fpga: mgr: add region_id to fpga_image_info

Quoted 13 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:52 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

Quoted 9 lines

This patch adds region_id to fpga_image_info data structure, it allows driver to pass region id information to fpga-mgr via fpga_image_info for fpga reconfiguration function.

---
 include/linux/fpga/fpga-mgr.h | 1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

include/linux/fpga/fpga-mgr.h struct fpga_image_info { · line 98

Quoted 9 lines
include/linux/fpga/fpga-mgr.h +1 -0
index 66d0e32..b222a57 100644
@@ -95,6 +95,7 @@ struct fpga_image_info {
        struct sg_table *sgt;
        const char *buf;
        size_t count;
+       int region_id;

Please add a line to document this region_id above where all the other struct members are documented.

Hi Alan

Thanks for the comments, I will fix this in the next version.

* @sgt: scatter/gather table containing FPGA image
* @buf: contiguous buffer containing FPGA image
* @count: size of buf
+ * @region_id: id of target region
* @overlay: Device Tree overlay
*/
struct fpga_image_info {

Thanks
Hao

Quoted 9 lines

Alan

Quoted 6 lines
include/linux/fpga/fpga-mgr.h +0 -1
 #ifdef CONFIG_OF
        struct device_node *overlay;
 #endif
--

1.8.3.1

author
#29 lore raw

PATCH v2 5/22 fpga: mgr: add status for fpga-mgr

In reply to Wu Hao

This patch adds status to fpga-manager data structure, to allow driver to store full/partial reconfiguration errors and other status information.

one sysfs interface created for user space application to read fpga-manager status.

---
 Documentation/ABI/testing/sysfs-class-fpga-manager | 10 +++++++++
 drivers/fpga/fpga-mgr.c                            | 24 ++++++++++++++++++++++
 include/linux/fpga/fpga-mgr.h                      |  9 ++++++++
 3 files changed, 43 insertions(+)
Documentation/ABI/testing/sysfs-class-fpga-manager +10 -0
index 23056c5..71b083e 100644
@@ -35,3 +35,13 @@ Description:	Read fpga manager state as a string.
 		* write complete	= Doing post programming steps
 		* write complete error	= Error while doing post programming
 		* operating		= FPGA is programmed and operating
+
+What:		/sys/class/fpga_manager/<fpga>/status
+Date:		June 2017
+KernelVersion:	4.12
+Contact:	Wu Hao <[email protected]>
+Description:	Read fpga manager status as a string.
+		If FPGA programming operation fails, it could be due to crc
+		error or incompatible bitstream image. The intent of this
+		interface is to provide more detailed information for FPGA
+		programming errors to userspace.
drivers/fpga/fpga-mgr.c +24 -0
index be13cce..2485658 100644
@@ -388,12 +388,36 @@ static ssize_t state_show(struct device *dev,
 	return sprintf(buf, "%s\n", state_str[mgr->state]);
 }
 
+static ssize_t status_show(struct device *dev,
+			   struct device_attribute *attr, char *buf)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+	int len = 0;
+
+	if (mgr->status & FPGA_MGR_STATUS_OPERATION_ERR)
+		len += sprintf(buf + len, "reconfig operation error\n");
+	if (mgr->status & FPGA_MGR_STATUS_CRC_ERR)
+		len += sprintf(buf + len, "reconfig crc error\n");
+	if (mgr->status & FPGA_MGR_STATUS_INCOMPATIBLE_BS_ERR)
+		len += sprintf(buf + len, "reconfig incompatible BS error\n");
+	if (mgr->status & FPGA_MGR_STATUS_IP_PROTOCOL_ERR)
+		len += sprintf(buf + len, "reconfig IP protocol error\n");
+	if (mgr->status & FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR)
+		len += sprintf(buf + len, "reconfig fifo overflow error\n");
+	if (mgr->status & FPGA_MGR_STATUS_SECURE_LOAD_ERR)
+		len += sprintf(buf + len, "reconfig secure load error\n");
+
+	return len;
+}
+
 static DEVICE_ATTR_RO(name);
 static DEVICE_ATTR_RO(state);
+static DEVICE_ATTR_RO(status);
 
 static struct attribute *fpga_mgr_attrs[] = {
 	&dev_attr_name.attr,
 	&dev_attr_state.attr,
+	&dev_attr_status.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(fpga_mgr);
include/linux/fpga/fpga-mgr.h +9 -0
index b222a57..8cb42ac 100644
@@ -128,6 +128,14 @@ struct fpga_manager_ops {
 	void (*fpga_remove)(struct fpga_manager *mgr);
 };
 
+/* FPGA manager status: Partial/Full Reconfiguration errors */
+#define FPGA_MGR_STATUS_OPERATION_ERR		BIT(0)
+#define FPGA_MGR_STATUS_CRC_ERR			BIT(1)
+#define FPGA_MGR_STATUS_INCOMPATIBLE_BS_ERR	BIT(2)
+#define FPGA_MGR_STATUS_IP_PROTOCOL_ERR		BIT(3)
+#define FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR	BIT(4)
+#define FPGA_MGR_STATUS_SECURE_LOAD_ERR		BIT(5)
+
 /**
  * struct fpga_manager - fpga manager structure
  * @name: name of low level fpga manager
@@ -142,6 +150,7 @@ struct fpga_manager {
 	struct device dev;
 	struct mutex ref_mutex;
 	enum fpga_mgr_states state;
+	u64 status;
 	const struct fpga_manager_ops *mops;
 	void *priv;
 };
Signature · 1 line
1.8.3.1
#30 lore raw

Re: [PATCH v2 05/22] fpga: mgr: add status for fpga-mgr

On Sun, Jun 25, 2017 at 8:52 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

Quoted 78 lines

This patch adds status to fpga-manager data structure, to allow driver to store full/partial reconfiguration errors and other status information.

one sysfs interface created for user space application to read fpga-manager status.

---
 Documentation/ABI/testing/sysfs-class-fpga-manager | 10 +++++++++
 drivers/fpga/fpga-mgr.c                            | 24 ++++++++++++++++++++++
 include/linux/fpga/fpga-mgr.h                      |  9 ++++++++
 3 files changed, 43 insertions(+)
Documentation/ABI/testing/sysfs-class-fpga-manager +10 -0
index 23056c5..71b083e 100644
@@ -35,3 +35,13 @@ Description: Read fpga manager state as a string.
                * write complete        = Doing post programming steps
                * write complete error  = Error while doing post programming
                * operating             = FPGA is programmed and operating
+
+What:          /sys/class/fpga_manager/<fpga>/status
+Date:          June 2017
+KernelVersion: 4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read fpga manager status as a string.
+               If FPGA programming operation fails, it could be due to crc
+               error or incompatible bitstream image. The intent of this
+               interface is to provide more detailed information for FPGA
+               programming errors to userspace.
drivers/fpga/fpga-mgr.c +24 -0
index be13cce..2485658 100644
@@ -388,12 +388,36 @@ static ssize_t state_show(struct device *dev,
        return sprintf(buf, "%s\n", state_str[mgr->state]);
 }
 
+static ssize_t status_show(struct device *dev,
+                          struct device_attribute *attr, char *buf)
+{
+       struct fpga_manager *mgr = to_fpga_manager(dev);
+       int len = 0;
+
+       if (mgr->status & FPGA_MGR_STATUS_OPERATION_ERR)
+               len += sprintf(buf + len, "reconfig operation error\n");
+       if (mgr->status & FPGA_MGR_STATUS_CRC_ERR)
+               len += sprintf(buf + len, "reconfig crc error\n");
+       if (mgr->status & FPGA_MGR_STATUS_INCOMPATIBLE_BS_ERR)
+               len += sprintf(buf + len, "reconfig incompatible BS error\n");
+       if (mgr->status & FPGA_MGR_STATUS_IP_PROTOCOL_ERR)
+               len += sprintf(buf + len, "reconfig IP protocol error\n");
+       if (mgr->status & FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR)
+               len += sprintf(buf + len, "reconfig fifo overflow error\n");
+       if (mgr->status & FPGA_MGR_STATUS_SECURE_LOAD_ERR)
+               len += sprintf(buf + len, "reconfig secure load error\n");
+
+       return len;
+}
+
 static DEVICE_ATTR_RO(name);
 static DEVICE_ATTR_RO(state);
+static DEVICE_ATTR_RO(status);
 
 static struct attribute *fpga_mgr_attrs[] = {
        &dev_attr_name.attr,
        &dev_attr_state.attr,
+       &dev_attr_status.attr,
        NULL,
 };
 ATTRIBUTE_GROUPS(fpga_mgr);
include/linux/fpga/fpga-mgr.h +0 -0
index b222a57..8cb42ac 100644

include/linux/fpga/fpga-mgr.h struct fpga_manager_ops { · line 134

Quoted 7 lines
include/linux/fpga/fpga-mgr.h +4 -0
@@ -128,6 +128,14 @@ struct fpga_manager_ops {
        void (*fpga_remove)(struct fpga_manager *mgr);
 };
 
+/* FPGA manager status: Partial/Full Reconfiguration errors */
+#define FPGA_MGR_STATUS_OPERATION_ERR          BIT(0)
+#define FPGA_MGR_STATUS_CRC_ERR                        BIT(1)
+#define FPGA_MGR_STATUS_INCOMPATIBLE_BS_ERR    BIT(2)

How about ..._INCOMPATIBLE_IMAGE_ERR? :)

include/linux/fpga/fpga-mgr.h struct fpga_manager { · line 153

Quoted 12 lines
include/linux/fpga/fpga-mgr.h +5 -0
+#define FPGA_MGR_STATUS_IP_PROTOCOL_ERR                BIT(3)
+#define FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR      BIT(4)
+#define FPGA_MGR_STATUS_SECURE_LOAD_ERR                BIT(5)
+
 /**
  * struct fpga_manager - fpga manager structure
  * @name: name of low level fpga manager
@@ -142,6 +150,7 @@ struct fpga_manager {
        struct device dev;
        struct mutex ref_mutex;
        enum fpga_mgr_states state;
+       u64 status;

With this implementation, the low level driver sets ops->status and that could be stale by the time the framework looks at it. I suggest adding a function to fpga_manager_ops that returns the status. That way whenever the status is requested, the low level driver will have the opportunity to read status registers.

Besides this, this new sysfs looks helpful.

Alan

Quoted 6 lines
include/linux/fpga/fpga-mgr.h +0 -1
        const struct fpga_manager_ops *mops;
        void *priv;
 };
--

1.8.3.1

author
#31 lore raw

Re: [PATCH v2 05/22] fpga: mgr: add status for fpga-mgr

Quoted 4 lines · Alan Tull

On Sun, Jun 25, 2017 at 8:52 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

Hi Alan

Thanks a lot for your feedback. : )

Quoted 81 lines
Quoted 81 lines

This patch adds status to fpga-manager data structure, to allow driver to store full/partial reconfiguration errors and other status information.

one sysfs interface created for user space application to read fpga-manager status.

---
 Documentation/ABI/testing/sysfs-class-fpga-manager | 10 +++++++++
 drivers/fpga/fpga-mgr.c                            | 24 ++++++++++++++++++++++
 include/linux/fpga/fpga-mgr.h                      |  9 ++++++++
 3 files changed, 43 insertions(+)
Documentation/ABI/testing/sysfs-class-fpga-manager +10 -0
index 23056c5..71b083e 100644
@@ -35,3 +35,13 @@ Description: Read fpga manager state as a string.
                * write complete        = Doing post programming steps
                * write complete error  = Error while doing post programming
                * operating             = FPGA is programmed and operating
+
+What:          /sys/class/fpga_manager/<fpga>/status
+Date:          June 2017
+KernelVersion: 4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read fpga manager status as a string.
+               If FPGA programming operation fails, it could be due to crc
+               error or incompatible bitstream image. The intent of this
+               interface is to provide more detailed information for FPGA
+               programming errors to userspace.
drivers/fpga/fpga-mgr.c +24 -0
index be13cce..2485658 100644
@@ -388,12 +388,36 @@ static ssize_t state_show(struct device *dev,
        return sprintf(buf, "%s\n", state_str[mgr->state]);
 }
 
+static ssize_t status_show(struct device *dev,
+                          struct device_attribute *attr, char *buf)
+{
+       struct fpga_manager *mgr = to_fpga_manager(dev);
+       int len = 0;
+
+       if (mgr->status & FPGA_MGR_STATUS_OPERATION_ERR)
+               len += sprintf(buf + len, "reconfig operation error\n");
+       if (mgr->status & FPGA_MGR_STATUS_CRC_ERR)
+               len += sprintf(buf + len, "reconfig crc error\n");
+       if (mgr->status & FPGA_MGR_STATUS_INCOMPATIBLE_BS_ERR)
+               len += sprintf(buf + len, "reconfig incompatible BS error\n");
+       if (mgr->status & FPGA_MGR_STATUS_IP_PROTOCOL_ERR)
+               len += sprintf(buf + len, "reconfig IP protocol error\n");
+       if (mgr->status & FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR)
+               len += sprintf(buf + len, "reconfig fifo overflow error\n");
+       if (mgr->status & FPGA_MGR_STATUS_SECURE_LOAD_ERR)
+               len += sprintf(buf + len, "reconfig secure load error\n");
+
+       return len;
+}
+
 static DEVICE_ATTR_RO(name);
 static DEVICE_ATTR_RO(state);
+static DEVICE_ATTR_RO(status);
 
 static struct attribute *fpga_mgr_attrs[] = {
        &dev_attr_name.attr,
        &dev_attr_state.attr,
+       &dev_attr_status.attr,
        NULL,
 };
 ATTRIBUTE_GROUPS(fpga_mgr);
include/linux/fpga/fpga-mgr.h +0 -0
index b222a57..8cb42ac 100644
@@ -128,6 +128,14 @@ struct fpga_manager_ops {
        void (*fpga_remove)(struct fpga_manager *mgr);
 };
 
Quoted 6 lines

include/linux/fpga/fpga-mgr.h struct fpga_manager_ops { · line 134

Quoted 4 lines
include/linux/fpga/fpga-mgr.h +4 -0
+/* FPGA manager status: Partial/Full Reconfiguration errors */
+#define FPGA_MGR_STATUS_OPERATION_ERR          BIT(0)
+#define FPGA_MGR_STATUS_CRC_ERR                        BIT(1)
+#define FPGA_MGR_STATUS_INCOMPATIBLE_BS_ERR    BIT(2)

How about ..._INCOMPATIBLE_IMAGE_ERR? :)

Hm.. It sounds better to me. : )
Will change it in next version patch set.

Quoted 20 lines

include/linux/fpga/fpga-mgr.h struct fpga_manager { · line 153

Quoted 13 lines
include/linux/fpga/fpga-mgr.h +5 -0
+#define FPGA_MGR_STATUS_IP_PROTOCOL_ERR                BIT(3)
+#define FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR      BIT(4)
+#define FPGA_MGR_STATUS_SECURE_LOAD_ERR                BIT(5)
+
 /**
  * struct fpga_manager - fpga manager structure
  * @name: name of low level fpga manager
@@ -142,6 +150,7 @@ struct fpga_manager {
        struct device dev;
        struct mutex ref_mutex;
        enum fpga_mgr_states state;
+       u64 status;

With this implementation, the low level driver sets ops->status and that could be stale by the time the framework looks at it. I suggest adding a function to fpga_manager_ops that returns the status. That way whenever the status is requested, the low level driver will have the opportunity to read status registers.

Quoted 1 line

Besides this, this new sysfs looks helpful.

I get your point, then I will add one member to fpga_manager_ops.

@@ -118,6 +118,7 @@ struct fpga_image_info {

 struct fpga_manager_ops {
        size_t initial_header_size;
        enum fpga_mgr_states (*state)(struct fpga_manager *mgr);

+ u64 (*status)(struct fpga_manager *mgr);
int (*write_init)(struct fpga_manager *mgr,
struct fpga_image_info *info, const char *buf, size_t count);

and add common action to get status (pr error) in complete function.

@@ -139,6 +139,8 @@ static int fpga_mgr_write_complete(struct fpga_manager *mgr,

        if (ret) {
                dev_err(&mgr->dev, "Error after writing image data to FPGA\n");
                mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
+               if (mgr->mops->status)
+                       mgr->status = mgr->mops->status(mgr);
                return ret;
        }
        mgr->state = FPGA_MGR_STATE_OPERATING;

after status is updated, then user could read it from the sysfs interface.

How do you think about this?

Thanks
Hao

author
#32 lore raw

PATCH v2 6/22 fpga: intel: add FPGA PCIe device driver

In reply to Wu Hao

From: Zhang Yi <[email protected]>

The Intel FPGA device appears as a PCIe device on the system. This patch implements the basic framework of the driver for Intel PCIe device which locates between CPU and Accelerated Function Units (AFUs).

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.

---
 drivers/fpga/Kconfig      |  28 +++++++++++
 drivers/fpga/Makefile     |   5 ++
 drivers/fpga/intel-pcie.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 159 insertions(+)
 create mode 100644 drivers/fpga/intel-pcie.c
drivers/fpga/Kconfig +28 -0
index c1d8f41..3f3b7f4 100644
@@ -117,6 +117,34 @@ config XILINX_PR_DECOUPLER
 	  region of the FPGA from the busses while that region is
 	  being reprogrammed during partial reconfig.
 
+menuconfig INTEL_FPGA
+	tristate "Intel(R) FPGA support"
+	depends on FPGA_DEVICE
+	help
+	  Select this option to enable driver support for Intel(R)
+	  Field-Programmable Gate Array (FPGA) solutions. This driver
+	  provides interfaces for userspace applications to configure,
+	  enumerate, open, and access FPGA accelerators on platforms
+	  equipped with Intel(R) FPGA solutions and enables system
+	  level management functions such as FPGA reconfiguration,
+	  power management, and virtualization.
+
+	  Say Y if your platform has this technology. Say N if unsure.
+
+if INTEL_FPGA
+
+config INTEL_FPGA_PCI
+	tristate "Intel FPGA PCIe Driver"
+	depends on PCI
+	help
+	  This is the driver for the PCIe device which locates between
+	  CPU and Accelerated Function Units (AFUs) and allows them to
+	  communicate with each other.
+
+	  To compile this as a module, choose M here.
+
+endif
+
 endif # FPGA
 
 endmenu
drivers/fpga/Makefile +5 -0
index 8950a8f..5613133 100644
@@ -27,3 +27,8 @@ obj-$(CONFIG_XILINX_PR_DECOUPLER)	+= xilinx-pr-decoupler.o
 # High Level Interfaces
 obj-$(CONFIG_FPGA_REGION)		+= fpga-region.o
 obj-$(CONFIG_OF_FPGA_REGION)		+= of-fpga-region.o
+
+# Intel FPGA Support
+obj-$(CONFIG_INTEL_FPGA_PCI)		+= intel-fpga-pci.o
+
+intel-fpga-pci-objs := intel-pcie.o
drivers/fpga/intel-pcie.c new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#33 lore raw

Re: [PATCH v2 06/22] fpga: intel: add FPGA PCIe device driver

On Sun, Jun 25, 2017 at 8:52 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

Making my way through your patchset.

Quoted 5 lines

From: Zhang Yi <[email protected]>

The Intel FPGA device appears as a PCIe device on the system. This patch implements the basic framework of the driver for Intel PCIe device which locates between CPU and Accelerated Function Units (AFUs).

...which is located between the CPU and...

Quoted 24 lines

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.

---
 drivers/fpga/Kconfig      |  28 +++++++++++
 drivers/fpga/Makefile     |   5 ++
 drivers/fpga/intel-pcie.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 159 insertions(+)
 create mode 100644 drivers/fpga/intel-pcie.c
drivers/fpga/Kconfig +0 -0
index c1d8f41..3f3b7f4 100644

drivers/fpga/Kconfig config XILINX_PR_DECOUPLER · line 125

Quoted 9 lines
drivers/fpga/Kconfig +6 -0
@@ -117,6 +117,34 @@ config XILINX_PR_DECOUPLER
          region of the FPGA from the busses while that region is
          being reprogrammed during partial reconfig.
 
+menuconfig INTEL_FPGA
+       tristate "Intel(R) FPGA support"
+       depends on FPGA_DEVICE
+       help
+         Select this option to enable driver support for Intel(R)
+         Field-Programmable Gate Array (FPGA) solutions. This driver

This is confusing since there is FPGA support for Intel FPGA devices in the kernel already such as any of the Altera support that already is in this Kconfig. This description will have to be more specific that this is one particular Intel solution.

Quoted 5 lines
drivers/fpga/Kconfig +5 -0
+         provides interfaces for userspace applications to configure,
+         enumerate, open, and access FPGA accelerators on platforms
+         equipped with Intel(R) FPGA solutions and enables system
+         level management functions such as FPGA reconfiguration,
+         power management, and virtualization.

drivers/fpga/Kconfig config XILINX_PR_DECOUPLER · line 140

Quoted 10 lines
drivers/fpga/Kconfig +10 -0
+
+         Say Y if your platform has this technology. Say N if unsure.
+
+if INTEL_FPGA
+
+config INTEL_FPGA_PCI
+       tristate "Intel FPGA PCIe Driver"
+       depends on PCI
+       help
+         This is the driver for the PCIe device which locates between

...which is located between...

Quoted 91 lines
drivers/fpga/Kconfig +7 -0
+         CPU and Accelerated Function Units (AFUs) and allows them to
+         communicate with each other.
+
+         To compile this as a module, choose M here.
+
+endif
+
 endif # FPGA
 
 endmenu
drivers/fpga/Makefile +5 -0
index 8950a8f..5613133 100644
@@ -27,3 +27,8 @@ obj-$(CONFIG_XILINX_PR_DECOUPLER)     += xilinx-pr-decoupler.o
 # High Level Interfaces
 obj-$(CONFIG_FPGA_REGION)              += fpga-region.o
 obj-$(CONFIG_OF_FPGA_REGION)           += of-fpga-region.o
+
+# Intel FPGA Support
+obj-$(CONFIG_INTEL_FPGA_PCI)           += intel-fpga-pci.o
+
+intel-fpga-pci-objs := intel-pcie.o
drivers/fpga/intel-pcie.c new file +62 -0
new file mode 100644
index 0000000..f697de4
@@ -0,0 +1,126 @@
+/*
+ * Driver for Intel FPGA PCIe device
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Zhang Yi <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *   Joseph Grecco <[email protected]>
+ *   Enno Luebbers <[email protected]>
+ *   Tim Whisonant <[email protected]>
+ *   Ananda Ravuri <[email protected]>
+ *   Henry Mitchel <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include <linux/pci.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/stddef.h>
+#include <linux/errno.h>
+#include <linux/aer.h>
+
+#define DRV_VERSION    "0.8"
+#define DRV_NAME       "intel-fpga-pci"
+
+/* PCI Device ID */
+#define PCIe_DEVICE_ID_PF_INT_5_X      0xBCBD
+#define PCIe_DEVICE_ID_PF_INT_6_X      0xBCC0
+#define PCIe_DEVICE_ID_PF_DSC_1_X      0x09C4
+/* VF Device */
+#define PCIe_DEVICE_ID_VF_INT_5_X      0xBCBF
+#define PCIe_DEVICE_ID_VF_INT_6_X      0xBCC1
+#define PCIe_DEVICE_ID_VF_DSC_1_X      0x09C5
+
+static struct pci_device_id cci_pcie_id_tbl[] = {
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_PF_INT_5_X),},
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_VF_INT_5_X),},
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_PF_INT_6_X),},
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_VF_INT_6_X),},
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_PF_DSC_1_X),},
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_VF_DSC_1_X),},
+       {0,}
+};
+MODULE_DEVICE_TABLE(pci, cci_pcie_id_tbl);
+
+static
+int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
+{
+       int ret;
+
+       ret = pci_enable_device(pcidev);
+       if (ret < 0) {
+               dev_err(&pcidev->dev, "Failed to enable device %d.\n", ret);
+               return ret;
+       }
+
+       ret = pci_enable_pcie_error_reporting(pcidev);
+       if (ret && ret != -EINVAL)

drivers/fpga/intel-pcie.c line 72

Quoted 10 lines
drivers/fpga/intel-pcie.c new file +10 -0
+               dev_info(&pcidev->dev, "PCIE AER unavailable %d.\n", ret);
+
+       ret = pci_request_regions(pcidev, DRV_NAME);
+       if (ret) {
+               dev_err(&pcidev->dev, "Failed to request regions.\n");
+               goto disable_error_report_exit;
+       }
+
+       pci_set_master(pcidev);
+       pci_save_state(pcidev);

Is pci_save_state needed here? Thought it was for going into suspend.

drivers/fpga/intel-pcie.c line 74

Quoted 2 lines
drivers/fpga/intel-pcie.c new file +2 -0
+
+       if (!dma_set_mask(&pcidev->dev, DMA_BIT_MASK(64))) {

Please use pci_dma_set_mask()

drivers/fpga/intel-pcie.c line 75

Quoted 1 line
drivers/fpga/intel-pcie.c new file +1 -0
+               dma_set_coherent_mask(&pcidev->dev, DMA_BIT_MASK(64));

pci_set_consistent_dma_mask() and check its return code.

drivers/fpga/intel-pcie.c line 77

Quoted 2 lines
drivers/fpga/intel-pcie.c new file +2 -0
+       } else if (!dma_set_mask(&pcidev->dev, DMA_BIT_MASK(32))) {
+               dma_set_coherent_mask(&pcidev->dev, DMA_BIT_MASK(32));

Same as above.

Quoted 52 lines
drivers/fpga/intel-pcie.c new file +49 -1
+       } else {
+               ret = -EIO;
+               dev_err(&pcidev->dev, "No suitable DMA support available.\n");
+               goto release_region_exit;
+       }
+
+       /* TODO: create and add the platform device per feature list */
+       return 0;
+
+release_region_exit:
+       pci_release_regions(pcidev);
+disable_error_report_exit:
+       pci_disable_pcie_error_reporting(pcidev);
+       pci_disable_device(pcidev);
+       return ret;
+}
+
+static void cci_pci_remove(struct pci_dev *pcidev)
+{
+       pci_release_regions(pcidev);
+       pci_disable_pcie_error_reporting(pcidev);
+       pci_disable_device(pcidev);
+}
+
+static struct pci_driver cci_pci_driver = {
+       .name = DRV_NAME,
+       .id_table = cci_pcie_id_tbl,
+       .probe = cci_pci_probe,
+       .remove = cci_pci_remove,
+};
+
+static int __init ccidrv_init(void)
+{
+       pr_info("Intel(R) FPGA PCIe Driver: Version %s\n", DRV_VERSION);
+
+       return pci_register_driver(&cci_pci_driver);
+}
+
+static void __exit ccidrv_exit(void)
+{
+       pci_unregister_driver(&cci_pci_driver);
+}
+
+module_init(ccidrv_init);
+module_exit(ccidrv_exit);
+
+MODULE_DESCRIPTION("Intel FPGA PCIe Device Driver");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
--

1.8.3.1

author
#34 lore raw

RE: [PATCH v2 06/22] fpga: intel: add FPGA PCIe device driver

Quoted 5 lines · Wu Hao

Hi Hao,

Making my way through your patchset.

Hi Alan

Thanks a lot for your comments. :)

Quoted 8 lines
Quoted 6 lines

From: Zhang Yi <[email protected]>

The Intel FPGA device appears as a PCIe device on the system. This patch implements the basic framework of the driver for Intel PCIe device which locates between CPU and Accelerated Function Units (AFUs).

...which is located between the CPU and...

Will fix this.

Quoted 29 lines
Quoted 9 lines
Quoted 8 lines

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.

---
 drivers/fpga/Kconfig      |  28 +++++++++++
 drivers/fpga/Makefile     |   5 ++
 drivers/fpga/intel-pcie.c | 126

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

Quoted 11 lines
 3 files changed, 159 insertions(+)
 create mode 100644 drivers/fpga/intel-pcie.c
drivers/fpga/Kconfig +0 -0
index c1d8f41..3f3b7f4 100644
@@ -117,6 +117,34 @@ config XILINX_PR_DECOUPLER
          region of the FPGA from the busses while that region is
          being reprogrammed during partial reconfig.
 
Quoted 11 lines

drivers/fpga/Kconfig config XILINX_PR_DECOUPLER · line 125

Quoted 6 lines
drivers/fpga/Kconfig +6 -0
+menuconfig INTEL_FPGA
+       tristate "Intel(R) FPGA support"
+       depends on FPGA_DEVICE
+       help
+         Select this option to enable driver support for Intel(R)
+         Field-Programmable Gate Array (FPGA) solutions. This driver

This is confusing since there is FPGA support for Intel FPGA devices in the kernel already such as any of the Altera support that already is in this Kconfig. This description will have to be more specific that this is one particular Intel solution.

Make sense, will add more specific description here.

Quoted 10 lines
Quoted 10 lines
drivers/fpga/Kconfig +9 -0
+         provides interfaces for userspace applications to configure,
+         enumerate, open, and access FPGA accelerators on platforms
+         equipped with Intel(R) FPGA solutions and enables system
+         level management functions such as FPGA reconfiguration,
+         power management, and virtualization.
+
+         Say Y if your platform has this technology. Say N if unsure.
+
+if INTEL_FPGA
Quoted 8 lines

drivers/fpga/Kconfig config XILINX_PR_DECOUPLER · line 140

Quoted 6 lines
drivers/fpga/Kconfig +6 -0
+
+config INTEL_FPGA_PCI
+       tristate "Intel FPGA PCIe Driver"
+       depends on PCI
+       help
+         This is the driver for the PCIe device which locates between

...which is located between...

Will fix this.

Quoted 97 lines

drivers/fpga/Makefile obj-$(CONFIG_XILINX_PR_DECOUPLER) += xilinx-pr-

Quoted 16 lines
drivers/fpga/Kconfig +7 -0
+         CPU and Accelerated Function Units (AFUs) and allows them to
+         communicate with each other.
+
+         To compile this as a module, choose M here.
+
+endif
+
 endif # FPGA
 
 endmenu
drivers/fpga/Makefile +0 -0
index 8950a8f..5613133 100644
@@ -27,3 +27,8 @@ obj-$(CONFIG_XILINX_PR_DECOUPLER)     += xilinx-pr-

decoupler.o

Quoted 80 lines
drivers/fpga/Makefile +5 -0
 # High Level Interfaces
 obj-$(CONFIG_FPGA_REGION)              += fpga-region.o
 obj-$(CONFIG_OF_FPGA_REGION)           += of-fpga-region.o
+
+# Intel FPGA Support
+obj-$(CONFIG_INTEL_FPGA_PCI)           += intel-fpga-pci.o
+
+intel-fpga-pci-objs := intel-pcie.o
drivers/fpga/intel-pcie.c new file +66 -0
new file mode 100644
index 0000000..f697de4
@@ -0,0 +1,126 @@
+/*
+ * Driver for Intel FPGA PCIe device
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Zhang Yi <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *   Joseph Grecco <[email protected]>
+ *   Enno Luebbers <[email protected]>
+ *   Tim Whisonant <[email protected]>
+ *   Ananda Ravuri <[email protected]>
+ *   Henry Mitchel <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include <linux/pci.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/stddef.h>
+#include <linux/errno.h>
+#include <linux/aer.h>
+
+#define DRV_VERSION    "0.8"
+#define DRV_NAME       "intel-fpga-pci"
+
+/* PCI Device ID */
+#define PCIe_DEVICE_ID_PF_INT_5_X      0xBCBD
+#define PCIe_DEVICE_ID_PF_INT_6_X      0xBCC0
+#define PCIe_DEVICE_ID_PF_DSC_1_X      0x09C4
+/* VF Device */
+#define PCIe_DEVICE_ID_VF_INT_5_X      0xBCBF
+#define PCIe_DEVICE_ID_VF_INT_6_X      0xBCC1
+#define PCIe_DEVICE_ID_VF_DSC_1_X      0x09C5
+
+static struct pci_device_id cci_pcie_id_tbl[] = {
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_PF_INT_5_X),},
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_VF_INT_5_X),},
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_PF_INT_6_X),},
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_VF_INT_6_X),},
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_PF_DSC_1_X),},
+       {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCIe_DEVICE_ID_VF_DSC_1_X),},
+       {0,}
+};
+MODULE_DEVICE_TABLE(pci, cci_pcie_id_tbl);
+
+static
+int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
+{
+       int ret;
+
+       ret = pci_enable_device(pcidev);
+       if (ret < 0) {
+               dev_err(&pcidev->dev, "Failed to enable device %d.\n", ret);
+               return ret;
+       }
+
+       ret = pci_enable_pcie_error_reporting(pcidev);
+       if (ret && ret != -EINVAL)
+               dev_info(&pcidev->dev, "PCIE AER unavailable %d.\n", ret);
+
+       ret = pci_request_regions(pcidev, DRV_NAME);
+       if (ret) {
Quoted 9 lines

drivers/fpga/intel-pcie.c line 72

Quoted 6 lines
drivers/fpga/intel-pcie.c new file +6 -0
+               dev_err(&pcidev->dev, "Failed to request regions.\n");
+               goto disable_error_report_exit;
+       }
+
+       pci_set_master(pcidev);
+       pci_save_state(pcidev);

Is pci_save_state needed here? Thought it was for going into suspend.

I think no, will remove this in the next version.

Quoted 4 lines

drivers/fpga/intel-pcie.c line 74

Quoted 2 lines
drivers/fpga/intel-pcie.c new file +2 -0
+
+       if (!dma_set_mask(&pcidev->dev, DMA_BIT_MASK(64))) {

Please use pci_dma_set_mask()

Will fix this.

Quoted 5 lines

drivers/fpga/intel-pcie.c line 75

Quoted 2 lines
drivers/fpga/intel-pcie.c new file +1 -0
+               dma_set_coherent_mask(&pcidev->dev, DMA_BIT_MASK(64));

pci_set_consistent_dma_mask() and check its return code.

Will fix this.

Quoted 5 lines

drivers/fpga/intel-pcie.c line 77

Quoted 2 lines
drivers/fpga/intel-pcie.c new file +2 -0
+       } else if (!dma_set_mask(&pcidev->dev, DMA_BIT_MASK(32))) {
+               dma_set_coherent_mask(&pcidev->dev, DMA_BIT_MASK(32));

Same as above.

Will fix this.

Thanks again for the review.

Hao

Quoted 52 lines
Quoted 52 lines
drivers/fpga/intel-pcie.c new file +49 -1
+       } else {
+               ret = -EIO;
+               dev_err(&pcidev->dev, "No suitable DMA support available.\n");
+               goto release_region_exit;
+       }
+
+       /* TODO: create and add the platform device per feature list */
+       return 0;
+
+release_region_exit:
+       pci_release_regions(pcidev);
+disable_error_report_exit:
+       pci_disable_pcie_error_reporting(pcidev);
+       pci_disable_device(pcidev);
+       return ret;
+}
+
+static void cci_pci_remove(struct pci_dev *pcidev)
+{
+       pci_release_regions(pcidev);
+       pci_disable_pcie_error_reporting(pcidev);
+       pci_disable_device(pcidev);
+}
+
+static struct pci_driver cci_pci_driver = {
+       .name = DRV_NAME,
+       .id_table = cci_pcie_id_tbl,
+       .probe = cci_pci_probe,
+       .remove = cci_pci_remove,
+};
+
+static int __init ccidrv_init(void)
+{
+       pr_info("Intel(R) FPGA PCIe Driver: Version %s\n", DRV_VERSION);
+
+       return pci_register_driver(&cci_pci_driver);
+}
+
+static void __exit ccidrv_exit(void)
+{
+       pci_unregister_driver(&cci_pci_driver);
+}
+
+module_init(ccidrv_init);
+module_exit(ccidrv_exit);
+
+MODULE_DESCRIPTION("Intel FPGA PCIe Device Driver");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
--

1.8.3.1

author
#35 lore raw

PATCH v2 7/22 fpga: intel: pcie: parse feature list and create platform device for features.

In reply to Wu Hao

From: Xiao Guangrong <[email protected]>

Device Feature List structure creates a link list of feature headers within the MMIO space to provide an extensible way of adding features.

The Intel FPGA PCIe driver walks through the feature headers to enumerate feature devices, FPGA Management Engine (FME) and FPGA Port for Accelerated Function Unit (AFU), and their private sub features. For feature devices, it creates the platform devices and linked the private sub features into their platform data.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.
fixed kbuild warning, typos and clean up the code.

---
 drivers/fpga/Makefile            |   2 +-
 drivers/fpga/intel-feature-dev.c | 130 ++++++
 drivers/fpga/intel-feature-dev.h | 341 ++++++++++++++++
 drivers/fpga/intel-pcie.c        | 841 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 1311 insertions(+), 3 deletions(-)
 create mode 100644 drivers/fpga/intel-feature-dev.c
 create mode 100644 drivers/fpga/intel-feature-dev.h
drivers/fpga/Makefile +1 -1
index 5613133..ad24b3d 100644
@@ -31,4 +31,4 @@ obj-$(CONFIG_OF_FPGA_REGION)		+= of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)		+= intel-fpga-pci.o
 
-intel-fpga-pci-objs := intel-pcie.o
+intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
drivers/fpga/intel-feature-dev.c new file +130 -0

130 lines not on this page · raw message

drivers/fpga/intel-feature-dev.h new file +341 -0

341 lines not on this page · raw message

drivers/fpga/intel-pcie.c +839 -2

878 lines not on this page · raw message

Signature · 1 line
1.8.3.1
maintainer
#36 lore raw

Re: [PATCH v2 07/22] fpga: intel: pcie: parse feature list and create platform device for features.

Hi Xiao / Wu,

general question, can't you make the /* */ comments for the structs kernel-doc ([1]) markup instead (see below)?

Quoted 252 lines · Wu Hao

From: Xiao Guangrong <[email protected]>

Device Feature List structure creates a link list of feature headers within the MMIO space to provide an extensible way of adding features.

The Intel FPGA PCIe driver walks through the feature headers to enumerate feature devices, FPGA Management Engine (FME) and FPGA Port for Accelerated Function Unit (AFU), and their private sub features. For feature devices, it creates the platform devices and linked the private sub features into their platform data.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.
fixed kbuild warning, typos and clean up the code.

---
 drivers/fpga/Makefile            |   2 +-
 drivers/fpga/intel-feature-dev.c | 130 ++++++
 drivers/fpga/intel-feature-dev.h | 341 ++++++++++++++++
 drivers/fpga/intel-pcie.c        | 841 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 1311 insertions(+), 3 deletions(-)
 create mode 100644 drivers/fpga/intel-feature-dev.c
 create mode 100644 drivers/fpga/intel-feature-dev.h
drivers/fpga/Makefile +1 -1
index 5613133..ad24b3d 100644
@@ -31,4 +31,4 @@ obj-$(CONFIG_OF_FPGA_REGION)		+= of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)		+= intel-fpga-pci.o
 
-intel-fpga-pci-objs := intel-pcie.o
+intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
drivers/fpga/intel-feature-dev.c new file +130 -0
new file mode 100644
index 0000000..68f9cba
@@ -0,0 +1,130 @@
+/*
+ * Intel FPGA Feature Device Driver
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "intel-feature-dev.h"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+			       int index, const char *name,
+			       int resource_index, void __iomem *ioaddr)
+{
+	WARN_ON(index >= pdata->num);
+
+	pdata->features[index].name = name;
+	pdata->features[index].resource_index = resource_index;
+	pdata->features[index].ioaddr = ioaddr;
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num)
+{
+	struct feature_platform_data *pdata;
+
+	pdata = kzalloc(feature_platform_data_size(num), GFP_KERNEL);
+	if (pdata) {
+		pdata->dev = dev;
+		pdata->num = num;
+		mutex_init(&pdata->lock);
+	}
+
+	return pdata;
+}
+
+int fme_feature_num(void)
+{
+	return FME_FEATURE_ID_MAX;
+}
+
+int port_feature_num(void)
+{
+	return PORT_FEATURE_ID_MAX;
+}
+
+int fpga_port_id(struct platform_device *pdev)
+{
+	struct feature_port_header *port_hdr;
+	struct feature_port_capability capability;
+
+	port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+					       PORT_FEATURE_ID_HEADER);
+	WARN_ON(!port_hdr);
+
+	capability.csr = readq(&port_hdr->capability);
+	return capability.port_number;
+}
+EXPORT_SYMBOL_GPL(fpga_port_id);
+
+/*
+ * Enable Port by clear the port soft reset bit, which is set by default.
+ * The User AFU is unable to respond to any MMIO access while in reset.
+ * __fpga_port_enable function should only be used after __fpga_port_disable
+ * function.
+ */
+void __fpga_port_enable(struct platform_device *pdev)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct feature_port_header *port_hdr;
+	struct feature_port_control control;
+
+	WARN_ON(!pdata->disable_count);
+
+	if (--pdata->disable_count != 0)
+		return;
+
+	port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+					       PORT_FEATURE_ID_HEADER);
+	WARN_ON(!port_hdr);
+
+	control.csr = readq(&port_hdr->control);
+	control.port_sftrst = 0x0;
+	writeq(control.csr, &port_hdr->control);
+}
+EXPORT_SYMBOL_GPL(__fpga_port_enable);
+
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
+int __fpga_port_disable(struct platform_device *pdev)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct feature_port_header *port_hdr;
+	struct feature_port_control control;
+
+	if (pdata->disable_count++ != 0)
+		return 0;
+
+	port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+					       PORT_FEATURE_ID_HEADER);
+	WARN_ON(!port_hdr);
+
+	/* Set port soft reset */
+	control.csr = readq(&port_hdr->control);
+	control.port_sftrst = 0x1;
+	writeq(control.csr, &port_hdr->control);
+
+	/*
+	 * HW sets ack bit to 1 when all outstanding requests have been drained
+	 * on this port and minimum soft reset pulse width has elapsed.
+	 * Driver polls port_soft_reset_ack to determine if reset done by HW.
+	 */
+	if (readq_poll_timeout(&port_hdr->control, control.csr,
+			       (control.port_sftrst_ack == 1),
+			       RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+		dev_err(&pdev->dev, "timeout, fail to reset device\n");
+		return -ETIMEDOUT;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(__fpga_port_disable);
drivers/fpga/intel-feature-dev.h new file +67 -0
new file mode 100644
index 0000000..f67784a
@@ -0,0 +1,341 @@
+/*
+ * Intel FPGA Feature Device Driver Header File
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef __INTEL_FPGA_FEATURE_H
+#define __INTEL_FPGA_FEATURE_H
+
+#include <linux/fs.h>
+#include <linux/pci.h>
+#include <linux/uuid.h>
+#include <linux/delay.h>
+#include <linux/iopoll.h>
+#include <linux/platform_device.h>
+
+#ifndef readq
+static inline u64 readq(void __iomem *addr)
+{
+	return readl(addr) + ((u64)readl(addr + 4) << 32);
+}
+#endif
+
+#ifndef writeq
+static inline void writeq(u64 val, void __iomem *addr)
+{
+	writel((u32) (val), addr);
+	writel((u32) (val >> 32), (addr + 4));
+}
+#endif
+
+/* maximum supported number of ports */
+#define MAX_FPGA_PORT_NUM 4
+/* plus one for fme device */
+#define MAX_FEATURE_DEV_NUM	(MAX_FPGA_PORT_NUM + 1)
+
+#define FME_FEATURE_HEADER          "fme_hdr"
+#define FME_FEATURE_THERMAL_MGMT    "fme_thermal"
+#define FME_FEATURE_POWER_MGMT      "fme_power"
+#define FME_FEATURE_GLOBAL_PERF     "fme_gperf"
+#define FME_FEATURE_GLOBAL_ERR      "fme_error"
+#define FME_FEATURE_PR_MGMT         "fme_pr"
+
+#define PORT_FEATURE_HEADER         "port_hdr"
+#define PORT_FEATURE_UAFU           "port_uafu"
+#define PORT_FEATURE_ERR            "port_err"
+#define PORT_FEATURE_UMSG           "port_umsg"
+#define PORT_FEATURE_PR             "port_pr"
+#define PORT_FEATURE_STP            "port_stp"
+
+/* All headers and structures must be byte-packed to match the spec. */
+#pragma pack(1)
+
+/* common header for all features */
+struct feature_header {
+	union {
+		u64 csr;
+		struct {

drivers/fpga/intel-feature-dev.h line 77

Quoted 10 lines
drivers/fpga/intel-feature-dev.h new file +10 -0
+			u64 id:12;
+			u64 revision:4;
+			u64 next_header_offset:24; /* offset to next header */
+			u64 rsvdz:20;
+			u64 type:4;		   /* feature type */
+#define FEATURE_TYPE_AFU		0x1
+#define FEATURE_TYPE_PRIVATE		0x3
+		};
+	};
+};

Here for example.

Quoted 1,153 lines
drivers/fpga/intel-feature-dev.h new file +264 -0
+
+/* common header for non-private features */
+struct feature_afu_header {
+	uuid_le guid;
+	union {
+		u64 csr;
+		struct {
+			u64 next_afu:24;	/* pointer to next afu header */
+			u64 rsvdz:40;
+		};
+	};
+};
+
+/* FME Header Register Set */
+/* FME Capability Register */
+struct feature_fme_capability {
+	union {
+		u64 csr;
+		struct {
+			u64 fabric_verid:8;	/* Fabric version ID */
+			u64 socket_id:1;	/* Socket id */
+			u64 rsvdz1:3;
+			u64 pcie0_link_avl:1;	/* PCIe0 link availability */
+			u64 pcie1_link_avl:1;	/* PCIe1 link availability */
+			u64 coherent_link_avl:1;/* Coherent link availability */
+			u64 rsvdz2:1;
+			u64 iommu_support:1;	/* IOMMU or VT-d supported */
+			u64 num_ports:3;	/* Num of ports implemented */
+			u64 rsvdz3:4;
+			u64 addr_width_bits:6;	/* Address width supported */
+			u64 rsvdz4:2;
+			u64 cache_size:12;	/* Cache size in kb */
+			u64 cache_assoc:4;	/* Cache Associativity */
+			u64 rsvdz5:15;
+			u64 lock_bit:1;		/* Latched lock bit by BIOS */
+		};
+	};
+};
+
+/* FME Port Offset Register */
+struct feature_fme_port {
+	union {
+		u64 csr;
+		struct {
+			u64 port_offset:24;	/* Offset to port header */
+			u64 rsvdz1:8;
+			u64 port_bar:3;		/* Bar id */
+			u64 rsvdz2:20;
+			u64 afu_access_ctrl:1;	/* AFU access type: PF/VF */
+			u64 rsvdz3:4;
+			u64 port_implemented:1;	/* Port implemented or not */
+			u64 rsvdz4:3;
+		};
+	};
+};
+
+struct feature_fme_header {
+	struct feature_header header;
+	struct feature_afu_header afu_header;
+	u64 rsvd[2];
+	struct feature_fme_capability capability;
+	struct feature_fme_port port[MAX_FPGA_PORT_NUM];
+};
+
+/* FME Thermal Sub Feature Register Set */
+struct feature_fme_thermal {
+	struct feature_header header;
+};
+
+/* FME Power Sub Feature Register Set */
+struct feature_fme_power {
+	struct feature_header header;
+};
+
+/* FME Global Performance Sub Feature Register Set */
+struct feature_fme_gperf {
+	struct feature_header header;
+};
+
+/* FME Error Sub Feature Register Set */
+struct feature_fme_err {
+	struct feature_header header;
+};
+
+/* FME Partial Reconfiguration Sub Feature Register Set */
+struct feature_fme_pr {
+	struct feature_header header;
+};
+
+/* PORT Header Register Set */
+/* Port Capability Register */
+struct feature_port_capability {
+	union {
+		u64 csr;
+		struct {
+			u64 port_number:2;	/* Port Number 0-3 */
+			u64 rsvdz1:6;
+			u64 mmio_size:16;	/* User MMIO size in KB */
+			u64 rsvdz2:8;
+			u64 sp_intr_num:4;	/* Supported interrupts num */
+			u64 rsvdz3:28;
+		};
+	};
+};
+
+/* Port Control Register */
+struct feature_port_control {
+	union {
+		u64 csr;
+		struct {
+			u64 port_sftrst:1;	/* Port Soft Reset */
+			u64 rsvdz1:1;
+			u64 latency_tolerance:1;/* '1' >= 40us, '0' < 40us */
+			u64 rsvdz2:1;
+			u64 port_sftrst_ack:1;	/* HW ACK for Soft Reset */
+			u64 rsvdz3:59;
+		};
+	};
+};
+
+struct feature_port_header {
+	struct feature_header header;
+	struct feature_afu_header afu_header;
+	u64 rsvd[2];
+	struct feature_port_capability capability;
+	struct feature_port_control control;
+};
+
+/* PORT Error Sub Feature Register Set */
+struct feature_port_error {
+	struct feature_header header;
+};
+
+/* PORT Unordered Message Sub Feature Register Set */
+struct feature_port_umsg {
+	struct feature_header header;
+};
+
+/* PORT SignalTap Sub Feature Register Set */
+struct feature_port_stp {
+	struct feature_header header;
+};
+
+#pragma pack()
+
+struct feature {
+	const char *name;
+	int resource_index;
+	void __iomem *ioaddr;
+};
+
+struct feature_platform_data {
+	/* list the feature dev to cci_drvdata->port_dev_list. */
+	struct list_head node;
+	struct mutex lock;
+	struct platform_device *dev;
+	unsigned int disable_count;	/* count for port disable */
+
+	int num;			/* number of features */
+	struct feature features[0];
+};
+
+enum fme_feature_id {
+	FME_FEATURE_ID_HEADER = 0x0,
+	FME_FEATURE_ID_THERMAL_MGMT = 0x1,
+	FME_FEATURE_ID_POWER_MGMT = 0x2,
+	FME_FEATURE_ID_GLOBAL_PERF = 0x3,
+	FME_FEATURE_ID_GLOBAL_ERR = 0x4,
+	FME_FEATURE_ID_PR_MGMT = 0x5,
+	FME_FEATURE_ID_MAX = 0x6,
+};
+
+enum port_feature_id {
+	PORT_FEATURE_ID_HEADER = 0x0,
+	PORT_FEATURE_ID_ERROR = 0x1,
+	PORT_FEATURE_ID_UMSG = 0x2,
+	PORT_FEATURE_ID_PR = 0x3,
+	PORT_FEATURE_ID_STP = 0x4,
+	PORT_FEATURE_ID_UAFU = 0x5,
+	PORT_FEATURE_ID_MAX = 0x6,
+};
+
+int fme_feature_num(void);
+int port_feature_num(void);
+
+#define FPGA_FEATURE_DEV_FME		"intel-fpga-fme"
+#define FPGA_FEATURE_DEV_PORT		"intel-fpga-port"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+			       int index, const char *name,
+			       int resource_index, void __iomem *ioaddr);
+
+static inline int feature_platform_data_size(const int num)
+{
+	return sizeof(struct feature_platform_data) +
+		num * sizeof(struct feature);
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num);
+
+int fpga_port_id(struct platform_device *pdev);
+
+static inline int fpga_port_check_id(struct platform_device *pdev,
+				     void *pport_id)
+{
+	return fpga_port_id(pdev) == *(int *)pport_id;
+}
+
+void __fpga_port_enable(struct platform_device *pdev);
+int __fpga_port_disable(struct platform_device *pdev);
+
+static inline void fpga_port_enable(struct platform_device *pdev)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+	mutex_lock(&pdata->lock);
+	__fpga_port_enable(pdev);
+	mutex_unlock(&pdata->lock);
+}
+
+static inline int fpga_port_disable(struct platform_device *pdev)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	int ret;
+
+	mutex_lock(&pdata->lock);
+	ret = __fpga_port_disable(pdev);
+	mutex_unlock(&pdata->lock);
+
+	return ret;
+}
+
+static inline int __fpga_port_reset(struct platform_device *pdev)
+{
+	int ret;
+
+	ret = __fpga_port_disable(pdev);
+	if (ret)
+		return ret;
+
+	__fpga_port_enable(pdev);
+	return 0;
+}
+
+static inline int fpga_port_reset(struct platform_device *pdev)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	int ret;
+
+	mutex_lock(&pdata->lock);
+	ret = __fpga_port_reset(pdev);
+	mutex_unlock(&pdata->lock);
+	return ret;
+}
+
+static inline void __iomem *
+get_feature_ioaddr_by_index(struct device *dev, int index)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(dev);
+
+	return pdata->features[index].ioaddr;
+}
+#endif
drivers/fpga/intel-pcie.c +839 -2
index f697de4..70b8284 100644
@@ -23,10 +23,827 @@
 #include <linux/stddef.h>
 #include <linux/errno.h>
 #include <linux/aer.h>
+#include <linux/fpga/fpga-dev.h>
+
+#include "intel-feature-dev.h"
 
 #define DRV_VERSION	"0.8"
 #define DRV_NAME	"intel-fpga-pci"
 
+#define INTEL_FPGA_DEV	"intel-fpga-dev"
+
+static DEFINE_MUTEX(fpga_id_mutex);
+
+enum fpga_id_type {
+	FME_ID,		/* fme id allocation and mapping */
+	PORT_ID,	/* port id allocation and mapping */
+	FPGA_ID_MAX,
+};
+
+/* it is protected by fpga_id_mutex */
+static struct idr fpga_ids[FPGA_ID_MAX];
+
+struct cci_drvdata {
+	struct device *fme_dev;
+
+	struct mutex lock;
+	struct list_head port_dev_list;
+
+	struct list_head regions; /* global list of pci bar mapping region */
+};
+
+/* pci bar mapping info */
+struct cci_pci_region {
+	int bar;
+	void __iomem *ioaddr;	/* pointer to mapped bar region */
+	struct list_head node;
+};
+
+static void fpga_ids_init(void)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+		idr_init(fpga_ids + i);
+}
+
+static void fpga_ids_destroy(void)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+		idr_destroy(fpga_ids + i);
+}
+
+static int alloc_fpga_id(enum fpga_id_type type, struct device *dev)
+{
+	int id;
+
+	WARN_ON(type >= FPGA_ID_MAX);
+	mutex_lock(&fpga_id_mutex);
+	id = idr_alloc(fpga_ids + type, dev, 0, 0, GFP_KERNEL);
+	mutex_unlock(&fpga_id_mutex);
+	return id;
+}
+
+static void free_fpga_id(enum fpga_id_type type, int id)
+{
+	WARN_ON(type >= FPGA_ID_MAX);
+	mutex_lock(&fpga_id_mutex);
+	idr_remove(fpga_ids + type, id);
+	mutex_unlock(&fpga_id_mutex);
+}
+
+static void cci_pci_add_port_dev(struct pci_dev *pdev,
+				 struct platform_device *port_dev)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+	struct feature_platform_data *pdata = dev_get_platdata(&port_dev->dev);
+
+	mutex_lock(&drvdata->lock);
+	list_add(&pdata->node, &drvdata->port_dev_list);
+	get_device(&pdata->dev->dev);
+	mutex_unlock(&drvdata->lock);
+}
+
+static void cci_pci_remove_port_devs(struct pci_dev *pdev)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+	struct feature_platform_data *pdata, *ptmp;
+
+	mutex_lock(&drvdata->lock);
+	list_for_each_entry_safe(pdata, ptmp, &drvdata->port_dev_list, node) {
+		struct platform_device *port_dev = pdata->dev;
+
+		/* the port should be unregistered first. */
+		WARN_ON(device_is_registered(&port_dev->dev));
+		list_del(&pdata->node);
+		free_fpga_id(PORT_ID, port_dev->id);
+		put_device(&port_dev->dev);
+	}
+	mutex_unlock(&drvdata->lock);
+}
+
+/* info collection during feature dev build. */
+struct build_feature_devs_info {
+	struct pci_dev *pdev;
+
+	/*
+	 * PCI BAR mapping info. Parsing feature list starts from
+	 * BAR 0 and switch to different BARs to parse Port
+	 */
+	void __iomem *ioaddr;
+	void __iomem *ioend;
+	int current_bar;
+
+	/* points to FME header where the port offset is figured out. */
+	void __iomem *pfme_hdr;
+
+	/* the container device for all feature devices */
+	struct fpga_dev *parent_dev;
+
+	/* current feature device */
+	struct platform_device *feature_dev;
+};
+
+static void cci_pci_release_regions(struct pci_dev *pdev)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+	struct cci_pci_region *tmp, *region;
+
+	list_for_each_entry_safe(region, tmp, &drvdata->regions, node) {
+		list_del(&region->node);
+		if (region->ioaddr)
+			pci_iounmap(pdev, region->ioaddr);
+		devm_kfree(&pdev->dev, region);
+	}
+}
+
+static void __iomem *cci_pci_ioremap_bar(struct pci_dev *pdev, int bar)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+	struct cci_pci_region *region;
+
+	list_for_each_entry(region, &drvdata->regions, node)
+		if (region->bar == bar) {
+			dev_dbg(&pdev->dev, "BAR %d region exists\n", bar);
+			return region->ioaddr;
+		}
+
+	region = devm_kzalloc(&pdev->dev, sizeof(*region), GFP_KERNEL);
+	if (!region)
+		return NULL;
+
+	region->bar = bar;
+	region->ioaddr = pci_ioremap_bar(pdev, bar);
+	if (!region->ioaddr) {
+		dev_err(&pdev->dev, "can't ioremap memory from BAR %d.\n", bar);
+		devm_kfree(&pdev->dev, region);
+		return NULL;
+	}
+
+	list_add(&region->node, &drvdata->regions);
+	return region->ioaddr;
+}
+
+static int parse_start_from(struct build_feature_devs_info *binfo, int bar)
+{
+	binfo->ioaddr = cci_pci_ioremap_bar(binfo->pdev, bar);
+	if (!binfo->ioaddr)
+		return -ENOMEM;
+
+	binfo->current_bar = bar;
+	binfo->ioend = binfo->ioaddr + pci_resource_len(binfo->pdev, bar);
+	return 0;
+}
+
+static int parse_start(struct build_feature_devs_info *binfo)
+{
+	/* fpga feature list starts from BAR 0 */
+	return parse_start_from(binfo, 0);
+}
+
+/* switch the memory mapping to BAR# @bar */
+static int parse_switch_to(struct build_feature_devs_info *binfo, int bar)
+{
+	return parse_start_from(binfo, bar);
+}
+
+static struct build_feature_devs_info *
+build_info_alloc_and_init(struct pci_dev *pdev)
+{
+	struct build_feature_devs_info *binfo;
+
+	binfo = devm_kzalloc(&pdev->dev, sizeof(*binfo), GFP_KERNEL);
+	if (binfo)
+		binfo->pdev = pdev;
+
+	return binfo;
+}
+
+static enum fpga_id_type feature_dev_id_type(struct platform_device *pdev)
+{
+	if (!strcmp(pdev->name, FPGA_FEATURE_DEV_FME))
+		return FME_ID;
+
+	if (!strcmp(pdev->name, FPGA_FEATURE_DEV_PORT))
+		return PORT_ID;
+
+	WARN_ON(1);
+	return FPGA_ID_MAX;
+}
+
+/*
+ * register current feature device, it is called when we need to switch to
+ * another feature parsing or we have parsed all features
+ */
+static int build_info_commit_dev(struct build_feature_devs_info *binfo)
+{
+	int ret;
+
+	if (!binfo->feature_dev)
+		return 0;
+
+	ret = platform_device_add(binfo->feature_dev);
+	if (!ret) {
+		struct cci_drvdata *drvdata;
+
+		drvdata = dev_get_drvdata(&binfo->pdev->dev);
+		if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
+			cci_pci_add_port_dev(binfo->pdev, binfo->feature_dev);
+		else
+			drvdata->fme_dev = get_device(&binfo->feature_dev->dev);
+
+		/*
+		 * reset it to avoid build_info_free() freeing their resource.
+		 *
+		 * The resource of successfully registered feature devices
+		 * will be freed by platform_device_unregister(). See the
+		 * comments in build_info_create_dev().
+		 */
+		binfo->feature_dev = NULL;
+	}
+
+	return ret;
+}
+
+static int
+build_info_create_dev(struct build_feature_devs_info *binfo,
+		      enum fpga_id_type type, int feature_nr, const char *name)
+{
+	struct platform_device *fdev;
+	struct resource *res;
+	struct feature_platform_data *pdata;
+	int ret;
+
+	/* we will create a new device, commit current device first */
+	ret = build_info_commit_dev(binfo);
+	if (ret)
+		return ret;
+
+	/*
+	 * we use -ENODEV as the initialization indicator which indicates
+	 * whether the id need to be reclaimed
+	 */
+	fdev = binfo->feature_dev = platform_device_alloc(name, -ENODEV);
+	if (!fdev)
+		return -ENOMEM;
+
+	fdev->id = alloc_fpga_id(type, &fdev->dev);
+	if (fdev->id < 0)
+		return fdev->id;
+
+	fdev->dev.parent = &binfo->parent_dev->dev;
+
+	/*
+	 * we do not need to care for the memory which is associated with
+	 * the platform device. After calling platform_device_unregister(),
+	 * it will be automatically freed by device's release() callback,
+	 * platform_device_release().
+	 */
+	pdata = feature_platform_data_alloc_and_init(fdev, feature_nr);
+	if (!pdata)
+		return -ENOMEM;
+
+	/*
+	 * the count should be initialized to 0 to make sure
+	 *__fpga_port_enable() following __fpga_port_disable()
+	 * works properly for port device.
+	 * and it should always be 0 for fme device.
+	 */
+	WARN_ON(pdata->disable_count);
+
+	fdev->dev.platform_data = pdata;
+	fdev->num_resources = feature_nr;
+	fdev->resource = kcalloc(feature_nr, sizeof(*res), GFP_KERNEL);
+	if (!fdev->resource)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static int remove_feature_dev(struct device *dev, void *data)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+
+	platform_device_unregister(pdev);
+	return 0;
+}
+
+static int remove_parent_dev(struct device *dev, void *data)
+{
+	/* remove platform devices attached in the parent device */
+	device_for_each_child(dev, NULL, remove_feature_dev);
+	fpga_dev_destroy(to_fpga_dev(dev));
+	return 0;
+}
+
+static void remove_all_devs(struct pci_dev *pdev)
+{
+	/* remove parent device and all its children. */
+	device_for_each_child(&pdev->dev, NULL, remove_parent_dev);
+}
+
+static void build_info_free(struct build_feature_devs_info *binfo)
+{
+	if (!IS_ERR_OR_NULL(binfo->parent_dev))
+		remove_all_devs(binfo->pdev);
+
+	/*
+	 * it is a valid id, free it. See comments in
+	 * build_info_create_dev()
+	 */
+	if (binfo->feature_dev && binfo->feature_dev->id >= 0)
+		free_fpga_id(feature_dev_id_type(binfo->feature_dev),
+			     binfo->feature_dev->id);
+
+	platform_device_put(binfo->feature_dev);
+
+	devm_kfree(&binfo->pdev->dev, binfo);
+}
+
+#define FEATURE_TYPE_AFU	0x1
+#define FEATURE_TYPE_PRIVATE	0x3
+
+/* FME and PORT GUID are fixed */
+#define FEATURE_FME_GUID "f9e17764-38f0-82fe-e346-524ae92aafbf"
+#define FEATURE_PORT_GUID "6b355b87-b06c-9642-eb42-8d139398b43a"
+
+static bool feature_is_fme(struct feature_afu_header *afu_hdr)
+{
+	uuid_le u;
+
+	uuid_le_to_bin(FEATURE_FME_GUID, &u);
+
+	return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+static bool feature_is_port(struct feature_afu_header *afu_hdr)
+{
+	uuid_le u;
+
+	uuid_le_to_bin(FEATURE_PORT_GUID, &u);
+
+	return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+/*
+ * UAFU GUID is dynamic as it can be changed after FME downloads different
+ * Green Bitstream to the port, so we treat the unknown GUIDs which are
+ * attached on port's feature list as UAFU.
+ */
+static bool feature_is_UAFU(struct build_feature_devs_info *binfo)
+{
+	if (!binfo->feature_dev ||
+	      feature_dev_id_type(binfo->feature_dev) != PORT_ID)
+		return false;
+
+	return true;
+}
+
+static void
+build_info_add_sub_feature(struct build_feature_devs_info *binfo,
+			   int feature_id, const char *feature_name,
+			   resource_size_t resource_size, void __iomem *start)
+{
+
+	struct platform_device *fdev = binfo->feature_dev;
+	struct feature_platform_data *pdata = dev_get_platdata(&fdev->dev);
+	struct resource *res = &fdev->resource[feature_id];
+
+	res->start = pci_resource_start(binfo->pdev, binfo->current_bar) +
+		start - binfo->ioaddr;
+	res->end = res->start + resource_size - 1;
+	res->flags = IORESOURCE_MEM;
+	res->name = feature_name;
+
+	feature_platform_data_add(pdata, feature_id,
+				  feature_name, feature_id, start);
+}
+
+struct feature_info {
+	const char *name;
+	resource_size_t resource_size;
+	int feature_index;
+};
+
+/* indexed by fme feature IDs which are defined in 'enum fme_feature_id'. */
+static struct feature_info fme_features[] = {
+	{
+		.name = FME_FEATURE_HEADER,
+		.resource_size = sizeof(struct feature_fme_header),
+		.feature_index = FME_FEATURE_ID_HEADER,
+	},
+	{
+		.name = FME_FEATURE_THERMAL_MGMT,
+		.resource_size = sizeof(struct feature_fme_thermal),
+		.feature_index = FME_FEATURE_ID_THERMAL_MGMT,
+	},
+	{
+		.name = FME_FEATURE_POWER_MGMT,
+		.resource_size = sizeof(struct feature_fme_power),
+		.feature_index = FME_FEATURE_ID_POWER_MGMT,
+	},
+	{
+		.name = FME_FEATURE_GLOBAL_PERF,
+		.resource_size = sizeof(struct feature_fme_gperf),
+		.feature_index = FME_FEATURE_ID_GLOBAL_PERF,
+	},
+	{
+		.name = FME_FEATURE_GLOBAL_ERR,
+		.resource_size = sizeof(struct feature_fme_err),
+		.feature_index = FME_FEATURE_ID_GLOBAL_ERR,
+	},
+	{
+		.name = FME_FEATURE_PR_MGMT,
+		.resource_size = sizeof(struct feature_fme_pr),
+		.feature_index = FME_FEATURE_ID_PR_MGMT,
+	}
+};
+
+/* indexed by port feature IDs which are defined in 'enum port_feature_id'. */
+static struct feature_info port_features[] = {
+	{
+		.name = PORT_FEATURE_HEADER,
+		.resource_size = sizeof(struct feature_port_header),
+		.feature_index = PORT_FEATURE_ID_HEADER,
+	},
+	{
+		.name = PORT_FEATURE_ERR,
+		.resource_size = sizeof(struct feature_port_error),
+		.feature_index = PORT_FEATURE_ID_ERROR,
+	},
+	{
+		.name = PORT_FEATURE_UMSG,
+		.resource_size = sizeof(struct feature_port_umsg),
+		.feature_index = PORT_FEATURE_ID_UMSG,
+	},
+	{
+		/* This feature isn't available for now */
+		.name = PORT_FEATURE_PR,
+		.resource_size = 0,
+		.feature_index = PORT_FEATURE_ID_PR,
+	},
+	{
+		.name = PORT_FEATURE_STP,
+		.resource_size = sizeof(struct feature_port_stp),
+		.feature_index = PORT_FEATURE_ID_STP,
+	},
+	{
+		/*
+		 * For User AFU feature, its region size is not fixed, but
+		 * reported by register PortCapability.mmio_size. Resource
+		 * size of UAFU will be set while parse port device.
+		 */
+		.name = PORT_FEATURE_UAFU,
+		.resource_size = 0,
+		.feature_index = PORT_FEATURE_ID_UAFU,
+	},
+};
+
+static int
+create_feature_instance(struct build_feature_devs_info *binfo,
+			void __iomem *start, struct feature_info *finfo)
+{
+	if (binfo->ioend - start < finfo->resource_size)
+		return -EINVAL;
+
+	build_info_add_sub_feature(binfo, finfo->feature_index, finfo->name,
+				   finfo->resource_size, start);
+	return 0;
+}
+
+static int parse_feature_fme(struct build_feature_devs_info *binfo,
+			     void __iomem *start)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&binfo->pdev->dev);
+	int ret;
+
+	ret = build_info_create_dev(binfo, FME_ID, fme_feature_num(),
+					FPGA_FEATURE_DEV_FME);
+	if (ret)
+		return ret;
+
+	if (drvdata->fme_dev) {
+		dev_err(&binfo->pdev->dev, "Multiple FMEs are detected.\n");
+		return -EINVAL;
+	}
+
+	return create_feature_instance(binfo, start,
+				       &fme_features[FME_FEATURE_ID_HEADER]);
+}
+
+static int parse_feature_fme_private(struct build_feature_devs_info *binfo,
+				     struct feature_header *hdr)
+{
+	struct feature_header header;
+
+	header.csr = readq(hdr);
+
+	if (header.id >= ARRAY_SIZE(fme_features)) {
+		dev_info(&binfo->pdev->dev, "FME feature id %x is not supported yet.\n",
+			 header.id);
+		return 0;
+	}
+
+	return create_feature_instance(binfo, hdr, &fme_features[header.id]);
+}
+
+static int parse_feature_port(struct build_feature_devs_info *binfo,
+			     void __iomem *start)
+{
+	int ret;
+
+	ret = build_info_create_dev(binfo, PORT_ID, port_feature_num(),
+					FPGA_FEATURE_DEV_PORT);
+	if (ret)
+		return ret;
+
+	return create_feature_instance(binfo, start,
+				       &port_features[PORT_FEATURE_ID_HEADER]);
+}
+
+static void enable_port_uafu(struct build_feature_devs_info *binfo,
+			     void __iomem *start)
+{
+	enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+	struct feature_port_header *port_hdr;
+	struct feature_port_capability capability;
+
+	port_hdr = (struct feature_port_header *)start;
+	capability.csr = readq(&port_hdr->capability);
+	port_features[id].resource_size = capability.mmio_size << 10;
+
+	/*
+	 * To enable User AFU, driver needs to clear reset bit on related port,
+	 * otherwise the mmio space of this user AFU will be invalid.
+	 */
+	if (port_features[id].resource_size)
+		fpga_port_reset(binfo->feature_dev);
+}
+
+static int parse_feature_port_private(struct build_feature_devs_info *binfo,
+				      struct feature_header *hdr)
+{
+	struct feature_header header;
+	enum port_feature_id id;
+
+	header.csr = readq(hdr);
+	/*
+	 * the region of port feature id is [0x10, 0x13], + 1 to reserve 0
+	 * which is dedicated for port-hdr.
+	 */
+	id = (header.id & 0x000f) + 1;
+
+	if (id >= ARRAY_SIZE(port_features)) {
+		dev_info(&binfo->pdev->dev, "Port feature id %x is not supported yet.\n",
+			 header.id);
+		return 0;
+	}
+
+	return create_feature_instance(binfo, hdr, &port_features[id]);
+}
+
+static int parse_feature_port_uafu(struct build_feature_devs_info *binfo,
+				 struct feature_header *hdr)
+{
+	enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+	int ret;
+
+	if (port_features[id].resource_size) {
+		ret = create_feature_instance(binfo, hdr, &port_features[id]);
+		port_features[id].resource_size = 0;
+	} else {
+		dev_err(&binfo->pdev->dev, "the uafu feature header is mis-configured.\n");
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+static int parse_feature_afus(struct build_feature_devs_info *binfo,
+			      struct feature_header *hdr)
+{
+	int ret;
+	struct feature_afu_header *afu_hdr, header;
+	void __iomem *start;
+	void __iomem *end = binfo->ioend;
+
+	start = hdr;
+	for (; start < end; start += header.next_afu) {
+		if (end - start < (sizeof(*afu_hdr) + sizeof(*hdr)))
+			return -EINVAL;
+
+		hdr = start;
+		afu_hdr = (struct feature_afu_header *) (hdr + 1);
+		header.csr = readq(&afu_hdr->csr);
+
+		if (feature_is_fme(afu_hdr)) {
+			ret = parse_feature_fme(binfo, hdr);
+			binfo->pfme_hdr = hdr;
+			if (ret)
+				return ret;
+		} else if (feature_is_port(afu_hdr)) {
+			ret = parse_feature_port(binfo, hdr);
+			enable_port_uafu(binfo, hdr);
+			if (ret)
+				return ret;
+		} else if (feature_is_UAFU(binfo)) {
+			ret = parse_feature_port_uafu(binfo, hdr);
+			if (ret)
+				return ret;
+		} else
+			dev_info(&binfo->pdev->dev, "AFU GUID %pUl is not supported yet.\n",
+				 afu_hdr->guid.b);
+
+		if (!header.next_afu)
+			break;
+	}
+
+	return 0;
+}
+
+static int parse_feature_private(struct build_feature_devs_info *binfo,
+				 struct feature_header *hdr)
+{
+	struct feature_header header;
+
+	header.csr = readq(hdr);
+
+	if (!binfo->feature_dev) {
+		dev_err(&binfo->pdev->dev, "the private feature %x does not belong to any AFU.\n",
+			header.id);
+		return -EINVAL;
+	}
+
+	switch (feature_dev_id_type(binfo->feature_dev)) {
+	case FME_ID:
+		return parse_feature_fme_private(binfo, hdr);
+	case PORT_ID:
+		return parse_feature_port_private(binfo, hdr);
+	default:
+		dev_info(&binfo->pdev->dev, "private feature %x belonging to AFU %s is not supported yet.\n",
+			 header.id, binfo->feature_dev->name);
+	}
+	return 0;
+}
+
+static int parse_feature(struct build_feature_devs_info *binfo,
+			 struct feature_header *hdr)
+{
+	struct feature_header header;
+	int ret = 0;
+
+	header.csr = readq(hdr);
+
+	switch (header.type) {
+	case FEATURE_TYPE_AFU:
+		ret = parse_feature_afus(binfo, hdr);
+		break;
+	case FEATURE_TYPE_PRIVATE:
+		ret = parse_feature_private(binfo, hdr);
+		break;
+	default:
+		dev_info(&binfo->pdev->dev,
+			 "Feature Type %x is not supported.\n", hdr->type);
+	};
+
+	return ret;
+}
+
+static int
+parse_feature_list(struct build_feature_devs_info *binfo, void __iomem *start)
+{
+	struct feature_header *hdr, header;
+	void __iomem *end = binfo->ioend;
+	int ret = 0;
+
+	for (; start < end; start += header.next_header_offset) {
+		if (end - start < sizeof(*hdr)) {
+			dev_err(&binfo->pdev->dev, "The region is too small to contain a feature.\n");
+			ret =  -EINVAL;
+			break;
+		}
+
+		hdr = (struct feature_header *)start;
+		ret = parse_feature(binfo, hdr);
+		if (ret)
+			break;
+
+		header.csr = readq(hdr);
+		if (!header.next_header_offset)
+			break;
+	}
+
+	return ret;
+}
+
+static int parse_ports_from_fme(struct build_feature_devs_info *binfo)
+{
+	struct feature_fme_header *fme_hdr;
+	struct feature_fme_port port;
+	int i = 0, ret = 0;
+
+	if (binfo->pfme_hdr == NULL) {
+		dev_dbg(&binfo->pdev->dev, "VF is detected.\n");
+		return ret;
+	}
+
+	fme_hdr = binfo->pfme_hdr;
+
+	do {
+		port.csr = readq(&fme_hdr->port[i]);
+		if (!port.port_implemented)
+			break;
+
+		ret = parse_switch_to(binfo, port.port_bar);
+		if (ret)
+			break;
+
+		ret = parse_feature_list(binfo,
+				binfo->ioaddr + port.port_offset);
+		if (ret)
+			break;
+	} while (++i < MAX_FPGA_PORT_NUM);
+
+	return ret;
+}
+
+static int create_init_drvdata(struct pci_dev *pdev)
+{
+	struct cci_drvdata *drvdata;
+
+	drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
+	if (!drvdata)
+		return -ENOMEM;
+
+	mutex_init(&drvdata->lock);
+	INIT_LIST_HEAD(&drvdata->port_dev_list);
+	INIT_LIST_HEAD(&drvdata->regions);
+
+	dev_set_drvdata(&pdev->dev, drvdata);
+	return 0;
+}
+
+static void destroy_drvdata(struct pci_dev *pdev)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+
+	if (drvdata->fme_dev) {
+		/* fme device should be unregistered first. */
+		WARN_ON(device_is_registered(drvdata->fme_dev));
+		free_fpga_id(FME_ID, to_platform_device(drvdata->fme_dev)->id);
+		put_device(drvdata->fme_dev);
+	}
+
+	cci_pci_remove_port_devs(pdev);
+	cci_pci_release_regions(pdev);
+	dev_set_drvdata(&pdev->dev, NULL);
+	devm_kfree(&pdev->dev, drvdata);
+}
+
+static int cci_pci_create_feature_devs(struct pci_dev *pdev)
+{
+	struct build_feature_devs_info *binfo;
+	int ret;
+
+	binfo = build_info_alloc_and_init(pdev);
+	if (!binfo)
+		return -ENOMEM;
+
+	binfo->parent_dev = fpga_dev_create(&pdev->dev, INTEL_FPGA_DEV);
+	if (IS_ERR(binfo->parent_dev)) {
+		ret = PTR_ERR(binfo->parent_dev);
+		goto free_binfo_exit;
+	}
+
+	ret = parse_start(binfo);
+	if (ret)
+		goto free_binfo_exit;
+
+	ret = parse_feature_list(binfo, binfo->ioaddr);
+	if (ret)
+		goto free_binfo_exit;
+
+	ret = parse_ports_from_fme(binfo);
+	if (ret)
+		goto free_binfo_exit;
+
+	ret = build_info_commit_dev(binfo);
+	if (ret)
+		goto free_binfo_exit;
+
+	/*
+	 * everything is okay, reset ->parent_dev to stop it being
+	 * freed by build_info_free()
+	 */
+	binfo->parent_dev = NULL;
+
+free_binfo_exit:
+	build_info_free(binfo);
+	return ret;
+}
+
 /* PCI Device ID */
 #define PCIe_DEVICE_ID_PF_INT_5_X	0xBCBD
 #define PCIe_DEVICE_ID_PF_INT_6_X	0xBCC0
@@ -81,9 +898,18 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
 		goto release_region_exit;
 	}
 
-	/* TODO: create and add the platform device per feature list */
+	ret = create_init_drvdata(pcidev);
+	if (ret)
+		goto release_region_exit;
+
+	ret = cci_pci_create_feature_devs(pcidev);
+	if (ret)
+		goto destroy_drvdata_exit;
+
 	return 0;
 
+destroy_drvdata_exit:
+	destroy_drvdata(pcidev);
 release_region_exit:
 	pci_release_regions(pcidev);
 disable_error_report_exit:
@@ -94,6 +920,8 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
 
 static void cci_pci_remove(struct pci_dev *pcidev)
 {
+	remove_all_devs(pcidev);
+	destroy_drvdata(pcidev);
 	pci_release_regions(pcidev);
 	pci_disable_pcie_error_reporting(pcidev);
 	pci_disable_device(pcidev);
@@ -108,14 +936,23 @@ static void cci_pci_remove(struct pci_dev *pcidev)
 
 static int __init ccidrv_init(void)
 {
+	int ret;
+
 	pr_info("Intel(R) FPGA PCIe Driver: Version %s\n", DRV_VERSION);
 
-	return pci_register_driver(&cci_pci_driver);
+	fpga_ids_init();
+
+	ret = pci_register_driver(&cci_pci_driver);
+	if (ret)
+		fpga_ids_destroy();
+
+	return ret;
 }
 
 static void __exit ccidrv_exit(void)
 {
 	pci_unregister_driver(&cci_pci_driver);
+	fpga_ids_destroy();
 }
 
 module_init(ccidrv_init);
Signature · 1 line
1.8.3.1

Thanks,

Moritz

[1] https://www.kernel.org/doc/html/v4.11/doc-guide/kernel-doc.html#writing-kernel-doc-comments

author
#37 lore raw

Re: [PATCH v2 07/22] fpga: intel: pcie: parse feature list and create platform device for features.

Quoted 4 lines · Moritz Fischer

Hi Xiao / Wu,

general question, can't you make the /* */ comments for the structs kernel-doc ([1]) markup instead (see below)?

Hi Moritz,

Thanks for your review. Sure, will fix this issue for all patches.

Hao

Quoted 1,430 lines · Wu Hao
Quoted 252 lines

From: Xiao Guangrong <[email protected]>

Device Feature List structure creates a link list of feature headers within the MMIO space to provide an extensible way of adding features.

The Intel FPGA PCIe driver walks through the feature headers to enumerate feature devices, FPGA Management Engine (FME) and FPGA Port for Accelerated Function Unit (AFU), and their private sub features. For feature devices, it creates the platform devices and linked the private sub features into their platform data.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.
fixed kbuild warning, typos and clean up the code.

---
 drivers/fpga/Makefile            |   2 +-
 drivers/fpga/intel-feature-dev.c | 130 ++++++
 drivers/fpga/intel-feature-dev.h | 341 ++++++++++++++++
 drivers/fpga/intel-pcie.c        | 841 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 1311 insertions(+), 3 deletions(-)
 create mode 100644 drivers/fpga/intel-feature-dev.c
 create mode 100644 drivers/fpga/intel-feature-dev.h
drivers/fpga/Makefile +1 -1
index 5613133..ad24b3d 100644
@@ -31,4 +31,4 @@ obj-$(CONFIG_OF_FPGA_REGION)		+= of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)		+= intel-fpga-pci.o
 
-intel-fpga-pci-objs := intel-pcie.o
+intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
drivers/fpga/intel-feature-dev.c new file +130 -0
new file mode 100644
index 0000000..68f9cba
@@ -0,0 +1,130 @@
+/*
+ * Intel FPGA Feature Device Driver
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "intel-feature-dev.h"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+			       int index, const char *name,
+			       int resource_index, void __iomem *ioaddr)
+{
+	WARN_ON(index >= pdata->num);
+
+	pdata->features[index].name = name;
+	pdata->features[index].resource_index = resource_index;
+	pdata->features[index].ioaddr = ioaddr;
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num)
+{
+	struct feature_platform_data *pdata;
+
+	pdata = kzalloc(feature_platform_data_size(num), GFP_KERNEL);
+	if (pdata) {
+		pdata->dev = dev;
+		pdata->num = num;
+		mutex_init(&pdata->lock);
+	}
+
+	return pdata;
+}
+
+int fme_feature_num(void)
+{
+	return FME_FEATURE_ID_MAX;
+}
+
+int port_feature_num(void)
+{
+	return PORT_FEATURE_ID_MAX;
+}
+
+int fpga_port_id(struct platform_device *pdev)
+{
+	struct feature_port_header *port_hdr;
+	struct feature_port_capability capability;
+
+	port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+					       PORT_FEATURE_ID_HEADER);
+	WARN_ON(!port_hdr);
+
+	capability.csr = readq(&port_hdr->capability);
+	return capability.port_number;
+}
+EXPORT_SYMBOL_GPL(fpga_port_id);
+
+/*
+ * Enable Port by clear the port soft reset bit, which is set by default.
+ * The User AFU is unable to respond to any MMIO access while in reset.
+ * __fpga_port_enable function should only be used after __fpga_port_disable
+ * function.
+ */
+void __fpga_port_enable(struct platform_device *pdev)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct feature_port_header *port_hdr;
+	struct feature_port_control control;
+
+	WARN_ON(!pdata->disable_count);
+
+	if (--pdata->disable_count != 0)
+		return;
+
+	port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+					       PORT_FEATURE_ID_HEADER);
+	WARN_ON(!port_hdr);
+
+	control.csr = readq(&port_hdr->control);
+	control.port_sftrst = 0x0;
+	writeq(control.csr, &port_hdr->control);
+}
+EXPORT_SYMBOL_GPL(__fpga_port_enable);
+
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
+int __fpga_port_disable(struct platform_device *pdev)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct feature_port_header *port_hdr;
+	struct feature_port_control control;
+
+	if (pdata->disable_count++ != 0)
+		return 0;
+
+	port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+					       PORT_FEATURE_ID_HEADER);
+	WARN_ON(!port_hdr);
+
+	/* Set port soft reset */
+	control.csr = readq(&port_hdr->control);
+	control.port_sftrst = 0x1;
+	writeq(control.csr, &port_hdr->control);
+
+	/*
+	 * HW sets ack bit to 1 when all outstanding requests have been drained
+	 * on this port and minimum soft reset pulse width has elapsed.
+	 * Driver polls port_soft_reset_ack to determine if reset done by HW.
+	 */
+	if (readq_poll_timeout(&port_hdr->control, control.csr,
+			       (control.port_sftrst_ack == 1),
+			       RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+		dev_err(&pdev->dev, "timeout, fail to reset device\n");
+		return -ETIMEDOUT;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(__fpga_port_disable);
drivers/fpga/intel-feature-dev.h new file +67 -0
new file mode 100644
index 0000000..f67784a
@@ -0,0 +1,341 @@
+/*
+ * Intel FPGA Feature Device Driver Header File
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef __INTEL_FPGA_FEATURE_H
+#define __INTEL_FPGA_FEATURE_H
+
+#include <linux/fs.h>
+#include <linux/pci.h>
+#include <linux/uuid.h>
+#include <linux/delay.h>
+#include <linux/iopoll.h>
+#include <linux/platform_device.h>
+
+#ifndef readq
+static inline u64 readq(void __iomem *addr)
+{
+	return readl(addr) + ((u64)readl(addr + 4) << 32);
+}
+#endif
+
+#ifndef writeq
+static inline void writeq(u64 val, void __iomem *addr)
+{
+	writel((u32) (val), addr);
+	writel((u32) (val >> 32), (addr + 4));
+}
+#endif
+
+/* maximum supported number of ports */
+#define MAX_FPGA_PORT_NUM 4
+/* plus one for fme device */
+#define MAX_FEATURE_DEV_NUM	(MAX_FPGA_PORT_NUM + 1)
+
+#define FME_FEATURE_HEADER          "fme_hdr"
+#define FME_FEATURE_THERMAL_MGMT    "fme_thermal"
+#define FME_FEATURE_POWER_MGMT      "fme_power"
+#define FME_FEATURE_GLOBAL_PERF     "fme_gperf"
+#define FME_FEATURE_GLOBAL_ERR      "fme_error"
+#define FME_FEATURE_PR_MGMT         "fme_pr"
+
+#define PORT_FEATURE_HEADER         "port_hdr"
+#define PORT_FEATURE_UAFU           "port_uafu"
+#define PORT_FEATURE_ERR            "port_err"
+#define PORT_FEATURE_UMSG           "port_umsg"
+#define PORT_FEATURE_PR             "port_pr"
+#define PORT_FEATURE_STP            "port_stp"
+
+/* All headers and structures must be byte-packed to match the spec. */
+#pragma pack(1)
+
+/* common header for all features */
+struct feature_header {
+	union {
+		u64 csr;
+		struct {

drivers/fpga/intel-feature-dev.h line 77

Quoted 10 lines
drivers/fpga/intel-feature-dev.h new file +10 -0
+			u64 id:12;
+			u64 revision:4;
+			u64 next_header_offset:24; /* offset to next header */
+			u64 rsvdz:20;
+			u64 type:4;		   /* feature type */
+#define FEATURE_TYPE_AFU		0x1
+#define FEATURE_TYPE_PRIVATE		0x3
+		};
+	};
+};

Here for example.

Quoted 1,153 lines
drivers/fpga/intel-feature-dev.h new file +264 -0
+
+/* common header for non-private features */
+struct feature_afu_header {
+	uuid_le guid;
+	union {
+		u64 csr;
+		struct {
+			u64 next_afu:24;	/* pointer to next afu header */
+			u64 rsvdz:40;
+		};
+	};
+};
+
+/* FME Header Register Set */
+/* FME Capability Register */
+struct feature_fme_capability {
+	union {
+		u64 csr;
+		struct {
+			u64 fabric_verid:8;	/* Fabric version ID */
+			u64 socket_id:1;	/* Socket id */
+			u64 rsvdz1:3;
+			u64 pcie0_link_avl:1;	/* PCIe0 link availability */
+			u64 pcie1_link_avl:1;	/* PCIe1 link availability */
+			u64 coherent_link_avl:1;/* Coherent link availability */
+			u64 rsvdz2:1;
+			u64 iommu_support:1;	/* IOMMU or VT-d supported */
+			u64 num_ports:3;	/* Num of ports implemented */
+			u64 rsvdz3:4;
+			u64 addr_width_bits:6;	/* Address width supported */
+			u64 rsvdz4:2;
+			u64 cache_size:12;	/* Cache size in kb */
+			u64 cache_assoc:4;	/* Cache Associativity */
+			u64 rsvdz5:15;
+			u64 lock_bit:1;		/* Latched lock bit by BIOS */
+		};
+	};
+};
+
+/* FME Port Offset Register */
+struct feature_fme_port {
+	union {
+		u64 csr;
+		struct {
+			u64 port_offset:24;	/* Offset to port header */
+			u64 rsvdz1:8;
+			u64 port_bar:3;		/* Bar id */
+			u64 rsvdz2:20;
+			u64 afu_access_ctrl:1;	/* AFU access type: PF/VF */
+			u64 rsvdz3:4;
+			u64 port_implemented:1;	/* Port implemented or not */
+			u64 rsvdz4:3;
+		};
+	};
+};
+
+struct feature_fme_header {
+	struct feature_header header;
+	struct feature_afu_header afu_header;
+	u64 rsvd[2];
+	struct feature_fme_capability capability;
+	struct feature_fme_port port[MAX_FPGA_PORT_NUM];
+};
+
+/* FME Thermal Sub Feature Register Set */
+struct feature_fme_thermal {
+	struct feature_header header;
+};
+
+/* FME Power Sub Feature Register Set */
+struct feature_fme_power {
+	struct feature_header header;
+};
+
+/* FME Global Performance Sub Feature Register Set */
+struct feature_fme_gperf {
+	struct feature_header header;
+};
+
+/* FME Error Sub Feature Register Set */
+struct feature_fme_err {
+	struct feature_header header;
+};
+
+/* FME Partial Reconfiguration Sub Feature Register Set */
+struct feature_fme_pr {
+	struct feature_header header;
+};
+
+/* PORT Header Register Set */
+/* Port Capability Register */
+struct feature_port_capability {
+	union {
+		u64 csr;
+		struct {
+			u64 port_number:2;	/* Port Number 0-3 */
+			u64 rsvdz1:6;
+			u64 mmio_size:16;	/* User MMIO size in KB */
+			u64 rsvdz2:8;
+			u64 sp_intr_num:4;	/* Supported interrupts num */
+			u64 rsvdz3:28;
+		};
+	};
+};
+
+/* Port Control Register */
+struct feature_port_control {
+	union {
+		u64 csr;
+		struct {
+			u64 port_sftrst:1;	/* Port Soft Reset */
+			u64 rsvdz1:1;
+			u64 latency_tolerance:1;/* '1' >= 40us, '0' < 40us */
+			u64 rsvdz2:1;
+			u64 port_sftrst_ack:1;	/* HW ACK for Soft Reset */
+			u64 rsvdz3:59;
+		};
+	};
+};
+
+struct feature_port_header {
+	struct feature_header header;
+	struct feature_afu_header afu_header;
+	u64 rsvd[2];
+	struct feature_port_capability capability;
+	struct feature_port_control control;
+};
+
+/* PORT Error Sub Feature Register Set */
+struct feature_port_error {
+	struct feature_header header;
+};
+
+/* PORT Unordered Message Sub Feature Register Set */
+struct feature_port_umsg {
+	struct feature_header header;
+};
+
+/* PORT SignalTap Sub Feature Register Set */
+struct feature_port_stp {
+	struct feature_header header;
+};
+
+#pragma pack()
+
+struct feature {
+	const char *name;
+	int resource_index;
+	void __iomem *ioaddr;
+};
+
+struct feature_platform_data {
+	/* list the feature dev to cci_drvdata->port_dev_list. */
+	struct list_head node;
+	struct mutex lock;
+	struct platform_device *dev;
+	unsigned int disable_count;	/* count for port disable */
+
+	int num;			/* number of features */
+	struct feature features[0];
+};
+
+enum fme_feature_id {
+	FME_FEATURE_ID_HEADER = 0x0,
+	FME_FEATURE_ID_THERMAL_MGMT = 0x1,
+	FME_FEATURE_ID_POWER_MGMT = 0x2,
+	FME_FEATURE_ID_GLOBAL_PERF = 0x3,
+	FME_FEATURE_ID_GLOBAL_ERR = 0x4,
+	FME_FEATURE_ID_PR_MGMT = 0x5,
+	FME_FEATURE_ID_MAX = 0x6,
+};
+
+enum port_feature_id {
+	PORT_FEATURE_ID_HEADER = 0x0,
+	PORT_FEATURE_ID_ERROR = 0x1,
+	PORT_FEATURE_ID_UMSG = 0x2,
+	PORT_FEATURE_ID_PR = 0x3,
+	PORT_FEATURE_ID_STP = 0x4,
+	PORT_FEATURE_ID_UAFU = 0x5,
+	PORT_FEATURE_ID_MAX = 0x6,
+};
+
+int fme_feature_num(void);
+int port_feature_num(void);
+
+#define FPGA_FEATURE_DEV_FME		"intel-fpga-fme"
+#define FPGA_FEATURE_DEV_PORT		"intel-fpga-port"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+			       int index, const char *name,
+			       int resource_index, void __iomem *ioaddr);
+
+static inline int feature_platform_data_size(const int num)
+{
+	return sizeof(struct feature_platform_data) +
+		num * sizeof(struct feature);
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num);
+
+int fpga_port_id(struct platform_device *pdev);
+
+static inline int fpga_port_check_id(struct platform_device *pdev,
+				     void *pport_id)
+{
+	return fpga_port_id(pdev) == *(int *)pport_id;
+}
+
+void __fpga_port_enable(struct platform_device *pdev);
+int __fpga_port_disable(struct platform_device *pdev);
+
+static inline void fpga_port_enable(struct platform_device *pdev)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+	mutex_lock(&pdata->lock);
+	__fpga_port_enable(pdev);
+	mutex_unlock(&pdata->lock);
+}
+
+static inline int fpga_port_disable(struct platform_device *pdev)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	int ret;
+
+	mutex_lock(&pdata->lock);
+	ret = __fpga_port_disable(pdev);
+	mutex_unlock(&pdata->lock);
+
+	return ret;
+}
+
+static inline int __fpga_port_reset(struct platform_device *pdev)
+{
+	int ret;
+
+	ret = __fpga_port_disable(pdev);
+	if (ret)
+		return ret;
+
+	__fpga_port_enable(pdev);
+	return 0;
+}
+
+static inline int fpga_port_reset(struct platform_device *pdev)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	int ret;
+
+	mutex_lock(&pdata->lock);
+	ret = __fpga_port_reset(pdev);
+	mutex_unlock(&pdata->lock);
+	return ret;
+}
+
+static inline void __iomem *
+get_feature_ioaddr_by_index(struct device *dev, int index)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(dev);
+
+	return pdata->features[index].ioaddr;
+}
+#endif
drivers/fpga/intel-pcie.c +839 -2
index f697de4..70b8284 100644
@@ -23,10 +23,827 @@
 #include <linux/stddef.h>
 #include <linux/errno.h>
 #include <linux/aer.h>
+#include <linux/fpga/fpga-dev.h>
+
+#include "intel-feature-dev.h"
 
 #define DRV_VERSION	"0.8"
 #define DRV_NAME	"intel-fpga-pci"
 
+#define INTEL_FPGA_DEV	"intel-fpga-dev"
+
+static DEFINE_MUTEX(fpga_id_mutex);
+
+enum fpga_id_type {
+	FME_ID,		/* fme id allocation and mapping */
+	PORT_ID,	/* port id allocation and mapping */
+	FPGA_ID_MAX,
+};
+
+/* it is protected by fpga_id_mutex */
+static struct idr fpga_ids[FPGA_ID_MAX];
+
+struct cci_drvdata {
+	struct device *fme_dev;
+
+	struct mutex lock;
+	struct list_head port_dev_list;
+
+	struct list_head regions; /* global list of pci bar mapping region */
+};
+
+/* pci bar mapping info */
+struct cci_pci_region {
+	int bar;
+	void __iomem *ioaddr;	/* pointer to mapped bar region */
+	struct list_head node;
+};
+
+static void fpga_ids_init(void)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+		idr_init(fpga_ids + i);
+}
+
+static void fpga_ids_destroy(void)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+		idr_destroy(fpga_ids + i);
+}
+
+static int alloc_fpga_id(enum fpga_id_type type, struct device *dev)
+{
+	int id;
+
+	WARN_ON(type >= FPGA_ID_MAX);
+	mutex_lock(&fpga_id_mutex);
+	id = idr_alloc(fpga_ids + type, dev, 0, 0, GFP_KERNEL);
+	mutex_unlock(&fpga_id_mutex);
+	return id;
+}
+
+static void free_fpga_id(enum fpga_id_type type, int id)
+{
+	WARN_ON(type >= FPGA_ID_MAX);
+	mutex_lock(&fpga_id_mutex);
+	idr_remove(fpga_ids + type, id);
+	mutex_unlock(&fpga_id_mutex);
+}
+
+static void cci_pci_add_port_dev(struct pci_dev *pdev,
+				 struct platform_device *port_dev)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+	struct feature_platform_data *pdata = dev_get_platdata(&port_dev->dev);
+
+	mutex_lock(&drvdata->lock);
+	list_add(&pdata->node, &drvdata->port_dev_list);
+	get_device(&pdata->dev->dev);
+	mutex_unlock(&drvdata->lock);
+}
+
+static void cci_pci_remove_port_devs(struct pci_dev *pdev)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+	struct feature_platform_data *pdata, *ptmp;
+
+	mutex_lock(&drvdata->lock);
+	list_for_each_entry_safe(pdata, ptmp, &drvdata->port_dev_list, node) {
+		struct platform_device *port_dev = pdata->dev;
+
+		/* the port should be unregistered first. */
+		WARN_ON(device_is_registered(&port_dev->dev));
+		list_del(&pdata->node);
+		free_fpga_id(PORT_ID, port_dev->id);
+		put_device(&port_dev->dev);
+	}
+	mutex_unlock(&drvdata->lock);
+}
+
+/* info collection during feature dev build. */
+struct build_feature_devs_info {
+	struct pci_dev *pdev;
+
+	/*
+	 * PCI BAR mapping info. Parsing feature list starts from
+	 * BAR 0 and switch to different BARs to parse Port
+	 */
+	void __iomem *ioaddr;
+	void __iomem *ioend;
+	int current_bar;
+
+	/* points to FME header where the port offset is figured out. */
+	void __iomem *pfme_hdr;
+
+	/* the container device for all feature devices */
+	struct fpga_dev *parent_dev;
+
+	/* current feature device */
+	struct platform_device *feature_dev;
+};
+
+static void cci_pci_release_regions(struct pci_dev *pdev)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+	struct cci_pci_region *tmp, *region;
+
+	list_for_each_entry_safe(region, tmp, &drvdata->regions, node) {
+		list_del(&region->node);
+		if (region->ioaddr)
+			pci_iounmap(pdev, region->ioaddr);
+		devm_kfree(&pdev->dev, region);
+	}
+}
+
+static void __iomem *cci_pci_ioremap_bar(struct pci_dev *pdev, int bar)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+	struct cci_pci_region *region;
+
+	list_for_each_entry(region, &drvdata->regions, node)
+		if (region->bar == bar) {
+			dev_dbg(&pdev->dev, "BAR %d region exists\n", bar);
+			return region->ioaddr;
+		}
+
+	region = devm_kzalloc(&pdev->dev, sizeof(*region), GFP_KERNEL);
+	if (!region)
+		return NULL;
+
+	region->bar = bar;
+	region->ioaddr = pci_ioremap_bar(pdev, bar);
+	if (!region->ioaddr) {
+		dev_err(&pdev->dev, "can't ioremap memory from BAR %d.\n", bar);
+		devm_kfree(&pdev->dev, region);
+		return NULL;
+	}
+
+	list_add(&region->node, &drvdata->regions);
+	return region->ioaddr;
+}
+
+static int parse_start_from(struct build_feature_devs_info *binfo, int bar)
+{
+	binfo->ioaddr = cci_pci_ioremap_bar(binfo->pdev, bar);
+	if (!binfo->ioaddr)
+		return -ENOMEM;
+
+	binfo->current_bar = bar;
+	binfo->ioend = binfo->ioaddr + pci_resource_len(binfo->pdev, bar);
+	return 0;
+}
+
+static int parse_start(struct build_feature_devs_info *binfo)
+{
+	/* fpga feature list starts from BAR 0 */
+	return parse_start_from(binfo, 0);
+}
+
+/* switch the memory mapping to BAR# @bar */
+static int parse_switch_to(struct build_feature_devs_info *binfo, int bar)
+{
+	return parse_start_from(binfo, bar);
+}
+
+static struct build_feature_devs_info *
+build_info_alloc_and_init(struct pci_dev *pdev)
+{
+	struct build_feature_devs_info *binfo;
+
+	binfo = devm_kzalloc(&pdev->dev, sizeof(*binfo), GFP_KERNEL);
+	if (binfo)
+		binfo->pdev = pdev;
+
+	return binfo;
+}
+
+static enum fpga_id_type feature_dev_id_type(struct platform_device *pdev)
+{
+	if (!strcmp(pdev->name, FPGA_FEATURE_DEV_FME))
+		return FME_ID;
+
+	if (!strcmp(pdev->name, FPGA_FEATURE_DEV_PORT))
+		return PORT_ID;
+
+	WARN_ON(1);
+	return FPGA_ID_MAX;
+}
+
+/*
+ * register current feature device, it is called when we need to switch to
+ * another feature parsing or we have parsed all features
+ */
+static int build_info_commit_dev(struct build_feature_devs_info *binfo)
+{
+	int ret;
+
+	if (!binfo->feature_dev)
+		return 0;
+
+	ret = platform_device_add(binfo->feature_dev);
+	if (!ret) {
+		struct cci_drvdata *drvdata;
+
+		drvdata = dev_get_drvdata(&binfo->pdev->dev);
+		if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
+			cci_pci_add_port_dev(binfo->pdev, binfo->feature_dev);
+		else
+			drvdata->fme_dev = get_device(&binfo->feature_dev->dev);
+
+		/*
+		 * reset it to avoid build_info_free() freeing their resource.
+		 *
+		 * The resource of successfully registered feature devices
+		 * will be freed by platform_device_unregister(). See the
+		 * comments in build_info_create_dev().
+		 */
+		binfo->feature_dev = NULL;
+	}
+
+	return ret;
+}
+
+static int
+build_info_create_dev(struct build_feature_devs_info *binfo,
+		      enum fpga_id_type type, int feature_nr, const char *name)
+{
+	struct platform_device *fdev;
+	struct resource *res;
+	struct feature_platform_data *pdata;
+	int ret;
+
+	/* we will create a new device, commit current device first */
+	ret = build_info_commit_dev(binfo);
+	if (ret)
+		return ret;
+
+	/*
+	 * we use -ENODEV as the initialization indicator which indicates
+	 * whether the id need to be reclaimed
+	 */
+	fdev = binfo->feature_dev = platform_device_alloc(name, -ENODEV);
+	if (!fdev)
+		return -ENOMEM;
+
+	fdev->id = alloc_fpga_id(type, &fdev->dev);
+	if (fdev->id < 0)
+		return fdev->id;
+
+	fdev->dev.parent = &binfo->parent_dev->dev;
+
+	/*
+	 * we do not need to care for the memory which is associated with
+	 * the platform device. After calling platform_device_unregister(),
+	 * it will be automatically freed by device's release() callback,
+	 * platform_device_release().
+	 */
+	pdata = feature_platform_data_alloc_and_init(fdev, feature_nr);
+	if (!pdata)
+		return -ENOMEM;
+
+	/*
+	 * the count should be initialized to 0 to make sure
+	 *__fpga_port_enable() following __fpga_port_disable()
+	 * works properly for port device.
+	 * and it should always be 0 for fme device.
+	 */
+	WARN_ON(pdata->disable_count);
+
+	fdev->dev.platform_data = pdata;
+	fdev->num_resources = feature_nr;
+	fdev->resource = kcalloc(feature_nr, sizeof(*res), GFP_KERNEL);
+	if (!fdev->resource)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static int remove_feature_dev(struct device *dev, void *data)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+
+	platform_device_unregister(pdev);
+	return 0;
+}
+
+static int remove_parent_dev(struct device *dev, void *data)
+{
+	/* remove platform devices attached in the parent device */
+	device_for_each_child(dev, NULL, remove_feature_dev);
+	fpga_dev_destroy(to_fpga_dev(dev));
+	return 0;
+}
+
+static void remove_all_devs(struct pci_dev *pdev)
+{
+	/* remove parent device and all its children. */
+	device_for_each_child(&pdev->dev, NULL, remove_parent_dev);
+}
+
+static void build_info_free(struct build_feature_devs_info *binfo)
+{
+	if (!IS_ERR_OR_NULL(binfo->parent_dev))
+		remove_all_devs(binfo->pdev);
+
+	/*
+	 * it is a valid id, free it. See comments in
+	 * build_info_create_dev()
+	 */
+	if (binfo->feature_dev && binfo->feature_dev->id >= 0)
+		free_fpga_id(feature_dev_id_type(binfo->feature_dev),
+			     binfo->feature_dev->id);
+
+	platform_device_put(binfo->feature_dev);
+
+	devm_kfree(&binfo->pdev->dev, binfo);
+}
+
+#define FEATURE_TYPE_AFU	0x1
+#define FEATURE_TYPE_PRIVATE	0x3
+
+/* FME and PORT GUID are fixed */
+#define FEATURE_FME_GUID "f9e17764-38f0-82fe-e346-524ae92aafbf"
+#define FEATURE_PORT_GUID "6b355b87-b06c-9642-eb42-8d139398b43a"
+
+static bool feature_is_fme(struct feature_afu_header *afu_hdr)
+{
+	uuid_le u;
+
+	uuid_le_to_bin(FEATURE_FME_GUID, &u);
+
+	return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+static bool feature_is_port(struct feature_afu_header *afu_hdr)
+{
+	uuid_le u;
+
+	uuid_le_to_bin(FEATURE_PORT_GUID, &u);
+
+	return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+/*
+ * UAFU GUID is dynamic as it can be changed after FME downloads different
+ * Green Bitstream to the port, so we treat the unknown GUIDs which are
+ * attached on port's feature list as UAFU.
+ */
+static bool feature_is_UAFU(struct build_feature_devs_info *binfo)
+{
+	if (!binfo->feature_dev ||
+	      feature_dev_id_type(binfo->feature_dev) != PORT_ID)
+		return false;
+
+	return true;
+}
+
+static void
+build_info_add_sub_feature(struct build_feature_devs_info *binfo,
+			   int feature_id, const char *feature_name,
+			   resource_size_t resource_size, void __iomem *start)
+{
+
+	struct platform_device *fdev = binfo->feature_dev;
+	struct feature_platform_data *pdata = dev_get_platdata(&fdev->dev);
+	struct resource *res = &fdev->resource[feature_id];
+
+	res->start = pci_resource_start(binfo->pdev, binfo->current_bar) +
+		start - binfo->ioaddr;
+	res->end = res->start + resource_size - 1;
+	res->flags = IORESOURCE_MEM;
+	res->name = feature_name;
+
+	feature_platform_data_add(pdata, feature_id,
+				  feature_name, feature_id, start);
+}
+
+struct feature_info {
+	const char *name;
+	resource_size_t resource_size;
+	int feature_index;
+};
+
+/* indexed by fme feature IDs which are defined in 'enum fme_feature_id'. */
+static struct feature_info fme_features[] = {
+	{
+		.name = FME_FEATURE_HEADER,
+		.resource_size = sizeof(struct feature_fme_header),
+		.feature_index = FME_FEATURE_ID_HEADER,
+	},
+	{
+		.name = FME_FEATURE_THERMAL_MGMT,
+		.resource_size = sizeof(struct feature_fme_thermal),
+		.feature_index = FME_FEATURE_ID_THERMAL_MGMT,
+	},
+	{
+		.name = FME_FEATURE_POWER_MGMT,
+		.resource_size = sizeof(struct feature_fme_power),
+		.feature_index = FME_FEATURE_ID_POWER_MGMT,
+	},
+	{
+		.name = FME_FEATURE_GLOBAL_PERF,
+		.resource_size = sizeof(struct feature_fme_gperf),
+		.feature_index = FME_FEATURE_ID_GLOBAL_PERF,
+	},
+	{
+		.name = FME_FEATURE_GLOBAL_ERR,
+		.resource_size = sizeof(struct feature_fme_err),
+		.feature_index = FME_FEATURE_ID_GLOBAL_ERR,
+	},
+	{
+		.name = FME_FEATURE_PR_MGMT,
+		.resource_size = sizeof(struct feature_fme_pr),
+		.feature_index = FME_FEATURE_ID_PR_MGMT,
+	}
+};
+
+/* indexed by port feature IDs which are defined in 'enum port_feature_id'. */
+static struct feature_info port_features[] = {
+	{
+		.name = PORT_FEATURE_HEADER,
+		.resource_size = sizeof(struct feature_port_header),
+		.feature_index = PORT_FEATURE_ID_HEADER,
+	},
+	{
+		.name = PORT_FEATURE_ERR,
+		.resource_size = sizeof(struct feature_port_error),
+		.feature_index = PORT_FEATURE_ID_ERROR,
+	},
+	{
+		.name = PORT_FEATURE_UMSG,
+		.resource_size = sizeof(struct feature_port_umsg),
+		.feature_index = PORT_FEATURE_ID_UMSG,
+	},
+	{
+		/* This feature isn't available for now */
+		.name = PORT_FEATURE_PR,
+		.resource_size = 0,
+		.feature_index = PORT_FEATURE_ID_PR,
+	},
+	{
+		.name = PORT_FEATURE_STP,
+		.resource_size = sizeof(struct feature_port_stp),
+		.feature_index = PORT_FEATURE_ID_STP,
+	},
+	{
+		/*
+		 * For User AFU feature, its region size is not fixed, but
+		 * reported by register PortCapability.mmio_size. Resource
+		 * size of UAFU will be set while parse port device.
+		 */
+		.name = PORT_FEATURE_UAFU,
+		.resource_size = 0,
+		.feature_index = PORT_FEATURE_ID_UAFU,
+	},
+};
+
+static int
+create_feature_instance(struct build_feature_devs_info *binfo,
+			void __iomem *start, struct feature_info *finfo)
+{
+	if (binfo->ioend - start < finfo->resource_size)
+		return -EINVAL;
+
+	build_info_add_sub_feature(binfo, finfo->feature_index, finfo->name,
+				   finfo->resource_size, start);
+	return 0;
+}
+
+static int parse_feature_fme(struct build_feature_devs_info *binfo,
+			     void __iomem *start)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&binfo->pdev->dev);
+	int ret;
+
+	ret = build_info_create_dev(binfo, FME_ID, fme_feature_num(),
+					FPGA_FEATURE_DEV_FME);
+	if (ret)
+		return ret;
+
+	if (drvdata->fme_dev) {
+		dev_err(&binfo->pdev->dev, "Multiple FMEs are detected.\n");
+		return -EINVAL;
+	}
+
+	return create_feature_instance(binfo, start,
+				       &fme_features[FME_FEATURE_ID_HEADER]);
+}
+
+static int parse_feature_fme_private(struct build_feature_devs_info *binfo,
+				     struct feature_header *hdr)
+{
+	struct feature_header header;
+
+	header.csr = readq(hdr);
+
+	if (header.id >= ARRAY_SIZE(fme_features)) {
+		dev_info(&binfo->pdev->dev, "FME feature id %x is not supported yet.\n",
+			 header.id);
+		return 0;
+	}
+
+	return create_feature_instance(binfo, hdr, &fme_features[header.id]);
+}
+
+static int parse_feature_port(struct build_feature_devs_info *binfo,
+			     void __iomem *start)
+{
+	int ret;
+
+	ret = build_info_create_dev(binfo, PORT_ID, port_feature_num(),
+					FPGA_FEATURE_DEV_PORT);
+	if (ret)
+		return ret;
+
+	return create_feature_instance(binfo, start,
+				       &port_features[PORT_FEATURE_ID_HEADER]);
+}
+
+static void enable_port_uafu(struct build_feature_devs_info *binfo,
+			     void __iomem *start)
+{
+	enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+	struct feature_port_header *port_hdr;
+	struct feature_port_capability capability;
+
+	port_hdr = (struct feature_port_header *)start;
+	capability.csr = readq(&port_hdr->capability);
+	port_features[id].resource_size = capability.mmio_size << 10;
+
+	/*
+	 * To enable User AFU, driver needs to clear reset bit on related port,
+	 * otherwise the mmio space of this user AFU will be invalid.
+	 */
+	if (port_features[id].resource_size)
+		fpga_port_reset(binfo->feature_dev);
+}
+
+static int parse_feature_port_private(struct build_feature_devs_info *binfo,
+				      struct feature_header *hdr)
+{
+	struct feature_header header;
+	enum port_feature_id id;
+
+	header.csr = readq(hdr);
+	/*
+	 * the region of port feature id is [0x10, 0x13], + 1 to reserve 0
+	 * which is dedicated for port-hdr.
+	 */
+	id = (header.id & 0x000f) + 1;
+
+	if (id >= ARRAY_SIZE(port_features)) {
+		dev_info(&binfo->pdev->dev, "Port feature id %x is not supported yet.\n",
+			 header.id);
+		return 0;
+	}
+
+	return create_feature_instance(binfo, hdr, &port_features[id]);
+}
+
+static int parse_feature_port_uafu(struct build_feature_devs_info *binfo,
+				 struct feature_header *hdr)
+{
+	enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+	int ret;
+
+	if (port_features[id].resource_size) {
+		ret = create_feature_instance(binfo, hdr, &port_features[id]);
+		port_features[id].resource_size = 0;
+	} else {
+		dev_err(&binfo->pdev->dev, "the uafu feature header is mis-configured.\n");
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+static int parse_feature_afus(struct build_feature_devs_info *binfo,
+			      struct feature_header *hdr)
+{
+	int ret;
+	struct feature_afu_header *afu_hdr, header;
+	void __iomem *start;
+	void __iomem *end = binfo->ioend;
+
+	start = hdr;
+	for (; start < end; start += header.next_afu) {
+		if (end - start < (sizeof(*afu_hdr) + sizeof(*hdr)))
+			return -EINVAL;
+
+		hdr = start;
+		afu_hdr = (struct feature_afu_header *) (hdr + 1);
+		header.csr = readq(&afu_hdr->csr);
+
+		if (feature_is_fme(afu_hdr)) {
+			ret = parse_feature_fme(binfo, hdr);
+			binfo->pfme_hdr = hdr;
+			if (ret)
+				return ret;
+		} else if (feature_is_port(afu_hdr)) {
+			ret = parse_feature_port(binfo, hdr);
+			enable_port_uafu(binfo, hdr);
+			if (ret)
+				return ret;
+		} else if (feature_is_UAFU(binfo)) {
+			ret = parse_feature_port_uafu(binfo, hdr);
+			if (ret)
+				return ret;
+		} else
+			dev_info(&binfo->pdev->dev, "AFU GUID %pUl is not supported yet.\n",
+				 afu_hdr->guid.b);
+
+		if (!header.next_afu)
+			break;
+	}
+
+	return 0;
+}
+
+static int parse_feature_private(struct build_feature_devs_info *binfo,
+				 struct feature_header *hdr)
+{
+	struct feature_header header;
+
+	header.csr = readq(hdr);
+
+	if (!binfo->feature_dev) {
+		dev_err(&binfo->pdev->dev, "the private feature %x does not belong to any AFU.\n",
+			header.id);
+		return -EINVAL;
+	}
+
+	switch (feature_dev_id_type(binfo->feature_dev)) {
+	case FME_ID:
+		return parse_feature_fme_private(binfo, hdr);
+	case PORT_ID:
+		return parse_feature_port_private(binfo, hdr);
+	default:
+		dev_info(&binfo->pdev->dev, "private feature %x belonging to AFU %s is not supported yet.\n",
+			 header.id, binfo->feature_dev->name);
+	}
+	return 0;
+}
+
+static int parse_feature(struct build_feature_devs_info *binfo,
+			 struct feature_header *hdr)
+{
+	struct feature_header header;
+	int ret = 0;
+
+	header.csr = readq(hdr);
+
+	switch (header.type) {
+	case FEATURE_TYPE_AFU:
+		ret = parse_feature_afus(binfo, hdr);
+		break;
+	case FEATURE_TYPE_PRIVATE:
+		ret = parse_feature_private(binfo, hdr);
+		break;
+	default:
+		dev_info(&binfo->pdev->dev,
+			 "Feature Type %x is not supported.\n", hdr->type);
+	};
+
+	return ret;
+}
+
+static int
+parse_feature_list(struct build_feature_devs_info *binfo, void __iomem *start)
+{
+	struct feature_header *hdr, header;
+	void __iomem *end = binfo->ioend;
+	int ret = 0;
+
+	for (; start < end; start += header.next_header_offset) {
+		if (end - start < sizeof(*hdr)) {
+			dev_err(&binfo->pdev->dev, "The region is too small to contain a feature.\n");
+			ret =  -EINVAL;
+			break;
+		}
+
+		hdr = (struct feature_header *)start;
+		ret = parse_feature(binfo, hdr);
+		if (ret)
+			break;
+
+		header.csr = readq(hdr);
+		if (!header.next_header_offset)
+			break;
+	}
+
+	return ret;
+}
+
+static int parse_ports_from_fme(struct build_feature_devs_info *binfo)
+{
+	struct feature_fme_header *fme_hdr;
+	struct feature_fme_port port;
+	int i = 0, ret = 0;
+
+	if (binfo->pfme_hdr == NULL) {
+		dev_dbg(&binfo->pdev->dev, "VF is detected.\n");
+		return ret;
+	}
+
+	fme_hdr = binfo->pfme_hdr;
+
+	do {
+		port.csr = readq(&fme_hdr->port[i]);
+		if (!port.port_implemented)
+			break;
+
+		ret = parse_switch_to(binfo, port.port_bar);
+		if (ret)
+			break;
+
+		ret = parse_feature_list(binfo,
+				binfo->ioaddr + port.port_offset);
+		if (ret)
+			break;
+	} while (++i < MAX_FPGA_PORT_NUM);
+
+	return ret;
+}
+
+static int create_init_drvdata(struct pci_dev *pdev)
+{
+	struct cci_drvdata *drvdata;
+
+	drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
+	if (!drvdata)
+		return -ENOMEM;
+
+	mutex_init(&drvdata->lock);
+	INIT_LIST_HEAD(&drvdata->port_dev_list);
+	INIT_LIST_HEAD(&drvdata->regions);
+
+	dev_set_drvdata(&pdev->dev, drvdata);
+	return 0;
+}
+
+static void destroy_drvdata(struct pci_dev *pdev)
+{
+	struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+
+	if (drvdata->fme_dev) {
+		/* fme device should be unregistered first. */
+		WARN_ON(device_is_registered(drvdata->fme_dev));
+		free_fpga_id(FME_ID, to_platform_device(drvdata->fme_dev)->id);
+		put_device(drvdata->fme_dev);
+	}
+
+	cci_pci_remove_port_devs(pdev);
+	cci_pci_release_regions(pdev);
+	dev_set_drvdata(&pdev->dev, NULL);
+	devm_kfree(&pdev->dev, drvdata);
+}
+
+static int cci_pci_create_feature_devs(struct pci_dev *pdev)
+{
+	struct build_feature_devs_info *binfo;
+	int ret;
+
+	binfo = build_info_alloc_and_init(pdev);
+	if (!binfo)
+		return -ENOMEM;
+
+	binfo->parent_dev = fpga_dev_create(&pdev->dev, INTEL_FPGA_DEV);
+	if (IS_ERR(binfo->parent_dev)) {
+		ret = PTR_ERR(binfo->parent_dev);
+		goto free_binfo_exit;
+	}
+
+	ret = parse_start(binfo);
+	if (ret)
+		goto free_binfo_exit;
+
+	ret = parse_feature_list(binfo, binfo->ioaddr);
+	if (ret)
+		goto free_binfo_exit;
+
+	ret = parse_ports_from_fme(binfo);
+	if (ret)
+		goto free_binfo_exit;
+
+	ret = build_info_commit_dev(binfo);
+	if (ret)
+		goto free_binfo_exit;
+
+	/*
+	 * everything is okay, reset ->parent_dev to stop it being
+	 * freed by build_info_free()
+	 */
+	binfo->parent_dev = NULL;
+
+free_binfo_exit:
+	build_info_free(binfo);
+	return ret;
+}
+
 /* PCI Device ID */
 #define PCIe_DEVICE_ID_PF_INT_5_X	0xBCBD
 #define PCIe_DEVICE_ID_PF_INT_6_X	0xBCC0
@@ -81,9 +898,18 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
 		goto release_region_exit;
 	}
 
-	/* TODO: create and add the platform device per feature list */
+	ret = create_init_drvdata(pcidev);
+	if (ret)
+		goto release_region_exit;
+
+	ret = cci_pci_create_feature_devs(pcidev);
+	if (ret)
+		goto destroy_drvdata_exit;
+
 	return 0;
 
+destroy_drvdata_exit:
+	destroy_drvdata(pcidev);
 release_region_exit:
 	pci_release_regions(pcidev);
 disable_error_report_exit:
@@ -94,6 +920,8 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
 
 static void cci_pci_remove(struct pci_dev *pcidev)
 {
+	remove_all_devs(pcidev);
+	destroy_drvdata(pcidev);
 	pci_release_regions(pcidev);
 	pci_disable_pcie_error_reporting(pcidev);
 	pci_disable_device(pcidev);
@@ -108,14 +936,23 @@ static void cci_pci_remove(struct pci_dev *pcidev)
 
 static int __init ccidrv_init(void)
 {
+	int ret;
+
 	pr_info("Intel(R) FPGA PCIe Driver: Version %s\n", DRV_VERSION);
 
-	return pci_register_driver(&cci_pci_driver);
+	fpga_ids_init();
+
+	ret = pci_register_driver(&cci_pci_driver);
+	if (ret)
+		fpga_ids_destroy();
+
+	return ret;
 }
 
 static void __exit ccidrv_exit(void)
 {
 	pci_unregister_driver(&cci_pci_driver);
+	fpga_ids_destroy();
 }
 
 module_init(ccidrv_init);
Signature · 1 line
1.8.3.1

Thanks,

Moritz

[1] https://www.kernel.org/doc/html/v4.11/doc-guide/kernel-doc.html#writing-kernel-doc-comments
--
To unsubscribe from this list: send the line "unsubscribe linux-fpga" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#38 lore raw

Re: [PATCH v2 07/22] fpga: intel: pcie: parse feature list and create platform device for features.

In reply to Moritz Fischer

On Mon, Jun 26, 2017 at 1:42 PM, Moritz Fischer <[email protected]> wrote:

Hi Hao,

I'll be out until July 5. I'll be able to start looking at this v2 after that.

Alan

Quoted 1,430 lines

Hi Xiao / Wu,

general question, can't you make the /* */ comments for the structs kernel-doc ([1]) markup instead (see below)?

Quoted 252 lines · Wu Hao

From: Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>

Device Feature List structure creates a link list of feature headers within the MMIO space to provide an extensible way of adding features.

The Intel FPGA PCIe driver walks through the feature headers to enumerate feature devices, FPGA Management Engine (FME) and FPGA Port for Accelerated Function Unit (AFU), and their private sub features. For feature devices, it creates the platform devices and linked the private sub features into their platform data.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.
fixed kbuild warning, typos and clean up the code.

---
 drivers/fpga/Makefile            |   2 +-
 drivers/fpga/intel-feature-dev.c | 130 ++++++
 drivers/fpga/intel-feature-dev.h | 341 ++++++++++++++++
 drivers/fpga/intel-pcie.c        | 841 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 1311 insertions(+), 3 deletions(-)
 create mode 100644 drivers/fpga/intel-feature-dev.c
 create mode 100644 drivers/fpga/intel-feature-dev.h
drivers/fpga/Makefile +1 -1
index 5613133..ad24b3d 100644
@@ -31,4 +31,4 @@ obj-$(CONFIG_OF_FPGA_REGION)                += of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)         += intel-fpga-pci.o
 
-intel-fpga-pci-objs := intel-pcie.o
+intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
drivers/fpga/intel-feature-dev.c new file +130 -0
new file mode 100644
index 0000000..68f9cba
@@ -0,0 +1,130 @@
+/*
+ * Intel FPGA Feature Device Driver
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "intel-feature-dev.h"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                            int index, const char *name,
+                            int resource_index, void __iomem *ioaddr)
+{
+     WARN_ON(index >= pdata->num);
+
+     pdata->features[index].name = name;
+     pdata->features[index].resource_index = resource_index;
+     pdata->features[index].ioaddr = ioaddr;
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num)
+{
+     struct feature_platform_data *pdata;
+
+     pdata = kzalloc(feature_platform_data_size(num), GFP_KERNEL);
+     if (pdata) {
+             pdata->dev = dev;
+             pdata->num = num;
+             mutex_init(&pdata->lock);
+     }
+
+     return pdata;
+}
+
+int fme_feature_num(void)
+{
+     return FME_FEATURE_ID_MAX;
+}
+
+int port_feature_num(void)
+{
+     return PORT_FEATURE_ID_MAX;
+}
+
+int fpga_port_id(struct platform_device *pdev)
+{
+     struct feature_port_header *port_hdr;
+     struct feature_port_capability capability;
+
+     port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                            PORT_FEATURE_ID_HEADER);
+     WARN_ON(!port_hdr);
+
+     capability.csr = readq(&port_hdr->capability);
+     return capability.port_number;
+}
+EXPORT_SYMBOL_GPL(fpga_port_id);
+
+/*
+ * Enable Port by clear the port soft reset bit, which is set by default.
+ * The User AFU is unable to respond to any MMIO access while in reset.
+ * __fpga_port_enable function should only be used after __fpga_port_disable
+ * function.
+ */
+void __fpga_port_enable(struct platform_device *pdev)
+{
+     struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+     struct feature_port_header *port_hdr;
+     struct feature_port_control control;
+
+     WARN_ON(!pdata->disable_count);
+
+     if (--pdata->disable_count != 0)
+             return;
+
+     port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                            PORT_FEATURE_ID_HEADER);
+     WARN_ON(!port_hdr);
+
+     control.csr = readq(&port_hdr->control);
+     control.port_sftrst = 0x0;
+     writeq(control.csr, &port_hdr->control);
+}
+EXPORT_SYMBOL_GPL(__fpga_port_enable);
+
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
+int __fpga_port_disable(struct platform_device *pdev)
+{
+     struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+     struct feature_port_header *port_hdr;
+     struct feature_port_control control;
+
+     if (pdata->disable_count++ != 0)
+             return 0;
+
+     port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                            PORT_FEATURE_ID_HEADER);
+     WARN_ON(!port_hdr);
+
+     /* Set port soft reset */
+     control.csr = readq(&port_hdr->control);
+     control.port_sftrst = 0x1;
+     writeq(control.csr, &port_hdr->control);
+
+     /*
+      * HW sets ack bit to 1 when all outstanding requests have been drained
+      * on this port and minimum soft reset pulse width has elapsed.
+      * Driver polls port_soft_reset_ack to determine if reset done by HW.
+      */
+     if (readq_poll_timeout(&port_hdr->control, control.csr,
+                            (control.port_sftrst_ack == 1),
+                            RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+             dev_err(&pdev->dev, "timeout, fail to reset device\n");
+             return -ETIMEDOUT;
+     }
+
+     return 0;
+}
+EXPORT_SYMBOL_GPL(__fpga_port_disable);
drivers/fpga/intel-feature-dev.h new file +67 -0
new file mode 100644
index 0000000..f67784a
@@ -0,0 +1,341 @@
+/*
+ * Intel FPGA Feature Device Driver Header File
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef __INTEL_FPGA_FEATURE_H
+#define __INTEL_FPGA_FEATURE_H
+
+#include <linux/fs.h>
+#include <linux/pci.h>
+#include <linux/uuid.h>
+#include <linux/delay.h>
+#include <linux/iopoll.h>
+#include <linux/platform_device.h>
+
+#ifndef readq
+static inline u64 readq(void __iomem *addr)
+{
+     return readl(addr) + ((u64)readl(addr + 4) << 32);
+}
+#endif
+
+#ifndef writeq
+static inline void writeq(u64 val, void __iomem *addr)
+{
+     writel((u32) (val), addr);
+     writel((u32) (val >> 32), (addr + 4));
+}
+#endif
+
+/* maximum supported number of ports */
+#define MAX_FPGA_PORT_NUM 4
+/* plus one for fme device */
+#define MAX_FEATURE_DEV_NUM  (MAX_FPGA_PORT_NUM + 1)
+
+#define FME_FEATURE_HEADER          "fme_hdr"
+#define FME_FEATURE_THERMAL_MGMT    "fme_thermal"
+#define FME_FEATURE_POWER_MGMT      "fme_power"
+#define FME_FEATURE_GLOBAL_PERF     "fme_gperf"
+#define FME_FEATURE_GLOBAL_ERR      "fme_error"
+#define FME_FEATURE_PR_MGMT         "fme_pr"
+
+#define PORT_FEATURE_HEADER         "port_hdr"
+#define PORT_FEATURE_UAFU           "port_uafu"
+#define PORT_FEATURE_ERR            "port_err"
+#define PORT_FEATURE_UMSG           "port_umsg"
+#define PORT_FEATURE_PR             "port_pr"
+#define PORT_FEATURE_STP            "port_stp"
+
+/* All headers and structures must be byte-packed to match the spec. */
+#pragma pack(1)
+
+/* common header for all features */
+struct feature_header {
+     union {
+             u64 csr;
+             struct {

drivers/fpga/intel-feature-dev.h line 77

Quoted 10 lines
drivers/fpga/intel-feature-dev.h new file +10 -0
+                     u64 id:12;
+                     u64 revision:4;
+                     u64 next_header_offset:24; /* offset to next header */
+                     u64 rsvdz:20;
+                     u64 type:4;                /* feature type */
+#define FEATURE_TYPE_AFU             0x1
+#define FEATURE_TYPE_PRIVATE         0x3
+             };
+     };
+};

Here for example.

Quoted 1,151 lines
drivers/fpga/intel-feature-dev.h new file +264 -0
+
+/* common header for non-private features */
+struct feature_afu_header {
+     uuid_le guid;
+     union {
+             u64 csr;
+             struct {
+                     u64 next_afu:24;        /* pointer to next afu header */
+                     u64 rsvdz:40;
+             };
+     };
+};
+
+/* FME Header Register Set */
+/* FME Capability Register */
+struct feature_fme_capability {
+     union {
+             u64 csr;
+             struct {
+                     u64 fabric_verid:8;     /* Fabric version ID */
+                     u64 socket_id:1;        /* Socket id */
+                     u64 rsvdz1:3;
+                     u64 pcie0_link_avl:1;   /* PCIe0 link availability */
+                     u64 pcie1_link_avl:1;   /* PCIe1 link availability */
+                     u64 coherent_link_avl:1;/* Coherent link availability */
+                     u64 rsvdz2:1;
+                     u64 iommu_support:1;    /* IOMMU or VT-d supported */
+                     u64 num_ports:3;        /* Num of ports implemented */
+                     u64 rsvdz3:4;
+                     u64 addr_width_bits:6;  /* Address width supported */
+                     u64 rsvdz4:2;
+                     u64 cache_size:12;      /* Cache size in kb */
+                     u64 cache_assoc:4;      /* Cache Associativity */
+                     u64 rsvdz5:15;
+                     u64 lock_bit:1;         /* Latched lock bit by BIOS */
+             };
+     };
+};
+
+/* FME Port Offset Register */
+struct feature_fme_port {
+     union {
+             u64 csr;
+             struct {
+                     u64 port_offset:24;     /* Offset to port header */
+                     u64 rsvdz1:8;
+                     u64 port_bar:3;         /* Bar id */
+                     u64 rsvdz2:20;
+                     u64 afu_access_ctrl:1;  /* AFU access type: PF/VF */
+                     u64 rsvdz3:4;
+                     u64 port_implemented:1; /* Port implemented or not */
+                     u64 rsvdz4:3;
+             };
+     };
+};
+
+struct feature_fme_header {
+     struct feature_header header;
+     struct feature_afu_header afu_header;
+     u64 rsvd[2];
+     struct feature_fme_capability capability;
+     struct feature_fme_port port[MAX_FPGA_PORT_NUM];
+};
+
+/* FME Thermal Sub Feature Register Set */
+struct feature_fme_thermal {
+     struct feature_header header;
+};
+
+/* FME Power Sub Feature Register Set */
+struct feature_fme_power {
+     struct feature_header header;
+};
+
+/* FME Global Performance Sub Feature Register Set */
+struct feature_fme_gperf {
+     struct feature_header header;
+};
+
+/* FME Error Sub Feature Register Set */
+struct feature_fme_err {
+     struct feature_header header;
+};
+
+/* FME Partial Reconfiguration Sub Feature Register Set */
+struct feature_fme_pr {
+     struct feature_header header;
+};
+
+/* PORT Header Register Set */
+/* Port Capability Register */
+struct feature_port_capability {
+     union {
+             u64 csr;
+             struct {
+                     u64 port_number:2;      /* Port Number 0-3 */
+                     u64 rsvdz1:6;
+                     u64 mmio_size:16;       /* User MMIO size in KB */
+                     u64 rsvdz2:8;
+                     u64 sp_intr_num:4;      /* Supported interrupts num */
+                     u64 rsvdz3:28;
+             };
+     };
+};
+
+/* Port Control Register */
+struct feature_port_control {
+     union {
+             u64 csr;
+             struct {
+                     u64 port_sftrst:1;      /* Port Soft Reset */
+                     u64 rsvdz1:1;
+                     u64 latency_tolerance:1;/* '1' >= 40us, '0' < 40us */
+                     u64 rsvdz2:1;
+                     u64 port_sftrst_ack:1;  /* HW ACK for Soft Reset */
+                     u64 rsvdz3:59;
+             };
+     };
+};
+
+struct feature_port_header {
+     struct feature_header header;
+     struct feature_afu_header afu_header;
+     u64 rsvd[2];
+     struct feature_port_capability capability;
+     struct feature_port_control control;
+};
+
+/* PORT Error Sub Feature Register Set */
+struct feature_port_error {
+     struct feature_header header;
+};
+
+/* PORT Unordered Message Sub Feature Register Set */
+struct feature_port_umsg {
+     struct feature_header header;
+};
+
+/* PORT SignalTap Sub Feature Register Set */
+struct feature_port_stp {
+     struct feature_header header;
+};
+
+#pragma pack()
+
+struct feature {
+     const char *name;
+     int resource_index;
+     void __iomem *ioaddr;
+};
+
+struct feature_platform_data {
+     /* list the feature dev to cci_drvdata->port_dev_list. */
+     struct list_head node;
+     struct mutex lock;
+     struct platform_device *dev;
+     unsigned int disable_count;     /* count for port disable */
+
+     int num;                        /* number of features */
+     struct feature features[0];
+};
+
+enum fme_feature_id {
+     FME_FEATURE_ID_HEADER = 0x0,
+     FME_FEATURE_ID_THERMAL_MGMT = 0x1,
+     FME_FEATURE_ID_POWER_MGMT = 0x2,
+     FME_FEATURE_ID_GLOBAL_PERF = 0x3,
+     FME_FEATURE_ID_GLOBAL_ERR = 0x4,
+     FME_FEATURE_ID_PR_MGMT = 0x5,
+     FME_FEATURE_ID_MAX = 0x6,
+};
+
+enum port_feature_id {
+     PORT_FEATURE_ID_HEADER = 0x0,
+     PORT_FEATURE_ID_ERROR = 0x1,
+     PORT_FEATURE_ID_UMSG = 0x2,
+     PORT_FEATURE_ID_PR = 0x3,
+     PORT_FEATURE_ID_STP = 0x4,
+     PORT_FEATURE_ID_UAFU = 0x5,
+     PORT_FEATURE_ID_MAX = 0x6,
+};
+
+int fme_feature_num(void);
+int port_feature_num(void);
+
+#define FPGA_FEATURE_DEV_FME         "intel-fpga-fme"
+#define FPGA_FEATURE_DEV_PORT                "intel-fpga-port"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                            int index, const char *name,
+                            int resource_index, void __iomem *ioaddr);
+
+static inline int feature_platform_data_size(const int num)
+{
+     return sizeof(struct feature_platform_data) +
+             num * sizeof(struct feature);
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num);
+
+int fpga_port_id(struct platform_device *pdev);
+
+static inline int fpga_port_check_id(struct platform_device *pdev,
+                                  void *pport_id)
+{
+     return fpga_port_id(pdev) == *(int *)pport_id;
+}
+
+void __fpga_port_enable(struct platform_device *pdev);
+int __fpga_port_disable(struct platform_device *pdev);
+
+static inline void fpga_port_enable(struct platform_device *pdev)
+{
+     struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+     mutex_lock(&pdata->lock);
+     __fpga_port_enable(pdev);
+     mutex_unlock(&pdata->lock);
+}
+
+static inline int fpga_port_disable(struct platform_device *pdev)
+{
+     struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+     int ret;
+
+     mutex_lock(&pdata->lock);
+     ret = __fpga_port_disable(pdev);
+     mutex_unlock(&pdata->lock);
+
+     return ret;
+}
+
+static inline int __fpga_port_reset(struct platform_device *pdev)
+{
+     int ret;
+
+     ret = __fpga_port_disable(pdev);
+     if (ret)
+             return ret;
+
+     __fpga_port_enable(pdev);
+     return 0;
+}
+
+static inline int fpga_port_reset(struct platform_device *pdev)
+{
+     struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+     int ret;
+
+     mutex_lock(&pdata->lock);
+     ret = __fpga_port_reset(pdev);
+     mutex_unlock(&pdata->lock);
+     return ret;
+}
+
+static inline void __iomem *
+get_feature_ioaddr_by_index(struct device *dev, int index)
+{
+     struct feature_platform_data *pdata = dev_get_platdata(dev);
+
+     return pdata->features[index].ioaddr;
+}
+#endif
drivers/fpga/intel-pcie.c +839 -3
index f697de4..70b8284 100644
@@ -23,10 +23,827 @@
 #include <linux/stddef.h>
 #include <linux/errno.h>
 #include <linux/aer.h>
+#include <linux/fpga/fpga-dev.h>
+
+#include "intel-feature-dev.h"
 
 #define DRV_VERSION  "0.8"
 #define DRV_NAME     "intel-fpga-pci"
 
+#define INTEL_FPGA_DEV       "intel-fpga-dev"
+
+static DEFINE_MUTEX(fpga_id_mutex);
+
+enum fpga_id_type {
+     FME_ID,         /* fme id allocation and mapping */
+     PORT_ID,        /* port id allocation and mapping */
+     FPGA_ID_MAX,
+};
+
+/* it is protected by fpga_id_mutex */
+static struct idr fpga_ids[FPGA_ID_MAX];
+
+struct cci_drvdata {
+     struct device *fme_dev;
+
+     struct mutex lock;
+     struct list_head port_dev_list;
+
+     struct list_head regions; /* global list of pci bar mapping region */
+};
+
+/* pci bar mapping info */
+struct cci_pci_region {
+     int bar;
+     void __iomem *ioaddr;   /* pointer to mapped bar region */
+     struct list_head node;
+};
+
+static void fpga_ids_init(void)
+{
+     int i;
+
+     for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+             idr_init(fpga_ids + i);
+}
+
+static void fpga_ids_destroy(void)
+{
+     int i;
+
+     for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+             idr_destroy(fpga_ids + i);
+}
+
+static int alloc_fpga_id(enum fpga_id_type type, struct device *dev)
+{
+     int id;
+
+     WARN_ON(type >= FPGA_ID_MAX);
+     mutex_lock(&fpga_id_mutex);
+     id = idr_alloc(fpga_ids + type, dev, 0, 0, GFP_KERNEL);
+     mutex_unlock(&fpga_id_mutex);
+     return id;
+}
+
+static void free_fpga_id(enum fpga_id_type type, int id)
+{
+     WARN_ON(type >= FPGA_ID_MAX);
+     mutex_lock(&fpga_id_mutex);
+     idr_remove(fpga_ids + type, id);
+     mutex_unlock(&fpga_id_mutex);
+}
+
+static void cci_pci_add_port_dev(struct pci_dev *pdev,
+                              struct platform_device *port_dev)
+{
+     struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+     struct feature_platform_data *pdata = dev_get_platdata(&port_dev->dev);
+
+     mutex_lock(&drvdata->lock);
+     list_add(&pdata->node, &drvdata->port_dev_list);
+     get_device(&pdata->dev->dev);
+     mutex_unlock(&drvdata->lock);
+}
+
+static void cci_pci_remove_port_devs(struct pci_dev *pdev)
+{
+     struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+     struct feature_platform_data *pdata, *ptmp;
+
+     mutex_lock(&drvdata->lock);
+     list_for_each_entry_safe(pdata, ptmp, &drvdata->port_dev_list, node) {
+             struct platform_device *port_dev = pdata->dev;
+
+             /* the port should be unregistered first. */
+             WARN_ON(device_is_registered(&port_dev->dev));
+             list_del(&pdata->node);
+             free_fpga_id(PORT_ID, port_dev->id);
+             put_device(&port_dev->dev);
+     }
+     mutex_unlock(&drvdata->lock);
+}
+
+/* info collection during feature dev build. */
+struct build_feature_devs_info {
+     struct pci_dev *pdev;
+
+     /*
+      * PCI BAR mapping info. Parsing feature list starts from
+      * BAR 0 and switch to different BARs to parse Port
+      */
+     void __iomem *ioaddr;
+     void __iomem *ioend;
+     int current_bar;
+
+     /* points to FME header where the port offset is figured out. */
+     void __iomem *pfme_hdr;
+
+     /* the container device for all feature devices */
+     struct fpga_dev *parent_dev;
+
+     /* current feature device */
+     struct platform_device *feature_dev;
+};
+
+static void cci_pci_release_regions(struct pci_dev *pdev)
+{
+     struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+     struct cci_pci_region *tmp, *region;
+
+     list_for_each_entry_safe(region, tmp, &drvdata->regions, node) {
+             list_del(&region->node);
+             if (region->ioaddr)
+                     pci_iounmap(pdev, region->ioaddr);
+             devm_kfree(&pdev->dev, region);
+     }
+}
+
+static void __iomem *cci_pci_ioremap_bar(struct pci_dev *pdev, int bar)
+{
+     struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+     struct cci_pci_region *region;
+
+     list_for_each_entry(region, &drvdata->regions, node)
+             if (region->bar == bar) {
+                     dev_dbg(&pdev->dev, "BAR %d region exists\n", bar);
+                     return region->ioaddr;
+             }
+
+     region = devm_kzalloc(&pdev->dev, sizeof(*region), GFP_KERNEL);
+     if (!region)
+             return NULL;
+
+     region->bar = bar;
+     region->ioaddr = pci_ioremap_bar(pdev, bar);
+     if (!region->ioaddr) {
+             dev_err(&pdev->dev, "can't ioremap memory from BAR %d.\n", bar);
+             devm_kfree(&pdev->dev, region);
+             return NULL;
+     }
+
+     list_add(&region->node, &drvdata->regions);
+     return region->ioaddr;
+}
+
+static int parse_start_from(struct build_feature_devs_info *binfo, int bar)
+{
+     binfo->ioaddr = cci_pci_ioremap_bar(binfo->pdev, bar);
+     if (!binfo->ioaddr)
+             return -ENOMEM;
+
+     binfo->current_bar = bar;
+     binfo->ioend = binfo->ioaddr + pci_resource_len(binfo->pdev, bar);
+     return 0;
+}
+
+static int parse_start(struct build_feature_devs_info *binfo)
+{
+     /* fpga feature list starts from BAR 0 */
+     return parse_start_from(binfo, 0);
+}
+
+/* switch the memory mapping to BAR# @bar */
+static int parse_switch_to(struct build_feature_devs_info *binfo, int bar)
+{
+     return parse_start_from(binfo, bar);
+}
+
+static struct build_feature_devs_info *
+build_info_alloc_and_init(struct pci_dev *pdev)
+{
+     struct build_feature_devs_info *binfo;
+
+     binfo = devm_kzalloc(&pdev->dev, sizeof(*binfo), GFP_KERNEL);
+     if (binfo)
+             binfo->pdev = pdev;
+
+     return binfo;
+}
+
+static enum fpga_id_type feature_dev_id_type(struct platform_device *pdev)
+{
+     if (!strcmp(pdev->name, FPGA_FEATURE_DEV_FME))
+             return FME_ID;
+
+     if (!strcmp(pdev->name, FPGA_FEATURE_DEV_PORT))
+             return PORT_ID;
+
+     WARN_ON(1);
+     return FPGA_ID_MAX;
+}
+
+/*
+ * register current feature device, it is called when we need to switch to
+ * another feature parsing or we have parsed all features
+ */
+static int build_info_commit_dev(struct build_feature_devs_info *binfo)
+{
+     int ret;
+
+     if (!binfo->feature_dev)
+             return 0;
+
+     ret = platform_device_add(binfo->feature_dev);
+     if (!ret) {
+             struct cci_drvdata *drvdata;
+
+             drvdata = dev_get_drvdata(&binfo->pdev->dev);
+             if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
+                     cci_pci_add_port_dev(binfo->pdev, binfo->feature_dev);
+             else
+                     drvdata->fme_dev = get_device(&binfo->feature_dev->dev);
+
+             /*
+              * reset it to avoid build_info_free() freeing their resource.
+              *
+              * The resource of successfully registered feature devices
+              * will be freed by platform_device_unregister(). See the
+              * comments in build_info_create_dev().
+              */
+             binfo->feature_dev = NULL;
+     }
+
+     return ret;
+}
+
+static int
+build_info_create_dev(struct build_feature_devs_info *binfo,
+                   enum fpga_id_type type, int feature_nr, const char *name)
+{
+     struct platform_device *fdev;
+     struct resource *res;
+     struct feature_platform_data *pdata;
+     int ret;
+
+     /* we will create a new device, commit current device first */
+     ret = build_info_commit_dev(binfo);
+     if (ret)
+             return ret;
+
+     /*
+      * we use -ENODEV as the initialization indicator which indicates
+      * whether the id need to be reclaimed
+      */
+     fdev = binfo->feature_dev = platform_device_alloc(name, -ENODEV);
+     if (!fdev)
+             return -ENOMEM;
+
+     fdev->id = alloc_fpga_id(type, &fdev->dev);
+     if (fdev->id < 0)
+             return fdev->id;
+
+     fdev->dev.parent = &binfo->parent_dev->dev;
+
+     /*
+      * we do not need to care for the memory which is associated with
+      * the platform device. After calling platform_device_unregister(),
+      * it will be automatically freed by device's release() callback,
+      * platform_device_release().
+      */
+     pdata = feature_platform_data_alloc_and_init(fdev, feature_nr);
+     if (!pdata)
+             return -ENOMEM;
+
+     /*
+      * the count should be initialized to 0 to make sure
+      *__fpga_port_enable() following __fpga_port_disable()
+      * works properly for port device.
+      * and it should always be 0 for fme device.
+      */
+     WARN_ON(pdata->disable_count);
+
+     fdev->dev.platform_data = pdata;
+     fdev->num_resources = feature_nr;
+     fdev->resource = kcalloc(feature_nr, sizeof(*res), GFP_KERNEL);
+     if (!fdev->resource)
+             return -ENOMEM;
+
+     return 0;
+}
+
+static int remove_feature_dev(struct device *dev, void *data)
+{
+     struct platform_device *pdev = to_platform_device(dev);
+
+     platform_device_unregister(pdev);
+     return 0;
+}
+
+static int remove_parent_dev(struct device *dev, void *data)
+{
+     /* remove platform devices attached in the parent device */
+     device_for_each_child(dev, NULL, remove_feature_dev);
+     fpga_dev_destroy(to_fpga_dev(dev));
+     return 0;
+}
+
+static void remove_all_devs(struct pci_dev *pdev)
+{
+     /* remove parent device and all its children. */
+     device_for_each_child(&pdev->dev, NULL, remove_parent_dev);
+}
+
+static void build_info_free(struct build_feature_devs_info *binfo)
+{
+     if (!IS_ERR_OR_NULL(binfo->parent_dev))
+             remove_all_devs(binfo->pdev);
+
+     /*
+      * it is a valid id, free it. See comments in
+      * build_info_create_dev()
+      */
+     if (binfo->feature_dev && binfo->feature_dev->id >= 0)
+             free_fpga_id(feature_dev_id_type(binfo->feature_dev),
+                          binfo->feature_dev->id);
+
+     platform_device_put(binfo->feature_dev);
+
+     devm_kfree(&binfo->pdev->dev, binfo);
+}
+
+#define FEATURE_TYPE_AFU     0x1
+#define FEATURE_TYPE_PRIVATE 0x3
+
+/* FME and PORT GUID are fixed */
+#define FEATURE_FME_GUID "f9e17764-38f0-82fe-e346-524ae92aafbf"
+#define FEATURE_PORT_GUID "6b355b87-b06c-9642-eb42-8d139398b43a"
+
+static bool feature_is_fme(struct feature_afu_header *afu_hdr)
+{
+     uuid_le u;
+
+     uuid_le_to_bin(FEATURE_FME_GUID, &u);
+
+     return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+static bool feature_is_port(struct feature_afu_header *afu_hdr)
+{
+     uuid_le u;
+
+     uuid_le_to_bin(FEATURE_PORT_GUID, &u);
+
+     return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+/*
+ * UAFU GUID is dynamic as it can be changed after FME downloads different
+ * Green Bitstream to the port, so we treat the unknown GUIDs which are
+ * attached on port's feature list as UAFU.
+ */
+static bool feature_is_UAFU(struct build_feature_devs_info *binfo)
+{
+     if (!binfo->feature_dev ||
+           feature_dev_id_type(binfo->feature_dev) != PORT_ID)
+             return false;
+
+     return true;
+}
+
+static void
+build_info_add_sub_feature(struct build_feature_devs_info *binfo,
+                        int feature_id, const char *feature_name,
+                        resource_size_t resource_size, void __iomem *start)
+{
+
+     struct platform_device *fdev = binfo->feature_dev;
+     struct feature_platform_data *pdata = dev_get_platdata(&fdev->dev);
+     struct resource *res = &fdev->resource[feature_id];
+
+     res->start = pci_resource_start(binfo->pdev, binfo->current_bar) +
+             start - binfo->ioaddr;
+     res->end = res->start + resource_size - 1;
+     res->flags = IORESOURCE_MEM;
+     res->name = feature_name;
+
+     feature_platform_data_add(pdata, feature_id,
+                               feature_name, feature_id, start);
+}
+
+struct feature_info {
+     const char *name;
+     resource_size_t resource_size;
+     int feature_index;
+};
+
+/* indexed by fme feature IDs which are defined in 'enum fme_feature_id'. */
+static struct feature_info fme_features[] = {
+     {
+             .name = FME_FEATURE_HEADER,
+             .resource_size = sizeof(struct feature_fme_header),
+             .feature_index = FME_FEATURE_ID_HEADER,
+     },
+     {
+             .name = FME_FEATURE_THERMAL_MGMT,
+             .resource_size = sizeof(struct feature_fme_thermal),
+             .feature_index = FME_FEATURE_ID_THERMAL_MGMT,
+     },
+     {
+             .name = FME_FEATURE_POWER_MGMT,
+             .resource_size = sizeof(struct feature_fme_power),
+             .feature_index = FME_FEATURE_ID_POWER_MGMT,
+     },
+     {
+             .name = FME_FEATURE_GLOBAL_PERF,
+             .resource_size = sizeof(struct feature_fme_gperf),
+             .feature_index = FME_FEATURE_ID_GLOBAL_PERF,
+     },
+     {
+             .name = FME_FEATURE_GLOBAL_ERR,
+             .resource_size = sizeof(struct feature_fme_err),
+             .feature_index = FME_FEATURE_ID_GLOBAL_ERR,
+     },
+     {
+             .name = FME_FEATURE_PR_MGMT,
+             .resource_size = sizeof(struct feature_fme_pr),
+             .feature_index = FME_FEATURE_ID_PR_MGMT,
+     }
+};
+
+/* indexed by port feature IDs which are defined in 'enum port_feature_id'. */
+static struct feature_info port_features[] = {
+     {
+             .name = PORT_FEATURE_HEADER,
+             .resource_size = sizeof(struct feature_port_header),
+             .feature_index = PORT_FEATURE_ID_HEADER,
+     },
+     {
+             .name = PORT_FEATURE_ERR,
+             .resource_size = sizeof(struct feature_port_error),
+             .feature_index = PORT_FEATURE_ID_ERROR,
+     },
+     {
+             .name = PORT_FEATURE_UMSG,
+             .resource_size = sizeof(struct feature_port_umsg),
+             .feature_index = PORT_FEATURE_ID_UMSG,
+     },
+     {
+             /* This feature isn't available for now */
+             .name = PORT_FEATURE_PR,
+             .resource_size = 0,
+             .feature_index = PORT_FEATURE_ID_PR,
+     },
+     {
+             .name = PORT_FEATURE_STP,
+             .resource_size = sizeof(struct feature_port_stp),
+             .feature_index = PORT_FEATURE_ID_STP,
+     },
+     {
+             /*
+              * For User AFU feature, its region size is not fixed, but
+              * reported by register PortCapability.mmio_size. Resource
+              * size of UAFU will be set while parse port device.
+              */
+             .name = PORT_FEATURE_UAFU,
+             .resource_size = 0,
+             .feature_index = PORT_FEATURE_ID_UAFU,
+     },
+};
+
+static int
+create_feature_instance(struct build_feature_devs_info *binfo,
+                     void __iomem *start, struct feature_info *finfo)
+{
+     if (binfo->ioend - start < finfo->resource_size)
+             return -EINVAL;
+
+     build_info_add_sub_feature(binfo, finfo->feature_index, finfo->name,
+                                finfo->resource_size, start);
+     return 0;
+}
+
+static int parse_feature_fme(struct build_feature_devs_info *binfo,
+                          void __iomem *start)
+{
+     struct cci_drvdata *drvdata = dev_get_drvdata(&binfo->pdev->dev);
+     int ret;
+
+     ret = build_info_create_dev(binfo, FME_ID, fme_feature_num(),
+                                     FPGA_FEATURE_DEV_FME);
+     if (ret)
+             return ret;
+
+     if (drvdata->fme_dev) {
+             dev_err(&binfo->pdev->dev, "Multiple FMEs are detected.\n");
+             return -EINVAL;
+     }
+
+     return create_feature_instance(binfo, start,
+                                    &fme_features[FME_FEATURE_ID_HEADER]);
+}
+
+static int parse_feature_fme_private(struct build_feature_devs_info *binfo,
+                                  struct feature_header *hdr)
+{
+     struct feature_header header;
+
+     header.csr = readq(hdr);
+
+     if (header.id >= ARRAY_SIZE(fme_features)) {
+             dev_info(&binfo->pdev->dev, "FME feature id %x is not supported yet.\n",
+                      header.id);
+             return 0;
+     }
+
+     return create_feature_instance(binfo, hdr, &fme_features[header.id]);
+}
+
+static int parse_feature_port(struct build_feature_devs_info *binfo,
+                          void __iomem *start)
+{
+     int ret;
+
+     ret = build_info_create_dev(binfo, PORT_ID, port_feature_num(),
+                                     FPGA_FEATURE_DEV_PORT);
+     if (ret)
+             return ret;
+
+     return create_feature_instance(binfo, start,
+                                    &port_features[PORT_FEATURE_ID_HEADER]);
+}
+
+static void enable_port_uafu(struct build_feature_devs_info *binfo,
+                          void __iomem *start)
+{
+     enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+     struct feature_port_header *port_hdr;
+     struct feature_port_capability capability;
+
+     port_hdr = (struct feature_port_header *)start;
+     capability.csr = readq(&port_hdr->capability);
+     port_features[id].resource_size = capability.mmio_size << 10;
+
+     /*
+      * To enable User AFU, driver needs to clear reset bit on related port,
+      * otherwise the mmio space of this user AFU will be invalid.
+      */
+     if (port_features[id].resource_size)
+             fpga_port_reset(binfo->feature_dev);
+}
+
+static int parse_feature_port_private(struct build_feature_devs_info *binfo,
+                                   struct feature_header *hdr)
+{
+     struct feature_header header;
+     enum port_feature_id id;
+
+     header.csr = readq(hdr);
+     /*
+      * the region of port feature id is [0x10, 0x13], + 1 to reserve 0
+      * which is dedicated for port-hdr.
+      */
+     id = (header.id & 0x000f) + 1;
+
+     if (id >= ARRAY_SIZE(port_features)) {
+             dev_info(&binfo->pdev->dev, "Port feature id %x is not supported yet.\n",
+                      header.id);
+             return 0;
+     }
+
+     return create_feature_instance(binfo, hdr, &port_features[id]);
+}
+
+static int parse_feature_port_uafu(struct build_feature_devs_info *binfo,
+                              struct feature_header *hdr)
+{
+     enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+     int ret;
+
+     if (port_features[id].resource_size) {
+             ret = create_feature_instance(binfo, hdr, &port_features[id]);
+             port_features[id].resource_size = 0;
+     } else {
+             dev_err(&binfo->pdev->dev, "the uafu feature header is mis-configured.\n");
+             ret = -EINVAL;
+     }
+
+     return ret;
+}
+
+static int parse_feature_afus(struct build_feature_devs_info *binfo,
+                           struct feature_header *hdr)
+{
+     int ret;
+     struct feature_afu_header *afu_hdr, header;
+     void __iomem *start;
+     void __iomem *end = binfo->ioend;
+
+     start = hdr;
+     for (; start < end; start += header.next_afu) {
+             if (end - start < (sizeof(*afu_hdr) + sizeof(*hdr)))
+                     return -EINVAL;
+
+             hdr = start;
+             afu_hdr = (struct feature_afu_header *) (hdr + 1);
+             header.csr = readq(&afu_hdr->csr);
+
+             if (feature_is_fme(afu_hdr)) {
+                     ret = parse_feature_fme(binfo, hdr);
+                     binfo->pfme_hdr = hdr;
+                     if (ret)
+                             return ret;
+             } else if (feature_is_port(afu_hdr)) {
+                     ret = parse_feature_port(binfo, hdr);
+                     enable_port_uafu(binfo, hdr);
+                     if (ret)
+                             return ret;
+             } else if (feature_is_UAFU(binfo)) {
+                     ret = parse_feature_port_uafu(binfo, hdr);
+                     if (ret)
+                             return ret;
+             } else
+                     dev_info(&binfo->pdev->dev, "AFU GUID %pUl is not supported yet.\n",
+                              afu_hdr->guid.b);
+
+             if (!header.next_afu)
+                     break;
+     }
+
+     return 0;
+}
+
+static int parse_feature_private(struct build_feature_devs_info *binfo,
+                              struct feature_header *hdr)
+{
+     struct feature_header header;
+
+     header.csr = readq(hdr);
+
+     if (!binfo->feature_dev) {
+             dev_err(&binfo->pdev->dev, "the private feature %x does not belong to any AFU.\n",
+                     header.id);
+             return -EINVAL;
+     }
+
+     switch (feature_dev_id_type(binfo->feature_dev)) {
+     case FME_ID:
+             return parse_feature_fme_private(binfo, hdr);
+     case PORT_ID:
+             return parse_feature_port_private(binfo, hdr);
+     default:
+             dev_info(&binfo->pdev->dev, "private feature %x belonging to AFU %s is not supported yet.\n",
+                      header.id, binfo->feature_dev->name);
+     }
+     return 0;
+}
+
+static int parse_feature(struct build_feature_devs_info *binfo,
+                      struct feature_header *hdr)
+{
+     struct feature_header header;
+     int ret = 0;
+
+     header.csr = readq(hdr);
+
+     switch (header.type) {
+     case FEATURE_TYPE_AFU:
+             ret = parse_feature_afus(binfo, hdr);
+             break;
+     case FEATURE_TYPE_PRIVATE:
+             ret = parse_feature_private(binfo, hdr);
+             break;
+     default:
+             dev_info(&binfo->pdev->dev,
+                      "Feature Type %x is not supported.\n", hdr->type);
+     };
+
+     return ret;
+}
+
+static int
+parse_feature_list(struct build_feature_devs_info *binfo, void __iomem *start)
+{
+     struct feature_header *hdr, header;
+     void __iomem *end = binfo->ioend;
+     int ret = 0;
+
+     for (; start < end; start += header.next_header_offset) {
+             if (end - start < sizeof(*hdr)) {
+                     dev_err(&binfo->pdev->dev, "The region is too small to contain a feature.\n");
+                     ret =  -EINVAL;
+                     break;
+             }
+
+             hdr = (struct feature_header *)start;
+             ret = parse_feature(binfo, hdr);
+             if (ret)
+                     break;
+
+             header.csr = readq(hdr);
+             if (!header.next_header_offset)
+                     break;
+     }
+
+     return ret;
+}
+
+static int parse_ports_from_fme(struct build_feature_devs_info *binfo)
+{
+     struct feature_fme_header *fme_hdr;
+     struct feature_fme_port port;
+     int i = 0, ret = 0;
+
+     if (binfo->pfme_hdr == NULL) {
+             dev_dbg(&binfo->pdev->dev, "VF is detected.\n");
+             return ret;
+     }
+
+     fme_hdr = binfo->pfme_hdr;
+
+     do {
+             port.csr = readq(&fme_hdr->port[i]);
+             if (!port.port_implemented)
+                     break;
+
+             ret = parse_switch_to(binfo, port.port_bar);
+             if (ret)
+                     break;
+
+             ret = parse_feature_list(binfo,
+                             binfo->ioaddr + port.port_offset);
+             if (ret)
+                     break;
+     } while (++i < MAX_FPGA_PORT_NUM);
+
+     return ret;
+}
+
+static int create_init_drvdata(struct pci_dev *pdev)
+{
+     struct cci_drvdata *drvdata;
+
+     drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
+     if (!drvdata)
+             return -ENOMEM;
+
+     mutex_init(&drvdata->lock);
+     INIT_LIST_HEAD(&drvdata->port_dev_list);
+     INIT_LIST_HEAD(&drvdata->regions);
+
+     dev_set_drvdata(&pdev->dev, drvdata);
+     return 0;
+}
+
+static void destroy_drvdata(struct pci_dev *pdev)
+{
+     struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+
+     if (drvdata->fme_dev) {
+             /* fme device should be unregistered first. */
+             WARN_ON(device_is_registered(drvdata->fme_dev));
+             free_fpga_id(FME_ID, to_platform_device(drvdata->fme_dev)->id);
+             put_device(drvdata->fme_dev);
+     }
+
+     cci_pci_remove_port_devs(pdev);
+     cci_pci_release_regions(pdev);
+     dev_set_drvdata(&pdev->dev, NULL);
+     devm_kfree(&pdev->dev, drvdata);
+}
+
+static int cci_pci_create_feature_devs(struct pci_dev *pdev)
+{
+     struct build_feature_devs_info *binfo;
+     int ret;
+
+     binfo = build_info_alloc_and_init(pdev);
+     if (!binfo)
+             return -ENOMEM;
+
+     binfo->parent_dev = fpga_dev_create(&pdev->dev, INTEL_FPGA_DEV);
+     if (IS_ERR(binfo->parent_dev)) {
+             ret = PTR_ERR(binfo->parent_dev);
+             goto free_binfo_exit;
+     }
+
+     ret = parse_start(binfo);
+     if (ret)
+             goto free_binfo_exit;
+
+     ret = parse_feature_list(binfo, binfo->ioaddr);
+     if (ret)
+             goto free_binfo_exit;
+
+     ret = parse_ports_from_fme(binfo);
+     if (ret)
+             goto free_binfo_exit;
+
+     ret = build_info_commit_dev(binfo);
+     if (ret)
+             goto free_binfo_exit;
+
+     /*
+      * everything is okay, reset ->parent_dev to stop it being
+      * freed by build_info_free()
+      */
+     binfo->parent_dev = NULL;
+
+free_binfo_exit:
+     build_info_free(binfo);
+     return ret;
+}
+
 /* PCI Device ID */
 #define PCIe_DEVICE_ID_PF_INT_5_X    0xBCBD
 #define PCIe_DEVICE_ID_PF_INT_6_X    0xBCC0
@@ -81,9 +898,18 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
              goto release_region_exit;
      }
 
-     /* TODO: create and add the platform device per feature list */
+     ret = create_init_drvdata(pcidev);
+     if (ret)
+             goto release_region_exit;
+
+     ret = cci_pci_create_feature_devs(pcidev);
+     if (ret)
+             goto destroy_drvdata_exit;
+
      return 0;
 
+destroy_drvdata_exit:
+     destroy_drvdata(pcidev);
 release_region_exit:
      pci_release_regions(pcidev);
 disable_error_report_exit:
@@ -94,6 +920,8 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
 
 static void cci_pci_remove(struct pci_dev *pcidev)
 {
+     remove_all_devs(pcidev);
+     destroy_drvdata(pcidev);
      pci_release_regions(pcidev);
      pci_disable_pcie_error_reporting(pcidev);
      pci_disable_device(pcidev);
@@ -108,14 +936,23 @@ static void cci_pci_remove(struct pci_dev *pcidev)
 
 static int __init ccidrv_init(void)
 {
+     int ret;
+
      pr_info("Intel(R) FPGA PCIe Driver: Version %s\n", DRV_VERSION);
 
-     return pci_register_driver(&cci_pci_driver);
+     fpga_ids_init();
+
+     ret = pci_register_driver(&cci_pci_driver);
+     if (ret)
+             fpga_ids_destroy();
+
+     return ret;
 }
 
 static void __exit ccidrv_exit(void)
 {
      pci_unregister_driver(&cci_pci_driver);
+     fpga_ids_destroy();
 }
 
 module_init(ccidrv_init);
--
Quoted 2 lines

1.8.3.1

Thanks,

Moritz

[1] https://www.kernel.org/doc/html/v4.11/doc-guide/kernel-doc.html#writing-kernel-doc-comments

#39 lore raw

Re: [PATCH v2 07/22] fpga: intel: pcie: parse feature list and create platform device for features.

In reply to Wu Hao
Quoted 252 lines · Wu Hao

From: Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>

Device Feature List structure creates a link list of feature headers within the MMIO space to provide an extensible way of adding features.

The Intel FPGA PCIe driver walks through the feature headers to enumerate feature devices, FPGA Management Engine (FME) and FPGA Port for Accelerated Function Unit (AFU), and their private sub features. For feature devices, it creates the platform devices and linked the private sub features into their platform data.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.
fixed kbuild warning, typos and clean up the code.

---
 drivers/fpga/Makefile            |   2 +-
 drivers/fpga/intel-feature-dev.c | 130 ++++++
 drivers/fpga/intel-feature-dev.h | 341 ++++++++++++++++
 drivers/fpga/intel-pcie.c        | 841 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 1311 insertions(+), 3 deletions(-)
 create mode 100644 drivers/fpga/intel-feature-dev.c
 create mode 100644 drivers/fpga/intel-feature-dev.h
drivers/fpga/Makefile +1 -1
index 5613133..ad24b3d 100644
@@ -31,4 +31,4 @@ obj-$(CONFIG_OF_FPGA_REGION)          += of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)           += intel-fpga-pci.o
 
-intel-fpga-pci-objs := intel-pcie.o
+intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
drivers/fpga/intel-feature-dev.c new file +130 -0
new file mode 100644
index 0000000..68f9cba
@@ -0,0 +1,130 @@
+/*
+ * Intel FPGA Feature Device Driver
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "intel-feature-dev.h"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                              int index, const char *name,
+                              int resource_index, void __iomem *ioaddr)
+{
+       WARN_ON(index >= pdata->num);
+
+       pdata->features[index].name = name;
+       pdata->features[index].resource_index = resource_index;
+       pdata->features[index].ioaddr = ioaddr;
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num)
+{
+       struct feature_platform_data *pdata;
+
+       pdata = kzalloc(feature_platform_data_size(num), GFP_KERNEL);
+       if (pdata) {
+               pdata->dev = dev;
+               pdata->num = num;
+               mutex_init(&pdata->lock);
+       }
+
+       return pdata;
+}
+
+int fme_feature_num(void)
+{
+       return FME_FEATURE_ID_MAX;
+}
+
+int port_feature_num(void)
+{
+       return PORT_FEATURE_ID_MAX;
+}
+
+int fpga_port_id(struct platform_device *pdev)
+{
+       struct feature_port_header *port_hdr;
+       struct feature_port_capability capability;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       capability.csr = readq(&port_hdr->capability);
+       return capability.port_number;
+}
+EXPORT_SYMBOL_GPL(fpga_port_id);
+
+/*
+ * Enable Port by clear the port soft reset bit, which is set by default.
+ * The User AFU is unable to respond to any MMIO access while in reset.
+ * __fpga_port_enable function should only be used after __fpga_port_disable
+ * function.
+ */
+void __fpga_port_enable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       WARN_ON(!pdata->disable_count);
+
+       if (--pdata->disable_count != 0)
+               return;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x0;
+       writeq(control.csr, &port_hdr->control);
+}
+EXPORT_SYMBOL_GPL(__fpga_port_enable);
+
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
+int __fpga_port_disable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       if (pdata->disable_count++ != 0)
+               return 0;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       /* Set port soft reset */
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x1;
+       writeq(control.csr, &port_hdr->control);
+
+       /*
+        * HW sets ack bit to 1 when all outstanding requests have been drained
+        * on this port and minimum soft reset pulse width has elapsed.
+        * Driver polls port_soft_reset_ack to determine if reset done by HW.
+        */
+       if (readq_poll_timeout(&port_hdr->control, control.csr,
+                              (control.port_sftrst_ack == 1),
+                              RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+               dev_err(&pdev->dev, "timeout, fail to reset device\n");
+               return -ETIMEDOUT;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(__fpga_port_disable);
drivers/fpga/intel-feature-dev.h new file +67 -0
new file mode 100644
index 0000000..f67784a
@@ -0,0 +1,341 @@
+/*
+ * Intel FPGA Feature Device Driver Header File
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef __INTEL_FPGA_FEATURE_H
+#define __INTEL_FPGA_FEATURE_H
+
+#include <linux/fs.h>
+#include <linux/pci.h>
+#include <linux/uuid.h>
+#include <linux/delay.h>
+#include <linux/iopoll.h>
+#include <linux/platform_device.h>
+
+#ifndef readq
+static inline u64 readq(void __iomem *addr)
+{
+       return readl(addr) + ((u64)readl(addr + 4) << 32);
+}
+#endif
+
+#ifndef writeq
+static inline void writeq(u64 val, void __iomem *addr)
+{
+       writel((u32) (val), addr);
+       writel((u32) (val >> 32), (addr + 4));
+}
+#endif
+
+/* maximum supported number of ports */
+#define MAX_FPGA_PORT_NUM 4
+/* plus one for fme device */
+#define MAX_FEATURE_DEV_NUM    (MAX_FPGA_PORT_NUM + 1)
+
+#define FME_FEATURE_HEADER          "fme_hdr"
+#define FME_FEATURE_THERMAL_MGMT    "fme_thermal"
+#define FME_FEATURE_POWER_MGMT      "fme_power"
+#define FME_FEATURE_GLOBAL_PERF     "fme_gperf"
+#define FME_FEATURE_GLOBAL_ERR      "fme_error"
+#define FME_FEATURE_PR_MGMT         "fme_pr"
+
+#define PORT_FEATURE_HEADER         "port_hdr"
+#define PORT_FEATURE_UAFU           "port_uafu"
+#define PORT_FEATURE_ERR            "port_err"
+#define PORT_FEATURE_UMSG           "port_umsg"
+#define PORT_FEATURE_PR             "port_pr"
+#define PORT_FEATURE_STP            "port_stp"
+
+/* All headers and structures must be byte-packed to match the spec. */
+#pragma pack(1)
+
+/* common header for all features */
+struct feature_header {
+       union {
+               u64 csr;
+               struct {

drivers/fpga/intel-feature-dev.h line 77

Quoted 10 lines
drivers/fpga/intel-feature-dev.h new file +10 -0
+                       u64 id:12;
+                       u64 revision:4;
+                       u64 next_header_offset:24; /* offset to next header */
+                       u64 rsvdz:20;
+                       u64 type:4;                /* feature type */
+#define FEATURE_TYPE_AFU               0x1
+#define FEATURE_TYPE_PRIVATE           0x3
+               };
+       };
+};

Hi Hao,

Aren't these union's introducing a portability problem? Can you use regmap instead?

Alan

Quoted 1,153 lines
drivers/fpga/intel-feature-dev.h new file +264 -0
+
+/* common header for non-private features */
+struct feature_afu_header {
+       uuid_le guid;
+       union {
+               u64 csr;
+               struct {
+                       u64 next_afu:24;        /* pointer to next afu header */
+                       u64 rsvdz:40;
+               };
+       };
+};
+
+/* FME Header Register Set */
+/* FME Capability Register */
+struct feature_fme_capability {
+       union {
+               u64 csr;
+               struct {
+                       u64 fabric_verid:8;     /* Fabric version ID */
+                       u64 socket_id:1;        /* Socket id */
+                       u64 rsvdz1:3;
+                       u64 pcie0_link_avl:1;   /* PCIe0 link availability */
+                       u64 pcie1_link_avl:1;   /* PCIe1 link availability */
+                       u64 coherent_link_avl:1;/* Coherent link availability */
+                       u64 rsvdz2:1;
+                       u64 iommu_support:1;    /* IOMMU or VT-d supported */
+                       u64 num_ports:3;        /* Num of ports implemented */
+                       u64 rsvdz3:4;
+                       u64 addr_width_bits:6;  /* Address width supported */
+                       u64 rsvdz4:2;
+                       u64 cache_size:12;      /* Cache size in kb */
+                       u64 cache_assoc:4;      /* Cache Associativity */
+                       u64 rsvdz5:15;
+                       u64 lock_bit:1;         /* Latched lock bit by BIOS */
+               };
+       };
+};
+
+/* FME Port Offset Register */
+struct feature_fme_port {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_offset:24;     /* Offset to port header */
+                       u64 rsvdz1:8;
+                       u64 port_bar:3;         /* Bar id */
+                       u64 rsvdz2:20;
+                       u64 afu_access_ctrl:1;  /* AFU access type: PF/VF */
+                       u64 rsvdz3:4;
+                       u64 port_implemented:1; /* Port implemented or not */
+                       u64 rsvdz4:3;
+               };
+       };
+};
+
+struct feature_fme_header {
+       struct feature_header header;
+       struct feature_afu_header afu_header;
+       u64 rsvd[2];
+       struct feature_fme_capability capability;
+       struct feature_fme_port port[MAX_FPGA_PORT_NUM];
+};
+
+/* FME Thermal Sub Feature Register Set */
+struct feature_fme_thermal {
+       struct feature_header header;
+};
+
+/* FME Power Sub Feature Register Set */
+struct feature_fme_power {
+       struct feature_header header;
+};
+
+/* FME Global Performance Sub Feature Register Set */
+struct feature_fme_gperf {
+       struct feature_header header;
+};
+
+/* FME Error Sub Feature Register Set */
+struct feature_fme_err {
+       struct feature_header header;
+};
+
+/* FME Partial Reconfiguration Sub Feature Register Set */
+struct feature_fme_pr {
+       struct feature_header header;
+};
+
+/* PORT Header Register Set */
+/* Port Capability Register */
+struct feature_port_capability {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_number:2;      /* Port Number 0-3 */
+                       u64 rsvdz1:6;
+                       u64 mmio_size:16;       /* User MMIO size in KB */
+                       u64 rsvdz2:8;
+                       u64 sp_intr_num:4;      /* Supported interrupts num */
+                       u64 rsvdz3:28;
+               };
+       };
+};
+
+/* Port Control Register */
+struct feature_port_control {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_sftrst:1;      /* Port Soft Reset */
+                       u64 rsvdz1:1;
+                       u64 latency_tolerance:1;/* '1' >= 40us, '0' < 40us */
+                       u64 rsvdz2:1;
+                       u64 port_sftrst_ack:1;  /* HW ACK for Soft Reset */
+                       u64 rsvdz3:59;
+               };
+       };
+};
+
+struct feature_port_header {
+       struct feature_header header;
+       struct feature_afu_header afu_header;
+       u64 rsvd[2];
+       struct feature_port_capability capability;
+       struct feature_port_control control;
+};
+
+/* PORT Error Sub Feature Register Set */
+struct feature_port_error {
+       struct feature_header header;
+};
+
+/* PORT Unordered Message Sub Feature Register Set */
+struct feature_port_umsg {
+       struct feature_header header;
+};
+
+/* PORT SignalTap Sub Feature Register Set */
+struct feature_port_stp {
+       struct feature_header header;
+};
+
+#pragma pack()
+
+struct feature {
+       const char *name;
+       int resource_index;
+       void __iomem *ioaddr;
+};
+
+struct feature_platform_data {
+       /* list the feature dev to cci_drvdata->port_dev_list. */
+       struct list_head node;
+       struct mutex lock;
+       struct platform_device *dev;
+       unsigned int disable_count;     /* count for port disable */
+
+       int num;                        /* number of features */
+       struct feature features[0];
+};
+
+enum fme_feature_id {
+       FME_FEATURE_ID_HEADER = 0x0,
+       FME_FEATURE_ID_THERMAL_MGMT = 0x1,
+       FME_FEATURE_ID_POWER_MGMT = 0x2,
+       FME_FEATURE_ID_GLOBAL_PERF = 0x3,
+       FME_FEATURE_ID_GLOBAL_ERR = 0x4,
+       FME_FEATURE_ID_PR_MGMT = 0x5,
+       FME_FEATURE_ID_MAX = 0x6,
+};
+
+enum port_feature_id {
+       PORT_FEATURE_ID_HEADER = 0x0,
+       PORT_FEATURE_ID_ERROR = 0x1,
+       PORT_FEATURE_ID_UMSG = 0x2,
+       PORT_FEATURE_ID_PR = 0x3,
+       PORT_FEATURE_ID_STP = 0x4,
+       PORT_FEATURE_ID_UAFU = 0x5,
+       PORT_FEATURE_ID_MAX = 0x6,
+};
+
+int fme_feature_num(void);
+int port_feature_num(void);
+
+#define FPGA_FEATURE_DEV_FME           "intel-fpga-fme"
+#define FPGA_FEATURE_DEV_PORT          "intel-fpga-port"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                              int index, const char *name,
+                              int resource_index, void __iomem *ioaddr);
+
+static inline int feature_platform_data_size(const int num)
+{
+       return sizeof(struct feature_platform_data) +
+               num * sizeof(struct feature);
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num);
+
+int fpga_port_id(struct platform_device *pdev);
+
+static inline int fpga_port_check_id(struct platform_device *pdev,
+                                    void *pport_id)
+{
+       return fpga_port_id(pdev) == *(int *)pport_id;
+}
+
+void __fpga_port_enable(struct platform_device *pdev);
+int __fpga_port_disable(struct platform_device *pdev);
+
+static inline void fpga_port_enable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+       mutex_lock(&pdata->lock);
+       __fpga_port_enable(pdev);
+       mutex_unlock(&pdata->lock);
+}
+
+static inline int fpga_port_disable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       int ret;
+
+       mutex_lock(&pdata->lock);
+       ret = __fpga_port_disable(pdev);
+       mutex_unlock(&pdata->lock);
+
+       return ret;
+}
+
+static inline int __fpga_port_reset(struct platform_device *pdev)
+{
+       int ret;
+
+       ret = __fpga_port_disable(pdev);
+       if (ret)
+               return ret;
+
+       __fpga_port_enable(pdev);
+       return 0;
+}
+
+static inline int fpga_port_reset(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       int ret;
+
+       mutex_lock(&pdata->lock);
+       ret = __fpga_port_reset(pdev);
+       mutex_unlock(&pdata->lock);
+       return ret;
+}
+
+static inline void __iomem *
+get_feature_ioaddr_by_index(struct device *dev, int index)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(dev);
+
+       return pdata->features[index].ioaddr;
+}
+#endif
drivers/fpga/intel-pcie.c +839 -3
index f697de4..70b8284 100644
@@ -23,10 +23,827 @@
 #include <linux/stddef.h>
 #include <linux/errno.h>
 #include <linux/aer.h>
+#include <linux/fpga/fpga-dev.h>
+
+#include "intel-feature-dev.h"
 
 #define DRV_VERSION    "0.8"
 #define DRV_NAME       "intel-fpga-pci"
 
+#define INTEL_FPGA_DEV "intel-fpga-dev"
+
+static DEFINE_MUTEX(fpga_id_mutex);
+
+enum fpga_id_type {
+       FME_ID,         /* fme id allocation and mapping */
+       PORT_ID,        /* port id allocation and mapping */
+       FPGA_ID_MAX,
+};
+
+/* it is protected by fpga_id_mutex */
+static struct idr fpga_ids[FPGA_ID_MAX];
+
+struct cci_drvdata {
+       struct device *fme_dev;
+
+       struct mutex lock;
+       struct list_head port_dev_list;
+
+       struct list_head regions; /* global list of pci bar mapping region */
+};
+
+/* pci bar mapping info */
+struct cci_pci_region {
+       int bar;
+       void __iomem *ioaddr;   /* pointer to mapped bar region */
+       struct list_head node;
+};
+
+static void fpga_ids_init(void)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+               idr_init(fpga_ids + i);
+}
+
+static void fpga_ids_destroy(void)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+               idr_destroy(fpga_ids + i);
+}
+
+static int alloc_fpga_id(enum fpga_id_type type, struct device *dev)
+{
+       int id;
+
+       WARN_ON(type >= FPGA_ID_MAX);
+       mutex_lock(&fpga_id_mutex);
+       id = idr_alloc(fpga_ids + type, dev, 0, 0, GFP_KERNEL);
+       mutex_unlock(&fpga_id_mutex);
+       return id;
+}
+
+static void free_fpga_id(enum fpga_id_type type, int id)
+{
+       WARN_ON(type >= FPGA_ID_MAX);
+       mutex_lock(&fpga_id_mutex);
+       idr_remove(fpga_ids + type, id);
+       mutex_unlock(&fpga_id_mutex);
+}
+
+static void cci_pci_add_port_dev(struct pci_dev *pdev,
+                                struct platform_device *port_dev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct feature_platform_data *pdata = dev_get_platdata(&port_dev->dev);
+
+       mutex_lock(&drvdata->lock);
+       list_add(&pdata->node, &drvdata->port_dev_list);
+       get_device(&pdata->dev->dev);
+       mutex_unlock(&drvdata->lock);
+}
+
+static void cci_pci_remove_port_devs(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct feature_platform_data *pdata, *ptmp;
+
+       mutex_lock(&drvdata->lock);
+       list_for_each_entry_safe(pdata, ptmp, &drvdata->port_dev_list, node) {
+               struct platform_device *port_dev = pdata->dev;
+
+               /* the port should be unregistered first. */
+               WARN_ON(device_is_registered(&port_dev->dev));
+               list_del(&pdata->node);
+               free_fpga_id(PORT_ID, port_dev->id);
+               put_device(&port_dev->dev);
+       }
+       mutex_unlock(&drvdata->lock);
+}
+
+/* info collection during feature dev build. */
+struct build_feature_devs_info {
+       struct pci_dev *pdev;
+
+       /*
+        * PCI BAR mapping info. Parsing feature list starts from
+        * BAR 0 and switch to different BARs to parse Port
+        */
+       void __iomem *ioaddr;
+       void __iomem *ioend;
+       int current_bar;
+
+       /* points to FME header where the port offset is figured out. */
+       void __iomem *pfme_hdr;
+
+       /* the container device for all feature devices */
+       struct fpga_dev *parent_dev;
+
+       /* current feature device */
+       struct platform_device *feature_dev;
+};
+
+static void cci_pci_release_regions(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct cci_pci_region *tmp, *region;
+
+       list_for_each_entry_safe(region, tmp, &drvdata->regions, node) {
+               list_del(&region->node);
+               if (region->ioaddr)
+                       pci_iounmap(pdev, region->ioaddr);
+               devm_kfree(&pdev->dev, region);
+       }
+}
+
+static void __iomem *cci_pci_ioremap_bar(struct pci_dev *pdev, int bar)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct cci_pci_region *region;
+
+       list_for_each_entry(region, &drvdata->regions, node)
+               if (region->bar == bar) {
+                       dev_dbg(&pdev->dev, "BAR %d region exists\n", bar);
+                       return region->ioaddr;
+               }
+
+       region = devm_kzalloc(&pdev->dev, sizeof(*region), GFP_KERNEL);
+       if (!region)
+               return NULL;
+
+       region->bar = bar;
+       region->ioaddr = pci_ioremap_bar(pdev, bar);
+       if (!region->ioaddr) {
+               dev_err(&pdev->dev, "can't ioremap memory from BAR %d.\n", bar);
+               devm_kfree(&pdev->dev, region);
+               return NULL;
+       }
+
+       list_add(&region->node, &drvdata->regions);
+       return region->ioaddr;
+}
+
+static int parse_start_from(struct build_feature_devs_info *binfo, int bar)
+{
+       binfo->ioaddr = cci_pci_ioremap_bar(binfo->pdev, bar);
+       if (!binfo->ioaddr)
+               return -ENOMEM;
+
+       binfo->current_bar = bar;
+       binfo->ioend = binfo->ioaddr + pci_resource_len(binfo->pdev, bar);
+       return 0;
+}
+
+static int parse_start(struct build_feature_devs_info *binfo)
+{
+       /* fpga feature list starts from BAR 0 */
+       return parse_start_from(binfo, 0);
+}
+
+/* switch the memory mapping to BAR# @bar */
+static int parse_switch_to(struct build_feature_devs_info *binfo, int bar)
+{
+       return parse_start_from(binfo, bar);
+}
+
+static struct build_feature_devs_info *
+build_info_alloc_and_init(struct pci_dev *pdev)
+{
+       struct build_feature_devs_info *binfo;
+
+       binfo = devm_kzalloc(&pdev->dev, sizeof(*binfo), GFP_KERNEL);
+       if (binfo)
+               binfo->pdev = pdev;
+
+       return binfo;
+}
+
+static enum fpga_id_type feature_dev_id_type(struct platform_device *pdev)
+{
+       if (!strcmp(pdev->name, FPGA_FEATURE_DEV_FME))
+               return FME_ID;
+
+       if (!strcmp(pdev->name, FPGA_FEATURE_DEV_PORT))
+               return PORT_ID;
+
+       WARN_ON(1);
+       return FPGA_ID_MAX;
+}
+
+/*
+ * register current feature device, it is called when we need to switch to
+ * another feature parsing or we have parsed all features
+ */
+static int build_info_commit_dev(struct build_feature_devs_info *binfo)
+{
+       int ret;
+
+       if (!binfo->feature_dev)
+               return 0;
+
+       ret = platform_device_add(binfo->feature_dev);
+       if (!ret) {
+               struct cci_drvdata *drvdata;
+
+               drvdata = dev_get_drvdata(&binfo->pdev->dev);
+               if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
+                       cci_pci_add_port_dev(binfo->pdev, binfo->feature_dev);
+               else
+                       drvdata->fme_dev = get_device(&binfo->feature_dev->dev);
+
+               /*
+                * reset it to avoid build_info_free() freeing their resource.
+                *
+                * The resource of successfully registered feature devices
+                * will be freed by platform_device_unregister(). See the
+                * comments in build_info_create_dev().
+                */
+               binfo->feature_dev = NULL;
+       }
+
+       return ret;
+}
+
+static int
+build_info_create_dev(struct build_feature_devs_info *binfo,
+                     enum fpga_id_type type, int feature_nr, const char *name)
+{
+       struct platform_device *fdev;
+       struct resource *res;
+       struct feature_platform_data *pdata;
+       int ret;
+
+       /* we will create a new device, commit current device first */
+       ret = build_info_commit_dev(binfo);
+       if (ret)
+               return ret;
+
+       /*
+        * we use -ENODEV as the initialization indicator which indicates
+        * whether the id need to be reclaimed
+        */
+       fdev = binfo->feature_dev = platform_device_alloc(name, -ENODEV);
+       if (!fdev)
+               return -ENOMEM;
+
+       fdev->id = alloc_fpga_id(type, &fdev->dev);
+       if (fdev->id < 0)
+               return fdev->id;
+
+       fdev->dev.parent = &binfo->parent_dev->dev;
+
+       /*
+        * we do not need to care for the memory which is associated with
+        * the platform device. After calling platform_device_unregister(),
+        * it will be automatically freed by device's release() callback,
+        * platform_device_release().
+        */
+       pdata = feature_platform_data_alloc_and_init(fdev, feature_nr);
+       if (!pdata)
+               return -ENOMEM;
+
+       /*
+        * the count should be initialized to 0 to make sure
+        *__fpga_port_enable() following __fpga_port_disable()
+        * works properly for port device.
+        * and it should always be 0 for fme device.
+        */
+       WARN_ON(pdata->disable_count);
+
+       fdev->dev.platform_data = pdata;
+       fdev->num_resources = feature_nr;
+       fdev->resource = kcalloc(feature_nr, sizeof(*res), GFP_KERNEL);
+       if (!fdev->resource)
+               return -ENOMEM;
+
+       return 0;
+}
+
+static int remove_feature_dev(struct device *dev, void *data)
+{
+       struct platform_device *pdev = to_platform_device(dev);
+
+       platform_device_unregister(pdev);
+       return 0;
+}
+
+static int remove_parent_dev(struct device *dev, void *data)
+{
+       /* remove platform devices attached in the parent device */
+       device_for_each_child(dev, NULL, remove_feature_dev);
+       fpga_dev_destroy(to_fpga_dev(dev));
+       return 0;
+}
+
+static void remove_all_devs(struct pci_dev *pdev)
+{
+       /* remove parent device and all its children. */
+       device_for_each_child(&pdev->dev, NULL, remove_parent_dev);
+}
+
+static void build_info_free(struct build_feature_devs_info *binfo)
+{
+       if (!IS_ERR_OR_NULL(binfo->parent_dev))
+               remove_all_devs(binfo->pdev);
+
+       /*
+        * it is a valid id, free it. See comments in
+        * build_info_create_dev()
+        */
+       if (binfo->feature_dev && binfo->feature_dev->id >= 0)
+               free_fpga_id(feature_dev_id_type(binfo->feature_dev),
+                            binfo->feature_dev->id);
+
+       platform_device_put(binfo->feature_dev);
+
+       devm_kfree(&binfo->pdev->dev, binfo);
+}
+
+#define FEATURE_TYPE_AFU       0x1
+#define FEATURE_TYPE_PRIVATE   0x3
+
+/* FME and PORT GUID are fixed */
+#define FEATURE_FME_GUID "f9e17764-38f0-82fe-e346-524ae92aafbf"
+#define FEATURE_PORT_GUID "6b355b87-b06c-9642-eb42-8d139398b43a"
+
+static bool feature_is_fme(struct feature_afu_header *afu_hdr)
+{
+       uuid_le u;
+
+       uuid_le_to_bin(FEATURE_FME_GUID, &u);
+
+       return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+static bool feature_is_port(struct feature_afu_header *afu_hdr)
+{
+       uuid_le u;
+
+       uuid_le_to_bin(FEATURE_PORT_GUID, &u);
+
+       return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+/*
+ * UAFU GUID is dynamic as it can be changed after FME downloads different
+ * Green Bitstream to the port, so we treat the unknown GUIDs which are
+ * attached on port's feature list as UAFU.
+ */
+static bool feature_is_UAFU(struct build_feature_devs_info *binfo)
+{
+       if (!binfo->feature_dev ||
+             feature_dev_id_type(binfo->feature_dev) != PORT_ID)
+               return false;
+
+       return true;
+}
+
+static void
+build_info_add_sub_feature(struct build_feature_devs_info *binfo,
+                          int feature_id, const char *feature_name,
+                          resource_size_t resource_size, void __iomem *start)
+{
+
+       struct platform_device *fdev = binfo->feature_dev;
+       struct feature_platform_data *pdata = dev_get_platdata(&fdev->dev);
+       struct resource *res = &fdev->resource[feature_id];
+
+       res->start = pci_resource_start(binfo->pdev, binfo->current_bar) +
+               start - binfo->ioaddr;
+       res->end = res->start + resource_size - 1;
+       res->flags = IORESOURCE_MEM;
+       res->name = feature_name;
+
+       feature_platform_data_add(pdata, feature_id,
+                                 feature_name, feature_id, start);
+}
+
+struct feature_info {
+       const char *name;
+       resource_size_t resource_size;
+       int feature_index;
+};
+
+/* indexed by fme feature IDs which are defined in 'enum fme_feature_id'. */
+static struct feature_info fme_features[] = {
+       {
+               .name = FME_FEATURE_HEADER,
+               .resource_size = sizeof(struct feature_fme_header),
+               .feature_index = FME_FEATURE_ID_HEADER,
+       },
+       {
+               .name = FME_FEATURE_THERMAL_MGMT,
+               .resource_size = sizeof(struct feature_fme_thermal),
+               .feature_index = FME_FEATURE_ID_THERMAL_MGMT,
+       },
+       {
+               .name = FME_FEATURE_POWER_MGMT,
+               .resource_size = sizeof(struct feature_fme_power),
+               .feature_index = FME_FEATURE_ID_POWER_MGMT,
+       },
+       {
+               .name = FME_FEATURE_GLOBAL_PERF,
+               .resource_size = sizeof(struct feature_fme_gperf),
+               .feature_index = FME_FEATURE_ID_GLOBAL_PERF,
+       },
+       {
+               .name = FME_FEATURE_GLOBAL_ERR,
+               .resource_size = sizeof(struct feature_fme_err),
+               .feature_index = FME_FEATURE_ID_GLOBAL_ERR,
+       },
+       {
+               .name = FME_FEATURE_PR_MGMT,
+               .resource_size = sizeof(struct feature_fme_pr),
+               .feature_index = FME_FEATURE_ID_PR_MGMT,
+       }
+};
+
+/* indexed by port feature IDs which are defined in 'enum port_feature_id'. */
+static struct feature_info port_features[] = {
+       {
+               .name = PORT_FEATURE_HEADER,
+               .resource_size = sizeof(struct feature_port_header),
+               .feature_index = PORT_FEATURE_ID_HEADER,
+       },
+       {
+               .name = PORT_FEATURE_ERR,
+               .resource_size = sizeof(struct feature_port_error),
+               .feature_index = PORT_FEATURE_ID_ERROR,
+       },
+       {
+               .name = PORT_FEATURE_UMSG,
+               .resource_size = sizeof(struct feature_port_umsg),
+               .feature_index = PORT_FEATURE_ID_UMSG,
+       },
+       {
+               /* This feature isn't available for now */
+               .name = PORT_FEATURE_PR,
+               .resource_size = 0,
+               .feature_index = PORT_FEATURE_ID_PR,
+       },
+       {
+               .name = PORT_FEATURE_STP,
+               .resource_size = sizeof(struct feature_port_stp),
+               .feature_index = PORT_FEATURE_ID_STP,
+       },
+       {
+               /*
+                * For User AFU feature, its region size is not fixed, but
+                * reported by register PortCapability.mmio_size. Resource
+                * size of UAFU will be set while parse port device.
+                */
+               .name = PORT_FEATURE_UAFU,
+               .resource_size = 0,
+               .feature_index = PORT_FEATURE_ID_UAFU,
+       },
+};
+
+static int
+create_feature_instance(struct build_feature_devs_info *binfo,
+                       void __iomem *start, struct feature_info *finfo)
+{
+       if (binfo->ioend - start < finfo->resource_size)
+               return -EINVAL;
+
+       build_info_add_sub_feature(binfo, finfo->feature_index, finfo->name,
+                                  finfo->resource_size, start);
+       return 0;
+}
+
+static int parse_feature_fme(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&binfo->pdev->dev);
+       int ret;
+
+       ret = build_info_create_dev(binfo, FME_ID, fme_feature_num(),
+                                       FPGA_FEATURE_DEV_FME);
+       if (ret)
+               return ret;
+
+       if (drvdata->fme_dev) {
+               dev_err(&binfo->pdev->dev, "Multiple FMEs are detected.\n");
+               return -EINVAL;
+       }
+
+       return create_feature_instance(binfo, start,
+                                      &fme_features[FME_FEATURE_ID_HEADER]);
+}
+
+static int parse_feature_fme_private(struct build_feature_devs_info *binfo,
+                                    struct feature_header *hdr)
+{
+       struct feature_header header;
+
+       header.csr = readq(hdr);
+
+       if (header.id >= ARRAY_SIZE(fme_features)) {
+               dev_info(&binfo->pdev->dev, "FME feature id %x is not supported yet.\n",
+                        header.id);
+               return 0;
+       }
+
+       return create_feature_instance(binfo, hdr, &fme_features[header.id]);
+}
+
+static int parse_feature_port(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       int ret;
+
+       ret = build_info_create_dev(binfo, PORT_ID, port_feature_num(),
+                                       FPGA_FEATURE_DEV_PORT);
+       if (ret)
+               return ret;
+
+       return create_feature_instance(binfo, start,
+                                      &port_features[PORT_FEATURE_ID_HEADER]);
+}
+
+static void enable_port_uafu(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+       struct feature_port_header *port_hdr;
+       struct feature_port_capability capability;
+
+       port_hdr = (struct feature_port_header *)start;
+       capability.csr = readq(&port_hdr->capability);
+       port_features[id].resource_size = capability.mmio_size << 10;
+
+       /*
+        * To enable User AFU, driver needs to clear reset bit on related port,
+        * otherwise the mmio space of this user AFU will be invalid.
+        */
+       if (port_features[id].resource_size)
+               fpga_port_reset(binfo->feature_dev);
+}
+
+static int parse_feature_port_private(struct build_feature_devs_info *binfo,
+                                     struct feature_header *hdr)
+{
+       struct feature_header header;
+       enum port_feature_id id;
+
+       header.csr = readq(hdr);
+       /*
+        * the region of port feature id is [0x10, 0x13], + 1 to reserve 0
+        * which is dedicated for port-hdr.
+        */
+       id = (header.id & 0x000f) + 1;
+
+       if (id >= ARRAY_SIZE(port_features)) {
+               dev_info(&binfo->pdev->dev, "Port feature id %x is not supported yet.\n",
+                        header.id);
+               return 0;
+       }
+
+       return create_feature_instance(binfo, hdr, &port_features[id]);
+}
+
+static int parse_feature_port_uafu(struct build_feature_devs_info *binfo,
+                                struct feature_header *hdr)
+{
+       enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+       int ret;
+
+       if (port_features[id].resource_size) {
+               ret = create_feature_instance(binfo, hdr, &port_features[id]);
+               port_features[id].resource_size = 0;
+       } else {
+               dev_err(&binfo->pdev->dev, "the uafu feature header is mis-configured.\n");
+               ret = -EINVAL;
+       }
+
+       return ret;
+}
+
+static int parse_feature_afus(struct build_feature_devs_info *binfo,
+                             struct feature_header *hdr)
+{
+       int ret;
+       struct feature_afu_header *afu_hdr, header;
+       void __iomem *start;
+       void __iomem *end = binfo->ioend;
+
+       start = hdr;
+       for (; start < end; start += header.next_afu) {
+               if (end - start < (sizeof(*afu_hdr) + sizeof(*hdr)))
+                       return -EINVAL;
+
+               hdr = start;
+               afu_hdr = (struct feature_afu_header *) (hdr + 1);
+               header.csr = readq(&afu_hdr->csr);
+
+               if (feature_is_fme(afu_hdr)) {
+                       ret = parse_feature_fme(binfo, hdr);
+                       binfo->pfme_hdr = hdr;
+                       if (ret)
+                               return ret;
+               } else if (feature_is_port(afu_hdr)) {
+                       ret = parse_feature_port(binfo, hdr);
+                       enable_port_uafu(binfo, hdr);
+                       if (ret)
+                               return ret;
+               } else if (feature_is_UAFU(binfo)) {
+                       ret = parse_feature_port_uafu(binfo, hdr);
+                       if (ret)
+                               return ret;
+               } else
+                       dev_info(&binfo->pdev->dev, "AFU GUID %pUl is not supported yet.\n",
+                                afu_hdr->guid.b);
+
+               if (!header.next_afu)
+                       break;
+       }
+
+       return 0;
+}
+
+static int parse_feature_private(struct build_feature_devs_info *binfo,
+                                struct feature_header *hdr)
+{
+       struct feature_header header;
+
+       header.csr = readq(hdr);
+
+       if (!binfo->feature_dev) {
+               dev_err(&binfo->pdev->dev, "the private feature %x does not belong to any AFU.\n",
+                       header.id);
+               return -EINVAL;
+       }
+
+       switch (feature_dev_id_type(binfo->feature_dev)) {
+       case FME_ID:
+               return parse_feature_fme_private(binfo, hdr);
+       case PORT_ID:
+               return parse_feature_port_private(binfo, hdr);
+       default:
+               dev_info(&binfo->pdev->dev, "private feature %x belonging to AFU %s is not supported yet.\n",
+                        header.id, binfo->feature_dev->name);
+       }
+       return 0;
+}
+
+static int parse_feature(struct build_feature_devs_info *binfo,
+                        struct feature_header *hdr)
+{
+       struct feature_header header;
+       int ret = 0;
+
+       header.csr = readq(hdr);
+
+       switch (header.type) {
+       case FEATURE_TYPE_AFU:
+               ret = parse_feature_afus(binfo, hdr);
+               break;
+       case FEATURE_TYPE_PRIVATE:
+               ret = parse_feature_private(binfo, hdr);
+               break;
+       default:
+               dev_info(&binfo->pdev->dev,
+                        "Feature Type %x is not supported.\n", hdr->type);
+       };
+
+       return ret;
+}
+
+static int
+parse_feature_list(struct build_feature_devs_info *binfo, void __iomem *start)
+{
+       struct feature_header *hdr, header;
+       void __iomem *end = binfo->ioend;
+       int ret = 0;
+
+       for (; start < end; start += header.next_header_offset) {
+               if (end - start < sizeof(*hdr)) {
+                       dev_err(&binfo->pdev->dev, "The region is too small to contain a feature.\n");
+                       ret =  -EINVAL;
+                       break;
+               }
+
+               hdr = (struct feature_header *)start;
+               ret = parse_feature(binfo, hdr);
+               if (ret)
+                       break;
+
+               header.csr = readq(hdr);
+               if (!header.next_header_offset)
+                       break;
+       }
+
+       return ret;
+}
+
+static int parse_ports_from_fme(struct build_feature_devs_info *binfo)
+{
+       struct feature_fme_header *fme_hdr;
+       struct feature_fme_port port;
+       int i = 0, ret = 0;
+
+       if (binfo->pfme_hdr == NULL) {
+               dev_dbg(&binfo->pdev->dev, "VF is detected.\n");
+               return ret;
+       }
+
+       fme_hdr = binfo->pfme_hdr;
+
+       do {
+               port.csr = readq(&fme_hdr->port[i]);
+               if (!port.port_implemented)
+                       break;
+
+               ret = parse_switch_to(binfo, port.port_bar);
+               if (ret)
+                       break;
+
+               ret = parse_feature_list(binfo,
+                               binfo->ioaddr + port.port_offset);
+               if (ret)
+                       break;
+       } while (++i < MAX_FPGA_PORT_NUM);
+
+       return ret;
+}
+
+static int create_init_drvdata(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata;
+
+       drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
+       if (!drvdata)
+               return -ENOMEM;
+
+       mutex_init(&drvdata->lock);
+       INIT_LIST_HEAD(&drvdata->port_dev_list);
+       INIT_LIST_HEAD(&drvdata->regions);
+
+       dev_set_drvdata(&pdev->dev, drvdata);
+       return 0;
+}
+
+static void destroy_drvdata(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+
+       if (drvdata->fme_dev) {
+               /* fme device should be unregistered first. */
+               WARN_ON(device_is_registered(drvdata->fme_dev));
+               free_fpga_id(FME_ID, to_platform_device(drvdata->fme_dev)->id);
+               put_device(drvdata->fme_dev);
+       }
+
+       cci_pci_remove_port_devs(pdev);
+       cci_pci_release_regions(pdev);
+       dev_set_drvdata(&pdev->dev, NULL);
+       devm_kfree(&pdev->dev, drvdata);
+}
+
+static int cci_pci_create_feature_devs(struct pci_dev *pdev)
+{
+       struct build_feature_devs_info *binfo;
+       int ret;
+
+       binfo = build_info_alloc_and_init(pdev);
+       if (!binfo)
+               return -ENOMEM;
+
+       binfo->parent_dev = fpga_dev_create(&pdev->dev, INTEL_FPGA_DEV);
+       if (IS_ERR(binfo->parent_dev)) {
+               ret = PTR_ERR(binfo->parent_dev);
+               goto free_binfo_exit;
+       }
+
+       ret = parse_start(binfo);
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = parse_feature_list(binfo, binfo->ioaddr);
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = parse_ports_from_fme(binfo);
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = build_info_commit_dev(binfo);
+       if (ret)
+               goto free_binfo_exit;
+
+       /*
+        * everything is okay, reset ->parent_dev to stop it being
+        * freed by build_info_free()
+        */
+       binfo->parent_dev = NULL;
+
+free_binfo_exit:
+       build_info_free(binfo);
+       return ret;
+}
+
 /* PCI Device ID */
 #define PCIe_DEVICE_ID_PF_INT_5_X      0xBCBD
 #define PCIe_DEVICE_ID_PF_INT_6_X      0xBCC0
@@ -81,9 +898,18 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
                goto release_region_exit;
        }
 
-       /* TODO: create and add the platform device per feature list */
+       ret = create_init_drvdata(pcidev);
+       if (ret)
+               goto release_region_exit;
+
+       ret = cci_pci_create_feature_devs(pcidev);
+       if (ret)
+               goto destroy_drvdata_exit;
+
        return 0;
 
+destroy_drvdata_exit:
+       destroy_drvdata(pcidev);
 release_region_exit:
        pci_release_regions(pcidev);
 disable_error_report_exit:
@@ -94,6 +920,8 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
 
 static void cci_pci_remove(struct pci_dev *pcidev)
 {
+       remove_all_devs(pcidev);
+       destroy_drvdata(pcidev);
        pci_release_regions(pcidev);
        pci_disable_pcie_error_reporting(pcidev);
        pci_disable_device(pcidev);
@@ -108,14 +936,23 @@ static void cci_pci_remove(struct pci_dev *pcidev)
 
 static int __init ccidrv_init(void)
 {
+       int ret;
+
        pr_info("Intel(R) FPGA PCIe Driver: Version %s\n", DRV_VERSION);
 
-       return pci_register_driver(&cci_pci_driver);
+       fpga_ids_init();
+
+       ret = pci_register_driver(&cci_pci_driver);
+       if (ret)
+               fpga_ids_destroy();
+
+       return ret;
 }
 
 static void __exit ccidrv_exit(void)
 {
        pci_unregister_driver(&cci_pci_driver);
+       fpga_ids_destroy();
 }
 
 module_init(ccidrv_init);
--

1.8.3.1

author
#40 lore raw

Re: [PATCH v2 07/22] fpga: intel: pcie: parse feature list and create platform device for features.

Quoted 266 lines · Alan Tull
Quoted 252 lines · Wu Hao

From: Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>

Device Feature List structure creates a link list of feature headers within the MMIO space to provide an extensible way of adding features.

The Intel FPGA PCIe driver walks through the feature headers to enumerate feature devices, FPGA Management Engine (FME) and FPGA Port for Accelerated Function Unit (AFU), and their private sub features. For feature devices, it creates the platform devices and linked the private sub features into their platform data.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.
fixed kbuild warning, typos and clean up the code.

---
 drivers/fpga/Makefile            |   2 +-
 drivers/fpga/intel-feature-dev.c | 130 ++++++
 drivers/fpga/intel-feature-dev.h | 341 ++++++++++++++++
 drivers/fpga/intel-pcie.c        | 841 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 1311 insertions(+), 3 deletions(-)
 create mode 100644 drivers/fpga/intel-feature-dev.c
 create mode 100644 drivers/fpga/intel-feature-dev.h
drivers/fpga/Makefile +1 -1
index 5613133..ad24b3d 100644
@@ -31,4 +31,4 @@ obj-$(CONFIG_OF_FPGA_REGION)          += of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)           += intel-fpga-pci.o
 
-intel-fpga-pci-objs := intel-pcie.o
+intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
drivers/fpga/intel-feature-dev.c new file +130 -0
new file mode 100644
index 0000000..68f9cba
@@ -0,0 +1,130 @@
+/*
+ * Intel FPGA Feature Device Driver
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "intel-feature-dev.h"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                              int index, const char *name,
+                              int resource_index, void __iomem *ioaddr)
+{
+       WARN_ON(index >= pdata->num);
+
+       pdata->features[index].name = name;
+       pdata->features[index].resource_index = resource_index;
+       pdata->features[index].ioaddr = ioaddr;
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num)
+{
+       struct feature_platform_data *pdata;
+
+       pdata = kzalloc(feature_platform_data_size(num), GFP_KERNEL);
+       if (pdata) {
+               pdata->dev = dev;
+               pdata->num = num;
+               mutex_init(&pdata->lock);
+       }
+
+       return pdata;
+}
+
+int fme_feature_num(void)
+{
+       return FME_FEATURE_ID_MAX;
+}
+
+int port_feature_num(void)
+{
+       return PORT_FEATURE_ID_MAX;
+}
+
+int fpga_port_id(struct platform_device *pdev)
+{
+       struct feature_port_header *port_hdr;
+       struct feature_port_capability capability;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       capability.csr = readq(&port_hdr->capability);
+       return capability.port_number;
+}
+EXPORT_SYMBOL_GPL(fpga_port_id);
+
+/*
+ * Enable Port by clear the port soft reset bit, which is set by default.
+ * The User AFU is unable to respond to any MMIO access while in reset.
+ * __fpga_port_enable function should only be used after __fpga_port_disable
+ * function.
+ */
+void __fpga_port_enable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       WARN_ON(!pdata->disable_count);
+
+       if (--pdata->disable_count != 0)
+               return;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x0;
+       writeq(control.csr, &port_hdr->control);
+}
+EXPORT_SYMBOL_GPL(__fpga_port_enable);
+
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
+int __fpga_port_disable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       if (pdata->disable_count++ != 0)
+               return 0;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       /* Set port soft reset */
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x1;
+       writeq(control.csr, &port_hdr->control);
+
+       /*
+        * HW sets ack bit to 1 when all outstanding requests have been drained
+        * on this port and minimum soft reset pulse width has elapsed.
+        * Driver polls port_soft_reset_ack to determine if reset done by HW.
+        */
+       if (readq_poll_timeout(&port_hdr->control, control.csr,
+                              (control.port_sftrst_ack == 1),
+                              RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+               dev_err(&pdev->dev, "timeout, fail to reset device\n");
+               return -ETIMEDOUT;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(__fpga_port_disable);
drivers/fpga/intel-feature-dev.h new file +67 -0
new file mode 100644
index 0000000..f67784a
@@ -0,0 +1,341 @@
+/*
+ * Intel FPGA Feature Device Driver Header File
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef __INTEL_FPGA_FEATURE_H
+#define __INTEL_FPGA_FEATURE_H
+
+#include <linux/fs.h>
+#include <linux/pci.h>
+#include <linux/uuid.h>
+#include <linux/delay.h>
+#include <linux/iopoll.h>
+#include <linux/platform_device.h>
+
+#ifndef readq
+static inline u64 readq(void __iomem *addr)
+{
+       return readl(addr) + ((u64)readl(addr + 4) << 32);
+}
+#endif
+
+#ifndef writeq
+static inline void writeq(u64 val, void __iomem *addr)
+{
+       writel((u32) (val), addr);
+       writel((u32) (val >> 32), (addr + 4));
+}
+#endif
+
+/* maximum supported number of ports */
+#define MAX_FPGA_PORT_NUM 4
+/* plus one for fme device */
+#define MAX_FEATURE_DEV_NUM    (MAX_FPGA_PORT_NUM + 1)
+
+#define FME_FEATURE_HEADER          "fme_hdr"
+#define FME_FEATURE_THERMAL_MGMT    "fme_thermal"
+#define FME_FEATURE_POWER_MGMT      "fme_power"
+#define FME_FEATURE_GLOBAL_PERF     "fme_gperf"
+#define FME_FEATURE_GLOBAL_ERR      "fme_error"
+#define FME_FEATURE_PR_MGMT         "fme_pr"
+
+#define PORT_FEATURE_HEADER         "port_hdr"
+#define PORT_FEATURE_UAFU           "port_uafu"
+#define PORT_FEATURE_ERR            "port_err"
+#define PORT_FEATURE_UMSG           "port_umsg"
+#define PORT_FEATURE_PR             "port_pr"
+#define PORT_FEATURE_STP            "port_stp"
+
+/* All headers and structures must be byte-packed to match the spec. */
+#pragma pack(1)
+
+/* common header for all features */
+struct feature_header {
+       union {
+               u64 csr;
+               struct {

drivers/fpga/intel-feature-dev.h line 77

Quoted 10 lines
drivers/fpga/intel-feature-dev.h new file +10 -0
+                       u64 id:12;
+                       u64 revision:4;
+                       u64 next_header_offset:24; /* offset to next header */
+                       u64 rsvdz:20;
+                       u64 type:4;                /* feature type */
+#define FEATURE_TYPE_AFU               0x1
+#define FEATURE_TYPE_PRIVATE           0x3
+               };
+       };
+};

Hi Hao,

Quoted 2 lines

Aren't these union's introducing a portability problem? Can you use regmap instead?

Hi Alan

I think it's fine to use union here, as endian conversion should be covered inside the readq/writeq function. :)

Thanks
Hao

Quoted 3 lines

Alan

#41 lore raw

Re: [PATCH v2 07/22] fpga: intel: pcie: parse feature list and create platform device for features.

In reply to Wu Hao
Quoted 214 lines · Wu Hao

From: Xiao Guangrong <[email protected]>

Device Feature List structure creates a link list of feature headers within the MMIO space to provide an extensible way of adding features.

The Intel FPGA PCIe driver walks through the feature headers to enumerate feature devices, FPGA Management Engine (FME) and FPGA Port for Accelerated Function Unit (AFU), and their private sub features. For feature devices, it creates the platform devices and linked the private sub features into their platform data.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.
fixed kbuild warning, typos and clean up the code.

---
 drivers/fpga/Makefile            |   2 +-
 drivers/fpga/intel-feature-dev.c | 130 ++++++
 drivers/fpga/intel-feature-dev.h | 341 ++++++++++++++++
 drivers/fpga/intel-pcie.c        | 841 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 1311 insertions(+), 3 deletions(-)
 create mode 100644 drivers/fpga/intel-feature-dev.c
 create mode 100644 drivers/fpga/intel-feature-dev.h
drivers/fpga/Makefile +1 -1
index 5613133..ad24b3d 100644
@@ -31,4 +31,4 @@ obj-$(CONFIG_OF_FPGA_REGION)          += of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)           += intel-fpga-pci.o
 
-intel-fpga-pci-objs := intel-pcie.o
+intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
drivers/fpga/intel-feature-dev.c new file +130 -0
new file mode 100644
index 0000000..68f9cba
@@ -0,0 +1,130 @@
+/*
+ * Intel FPGA Feature Device Driver
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "intel-feature-dev.h"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                              int index, const char *name,
+                              int resource_index, void __iomem *ioaddr)
+{
+       WARN_ON(index >= pdata->num);
+
+       pdata->features[index].name = name;
+       pdata->features[index].resource_index = resource_index;
+       pdata->features[index].ioaddr = ioaddr;
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num)
+{
+       struct feature_platform_data *pdata;
+
+       pdata = kzalloc(feature_platform_data_size(num), GFP_KERNEL);
+       if (pdata) {
+               pdata->dev = dev;
+               pdata->num = num;
+               mutex_init(&pdata->lock);
+       }
+
+       return pdata;
+}
+
+int fme_feature_num(void)
+{
+       return FME_FEATURE_ID_MAX;
+}
+
+int port_feature_num(void)
+{
+       return PORT_FEATURE_ID_MAX;
+}
+
+int fpga_port_id(struct platform_device *pdev)
+{
+       struct feature_port_header *port_hdr;
+       struct feature_port_capability capability;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       capability.csr = readq(&port_hdr->capability);
+       return capability.port_number;
+}
+EXPORT_SYMBOL_GPL(fpga_port_id);
+
+/*
+ * Enable Port by clear the port soft reset bit, which is set by default.
+ * The User AFU is unable to respond to any MMIO access while in reset.
+ * __fpga_port_enable function should only be used after __fpga_port_disable
+ * function.
+ */
+void __fpga_port_enable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       WARN_ON(!pdata->disable_count);
+
+       if (--pdata->disable_count != 0)
+               return;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x0;
+       writeq(control.csr, &port_hdr->control);
+}
+EXPORT_SYMBOL_GPL(__fpga_port_enable);
+
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
+int __fpga_port_disable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       if (pdata->disable_count++ != 0)
+               return 0;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       /* Set port soft reset */
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x1;
+       writeq(control.csr, &port_hdr->control);
+
+       /*
+        * HW sets ack bit to 1 when all outstanding requests have been drained
+        * on this port and minimum soft reset pulse width has elapsed.
+        * Driver polls port_soft_reset_ack to determine if reset done by HW.
+        */
+       if (readq_poll_timeout(&port_hdr->control, control.csr,
+                              (control.port_sftrst_ack == 1),
+                              RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+               dev_err(&pdev->dev, "timeout, fail to reset device\n");
+               return -ETIMEDOUT;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(__fpga_port_disable);
drivers/fpga/intel-feature-dev.h new file +29 -0
new file mode 100644
index 0000000..f67784a
@@ -0,0 +1,341 @@
+/*
+ * Intel FPGA Feature Device Driver Header File
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef __INTEL_FPGA_FEATURE_H
+#define __INTEL_FPGA_FEATURE_H
+
+#include <linux/fs.h>
+#include <linux/pci.h>
+#include <linux/uuid.h>
+#include <linux/delay.h>
+#include <linux/iopoll.h>
+#include <linux/platform_device.h>
+
+#ifndef readq
+static inline u64 readq(void __iomem *addr)
+{
+       return readl(addr) + ((u64)readl(addr + 4) << 32);

drivers/fpga/intel-feature-dev.h line 39

Quoted 10 lines
drivers/fpga/intel-feature-dev.h new file +10 -0
+}
+#endif
+
+#ifndef writeq
+static inline void writeq(u64 val, void __iomem *addr)
+{
+       writel((u32) (val), addr);
+       writel((u32) (val >> 32), (addr + 4));
+}
+#endif

Hi Hao,

Was there a reason readq and writeq had to be created for this? Do these get used?

Alan

Quoted 1,191 lines
drivers/fpga/intel-feature-dev.h new file +302 -0
+
+/* maximum supported number of ports */
+#define MAX_FPGA_PORT_NUM 4
+/* plus one for fme device */
+#define MAX_FEATURE_DEV_NUM    (MAX_FPGA_PORT_NUM + 1)
+
+#define FME_FEATURE_HEADER          "fme_hdr"
+#define FME_FEATURE_THERMAL_MGMT    "fme_thermal"
+#define FME_FEATURE_POWER_MGMT      "fme_power"
+#define FME_FEATURE_GLOBAL_PERF     "fme_gperf"
+#define FME_FEATURE_GLOBAL_ERR      "fme_error"
+#define FME_FEATURE_PR_MGMT         "fme_pr"
+
+#define PORT_FEATURE_HEADER         "port_hdr"
+#define PORT_FEATURE_UAFU           "port_uafu"
+#define PORT_FEATURE_ERR            "port_err"
+#define PORT_FEATURE_UMSG           "port_umsg"
+#define PORT_FEATURE_PR             "port_pr"
+#define PORT_FEATURE_STP            "port_stp"
+
+/* All headers and structures must be byte-packed to match the spec. */
+#pragma pack(1)
+
+/* common header for all features */
+struct feature_header {
+       union {
+               u64 csr;
+               struct {
+                       u64 id:12;
+                       u64 revision:4;
+                       u64 next_header_offset:24; /* offset to next header */
+                       u64 rsvdz:20;
+                       u64 type:4;                /* feature type */
+#define FEATURE_TYPE_AFU               0x1
+#define FEATURE_TYPE_PRIVATE           0x3
+               };
+       };
+};
+
+/* common header for non-private features */
+struct feature_afu_header {
+       uuid_le guid;
+       union {
+               u64 csr;
+               struct {
+                       u64 next_afu:24;        /* pointer to next afu header */
+                       u64 rsvdz:40;
+               };
+       };
+};
+
+/* FME Header Register Set */
+/* FME Capability Register */
+struct feature_fme_capability {
+       union {
+               u64 csr;
+               struct {
+                       u64 fabric_verid:8;     /* Fabric version ID */
+                       u64 socket_id:1;        /* Socket id */
+                       u64 rsvdz1:3;
+                       u64 pcie0_link_avl:1;   /* PCIe0 link availability */
+                       u64 pcie1_link_avl:1;   /* PCIe1 link availability */
+                       u64 coherent_link_avl:1;/* Coherent link availability */
+                       u64 rsvdz2:1;
+                       u64 iommu_support:1;    /* IOMMU or VT-d supported */
+                       u64 num_ports:3;        /* Num of ports implemented */
+                       u64 rsvdz3:4;
+                       u64 addr_width_bits:6;  /* Address width supported */
+                       u64 rsvdz4:2;
+                       u64 cache_size:12;      /* Cache size in kb */
+                       u64 cache_assoc:4;      /* Cache Associativity */
+                       u64 rsvdz5:15;
+                       u64 lock_bit:1;         /* Latched lock bit by BIOS */
+               };
+       };
+};
+
+/* FME Port Offset Register */
+struct feature_fme_port {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_offset:24;     /* Offset to port header */
+                       u64 rsvdz1:8;
+                       u64 port_bar:3;         /* Bar id */
+                       u64 rsvdz2:20;
+                       u64 afu_access_ctrl:1;  /* AFU access type: PF/VF */
+                       u64 rsvdz3:4;
+                       u64 port_implemented:1; /* Port implemented or not */
+                       u64 rsvdz4:3;
+               };
+       };
+};
+
+struct feature_fme_header {
+       struct feature_header header;
+       struct feature_afu_header afu_header;
+       u64 rsvd[2];
+       struct feature_fme_capability capability;
+       struct feature_fme_port port[MAX_FPGA_PORT_NUM];
+};
+
+/* FME Thermal Sub Feature Register Set */
+struct feature_fme_thermal {
+       struct feature_header header;
+};
+
+/* FME Power Sub Feature Register Set */
+struct feature_fme_power {
+       struct feature_header header;
+};
+
+/* FME Global Performance Sub Feature Register Set */
+struct feature_fme_gperf {
+       struct feature_header header;
+};
+
+/* FME Error Sub Feature Register Set */
+struct feature_fme_err {
+       struct feature_header header;
+};
+
+/* FME Partial Reconfiguration Sub Feature Register Set */
+struct feature_fme_pr {
+       struct feature_header header;
+};
+
+/* PORT Header Register Set */
+/* Port Capability Register */
+struct feature_port_capability {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_number:2;      /* Port Number 0-3 */
+                       u64 rsvdz1:6;
+                       u64 mmio_size:16;       /* User MMIO size in KB */
+                       u64 rsvdz2:8;
+                       u64 sp_intr_num:4;      /* Supported interrupts num */
+                       u64 rsvdz3:28;
+               };
+       };
+};
+
+/* Port Control Register */
+struct feature_port_control {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_sftrst:1;      /* Port Soft Reset */
+                       u64 rsvdz1:1;
+                       u64 latency_tolerance:1;/* '1' >= 40us, '0' < 40us */
+                       u64 rsvdz2:1;
+                       u64 port_sftrst_ack:1;  /* HW ACK for Soft Reset */
+                       u64 rsvdz3:59;
+               };
+       };
+};
+
+struct feature_port_header {
+       struct feature_header header;
+       struct feature_afu_header afu_header;
+       u64 rsvd[2];
+       struct feature_port_capability capability;
+       struct feature_port_control control;
+};
+
+/* PORT Error Sub Feature Register Set */
+struct feature_port_error {
+       struct feature_header header;
+};
+
+/* PORT Unordered Message Sub Feature Register Set */
+struct feature_port_umsg {
+       struct feature_header header;
+};
+
+/* PORT SignalTap Sub Feature Register Set */
+struct feature_port_stp {
+       struct feature_header header;
+};
+
+#pragma pack()
+
+struct feature {
+       const char *name;
+       int resource_index;
+       void __iomem *ioaddr;
+};
+
+struct feature_platform_data {
+       /* list the feature dev to cci_drvdata->port_dev_list. */
+       struct list_head node;
+       struct mutex lock;
+       struct platform_device *dev;
+       unsigned int disable_count;     /* count for port disable */
+
+       int num;                        /* number of features */
+       struct feature features[0];
+};
+
+enum fme_feature_id {
+       FME_FEATURE_ID_HEADER = 0x0,
+       FME_FEATURE_ID_THERMAL_MGMT = 0x1,
+       FME_FEATURE_ID_POWER_MGMT = 0x2,
+       FME_FEATURE_ID_GLOBAL_PERF = 0x3,
+       FME_FEATURE_ID_GLOBAL_ERR = 0x4,
+       FME_FEATURE_ID_PR_MGMT = 0x5,
+       FME_FEATURE_ID_MAX = 0x6,
+};
+
+enum port_feature_id {
+       PORT_FEATURE_ID_HEADER = 0x0,
+       PORT_FEATURE_ID_ERROR = 0x1,
+       PORT_FEATURE_ID_UMSG = 0x2,
+       PORT_FEATURE_ID_PR = 0x3,
+       PORT_FEATURE_ID_STP = 0x4,
+       PORT_FEATURE_ID_UAFU = 0x5,
+       PORT_FEATURE_ID_MAX = 0x6,
+};
+
+int fme_feature_num(void);
+int port_feature_num(void);
+
+#define FPGA_FEATURE_DEV_FME           "intel-fpga-fme"
+#define FPGA_FEATURE_DEV_PORT          "intel-fpga-port"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                              int index, const char *name,
+                              int resource_index, void __iomem *ioaddr);
+
+static inline int feature_platform_data_size(const int num)
+{
+       return sizeof(struct feature_platform_data) +
+               num * sizeof(struct feature);
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num);
+
+int fpga_port_id(struct platform_device *pdev);
+
+static inline int fpga_port_check_id(struct platform_device *pdev,
+                                    void *pport_id)
+{
+       return fpga_port_id(pdev) == *(int *)pport_id;
+}
+
+void __fpga_port_enable(struct platform_device *pdev);
+int __fpga_port_disable(struct platform_device *pdev);
+
+static inline void fpga_port_enable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+       mutex_lock(&pdata->lock);
+       __fpga_port_enable(pdev);
+       mutex_unlock(&pdata->lock);
+}
+
+static inline int fpga_port_disable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       int ret;
+
+       mutex_lock(&pdata->lock);
+       ret = __fpga_port_disable(pdev);
+       mutex_unlock(&pdata->lock);
+
+       return ret;
+}
+
+static inline int __fpga_port_reset(struct platform_device *pdev)
+{
+       int ret;
+
+       ret = __fpga_port_disable(pdev);
+       if (ret)
+               return ret;
+
+       __fpga_port_enable(pdev);
+       return 0;
+}
+
+static inline int fpga_port_reset(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       int ret;
+
+       mutex_lock(&pdata->lock);
+       ret = __fpga_port_reset(pdev);
+       mutex_unlock(&pdata->lock);
+       return ret;
+}
+
+static inline void __iomem *
+get_feature_ioaddr_by_index(struct device *dev, int index)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(dev);
+
+       return pdata->features[index].ioaddr;
+}
+#endif
drivers/fpga/intel-pcie.c +839 -3
index f697de4..70b8284 100644
@@ -23,10 +23,827 @@
 #include <linux/stddef.h>
 #include <linux/errno.h>
 #include <linux/aer.h>
+#include <linux/fpga/fpga-dev.h>
+
+#include "intel-feature-dev.h"
 
 #define DRV_VERSION    "0.8"
 #define DRV_NAME       "intel-fpga-pci"
 
+#define INTEL_FPGA_DEV "intel-fpga-dev"
+
+static DEFINE_MUTEX(fpga_id_mutex);
+
+enum fpga_id_type {
+       FME_ID,         /* fme id allocation and mapping */
+       PORT_ID,        /* port id allocation and mapping */
+       FPGA_ID_MAX,
+};
+
+/* it is protected by fpga_id_mutex */
+static struct idr fpga_ids[FPGA_ID_MAX];
+
+struct cci_drvdata {
+       struct device *fme_dev;
+
+       struct mutex lock;
+       struct list_head port_dev_list;
+
+       struct list_head regions; /* global list of pci bar mapping region */
+};
+
+/* pci bar mapping info */
+struct cci_pci_region {
+       int bar;
+       void __iomem *ioaddr;   /* pointer to mapped bar region */
+       struct list_head node;
+};
+
+static void fpga_ids_init(void)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+               idr_init(fpga_ids + i);
+}
+
+static void fpga_ids_destroy(void)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+               idr_destroy(fpga_ids + i);
+}
+
+static int alloc_fpga_id(enum fpga_id_type type, struct device *dev)
+{
+       int id;
+
+       WARN_ON(type >= FPGA_ID_MAX);
+       mutex_lock(&fpga_id_mutex);
+       id = idr_alloc(fpga_ids + type, dev, 0, 0, GFP_KERNEL);
+       mutex_unlock(&fpga_id_mutex);
+       return id;
+}
+
+static void free_fpga_id(enum fpga_id_type type, int id)
+{
+       WARN_ON(type >= FPGA_ID_MAX);
+       mutex_lock(&fpga_id_mutex);
+       idr_remove(fpga_ids + type, id);
+       mutex_unlock(&fpga_id_mutex);
+}
+
+static void cci_pci_add_port_dev(struct pci_dev *pdev,
+                                struct platform_device *port_dev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct feature_platform_data *pdata = dev_get_platdata(&port_dev->dev);
+
+       mutex_lock(&drvdata->lock);
+       list_add(&pdata->node, &drvdata->port_dev_list);
+       get_device(&pdata->dev->dev);
+       mutex_unlock(&drvdata->lock);
+}
+
+static void cci_pci_remove_port_devs(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct feature_platform_data *pdata, *ptmp;
+
+       mutex_lock(&drvdata->lock);
+       list_for_each_entry_safe(pdata, ptmp, &drvdata->port_dev_list, node) {
+               struct platform_device *port_dev = pdata->dev;
+
+               /* the port should be unregistered first. */
+               WARN_ON(device_is_registered(&port_dev->dev));
+               list_del(&pdata->node);
+               free_fpga_id(PORT_ID, port_dev->id);
+               put_device(&port_dev->dev);
+       }
+       mutex_unlock(&drvdata->lock);
+}
+
+/* info collection during feature dev build. */
+struct build_feature_devs_info {
+       struct pci_dev *pdev;
+
+       /*
+        * PCI BAR mapping info. Parsing feature list starts from
+        * BAR 0 and switch to different BARs to parse Port
+        */
+       void __iomem *ioaddr;
+       void __iomem *ioend;
+       int current_bar;
+
+       /* points to FME header where the port offset is figured out. */
+       void __iomem *pfme_hdr;
+
+       /* the container device for all feature devices */
+       struct fpga_dev *parent_dev;
+
+       /* current feature device */
+       struct platform_device *feature_dev;
+};
+
+static void cci_pci_release_regions(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct cci_pci_region *tmp, *region;
+
+       list_for_each_entry_safe(region, tmp, &drvdata->regions, node) {
+               list_del(&region->node);
+               if (region->ioaddr)
+                       pci_iounmap(pdev, region->ioaddr);
+               devm_kfree(&pdev->dev, region);
+       }
+}
+
+static void __iomem *cci_pci_ioremap_bar(struct pci_dev *pdev, int bar)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct cci_pci_region *region;
+
+       list_for_each_entry(region, &drvdata->regions, node)
+               if (region->bar == bar) {
+                       dev_dbg(&pdev->dev, "BAR %d region exists\n", bar);
+                       return region->ioaddr;
+               }
+
+       region = devm_kzalloc(&pdev->dev, sizeof(*region), GFP_KERNEL);
+       if (!region)
+               return NULL;
+
+       region->bar = bar;
+       region->ioaddr = pci_ioremap_bar(pdev, bar);
+       if (!region->ioaddr) {
+               dev_err(&pdev->dev, "can't ioremap memory from BAR %d.\n", bar);
+               devm_kfree(&pdev->dev, region);
+               return NULL;
+       }
+
+       list_add(&region->node, &drvdata->regions);
+       return region->ioaddr;
+}
+
+static int parse_start_from(struct build_feature_devs_info *binfo, int bar)
+{
+       binfo->ioaddr = cci_pci_ioremap_bar(binfo->pdev, bar);
+       if (!binfo->ioaddr)
+               return -ENOMEM;
+
+       binfo->current_bar = bar;
+       binfo->ioend = binfo->ioaddr + pci_resource_len(binfo->pdev, bar);
+       return 0;
+}
+
+static int parse_start(struct build_feature_devs_info *binfo)
+{
+       /* fpga feature list starts from BAR 0 */
+       return parse_start_from(binfo, 0);
+}
+
+/* switch the memory mapping to BAR# @bar */
+static int parse_switch_to(struct build_feature_devs_info *binfo, int bar)
+{
+       return parse_start_from(binfo, bar);
+}
+
+static struct build_feature_devs_info *
+build_info_alloc_and_init(struct pci_dev *pdev)
+{
+       struct build_feature_devs_info *binfo;
+
+       binfo = devm_kzalloc(&pdev->dev, sizeof(*binfo), GFP_KERNEL);
+       if (binfo)
+               binfo->pdev = pdev;
+
+       return binfo;
+}
+
+static enum fpga_id_type feature_dev_id_type(struct platform_device *pdev)
+{
+       if (!strcmp(pdev->name, FPGA_FEATURE_DEV_FME))
+               return FME_ID;
+
+       if (!strcmp(pdev->name, FPGA_FEATURE_DEV_PORT))
+               return PORT_ID;
+
+       WARN_ON(1);
+       return FPGA_ID_MAX;
+}
+
+/*
+ * register current feature device, it is called when we need to switch to
+ * another feature parsing or we have parsed all features
+ */
+static int build_info_commit_dev(struct build_feature_devs_info *binfo)
+{
+       int ret;
+
+       if (!binfo->feature_dev)
+               return 0;
+
+       ret = platform_device_add(binfo->feature_dev);
+       if (!ret) {
+               struct cci_drvdata *drvdata;
+
+               drvdata = dev_get_drvdata(&binfo->pdev->dev);
+               if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
+                       cci_pci_add_port_dev(binfo->pdev, binfo->feature_dev);
+               else
+                       drvdata->fme_dev = get_device(&binfo->feature_dev->dev);
+
+               /*
+                * reset it to avoid build_info_free() freeing their resource.
+                *
+                * The resource of successfully registered feature devices
+                * will be freed by platform_device_unregister(). See the
+                * comments in build_info_create_dev().
+                */
+               binfo->feature_dev = NULL;
+       }
+
+       return ret;
+}
+
+static int
+build_info_create_dev(struct build_feature_devs_info *binfo,
+                     enum fpga_id_type type, int feature_nr, const char *name)
+{
+       struct platform_device *fdev;
+       struct resource *res;
+       struct feature_platform_data *pdata;
+       int ret;
+
+       /* we will create a new device, commit current device first */
+       ret = build_info_commit_dev(binfo);
+       if (ret)
+               return ret;
+
+       /*
+        * we use -ENODEV as the initialization indicator which indicates
+        * whether the id need to be reclaimed
+        */
+       fdev = binfo->feature_dev = platform_device_alloc(name, -ENODEV);
+       if (!fdev)
+               return -ENOMEM;
+
+       fdev->id = alloc_fpga_id(type, &fdev->dev);
+       if (fdev->id < 0)
+               return fdev->id;
+
+       fdev->dev.parent = &binfo->parent_dev->dev;
+
+       /*
+        * we do not need to care for the memory which is associated with
+        * the platform device. After calling platform_device_unregister(),
+        * it will be automatically freed by device's release() callback,
+        * platform_device_release().
+        */
+       pdata = feature_platform_data_alloc_and_init(fdev, feature_nr);
+       if (!pdata)
+               return -ENOMEM;
+
+       /*
+        * the count should be initialized to 0 to make sure
+        *__fpga_port_enable() following __fpga_port_disable()
+        * works properly for port device.
+        * and it should always be 0 for fme device.
+        */
+       WARN_ON(pdata->disable_count);
+
+       fdev->dev.platform_data = pdata;
+       fdev->num_resources = feature_nr;
+       fdev->resource = kcalloc(feature_nr, sizeof(*res), GFP_KERNEL);
+       if (!fdev->resource)
+               return -ENOMEM;
+
+       return 0;
+}
+
+static int remove_feature_dev(struct device *dev, void *data)
+{
+       struct platform_device *pdev = to_platform_device(dev);
+
+       platform_device_unregister(pdev);
+       return 0;
+}
+
+static int remove_parent_dev(struct device *dev, void *data)
+{
+       /* remove platform devices attached in the parent device */
+       device_for_each_child(dev, NULL, remove_feature_dev);
+       fpga_dev_destroy(to_fpga_dev(dev));
+       return 0;
+}
+
+static void remove_all_devs(struct pci_dev *pdev)
+{
+       /* remove parent device and all its children. */
+       device_for_each_child(&pdev->dev, NULL, remove_parent_dev);
+}
+
+static void build_info_free(struct build_feature_devs_info *binfo)
+{
+       if (!IS_ERR_OR_NULL(binfo->parent_dev))
+               remove_all_devs(binfo->pdev);
+
+       /*
+        * it is a valid id, free it. See comments in
+        * build_info_create_dev()
+        */
+       if (binfo->feature_dev && binfo->feature_dev->id >= 0)
+               free_fpga_id(feature_dev_id_type(binfo->feature_dev),
+                            binfo->feature_dev->id);
+
+       platform_device_put(binfo->feature_dev);
+
+       devm_kfree(&binfo->pdev->dev, binfo);
+}
+
+#define FEATURE_TYPE_AFU       0x1
+#define FEATURE_TYPE_PRIVATE   0x3
+
+/* FME and PORT GUID are fixed */
+#define FEATURE_FME_GUID "f9e17764-38f0-82fe-e346-524ae92aafbf"
+#define FEATURE_PORT_GUID "6b355b87-b06c-9642-eb42-8d139398b43a"
+
+static bool feature_is_fme(struct feature_afu_header *afu_hdr)
+{
+       uuid_le u;
+
+       uuid_le_to_bin(FEATURE_FME_GUID, &u);
+
+       return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+static bool feature_is_port(struct feature_afu_header *afu_hdr)
+{
+       uuid_le u;
+
+       uuid_le_to_bin(FEATURE_PORT_GUID, &u);
+
+       return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+/*
+ * UAFU GUID is dynamic as it can be changed after FME downloads different
+ * Green Bitstream to the port, so we treat the unknown GUIDs which are
+ * attached on port's feature list as UAFU.
+ */
+static bool feature_is_UAFU(struct build_feature_devs_info *binfo)
+{
+       if (!binfo->feature_dev ||
+             feature_dev_id_type(binfo->feature_dev) != PORT_ID)
+               return false;
+
+       return true;
+}
+
+static void
+build_info_add_sub_feature(struct build_feature_devs_info *binfo,
+                          int feature_id, const char *feature_name,
+                          resource_size_t resource_size, void __iomem *start)
+{
+
+       struct platform_device *fdev = binfo->feature_dev;
+       struct feature_platform_data *pdata = dev_get_platdata(&fdev->dev);
+       struct resource *res = &fdev->resource[feature_id];
+
+       res->start = pci_resource_start(binfo->pdev, binfo->current_bar) +
+               start - binfo->ioaddr;
+       res->end = res->start + resource_size - 1;
+       res->flags = IORESOURCE_MEM;
+       res->name = feature_name;
+
+       feature_platform_data_add(pdata, feature_id,
+                                 feature_name, feature_id, start);
+}
+
+struct feature_info {
+       const char *name;
+       resource_size_t resource_size;
+       int feature_index;
+};
+
+/* indexed by fme feature IDs which are defined in 'enum fme_feature_id'. */
+static struct feature_info fme_features[] = {
+       {
+               .name = FME_FEATURE_HEADER,
+               .resource_size = sizeof(struct feature_fme_header),
+               .feature_index = FME_FEATURE_ID_HEADER,
+       },
+       {
+               .name = FME_FEATURE_THERMAL_MGMT,
+               .resource_size = sizeof(struct feature_fme_thermal),
+               .feature_index = FME_FEATURE_ID_THERMAL_MGMT,
+       },
+       {
+               .name = FME_FEATURE_POWER_MGMT,
+               .resource_size = sizeof(struct feature_fme_power),
+               .feature_index = FME_FEATURE_ID_POWER_MGMT,
+       },
+       {
+               .name = FME_FEATURE_GLOBAL_PERF,
+               .resource_size = sizeof(struct feature_fme_gperf),
+               .feature_index = FME_FEATURE_ID_GLOBAL_PERF,
+       },
+       {
+               .name = FME_FEATURE_GLOBAL_ERR,
+               .resource_size = sizeof(struct feature_fme_err),
+               .feature_index = FME_FEATURE_ID_GLOBAL_ERR,
+       },
+       {
+               .name = FME_FEATURE_PR_MGMT,
+               .resource_size = sizeof(struct feature_fme_pr),
+               .feature_index = FME_FEATURE_ID_PR_MGMT,
+       }
+};
+
+/* indexed by port feature IDs which are defined in 'enum port_feature_id'. */
+static struct feature_info port_features[] = {
+       {
+               .name = PORT_FEATURE_HEADER,
+               .resource_size = sizeof(struct feature_port_header),
+               .feature_index = PORT_FEATURE_ID_HEADER,
+       },
+       {
+               .name = PORT_FEATURE_ERR,
+               .resource_size = sizeof(struct feature_port_error),
+               .feature_index = PORT_FEATURE_ID_ERROR,
+       },
+       {
+               .name = PORT_FEATURE_UMSG,
+               .resource_size = sizeof(struct feature_port_umsg),
+               .feature_index = PORT_FEATURE_ID_UMSG,
+       },
+       {
+               /* This feature isn't available for now */
+               .name = PORT_FEATURE_PR,
+               .resource_size = 0,
+               .feature_index = PORT_FEATURE_ID_PR,
+       },
+       {
+               .name = PORT_FEATURE_STP,
+               .resource_size = sizeof(struct feature_port_stp),
+               .feature_index = PORT_FEATURE_ID_STP,
+       },
+       {
+               /*
+                * For User AFU feature, its region size is not fixed, but
+                * reported by register PortCapability.mmio_size. Resource
+                * size of UAFU will be set while parse port device.
+                */
+               .name = PORT_FEATURE_UAFU,
+               .resource_size = 0,
+               .feature_index = PORT_FEATURE_ID_UAFU,
+       },
+};
+
+static int
+create_feature_instance(struct build_feature_devs_info *binfo,
+                       void __iomem *start, struct feature_info *finfo)
+{
+       if (binfo->ioend - start < finfo->resource_size)
+               return -EINVAL;
+
+       build_info_add_sub_feature(binfo, finfo->feature_index, finfo->name,
+                                  finfo->resource_size, start);
+       return 0;
+}
+
+static int parse_feature_fme(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&binfo->pdev->dev);
+       int ret;
+
+       ret = build_info_create_dev(binfo, FME_ID, fme_feature_num(),
+                                       FPGA_FEATURE_DEV_FME);
+       if (ret)
+               return ret;
+
+       if (drvdata->fme_dev) {
+               dev_err(&binfo->pdev->dev, "Multiple FMEs are detected.\n");
+               return -EINVAL;
+       }
+
+       return create_feature_instance(binfo, start,
+                                      &fme_features[FME_FEATURE_ID_HEADER]);
+}
+
+static int parse_feature_fme_private(struct build_feature_devs_info *binfo,
+                                    struct feature_header *hdr)
+{
+       struct feature_header header;
+
+       header.csr = readq(hdr);
+
+       if (header.id >= ARRAY_SIZE(fme_features)) {
+               dev_info(&binfo->pdev->dev, "FME feature id %x is not supported yet.\n",
+                        header.id);
+               return 0;
+       }
+
+       return create_feature_instance(binfo, hdr, &fme_features[header.id]);
+}
+
+static int parse_feature_port(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       int ret;
+
+       ret = build_info_create_dev(binfo, PORT_ID, port_feature_num(),
+                                       FPGA_FEATURE_DEV_PORT);
+       if (ret)
+               return ret;
+
+       return create_feature_instance(binfo, start,
+                                      &port_features[PORT_FEATURE_ID_HEADER]);
+}
+
+static void enable_port_uafu(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+       struct feature_port_header *port_hdr;
+       struct feature_port_capability capability;
+
+       port_hdr = (struct feature_port_header *)start;
+       capability.csr = readq(&port_hdr->capability);
+       port_features[id].resource_size = capability.mmio_size << 10;
+
+       /*
+        * To enable User AFU, driver needs to clear reset bit on related port,
+        * otherwise the mmio space of this user AFU will be invalid.
+        */
+       if (port_features[id].resource_size)
+               fpga_port_reset(binfo->feature_dev);
+}
+
+static int parse_feature_port_private(struct build_feature_devs_info *binfo,
+                                     struct feature_header *hdr)
+{
+       struct feature_header header;
+       enum port_feature_id id;
+
+       header.csr = readq(hdr);
+       /*
+        * the region of port feature id is [0x10, 0x13], + 1 to reserve 0
+        * which is dedicated for port-hdr.
+        */
+       id = (header.id & 0x000f) + 1;
+
+       if (id >= ARRAY_SIZE(port_features)) {
+               dev_info(&binfo->pdev->dev, "Port feature id %x is not supported yet.\n",
+                        header.id);
+               return 0;
+       }
+
+       return create_feature_instance(binfo, hdr, &port_features[id]);
+}
+
+static int parse_feature_port_uafu(struct build_feature_devs_info *binfo,
+                                struct feature_header *hdr)
+{
+       enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+       int ret;
+
+       if (port_features[id].resource_size) {
+               ret = create_feature_instance(binfo, hdr, &port_features[id]);
+               port_features[id].resource_size = 0;
+       } else {
+               dev_err(&binfo->pdev->dev, "the uafu feature header is mis-configured.\n");
+               ret = -EINVAL;
+       }
+
+       return ret;
+}
+
+static int parse_feature_afus(struct build_feature_devs_info *binfo,
+                             struct feature_header *hdr)
+{
+       int ret;
+       struct feature_afu_header *afu_hdr, header;
+       void __iomem *start;
+       void __iomem *end = binfo->ioend;
+
+       start = hdr;
+       for (; start < end; start += header.next_afu) {
+               if (end - start < (sizeof(*afu_hdr) + sizeof(*hdr)))
+                       return -EINVAL;
+
+               hdr = start;
+               afu_hdr = (struct feature_afu_header *) (hdr + 1);
+               header.csr = readq(&afu_hdr->csr);
+
+               if (feature_is_fme(afu_hdr)) {
+                       ret = parse_feature_fme(binfo, hdr);
+                       binfo->pfme_hdr = hdr;
+                       if (ret)
+                               return ret;
+               } else if (feature_is_port(afu_hdr)) {
+                       ret = parse_feature_port(binfo, hdr);
+                       enable_port_uafu(binfo, hdr);
+                       if (ret)
+                               return ret;
+               } else if (feature_is_UAFU(binfo)) {
+                       ret = parse_feature_port_uafu(binfo, hdr);
+                       if (ret)
+                               return ret;
+               } else
+                       dev_info(&binfo->pdev->dev, "AFU GUID %pUl is not supported yet.\n",
+                                afu_hdr->guid.b);
+
+               if (!header.next_afu)
+                       break;
+       }
+
+       return 0;
+}
+
+static int parse_feature_private(struct build_feature_devs_info *binfo,
+                                struct feature_header *hdr)
+{
+       struct feature_header header;
+
+       header.csr = readq(hdr);
+
+       if (!binfo->feature_dev) {
+               dev_err(&binfo->pdev->dev, "the private feature %x does not belong to any AFU.\n",
+                       header.id);
+               return -EINVAL;
+       }
+
+       switch (feature_dev_id_type(binfo->feature_dev)) {
+       case FME_ID:
+               return parse_feature_fme_private(binfo, hdr);
+       case PORT_ID:
+               return parse_feature_port_private(binfo, hdr);
+       default:
+               dev_info(&binfo->pdev->dev, "private feature %x belonging to AFU %s is not supported yet.\n",
+                        header.id, binfo->feature_dev->name);
+       }
+       return 0;
+}
+
+static int parse_feature(struct build_feature_devs_info *binfo,
+                        struct feature_header *hdr)
+{
+       struct feature_header header;
+       int ret = 0;
+
+       header.csr = readq(hdr);
+
+       switch (header.type) {
+       case FEATURE_TYPE_AFU:
+               ret = parse_feature_afus(binfo, hdr);
+               break;
+       case FEATURE_TYPE_PRIVATE:
+               ret = parse_feature_private(binfo, hdr);
+               break;
+       default:
+               dev_info(&binfo->pdev->dev,
+                        "Feature Type %x is not supported.\n", hdr->type);
+       };
+
+       return ret;
+}
+
+static int
+parse_feature_list(struct build_feature_devs_info *binfo, void __iomem *start)
+{
+       struct feature_header *hdr, header;
+       void __iomem *end = binfo->ioend;
+       int ret = 0;
+
+       for (; start < end; start += header.next_header_offset) {
+               if (end - start < sizeof(*hdr)) {
+                       dev_err(&binfo->pdev->dev, "The region is too small to contain a feature.\n");
+                       ret =  -EINVAL;
+                       break;
+               }
+
+               hdr = (struct feature_header *)start;
+               ret = parse_feature(binfo, hdr);
+               if (ret)
+                       break;
+
+               header.csr = readq(hdr);
+               if (!header.next_header_offset)
+                       break;
+       }
+
+       return ret;
+}
+
+static int parse_ports_from_fme(struct build_feature_devs_info *binfo)
+{
+       struct feature_fme_header *fme_hdr;
+       struct feature_fme_port port;
+       int i = 0, ret = 0;
+
+       if (binfo->pfme_hdr == NULL) {
+               dev_dbg(&binfo->pdev->dev, "VF is detected.\n");
+               return ret;
+       }
+
+       fme_hdr = binfo->pfme_hdr;
+
+       do {
+               port.csr = readq(&fme_hdr->port[i]);
+               if (!port.port_implemented)
+                       break;
+
+               ret = parse_switch_to(binfo, port.port_bar);
+               if (ret)
+                       break;
+
+               ret = parse_feature_list(binfo,
+                               binfo->ioaddr + port.port_offset);
+               if (ret)
+                       break;
+       } while (++i < MAX_FPGA_PORT_NUM);
+
+       return ret;
+}
+
+static int create_init_drvdata(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata;
+
+       drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
+       if (!drvdata)
+               return -ENOMEM;
+
+       mutex_init(&drvdata->lock);
+       INIT_LIST_HEAD(&drvdata->port_dev_list);
+       INIT_LIST_HEAD(&drvdata->regions);
+
+       dev_set_drvdata(&pdev->dev, drvdata);
+       return 0;
+}
+
+static void destroy_drvdata(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+
+       if (drvdata->fme_dev) {
+               /* fme device should be unregistered first. */
+               WARN_ON(device_is_registered(drvdata->fme_dev));
+               free_fpga_id(FME_ID, to_platform_device(drvdata->fme_dev)->id);
+               put_device(drvdata->fme_dev);
+       }
+
+       cci_pci_remove_port_devs(pdev);
+       cci_pci_release_regions(pdev);
+       dev_set_drvdata(&pdev->dev, NULL);
+       devm_kfree(&pdev->dev, drvdata);
+}
+
+static int cci_pci_create_feature_devs(struct pci_dev *pdev)
+{
+       struct build_feature_devs_info *binfo;
+       int ret;
+
+       binfo = build_info_alloc_and_init(pdev);
+       if (!binfo)
+               return -ENOMEM;
+
+       binfo->parent_dev = fpga_dev_create(&pdev->dev, INTEL_FPGA_DEV);
+       if (IS_ERR(binfo->parent_dev)) {
+               ret = PTR_ERR(binfo->parent_dev);
+               goto free_binfo_exit;
+       }
+
+       ret = parse_start(binfo);
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = parse_feature_list(binfo, binfo->ioaddr);
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = parse_ports_from_fme(binfo);
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = build_info_commit_dev(binfo);
+       if (ret)
+               goto free_binfo_exit;
+
+       /*
+        * everything is okay, reset ->parent_dev to stop it being
+        * freed by build_info_free()
+        */
+       binfo->parent_dev = NULL;
+
+free_binfo_exit:
+       build_info_free(binfo);
+       return ret;
+}
+
 /* PCI Device ID */
 #define PCIe_DEVICE_ID_PF_INT_5_X      0xBCBD
 #define PCIe_DEVICE_ID_PF_INT_6_X      0xBCC0
@@ -81,9 +898,18 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
                goto release_region_exit;
        }
 
-       /* TODO: create and add the platform device per feature list */
+       ret = create_init_drvdata(pcidev);
+       if (ret)
+               goto release_region_exit;
+
+       ret = cci_pci_create_feature_devs(pcidev);
+       if (ret)
+               goto destroy_drvdata_exit;
+
        return 0;
 
+destroy_drvdata_exit:
+       destroy_drvdata(pcidev);
 release_region_exit:
        pci_release_regions(pcidev);
 disable_error_report_exit:
@@ -94,6 +920,8 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
 
 static void cci_pci_remove(struct pci_dev *pcidev)
 {
+       remove_all_devs(pcidev);
+       destroy_drvdata(pcidev);
        pci_release_regions(pcidev);
        pci_disable_pcie_error_reporting(pcidev);
        pci_disable_device(pcidev);
@@ -108,14 +936,23 @@ static void cci_pci_remove(struct pci_dev *pcidev)
 
 static int __init ccidrv_init(void)
 {
+       int ret;
+
        pr_info("Intel(R) FPGA PCIe Driver: Version %s\n", DRV_VERSION);
 
-       return pci_register_driver(&cci_pci_driver);
+       fpga_ids_init();
+
+       ret = pci_register_driver(&cci_pci_driver);
+       if (ret)
+               fpga_ids_destroy();
+
+       return ret;
 }
 
 static void __exit ccidrv_exit(void)
 {
        pci_unregister_driver(&cci_pci_driver);
+       fpga_ids_destroy();
 }
 
 module_init(ccidrv_init);
--

1.8.3.1

author
#42 lore raw

RE: [PATCH v2 07/22] fpga: intel: pcie: parse feature list and create platform device for features.

Quoted 232 lines · Wu Hao
Quoted 7 lines

From: Xiao Guangrong <[email protected]>

Device Feature List structure creates a link list of feature headers within the MMIO space to provide an extensible way of adding features.

The Intel FPGA PCIe driver walks through the feature headers to enumerate feature devices, FPGA Management Engine (FME) and FPGA Port for

Accelerated

Quoted 17 lines

Function Unit (AFU), and their private sub features. For feature devices, it creates the platform devices and linked the private sub features into their platform data.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.
fixed kbuild warning, typos and clean up the code.

Quoted 5 lines
---
 drivers/fpga/Makefile            |   2 +-
 drivers/fpga/intel-feature-dev.c | 130 ++++++
 drivers/fpga/intel-feature-dev.h | 341 ++++++++++++++++
 drivers/fpga/intel-pcie.c        | 841

++++++++++++++++++++++++++++++++++++++-

drivers/fpga/Makefile obj-$(CONFIG_OF_FPGA_REGION) += of-fpga-

Quoted 9 lines
 4 files changed, 1311 insertions(+), 3 deletions(-)
 create mode 100644 drivers/fpga/intel-feature-dev.c
 create mode 100644 drivers/fpga/intel-feature-dev.h
drivers/fpga/Makefile +0 -0
index 5613133..ad24b3d 100644
@@ -31,4 +31,4 @@ obj-$(CONFIG_OF_FPGA_REGION)          += of-fpga-

region.o

Quoted 72 lines
drivers/fpga/Makefile +1 -1
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)           += intel-fpga-pci.o
 
-intel-fpga-pci-objs := intel-pcie.o
+intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
drivers/fpga/intel-feature-dev.c new file +61 -0
new file mode 100644
index 0000000..68f9cba
@@ -0,0 +1,130 @@
+/*
+ * Intel FPGA Feature Device Driver
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "intel-feature-dev.h"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                              int index, const char *name,
+                              int resource_index, void __iomem *ioaddr)
+{
+       WARN_ON(index >= pdata->num);
+
+       pdata->features[index].name = name;
+       pdata->features[index].resource_index = resource_index;
+       pdata->features[index].ioaddr = ioaddr;
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num)
+{
+       struct feature_platform_data *pdata;
+
+       pdata = kzalloc(feature_platform_data_size(num), GFP_KERNEL);
+       if (pdata) {
+               pdata->dev = dev;
+               pdata->num = num;
+               mutex_init(&pdata->lock);
+       }
+
+       return pdata;
+}
+
+int fme_feature_num(void)
+{
+       return FME_FEATURE_ID_MAX;
+}
+
+int port_feature_num(void)
+{
+       return PORT_FEATURE_ID_MAX;
+}
+
+int fpga_port_id(struct platform_device *pdev)
+{
+       struct feature_port_header *port_hdr;
+       struct feature_port_capability capability;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);

drivers/fpga/intel-feature-dev.c line 71

Quoted 10 lines
drivers/fpga/intel-feature-dev.c new file +10 -0
+
+       capability.csr = readq(&port_hdr->capability);
+       return capability.port_number;
+}
+EXPORT_SYMBOL_GPL(fpga_port_id);
+
+/*
+ * Enable Port by clear the port soft reset bit, which is set by default.
+ * The User AFU is unable to respond to any MMIO access while in reset.
+ * __fpga_port_enable function should only be used after

__fpga_port_disable

Quoted 94 lines
drivers/fpga/intel-feature-dev.c new file +59 -0
+ * function.
+ */
+void __fpga_port_enable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       WARN_ON(!pdata->disable_count);
+
+       if (--pdata->disable_count != 0)
+               return;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x0;
+       writeq(control.csr, &port_hdr->control);
+}
+EXPORT_SYMBOL_GPL(__fpga_port_enable);
+
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
+int __fpga_port_disable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       if (pdata->disable_count++ != 0)
+               return 0;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       /* Set port soft reset */
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x1;
+       writeq(control.csr, &port_hdr->control);
+
+       /*
+        * HW sets ack bit to 1 when all outstanding requests have been drained
+        * on this port and minimum soft reset pulse width has elapsed.
+        * Driver polls port_soft_reset_ack to determine if reset done by HW.
+        */
+       if (readq_poll_timeout(&port_hdr->control, control.csr,
+                              (control.port_sftrst_ack == 1),
+                              RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+               dev_err(&pdev->dev, "timeout, fail to reset device\n");
+               return -ETIMEDOUT;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(__fpga_port_disable);
drivers/fpga/intel-feature-dev.h new file +29 -0
new file mode 100644
index 0000000..f67784a
@@ -0,0 +1,341 @@
+/*
+ * Intel FPGA Feature Device Driver Header File
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef __INTEL_FPGA_FEATURE_H
+#define __INTEL_FPGA_FEATURE_H
+
+#include <linux/fs.h>
+#include <linux/pci.h>
+#include <linux/uuid.h>
+#include <linux/delay.h>
+#include <linux/iopoll.h>
+#include <linux/platform_device.h>
+
+#ifndef readq
+static inline u64 readq(void __iomem *addr)
+{
+       return readl(addr) + ((u64)readl(addr + 4) << 32);

drivers/fpga/intel-feature-dev.h line 39

Quoted 10 lines
drivers/fpga/intel-feature-dev.h new file +10 -0
+}
+#endif
+
+#ifndef writeq
+static inline void writeq(u64 val, void __iomem *addr)
+{
+       writel((u32) (val), addr);
+       writel((u32) (val >> 32), (addr + 4));
+}
+#endif

Hi Hao,

Quoted 3 lines

Was there a reason readq and writeq had to be created for this? Do these get used?

Hi Alan

Thanks for your review.

Driver uses writeq and readq to access HW registers.

Actually this is a problem reported by kbuild against the patch set v1[1]. Writeq/readq are missed in some arch, so I added writeq/readq definitions to resolve this problem following the same way done by some existing drivers. After recheck this today, I found I should use linux/io-64-nonatomic-lo-hi.h instead of rewriting them. I will fix it in the next version.

[1] http://marc.info/?l=linux-kernel&m=149100396816882&w=2

Thanks
Hao

Quoted 1 line

Alan

#43 lore raw

Re: [PATCH v2 07/22] fpga: intel: pcie: parse feature list and create platform device for features.

In reply to Wu Hao

a (wh., *()On Sun, Jun 25, 2017 at 8:52 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

I'm done with some board bringup so I have time to look at your patchset again.

Something I can't help but notice is that this patchset will be almost completely reusable for embedded FPGAs if the enumeration is separated from the pcie code. After the devices are created, they are just mmio devices. That makes this whole scheme available for embedded FPGAs.

The division of things would be that the pcie code would read the headers and do ioremapping. Then pass the headers to the enumeration code to create the devices. With that division, another platform that is embedded could have its own code that read the headers and did the iomapping and then used the separate enumeration code.

Going through intel-pcie.c, the truly pcie specific parts are already pretty well contained. The pcie code is in probe, cci_pci_ioremap_bar, cci_pci_release_regions, and the 'parse_s*' functions that ultimately call cci_pci_ioremap_bar. So it all comes down to the code that calls the 'parse_s*' functions. That is, the code the reads the fme header and the port headers. Currently, if I'm reading this right, this code ioremaps each region, reads its header, and creates its device before reading the next header. So we just need to change the order a little and ioremap/read all the headers before starting to create devices. That separates the pci ioremap from creating the devices. So the probe function could ioremap and read in the fme header. Then it would ioremap and read in each of the port headers. After they are read in, then pass the all these headers to the separate enumeration code that will parse it and create the devices. That part is not pcie specific, right? With the enumeration code in a separate file (and assuming that the ioremap has already happened), another platform that is embedded could have its probe do the iomapping and reading the headers, and then call the same enumeration code.

Actually, there's a bit more to it than that but I think this is doable. build_info_add_sub_feature() (which is called by create_feature_instance) saves off resource info for each sub device. Does this mean this code is ioremapping again (in intel-fpga-fme-mgr.c's probe)?

A few more things below...

Quoted 91 lines

From: Xiao Guangrong <[email protected]>

Device Feature List structure creates a link list of feature headers within the MMIO space to provide an extensible way of adding features.

The Intel FPGA PCIe driver walks through the feature headers to enumerate feature devices, FPGA Management Engine (FME) and FPGA Port for Accelerated Function Unit (AFU), and their private sub features. For feature devices, it creates the platform devices and linked the private sub features into their platform data.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.
fixed kbuild warning, typos and clean up the code.

---
 drivers/fpga/Makefile            |   2 +-
 drivers/fpga/intel-feature-dev.c | 130 ++++++
 drivers/fpga/intel-feature-dev.h | 341 ++++++++++++++++
 drivers/fpga/intel-pcie.c        | 841 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 1311 insertions(+), 3 deletions(-)
 create mode 100644 drivers/fpga/intel-feature-dev.c
 create mode 100644 drivers/fpga/intel-feature-dev.h
drivers/fpga/Makefile +1 -1
index 5613133..ad24b3d 100644
@@ -31,4 +31,4 @@ obj-$(CONFIG_OF_FPGA_REGION)          += of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)           += intel-fpga-pci.o
 
-intel-fpga-pci-objs := intel-pcie.o
+intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
drivers/fpga/intel-feature-dev.c new file +42 -0
new file mode 100644
index 0000000..68f9cba
@@ -0,0 +1,130 @@
+/*
+ * Intel FPGA Feature Device Driver
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "intel-feature-dev.h"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                              int index, const char *name,
+                              int resource_index, void __iomem *ioaddr)
+{
+       WARN_ON(index >= pdata->num);
+
+       pdata->features[index].name = name;
+       pdata->features[index].resource_index = resource_index;
+       pdata->features[index].ioaddr = ioaddr;
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num)
+{
+       struct feature_platform_data *pdata;
+
+       pdata = kzalloc(feature_platform_data_size(num), GFP_KERNEL);
+       if (pdata) {
+               pdata->dev = dev;
+               pdata->num = num;
+               mutex_init(&pdata->lock);
+       }
+
+       return pdata;
+}

drivers/fpga/intel-feature-dev.c line 52

Quoted 10 lines
drivers/fpga/intel-feature-dev.c new file +10 -0
+
+int fme_feature_num(void)
+{
+       return FME_FEATURE_ID_MAX;
+}
+
+int port_feature_num(void)
+{
+       return PORT_FEATURE_ID_MAX;
+}

Do these need to be functions? If so, static. But if you can just use the #define'd values that's one level of indirection we can get rid of. And when I'm going through code where there's extra levels of indirection that don't do anything, it makes it harder to understand ;)

Quoted 4 lines
drivers/fpga/intel-feature-dev.c new file +4 -0
+
+int fpga_port_id(struct platform_device *pdev)
+{
+       struct feature_port_header *port_hdr;

drivers/fpga/intel-feature-dev.c line 66

Quoted 10 lines
drivers/fpga/intel-feature-dev.c new file +10 -0
+       struct feature_port_capability capability;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       capability.csr = readq(&port_hdr->capability);
+       return capability.port_number;
+}
+EXPORT_SYMBOL_GPL(fpga_port_id);

This is here because every feature is a port, right?

Quoted 594 lines
drivers/fpga/intel-feature-dev.c new file +64 -0
+
+/*
+ * Enable Port by clear the port soft reset bit, which is set by default.
+ * The User AFU is unable to respond to any MMIO access while in reset.
+ * __fpga_port_enable function should only be used after __fpga_port_disable
+ * function.
+ */
+void __fpga_port_enable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       WARN_ON(!pdata->disable_count);
+
+       if (--pdata->disable_count != 0)
+               return;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x0;
+       writeq(control.csr, &port_hdr->control);
+}
+EXPORT_SYMBOL_GPL(__fpga_port_enable);
+
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
+int __fpga_port_disable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       if (pdata->disable_count++ != 0)
+               return 0;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       /* Set port soft reset */
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x1;
+       writeq(control.csr, &port_hdr->control);
+
+       /*
+        * HW sets ack bit to 1 when all outstanding requests have been drained
+        * on this port and minimum soft reset pulse width has elapsed.
+        * Driver polls port_soft_reset_ack to determine if reset done by HW.
+        */
+       if (readq_poll_timeout(&port_hdr->control, control.csr,
+                              (control.port_sftrst_ack == 1),
+                              RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+               dev_err(&pdev->dev, "timeout, fail to reset device\n");
+               return -ETIMEDOUT;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(__fpga_port_disable);
drivers/fpga/intel-feature-dev.h new file +341 -0
new file mode 100644
index 0000000..f67784a
@@ -0,0 +1,341 @@
+/*
+ * Intel FPGA Feature Device Driver Header File
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef __INTEL_FPGA_FEATURE_H
+#define __INTEL_FPGA_FEATURE_H
+
+#include <linux/fs.h>
+#include <linux/pci.h>
+#include <linux/uuid.h>
+#include <linux/delay.h>
+#include <linux/iopoll.h>
+#include <linux/platform_device.h>
+
+#ifndef readq
+static inline u64 readq(void __iomem *addr)
+{
+       return readl(addr) + ((u64)readl(addr + 4) << 32);
+}
+#endif
+
+#ifndef writeq
+static inline void writeq(u64 val, void __iomem *addr)
+{
+       writel((u32) (val), addr);
+       writel((u32) (val >> 32), (addr + 4));
+}
+#endif
+
+/* maximum supported number of ports */
+#define MAX_FPGA_PORT_NUM 4
+/* plus one for fme device */
+#define MAX_FEATURE_DEV_NUM    (MAX_FPGA_PORT_NUM + 1)
+
+#define FME_FEATURE_HEADER          "fme_hdr"
+#define FME_FEATURE_THERMAL_MGMT    "fme_thermal"
+#define FME_FEATURE_POWER_MGMT      "fme_power"
+#define FME_FEATURE_GLOBAL_PERF     "fme_gperf"
+#define FME_FEATURE_GLOBAL_ERR      "fme_error"
+#define FME_FEATURE_PR_MGMT         "fme_pr"
+
+#define PORT_FEATURE_HEADER         "port_hdr"
+#define PORT_FEATURE_UAFU           "port_uafu"
+#define PORT_FEATURE_ERR            "port_err"
+#define PORT_FEATURE_UMSG           "port_umsg"
+#define PORT_FEATURE_PR             "port_pr"
+#define PORT_FEATURE_STP            "port_stp"
+
+/* All headers and structures must be byte-packed to match the spec. */
+#pragma pack(1)
+
+/* common header for all features */
+struct feature_header {
+       union {
+               u64 csr;
+               struct {
+                       u64 id:12;
+                       u64 revision:4;
+                       u64 next_header_offset:24; /* offset to next header */
+                       u64 rsvdz:20;
+                       u64 type:4;                /* feature type */
+#define FEATURE_TYPE_AFU               0x1
+#define FEATURE_TYPE_PRIVATE           0x3
+               };
+       };
+};
+
+/* common header for non-private features */
+struct feature_afu_header {
+       uuid_le guid;
+       union {
+               u64 csr;
+               struct {
+                       u64 next_afu:24;        /* pointer to next afu header */
+                       u64 rsvdz:40;
+               };
+       };
+};
+
+/* FME Header Register Set */
+/* FME Capability Register */
+struct feature_fme_capability {
+       union {
+               u64 csr;
+               struct {
+                       u64 fabric_verid:8;     /* Fabric version ID */
+                       u64 socket_id:1;        /* Socket id */
+                       u64 rsvdz1:3;
+                       u64 pcie0_link_avl:1;   /* PCIe0 link availability */
+                       u64 pcie1_link_avl:1;   /* PCIe1 link availability */
+                       u64 coherent_link_avl:1;/* Coherent link availability */
+                       u64 rsvdz2:1;
+                       u64 iommu_support:1;    /* IOMMU or VT-d supported */
+                       u64 num_ports:3;        /* Num of ports implemented */
+                       u64 rsvdz3:4;
+                       u64 addr_width_bits:6;  /* Address width supported */
+                       u64 rsvdz4:2;
+                       u64 cache_size:12;      /* Cache size in kb */
+                       u64 cache_assoc:4;      /* Cache Associativity */
+                       u64 rsvdz5:15;
+                       u64 lock_bit:1;         /* Latched lock bit by BIOS */
+               };
+       };
+};
+
+/* FME Port Offset Register */
+struct feature_fme_port {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_offset:24;     /* Offset to port header */
+                       u64 rsvdz1:8;
+                       u64 port_bar:3;         /* Bar id */
+                       u64 rsvdz2:20;
+                       u64 afu_access_ctrl:1;  /* AFU access type: PF/VF */
+                       u64 rsvdz3:4;
+                       u64 port_implemented:1; /* Port implemented or not */
+                       u64 rsvdz4:3;
+               };
+       };
+};
+
+struct feature_fme_header {
+       struct feature_header header;
+       struct feature_afu_header afu_header;
+       u64 rsvd[2];
+       struct feature_fme_capability capability;
+       struct feature_fme_port port[MAX_FPGA_PORT_NUM];
+};
+
+/* FME Thermal Sub Feature Register Set */
+struct feature_fme_thermal {
+       struct feature_header header;
+};
+
+/* FME Power Sub Feature Register Set */
+struct feature_fme_power {
+       struct feature_header header;
+};
+
+/* FME Global Performance Sub Feature Register Set */
+struct feature_fme_gperf {
+       struct feature_header header;
+};
+
+/* FME Error Sub Feature Register Set */
+struct feature_fme_err {
+       struct feature_header header;
+};
+
+/* FME Partial Reconfiguration Sub Feature Register Set */
+struct feature_fme_pr {
+       struct feature_header header;
+};
+
+/* PORT Header Register Set */
+/* Port Capability Register */
+struct feature_port_capability {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_number:2;      /* Port Number 0-3 */
+                       u64 rsvdz1:6;
+                       u64 mmio_size:16;       /* User MMIO size in KB */
+                       u64 rsvdz2:8;
+                       u64 sp_intr_num:4;      /* Supported interrupts num */
+                       u64 rsvdz3:28;
+               };
+       };
+};
+
+/* Port Control Register */
+struct feature_port_control {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_sftrst:1;      /* Port Soft Reset */
+                       u64 rsvdz1:1;
+                       u64 latency_tolerance:1;/* '1' >= 40us, '0' < 40us */
+                       u64 rsvdz2:1;
+                       u64 port_sftrst_ack:1;  /* HW ACK for Soft Reset */
+                       u64 rsvdz3:59;
+               };
+       };
+};
+
+struct feature_port_header {
+       struct feature_header header;
+       struct feature_afu_header afu_header;
+       u64 rsvd[2];
+       struct feature_port_capability capability;
+       struct feature_port_control control;
+};
+
+/* PORT Error Sub Feature Register Set */
+struct feature_port_error {
+       struct feature_header header;
+};
+
+/* PORT Unordered Message Sub Feature Register Set */
+struct feature_port_umsg {
+       struct feature_header header;
+};
+
+/* PORT SignalTap Sub Feature Register Set */
+struct feature_port_stp {
+       struct feature_header header;
+};
+
+#pragma pack()
+
+struct feature {
+       const char *name;
+       int resource_index;
+       void __iomem *ioaddr;
+};
+
+struct feature_platform_data {
+       /* list the feature dev to cci_drvdata->port_dev_list. */
+       struct list_head node;
+       struct mutex lock;
+       struct platform_device *dev;
+       unsigned int disable_count;     /* count for port disable */
+
+       int num;                        /* number of features */
+       struct feature features[0];
+};
+
+enum fme_feature_id {
+       FME_FEATURE_ID_HEADER = 0x0,
+       FME_FEATURE_ID_THERMAL_MGMT = 0x1,
+       FME_FEATURE_ID_POWER_MGMT = 0x2,
+       FME_FEATURE_ID_GLOBAL_PERF = 0x3,
+       FME_FEATURE_ID_GLOBAL_ERR = 0x4,
+       FME_FEATURE_ID_PR_MGMT = 0x5,
+       FME_FEATURE_ID_MAX = 0x6,
+};
+
+enum port_feature_id {
+       PORT_FEATURE_ID_HEADER = 0x0,
+       PORT_FEATURE_ID_ERROR = 0x1,
+       PORT_FEATURE_ID_UMSG = 0x2,
+       PORT_FEATURE_ID_PR = 0x3,
+       PORT_FEATURE_ID_STP = 0x4,
+       PORT_FEATURE_ID_UAFU = 0x5,
+       PORT_FEATURE_ID_MAX = 0x6,
+};
+
+int fme_feature_num(void);
+int port_feature_num(void);
+
+#define FPGA_FEATURE_DEV_FME           "intel-fpga-fme"
+#define FPGA_FEATURE_DEV_PORT          "intel-fpga-port"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                              int index, const char *name,
+                              int resource_index, void __iomem *ioaddr);
+
+static inline int feature_platform_data_size(const int num)
+{
+       return sizeof(struct feature_platform_data) +
+               num * sizeof(struct feature);
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num);
+
+int fpga_port_id(struct platform_device *pdev);
+
+static inline int fpga_port_check_id(struct platform_device *pdev,
+                                    void *pport_id)
+{
+       return fpga_port_id(pdev) == *(int *)pport_id;
+}
+
+void __fpga_port_enable(struct platform_device *pdev);
+int __fpga_port_disable(struct platform_device *pdev);
+
+static inline void fpga_port_enable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+       mutex_lock(&pdata->lock);
+       __fpga_port_enable(pdev);
+       mutex_unlock(&pdata->lock);
+}
+
+static inline int fpga_port_disable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       int ret;
+
+       mutex_lock(&pdata->lock);
+       ret = __fpga_port_disable(pdev);
+       mutex_unlock(&pdata->lock);
+
+       return ret;
+}
+
+static inline int __fpga_port_reset(struct platform_device *pdev)
+{
+       int ret;
+
+       ret = __fpga_port_disable(pdev);
+       if (ret)
+               return ret;
+
+       __fpga_port_enable(pdev);
+       return 0;
+}
+
+static inline int fpga_port_reset(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       int ret;
+
+       mutex_lock(&pdata->lock);
+       ret = __fpga_port_reset(pdev);
+       mutex_unlock(&pdata->lock);
+       return ret;
+}
+
+static inline void __iomem *
+get_feature_ioaddr_by_index(struct device *dev, int index)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(dev);
+
+       return pdata->features[index].ioaddr;
+}
+#endif
drivers/fpga/intel-pcie.c +171 -0
index f697de4..70b8284 100644
@@ -23,10 +23,827 @@
 #include <linux/stddef.h>
 #include <linux/errno.h>
 #include <linux/aer.h>
+#include <linux/fpga/fpga-dev.h>
+
+#include "intel-feature-dev.h"
 
 #define DRV_VERSION    "0.8"
 #define DRV_NAME       "intel-fpga-pci"
 
+#define INTEL_FPGA_DEV "intel-fpga-dev"
+
+static DEFINE_MUTEX(fpga_id_mutex);
+
+enum fpga_id_type {
+       FME_ID,         /* fme id allocation and mapping */
+       PORT_ID,        /* port id allocation and mapping */
+       FPGA_ID_MAX,
+};
+
+/* it is protected by fpga_id_mutex */
+static struct idr fpga_ids[FPGA_ID_MAX];
+
+struct cci_drvdata {
+       struct device *fme_dev;
+
+       struct mutex lock;
+       struct list_head port_dev_list;
+
+       struct list_head regions; /* global list of pci bar mapping region */
+};
+
+/* pci bar mapping info */
+struct cci_pci_region {
+       int bar;
+       void __iomem *ioaddr;   /* pointer to mapped bar region */
+       struct list_head node;
+};
+
+static void fpga_ids_init(void)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+               idr_init(fpga_ids + i);
+}
+
+static void fpga_ids_destroy(void)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+               idr_destroy(fpga_ids + i);
+}
+
+static int alloc_fpga_id(enum fpga_id_type type, struct device *dev)
+{
+       int id;
+
+       WARN_ON(type >= FPGA_ID_MAX);
+       mutex_lock(&fpga_id_mutex);
+       id = idr_alloc(fpga_ids + type, dev, 0, 0, GFP_KERNEL);
+       mutex_unlock(&fpga_id_mutex);
+       return id;
+}
+
+static void free_fpga_id(enum fpga_id_type type, int id)
+{
+       WARN_ON(type >= FPGA_ID_MAX);
+       mutex_lock(&fpga_id_mutex);
+       idr_remove(fpga_ids + type, id);
+       mutex_unlock(&fpga_id_mutex);
+}
+
+static void cci_pci_add_port_dev(struct pci_dev *pdev,
+                                struct platform_device *port_dev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct feature_platform_data *pdata = dev_get_platdata(&port_dev->dev);
+
+       mutex_lock(&drvdata->lock);
+       list_add(&pdata->node, &drvdata->port_dev_list);
+       get_device(&pdata->dev->dev);
+       mutex_unlock(&drvdata->lock);
+}
+
+static void cci_pci_remove_port_devs(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct feature_platform_data *pdata, *ptmp;
+
+       mutex_lock(&drvdata->lock);
+       list_for_each_entry_safe(pdata, ptmp, &drvdata->port_dev_list, node) {
+               struct platform_device *port_dev = pdata->dev;
+
+               /* the port should be unregistered first. */
+               WARN_ON(device_is_registered(&port_dev->dev));
+               list_del(&pdata->node);
+               free_fpga_id(PORT_ID, port_dev->id);
+               put_device(&port_dev->dev);
+       }
+       mutex_unlock(&drvdata->lock);
+}
+
+/* info collection during feature dev build. */
+struct build_feature_devs_info {
+       struct pci_dev *pdev;
+
+       /*
+        * PCI BAR mapping info. Parsing feature list starts from
+        * BAR 0 and switch to different BARs to parse Port
+        */
+       void __iomem *ioaddr;
+       void __iomem *ioend;
+       int current_bar;
+
+       /* points to FME header where the port offset is figured out. */
+       void __iomem *pfme_hdr;
+
+       /* the container device for all feature devices */
+       struct fpga_dev *parent_dev;
+
+       /* current feature device */
+       struct platform_device *feature_dev;
+};
+
+static void cci_pci_release_regions(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct cci_pci_region *tmp, *region;
+
+       list_for_each_entry_safe(region, tmp, &drvdata->regions, node) {
+               list_del(&region->node);
+               if (region->ioaddr)
+                       pci_iounmap(pdev, region->ioaddr);
+               devm_kfree(&pdev->dev, region);
+       }
+}
+
+static void __iomem *cci_pci_ioremap_bar(struct pci_dev *pdev, int bar)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct cci_pci_region *region;
+
+       list_for_each_entry(region, &drvdata->regions, node)
+               if (region->bar == bar) {
+                       dev_dbg(&pdev->dev, "BAR %d region exists\n", bar);
+                       return region->ioaddr;
+               }
+
+       region = devm_kzalloc(&pdev->dev, sizeof(*region), GFP_KERNEL);
+       if (!region)
+               return NULL;
+
+       region->bar = bar;
+       region->ioaddr = pci_ioremap_bar(pdev, bar);
+       if (!region->ioaddr) {
+               dev_err(&pdev->dev, "can't ioremap memory from BAR %d.\n", bar);
+               devm_kfree(&pdev->dev, region);
+               return NULL;
+       }
+
+       list_add(&region->node, &drvdata->regions);
+       return region->ioaddr;
+}
+
+static int parse_start_from(struct build_feature_devs_info *binfo, int bar)
+{
+       binfo->ioaddr = cci_pci_ioremap_bar(binfo->pdev, bar);
+       if (!binfo->ioaddr)
+               return -ENOMEM;
+
+       binfo->current_bar = bar;
+       binfo->ioend = binfo->ioaddr + pci_resource_len(binfo->pdev, bar);
+       return 0;
+}
+
+static int parse_start(struct build_feature_devs_info *binfo)

drivers/fpga/intel-pcie.c line 210

Quoted 10 lines
drivers/fpga/intel-pcie.c +10 -0
+{
+       /* fpga feature list starts from BAR 0 */
+       return parse_start_from(binfo, 0);
+}
+
+/* switch the memory mapping to BAR# @bar */
+static int parse_switch_to(struct build_feature_devs_info *binfo, int bar)
+{
+       return parse_start_from(binfo, bar);
+}

parse_switch_to() and parse_start() are both one line wrappers for parse_start_from() and are only called once. :-) Please just use parse_start_from and get rid of parse_start and parse_switch_to. I don't think this code is all that super complicated. But as I'm grepping through this code, I'm trying to see all the places that do the same thing; if I have to look for multiple function names, it takes longer to get what is going on here.

Actually, cci_pci_ioremap_bar is only called in one place - parse_start_from - which really just adds two lines of code. parse_start_from could go away. Just a suggestion. The reason this comes up is that cci_pci_ioremap_bar is the more descriptive function name. The parse_start/switch function names hide the biggest thing they do, which is ioremapping. They don't actually do any parsing ;)

drivers/fpga/intel-pcie.c line 219

Quoted 9 lines
drivers/fpga/intel-pcie.c +9 -0
+
+static struct build_feature_devs_info *
+build_info_alloc_and_init(struct pci_dev *pdev)
+{
+       struct build_feature_devs_info *binfo;
+
+       binfo = devm_kzalloc(&pdev->dev, sizeof(*binfo), GFP_KERNEL);
+       if (binfo)
+               binfo->pdev = pdev;

build_info_alloc_and_init() is only doing a devm_kzalloc and is only called once. The code would be more readable if you just had the devm_kzalloc in the function that called this (cci_pci_create_feature_devs).

Quoted 285 lines
drivers/fpga/intel-pcie.c +285 -0
+
+       return binfo;
+}
+
+static enum fpga_id_type feature_dev_id_type(struct platform_device *pdev)
+{
+       if (!strcmp(pdev->name, FPGA_FEATURE_DEV_FME))
+               return FME_ID;
+
+       if (!strcmp(pdev->name, FPGA_FEATURE_DEV_PORT))
+               return PORT_ID;
+
+       WARN_ON(1);
+       return FPGA_ID_MAX;
+}
+
+/*
+ * register current feature device, it is called when we need to switch to
+ * another feature parsing or we have parsed all features
+ */
+static int build_info_commit_dev(struct build_feature_devs_info *binfo)
+{
+       int ret;
+
+       if (!binfo->feature_dev)
+               return 0;
+
+       ret = platform_device_add(binfo->feature_dev);
+       if (!ret) {
+               struct cci_drvdata *drvdata;
+
+               drvdata = dev_get_drvdata(&binfo->pdev->dev);
+               if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
+                       cci_pci_add_port_dev(binfo->pdev, binfo->feature_dev);
+               else
+                       drvdata->fme_dev = get_device(&binfo->feature_dev->dev);
+
+               /*
+                * reset it to avoid build_info_free() freeing their resource.
+                *
+                * The resource of successfully registered feature devices
+                * will be freed by platform_device_unregister(). See the
+                * comments in build_info_create_dev().
+                */
+               binfo->feature_dev = NULL;
+       }
+
+       return ret;
+}
+
+static int
+build_info_create_dev(struct build_feature_devs_info *binfo,
+                     enum fpga_id_type type, int feature_nr, const char *name)
+{
+       struct platform_device *fdev;
+       struct resource *res;
+       struct feature_platform_data *pdata;
+       int ret;
+
+       /* we will create a new device, commit current device first */
+       ret = build_info_commit_dev(binfo);
+       if (ret)
+               return ret;
+
+       /*
+        * we use -ENODEV as the initialization indicator which indicates
+        * whether the id need to be reclaimed
+        */
+       fdev = binfo->feature_dev = platform_device_alloc(name, -ENODEV);
+       if (!fdev)
+               return -ENOMEM;
+
+       fdev->id = alloc_fpga_id(type, &fdev->dev);
+       if (fdev->id < 0)
+               return fdev->id;
+
+       fdev->dev.parent = &binfo->parent_dev->dev;
+
+       /*
+        * we do not need to care for the memory which is associated with
+        * the platform device. After calling platform_device_unregister(),
+        * it will be automatically freed by device's release() callback,
+        * platform_device_release().
+        */
+       pdata = feature_platform_data_alloc_and_init(fdev, feature_nr);
+       if (!pdata)
+               return -ENOMEM;
+
+       /*
+        * the count should be initialized to 0 to make sure
+        *__fpga_port_enable() following __fpga_port_disable()
+        * works properly for port device.
+        * and it should always be 0 for fme device.
+        */
+       WARN_ON(pdata->disable_count);
+
+       fdev->dev.platform_data = pdata;
+       fdev->num_resources = feature_nr;
+       fdev->resource = kcalloc(feature_nr, sizeof(*res), GFP_KERNEL);
+       if (!fdev->resource)
+               return -ENOMEM;
+
+       return 0;
+}
+
+static int remove_feature_dev(struct device *dev, void *data)
+{
+       struct platform_device *pdev = to_platform_device(dev);
+
+       platform_device_unregister(pdev);
+       return 0;
+}
+
+static int remove_parent_dev(struct device *dev, void *data)
+{
+       /* remove platform devices attached in the parent device */
+       device_for_each_child(dev, NULL, remove_feature_dev);
+       fpga_dev_destroy(to_fpga_dev(dev));
+       return 0;
+}
+
+static void remove_all_devs(struct pci_dev *pdev)
+{
+       /* remove parent device and all its children. */
+       device_for_each_child(&pdev->dev, NULL, remove_parent_dev);
+}
+
+static void build_info_free(struct build_feature_devs_info *binfo)
+{
+       if (!IS_ERR_OR_NULL(binfo->parent_dev))
+               remove_all_devs(binfo->pdev);
+
+       /*
+        * it is a valid id, free it. See comments in
+        * build_info_create_dev()
+        */
+       if (binfo->feature_dev && binfo->feature_dev->id >= 0)
+               free_fpga_id(feature_dev_id_type(binfo->feature_dev),
+                            binfo->feature_dev->id);
+
+       platform_device_put(binfo->feature_dev);
+
+       devm_kfree(&binfo->pdev->dev, binfo);
+}
+
+#define FEATURE_TYPE_AFU       0x1
+#define FEATURE_TYPE_PRIVATE   0x3
+
+/* FME and PORT GUID are fixed */
+#define FEATURE_FME_GUID "f9e17764-38f0-82fe-e346-524ae92aafbf"
+#define FEATURE_PORT_GUID "6b355b87-b06c-9642-eb42-8d139398b43a"
+
+static bool feature_is_fme(struct feature_afu_header *afu_hdr)
+{
+       uuid_le u;
+
+       uuid_le_to_bin(FEATURE_FME_GUID, &u);
+
+       return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+static bool feature_is_port(struct feature_afu_header *afu_hdr)
+{
+       uuid_le u;
+
+       uuid_le_to_bin(FEATURE_PORT_GUID, &u);
+
+       return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+/*
+ * UAFU GUID is dynamic as it can be changed after FME downloads different
+ * Green Bitstream to the port, so we treat the unknown GUIDs which are
+ * attached on port's feature list as UAFU.
+ */
+static bool feature_is_UAFU(struct build_feature_devs_info *binfo)
+{
+       if (!binfo->feature_dev ||
+             feature_dev_id_type(binfo->feature_dev) != PORT_ID)
+               return false;
+
+       return true;
+}
+
+static void
+build_info_add_sub_feature(struct build_feature_devs_info *binfo,
+                          int feature_id, const char *feature_name,
+                          resource_size_t resource_size, void __iomem *start)
+{
+
+       struct platform_device *fdev = binfo->feature_dev;
+       struct feature_platform_data *pdata = dev_get_platdata(&fdev->dev);
+       struct resource *res = &fdev->resource[feature_id];
+
+       res->start = pci_resource_start(binfo->pdev, binfo->current_bar) +
+               start - binfo->ioaddr;
+       res->end = res->start + resource_size - 1;
+       res->flags = IORESOURCE_MEM;
+       res->name = feature_name;
+
+       feature_platform_data_add(pdata, feature_id,
+                                 feature_name, feature_id, start);
+}
+
+struct feature_info {
+       const char *name;
+       resource_size_t resource_size;
+       int feature_index;
+};
+
+/* indexed by fme feature IDs which are defined in 'enum fme_feature_id'. */
+static struct feature_info fme_features[] = {
+       {
+               .name = FME_FEATURE_HEADER,
+               .resource_size = sizeof(struct feature_fme_header),
+               .feature_index = FME_FEATURE_ID_HEADER,
+       },
+       {
+               .name = FME_FEATURE_THERMAL_MGMT,
+               .resource_size = sizeof(struct feature_fme_thermal),
+               .feature_index = FME_FEATURE_ID_THERMAL_MGMT,
+       },
+       {
+               .name = FME_FEATURE_POWER_MGMT,
+               .resource_size = sizeof(struct feature_fme_power),
+               .feature_index = FME_FEATURE_ID_POWER_MGMT,
+       },
+       {
+               .name = FME_FEATURE_GLOBAL_PERF,
+               .resource_size = sizeof(struct feature_fme_gperf),
+               .feature_index = FME_FEATURE_ID_GLOBAL_PERF,
+       },
+       {
+               .name = FME_FEATURE_GLOBAL_ERR,
+               .resource_size = sizeof(struct feature_fme_err),
+               .feature_index = FME_FEATURE_ID_GLOBAL_ERR,
+       },
+       {
+               .name = FME_FEATURE_PR_MGMT,
+               .resource_size = sizeof(struct feature_fme_pr),
+               .feature_index = FME_FEATURE_ID_PR_MGMT,
+       }
+};
+
+/* indexed by port feature IDs which are defined in 'enum port_feature_id'. */
+static struct feature_info port_features[] = {
+       {
+               .name = PORT_FEATURE_HEADER,
+               .resource_size = sizeof(struct feature_port_header),
+               .feature_index = PORT_FEATURE_ID_HEADER,
+       },
+       {
+               .name = PORT_FEATURE_ERR,
+               .resource_size = sizeof(struct feature_port_error),
+               .feature_index = PORT_FEATURE_ID_ERROR,
+       },
+       {
+               .name = PORT_FEATURE_UMSG,
+               .resource_size = sizeof(struct feature_port_umsg),
+               .feature_index = PORT_FEATURE_ID_UMSG,
+       },
+       {
+               /* This feature isn't available for now */
+               .name = PORT_FEATURE_PR,
+               .resource_size = 0,
+               .feature_index = PORT_FEATURE_ID_PR,
+       },
+       {
+               .name = PORT_FEATURE_STP,
+               .resource_size = sizeof(struct feature_port_stp),
+               .feature_index = PORT_FEATURE_ID_STP,
+       },
+       {
+               /*
+                * For User AFU feature, its region size is not fixed, but
+                * reported by register PortCapability.mmio_size. Resource
+                * size of UAFU will be set while parse port device.
+                */
+               .name = PORT_FEATURE_UAFU,
+               .resource_size = 0,
+               .feature_index = PORT_FEATURE_ID_UAFU,
+       },
+};
+
+static int

drivers/fpga/intel-pcie.c line 514

Quoted 10 lines
drivers/fpga/intel-pcie.c +10 -0
+create_feature_instance(struct build_feature_devs_info *binfo,
+                       void __iomem *start, struct feature_info *finfo)
+{
+       if (binfo->ioend - start < finfo->resource_size)
+               return -EINVAL;
+
+       build_info_add_sub_feature(binfo, finfo->feature_index, finfo->name,
+                                  finfo->resource_size, start);
+       return 0;
+}

build_info_add_sub_feature() is only called one time - here. Could you collapse these two functions together?

Quoted 196 lines
drivers/fpga/intel-pcie.c +196 -0
+
+static int parse_feature_fme(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&binfo->pdev->dev);
+       int ret;
+
+       ret = build_info_create_dev(binfo, FME_ID, fme_feature_num(),
+                                       FPGA_FEATURE_DEV_FME);
+       if (ret)
+               return ret;
+
+       if (drvdata->fme_dev) {
+               dev_err(&binfo->pdev->dev, "Multiple FMEs are detected.\n");
+               return -EINVAL;
+       }
+
+       return create_feature_instance(binfo, start,
+                                      &fme_features[FME_FEATURE_ID_HEADER]);
+}
+
+static int parse_feature_fme_private(struct build_feature_devs_info *binfo,
+                                    struct feature_header *hdr)
+{
+       struct feature_header header;
+
+       header.csr = readq(hdr);
+
+       if (header.id >= ARRAY_SIZE(fme_features)) {
+               dev_info(&binfo->pdev->dev, "FME feature id %x is not supported yet.\n",
+                        header.id);
+               return 0;
+       }
+
+       return create_feature_instance(binfo, hdr, &fme_features[header.id]);
+}
+
+static int parse_feature_port(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       int ret;
+
+       ret = build_info_create_dev(binfo, PORT_ID, port_feature_num(),
+                                       FPGA_FEATURE_DEV_PORT);
+       if (ret)
+               return ret;
+
+       return create_feature_instance(binfo, start,
+                                      &port_features[PORT_FEATURE_ID_HEADER]);
+}
+
+static void enable_port_uafu(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+       struct feature_port_header *port_hdr;
+       struct feature_port_capability capability;
+
+       port_hdr = (struct feature_port_header *)start;
+       capability.csr = readq(&port_hdr->capability);
+       port_features[id].resource_size = capability.mmio_size << 10;
+
+       /*
+        * To enable User AFU, driver needs to clear reset bit on related port,
+        * otherwise the mmio space of this user AFU will be invalid.
+        */
+       if (port_features[id].resource_size)
+               fpga_port_reset(binfo->feature_dev);
+}
+
+static int parse_feature_port_private(struct build_feature_devs_info *binfo,
+                                     struct feature_header *hdr)
+{
+       struct feature_header header;
+       enum port_feature_id id;
+
+       header.csr = readq(hdr);
+       /*
+        * the region of port feature id is [0x10, 0x13], + 1 to reserve 0
+        * which is dedicated for port-hdr.
+        */
+       id = (header.id & 0x000f) + 1;
+
+       if (id >= ARRAY_SIZE(port_features)) {
+               dev_info(&binfo->pdev->dev, "Port feature id %x is not supported yet.\n",
+                        header.id);
+               return 0;
+       }
+
+       return create_feature_instance(binfo, hdr, &port_features[id]);
+}
+
+static int parse_feature_port_uafu(struct build_feature_devs_info *binfo,
+                                struct feature_header *hdr)
+{
+       enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+       int ret;
+
+       if (port_features[id].resource_size) {
+               ret = create_feature_instance(binfo, hdr, &port_features[id]);
+               port_features[id].resource_size = 0;
+       } else {
+               dev_err(&binfo->pdev->dev, "the uafu feature header is mis-configured.\n");
+               ret = -EINVAL;
+       }
+
+       return ret;
+}
+
+static int parse_feature_afus(struct build_feature_devs_info *binfo,
+                             struct feature_header *hdr)
+{
+       int ret;
+       struct feature_afu_header *afu_hdr, header;
+       void __iomem *start;
+       void __iomem *end = binfo->ioend;
+
+       start = hdr;
+       for (; start < end; start += header.next_afu) {
+               if (end - start < (sizeof(*afu_hdr) + sizeof(*hdr)))
+                       return -EINVAL;
+
+               hdr = start;
+               afu_hdr = (struct feature_afu_header *) (hdr + 1);
+               header.csr = readq(&afu_hdr->csr);
+
+               if (feature_is_fme(afu_hdr)) {
+                       ret = parse_feature_fme(binfo, hdr);
+                       binfo->pfme_hdr = hdr;
+                       if (ret)
+                               return ret;
+               } else if (feature_is_port(afu_hdr)) {
+                       ret = parse_feature_port(binfo, hdr);
+                       enable_port_uafu(binfo, hdr);
+                       if (ret)
+                               return ret;
+               } else if (feature_is_UAFU(binfo)) {
+                       ret = parse_feature_port_uafu(binfo, hdr);
+                       if (ret)
+                               return ret;
+               } else
+                       dev_info(&binfo->pdev->dev, "AFU GUID %pUl is not supported yet.\n",
+                                afu_hdr->guid.b);
+
+               if (!header.next_afu)
+                       break;
+       }
+
+       return 0;
+}
+
+static int parse_feature_private(struct build_feature_devs_info *binfo,
+                                struct feature_header *hdr)
+{
+       struct feature_header header;
+
+       header.csr = readq(hdr);
+
+       if (!binfo->feature_dev) {
+               dev_err(&binfo->pdev->dev, "the private feature %x does not belong to any AFU.\n",
+                       header.id);
+               return -EINVAL;
+       }
+
+       switch (feature_dev_id_type(binfo->feature_dev)) {
+       case FME_ID:
+               return parse_feature_fme_private(binfo, hdr);
+       case PORT_ID:
+               return parse_feature_port_private(binfo, hdr);
+       default:
+               dev_info(&binfo->pdev->dev, "private feature %x belonging to AFU %s is not supported yet.\n",
+                        header.id, binfo->feature_dev->name);
+       }
+       return 0;
+}
+
+static int parse_feature(struct build_feature_devs_info *binfo,
+                        struct feature_header *hdr)
+{
+       struct feature_header header;
+       int ret = 0;
+
+       header.csr = readq(hdr);
+
+       switch (header.type) {
+       case FEATURE_TYPE_AFU:
+               ret = parse_feature_afus(binfo, hdr);
+               break;
+       case FEATURE_TYPE_PRIVATE:
+               ret = parse_feature_private(binfo, hdr);
+               break;
+       default:
+               dev_info(&binfo->pdev->dev,
+                        "Feature Type %x is not supported.\n", hdr->type);
+       };
+

drivers/fpga/intel-pcie.c line 720

Quoted 10 lines
drivers/fpga/intel-pcie.c +10 -0
+       return ret;
+}
+
+static int
+parse_feature_list(struct build_feature_devs_info *binfo, void __iomem *start)
+{
+       struct feature_header *hdr, header;
+       void __iomem *end = binfo->ioend;
+       int ret = 0;
+

Maybe a helpful comment that we are stepping through a linked list of features.

drivers/fpga/intel-pcie.c line 731

Quoted 11 lines
drivers/fpga/intel-pcie.c +11 -0
+       for (; start < end; start += header.next_header_offset) {
+               if (end - start < sizeof(*hdr)) {
+                       dev_err(&binfo->pdev->dev, "The region is too small to contain a feature.\n");
+                       ret =  -EINVAL;
+                       break;
+               }
+
+               hdr = (struct feature_header *)start;
+               ret = parse_feature(binfo, hdr);
+               if (ret)
+                       break;

Instead of parse_feature, this can save off the enumeration info and continue to read in the linked list. After all the headers are read in, call the (separate) enumeration code to step through the saved headers, parse them, and create the devices. Since the memory is iomapped during the process of reading in the headers, the enumeration code doesn't have to be so pcie specific. Then this code base is better set to run on embedded devices also.

Quoted 89 lines
drivers/fpga/intel-pcie.c +89 -0
+
+               header.csr = readq(hdr);
+               if (!header.next_header_offset)
+                       break;
+       }
+
+       return ret;
+}
+
+static int parse_ports_from_fme(struct build_feature_devs_info *binfo)
+{
+       struct feature_fme_header *fme_hdr;
+       struct feature_fme_port port;
+       int i = 0, ret = 0;
+
+       if (binfo->pfme_hdr == NULL) {
+               dev_dbg(&binfo->pdev->dev, "VF is detected.\n");
+               return ret;
+       }
+
+       fme_hdr = binfo->pfme_hdr;
+
+       do {
+               port.csr = readq(&fme_hdr->port[i]);
+               if (!port.port_implemented)
+                       break;
+
+               ret = parse_switch_to(binfo, port.port_bar);
+               if (ret)
+                       break;
+
+               ret = parse_feature_list(binfo,
+                               binfo->ioaddr + port.port_offset);
+               if (ret)
+                       break;
+       } while (++i < MAX_FPGA_PORT_NUM);
+
+       return ret;
+}
+
+static int create_init_drvdata(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata;
+
+       drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
+       if (!drvdata)
+               return -ENOMEM;
+
+       mutex_init(&drvdata->lock);
+       INIT_LIST_HEAD(&drvdata->port_dev_list);
+       INIT_LIST_HEAD(&drvdata->regions);
+
+       dev_set_drvdata(&pdev->dev, drvdata);
+       return 0;
+}
+
+static void destroy_drvdata(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+
+       if (drvdata->fme_dev) {
+               /* fme device should be unregistered first. */
+               WARN_ON(device_is_registered(drvdata->fme_dev));
+               free_fpga_id(FME_ID, to_platform_device(drvdata->fme_dev)->id);
+               put_device(drvdata->fme_dev);
+       }
+
+       cci_pci_remove_port_devs(pdev);
+       cci_pci_release_regions(pdev);
+       dev_set_drvdata(&pdev->dev, NULL);
+       devm_kfree(&pdev->dev, drvdata);
+}
+
+static int cci_pci_create_feature_devs(struct pci_dev *pdev)
+{
+       struct build_feature_devs_info *binfo;
+       int ret;
+
+       binfo = build_info_alloc_and_init(pdev);
+       if (!binfo)
+               return -ENOMEM;
+
+       binfo->parent_dev = fpga_dev_create(&pdev->dev, INTEL_FPGA_DEV);
+       if (IS_ERR(binfo->parent_dev)) {
+               ret = PTR_ERR(binfo->parent_dev);
+               goto free_binfo_exit;
+       }
+
+       ret = parse_start(binfo);

drivers/fpga/intel-pcie.c line 830

Quoted 10 lines
drivers/fpga/intel-pcie.c +10 -0
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = parse_feature_list(binfo, binfo->ioaddr);
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = parse_ports_from_fme(binfo);
+       if (ret)
+               goto free_binfo_exit;

So ideally, there would be a function call here that read all the headers from hardware, ioremapping as it went along. Then after that, call the enumeration code to create the devices.

Quoted 76 lines
drivers/fpga/intel-pcie.c +38 -3
+
+       ret = build_info_commit_dev(binfo);
+       if (ret)
+               goto free_binfo_exit;
+
+       /*
+        * everything is okay, reset ->parent_dev to stop it being
+        * freed by build_info_free()
+        */
+       binfo->parent_dev = NULL;
+
+free_binfo_exit:
+       build_info_free(binfo);
+       return ret;
+}
+
 /* PCI Device ID */
 #define PCIe_DEVICE_ID_PF_INT_5_X      0xBCBD
 #define PCIe_DEVICE_ID_PF_INT_6_X      0xBCC0
@@ -81,9 +898,18 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
                goto release_region_exit;
        }
 
-       /* TODO: create and add the platform device per feature list */
+       ret = create_init_drvdata(pcidev);
+       if (ret)
+               goto release_region_exit;
+
+       ret = cci_pci_create_feature_devs(pcidev);
+       if (ret)
+               goto destroy_drvdata_exit;
+
        return 0;
 
+destroy_drvdata_exit:
+       destroy_drvdata(pcidev);
 release_region_exit:
        pci_release_regions(pcidev);
 disable_error_report_exit:
@@ -94,6 +920,8 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
 
 static void cci_pci_remove(struct pci_dev *pcidev)
 {
+       remove_all_devs(pcidev);
+       destroy_drvdata(pcidev);
        pci_release_regions(pcidev);
        pci_disable_pcie_error_reporting(pcidev);
        pci_disable_device(pcidev);
@@ -108,14 +936,23 @@ static void cci_pci_remove(struct pci_dev *pcidev)
 
 static int __init ccidrv_init(void)
 {
+       int ret;
+
        pr_info("Intel(R) FPGA PCIe Driver: Version %s\n", DRV_VERSION);
 
-       return pci_register_driver(&cci_pci_driver);
+       fpga_ids_init();
+
+       ret = pci_register_driver(&cci_pci_driver);
+       if (ret)
+               fpga_ids_destroy();
+
+       return ret;
 }
 
 static void __exit ccidrv_exit(void)
 {
        pci_unregister_driver(&cci_pci_driver);
+       fpga_ids_destroy();
 }
 
 module_init(ccidrv_init);
--

1.8.3.1

#44 lore raw

Re: [PATCH v2 07/22] fpga: intel: pcie: parse feature list and create platform device for features.

)On Wed, Sep 20, 2017 at 4:24 PM, Alan Tull <[email protected]> wrote:

Hi Hao,

A few more minor things below.

Quoted 113 lines

a (wh., *()On Sun, Jun 25, 2017 at 8:52 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

I'm done with some board bringup so I have time to look at your patchset again.

Something I can't help but notice is that this patchset will be almost completely reusable for embedded FPGAs if the enumeration is separated from the pcie code. After the devices are created, they are just mmio devices. That makes this whole scheme available for embedded FPGAs.

The division of things would be that the pcie code would read the headers and do ioremapping. Then pass the headers to the enumeration code to create the devices. With that division, another platform that is embedded could have its own code that read the headers and did the iomapping and then used the separate enumeration code.

Going through intel-pcie.c, the truly pcie specific parts are already pretty well contained. The pcie code is in probe, cci_pci_ioremap_bar, cci_pci_release_regions, and the 'parse_s*' functions that ultimately call cci_pci_ioremap_bar. So it all comes down to the code that calls the 'parse_s*' functions. That is, the code the reads the fme header and the port headers. Currently, if I'm reading this right, this code ioremaps each region, reads its header, and creates its device before reading the next header. So we just need to change the order a little and ioremap/read all the headers before starting to create devices. That separates the pci ioremap from creating the devices. So the probe function could ioremap and read in the fme header. Then it would ioremap and read in each of the port headers. After they are read in, then pass the all these headers to the separate enumeration code that will parse it and create the devices. That part is not pcie specific, right? With the enumeration code in a separate file (and assuming that the ioremap has already happened), another platform that is embedded could have its probe do the iomapping and reading the headers, and then call the same enumeration code.

Actually, there's a bit more to it than that but I think this is doable. build_info_add_sub_feature() (which is called by create_feature_instance) saves off resource info for each sub device. Does this mean this code is ioremapping again (in intel-fpga-fme-mgr.c's probe)?

A few more things below...

Quoted 68 lines

From: Xiao Guangrong <[email protected]>

Device Feature List structure creates a link list of feature headers within the MMIO space to provide an extensible way of adding features.

The Intel FPGA PCIe driver walks through the feature headers to enumerate feature devices, FPGA Management Engine (FME) and FPGA Port for Accelerated Function Unit (AFU), and their private sub features. For feature devices, it creates the platform devices and linked the private sub features into their platform data.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed comments from Moritz Fischer.
fixed kbuild warning, typos and clean up the code.

---
 drivers/fpga/Makefile            |   2 +-
 drivers/fpga/intel-feature-dev.c | 130 ++++++
 drivers/fpga/intel-feature-dev.h | 341 ++++++++++++++++
 drivers/fpga/intel-pcie.c        | 841 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 1311 insertions(+), 3 deletions(-)
 create mode 100644 drivers/fpga/intel-feature-dev.c
 create mode 100644 drivers/fpga/intel-feature-dev.h
drivers/fpga/Makefile +1 -1
index 5613133..ad24b3d 100644
@@ -31,4 +31,4 @@ obj-$(CONFIG_OF_FPGA_REGION)          += of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)           += intel-fpga-pci.o
 
-intel-fpga-pci-objs := intel-pcie.o
+intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
drivers/fpga/intel-feature-dev.c new file +19 -0
new file mode 100644
index 0000000..68f9cba
@@ -0,0 +1,130 @@
+/*
+ * Intel FPGA Feature Device Driver
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "intel-feature-dev.h"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                              int index, const char *name,
Quoted 8 lines

drivers/fpga/intel-feature-dev.c line 27

Quoted 8 lines
drivers/fpga/intel-feature-dev.c new file +8 -0
+                              int resource_index, void __iomem *ioaddr)
+{
+       WARN_ON(index >= pdata->num);
+
+       pdata->features[index].name = name;
+       pdata->features[index].resource_index = resource_index;
+       pdata->features[index].ioaddr = ioaddr;
+}

This function is only called once. I understand that it is desirable to break things up into little functions and avoid overly long functions, but in this case, the calling function
(build_info_add_sub_feature) is pretty short to begin with. Someone
reading the code will know what is happening better if you just add its contents to its calling function.

Quoted 7 lines
Quoted 7 lines
drivers/fpga/intel-feature-dev.c new file +7 -0
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num)
+{
+       struct feature_platform_data *pdata;
+
+       pdata = kzalloc(feature_platform_data_size(num), GFP_KERNEL);
Quoted 8 lines

drivers/fpga/intel-feature-dev.c line 42

Quoted 8 lines
drivers/fpga/intel-feature-dev.c new file +8 -0
+       if (pdata) {
+               pdata->dev = dev;
+               pdata->num = num;
+               mutex_init(&pdata->lock);
+       }
+
+       return pdata;
+}

feature_platform_data_alloc_and_init is another function that's small, called once, and would be better included in its caller.

Quoted 1,377 lines

drivers/fpga/intel-feature-dev.c line 52

Quoted 10 lines
drivers/fpga/intel-feature-dev.c new file +10 -0
+
+int fme_feature_num(void)
+{
+       return FME_FEATURE_ID_MAX;
+}
+
+int port_feature_num(void)
+{
+       return PORT_FEATURE_ID_MAX;
+}

Do these need to be functions? If so, static. But if you can just use the #define'd values that's one level of indirection we can get rid of. And when I'm going through code where there's extra levels of indirection that don't do anything, it makes it harder to understand ;)

Quoted 4 lines
drivers/fpga/intel-feature-dev.c new file +4 -0
+
+int fpga_port_id(struct platform_device *pdev)
+{
+       struct feature_port_header *port_hdr;

drivers/fpga/intel-feature-dev.c line 66

Quoted 10 lines
drivers/fpga/intel-feature-dev.c new file +10 -0
+       struct feature_port_capability capability;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       capability.csr = readq(&port_hdr->capability);
+       return capability.port_number;
+}
+EXPORT_SYMBOL_GPL(fpga_port_id);

This is here because every feature is a port, right?

Quoted 594 lines
drivers/fpga/intel-feature-dev.c new file +64 -0
+
+/*
+ * Enable Port by clear the port soft reset bit, which is set by default.
+ * The User AFU is unable to respond to any MMIO access while in reset.
+ * __fpga_port_enable function should only be used after __fpga_port_disable
+ * function.
+ */
+void __fpga_port_enable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       WARN_ON(!pdata->disable_count);
+
+       if (--pdata->disable_count != 0)
+               return;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x0;
+       writeq(control.csr, &port_hdr->control);
+}
+EXPORT_SYMBOL_GPL(__fpga_port_enable);
+
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
+int __fpga_port_disable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature_port_header *port_hdr;
+       struct feature_port_control control;
+
+       if (pdata->disable_count++ != 0)
+               return 0;
+
+       port_hdr = get_feature_ioaddr_by_index(&pdev->dev,
+                                              PORT_FEATURE_ID_HEADER);
+       WARN_ON(!port_hdr);
+
+       /* Set port soft reset */
+       control.csr = readq(&port_hdr->control);
+       control.port_sftrst = 0x1;
+       writeq(control.csr, &port_hdr->control);
+
+       /*
+        * HW sets ack bit to 1 when all outstanding requests have been drained
+        * on this port and minimum soft reset pulse width has elapsed.
+        * Driver polls port_soft_reset_ack to determine if reset done by HW.
+        */
+       if (readq_poll_timeout(&port_hdr->control, control.csr,
+                              (control.port_sftrst_ack == 1),
+                              RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+               dev_err(&pdev->dev, "timeout, fail to reset device\n");
+               return -ETIMEDOUT;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(__fpga_port_disable);
drivers/fpga/intel-feature-dev.h new file +341 -0
new file mode 100644
index 0000000..f67784a
@@ -0,0 +1,341 @@
+/*
+ * Intel FPGA Feature Device Driver Header File
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Zhang Yi <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef __INTEL_FPGA_FEATURE_H
+#define __INTEL_FPGA_FEATURE_H
+
+#include <linux/fs.h>
+#include <linux/pci.h>
+#include <linux/uuid.h>
+#include <linux/delay.h>
+#include <linux/iopoll.h>
+#include <linux/platform_device.h>
+
+#ifndef readq
+static inline u64 readq(void __iomem *addr)
+{
+       return readl(addr) + ((u64)readl(addr + 4) << 32);
+}
+#endif
+
+#ifndef writeq
+static inline void writeq(u64 val, void __iomem *addr)
+{
+       writel((u32) (val), addr);
+       writel((u32) (val >> 32), (addr + 4));
+}
+#endif
+
+/* maximum supported number of ports */
+#define MAX_FPGA_PORT_NUM 4
+/* plus one for fme device */
+#define MAX_FEATURE_DEV_NUM    (MAX_FPGA_PORT_NUM + 1)
+
+#define FME_FEATURE_HEADER          "fme_hdr"
+#define FME_FEATURE_THERMAL_MGMT    "fme_thermal"
+#define FME_FEATURE_POWER_MGMT      "fme_power"
+#define FME_FEATURE_GLOBAL_PERF     "fme_gperf"
+#define FME_FEATURE_GLOBAL_ERR      "fme_error"
+#define FME_FEATURE_PR_MGMT         "fme_pr"
+
+#define PORT_FEATURE_HEADER         "port_hdr"
+#define PORT_FEATURE_UAFU           "port_uafu"
+#define PORT_FEATURE_ERR            "port_err"
+#define PORT_FEATURE_UMSG           "port_umsg"
+#define PORT_FEATURE_PR             "port_pr"
+#define PORT_FEATURE_STP            "port_stp"
+
+/* All headers and structures must be byte-packed to match the spec. */
+#pragma pack(1)
+
+/* common header for all features */
+struct feature_header {
+       union {
+               u64 csr;
+               struct {
+                       u64 id:12;
+                       u64 revision:4;
+                       u64 next_header_offset:24; /* offset to next header */
+                       u64 rsvdz:20;
+                       u64 type:4;                /* feature type */
+#define FEATURE_TYPE_AFU               0x1
+#define FEATURE_TYPE_PRIVATE           0x3
+               };
+       };
+};
+
+/* common header for non-private features */
+struct feature_afu_header {
+       uuid_le guid;
+       union {
+               u64 csr;
+               struct {
+                       u64 next_afu:24;        /* pointer to next afu header */
+                       u64 rsvdz:40;
+               };
+       };
+};
+
+/* FME Header Register Set */
+/* FME Capability Register */
+struct feature_fme_capability {
+       union {
+               u64 csr;
+               struct {
+                       u64 fabric_verid:8;     /* Fabric version ID */
+                       u64 socket_id:1;        /* Socket id */
+                       u64 rsvdz1:3;
+                       u64 pcie0_link_avl:1;   /* PCIe0 link availability */
+                       u64 pcie1_link_avl:1;   /* PCIe1 link availability */
+                       u64 coherent_link_avl:1;/* Coherent link availability */
+                       u64 rsvdz2:1;
+                       u64 iommu_support:1;    /* IOMMU or VT-d supported */
+                       u64 num_ports:3;        /* Num of ports implemented */
+                       u64 rsvdz3:4;
+                       u64 addr_width_bits:6;  /* Address width supported */
+                       u64 rsvdz4:2;
+                       u64 cache_size:12;      /* Cache size in kb */
+                       u64 cache_assoc:4;      /* Cache Associativity */
+                       u64 rsvdz5:15;
+                       u64 lock_bit:1;         /* Latched lock bit by BIOS */
+               };
+       };
+};
+
+/* FME Port Offset Register */
+struct feature_fme_port {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_offset:24;     /* Offset to port header */
+                       u64 rsvdz1:8;
+                       u64 port_bar:3;         /* Bar id */
+                       u64 rsvdz2:20;
+                       u64 afu_access_ctrl:1;  /* AFU access type: PF/VF */
+                       u64 rsvdz3:4;
+                       u64 port_implemented:1; /* Port implemented or not */
+                       u64 rsvdz4:3;
+               };
+       };
+};
+
+struct feature_fme_header {
+       struct feature_header header;
+       struct feature_afu_header afu_header;
+       u64 rsvd[2];
+       struct feature_fme_capability capability;
+       struct feature_fme_port port[MAX_FPGA_PORT_NUM];
+};
+
+/* FME Thermal Sub Feature Register Set */
+struct feature_fme_thermal {
+       struct feature_header header;
+};
+
+/* FME Power Sub Feature Register Set */
+struct feature_fme_power {
+       struct feature_header header;
+};
+
+/* FME Global Performance Sub Feature Register Set */
+struct feature_fme_gperf {
+       struct feature_header header;
+};
+
+/* FME Error Sub Feature Register Set */
+struct feature_fme_err {
+       struct feature_header header;
+};
+
+/* FME Partial Reconfiguration Sub Feature Register Set */
+struct feature_fme_pr {
+       struct feature_header header;
+};
+
+/* PORT Header Register Set */
+/* Port Capability Register */
+struct feature_port_capability {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_number:2;      /* Port Number 0-3 */
+                       u64 rsvdz1:6;
+                       u64 mmio_size:16;       /* User MMIO size in KB */
+                       u64 rsvdz2:8;
+                       u64 sp_intr_num:4;      /* Supported interrupts num */
+                       u64 rsvdz3:28;
+               };
+       };
+};
+
+/* Port Control Register */
+struct feature_port_control {
+       union {
+               u64 csr;
+               struct {
+                       u64 port_sftrst:1;      /* Port Soft Reset */
+                       u64 rsvdz1:1;
+                       u64 latency_tolerance:1;/* '1' >= 40us, '0' < 40us */
+                       u64 rsvdz2:1;
+                       u64 port_sftrst_ack:1;  /* HW ACK for Soft Reset */
+                       u64 rsvdz3:59;
+               };
+       };
+};
+
+struct feature_port_header {
+       struct feature_header header;
+       struct feature_afu_header afu_header;
+       u64 rsvd[2];
+       struct feature_port_capability capability;
+       struct feature_port_control control;
+};
+
+/* PORT Error Sub Feature Register Set */
+struct feature_port_error {
+       struct feature_header header;
+};
+
+/* PORT Unordered Message Sub Feature Register Set */
+struct feature_port_umsg {
+       struct feature_header header;
+};
+
+/* PORT SignalTap Sub Feature Register Set */
+struct feature_port_stp {
+       struct feature_header header;
+};
+
+#pragma pack()
+
+struct feature {
+       const char *name;
+       int resource_index;
+       void __iomem *ioaddr;
+};
+
+struct feature_platform_data {
+       /* list the feature dev to cci_drvdata->port_dev_list. */
+       struct list_head node;
+       struct mutex lock;
+       struct platform_device *dev;
+       unsigned int disable_count;     /* count for port disable */
+
+       int num;                        /* number of features */
+       struct feature features[0];
+};
+
+enum fme_feature_id {
+       FME_FEATURE_ID_HEADER = 0x0,
+       FME_FEATURE_ID_THERMAL_MGMT = 0x1,
+       FME_FEATURE_ID_POWER_MGMT = 0x2,
+       FME_FEATURE_ID_GLOBAL_PERF = 0x3,
+       FME_FEATURE_ID_GLOBAL_ERR = 0x4,
+       FME_FEATURE_ID_PR_MGMT = 0x5,
+       FME_FEATURE_ID_MAX = 0x6,
+};
+
+enum port_feature_id {
+       PORT_FEATURE_ID_HEADER = 0x0,
+       PORT_FEATURE_ID_ERROR = 0x1,
+       PORT_FEATURE_ID_UMSG = 0x2,
+       PORT_FEATURE_ID_PR = 0x3,
+       PORT_FEATURE_ID_STP = 0x4,
+       PORT_FEATURE_ID_UAFU = 0x5,
+       PORT_FEATURE_ID_MAX = 0x6,
+};
+
+int fme_feature_num(void);
+int port_feature_num(void);
+
+#define FPGA_FEATURE_DEV_FME           "intel-fpga-fme"
+#define FPGA_FEATURE_DEV_PORT          "intel-fpga-port"
+
+void feature_platform_data_add(struct feature_platform_data *pdata,
+                              int index, const char *name,
+                              int resource_index, void __iomem *ioaddr);
+
+static inline int feature_platform_data_size(const int num)
+{
+       return sizeof(struct feature_platform_data) +
+               num * sizeof(struct feature);
+}
+
+struct feature_platform_data *
+feature_platform_data_alloc_and_init(struct platform_device *dev, int num);
+
+int fpga_port_id(struct platform_device *pdev);
+
+static inline int fpga_port_check_id(struct platform_device *pdev,
+                                    void *pport_id)
+{
+       return fpga_port_id(pdev) == *(int *)pport_id;
+}
+
+void __fpga_port_enable(struct platform_device *pdev);
+int __fpga_port_disable(struct platform_device *pdev);
+
+static inline void fpga_port_enable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+       mutex_lock(&pdata->lock);
+       __fpga_port_enable(pdev);
+       mutex_unlock(&pdata->lock);
+}
+
+static inline int fpga_port_disable(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       int ret;
+
+       mutex_lock(&pdata->lock);
+       ret = __fpga_port_disable(pdev);
+       mutex_unlock(&pdata->lock);
+
+       return ret;
+}
+
+static inline int __fpga_port_reset(struct platform_device *pdev)
+{
+       int ret;
+
+       ret = __fpga_port_disable(pdev);
+       if (ret)
+               return ret;
+
+       __fpga_port_enable(pdev);
+       return 0;
+}
+
+static inline int fpga_port_reset(struct platform_device *pdev)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       int ret;
+
+       mutex_lock(&pdata->lock);
+       ret = __fpga_port_reset(pdev);
+       mutex_unlock(&pdata->lock);
+       return ret;
+}
+
+static inline void __iomem *
+get_feature_ioaddr_by_index(struct device *dev, int index)
+{
+       struct feature_platform_data *pdata = dev_get_platdata(dev);
+
+       return pdata->features[index].ioaddr;
+}
+#endif
drivers/fpga/intel-pcie.c +171 -0
index f697de4..70b8284 100644
@@ -23,10 +23,827 @@
 #include <linux/stddef.h>
 #include <linux/errno.h>
 #include <linux/aer.h>
+#include <linux/fpga/fpga-dev.h>
+
+#include "intel-feature-dev.h"
 
 #define DRV_VERSION    "0.8"
 #define DRV_NAME       "intel-fpga-pci"
 
+#define INTEL_FPGA_DEV "intel-fpga-dev"
+
+static DEFINE_MUTEX(fpga_id_mutex);
+
+enum fpga_id_type {
+       FME_ID,         /* fme id allocation and mapping */
+       PORT_ID,        /* port id allocation and mapping */
+       FPGA_ID_MAX,
+};
+
+/* it is protected by fpga_id_mutex */
+static struct idr fpga_ids[FPGA_ID_MAX];
+
+struct cci_drvdata {
+       struct device *fme_dev;
+
+       struct mutex lock;
+       struct list_head port_dev_list;
+
+       struct list_head regions; /* global list of pci bar mapping region */
+};
+
+/* pci bar mapping info */
+struct cci_pci_region {
+       int bar;
+       void __iomem *ioaddr;   /* pointer to mapped bar region */
+       struct list_head node;
+};
+
+static void fpga_ids_init(void)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+               idr_init(fpga_ids + i);
+}
+
+static void fpga_ids_destroy(void)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(fpga_ids); i++)
+               idr_destroy(fpga_ids + i);
+}
+
+static int alloc_fpga_id(enum fpga_id_type type, struct device *dev)
+{
+       int id;
+
+       WARN_ON(type >= FPGA_ID_MAX);
+       mutex_lock(&fpga_id_mutex);
+       id = idr_alloc(fpga_ids + type, dev, 0, 0, GFP_KERNEL);
+       mutex_unlock(&fpga_id_mutex);
+       return id;
+}
+
+static void free_fpga_id(enum fpga_id_type type, int id)
+{
+       WARN_ON(type >= FPGA_ID_MAX);
+       mutex_lock(&fpga_id_mutex);
+       idr_remove(fpga_ids + type, id);
+       mutex_unlock(&fpga_id_mutex);
+}
+
+static void cci_pci_add_port_dev(struct pci_dev *pdev,
+                                struct platform_device *port_dev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct feature_platform_data *pdata = dev_get_platdata(&port_dev->dev);
+
+       mutex_lock(&drvdata->lock);
+       list_add(&pdata->node, &drvdata->port_dev_list);
+       get_device(&pdata->dev->dev);
+       mutex_unlock(&drvdata->lock);
+}
+
+static void cci_pci_remove_port_devs(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct feature_platform_data *pdata, *ptmp;
+
+       mutex_lock(&drvdata->lock);
+       list_for_each_entry_safe(pdata, ptmp, &drvdata->port_dev_list, node) {
+               struct platform_device *port_dev = pdata->dev;
+
+               /* the port should be unregistered first. */
+               WARN_ON(device_is_registered(&port_dev->dev));
+               list_del(&pdata->node);
+               free_fpga_id(PORT_ID, port_dev->id);
+               put_device(&port_dev->dev);
+       }
+       mutex_unlock(&drvdata->lock);
+}
+
+/* info collection during feature dev build. */
+struct build_feature_devs_info {
+       struct pci_dev *pdev;
+
+       /*
+        * PCI BAR mapping info. Parsing feature list starts from
+        * BAR 0 and switch to different BARs to parse Port
+        */
+       void __iomem *ioaddr;
+       void __iomem *ioend;
+       int current_bar;
+
+       /* points to FME header where the port offset is figured out. */
+       void __iomem *pfme_hdr;
+
+       /* the container device for all feature devices */
+       struct fpga_dev *parent_dev;
+
+       /* current feature device */
+       struct platform_device *feature_dev;
+};
+
+static void cci_pci_release_regions(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct cci_pci_region *tmp, *region;
+
+       list_for_each_entry_safe(region, tmp, &drvdata->regions, node) {
+               list_del(&region->node);
+               if (region->ioaddr)
+                       pci_iounmap(pdev, region->ioaddr);
+               devm_kfree(&pdev->dev, region);
+       }
+}
+
+static void __iomem *cci_pci_ioremap_bar(struct pci_dev *pdev, int bar)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+       struct cci_pci_region *region;
+
+       list_for_each_entry(region, &drvdata->regions, node)
+               if (region->bar == bar) {
+                       dev_dbg(&pdev->dev, "BAR %d region exists\n", bar);
+                       return region->ioaddr;
+               }
+
+       region = devm_kzalloc(&pdev->dev, sizeof(*region), GFP_KERNEL);
+       if (!region)
+               return NULL;
+
+       region->bar = bar;
+       region->ioaddr = pci_ioremap_bar(pdev, bar);
+       if (!region->ioaddr) {
+               dev_err(&pdev->dev, "can't ioremap memory from BAR %d.\n", bar);
+               devm_kfree(&pdev->dev, region);
+               return NULL;
+       }
+
+       list_add(&region->node, &drvdata->regions);
+       return region->ioaddr;
+}
+
+static int parse_start_from(struct build_feature_devs_info *binfo, int bar)
+{
+       binfo->ioaddr = cci_pci_ioremap_bar(binfo->pdev, bar);
+       if (!binfo->ioaddr)
+               return -ENOMEM;
+
+       binfo->current_bar = bar;
+       binfo->ioend = binfo->ioaddr + pci_resource_len(binfo->pdev, bar);
+       return 0;
+}
+
+static int parse_start(struct build_feature_devs_info *binfo)

drivers/fpga/intel-pcie.c line 210

Quoted 10 lines
drivers/fpga/intel-pcie.c +10 -0
+{
+       /* fpga feature list starts from BAR 0 */
+       return parse_start_from(binfo, 0);
+}
+
+/* switch the memory mapping to BAR# @bar */
+static int parse_switch_to(struct build_feature_devs_info *binfo, int bar)
+{
+       return parse_start_from(binfo, bar);
+}

parse_switch_to() and parse_start() are both one line wrappers for parse_start_from() and are only called once. :-) Please just use parse_start_from and get rid of parse_start and parse_switch_to. I don't think this code is all that super complicated. But as I'm grepping through this code, I'm trying to see all the places that do the same thing; if I have to look for multiple function names, it takes longer to get what is going on here.

Actually, cci_pci_ioremap_bar is only called in one place - parse_start_from - which really just adds two lines of code. parse_start_from could go away. Just a suggestion. The reason this comes up is that cci_pci_ioremap_bar is the more descriptive function name. The parse_start/switch function names hide the biggest thing they do, which is ioremapping. They don't actually do any parsing ;)

drivers/fpga/intel-pcie.c line 219

Quoted 9 lines
drivers/fpga/intel-pcie.c +9 -0
+
+static struct build_feature_devs_info *
+build_info_alloc_and_init(struct pci_dev *pdev)
+{
+       struct build_feature_devs_info *binfo;
+
+       binfo = devm_kzalloc(&pdev->dev, sizeof(*binfo), GFP_KERNEL);
+       if (binfo)
+               binfo->pdev = pdev;

build_info_alloc_and_init() is only doing a devm_kzalloc and is only called once. The code would be more readable if you just had the devm_kzalloc in the function that called this (cci_pci_create_feature_devs).

Quoted 285 lines
drivers/fpga/intel-pcie.c +285 -0
+
+       return binfo;
+}
+
+static enum fpga_id_type feature_dev_id_type(struct platform_device *pdev)
+{
+       if (!strcmp(pdev->name, FPGA_FEATURE_DEV_FME))
+               return FME_ID;
+
+       if (!strcmp(pdev->name, FPGA_FEATURE_DEV_PORT))
+               return PORT_ID;
+
+       WARN_ON(1);
+       return FPGA_ID_MAX;
+}
+
+/*
+ * register current feature device, it is called when we need to switch to
+ * another feature parsing or we have parsed all features
+ */
+static int build_info_commit_dev(struct build_feature_devs_info *binfo)
+{
+       int ret;
+
+       if (!binfo->feature_dev)
+               return 0;
+
+       ret = platform_device_add(binfo->feature_dev);
+       if (!ret) {
+               struct cci_drvdata *drvdata;
+
+               drvdata = dev_get_drvdata(&binfo->pdev->dev);
+               if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
+                       cci_pci_add_port_dev(binfo->pdev, binfo->feature_dev);
+               else
+                       drvdata->fme_dev = get_device(&binfo->feature_dev->dev);
+
+               /*
+                * reset it to avoid build_info_free() freeing their resource.
+                *
+                * The resource of successfully registered feature devices
+                * will be freed by platform_device_unregister(). See the
+                * comments in build_info_create_dev().
+                */
+               binfo->feature_dev = NULL;
+       }
+
+       return ret;
+}
+
+static int
+build_info_create_dev(struct build_feature_devs_info *binfo,
+                     enum fpga_id_type type, int feature_nr, const char *name)
+{
+       struct platform_device *fdev;
+       struct resource *res;
+       struct feature_platform_data *pdata;
+       int ret;
+
+       /* we will create a new device, commit current device first */
+       ret = build_info_commit_dev(binfo);
+       if (ret)
+               return ret;
+
+       /*
+        * we use -ENODEV as the initialization indicator which indicates
+        * whether the id need to be reclaimed
+        */
+       fdev = binfo->feature_dev = platform_device_alloc(name, -ENODEV);
+       if (!fdev)
+               return -ENOMEM;
+
+       fdev->id = alloc_fpga_id(type, &fdev->dev);
+       if (fdev->id < 0)
+               return fdev->id;
+
+       fdev->dev.parent = &binfo->parent_dev->dev;
+
+       /*
+        * we do not need to care for the memory which is associated with
+        * the platform device. After calling platform_device_unregister(),
+        * it will be automatically freed by device's release() callback,
+        * platform_device_release().
+        */
+       pdata = feature_platform_data_alloc_and_init(fdev, feature_nr);
+       if (!pdata)
+               return -ENOMEM;
+
+       /*
+        * the count should be initialized to 0 to make sure
+        *__fpga_port_enable() following __fpga_port_disable()
+        * works properly for port device.
+        * and it should always be 0 for fme device.
+        */
+       WARN_ON(pdata->disable_count);
+
+       fdev->dev.platform_data = pdata;
+       fdev->num_resources = feature_nr;
+       fdev->resource = kcalloc(feature_nr, sizeof(*res), GFP_KERNEL);
+       if (!fdev->resource)
+               return -ENOMEM;
+
+       return 0;
+}
+
+static int remove_feature_dev(struct device *dev, void *data)
+{
+       struct platform_device *pdev = to_platform_device(dev);
+
+       platform_device_unregister(pdev);
+       return 0;
+}
+
+static int remove_parent_dev(struct device *dev, void *data)
+{
+       /* remove platform devices attached in the parent device */
+       device_for_each_child(dev, NULL, remove_feature_dev);
+       fpga_dev_destroy(to_fpga_dev(dev));
+       return 0;
+}
+
+static void remove_all_devs(struct pci_dev *pdev)
+{
+       /* remove parent device and all its children. */
+       device_for_each_child(&pdev->dev, NULL, remove_parent_dev);
+}
+
+static void build_info_free(struct build_feature_devs_info *binfo)
+{
+       if (!IS_ERR_OR_NULL(binfo->parent_dev))
+               remove_all_devs(binfo->pdev);
+
+       /*
+        * it is a valid id, free it. See comments in
+        * build_info_create_dev()
+        */
+       if (binfo->feature_dev && binfo->feature_dev->id >= 0)
+               free_fpga_id(feature_dev_id_type(binfo->feature_dev),
+                            binfo->feature_dev->id);
+
+       platform_device_put(binfo->feature_dev);
+
+       devm_kfree(&binfo->pdev->dev, binfo);
+}
+
+#define FEATURE_TYPE_AFU       0x1
+#define FEATURE_TYPE_PRIVATE   0x3
+
+/* FME and PORT GUID are fixed */
+#define FEATURE_FME_GUID "f9e17764-38f0-82fe-e346-524ae92aafbf"
+#define FEATURE_PORT_GUID "6b355b87-b06c-9642-eb42-8d139398b43a"
+
+static bool feature_is_fme(struct feature_afu_header *afu_hdr)
+{
+       uuid_le u;
+
+       uuid_le_to_bin(FEATURE_FME_GUID, &u);
+
+       return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+static bool feature_is_port(struct feature_afu_header *afu_hdr)
+{
+       uuid_le u;
+
+       uuid_le_to_bin(FEATURE_PORT_GUID, &u);
+
+       return !uuid_le_cmp(u, afu_hdr->guid);
+}
+
+/*
+ * UAFU GUID is dynamic as it can be changed after FME downloads different
+ * Green Bitstream to the port, so we treat the unknown GUIDs which are
+ * attached on port's feature list as UAFU.
+ */
+static bool feature_is_UAFU(struct build_feature_devs_info *binfo)
+{
+       if (!binfo->feature_dev ||
+             feature_dev_id_type(binfo->feature_dev) != PORT_ID)
+               return false;
+
+       return true;
+}
+
+static void
+build_info_add_sub_feature(struct build_feature_devs_info *binfo,
+                          int feature_id, const char *feature_name,
+                          resource_size_t resource_size, void __iomem *start)
+{
+
+       struct platform_device *fdev = binfo->feature_dev;
+       struct feature_platform_data *pdata = dev_get_platdata(&fdev->dev);
+       struct resource *res = &fdev->resource[feature_id];
+
+       res->start = pci_resource_start(binfo->pdev, binfo->current_bar) +
+               start - binfo->ioaddr;
+       res->end = res->start + resource_size - 1;
+       res->flags = IORESOURCE_MEM;
+       res->name = feature_name;
+
+       feature_platform_data_add(pdata, feature_id,
+                                 feature_name, feature_id, start);
+}
+
+struct feature_info {
+       const char *name;
+       resource_size_t resource_size;
+       int feature_index;
+};
+
+/* indexed by fme feature IDs which are defined in 'enum fme_feature_id'. */
+static struct feature_info fme_features[] = {
+       {
+               .name = FME_FEATURE_HEADER,
+               .resource_size = sizeof(struct feature_fme_header),
+               .feature_index = FME_FEATURE_ID_HEADER,
+       },
+       {
+               .name = FME_FEATURE_THERMAL_MGMT,
+               .resource_size = sizeof(struct feature_fme_thermal),
+               .feature_index = FME_FEATURE_ID_THERMAL_MGMT,
+       },
+       {
+               .name = FME_FEATURE_POWER_MGMT,
+               .resource_size = sizeof(struct feature_fme_power),
+               .feature_index = FME_FEATURE_ID_POWER_MGMT,
+       },
+       {
+               .name = FME_FEATURE_GLOBAL_PERF,
+               .resource_size = sizeof(struct feature_fme_gperf),
+               .feature_index = FME_FEATURE_ID_GLOBAL_PERF,
+       },
+       {
+               .name = FME_FEATURE_GLOBAL_ERR,
+               .resource_size = sizeof(struct feature_fme_err),
+               .feature_index = FME_FEATURE_ID_GLOBAL_ERR,
+       },
+       {
+               .name = FME_FEATURE_PR_MGMT,
+               .resource_size = sizeof(struct feature_fme_pr),
+               .feature_index = FME_FEATURE_ID_PR_MGMT,
+       }
+};
+
+/* indexed by port feature IDs which are defined in 'enum port_feature_id'. */
+static struct feature_info port_features[] = {
+       {
+               .name = PORT_FEATURE_HEADER,
+               .resource_size = sizeof(struct feature_port_header),
+               .feature_index = PORT_FEATURE_ID_HEADER,
+       },
+       {
+               .name = PORT_FEATURE_ERR,
+               .resource_size = sizeof(struct feature_port_error),
+               .feature_index = PORT_FEATURE_ID_ERROR,
+       },
+       {
+               .name = PORT_FEATURE_UMSG,
+               .resource_size = sizeof(struct feature_port_umsg),
+               .feature_index = PORT_FEATURE_ID_UMSG,
+       },
+       {
+               /* This feature isn't available for now */
+               .name = PORT_FEATURE_PR,
+               .resource_size = 0,
+               .feature_index = PORT_FEATURE_ID_PR,
+       },
+       {
+               .name = PORT_FEATURE_STP,
+               .resource_size = sizeof(struct feature_port_stp),
+               .feature_index = PORT_FEATURE_ID_STP,
+       },
+       {
+               /*
+                * For User AFU feature, its region size is not fixed, but
+                * reported by register PortCapability.mmio_size. Resource
+                * size of UAFU will be set while parse port device.
+                */
+               .name = PORT_FEATURE_UAFU,
+               .resource_size = 0,
+               .feature_index = PORT_FEATURE_ID_UAFU,
+       },
+};
+
+static int

drivers/fpga/intel-pcie.c line 514

Quoted 10 lines
drivers/fpga/intel-pcie.c +10 -0
+create_feature_instance(struct build_feature_devs_info *binfo,
+                       void __iomem *start, struct feature_info *finfo)
+{
+       if (binfo->ioend - start < finfo->resource_size)
+               return -EINVAL;
+
+       build_info_add_sub_feature(binfo, finfo->feature_index, finfo->name,
+                                  finfo->resource_size, start);
+       return 0;
+}

build_info_add_sub_feature() is only called one time - here. Could you collapse these two functions together?

Quoted 196 lines
drivers/fpga/intel-pcie.c +196 -0
+
+static int parse_feature_fme(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&binfo->pdev->dev);
+       int ret;
+
+       ret = build_info_create_dev(binfo, FME_ID, fme_feature_num(),
+                                       FPGA_FEATURE_DEV_FME);
+       if (ret)
+               return ret;
+
+       if (drvdata->fme_dev) {
+               dev_err(&binfo->pdev->dev, "Multiple FMEs are detected.\n");
+               return -EINVAL;
+       }
+
+       return create_feature_instance(binfo, start,
+                                      &fme_features[FME_FEATURE_ID_HEADER]);
+}
+
+static int parse_feature_fme_private(struct build_feature_devs_info *binfo,
+                                    struct feature_header *hdr)
+{
+       struct feature_header header;
+
+       header.csr = readq(hdr);
+
+       if (header.id >= ARRAY_SIZE(fme_features)) {
+               dev_info(&binfo->pdev->dev, "FME feature id %x is not supported yet.\n",
+                        header.id);
+               return 0;
+       }
+
+       return create_feature_instance(binfo, hdr, &fme_features[header.id]);
+}
+
+static int parse_feature_port(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       int ret;
+
+       ret = build_info_create_dev(binfo, PORT_ID, port_feature_num(),
+                                       FPGA_FEATURE_DEV_PORT);
+       if (ret)
+               return ret;
+
+       return create_feature_instance(binfo, start,
+                                      &port_features[PORT_FEATURE_ID_HEADER]);
+}
+
+static void enable_port_uafu(struct build_feature_devs_info *binfo,
+                            void __iomem *start)
+{
+       enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+       struct feature_port_header *port_hdr;
+       struct feature_port_capability capability;
+
+       port_hdr = (struct feature_port_header *)start;
+       capability.csr = readq(&port_hdr->capability);
+       port_features[id].resource_size = capability.mmio_size << 10;
+
+       /*
+        * To enable User AFU, driver needs to clear reset bit on related port,
+        * otherwise the mmio space of this user AFU will be invalid.
+        */
+       if (port_features[id].resource_size)
+               fpga_port_reset(binfo->feature_dev);
+}
+
+static int parse_feature_port_private(struct build_feature_devs_info *binfo,
+                                     struct feature_header *hdr)
+{
+       struct feature_header header;
+       enum port_feature_id id;
+
+       header.csr = readq(hdr);
+       /*
+        * the region of port feature id is [0x10, 0x13], + 1 to reserve 0
+        * which is dedicated for port-hdr.
+        */
+       id = (header.id & 0x000f) + 1;
+
+       if (id >= ARRAY_SIZE(port_features)) {
+               dev_info(&binfo->pdev->dev, "Port feature id %x is not supported yet.\n",
+                        header.id);
+               return 0;
+       }
+
+       return create_feature_instance(binfo, hdr, &port_features[id]);
+}
+
+static int parse_feature_port_uafu(struct build_feature_devs_info *binfo,
+                                struct feature_header *hdr)
+{
+       enum port_feature_id id = PORT_FEATURE_ID_UAFU;
+       int ret;
+
+       if (port_features[id].resource_size) {
+               ret = create_feature_instance(binfo, hdr, &port_features[id]);
+               port_features[id].resource_size = 0;
+       } else {
+               dev_err(&binfo->pdev->dev, "the uafu feature header is mis-configured.\n");
+               ret = -EINVAL;
+       }
+
+       return ret;
+}
+
+static int parse_feature_afus(struct build_feature_devs_info *binfo,
+                             struct feature_header *hdr)
+{
+       int ret;
+       struct feature_afu_header *afu_hdr, header;
+       void __iomem *start;
+       void __iomem *end = binfo->ioend;
+
+       start = hdr;
+       for (; start < end; start += header.next_afu) {
+               if (end - start < (sizeof(*afu_hdr) + sizeof(*hdr)))
+                       return -EINVAL;
+
+               hdr = start;
+               afu_hdr = (struct feature_afu_header *) (hdr + 1);
+               header.csr = readq(&afu_hdr->csr);
+
+               if (feature_is_fme(afu_hdr)) {
+                       ret = parse_feature_fme(binfo, hdr);
+                       binfo->pfme_hdr = hdr;
+                       if (ret)
+                               return ret;
+               } else if (feature_is_port(afu_hdr)) {
+                       ret = parse_feature_port(binfo, hdr);
+                       enable_port_uafu(binfo, hdr);
+                       if (ret)
+                               return ret;
+               } else if (feature_is_UAFU(binfo)) {
+                       ret = parse_feature_port_uafu(binfo, hdr);
+                       if (ret)
+                               return ret;
+               } else
+                       dev_info(&binfo->pdev->dev, "AFU GUID %pUl is not supported yet.\n",
+                                afu_hdr->guid.b);
+
+               if (!header.next_afu)
+                       break;
+       }
+
+       return 0;
+}
+
+static int parse_feature_private(struct build_feature_devs_info *binfo,
+                                struct feature_header *hdr)
+{
+       struct feature_header header;
+
+       header.csr = readq(hdr);
+
+       if (!binfo->feature_dev) {
+               dev_err(&binfo->pdev->dev, "the private feature %x does not belong to any AFU.\n",
+                       header.id);
+               return -EINVAL;
+       }
+
+       switch (feature_dev_id_type(binfo->feature_dev)) {
+       case FME_ID:
+               return parse_feature_fme_private(binfo, hdr);
+       case PORT_ID:
+               return parse_feature_port_private(binfo, hdr);
+       default:
+               dev_info(&binfo->pdev->dev, "private feature %x belonging to AFU %s is not supported yet.\n",
+                        header.id, binfo->feature_dev->name);
+       }
+       return 0;
+}
+
+static int parse_feature(struct build_feature_devs_info *binfo,
+                        struct feature_header *hdr)
+{
+       struct feature_header header;
+       int ret = 0;
+
+       header.csr = readq(hdr);
+
+       switch (header.type) {
+       case FEATURE_TYPE_AFU:
+               ret = parse_feature_afus(binfo, hdr);
+               break;
+       case FEATURE_TYPE_PRIVATE:
+               ret = parse_feature_private(binfo, hdr);
+               break;
+       default:
+               dev_info(&binfo->pdev->dev,
+                        "Feature Type %x is not supported.\n", hdr->type);
+       };
+

drivers/fpga/intel-pcie.c line 720

Quoted 10 lines
drivers/fpga/intel-pcie.c +10 -0
+       return ret;
+}
+
+static int
+parse_feature_list(struct build_feature_devs_info *binfo, void __iomem *start)
+{
+       struct feature_header *hdr, header;
+       void __iomem *end = binfo->ioend;
+       int ret = 0;
+

Maybe a helpful comment that we are stepping through a linked list of features.

drivers/fpga/intel-pcie.c line 731

Quoted 11 lines
drivers/fpga/intel-pcie.c +11 -0
+       for (; start < end; start += header.next_header_offset) {
+               if (end - start < sizeof(*hdr)) {
+                       dev_err(&binfo->pdev->dev, "The region is too small to contain a feature.\n");
+                       ret =  -EINVAL;
+                       break;
+               }
+
+               hdr = (struct feature_header *)start;
+               ret = parse_feature(binfo, hdr);
+               if (ret)
+                       break;

Instead of parse_feature, this can save off the enumeration info and continue to read in the linked list. After all the headers are read in, call the (separate) enumeration code to step through the saved headers, parse them, and create the devices. Since the memory is iomapped during the process of reading in the headers, the enumeration code doesn't have to be so pcie specific. Then this code base is better set to run on embedded devices also.

Quoted 89 lines
drivers/fpga/intel-pcie.c +89 -0
+
+               header.csr = readq(hdr);
+               if (!header.next_header_offset)
+                       break;
+       }
+
+       return ret;
+}
+
+static int parse_ports_from_fme(struct build_feature_devs_info *binfo)
+{
+       struct feature_fme_header *fme_hdr;
+       struct feature_fme_port port;
+       int i = 0, ret = 0;
+
+       if (binfo->pfme_hdr == NULL) {
+               dev_dbg(&binfo->pdev->dev, "VF is detected.\n");
+               return ret;
+       }
+
+       fme_hdr = binfo->pfme_hdr;
+
+       do {
+               port.csr = readq(&fme_hdr->port[i]);
+               if (!port.port_implemented)
+                       break;
+
+               ret = parse_switch_to(binfo, port.port_bar);
+               if (ret)
+                       break;
+
+               ret = parse_feature_list(binfo,
+                               binfo->ioaddr + port.port_offset);
+               if (ret)
+                       break;
+       } while (++i < MAX_FPGA_PORT_NUM);
+
+       return ret;
+}
+
+static int create_init_drvdata(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata;
+
+       drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
+       if (!drvdata)
+               return -ENOMEM;
+
+       mutex_init(&drvdata->lock);
+       INIT_LIST_HEAD(&drvdata->port_dev_list);
+       INIT_LIST_HEAD(&drvdata->regions);
+
+       dev_set_drvdata(&pdev->dev, drvdata);
+       return 0;
+}
+
+static void destroy_drvdata(struct pci_dev *pdev)
+{
+       struct cci_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
+
+       if (drvdata->fme_dev) {
+               /* fme device should be unregistered first. */
+               WARN_ON(device_is_registered(drvdata->fme_dev));
+               free_fpga_id(FME_ID, to_platform_device(drvdata->fme_dev)->id);
+               put_device(drvdata->fme_dev);
+       }
+
+       cci_pci_remove_port_devs(pdev);
+       cci_pci_release_regions(pdev);
+       dev_set_drvdata(&pdev->dev, NULL);
+       devm_kfree(&pdev->dev, drvdata);
+}
+
+static int cci_pci_create_feature_devs(struct pci_dev *pdev)
+{
+       struct build_feature_devs_info *binfo;
+       int ret;
+
+       binfo = build_info_alloc_and_init(pdev);
+       if (!binfo)
+               return -ENOMEM;
+
+       binfo->parent_dev = fpga_dev_create(&pdev->dev, INTEL_FPGA_DEV);
+       if (IS_ERR(binfo->parent_dev)) {
+               ret = PTR_ERR(binfo->parent_dev);
+               goto free_binfo_exit;
+       }
+
+       ret = parse_start(binfo);

drivers/fpga/intel-pcie.c line 830

Quoted 10 lines
drivers/fpga/intel-pcie.c +10 -0
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = parse_feature_list(binfo, binfo->ioaddr);
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = parse_ports_from_fme(binfo);
+       if (ret)
+               goto free_binfo_exit;

So ideally, there would be a function call here that read all the headers from hardware, ioremapping as it went along. Then after that, call the enumeration code to create the devices.

Quoted 76 lines
drivers/fpga/intel-pcie.c +38 -3
+
+       ret = build_info_commit_dev(binfo);
+       if (ret)
+               goto free_binfo_exit;
+
+       /*
+        * everything is okay, reset ->parent_dev to stop it being
+        * freed by build_info_free()
+        */
+       binfo->parent_dev = NULL;
+
+free_binfo_exit:
+       build_info_free(binfo);
+       return ret;
+}
+
 /* PCI Device ID */
 #define PCIe_DEVICE_ID_PF_INT_5_X      0xBCBD
 #define PCIe_DEVICE_ID_PF_INT_6_X      0xBCC0
@@ -81,9 +898,18 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
                goto release_region_exit;
        }
 
-       /* TODO: create and add the platform device per feature list */
+       ret = create_init_drvdata(pcidev);
+       if (ret)
+               goto release_region_exit;
+
+       ret = cci_pci_create_feature_devs(pcidev);
+       if (ret)
+               goto destroy_drvdata_exit;
+
        return 0;
 
+destroy_drvdata_exit:
+       destroy_drvdata(pcidev);
 release_region_exit:
        pci_release_regions(pcidev);
 disable_error_report_exit:
@@ -94,6 +920,8 @@ int cci_pci_probe(struct pci_dev *pcidev, const struct pci_device_id *pcidevid)
 
 static void cci_pci_remove(struct pci_dev *pcidev)
 {
+       remove_all_devs(pcidev);
+       destroy_drvdata(pcidev);
        pci_release_regions(pcidev);
        pci_disable_pcie_error_reporting(pcidev);
        pci_disable_device(pcidev);
@@ -108,14 +936,23 @@ static void cci_pci_remove(struct pci_dev *pcidev)
 
 static int __init ccidrv_init(void)
 {
+       int ret;
+
        pr_info("Intel(R) FPGA PCIe Driver: Version %s\n", DRV_VERSION);
 
-       return pci_register_driver(&cci_pci_driver);
+       fpga_ids_init();
+
+       ret = pci_register_driver(&cci_pci_driver);
+       if (ret)
+               fpga_ids_destroy();
+
+       return ret;
 }
 
 static void __exit ccidrv_exit(void)
 {
        pci_unregister_driver(&cci_pci_driver);
+       fpga_ids_destroy();
 }
 
 module_init(ccidrv_init);
--

1.8.3.1