# FPGAs

---

# FPGA Subsystem

_Peios Kernel / Hardware and drivers / Driver development / FPGAs_

> Reference for FPGA Subsystem.

<a id="rst-fpga-subsystem"></a>

**Author:** Alan Tull

- [Introduction](</kernel/hardware/driver-api/fpga/intro.md>)
- [FPGA Manager](</kernel/hardware/driver-api/fpga/fpga-mgr.md>)
- [FPGA Bridge](</kernel/hardware/driver-api/fpga/fpga-bridge.md>)
- [FPGA Region](</kernel/hardware/driver-api/fpga/fpga-region.md>)
- [In-kernel API for FPGA Programming](</kernel/hardware/driver-api/fpga/fpga-programming.md>)

---

# FPGA Bridge

_Peios Kernel / Hardware and drivers / Driver development / FPGAs_

> struct fpga_bridge - The FPGA Bridge structure

<a id="rst-fpga-bridge"></a>

<a id="rst-api-to-implement-a-new-fpga-bridge"></a>

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


#### <a id="c-struct-fpga_bridge"></a>`struct fpga_bridge`

FPGA bridge structure

**Definition**

```c
struct fpga_bridge {
    const char *name;
    struct device dev;
    struct mutex mutex;
    const struct fpga_bridge_ops *br_ops;
    struct module *br_ops_owner;
    struct fpga_image_info *info;
    struct list_head node;
    void *priv;
};
```

**Members**

- `name`: name of low level FPGA bridge
- `dev`: FPGA bridge device
- `mutex`: enforces exclusive reference to bridge
- `br_ops`: pointer to struct of FPGA bridge ops
- `br_ops_owner`: module containing the br_ops
- `info`: fpga image specific information
- `node`: FPGA bridge list node
- `priv`: low level driver private date



#### <a id="c-struct-fpga_bridge_ops"></a>`struct fpga_bridge_ops`

ops for low level FPGA bridge drivers

**Definition**

```c
struct fpga_bridge_ops {
    int (*enable_show)(struct fpga_bridge *bridge);
    int (*enable_set)(struct fpga_bridge *bridge, bool enable);
    void (*fpga_bridge_remove)(struct fpga_bridge *bridge);
    const struct attribute_group **groups;
};
```

**Members**

- `enable_show`: returns the FPGA bridge's status
- `enable_set`: set an FPGA bridge as enabled or disabled
- `fpga_bridge_remove`: set FPGA into a specific state during driver remove
- `groups`: optional attribute groups.



#### <a id="c-__fpga_bridge_register"></a>`__fpga_bridge_register()`

```c
struct fpga_bridge * __fpga_bridge_register(struct device *parent, const char *name, const struct fpga_bridge_ops *br_ops, void *priv, struct module *owner)
```

create and register an FPGA Bridge device

**Parameters**

- `struct device *parent`: FPGA bridge device from pdev
- `const char *name`: FPGA bridge name
- `const struct fpga_bridge_ops *br_ops`: pointer to structure of fpga bridge ops
- `void *priv`: FPGA bridge private data
- `struct module *owner`: owner module containing the br_ops

**Return**

struct fpga_bridge pointer or [`ERR_PTR()`](/kernel/internals/core-api/kernel-api.md#c-ERR_PTR)



#### <a id="c-fpga_bridge_unregister"></a>`fpga_bridge_unregister()`

```c
void fpga_bridge_unregister(struct fpga_bridge *bridge)
```

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_

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

<a id="rst-fpga-manager"></a>

<a id="rst-overview"></a>

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

<a id="rst-how-to-support-a-new-fpga-device"></a>

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



```text
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::**

  1. .parse_header (optional, may be called once or multiple times)

  2. .write_init

  3. .write or .write_sg (may be called once or multiple times)

  4. .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-&gt;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-&gt;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.

<a id="rst-api-for-implementing-a-new-fpga-manager-driver"></a>

## API for implementing a new FPGA Manager driver

- `fpga_mgr_states` -  Values for `fpga_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.


#### <a id="c-enum-fpga_mgr_states"></a>`enum fpga_mgr_states`

fpga framework states

**Constants**

- `FPGA_MGR_STATE_UNKNOWN`: can't determine state
- `FPGA_MGR_STATE_POWER_OFF`: FPGA power is off
- `FPGA_MGR_STATE_POWER_UP`: FPGA reports power is up
- `FPGA_MGR_STATE_RESET`: FPGA in reset state
- `FPGA_MGR_STATE_FIRMWARE_REQ`: firmware request in progress
- `FPGA_MGR_STATE_FIRMWARE_REQ_ERR`: firmware request failed
- `FPGA_MGR_STATE_PARSE_HEADER`: parse FPGA image header
- `FPGA_MGR_STATE_PARSE_HEADER_ERR`: Error during PARSE_HEADER stage
- `FPGA_MGR_STATE_WRITE_INIT`: preparing FPGA for programming
- `FPGA_MGR_STATE_WRITE_INIT_ERR`: Error during WRITE_INIT stage
- `FPGA_MGR_STATE_WRITE`: writing image to FPGA
- `FPGA_MGR_STATE_WRITE_ERR`: Error while writing FPGA
- `FPGA_MGR_STATE_WRITE_COMPLETE`: Doing post programming steps
- `FPGA_MGR_STATE_WRITE_COMPLETE_ERR`: Error during WRITE_COMPLETE
- `FPGA_MGR_STATE_OPERATING`: FPGA is programmed and operating



#### <a id="c-struct-fpga_manager"></a>`struct fpga_manager`

fpga manager structure

**Definition**

```c
struct fpga_manager {
    const char *name;
    struct device dev;
    struct mutex ref_mutex;
    enum fpga_mgr_states state;
    struct fpga_compat_id *compat_id;
    const struct fpga_manager_ops *mops;
    struct module *mops_owner;
    void *priv;
};
```

**Members**

- `name`: name of low level fpga manager
- `dev`: fpga manager device
- `ref_mutex`: only allows one reference to fpga manager
- `state`: state of fpga manager
- `compat_id`: FPGA manager id for compatibility check.
- `mops`: pointer to struct of fpga manager ops
- `mops_owner`: module containing the mops
- `priv`: low level driver private date



#### <a id="c-struct-fpga_manager_ops"></a>`struct fpga_manager_ops`

ops for low level fpga manager drivers

**Definition**

```c
struct fpga_manager_ops {
    size_t initial_header_size;
    bool skip_header;
    enum fpga_mgr_states (*state)(struct fpga_manager *mgr);
    u64 (*status)(struct fpga_manager *mgr);
    int (*parse_header)(struct fpga_manager *mgr, struct fpga_image_info *info, const char *buf, size_t count);
    int (*write_init)(struct fpga_manager *mgr, struct fpga_image_info *info, const char *buf, size_t count);
    int (*write)(struct fpga_manager *mgr, const char *buf, size_t count);
    int (*write_sg)(struct fpga_manager *mgr, struct sg_table *sgt);
    int (*write_complete)(struct fpga_manager *mgr, struct fpga_image_info *info);
    void (*fpga_remove)(struct fpga_manager *mgr);
    const struct attribute_group **groups;
};
```

**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 state
- `status`: returns status of the FPGA, including reconfiguration error code
- `parse_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 data
- `write`: write count bytes of configuration data to the FPGA
- `write_sg`: write the scatter list 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
- `groups`: 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.



#### <a id="c-struct-fpga_manager_info"></a>`struct fpga_manager_info`

collection of parameters for an FPGA Manager

**Definition**

```c
struct fpga_manager_info {
    const char *name;
    struct fpga_compat_id *compat_id;
    const struct fpga_manager_ops *mops;
    void *priv;
};
```

**Members**

- `name`: fpga manager name
- `compat_id`: FPGA manager id for compatibility check.
- `mops`: pointer to structure of fpga manager ops
- `priv`: 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.



#### <a id="c-__fpga_mgr_register_full"></a>`__fpga_mgr_register_full()`

```c
struct fpga_manager * __fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info, struct module *owner)
```

create and register an FPGA Manager device

**Parameters**

- `struct device *parent`: fpga manager device from pdev
- `const struct fpga_manager_info *info`: parameters for fpga manager
- `struct module *owner`: owner module containing the ops

**Description**

The caller of this function is responsible for calling [`fpga_mgr_unregister()`](#c-fpga_mgr_unregister).
Using `devm_fpga_mgr_register_full()` instead is recommended.

**Return**

pointer to struct fpga_manager pointer or [`ERR_PTR()`](/kernel/internals/core-api/kernel-api.md#c-ERR_PTR)



#### <a id="c-__fpga_mgr_register"></a>`__fpga_mgr_register()`

```c
struct fpga_manager * __fpga_mgr_register(struct device *parent, const char *name, const struct fpga_manager_ops *mops, void *priv, struct module *owner)
```

create and register an FPGA Manager device

**Parameters**

- `struct device *parent`: fpga manager device from pdev
- `const char *name`: fpga manager name
- `const struct fpga_manager_ops *mops`: pointer to structure of fpga manager ops
- `void *priv`: fpga manager private data
- `struct module *owner`: owner module containing the ops

**Description**

The caller of this function is responsible for calling [`fpga_mgr_unregister()`](#c-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()`](/kernel/internals/core-api/kernel-api.md#c-ERR_PTR)



#### <a id="c-__devm_fpga_mgr_register_full"></a>`__devm_fpga_mgr_register_full()`

```c
struct fpga_manager * __devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info, struct module *owner)
```

resource managed variant of `fpga_mgr_register()`

**Parameters**

- `struct device *parent`: fpga manager device from pdev
- `const struct fpga_manager_info *info`: parameters for fpga manager
- `struct 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.



#### <a id="c-__devm_fpga_mgr_register"></a>`__devm_fpga_mgr_register()`

```c
struct fpga_manager * __devm_fpga_mgr_register(struct device *parent, const char *name, const struct fpga_manager_ops *mops, void *priv, struct module *owner)
```

resource managed variant of `fpga_mgr_register()`

**Parameters**

- `struct device *parent`: fpga manager device from pdev
- `const char *name`: fpga manager name
- `const struct fpga_manager_ops *mops`: pointer to structure of fpga manager ops
- `void *priv`: fpga manager private data
- `struct 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.



#### <a id="c-fpga_mgr_unregister"></a>`fpga_mgr_unregister()`

```c
void fpga_mgr_unregister(struct fpga_manager *mgr)
```

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_

> 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().

<a id="rst-in-kernel-api-for-fpga-programming"></a>

<a id="rst-overview"></a>

## 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()

<a id="rst-how-to-program-an-fpga-using-a-region"></a>

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



```text
#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. */
```

<a id="rst-api-for-programming-an-fpga"></a>

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


#### <a id="c-fpga_region_program_fpga"></a>`fpga_region_program_fpga()`

```c
int fpga_region_program_fpga(struct fpga_region *region)
```

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()`](/kernel/hardware/driver-api/fpga/fpga-region.md#c-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`](#c-struct-fpga_image_info) 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



#### <a id="c-struct-fpga_image_info"></a>`struct fpga_image_info`

information specific to an FPGA image

**Definition**

```c
struct fpga_image_info {
        u32 flags;
        u32 enable_timeout_us;
        u32 disable_timeout_us;
        u32 config_complete_timeout_us;
        char *firmware_name;
        struct sg_table *sgt;
        const char *buf;
        size_t count;
        size_t header_size;
        size_t data_size;
        int region_id;
        struct device *dev;
    #ifdef CONFIG_OF;
        struct device_node *overlay;
    #endif;
};
```

**Members**

- `flags`: boolean flags as defined above
- `enable_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 file
- `sgt`: scatter/gather table containing FPGA image
- `buf`: contiguous buffer containing FPGA image
- `count`: size of buf
- `header_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 region
- `dev`: device that owns this
- `overlay`: Device Tree overlay



#### <a id="c-fpga_image_info_alloc"></a>`fpga_image_info_alloc()`

```c
struct fpga_image_info * fpga_image_info_alloc(struct device *dev)
```

Allocate an FPGA image info struct

**Parameters**

- `struct device *dev`: owning device

**Return**

struct fpga_image_info or NULL



#### <a id="c-fpga_image_info_free"></a>`fpga_image_info_free()`

```c
void fpga_image_info_free(struct fpga_image_info *info)
```

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_

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

<a id="rst-fpga-region"></a>

<a id="rst-overview"></a>

## 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 <a id="rst-footnote-reference-1"></a>[1](#rst-f1).

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 <a id="rst-footnote-reference-2"></a>[1](#rst-f1).  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.

<a id="rst-how-to-add-a-new-fpga-region"></a>

## How to add a new FPGA region

An example of usage can be seen in the probe function of <a id="rst-footnote-reference-3"></a>[2](#rst-f2).

<a id="rst-f1"></a>



**[1]** ../devicetree/bindings/fpga/fpga-region.txt

<a id="rst-f2"></a>



**[2]** ../../drivers/fpga/of-fpga-region.c

<a id="rst-api-to-add-a-new-fpga-region"></a>

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


#### <a id="c-struct-fpga_region"></a>`struct fpga_region`

FPGA Region structure

**Definition**

```c
struct fpga_region {
    struct device dev;
    struct mutex mutex;
    struct list_head bridge_list;
    struct fpga_manager *mgr;
    struct fpga_image_info *info;
    struct fpga_compat_id *compat_id;
    struct module *ops_owner;
    void *priv;
    int (*get_bridges)(struct fpga_region *region);
};
```

**Members**

- `dev`: FPGA Region device
- `mutex`: enforces exclusive reference to region
- `bridge_list`: list of FPGA bridges specified in region
- `mgr`: FPGA manager
- `info`: FPGA image info
- `compat_id`: FPGA region id for compatibility check.
- `ops_owner`: module containing the get_bridges function
- `priv`: private data
- `get_bridges`: optional function to get bridges to a list



#### <a id="c-struct-fpga_region_info"></a>`struct fpga_region_info`

collection of parameters an FPGA Region

**Definition**

```c
struct fpga_region_info {
    struct fpga_manager *mgr;
    struct fpga_compat_id *compat_id;
    void *priv;
    int (*get_bridges)(struct fpga_region *region);
};
```

**Members**

- `mgr`: fpga region manager
- `compat_id`: FPGA region id for compatibility check.
- `priv`: fpga region private data
- `get_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.



#### <a id="c-__fpga_region_register_full"></a>`__fpga_region_register_full()`

```c
struct fpga_region * __fpga_region_register_full(struct device *parent, const struct fpga_region_info *info, struct module *owner)
```

create and register an FPGA Region device

**Parameters**

- `struct device *parent`: device parent
- `const struct fpga_region_info *info`: parameters for FPGA Region
- `struct module *owner`: module containing the get_bridges function

**Return**

struct fpga_region or [`ERR_PTR()`](/kernel/internals/core-api/kernel-api.md#c-ERR_PTR)



#### <a id="c-__fpga_region_register"></a>`__fpga_region_register()`

```c
struct fpga_region * __fpga_region_register(struct device *parent, struct fpga_manager *mgr, int (*get_bridges)(struct fpga_region *), struct module *owner)
```

create and register an FPGA Region device

**Parameters**

- `struct device *parent`: device parent
- `struct fpga_manager *mgr`: manager that programs this region
- `int (*get_bridges)(struct fpga_region *)`: optional function to get bridges to a list
- `struct 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()`](/kernel/internals/core-api/kernel-api.md#c-ERR_PTR)



#### <a id="c-fpga_region_unregister"></a>`fpga_region_unregister()`

```c
void fpga_region_unregister(struct fpga_region *region)
```

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.



#### <a id="c-fpga_mgr_get"></a>`fpga_mgr_get()`

```c
struct fpga_manager * fpga_mgr_get(struct device *dev)
```

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()`](/kernel/internals/core-api/kernel-api.md#c-IS_ERR) condition containing error code.



#### <a id="c-of_fpga_mgr_get"></a>`of_fpga_mgr_get()`

```c
struct fpga_manager * of_fpga_mgr_get(struct device_node *node)
```

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()`](/kernel/internals/core-api/kernel-api.md#c-IS_ERR) condition containing error code.



#### <a id="c-fpga_mgr_put"></a>`fpga_mgr_put()`

```c
void fpga_mgr_put(struct fpga_manager *mgr)
```

release a reference to an fpga manager

**Parameters**

- `struct fpga_manager *mgr`: fpga manager structure



#### <a id="c-fpga_bridge_get_to_list"></a>`fpga_bridge_get_to_list()`

```c
int fpga_bridge_get_to_list(struct device *dev, struct fpga_image_info *info, struct list_head *bridge_list)
```

given device, get a bridge, add it to a list

**Parameters**

- `struct device *dev`: FPGA bridge device
- `struct fpga_image_info *info`: fpga image specific information
- `struct 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.



#### <a id="c-of_fpga_bridge_get_to_list"></a>`of_fpga_bridge_get_to_list()`

```c
int of_fpga_bridge_get_to_list(struct device_node *np, struct fpga_image_info *info, struct list_head *bridge_list)
```

get a bridge, add it to a list

**Parameters**

- `struct device_node *np`: node pointer of an FPGA bridge
- `struct fpga_image_info *info`: fpga image specific information
- `struct 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.



#### <a id="c-fpga_bridges_put"></a>`fpga_bridges_put()`

```c
void fpga_bridges_put(struct list_head *bridge_list)
```

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:

<a id="rst-introduction"></a>

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:

<a id="rst-fpga-manager"></a>

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

<a id="rst-fpga-bridge"></a>

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

<a id="rst-fpga-region"></a>

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