]> Git Repo - J-linux.git/blobdiff - drivers/fpga/fpga-mgr.c
Merge tag 'edac_for_4.18' of git://git.kernel.org/pub/scm/linux/kernel/git/bp/bp
[J-linux.git] / drivers / fpga / fpga-mgr.c
index 188ffefa3cc3e25c3befebde04aec83e6c80b0fc..c1564cf827fe0cf8d92abba39bba703b22e71ee8 100644 (file)
@@ -1,22 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * FPGA Manager Core
  *
  *  Copyright (C) 2013-2015 Altera Corporation
+ *  Copyright (C) 2017 Intel Corporation
  *
  * With code from the mailing list:
  * Copyright (C) 2013 Xilinx, Inc.
- *
- * 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/firmware.h>
 #include <linux/fpga/fpga-mgr.h>
 static DEFINE_IDA(fpga_mgr_ida);
 static struct class *fpga_mgr_class;
 
+/**
+ * fpga_image_info_alloc - Allocate a FPGA image info struct
+ * @dev: owning device
+ *
+ * Return: struct fpga_image_info or NULL
+ */
+struct fpga_image_info *fpga_image_info_alloc(struct device *dev)
+{
+       struct fpga_image_info *info;
+
+       get_device(dev);
+
+       info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
+       if (!info) {
+               put_device(dev);
+               return NULL;
+       }
+
+       info->dev = dev;
+
+       return info;
+}
+EXPORT_SYMBOL_GPL(fpga_image_info_alloc);
+
+/**
+ * fpga_image_info_free - Free a FPGA image info struct
+ * @info: FPGA image info struct to free
+ */
+void fpga_image_info_free(struct fpga_image_info *info)
+{
+       struct device *dev;
+
+       if (!info)
+               return;
+
+       dev = info->dev;
+       if (info->firmware_name)
+               devm_kfree(dev, info->firmware_name);
+
+       devm_kfree(dev, info);
+       put_device(dev);
+}
+EXPORT_SYMBOL_GPL(fpga_image_info_free);
+
 /*
  * Call the low level driver's write_init function.  This will do the
  * device-specific things to get the FPGA into the state where it is ready to
@@ -137,8 +171,9 @@ static int fpga_mgr_write_complete(struct fpga_manager *mgr,
  *
  * Return: 0 on success, negative error code otherwise.
  */
-int fpga_mgr_buf_load_sg(struct fpga_manager *mgr, struct fpga_image_info *info,
-                        struct sg_table *sgt)
+static int fpga_mgr_buf_load_sg(struct fpga_manager *mgr,
+                               struct fpga_image_info *info,
+                               struct sg_table *sgt)
 {
        int ret;
 
@@ -170,7 +205,6 @@ int fpga_mgr_buf_load_sg(struct fpga_manager *mgr, struct fpga_image_info *info,
 
        return fpga_mgr_write_complete(mgr, info);
 }
-EXPORT_SYMBOL_GPL(fpga_mgr_buf_load_sg);
 
 static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr,
                                    struct fpga_image_info *info,
@@ -199,7 +233,7 @@ static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr,
 /**
  * fpga_mgr_buf_load - load fpga from image in buffer
  * @mgr:       fpga manager
- * @flags:     flags setting fpga confuration modes
+ * @info:      fpga image info
  * @buf:       buffer contain fpga image
  * @count:     byte count of buf
  *
@@ -210,8 +244,9 @@ static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr,
  *
  * Return: 0 on success, negative error code otherwise.
  */
-int fpga_mgr_buf_load(struct fpga_manager *mgr, struct fpga_image_info *info,
-                     const char *buf, size_t count)
+static int fpga_mgr_buf_load(struct fpga_manager *mgr,
+                            struct fpga_image_info *info,
+                            const char *buf, size_t count)
 {
        struct page **pages;
        struct sg_table sgt;
@@ -266,7 +301,6 @@ int fpga_mgr_buf_load(struct fpga_manager *mgr, struct fpga_image_info *info,
 
        return rc;
 }
-EXPORT_SYMBOL_GPL(fpga_mgr_buf_load);
 
 /**
  * fpga_mgr_firmware_load - request firmware and load to fpga
@@ -282,9 +316,9 @@ EXPORT_SYMBOL_GPL(fpga_mgr_buf_load);
  *
  * Return: 0 on success, negative error code otherwise.
  */
-int fpga_mgr_firmware_load(struct fpga_manager *mgr,
-                          struct fpga_image_info *info,
-                          const char *image_name)
+static int fpga_mgr_firmware_load(struct fpga_manager *mgr,
+                                 struct fpga_image_info *info,
+                                 const char *image_name)
 {
        struct device *dev = &mgr->dev;
        const struct firmware *fw;
@@ -307,7 +341,28 @@ int fpga_mgr_firmware_load(struct fpga_manager *mgr,
 
        return ret;
 }
-EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load);
+
+/**
+ * fpga_mgr_load - load FPGA from scatter/gather table, buffer, or firmware
+ * @mgr:       fpga manager
+ * @info:      fpga image information.
+ *
+ * Load the FPGA from an image which is indicated in @info.  If successful, the
+ * FPGA ends up in operating mode.
+ *
+ * Return: 0 on success, negative error code otherwise.
+ */
+int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info)
+{
+       if (info->sgt)
+               return fpga_mgr_buf_load_sg(mgr, info, info->sgt);
+       if (info->buf && info->count)
+               return fpga_mgr_buf_load(mgr, info, info->buf, info->count);
+       if (info->firmware_name)
+               return fpga_mgr_firmware_load(mgr, info, info->firmware_name);
+       return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_load);
 
 static const char * const state_str[] = {
        [FPGA_MGR_STATE_UNKNOWN] =              "unknown",
@@ -364,28 +419,17 @@ ATTRIBUTE_GROUPS(fpga_mgr);
 static struct fpga_manager *__fpga_mgr_get(struct device *dev)
 {
        struct fpga_manager *mgr;
-       int ret = -ENODEV;
 
        mgr = to_fpga_manager(dev);
-       if (!mgr)
-               goto err_dev;
-
-       /* Get exclusive use of fpga manager */
-       if (!mutex_trylock(&mgr->ref_mutex)) {
-               ret = -EBUSY;
-               goto err_dev;
-       }
 
        if (!try_module_get(dev->parent->driver->owner))
-               goto err_ll_mod;
+               goto err_dev;
 
        return mgr;
 
-err_ll_mod:
-       mutex_unlock(&mgr->ref_mutex);
 err_dev:
        put_device(dev);
-       return ERR_PTR(ret);
+       return ERR_PTR(-ENODEV);
 }
 
 static int fpga_mgr_dev_match(struct device *dev, const void *data)
@@ -394,11 +438,9 @@ static int fpga_mgr_dev_match(struct device *dev, const void *data)
 }
 
 /**
- * fpga_mgr_get - get an exclusive reference to a fpga mgr
+ * fpga_mgr_get - Given a device, get a reference to a fpga mgr.
  * @dev:       parent device that fpga mgr was registered with
  *
- * Given a device, get an exclusive reference to a fpga mgr.
- *
  * Return: fpga manager struct or IS_ERR() condition containing error code.
  */
 struct fpga_manager *fpga_mgr_get(struct device *dev)
@@ -418,10 +460,9 @@ static int fpga_mgr_of_node_match(struct device *dev, const void *data)
 }
 
 /**
- * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
- * @node:      device node
+ * of_fpga_mgr_get - Given a device node, get a reference to a fpga mgr.
  *
- * Given a device node, get an exclusive reference to a fpga mgr.
+ * @node:      device node
  *
  * Return: fpga manager struct or IS_ERR() condition containing error code.
  */
@@ -445,23 +486,55 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
 void fpga_mgr_put(struct fpga_manager *mgr)
 {
        module_put(mgr->dev.parent->driver->owner);
-       mutex_unlock(&mgr->ref_mutex);
        put_device(&mgr->dev);
 }
 EXPORT_SYMBOL_GPL(fpga_mgr_put);
 
 /**
- * fpga_mgr_register - register a low level fpga manager driver
+ * fpga_mgr_lock - Lock FPGA manager for exclusive use
+ * @mgr:       fpga manager
+ *
+ * Given a pointer to FPGA Manager (from fpga_mgr_get() or
+ * of_fpga_mgr_put()) attempt to get the mutex. The user should call
+ * fpga_mgr_lock() and verify that it returns 0 before attempting to
+ * program the FPGA.  Likewise, the user should call fpga_mgr_unlock
+ * when done programming the FPGA.
+ *
+ * Return: 0 for success or -EBUSY
+ */
+int fpga_mgr_lock(struct fpga_manager *mgr)
+{
+       if (!mutex_trylock(&mgr->ref_mutex)) {
+               dev_err(&mgr->dev, "FPGA manager is in use.\n");
+               return -EBUSY;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_lock);
+
+/**
+ * fpga_mgr_unlock - Unlock FPGA manager after done programming
+ * @mgr:       fpga manager
+ */
+void fpga_mgr_unlock(struct fpga_manager *mgr)
+{
+       mutex_unlock(&mgr->ref_mutex);
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
+
+/**
+ * fpga_mgr_create - create and initialize a FPGA manager struct
  * @dev:       fpga manager device from pdev
  * @name:      fpga manager name
  * @mops:      pointer to structure of fpga manager ops
  * @priv:      fpga manager private data
  *
- * Return: 0 on success, negative error code otherwise.
+ * Return: pointer to struct fpga_manager or NULL
  */
-int fpga_mgr_register(struct device *dev, const char *name,
-                     const struct fpga_manager_ops *mops,
-                     void *priv)
+struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
+                                    const struct fpga_manager_ops *mops,
+                                    void *priv)
 {
        struct fpga_manager *mgr;
        int id, ret;
@@ -470,17 +543,17 @@ int fpga_mgr_register(struct device *dev, const char *name,
            !mops->write_init || (!mops->write && !mops->write_sg) ||
            (mops->write && mops->write_sg)) {
                dev_err(dev, "Attempt to register without fpga_manager_ops\n");
-               return -EINVAL;
+               return NULL;
        }
 
        if (!name || !strlen(name)) {
                dev_err(dev, "Attempt to register with no name!\n");
-               return -EINVAL;
+               return NULL;
        }
 
        mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
        if (!mgr)
-               return -ENOMEM;
+               return NULL;
 
        id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
        if (id < 0) {
@@ -494,24 +567,56 @@ int fpga_mgr_register(struct device *dev, const char *name,
        mgr->mops = mops;
        mgr->priv = priv;
 
-       /*
-        * Initialize framework state by requesting low level driver read state
-        * from device.  FPGA may be in reset mode or may have been programmed
-        * by bootloader or EEPROM.
-        */
-       mgr->state = mgr->mops->state(mgr);
-
        device_initialize(&mgr->dev);
        mgr->dev.class = fpga_mgr_class;
+       mgr->dev.groups = mops->groups;
        mgr->dev.parent = dev;
        mgr->dev.of_node = dev->of_node;
        mgr->dev.id = id;
-       dev_set_drvdata(dev, mgr);
 
        ret = dev_set_name(&mgr->dev, "fpga%d", id);
        if (ret)
                goto error_device;
 
+       return mgr;
+
+error_device:
+       ida_simple_remove(&fpga_mgr_ida, id);
+error_kfree:
+       kfree(mgr);
+
+       return NULL;
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_create);
+
+/**
+ * fpga_mgr_free - deallocate a FPGA manager
+ * @mgr:       fpga manager struct created by fpga_mgr_create
+ */
+void fpga_mgr_free(struct fpga_manager *mgr)
+{
+       ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
+       kfree(mgr);
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_free);
+
+/**
+ * fpga_mgr_register - register a FPGA manager
+ * @mgr:       fpga manager struct created by fpga_mgr_create
+ *
+ * Return: 0 on success, negative error code otherwise.
+ */
+int fpga_mgr_register(struct fpga_manager *mgr)
+{
+       int ret;
+
+       /*
+        * Initialize framework state by requesting low level driver read state
+        * from device.  FPGA may be in reset mode or may have been programmed
+        * by bootloader or EEPROM.
+        */
+       mgr->state = mgr->mops->state(mgr);
+
        ret = device_add(&mgr->dev);
        if (ret)
                goto error_device;
@@ -521,22 +626,18 @@ int fpga_mgr_register(struct device *dev, const char *name,
        return 0;
 
 error_device:
-       ida_simple_remove(&fpga_mgr_ida, id);
-error_kfree:
-       kfree(mgr);
+       ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
 
        return ret;
 }
 EXPORT_SYMBOL_GPL(fpga_mgr_register);
 
 /**
- * fpga_mgr_unregister - unregister a low level fpga manager driver
- * @dev:       fpga manager device from pdev
+ * fpga_mgr_unregister - unregister and free a FPGA manager
+ * @mgr:       fpga manager struct
  */
-void fpga_mgr_unregister(struct device *dev)
+void fpga_mgr_unregister(struct fpga_manager *mgr)
 {
-       struct fpga_manager *mgr = dev_get_drvdata(dev);
-
        dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
 
        /*
@@ -554,8 +655,7 @@ static void fpga_mgr_dev_release(struct device *dev)
 {
        struct fpga_manager *mgr = to_fpga_manager(dev);
 
-       ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
-       kfree(mgr);
+       fpga_mgr_free(mgr);
 }
 
 static int __init fpga_mgr_class_init(void)
@@ -578,7 +678,7 @@ static void __exit fpga_mgr_class_exit(void)
        ida_destroy(&fpga_mgr_ida);
 }
 
-MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
+MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
 MODULE_DESCRIPTION("FPGA manager framework");
 MODULE_LICENSE("GPL v2");
 
This page took 0.040958 seconds and 4 git commands to generate.