FPGAs
Single-page view · as markdown
FPGA Subsystem
Peios Kernel / Hardware and drivers / Driver development / FPGAs
Author: Alan Tull
FPGA Bridge
Peios Kernel / Hardware and drivers / Driver development / FPGAs
API to implement a new FPGA bridge #
-
struct fpga_bridge - The FPGA Bridge structure
-
struct fpga_bridge_ops - Low level Bridge driver ops
-
__fpga_bridge_register() - Create and register a bridge
-
fpga_bridge_unregister() - Unregister a bridge
The helper macro fpga_bridge_register() automatically sets the module that registers the FPGA bridge as the owner.
struct fpga_bridge #
FPGA bridge structure
Definition
;
Members
name: name of low level FPGA bridgedev: FPGA bridge devicemutex: enforces exclusive reference to bridgebr_ops: pointer to struct of FPGA bridge opsbr_ops_owner: module containing the br_opsinfo: fpga image specific informationnode: FPGA bridge list nodepriv: low level driver private date
struct fpga_bridge_ops #
ops for low level FPGA bridge drivers
Definition
;
Members
enable_show: returns the FPGA bridge's statusenable_set: set an FPGA bridge as enabled or disabledfpga_bridge_remove: set FPGA into a specific state during driver removegroups: optional attribute groups.
__fpga_bridge_register() #
struct fpga_bridge * create and register an FPGA Bridge device
Parameters
struct device *parent: FPGA bridge device from pdevconst char *name: FPGA bridge nameconst struct fpga_bridge_ops *br_ops: pointer to structure of fpga bridge opsvoid *priv: FPGA bridge private datastruct module *owner: owner module containing the br_ops
Return
struct fpga_bridge pointer or ERR_PTR()
fpga_bridge_unregister() #
void unregister an FPGA bridge
Parameters
struct fpga_bridge *bridge: FPGA bridge struct
Description
This function is intended for use in an FPGA bridge driver's remove function.
FPGA Manager
Peios Kernel / Hardware and drivers / Driver development / FPGAs
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.
The FPGA image to be programmed can be in a scatter gather list, a single contiguous buffer, or a firmware file. Because allocating contiguous kernel memory for the buffer should be avoided, users are encouraged to use a scatter gather list instead if possible.
The particulars for programming the image are presented in a structure (struct fpga_image_info). This struct contains parameters such as pointers to the FPGA image as well as image-specific particulars such as whether the image was built for full or partial reconfiguration.
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() or fpga_mgr_register_full(), 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;
struct fpga_manager *mgr;
int ret;
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
/*
* do ioremaps, get interrupts, etc. and save
* them in priv
*/
mgr = fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
&socfpga_fpga_ops, priv);
if (IS_ERR(mgr))
return PTR_ERR(mgr);
platform_set_drvdata(pdev, mgr);
return 0;
}
static int socfpga_fpga_remove(struct platform_device *pdev)
{
struct fpga_manager *mgr = platform_get_drvdata(pdev);
fpga_mgr_unregister(mgr);
return 0;
}
Alternatively, the probe function could call one of the resource managed register functions, devm_fpga_mgr_register() or devm_fpga_mgr_register_full(). When these functions are used, the parameter syntax is the same, but the call to fpga_mgr_unregister() should be removed. In the above example, the socfpga_fpga_remove() function would not be required.
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::
-
.parse_header (optional, may be called once or multiple times)
-
.write_init
-
.write or .write_sg (may be called once or multiple times)
-
.write_complete
-
The .parse_header function will set header_size and data_size to struct fpga_image_info. Before parse_header call, header_size is initialized with initial_header_size. If flag skip_header of fpga_manager_ops is true, .write function will get image buffer starting at header_size offset from the beginning. If data_size is set, .write function will get data_size bytes of the image buffer, otherwise .write will get data up to the end of image buffer. This will not affect .write_sg, .write_sg will still get whole image in sg_table form. If FPGA image is already mapped as a single contiguous buffer, whole buffer will be passed into .parse_header. If image is in scatter-gather form, core code will buffer up at least .initial_header_size before the first call of .parse_header, if it is not enough, .parse_header should set desired size into info->header_size and return -EAGAIN, then it will be called again with greater part of image buffer on the input.
The .write_init function will prepare the FPGA to receive the image data. The buffer passed into .write_init will be at least info->header_size bytes long; if the whole bitstream is not immediately available then the core code will buffer up at least this much before starting.
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. This interface is suitable for drivers which use PIO.
The .write_sg version behaves the same as .write except the input is a sg_table scatter list. This interface is suitable for drivers which use DMA.
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 determine the state the FPGA is in and return a code of type enum fpga_mgr_states. It doesn't result in a change in state.
API for implementing a new FPGA Manager driver #
-
fpga_mgr_states- Values forfpga_manager->state. -
struct fpga_manager - the FPGA manager struct
-
struct fpga_manager_ops - Low level FPGA manager driver ops
-
struct fpga_manager_info - Parameter structure for fpga_mgr_register_full()
-
__fpga_mgr_register_full() - Create and register an FPGA manager using the fpga_mgr_info structure to provide the full flexibility of options
-
__fpga_mgr_register() - Create and register an FPGA manager using standard arguments
-
__devm_fpga_mgr_register_full() - Resource managed version of __fpga_mgr_register_full()
-
__devm_fpga_mgr_register() - Resource managed version of __fpga_mgr_register()
-
fpga_mgr_unregister() - Unregister an FPGA manager
Helper macros fpga_mgr_register_full(), fpga_mgr_register(), devm_fpga_mgr_register_full(), and devm_fpga_mgr_register() are available to ease the registration.
enum fpga_mgr_states #
fpga framework states
Constants
FPGA_MGR_STATE_UNKNOWN: can't determine stateFPGA_MGR_STATE_POWER_OFF: FPGA power is offFPGA_MGR_STATE_POWER_UP: FPGA reports power is upFPGA_MGR_STATE_RESET: FPGA in reset stateFPGA_MGR_STATE_FIRMWARE_REQ: firmware request in progressFPGA_MGR_STATE_FIRMWARE_REQ_ERR: firmware request failedFPGA_MGR_STATE_PARSE_HEADER: parse FPGA image headerFPGA_MGR_STATE_PARSE_HEADER_ERR: Error during PARSE_HEADER stageFPGA_MGR_STATE_WRITE_INIT: preparing FPGA for programmingFPGA_MGR_STATE_WRITE_INIT_ERR: Error during WRITE_INIT stageFPGA_MGR_STATE_WRITE: writing image to FPGAFPGA_MGR_STATE_WRITE_ERR: Error while writing FPGAFPGA_MGR_STATE_WRITE_COMPLETE: Doing post programming stepsFPGA_MGR_STATE_WRITE_COMPLETE_ERR: Error during WRITE_COMPLETEFPGA_MGR_STATE_OPERATING: FPGA is programmed and operating
struct fpga_manager #
fpga manager structure
Definition
;
Members
name: name of low level fpga managerdev: fpga manager deviceref_mutex: only allows one reference to fpga managerstate: state of fpga managercompat_id: FPGA manager id for compatibility check.mops: pointer to struct of fpga manager opsmops_owner: module containing the mopspriv: low level driver private date
struct fpga_manager_ops #
ops for low level fpga manager drivers
Definition
;
Members
initial_header_size: minimum number of bytes that should be passed into parse_header and write_init.skip_header: bool flag to tell fpga-mgr core whether it should skip info->header_size part at the beginning of the image when invoking write callback.state: returns an enum value of the FPGA's statestatus: returns status of the FPGA, including reconfiguration error codeparse_header: parse FPGA image header to set info->header_size and info->data_size. In case the input buffer is not large enough, set required size to info->header_size and return -EAGAIN.write_init: prepare the FPGA to receive configuration datawrite: write count bytes of configuration data to the FPGAwrite_sg: write the scatter list of configuration data to the FPGAwrite_complete: set FPGA to operating state after writing is donefpga_remove: optional: Set FPGA into a specific state during driver removegroups: optional attribute groups.
Description
fpga_manager_ops are the low level functions implemented by a specific fpga manager driver. The optional ones are tested for NULL before being called, so leaving them out is fine.
struct fpga_manager_info #
collection of parameters for an FPGA Manager
Definition
;
Members
name: fpga manager namecompat_id: FPGA manager id for compatibility check.mops: pointer to structure of fpga manager opspriv: fpga manager private data
Description
fpga_manager_info contains parameters for the register_full function. These are separated into an info structure because they some are optional others could be added to in the future. The info structure facilitates maintaining a stable API.
__fpga_mgr_register_full() #
struct fpga_manager * create and register an FPGA Manager device
Parameters
struct device *parent: fpga manager device from pdevconst struct fpga_manager_info *info: parameters for fpga managerstruct module *owner: owner module containing the ops
Description
The caller of this function is responsible for calling fpga_mgr_unregister().
Using devm_fpga_mgr_register_full() instead is recommended.
Return
pointer to struct fpga_manager pointer or ERR_PTR()
__fpga_mgr_register() #
struct fpga_manager * create and register an FPGA Manager device
Parameters
struct device *parent: fpga manager device from pdevconst char *name: fpga manager nameconst struct fpga_manager_ops *mops: pointer to structure of fpga manager opsvoid *priv: fpga manager private datastruct module *owner: owner module containing the ops
Description
The caller of this function is responsible for calling fpga_mgr_unregister().
Using devm_fpga_mgr_register() instead is recommended. This simple
version of the register function should be sufficient for most users. The
fpga_mgr_register_full() function is available for users that need to pass
additional, optional parameters.
Return
pointer to struct fpga_manager pointer or ERR_PTR()
__devm_fpga_mgr_register_full() #
struct fpga_manager * resource managed variant of fpga_mgr_register()
Parameters
struct device *parent: fpga manager device from pdevconst struct fpga_manager_info *info: parameters for fpga managerstruct module *owner: owner module containing the ops
Return
fpga manager pointer on success, negative error code otherwise.
Description
This is the devres variant of fpga_mgr_register_full() for which the unregister
function will be called automatically when the managing device is detached.
__devm_fpga_mgr_register() #
struct fpga_manager * resource managed variant of fpga_mgr_register()
Parameters
struct device *parent: fpga manager device from pdevconst char *name: fpga manager nameconst struct fpga_manager_ops *mops: pointer to structure of fpga manager opsvoid *priv: fpga manager private datastruct module *owner: owner module containing the ops
Return
fpga manager pointer on success, negative error code otherwise.
Description
This is the devres variant of fpga_mgr_register() for which the
unregister function will be called automatically when the managing
device is detached.
fpga_mgr_unregister() #
void unregister an FPGA manager
Parameters
struct fpga_manager *mgr: fpga manager struct
Description
This function is intended for use in an FPGA manager driver's remove function.
In-kernel API for FPGA Programming
Peios Kernel / Hardware and drivers / Driver development / FPGAs
Overview #
The in-kernel API for FPGA programming is a combination of APIs from FPGA manager, bridge, and regions. The actual function used to trigger FPGA programming is fpga_region_program_fpga().
fpga_region_program_fpga() uses functionality supplied by the FPGA manager and bridges. It will:
lock the region's mutex
lock the mutex of the region's FPGA manager
build a list of FPGA bridges if a method has been specified to do so
disable the bridges
program the FPGA using info passed in
fpga_region->info.re-enable the bridges
release the locks
The struct fpga_image_info specifies what FPGA image to program. It is allocated/freed by fpga_image_info_alloc() and freed with fpga_image_info_free()
How to program an FPGA using a region #
When the FPGA region driver probed, it was given a pointer to an FPGA manager driver so it knows which manager to use. The region also either has a list of bridges to control during programming or it has a pointer to a function that will generate that list. Here's some sample code of what to do next:
#include <linux/fpga/fpga-mgr.h>
#include <linux/fpga/fpga-region.h>
struct fpga_image_info *info;
int ret;
/*
* First, alloc the struct with information about the FPGA image to
* program.
*/
info = fpga_image_info_alloc(dev);
if (!info)
return -ENOMEM;
/* Set flags as needed, such as: */
info->flags = FPGA_MGR_PARTIAL_RECONFIG;
/*
* Indicate where the FPGA image is. This is pseudo-code; you're
* going to use one of these three.
*/
if (image is in a scatter gather table) {
info->sgt = [your scatter gather table]
} else if (image is in a buffer) {
info->buf = [your image buffer]
info->count = [image buffer size]
} else if (image is in a firmware file) {
info->firmware_name = devm_kstrdup(dev, firmware_name,
GFP_KERNEL);
}
/* Add info to region and do the programming */
region->info = info;
ret = fpga_region_program_fpga(region);
/* Deallocate the image info if you're done with it */
region->info = NULL;
fpga_image_info_free(info);
if (ret)
return ret;
/* Now enumerate whatever hardware has appeared in the FPGA. */
API for programming an FPGA #
-
fpga_region_program_fpga() - Program an FPGA
-
fpga_image_info() - Specifies what FPGA image to program
-
fpga_image_info_alloc() - Allocate an FPGA image info struct
-
fpga_image_info_free() - Free an FPGA image info struct
fpga_region_program_fpga() #
int program FPGA
Parameters
struct fpga_region *region: FPGA region
Description
Program an FPGA using fpga image info (region->info).
If the region has a get_bridges function, the exclusive reference for the
bridges will be held if programming succeeds. This is intended to prevent
reprogramming the region until the caller considers it safe to do so.
The caller will need to call fpga_bridges_put() before attempting to
reprogram the region.
Return
0 for success or negative error code.
FPGA Manager flags
Flags used in the fpga_image_info->flags field
FPGA_MGR_PARTIAL_RECONFIG: do partial reconfiguration if supported
FPGA_MGR_EXTERNAL_CONFIG: FPGA has been configured prior to Linux booting
FPGA_MGR_ENCRYPTED_BITSTREAM: indicates bitstream is encrypted
FPGA_MGR_BITSTREAM_LSB_FIRST: SPI bitstream bit order is LSB first
FPGA_MGR_COMPRESSED_BITSTREAM: FPGA bitstream is compressed
struct fpga_image_info #
information specific to an FPGA image
Definition
;
Members
flags: boolean flags as defined aboveenable_timeout_us: maximum time to enable traffic through bridge (uSec)disable_timeout_us: maximum time to disable traffic through bridge (uSec)config_complete_timeout_us: maximum time for FPGA to switch to operating status in the write_complete op.firmware_name: name of FPGA image firmware filesgt: scatter/gather table containing FPGA imagebuf: contiguous buffer containing FPGA imagecount: size of bufheader_size: size of image header.data_size: size of image data to be sent to the device. If not specified, whole image will be used. Header may be skipped in either case.region_id: id of target regiondev: device that owns thisoverlay: Device Tree overlay
fpga_image_info_alloc() #
struct fpga_image_info * Allocate an FPGA image info struct
Parameters
struct device *dev: owning device
Return
struct fpga_image_info or NULL
fpga_image_info_free() #
void Free an FPGA image info struct
Parameters
struct fpga_image_info *info: FPGA image info struct to free
FPGA Region
Peios Kernel / Hardware and drivers / Driver development / FPGAs
Overview #
This document is meant to be a brief overview of the FPGA region API usage. A more conceptual look at regions can be found in the Device Tree binding document 1.
For the purposes of this API document, let's just say that a region associates an FPGA Manager and a bridge (or bridges) with a reprogrammable region of an FPGA or the whole FPGA. The API provides a way to register a region and to program a region.
Currently the only layer above fpga-region.c in the kernel is the Device Tree support (of-fpga-region.c) described in 1. The DT support layer uses regions to program the FPGA and then DT to handle enumeration. The common region code is intended to be used by other schemes that have other ways of accomplishing enumeration after programming.
An fpga-region can be set up to know the following things:
which FPGA manager to use to do the programming
which bridges to disable before programming and enable afterwards.
Additional info needed to program the FPGA image is passed in the struct fpga_image_info including:
pointers to the image as either a scatter-gather buffer, a contiguous buffer, or the name of firmware file
flags indicating specifics such as whether the image is for partial reconfiguration.
How to add a new FPGA region #
An example of usage can be seen in the probe function of 2.
[1] ../devicetree/bindings/fpga/fpga-region.txt
[2] ../../drivers/fpga/of-fpga-region.c
API to add a new FPGA region #
-
struct fpga_region - The FPGA region struct
-
struct fpga_region_info - Parameter structure for __fpga_region_register_full()
-
__fpga_region_register_full() - Create and register an FPGA region using the fpga_region_info structure to provide the full flexibility of options
-
__fpga_region_register() - Create and register an FPGA region using standard arguments
-
fpga_region_unregister() - Unregister an FPGA region
Helper macros fpga_region_register() and fpga_region_register_full() automatically set the module that registers the FPGA region as the owner.
The FPGA region's probe function will need to get a reference to the FPGA Manager it will be using to do the programming. This usually would happen during the region's probe function.
-
fpga_mgr_get() - Get a reference to an FPGA manager, raise ref count
-
of_fpga_mgr_get() - Get a reference to an FPGA manager, raise ref count, given a device node.
-
fpga_mgr_put() - Put an FPGA manager
The FPGA region will need to specify which bridges to control while programming the FPGA. The region driver can build a list of bridges during probe time (fpga_region->bridge_list) or it can have a function that creates the list of bridges to program just before programming (fpga_region->get_bridges). The FPGA bridge framework supplies the following APIs to handle building or tearing down that list.
-
fpga_bridge_get_to_list() - Get a ref of an FPGA bridge, add it to a list
-
of_fpga_bridge_get_to_list() - Get a ref of an FPGA bridge, add it to a list, given a device node
-
fpga_bridges_put() - Given a list of bridges, put them
struct fpga_region #
FPGA Region structure
Definition
;
Members
dev: FPGA Region devicemutex: enforces exclusive reference to regionbridge_list: list of FPGA bridges specified in regionmgr: FPGA managerinfo: FPGA image infocompat_id: FPGA region id for compatibility check.ops_owner: module containing the get_bridges functionpriv: private dataget_bridges: optional function to get bridges to a list
struct fpga_region_info #
collection of parameters an FPGA Region
Definition
;
Members
mgr: fpga region managercompat_id: FPGA region id for compatibility check.priv: fpga region private dataget_bridges: optional function to get bridges to a list
Description
fpga_region_info contains parameters for the register_full function. These are separated into an info structure because they some are optional others could be added to in the future. The info structure facilitates maintaining a stable API.
__fpga_region_register_full() #
struct fpga_region * create and register an FPGA Region device
Parameters
struct device *parent: device parentconst struct fpga_region_info *info: parameters for FPGA Regionstruct module *owner: module containing the get_bridges function
Return
struct fpga_region or ERR_PTR()
__fpga_region_register() #
struct fpga_region * create and register an FPGA Region device
Parameters
struct device *parent: device parentstruct fpga_manager *mgr: manager that programs this regionint (*get_bridges)(struct fpga_region *): optional function to get bridges to a liststruct module *owner: module containing the get_bridges function
Description
This simple version of the register function should be sufficient for most users.
The fpga_region_register_full() function is available for users that need to
pass additional, optional parameters.
Return
struct fpga_region or ERR_PTR()
fpga_region_unregister() #
void unregister an FPGA region
Parameters
struct fpga_region *region: FPGA region
Description
This function is intended for use in an FPGA region driver's remove function.
fpga_mgr_get() #
struct fpga_manager * Given a device, get a reference to an fpga mgr.
Parameters
struct device *dev: parent device that fpga mgr was registered with
Return
fpga manager struct or IS_ERR() condition containing error code.
of_fpga_mgr_get() #
struct fpga_manager * Given a device node, get a reference to an fpga mgr.
Parameters
struct device_node *node: device node
Return
fpga manager struct or IS_ERR() condition containing error code.
fpga_mgr_put() #
void release a reference to an fpga manager
Parameters
struct fpga_manager *mgr: fpga manager structure
fpga_bridge_get_to_list() #
int given device, get a bridge, add it to a list
Parameters
struct device *dev: FPGA bridge devicestruct fpga_image_info *info: fpga image specific informationstruct list_head *bridge_list: list of FPGA bridges
Description
Get an exclusive reference to the bridge and it to the list.
Return
0 for success, error code from fpga_bridge_get() otherwise.
of_fpga_bridge_get_to_list() #
int get a bridge, add it to a list
Parameters
struct device_node *np: node pointer of an FPGA bridgestruct fpga_image_info *info: fpga image specific informationstruct list_head *bridge_list: list of FPGA bridges
Description
Get an exclusive reference to the bridge and it to the list.
Return
0 for success, error code from of_fpga_bridge_get() otherwise.
fpga_bridges_put() #
void put bridges
Parameters
struct list_head *bridge_list: list of FPGA bridges
Description
For each bridge in the list, put the bridge and remove it from the list. If list is empty, do nothing.
Introduction
Peios Kernel / Hardware and drivers / Driver development / FPGAs
The FPGA subsystem supports reprogramming FPGAs dynamically under Linux. Some of the core intentions of the FPGA subsystems are:
-
The FPGA subsystem is vendor agnostic.
-
The FPGA subsystem separates upper layers (userspace interfaces and enumeration) from lower layers that know how to program a specific FPGA.
-
Code should not be shared between upper and lower layers. This should go without saying. If that seems necessary, there's probably framework functionality that can be added that will benefit other users. Write the linux-fpga mailing list and maintainers and seek out a solution that expands the framework for broad reuse.
-
Generally, when adding code, think of the future. Plan for reuse.
The framework in the kernel is divided into:
FPGA Manager #
If you are adding a new FPGA or a new method of programming an FPGA, this is the subsystem for you. Low level FPGA manager drivers contain the knowledge of how to program a specific device. This subsystem includes the framework in fpga-mgr.c and the low level drivers that are registered with it.
FPGA Bridge #
FPGA Bridges prevent spurious signals from going out of an FPGA or a region of an FPGA during programming. They are disabled before programming begins and re-enabled afterwards. An FPGA bridge may be actual hard hardware that gates a bus to a CPU or a soft ("freeze") bridge in FPGA fabric that surrounds a partial reconfiguration region of an FPGA. This subsystem includes fpga-bridge.c and the low level drivers that are registered with it.
FPGA Region #
If you are adding a new interface to the FPGA framework, add it on top of an FPGA region.
The FPGA Region framework (fpga-region.c) associates managers and bridges as reconfigurable regions. A region may refer to the whole FPGA in full reconfiguration or to a partial reconfiguration region.
The Device Tree FPGA Region support (of-fpga-region.c) handles reprogramming FPGAs when device tree overlays are applied.