Skip to contentSkip to conversation

FPGA Manager Framework

PATCH Superseded [email protected] 5 participants 19 messages Sep 22, 2015 – Sep 25, 2015

Subsystems (2)

v11 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v11 cover FPGA Manager Framework

From: Alan Tull <[email protected]>

This patch set adds the FPGA manager core which exports API functions that write an image to a FPGA

I'm holding off on the DT overlay support a little for now.

The core's API is minimal to start with: only 6 functions. This gives a manufacturer-agnostic interface for programming FPGA's such that higher level interfaces (such as DT Overlays) can be shared.

Alan Tull (4):
usage documentation for FPGA manager core fpga manager: add sysfs interface document add FPGA manager core
fpga manager: add driver for socfpga fpga manager

 Documentation/ABI/testing/sysfs-class-fpga-manager |   37 ++
 Documentation/fpga/fpga-mgr.txt                    |  171 ++++++
 drivers/Kconfig                                    |    2 +
 drivers/Makefile                                   |    1 +
 drivers/fpga/Kconfig                               |   24 +
 drivers/fpga/Makefile                              |    9 +
 drivers/fpga/fpga-mgr.c                            |  382 ++++++++++++
 drivers/fpga/socfpga.c                             |  616 ++++++++++++++++++++
 include/linux/fpga/fpga-mgr.h                      |  127 ++++
 9 files changed, 1369 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-manager
 create mode 100644 Documentation/fpga/fpga-mgr.txt
 create mode 100644 drivers/fpga/Kconfig
 create mode 100644 drivers/fpga/Makefile
 create mode 100644 drivers/fpga/fpga-mgr.c
 create mode 100644 drivers/fpga/socfpga.c
 create mode 100644 include/linux/fpga/fpga-mgr.h
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v11 1/4 usage documentation for FPGA manager core

From: Alan Tull <[email protected]>

Add a document on the new FPGA manager core.

---
v9: initial version where this patch was added

v10: requested cleanups to formatting and otherwise
s/fpga/FPGA/g
rewrite implementation section to not reference socfpga.c by name other rewrites
Moved to Documentation/fpga/

v11: s/with image/with an image/
s/on the path/in the path/

---
 Documentation/fpga/fpga-mgr.txt |  171 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 Documentation/fpga/fpga-mgr.txt
Documentation/fpga/fpga-mgr.txt new file +171 -0
new file mode 100644
index 0000000..ce3e84f
@@ -0,0 +1,171 @@
+FPGA Manager Core
+
+Alan Tull 2015
+
+Overview
+========
+
+The FPGA manager core exports a set of functions for programming an FPGA with
+an image.  The API is manufacturer agnostic.  All manufacturer specifics are
+hidden away in a low level driver which registers a set of ops with the core.
+The FPGA image data itself is very manufacturer specific, but for our purposes
+it's just binary data.  The FPGA manager core won't parse it.
+
+
+API Functions:
+==============
+
+To program the FPGA from a file or from a buffer:
+-------------------------------------------------
+
+	int fpga_mgr_buf_load(struct fpga_manager *mgr, u32 flags,
+		              const char *buf, size_t count);
+
+Load the FPGA from an image which exists as a buffer in memory.
+
+	int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,
+		                   const char *image_name);
+
+Load the FPGA from an image which exists as a file.  The image file must be on
+the firmware search path (see the firmware class documentation).
+
+For both these functions, flags == 0 for normal full reconfiguration or
+FPGA_MGR_PARTIAL_RECONFIG for partial reconfiguration.  If successful, the FPGA
+ends up in operating mode.  Return 0 on success or a negative error code.
+
+
+To get/put a reference to a FPGA manager:
+-----------------------------------------
+
+	struct fpga_manager *of_fpga_mgr_get(struct device_node *node);
+
+	void fpga_mgr_put(struct fpga_manager *mgr);
+
+Given a DT node, get an exclusive reference to a FPGA manager or release
+the reference.
+
+
+To register or unregister the low level FPGA-specific driver:
+-------------------------------------------------------------
+
+	int fpga_mgr_register(struct device *dev, const char *name,
+		              const struct fpga_manager_ops *mops,
+		              void *priv);
+
+	void fpga_mgr_unregister(struct device *dev);
+
+Use of these two functions is described below in "How To Support a new FPGA
+device."
+
+
+How to write an image buffer to a supported FPGA
+================================================
+/* Include to get the API */
+#include <linux/fpga/fpga-mgr.h>
+
+/* device node that specifies the FPGA manager to use */
+struct device_node *mgr_node = ...
+
+/* FPGA image is in this buffer.  count is size of the buffer. */
+char *buf = ...
+int count = ...
+
+/* flags indicates whether to do full or partial reconfiguration */
+int flags = 0;
+
+int ret;
+
+/* Get exclusive control of FPGA manager */
+struct fpga_manager *mgr = of_fpga_mgr_get(mgr_node);
+
+/* Load the buffer to the FPGA */
+ret = fpga_mgr_buf_load(mgr, flags, buf, count);
+
+/* Release the FPGA manager */
+fpga_mgr_put(mgr);
+
+
+How to write an image file to a supported FPGA
+==============================================
+/* Include to get the API */
+#include <linux/fpga/fpga-mgr.h>
+
+/* device node that specifies the FPGA manager to use */
+struct device_node *mgr_node = ...
+
+/* FPGA image is in this file which is in the firmware search path */
+const char *path = "fpga-image-9.rbf"
+
+/* flags indicates whether to do full or partial reconfiguration */
+int flags = 0;
+
+int ret;
+
+/* Get exclusive control of FPGA manager */
+struct fpga_manager *mgr = of_fpga_mgr_get(mgr_node);
+
+/* Get the firmware image (path) and load it to the FPGA */
+ret = fpga_mgr_firmware_load(mgr, flags, path);
+
+/* Release the FPGA manager */
+fpga_mgr_put(mgr);
+
+
+How to support a new FPGA device
+================================
+To add another FPGA manager, write a driver that implements a set of ops.  The
+probe function calls fpga_mgr_register(), such as:
+
+static const struct fpga_manager_ops socfpga_fpga_ops = {
+       .write_init = socfpga_fpga_ops_configure_init,
+       .write = socfpga_fpga_ops_configure_write,
+       .write_complete = socfpga_fpga_ops_configure_complete,
+       .state = socfpga_fpga_ops_state,
+};
+
+static int socfpga_fpga_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct socfpga_fpga_priv *priv;
+	int ret;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	/* ... do ioremaps, get interrupts, etc. and save
+	   them in priv... */
+
+	return fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
+				 &socfpga_fpga_ops, priv);
+}
+
+static int socfpga_fpga_remove(struct platform_device *pdev)
+{
+	fpga_mgr_unregister(&pdev->dev);
+
+	return 0;
+}
+
+
+The ops will implement whatever device specific register writes are needed to
+do the programming sequence for this particular FPGA.  These ops return 0 for
+success or negative error codes otherwise.
+
+The programming sequence is:
+ 1. .write_init
+ 2. .write (may be called once or multiple times)
+ 3. .write_complete
+
+The .write_init function will prepare the FPGA to receive the image data.
+
+The .write function writes a buffer to the FPGA. The buffer may be contain the
+whole FPGA image or may be a smaller chunk of an FPGA image.  In the latter
+case, this function is called multiple times for successive chunks.
+
+The .write_complete function is called after all the image has been written
+to put the FPGA into operating mode.
+
+The ops include a .state function which will read the hardware FPGA manager and
+return a code of type enum fpga_mgr_states.  It doesn't result in a change in
+hardware state.
Signature · 1 line
1.7.9.5
Reviewed-by on v11 1/4
#2 lore raw

Re: [PATCH v11 1/4] usage documentation for FPGA manager core

Hi Alan,

Quoted 4 lines · On Tue, Sep 22, 2015 at 8:21 AM, <[email protected]> wrote:

From: Alan Tull <[email protected]>

Add a document on the new FPGA manager core.

Quoted 195 lines

---
v9: initial version where this patch was added

v10: requested cleanups to formatting and otherwise
s/fpga/FPGA/g
rewrite implementation section to not reference socfpga.c by name other rewrites
Moved to Documentation/fpga/

v11: s/with image/with an image/
s/on the path/in the path/

---
 Documentation/fpga/fpga-mgr.txt |  171 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 Documentation/fpga/fpga-mgr.txt
Documentation/fpga/fpga-mgr.txt new file +171 -1
new file mode 100644
index 0000000..ce3e84f
@@ -0,0 +1,171 @@
+FPGA Manager Core
+
+Alan Tull 2015
+
+Overview
+========
+
+The FPGA manager core exports a set of functions for programming an FPGA with
+an image.  The API is manufacturer agnostic.  All manufacturer specifics are
+hidden away in a low level driver which registers a set of ops with the core.
+The FPGA image data itself is very manufacturer specific, but for our purposes
+it's just binary data.  The FPGA manager core won't parse it.
+
+
+API Functions:
+==============
+
+To program the FPGA from a file or from a buffer:
+-------------------------------------------------
+
+       int fpga_mgr_buf_load(struct fpga_manager *mgr, u32 flags,
+                             const char *buf, size_t count);
+
+Load the FPGA from an image which exists as a buffer in memory.
+
+       int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,
+                                  const char *image_name);
+
+Load the FPGA from an image which exists as a file.  The image file must be on
+the firmware search path (see the firmware class documentation).
+
+For both these functions, flags == 0 for normal full reconfiguration or
+FPGA_MGR_PARTIAL_RECONFIG for partial reconfiguration.  If successful, the FPGA
+ends up in operating mode.  Return 0 on success or a negative error code.
+
+
+To get/put a reference to a FPGA manager:
+-----------------------------------------
+
+       struct fpga_manager *of_fpga_mgr_get(struct device_node *node);
+
+       void fpga_mgr_put(struct fpga_manager *mgr);
+
+Given a DT node, get an exclusive reference to a FPGA manager or release
+the reference.
+
+
+To register or unregister the low level FPGA-specific driver:
+-------------------------------------------------------------
+
+       int fpga_mgr_register(struct device *dev, const char *name,
+                             const struct fpga_manager_ops *mops,
+                             void *priv);
+
+       void fpga_mgr_unregister(struct device *dev);
+
+Use of these two functions is described below in "How To Support a new FPGA
+device."
+
+
+How to write an image buffer to a supported FPGA
+================================================
+/* Include to get the API */
+#include <linux/fpga/fpga-mgr.h>
+
+/* device node that specifies the FPGA manager to use */
+struct device_node *mgr_node = ...
+
+/* FPGA image is in this buffer.  count is size of the buffer. */
+char *buf = ...
+int count = ...
+
+/* flags indicates whether to do full or partial reconfiguration */
+int flags = 0;
+
+int ret;
+
+/* Get exclusive control of FPGA manager */
+struct fpga_manager *mgr = of_fpga_mgr_get(mgr_node);
+
+/* Load the buffer to the FPGA */
+ret = fpga_mgr_buf_load(mgr, flags, buf, count);
+
+/* Release the FPGA manager */
+fpga_mgr_put(mgr);
+
+
+How to write an image file to a supported FPGA
+==============================================
+/* Include to get the API */
+#include <linux/fpga/fpga-mgr.h>
+
+/* device node that specifies the FPGA manager to use */
+struct device_node *mgr_node = ...
+
+/* FPGA image is in this file which is in the firmware search path */
+const char *path = "fpga-image-9.rbf"
+
+/* flags indicates whether to do full or partial reconfiguration */
+int flags = 0;
+
+int ret;
+
+/* Get exclusive control of FPGA manager */
+struct fpga_manager *mgr = of_fpga_mgr_get(mgr_node);
+
+/* Get the firmware image (path) and load it to the FPGA */
+ret = fpga_mgr_firmware_load(mgr, flags, path);
+
+/* Release the FPGA manager */
+fpga_mgr_put(mgr);
+
+
+How to support a new FPGA device
+================================
+To add another FPGA manager, write a driver that implements a set of ops.  The
+probe function calls fpga_mgr_register(), such as:
+
+static const struct fpga_manager_ops socfpga_fpga_ops = {
+       .write_init = socfpga_fpga_ops_configure_init,
+       .write = socfpga_fpga_ops_configure_write,
+       .write_complete = socfpga_fpga_ops_configure_complete,
+       .state = socfpga_fpga_ops_state,
+};
+
+static int socfpga_fpga_probe(struct platform_device *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct socfpga_fpga_priv *priv;
+       int ret;
+
+       priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+       if (!priv)
+               return -ENOMEM;
+
+       /* ... do ioremaps, get interrupts, etc. and save
+          them in priv... */
+
+       return fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
+                                &socfpga_fpga_ops, priv);
+}
+
+static int socfpga_fpga_remove(struct platform_device *pdev)
+{
+       fpga_mgr_unregister(&pdev->dev);
+
+       return 0;
+}
+
+
+The ops will implement whatever device specific register writes are needed to
+do the programming sequence for this particular FPGA.  These ops return 0 for
+success or negative error codes otherwise.
+
+The programming sequence is:
+ 1. .write_init
+ 2. .write (may be called once or multiple times)
+ 3. .write_complete
+
+The .write_init function will prepare the FPGA to receive the image data.
+
+The .write function writes a buffer to the FPGA. The buffer may be contain the
+whole FPGA image or may be a smaller chunk of an FPGA image.  In the latter
+case, this function is called multiple times for successive chunks.
+
+The .write_complete function is called after all the image has been written
+to put the FPGA into operating mode.
+
+The ops include a .state function which will read the hardware FPGA manager and
+return a code of type enum fpga_mgr_states.  It doesn't result in a change in
+hardware state.
--
Quoted 2 lines

1.7.9.5

Cheers,

Moritz

#3 lore raw

PATCH v11 2/4 fpga manager: add sysfs interface document

In reply to [email protected]

From: Alan Tull <[email protected]>

Add documentation under drivers/staging for new fpga manager's sysfs interface.

---
v5 : (actually second version, but keeping version numbers
aligned with rest of patch series)
Move document to drivers/staging/fpga/Documentation/ABI

v6 : No change in this patch for v6 of the patch set v7 : No change in this patch for v7 of the patch set v8 : No change in this patch for v8 of the patch set

v9 : Remove 'firmware' and 'reset' files
Update state strings

v10 : Clarifications about state attribute
Move to Documentation/ABI/testing/

v11 : No change in this patch for v11 of the patch set

---
 Documentation/ABI/testing/sysfs-class-fpga-manager |   37 ++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-manager
Documentation/ABI/testing/sysfs-class-fpga-manager new file +37 -0
new file mode 100644
index 0000000..23056c5
@@ -0,0 +1,37 @@
+What:		/sys/class/fpga_manager/<fpga>/name
+Date:		August 2015
+KernelVersion:	4.3
+Contact:	Alan Tull <[email protected]>
+Description:	Name of low level fpga manager driver.
+
+What:		/sys/class/fpga_manager/<fpga>/state
+Date:		August 2015
+KernelVersion:	4.3
+Contact:	Alan Tull <[email protected]>
+Description:	Read fpga manager state as a string.
+		The intent is to provide enough detail that if something goes
+		wrong during FPGA programming (something that the driver can't
+		fix) then userspace can know, i.e. if the firmware request
+		fails, that could be due to not being able to find the firmware
+		file.
+
+		This is a superset of FPGA states and fpga manager driver
+		states.  The fpga manager driver is walking through these steps
+		to get the FPGA into a known operating state.  It's a sequence,
+		though some steps may get skipped.  Valid FPGA states will vary
+		by manufacturer; this is a superset.
+
+		* unknown		= can't determine state
+		* power off		= FPGA power is off
+		* power up		= FPGA reports power is up
+		* reset			= FPGA held in reset state
+		* firmware request	= firmware class request in progress
+		* firmware request error = firmware request failed
+		* write init		= preparing FPGA for programming
+		* write init error	= Error while preparing FPGA for
+					  programming
+		* write			= FPGA ready to receive image data
+		* write error		= Error while programming
+		* write complete	= Doing post programming steps
+		* write complete error	= Error while doing post programming
+		* operating		= FPGA is programmed and operating
Signature · 1 line
1.7.9.5
Reviewed-by on v11 2/4
#4 lore raw

Re: [PATCH v11 2/4] fpga manager: add sysfs interface document

Quoted 5 lines · On Tue, Sep 22, 2015 at 8:21 AM, <atull-yzvPICuk2ABMcg4IHK0kFoH6Mc4MB0Vx@publi…

From: Alan Tull <[email protected]>

Add documentation under drivers/staging for new fpga manager's sysfs interface.

Quoted 66 lines

---
v5 : (actually second version, but keeping version numbers
aligned with rest of patch series)
Move document to drivers/staging/fpga/Documentation/ABI

v6 : No change in this patch for v6 of the patch set v7 : No change in this patch for v7 of the patch set v8 : No change in this patch for v8 of the patch set

v9 : Remove 'firmware' and 'reset' files
Update state strings

v10 : Clarifications about state attribute
Move to Documentation/ABI/testing/

v11 : No change in this patch for v11 of the patch set

---
 Documentation/ABI/testing/sysfs-class-fpga-manager |   37 ++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-manager
Documentation/ABI/testing/sysfs-class-fpga-manager new file +37 -1
new file mode 100644
index 0000000..23056c5
@@ -0,0 +1,37 @@
+What:          /sys/class/fpga_manager/<fpga>/name
+Date:          August 2015
+KernelVersion: 4.3
+Contact:       Alan Tull <[email protected]>
+Description:   Name of low level fpga manager driver.
+
+What:          /sys/class/fpga_manager/<fpga>/state
+Date:          August 2015
+KernelVersion: 4.3
+Contact:       Alan Tull <[email protected]>
+Description:   Read fpga manager state as a string.
+               The intent is to provide enough detail that if something goes
+               wrong during FPGA programming (something that the driver can't
+               fix) then userspace can know, i.e. if the firmware request
+               fails, that could be due to not being able to find the firmware
+               file.
+
+               This is a superset of FPGA states and fpga manager driver
+               states.  The fpga manager driver is walking through these steps
+               to get the FPGA into a known operating state.  It's a sequence,
+               though some steps may get skipped.  Valid FPGA states will vary
+               by manufacturer; this is a superset.
+
+               * unknown               = can't determine state
+               * power off             = FPGA power is off
+               * power up              = FPGA reports power is up
+               * reset                 = FPGA held in reset state
+               * firmware request      = firmware class request in progress
+               * firmware request error = firmware request failed
+               * write init            = preparing FPGA for programming
+               * write init error      = Error while preparing FPGA for
+                                         programming
+               * write                 = FPGA ready to receive image data
+               * write error           = Error while programming
+               * write complete        = Doing post programming steps
+               * write complete error  = Error while doing post programming
+               * operating             = FPGA is programmed and operating
--
Quoted 2 lines

1.7.9.5

Cheers,

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

#5 lore raw

PATCH v11 3/4 add FPGA manager core

In reply to [email protected]

From: Alan Tull <[email protected]>

API to support programming FPGA's.

The following functions are exported as GPL:
* fpga_mgr_buf_load
Load fpga from image in buffer

* fpga_mgr_firmware_load
Request firmware and load it to the FPGA.

* fpga_mgr_register
* fpga_mgr_unregister
FPGA device drivers can be added by calling
fpga_mgr_register() to register a set of
fpga_manager_ops to do device specific stuff.

* of_fpga_mgr_get
* fpga_mgr_put
Get/put a reference to a fpga manager.

The following sysfs files are created:
* /sys/class/fpga_manager/<fpga>/name
Name of low level driver.

* /sys/class/fpga_manager/<fpga>/state
State of fpga manager

---
v2: s/mangager/manager/
check for invalid request_nr
add fpga reset interface
rework the state code
remove FPGA_MGR_FAIL flag
add _ERR states to fpga manager states enum low level state op now returns a state enum value initialize framework state from driver state op remove unused fpga read stuff
merge sysfs.c into fpga-mgr.c
move suspend/resume from bus.c to fpga-mgr.c

v3: Add struct device to fpga_manager (not as a pointer)
Add to_fpga_manager
Don't get irq in fpga-mgr.c (let low level driver do it) remove from struct fpga_manager: nr, np, parent get rid of fpga_mgr_get_new_minor()
simplify fpga_manager_register:
reorder parameters
use dev instead of pdev
get rid of code that used to make more sense when this
was a char driver, don't alloc_chrdev_region
use a mutex instead of flags

v4: Move to drivers/staging

v5: Make some things be static
Kconfig: add 'if FPGA'
Makefile: s/fpga-mgr-core.o/fpga-mgr.o/ clean up includes
use enum fpga_mgr_states instead of int static const char *state_str
use DEVICE_ATTR_RO/RW/WO and ATTRIBUTE_GROUPS return -EINVAL instead of BUG_ON
move ida_simple_get after kzalloc
clean up fpga_mgr_remove
fpga-mgr.h: remove '#if IS_ENABLED(CONFIG_FPGA)' add kernel-doc documentation
Move header to new include/linux/fpga folder static const struct fpga_mgr_ops dev_info msg simplified

v6: no statically allocated string for image_name
kernel doc fixes
changes regarding enabling SYSFS for fpga mgr
Makefile cleanup

v7: no change in this patch for v7 of the patchset

v8: no change in this patch for v8 of the patchset

v9: remove writable attributes 'firmware' and 'reset'
remove suspend/resume support for now
remove list of fpga managers; use class device iterators instead simplify locking by giving out only one ref exclusively add device tree support
add flags
par down API into fewer functions
update copyright year
rename some functions for clarity
clean up unnecessary #includes
add some error messages
write_init may need to look at buffer header, so add to params

v10: Make this a tristate in Kconfig
pass flags parameter to write_complete
use BIT(0) in macro
move to drivers/fpga
fpga_manager_get/put call module_try_get/module_put

v11: Set state to 'operating' after successfully programming the FPGA

---
 drivers/Kconfig               |    2 +
 drivers/Makefile              |    1 +
 drivers/fpga/Kconfig          |   14 ++
 drivers/fpga/Makefile         |    8 +
 drivers/fpga/fpga-mgr.c       |  382 +++++++++++++++++++++++++++++++++++++++++
 include/linux/fpga/fpga-mgr.h |  127 ++++++++++++++
 6 files changed, 534 insertions(+)
 create mode 100644 drivers/fpga/Kconfig
 create mode 100644 drivers/fpga/Makefile
 create mode 100644 drivers/fpga/fpga-mgr.c
 create mode 100644 include/linux/fpga/fpga-mgr.h
drivers/Kconfig +2 -0
index 6e973b8..2683346 100644
@@ -184,4 +184,6 @@ source "drivers/android/Kconfig"
 
 source "drivers/nvdimm/Kconfig"
 
+source "drivers/fpga/Kconfig"
+
 endmenu
drivers/Makefile +1 -0
index b64b49f..832a6e0 100644
@@ -165,3 +165,4 @@ obj-$(CONFIG_RAS)		+= ras/
 obj-$(CONFIG_THUNDERBOLT)	+= thunderbolt/
 obj-$(CONFIG_CORESIGHT)		+= hwtracing/coresight/
 obj-$(CONFIG_ANDROID)		+= android/
+obj-$(CONFIG_FPGA)		+= fpga/
drivers/fpga/Kconfig new file +14 -0
new file mode 100644
index 0000000..f1f1f6d
@@ -0,0 +1,14 @@
+#
+# FPGA framework configuration
+#
+
+menu "FPGA Configuration Support"
+
+config FPGA
+	tristate "FPGA Configuration Framework"
+	help
+	  Say Y here if you want support for configuring FPGAs from the
+	  kernel.  The FPGA framework adds a FPGA manager class and FPGA
+	  manager drivers.
+
+endmenu
drivers/fpga/Makefile new file +8 -0
new file mode 100644
index 0000000..3313c52
@@ -0,0 +1,8 @@
+#
+# Makefile for the fpga framework and fpga manager drivers.
+#
+
+# Core FPGA Manager Framework
+obj-$(CONFIG_FPGA)			+= fpga-mgr.o
+
+# FPGA Manager Drivers
drivers/fpga/fpga-mgr.c new file +382 -0

382 lines not on this page · raw message

include/linux/fpga/fpga-mgr.h new file +127 -0

127 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#6 lore raw

Re: [PATCH v11 3/4] add FPGA manager core

Quoted 17 lines · On Tue, Sep 22, 2015 at 10:21:10AM -0500…

From: Alan Tull <[email protected]>

API to support programming FPGA's.

The following functions are exported as GPL:
* fpga_mgr_buf_load
Load fpga from image in buffer

* fpga_mgr_firmware_load
Request firmware and load it to the FPGA.

* fpga_mgr_register
* fpga_mgr_unregister
FPGA device drivers can be added by calling
fpga_mgr_register() to register a set of
fpga_manager_ops to do device specific stuff.

Quoted 7 lines

* of_fpga_mgr_get
* fpga_mgr_put
Get/put a reference to a fpga manager.

The following sysfs files are created:
* /sys/class/fpga_manager/<fpga>/name
Name of low level driver.

Don't 'struct device's already have a name? Why do you need another name attribute?

Quoted 6 lines

* /sys/class/fpga_manager/<fpga>/state
State of fpga manager

[..]

Quoted 2 lines

+++ b/drivers/fpga/fpga-mgr.c

(quoted diff) +0 -0
@@ -0,0 +1,382 @@

[..]

drivers/fpga/fpga-mgr.c line 52

Quoted 8 lines
drivers/fpga/fpga-mgr.c new file +8 -0
+int fpga_mgr_buf_load(struct fpga_manager *mgr, u32 flags, const char *buf,
+		      size_t count)
+{
+	struct device *dev = &mgr->dev;
+	int ret;
+
+	if (!mgr)
+		return -ENODEV;

This seems overly defensive.

[..]

drivers/fpga/fpga-mgr.c line 115

Quoted 9 lines
drivers/fpga/fpga-mgr.c new file +9 -0
+int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,
+			   const char *image_name)
+{
+	struct device *dev = &mgr->dev;
+	const struct firmware *fw;
+	int ret;
+
+	if (!mgr)
+		return -ENODEV;

Again; I'm of the opinion this is needlessly defensive.

Quoted 37 lines
drivers/fpga/fpga-mgr.c new file +37 -0
+
+	dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
+
+	mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
+
+	ret = request_firmware(&fw, image_name, dev);
+	if (ret) {
+		mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
+		dev_err(dev, "Error requesting firmware %s\n", image_name);
+		return ret;
+	}
+
+	ret = fpga_mgr_buf_load(mgr, flags, fw->data, fw->size);
+	if (ret)
+		return ret;
+
+	release_firmware(fw);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load);
+
+static const char * const state_str[] = {
+	[FPGA_MGR_STATE_UNKNOWN] =		"unknown",
+	[FPGA_MGR_STATE_POWER_OFF] =		"power off",
+	[FPGA_MGR_STATE_POWER_UP] =		"power up",
+	[FPGA_MGR_STATE_RESET] =		"reset",
+
+	/* requesting FPGA image from firmware */
+	[FPGA_MGR_STATE_FIRMWARE_REQ] =		"firmware request",
+	[FPGA_MGR_STATE_FIRMWARE_REQ_ERR] =	"firmware request error",
+
+	/* Preparing FPGA to receive image */
+	[FPGA_MGR_STATE_WRITE_INIT] =		"write init",
+	[FPGA_MGR_STATE_WRITE_INIT_ERR] =	"write init error",
+
+	/* Writing image to FPGA */

drivers/fpga/fpga-mgr.c line 162

Quoted 10 lines
drivers/fpga/fpga-mgr.c new file +10 -0
+	[FPGA_MGR_STATE_WRITE] =		"write",
+	[FPGA_MGR_STATE_WRITE_ERR] =		"write error",
+
+	/* Finishing configuration after image has been written */
+	[FPGA_MGR_STATE_WRITE_COMPLETE] =	"write complete",
+	[FPGA_MGR_STATE_WRITE_COMPLETE_ERR] =	"write complete error",
+
+	/* FPGA reports to be in normal operating mode */
+	[FPGA_MGR_STATE_OPERATING] =		"operating",
+};

Is it really necessary to expose the whole FPGA manager state machine to userspace? Is the only justification "for debugging"?

If so, that seems pretty short-sighted, as it then makes the state machine part of the stable usermode API. It even makes less sense given this patchsets current state: configuration of the FPGA is not something that userspace is directly triggering.

Quoted 6 lines
drivers/fpga/fpga-mgr.c new file +6 -0
+
+static ssize_t name_show(struct device *dev,
+			 struct device_attribute *attr, char *buf)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+

drivers/fpga/fpga-mgr.c line 178

Quoted 10 lines
drivers/fpga/fpga-mgr.c new file +10 -0
+	return sprintf(buf, "%s\n", mgr->name);
+}
+
+static ssize_t state_show(struct device *dev,
+			  struct device_attribute *attr, char *buf)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	return sprintf(buf, "%s\n", state_str[mgr->state]);
+}

Is it possible that the state of the FPGA has changed since the last time we've done an update? Should the lower-level drivers' state() callback be invoked here?

Quoted 29 lines
drivers/fpga/fpga-mgr.c new file +29 -0
+
+static DEVICE_ATTR_RO(name);
+static DEVICE_ATTR_RO(state);
+
+static struct attribute *fpga_mgr_attrs[] = {
+	&dev_attr_name.attr,
+	&dev_attr_state.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(fpga_mgr);
+
+static int fpga_mgr_of_node_match(struct device *dev, const void *data)
+{
+	return dev->of_node == data;
+}
+
+/**
+ * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
+ * @node:	device node
+ *
+ * Given a device node, get an exclusive reference to a fpga mgr.
+ *
+ * Return: fpga manager struct or IS_ERR() condition containing error code.
+ */
+struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
+{
+	struct fpga_manager *mgr;
+	struct device *dev;
+

drivers/fpga/fpga-mgr.c line 217

Quoted 10 lines
drivers/fpga/fpga-mgr.c new file +10 -0
+	if (!node)
+		return ERR_PTR(-EINVAL);
+
+	dev = class_find_device(fpga_mgr_class, NULL, node,
+				fpga_mgr_of_node_match);
+	if (!dev)
+		return ERR_PTR(-ENODEV);
+
+	mgr = to_fpga_manager(dev);
+	put_device(dev);

Who's ensuring the FPGA manager device's lifetime between _get and _put?

Quoted 14 lines
drivers/fpga/fpga-mgr.c new file +14 -0
+	if (!mgr)
+		return ERR_PTR(-ENODEV);
+
+	/* Get exclusive use of fpga manager */
+	if (!mutex_trylock(&mgr->ref_mutex))
+		return ERR_PTR(-EBUSY);
+
+	if (!try_module_get(THIS_MODULE)) {
+		mutex_unlock(&mgr->ref_mutex);
+		return ERR_PTR(-ENODEV);
+	}
+
+	return mgr;
+}

[..]

Quoted 44 lines
drivers/fpga/fpga-mgr.c new file +44 -0
+int fpga_mgr_register(struct device *dev, const char *name,
+		      const struct fpga_manager_ops *mops,
+		      void *priv)
+{
+	struct fpga_manager *mgr;
+	const char *dt_label;
+	int id, ret;
+
+	if (!mops || !mops->write_init || !mops->write ||
+	    !mops->write_complete || !mops->state) {
+		dev_err(dev, "Attempt to register without fpga_manager_ops\n");
+		return -EINVAL;
+	}
+
+	if (!name || !strlen(name)) {
+		dev_err(dev, "Attempt to register with no name!\n");
+		return -EINVAL;
+	}
+
+	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
+	if (!mgr)
+		return -ENOMEM;
+
+	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
+	if (id < 0) {
+		ret = id;
+		goto error_kfree;
+	}
+
+	mutex_init(&mgr->ref_mutex);
+
+	mgr->name = name;
+	mgr->mops = mops;
+	mgr->priv = priv;
+
+	/*
+	 * Initialize framework state by requesting low level driver read state
+	 * from device.  FPGA may be in reset mode or may have been programmed
+	 * by bootloader or EEPROM.
+	 */
+	mgr->state = mgr->mops->state(mgr);
+
+	device_initialize(&mgr->dev);
+	mgr->dev.class = fpga_mgr_class;

drivers/fpga/fpga-mgr.c line 309

Quoted 10 lines
drivers/fpga/fpga-mgr.c new file +10 -0
+	mgr->dev.parent = dev;
+	mgr->dev.of_node = dev->of_node;
+	mgr->dev.id = id;
+	dev_set_drvdata(dev, mgr);
+
+	dt_label = of_get_property(mgr->dev.of_node, "label", NULL);
+	if (dt_label)
+		ret = dev_set_name(&mgr->dev, "%s", dt_label);
+	else
+		ret = dev_set_name(&mgr->dev, "fpga%d", id);

I'm wondering if an alias {} node is better for this.

[..]

Quoted 1 line

+++ b/include/linux/fpga/fpga-mgr.h

[..]

include/linux/fpga/fpga-mgr.h line 74

Quoted 10 lines
include/linux/fpga/fpga-mgr.h new file +10 -0
+/*
+ * FPGA Manager flags
+ * FPGA_MGR_PARTIAL_RECONFIG: do partial reconfiguration if supported
+ */
+#define FPGA_MGR_PARTIAL_RECONFIG	BIT(0)
+
+/**
+ * struct fpga_manager_ops - ops for low level fpga manager drivers
+ * @state: returns an enum value of the FPGA's state
+ * @write_init: prepare the FPGA to receive confuration data

^configuration

Quoted 3 lines

+ * @write: write count bytes of configuration data to the FPGA
+ * @write_complete: set FPGA to operating state after writing is done
+ * @fpga_remove: optional: Set FPGA into a specific state during driver remove

Any specific state? State in the FPGA-manager-state-machine case? Or a basic 'reset' state?

Josh

#7 lore raw

Re: [PATCH v11 3/4] add FPGA manager core

Hi!

Quoted 15 lines
Quoted 2 lines

+++ b/drivers/fpga/fpga-mgr.c

(quoted diff) +0 -0
@@ -0,0 +1,382 @@

[..]

drivers/fpga/fpga-mgr.c line 52

Quoted 8 lines
drivers/fpga/fpga-mgr.c new file +8 -0
+int fpga_mgr_buf_load(struct fpga_manager *mgr, u32 flags, const char *buf,
+		      size_t count)
+{
+	struct device *dev = &mgr->dev;
+	int ret;
+
+	if (!mgr)
+		return -ENODEV;

This seems overly defensive.

[..]

Quoted 11 lines

drivers/fpga/fpga-mgr.c line 115

Quoted 9 lines
drivers/fpga/fpga-mgr.c new file +9 -0
+int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,
+			   const char *image_name)
+{
+	struct device *dev = &mgr->dev;
+	const struct firmware *fw;
+	int ret;
+
+	if (!mgr)
+		return -ENODEV;

Again; I'm of the opinion this is needlessly defensive.

Not only that, it can never happen. mgr is already dereferenced above.

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#8 lore raw

Re: [PATCH v11 3/4] add FPGA manager core

Quoted 12 lines · Pavel Machek
Quoted 11 lines

drivers/fpga/fpga-mgr.c line 115

Quoted 9 lines
drivers/fpga/fpga-mgr.c new file +9 -0
+int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,
+			   const char *image_name)
+{
+	struct device *dev = &mgr->dev;
+	const struct firmware *fw;
+	int ret;
+
+	if (!mgr)
+		return -ENODEV;

Again; I'm of the opinion this is needlessly defensive.

Quoted 2 lines

Not only that, it can never happen. mgr is already dereferenced above.

It's not dereferenced. We're taking the address of mgr->dev but we don't dereference mgr.

regards,
dan carpenter

author
#9 lore raw

Re: [PATCH v11 3/4] add FPGA manager core

Quoted 13 lines · Dan Carpenter
Quoted 12 lines · Pavel Machek
Quoted 11 lines

drivers/fpga/fpga-mgr.c line 115

Quoted 9 lines
drivers/fpga/fpga-mgr.c new file +9 -0
+int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,
+			   const char *image_name)
+{
+	struct device *dev = &mgr->dev;
+	const struct firmware *fw;
+	int ret;
+
+	if (!mgr)
+		return -ENODEV;

Again; I'm of the opinion this is needlessly defensive.

Quoted 10 lines
Quoted 2 lines

Not only that, it can never happen. mgr is already dereferenced above.

It's not dereferenced. We're taking the address of mgr->dev but we don't dereference mgr.

regards,
dan carpenter

That's correct, it's not dereferenced.

Is there some community agreement on whether we want to check a pointer that has been passed for NULL or not? This is C code after all. Checking a passed pointer for NULL is a very common reason to return -ENODEV.

Alan

#10 lore raw

Re: [PATCH v11 3/4] add FPGA manager core

Of course, the maintainer gets the last word regardless of what anyone else thinks.

Generally, minimal code is better. Trying to future proof code is a waste of time because you can't predict what will happen in the future. It's way more likely that some pointer you never expected to be NULL will be NULL instead of the few checked at the beginning of a function. Adding useless code uses RAM and makes the function slower. It's a bit confusing for users as well because they will wonder when the NULL check is used. A lot of times this sort of error handling is a bit fake and what I mean is that it looks correct but the system will just crash in a later function.

Also especially with a simple NULL dereferences like this theoretical one, it's better to just get the oops. It kills the module but you get a good message in the log and it's normally straight forward to debug.

We spent a surprising amount of time discussing useless code. I made someone redo a patch yesterday because they had incomplete error handling for a situation which could never happen.

regards,
dan carpenter

author
#11 lore raw

Re: [PATCH v11 3/4] add FPGA manager core

Quoted 17 lines · Dan Carpenter

Of course, the maintainer gets the last word regardless of what anyone else thinks.

Generally, minimal code is better. Trying to future proof code is a waste of time because you can't predict what will happen in the future. It's way more likely that some pointer you never expected to be NULL will be NULL instead of the few checked at the beginning of a function. Adding useless code uses RAM and makes the function slower. It's a bit confusing for users as well because they will wonder when the NULL check is used. A lot of times this sort of error handling is a bit fake and what I mean is that it looks correct but the system will just crash in a later function.

Also especially with a simple NULL dereferences like this theoretical one, it's better to just get the oops. It kills the module but you get a good message in the log and it's normally straight forward to debug.

Quoted 8 lines

We spent a surprising amount of time discussing useless code. I made someone redo a patch yesterday because they had incomplete error handling for a situation which could never happen.

regards,
dan carpenter

Thanks for the discussion.

Interesting. The amount of code bloat here compiles down to about two machine instructions (in two places). Actually a little more since I should be using IS_ERR_OR_NULL. But the main question is whether I should do it at all.

The behaviour I should drive here is that the user will do their own error checking. After they get a pointer to a FPGA manager using of_fpga_mgr_get(), they should check it and not assume that fpga_mgr_firmware_load() will do it for them, i.e.

	mgr = of_fpga_mgr_get(mgr_node);
	if (IS_ERR(mgr))
		return PTR_ERR(mgr);
	fpga_mgr_firmware_load(mgr, flags, path);

I could take out these NULL pointer checks and it won't hurt anything unless someone is just using the functions badly, in which case: kablooey.

Alan

#12 lore raw

Re: [PATCH v11 3/4] add FPGA manager core

Hi!

Quoted 33 lines
Quoted 13 lines

Of course, the maintainer gets the last word regardless of what anyone else thinks.

Generally, minimal code is better. Trying to future proof code is a waste of time because you can't predict what will happen in the future. It's way more likely that some pointer you never expected to be NULL will be NULL instead of the few checked at the beginning of a function. Adding useless code uses RAM and makes the function slower. It's a bit confusing for users as well because they will wonder when the NULL check is used. A lot of times this sort of error handling is a bit fake and what I mean is that it looks correct but the system will just crash in a later function.

Quoted 7 lines

Also especially with a simple NULL dereferences like this theoretical one, it's better to just get the oops. It kills the module but you get a good message in the log and it's normally straight forward to debug.

We spent a surprising amount of time discussing useless code. I made someone redo a patch yesterday because they had incomplete error handling for a situation which could never happen.

Thanks for the discussion.

Interesting. The amount of code bloat here compiles down to about two machine instructions (in two places). Actually a little more since I should be using IS_ERR_OR_NULL. But the main question is whether I should do it at all.

The behaviour I should drive here is that the user will do their own error checking. After they get a pointer to a FPGA manager using of_fpga_mgr_get(), they should check it and not assume that fpga_mgr_firmware_load() will do it for them, i.e.

Quoted 7 lines
	mgr = of_fpga_mgr_get(mgr_node);
	if (IS_ERR(mgr))
		return PTR_ERR(mgr);
	fpga_mgr_firmware_load(mgr, flags, path);

I could take out these NULL pointer checks and it won't hurt anything unless someone is just using the functions badly, in which case: kablooey.

2 instructions is not that bad, do whatever is easier for you. These patches received enough bikeshed painting.

Best regards,
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#13 lore raw

Re: [PATCH v11 3/4] add FPGA manager core

In reply to atull
Quoted 5 lines · atull

Interesting. The amount of code bloat here compiles down to about two machine instructions (in two places). Actually a little more since I should be using IS_ERR_OR_NULL. But the main question is whether I should do it at all.

They kernel already has too many bogus checks for IS_ERR(). It's a very common bug to check for IS_ERR() when you should be checking for NULL.

- foo = some_allocator();

+	foo = kmalloc();
	if (IS_ERR(foo))
		goto fail;

I have a static checker for "warn: 'foo' isn't an ERR_PTR" but I haven't published it because too much code has impossible checks.

Quoted 10 lines

The behaviour I should drive here is that the user will do their own error checking. After they get a pointer to a FPGA manager using of_fpga_mgr_get(), they should check it and not assume that fpga_mgr_firmware_load() will do it for them, i.e.

	mgr = of_fpga_mgr_get(mgr_node);
	if (IS_ERR(mgr))
		return PTR_ERR(mgr);
	fpga_mgr_firmware_load(mgr, flags, path);

I don't understand completely how of_fpga_mgr_get() ever returns NULL. A lot of the of_ functions return ERR_PTRs if OF_ is compiled in but they return NULL if it's not. I think this is so people can build with COMPILE_TEST so we get more coverage with static analysis?

Quoted 2 lines

I could take out these NULL pointer checks and it won't hurt anything unless someone is just using the functions badly, in which case: kablooey.

Linux devs are very good about doing error checking. An early kablooey is what we want for people who don't.

Also if you provide a sanity check then Markus Elfring will remove all the error checking in the callers. Don't do it.

regards,
dan carpenter

author
#14 lore raw

Re: [PATCH v11 3/4] add FPGA manager core

In reply to Josh Cartwright

Hi Josh,

Thanks for the review. This is all at the tail end of a long (>2 years) discussion on this. I hope that the way this has shaped out still meets the needs of the people who have been in this discussion the most and have had the strongest feelings (due to being current users of FPGAs under Linux).

Quoted 7 lines · Josh Cartwright
Quoted 4 lines

The following sysfs files are created:
* /sys/class/fpga_manager/<fpga>/name
Name of low level driver.

Don't 'struct device's already have a name? Why do you need another name attribute?

It's a nicety but not a necessity. And it's not the only kernel framework that has human readable names like that. Look at i2c for one example. Nobody's complained about this one before but I guess it's not absolutely needed.

Quoted 30 lines
Quoted 16 lines
drivers/fpga/fpga-mgr.c new file +16 -0
+
+static const char * const state_str[] = {
+	[FPGA_MGR_STATE_UNKNOWN] =		"unknown",
+	[FPGA_MGR_STATE_POWER_OFF] =		"power off",
+	[FPGA_MGR_STATE_POWER_UP] =		"power up",
+	[FPGA_MGR_STATE_RESET] =		"reset",
+
+	/* requesting FPGA image from firmware */
+	[FPGA_MGR_STATE_FIRMWARE_REQ] =		"firmware request",
+	[FPGA_MGR_STATE_FIRMWARE_REQ_ERR] =	"firmware request error",
+
+	/* Preparing FPGA to receive image */
+	[FPGA_MGR_STATE_WRITE_INIT] =		"write init",
+	[FPGA_MGR_STATE_WRITE_INIT_ERR] =	"write init error",
+
+	/* Writing image to FPGA */

drivers/fpga/fpga-mgr.c line 162

Quoted 10 lines
drivers/fpga/fpga-mgr.c new file +10 -0
+	[FPGA_MGR_STATE_WRITE] =		"write",
+	[FPGA_MGR_STATE_WRITE_ERR] =		"write error",
+
+	/* Finishing configuration after image has been written */
+	[FPGA_MGR_STATE_WRITE_COMPLETE] =	"write complete",
+	[FPGA_MGR_STATE_WRITE_COMPLETE_ERR] =	"write complete error",
+
+	/* FPGA reports to be in normal operating mode */
+	[FPGA_MGR_STATE_OPERATING] =		"operating",
+};

Is it really necessary to expose the whole FPGA manager state machine to userspace? Is the only justification "for debugging"?

Quoted 4 lines

If so, that seems pretty short-sighted, as it then makes the state machine part of the stable usermode API. It even makes less sense given this patchsets current state: configuration of the FPGA is not something that userspace is directly triggering.

Nope, not for debugging.

This feature was requested two years ago by folks who have userspace bringing up a complicated system in FPGAs under Linux.

https://lkml.org/lkml/2013/9/18/490

There are likely to be interfaces that can be triggered by userspace. Please look at my last patchset for one example: using device tree overlays. That can be triggered from userspace. You could have a layered system where userspace loads one DT overlay (which triggers FPGA programming and drivers probing), checks for success, then loads the next
DT overlay to program the next FPGA in the system. In this case the userspace interface is the configfs interface for DT overlays so I would not be able to add FPGA manager status to that interface. The only status I would have would be in the configfs, indicating whether the overlay got applied successfully or not.

Quoted 13 lines
Quoted 13 lines
drivers/fpga/fpga-mgr.c new file +12 -0
+
+static ssize_t name_show(struct device *dev,
+			 struct device_attribute *attr, char *buf)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	return sprintf(buf, "%s\n", mgr->name);
+}
+
+static ssize_t state_show(struct device *dev,
+			  struct device_attribute *attr, char *buf)
+{
Quoted 8 lines

drivers/fpga/fpga-mgr.c line 178

Quoted 4 lines
drivers/fpga/fpga-mgr.c new file +4 -0
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	return sprintf(buf, "%s\n", state_str[mgr->state]);
+}

Is it possible that the state of the FPGA has changed since the last time we've done an update? Should the lower-level drivers' state() callback be invoked here?

Exposing the low level driver's state is not exactly the intent here. Above I commented further on the intent of exposing state. I want to expose how far the FPGA manager core's state machine gets in the process of programming. So if we can't get the FPGA into a state ready for receiving data, it will be "write init error", for instance. If we can't load the specific firmware file we wanted, it's "firmware request error."

Quoted 34 lines
Quoted 34 lines
drivers/fpga/fpga-mgr.c new file +33 -0
+
+static DEVICE_ATTR_RO(name);
+static DEVICE_ATTR_RO(state);
+
+static struct attribute *fpga_mgr_attrs[] = {
+	&dev_attr_name.attr,
+	&dev_attr_state.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(fpga_mgr);
+
+static int fpga_mgr_of_node_match(struct device *dev, const void *data)
+{
+	return dev->of_node == data;
+}
+
+/**
+ * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
+ * @node:	device node
+ *
+ * Given a device node, get an exclusive reference to a fpga mgr.
+ *
+ * Return: fpga manager struct or IS_ERR() condition containing error code.
+ */
+struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
+{
+	struct fpga_manager *mgr;
+	struct device *dev;
+
+	if (!node)
+		return ERR_PTR(-EINVAL);
+
+	dev = class_find_device(fpga_mgr_class, NULL, node,
Quoted 8 lines

drivers/fpga/fpga-mgr.c line 217

Quoted 6 lines
drivers/fpga/fpga-mgr.c new file +6 -0
+				fpga_mgr_of_node_match);
+	if (!dev)
+		return ERR_PTR(-ENODEV);
+
+	mgr = to_fpga_manager(dev);
+	put_device(dev);

Who's ensuring the FPGA manager device's lifetime between _get and _put?

Well... get_device and put_device are not sufficient?

Quoted 64 lines
Quoted 15 lines
drivers/fpga/fpga-mgr.c new file +14 -0
+	if (!mgr)
+		return ERR_PTR(-ENODEV);
+
+	/* Get exclusive use of fpga manager */
+	if (!mutex_trylock(&mgr->ref_mutex))
+		return ERR_PTR(-EBUSY);
+
+	if (!try_module_get(THIS_MODULE)) {
+		mutex_unlock(&mgr->ref_mutex);
+		return ERR_PTR(-ENODEV);
+	}
+
+	return mgr;
+}

[..]

Quoted 48 lines
drivers/fpga/fpga-mgr.c new file +48 -0
+int fpga_mgr_register(struct device *dev, const char *name,
+		      const struct fpga_manager_ops *mops,
+		      void *priv)
+{
+	struct fpga_manager *mgr;
+	const char *dt_label;
+	int id, ret;
+
+	if (!mops || !mops->write_init || !mops->write ||
+	    !mops->write_complete || !mops->state) {
+		dev_err(dev, "Attempt to register without fpga_manager_ops\n");
+		return -EINVAL;
+	}
+
+	if (!name || !strlen(name)) {
+		dev_err(dev, "Attempt to register with no name!\n");
+		return -EINVAL;
+	}
+
+	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
+	if (!mgr)
+		return -ENOMEM;
+
+	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
+	if (id < 0) {
+		ret = id;
+		goto error_kfree;
+	}
+
+	mutex_init(&mgr->ref_mutex);
+
+	mgr->name = name;
+	mgr->mops = mops;
+	mgr->priv = priv;
+
+	/*
+	 * Initialize framework state by requesting low level driver read state
+	 * from device.  FPGA may be in reset mode or may have been programmed
+	 * by bootloader or EEPROM.
+	 */
+	mgr->state = mgr->mops->state(mgr);
+
+	device_initialize(&mgr->dev);
+	mgr->dev.class = fpga_mgr_class;
+	mgr->dev.parent = dev;
+	mgr->dev.of_node = dev->of_node;
+	mgr->dev.id = id;
+	dev_set_drvdata(dev, mgr);
Quoted 8 lines

drivers/fpga/fpga-mgr.c line 309

Quoted 6 lines
drivers/fpga/fpga-mgr.c new file +6 -0
+
+	dt_label = of_get_property(mgr->dev.of_node, "label", NULL);
+	if (dt_label)
+		ret = dev_set_name(&mgr->dev, "%s", dt_label);
+	else
+		ret = dev_set_name(&mgr->dev, "fpga%d", id);

I'm wondering if an alias {} node is better for this.

I could look into that. Is there an example of that you particularly like for this?

Quoted 4 lines

[..]

Quoted 1 line

+++ b/include/linux/fpga/fpga-mgr.h

[..]

Quoted 12 lines

include/linux/fpga/fpga-mgr.h line 74

Quoted 10 lines
include/linux/fpga/fpga-mgr.h new file +10 -0
+/*
+ * FPGA Manager flags
+ * FPGA_MGR_PARTIAL_RECONFIG: do partial reconfiguration if supported
+ */
+#define FPGA_MGR_PARTIAL_RECONFIG	BIT(0)
+
+/**
+ * struct fpga_manager_ops - ops for low level fpga manager drivers
+ * @state: returns an enum value of the FPGA's state
+ * @write_init: prepare the FPGA to receive confuration data

^configuration

Ah spelling. Thanks!

Quoted 7 lines
Quoted 4 lines

+ * @write: write count bytes of configuration data to the FPGA
+ * @write_complete: set FPGA to operating state after writing is done
+ * @fpga_remove: optional: Set FPGA into a specific state during driver remove

Any specific state? State in the FPGA-manager-state-machine case? Or a basic 'reset' state?

Could be reset. Leaving that one open for the person implemening the low level driver.

Quoted 3 lines

Josh

#15 lore raw

Re: [PATCH v11 3/4] add FPGA manager core

Quoted 1 line · atull

On Tue, 22 Sep 2015, Josh Cartwright wrote:

[..]

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/fpga/fpga-mgr.c new file +9 -0
+struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
+{
+	struct fpga_manager *mgr;
+	struct device *dev;
+
+	if (!node)
+		return ERR_PTR(-EINVAL);
+
+	dev = class_find_device(fpga_mgr_class, NULL, node,
Quoted 8 lines

drivers/fpga/fpga-mgr.c line 217

Quoted 6 lines
drivers/fpga/fpga-mgr.c new file +6 -0
+				fpga_mgr_of_node_match);
+	if (!dev)
+		return ERR_PTR(-ENODEV);
+
+	mgr = to_fpga_manager(dev);
+	put_device(dev);

Who's ensuring the FPGA manager device's lifetime between _get and _put?

Quoted 1 line

Well... get_device and put_device are not sufficient?

Sorry if I was too opaque.

You've just put_device()'d the only reference to the device you had here, so nothing is preventing from the device from being ripped away while it's being used.

You should remove the put_device() call here, and add a corresponding put_device() in fpga_mgr_put().

The module refcounting is also a bit strange, as you are acquiring/releasing a reference to THIS_MODULE, but you also should care about the lower-level driver's module refcount.

[..]

Quoted 10 lines
Quoted 7 lines

drivers/fpga/fpga-mgr.c line 309

Quoted 5 lines
drivers/fpga/fpga-mgr.c new file +5 -0
+	dt_label = of_get_property(mgr->dev.of_node, "label", NULL);
+	if (dt_label)
+		ret = dev_set_name(&mgr->dev, "%s", dt_label);
+	else
+		ret = dev_set_name(&mgr->dev, "fpga%d", id);

I'm wondering if an alias {} node is better for this.

I could look into that. Is there an example of that you particularly like for this?

Look at i2c's usage of the of_alias_*() functions.

Josh

_______________________________________________
devel mailing list
[email protected]
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

author
#16 lore raw

Re: [PATCH v11 3/4] add FPGA manager core

Quoted 29 lines · Josh Cartwright
Quoted 1 line · atull

On Tue, 22 Sep 2015, Josh Cartwright wrote:

[..]

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/fpga/fpga-mgr.c new file +9 -0
+struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
+{
+	struct fpga_manager *mgr;
+	struct device *dev;
+
+	if (!node)
+		return ERR_PTR(-EINVAL);
+
+	dev = class_find_device(fpga_mgr_class, NULL, node,
Quoted 8 lines

drivers/fpga/fpga-mgr.c line 217

Quoted 6 lines
drivers/fpga/fpga-mgr.c new file +6 -0
+				fpga_mgr_of_node_match);
+	if (!dev)
+		return ERR_PTR(-ENODEV);
+
+	mgr = to_fpga_manager(dev);
+	put_device(dev);

Who's ensuring the FPGA manager device's lifetime between _get and _put?

Quoted 1 line

Well... get_device and put_device are not sufficient?

Sorry if I was too opaque.

You've just put_device()'d the only reference to the device you had here, so nothing is preventing from the device from being ripped away while it's being used.

Quoted 7 lines

You should remove the put_device() call here, and add a corresponding put_device() in fpga_mgr_put().

The module refcounting is also a bit strange, as you are acquiring/releasing a reference to THIS_MODULE, but you also should care about the lower-level driver's module refcount.

Good call. Thanks for the clarification. I'll have something more robust in v12.

Quoted 9 lines

[..]

Quoted 8 lines
Quoted 7 lines

drivers/fpga/fpga-mgr.c line 309

Quoted 5 lines
drivers/fpga/fpga-mgr.c new file +5 -0
+	dt_label = of_get_property(mgr->dev.of_node, "label", NULL);
+	if (dt_label)
+		ret = dev_set_name(&mgr->dev, "%s", dt_label);
+	else
+		ret = dev_set_name(&mgr->dev, "fpga%d", id);

I'm wondering if an alias {} node is better for this.

Quoted 4 lines
Quoted 2 lines

I could look into that. Is there an example of that you particularly like for this?

Look at i2c's usage of the of_alias_*() functions.

OK yes, will use that in the next version.

Thanks for the review,
Alan

Quoted 3 lines

Josh

#17 lore raw

PATCH v11 4/4 fpga manager: add driver for socfpga fpga manager

In reply to [email protected]

From: Alan Tull <[email protected]>

Add driver to fpga manager framework to allow configuration of FPGA in Altera SoCFPGA parts.

---
v2: fpga_manager struct now contains struct device
fpga_manager_register parameters now take device

v3: skip a version to align versions

v4: move to drivers/staging

v5: fix array_size.cocci warnings
fix platform_no_drv_owner.cocci warnings
Remove .owner = THIS_MODULE
include asm/irq.h
clean up list of includes
use altera_fpga_reset for ops
use enum fpga_mgr_states or u32 as needed
use devm_request_irq
check irq <= 0 instead of == NO_IRQ
Use ARRAY_SIZE
rename altera -> socfpga
static const socfpga_fpga_ops
header moved to linux/fpga/ folder
remove ifdef'ed code
use platform_get_resource and platform_get_irq move .probe and .remove lines adjacent
use module_platform_driver
use __maybe_unused
only need to 'if (IS_ENABLED(CONFIG_REGULATOR))' in one fn fix "unsigned 'mode' is never < 0"

v6: return error for (unused) default of case statement

v7: PTR_ERR should access the value just tested by IS_ERR

v8: change compatible string to be more chip specific

v9: update copyright year
remove suspend/resume support and regulators use updated fn names for register/unregister use updated params for write_init
check for partial reconfiguration request

v10: make tristate in Kconfig
clean up include
add flags param to write_complete()
move do drivers/fpga

v11: No code change in this patch for v11 of the patch set
Adding Moritz' ACK

---
 drivers/fpga/Kconfig   |   10 +
 drivers/fpga/Makefile  |    1 +
 drivers/fpga/socfpga.c |  616 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 627 insertions(+)
 create mode 100644 drivers/fpga/socfpga.c
drivers/fpga/Kconfig +10 -0
index f1f1f6d..dfc1f1e 100644
@@ -11,4 +11,14 @@ config FPGA
 	  kernel.  The FPGA framework adds a FPGA manager class and FPGA
 	  manager drivers.
 
+if FPGA
+
+config FPGA_MGR_SOCFPGA
+	tristate "Altera SOCFPGA FPGA Manager"
+	depends on ARCH_SOCFPGA
+	help
+	  FPGA manager driver support for Altera SOCFPGA.
+
+endif # FPGA
+
 endmenu
drivers/fpga/Makefile +1 -0
index 3313c52..ba6c5c5 100644
@@ -6,3 +6,4 @@
 obj-$(CONFIG_FPGA)			+= fpga-mgr.o
 
 # FPGA Manager Drivers
+obj-$(CONFIG_FPGA_MGR_SOCFPGA)		+= socfpga.o
drivers/fpga/socfpga.c new file +616 -0

616 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#18 lore raw

Re: [PATCH v11 4/4] fpga manager: add driver for socfpga fpga manager

Quoted 8 lines · On Tue, Sep 22, 2015 at 10:21:11AM -0500, [email protected] wrote:

From: Alan Tull <[email protected]>

Add driver to fpga manager framework to allow configuration of FPGA in Altera SoCFPGA parts.

[..]

drivers/fpga/Kconfig config FPGA · line 14

Quoted 6 lines

+++ b/drivers/fpga/Kconfig

drivers/fpga/Kconfig +1 -0
@@ -11,4 +11,14 @@ config FPGA
 	  kernel.  The FPGA framework adds a FPGA manager class and FPGA
 	  manager drivers.
 
+if FPGA

FPGA is unconditionally set here, otherwise drivers/fpga/Kconfig wouldn't even be considered.

Quoted 24 lines
drivers/fpga/Kconfig +9 -0
+
+config FPGA_MGR_SOCFPGA
+	tristate "Altera SOCFPGA FPGA Manager"
+	depends on ARCH_SOCFPGA
+	help
+	  FPGA manager driver support for Altera SOCFPGA.
+
+endif # FPGA
+
 endmenu
drivers/fpga/Makefile +1 -0
index 3313c52..ba6c5c5 100644
@@ -6,3 +6,4 @@
 obj-$(CONFIG_FPGA)			+= fpga-mgr.o
 
 # FPGA Manager Drivers
+obj-$(CONFIG_FPGA_MGR_SOCFPGA)		+= socfpga.o
drivers/fpga/socfpga.c new file +0 -0
new file mode 100644
index 0000000..706b80d

[..]

drivers/fpga/socfpga.c line 447

Quoted 8 lines
drivers/fpga/socfpga.c new file +8 -0
+/*
+ * Step 9: write data to the FPGA data register
+ */
+static int socfpga_fpga_ops_configure_write(struct fpga_manager *mgr,
+					    const char *buf, size_t count)
+{
+	struct socfpga_fpga_priv *priv = mgr->priv;
+	u32 *buffer_32 = (u32 *)buf;

Seems sketchy from an endianess perspective, but it may be okay if SOCFPGA doesn't support BE (which my follow up question would be: why not?). Same thing applies with seemingly cavalier usages of the __raw_readl/writel variants.

Josh

_______________________________________________
devel mailing list
[email protected]
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel