Skip to contentSkip to conversation

Intel FPGA Device Drivers

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

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
author
#45 lore raw

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

In reply to Alan Tull
Quoted 5 lines · Alan Tull

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

Hi Hao,

A few more minor things below.

Hi Alan

Thanks for your comments, will fix them in the next version.

Thanks
Hao

Quoted 1,530 lines
Quoted 114 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 <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 +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 <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,
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,373 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 <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 {
+                       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 72 lines
drivers/fpga/intel-pcie.c +38 -2
+
+       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();
 }
 
Quoted 4 lines
Quoted 4 lines
drivers/fpga/intel-pcie.c +0 -1
 module_init(ccidrv_init);
--

1.8.3.1

--
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
#46 lore raw

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

In reply to Alan Tull
Quoted 5 lines · Alan Tull

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.

Hi Alan

Thanks for your time on the review. : )

Quoted 23 lines

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

Quoted 8 lines

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.

So it's suggested that we have some kind of pre-enumeration code in the intel-pcie.c to do ioremap and locate the address of device feature lists for FME and Ports, and hand them to common enumeration code. Is my understanding correct?

I have considered this some time ago, this is doable for current code, as the pre-enumeration code will be simple (only need to find FME header and then read some register in FME header register set to locate the Port header). But if in the future, some pcie specific things are added to some sub features of FME, then pre-enumeration code has to parse FME and all its sub features to handle pcie specific things. We will have more duplicate code on pre-enumeration in intel-pcie.c and common enumeration. Another point is, it's hard to avoid pcie specific things in sub features, as they may be designed to use PCIE specific function or resources. e.g some sub features require the interrupts from PCIE device, like PCIE MSI / MSIX. So keep enumeration code away from PCI things may be difficult.

Quoted 6 lines

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)?

Yes, you are correct. Is it fine to have the MMIO region mapped twice? or we have to pass the mapped address to intel-fpga-fme-mgr directly?

Quoted 102 lines

A few more things below...

Quoted 99 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 +50 -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)
+{
Quoted 8 lines

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

Quoted 2 lines
drivers/fpga/intel-feature-dev.c new file +2 -0
+       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 ;)

Sure, will fix this.

Quoted 17 lines
Quoted 5 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?

No, this fpga_port_id function will be used in both afu/port driver and also the pcie driver, so this function in added here.
The code in pcie driver which uses this function isn't submitted yet, it's part of the SRIOV support. The pcie driver needs to manage which port could be turned to VF.

Quoted 614 lines
Quoted 595 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.

Quoted 6 lines

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 ;)

I think I need to explain a little more on the enumeration steps in this cci_pci_create_feature_devs function, and we use these names for these functions.

1) parse_start -> that initializes the enumeration from PCI Bar0. In PF case, the first byte of BAR0, will be FME header. In VF case, the first byte of BAR0, will be Port header. (FME module is always in PF, no FME module in VF).

2) parse_feature_list -> parse one device feature list

This function parses a feature list, it could be FME device feature list (PF) or Port device feature list (VF).
FME device feature list and Port device feature list are not linked together, as they are in differnet BARs.

3) parse_port_from_fme -> parse Port device feature lists if FME presents.

If FME is detected (PF), read FME registers to know the location (BAR n
+ offset) of each port's header (port device feature list). call parse_switch_to(BAR n) and call parse_feature_list to parse the port device feature list.

If FME is not detected (VF), just do nothing.

Do you think if we could keep these functions? We use these names to help people to understand the enumeration process better, but looks like I should add more descriptions/comments here to avoid misunderstanding. :)

Quoted 15 lines

drivers/fpga/intel-pcie.c line 219

Quoted 10 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).

Sure.

Quoted 291 lines
Quoted 291 lines
drivers/fpga/intel-pcie.c +290 -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
+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;
Quoted 8 lines

drivers/fpga/intel-pcie.c line 514

Quoted 5 lines
drivers/fpga/intel-pcie.c +5 -0
+
+       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?

Sure.

Quoted 201 lines
Quoted 201 lines
drivers/fpga/intel-pcie.c +200 -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
Quoted 8 lines

drivers/fpga/intel-pcie.c line 720

Quoted 6 lines
drivers/fpga/intel-pcie.c +6 -0
+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.

Sure, will add more comments.

All kind of features has a common header, they are linked together via the header.next_header_offset.

If it's a TYPE=AFU feature, it will have one more pointer named NEXT_AFU, which could point to another TYPE=AFU feature, please refer to function parse_feature_afus.

If it's a TYPE=private feature, there is no other pointers, please refer to function parse_feature_private.

Quoted 13 lines

drivers/fpga/intel-pcie.c line 731

Quoted 12 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;
Quoted 7 lines

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.

Actually, it has parse_feature here, because they're different type of features. e.g for TYPE = AFU, GUID = PORT, it requires driver to check its NEXT_AFU pointer to find User AFU (and its header). For private features, there is no next_afu pointer at all.

Quoted 96 lines
Quoted 96 lines
drivers/fpga/intel-pcie.c +95 -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);
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = parse_feature_list(binfo, binfo->ioaddr);
+       if (ret)
+               goto free_binfo_exit;
Quoted 8 lines

drivers/fpga/intel-pcie.c line 830

Quoted 4 lines
drivers/fpga/intel-pcie.c +4 -0
+
+       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.

If we have a function call here which read all the headers, then it finish most works of parsing the device feature list. The only thing that enumeration code to do is to create the devices with prepared resources. It's fine to me, as we can just save information to this binfo, and create devices later, but is this matches with what you think? :)

Thanks
Hao

Quoted 77 lines
Quoted 77 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

#47 lore raw

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

Quoted 1 line · Wu Hao

On Wed, Sep 20, 2017 at 04:24:10PM -0500, Alan Tull wrote:

HI Hao,

Quoted 42 lines
Quoted 5 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.

Hi Alan

Thanks for your time on the review. : )

Quoted 23 lines

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

Quoted 8 lines

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.

Quoted 4 lines

So it's suggested that we have some kind of pre-enumeration code in the intel-pcie.c to do ioremap and locate the address of device feature lists for FME and Ports, and hand them to common enumeration code. Is my understanding correct?

I was suggesting reading in the structures and saving that info to pass to the enumeration code. Passing the ioremapped addresses would probably be just as fine.

Please keep in mind that I am just making suggestions about how I think this can be made to be platform independent. We can work out the details, but my main point here is that this infrastructure is going to need to support embedded FPGAs. As you suggest below, if we wait it will become more PCIe-centric and it will be harder later for this code base to support other parts.

Quoted 5 lines

I have considered this some time ago, this is doable for current code, as the pre-enumeration code will be simple (only need to find FME header and then read some register in FME header register set to locate the Port header).

A lot of my intention in reviewing this is to make sure the code is broadly usable. That way it will have a future on other platforms, for example the Stratix10 SoC.

That is also why it has been important to use the FPGA manager API and expand it where needed. (You made some good changes for v2 in this
area, thanks!) When this infrastructure runs on a SoC, the FME will
be divided up, some of the functionality will appear as it currently does, but the PR part will use whatever PR hardware the SoC has.

Quoted 795 lines

But if in the future, some pcie specific things are added to some sub features of FME, then pre-enumeration code has to parse FME and all its sub features to handle pcie specific things. We will have more duplicate code on pre-enumeration in intel-pcie.c and common enumeration. Another point is, it's hard to avoid pcie specific things in sub features, as they may be designed to use PCIE specific function or resources. e.g some sub features require the interrupts from PCIE device, like PCIE MSI / MSIX. So keep enumeration code away from PCI things may be difficult.

Quoted 6 lines

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)?

Yes, you are correct. Is it fine to have the MMIO region mapped twice? or we have to pass the mapped address to intel-fpga-fme-mgr directly?

Quoted 102 lines

A few more things below...

Quoted 99 lines

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 +50 -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)
+{
Quoted 8 lines

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

Quoted 2 lines
drivers/fpga/intel-feature-dev.c new file +2 -0
+       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 ;)

Sure, will fix this.

Quoted 17 lines
Quoted 5 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?

No, this fpga_port_id function will be used in both afu/port driver and also the pcie driver, so this function in added here.
The code in pcie driver which uses this function isn't submitted yet, it's part of the SRIOV support. The pcie driver needs to manage which port could be turned to VF.

Quoted 614 lines
Quoted 595 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 <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 {
+                       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.

Quoted 6 lines

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 ;)

I think I need to explain a little more on the enumeration steps in this cci_pci_create_feature_devs function, and we use these names for these functions.

1) parse_start -> that initializes the enumeration from PCI Bar0. In PF case, the first byte of BAR0, will be FME header. In VF case, the first byte of BAR0, will be Port header. (FME module is always in PF, no FME module in VF).

2) parse_feature_list -> parse one device feature list

This function parses a feature list, it could be FME device feature list (PF) or Port device feature list (VF).
FME device feature list and Port device feature list are not linked together, as they are in differnet BARs.

3) parse_port_from_fme -> parse Port device feature lists if FME presents.

Quoted 8 lines

If FME is detected (PF), read FME registers to know the location (BAR n
+ offset) of each port's header (port device feature list). call parse_switch_to(BAR n) and call parse_feature_list to parse the port device feature list.

If FME is not detected (VF), just do nothing.

Do you think if we could keep these functions?

I'm OK with parse_feature_list and parse_port_from_fme, etc. I intended to just comment about parse_start, parse_start_from, and parse_switch_to. I was suggesting to get rid of parse_start and parse_switch_to and just use parse_start_from instead.

Quoted 539 lines

We use these names to
help people to understand the enumeration process better, but looks like I should add more descriptions/comments here to avoid misunderstanding. :)

Quoted 15 lines

drivers/fpga/intel-pcie.c line 219

Quoted 10 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).

Sure.

Quoted 291 lines
Quoted 291 lines
drivers/fpga/intel-pcie.c +290 -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
+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;
Quoted 8 lines

drivers/fpga/intel-pcie.c line 514

Quoted 5 lines
drivers/fpga/intel-pcie.c +5 -0
+
+       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?

Sure.

Quoted 201 lines
Quoted 201 lines
drivers/fpga/intel-pcie.c +200 -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
Quoted 8 lines

drivers/fpga/intel-pcie.c line 720

Quoted 6 lines
drivers/fpga/intel-pcie.c +6 -0
+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.

Sure, will add more comments.

All kind of features has a common header, they are linked together via the header.next_header_offset.

Quoted 6 lines

If it's a TYPE=AFU feature, it will have one more pointer named NEXT_AFU, which could point to another TYPE=AFU feature, please refer to function parse_feature_afus.

If it's a TYPE=private feature, there is no other pointers, please refer to function parse_feature_private.

OK, yes that will be helpful.

Quoted 132 lines
Quoted 14 lines

drivers/fpga/intel-pcie.c line 731

Quoted 13 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;
Quoted 7 lines

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.

Actually, it has parse_feature here, because they're different type of features. e.g for TYPE = AFU, GUID = PORT, it requires driver to check its NEXT_AFU pointer to find User AFU (and its header). For private features, there is no next_afu pointer at all.

Quoted 96 lines
Quoted 96 lines
drivers/fpga/intel-pcie.c +95 -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);
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = parse_feature_list(binfo, binfo->ioaddr);
+       if (ret)
+               goto free_binfo_exit;
Quoted 8 lines

drivers/fpga/intel-pcie.c line 830

Quoted 4 lines
drivers/fpga/intel-pcie.c +4 -0
+
+       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 6 lines

If we have a function call here which read all the headers, then it finish most works of parsing the device feature list. The only thing that enumeration code to do is to create the devices with prepared resources. It's fine to me, as we can just save information to this binfo, and create devices later, but is this matches with what you think? :)

That is what I'm thinking more or less. I'm trying to see this
implemented some way where iomapping is separate from creating devices. There are drivers in the kernel that have 3 parts: core, platform, and pci. The core has most of the functionality specific to the driver where the platform and pci parts handle iomapping depending on which bus the device is on.

Alan

Quoted 81 lines

Thanks
Hao

Quoted 77 lines
Quoted 77 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

author
#48 lore raw

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

Quoted 4 lines · Alan Tull
Quoted 1 line · Wu Hao

On Wed, Sep 20, 2017 at 04:24:10PM -0500, Alan Tull wrote:

HI Hao,

Hi Alan

Thanks for your comments. :)

Quoted 52 lines
Quoted 43 lines
Quoted 6 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.

Hi Alan

Thanks for your time on the review. : )

Quoted 23 lines

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

Quoted 8 lines

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.

Quoted 4 lines

So it's suggested that we have some kind of pre-enumeration code in the intel-pcie.c to do ioremap and locate the address of device feature lists for FME and Ports, and hand them to common enumeration code. Is my understanding correct?

I was suggesting reading in the structures and saving that info to pass to the enumeration code. Passing the ioremapped addresses would probably be just as fine.

Quoted 6 lines

Please keep in mind that I am just making suggestions about how I think this can be made to be platform independent. We can work out the details, but my main point here is that this infrastructure is going to need to support embedded FPGAs. As you suggest below, if we wait it will become more PCIe-centric and it will be harder later for this code base to support other parts.

Agree, understand your point. It would be great that this infrastructure could support embedded FPGAs as well. So I would like have a try to split this code, do some pre-enumeration in PCIe driver, and hand required resources to some common enumeration code which creates the feature devices. You're right, we should not wait without doing anything as we already have a goal here. :)

Quoted 817 lines
Quoted 6 lines

I have considered this some time ago, this is doable for current code, as the pre-enumeration code will be simple (only need to find FME header and then read some register in FME header register set to locate the Port header).

A lot of my intention in reviewing this is to make sure the code is broadly usable. That way it will have a future on other platforms, for example the Stratix10 SoC.

That is also why it has been important to use the FPGA manager API and expand it where needed. (You made some good changes for v2 in this
area, thanks!) When this infrastructure runs on a SoC, the FME will
be divided up, some of the functionality will appear as it currently does, but the PR part will use whatever PR hardware the SoC has.

Quoted 800 lines

But if in the future, some pcie specific things are added to some sub features of FME, then pre-enumeration code has to parse FME and all its sub features to handle pcie specific things. We will have more duplicate code on pre-enumeration in intel-pcie.c and common enumeration. Another point is, it's hard to avoid pcie specific things in sub features, as they may be designed to use PCIE specific function or resources. e.g some sub features require the interrupts from PCIE device, like PCIE MSI / MSIX. So keep enumeration code away from PCI things may be difficult.

Quoted 6 lines

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)?

Yes, you are correct. Is it fine to have the MMIO region mapped twice? or we have to pass the mapped address to intel-fpga-fme-mgr directly?

Quoted 102 lines

A few more things below...

Quoted 99 lines

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 +50 -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)
+{
Quoted 8 lines

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

Quoted 2 lines
drivers/fpga/intel-feature-dev.c new file +2 -0
+       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 ;)

Sure, will fix this.

Quoted 17 lines
Quoted 5 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?

No, this fpga_port_id function will be used in both afu/port driver and also the pcie driver, so this function in added here.
The code in pcie driver which uses this function isn't submitted yet, it's part of the SRIOV support. The pcie driver needs to manage which port could be turned to VF.

Quoted 614 lines
Quoted 595 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 <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 {
+                       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.

Quoted 6 lines

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 ;)

I think I need to explain a little more on the enumeration steps in this cci_pci_create_feature_devs function, and we use these names for these functions.

1) parse_start -> that initializes the enumeration from PCI Bar0. In PF case, the first byte of BAR0, will be FME header. In VF case, the first byte of BAR0, will be Port header. (FME module is always in PF, no FME module in VF).

2) parse_feature_list -> parse one device feature list

This function parses a feature list, it could be FME device feature list (PF) or Port device feature list (VF).
FME device feature list and Port device feature list are not linked together, as they are in differnet BARs.

3) parse_port_from_fme -> parse Port device feature lists if FME presents.

If FME is detected (PF), read FME registers to know the location (BAR n
+ offset) of each port's header (port device feature list). call parse_switch_to(BAR n) and call parse_feature_list to parse the port device feature list.

Quoted 8 lines
Quoted 3 lines

If FME is not detected (VF), just do nothing.

Do you think if we could keep these functions?

I'm OK with parse_feature_list and parse_port_from_fme, etc. I intended to just comment about parse_start, parse_start_from, and parse_switch_to. I was suggesting to get rid of parse_start and parse_switch_to and just use parse_start_from instead.

Sure, will fix this.

Quoted 688 lines
Quoted 540 lines

We use these names to
help people to understand the enumeration process better, but looks like I should add more descriptions/comments here to avoid misunderstanding. :)

Quoted 15 lines

drivers/fpga/intel-pcie.c line 219

Quoted 10 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).

Sure.

Quoted 291 lines
Quoted 291 lines
drivers/fpga/intel-pcie.c +290 -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
+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;
Quoted 8 lines

drivers/fpga/intel-pcie.c line 514

Quoted 5 lines
drivers/fpga/intel-pcie.c +5 -0
+
+       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?

Sure.

Quoted 201 lines
Quoted 201 lines
drivers/fpga/intel-pcie.c +200 -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
Quoted 8 lines

drivers/fpga/intel-pcie.c line 720

Quoted 6 lines
drivers/fpga/intel-pcie.c +6 -0
+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.

Sure, will add more comments.

All kind of features has a common header, they are linked together via the header.next_header_offset.

Quoted 6 lines

If it's a TYPE=AFU feature, it will have one more pointer named NEXT_AFU, which could point to another TYPE=AFU feature, please refer to function parse_feature_afus.

If it's a TYPE=private feature, there is no other pointers, please refer to function parse_feature_private.

OK, yes that will be helpful.

Quoted 132 lines
Quoted 14 lines

drivers/fpga/intel-pcie.c line 731

Quoted 13 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;
Quoted 7 lines

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.

Actually, it has parse_feature here, because they're different type of features. e.g for TYPE = AFU, GUID = PORT, it requires driver to check its NEXT_AFU pointer to find User AFU (and its header). For private features, there is no next_afu pointer at all.

Quoted 96 lines
Quoted 96 lines
drivers/fpga/intel-pcie.c +95 -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);
+       if (ret)
+               goto free_binfo_exit;
+
+       ret = parse_feature_list(binfo, binfo->ioaddr);
+       if (ret)
+               goto free_binfo_exit;
Quoted 8 lines

drivers/fpga/intel-pcie.c line 830

Quoted 4 lines
drivers/fpga/intel-pcie.c +4 -0
+
+       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 6 lines

If we have a function call here which read all the headers, then it finish most works of parsing the device feature list. The only thing that enumeration code to do is to create the devices with prepared resources. It's fine to me, as we can just save information to this binfo, and create devices later, but is this matches with what you think? :)

Quoted 6 lines

That is what I'm thinking more or less. I'm trying to see this
implemented some way where iomapping is separate from creating devices. There are drivers in the kernel that have 3 parts: core, platform, and pci. The core has most of the functionality specific to the driver where the platform and pci parts handle iomapping depending on which bus the device is on.

Yes, I think the core code here is the enumeration part, as we can re-use platform drivers for the feature devices created by the enumeration part. As I mentioned above, will have a try and see. Thanks a lot for these suggestions. :)

Thanks
Hao

Quoted 84 lines

Alan

Quoted 81 lines

Thanks
Hao

Quoted 77 lines
Quoted 77 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

author
#49 lore raw

PATCH v2 8/22 fpga: intel: pcie: add chardev support for feature devices

In reply to Wu Hao

From: Xiao Guangrong <[email protected]>

For feature devices drivers, both the FPGA Management Engine (FME) and Accelerated Function Unit (AFU) driver need to expose user interfaces via the device file, for example, mmap and ioctls.

This patch adds chardev support in the pcie driver for feature devices, FME and AFU. It reserves the chardev regions for FME and AFU, and provide interfaces for FME and AFU driver to register their device file operations.

---
v2: rebased

---
 drivers/fpga/intel-feature-dev.c | 76 ++++++++++++++++++++++++++++++++++++++++
 drivers/fpga/intel-feature-dev.h | 16 +++++++++
 drivers/fpga/intel-pcie.c        | 18 +++++++++-
 3 files changed, 109 insertions(+), 1 deletion(-)
drivers/fpga/intel-feature-dev.c +76 -0
index 68f9cba..45788fe 100644
@@ -51,6 +51,82 @@ int port_feature_num(void)
 	return PORT_FEATURE_ID_MAX;
 }
 
+struct fpga_chardev_info {
+	const char *name;
+	dev_t devt;
+};
+
+/* indexed by enum fpga_devt_type */
+struct fpga_chardev_info fpga_chrdevs[] = {
+	{.name = FPGA_FEATURE_DEV_FME},		/* FPGA_DEVT_FME */
+	{.name = FPGA_FEATURE_DEV_PORT},	/* FPGA_DEVT_AFU */
+};
+
+void fpga_chardev_uinit(void)
+{
+	int i;
+
+	for (i = 0; i < FPGA_DEVT_MAX; i++)
+		if (MAJOR(fpga_chrdevs[i].devt)) {
+			unregister_chrdev_region(fpga_chrdevs[i].devt,
+						 MINORMASK);
+			fpga_chrdevs[i].devt = MKDEV(0, 0);
+		}
+}
+
+int fpga_chardev_init(void)
+{
+	int i, ret;
+
+	for (i = 0; i < FPGA_DEVT_MAX; i++) {
+		ret = alloc_chrdev_region(&fpga_chrdevs[i].devt, 0, MINORMASK,
+					  fpga_chrdevs[i].name);
+		if (ret)
+			goto exit;
+	}
+
+	return 0;
+
+exit:
+	fpga_chardev_uinit();
+	return ret;
+}
+
+dev_t fpga_get_devt(enum fpga_devt_type type, int id)
+{
+	WARN_ON(type >= FPGA_DEVT_MAX);
+
+	return MKDEV(MAJOR(fpga_chrdevs[type].devt), id);
+}
+
+int fpga_register_dev_ops(struct platform_device *pdev,
+			  const struct file_operations *fops,
+			  struct module *owner)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+	cdev_init(&pdata->cdev, fops);
+	pdata->cdev.owner = owner;
+
+	/*
+	 * set parent to the feature device so that its refcount is
+	 * decreased after the last refcount of cdev is gone, that
+	 * makes sure the feature device is valid during device
+	 * file's life-cycle.
+	 */
+	pdata->cdev.kobj.parent = &pdev->dev.kobj;
+	return cdev_add(&pdata->cdev, pdev->dev.devt, 1);
+}
+EXPORT_SYMBOL_GPL(fpga_register_dev_ops);
+
+void fpga_unregister_dev_ops(struct platform_device *pdev)
+{
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+	cdev_del(&pdata->cdev);
+}
+EXPORT_SYMBOL_GPL(fpga_unregister_dev_ops);
+
 int fpga_port_id(struct platform_device *pdev)
 {
 	struct feature_port_header *port_hdr;
drivers/fpga/intel-feature-dev.h +16 -0
index f67784a..d64a51e 100644
@@ -17,6 +17,7 @@
 #define __INTEL_FPGA_FEATURE_H
 
 #include <linux/fs.h>
+#include <linux/cdev.h>
 #include <linux/pci.h>
 #include <linux/uuid.h>
 #include <linux/delay.h>
@@ -230,6 +231,7 @@ struct feature_platform_data {
 	/* list the feature dev to cci_drvdata->port_dev_list. */
 	struct list_head node;
 	struct mutex lock;
+	struct cdev cdev;
 	struct platform_device *dev;
 	unsigned int disable_count;	/* count for port disable */
 
@@ -276,6 +278,20 @@ static inline int feature_platform_data_size(const int num)
 struct feature_platform_data *
 feature_platform_data_alloc_and_init(struct platform_device *dev, int num);
 
+enum fpga_devt_type {
+	FPGA_DEVT_FME,
+	FPGA_DEVT_PORT,
+	FPGA_DEVT_MAX,
+};
+
+void fpga_chardev_uinit(void);
+int fpga_chardev_init(void);
+dev_t fpga_get_devt(enum fpga_devt_type type, int id);
+int fpga_register_dev_ops(struct platform_device *pdev,
+			  const struct file_operations *fops,
+			  struct module *owner);
+void fpga_unregister_dev_ops(struct platform_device *pdev);
+
 int fpga_port_id(struct platform_device *pdev);
 
 static inline int fpga_port_check_id(struct platform_device *pdev,
drivers/fpga/intel-pcie.c +17 -1
index 70b8284..54c0e3a 100644
@@ -274,8 +274,12 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	struct platform_device *fdev;
 	struct resource *res;
 	struct feature_platform_data *pdata;
+	enum fpga_devt_type devt_type = FPGA_DEVT_FME;
 	int ret;
 
+	if (type == PORT_ID)
+		devt_type = FPGA_DEVT_PORT;
+
 	/* we will create a new device, commit current device first */
 	ret = build_info_commit_dev(binfo);
 	if (ret)
@@ -294,6 +298,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 		return fdev->id;
 
 	fdev->dev.parent = &binfo->parent_dev->dev;
+	fdev->dev.devt = fpga_get_devt(devt_type, fdev->id);
 
 	/*
 	 * we do not need to care for the memory which is associated with
@@ -942,16 +947,27 @@ static int __init ccidrv_init(void)
 
 	fpga_ids_init();
 
+	ret = fpga_chardev_init();
+	if (ret)
+		goto exit_ids;
+
 	ret = pci_register_driver(&cci_pci_driver);
 	if (ret)
-		fpga_ids_destroy();
+		goto exit_chardev;
 
+	return 0;
+
+exit_chardev:
+	fpga_chardev_uinit();
+exit_ids:
+	fpga_ids_destroy();
 	return ret;
 }
 
 static void __exit ccidrv_exit(void)
 {
 	pci_unregister_driver(&cci_pci_driver);
+	fpga_chardev_uinit();
 	fpga_ids_destroy();
 }
 
Signature · 1 line
1.8.3.1
author
#50 lore raw

PATCH v2 9/22 fpga: intel: pcie: adds fpga_for_each_port callback for fme device

In reply to Wu Hao

For FPGA Management Engine (FME), it requires fpga_for_each_port callback for actions on ports, so export this function from PCIe driver by adding the callback to the platform data.

---
v2: rebased

---
 drivers/fpga/intel-feature-dev.h |  9 +++++++++
 drivers/fpga/intel-pcie.c        | 24 ++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
drivers/fpga/intel-feature-dev.h +9 -0
index d64a51e..06b3fb6 100644
@@ -235,6 +235,9 @@ struct feature_platform_data {
 	struct platform_device *dev;
 	unsigned int disable_count;	/* count for port disable */
 
+	struct platform_device *(*fpga_for_each_port)(struct platform_device *,
+			void *, int (*match)(struct platform_device *, void *));
+
 	int num;			/* number of features */
 	struct feature features[0];
 };
@@ -354,4 +357,10 @@ static inline int fpga_port_reset(struct platform_device *pdev)
 
 	return pdata->features[index].ioaddr;
 }
+
+static inline struct device *
+fpga_feature_dev_to_pcidev(struct platform_device *dev)
+{
+	return dev->dev.parent->parent;
+}
 #endif
drivers/fpga/intel-pcie.c +24 -0
index 54c0e3a..86ea5c7 100644
@@ -209,6 +209,27 @@ static int parse_switch_to(struct build_feature_devs_info *binfo, int bar)
 	return parse_start_from(binfo, bar);
 }
 
+static struct platform_device *fpga_for_each_port(struct platform_device *pdev,
+		     void *data, int (*match)(struct platform_device *, void *))
+{
+	struct device *pci_dev = fpga_feature_dev_to_pcidev(pdev);
+	struct cci_drvdata *drvdata = dev_get_drvdata(pci_dev);
+	struct feature_platform_data *pdata;
+	struct platform_device *port_dev;
+
+	mutex_lock(&drvdata->lock);
+	list_for_each_entry(pdata, &drvdata->port_dev_list, node) {
+		port_dev = pdata->dev;
+
+		if (match(port_dev, data) && get_device(&port_dev->dev))
+			goto exit;
+	}
+	port_dev = NULL;
+exit:
+	mutex_unlock(&drvdata->lock);
+	return port_dev;
+}
+
 static struct build_feature_devs_info *
 build_info_alloc_and_init(struct pci_dev *pdev)
 {
@@ -310,6 +331,9 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	if (!pdata)
 		return -ENOMEM;
 
+	if (type == FME_ID)
+		pdata->fpga_for_each_port = fpga_for_each_port;
+
 	/*
 	 * the count should be initialized to 0 to make sure
 	 *__fpga_port_enable() following __fpga_port_disable()
Signature · 1 line
1.8.3.1
#51 lore raw

Re: [PATCH v2 09/22] fpga: intel: pcie: adds fpga_for_each_port callback for fme device

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

Hi Hao,

Quoted 47 lines

For FPGA Management Engine (FME), it requires fpga_for_each_port callback for actions on ports, so export this function from PCIe driver by adding the callback to the platform data.

---
v2: rebased

---
 drivers/fpga/intel-feature-dev.h |  9 +++++++++
 drivers/fpga/intel-pcie.c        | 24 ++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
drivers/fpga/intel-feature-dev.h +9 -0
index d64a51e..06b3fb6 100644
@@ -235,6 +235,9 @@ struct feature_platform_data {
        struct platform_device *dev;
        unsigned int disable_count;     /* count for port disable */
 
+       struct platform_device *(*fpga_for_each_port)(struct platform_device *,
+                       void *, int (*match)(struct platform_device *, void *));
+
        int num;                        /* number of features */
        struct feature features[0];
 };
@@ -354,4 +357,10 @@ static inline int fpga_port_reset(struct platform_device *pdev)
 
        return pdata->features[index].ioaddr;
 }
+
+static inline struct device *
+fpga_feature_dev_to_pcidev(struct platform_device *dev)
+{
+       return dev->dev.parent->parent;
+}
 #endif
drivers/fpga/intel-pcie.c +0 -0
index 54c0e3a..86ea5c7 100644

drivers/fpga/intel-pcie.c static int parse_switch_to(struct build_feature_devs_info *binfo, int bar) · line 212

Quoted 4 lines
drivers/fpga/intel-pcie.c +1 -0
@@ -209,6 +209,27 @@ static int parse_switch_to(struct build_feature_devs_info *binfo, int bar)
        return parse_start_from(binfo, bar);
 }
 
+static struct platform_device *fpga_for_each_port(struct platform_device *pdev,

Regarding the name of this function, isn't this more of a 'find' function like class_find_device() than a 'for_each' function?

Also please document that the caller will need to put_device().

Thanks,
Alan

Quoted 36 lines
drivers/fpga/intel-pcie.c +23 -1
+                    void *data, int (*match)(struct platform_device *, void *))
+{
+       struct device *pci_dev = fpga_feature_dev_to_pcidev(pdev);
+       struct cci_drvdata *drvdata = dev_get_drvdata(pci_dev);
+       struct feature_platform_data *pdata;
+       struct platform_device *port_dev;
+
+       mutex_lock(&drvdata->lock);
+       list_for_each_entry(pdata, &drvdata->port_dev_list, node) {
+               port_dev = pdata->dev;
+
+               if (match(port_dev, data) && get_device(&port_dev->dev))
+                       goto exit;
+       }
+       port_dev = NULL;
+exit:
+       mutex_unlock(&drvdata->lock);
+       return port_dev;
+}
+
 static struct build_feature_devs_info *
 build_info_alloc_and_init(struct pci_dev *pdev)
 {
@@ -310,6 +331,9 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
        if (!pdata)
                return -ENOMEM;
 
+       if (type == FME_ID)
+               pdata->fpga_for_each_port = fpga_for_each_port;
+
        /*
         * the count should be initialized to 0 to make sure
         *__fpga_port_enable() following __fpga_port_disable()
--

1.8.3.1

author
#52 lore raw

Re: [PATCH v2 09/22] fpga: intel: pcie: adds fpga_for_each_port callback for fme device

Quoted 54 lines · Alan Tull

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

Hi Hao,

Quoted 50 lines

For FPGA Management Engine (FME), it requires fpga_for_each_port callback for actions on ports, so export this function from PCIe driver by adding the callback to the platform data.

---
v2: rebased

---
 drivers/fpga/intel-feature-dev.h |  9 +++++++++
 drivers/fpga/intel-pcie.c        | 24 ++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
drivers/fpga/intel-feature-dev.h +9 -0
index d64a51e..06b3fb6 100644
@@ -235,6 +235,9 @@ struct feature_platform_data {
        struct platform_device *dev;
        unsigned int disable_count;     /* count for port disable */
 
+       struct platform_device *(*fpga_for_each_port)(struct platform_device *,
+                       void *, int (*match)(struct platform_device *, void *));
+
        int num;                        /* number of features */
        struct feature features[0];
 };
@@ -354,4 +357,10 @@ static inline int fpga_port_reset(struct platform_device *pdev)
 
        return pdata->features[index].ioaddr;
 }
+
+static inline struct device *
+fpga_feature_dev_to_pcidev(struct platform_device *dev)
+{
+       return dev->dev.parent->parent;
+}
 #endif
drivers/fpga/intel-pcie.c +0 -0
index 54c0e3a..86ea5c7 100644
@@ -209,6 +209,27 @@ static int parse_switch_to(struct build_feature_devs_info *binfo, int bar)
        return parse_start_from(binfo, bar);
 }
 
Quoted 6 lines

drivers/fpga/intel-pcie.c static int parse_switch_to(struct build_feature_devs_info *binfo, int bar) · line 212

Quoted 1 line
drivers/fpga/intel-pcie.c +1 -0
+static struct platform_device *fpga_for_each_port(struct platform_device *pdev,

Regarding the name of this function, isn't this more of a 'find' function like class_find_device() than a 'for_each' function?

Also please document that the caller will need to put_device().

Yes, agree, fpga_find_port() should be a better name. I will fix this and add comments for this function.

Thanks for your comments.
Hao

Quoted 40 lines

Thanks,
Alan

Quoted 36 lines
drivers/fpga/intel-pcie.c +23 -1
+                    void *data, int (*match)(struct platform_device *, void *))
+{
+       struct device *pci_dev = fpga_feature_dev_to_pcidev(pdev);
+       struct cci_drvdata *drvdata = dev_get_drvdata(pci_dev);
+       struct feature_platform_data *pdata;
+       struct platform_device *port_dev;
+
+       mutex_lock(&drvdata->lock);
+       list_for_each_entry(pdata, &drvdata->port_dev_list, node) {
+               port_dev = pdata->dev;
+
+               if (match(port_dev, data) && get_device(&port_dev->dev))
+                       goto exit;
+       }
+       port_dev = NULL;
+exit:
+       mutex_unlock(&drvdata->lock);
+       return port_dev;
+}
+
 static struct build_feature_devs_info *
 build_info_alloc_and_init(struct pci_dev *pdev)
 {
@@ -310,6 +331,9 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
        if (!pdata)
                return -ENOMEM;
 
+       if (type == FME_ID)
+               pdata->fpga_for_each_port = fpga_for_each_port;
+
        /*
         * the count should be initialized to 0 to make sure
         *__fpga_port_enable() following __fpga_port_disable()
--

1.8.3.1

author
#53 lore raw

PATCH v2 10/22 fpga: intel: add feature device infrastructure

In reply to Wu Hao

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

This patch abstracts the common operations of the sub features, and defines the feature_ops data structure, including init, uinit and ioctl function pointers. And this patch adds some common helper functions for FME and AFU drivers, e.g feature_dev_use_begin/end which are used to ensure exclusive usage of the feature device file.

---
v2: rebased

---
 drivers/fpga/intel-feature-dev.c | 66 +++++++++++++++++++++++++++++++++++++
 drivers/fpga/intel-feature-dev.h | 71 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+)
drivers/fpga/intel-feature-dev.c +66 -0
index 45788fe..833bfa9 100644
@@ -51,6 +51,72 @@ int port_feature_num(void)
 	return PORT_FEATURE_ID_MAX;
 }
 
+int fme_feature_to_resource_index(int feature_id)
+{
+	WARN_ON(feature_id >= FME_FEATURE_ID_MAX);
+	return feature_id;
+}
+
+void fpga_dev_feature_uinit(struct platform_device *pdev)
+{
+	struct feature *feature;
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+	fpga_dev_for_each_feature(pdata, feature)
+		if (feature->ops) {
+			feature->ops->uinit(pdev, feature);
+			feature->ops = NULL;
+		}
+}
+EXPORT_SYMBOL_GPL(fpga_dev_feature_uinit);
+
+static int
+feature_instance_init(struct platform_device *pdev,
+		      struct feature_platform_data *pdata,
+		      struct feature *feature, struct feature_driver *drv)
+{
+	int ret;
+
+	WARN_ON(!feature->ioaddr);
+
+	ret = drv->ops->init(pdev, feature);
+	if (ret)
+		return ret;
+
+	feature->ops = drv->ops;
+	return ret;
+}
+
+int fpga_dev_feature_init(struct platform_device *pdev,
+			  struct feature_driver *feature_drvs)
+{
+	struct feature *feature;
+	struct feature_driver *drv = feature_drvs;
+	struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	int ret;
+
+	while (drv->ops) {
+		fpga_dev_for_each_feature(pdata, feature) {
+			/* skip the feature which is not initialized. */
+			if (!feature->name)
+				continue;
+
+			if (!strcmp(drv->name, feature->name)) {
+				ret = feature_instance_init(pdev, pdata,
+							    feature, drv);
+				if (ret)
+					goto exit;
+			}
+		}
+		drv++;
+	}
+	return 0;
+exit:
+	fpga_dev_feature_uinit(pdev);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(fpga_dev_feature_init);
+
 struct fpga_chardev_info {
 	const char *name;
 	dev_t devt;
drivers/fpga/intel-feature-dev.h +71 -0

116 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author
#54 lore raw

PATCH v2 11/22 fpga: intel: add FPGA Management Engine driver basic framework

In reply to Wu Hao

From: Kang Luwei <[email protected]>

The FPGA Management Engine (FME) provides power, thermal management, performance counters, partial reconfiguration and other functions. For each function, it is packaged into a private feature linked to the FME feature device in the 'Device Feature List'. It's a platform device created by Intel FPGA PCIe driver.

This patch adds the basic framework of FME platform driver. It defines sub feature drivers to handle the different sub features, including init, uinit and ioctl. It also registers the file operations for the device file.

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

---
 drivers/fpga/Kconfig          |   8 +++
 drivers/fpga/Makefile         |   2 +
 drivers/fpga/intel-fme-main.c | 156 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 166 insertions(+)
 create mode 100644 drivers/fpga/intel-fme-main.c
drivers/fpga/Kconfig +8 -0
index 3f3b7f4..b91458f 100644
@@ -143,6 +143,14 @@ config INTEL_FPGA_PCI
 
 	  To compile this as a module, choose M here.
 
+config INTEL_FPGA_FME
+	tristate "Intel FPGA FME Driver"
+	depends on INTEL_FPGA_PCI
+	help
+	  This is the driver for FPGA Management Engine which implements
+	  all FPGA platform level management features. There shall be 1
+	  FME per Intel FPGA.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +2 -0
index ad24b3d..09bf281 100644
@@ -30,5 +30,7 @@ obj-$(CONFIG_OF_FPGA_REGION)		+= of-fpga-region.o
 
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)		+= intel-fpga-pci.o
+obj-$(CONFIG_INTEL_FPGA_FME)		+= intel-fpga-fme.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
+intel-fpga-fme-objs := intel-fme-main.o
drivers/fpga/intel-fme-main.c new file +156 -0

156 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author
#55 lore raw

PATCH v2 12/22 fpga: intel: fme: add header sub feature support

In reply to Wu Hao

From: Kang Luwei <[email protected]>

The header register set is always present for FPGA Management Engine (FME), this patch implements init and uinit function for header sub feature and introduce several read-only sysfs interfaces for the capability and status.

Sysfs interfaces:
* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/ports_num Read-only. Number of ports implemented

* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/bitstream_id Read-only. Blue Bitstream identifier number

* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/bitstream_metadata Read-only. Blue Bitstream meta data

---
v2: add sysfs documentation

---
 .../ABI/testing/sysfs-platform-intel-fpga-fme      | 19 ++++++++
 drivers/fpga/intel-feature-dev.h                   |  3 ++
 drivers/fpga/intel-fme-main.c                      | 55 ++++++++++++++++++++++
 3 files changed, 77 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-fme
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme new file +19 -0
new file mode 100644
index 0000000..783cfa9
@@ -0,0 +1,19 @@
+What:		/sys/bus/platform/devices/intel-fpga-fme.0/ports_num
+Date:		June 2017
+KernelVersion:  4.12
+Contact:	Wu Hao <[email protected]>
+Description:	Read-only. One Intel FPGA device may have more than 1
+		port/Accelerator Function Unit (AFU). It returns the
+		number of ports on the FPGA device when read it.
+
+What:		/sys/bus/platform/devices/intel-fpga-fme.0/bitstream_id
+Date:		June 2017
+KernelVersion:  4.12
+Contact:	Wu Hao <[email protected]>
+Description:	Read-only. It returns Blue Bitstream identifier number.
+
+What:		/sys/bus/platform/devices/intel-fpga-fme.0/bitstream_meta
+Date:		June 2017
+KernelVersion:  4.12
+Contact:	Wu Hao <[email protected]>
+Description:	Read-only. It returns Blue Bitstream meta data.
drivers/fpga/intel-feature-dev.h +3 -0
index 635b857..3f97b75 100644
@@ -138,6 +138,9 @@ struct feature_fme_header {
 	u64 rsvd[2];
 	struct feature_fme_capability capability;
 	struct feature_fme_port port[MAX_FPGA_PORT_NUM];
+	u64 rsvd1;
+	u64 bitstream_id;
+	u64 bitstream_md;
 };
 
 /* FME Thermal Sub Feature Register Set */
drivers/fpga/intel-fme-main.c +55 -0

70 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#56 lore raw

Re: [PATCH v2 12/22] fpga: intel: fme: add header sub feature support

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

Hi Hao,

I'm making my way through this (very large) patchset. Some minor comments below.

Quoted 6 lines

From: Kang Luwei <[email protected]>

The header register set is always present for FPGA Management Engine (FME), this patch implements init and uinit function for header sub feature and introduce several read-only sysfs interfaces for the capability and status.

Quoted 6 lines

Sysfs interfaces:
* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/ports_num Read-only. Number of ports implemented

* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/bitstream_id Read-only. Blue Bitstream identifier number

Blue and Green bitstreams are an Intel implementation terminology. I see that you've defined them in drivers/fpga, but it will be helpful to add in "static region" and "partial reconfiguration region" added in any API documentation files that use the green/blue terminology to keep it accessible.

Quoted 29 lines

* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/bitstream_metadata Read-only. Blue Bitstream meta data

---
v2: add sysfs documentation

---
 .../ABI/testing/sysfs-platform-intel-fpga-fme      | 19 ++++++++
 drivers/fpga/intel-feature-dev.h                   |  3 ++
 drivers/fpga/intel-fme-main.c                      | 55 ++++++++++++++++++++++
 3 files changed, 77 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-fme
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme new file +3 -0
new file mode 100644
index 0000000..783cfa9
@@ -0,0 +1,19 @@
+What:          /sys/bus/platform/devices/intel-fpga-fme.0/ports_num
+Date:          June 2017
+KernelVersion:  4.12

Documentation/ABI/testing/sysfs-platform-intel-fpga-fme line 13

Quoted 10 lines
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme new file +10 -0
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. One Intel FPGA device may have more than 1
+               port/Accelerator Function Unit (AFU). It returns the
+               number of ports on the FPGA device when read it.
+
+What:          /sys/bus/platform/devices/intel-fpga-fme.0/bitstream_id
+Date:          June 2017
+KernelVersion:  4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. It returns Blue Bitstream identifier number.

Here

Documentation/ABI/testing/sysfs-platform-intel-fpga-fme line 19

Quoted 6 lines
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme new file +6 -0
+
+What:          /sys/bus/platform/devices/intel-fpga-fme.0/bitstream_meta
+Date:          June 2017
+KernelVersion:  4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. It returns Blue Bitstream meta data.

And here

Quoted 92 lines
drivers/fpga/intel-feature-dev.h +3 -0
index 635b857..3f97b75 100644
@@ -138,6 +138,9 @@ struct feature_fme_header {
        u64 rsvd[2];
        struct feature_fme_capability capability;
        struct feature_fme_port port[MAX_FPGA_PORT_NUM];
+       u64 rsvd1;
+       u64 bitstream_id;
+       u64 bitstream_md;
 };
 
 /* FME Thermal Sub Feature Register Set */
drivers/fpga/intel-fme-main.c +55 -1
index c16cf81..dfbb17c 100644
@@ -21,15 +21,70 @@
 
 #include "intel-feature-dev.h"
 
+static ssize_t ports_num_show(struct device *dev,
+                             struct device_attribute *attr, char *buf)
+{
+       struct feature_fme_header *fme_hdr
+               = get_feature_ioaddr_by_index(dev, FME_FEATURE_ID_HEADER);
+       struct feature_fme_capability fme_capability;
+
+       fme_capability.csr = readq(&fme_hdr->capability);
+
+       return scnprintf(buf, PAGE_SIZE, "%d\n", fme_capability.num_ports);
+}
+static DEVICE_ATTR_RO(ports_num);
+
+static ssize_t bitstream_id_show(struct device *dev,
+                                struct device_attribute *attr, char *buf)
+{
+       struct feature_fme_header *fme_hdr
+               = get_feature_ioaddr_by_index(dev, FME_FEATURE_ID_HEADER);
+       u64 bitstream_id = readq(&fme_hdr->bitstream_id);
+
+       return scnprintf(buf, PAGE_SIZE, "0x%llx\n",
+                               (unsigned long long)bitstream_id);
+}
+static DEVICE_ATTR_RO(bitstream_id);
+
+static ssize_t bitstream_metadata_show(struct device *dev,
+                                      struct device_attribute *attr, char *buf)
+{
+       struct feature_fme_header *fme_hdr
+               = get_feature_ioaddr_by_index(dev, FME_FEATURE_ID_HEADER);
+       u64 bitstream_md = readq(&fme_hdr->bitstream_md);
+
+       return scnprintf(buf, PAGE_SIZE, "0x%llx\n",
+                               (unsigned long long)bitstream_md);
+}
+static DEVICE_ATTR_RO(bitstream_metadata);
+
+static const struct attribute *fme_hdr_attrs[] = {
+       &dev_attr_ports_num.attr,
+       &dev_attr_bitstream_id.attr,
+       &dev_attr_bitstream_metadata.attr,
+       NULL,
+};
+
 static int fme_hdr_init(struct platform_device *pdev, struct feature *feature)
 {
+       struct feature_fme_header *fme_hdr = feature->ioaddr;
+       int ret;
+
        dev_dbg(&pdev->dev, "FME HDR Init.\n");
+       dev_dbg(&pdev->dev, "FME cap %llx.\n",
+                               (unsigned long long)fme_hdr->capability.csr);
+
+       ret = sysfs_create_files(&pdev->dev.kobj, fme_hdr_attrs);
+       if (ret)
+               return ret;
+
        return 0;
 }
 
 static void fme_hdr_uinit(struct platform_device *pdev, struct feature *feature)
 {
        dev_dbg(&pdev->dev, "FME HDR UInit.\n");
+       sysfs_remove_files(&pdev->dev.kobj, fme_hdr_attrs);
 }
 
 struct feature_ops fme_hdr_ops = {
--

1.8.3.1

author
#57 lore raw

RE: [PATCH v2 12/22] fpga: intel: fme: add header sub feature support

Quoted 7 lines · Wu Hao

Hi Hao,

I'm making my way through this (very large) patchset. Some minor comments below.

Hi Alan

Thanks for your review. : )

Quoted 10 lines
Quoted 10 lines

From: Kang Luwei <[email protected]>

The header register set is always present for FPGA Management Engine (FME), this patch implements init and uinit function for header sub feature and introduce several read-only sysfs interfaces for the capability and status.

Sysfs interfaces:
* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/ports_num Read-only. Number of ports implemented

Quoted 9 lines
Quoted 2 lines

* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/bitstream_id Read-only. Blue Bitstream identifier number

Blue and Green bitstreams are an Intel implementation terminology. I see that you've defined them in drivers/fpga, but it will be helpful to add in "static region" and "partial reconfiguration region" added in any API documentation files that use the green/blue terminology to keep it accessible.

Sure, thanks for your suggestion, will update it like this.

* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/bitstream_id Read-only. Blue Bitstream (static FPGA region) identifier number

* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/bitstream_metadata Read-only. Blue Bitstream (static FPGA region) meta data

Quoted 32 lines
Quoted 8 lines
Quoted 8 lines

---
v2: add sysfs documentation

---
 .../ABI/testing/sysfs-platform-intel-fpga-fme      | 19 ++++++++
 drivers/fpga/intel-feature-dev.h                   |  3 ++
 drivers/fpga/intel-fme-main.c                      | 55 ++++++++++++++++++++++
 3 files changed, 77 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-

fme

Review

Quoted 2 lines
(quoted diff) +0 -0

b/Documentation/ABI/testing/sysfs-platform-intel-fpga-fme

Quoted 12 lines

new file mode 100644
index 0000000..783cfa9

Documentation/ABI/testing/sysfs-platform-intel-fpga-fme new file +7 -0
@@ -0,0 +1,19 @@
+What:          /sys/bus/platform/devices/intel-fpga-fme.0/ports_num
+Date:          June 2017
+KernelVersion:  4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. One Intel FPGA device may have more than 1
+               port/Accelerator Function Unit (AFU). It returns the
+               number of ports on the FPGA device when read it.
Quoted 8 lines

Documentation/ABI/testing/sysfs-platform-intel-fpga-fme line 13

Quoted 6 lines
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme new file +6 -0
+
+What:          /sys/bus/platform/devices/intel-fpga-fme.0/bitstream_id
+Date:          June 2017
+KernelVersion:  4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. It returns Blue Bitstream identifier number.

Here

Will update this patch as below.

+Description: Read-only. It returns Blue Bitstream (static FPGA region)
+ identifier number.

Quoted 9 lines

Documentation/ABI/testing/sysfs-platform-intel-fpga-fme line 19

Quoted 7 lines
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme new file +6 -0
+
+What:          /sys/bus/platform/devices/intel-fpga-fme.0/bitstream_meta
+Date:          June 2017
+KernelVersion:  4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. It returns Blue Bitstream meta data.

And here

Will update this patch as below.

+Description: Read-only. It returns Blue Bitstream (static FPGA region)
+ meta data.

Thanks
Hao

#58 lore raw

Re: [PATCH v2 12/22] fpga: intel: fme: add header sub feature support

Quoted 11 lines · Hao
Quoted 7 lines · Wu Hao

Hi Hao,

I'm making my way through this (very large) patchset. Some minor comments below.

Hi Alan

Thanks for your review. : )

Hi Hao,

Thanks, this looks good and will be helpful for folks who are new to this.

Alan

Quoted 92 lines
Quoted 11 lines
Quoted 11 lines

From: Kang Luwei <[email protected]>

The header register set is always present for FPGA Management Engine (FME), this patch implements init and uinit function for header sub feature and introduce several read-only sysfs interfaces for the capability and status.

Sysfs interfaces:
* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/ports_num Read-only. Number of ports implemented

Quoted 9 lines
Quoted 2 lines

* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/bitstream_id Read-only. Blue Bitstream identifier number

Blue and Green bitstreams are an Intel implementation terminology. I see that you've defined them in drivers/fpga, but it will be helpful to add in "static region" and "partial reconfiguration region" added in any API documentation files that use the green/blue terminology to keep it accessible.

Sure, thanks for your suggestion, will update it like this.

* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/bitstream_id Read-only. Blue Bitstream (static FPGA region) identifier number

* /sys/class/fpga/<fpga.x>/<intel-fpga-fme.x>/bitstream_metadata Read-only. Blue Bitstream (static FPGA region) meta data

Quoted 32 lines
Quoted 8 lines
Quoted 8 lines

---
v2: add sysfs documentation

---
 .../ABI/testing/sysfs-platform-intel-fpga-fme      | 19 ++++++++
 drivers/fpga/intel-feature-dev.h                   |  3 ++
 drivers/fpga/intel-fme-main.c                      | 55 ++++++++++++++++++++++
 3 files changed, 77 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-

fme

Review

Quoted 2 lines
(quoted diff) +0 -0

b/Documentation/ABI/testing/sysfs-platform-intel-fpga-fme

Quoted 12 lines

new file mode 100644
index 0000000..783cfa9

Documentation/ABI/testing/sysfs-platform-intel-fpga-fme new file +7 -0
@@ -0,0 +1,19 @@
+What:          /sys/bus/platform/devices/intel-fpga-fme.0/ports_num
+Date:          June 2017
+KernelVersion:  4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. One Intel FPGA device may have more than 1
+               port/Accelerator Function Unit (AFU). It returns the
+               number of ports on the FPGA device when read it.
Quoted 8 lines

Documentation/ABI/testing/sysfs-platform-intel-fpga-fme line 13

Quoted 6 lines
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme new file +6 -0
+
+What:          /sys/bus/platform/devices/intel-fpga-fme.0/bitstream_id
+Date:          June 2017
+KernelVersion:  4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. It returns Blue Bitstream identifier number.

Here

Will update this patch as below.

(quoted diff) +2 -0
+Description:   Read-only. It returns Blue Bitstream (static FPGA region)
+               identifier number.
Quoted 10 lines

Documentation/ABI/testing/sysfs-platform-intel-fpga-fme line 19

Quoted 8 lines
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme new file +6 -0
+
+What:          /sys/bus/platform/devices/intel-fpga-fme.0/bitstream_meta
+Date:          June 2017
+KernelVersion:  4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. It returns Blue Bitstream meta data.

And here

Will update this patch as below.

(quoted diff) +2 -0
+Description:   Read-only. It returns Blue Bitstream (static FPGA region)
+               meta data.

Thanks
Hao

author
#59 lore raw

PATCH v2 13/22 fpga: intel: fme: add FPGA_GET_API_VERSION/CHECK_EXTENSION ioctls support

In reply to Wu Hao

FPGA_GET_API_VERSION and FPGA_CHECK_EXTENSION ioctls are common ones which need to be supported by all feature devices drivers including FME and AFU. Userspace application can use these ioctl interfaces to get the API info and check if specific extension is supported or not in current driver.

This patch implements above 2 ioctls in Intel FPGA Management Engine (FME) driver.

---
v2: switched to GPLv2 license.

---
 Documentation/ioctl/ioctl-number.txt |  1 +
 drivers/fpga/intel-fme-main.c        | 12 +++++++++
 include/uapi/linux/intel-fpga.h      | 50 ++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)
 create mode 100644 include/uapi/linux/intel-fpga.h
Documentation/ioctl/ioctl-number.txt +1 -0
index 1e9fcb4..a0d8be5 100644
@@ -324,6 +324,7 @@ Code  Seq#(hex)	Include File		Comments
 0xB3	00	linux/mmc/ioctl.h
 0xB4	00-0F	linux/gpio.h		<mailto:[email protected]>
 0xB5	00-0F	uapi/linux/rpmsg.h	<mailto:[email protected]>
+0xB6	all	linux/intel-fpga.h
 0xC0	00-0F	linux/usb/iowarrior.h
 0xCA	00-0F	uapi/misc/cxl.h
 0xCA	80-8F	uapi/scsi/cxlflash_ioctl.h
drivers/fpga/intel-fme-main.c +12 -0
index dfbb17c..9fd0baf 100644
@@ -18,6 +18,7 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/intel-fpga.h>
 
 #include "intel-feature-dev.h"
 
@@ -102,6 +103,13 @@ struct feature_ops fme_hdr_ops = {
 	},
 };
 
+static long fme_ioctl_check_extension(struct feature_platform_data *pdata,
+				     unsigned long arg)
+{
+	/* No extension support for now */
+	return 0;
+}
+
 static int fme_open(struct inode *inode, struct file *filp)
 {
 	struct platform_device *fdev = fpga_inode_to_feature_dev(inode);
@@ -140,6 +148,10 @@ static long fme_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
 
 	switch (cmd) {
+	case FPGA_GET_API_VERSION:
+		return FPGA_API_VERSION;
+	case FPGA_CHECK_EXTENSION:
+		return fme_ioctl_check_extension(pdata, arg);
 	default:
 		/*
 		 * Let sub-feature's ioctl function to handle the cmd
include/uapi/linux/intel-fpga.h new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
1.8.3.1
Acked-by on v2 13/22
#60 lore raw

Re: [PATCH v2 13/22] fpga: intel: fme: add FPGA_GET_API_VERSION/CHECK_EXTENSION ioctls support

Quoted 8 lines · Wu Hao

FPGA_GET_API_VERSION and FPGA_CHECK_EXTENSION ioctls are common ones which need to be supported by all feature devices drivers including FME and AFU. Userspace application can use these ioctl interfaces to get the API info and check if specific extension is supported or not in current driver.

This patch implements above 2 ioctls in Intel FPGA Management Engine (FME) driver.

Quoted 6 lines
Quoted 117 lines

---
v2: switched to GPLv2 license.

---
 Documentation/ioctl/ioctl-number.txt |  1 +
 drivers/fpga/intel-fme-main.c        | 12 +++++++++
 include/uapi/linux/intel-fpga.h      | 50 ++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)
 create mode 100644 include/uapi/linux/intel-fpga.h
Documentation/ioctl/ioctl-number.txt +1 -0
index 1e9fcb4..a0d8be5 100644
@@ -324,6 +324,7 @@ Code  Seq#(hex)     Include File            Comments
 0xB3   00      linux/mmc/ioctl.h
 0xB4   00-0F   linux/gpio.h            <mailto:[email protected]>
 0xB5   00-0F   uapi/linux/rpmsg.h      <mailto:[email protected]>
+0xB6   all     linux/intel-fpga.h
 0xC0   00-0F   linux/usb/iowarrior.h
 0xCA   00-0F   uapi/misc/cxl.h
 0xCA   80-8F   uapi/scsi/cxlflash_ioctl.h
drivers/fpga/intel-fme-main.c +12 -0
index dfbb17c..9fd0baf 100644
@@ -18,6 +18,7 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/intel-fpga.h>
 
 #include "intel-feature-dev.h"
 
@@ -102,6 +103,13 @@ struct feature_ops fme_hdr_ops = {
        },
 };
 
+static long fme_ioctl_check_extension(struct feature_platform_data *pdata,
+                                    unsigned long arg)
+{
+       /* No extension support for now */
+       return 0;
+}
+
 static int fme_open(struct inode *inode, struct file *filp)
 {
        struct platform_device *fdev = fpga_inode_to_feature_dev(inode);
@@ -140,6 +148,10 @@ static long fme_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
        dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
 
        switch (cmd) {
+       case FPGA_GET_API_VERSION:
+               return FPGA_API_VERSION;
+       case FPGA_CHECK_EXTENSION:
+               return fme_ioctl_check_extension(pdata, arg);
        default:
                /*
                 * Let sub-feature's ioctl function to handle the cmd
include/uapi/linux/intel-fpga.h new file +50 -1
new file mode 100644
index 0000000..e340ef9
@@ -0,0 +1,50 @@
+/*
+ * Header File for Intel FPGA User API
+ *
+ * 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 _UAPI_LINUX_INTEL_FPGA_H
+#define _UAPI_LINUX_INTEL_FPGA_H
+
+#define FPGA_API_VERSION 0
+
+/*
+ * The IOCTL interface for Intel FPGA is designed for extensibility by
+ * embedding the structure length (argsz) and flags into structures passed
+ * between kernel and userspace. This design referenced the VFIO IOCTL
+ * interface (include/uapi/linux/vfio.h).
+ */
+
+#define FPGA_MAGIC 0xB6
+
+#define FPGA_BASE 0
+
+/**
+ * FPGA_GET_API_VERSION - _IO(FPGA_MAGIC, FPGA_BASE + 0)
+ *
+ * Report the version of the driver API.
+ * Return: Driver API Version.
+ */
+
+#define FPGA_GET_API_VERSION   _IO(FPGA_MAGIC, FPGA_BASE + 0)
+
+/**
+ * FPGA_CHECK_EXTENSION - _IO(FPGA_MAGIC, FPGA_BASE + 1)
+ *
+ * Check whether an extension is supported.
+ * Return: 0 if not supported, otherwise the extension is supported.
+ */
+
+#define FPGA_CHECK_EXTENSION   _IO(FPGA_MAGIC, FPGA_BASE + 1)
+
+#endif /* _UAPI_INTEL_FPGA_H */
--

1.8.3.1

author
#61 lore raw

PATCH v2 14/22 fpga: intel: fme: add partial reconfiguration sub feature support

In reply to Wu Hao

From: Kang Luwei <[email protected]>

Partial Reconfiguration (PR) is the most important function for FME. It allows reconfiguration for given Port/Accelerated Function Unit (AFU).

It creates platform devices for fpga-mgr, fpga-regions and fpga-bridges, and invokes fpga-region's interface (fpga_region_program_fpga) for PR operation once PR request received via ioctl. Below user space interface is exposed by this sub feature.

Ioctl interface:
* FPGA_FME_PORT_PR
Do partial reconfiguration per information from userspace, including target port(AFU), buffer size and address info. It returns error code to userspace if failed. For detailed PR error information, user needs to read fpga-mgr's status sysfs interface.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
removed status from FPGA_FME_PORT_PR ioctl data structure. added platform devices creation for fpga-mgr/fpga-region/fpga-bridge. switched to fpga-region interface fpga_region_program_fpga for PR. fixed comments from Alan Tull on FPGA_MGR_PARTIAL_RECONFIG flag usage. fixed kbuild warnings.

---
 drivers/fpga/Makefile           |   2 +-
 drivers/fpga/intel-fme-main.c   |  44 +++-
 drivers/fpga/intel-fme-pr.c     | 494 ++++++++++++++++++++++++++++++++++++++++
 drivers/fpga/intel-fme.h        |  57 +++++
 include/uapi/linux/intel-fpga.h |  27 +++
 5 files changed, 622 insertions(+), 2 deletions(-)
 create mode 100644 drivers/fpga/intel-fme-pr.c
 create mode 100644 drivers/fpga/intel-fme.h
drivers/fpga/Makefile +1 -1
index 09bf281..acda5ca 100644
@@ -33,4 +33,4 @@ obj-$(CONFIG_INTEL_FPGA_PCI)		+= intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)		+= intel-fpga-fme.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
-intel-fpga-fme-objs := intel-fme-main.o
+intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-fme-main.c +43 -1

81 lines not on this page · raw message

drivers/fpga/intel-fme-pr.c new file +494 -0

494 lines not on this page · raw message

drivers/fpga/intel-fme.h new file +57 -0

57 lines not on this page · raw message

include/uapi/linux/intel-fpga.h +27 -0
index e340ef9..be295ae 100644
@@ -16,6 +16,8 @@
 #ifndef _UAPI_LINUX_INTEL_FPGA_H
 #define _UAPI_LINUX_INTEL_FPGA_H
 
+#include <linux/types.h>
+
 #define FPGA_API_VERSION 0
 
 /*
@@ -28,6 +30,7 @@
 #define FPGA_MAGIC 0xB6
 
 #define FPGA_BASE 0
+#define FME_BASE 0x80
 
 /**
  * FPGA_GET_API_VERSION - _IO(FPGA_MAGIC, FPGA_BASE + 0)
@@ -47,4 +50,28 @@
 
 #define FPGA_CHECK_EXTENSION	_IO(FPGA_MAGIC, FPGA_BASE + 1)
 
+/* IOCTLs for FME file descriptor */
+
+/**
+ * FPGA_FME_PORT_PR - _IOW(FPGA_MAGIC, FME_BASE + 0, struct fpga_fme_port_pr)
+ *
+ * Driver does Partial Reconfiguration based on Port ID and Buffer (Image)
+ * provided by caller.
+ * Return: 0 on success, -errno on failure.
+ * If FPGA_FME_PORT_PR returns -EIO, that indicates the HW has detected
+ * some errors during PR, under this case, the user can fetch HW error info
+ * from the status of FME's fpga manager.
+ */
+
+struct fpga_fme_port_pr {
+	/* Input */
+	__u32 argsz;		/* Structure length */
+	__u32 flags;		/* Zero for now */
+	__u32 port_id;
+	__u32 buffer_size;
+	__u64 buffer_address;	/* Userspace address to the buffer for PR */
+};
+
+#define FPGA_FME_PORT_PR	_IO(FPGA_MAGIC, FME_BASE + 0)
+
 #endif /* _UAPI_INTEL_FPGA_H */
Signature · 1 line
1.8.3.1
author
#62 lore raw

PATCH v2 15/22 fpga: intel: add fpga manager platform driver for FME

In reply to Wu Hao

This patch adds fpga manager driver for Intel FPGA Management Engine(FME). It implements fpga_manager_ops for FPGA Partial Reconfiguration function.

---
 .../ABI/testing/sysfs-platform-intel-fpga-fme-mgr  |   8 +
 drivers/fpga/Kconfig                               |   7 +
 drivers/fpga/Makefile                              |   1 +
 drivers/fpga/intel-feature-dev.h                   |  75 +++++
 drivers/fpga/intel-fpga-fme-mgr.c                  | 307 +++++++++++++++++++++
 5 files changed, 398 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-fme-mgr
 create mode 100644 drivers/fpga/intel-fpga-fme-mgr.c
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme-mgr new file +8 -0
new file mode 100644
index 0000000..40771fb
@@ -0,0 +1,8 @@
+What:		/sys/bus/platform/devices/intel-fpga-fme-mgr.0/interface_id
+Date:		June 2017
+KernelVersion:  4.12
+Contact:	Wu Hao <[email protected]>
+Description:	Read-only. It returns interface id of partial reconfiguration
+		hardware. Userspace could use this information to check if
+		current hardware is compatible with given image before FPGA
+		programming.
drivers/fpga/Kconfig +7 -0
index b91458f..6f2f623 100644
@@ -151,6 +151,13 @@ config INTEL_FPGA_FME
 	  all FPGA platform level management features. There shall be 1
 	  FME per Intel FPGA.
 
+config INTEL_FPGA_FME_MGR
+	tristate "Intel FPGA FME Manager Driver"
+	depends on INTEL_FPGA_FME
+	help
+	  Say Y to enable FPGA Manager driver for Intel FPGA Management
+	  Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index acda5ca..d1d588b 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_OF_FPGA_REGION)		+= of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)		+= intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)		+= intel-fpga-fme.o
+obj-$(CONFIG_INTEL_FPGA_FME_MGR)	+= intel-fpga-fme-mgr.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-feature-dev.h +75 -0

83 lines not on this page · raw message

drivers/fpga/intel-fpga-fme-mgr.c new file +307 -0

307 lines not on this page · raw message

Signature · 1 line
1.8.3.1
maintainer
#63 lore raw

Re: [PATCH v2 15/22] fpga: intel: add fpga manager platform driver for FME

Hi Hao,

Quoted 128 lines · Wu Hao

This patch adds fpga manager driver for Intel FPGA Management Engine(FME). It implements fpga_manager_ops for FPGA Partial Reconfiguration function.

---
 .../ABI/testing/sysfs-platform-intel-fpga-fme-mgr  |   8 +
 drivers/fpga/Kconfig                               |   7 +
 drivers/fpga/Makefile                              |   1 +
 drivers/fpga/intel-feature-dev.h                   |  75 +++++
 drivers/fpga/intel-fpga-fme-mgr.c                  | 307 +++++++++++++++++++++
 5 files changed, 398 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-fme-mgr
 create mode 100644 drivers/fpga/intel-fpga-fme-mgr.c
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme-mgr new file +8 -0
new file mode 100644
index 0000000..40771fb
@@ -0,0 +1,8 @@
+What:		/sys/bus/platform/devices/intel-fpga-fme-mgr.0/interface_id
+Date:		June 2017
+KernelVersion:  4.12
+Contact:	Wu Hao <[email protected]>
+Description:	Read-only. It returns interface id of partial reconfiguration
+		hardware. Userspace could use this information to check if
+		current hardware is compatible with given image before FPGA
+		programming.
drivers/fpga/Kconfig +7 -0
index b91458f..6f2f623 100644
@@ -151,6 +151,13 @@ config INTEL_FPGA_FME
 	  all FPGA platform level management features. There shall be 1
 	  FME per Intel FPGA.
 
+config INTEL_FPGA_FME_MGR
+	tristate "Intel FPGA FME Manager Driver"
+	depends on INTEL_FPGA_FME
+	help
+	  Say Y to enable FPGA Manager driver for Intel FPGA Management
+	  Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index acda5ca..d1d588b 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_OF_FPGA_REGION)		+= of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)		+= intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)		+= intel-fpga-fme.o
+obj-$(CONFIG_INTEL_FPGA_FME_MGR)	+= intel-fpga-fme-mgr.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-feature-dev.h +55 -0
index 3f97b75..f33923b 100644
@@ -164,8 +164,83 @@ struct feature_fme_err {
 };
 
 /* FME Partial Reconfiguration Sub Feature Register Set */
+/* FME PR Control Register */
+struct feature_fme_pr_ctl {
+	union {
+		u64 csr;
+		struct {
+			u64 pr_reset:1;		/* Reset PR Engine */
+			u64 rsvdz1:3;
+			u64 pr_reset_ack:1;	/* Reset PR Engine Ack */
+			u64 rsvdz2:3;
+			u64 pr_regionid:2;	/* PR Region ID */
+			u64 rsvdz3:2;
+			u64 pr_start_req:1;	/* PR Start Request */
+			u64 pr_push_complete:1;	/* PR Data push complete */
+			u64 pr_kind:1;		/* Load Customer or Intel GBS */
+			u64 rsvdz4:17;
+			u64 config_data:32;
+		};
+	};
+};
+
+/* FME PR Status Register */
+struct feature_fme_pr_status {
+	union {
+		u64 csr;
+		struct {
+			u64 pr_credit:9;	/* Number of PR Credits */
+			u64 rsvdz1:7;
+			u64 pr_status:1;	/* PR Operation status */
+			u64 rsvdz2:3;
+			u64 pr_ctrlr_status:3;	/* Controller status */
+			u64 rsvdz3:1;
+			u64 pr_host_status:4;	/* PR Host status */
+			u64 rsvdz4:36;
+		};
+	};
+};
+
+/* FME PR Data Register */
+struct feature_fme_pr_data {
+	union {
+		u64 csr;
+		struct {
+			/* PR data from the raw-binary file */
+			u64 pr_data_raw:32;
+			u64 rsvd:32;
+		};
+	};
+};
+
+/* FME PR Error Register */
+struct feature_fme_pr_error {
+	union {
+		u64 csr;
+		struct {
+			u64 operation_err:1;	/* Previous PR error detected */

drivers/fpga/intel-feature-dev.h struct feature_fme_err { · line 231

Quoted 10 lines
drivers/fpga/intel-feature-dev.h +10 -0
+			u64 crc_err:1;		/* CRC error detected */
+			u64 incompatiable_bs:1; /* Incompatiable Bitstream */
+			u64 protocol_err:1;	/* Data push protocol error */
+			u64 fifo_overflow:1;	/* Data fifo overflow */
+			u64 rsvdz1:1;
+			u64 secure_load_err:1;	/* Secure PR load error */
+			u64 rsvdz2:57;
+		};
+	};
+};

From what I've seen [1], [2] people weren't too happy with using bitfields, any reason we can't use explicit masking and shifts? I don't know if this is still an issue.

Quoted 76 lines
drivers/fpga/intel-feature-dev.h +10 -0
+
 struct feature_fme_pr {
 	struct feature_header header;
+	struct feature_fme_pr_ctl control;
+	struct feature_fme_pr_status status;
+	struct feature_fme_pr_data data;
+	struct feature_fme_pr_error error;
+
+	u64 rsvd[16];
+
+	u64 intfc_id_l;		/* PR interface Id Low */
+	u64 intfc_id_h;		/* PR interface Id High */
 };
 
 /* PORT Header Register Set */
drivers/fpga/intel-fpga-fme-mgr.c new file +55 -0
new file mode 100644
index 0000000..5f93dfb
@@ -0,0 +1,307 @@
+/*
+ * FPGA Manager Driver for Intel FPGA Management Engine (FME)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Joseph Grecco <[email protected]>
+ *   Enno Luebbers <[email protected]>
+ *   Tim Whisonant <[email protected]>
+ *   Ananda Ravuri <[email protected]>
+ *   Christopher Rauer <[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/module.h>
+#include <linux/iopoll.h>
+#include <linux/fpga/fpga-mgr.h>
+
+#include "intel-feature-dev.h"
+#include "intel-fme.h"
+
+#define PR_WAIT_TIMEOUT   8000000
+#define PR_HOST_STATUS_IDLE	0
+
+struct fme_mgr_priv {
+	void __iomem *ioaddr;
+};
+
+static ssize_t interface_id_show(struct device *dev,
+				 struct device_attribute *attr, char *buf)
+{
+	struct fpga_manager *mgr = dev_get_drvdata(dev);
+	struct fme_mgr_priv *priv = mgr->priv;
+	struct feature_fme_pr *fme_pr;
+	u64 intfc_id_l, intfc_id_h;
+
+	fme_pr = priv->ioaddr;
+
+	intfc_id_l = readq(&fme_pr->intfc_id_l);
+	intfc_id_h = readq(&fme_pr->intfc_id_h);
+
+	return scnprintf(buf, PAGE_SIZE, "%016llx%016llx\n",
+			(unsigned long long)intfc_id_h,
+			(unsigned long long)intfc_id_l);
+}
+static DEVICE_ATTR_RO(interface_id);
+
+static const struct attribute *fme_mgr_attrs[] = {
+	&dev_attr_interface_id.attr,

drivers/fpga/intel-fpga-fme-mgr.c line 65

Quoted 10 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +10 -0
+	NULL,
+};
+
+static void fme_mgr_pr_update_status(struct fpga_manager *mgr,
+				     struct feature_fme_pr_error err)
+{
+	mgr->status = 0;
+
+	if (err.operation_err)
+		mgr->status |= FPGA_MGR_STATUS_OPERATION_ERR;

Can we just have a (err & FEATURE_FME_PR_ERROR) here instead ?

drivers/fpga/intel-fpga-fme-mgr.c line 67

Quoted 2 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +2 -0
+	if (err.crc_err)
+		mgr->status |= FPGA_MGR_STATUS_CRC_ERR;

Can we just have a (err & FEATURE_FME_CRC_ERROR) here instead ?

Quoted 243 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +240 -0
+	if (err.incompatiable_bs)
+		mgr->status |= FPGA_MGR_STATUS_INCOMPATIBLE_BS_ERR;
+	if (err.protocol_err)
+		mgr->status |= FPGA_MGR_STATUS_IP_PROTOCOL_ERR;
+	if (err.fifo_overflow)
+		mgr->status |= FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR;
+	if (err.secure_load_err)
+		mgr->status |= FPGA_MGR_STATUS_SECURE_LOAD_ERR;
+}
+
+static u64 fme_mgr_pr_err_handle(struct feature_fme_pr *fme_pr)
+{
+	struct feature_fme_pr_status fme_pr_status;
+	struct feature_fme_pr_error fme_pr_error;
+
+	fme_pr_status.csr = readq(&fme_pr->status);
+	if (!fme_pr_status.pr_status)
+		return 0;
+
+	fme_pr_error.csr = readq(&fme_pr->error);
+	writeq(fme_pr_error.csr, &fme_pr->error);
+
+	return fme_pr_error.csr;
+}
+
+static int fme_mgr_write_init(struct fpga_manager *mgr,
+			      struct fpga_image_info *info,
+			      const char *buf, size_t count)
+{
+	struct device *dev = &mgr->dev;
+	struct fme_mgr_priv *priv = mgr->priv;
+	struct feature_fme_pr *fme_pr = priv->ioaddr;
+	struct feature_fme_pr_ctl fme_pr_ctl;
+	struct feature_fme_pr_status fme_pr_status;
+	struct feature_fme_pr_error fme_pr_err;
+
+	if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
+		dev_err(dev, "only support partial reconfiguration.\n");
+		return -EINVAL;
+	}
+
+	dev_dbg(dev, "resetting PR before initiated PR\n");
+
+	fme_pr_ctl.csr = readq(&fme_pr->control);
+	fme_pr_ctl.pr_reset = 1;
+	writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+	if (readq_poll_timeout(&fme_pr->control, fme_pr_ctl.csr,
+			       (fme_pr_ctl.pr_reset_ack == 1),
+			       1, PR_WAIT_TIMEOUT)) {
+		dev_err(dev, "maximum PR timeout\n");
+		return -ETIMEDOUT;
+	}
+
+	fme_pr_ctl.csr = readq(&fme_pr->control);
+	fme_pr_ctl.pr_reset = 0;
+	writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+	dev_dbg(dev,
+		"waiting for PR resource in HW to be initialized and ready\n");
+
+	fme_pr_status.pr_host_status = PR_HOST_STATUS_IDLE;
+
+	if (readq_poll_timeout(&fme_pr->status, fme_pr_status.csr,
+			(fme_pr_status.pr_host_status == PR_HOST_STATUS_IDLE),
+			1, PR_WAIT_TIMEOUT)) {
+		dev_err(dev, "maximum PR timeout\n");
+		return -ETIMEDOUT;
+	}
+
+	dev_dbg(dev, "check and clear previous PR error\n");
+	fme_pr_err.csr = fme_mgr_pr_err_handle(fme_pr);
+	if (fme_pr_err.csr)
+		dev_dbg(dev, "previous PR error detected %llx\n",
+			     (unsigned long long)fme_pr_err.csr);
+
+	/* Clear all PR errors */
+	fme_pr_err.csr = 0;
+	fme_mgr_pr_update_status(mgr, fmCRCe_pr_err);
+
+	dev_dbg(dev, "set PR port ID\n");
+
+	fme_pr_ctl.csr = readq(&fme_pr->control);
+	fme_pr_ctl.pr_regionid = info->region_id;
+	writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+	return 0;
+}
+
+static int fme_mgr_write(struct fpga_manager *mgr,
+			 const char *buf, size_t count)
+{
+	struct device *dev = &mgr->dev;
+	struct fme_mgr_priv *priv = mgr->priv;
+	struct feature_fme_pr *fme_pr = priv->ioaddr;
+	struct feature_fme_pr_ctl fme_pr_ctl;
+	struct feature_fme_pr_status fme_pr_status;
+	struct feature_fme_pr_data fme_pr_data;
+	int delay, pr_credit, i = 0;
+
+	dev_dbg(dev, "start request\n");
+
+	fme_pr_ctl.csr = readq(&fme_pr->control);
+	fme_pr_ctl.pr_start_req = 1;
+	writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+	dev_dbg(dev, "pushing data from bitstream to HW\n");
+
+	fme_pr_status.csr = readq(&fme_pr->status);
+	pr_credit = fme_pr_status.pr_credit;
+
+	while (count > 0) {
+		delay = 0;
+		while (pr_credit <= 1) {
+			if (delay++ > PR_WAIT_TIMEOUT) {
+				dev_err(dev, "maximum try\n");
+				return -ETIMEDOUT;
+			}
+			udelay(1);
+
+			fme_pr_status.csr = readq(&fme_pr->status);
+			pr_credit = fme_pr_status.pr_credit;
+		};
+
+		if (count >= 4) {
+			fme_pr_data.rsvd = 0;
+			fme_pr_data.pr_data_raw = *(((u32 *)buf) + i);
+			writeq(fme_pr_data.csr, &fme_pr->data);
+			count -= 4;
+			pr_credit--;
+			i++;
+		} else {
+			WARN_ON(1);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
+static int fme_mgr_write_complete(struct fpga_manager *mgr,
+				  struct fpga_image_info *info)
+{
+	struct device *dev = &mgr->dev;
+	struct fme_mgr_priv *priv = mgr->priv;
+	struct feature_fme_pr *fme_pr = priv->ioaddr;
+	struct feature_fme_pr_ctl fme_pr_ctl;
+	struct feature_fme_pr_error fme_pr_err;
+
+	fme_pr_ctl.csr = readq(&fme_pr->control);
+	fme_pr_ctl.pr_push_complete = 1;
+	writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+	dev_dbg(dev, "green bitstream push complete\n");
+	dev_dbg(dev, "waiting for HW to release PR resource\n");
+
+	fme_pr_ctl.pr_start_req = 0;
+
+	if (readq_poll_timeout(&fme_pr->control, fme_pr_ctl.csr,
+			       (fme_pr_ctl.pr_start_req == 0),
+			       1, PR_WAIT_TIMEOUT)) {
+		dev_err(dev, "maximum try.\n");
+		return -ETIMEDOUT;
+	}
+
+	dev_dbg(dev, "PR operation complete, checking status\n");
+	fme_pr_err.csr = fme_mgr_pr_err_handle(fme_pr);
+	if (fme_pr_err.csr) {
+		fme_mgr_pr_update_status(mgr, fme_pr_err);
+		return -EIO;
+	}
+
+	dev_dbg(dev, "PR done successfully\n");
+	return 0;
+}
+
+static enum fpga_mgr_states fme_mgr_state(struct fpga_manager *mgr)
+{
+	return FPGA_MGR_STATE_UNKNOWN;
+}
+
+static const struct fpga_manager_ops fme_mgr_ops = {
+	.write_init = fme_mgr_write_init,
+	.write = fme_mgr_write,
+	.write_complete = fme_mgr_write_complete,
+	.state = fme_mgr_state,
+};
+
+static int fme_mgr_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct fme_mgr_priv *priv;
+	struct resource *res;
+	int ret;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	priv->ioaddr = devm_ioremap(dev, res->start, resource_size(res));
+	if (IS_ERR(priv->ioaddr))
+		return PTR_ERR(priv->ioaddr);
+
+	ret = sysfs_create_files(&pdev->dev.kobj, fme_mgr_attrs);
+	if (ret)
+		return ret;
+
+	ret = fpga_mgr_register(dev, "Intel FPGA Manager", &fme_mgr_ops, priv);
+	if (ret) {
+		dev_err(dev, "unable to register FPGA manager\n");
+		sysfs_remove_files(&pdev->dev.kobj, fme_mgr_attrs);
+		return ret;
+	}
+
+	return 0;
+}
+
+static int fme_mgr_remove(struct platform_device *pdev)
+{
+	fpga_mgr_unregister(&pdev->dev);
+	sysfs_remove_files(&pdev->dev.kobj, fme_mgr_attrs);
+
+	return 0;
+}
+
+static struct platform_driver fme_mgr_driver = {
+	.driver	= {
+		.name    = INTEL_FPGA_FME_MGR,
+	},
+	.probe   = fme_mgr_probe,
+	.remove  = fme_mgr_remove,
+};
+
+module_platform_driver(fme_mgr_driver);
+
+MODULE_DESCRIPTION("FPGA Manager for Intel FPGA Management Engine");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-fme-mgr");
Signature · 1 line
1.8.3.1

[1] http://yarchive.net/comp/linux/bitfields.html
[2] https://lwn.net/Articles/478657/

Thanks,
Moritz

author
#64 lore raw

Re: [PATCH v2 15/22] fpga: intel: add fpga manager platform driver for FME

Quoted 1 line · Moritz Fischer

Hi Hao,

Hi Moritz

Thanks for your review. :)

Quoted 136 lines · Wu Hao
Quoted 134 lines

This patch adds fpga manager driver for Intel FPGA Management Engine(FME). It implements fpga_manager_ops for FPGA Partial Reconfiguration function.

---
 .../ABI/testing/sysfs-platform-intel-fpga-fme-mgr  |   8 +
 drivers/fpga/Kconfig                               |   7 +
 drivers/fpga/Makefile                              |   1 +
 drivers/fpga/intel-feature-dev.h                   |  75 +++++
 drivers/fpga/intel-fpga-fme-mgr.c                  | 307 +++++++++++++++++++++
 5 files changed, 398 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-fme-mgr
 create mode 100644 drivers/fpga/intel-fpga-fme-mgr.c
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme-mgr new file +8 -0
new file mode 100644
index 0000000..40771fb
@@ -0,0 +1,8 @@
+What:		/sys/bus/platform/devices/intel-fpga-fme-mgr.0/interface_id
+Date:		June 2017
+KernelVersion:  4.12
+Contact:	Wu Hao <[email protected]>
+Description:	Read-only. It returns interface id of partial reconfiguration
+		hardware. Userspace could use this information to check if
+		current hardware is compatible with given image before FPGA
+		programming.
drivers/fpga/Kconfig +7 -0
index b91458f..6f2f623 100644
@@ -151,6 +151,13 @@ config INTEL_FPGA_FME
 	  all FPGA platform level management features. There shall be 1
 	  FME per Intel FPGA.
 
+config INTEL_FPGA_FME_MGR
+	tristate "Intel FPGA FME Manager Driver"
+	depends on INTEL_FPGA_FME
+	help
+	  Say Y to enable FPGA Manager driver for Intel FPGA Management
+	  Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index acda5ca..d1d588b 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_OF_FPGA_REGION)		+= of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)		+= intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)		+= intel-fpga-fme.o
+obj-$(CONFIG_INTEL_FPGA_FME_MGR)	+= intel-fpga-fme-mgr.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-feature-dev.h +61 -0
index 3f97b75..f33923b 100644
@@ -164,8 +164,83 @@ struct feature_fme_err {
 };
 
 /* FME Partial Reconfiguration Sub Feature Register Set */
+/* FME PR Control Register */
+struct feature_fme_pr_ctl {
+	union {
+		u64 csr;
+		struct {
+			u64 pr_reset:1;		/* Reset PR Engine */
+			u64 rsvdz1:3;
+			u64 pr_reset_ack:1;	/* Reset PR Engine Ack */
+			u64 rsvdz2:3;
+			u64 pr_regionid:2;	/* PR Region ID */
+			u64 rsvdz3:2;
+			u64 pr_start_req:1;	/* PR Start Request */
+			u64 pr_push_complete:1;	/* PR Data push complete */
+			u64 pr_kind:1;		/* Load Customer or Intel GBS */
+			u64 rsvdz4:17;
+			u64 config_data:32;
+		};
+	};
+};
+
+/* FME PR Status Register */
+struct feature_fme_pr_status {
+	union {
+		u64 csr;
+		struct {
+			u64 pr_credit:9;	/* Number of PR Credits */
+			u64 rsvdz1:7;
+			u64 pr_status:1;	/* PR Operation status */
+			u64 rsvdz2:3;
+			u64 pr_ctrlr_status:3;	/* Controller status */
+			u64 rsvdz3:1;
+			u64 pr_host_status:4;	/* PR Host status */
+			u64 rsvdz4:36;
+		};
+	};
+};
+
+/* FME PR Data Register */
+struct feature_fme_pr_data {
+	union {
+		u64 csr;
+		struct {
+			/* PR data from the raw-binary file */
+			u64 pr_data_raw:32;
+			u64 rsvd:32;
+		};
+	};
+};
+
+/* FME PR Error Register */
+struct feature_fme_pr_error {
+	union {
+		u64 csr;
+		struct {
+			u64 operation_err:1;	/* Previous PR error detected */
+			u64 crc_err:1;		/* CRC error detected */
+			u64 incompatiable_bs:1; /* Incompatiable Bitstream */
+			u64 protocol_err:1;	/* Data push protocol error */
+			u64 fifo_overflow:1;	/* Data fifo overflow */
+			u64 rsvdz1:1;
+			u64 secure_load_err:1;	/* Secure PR load error */
Quoted 8 lines

drivers/fpga/intel-feature-dev.h struct feature_fme_err { · line 231

Quoted 4 lines
drivers/fpga/intel-feature-dev.h +4 -0
+			u64 rsvdz2:57;
+		};
+	};
+};

From what I've seen [1], [2] people weren't too happy with using bitfields, any reason we can't use explicit masking and shifts? I don't know if this is still an issue.

I think both should work fine in our case, and compiler should give same result on both. We choose bitfields as it seems to be more readable, and could save some code. Do you think if we can keep it? :)

Quoted 89 lines
Quoted 77 lines
drivers/fpga/intel-feature-dev.h +10 -0
+
 struct feature_fme_pr {
 	struct feature_header header;
+	struct feature_fme_pr_ctl control;
+	struct feature_fme_pr_status status;
+	struct feature_fme_pr_data data;
+	struct feature_fme_pr_error error;
+
+	u64 rsvd[16];
+
+	u64 intfc_id_l;		/* PR interface Id Low */
+	u64 intfc_id_h;		/* PR interface Id High */
 };
 
 /* PORT Header Register Set */
drivers/fpga/intel-fpga-fme-mgr.c new file +55 -0
new file mode 100644
index 0000000..5f93dfb
@@ -0,0 +1,307 @@
+/*
+ * FPGA Manager Driver for Intel FPGA Management Engine (FME)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Joseph Grecco <[email protected]>
+ *   Enno Luebbers <[email protected]>
+ *   Tim Whisonant <[email protected]>
+ *   Ananda Ravuri <[email protected]>
+ *   Christopher Rauer <[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/module.h>
+#include <linux/iopoll.h>
+#include <linux/fpga/fpga-mgr.h>
+
+#include "intel-feature-dev.h"
+#include "intel-fme.h"
+
+#define PR_WAIT_TIMEOUT   8000000
+#define PR_HOST_STATUS_IDLE	0
+
+struct fme_mgr_priv {
+	void __iomem *ioaddr;
+};
+
+static ssize_t interface_id_show(struct device *dev,
+				 struct device_attribute *attr, char *buf)
+{
+	struct fpga_manager *mgr = dev_get_drvdata(dev);
+	struct fme_mgr_priv *priv = mgr->priv;
+	struct feature_fme_pr *fme_pr;
+	u64 intfc_id_l, intfc_id_h;
+
+	fme_pr = priv->ioaddr;
+
+	intfc_id_l = readq(&fme_pr->intfc_id_l);
+	intfc_id_h = readq(&fme_pr->intfc_id_h);
+
+	return scnprintf(buf, PAGE_SIZE, "%016llx%016llx\n",
+			(unsigned long long)intfc_id_h,
+			(unsigned long long)intfc_id_l);
+}
+static DEVICE_ATTR_RO(interface_id);
+
+static const struct attribute *fme_mgr_attrs[] = {
+	&dev_attr_interface_id.attr,

drivers/fpga/intel-fpga-fme-mgr.c line 65

Quoted 10 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +10 -0
+	NULL,
+};
+
+static void fme_mgr_pr_update_status(struct fpga_manager *mgr,
+				     struct feature_fme_pr_error err)
+{
+	mgr->status = 0;
+
+	if (err.operation_err)
+		mgr->status |= FPGA_MGR_STATUS_OPERATION_ERR;

Can we just have a (err & FEATURE_FME_PR_ERROR) here instead ?

Quoted 4 lines

drivers/fpga/intel-fpga-fme-mgr.c line 67

Quoted 2 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +2 -0
+	if (err.crc_err)
+		mgr->status |= FPGA_MGR_STATUS_CRC_ERR;

Can we just have a (err & FEATURE_FME_CRC_ERROR) here instead ?

As above, do you think if we can keep the origin code here? Use "if (err.crc_err)" not "if (err & FEATURE_FME_CRC_ERROR)", it makes the code shorter in most cases. :)

Thanks
Hao

Quoted 249 lines
Quoted 243 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +240 -0
+	if (err.incompatiable_bs)
+		mgr->status |= FPGA_MGR_STATUS_INCOMPATIBLE_BS_ERR;
+	if (err.protocol_err)
+		mgr->status |= FPGA_MGR_STATUS_IP_PROTOCOL_ERR;
+	if (err.fifo_overflow)
+		mgr->status |= FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR;
+	if (err.secure_load_err)
+		mgr->status |= FPGA_MGR_STATUS_SECURE_LOAD_ERR;
+}
+
+static u64 fme_mgr_pr_err_handle(struct feature_fme_pr *fme_pr)
+{
+	struct feature_fme_pr_status fme_pr_status;
+	struct feature_fme_pr_error fme_pr_error;
+
+	fme_pr_status.csr = readq(&fme_pr->status);
+	if (!fme_pr_status.pr_status)
+		return 0;
+
+	fme_pr_error.csr = readq(&fme_pr->error);
+	writeq(fme_pr_error.csr, &fme_pr->error);
+
+	return fme_pr_error.csr;
+}
+
+static int fme_mgr_write_init(struct fpga_manager *mgr,
+			      struct fpga_image_info *info,
+			      const char *buf, size_t count)
+{
+	struct device *dev = &mgr->dev;
+	struct fme_mgr_priv *priv = mgr->priv;
+	struct feature_fme_pr *fme_pr = priv->ioaddr;
+	struct feature_fme_pr_ctl fme_pr_ctl;
+	struct feature_fme_pr_status fme_pr_status;
+	struct feature_fme_pr_error fme_pr_err;
+
+	if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
+		dev_err(dev, "only support partial reconfiguration.\n");
+		return -EINVAL;
+	}
+
+	dev_dbg(dev, "resetting PR before initiated PR\n");
+
+	fme_pr_ctl.csr = readq(&fme_pr->control);
+	fme_pr_ctl.pr_reset = 1;
+	writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+	if (readq_poll_timeout(&fme_pr->control, fme_pr_ctl.csr,
+			       (fme_pr_ctl.pr_reset_ack == 1),
+			       1, PR_WAIT_TIMEOUT)) {
+		dev_err(dev, "maximum PR timeout\n");
+		return -ETIMEDOUT;
+	}
+
+	fme_pr_ctl.csr = readq(&fme_pr->control);
+	fme_pr_ctl.pr_reset = 0;
+	writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+	dev_dbg(dev,
+		"waiting for PR resource in HW to be initialized and ready\n");
+
+	fme_pr_status.pr_host_status = PR_HOST_STATUS_IDLE;
+
+	if (readq_poll_timeout(&fme_pr->status, fme_pr_status.csr,
+			(fme_pr_status.pr_host_status == PR_HOST_STATUS_IDLE),
+			1, PR_WAIT_TIMEOUT)) {
+		dev_err(dev, "maximum PR timeout\n");
+		return -ETIMEDOUT;
+	}
+
+	dev_dbg(dev, "check and clear previous PR error\n");
+	fme_pr_err.csr = fme_mgr_pr_err_handle(fme_pr);
+	if (fme_pr_err.csr)
+		dev_dbg(dev, "previous PR error detected %llx\n",
+			     (unsigned long long)fme_pr_err.csr);
+
+	/* Clear all PR errors */
+	fme_pr_err.csr = 0;
+	fme_mgr_pr_update_status(mgr, fmCRCe_pr_err);
+
+	dev_dbg(dev, "set PR port ID\n");
+
+	fme_pr_ctl.csr = readq(&fme_pr->control);
+	fme_pr_ctl.pr_regionid = info->region_id;
+	writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+	return 0;
+}
+
+static int fme_mgr_write(struct fpga_manager *mgr,
+			 const char *buf, size_t count)
+{
+	struct device *dev = &mgr->dev;
+	struct fme_mgr_priv *priv = mgr->priv;
+	struct feature_fme_pr *fme_pr = priv->ioaddr;
+	struct feature_fme_pr_ctl fme_pr_ctl;
+	struct feature_fme_pr_status fme_pr_status;
+	struct feature_fme_pr_data fme_pr_data;
+	int delay, pr_credit, i = 0;
+
+	dev_dbg(dev, "start request\n");
+
+	fme_pr_ctl.csr = readq(&fme_pr->control);
+	fme_pr_ctl.pr_start_req = 1;
+	writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+	dev_dbg(dev, "pushing data from bitstream to HW\n");
+
+	fme_pr_status.csr = readq(&fme_pr->status);
+	pr_credit = fme_pr_status.pr_credit;
+
+	while (count > 0) {
+		delay = 0;
+		while (pr_credit <= 1) {
+			if (delay++ > PR_WAIT_TIMEOUT) {
+				dev_err(dev, "maximum try\n");
+				return -ETIMEDOUT;
+			}
+			udelay(1);
+
+			fme_pr_status.csr = readq(&fme_pr->status);
+			pr_credit = fme_pr_status.pr_credit;
+		};
+
+		if (count >= 4) {
+			fme_pr_data.rsvd = 0;
+			fme_pr_data.pr_data_raw = *(((u32 *)buf) + i);
+			writeq(fme_pr_data.csr, &fme_pr->data);
+			count -= 4;
+			pr_credit--;
+			i++;
+		} else {
+			WARN_ON(1);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
+static int fme_mgr_write_complete(struct fpga_manager *mgr,
+				  struct fpga_image_info *info)
+{
+	struct device *dev = &mgr->dev;
+	struct fme_mgr_priv *priv = mgr->priv;
+	struct feature_fme_pr *fme_pr = priv->ioaddr;
+	struct feature_fme_pr_ctl fme_pr_ctl;
+	struct feature_fme_pr_error fme_pr_err;
+
+	fme_pr_ctl.csr = readq(&fme_pr->control);
+	fme_pr_ctl.pr_push_complete = 1;
+	writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+	dev_dbg(dev, "green bitstream push complete\n");
+	dev_dbg(dev, "waiting for HW to release PR resource\n");
+
+	fme_pr_ctl.pr_start_req = 0;
+
+	if (readq_poll_timeout(&fme_pr->control, fme_pr_ctl.csr,
+			       (fme_pr_ctl.pr_start_req == 0),
+			       1, PR_WAIT_TIMEOUT)) {
+		dev_err(dev, "maximum try.\n");
+		return -ETIMEDOUT;
+	}
+
+	dev_dbg(dev, "PR operation complete, checking status\n");
+	fme_pr_err.csr = fme_mgr_pr_err_handle(fme_pr);
+	if (fme_pr_err.csr) {
+		fme_mgr_pr_update_status(mgr, fme_pr_err);
+		return -EIO;
+	}
+
+	dev_dbg(dev, "PR done successfully\n");
+	return 0;
+}
+
+static enum fpga_mgr_states fme_mgr_state(struct fpga_manager *mgr)
+{
+	return FPGA_MGR_STATE_UNKNOWN;
+}
+
+static const struct fpga_manager_ops fme_mgr_ops = {
+	.write_init = fme_mgr_write_init,
+	.write = fme_mgr_write,
+	.write_complete = fme_mgr_write_complete,
+	.state = fme_mgr_state,
+};
+
+static int fme_mgr_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct fme_mgr_priv *priv;
+	struct resource *res;
+	int ret;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	priv->ioaddr = devm_ioremap(dev, res->start, resource_size(res));
+	if (IS_ERR(priv->ioaddr))
+		return PTR_ERR(priv->ioaddr);
+
+	ret = sysfs_create_files(&pdev->dev.kobj, fme_mgr_attrs);
+	if (ret)
+		return ret;
+
+	ret = fpga_mgr_register(dev, "Intel FPGA Manager", &fme_mgr_ops, priv);
+	if (ret) {
+		dev_err(dev, "unable to register FPGA manager\n");
+		sysfs_remove_files(&pdev->dev.kobj, fme_mgr_attrs);
+		return ret;
+	}
+
+	return 0;
+}
+
+static int fme_mgr_remove(struct platform_device *pdev)
+{
+	fpga_mgr_unregister(&pdev->dev);
+	sysfs_remove_files(&pdev->dev.kobj, fme_mgr_attrs);
+
+	return 0;
+}
+
+static struct platform_driver fme_mgr_driver = {
+	.driver	= {
+		.name    = INTEL_FPGA_FME_MGR,
+	},
+	.probe   = fme_mgr_probe,
+	.remove  = fme_mgr_remove,
+};
+
+module_platform_driver(fme_mgr_driver);
+
+MODULE_DESCRIPTION("FPGA Manager for Intel FPGA Management Engine");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-fme-mgr");
Signature · 1 line
1.8.3.1

[1] http://yarchive.net/comp/linux/bitfields.html
[2] https://lwn.net/Articles/478657/

Thanks,
Moritz

#65 lore raw

Re: [PATCH v2 15/22] fpga: intel: add fpga manager platform driver for FME

On Tue, Sep 26, 2017 at 8:18 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

Quoted 152 lines · Moritz Fischer
Quoted 1 line

Hi Hao,

Hi Moritz

Thanks for your review. :)

Quoted 136 lines · Wu Hao
Quoted 134 lines

This patch adds fpga manager driver for Intel FPGA Management Engine(FME). It implements fpga_manager_ops for FPGA Partial Reconfiguration function.

---
 .../ABI/testing/sysfs-platform-intel-fpga-fme-mgr  |   8 +
 drivers/fpga/Kconfig                               |   7 +
 drivers/fpga/Makefile                              |   1 +
 drivers/fpga/intel-feature-dev.h                   |  75 +++++
 drivers/fpga/intel-fpga-fme-mgr.c                  | 307 +++++++++++++++++++++
 5 files changed, 398 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-fme-mgr
 create mode 100644 drivers/fpga/intel-fpga-fme-mgr.c
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme-mgr new file +8 -0
new file mode 100644
index 0000000..40771fb
@@ -0,0 +1,8 @@
+What:              /sys/bus/platform/devices/intel-fpga-fme-mgr.0/interface_id
+Date:              June 2017
+KernelVersion:  4.12
+Contact:   Wu Hao <[email protected]>
+Description:       Read-only. It returns interface id of partial reconfiguration
+           hardware. Userspace could use this information to check if
+           current hardware is compatible with given image before FPGA
+           programming.
drivers/fpga/Kconfig +7 -0
index b91458f..6f2f623 100644
@@ -151,6 +151,13 @@ config INTEL_FPGA_FME
      all FPGA platform level management features. There shall be 1
      FME per Intel FPGA.
 
+config INTEL_FPGA_FME_MGR
+   tristate "Intel FPGA FME Manager Driver"
+   depends on INTEL_FPGA_FME
+   help
+     Say Y to enable FPGA Manager driver for Intel FPGA Management
+     Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index acda5ca..d1d588b 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_OF_FPGA_REGION)              += of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)               += intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)               += intel-fpga-fme.o
+obj-$(CONFIG_INTEL_FPGA_FME_MGR)   += intel-fpga-fme-mgr.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-feature-dev.h +61 -0
index 3f97b75..f33923b 100644
@@ -164,8 +164,83 @@ struct feature_fme_err {
 };
 
 /* FME Partial Reconfiguration Sub Feature Register Set */
+/* FME PR Control Register */
+struct feature_fme_pr_ctl {
+   union {
+           u64 csr;
+           struct {
+                   u64 pr_reset:1;         /* Reset PR Engine */
+                   u64 rsvdz1:3;
+                   u64 pr_reset_ack:1;     /* Reset PR Engine Ack */
+                   u64 rsvdz2:3;
+                   u64 pr_regionid:2;      /* PR Region ID */
+                   u64 rsvdz3:2;
+                   u64 pr_start_req:1;     /* PR Start Request */
+                   u64 pr_push_complete:1; /* PR Data push complete */
+                   u64 pr_kind:1;          /* Load Customer or Intel GBS */
+                   u64 rsvdz4:17;
+                   u64 config_data:32;
+           };
+   };
+};
+
+/* FME PR Status Register */
+struct feature_fme_pr_status {
+   union {
+           u64 csr;
+           struct {
+                   u64 pr_credit:9;        /* Number of PR Credits */
+                   u64 rsvdz1:7;
+                   u64 pr_status:1;        /* PR Operation status */
+                   u64 rsvdz2:3;
+                   u64 pr_ctrlr_status:3;  /* Controller status */
+                   u64 rsvdz3:1;
+                   u64 pr_host_status:4;   /* PR Host status */
+                   u64 rsvdz4:36;
+           };
+   };
+};
+
+/* FME PR Data Register */
+struct feature_fme_pr_data {
+   union {
+           u64 csr;
+           struct {
+                   /* PR data from the raw-binary file */
+                   u64 pr_data_raw:32;
+                   u64 rsvd:32;
+           };
+   };
+};
+
+/* FME PR Error Register */
+struct feature_fme_pr_error {
+   union {
+           u64 csr;
+           struct {
+                   u64 operation_err:1;    /* Previous PR error detected */
+                   u64 crc_err:1;          /* CRC error detected */
+                   u64 incompatiable_bs:1; /* Incompatiable Bitstream */
+                   u64 protocol_err:1;     /* Data push protocol error */
+                   u64 fifo_overflow:1;    /* Data fifo overflow */
+                   u64 rsvdz1:1;
+                   u64 secure_load_err:1;  /* Secure PR load error */
Quoted 8 lines

drivers/fpga/intel-feature-dev.h struct feature_fme_err { · line 231

Quoted 4 lines
drivers/fpga/intel-feature-dev.h +4 -0
+                   u64 rsvdz2:57;
+           };
+   };
+};

From what I've seen [1], [2] people weren't too happy with using bitfields, any reason we can't use explicit masking and shifts? I don't know if this is still an issue.

Quoted 3 lines

I think both should work fine in our case, and compiler should give same result on both. We choose bitfields as it seems to be more readable, and could save some code. Do you think if we can keep it? :)

I have the same concerns as Moritz here. I have suggested using regmap before back in July. Using bitmasks and shifts would be be preferable.

Alan

Quoted 353 lines
Quoted 90 lines
Quoted 78 lines
drivers/fpga/intel-feature-dev.h +10 -0
+
 struct feature_fme_pr {
    struct feature_header header;
+   struct feature_fme_pr_ctl control;
+   struct feature_fme_pr_status status;
+   struct feature_fme_pr_data data;
+   struct feature_fme_pr_error error;
+
+   u64 rsvd[16];
+
+   u64 intfc_id_l;         /* PR interface Id Low */
+   u64 intfc_id_h;         /* PR interface Id High */
 };
 
 /* PORT Header Register Set */
drivers/fpga/intel-fpga-fme-mgr.c new file +55 -0
new file mode 100644
index 0000000..5f93dfb
@@ -0,0 +1,307 @@
+/*
+ * FPGA Manager Driver for Intel FPGA Management Engine (FME)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Xiao Guangrong <[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Joseph Grecco <[email protected]>
+ *   Enno Luebbers <[email protected]>
+ *   Tim Whisonant <[email protected]>
+ *   Ananda Ravuri <[email protected]>
+ *   Christopher Rauer <[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/module.h>
+#include <linux/iopoll.h>
+#include <linux/fpga/fpga-mgr.h>
+
+#include "intel-feature-dev.h"
+#include "intel-fme.h"
+
+#define PR_WAIT_TIMEOUT   8000000
+#define PR_HOST_STATUS_IDLE        0
+
+struct fme_mgr_priv {
+   void __iomem *ioaddr;
+};
+
+static ssize_t interface_id_show(struct device *dev,
+                            struct device_attribute *attr, char *buf)
+{
+   struct fpga_manager *mgr = dev_get_drvdata(dev);
+   struct fme_mgr_priv *priv = mgr->priv;
+   struct feature_fme_pr *fme_pr;
+   u64 intfc_id_l, intfc_id_h;
+
+   fme_pr = priv->ioaddr;
+
+   intfc_id_l = readq(&fme_pr->intfc_id_l);
+   intfc_id_h = readq(&fme_pr->intfc_id_h);
+
+   return scnprintf(buf, PAGE_SIZE, "%016llx%016llx\n",
+                   (unsigned long long)intfc_id_h,
+                   (unsigned long long)intfc_id_l);
+}
+static DEVICE_ATTR_RO(interface_id);
+
+static const struct attribute *fme_mgr_attrs[] = {
+   &dev_attr_interface_id.attr,

drivers/fpga/intel-fpga-fme-mgr.c line 65

Quoted 10 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +10 -0
+   NULL,
+};
+
+static void fme_mgr_pr_update_status(struct fpga_manager *mgr,
+                                struct feature_fme_pr_error err)
+{
+   mgr->status = 0;
+
+   if (err.operation_err)
+           mgr->status |= FPGA_MGR_STATUS_OPERATION_ERR;

Can we just have a (err & FEATURE_FME_PR_ERROR) here instead ?

Quoted 4 lines

drivers/fpga/intel-fpga-fme-mgr.c line 67

Quoted 2 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +2 -0
+   if (err.crc_err)
+           mgr->status |= FPGA_MGR_STATUS_CRC_ERR;

Can we just have a (err & FEATURE_FME_CRC_ERROR) here instead ?

As above, do you think if we can keep the origin code here? Use "if (err.crc_err)" not "if (err & FEATURE_FME_CRC_ERROR)", it makes the code shorter in most cases. :)

Thanks
Hao

Quoted 249 lines
Quoted 241 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +240 -1
+   if (err.incompatiable_bs)
+           mgr->status |= FPGA_MGR_STATUS_INCOMPATIBLE_BS_ERR;
+   if (err.protocol_err)
+           mgr->status |= FPGA_MGR_STATUS_IP_PROTOCOL_ERR;
+   if (err.fifo_overflow)
+           mgr->status |= FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR;
+   if (err.secure_load_err)
+           mgr->status |= FPGA_MGR_STATUS_SECURE_LOAD_ERR;
+}
+
+static u64 fme_mgr_pr_err_handle(struct feature_fme_pr *fme_pr)
+{
+   struct feature_fme_pr_status fme_pr_status;
+   struct feature_fme_pr_error fme_pr_error;
+
+   fme_pr_status.csr = readq(&fme_pr->status);
+   if (!fme_pr_status.pr_status)
+           return 0;
+
+   fme_pr_error.csr = readq(&fme_pr->error);
+   writeq(fme_pr_error.csr, &fme_pr->error);
+
+   return fme_pr_error.csr;
+}
+
+static int fme_mgr_write_init(struct fpga_manager *mgr,
+                         struct fpga_image_info *info,
+                         const char *buf, size_t count)
+{
+   struct device *dev = &mgr->dev;
+   struct fme_mgr_priv *priv = mgr->priv;
+   struct feature_fme_pr *fme_pr = priv->ioaddr;
+   struct feature_fme_pr_ctl fme_pr_ctl;
+   struct feature_fme_pr_status fme_pr_status;
+   struct feature_fme_pr_error fme_pr_err;
+
+   if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
+           dev_err(dev, "only support partial reconfiguration.\n");
+           return -EINVAL;
+   }
+
+   dev_dbg(dev, "resetting PR before initiated PR\n");
+
+   fme_pr_ctl.csr = readq(&fme_pr->control);
+   fme_pr_ctl.pr_reset = 1;
+   writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+   if (readq_poll_timeout(&fme_pr->control, fme_pr_ctl.csr,
+                          (fme_pr_ctl.pr_reset_ack == 1),
+                          1, PR_WAIT_TIMEOUT)) {
+           dev_err(dev, "maximum PR timeout\n");
+           return -ETIMEDOUT;
+   }
+
+   fme_pr_ctl.csr = readq(&fme_pr->control);
+   fme_pr_ctl.pr_reset = 0;
+   writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+   dev_dbg(dev,
+           "waiting for PR resource in HW to be initialized and ready\n");
+
+   fme_pr_status.pr_host_status = PR_HOST_STATUS_IDLE;
+
+   if (readq_poll_timeout(&fme_pr->status, fme_pr_status.csr,
+                   (fme_pr_status.pr_host_status == PR_HOST_STATUS_IDLE),
+                   1, PR_WAIT_TIMEOUT)) {
+           dev_err(dev, "maximum PR timeout\n");
+           return -ETIMEDOUT;
+   }
+
+   dev_dbg(dev, "check and clear previous PR error\n");
+   fme_pr_err.csr = fme_mgr_pr_err_handle(fme_pr);
+   if (fme_pr_err.csr)
+           dev_dbg(dev, "previous PR error detected %llx\n",
+                        (unsigned long long)fme_pr_err.csr);
+
+   /* Clear all PR errors */
+   fme_pr_err.csr = 0;
+   fme_mgr_pr_update_status(mgr, fmCRCe_pr_err);
+
+   dev_dbg(dev, "set PR port ID\n");
+
+   fme_pr_ctl.csr = readq(&fme_pr->control);
+   fme_pr_ctl.pr_regionid = info->region_id;
+   writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+   return 0;
+}
+
+static int fme_mgr_write(struct fpga_manager *mgr,
+                    const char *buf, size_t count)
+{
+   struct device *dev = &mgr->dev;
+   struct fme_mgr_priv *priv = mgr->priv;
+   struct feature_fme_pr *fme_pr = priv->ioaddr;
+   struct feature_fme_pr_ctl fme_pr_ctl;
+   struct feature_fme_pr_status fme_pr_status;
+   struct feature_fme_pr_data fme_pr_data;
+   int delay, pr_credit, i = 0;
+
+   dev_dbg(dev, "start request\n");
+
+   fme_pr_ctl.csr = readq(&fme_pr->control);
+   fme_pr_ctl.pr_start_req = 1;
+   writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+   dev_dbg(dev, "pushing data from bitstream to HW\n");
+
+   fme_pr_status.csr = readq(&fme_pr->status);
+   pr_credit = fme_pr_status.pr_credit;
+
+   while (count > 0) {
+           delay = 0;
+           while (pr_credit <= 1) {
+                   if (delay++ > PR_WAIT_TIMEOUT) {
+                           dev_err(dev, "maximum try\n");
+                           return -ETIMEDOUT;
+                   }
+                   udelay(1);
+
+                   fme_pr_status.csr = readq(&fme_pr->status);
+                   pr_credit = fme_pr_status.pr_credit;
+           };
+
+           if (count >= 4) {
+                   fme_pr_data.rsvd = 0;
+                   fme_pr_data.pr_data_raw = *(((u32 *)buf) + i);
+                   writeq(fme_pr_data.csr, &fme_pr->data);
+                   count -= 4;
+                   pr_credit--;
+                   i++;
+           } else {
+                   WARN_ON(1);
+                   return -EINVAL;
+           }
+   }
+
+   return 0;
+}
+
+static int fme_mgr_write_complete(struct fpga_manager *mgr,
+                             struct fpga_image_info *info)
+{
+   struct device *dev = &mgr->dev;
+   struct fme_mgr_priv *priv = mgr->priv;
+   struct feature_fme_pr *fme_pr = priv->ioaddr;
+   struct feature_fme_pr_ctl fme_pr_ctl;
+   struct feature_fme_pr_error fme_pr_err;
+
+   fme_pr_ctl.csr = readq(&fme_pr->control);
+   fme_pr_ctl.pr_push_complete = 1;
+   writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+   dev_dbg(dev, "green bitstream push complete\n");
+   dev_dbg(dev, "waiting for HW to release PR resource\n");
+
+   fme_pr_ctl.pr_start_req = 0;
+
+   if (readq_poll_timeout(&fme_pr->control, fme_pr_ctl.csr,
+                          (fme_pr_ctl.pr_start_req == 0),
+                          1, PR_WAIT_TIMEOUT)) {
+           dev_err(dev, "maximum try.\n");
+           return -ETIMEDOUT;
+   }
+
+   dev_dbg(dev, "PR operation complete, checking status\n");
+   fme_pr_err.csr = fme_mgr_pr_err_handle(fme_pr);
+   if (fme_pr_err.csr) {
+           fme_mgr_pr_update_status(mgr, fme_pr_err);
+           return -EIO;
+   }
+
+   dev_dbg(dev, "PR done successfully\n");
+   return 0;
+}
+
+static enum fpga_mgr_states fme_mgr_state(struct fpga_manager *mgr)
+{
+   return FPGA_MGR_STATE_UNKNOWN;
+}
+
+static const struct fpga_manager_ops fme_mgr_ops = {
+   .write_init = fme_mgr_write_init,
+   .write = fme_mgr_write,
+   .write_complete = fme_mgr_write_complete,
+   .state = fme_mgr_state,
+};
+
+static int fme_mgr_probe(struct platform_device *pdev)
+{
+   struct device *dev = &pdev->dev;
+   struct fme_mgr_priv *priv;
+   struct resource *res;
+   int ret;
+
+   priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+   if (!priv)
+           return -ENOMEM;
+
+   res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+   priv->ioaddr = devm_ioremap(dev, res->start, resource_size(res));
+   if (IS_ERR(priv->ioaddr))
+           return PTR_ERR(priv->ioaddr);
+
+   ret = sysfs_create_files(&pdev->dev.kobj, fme_mgr_attrs);
+   if (ret)
+           return ret;
+
+   ret = fpga_mgr_register(dev, "Intel FPGA Manager", &fme_mgr_ops, priv);
+   if (ret) {
+           dev_err(dev, "unable to register FPGA manager\n");
+           sysfs_remove_files(&pdev->dev.kobj, fme_mgr_attrs);
+           return ret;
+   }
+
+   return 0;
+}
+
+static int fme_mgr_remove(struct platform_device *pdev)
+{
+   fpga_mgr_unregister(&pdev->dev);
+   sysfs_remove_files(&pdev->dev.kobj, fme_mgr_attrs);
+
+   return 0;
+}
+
+static struct platform_driver fme_mgr_driver = {
+   .driver = {
+           .name    = INTEL_FPGA_FME_MGR,
+   },
+   .probe   = fme_mgr_probe,
+   .remove  = fme_mgr_remove,
+};
+
+module_platform_driver(fme_mgr_driver);
+
+MODULE_DESCRIPTION("FPGA Manager for Intel FPGA Management Engine");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-fme-mgr");
--
Quoted 2 lines

1.8.3.1

[1] http://yarchive.net/comp/linux/bitfields.html
[2] https://lwn.net/Articles/478657/

Thanks,
Moritz

author
#66 lore raw

Re: [PATCH v2 15/22] fpga: intel: add fpga manager platform driver for FME

Quoted 156 lines · Alan Tull

On Tue, Sep 26, 2017 at 8:18 PM, Wu Hao <[email protected]> wrote:

Hi Hao,

Quoted 152 lines · Moritz Fischer
Quoted 1 line

Hi Hao,

Hi Moritz

Thanks for your review. :)

Quoted 144 lines · Wu Hao
Quoted 128 lines

This patch adds fpga manager driver for Intel FPGA Management Engine(FME). It implements fpga_manager_ops for FPGA Partial Reconfiguration function.

---
 .../ABI/testing/sysfs-platform-intel-fpga-fme-mgr  |   8 +
 drivers/fpga/Kconfig                               |   7 +
 drivers/fpga/Makefile                              |   1 +
 drivers/fpga/intel-feature-dev.h                   |  75 +++++
 drivers/fpga/intel-fpga-fme-mgr.c                  | 307 +++++++++++++++++++++
 5 files changed, 398 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-fme-mgr
 create mode 100644 drivers/fpga/intel-fpga-fme-mgr.c
Documentation/ABI/testing/sysfs-platform-intel-fpga-fme-mgr new file +8 -0
new file mode 100644
index 0000000..40771fb
@@ -0,0 +1,8 @@
+What:              /sys/bus/platform/devices/intel-fpga-fme-mgr.0/interface_id
+Date:              June 2017
+KernelVersion:  4.12
+Contact:   Wu Hao <[email protected]>
+Description:       Read-only. It returns interface id of partial reconfiguration
+           hardware. Userspace could use this information to check if
+           current hardware is compatible with given image before FPGA
+           programming.
drivers/fpga/Kconfig +7 -0
index b91458f..6f2f623 100644
@@ -151,6 +151,13 @@ config INTEL_FPGA_FME
      all FPGA platform level management features. There shall be 1
      FME per Intel FPGA.
 
+config INTEL_FPGA_FME_MGR
+   tristate "Intel FPGA FME Manager Driver"
+   depends on INTEL_FPGA_FME
+   help
+     Say Y to enable FPGA Manager driver for Intel FPGA Management
+     Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index acda5ca..d1d588b 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_OF_FPGA_REGION)              += of-fpga-region.o
 # Intel FPGA Support
 obj-$(CONFIG_INTEL_FPGA_PCI)               += intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)               += intel-fpga-fme.o
+obj-$(CONFIG_INTEL_FPGA_FME_MGR)   += intel-fpga-fme-mgr.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-feature-dev.h +55 -0
index 3f97b75..f33923b 100644
@@ -164,8 +164,83 @@ struct feature_fme_err {
 };
 
 /* FME Partial Reconfiguration Sub Feature Register Set */
+/* FME PR Control Register */
+struct feature_fme_pr_ctl {
+   union {
+           u64 csr;
+           struct {
+                   u64 pr_reset:1;         /* Reset PR Engine */
+                   u64 rsvdz1:3;
+                   u64 pr_reset_ack:1;     /* Reset PR Engine Ack */
+                   u64 rsvdz2:3;
+                   u64 pr_regionid:2;      /* PR Region ID */
+                   u64 rsvdz3:2;
+                   u64 pr_start_req:1;     /* PR Start Request */
+                   u64 pr_push_complete:1; /* PR Data push complete */
+                   u64 pr_kind:1;          /* Load Customer or Intel GBS */
+                   u64 rsvdz4:17;
+                   u64 config_data:32;
+           };
+   };
+};
+
+/* FME PR Status Register */
+struct feature_fme_pr_status {
+   union {
+           u64 csr;
+           struct {
+                   u64 pr_credit:9;        /* Number of PR Credits */
+                   u64 rsvdz1:7;
+                   u64 pr_status:1;        /* PR Operation status */
+                   u64 rsvdz2:3;
+                   u64 pr_ctrlr_status:3;  /* Controller status */
+                   u64 rsvdz3:1;
+                   u64 pr_host_status:4;   /* PR Host status */
+                   u64 rsvdz4:36;
+           };
+   };
+};
+
+/* FME PR Data Register */
+struct feature_fme_pr_data {
+   union {
+           u64 csr;
+           struct {
+                   /* PR data from the raw-binary file */
+                   u64 pr_data_raw:32;
+                   u64 rsvd:32;
+           };
+   };
+};
+
+/* FME PR Error Register */
+struct feature_fme_pr_error {
+   union {
+           u64 csr;
+           struct {
+                   u64 operation_err:1;    /* Previous PR error detected */

drivers/fpga/intel-feature-dev.h struct feature_fme_err { · line 231

Quoted 10 lines
drivers/fpga/intel-feature-dev.h +10 -0
+                   u64 crc_err:1;          /* CRC error detected */
+                   u64 incompatiable_bs:1; /* Incompatiable Bitstream */
+                   u64 protocol_err:1;     /* Data push protocol error */
+                   u64 fifo_overflow:1;    /* Data fifo overflow */
+                   u64 rsvdz1:1;
+                   u64 secure_load_err:1;  /* Secure PR load error */
+                   u64 rsvdz2:57;
+           };
+   };
+};

From what I've seen [1], [2] people weren't too happy with using bitfields, any reason we can't use explicit masking and shifts? I don't know if this is still an issue.

Quoted 7 lines
Quoted 3 lines

I think both should work fine in our case, and compiler should give same result on both. We choose bitfields as it seems to be more readable, and could save some code. Do you think if we can keep it? :)

I have the same concerns as Moritz here. I have suggested using regmap before back in July. Using bitmasks and shifts would be be preferable.

Sure, will switch to bitmasks and shifts in the next version. : )

Thanks
Hao

Quoted 356 lines

Alan

Quoted 353 lines
Quoted 90 lines
Quoted 78 lines
drivers/fpga/intel-feature-dev.h +10 -0
+
 struct feature_fme_pr {
    struct feature_header header;
+   struct feature_fme_pr_ctl control;
+   struct feature_fme_pr_status status;
+   struct feature_fme_pr_data data;
+   struct feature_fme_pr_error error;
+
+   u64 rsvd[16];
+
+   u64 intfc_id_l;         /* PR interface Id Low */
+   u64 intfc_id_h;         /* PR interface Id High */
 };
 
 /* PORT Header Register Set */
drivers/fpga/intel-fpga-fme-mgr.c new file +55 -0
new file mode 100644
index 0000000..5f93dfb
@@ -0,0 +1,307 @@
+/*
+ * FPGA Manager Driver for Intel FPGA Management Engine (FME)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Kang Luwei <[email protected]>
+ *   Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>
+ *   Wu Hao <[email protected]>
+ *   Joseph Grecco <[email protected]>
+ *   Enno Luebbers <[email protected]>
+ *   Tim Whisonant <[email protected]>
+ *   Ananda Ravuri <[email protected]>
+ *   Christopher Rauer <[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/module.h>
+#include <linux/iopoll.h>
+#include <linux/fpga/fpga-mgr.h>
+
+#include "intel-feature-dev.h"
+#include "intel-fme.h"
+
+#define PR_WAIT_TIMEOUT   8000000
+#define PR_HOST_STATUS_IDLE        0
+
+struct fme_mgr_priv {
+   void __iomem *ioaddr;
+};
+
+static ssize_t interface_id_show(struct device *dev,
+                            struct device_attribute *attr, char *buf)
+{
+   struct fpga_manager *mgr = dev_get_drvdata(dev);
+   struct fme_mgr_priv *priv = mgr->priv;
+   struct feature_fme_pr *fme_pr;
+   u64 intfc_id_l, intfc_id_h;
+
+   fme_pr = priv->ioaddr;
+
+   intfc_id_l = readq(&fme_pr->intfc_id_l);
+   intfc_id_h = readq(&fme_pr->intfc_id_h);
+
+   return scnprintf(buf, PAGE_SIZE, "%016llx%016llx\n",
+                   (unsigned long long)intfc_id_h,
+                   (unsigned long long)intfc_id_l);
+}
+static DEVICE_ATTR_RO(interface_id);
+
+static const struct attribute *fme_mgr_attrs[] = {
+   &dev_attr_interface_id.attr,

drivers/fpga/intel-fpga-fme-mgr.c line 65

Quoted 10 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +10 -0
+   NULL,
+};
+
+static void fme_mgr_pr_update_status(struct fpga_manager *mgr,
+                                struct feature_fme_pr_error err)
+{
+   mgr->status = 0;
+
+   if (err.operation_err)
+           mgr->status |= FPGA_MGR_STATUS_OPERATION_ERR;

Can we just have a (err & FEATURE_FME_PR_ERROR) here instead ?

Quoted 4 lines

drivers/fpga/intel-fpga-fme-mgr.c line 67

Quoted 2 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +2 -0
+   if (err.crc_err)
+           mgr->status |= FPGA_MGR_STATUS_CRC_ERR;

Can we just have a (err & FEATURE_FME_CRC_ERROR) here instead ?

As above, do you think if we can keep the origin code here? Use "if (err.crc_err)" not "if (err & FEATURE_FME_CRC_ERROR)", it makes the code shorter in most cases. :)

Thanks
Hao

Quoted 249 lines
Quoted 241 lines
drivers/fpga/intel-fpga-fme-mgr.c new file +240 -1
+   if (err.incompatiable_bs)
+           mgr->status |= FPGA_MGR_STATUS_INCOMPATIBLE_BS_ERR;
+   if (err.protocol_err)
+           mgr->status |= FPGA_MGR_STATUS_IP_PROTOCOL_ERR;
+   if (err.fifo_overflow)
+           mgr->status |= FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR;
+   if (err.secure_load_err)
+           mgr->status |= FPGA_MGR_STATUS_SECURE_LOAD_ERR;
+}
+
+static u64 fme_mgr_pr_err_handle(struct feature_fme_pr *fme_pr)
+{
+   struct feature_fme_pr_status fme_pr_status;
+   struct feature_fme_pr_error fme_pr_error;
+
+   fme_pr_status.csr = readq(&fme_pr->status);
+   if (!fme_pr_status.pr_status)
+           return 0;
+
+   fme_pr_error.csr = readq(&fme_pr->error);
+   writeq(fme_pr_error.csr, &fme_pr->error);
+
+   return fme_pr_error.csr;
+}
+
+static int fme_mgr_write_init(struct fpga_manager *mgr,
+                         struct fpga_image_info *info,
+                         const char *buf, size_t count)
+{
+   struct device *dev = &mgr->dev;
+   struct fme_mgr_priv *priv = mgr->priv;
+   struct feature_fme_pr *fme_pr = priv->ioaddr;
+   struct feature_fme_pr_ctl fme_pr_ctl;
+   struct feature_fme_pr_status fme_pr_status;
+   struct feature_fme_pr_error fme_pr_err;
+
+   if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
+           dev_err(dev, "only support partial reconfiguration.\n");
+           return -EINVAL;
+   }
+
+   dev_dbg(dev, "resetting PR before initiated PR\n");
+
+   fme_pr_ctl.csr = readq(&fme_pr->control);
+   fme_pr_ctl.pr_reset = 1;
+   writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+   if (readq_poll_timeout(&fme_pr->control, fme_pr_ctl.csr,
+                          (fme_pr_ctl.pr_reset_ack == 1),
+                          1, PR_WAIT_TIMEOUT)) {
+           dev_err(dev, "maximum PR timeout\n");
+           return -ETIMEDOUT;
+   }
+
+   fme_pr_ctl.csr = readq(&fme_pr->control);
+   fme_pr_ctl.pr_reset = 0;
+   writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+   dev_dbg(dev,
+           "waiting for PR resource in HW to be initialized and ready\n");
+
+   fme_pr_status.pr_host_status = PR_HOST_STATUS_IDLE;
+
+   if (readq_poll_timeout(&fme_pr->status, fme_pr_status.csr,
+                   (fme_pr_status.pr_host_status == PR_HOST_STATUS_IDLE),
+                   1, PR_WAIT_TIMEOUT)) {
+           dev_err(dev, "maximum PR timeout\n");
+           return -ETIMEDOUT;
+   }
+
+   dev_dbg(dev, "check and clear previous PR error\n");
+   fme_pr_err.csr = fme_mgr_pr_err_handle(fme_pr);
+   if (fme_pr_err.csr)
+           dev_dbg(dev, "previous PR error detected %llx\n",
+                        (unsigned long long)fme_pr_err.csr);
+
+   /* Clear all PR errors */
+   fme_pr_err.csr = 0;
+   fme_mgr_pr_update_status(mgr, fmCRCe_pr_err);
+
+   dev_dbg(dev, "set PR port ID\n");
+
+   fme_pr_ctl.csr = readq(&fme_pr->control);
+   fme_pr_ctl.pr_regionid = info->region_id;
+   writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+   return 0;
+}
+
+static int fme_mgr_write(struct fpga_manager *mgr,
+                    const char *buf, size_t count)
+{
+   struct device *dev = &mgr->dev;
+   struct fme_mgr_priv *priv = mgr->priv;
+   struct feature_fme_pr *fme_pr = priv->ioaddr;
+   struct feature_fme_pr_ctl fme_pr_ctl;
+   struct feature_fme_pr_status fme_pr_status;
+   struct feature_fme_pr_data fme_pr_data;
+   int delay, pr_credit, i = 0;
+
+   dev_dbg(dev, "start request\n");
+
+   fme_pr_ctl.csr = readq(&fme_pr->control);
+   fme_pr_ctl.pr_start_req = 1;
+   writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+   dev_dbg(dev, "pushing data from bitstream to HW\n");
+
+   fme_pr_status.csr = readq(&fme_pr->status);
+   pr_credit = fme_pr_status.pr_credit;
+
+   while (count > 0) {
+           delay = 0;
+           while (pr_credit <= 1) {
+                   if (delay++ > PR_WAIT_TIMEOUT) {
+                           dev_err(dev, "maximum try\n");
+                           return -ETIMEDOUT;
+                   }
+                   udelay(1);
+
+                   fme_pr_status.csr = readq(&fme_pr->status);
+                   pr_credit = fme_pr_status.pr_credit;
+           };
+
+           if (count >= 4) {
+                   fme_pr_data.rsvd = 0;
+                   fme_pr_data.pr_data_raw = *(((u32 *)buf) + i);
+                   writeq(fme_pr_data.csr, &fme_pr->data);
+                   count -= 4;
+                   pr_credit--;
+                   i++;
+           } else {
+                   WARN_ON(1);
+                   return -EINVAL;
+           }
+   }
+
+   return 0;
+}
+
+static int fme_mgr_write_complete(struct fpga_manager *mgr,
+                             struct fpga_image_info *info)
+{
+   struct device *dev = &mgr->dev;
+   struct fme_mgr_priv *priv = mgr->priv;
+   struct feature_fme_pr *fme_pr = priv->ioaddr;
+   struct feature_fme_pr_ctl fme_pr_ctl;
+   struct feature_fme_pr_error fme_pr_err;
+
+   fme_pr_ctl.csr = readq(&fme_pr->control);
+   fme_pr_ctl.pr_push_complete = 1;
+   writeq(fme_pr_ctl.csr, &fme_pr->control);
+
+   dev_dbg(dev, "green bitstream push complete\n");
+   dev_dbg(dev, "waiting for HW to release PR resource\n");
+
+   fme_pr_ctl.pr_start_req = 0;
+
+   if (readq_poll_timeout(&fme_pr->control, fme_pr_ctl.csr,
+                          (fme_pr_ctl.pr_start_req == 0),
+                          1, PR_WAIT_TIMEOUT)) {
+           dev_err(dev, "maximum try.\n");
+           return -ETIMEDOUT;
+   }
+
+   dev_dbg(dev, "PR operation complete, checking status\n");
+   fme_pr_err.csr = fme_mgr_pr_err_handle(fme_pr);
+   if (fme_pr_err.csr) {
+           fme_mgr_pr_update_status(mgr, fme_pr_err);
+           return -EIO;
+   }
+
+   dev_dbg(dev, "PR done successfully\n");
+   return 0;
+}
+
+static enum fpga_mgr_states fme_mgr_state(struct fpga_manager *mgr)
+{
+   return FPGA_MGR_STATE_UNKNOWN;
+}
+
+static const struct fpga_manager_ops fme_mgr_ops = {
+   .write_init = fme_mgr_write_init,
+   .write = fme_mgr_write,
+   .write_complete = fme_mgr_write_complete,
+   .state = fme_mgr_state,
+};
+
+static int fme_mgr_probe(struct platform_device *pdev)
+{
+   struct device *dev = &pdev->dev;
+   struct fme_mgr_priv *priv;
+   struct resource *res;
+   int ret;
+
+   priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+   if (!priv)
+           return -ENOMEM;
+
+   res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+   priv->ioaddr = devm_ioremap(dev, res->start, resource_size(res));
+   if (IS_ERR(priv->ioaddr))
+           return PTR_ERR(priv->ioaddr);
+
+   ret = sysfs_create_files(&pdev->dev.kobj, fme_mgr_attrs);
+   if (ret)
+           return ret;
+
+   ret = fpga_mgr_register(dev, "Intel FPGA Manager", &fme_mgr_ops, priv);
+   if (ret) {
+           dev_err(dev, "unable to register FPGA manager\n");
+           sysfs_remove_files(&pdev->dev.kobj, fme_mgr_attrs);
+           return ret;
+   }
+
+   return 0;
+}
+
+static int fme_mgr_remove(struct platform_device *pdev)
+{
+   fpga_mgr_unregister(&pdev->dev);
+   sysfs_remove_files(&pdev->dev.kobj, fme_mgr_attrs);
+
+   return 0;
+}
+
+static struct platform_driver fme_mgr_driver = {
+   .driver = {
+           .name    = INTEL_FPGA_FME_MGR,
+   },
+   .probe   = fme_mgr_probe,
+   .remove  = fme_mgr_remove,
+};
+
+module_platform_driver(fme_mgr_driver);
+
+MODULE_DESCRIPTION("FPGA Manager for Intel FPGA Management Engine");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-fme-mgr");
--
Quoted 2 lines

1.8.3.1

[1] http://yarchive.net/comp/linux/bitfields.html
[2] https://lwn.net/Articles/478657/

Thanks,
Moritz

author
#67 lore raw

PATCH v2 16/22 fpga: intel: add fpga bridge platform driver for FME

In reply to Wu Hao

This patch adds fpga bridge platform driver for Intel FPGA Management Engine. It implements the enable_set call back for fpga bridge.

---
 drivers/fpga/Kconfig             |  7 ++++
 drivers/fpga/Makefile            |  1 +
 drivers/fpga/intel-fpga-fme-br.c | 77 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+)
 create mode 100644 drivers/fpga/intel-fpga-fme-br.c
drivers/fpga/Kconfig +7 -0
index 6f2f623..05e2a8e 100644
@@ -158,6 +158,13 @@ config INTEL_FPGA_FME_MGR
 	  Say Y to enable FPGA Manager driver for Intel FPGA Management
 	  Engine.
 
+config INTEL_FPGA_FME_BRIDGE
+	tristate "Intel FPGA FME Bridge Driver"
+	depends on INTEL_FPGA_FME && FPGA_BRIDGE
+	help
+	  Say Y to enable FPGA Bridge driver for Intel FPGA Management
+	  Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index d1d588b..131c105 100644
@@ -32,6 +32,7 @@ obj-$(CONFIG_OF_FPGA_REGION)		+= of-fpga-region.o
 obj-$(CONFIG_INTEL_FPGA_PCI)		+= intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)		+= intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)	+= intel-fpga-fme-mgr.o
+obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)	+= intel-fpga-fme-br.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-fpga-fme-br.c new file +77 -0

77 lines not on this page · raw message

Signature · 1 line
1.8.3.1
Acked-by on v2 16/22
#68 lore raw

Re: [PATCH v2 16/22] fpga: intel: add fpga bridge platform driver for FME

Quoted 8 lines · Wu Hao

This patch adds fpga bridge platform driver for Intel FPGA Management Engine. It implements the enable_set call back for fpga bridge.

Quoted 123 lines
---
 drivers/fpga/Kconfig             |  7 ++++
 drivers/fpga/Makefile            |  1 +
 drivers/fpga/intel-fpga-fme-br.c | 77 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+)
 create mode 100644 drivers/fpga/intel-fpga-fme-br.c
drivers/fpga/Kconfig +7 -0
index 6f2f623..05e2a8e 100644
@@ -158,6 +158,13 @@ config INTEL_FPGA_FME_MGR
          Say Y to enable FPGA Manager driver for Intel FPGA Management
          Engine.
 
+config INTEL_FPGA_FME_BRIDGE
+       tristate "Intel FPGA FME Bridge Driver"
+       depends on INTEL_FPGA_FME && FPGA_BRIDGE
+       help
+         Say Y to enable FPGA Bridge driver for Intel FPGA Management
+         Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index d1d588b..131c105 100644
@@ -32,6 +32,7 @@ obj-$(CONFIG_OF_FPGA_REGION)          += of-fpga-region.o
 obj-$(CONFIG_INTEL_FPGA_PCI)           += intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)           += intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)       += intel-fpga-fme-mgr.o
+obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)    += intel-fpga-fme-br.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-fpga-fme-br.c new file +77 -1
new file mode 100644
index 0000000..9b922d2
@@ -0,0 +1,77 @@
+/*
+ * FPGA Bridge Driver for Intel FPGA Management Engine (FME)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Wu Hao <[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/module.h>
+#include <linux/fpga/fpga-bridge.h>
+
+#include "intel-feature-dev.h"
+#include "intel-fme.h"
+
+static int fme_bridge_enable_set(struct fpga_bridge *bridge, bool enable)
+{
+       struct fme_br_pdata *pdata = bridge->priv;
+       int ret = 0;
+
+       if (enable)
+               fpga_port_enable(pdata->port);
+       else
+               ret = fpga_port_disable(pdata->port);
+
+       return ret;
+}
+
+static const struct fpga_bridge_ops fme_bridge_ops = {
+       .enable_set = fme_bridge_enable_set,
+};
+
+static int fme_br_probe(struct platform_device *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct fme_br_pdata *pdata = dev_get_platdata(dev);
+       int ret;
+
+       ret = fpga_bridge_register(dev, "Intel FPGA FME Bridge",
+                                  &fme_bridge_ops, pdata);
+       if (ret)
+               dev_err(dev, "unable to register FPGA Bridge\n");
+
+       dev_dbg(dev, "Intel FME FPGA Bridge probed\n");
+
+       return ret;
+}
+
+static int fme_br_remove(struct platform_device *pdev)
+{
+       fpga_bridge_unregister(&pdev->dev);
+
+       return 0;
+}
+
+static struct platform_driver fme_br_driver = {
+       .driver = {
+               .name    = INTEL_FPGA_FME_BRIDGE,
+       },
+       .probe   = fme_br_probe,
+       .remove  = fme_br_remove,
+};
+
+module_platform_driver(fme_br_driver);
+
+MODULE_DESCRIPTION("FPGA Bridge for Intel FPGA Management Engine");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-fme-bridge");
--

1.8.3.1

Reviewed-by on v2 16/22
#69 lore raw

Re: [PATCH v2 16/22] fpga: intel: add fpga bridge platform driver for FME

In reply to Wu Hao

Hi Wu,

looks good. Minor nits inline.

Quoted 8 lines · Wu Hao

This patch adds fpga bridge platform driver for Intel FPGA Management Engine. It implements the enable_set call back for fpga bridge.

Quoted 48 lines
---
 drivers/fpga/Kconfig             |  7 ++++
 drivers/fpga/Makefile            |  1 +
 drivers/fpga/intel-fpga-fme-br.c | 77 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+)
 create mode 100644 drivers/fpga/intel-fpga-fme-br.c
drivers/fpga/Kconfig +7 -0
index 6f2f623..05e2a8e 100644
@@ -158,6 +158,13 @@ config INTEL_FPGA_FME_MGR
          Say Y to enable FPGA Manager driver for Intel FPGA Management
          Engine.
 
+config INTEL_FPGA_FME_BRIDGE
+       tristate "Intel FPGA FME Bridge Driver"
+       depends on INTEL_FPGA_FME && FPGA_BRIDGE
+       help
+         Say Y to enable FPGA Bridge driver for Intel FPGA Management
+         Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index d1d588b..131c105 100644
@@ -32,6 +32,7 @@ obj-$(CONFIG_OF_FPGA_REGION)          += of-fpga-region.o
 obj-$(CONFIG_INTEL_FPGA_PCI)           += intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)           += intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)       += intel-fpga-fme-mgr.o
+obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)    += intel-fpga-fme-br.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-fpga-fme-br.c new file +5 -0
new file mode 100644
index 0000000..9b922d2
@@ -0,0 +1,77 @@
+/*
+ * FPGA Bridge Driver for Intel FPGA Management Engine (FME)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *

drivers/fpga/intel-fpga-fme-br.c line 15

Quoted 10 lines
drivers/fpga/intel-fpga-fme-br.c new file +10 -0
+ * Authors:
+ *   Wu Hao <[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.

Could replace that with SPDX-Licence-Identifier: GPL-2.0

Quoted 27 lines
drivers/fpga/intel-fpga-fme-br.c new file +27 -0
+ */
+
+#include <linux/module.h>
+#include <linux/fpga/fpga-bridge.h>
+
+#include "intel-feature-dev.h"
+#include "intel-fme.h"
+
+static int fme_bridge_enable_set(struct fpga_bridge *bridge, bool enable)
+{
+       struct fme_br_pdata *pdata = bridge->priv;
+       int ret = 0;
+
+       if (enable)
+               fpga_port_enable(pdata->port);
+       else
+               ret = fpga_port_disable(pdata->port);
+
+       return ret;
+}
+
+static const struct fpga_bridge_ops fme_bridge_ops = {
+       .enable_set = fme_bridge_enable_set,
+};
+
+static int fme_br_probe(struct platform_device *pdev)
+{

drivers/fpga/intel-fpga-fme-br.c line 52

Quoted 10 lines
drivers/fpga/intel-fpga-fme-br.c new file +10 -0
+       struct device *dev = &pdev->dev;
+       struct fme_br_pdata *pdata = dev_get_platdata(dev);
+       int ret;
+
+       ret = fpga_bridge_register(dev, "Intel FPGA FME Bridge",
+                                  &fme_bridge_ops, pdata);
+       if (ret)
+               dev_err(dev, "unable to register FPGA Bridge\n");
+
+       dev_dbg(dev, "Intel FME FPGA Bridge probed\n");

Mixed signals here. Did it work or did it not. Either return early, in case of error. Or get rid of dev_dbg().

Quoted 28 lines
drivers/fpga/intel-fpga-fme-br.c new file +25 -1
+
+       return ret;
+}
+
+static int fme_br_remove(struct platform_device *pdev)
+{
+       fpga_bridge_unregister(&pdev->dev);
+
+       return 0;
+}
+
+static struct platform_driver fme_br_driver = {
+       .driver = {
+               .name    = INTEL_FPGA_FME_BRIDGE,
+       },
+       .probe   = fme_br_probe,
+       .remove  = fme_br_remove,
+};
+
+module_platform_driver(fme_br_driver);
+
+MODULE_DESCRIPTION("FPGA Bridge for Intel FPGA Management Engine");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-fme-bridge");
--

1.8.3.1

Quoted 4 lines

--
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

Thanks,

Moritz

author
#70 lore raw

Re: [PATCH v2 16/22] fpga: intel: add fpga bridge platform driver for FME

Quoted 3 lines · Moritz Fischer

Hi Wu,

looks good. Minor nits inline.

Hi Moritz,

Thanks for your comments. : )

Quoted 64 lines · Wu Hao
Quoted 8 lines

This patch adds fpga bridge platform driver for Intel FPGA Management Engine. It implements the enable_set call back for fpga bridge.

Quoted 52 lines
---
 drivers/fpga/Kconfig             |  7 ++++
 drivers/fpga/Makefile            |  1 +
 drivers/fpga/intel-fpga-fme-br.c | 77 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+)
 create mode 100644 drivers/fpga/intel-fpga-fme-br.c
drivers/fpga/Kconfig +7 -0
index 6f2f623..05e2a8e 100644
@@ -158,6 +158,13 @@ config INTEL_FPGA_FME_MGR
          Say Y to enable FPGA Manager driver for Intel FPGA Management
          Engine.
 
+config INTEL_FPGA_FME_BRIDGE
+       tristate "Intel FPGA FME Bridge Driver"
+       depends on INTEL_FPGA_FME && FPGA_BRIDGE
+       help
+         Say Y to enable FPGA Bridge driver for Intel FPGA Management
+         Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index d1d588b..131c105 100644
@@ -32,6 +32,7 @@ obj-$(CONFIG_OF_FPGA_REGION)          += of-fpga-region.o
 obj-$(CONFIG_INTEL_FPGA_PCI)           += intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)           += intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)       += intel-fpga-fme-mgr.o
+obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)    += intel-fpga-fme-br.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-fpga-fme-br.c new file +9 -0
new file mode 100644
index 0000000..9b922d2
@@ -0,0 +1,77 @@
+/*
+ * FPGA Bridge Driver for Intel FPGA Management Engine (FME)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Wu Hao <[email protected]>
+ *   Joseph Grecco <[email protected]>
+ *   Enno Luebbers <[email protected]>
Quoted 8 lines

drivers/fpga/intel-fpga-fme-br.c line 15

Quoted 6 lines
drivers/fpga/intel-fpga-fme-br.c new file +6 -0
+ *   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.

Could replace that with SPDX-Licence-Identifier: GPL-2.0

Sure, I will update the header like this. : )

* This work is licensed under the terms of the GNU GPL version 2.
* SPDX-License-Identifier: GPL-2.0
*/

Quoted 32 lines
Quoted 32 lines
drivers/fpga/intel-fpga-fme-br.c new file +32 -0
+ */
+
+#include <linux/module.h>
+#include <linux/fpga/fpga-bridge.h>
+
+#include "intel-feature-dev.h"
+#include "intel-fme.h"
+
+static int fme_bridge_enable_set(struct fpga_bridge *bridge, bool enable)
+{
+       struct fme_br_pdata *pdata = bridge->priv;
+       int ret = 0;
+
+       if (enable)
+               fpga_port_enable(pdata->port);
+       else
+               ret = fpga_port_disable(pdata->port);
+
+       return ret;
+}
+
+static const struct fpga_bridge_ops fme_bridge_ops = {
+       .enable_set = fme_bridge_enable_set,
+};
+
+static int fme_br_probe(struct platform_device *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct fme_br_pdata *pdata = dev_get_platdata(dev);
+       int ret;
+
+       ret = fpga_bridge_register(dev, "Intel FPGA FME Bridge",
Quoted 8 lines

drivers/fpga/intel-fpga-fme-br.c line 52

Quoted 5 lines
drivers/fpga/intel-fpga-fme-br.c new file +5 -0
+                                  &fme_bridge_ops, pdata);
+       if (ret)
+               dev_err(dev, "unable to register FPGA Bridge\n");
+
+       dev_dbg(dev, "Intel FME FPGA Bridge probed\n");

Mixed signals here. Did it work or did it not. Either return early, in case of error. Or get rid of dev_dbg().

Agree, will remove this dev_dbg. As fpga_bridge_register will print some message using dev_info if success.

Thanks
Hao

Quoted 36 lines
Quoted 28 lines
drivers/fpga/intel-fpga-fme-br.c new file +25 -1
+
+       return ret;
+}
+
+static int fme_br_remove(struct platform_device *pdev)
+{
+       fpga_bridge_unregister(&pdev->dev);
+
+       return 0;
+}
+
+static struct platform_driver fme_br_driver = {
+       .driver = {
+               .name    = INTEL_FPGA_FME_BRIDGE,
+       },
+       .probe   = fme_br_probe,
+       .remove  = fme_br_remove,
+};
+
+module_platform_driver(fme_br_driver);
+
+MODULE_DESCRIPTION("FPGA Bridge for Intel FPGA Management Engine");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-fme-bridge");
--

1.8.3.1

Quoted 4 lines

--
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

Thanks,

Moritz

author
#71 lore raw

PATCH v2 17/22 fpga: intel: add fpga region platform driver for FME

In reply to Wu Hao

This patch adds fpga region platform driver for Intel FPGA Management Engine. It register a fpga region with given fpga manager / bridge device.

---
 drivers/fpga/Kconfig                 |  7 +++
 drivers/fpga/Makefile                |  1 +
 drivers/fpga/intel-fpga-fme-region.c | 91 ++++++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+)
 create mode 100644 drivers/fpga/intel-fpga-fme-region.c
drivers/fpga/Kconfig +7 -0
index 05e2a8e..db1a6ea 100644
@@ -165,6 +165,13 @@ config INTEL_FPGA_FME_BRIDGE
 	  Say Y to enable FPGA Bridge driver for Intel FPGA Management
 	  Engine.
 
+config INTEL_FPGA_FME_REGION
+	tristate "Intel FPGA FME Region Driver"
+	depends on INTEL_FPGA_FME && FPGA_REGION
+	help
+	  Say Y to enable FPGA Region driver for Intel FPGA Management
+	  Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index 131c105..fd0a71f 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_INTEL_FPGA_PCI)		+= intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)		+= intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)	+= intel-fpga-fme-mgr.o
 obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)	+= intel-fpga-fme-br.o
+obj-$(CONFIG_INTEL_FPGA_FME_REGION)	+= intel-fpga-fme-region.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-fpga-fme-region.c new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#72 lore raw

Re: [PATCH v2 17/22] fpga: intel: add fpga region platform driver for FME

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

Hi Hao,

Thanks for using the region patchset. This looks good except for one thing below.

Quoted 115 lines

This patch adds fpga region platform driver for Intel FPGA Management Engine. It register a fpga region with given fpga manager / bridge device.

---
 drivers/fpga/Kconfig                 |  7 +++
 drivers/fpga/Makefile                |  1 +
 drivers/fpga/intel-fpga-fme-region.c | 91 ++++++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+)
 create mode 100644 drivers/fpga/intel-fpga-fme-region.c
drivers/fpga/Kconfig +7 -0
index 05e2a8e..db1a6ea 100644
@@ -165,6 +165,13 @@ config INTEL_FPGA_FME_BRIDGE
          Say Y to enable FPGA Bridge driver for Intel FPGA Management
          Engine.
 
+config INTEL_FPGA_FME_REGION
+       tristate "Intel FPGA FME Region Driver"
+       depends on INTEL_FPGA_FME && FPGA_REGION
+       help
+         Say Y to enable FPGA Region driver for Intel FPGA Management
+         Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index 131c105..fd0a71f 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_INTEL_FPGA_PCI)          += intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)           += intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)       += intel-fpga-fme-mgr.o
 obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)    += intel-fpga-fme-br.o
+obj-$(CONFIG_INTEL_FPGA_FME_REGION)    += intel-fpga-fme-region.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-fpga-fme-region.c new file +63 -0
new file mode 100644
index 0000000..2b5ca4d
@@ -0,0 +1,91 @@
+/*
+ * FPGA Region Driver for Intel FPGA Management Engine (FME)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Wu Hao <[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/module.h>
+#include <linux/fpga/fpga-region.h>
+
+#include "intel-feature-dev.h"
+#include "intel-fme.h"
+
+static int fme_region_get_bridges(struct fpga_region *region,
+                                 struct fpga_image_info *info)
+{
+       struct fme_region_pdata *pdata = region->priv;
+       struct device *dev = &pdata->br->dev;
+
+       return fpga_bridge_get_to_list(dev, info, &region->bridge_list);
+}
+
+static int fme_region_probe(struct platform_device *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct fme_region_pdata *pdata = dev_get_platdata(dev);
+       struct fpga_region *region;
+       struct fpga_manager *mgr;
+       int ret;
+
+       mgr = fpga_mgr_get(&pdata->mgr->dev);
+       if (IS_ERR(mgr))
+               return -EPROBE_DEFER;
+
+       region = devm_kzalloc(dev, sizeof(*region), GFP_KERNEL);
+       if (!region) {
+               ret = -ENOMEM;
+               goto eprobe_mgr_put;
+       }
+
+       region->mgr = mgr;
+       region->get_bridges = fme_region_get_bridges;
+       region->priv = pdata;
+
+       ret = fpga_region_register(dev, region);
+       if (ret)
+               goto eprobe_mgr_put;
+
+       dev_dbg(dev, "Intel FME FPGA Region probed\n");
+
+       return 0;
+
+eprobe_mgr_put:

drivers/fpga/intel-fpga-fme-region.c line 73

Quoted 10 lines
drivers/fpga/intel-fpga-fme-region.c new file +10 -0
+       fpga_mgr_put(mgr);
+       return ret;
+}
+
+static int fme_region_remove(struct platform_device *pdev)
+{
+       struct fpga_region *region = dev_get_drvdata(&pdev->dev);
+
+       fpga_mgr_put(region->mgr);
+       fpga_region_unregister(region);

Please unregister before putting the mgr ;)

Alan

Quoted 21 lines
drivers/fpga/intel-fpga-fme-region.c new file +18 -1
+
+       return 0;
+}
+
+static struct platform_driver fme_region_driver = {
+       .driver = {
+               .name    = INTEL_FPGA_FME_REGION,
+       },
+       .probe   = fme_region_probe,
+       .remove  = fme_region_remove,
+};
+
+module_platform_driver(fme_region_driver);
+
+MODULE_DESCRIPTION("FPGA Region for Intel FPGA Management Engine");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-fme-region");
--

1.8.3.1

author
#73 lore raw

Re: [PATCH v2 17/22] fpga: intel: add fpga region platform driver for FME

Quoted 6 lines · Alan Tull

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

Hi Hao,

Thanks for using the region patchset. This looks good except for one thing below.

Hi Alan

Thank you very much for your review. : )

Quoted 120 lines
Quoted 120 lines

This patch adds fpga region platform driver for Intel FPGA Management Engine. It register a fpga region with given fpga manager / bridge device.

---
 drivers/fpga/Kconfig                 |  7 +++
 drivers/fpga/Makefile                |  1 +
 drivers/fpga/intel-fpga-fme-region.c | 91 ++++++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+)
 create mode 100644 drivers/fpga/intel-fpga-fme-region.c
drivers/fpga/Kconfig +7 -0
index 05e2a8e..db1a6ea 100644
@@ -165,6 +165,13 @@ config INTEL_FPGA_FME_BRIDGE
          Say Y to enable FPGA Bridge driver for Intel FPGA Management
          Engine.
 
+config INTEL_FPGA_FME_REGION
+       tristate "Intel FPGA FME Region Driver"
+       depends on INTEL_FPGA_FME && FPGA_REGION
+       help
+         Say Y to enable FPGA Region driver for Intel FPGA Management
+         Engine.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +1 -0
index 131c105..fd0a71f 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_INTEL_FPGA_PCI)          += intel-fpga-pci.o
 obj-$(CONFIG_INTEL_FPGA_FME)           += intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)       += intel-fpga-fme-mgr.o
 obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)    += intel-fpga-fme-br.o
+obj-$(CONFIG_INTEL_FPGA_FME_REGION)    += intel-fpga-fme-region.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
drivers/fpga/intel-fpga-fme-region.c new file +67 -0
new file mode 100644
index 0000000..2b5ca4d
@@ -0,0 +1,91 @@
+/*
+ * FPGA Region Driver for Intel FPGA Management Engine (FME)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Wu Hao <[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/module.h>
+#include <linux/fpga/fpga-region.h>
+
+#include "intel-feature-dev.h"
+#include "intel-fme.h"
+
+static int fme_region_get_bridges(struct fpga_region *region,
+                                 struct fpga_image_info *info)
+{
+       struct fme_region_pdata *pdata = region->priv;
+       struct device *dev = &pdata->br->dev;
+
+       return fpga_bridge_get_to_list(dev, info, &region->bridge_list);
+}
+
+static int fme_region_probe(struct platform_device *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct fme_region_pdata *pdata = dev_get_platdata(dev);
+       struct fpga_region *region;
+       struct fpga_manager *mgr;
+       int ret;
+
+       mgr = fpga_mgr_get(&pdata->mgr->dev);
+       if (IS_ERR(mgr))
+               return -EPROBE_DEFER;
+
+       region = devm_kzalloc(dev, sizeof(*region), GFP_KERNEL);
+       if (!region) {
+               ret = -ENOMEM;
+               goto eprobe_mgr_put;
+       }
+
+       region->mgr = mgr;
+       region->get_bridges = fme_region_get_bridges;
+       region->priv = pdata;
+
+       ret = fpga_region_register(dev, region);
+       if (ret)
+               goto eprobe_mgr_put;
+
+       dev_dbg(dev, "Intel FME FPGA Region probed\n");
+
+       return 0;
+
+eprobe_mgr_put:
+       fpga_mgr_put(mgr);
+       return ret;
+}
+
Quoted 9 lines

drivers/fpga/intel-fpga-fme-region.c line 73

Quoted 6 lines
drivers/fpga/intel-fpga-fme-region.c new file +6 -0
+static int fme_region_remove(struct platform_device *pdev)
+{
+       struct fpga_region *region = dev_get_drvdata(&pdev->dev);
+
+       fpga_mgr_put(region->mgr);
+       fpga_region_unregister(region);

Please unregister before putting the mgr ;)

Sure, make sense, I will fix this in the next version.

Thanks
Hao

author
#74 lore raw

PATCH v2 18/22 fpga: intel: add FPGA Accelerated Function Unit driver basic framework

In reply to Wu Hao

On Intel FPGA devices, the Accelerated Function Unit (AFU), can be reprogrammed for different functions. It connects to the FPGA infrastructure("blue bistream") via a Port. Port CSRs are implemented separately from the AFU CSRs to provide control and status of the Port. Once valid green bitstream is programmed into the AFU, it allows access to the AFU CSRs in the AFU MMIO space.

This patch only implements basic driver framework for AFU, including device file operation framework.

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

---
 drivers/fpga/Kconfig          |   9 +++
 drivers/fpga/Makefile         |   2 +
 drivers/fpga/intel-afu-main.c | 159 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 170 insertions(+)
 create mode 100644 drivers/fpga/intel-afu-main.c
drivers/fpga/Kconfig +9 -0
index db1a6ea..4658a13 100644
@@ -172,6 +172,15 @@ config INTEL_FPGA_FME_REGION
 	  Say Y to enable FPGA Region driver for Intel FPGA Management
 	  Engine.
 
+config INTEL_FPGA_AFU
+	tristate "Intel FPGA AFU Driver"
+	depends on INTEL_FPGA_PCI
+	help
+	  This is the driver for FPGA Accelerated Function Unit (AFU) which
+	  implements AFU and Port management features. A User AFU connects
+	  to the FPGA infrastructure via a Port. There may be more than 1
+	  Port/AFU per Intel FPGA.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +2 -0
index fd0a71f..ce08833 100644
@@ -34,6 +34,8 @@ obj-$(CONFIG_INTEL_FPGA_FME)		+= intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)	+= intel-fpga-fme-mgr.o
 obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)	+= intel-fpga-fme-br.o
 obj-$(CONFIG_INTEL_FPGA_FME_REGION)	+= intel-fpga-fme-region.o
+obj-$(CONFIG_INTEL_FPGA_AFU)		+= intel-fpga-afu.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
+intel-fpga-afu-objs := intel-afu-main.o
drivers/fpga/intel-afu-main.c new file +159 -0

159 lines not on this page · raw message

Signature · 1 line
1.8.3.1
Acked-by on v2 18/22
#75 lore raw

Re: [PATCH v2 18/22] fpga: intel: add FPGA Accelerated Function Unit driver basic framework

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

Hi Hao,

Just a few minor things below. Otherwise, it's fine.

Quoted 10 lines

On Intel FPGA devices, the Accelerated Function Unit (AFU), can be reprogrammed for different functions. It connects to the FPGA infrastructure("blue bistream") via a Port. Port CSRs are implemented separately from the AFU CSRs to provide control and status of the Port. Once valid green bitstream is programmed into the AFU, it allows access to the AFU CSRs in the AFU MMIO space.

This patch only implements basic driver framework for AFU, including device file operation framework.

Quoted 6 lines
Quoted 76 lines

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

---
 drivers/fpga/Kconfig          |   9 +++
 drivers/fpga/Makefile         |   2 +
 drivers/fpga/intel-afu-main.c | 159 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 170 insertions(+)
 create mode 100644 drivers/fpga/intel-afu-main.c
drivers/fpga/Kconfig +9 -0
index db1a6ea..4658a13 100644
@@ -172,6 +172,15 @@ config INTEL_FPGA_FME_REGION
          Say Y to enable FPGA Region driver for Intel FPGA Management
          Engine.
 
+config INTEL_FPGA_AFU
+       tristate "Intel FPGA AFU Driver"
+       depends on INTEL_FPGA_PCI
+       help
+         This is the driver for FPGA Accelerated Function Unit (AFU) which
+         implements AFU and Port management features. A User AFU connects
+         to the FPGA infrastructure via a Port. There may be more than 1
+         Port/AFU per Intel FPGA.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +2 -0
index fd0a71f..ce08833 100644
@@ -34,6 +34,8 @@ obj-$(CONFIG_INTEL_FPGA_FME)          += intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)       += intel-fpga-fme-mgr.o
 obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)    += intel-fpga-fme-br.o
 obj-$(CONFIG_INTEL_FPGA_FME_REGION)    += intel-fpga-fme-region.o
+obj-$(CONFIG_INTEL_FPGA_AFU)           += intel-fpga-afu.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
+intel-fpga-afu-objs := intel-afu-main.o
drivers/fpga/intel-afu-main.c new file +27 -0
new file mode 100644
index 0000000..96d0367
@@ -0,0 +1,159 @@
+/*
+ * Driver for Intel FPGA Accelerated Function Unit (AFU)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[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/kernel.h>
+#include <linux/module.h>
+
+#include "intel-feature-dev.h"
+
+static int port_hdr_init(struct platform_device *pdev, struct feature *feature)
+{
+       dev_dbg(&pdev->dev, "PORT HDR Init.\n");
+

drivers/fpga/intel-afu-main.c line 37

Quoted 10 lines
drivers/fpga/intel-afu-main.c new file +10 -0
+       return 0;
+}
+
+static void port_hdr_uinit(struct platform_device *pdev,
+                                       struct feature *feature)
+{
+       dev_dbg(&pdev->dev, "PORT HDR UInit.\n");
+}
+
+struct feature_ops port_hdr_ops = {

static const struct?

drivers/fpga/intel-afu-main.c line 42

Quoted 5 lines
drivers/fpga/intel-afu-main.c new file +5 -0
+       .init = port_hdr_init,
+       .uinit = port_hdr_uinit,
+};
+
+static struct feature_driver port_feature_drvs[] = {

static const struct?

Quoted 27 lines
drivers/fpga/intel-afu-main.c new file +27 -0
+       {
+               .name = PORT_FEATURE_HEADER,
+               .ops = &port_hdr_ops,
+       },
+       {
+               .ops = NULL,
+       }
+};
+
+static int afu_open(struct inode *inode, struct file *filp)
+{
+       struct platform_device *fdev = fpga_inode_to_feature_dev(inode);
+       struct feature_platform_data *pdata;
+       int ret;
+
+       pdata = dev_get_platdata(&fdev->dev);
+       if (WARN_ON(!pdata))
+               return -ENODEV;
+
+       ret = feature_dev_use_begin(pdata);
+       if (ret)
+               return ret;
+
+       dev_dbg(&fdev->dev, "Device File Open\n");
+       filp->private_data = fdev;
+
+       return 0;

drivers/fpga/intel-afu-main.c line 79

Quoted 10 lines
drivers/fpga/intel-afu-main.c new file +10 -0
+}
+
+static int afu_release(struct inode *inode, struct file *filp)
+{
+       struct platform_device *pdev = filp->private_data;
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+       dev_dbg(&pdev->dev, "Device File Release\n");
+
+       feature_dev_use_end(pdata);

Please skip a line before return.

Quoted 53 lines
drivers/fpga/intel-afu-main.c new file +53 -0
+       return 0;
+}
+
+static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+       struct platform_device *pdev = filp->private_data;
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature *f;
+       long ret;
+
+       dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
+
+       switch (cmd) {
+       default:
+               /*
+                * Let sub-feature's ioctl function to handle the cmd
+                * Sub-feature's ioctl returns -ENODEV when cmd is not
+                * handled in this sub feature, and returns 0 and other
+                * error code if cmd is handled.
+                */
+               fpga_dev_for_each_feature(pdata, f)
+                       if (f->ops && f->ops->ioctl) {
+                               ret = f->ops->ioctl(pdev, f, cmd, arg);
+                               if (ret == -ENODEV)
+                                       continue;
+                               else
+                                       return ret;
+                       }
+       }
+
+       return -EINVAL;
+}
+
+static const struct file_operations afu_fops = {
+       .owner = THIS_MODULE,
+       .open = afu_open,
+       .release = afu_release,
+       .unlocked_ioctl = afu_ioctl,
+};
+
+static int afu_probe(struct platform_device *pdev)
+{
+       int ret;
+
+       dev_dbg(&pdev->dev, "%s\n", __func__);
+
+       ret = fpga_dev_feature_init(pdev, port_feature_drvs);
+       if (ret)
+               return ret;
+
+       ret = fpga_register_dev_ops(pdev, &afu_fops, THIS_MODULE);
+       if (ret)
+               fpga_dev_feature_uinit(pdev);

drivers/fpga/intel-afu-main.c line 142

Quoted 10 lines
drivers/fpga/intel-afu-main.c new file +10 -0
+
+       return ret;
+}
+
+static int afu_remove(struct platform_device *pdev)
+{
+       dev_dbg(&pdev->dev, "%s\n", __func__);
+
+       fpga_dev_feature_uinit(pdev);
+       fpga_unregister_dev_ops(pdev);

Please skip a line before return.

Quoted 20 lines
drivers/fpga/intel-afu-main.c new file +17 -1
+       return 0;
+}
+
+static struct platform_driver afu_driver = {
+       .driver = {
+               .name    = "intel-fpga-port",
+       },
+       .probe   = afu_probe,
+       .remove  = afu_remove,
+};
+
+module_platform_driver(afu_driver);
+
+MODULE_DESCRIPTION("Intel FPGA Accelerated Function Unit driver");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-port");
--

1.8.3.1

author
#76 lore raw

Re: [PATCH v2 18/22] fpga: intel: add FPGA Accelerated Function Unit driver basic framework

Quoted 5 lines · Alan Tull

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

Hi Hao,

Just a few minor things below. Otherwise, it's fine.

Hi Alan

Thanks a lot for the review. :)

Quoted 99 lines
Quoted 11 lines

On Intel FPGA devices, the Accelerated Function Unit (AFU), can be reprogrammed for different functions. It connects to the FPGA infrastructure("blue bistream") via a Port. Port CSRs are implemented separately from the AFU CSRs to provide control and status of the Port. Once valid green bitstream is programmed into the AFU, it allows access to the AFU CSRs in the AFU MMIO space.

This patch only implements basic driver framework for AFU, including device file operation framework.

Quoted 6 lines
Quoted 80 lines

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

---
 drivers/fpga/Kconfig          |   9 +++
 drivers/fpga/Makefile         |   2 +
 drivers/fpga/intel-afu-main.c | 159 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 170 insertions(+)
 create mode 100644 drivers/fpga/intel-afu-main.c
drivers/fpga/Kconfig +9 -0
index db1a6ea..4658a13 100644
@@ -172,6 +172,15 @@ config INTEL_FPGA_FME_REGION
          Say Y to enable FPGA Region driver for Intel FPGA Management
          Engine.
 
+config INTEL_FPGA_AFU
+       tristate "Intel FPGA AFU Driver"
+       depends on INTEL_FPGA_PCI
+       help
+         This is the driver for FPGA Accelerated Function Unit (AFU) which
+         implements AFU and Port management features. A User AFU connects
+         to the FPGA infrastructure via a Port. There may be more than 1
+         Port/AFU per Intel FPGA.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +2 -0
index fd0a71f..ce08833 100644
@@ -34,6 +34,8 @@ obj-$(CONFIG_INTEL_FPGA_FME)          += intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)       += intel-fpga-fme-mgr.o
 obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)    += intel-fpga-fme-br.o
 obj-$(CONFIG_INTEL_FPGA_FME_REGION)    += intel-fpga-fme-region.o
+obj-$(CONFIG_INTEL_FPGA_AFU)           += intel-fpga-afu.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
+intel-fpga-afu-objs := intel-afu-main.o
drivers/fpga/intel-afu-main.c new file +31 -0
new file mode 100644
index 0000000..96d0367
@@ -0,0 +1,159 @@
+/*
+ * Driver for Intel FPGA Accelerated Function Unit (AFU)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[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/kernel.h>
+#include <linux/module.h>
+
+#include "intel-feature-dev.h"
+
+static int port_hdr_init(struct platform_device *pdev, struct feature *feature)
+{
+       dev_dbg(&pdev->dev, "PORT HDR Init.\n");
+
+       return 0;
+}
+
+static void port_hdr_uinit(struct platform_device *pdev,
Quoted 8 lines

drivers/fpga/intel-afu-main.c line 37

Quoted 6 lines
drivers/fpga/intel-afu-main.c new file +6 -0
+                                       struct feature *feature)
+{
+       dev_dbg(&pdev->dev, "PORT HDR UInit.\n");
+}
+
+struct feature_ops port_hdr_ops = {

static const struct?

Will fix this.

Quoted 8 lines

drivers/fpga/intel-afu-main.c line 42

Quoted 6 lines
drivers/fpga/intel-afu-main.c new file +5 -0
+       .init = port_hdr_init,
+       .uinit = port_hdr_uinit,
+};
+
+static struct feature_driver port_feature_drvs[] = {

static const struct?

Will fix this.

Quoted 32 lines
Quoted 32 lines
drivers/fpga/intel-afu-main.c new file +31 -0
+       {
+               .name = PORT_FEATURE_HEADER,
+               .ops = &port_hdr_ops,
+       },
+       {
+               .ops = NULL,
+       }
+};
+
+static int afu_open(struct inode *inode, struct file *filp)
+{
+       struct platform_device *fdev = fpga_inode_to_feature_dev(inode);
+       struct feature_platform_data *pdata;
+       int ret;
+
+       pdata = dev_get_platdata(&fdev->dev);
+       if (WARN_ON(!pdata))
+               return -ENODEV;
+
+       ret = feature_dev_use_begin(pdata);
+       if (ret)
+               return ret;
+
+       dev_dbg(&fdev->dev, "Device File Open\n");
+       filp->private_data = fdev;
+
+       return 0;
+}
+
+static int afu_release(struct inode *inode, struct file *filp)
+{
Quoted 8 lines

drivers/fpga/intel-afu-main.c line 79

Quoted 6 lines
drivers/fpga/intel-afu-main.c new file +6 -0
+       struct platform_device *pdev = filp->private_data;
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+       dev_dbg(&pdev->dev, "Device File Release\n");
+
+       feature_dev_use_end(pdata);

Please skip a line before return.

Will fix this.

Quoted 58 lines
Quoted 58 lines
drivers/fpga/intel-afu-main.c new file +57 -0
+       return 0;
+}
+
+static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+       struct platform_device *pdev = filp->private_data;
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature *f;
+       long ret;
+
+       dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
+
+       switch (cmd) {
+       default:
+               /*
+                * Let sub-feature's ioctl function to handle the cmd
+                * Sub-feature's ioctl returns -ENODEV when cmd is not
+                * handled in this sub feature, and returns 0 and other
+                * error code if cmd is handled.
+                */
+               fpga_dev_for_each_feature(pdata, f)
+                       if (f->ops && f->ops->ioctl) {
+                               ret = f->ops->ioctl(pdev, f, cmd, arg);
+                               if (ret == -ENODEV)
+                                       continue;
+                               else
+                                       return ret;
+                       }
+       }
+
+       return -EINVAL;
+}
+
+static const struct file_operations afu_fops = {
+       .owner = THIS_MODULE,
+       .open = afu_open,
+       .release = afu_release,
+       .unlocked_ioctl = afu_ioctl,
+};
+
+static int afu_probe(struct platform_device *pdev)
+{
+       int ret;
+
+       dev_dbg(&pdev->dev, "%s\n", __func__);
+
+       ret = fpga_dev_feature_init(pdev, port_feature_drvs);
+       if (ret)
+               return ret;
+
+       ret = fpga_register_dev_ops(pdev, &afu_fops, THIS_MODULE);
+       if (ret)
+               fpga_dev_feature_uinit(pdev);
+
+       return ret;
+}
+
Quoted 8 lines

drivers/fpga/intel-afu-main.c line 142

Quoted 6 lines
drivers/fpga/intel-afu-main.c new file +6 -0
+static int afu_remove(struct platform_device *pdev)
+{
+       dev_dbg(&pdev->dev, "%s\n", __func__);
+
+       fpga_dev_feature_uinit(pdev);
+       fpga_unregister_dev_ops(pdev);

Please skip a line before return.

Will fix this.

Thanks
Hao

Quoted 21 lines
Quoted 21 lines
drivers/fpga/intel-afu-main.c new file +17 -1
+       return 0;
+}
+
+static struct platform_driver afu_driver = {
+       .driver = {
+               .name    = "intel-fpga-port",
+       },
+       .probe   = afu_probe,
+       .remove  = afu_remove,
+};
+
+module_platform_driver(afu_driver);
+
+MODULE_DESCRIPTION("Intel FPGA Accelerated Function Unit driver");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-port");
--

1.8.3.1

#77 lore raw

Re: [PATCH v2 18/22] fpga: intel: add FPGA Accelerated Function Unit driver basic framework

In reply to Wu Hao
Quoted 71 lines · Wu Hao

On Intel FPGA devices, the Accelerated Function Unit (AFU), can be reprogrammed for different functions. It connects to the FPGA infrastructure("blue bistream") via a Port. Port CSRs are implemented separately from the AFU CSRs to provide control and status of the Port. Once valid green bitstream is programmed into the AFU, it allows access to the AFU CSRs in the AFU MMIO space.

This patch only implements basic driver framework for AFU, including device file operation framework.

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

---
 drivers/fpga/Kconfig          |   9 +++
 drivers/fpga/Makefile         |   2 +
 drivers/fpga/intel-afu-main.c | 159 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 170 insertions(+)
 create mode 100644 drivers/fpga/intel-afu-main.c
drivers/fpga/Kconfig +9 -0
index db1a6ea..4658a13 100644
@@ -172,6 +172,15 @@ config INTEL_FPGA_FME_REGION
          Say Y to enable FPGA Region driver for Intel FPGA Management
          Engine.
 
+config INTEL_FPGA_AFU
+       tristate "Intel FPGA AFU Driver"
+       depends on INTEL_FPGA_PCI
+       help
+         This is the driver for FPGA Accelerated Function Unit (AFU) which
+         implements AFU and Port management features. A User AFU connects
+         to the FPGA infrastructure via a Port. There may be more than 1
+         Port/AFU per Intel FPGA.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +2 -0
index fd0a71f..ce08833 100644
@@ -34,6 +34,8 @@ obj-$(CONFIG_INTEL_FPGA_FME)          += intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)       += intel-fpga-fme-mgr.o
 obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)    += intel-fpga-fme-br.o
 obj-$(CONFIG_INTEL_FPGA_FME_REGION)    += intel-fpga-fme-region.o
+obj-$(CONFIG_INTEL_FPGA_AFU)           += intel-fpga-afu.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
+intel-fpga-afu-objs := intel-afu-main.o
drivers/fpga/intel-afu-main.c new file +6 -0
new file mode 100644
index 0000000..96d0367
@@ -0,0 +1,159 @@
+/*
+ * Driver for Intel FPGA Accelerated Function Unit (AFU)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:

drivers/fpga/intel-afu-main.c line 16

Quoted 10 lines
drivers/fpga/intel-afu-main.c new file +10 -0
+ *   Wu Hao <[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.

Or just: SPDX-License-Identifier: GPL-2.0

Quoted 146 lines
drivers/fpga/intel-afu-main.c new file +143 -1
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#include "intel-feature-dev.h"
+
+static int port_hdr_init(struct platform_device *pdev, struct feature *feature)
+{
+       dev_dbg(&pdev->dev, "PORT HDR Init.\n");
+
+       return 0;
+}
+
+static void port_hdr_uinit(struct platform_device *pdev,
+                                       struct feature *feature)
+{
+       dev_dbg(&pdev->dev, "PORT HDR UInit.\n");
+}
+
+struct feature_ops port_hdr_ops = {
+       .init = port_hdr_init,
+       .uinit = port_hdr_uinit,
+};
+
+static struct feature_driver port_feature_drvs[] = {
+       {
+               .name = PORT_FEATURE_HEADER,
+               .ops = &port_hdr_ops,
+       },
+       {
+               .ops = NULL,
+       }
+};
+
+static int afu_open(struct inode *inode, struct file *filp)
+{
+       struct platform_device *fdev = fpga_inode_to_feature_dev(inode);
+       struct feature_platform_data *pdata;
+       int ret;
+
+       pdata = dev_get_platdata(&fdev->dev);
+       if (WARN_ON(!pdata))
+               return -ENODEV;
+
+       ret = feature_dev_use_begin(pdata);
+       if (ret)
+               return ret;
+
+       dev_dbg(&fdev->dev, "Device File Open\n");
+       filp->private_data = fdev;
+
+       return 0;
+}
+
+static int afu_release(struct inode *inode, struct file *filp)
+{
+       struct platform_device *pdev = filp->private_data;
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+       dev_dbg(&pdev->dev, "Device File Release\n");
+
+       feature_dev_use_end(pdata);
+       return 0;
+}
+
+static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+       struct platform_device *pdev = filp->private_data;
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature *f;
+       long ret;
+
+       dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
+
+       switch (cmd) {
+       default:
+               /*
+                * Let sub-feature's ioctl function to handle the cmd
+                * Sub-feature's ioctl returns -ENODEV when cmd is not
+                * handled in this sub feature, and returns 0 and other
+                * error code if cmd is handled.
+                */
+               fpga_dev_for_each_feature(pdata, f)
+                       if (f->ops && f->ops->ioctl) {
+                               ret = f->ops->ioctl(pdev, f, cmd, arg);
+                               if (ret == -ENODEV)
+                                       continue;
+                               else
+                                       return ret;
+                       }
+       }
+
+       return -EINVAL;
+}
+
+static const struct file_operations afu_fops = {
+       .owner = THIS_MODULE,
+       .open = afu_open,
+       .release = afu_release,
+       .unlocked_ioctl = afu_ioctl,
+};
+
+static int afu_probe(struct platform_device *pdev)
+{
+       int ret;
+
+       dev_dbg(&pdev->dev, "%s\n", __func__);
+
+       ret = fpga_dev_feature_init(pdev, port_feature_drvs);
+       if (ret)
+               return ret;
+
+       ret = fpga_register_dev_ops(pdev, &afu_fops, THIS_MODULE);
+       if (ret)
+               fpga_dev_feature_uinit(pdev);
+
+       return ret;
+}
+
+static int afu_remove(struct platform_device *pdev)
+{
+       dev_dbg(&pdev->dev, "%s\n", __func__);
+
+       fpga_dev_feature_uinit(pdev);
+       fpga_unregister_dev_ops(pdev);
+       return 0;
+}
+
+static struct platform_driver afu_driver = {
+       .driver = {
+               .name    = "intel-fpga-port",
+       },
+       .probe   = afu_probe,
+       .remove  = afu_remove,
+};
+
+module_platform_driver(afu_driver);
+
+MODULE_DESCRIPTION("Intel FPGA Accelerated Function Unit driver");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-port");
--

1.8.3.1

Quoted 4 lines

--
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

Thanks,

Moritz

author
#78 lore raw

Re: [PATCH v2 18/22] fpga: intel: add FPGA Accelerated Function Unit driver basic framework

Quoted 76 lines · Moritz Fischer
Quoted 75 lines · Wu Hao

On Intel FPGA devices, the Accelerated Function Unit (AFU), can be reprogrammed for different functions. It connects to the FPGA infrastructure("blue bistream") via a Port. Port CSRs are implemented separately from the AFU CSRs to provide control and status of the Port. Once valid green bitstream is programmed into the AFU, it allows access to the AFU CSRs in the AFU MMIO space.

This patch only implements basic driver framework for AFU, including device file operation framework.

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

---
 drivers/fpga/Kconfig          |   9 +++
 drivers/fpga/Makefile         |   2 +
 drivers/fpga/intel-afu-main.c | 159 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 170 insertions(+)
 create mode 100644 drivers/fpga/intel-afu-main.c
drivers/fpga/Kconfig +9 -0
index db1a6ea..4658a13 100644
@@ -172,6 +172,15 @@ config INTEL_FPGA_FME_REGION
          Say Y to enable FPGA Region driver for Intel FPGA Management
          Engine.
 
+config INTEL_FPGA_AFU
+       tristate "Intel FPGA AFU Driver"
+       depends on INTEL_FPGA_PCI
+       help
+         This is the driver for FPGA Accelerated Function Unit (AFU) which
+         implements AFU and Port management features. A User AFU connects
+         to the FPGA infrastructure via a Port. There may be more than 1
+         Port/AFU per Intel FPGA.
+
 endif
 
 endif # FPGA
drivers/fpga/Makefile +2 -0
index fd0a71f..ce08833 100644
@@ -34,6 +34,8 @@ obj-$(CONFIG_INTEL_FPGA_FME)          += intel-fpga-fme.o
 obj-$(CONFIG_INTEL_FPGA_FME_MGR)       += intel-fpga-fme-mgr.o
 obj-$(CONFIG_INTEL_FPGA_FME_BRIDGE)    += intel-fpga-fme-br.o
 obj-$(CONFIG_INTEL_FPGA_FME_REGION)    += intel-fpga-fme-region.o
+obj-$(CONFIG_INTEL_FPGA_AFU)           += intel-fpga-afu.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
+intel-fpga-afu-objs := intel-afu-main.o
drivers/fpga/intel-afu-main.c new file +10 -0
new file mode 100644
index 0000000..96d0367
@@ -0,0 +1,159 @@
+/*
+ * Driver for Intel FPGA Accelerated Function Unit (AFU)
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   Wu Hao <[email protected]>
+ *   Xiao Guangrong <guangrong.xiao-VuQAYsv1563Yd54FQh9/[email protected]>
+ *   Joseph Grecco <[email protected]>
+ *   Enno Luebbers <[email protected]>
Quoted 8 lines

drivers/fpga/intel-afu-main.c line 16

Quoted 6 lines
drivers/fpga/intel-afu-main.c new file +6 -0
+ *   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.

Or just: SPDX-License-Identifier: GPL-2.0

Sure, will update it.

Thanks
Hao

Quoted 154 lines
Quoted 146 lines
drivers/fpga/intel-afu-main.c new file +143 -1
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#include "intel-feature-dev.h"
+
+static int port_hdr_init(struct platform_device *pdev, struct feature *feature)
+{
+       dev_dbg(&pdev->dev, "PORT HDR Init.\n");
+
+       return 0;
+}
+
+static void port_hdr_uinit(struct platform_device *pdev,
+                                       struct feature *feature)
+{
+       dev_dbg(&pdev->dev, "PORT HDR UInit.\n");
+}
+
+struct feature_ops port_hdr_ops = {
+       .init = port_hdr_init,
+       .uinit = port_hdr_uinit,
+};
+
+static struct feature_driver port_feature_drvs[] = {
+       {
+               .name = PORT_FEATURE_HEADER,
+               .ops = &port_hdr_ops,
+       },
+       {
+               .ops = NULL,
+       }
+};
+
+static int afu_open(struct inode *inode, struct file *filp)
+{
+       struct platform_device *fdev = fpga_inode_to_feature_dev(inode);
+       struct feature_platform_data *pdata;
+       int ret;
+
+       pdata = dev_get_platdata(&fdev->dev);
+       if (WARN_ON(!pdata))
+               return -ENODEV;
+
+       ret = feature_dev_use_begin(pdata);
+       if (ret)
+               return ret;
+
+       dev_dbg(&fdev->dev, "Device File Open\n");
+       filp->private_data = fdev;
+
+       return 0;
+}
+
+static int afu_release(struct inode *inode, struct file *filp)
+{
+       struct platform_device *pdev = filp->private_data;
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+
+       dev_dbg(&pdev->dev, "Device File Release\n");
+
+       feature_dev_use_end(pdata);
+       return 0;
+}
+
+static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+       struct platform_device *pdev = filp->private_data;
+       struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct feature *f;
+       long ret;
+
+       dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
+
+       switch (cmd) {
+       default:
+               /*
+                * Let sub-feature's ioctl function to handle the cmd
+                * Sub-feature's ioctl returns -ENODEV when cmd is not
+                * handled in this sub feature, and returns 0 and other
+                * error code if cmd is handled.
+                */
+               fpga_dev_for_each_feature(pdata, f)
+                       if (f->ops && f->ops->ioctl) {
+                               ret = f->ops->ioctl(pdev, f, cmd, arg);
+                               if (ret == -ENODEV)
+                                       continue;
+                               else
+                                       return ret;
+                       }
+       }
+
+       return -EINVAL;
+}
+
+static const struct file_operations afu_fops = {
+       .owner = THIS_MODULE,
+       .open = afu_open,
+       .release = afu_release,
+       .unlocked_ioctl = afu_ioctl,
+};
+
+static int afu_probe(struct platform_device *pdev)
+{
+       int ret;
+
+       dev_dbg(&pdev->dev, "%s\n", __func__);
+
+       ret = fpga_dev_feature_init(pdev, port_feature_drvs);
+       if (ret)
+               return ret;
+
+       ret = fpga_register_dev_ops(pdev, &afu_fops, THIS_MODULE);
+       if (ret)
+               fpga_dev_feature_uinit(pdev);
+
+       return ret;
+}
+
+static int afu_remove(struct platform_device *pdev)
+{
+       dev_dbg(&pdev->dev, "%s\n", __func__);
+
+       fpga_dev_feature_uinit(pdev);
+       fpga_unregister_dev_ops(pdev);
+       return 0;
+}
+
+static struct platform_driver afu_driver = {
+       .driver = {
+               .name    = "intel-fpga-port",
+       },
+       .probe   = afu_probe,
+       .remove  = afu_remove,
+};
+
+module_platform_driver(afu_driver);
+
+MODULE_DESCRIPTION("Intel FPGA Accelerated Function Unit driver");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:intel-fpga-port");
--

1.8.3.1

Quoted 4 lines

--
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

Thanks,

Moritz

author
#79 lore raw

PATCH v2 19/22 fpga: intel: afu: add header sub feature support

In reply to Wu Hao

The header register set is always present for the Port/AFU, it is mainly for capability, control and status of the ports that AFU connected to.

This patch implements header sub feature support. Below user interfaces are created by this patch.

Sysfs interface:
* /sys/class/fpga/<fpga.x>/<intel-fpga-port.x>/id
Read-only. Port ID.

Ioctl interface:
* FPGA_PORT_RESET
Reset the FPGA AFU Port.

---
v2: add sysfs documentation.

---
 .../ABI/testing/sysfs-platform-intel-fpga-afu      |  7 ++++
 drivers/fpga/intel-afu-main.c                      | 44 +++++++++++++++++++++-
 include/uapi/linux/intel-fpga.h                    | 14 +++++++
 3 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-afu
Documentation/ABI/testing/sysfs-platform-intel-fpga-afu new file +7 -0
new file mode 100644
index 0000000..8ad22c9
@@ -0,0 +1,7 @@
+What:		/sys/bus/platform/devices/intel-fpga-port.0/id
+Date:		June 2017
+KernelVersion:  4.12
+Contact:	Wu Hao <[email protected]>
+Description:	Read-only. It returns id of this port. One Intel FPGA device
+		may have more than one port. Userspace could use this id to
+		distinguish different ports under same FPGA device.
drivers/fpga/intel-afu-main.c +43 -1

74 lines not on this page · raw message

include/uapi/linux/intel-fpga.h +14 -0
index be295ae..be5f813 100644
@@ -30,8 +30,11 @@
 #define FPGA_MAGIC 0xB6
 
 #define FPGA_BASE 0
+#define PORT_BASE 0x40
 #define FME_BASE 0x80
 
+/* Common IOCTLs for both FME and AFU file descriptor */
+
 /**
  * FPGA_GET_API_VERSION - _IO(FPGA_MAGIC, FPGA_BASE + 0)
  *
@@ -50,6 +53,17 @@
 
 #define FPGA_CHECK_EXTENSION	_IO(FPGA_MAGIC, FPGA_BASE + 1)
 
+/* IOCTLs for AFU file descriptor */
+
+/**
+ * FPGA_PORT_RESET - _IO(FPGA_MAGIC, PORT_BASE + 0)
+ *
+ * Reset the FPGA AFU Port. No parameters are supported.
+ * Return: 0 on success, -errno of failure
+ */
+
+#define FPGA_PORT_RESET		_IO(FPGA_MAGIC, PORT_BASE + 0)
+
 /* IOCTLs for FME file descriptor */
 
 /**
Signature · 1 line
1.8.3.1
#80 lore raw

Re: [PATCH v2 19/22] fpga: intel: afu: add header sub feature support

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

Hi Hao,

Quoted 2 lines

The header register set is always present for the Port/AFU, it is mainly for capability, control and status of the ports that AFU connected to.

So just to be clear, the reset function is acting on the Port, not the AFU, right?

Quoted 2 lines

This patch implements header sub feature support.

Please add a brief reminder here what the 'header' is. It's defined in patch 7 as being part of the feature list, but hardly mentioned when I grep intel-fpga.txt.

Quoted 61 lines

Below user interfaces
are created by this patch.

Sysfs interface:
* /sys/class/fpga/<fpga.x>/<intel-fpga-port.x>/id
Read-only. Port ID.

Ioctl interface:
* FPGA_PORT_RESET
Reset the FPGA AFU Port.

---
v2: add sysfs documentation.

---
 .../ABI/testing/sysfs-platform-intel-fpga-afu      |  7 ++++
 drivers/fpga/intel-afu-main.c                      | 44 +++++++++++++++++++++-
 include/uapi/linux/intel-fpga.h                    | 14 +++++++
 3 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-afu
Documentation/ABI/testing/sysfs-platform-intel-fpga-afu new file +7 -0
new file mode 100644
index 0000000..8ad22c9
@@ -0,0 +1,7 @@
+What:          /sys/bus/platform/devices/intel-fpga-port.0/id
+Date:          June 2017
+KernelVersion:  4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. It returns id of this port. One Intel FPGA device
+               may have more than one port. Userspace could use this id to
+               distinguish different ports under same FPGA device.
drivers/fpga/intel-afu-main.c +11 -0
index 96d0367..2a17cde 100644
@@ -18,25 +18,66 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/intel-fpga.h>
 
 #include "intel-feature-dev.h"
 
+static ssize_t
+id_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       int id = fpga_port_id(to_platform_device(dev));
+
+       return scnprintf(buf, PAGE_SIZE, "%d\n", id);
+}
+static DEVICE_ATTR_RO(id);
+
+static const struct attribute *port_hdr_attrs[] = {

drivers/fpga/intel-afu-main.c line 43

Quoted 10 lines
drivers/fpga/intel-afu-main.c +5 -1
+       &dev_attr_id.attr,
+       NULL,
+};
+
 static int port_hdr_init(struct platform_device *pdev, struct feature *feature)
 {
        dev_dbg(&pdev->dev, "PORT HDR Init.\n");
 
-       return 0;
+       fpga_port_reset(pdev);

So the port will be reset here, which happens during fme_probe(). IIUC the PR region is empty then, there is just the static region, right?

drivers/fpga/intel-afu-main.c line 45

Quoted 2 lines
drivers/fpga/intel-afu-main.c +2 -0
+
+       return sysfs_create_files(&pdev->dev.kobj, port_hdr_attrs);

Greg wrote an article that there could be a race condition caused by creating sysfs files this late [1] and I see sysfs_create_files() used very sparingly in the kernel. I'm thinking that fpga-bridge should provide a place to create sysfs files earlier by adding an attribute_group to fpga_bridge_ops (same for fpga-mgr and fpga-region) and then fpga_bridge_register could do bridge->dev.groups = br_ops->groups. I'll put a patch for that out soon.

Quoted 10 lines
drivers/fpga/intel-afu-main.c +4 -0
 }
 
 static void port_hdr_uinit(struct platform_device *pdev,
                                        struct feature *feature)
 {
        dev_dbg(&pdev->dev, "PORT HDR UInit.\n");
+
+       sysfs_remove_files(&pdev->dev.kobj, port_hdr_attrs);
+}
+

drivers/fpga/intel-afu-main.c line 65

Quoted 10 lines
drivers/fpga/intel-afu-main.c +10 -0
+static long
+port_hdr_ioctl(struct platform_device *pdev, struct feature *feature,
+                                       unsigned int cmd, unsigned long arg)
+{
+       long ret;
+
+       switch (cmd) {
+       case FPGA_PORT_RESET:
+               if (!arg)
+                       ret = fpga_port_reset(pdev);

fpga_port_reset() disables and reenables traffic on the port. Is there ever a time when that would be unsafe to do? Like while DMA is happening? When I see a function called 'reset' exposed to userspace, I become concerned that hitting that reset at the wrong time could cause problems. We've discussed this some, but could you please remind me when userspace would need to reset the port? Please add documentation of what the intended use of this ioctl would be, when it is valid to request a reset from userspace and when userspace should never do that.

The pcie code, the AFU file interface, and the bridge code all do port reset. This code is spread out over a few patches, but I'll comment here for now. I'm trying to keep track of everything that resets the port. The port gets reset in afu_probe, fme_probe, the AFU file release, and intel-pcie.c after parsing the features. Also the port is esentially reset after doing reprogramming, since that involves a
bridge disable/enable. In the v1 review, the issue raised that the
port functionality could be an expansion of fpga-bridge. If reset were added to the fpga_bridge_ops and a fpga_bridge_reset API added to fpga-bridge, then anything in the kernel that owns the bridge could reset it. That is of course assuming that this code doesn't need to reset the port before the fpga-bridge is created.

Thanks,
Alan Tull

[1] http://www.kroah.com/log/blog/2013/06/26/how-to-create-a-sysfs-file-correctly/

Quoted 63 lines
drivers/fpga/intel-afu-main.c +11 -0
+               else
+                       ret = -EINVAL;
+               break;
+       default:
+               dev_dbg(&pdev->dev, "%x cmd not handled", cmd);
+               ret = -ENODEV;
+       }
+
+       return ret;
 }
 
 struct feature_ops port_hdr_ops = {
        .init = port_hdr_init,
        .uinit = port_hdr_uinit,
+       .ioctl = port_hdr_ioctl,
 };
 
 static struct feature_driver port_feature_drvs[] = {
@@ -76,6 +117,7 @@ static int afu_release(struct inode *inode, struct file *filp)
 
        dev_dbg(&pdev->dev, "Device File Release\n");
 
+       fpga_port_reset(pdev);
        feature_dev_use_end(pdata);
        return 0;
 }
include/uapi/linux/intel-fpga.h +14 -1
index be295ae..be5f813 100644
@@ -30,8 +30,11 @@
 #define FPGA_MAGIC 0xB6
 
 #define FPGA_BASE 0
+#define PORT_BASE 0x40
 #define FME_BASE 0x80
 
+/* Common IOCTLs for both FME and AFU file descriptor */
+
 /**
  * FPGA_GET_API_VERSION - _IO(FPGA_MAGIC, FPGA_BASE + 0)
  *
@@ -50,6 +53,17 @@
 
 #define FPGA_CHECK_EXTENSION   _IO(FPGA_MAGIC, FPGA_BASE + 1)
 
+/* IOCTLs for AFU file descriptor */
+
+/**
+ * FPGA_PORT_RESET - _IO(FPGA_MAGIC, PORT_BASE + 0)
+ *
+ * Reset the FPGA AFU Port. No parameters are supported.
+ * Return: 0 on success, -errno of failure
+ */
+
+#define FPGA_PORT_RESET                _IO(FPGA_MAGIC, PORT_BASE + 0)
+
 /* IOCTLs for FME file descriptor */
 
 /**
--

1.8.3.1

author
#81 lore raw

RE: [PATCH v2 19/22] fpga: intel: afu: add header sub feature support

Quoted 9 lines · Wu Hao

Hi Hao,

Quoted 2 lines

The header register set is always present for the Port/AFU, it is mainly for capability, control and status of the ports that AFU connected to.

So just to be clear, the reset function is acting on the Port, not the AFU, right?

Hi Alan

AFU will be reset as well. Once port reset is issued, a compliant HW AFU will reset all its state and stop sending requests, and HW will set port reset ack bit when all outstanding requests initiated have been drained.

Will add some notes here.

Quoted 7 lines
Quoted 3 lines

This patch implements header sub feature support.

Please add a brief reminder here what the 'header' is. It's defined in patch 7 as being part of the feature list, but hardly mentioned when I grep intel-fpga.txt.

Sure, will adds some notes here.

Actually the header sub feature means the registers belong to the feature device (e.g port and FME), not any sub features (e.g PR, Power management).

Quoted 72 lines
Quoted 18 lines

Below user interfaces
are created by this patch.

Sysfs interface:
* /sys/class/fpga/<fpga.x>/<intel-fpga-port.x>/id
Read-only. Port ID.

Ioctl interface:
* FPGA_PORT_RESET
Reset the FPGA AFU Port.

Quoted 8 lines

---
v2: add sysfs documentation.

---
 .../ABI/testing/sysfs-platform-intel-fpga-afu      |  7 ++++
 drivers/fpga/intel-afu-main.c                      | 44 +++++++++++++++++++++-
 include/uapi/linux/intel-fpga.h                    | 14 +++++++
 3 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-

afu

Review

Quoted 2 lines
(quoted diff) +0 -0

b/Documentation/ABI/testing/sysfs-platform-intel-fpga-afu

Quoted 42 lines

new file mode 100644
index 0000000..8ad22c9

Documentation/ABI/testing/sysfs-platform-intel-fpga-afu new file +7 -0
@@ -0,0 +1,7 @@
+What:          /sys/bus/platform/devices/intel-fpga-port.0/id
+Date:          June 2017
+KernelVersion:  4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. It returns id of this port. One Intel FPGA device
+               may have more than one port. Userspace could use this id to
+               distinguish different ports under same FPGA device.
drivers/fpga/intel-afu-main.c +15 -0
index 96d0367..2a17cde 100644
@@ -18,25 +18,66 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/intel-fpga.h>
 
 #include "intel-feature-dev.h"
 
+static ssize_t
+id_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       int id = fpga_port_id(to_platform_device(dev));
+
+       return scnprintf(buf, PAGE_SIZE, "%d\n", id);
+}
+static DEVICE_ATTR_RO(id);
+
+static const struct attribute *port_hdr_attrs[] = {
+       &dev_attr_id.attr,
+       NULL,
+};
+
 static int port_hdr_init(struct platform_device *pdev, struct feature *feature)
 {
        dev_dbg(&pdev->dev, "PORT HDR Init.\n");
 
Quoted 6 lines

drivers/fpga/intel-afu-main.c line 43

Quoted 2 lines
drivers/fpga/intel-afu-main.c +1 -1
-       return 0;
+       fpga_port_reset(pdev);

So the port will be reset here, which happens during fme_probe(). IIUC the PR region is empty then, there is just the static region, right?

port_hdr_init is invoked during afu_probe() function. The fpga_port_reset only resets the AFU's state and not empty the PR region. User doesn't need to program it again after port reset.

The purpose of this reset in port_hdr_init function, is to make sure that we could have a clean start whenever the port driver module is loaded. And similar as the one added in afu release.

Quoted 4 lines

drivers/fpga/intel-afu-main.c line 45

Quoted 3 lines
drivers/fpga/intel-afu-main.c +2 -0
+
+       return sysfs_create_files(&pdev->dev.kobj, port_hdr_attrs);
Quoted 8 lines

Greg wrote an article that there could be a race condition caused by creating sysfs files this late [1] and I see sysfs_create_files() used very sparingly in the kernel. I'm thinking that fpga-bridge should provide a place to create sysfs files earlier by adding an attribute_group to fpga_bridge_ops (same for fpga-mgr and fpga-region) and then fpga_bridge_register could do bridge->dev.groups = br_ops->groups. I'll put a patch for that out soon.

Hm... I understand there could be a race condition if creates sysfs files late. Actually the reasons I prefer to have each sub feature to create its own sysfs files are, 1) if any sub feature is not present, then related init function won't be invoked and related sysfs files won't be created at all. Then end user could know which sub features are present by checking these sysfs nodes easily.
2) Another point of view is about extension of each sub feature, for example, Some new registers introduced when sub feature's revision > n, so each sub feature's init function could check the sub feature's revision to decide which sysfs interfaces are needed. I think this should be a flexible way for extension.

Quoted 22 lines
Quoted 10 lines
drivers/fpga/intel-afu-main.c +4 -0
 }
 
 static void port_hdr_uinit(struct platform_device *pdev,
                                        struct feature *feature)
 {
        dev_dbg(&pdev->dev, "PORT HDR UInit.\n");
+
+       sysfs_remove_files(&pdev->dev.kobj, port_hdr_attrs);
+}
+

drivers/fpga/intel-afu-main.c line 65

Quoted 10 lines
drivers/fpga/intel-afu-main.c +10 -0
+static long
+port_hdr_ioctl(struct platform_device *pdev, struct feature *feature,
+                                       unsigned int cmd, unsigned long arg)
+{
+       long ret;
+
+       switch (cmd) {
+       case FPGA_PORT_RESET:
+               if (!arg)
+                       ret = fpga_port_reset(pdev);

fpga_port_reset() disables and reenables traffic on the port. Is

Quoted 8 lines

there ever a time when that would be unsafe to do? Like while DMA is happening? When I see a function called 'reset' exposed to userspace, I become concerned that hitting that reset at the wrong time could cause problems. We've discussed this some, but could you please remind me when userspace would need to reset the port? Please add documentation of what the intended use of this ioctl would be, when it is valid to request a reset from userspace and when userspace should never do that.

Sure, I will add some more description in uapi header file on this IOCTL.

Per my understanding, this API could be invoked at any time, even there is some DMA operation or PR operation at the same time. It should not cause any system level problem, but may cause functional failures ( e.g DMA or PR operation failure) and it should be recoverable.

Quoted 13 lines

The pcie code, the AFU file interface, and the bridge code all do port reset. This code is spread out over a few patches, but I'll comment here for now. I'm trying to keep track of everything that resets the port. The port gets reset in afu_probe, fme_probe, the AFU file release, and intel-pcie.c after parsing the features. Also the port is esentially reset after doing reprogramming, since that involves a
bridge disable/enable. In the v1 review, the issue raised that the
port functionality could be an expansion of fpga-bridge. If reset were added to the fpga_bridge_ops and a fpga_bridge_reset API added to fpga-bridge, then anything in the kernel that owns the bridge could reset it. That is of course assuming that this code doesn't need to reset the port before the fpga-bridge is created.

Actually in the pcie code, it needs to enable fpga port (put it out of reset, as port is in reset by default), otherwise the AFU MMIO region is not accessible. It's only a one time action.

For the AFU interface, it gives application a way to reset the AFU if anything goes wrong or application wants to make AFU back to default state.

For the bridge code, it just makes sure port is in reset during PR, which is requested by the PR flow of Intel FPGA device.

As you know, the fpga-bridge is created under FME module now, and port/AFU is another module which could be turned into a VF and assigned to a virtual machine, even we added a reset ops to fpga-bridge, we still need an interface to do port reset as fpga-bridges (and FME) are always in PF in host. :)

Thanks
Hao

Quoted 71 lines

Thanks,
Alan Tull

[1] http://www.kroah.com/log/blog/2013/06/26/how-to-create-a-sysfs-file-
correctly/

drivers/fpga/intel-afu-main.c static int afu_release(struct inode *inode, struct file

Quoted 19 lines
drivers/fpga/intel-afu-main.c +10 -0
+               else
+                       ret = -EINVAL;
+               break;
+       default:
+               dev_dbg(&pdev->dev, "%x cmd not handled", cmd);
+               ret = -ENODEV;
+       }
+
+       return ret;
 }
 
 struct feature_ops port_hdr_ops = {
        .init = port_hdr_init,
        .uinit = port_hdr_uinit,
+       .ioctl = port_hdr_ioctl,
 };
 
 static struct feature_driver port_feature_drvs[] = {
@@ -76,6 +117,7 @@ static int afu_release(struct inode *inode, struct file

*filp)

Quoted 44 lines
drivers/fpga/intel-afu-main.c +1 -0
        dev_dbg(&pdev->dev, "Device File Release\n");
 
+       fpga_port_reset(pdev);
        feature_dev_use_end(pdata);
        return 0;
 }
include/uapi/linux/intel-fpga.h +14 -1
index be295ae..be5f813 100644
@@ -30,8 +30,11 @@
 #define FPGA_MAGIC 0xB6
 
 #define FPGA_BASE 0
+#define PORT_BASE 0x40
 #define FME_BASE 0x80
 
+/* Common IOCTLs for both FME and AFU file descriptor */
+
 /**
  * FPGA_GET_API_VERSION - _IO(FPGA_MAGIC, FPGA_BASE + 0)
  *
@@ -50,6 +53,17 @@
 
 #define FPGA_CHECK_EXTENSION   _IO(FPGA_MAGIC, FPGA_BASE + 1)
 
+/* IOCTLs for AFU file descriptor */
+
+/**
+ * FPGA_PORT_RESET - _IO(FPGA_MAGIC, PORT_BASE + 0)
+ *
+ * Reset the FPGA AFU Port. No parameters are supported.
+ * Return: 0 on success, -errno of failure
+ */
+
+#define FPGA_PORT_RESET                _IO(FPGA_MAGIC, PORT_BASE + 0)
+
 /* IOCTLs for FME file descriptor */
 
 /**
--

1.8.3.1

#82 lore raw

Re: [PATCH v2 19/22] fpga: intel: afu: add header sub feature support

Quoted 174 lines · Hao
Quoted 9 lines · Wu Hao

Hi Hao,

Quoted 2 lines

The header register set is always present for the Port/AFU, it is mainly for capability, control and status of the ports that AFU connected to.

So just to be clear, the reset function is acting on the Port, not the AFU, right?

Hi Alan

AFU will be reset as well. Once port reset is issued, a compliant HW AFU will reset all its state and stop sending requests, and HW will set port reset ack bit when all outstanding requests initiated have been drained.

Will add some notes here.

Quoted 7 lines
Quoted 3 lines

This patch implements header sub feature support.

Please add a brief reminder here what the 'header' is. It's defined in patch 7 as being part of the feature list, but hardly mentioned when I grep intel-fpga.txt.

Sure, will adds some notes here.

Actually the header sub feature means the registers belong to the feature device (e.g port and FME), not any sub features (e.g PR, Power management).

Quoted 72 lines
Quoted 18 lines

Below user interfaces
are created by this patch.

Sysfs interface:
* /sys/class/fpga/<fpga.x>/<intel-fpga-port.x>/id
Read-only. Port ID.

Ioctl interface:
* FPGA_PORT_RESET
Reset the FPGA AFU Port.

Quoted 8 lines

---
v2: add sysfs documentation.

---
 .../ABI/testing/sysfs-platform-intel-fpga-afu      |  7 ++++
 drivers/fpga/intel-afu-main.c                      | 44 +++++++++++++++++++++-
 include/uapi/linux/intel-fpga.h                    | 14 +++++++
 3 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-platform-intel-fpga-

afu

Review

Quoted 2 lines
(quoted diff) +0 -0

b/Documentation/ABI/testing/sysfs-platform-intel-fpga-afu

Quoted 42 lines

new file mode 100644
index 0000000..8ad22c9

Documentation/ABI/testing/sysfs-platform-intel-fpga-afu new file +7 -0
@@ -0,0 +1,7 @@
+What:          /sys/bus/platform/devices/intel-fpga-port.0/id
+Date:          June 2017
+KernelVersion:  4.12
+Contact:       Wu Hao <[email protected]>
+Description:   Read-only. It returns id of this port. One Intel FPGA device
+               may have more than one port. Userspace could use this id to
+               distinguish different ports under same FPGA device.
drivers/fpga/intel-afu-main.c +15 -0
index 96d0367..2a17cde 100644
@@ -18,25 +18,66 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/intel-fpga.h>
 
 #include "intel-feature-dev.h"
 
+static ssize_t
+id_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       int id = fpga_port_id(to_platform_device(dev));
+
+       return scnprintf(buf, PAGE_SIZE, "%d\n", id);
+}
+static DEVICE_ATTR_RO(id);
+
+static const struct attribute *port_hdr_attrs[] = {
+       &dev_attr_id.attr,
+       NULL,
+};
+
 static int port_hdr_init(struct platform_device *pdev, struct feature *feature)
 {
        dev_dbg(&pdev->dev, "PORT HDR Init.\n");
 
Quoted 6 lines

drivers/fpga/intel-afu-main.c line 43

Quoted 2 lines
drivers/fpga/intel-afu-main.c +1 -1
-       return 0;
+       fpga_port_reset(pdev);

So the port will be reset here, which happens during fme_probe(). IIUC the PR region is empty then, there is just the static region, right?

port_hdr_init is invoked during afu_probe() function. The fpga_port_reset only resets the AFU's state and not empty the PR region. User doesn't need to program it again after port reset.

The purpose of this reset in port_hdr_init function, is to make sure that we could have a clean start whenever the port driver module is loaded. And similar as the one added in afu release.

Quoted 4 lines

drivers/fpga/intel-afu-main.c line 45

Quoted 3 lines
drivers/fpga/intel-afu-main.c +2 -0
+
+       return sysfs_create_files(&pdev->dev.kobj, port_hdr_attrs);
Quoted 8 lines

Greg wrote an article that there could be a race condition caused by creating sysfs files this late [1] and I see sysfs_create_files() used very sparingly in the kernel. I'm thinking that fpga-bridge should provide a place to create sysfs files earlier by adding an attribute_group to fpga_bridge_ops (same for fpga-mgr and fpga-region) and then fpga_bridge_register could do bridge->dev.groups = br_ops->groups. I'll put a patch for that out soon.

Hm... I understand there could be a race condition if creates sysfs files late. Actually the reasons I prefer to have each sub feature to create its own sysfs files are, 1) if any sub feature is not present, then related init function won't be invoked and related sysfs files won't be created at all. Then end user could know which sub features are present by checking these sysfs nodes easily.
2) Another point of view is about extension of each sub feature, for example, Some new registers introduced when sub feature's revision > n, so each sub feature's init function could check the sub feature's revision to decide which sysfs interfaces are needed. I think this should be a flexible way for extension.

Quoted 22 lines
Quoted 10 lines
drivers/fpga/intel-afu-main.c +4 -0
 }
 
 static void port_hdr_uinit(struct platform_device *pdev,
                                        struct feature *feature)
 {
        dev_dbg(&pdev->dev, "PORT HDR UInit.\n");
+
+       sysfs_remove_files(&pdev->dev.kobj, port_hdr_attrs);
+}
+

drivers/fpga/intel-afu-main.c line 65

Quoted 10 lines
drivers/fpga/intel-afu-main.c +10 -0
+static long
+port_hdr_ioctl(struct platform_device *pdev, struct feature *feature,
+                                       unsigned int cmd, unsigned long arg)
+{
+       long ret;
+
+       switch (cmd) {
+       case FPGA_PORT_RESET:
+               if (!arg)
+                       ret = fpga_port_reset(pdev);

fpga_port_reset() disables and reenables traffic on the port. Is

Quoted 8 lines

there ever a time when that would be unsafe to do? Like while DMA is happening? When I see a function called 'reset' exposed to userspace, I become concerned that hitting that reset at the wrong time could cause problems. We've discussed this some, but could you please remind me when userspace would need to reset the port? Please add documentation of what the intended use of this ioctl would be, when it is valid to request a reset from userspace and when userspace should never do that.

Quoted 6 lines

Sure, I will add some more description in uapi header file on this IOCTL.

Per my understanding, this API could be invoked at any time, even there is some DMA operation or PR operation at the same time. It should not cause any system level problem, but may cause functional failures ( e.g DMA or PR operation failure) and it should be recoverable.

OK that was my biggest concern here with the reset.

Quoted 23 lines
Quoted 14 lines

The pcie code, the AFU file interface, and the bridge code all do port reset. This code is spread out over a few patches, but I'll comment here for now. I'm trying to keep track of everything that resets the port. The port gets reset in afu_probe, fme_probe, the AFU file release, and intel-pcie.c after parsing the features. Also the port is esentially reset after doing reprogramming, since that involves a
bridge disable/enable. In the v1 review, the issue raised that the
port functionality could be an expansion of fpga-bridge. If reset were added to the fpga_bridge_ops and a fpga_bridge_reset API added to fpga-bridge, then anything in the kernel that owns the bridge could reset it. That is of course assuming that this code doesn't need to reset the port before the fpga-bridge is created.

Actually in the pcie code, it needs to enable fpga port (put it out of reset, as port is in reset by default), otherwise the AFU MMIO region is not accessible. It's only a one time action.

For the AFU interface, it gives application a way to reset the AFU if anything goes wrong or application wants to make AFU back to default state.

Quoted 8 lines

For the bridge code, it just makes sure port is in reset during PR, which is requested by the PR flow of Intel FPGA device.

As you know, the fpga-bridge is created under FME module now, and port/AFU is another module which could be turned into a VF and assigned to a virtual machine, even we added a reset ops to fpga-bridge, we still need an interface to do port reset as fpga-bridges (and FME) are always in PF in host. :)

OK

Alan

Quoted 75 lines

Thanks
Hao

Quoted 71 lines

Thanks,
Alan Tull

[1] http://www.kroah.com/log/blog/2013/06/26/how-to-create-a-sysfs-file-
correctly/

drivers/fpga/intel-afu-main.c static int afu_release(struct inode *inode, struct file

Quoted 19 lines
drivers/fpga/intel-afu-main.c +10 -0
+               else
+                       ret = -EINVAL;
+               break;
+       default:
+               dev_dbg(&pdev->dev, "%x cmd not handled", cmd);
+               ret = -ENODEV;
+       }
+
+       return ret;
 }
 
 struct feature_ops port_hdr_ops = {
        .init = port_hdr_init,
        .uinit = port_hdr_uinit,
+       .ioctl = port_hdr_ioctl,
 };
 
 static struct feature_driver port_feature_drvs[] = {
@@ -76,6 +117,7 @@ static int afu_release(struct inode *inode, struct file

*filp)

Quoted 44 lines
drivers/fpga/intel-afu-main.c +1 -0
        dev_dbg(&pdev->dev, "Device File Release\n");
 
+       fpga_port_reset(pdev);
        feature_dev_use_end(pdata);
        return 0;
 }
include/uapi/linux/intel-fpga.h +14 -1
index be295ae..be5f813 100644
@@ -30,8 +30,11 @@
 #define FPGA_MAGIC 0xB6
 
 #define FPGA_BASE 0
+#define PORT_BASE 0x40
 #define FME_BASE 0x80
 
+/* Common IOCTLs for both FME and AFU file descriptor */
+
 /**
  * FPGA_GET_API_VERSION - _IO(FPGA_MAGIC, FPGA_BASE + 0)
  *
@@ -50,6 +53,17 @@
 
 #define FPGA_CHECK_EXTENSION   _IO(FPGA_MAGIC, FPGA_BASE + 1)
 
+/* IOCTLs for AFU file descriptor */
+
+/**
+ * FPGA_PORT_RESET - _IO(FPGA_MAGIC, PORT_BASE + 0)
+ *
+ * Reset the FPGA AFU Port. No parameters are supported.
+ * Return: 0 on success, -errno of failure
+ */
+
+#define FPGA_PORT_RESET                _IO(FPGA_MAGIC, PORT_BASE + 0)
+
 /* IOCTLs for FME file descriptor */
 
 /**
--

1.8.3.1

author
#83 lore raw

PATCH v2 20/22 fpga: intel: afu add FPGA_GET_API_VERSION/CHECK_EXTENSION ioctls support

In reply to Wu Hao

FPGA_GET_API_VERSION and FPGA_CHECK_EXTENSION ioctls are common ones which need to be supported by all feature devices drivers including FME and AFU. This patch implements above 2 ioctls in Intel FPGA Accelerated Function Unit (AFU) driver.

---
v2: rebased

---
 drivers/fpga/intel-afu-main.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/fpga/intel-afu-main.c +11 -0
index 2a17cde..22f77f2 100644
@@ -122,6 +122,13 @@ static int afu_release(struct inode *inode, struct file *filp)
 	return 0;
 }
 
+static long afu_ioctl_check_extension(struct feature_platform_data *pdata,
+				     unsigned long arg)
+{
+	/* No extension support for now */
+	return 0;
+}
+
 static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
 	struct platform_device *pdev = filp->private_data;
@@ -132,6 +139,10 @@ static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
 
 	switch (cmd) {
+	case FPGA_GET_API_VERSION:
+		return FPGA_API_VERSION;
+	case FPGA_CHECK_EXTENSION:
+		return afu_ioctl_check_extension(pdata, arg);
 	default:
 		/*
 		 * Let sub-feature's ioctl function to handle the cmd
Signature · 1 line
1.8.3.1
Acked-by on v2 20/22
#84 lore raw

Re: [PATCH v2 20/22] fpga: intel: afu add FPGA_GET_API_VERSION/CHECK_EXTENSION ioctls support

Quoted 11 lines · Wu Hao

FPGA_GET_API_VERSION and FPGA_CHECK_EXTENSION ioctls are common ones which need to be supported by all feature devices drivers including FME and AFU. This patch implements above 2 ioctls in Intel FPGA Accelerated Function Unit (AFU) driver.

Quoted 38 lines

---
v2: rebased

---
 drivers/fpga/intel-afu-main.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/fpga/intel-afu-main.c +11 -1
index 2a17cde..22f77f2 100644
@@ -122,6 +122,13 @@ static int afu_release(struct inode *inode, struct file *filp)
        return 0;
 }
 
+static long afu_ioctl_check_extension(struct feature_platform_data *pdata,
+                                    unsigned long arg)
+{
+       /* No extension support for now */
+       return 0;
+}
+
 static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
        struct platform_device *pdev = filp->private_data;
@@ -132,6 +139,10 @@ static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
        dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
 
        switch (cmd) {
+       case FPGA_GET_API_VERSION:
+               return FPGA_API_VERSION;
+       case FPGA_CHECK_EXTENSION:
+               return afu_ioctl_check_extension(pdata, arg);
        default:
                /*
                 * Let sub-feature's ioctl function to handle the cmd
--

1.8.3.1

#85 lore raw

Re: [PATCH v2 20/22] fpga: intel: afu add FPGA_GET_API_VERSION/CHECK_EXTENSION ioctls support

In reply to Wu Hao

Hi,

Quoted 22 lines · Wu Hao

FPGA_GET_API_VERSION and FPGA_CHECK_EXTENSION ioctls are common ones which need to be supported by all feature devices drivers including FME and AFU. This patch implements above 2 ioctls in Intel FPGA Accelerated Function Unit (AFU) driver.

---
v2: rebased

---
 drivers/fpga/intel-afu-main.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/fpga/intel-afu-main.c +0 -0
index 2a17cde..22f77f2 100644

drivers/fpga/intel-afu-main.c static int afu_release(struct inode *inode, struct file *filp) · line 128

Quoted 7 lines
drivers/fpga/intel-afu-main.c +4 -0
@@ -122,6 +122,13 @@ static int afu_release(struct inode *inode, struct file *filp)
        return 0;
 }
 
+static long afu_ioctl_check_extension(struct feature_platform_data *pdata,
+                                    unsigned long arg)
+{
+       /* No extension support for now */

Do you maybe make that an -ENOTSUPP or -EINVAL then?

Quoted 20 lines
drivers/fpga/intel-afu-main.c +7 -1
+       return 0;
+}
+
 static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
        struct platform_device *pdev = filp->private_data;
@@ -132,6 +139,10 @@ static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
        dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
 
        switch (cmd) {
+       case FPGA_GET_API_VERSION:
+               return FPGA_API_VERSION;
+       case FPGA_CHECK_EXTENSION:
+               return afu_ioctl_check_extension(pdata, arg);
        default:
                /*
                 * Let sub-feature's ioctl function to handle the cmd
--

1.8.3.1

Quoted 4 lines

--
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

Thanks,

Moritz

author
#86 lore raw

Re: [PATCH v2 20/22] fpga: intel: afu add FPGA_GET_API_VERSION/CHECK_EXTENSION ioctls support

Quoted 28 lines · Moritz Fischer

Hi,

Quoted 25 lines · Wu Hao

FPGA_GET_API_VERSION and FPGA_CHECK_EXTENSION ioctls are common ones which need to be supported by all feature devices drivers including FME and AFU. This patch implements above 2 ioctls in Intel FPGA Accelerated Function Unit (AFU) driver.

---
v2: rebased

---
 drivers/fpga/intel-afu-main.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/fpga/intel-afu-main.c +0 -0
index 2a17cde..22f77f2 100644
@@ -122,6 +122,13 @@ static int afu_release(struct inode *inode, struct file *filp)
        return 0;
 }
 
Quoted 6 lines

drivers/fpga/intel-afu-main.c static int afu_release(struct inode *inode, struct file *filp) · line 128

Quoted 4 lines
drivers/fpga/intel-afu-main.c +4 -0
+static long afu_ioctl_check_extension(struct feature_platform_data *pdata,
+                                    unsigned long arg)
+{
+       /* No extension support for now */

Do you maybe make that an -ENOTSUPP or -EINVAL then?

Hi Moritz

User will use this ioctl to check if extension X is supported. It always returns 0 means any extension indicated by the arg, is not supported.

I think we don't have to return an -ENOTSUPP or -EINVAL here, which may confuse the enduser. : )

Thanks
Hao

Quoted 28 lines
Quoted 20 lines
drivers/fpga/intel-afu-main.c +7 -1
+       return 0;
+}
+
 static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
        struct platform_device *pdev = filp->private_data;
@@ -132,6 +139,10 @@ static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
        dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
 
        switch (cmd) {
+       case FPGA_GET_API_VERSION:
+               return FPGA_API_VERSION;
+       case FPGA_CHECK_EXTENSION:
+               return afu_ioctl_check_extension(pdata, arg);
        default:
                /*
                 * Let sub-feature's ioctl function to handle the cmd
--

1.8.3.1

Quoted 4 lines

--
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

Thanks,

Moritz

author
#87 lore raw

PATCH v2 21/22 fpga: intel: afu: add user afu sub feature support

In reply to Wu Hao

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

User Accelerated Function Unit sub feature exposes the MMIO region of the AFU. After valid green bitstream (GBS) is programmed and port is enabled, then this MMIO region could be accessed.

This patch adds support to enumerate the AFU MMIO region and expose it to userspace via mmap file operation. Below interfaces are exposed to user:

Sysfs interface:
* /sys/class/fpga/<fpga.x>/<intel-fpga-port.x>/afu_id Read-only. Indicate which green bitstream is programmed to this AFU.

Ioctl interfaces:
* FPGA_PORT_GET_INFO
Provide info to userspace on the number of supported region. Only UAFU region is supported now.

* FPGA_PORT_GET_REGION_INFO
Provide region information, including access permission, region size, offset from the start of device fd.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
add sysfs documentation.
switched to GPLv2 license.

---
 .../ABI/testing/sysfs-platform-intel-fpga-afu      |   9 +
 drivers/fpga/Makefile                              |   2 +-
 drivers/fpga/intel-afu-main.c                      | 204 ++++++++++++++++++++-
 drivers/fpga/intel-afu-region.c                    | 127 +++++++++++++
 drivers/fpga/intel-afu.h                           |  52 ++++++
 include/uapi/linux/intel-fpga.h                    |  47 +++++
 6 files changed, 437 insertions(+), 4 deletions(-)
 create mode 100644 drivers/fpga/intel-afu-region.c
 create mode 100644 drivers/fpga/intel-afu.h
Documentation/ABI/testing/sysfs-platform-intel-fpga-afu +9 -0
index 8ad22c9..6cd8781 100644
@@ -5,3 +5,12 @@ Contact:	Wu Hao <[email protected]>
 Description:	Read-only. It returns id of this port. One Intel FPGA device
 		may have more than one port. Userspace could use this id to
 		distinguish different ports under same FPGA device.
+
+What:		/sys/bus/platform/devices/intel-fpga-port.0/afu_id
+Date:		June 2017
+KernelVersion:  4.12
+Contact:	Wu Hao <[email protected]>
+Description:	Read-only. User can program different green bitstreams (GBS) to
+		FPGA Accelerator Function Unit (AFU) for different functions.
+		It returns uuid which could be used to identify which GBS is
+		programmed in this AFU.
drivers/fpga/Makefile +1 -1
index ce08833..45c0538 100644
@@ -38,4 +38,4 @@ obj-$(CONFIG_INTEL_FPGA_AFU)		+= intel-fpga-afu.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
-intel-fpga-afu-objs := intel-afu-main.o
+intel-fpga-afu-objs := intel-afu-main.o intel-afu-region.o
drivers/fpga/intel-afu-main.c +201 -3

267 lines not on this page · raw message

drivers/fpga/intel-afu-region.c new file +127 -0

127 lines not on this page · raw message

drivers/fpga/intel-afu.h new file +52 -0

52 lines not on this page · raw message

include/uapi/linux/intel-fpga.h +47 -0

53 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author
#88 lore raw

PATCH v2 22/22 fpga: intel: afu: add FPGA_PORT_DMA_MAP/UNMAP ioctls support

In reply to Wu Hao

DMA memory regions are required for Accelerated Function Unit (AFU) usage. These two ioctls allow user space applications to map user memory regions for dma, and unmap them after use. Iova is returned from driver to user space application via FPGA_PORT_DMA_MAP ioctl. Application needs to unmap it after use, otherwise, driver will unmap them in device file release operation.

All the mapped regions are managed via a rb tree.

Ioctl interfaces:
* FPGA_PORT_DMA_MAP
Do the dma mapping per user_addr and length which provided by user. Return iova in provided struct afu_port_dma_map.

* FPGA_PORT_DMA_UNMAP
Unmap the dma region per iova provided by user.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed kbuild warnings.

---
 drivers/fpga/Makefile               |   3 +-
 drivers/fpga/intel-afu-dma-region.c | 372 ++++++++++++++++++++++++++++++++++++
 drivers/fpga/intel-afu-main.c       |  61 +++++-
 drivers/fpga/intel-afu.h            |  18 ++
 include/uapi/linux/intel-fpga.h     |  37 ++++
 5 files changed, 489 insertions(+), 2 deletions(-)
 create mode 100644 drivers/fpga/intel-afu-dma-region.c
drivers/fpga/Makefile +2 -1
index 45c0538..339d1f3 100644
@@ -38,4 +38,5 @@ obj-$(CONFIG_INTEL_FPGA_AFU)		+= intel-fpga-afu.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
-intel-fpga-afu-objs := intel-afu-main.o intel-afu-region.o
+intel-fpga-afu-objs := intel-afu-main.o intel-afu-region.o \
+		       intel-afu-dma-region.o
drivers/fpga/intel-afu-dma-region.c new file +372 -0

372 lines not on this page · raw message

drivers/fpga/intel-afu-main.c +60 -1

91 lines not on this page · raw message

drivers/fpga/intel-afu.h +18 -0
index 3417780d..23f7e24 100644
@@ -30,11 +30,21 @@ struct fpga_afu_region {
 	struct list_head node;
 };
 
+struct fpga_afu_dma_region {
+	u64 user_addr;
+	u64 length;
+	u64 iova;
+	struct page **pages;
+	struct rb_node node;
+	bool in_use;
+};
+
 struct fpga_afu {
 	u64 region_cur_offset;
 	int num_regions;
 	u8 num_umsgs;
 	struct list_head regions;
+	struct rb_root dma_regions;
 
 	struct feature_platform_data *pdata;
 };
@@ -49,4 +59,12 @@ int afu_get_region_by_offset(struct feature_platform_data *pdata,
 			    u64 offset, u64 size,
 			    struct fpga_afu_region *pregion);
 
+void afu_dma_region_init(struct feature_platform_data *pdata);
+void afu_dma_region_destroy(struct feature_platform_data *pdata);
+long afu_dma_map_region(struct feature_platform_data *pdata,
+		       u64 user_addr, u64 length, u64 *iova);
+long afu_dma_unmap_region(struct feature_platform_data *pdata, u64 iova);
+struct fpga_afu_dma_region *afu_dma_region_find(
+		struct feature_platform_data *pdata, u64 iova, u64 size);
+
 #endif
include/uapi/linux/intel-fpga.h +37 -0
index a2ad332..b97ea02 100644
@@ -111,6 +111,43 @@ struct fpga_port_region_info {
 
 #define FPGA_PORT_GET_REGION_INFO	_IO(FPGA_MAGIC, PORT_BASE + 2)
 
+/**
+ * FPGA_PORT_DMA_MAP - _IOWR(FPGA_MAGIC, PORT_BASE + 3,
+ *						struct fpga_port_dma_map)
+ *
+ * Map the dma memory per user_addr and length which are provided by caller.
+ * Driver fills the iova in provided struct afu_port_dma_map.
+ * This interface only accepts page-size aligned user memory for dma mapping.
+ * Return: 0 on success, -errno on failure.
+ */
+struct fpga_port_dma_map {
+	/* Input */
+	__u32 argsz;		/* Structure length */
+	__u32 flags;		/* Zero for now */
+	__u64 user_addr;        /* Process virtual address */
+	__u64 length;           /* Length of mapping (bytes)*/
+	/* Output */
+	__u64 iova;             /* IO virtual address */
+};
+
+#define FPGA_PORT_DMA_MAP	_IO(FPGA_MAGIC, PORT_BASE + 3)
+
+/**
+ * FPGA_PORT_DMA_UNMAP - _IOW(FPGA_MAGIC, PORT_BASE + 4,
+ *						struct fpga_port_dma_unmap)
+ *
+ * Unmap the dma memory per iova provided by caller.
+ * Return: 0 on success, -errno on failure.
+ */
+struct fpga_port_dma_unmap {
+	/* Input */
+	__u32 argsz;		/* Structure length */
+	__u32 flags;		/* Zero for now */
+	__u64 iova;		/* IO virtual address */
+};
+
+#define FPGA_PORT_DMA_UNMAP	_IO(FPGA_MAGIC, PORT_BASE + 4)
+
 /* IOCTLs for FME file descriptor */
 
 /**
Signature · 1 line
1.8.3.1
#89 lore raw

Re: [PATCH v2 22/22] fpga: intel: afu: add FPGA_PORT_DMA_MAP/UNMAP ioctls support

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

Hi Hao,

Please run checkpatch.pl --strict on this.

Could you add some kernel-doc function comments here to help the new user (or reviewer) get a better handle on what this code is doing?

A few mostly minor comments below.

Quoted 8 lines

DMA memory regions are required for Accelerated Function Unit (AFU) usage. These two ioctls allow user space applications to map user memory regions for dma, and unmap them after use. Iova is returned from driver to user space application via FPGA_PORT_DMA_MAP ioctl. Application needs to unmap it after use, otherwise, driver will unmap them in device file release operation.

All the mapped regions are managed via a rb tree.

Please note that each afu has its own rb tree (this comment sounds like there's one rb tree for all) to keep track of its DMA regions.

Quoted 90 lines

Ioctl interfaces:
* FPGA_PORT_DMA_MAP
Do the dma mapping per user_addr and length which provided by user. Return iova in provided struct afu_port_dma_map.

* FPGA_PORT_DMA_UNMAP
Unmap the dma region per iova provided by user.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed kbuild warnings.

---
 drivers/fpga/Makefile               |   3 +-
 drivers/fpga/intel-afu-dma-region.c | 372 ++++++++++++++++++++++++++++++++++++
 drivers/fpga/intel-afu-main.c       |  61 +++++-
 drivers/fpga/intel-afu.h            |  18 ++
 include/uapi/linux/intel-fpga.h     |  37 ++++
 5 files changed, 489 insertions(+), 2 deletions(-)
 create mode 100644 drivers/fpga/intel-afu-dma-region.c
drivers/fpga/Makefile +2 -1
index 45c0538..339d1f3 100644
@@ -38,4 +38,5 @@ obj-$(CONFIG_INTEL_FPGA_AFU)          += intel-fpga-afu.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
-intel-fpga-afu-objs := intel-afu-main.o intel-afu-region.o
+intel-fpga-afu-objs := intel-afu-main.o intel-afu-region.o \
+                      intel-afu-dma-region.o
drivers/fpga/intel-afu-dma-region.c new file +45 -0
new file mode 100644
index 0000000..982a9b5
@@ -0,0 +1,372 @@
+/*
+ * Driver for Intel FPGA Accelerated Function Unit (AFU) DMA Region Management
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   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 <linux/sched/signal.h>
+#include <linux/uaccess.h>
+
+#include "intel-afu.h"
+
+static void put_all_pages(struct page **pages, int npages)
+{
+       int i;
+
+       for (i = 0; i < npages; i++)
+               if (pages[i] != NULL)
+                       put_page(pages[i]);
+}
+
+void afu_dma_region_init(struct feature_platform_data *pdata)
+{
+       struct fpga_afu *afu = fpga_pdata_get_private(pdata);
+
+       afu->dma_regions = RB_ROOT;
+}
+
+static long afu_dma_adjust_locked_vm(struct device *dev, long npages, bool incr)
+{
+       unsigned long locked, lock_limit;
+       int ret = 0;
+
+       /* the task is exiting. */
+       if (!current->mm)
+               return 0;
+
+       down_write(&current->mm->mmap_sem);
+

drivers/fpga/intel-afu-dma-region.c line 55

Quoted 10 lines
drivers/fpga/intel-afu-dma-region.c new file +10 -0
+       if (incr) {
+               locked = current->mm->locked_vm + npages;
+               lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
+
+               if (locked > lock_limit && !capable(CAP_IPC_LOCK))
+                       ret = -ENOMEM;
+               else
+                       current->mm->locked_vm += npages;
+       } else {
+

Don't need to skip a line here.

Quoted 9 lines
drivers/fpga/intel-afu-dma-region.c new file +9 -0
+               if (WARN_ON_ONCE(npages > current->mm->locked_vm))
+                       npages = current->mm->locked_vm;
+               current->mm->locked_vm -= npages;
+       }
+
+       dev_dbg(dev, "[%d] RLIMIT_MEMLOCK %c%ld %ld/%ld%s\n", current->pid,
+                               incr ? '+' : '-',
+                               npages << PAGE_SHIFT,
+                               current->mm->locked_vm << PAGE_SHIFT,

drivers/fpga/intel-afu-dma-region.c line 74

Quoted 10 lines
drivers/fpga/intel-afu-dma-region.c new file +10 -0
+                               rlimit(RLIMIT_MEMLOCK),
+                               ret ? "- execeeded" : "");
+
+       up_write(&current->mm->mmap_sem);
+
+       return ret;
+}
+
+static long afu_dma_pin_pages(struct feature_platform_data *pdata,
+                               struct fpga_afu_dma_region *region)

Conventionally, return type is usually int in kernel functions that return 0 or error code.

Quoted 9 lines
drivers/fpga/intel-afu-dma-region.c new file +9 -0
+{
+       long npages = region->length >> PAGE_SHIFT;
+       struct device *dev = &pdata->dev->dev;
+       long ret, pinned;
+
+       ret = afu_dma_adjust_locked_vm(dev, npages, true);
+       if (ret)
+               return ret;
+

drivers/fpga/intel-afu-dma-region.c line 93

Quoted 10 lines
drivers/fpga/intel-afu-dma-region.c new file +10 -0
+       region->pages = kcalloc(npages, sizeof(struct page *), GFP_KERNEL);
+       if (!region->pages) {
+               afu_dma_adjust_locked_vm(dev, npages, false);
+               return -ENOMEM;
+       }
+
+       pinned = get_user_pages_fast(region->user_addr, npages, 1,
+                                       region->pages);
+       if (pinned < 0) {
+               ret = pinned;

This return value gets all the way to be returned by the ioctl and isn't used for its value as the number that actually got pinned by the caller.

Quoted 36 lines
drivers/fpga/intel-afu-dma-region.c new file +36 -0
+               goto err_put_pages;
+       } else if (pinned != npages) {
+               ret = -EFAULT;
+               goto err;
+       }
+
+       dev_dbg(dev, "%ld pages pinned\n", pinned);
+
+       return 0;
+
+err_put_pages:
+       put_all_pages(region->pages, pinned);
+err:
+       kfree(region->pages);
+       afu_dma_adjust_locked_vm(dev, npages, false);
+       return ret;
+}
+
+static void afu_dma_unpin_pages(struct feature_platform_data *pdata,
+                               struct fpga_afu_dma_region *region)
+{
+       long npages = region->length >> PAGE_SHIFT;
+       struct device *dev = &pdata->dev->dev;
+
+       put_all_pages(region->pages, npages);
+       kfree(region->pages);
+       afu_dma_adjust_locked_vm(dev, npages, false);
+
+       dev_dbg(dev, "%ld pages unpinned\n", npages);
+}
+
+static bool afu_dma_check_continuous_pages(struct fpga_afu_dma_region *region)
+{
+       int npages = region->length >> PAGE_SHIFT;
+       int i;
+

drivers/fpga/intel-afu-dma-region.c line 139

Quoted 10 lines
drivers/fpga/intel-afu-dma-region.c new file +10 -0
+       for (i = 0; i < npages - 1; i++)
+               if (page_to_pfn(region->pages[i]) + 1 !=
+                                       page_to_pfn(region->pages[i+1]))
+                       return false;
+
+       return true;
+}
+
+static bool dma_region_check_iova(struct fpga_afu_dma_region *region,
+                                 u64 iova, u64 size)

This function checks that the area defined by iova and size is fully contained in the region, right? A comment would help.

Quoted 254 lines
drivers/fpga/intel-afu-dma-region.c new file +233 -0
+{
+       if (!size && region->iova != iova)
+               return false;
+
+       return (region->iova <= iova) &&
+               (region->length + region->iova >= iova + size);
+}
+
+/* Need to be called with pdata->lock held */
+static int afu_dma_region_add(struct feature_platform_data *pdata,
+                                       struct fpga_afu_dma_region *region)
+{
+       struct fpga_afu *afu = fpga_pdata_get_private(pdata);
+       struct rb_node **new, *parent = NULL;
+
+       dev_dbg(&pdata->dev->dev, "add region (iova = %llx)\n",
+                                       (unsigned long long)region->iova);
+
+       new = &(afu->dma_regions.rb_node);
+
+       while (*new) {
+               struct fpga_afu_dma_region *this;
+
+               this = container_of(*new, struct fpga_afu_dma_region, node);
+
+               parent = *new;
+
+               if (dma_region_check_iova(this, region->iova, region->length))
+                       return -EEXIST;
+
+               if (region->iova < this->iova)
+                       new = &((*new)->rb_left);
+               else if (region->iova > this->iova)
+                       new = &((*new)->rb_right);
+               else
+                       return -EEXIST;
+       }
+
+       rb_link_node(&region->node, parent, new);
+       rb_insert_color(&region->node, &afu->dma_regions);
+
+       return 0;
+}
+
+/* Need to be called with pdata->lock held */
+static void afu_dma_region_remove(struct feature_platform_data *pdata,
+                                       struct fpga_afu_dma_region *region)
+{
+       struct fpga_afu *afu;
+
+       dev_dbg(&pdata->dev->dev, "del region (iova = %llx)\n",
+                                       (unsigned long long)region->iova);
+
+       afu = fpga_pdata_get_private(pdata);
+       rb_erase(&region->node, &afu->dma_regions);
+}
+
+/* Need to be called with pdata->lock held */
+void afu_dma_region_destroy(struct feature_platform_data *pdata)
+{
+       struct fpga_afu *afu = fpga_pdata_get_private(pdata);
+       struct rb_node *node = rb_first(&afu->dma_regions);
+       struct fpga_afu_dma_region *region;
+
+       while (node) {
+               region = container_of(node, struct fpga_afu_dma_region, node);
+
+               dev_dbg(&pdata->dev->dev, "del region (iova = %llx)\n",
+                                       (unsigned long long)region->iova);
+
+               rb_erase(node, &afu->dma_regions);
+
+               if (region->iova)
+                       dma_unmap_page(fpga_pdata_to_pcidev(pdata),
+                                       region->iova, region->length,
+                                       DMA_BIDIRECTIONAL);
+
+               if (region->pages)
+                       afu_dma_unpin_pages(pdata, region);
+
+               node = rb_next(node);
+               kfree(region);
+       }
+}
+
+/*
+ * It finds the dma region from the rbtree based on @iova and @size:
+ * - if @size == 0, it finds the dma region which starts from @iova
+ * - otherwise, it finds the dma region which fully contains
+ *   [@iova, @iova+size)
+ * If nothing is matched returns NULL.
+ *
+ * Need to be called with pdata->lock held.
+ */
+struct fpga_afu_dma_region *
+afu_dma_region_find(struct feature_platform_data *pdata, u64 iova, u64 size)
+{
+       struct fpga_afu *afu = fpga_pdata_get_private(pdata);
+       struct rb_node *node = afu->dma_regions.rb_node;
+       struct device *dev = &pdata->dev->dev;
+
+       while (node) {
+               struct fpga_afu_dma_region *region;
+
+               region = container_of(node, struct fpga_afu_dma_region, node);
+
+               if (dma_region_check_iova(region, iova, size)) {
+                       dev_dbg(dev, "find region (iova = %llx)\n",
+                               (unsigned long long)region->iova);
+                       return region;
+               }
+
+               if (iova < region->iova)
+                       node = node->rb_left;
+               else if (iova > region->iova)
+                       node = node->rb_right;
+               else
+                       /* the iova region is not fully covered. */
+                       break;
+       }
+
+       dev_dbg(dev, "region with iova %llx and size %llx is not found\n",
+               (unsigned long long)iova, (unsigned long long)size);
+       return NULL;
+}
+
+static struct fpga_afu_dma_region *
+afu_dma_region_find_iova(struct feature_platform_data *pdata, u64 iova)
+{
+       return afu_dma_region_find(pdata, iova, 0);
+}
+
+long afu_dma_map_region(struct feature_platform_data *pdata,
+                      u64 user_addr, u64 length, u64 *iova)
+{
+       struct fpga_afu_dma_region *region;
+       int ret;
+
+       /*
+        * Check Inputs, only accept page-aligned user memory region with
+        * valid length.
+        */
+       if (!PAGE_ALIGNED(user_addr) || !PAGE_ALIGNED(length) || !length)
+               return -EINVAL;
+
+       /* Check overflow */
+       if (user_addr + length < user_addr)
+               return -EINVAL;
+
+       if (!access_ok(VERIFY_WRITE, (void __user *)(unsigned long)user_addr,
+                      length))
+               return -EINVAL;
+
+       region = kzalloc(sizeof(*region), GFP_KERNEL);
+       if (!region)
+               return -ENOMEM;
+
+       region->user_addr = user_addr;
+       region->length = length;
+
+       /* Pin the user memory region */
+       ret = afu_dma_pin_pages(pdata, region);
+       if (ret) {
+               dev_err(&pdata->dev->dev, "fail to pin memory region\n");
+               goto free_region;
+       }
+
+       /* Only accept continuous pages, return error if no */
+       if (!afu_dma_check_continuous_pages(region)) {
+               dev_err(&pdata->dev->dev, "pages are not continuous\n");
+               ret = -EINVAL;
+               goto unpin_pages;
+       }
+
+       /* As pages are continuous then start to do DMA mapping */
+       region->iova = dma_map_page(fpga_pdata_to_pcidev(pdata),
+                                   region->pages[0], 0,
+                                   region->length,
+                                   DMA_BIDIRECTIONAL);
+       if (dma_mapping_error(&pdata->dev->dev, region->iova)) {
+               dev_err(&pdata->dev->dev, "fail to map dma mapping\n");
+               ret = -EFAULT;
+               goto unpin_pages;
+       }
+
+       *iova = region->iova;
+
+       mutex_lock(&pdata->lock);
+       ret = afu_dma_region_add(pdata, region);
+       mutex_unlock(&pdata->lock);
+       if (ret) {
+               dev_err(&pdata->dev->dev, "fail to add dma region\n");
+               goto unmap_dma;
+       }
+
+       return 0;
+
+unmap_dma:
+       dma_unmap_page(fpga_pdata_to_pcidev(pdata),
+                      region->iova, region->length, DMA_BIDIRECTIONAL);
+unpin_pages:
+       afu_dma_unpin_pages(pdata, region);
+free_region:
+       kfree(region);
+       return ret;
+}
+
+long afu_dma_unmap_region(struct feature_platform_data *pdata, u64 iova)
+{
+       struct fpga_afu_dma_region *region;
+
+       mutex_lock(&pdata->lock);
+       region = afu_dma_region_find_iova(pdata, iova);
+       if (!region) {
+               mutex_unlock(&pdata->lock);
+               return -EINVAL;
+       }
+
+       if (region->in_use) {
+               mutex_unlock(&pdata->lock);
+               return -EBUSY;
+       }
+
+       afu_dma_region_remove(pdata, region);
+       mutex_unlock(&pdata->lock);
+
+       dma_unmap_page(fpga_pdata_to_pcidev(pdata),
+                      region->iova, region->length, DMA_BIDIRECTIONAL);
+       afu_dma_unpin_pages(pdata, region);
+       kfree(region);
+
+       return 0;
+}
drivers/fpga/intel-afu-main.c +5 -1
index 8c7aa70..d9f1ebf 100644
@@ -175,7 +175,11 @@ static int afu_release(struct inode *inode, struct file *filp)
 
        dev_dbg(&pdev->dev, "Device File Release\n");
 
-       fpga_port_reset(pdev);
+       mutex_lock(&pdata->lock);
+       __fpga_port_reset(pdev);
+       afu_dma_region_destroy(pdata);
+       mutex_unlock(&pdata->lock);
+
        feature_dev_use_end(pdata);
        return 0;
 }
@@ -245,6 +249,55 @@ static long afu_ioctl_check_extension(struct feature_platform_data *pdata,
        return 0;
 }
 

drivers/fpga/intel-afu-main.c static long afu_ioctl_check_extension(struct feature_platform_data *pdata, · line 261

Quoted 10 lines
drivers/fpga/intel-afu-main.c +10 -0
+static long
+afu_ioctl_dma_map(struct feature_platform_data *pdata, void __user *arg)
+{
+       struct fpga_port_dma_map map;
+       unsigned long minsz;
+       long ret;
+
+       minsz = offsetofend(struct fpga_port_dma_map, iova);
+
+       if (copy_from_user(&map, arg, minsz))

Why are you using offsetofend() instead of sizeof(map)? Is this struct going to expand beyond iova? Also below in one place.

Quoted 19 lines
drivers/fpga/intel-afu-main.c +19 -0
+               return -EFAULT;
+
+       if (map.argsz < minsz || map.flags)
+               return -EINVAL;
+
+       ret = afu_dma_map_region(pdata, map.user_addr, map.length, &map.iova);
+       if (ret)
+               return ret;
+
+       if (copy_to_user(arg, &map, sizeof(map))) {
+               afu_dma_unmap_region(pdata, map.iova);
+               return -EFAULT;
+       }
+
+       dev_dbg(&pdata->dev->dev, "dma map: ua=%llx, len=%llx, iova=%llx\n",
+                               (unsigned long long)map.user_addr,
+                               (unsigned long long)map.length,
+                               (unsigned long long)map.iova);
+

drivers/fpga/intel-afu-main.c static long afu_ioctl_check_extension(struct feature_platform_data *pdata, · line 290

Quoted 10 lines
drivers/fpga/intel-afu-main.c +10 -0
+       return 0;
+}
+
+static long
+afu_ioctl_dma_unmap(struct feature_platform_data *pdata, void __user *arg)
+{
+       struct fpga_port_dma_unmap unmap;
+       unsigned long minsz;
+
+       minsz = offsetofend(struct fpga_port_dma_unmap, iova);

Here as well.

Quoted 130 lines
drivers/fpga/intel-afu-main.c +16 -0
+
+       if (copy_from_user(&unmap, arg, minsz))
+               return -EFAULT;
+
+       if (unmap.argsz < minsz || unmap.flags)
+               return -EINVAL;
+
+       return afu_dma_unmap_region(pdata, unmap.iova);
+}
+
 static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
        struct platform_device *pdev = filp->private_data;
@@ -263,6 +316,10 @@ static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
                return afu_ioctl_get_info(pdata, (void __user *)arg);
        case FPGA_PORT_GET_REGION_INFO:
                return afu_ioctl_get_region_info(pdata, (void __user *)arg);
+       case FPGA_PORT_DMA_MAP:
+               return afu_ioctl_dma_map(pdata, (void __user *)arg);
+       case FPGA_PORT_DMA_UNMAP:
+               return afu_ioctl_dma_unmap(pdata, (void __user *)arg);
        default:
                /*
                 * Let sub-feature's ioctl function to handle the cmd
@@ -337,6 +394,7 @@ static int afu_dev_init(struct platform_device *pdev)
        mutex_lock(&pdata->lock);
        fpga_pdata_set_private(pdata, afu);
        afu_region_init(pdata);
+       afu_dma_region_init(pdata);
        mutex_unlock(&pdata->lock);
        return 0;
 }
@@ -349,6 +407,7 @@ static int afu_dev_destroy(struct platform_device *pdev)
        mutex_lock(&pdata->lock);
        afu = fpga_pdata_get_private(pdata);
        afu_region_destroy(pdata);
+       afu_dma_region_destroy(pdata);
        fpga_pdata_set_private(pdata, NULL);
        mutex_unlock(&pdata->lock);
 
drivers/fpga/intel-afu.h +18 -0
index 3417780d..23f7e24 100644
@@ -30,11 +30,21 @@ struct fpga_afu_region {
        struct list_head node;
 };
 
+struct fpga_afu_dma_region {
+       u64 user_addr;
+       u64 length;
+       u64 iova;
+       struct page **pages;
+       struct rb_node node;
+       bool in_use;
+};
+
 struct fpga_afu {
        u64 region_cur_offset;
        int num_regions;
        u8 num_umsgs;
        struct list_head regions;
+       struct rb_root dma_regions;
 
        struct feature_platform_data *pdata;
 };
@@ -49,4 +59,12 @@ int afu_get_region_by_offset(struct feature_platform_data *pdata,
                            u64 offset, u64 size,
                            struct fpga_afu_region *pregion);
 
+void afu_dma_region_init(struct feature_platform_data *pdata);
+void afu_dma_region_destroy(struct feature_platform_data *pdata);
+long afu_dma_map_region(struct feature_platform_data *pdata,
+                      u64 user_addr, u64 length, u64 *iova);
+long afu_dma_unmap_region(struct feature_platform_data *pdata, u64 iova);
+struct fpga_afu_dma_region *afu_dma_region_find(
+               struct feature_platform_data *pdata, u64 iova, u64 size);
+
 #endif
include/uapi/linux/intel-fpga.h +37 -1
index a2ad332..b97ea02 100644
@@ -111,6 +111,43 @@ struct fpga_port_region_info {
 
 #define FPGA_PORT_GET_REGION_INFO      _IO(FPGA_MAGIC, PORT_BASE + 2)
 
+/**
+ * FPGA_PORT_DMA_MAP - _IOWR(FPGA_MAGIC, PORT_BASE + 3,
+ *                                             struct fpga_port_dma_map)
+ *
+ * Map the dma memory per user_addr and length which are provided by caller.
+ * Driver fills the iova in provided struct afu_port_dma_map.
+ * This interface only accepts page-size aligned user memory for dma mapping.
+ * Return: 0 on success, -errno on failure.
+ */
+struct fpga_port_dma_map {
+       /* Input */
+       __u32 argsz;            /* Structure length */
+       __u32 flags;            /* Zero for now */
+       __u64 user_addr;        /* Process virtual address */
+       __u64 length;           /* Length of mapping (bytes)*/
+       /* Output */
+       __u64 iova;             /* IO virtual address */
+};
+
+#define FPGA_PORT_DMA_MAP      _IO(FPGA_MAGIC, PORT_BASE + 3)
+
+/**
+ * FPGA_PORT_DMA_UNMAP - _IOW(FPGA_MAGIC, PORT_BASE + 4,
+ *                                             struct fpga_port_dma_unmap)
+ *
+ * Unmap the dma memory per iova provided by caller.
+ * Return: 0 on success, -errno on failure.
+ */
+struct fpga_port_dma_unmap {
+       /* Input */
+       __u32 argsz;            /* Structure length */
+       __u32 flags;            /* Zero for now */
+       __u64 iova;             /* IO virtual address */
+};
+
+#define FPGA_PORT_DMA_UNMAP    _IO(FPGA_MAGIC, PORT_BASE + 4)
+
 /* IOCTLs for FME file descriptor */
 
 /**
--

1.8.3.1

author
#90 lore raw

Re: [PATCH v2 22/22] fpga: intel: afu: add FPGA_PORT_DMA_MAP/UNMAP ioctls support

Quoted 1 line · Alan Tull

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

Hi Alan

Thanks a lot for the code review. :)

Quoted 4 lines

Hi Hao,

Please run checkpatch.pl --strict on this.

Sure, thanks for the suggestion, will run this on all the patches and fix the items reported.

Quoted 3 lines

Could you add some kernel-doc function comments here to help the new user (or reviewer) get a better handle on what this code is doing?

Do make sense to me, sure, will add kernel-doc style function comments.

Quoted 10 lines

A few mostly minor comments below.

Quoted 7 lines

DMA memory regions are required for Accelerated Function Unit (AFU) usage. These two ioctls allow user space applications to map user memory regions for dma, and unmap them after use. Iova is returned from driver to user space application via FPGA_PORT_DMA_MAP ioctl. Application needs to unmap it after use, otherwise, driver will unmap them in device file release operation.

Quoted 4 lines
Quoted 1 line

All the mapped regions are managed via a rb tree.

Please note that each afu has its own rb tree (this comment sounds like there's one rb tree for all) to keep track of its DMA regions.

Yes, each afu has its own rb tree to manage the DMA regions, as each afu could be assigned to a separated virtual machine.

I will improve the description above to avoid confusing things.

Quoted 95 lines
Quoted 95 lines

Ioctl interfaces:
* FPGA_PORT_DMA_MAP
Do the dma mapping per user_addr and length which provided by user. Return iova in provided struct afu_port_dma_map.

* FPGA_PORT_DMA_UNMAP
Unmap the dma region per iova provided by user.

---
v2: moved the code to drivers/fpga folder as suggested by Alan Tull.
switched to GPLv2 license.
fixed kbuild warnings.

---
 drivers/fpga/Makefile               |   3 +-
 drivers/fpga/intel-afu-dma-region.c | 372 ++++++++++++++++++++++++++++++++++++
 drivers/fpga/intel-afu-main.c       |  61 +++++-
 drivers/fpga/intel-afu.h            |  18 ++
 include/uapi/linux/intel-fpga.h     |  37 ++++
 5 files changed, 489 insertions(+), 2 deletions(-)
 create mode 100644 drivers/fpga/intel-afu-dma-region.c
drivers/fpga/Makefile +2 -1
index 45c0538..339d1f3 100644
@@ -38,4 +38,5 @@ obj-$(CONFIG_INTEL_FPGA_AFU)          += intel-fpga-afu.o
 
 intel-fpga-pci-objs := intel-pcie.o intel-feature-dev.o
 intel-fpga-fme-objs := intel-fme-main.o intel-fme-pr.o
-intel-fpga-afu-objs := intel-afu-main.o intel-afu-region.o
+intel-fpga-afu-objs := intel-afu-main.o intel-afu-region.o \
+                      intel-afu-dma-region.o
drivers/fpga/intel-afu-dma-region.c new file +49 -0
new file mode 100644
index 0000000..982a9b5
@@ -0,0 +1,372 @@
+/*
+ * Driver for Intel FPGA Accelerated Function Unit (AFU) DMA Region Management
+ *
+ * Copyright (C) 2017 Intel Corporation, Inc.
+ *
+ * Authors:
+ *   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 <linux/sched/signal.h>
+#include <linux/uaccess.h>
+
+#include "intel-afu.h"
+
+static void put_all_pages(struct page **pages, int npages)
+{
+       int i;
+
+       for (i = 0; i < npages; i++)
+               if (pages[i] != NULL)
+                       put_page(pages[i]);
+}
+
+void afu_dma_region_init(struct feature_platform_data *pdata)
+{
+       struct fpga_afu *afu = fpga_pdata_get_private(pdata);
+
+       afu->dma_regions = RB_ROOT;
+}
+
+static long afu_dma_adjust_locked_vm(struct device *dev, long npages, bool incr)
+{
+       unsigned long locked, lock_limit;
+       int ret = 0;
+
+       /* the task is exiting. */
+       if (!current->mm)
+               return 0;
+
+       down_write(&current->mm->mmap_sem);
+
+       if (incr) {
+               locked = current->mm->locked_vm + npages;
+               lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
+
Quoted 8 lines

drivers/fpga/intel-afu-dma-region.c line 55

Quoted 6 lines
drivers/fpga/intel-afu-dma-region.c new file +6 -0
+               if (locked > lock_limit && !capable(CAP_IPC_LOCK))
+                       ret = -ENOMEM;
+               else
+                       current->mm->locked_vm += npages;
+       } else {
+

Don't need to skip a line here.

Sorry, will fix this in next version.

Quoted 15 lines
Quoted 15 lines
drivers/fpga/intel-afu-dma-region.c new file +14 -0
+               if (WARN_ON_ONCE(npages > current->mm->locked_vm))
+                       npages = current->mm->locked_vm;
+               current->mm->locked_vm -= npages;
+       }
+
+       dev_dbg(dev, "[%d] RLIMIT_MEMLOCK %c%ld %ld/%ld%s\n", current->pid,
+                               incr ? '+' : '-',
+                               npages << PAGE_SHIFT,
+                               current->mm->locked_vm << PAGE_SHIFT,
+                               rlimit(RLIMIT_MEMLOCK),
+                               ret ? "- execeeded" : "");
+
+       up_write(&current->mm->mmap_sem);
+
Quoted 8 lines

drivers/fpga/intel-afu-dma-region.c line 74

Quoted 5 lines
drivers/fpga/intel-afu-dma-region.c new file +5 -0
+       return ret;
+}
+
+static long afu_dma_pin_pages(struct feature_platform_data *pdata,
+                               struct fpga_afu_dma_region *region)

Conventionally, return type is usually int in kernel functions that return 0 or error code.

The ioctl function has long return value, so I made these functions to have the long return value too.

I could update the code to change it to int for sure, I think no impact to any functionality. : )

Quoted 16 lines
Quoted 16 lines
drivers/fpga/intel-afu-dma-region.c new file +15 -0
+{
+       long npages = region->length >> PAGE_SHIFT;
+       struct device *dev = &pdata->dev->dev;
+       long ret, pinned;
+
+       ret = afu_dma_adjust_locked_vm(dev, npages, true);
+       if (ret)
+               return ret;
+
+       region->pages = kcalloc(npages, sizeof(struct page *), GFP_KERNEL);
+       if (!region->pages) {
+               afu_dma_adjust_locked_vm(dev, npages, false);
+               return -ENOMEM;
+       }
+
Quoted 8 lines

drivers/fpga/intel-afu-dma-region.c line 93

Quoted 4 lines
drivers/fpga/intel-afu-dma-region.c new file +4 -0
+       pinned = get_user_pages_fast(region->user_addr, npages, 1,
+                                       region->pages);
+       if (pinned < 0) {
+               ret = pinned;

This return value gets all the way to be returned by the ioctl and isn't used for its value as the number that actually got pinned by the caller.

Yes, user application doesn't care how many pages are pinned, it only needs to know the dma mapping request result for given memory region.

So if get_user_pages_fast returns error code, or only pins a page number which is smaller than expected, then return error code to userspace application. If it works as expected, then return 0.

Will put them into function comment in next version.

Quoted 42 lines
Quoted 42 lines
drivers/fpga/intel-afu-dma-region.c new file +41 -0
+               goto err_put_pages;
+       } else if (pinned != npages) {
+               ret = -EFAULT;
+               goto err;
+       }
+
+       dev_dbg(dev, "%ld pages pinned\n", pinned);
+
+       return 0;
+
+err_put_pages:
+       put_all_pages(region->pages, pinned);
+err:
+       kfree(region->pages);
+       afu_dma_adjust_locked_vm(dev, npages, false);
+       return ret;
+}
+
+static void afu_dma_unpin_pages(struct feature_platform_data *pdata,
+                               struct fpga_afu_dma_region *region)
+{
+       long npages = region->length >> PAGE_SHIFT;
+       struct device *dev = &pdata->dev->dev;
+
+       put_all_pages(region->pages, npages);
+       kfree(region->pages);
+       afu_dma_adjust_locked_vm(dev, npages, false);
+
+       dev_dbg(dev, "%ld pages unpinned\n", npages);
+}
+
+static bool afu_dma_check_continuous_pages(struct fpga_afu_dma_region *region)
+{
+       int npages = region->length >> PAGE_SHIFT;
+       int i;
+
+       for (i = 0; i < npages - 1; i++)
+               if (page_to_pfn(region->pages[i]) + 1 !=
+                                       page_to_pfn(region->pages[i+1]))
+                       return false;
+
Quoted 8 lines

drivers/fpga/intel-afu-dma-region.c line 139

Quoted 5 lines
drivers/fpga/intel-afu-dma-region.c new file +5 -0
+       return true;
+}
+
+static bool dma_region_check_iova(struct fpga_afu_dma_region *region,
+                                 u64 iova, u64 size)

This function checks that the area defined by iova and size is fully contained in the region, right? A comment would help.

Yes , will add comment for this function.

Quoted 254 lines
Quoted 254 lines
drivers/fpga/intel-afu-dma-region.c new file +233 -0
+{
+       if (!size && region->iova != iova)
+               return false;
+
+       return (region->iova <= iova) &&
+               (region->length + region->iova >= iova + size);
+}
+
+/* Need to be called with pdata->lock held */
+static int afu_dma_region_add(struct feature_platform_data *pdata,
+                                       struct fpga_afu_dma_region *region)
+{
+       struct fpga_afu *afu = fpga_pdata_get_private(pdata);
+       struct rb_node **new, *parent = NULL;
+
+       dev_dbg(&pdata->dev->dev, "add region (iova = %llx)\n",
+                                       (unsigned long long)region->iova);
+
+       new = &(afu->dma_regions.rb_node);
+
+       while (*new) {
+               struct fpga_afu_dma_region *this;
+
+               this = container_of(*new, struct fpga_afu_dma_region, node);
+
+               parent = *new;
+
+               if (dma_region_check_iova(this, region->iova, region->length))
+                       return -EEXIST;
+
+               if (region->iova < this->iova)
+                       new = &((*new)->rb_left);
+               else if (region->iova > this->iova)
+                       new = &((*new)->rb_right);
+               else
+                       return -EEXIST;
+       }
+
+       rb_link_node(&region->node, parent, new);
+       rb_insert_color(&region->node, &afu->dma_regions);
+
+       return 0;
+}
+
+/* Need to be called with pdata->lock held */
+static void afu_dma_region_remove(struct feature_platform_data *pdata,
+                                       struct fpga_afu_dma_region *region)
+{
+       struct fpga_afu *afu;
+
+       dev_dbg(&pdata->dev->dev, "del region (iova = %llx)\n",
+                                       (unsigned long long)region->iova);
+
+       afu = fpga_pdata_get_private(pdata);
+       rb_erase(&region->node, &afu->dma_regions);
+}
+
+/* Need to be called with pdata->lock held */
+void afu_dma_region_destroy(struct feature_platform_data *pdata)
+{
+       struct fpga_afu *afu = fpga_pdata_get_private(pdata);
+       struct rb_node *node = rb_first(&afu->dma_regions);
+       struct fpga_afu_dma_region *region;
+
+       while (node) {
+               region = container_of(node, struct fpga_afu_dma_region, node);
+
+               dev_dbg(&pdata->dev->dev, "del region (iova = %llx)\n",
+                                       (unsigned long long)region->iova);
+
+               rb_erase(node, &afu->dma_regions);
+
+               if (region->iova)
+                       dma_unmap_page(fpga_pdata_to_pcidev(pdata),
+                                       region->iova, region->length,
+                                       DMA_BIDIRECTIONAL);
+
+               if (region->pages)
+                       afu_dma_unpin_pages(pdata, region);
+
+               node = rb_next(node);
+               kfree(region);
+       }
+}
+
+/*
+ * It finds the dma region from the rbtree based on @iova and @size:
+ * - if @size == 0, it finds the dma region which starts from @iova
+ * - otherwise, it finds the dma region which fully contains
+ *   [@iova, @iova+size)
+ * If nothing is matched returns NULL.
+ *
+ * Need to be called with pdata->lock held.
+ */
+struct fpga_afu_dma_region *
+afu_dma_region_find(struct feature_platform_data *pdata, u64 iova, u64 size)
+{
+       struct fpga_afu *afu = fpga_pdata_get_private(pdata);
+       struct rb_node *node = afu->dma_regions.rb_node;
+       struct device *dev = &pdata->dev->dev;
+
+       while (node) {
+               struct fpga_afu_dma_region *region;
+
+               region = container_of(node, struct fpga_afu_dma_region, node);
+
+               if (dma_region_check_iova(region, iova, size)) {
+                       dev_dbg(dev, "find region (iova = %llx)\n",
+                               (unsigned long long)region->iova);
+                       return region;
+               }
+
+               if (iova < region->iova)
+                       node = node->rb_left;
+               else if (iova > region->iova)
+                       node = node->rb_right;
+               else
+                       /* the iova region is not fully covered. */
+                       break;
+       }
+
+       dev_dbg(dev, "region with iova %llx and size %llx is not found\n",
+               (unsigned long long)iova, (unsigned long long)size);
+       return NULL;
+}
+
+static struct fpga_afu_dma_region *
+afu_dma_region_find_iova(struct feature_platform_data *pdata, u64 iova)
+{
+       return afu_dma_region_find(pdata, iova, 0);
+}
+
+long afu_dma_map_region(struct feature_platform_data *pdata,
+                      u64 user_addr, u64 length, u64 *iova)
+{
+       struct fpga_afu_dma_region *region;
+       int ret;
+
+       /*
+        * Check Inputs, only accept page-aligned user memory region with
+        * valid length.
+        */
+       if (!PAGE_ALIGNED(user_addr) || !PAGE_ALIGNED(length) || !length)
+               return -EINVAL;
+
+       /* Check overflow */
+       if (user_addr + length < user_addr)
+               return -EINVAL;
+
+       if (!access_ok(VERIFY_WRITE, (void __user *)(unsigned long)user_addr,
+                      length))
+               return -EINVAL;
+
+       region = kzalloc(sizeof(*region), GFP_KERNEL);
+       if (!region)
+               return -ENOMEM;
+
+       region->user_addr = user_addr;
+       region->length = length;
+
+       /* Pin the user memory region */
+       ret = afu_dma_pin_pages(pdata, region);
+       if (ret) {
+               dev_err(&pdata->dev->dev, "fail to pin memory region\n");
+               goto free_region;
+       }
+
+       /* Only accept continuous pages, return error if no */
+       if (!afu_dma_check_continuous_pages(region)) {
+               dev_err(&pdata->dev->dev, "pages are not continuous\n");
+               ret = -EINVAL;
+               goto unpin_pages;
+       }
+
+       /* As pages are continuous then start to do DMA mapping */
+       region->iova = dma_map_page(fpga_pdata_to_pcidev(pdata),
+                                   region->pages[0], 0,
+                                   region->length,
+                                   DMA_BIDIRECTIONAL);
+       if (dma_mapping_error(&pdata->dev->dev, region->iova)) {
+               dev_err(&pdata->dev->dev, "fail to map dma mapping\n");
+               ret = -EFAULT;
+               goto unpin_pages;
+       }
+
+       *iova = region->iova;
+
+       mutex_lock(&pdata->lock);
+       ret = afu_dma_region_add(pdata, region);
+       mutex_unlock(&pdata->lock);
+       if (ret) {
+               dev_err(&pdata->dev->dev, "fail to add dma region\n");
+               goto unmap_dma;
+       }
+
+       return 0;
+
+unmap_dma:
+       dma_unmap_page(fpga_pdata_to_pcidev(pdata),
+                      region->iova, region->length, DMA_BIDIRECTIONAL);
+unpin_pages:
+       afu_dma_unpin_pages(pdata, region);
+free_region:
+       kfree(region);
+       return ret;
+}
+
+long afu_dma_unmap_region(struct feature_platform_data *pdata, u64 iova)
+{
+       struct fpga_afu_dma_region *region;
+
+       mutex_lock(&pdata->lock);
+       region = afu_dma_region_find_iova(pdata, iova);
+       if (!region) {
+               mutex_unlock(&pdata->lock);
+               return -EINVAL;
+       }
+
+       if (region->in_use) {
+               mutex_unlock(&pdata->lock);
+               return -EBUSY;
+       }
+
+       afu_dma_region_remove(pdata, region);
+       mutex_unlock(&pdata->lock);
+
+       dma_unmap_page(fpga_pdata_to_pcidev(pdata),
+                      region->iova, region->length, DMA_BIDIRECTIONAL);
+       afu_dma_unpin_pages(pdata, region);
+       kfree(region);
+
+       return 0;
+}
drivers/fpga/intel-afu-main.c +5 -1
index 8c7aa70..d9f1ebf 100644
@@ -175,7 +175,11 @@ static int afu_release(struct inode *inode, struct file *filp)
 
        dev_dbg(&pdev->dev, "Device File Release\n");
 
-       fpga_port_reset(pdev);
+       mutex_lock(&pdata->lock);
+       __fpga_port_reset(pdev);
+       afu_dma_region_destroy(pdata);
+       mutex_unlock(&pdata->lock);
+
        feature_dev_use_end(pdata);
        return 0;
 }
@@ -245,6 +249,55 @@ static long afu_ioctl_check_extension(struct feature_platform_data *pdata,
        return 0;
 }
 
Quoted 13 lines

drivers/fpga/intel-afu-main.c static long afu_ioctl_check_extension(struct feature_platform_data *pdata, · line 261

Quoted 10 lines
drivers/fpga/intel-afu-main.c +10 -0
+static long
+afu_ioctl_dma_map(struct feature_platform_data *pdata, void __user *arg)
+{
+       struct fpga_port_dma_map map;
+       unsigned long minsz;
+       long ret;
+
+       minsz = offsetofend(struct fpga_port_dma_map, iova);
+
+       if (copy_from_user(&map, arg, minsz))

Why are you using offsetofend() instead of sizeof(map)? Is this struct going to expand beyond iova? Also below in one place.

Yes, we just want to make these ioctls extendable.

Actually this is following the same style as vfio. Here is one reference case in vfio[1]. Define some new flags and introduce new members in newer version API data structure, and new version driver still works with the applications using old version API data structure. : )

And other ioctls in intel fpga device drivers are using the same style too.

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

Quoted 23 lines
Quoted 23 lines
drivers/fpga/intel-afu-main.c +23 -0
+               return -EFAULT;
+
+       if (map.argsz < minsz || map.flags)
+               return -EINVAL;
+
+       ret = afu_dma_map_region(pdata, map.user_addr, map.length, &map.iova);
+       if (ret)
+               return ret;
+
+       if (copy_to_user(arg, &map, sizeof(map))) {
+               afu_dma_unmap_region(pdata, map.iova);
+               return -EFAULT;
+       }
+
+       dev_dbg(&pdata->dev->dev, "dma map: ua=%llx, len=%llx, iova=%llx\n",
+                               (unsigned long long)map.user_addr,
+                               (unsigned long long)map.length,
+                               (unsigned long long)map.iova);
+
+       return 0;
+}
+
+static long
Quoted 8 lines

drivers/fpga/intel-afu-main.c static long afu_ioctl_check_extension(struct feature_platform_data *pdata, · line 290

Quoted 6 lines
drivers/fpga/intel-afu-main.c +6 -0
+afu_ioctl_dma_unmap(struct feature_platform_data *pdata, void __user *arg)
+{
+       struct fpga_port_dma_unmap unmap;
+       unsigned long minsz;
+
+       minsz = offsetofend(struct fpga_port_dma_unmap, iova);

Here as well.

Same as above.

Thanks
Hao

Quoted 131 lines
Quoted 131 lines
drivers/fpga/intel-afu-main.c +16 -0
+
+       if (copy_from_user(&unmap, arg, minsz))
+               return -EFAULT;
+
+       if (unmap.argsz < minsz || unmap.flags)
+               return -EINVAL;
+
+       return afu_dma_unmap_region(pdata, unmap.iova);
+}
+
 static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
        struct platform_device *pdev = filp->private_data;
@@ -263,6 +316,10 @@ static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
                return afu_ioctl_get_info(pdata, (void __user *)arg);
        case FPGA_PORT_GET_REGION_INFO:
                return afu_ioctl_get_region_info(pdata, (void __user *)arg);
+       case FPGA_PORT_DMA_MAP:
+               return afu_ioctl_dma_map(pdata, (void __user *)arg);
+       case FPGA_PORT_DMA_UNMAP:
+               return afu_ioctl_dma_unmap(pdata, (void __user *)arg);
        default:
                /*
                 * Let sub-feature's ioctl function to handle the cmd
@@ -337,6 +394,7 @@ static int afu_dev_init(struct platform_device *pdev)
        mutex_lock(&pdata->lock);
        fpga_pdata_set_private(pdata, afu);
        afu_region_init(pdata);
+       afu_dma_region_init(pdata);
        mutex_unlock(&pdata->lock);
        return 0;
 }
@@ -349,6 +407,7 @@ static int afu_dev_destroy(struct platform_device *pdev)
        mutex_lock(&pdata->lock);
        afu = fpga_pdata_get_private(pdata);
        afu_region_destroy(pdata);
+       afu_dma_region_destroy(pdata);
        fpga_pdata_set_private(pdata, NULL);
        mutex_unlock(&pdata->lock);
 
drivers/fpga/intel-afu.h +18 -0
index 3417780d..23f7e24 100644
@@ -30,11 +30,21 @@ struct fpga_afu_region {
        struct list_head node;
 };
 
+struct fpga_afu_dma_region {
+       u64 user_addr;
+       u64 length;
+       u64 iova;
+       struct page **pages;
+       struct rb_node node;
+       bool in_use;
+};
+
 struct fpga_afu {
        u64 region_cur_offset;
        int num_regions;
        u8 num_umsgs;
        struct list_head regions;
+       struct rb_root dma_regions;
 
        struct feature_platform_data *pdata;
 };
@@ -49,4 +59,12 @@ int afu_get_region_by_offset(struct feature_platform_data *pdata,
                            u64 offset, u64 size,
                            struct fpga_afu_region *pregion);
 
+void afu_dma_region_init(struct feature_platform_data *pdata);
+void afu_dma_region_destroy(struct feature_platform_data *pdata);
+long afu_dma_map_region(struct feature_platform_data *pdata,
+                      u64 user_addr, u64 length, u64 *iova);
+long afu_dma_unmap_region(struct feature_platform_data *pdata, u64 iova);
+struct fpga_afu_dma_region *afu_dma_region_find(
+               struct feature_platform_data *pdata, u64 iova, u64 size);
+
 #endif
include/uapi/linux/intel-fpga.h +37 -1
index a2ad332..b97ea02 100644
@@ -111,6 +111,43 @@ struct fpga_port_region_info {
 
 #define FPGA_PORT_GET_REGION_INFO      _IO(FPGA_MAGIC, PORT_BASE + 2)
 
+/**
+ * FPGA_PORT_DMA_MAP - _IOWR(FPGA_MAGIC, PORT_BASE + 3,
+ *                                             struct fpga_port_dma_map)
+ *
+ * Map the dma memory per user_addr and length which are provided by caller.
+ * Driver fills the iova in provided struct afu_port_dma_map.
+ * This interface only accepts page-size aligned user memory for dma mapping.
+ * Return: 0 on success, -errno on failure.
+ */
+struct fpga_port_dma_map {
+       /* Input */
+       __u32 argsz;            /* Structure length */
+       __u32 flags;            /* Zero for now */
+       __u64 user_addr;        /* Process virtual address */
+       __u64 length;           /* Length of mapping (bytes)*/
+       /* Output */
+       __u64 iova;             /* IO virtual address */
+};
+
+#define FPGA_PORT_DMA_MAP      _IO(FPGA_MAGIC, PORT_BASE + 3)
+
+/**
+ * FPGA_PORT_DMA_UNMAP - _IOW(FPGA_MAGIC, PORT_BASE + 4,
+ *                                             struct fpga_port_dma_unmap)
+ *
+ * Unmap the dma memory per iova provided by caller.
+ * Return: 0 on success, -errno on failure.
+ */
+struct fpga_port_dma_unmap {
+       /* Input */
+       __u32 argsz;            /* Structure length */
+       __u32 flags;            /* Zero for now */
+       __u64 iova;             /* IO virtual address */
+};
+
+#define FPGA_PORT_DMA_UNMAP    _IO(FPGA_MAGIC, PORT_BASE + 4)
+
 /* IOCTLs for FME file descriptor */
 
 /**
--

1.8.3.1