2016-11-02 03:14:29 +08:00
|
|
|
/*
|
|
|
|
* FPGA Region - Device Tree support for FPGA programming under Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2016 Altera Corporation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/fpga/fpga-bridge.h>
|
|
|
|
#include <linux/fpga/fpga-mgr.h>
|
2017-11-16 04:20:21 +08:00
|
|
|
#include <linux/fpga/fpga-region.h>
|
2016-11-02 03:14:29 +08:00
|
|
|
#include <linux/idr.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of_platform.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
|
|
|
|
static DEFINE_IDA(fpga_region_ida);
|
|
|
|
static struct class *fpga_region_class;
|
|
|
|
|
2017-11-16 04:20:24 +08:00
|
|
|
struct fpga_region *fpga_region_class_find(
|
|
|
|
struct device *start, const void *data,
|
|
|
|
int (*match)(struct device *, const void *))
|
|
|
|
{
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
dev = class_find_device(fpga_region_class, start, data, match);
|
|
|
|
if (!dev)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return to_fpga_region(dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(fpga_region_class_find);
|
|
|
|
|
2016-11-02 03:14:29 +08:00
|
|
|
static const struct of_device_id fpga_region_of_match[] = {
|
|
|
|
{ .compatible = "fpga-region", },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, fpga_region_of_match);
|
|
|
|
|
|
|
|
static int fpga_region_of_node_match(struct device *dev, const void *data)
|
|
|
|
{
|
|
|
|
return dev->of_node == data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-16 04:20:22 +08:00
|
|
|
* of_fpga_region_find - find FPGA region
|
2016-11-02 03:14:29 +08:00
|
|
|
* @np: device node of FPGA Region
|
2017-11-16 04:20:22 +08:00
|
|
|
*
|
2016-11-02 03:14:29 +08:00
|
|
|
* Caller will need to put_device(®ion->dev) when done.
|
2017-11-16 04:20:22 +08:00
|
|
|
*
|
2016-11-02 03:14:29 +08:00
|
|
|
* Returns FPGA Region struct or NULL
|
|
|
|
*/
|
2017-11-16 04:20:22 +08:00
|
|
|
static struct fpga_region *of_fpga_region_find(struct device_node *np)
|
2016-11-02 03:14:29 +08:00
|
|
|
{
|
2017-11-16 04:20:24 +08:00
|
|
|
return fpga_region_class_find(NULL, np, fpga_region_of_node_match);
|
2016-11-02 03:14:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fpga_region_get - get an exclusive reference to a fpga region
|
|
|
|
* @region: FPGA Region struct
|
|
|
|
*
|
|
|
|
* Caller should call fpga_region_put() when done with region.
|
|
|
|
*
|
|
|
|
* Return fpga_region struct if successful.
|
|
|
|
* Return -EBUSY if someone already has a reference to the region.
|
|
|
|
* Return -ENODEV if @np is not a FPGA Region.
|
|
|
|
*/
|
|
|
|
static struct fpga_region *fpga_region_get(struct fpga_region *region)
|
|
|
|
{
|
|
|
|
struct device *dev = ®ion->dev;
|
|
|
|
|
|
|
|
if (!mutex_trylock(®ion->mutex)) {
|
|
|
|
dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
|
|
|
|
return ERR_PTR(-EBUSY);
|
|
|
|
}
|
|
|
|
|
|
|
|
get_device(dev);
|
|
|
|
if (!try_module_get(dev->parent->driver->owner)) {
|
|
|
|
put_device(dev);
|
|
|
|
mutex_unlock(®ion->mutex);
|
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:20:14 +08:00
|
|
|
dev_dbg(dev, "get\n");
|
2016-11-02 03:14:29 +08:00
|
|
|
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fpga_region_put - release a reference to a region
|
|
|
|
*
|
|
|
|
* @region: FPGA region
|
|
|
|
*/
|
|
|
|
static void fpga_region_put(struct fpga_region *region)
|
|
|
|
{
|
|
|
|
struct device *dev = ®ion->dev;
|
|
|
|
|
2017-11-16 04:20:14 +08:00
|
|
|
dev_dbg(dev, "put\n");
|
2016-11-02 03:14:29 +08:00
|
|
|
|
|
|
|
module_put(dev->parent->driver->owner);
|
|
|
|
put_device(dev);
|
|
|
|
mutex_unlock(®ion->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-16 04:20:22 +08:00
|
|
|
* of_fpga_region_get_mgr - get reference for FPGA manager
|
2017-11-16 04:20:16 +08:00
|
|
|
* @np: device node of FPGA region
|
2016-11-02 03:14:29 +08:00
|
|
|
*
|
|
|
|
* Get FPGA Manager from "fpga-mgr" property or from ancestor region.
|
|
|
|
*
|
|
|
|
* Caller should call fpga_mgr_put() when done with manager.
|
|
|
|
*
|
|
|
|
* Return: fpga manager struct or IS_ERR() condition containing error code.
|
|
|
|
*/
|
2017-11-16 04:20:22 +08:00
|
|
|
static struct fpga_manager *of_fpga_region_get_mgr(struct device_node *np)
|
2016-11-02 03:14:29 +08:00
|
|
|
{
|
|
|
|
struct device_node *mgr_node;
|
|
|
|
struct fpga_manager *mgr;
|
|
|
|
|
|
|
|
of_node_get(np);
|
|
|
|
while (np) {
|
|
|
|
if (of_device_is_compatible(np, "fpga-region")) {
|
|
|
|
mgr_node = of_parse_phandle(np, "fpga-mgr", 0);
|
|
|
|
if (mgr_node) {
|
|
|
|
mgr = of_fpga_mgr_get(mgr_node);
|
|
|
|
of_node_put(np);
|
|
|
|
return mgr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
np = of_get_next_parent(np);
|
|
|
|
}
|
|
|
|
of_node_put(np);
|
|
|
|
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-16 04:20:22 +08:00
|
|
|
* of_fpga_region_get_bridges - create a list of bridges
|
2016-11-02 03:14:29 +08:00
|
|
|
* @region: FPGA region
|
|
|
|
*
|
|
|
|
* Create a list of bridges including the parent bridge and the bridges
|
|
|
|
* specified by "fpga-bridges" property. Note that the
|
|
|
|
* fpga_bridges_enable/disable/put functions are all fine with an empty list
|
|
|
|
* if that happens.
|
|
|
|
*
|
|
|
|
* Caller should call fpga_bridges_put(®ion->bridge_list) when
|
|
|
|
* done with the bridges.
|
|
|
|
*
|
|
|
|
* Return 0 for success (even if there are no bridges specified)
|
|
|
|
* or -EBUSY if any of the bridges are in use.
|
|
|
|
*/
|
2017-11-16 04:20:23 +08:00
|
|
|
static int of_fpga_region_get_bridges(struct fpga_region *region)
|
2016-11-02 03:14:29 +08:00
|
|
|
{
|
|
|
|
struct device *dev = ®ion->dev;
|
|
|
|
struct device_node *region_np = dev->of_node;
|
2017-11-16 04:20:23 +08:00
|
|
|
struct fpga_image_info *info = region->info;
|
2016-11-02 03:14:29 +08:00
|
|
|
struct device_node *br, *np, *parent_br = NULL;
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
/* If parent is a bridge, add to list */
|
2017-11-16 04:20:22 +08:00
|
|
|
ret = of_fpga_bridge_get_to_list(region_np->parent, info,
|
2017-11-16 04:20:11 +08:00
|
|
|
®ion->bridge_list);
|
|
|
|
|
|
|
|
/* -EBUSY means parent is a bridge that is under use. Give up. */
|
2016-11-02 03:14:29 +08:00
|
|
|
if (ret == -EBUSY)
|
|
|
|
return ret;
|
|
|
|
|
2017-11-16 04:20:11 +08:00
|
|
|
/* Zero return code means parent was a bridge and was added to list. */
|
2016-11-02 03:14:29 +08:00
|
|
|
if (!ret)
|
|
|
|
parent_br = region_np->parent;
|
|
|
|
|
|
|
|
/* If overlay has a list of bridges, use it. */
|
2017-11-16 04:20:22 +08:00
|
|
|
if (of_parse_phandle(info->overlay, "fpga-bridges", 0))
|
|
|
|
np = info->overlay;
|
2016-11-02 03:14:29 +08:00
|
|
|
else
|
|
|
|
np = region_np;
|
|
|
|
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
br = of_parse_phandle(np, "fpga-bridges", i);
|
|
|
|
if (!br)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* If parent bridge is in list, skip it. */
|
|
|
|
if (br == parent_br)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* If node is a bridge, get it and add to list */
|
2017-11-16 04:20:23 +08:00
|
|
|
ret = of_fpga_bridge_get_to_list(br, info,
|
2017-11-16 04:20:11 +08:00
|
|
|
®ion->bridge_list);
|
2016-11-02 03:14:29 +08:00
|
|
|
|
|
|
|
/* If any of the bridges are in use, give up */
|
|
|
|
if (ret == -EBUSY) {
|
|
|
|
fpga_bridges_put(®ion->bridge_list);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fpga_region_program_fpga - program FPGA
|
|
|
|
* @region: FPGA region
|
2017-11-16 04:20:19 +08:00
|
|
|
* Program an FPGA using fpga image info (region->info).
|
2016-11-02 03:14:29 +08:00
|
|
|
* Return 0 for success or negative error code.
|
|
|
|
*/
|
2017-11-16 04:20:21 +08:00
|
|
|
int fpga_region_program_fpga(struct fpga_region *region)
|
2016-11-02 03:14:29 +08:00
|
|
|
{
|
2017-11-16 04:20:13 +08:00
|
|
|
struct device *dev = ®ion->dev;
|
2017-11-16 04:20:19 +08:00
|
|
|
struct fpga_image_info *info = region->info;
|
2016-11-02 03:14:29 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
region = fpga_region_get(region);
|
|
|
|
if (IS_ERR(region)) {
|
2017-11-16 04:20:14 +08:00
|
|
|
dev_err(dev, "failed to get FPGA region\n");
|
2016-11-02 03:14:29 +08:00
|
|
|
return PTR_ERR(region);
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:20:16 +08:00
|
|
|
ret = fpga_mgr_lock(region->mgr);
|
2017-11-16 04:20:13 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "FPGA manager is busy\n");
|
2017-11-16 04:20:16 +08:00
|
|
|
goto err_put_region;
|
2017-11-16 04:20:13 +08:00
|
|
|
}
|
|
|
|
|
2017-11-16 04:20:23 +08:00
|
|
|
/*
|
|
|
|
* In some cases, we already have a list of bridges in the
|
|
|
|
* fpga region struct. Or we don't have any bridges.
|
|
|
|
*/
|
|
|
|
if (region->get_bridges) {
|
|
|
|
ret = region->get_bridges(region);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "failed to get fpga region bridges\n");
|
|
|
|
goto err_unlock_mgr;
|
|
|
|
}
|
2016-11-02 03:14:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = fpga_bridges_disable(®ion->bridge_list);
|
|
|
|
if (ret) {
|
2017-11-16 04:20:14 +08:00
|
|
|
dev_err(dev, "failed to disable bridges\n");
|
2016-11-02 03:14:29 +08:00
|
|
|
goto err_put_br;
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:20:19 +08:00
|
|
|
ret = fpga_mgr_load(region->mgr, info);
|
2016-11-02 03:14:29 +08:00
|
|
|
if (ret) {
|
2017-11-16 04:20:14 +08:00
|
|
|
dev_err(dev, "failed to load FPGA image\n");
|
2016-11-02 03:14:29 +08:00
|
|
|
goto err_put_br;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fpga_bridges_enable(®ion->bridge_list);
|
|
|
|
if (ret) {
|
2017-11-16 04:20:14 +08:00
|
|
|
dev_err(dev, "failed to enable region bridges\n");
|
2016-11-02 03:14:29 +08:00
|
|
|
goto err_put_br;
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:20:16 +08:00
|
|
|
fpga_mgr_unlock(region->mgr);
|
2016-11-02 03:14:29 +08:00
|
|
|
fpga_region_put(region);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_put_br:
|
2017-11-16 04:20:23 +08:00
|
|
|
if (region->get_bridges)
|
|
|
|
fpga_bridges_put(®ion->bridge_list);
|
2017-11-16 04:20:13 +08:00
|
|
|
err_unlock_mgr:
|
2017-11-16 04:20:16 +08:00
|
|
|
fpga_mgr_unlock(region->mgr);
|
2017-04-25 05:34:21 +08:00
|
|
|
err_put_region:
|
2016-11-02 03:14:29 +08:00
|
|
|
fpga_region_put(region);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-11-16 04:20:21 +08:00
|
|
|
EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
|
2016-11-02 03:14:29 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* child_regions_with_firmware
|
|
|
|
* @overlay: device node of the overlay
|
|
|
|
*
|
|
|
|
* If the overlay adds child FPGA regions, they are not allowed to have
|
|
|
|
* firmware-name property.
|
|
|
|
*
|
|
|
|
* Return 0 for OK or -EINVAL if child FPGA region adds firmware-name.
|
|
|
|
*/
|
|
|
|
static int child_regions_with_firmware(struct device_node *overlay)
|
|
|
|
{
|
|
|
|
struct device_node *child_region;
|
|
|
|
const char *child_firmware_name;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
of_node_get(overlay);
|
|
|
|
|
|
|
|
child_region = of_find_matching_node(overlay, fpga_region_of_match);
|
|
|
|
while (child_region) {
|
|
|
|
if (!of_property_read_string(child_region, "firmware-name",
|
|
|
|
&child_firmware_name)) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
child_region = of_find_matching_node(child_region,
|
|
|
|
fpga_region_of_match);
|
|
|
|
}
|
|
|
|
|
|
|
|
of_node_put(child_region);
|
|
|
|
|
|
|
|
if (ret)
|
2017-08-02 10:20:51 +08:00
|
|
|
pr_err("firmware-name not allowed in child FPGA region: %pOF",
|
|
|
|
child_region);
|
2016-11-02 03:14:29 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-16 04:20:20 +08:00
|
|
|
* of_fpga_region_parse_ov - parse and check overlay applied to region
|
2016-11-02 03:14:29 +08:00
|
|
|
*
|
2017-11-16 04:20:20 +08:00
|
|
|
* @region: FPGA region
|
|
|
|
* @overlay: overlay applied to the FPGA region
|
2016-11-02 03:14:29 +08:00
|
|
|
*
|
2017-11-16 04:20:20 +08:00
|
|
|
* Given an overlay applied to a FPGA region, parse the FPGA image specific
|
|
|
|
* info in the overlay and do some checking.
|
2016-11-02 03:14:29 +08:00
|
|
|
*
|
2017-11-16 04:20:20 +08:00
|
|
|
* Returns:
|
|
|
|
* NULL if overlay doesn't direct us to program the FPGA.
|
|
|
|
* fpga_image_info struct if there is an image to program.
|
|
|
|
* error code for invalid overlay.
|
2016-11-02 03:14:29 +08:00
|
|
|
*/
|
2017-11-16 04:20:20 +08:00
|
|
|
static struct fpga_image_info *of_fpga_region_parse_ov(
|
|
|
|
struct fpga_region *region,
|
|
|
|
struct device_node *overlay)
|
2016-11-02 03:14:29 +08:00
|
|
|
{
|
2017-11-16 04:20:12 +08:00
|
|
|
struct device *dev = ®ion->dev;
|
2016-11-02 03:14:29 +08:00
|
|
|
struct fpga_image_info *info;
|
2017-11-16 04:20:12 +08:00
|
|
|
const char *firmware_name;
|
2016-11-02 03:14:29 +08:00
|
|
|
int ret;
|
|
|
|
|
2017-11-16 04:20:18 +08:00
|
|
|
if (region->info) {
|
|
|
|
dev_err(dev, "Region already has overlay applied.\n");
|
2017-11-16 04:20:20 +08:00
|
|
|
return ERR_PTR(-EINVAL);
|
2017-11-16 04:20:18 +08:00
|
|
|
}
|
|
|
|
|
2017-11-16 04:20:17 +08:00
|
|
|
/*
|
|
|
|
* Reject overlay if child FPGA Regions added in the overlay have
|
|
|
|
* firmware-name property (would mean that an FPGA region that has
|
|
|
|
* not been added to the live tree yet is doing FPGA programming).
|
|
|
|
*/
|
2017-11-16 04:20:20 +08:00
|
|
|
ret = child_regions_with_firmware(overlay);
|
2016-11-02 03:14:29 +08:00
|
|
|
if (ret)
|
2017-11-16 04:20:20 +08:00
|
|
|
return ERR_PTR(ret);
|
2016-11-02 03:14:29 +08:00
|
|
|
|
2017-11-16 04:20:17 +08:00
|
|
|
info = fpga_image_info_alloc(dev);
|
|
|
|
if (!info)
|
2017-11-16 04:20:20 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2017-11-16 04:20:17 +08:00
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
info->overlay = overlay;
|
2017-11-16 04:20:19 +08:00
|
|
|
|
2016-11-02 03:14:29 +08:00
|
|
|
/* Read FPGA region properties from the overlay */
|
2017-11-16 04:20:20 +08:00
|
|
|
if (of_property_read_bool(overlay, "partial-fpga-config"))
|
2016-11-02 03:14:29 +08:00
|
|
|
info->flags |= FPGA_MGR_PARTIAL_RECONFIG;
|
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
if (of_property_read_bool(overlay, "external-fpga-config"))
|
2016-11-02 03:14:29 +08:00
|
|
|
info->flags |= FPGA_MGR_EXTERNAL_CONFIG;
|
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
if (of_property_read_bool(overlay, "encrypted-fpga-config"))
|
2017-02-27 23:19:02 +08:00
|
|
|
info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM;
|
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
if (!of_property_read_string(overlay, "firmware-name",
|
2017-11-16 04:20:12 +08:00
|
|
|
&firmware_name)) {
|
|
|
|
info->firmware_name = devm_kstrdup(dev, firmware_name,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!info->firmware_name)
|
2017-11-16 04:20:20 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2017-11-16 04:20:12 +08:00
|
|
|
}
|
2016-11-02 03:14:29 +08:00
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
of_property_read_u32(overlay, "region-unfreeze-timeout-us",
|
2016-11-02 03:14:29 +08:00
|
|
|
&info->enable_timeout_us);
|
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
of_property_read_u32(overlay, "region-freeze-timeout-us",
|
2016-11-02 03:14:29 +08:00
|
|
|
&info->disable_timeout_us);
|
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
of_property_read_u32(overlay, "config-complete-timeout-us",
|
2017-03-24 08:34:27 +08:00
|
|
|
&info->config_complete_timeout_us);
|
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
/* If overlay is not programming the FPGA, don't need FPGA image info */
|
|
|
|
if (!info->firmware_name) {
|
|
|
|
ret = 0;
|
|
|
|
goto ret_no_info;
|
2016-11-02 03:14:29 +08:00
|
|
|
}
|
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
/*
|
|
|
|
* If overlay informs us FPGA was externally programmed, specifying
|
|
|
|
* firmware here would be ambiguous.
|
|
|
|
*/
|
2017-11-16 04:20:12 +08:00
|
|
|
if (info->flags & FPGA_MGR_EXTERNAL_CONFIG) {
|
2017-11-16 04:20:20 +08:00
|
|
|
dev_err(dev, "error: specified firmware and external-fpga-config");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto ret_no_info;
|
2017-11-16 04:20:12 +08:00
|
|
|
}
|
2016-11-02 03:14:29 +08:00
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
return info;
|
|
|
|
ret_no_info:
|
|
|
|
fpga_image_info_free(info);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-16 04:20:22 +08:00
|
|
|
* of_fpga_region_notify_pre_apply - pre-apply overlay notification
|
2017-11-16 04:20:20 +08:00
|
|
|
*
|
|
|
|
* @region: FPGA region that the overlay was applied to
|
|
|
|
* @nd: overlay notification data
|
|
|
|
*
|
|
|
|
* Called when an overlay targeted to a FPGA Region is about to be applied.
|
|
|
|
* Parses the overlay for properties that influence how the FPGA will be
|
|
|
|
* programmed and does some checking. If the checks pass, programs the FPGA.
|
|
|
|
* If the checks fail, overlay is rejected and does not get added to the
|
|
|
|
* live tree.
|
|
|
|
*
|
|
|
|
* Returns 0 for success or negative error code for failure.
|
|
|
|
*/
|
2017-11-16 04:20:22 +08:00
|
|
|
static int of_fpga_region_notify_pre_apply(struct fpga_region *region,
|
|
|
|
struct of_overlay_notify_data *nd)
|
2017-11-16 04:20:20 +08:00
|
|
|
{
|
|
|
|
struct device *dev = ®ion->dev;
|
|
|
|
struct fpga_image_info *info;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (region->info) {
|
|
|
|
dev_err(dev, "Region already has overlay applied.\n");
|
2016-11-02 03:14:29 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
info = of_fpga_region_parse_ov(region, nd->overlay);
|
|
|
|
if (IS_ERR(info))
|
|
|
|
return PTR_ERR(info);
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
return 0;
|
2017-11-16 04:20:19 +08:00
|
|
|
|
2017-11-16 04:20:20 +08:00
|
|
|
region->info = info;
|
2017-11-16 04:20:19 +08:00
|
|
|
ret = fpga_region_program_fpga(region);
|
2017-11-16 04:20:12 +08:00
|
|
|
if (ret) {
|
2017-11-16 04:20:20 +08:00
|
|
|
/* error; reject overlay */
|
2017-11-16 04:20:12 +08:00
|
|
|
fpga_image_info_free(info);
|
|
|
|
region->info = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2016-11-02 03:14:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-16 04:20:22 +08:00
|
|
|
* of_fpga_region_notify_post_remove - post-remove overlay notification
|
2016-11-02 03:14:29 +08:00
|
|
|
*
|
|
|
|
* @region: FPGA region that was targeted by the overlay that was removed
|
|
|
|
* @nd: overlay notification data
|
|
|
|
*
|
|
|
|
* Called after an overlay has been removed if the overlay's target was a
|
|
|
|
* FPGA region.
|
|
|
|
*/
|
2017-11-16 04:20:22 +08:00
|
|
|
static void of_fpga_region_notify_post_remove(struct fpga_region *region,
|
|
|
|
struct of_overlay_notify_data *nd)
|
2016-11-02 03:14:29 +08:00
|
|
|
{
|
|
|
|
fpga_bridges_disable(®ion->bridge_list);
|
|
|
|
fpga_bridges_put(®ion->bridge_list);
|
2017-11-16 04:20:12 +08:00
|
|
|
fpga_image_info_free(region->info);
|
2016-11-02 03:14:29 +08:00
|
|
|
region->info = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* of_fpga_region_notify - reconfig notifier for dynamic DT changes
|
|
|
|
* @nb: notifier block
|
|
|
|
* @action: notifier action
|
|
|
|
* @arg: reconfig data
|
|
|
|
*
|
|
|
|
* This notifier handles programming a FPGA when a "firmware-name" property is
|
|
|
|
* added to a fpga-region.
|
|
|
|
*
|
|
|
|
* Returns NOTIFY_OK or error if FPGA programming fails.
|
|
|
|
*/
|
|
|
|
static int of_fpga_region_notify(struct notifier_block *nb,
|
|
|
|
unsigned long action, void *arg)
|
|
|
|
{
|
|
|
|
struct of_overlay_notify_data *nd = arg;
|
|
|
|
struct fpga_region *region;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case OF_OVERLAY_PRE_APPLY:
|
|
|
|
pr_debug("%s OF_OVERLAY_PRE_APPLY\n", __func__);
|
|
|
|
break;
|
|
|
|
case OF_OVERLAY_POST_APPLY:
|
|
|
|
pr_debug("%s OF_OVERLAY_POST_APPLY\n", __func__);
|
|
|
|
return NOTIFY_OK; /* not for us */
|
|
|
|
case OF_OVERLAY_PRE_REMOVE:
|
|
|
|
pr_debug("%s OF_OVERLAY_PRE_REMOVE\n", __func__);
|
|
|
|
return NOTIFY_OK; /* not for us */
|
|
|
|
case OF_OVERLAY_POST_REMOVE:
|
|
|
|
pr_debug("%s OF_OVERLAY_POST_REMOVE\n", __func__);
|
|
|
|
break;
|
|
|
|
default: /* should not happen */
|
|
|
|
return NOTIFY_OK;
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:20:22 +08:00
|
|
|
region = of_fpga_region_find(nd->target);
|
2016-11-02 03:14:29 +08:00
|
|
|
if (!region)
|
|
|
|
return NOTIFY_OK;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
switch (action) {
|
|
|
|
case OF_OVERLAY_PRE_APPLY:
|
2017-11-16 04:20:22 +08:00
|
|
|
ret = of_fpga_region_notify_pre_apply(region, nd);
|
2016-11-02 03:14:29 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OF_OVERLAY_POST_REMOVE:
|
2017-11-16 04:20:22 +08:00
|
|
|
of_fpga_region_notify_post_remove(region, nd);
|
2016-11-02 03:14:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
put_device(®ion->dev);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return notifier_from_errno(ret);
|
|
|
|
|
|
|
|
return NOTIFY_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block fpga_region_of_nb = {
|
|
|
|
.notifier_call = of_fpga_region_notify,
|
|
|
|
};
|
|
|
|
|
2017-11-16 04:20:23 +08:00
|
|
|
int fpga_region_register(struct device *dev, struct fpga_region *region)
|
2016-11-02 03:14:29 +08:00
|
|
|
{
|
|
|
|
int id, ret = 0;
|
|
|
|
|
|
|
|
id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
|
2017-11-16 04:20:23 +08:00
|
|
|
if (id < 0)
|
|
|
|
return id;
|
2016-11-02 03:14:29 +08:00
|
|
|
|
|
|
|
mutex_init(®ion->mutex);
|
|
|
|
INIT_LIST_HEAD(®ion->bridge_list);
|
|
|
|
device_initialize(®ion->dev);
|
|
|
|
region->dev.class = fpga_region_class;
|
|
|
|
region->dev.parent = dev;
|
2017-11-16 04:20:23 +08:00
|
|
|
region->dev.of_node = dev->of_node;
|
2016-11-02 03:14:29 +08:00
|
|
|
region->dev.id = id;
|
|
|
|
dev_set_drvdata(dev, region);
|
|
|
|
|
|
|
|
ret = dev_set_name(®ion->dev, "region%d", id);
|
|
|
|
if (ret)
|
|
|
|
goto err_remove;
|
|
|
|
|
|
|
|
ret = device_add(®ion->dev);
|
|
|
|
if (ret)
|
|
|
|
goto err_remove;
|
|
|
|
|
2017-11-16 04:20:23 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_remove:
|
|
|
|
ida_simple_remove(&fpga_region_ida, id);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(fpga_region_register);
|
|
|
|
|
|
|
|
int fpga_region_unregister(struct fpga_region *region)
|
|
|
|
{
|
|
|
|
device_unregister(®ion->dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(fpga_region_unregister);
|
|
|
|
|
|
|
|
static int of_fpga_region_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct device_node *np = dev->of_node;
|
|
|
|
struct fpga_region *region;
|
|
|
|
struct fpga_manager *mgr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Find the FPGA mgr specified by region or parent region. */
|
|
|
|
mgr = of_fpga_region_get_mgr(np);
|
|
|
|
if (IS_ERR(mgr))
|
|
|
|
return -EPROBE_DEFER;
|
|
|
|
|
|
|
|
region = devm_kzalloc(dev, sizeof(*region), GFP_KERNEL);
|
|
|
|
if (!region) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto eprobe_mgr_put;
|
|
|
|
}
|
|
|
|
|
|
|
|
region->mgr = mgr;
|
|
|
|
|
|
|
|
/* Specify how to get bridges for this type of region. */
|
|
|
|
region->get_bridges = of_fpga_region_get_bridges;
|
|
|
|
|
|
|
|
ret = fpga_region_register(dev, region);
|
|
|
|
if (ret)
|
|
|
|
goto eprobe_mgr_put;
|
|
|
|
|
2016-11-02 03:14:29 +08:00
|
|
|
of_platform_populate(np, fpga_region_of_match, NULL, ®ion->dev);
|
|
|
|
|
|
|
|
dev_info(dev, "FPGA Region probed\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2017-11-16 04:20:23 +08:00
|
|
|
eprobe_mgr_put:
|
2017-11-16 04:20:16 +08:00
|
|
|
fpga_mgr_put(mgr);
|
2016-11-02 03:14:29 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:20:22 +08:00
|
|
|
static int of_fpga_region_remove(struct platform_device *pdev)
|
2016-11-02 03:14:29 +08:00
|
|
|
{
|
|
|
|
struct fpga_region *region = platform_get_drvdata(pdev);
|
|
|
|
|
2017-11-16 04:20:23 +08:00
|
|
|
fpga_region_unregister(region);
|
2017-11-16 04:20:16 +08:00
|
|
|
fpga_mgr_put(region->mgr);
|
2016-11-02 03:14:29 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:20:22 +08:00
|
|
|
static struct platform_driver of_fpga_region_driver = {
|
|
|
|
.probe = of_fpga_region_probe,
|
|
|
|
.remove = of_fpga_region_remove,
|
2016-11-02 03:14:29 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "fpga-region",
|
|
|
|
.of_match_table = of_match_ptr(fpga_region_of_match),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void fpga_region_dev_release(struct device *dev)
|
|
|
|
{
|
|
|
|
struct fpga_region *region = to_fpga_region(dev);
|
|
|
|
|
|
|
|
ida_simple_remove(&fpga_region_ida, region->dev.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fpga_region_init - init function for fpga_region class
|
|
|
|
* Creates the fpga_region class and registers a reconfig notifier.
|
|
|
|
*/
|
|
|
|
static int __init fpga_region_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
fpga_region_class = class_create(THIS_MODULE, "fpga_region");
|
|
|
|
if (IS_ERR(fpga_region_class))
|
|
|
|
return PTR_ERR(fpga_region_class);
|
|
|
|
|
|
|
|
fpga_region_class->dev_release = fpga_region_dev_release;
|
|
|
|
|
|
|
|
ret = of_overlay_notifier_register(&fpga_region_of_nb);
|
|
|
|
if (ret)
|
|
|
|
goto err_class;
|
|
|
|
|
2017-11-16 04:20:22 +08:00
|
|
|
ret = platform_driver_register(&of_fpga_region_driver);
|
2016-11-02 03:14:29 +08:00
|
|
|
if (ret)
|
|
|
|
goto err_plat;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_plat:
|
|
|
|
of_overlay_notifier_unregister(&fpga_region_of_nb);
|
|
|
|
err_class:
|
|
|
|
class_destroy(fpga_region_class);
|
|
|
|
ida_destroy(&fpga_region_ida);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit fpga_region_exit(void)
|
|
|
|
{
|
2017-11-16 04:20:22 +08:00
|
|
|
platform_driver_unregister(&of_fpga_region_driver);
|
2016-11-02 03:14:29 +08:00
|
|
|
of_overlay_notifier_unregister(&fpga_region_of_nb);
|
|
|
|
class_destroy(fpga_region_class);
|
|
|
|
ida_destroy(&fpga_region_ida);
|
|
|
|
}
|
|
|
|
|
|
|
|
subsys_initcall(fpga_region_init);
|
|
|
|
module_exit(fpga_region_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("FPGA Region");
|
2017-11-16 04:20:21 +08:00
|
|
|
MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
|
2016-11-02 03:14:29 +08:00
|
|
|
MODULE_LICENSE("GPL v2");
|