linux/drivers/fpga/fpga-region.c
<<
>>
Prefs
   1/*
   2 * FPGA Region - Device Tree support for FPGA programming under Linux
   3 *
   4 *  Copyright (C) 2013-2016 Altera Corporation
   5 *  Copyright (C) 2017 Intel Corporation
   6 *
   7 * This program is free software; you can redistribute it and/or modify it
   8 * under the terms and conditions of the GNU General Public License,
   9 * version 2, as published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope it will be useful, but WITHOUT
  12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  14 * more details.
  15 *
  16 * You should have received a copy of the GNU General Public License along with
  17 * this program.  If not, see <http://www.gnu.org/licenses/>.
  18 */
  19
  20#include <linux/fpga/fpga-bridge.h>
  21#include <linux/fpga/fpga-mgr.h>
  22#include <linux/fpga/fpga-region.h>
  23#include <linux/idr.h>
  24#include <linux/kernel.h>
  25#include <linux/list.h>
  26#include <linux/module.h>
  27#include <linux/slab.h>
  28#include <linux/spinlock.h>
  29
  30static DEFINE_IDA(fpga_region_ida);
  31static struct class *fpga_region_class;
  32
  33struct fpga_region *fpga_region_class_find(
  34        struct device *start, const void *data,
  35        int (*match)(struct device *, const void *))
  36{
  37        struct device *dev;
  38
  39        dev = class_find_device(fpga_region_class, start, data, match);
  40        if (!dev)
  41                return NULL;
  42
  43        return to_fpga_region(dev);
  44}
  45EXPORT_SYMBOL_GPL(fpga_region_class_find);
  46
  47/**
  48 * fpga_region_get - get an exclusive reference to a fpga region
  49 * @region: FPGA Region struct
  50 *
  51 * Caller should call fpga_region_put() when done with region.
  52 *
  53 * Return fpga_region struct if successful.
  54 * Return -EBUSY if someone already has a reference to the region.
  55 * Return -ENODEV if @np is not a FPGA Region.
  56 */
  57static struct fpga_region *fpga_region_get(struct fpga_region *region)
  58{
  59        struct device *dev = &region->dev;
  60
  61        if (!mutex_trylock(&region->mutex)) {
  62                dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
  63                return ERR_PTR(-EBUSY);
  64        }
  65
  66        get_device(dev);
  67        if (!try_module_get(dev->parent->driver->owner)) {
  68                put_device(dev);
  69                mutex_unlock(&region->mutex);
  70                return ERR_PTR(-ENODEV);
  71        }
  72
  73        dev_dbg(dev, "get\n");
  74
  75        return region;
  76}
  77
  78/**
  79 * fpga_region_put - release a reference to a region
  80 *
  81 * @region: FPGA region
  82 */
  83static void fpga_region_put(struct fpga_region *region)
  84{
  85        struct device *dev = &region->dev;
  86
  87        dev_dbg(dev, "put\n");
  88
  89        module_put(dev->parent->driver->owner);
  90        put_device(dev);
  91        mutex_unlock(&region->mutex);
  92}
  93
  94/**
  95 * fpga_region_program_fpga - program FPGA
  96 * @region: FPGA region
  97 * Program an FPGA using fpga image info (region->info).
  98 * Return 0 for success or negative error code.
  99 */
 100int fpga_region_program_fpga(struct fpga_region *region)
 101{
 102        struct device *dev = &region->dev;
 103        struct fpga_image_info *info = region->info;
 104        int ret;
 105
 106        region = fpga_region_get(region);
 107        if (IS_ERR(region)) {
 108                dev_err(dev, "failed to get FPGA region\n");
 109                return PTR_ERR(region);
 110        }
 111
 112        ret = fpga_mgr_lock(region->mgr);
 113        if (ret) {
 114                dev_err(dev, "FPGA manager is busy\n");
 115                goto err_put_region;
 116        }
 117
 118        /*
 119         * In some cases, we already have a list of bridges in the
 120         * fpga region struct.  Or we don't have any bridges.
 121         */
 122        if (region->get_bridges) {
 123                ret = region->get_bridges(region);
 124                if (ret) {
 125                        dev_err(dev, "failed to get fpga region bridges\n");
 126                        goto err_unlock_mgr;
 127                }
 128        }
 129
 130        ret = fpga_bridges_disable(&region->bridge_list);
 131        if (ret) {
 132                dev_err(dev, "failed to disable bridges\n");
 133                goto err_put_br;
 134        }
 135
 136        ret = fpga_mgr_load(region->mgr, info);
 137        if (ret) {
 138                dev_err(dev, "failed to load FPGA image\n");
 139                goto err_put_br;
 140        }
 141
 142        ret = fpga_bridges_enable(&region->bridge_list);
 143        if (ret) {
 144                dev_err(dev, "failed to enable region bridges\n");
 145                goto err_put_br;
 146        }
 147
 148        fpga_mgr_unlock(region->mgr);
 149        fpga_region_put(region);
 150
 151        return 0;
 152
 153err_put_br:
 154        if (region->get_bridges)
 155                fpga_bridges_put(&region->bridge_list);
 156err_unlock_mgr:
 157        fpga_mgr_unlock(region->mgr);
 158err_put_region:
 159        fpga_region_put(region);
 160
 161        return ret;
 162}
 163EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
 164
 165int fpga_region_register(struct device *dev, struct fpga_region *region)
 166{
 167        int id, ret = 0;
 168
 169        id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
 170        if (id < 0)
 171                return id;
 172
 173        mutex_init(&region->mutex);
 174        INIT_LIST_HEAD(&region->bridge_list);
 175        device_initialize(&region->dev);
 176        region->dev.groups = region->groups;
 177        region->dev.class = fpga_region_class;
 178        region->dev.parent = dev;
 179        region->dev.of_node = dev->of_node;
 180        region->dev.id = id;
 181        dev_set_drvdata(dev, region);
 182
 183        ret = dev_set_name(&region->dev, "region%d", id);
 184        if (ret)
 185                goto err_remove;
 186
 187        ret = device_add(&region->dev);
 188        if (ret)
 189                goto err_remove;
 190
 191        return 0;
 192
 193err_remove:
 194        ida_simple_remove(&fpga_region_ida, id);
 195        return ret;
 196}
 197EXPORT_SYMBOL_GPL(fpga_region_register);
 198
 199int fpga_region_unregister(struct fpga_region *region)
 200{
 201        device_unregister(&region->dev);
 202
 203        return 0;
 204}
 205EXPORT_SYMBOL_GPL(fpga_region_unregister);
 206
 207static void fpga_region_dev_release(struct device *dev)
 208{
 209        struct fpga_region *region = to_fpga_region(dev);
 210
 211        ida_simple_remove(&fpga_region_ida, region->dev.id);
 212}
 213
 214/**
 215 * fpga_region_init - init function for fpga_region class
 216 * Creates the fpga_region class and registers a reconfig notifier.
 217 */
 218static int __init fpga_region_init(void)
 219{
 220        fpga_region_class = class_create(THIS_MODULE, "fpga_region");
 221        if (IS_ERR(fpga_region_class))
 222                return PTR_ERR(fpga_region_class);
 223
 224        fpga_region_class->dev_release = fpga_region_dev_release;
 225
 226        return 0;
 227}
 228
 229static void __exit fpga_region_exit(void)
 230{
 231        class_destroy(fpga_region_class);
 232        ida_destroy(&fpga_region_ida);
 233}
 234
 235subsys_initcall(fpga_region_init);
 236module_exit(fpga_region_exit);
 237
 238MODULE_DESCRIPTION("FPGA Region");
 239MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
 240MODULE_LICENSE("GPL v2");
 241