linux/drivers/fpga/fpga-mgr.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * FPGA Manager Core
   4 *
   5 *  Copyright (C) 2013-2015 Altera Corporation
   6 *  Copyright (C) 2017 Intel Corporation
   7 *
   8 * With code from the mailing list:
   9 * Copyright (C) 2013 Xilinx, Inc.
  10 */
  11#include <linux/dma-buf.h>
  12#include <linux/dma-map-ops.h>
  13#include <linux/kernel.h>
  14#include <linux/firmware.h>
  15#include <linux/fpga/fpga-mgr.h>
  16#include <linux/idr.h>
  17#include <linux/module.h>
  18#include <linux/of.h>
  19#include <linux/mutex.h>
  20#include <linux/slab.h>
  21#include <linux/scatterlist.h>
  22#include <linux/highmem.h>
  23
  24static DEFINE_IDA(fpga_mgr_ida);
  25static struct class *fpga_mgr_class;
  26
  27/**
  28 * fpga_image_info_alloc - Allocate a FPGA image info struct
  29 * @dev: owning device
  30 *
  31 * Return: struct fpga_image_info or NULL
  32 */
  33struct fpga_image_info *fpga_image_info_alloc(struct device *dev)
  34{
  35        struct fpga_image_info *info;
  36
  37        get_device(dev);
  38
  39        info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  40        if (!info) {
  41                put_device(dev);
  42                return NULL;
  43        }
  44
  45        info->dev = dev;
  46
  47        return info;
  48}
  49EXPORT_SYMBOL_GPL(fpga_image_info_alloc);
  50
  51/**
  52 * fpga_image_info_free - Free a FPGA image info struct
  53 * @info: FPGA image info struct to free
  54 */
  55void fpga_image_info_free(struct fpga_image_info *info)
  56{
  57        struct device *dev;
  58
  59        if (!info)
  60                return;
  61
  62        dev = info->dev;
  63        if (info->firmware_name)
  64                devm_kfree(dev, info->firmware_name);
  65
  66        devm_kfree(dev, info);
  67        put_device(dev);
  68}
  69EXPORT_SYMBOL_GPL(fpga_image_info_free);
  70
  71/*
  72 * Call the low level driver's write_init function.  This will do the
  73 * device-specific things to get the FPGA into the state where it is ready to
  74 * receive an FPGA image. The low level driver only gets to see the first
  75 * initial_header_size bytes in the buffer.
  76 */
  77static int fpga_mgr_write_init_buf(struct fpga_manager *mgr,
  78                                   struct fpga_image_info *info,
  79                                   const char *buf, size_t count)
  80{
  81        int ret;
  82
  83        mgr->state = FPGA_MGR_STATE_WRITE_INIT;
  84        if (!mgr->mops->initial_header_size)
  85                ret = mgr->mops->write_init(mgr, info, NULL, 0);
  86        else
  87                ret = mgr->mops->write_init(
  88                    mgr, info, buf, min(mgr->mops->initial_header_size, count));
  89
  90        if (ret) {
  91                dev_err(&mgr->dev, "Error preparing FPGA for writing\n");
  92                mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
  93                return ret;
  94        }
  95
  96        return 0;
  97}
  98
  99static int fpga_mgr_write_init_sg(struct fpga_manager *mgr,
 100                                  struct fpga_image_info *info,
 101                                  struct sg_table *sgt)
 102{
 103        struct sg_mapping_iter miter;
 104        size_t len;
 105        char *buf;
 106        int ret;
 107
 108        if (!mgr->mops->initial_header_size)
 109                return fpga_mgr_write_init_buf(mgr, info, NULL, 0);
 110
 111        /*
 112         * First try to use miter to map the first fragment to access the
 113         * header, this is the typical path.
 114         */
 115        sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
 116        if (sg_miter_next(&miter) &&
 117            miter.length >= mgr->mops->initial_header_size) {
 118                ret = fpga_mgr_write_init_buf(mgr, info, miter.addr,
 119                                              miter.length);
 120                sg_miter_stop(&miter);
 121                return ret;
 122        }
 123        sg_miter_stop(&miter);
 124
 125        /* Otherwise copy the fragments into temporary memory. */
 126        buf = kmalloc(mgr->mops->initial_header_size, GFP_KERNEL);
 127        if (!buf)
 128                return -ENOMEM;
 129
 130        len = sg_copy_to_buffer(sgt->sgl, sgt->nents, buf,
 131                                mgr->mops->initial_header_size);
 132        ret = fpga_mgr_write_init_buf(mgr, info, buf, len);
 133
 134        kfree(buf);
 135
 136        return ret;
 137}
 138
 139/*
 140 * After all the FPGA image has been written, do the device specific steps to
 141 * finish and set the FPGA into operating mode.
 142 */
 143static int fpga_mgr_write_complete(struct fpga_manager *mgr,
 144                                   struct fpga_image_info *info)
 145{
 146        int ret;
 147
 148        mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
 149        ret = mgr->mops->write_complete(mgr, info);
 150        if (ret) {
 151                dev_err(&mgr->dev, "Error after writing image data to FPGA\n");
 152                mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
 153                return ret;
 154        }
 155        mgr->state = FPGA_MGR_STATE_OPERATING;
 156
 157        return 0;
 158}
 159
 160/**
 161 * fpga_mgr_buf_load_sg - load fpga from image in buffer from a scatter list
 162 * @mgr:        fpga manager
 163 * @info:       fpga image specific information
 164 * @sgt:        scatterlist table
 165 *
 166 * Step the low level fpga manager through the device-specific steps of getting
 167 * an FPGA ready to be configured, writing the image to it, then doing whatever
 168 * post-configuration steps necessary.  This code assumes the caller got the
 169 * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is
 170 * not an error code.
 171 *
 172 * This is the preferred entry point for FPGA programming, it does not require
 173 * any contiguous kernel memory.
 174 *
 175 * Return: 0 on success, negative error code otherwise.
 176 */
 177static int fpga_mgr_buf_load_sg(struct fpga_manager *mgr,
 178                                struct fpga_image_info *info,
 179                                struct sg_table *sgt)
 180{
 181        int ret;
 182
 183        if (info->flags & FPGA_MGR_USERKEY_ENCRYPTED_BITSTREAM)
 184                memcpy(info->key, mgr->key, ENCRYPTED_KEY_LEN);
 185
 186        ret = fpga_mgr_write_init_sg(mgr, info, sgt);
 187        if (ret)
 188                return ret;
 189
 190        /* Write the FPGA image to the FPGA. */
 191        mgr->state = FPGA_MGR_STATE_WRITE;
 192        if (mgr->mops->write_sg) {
 193                ret = mgr->mops->write_sg(mgr, sgt);
 194        } else {
 195                struct sg_mapping_iter miter;
 196
 197                sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
 198                while (sg_miter_next(&miter)) {
 199                        ret = mgr->mops->write(mgr, miter.addr, miter.length);
 200                        if (ret)
 201                                break;
 202                }
 203                sg_miter_stop(&miter);
 204        }
 205
 206        if (ret) {
 207                dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
 208                mgr->state = FPGA_MGR_STATE_WRITE_ERR;
 209                return ret;
 210        }
 211
 212        return fpga_mgr_write_complete(mgr, info);
 213}
 214
 215static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr,
 216                                    struct fpga_image_info *info,
 217                                    const char *buf, size_t count)
 218{
 219        int ret;
 220
 221        ret = fpga_mgr_write_init_buf(mgr, info, buf, count);
 222        if (ret)
 223                return ret;
 224
 225        /*
 226         * Write the FPGA image to the FPGA.
 227         */
 228        mgr->state = FPGA_MGR_STATE_WRITE;
 229        ret = mgr->mops->write(mgr, buf, count);
 230        if (ret) {
 231                dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
 232                mgr->state = FPGA_MGR_STATE_WRITE_ERR;
 233                return ret;
 234        }
 235
 236        return fpga_mgr_write_complete(mgr, info);
 237}
 238
 239/**
 240 * fpga_mgr_buf_load - load fpga from image in buffer
 241 * @mgr:        fpga manager
 242 * @info:       fpga image info
 243 * @buf:        buffer contain fpga image
 244 * @count:      byte count of buf
 245 *
 246 * Step the low level fpga manager through the device-specific steps of getting
 247 * an FPGA ready to be configured, writing the image to it, then doing whatever
 248 * post-configuration steps necessary.  This code assumes the caller got the
 249 * mgr pointer from of_fpga_mgr_get() and checked that it is not an error code.
 250 *
 251 * Return: 0 on success, negative error code otherwise.
 252 */
 253static int fpga_mgr_buf_load(struct fpga_manager *mgr,
 254                             struct fpga_image_info *info,
 255                             const char *buf, size_t count)
 256{
 257        struct page **pages;
 258        struct sg_table sgt;
 259        const void *p;
 260        int nr_pages;
 261        int index;
 262        int rc;
 263
 264        /*
 265         * This is just a fast path if the caller has already created a
 266         * contiguous kernel buffer and the driver doesn't require SG, non-SG
 267         * drivers will still work on the slow path.
 268         */
 269        if (mgr->mops->write)
 270                return fpga_mgr_buf_load_mapped(mgr, info, buf, count);
 271
 272        /*
 273         * Convert the linear kernel pointer into a sg_table of pages for use
 274         * by the driver.
 275         */
 276        nr_pages = DIV_ROUND_UP((unsigned long)buf + count, PAGE_SIZE) -
 277                   (unsigned long)buf / PAGE_SIZE;
 278        pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
 279        if (!pages)
 280                return -ENOMEM;
 281
 282        p = buf - offset_in_page(buf);
 283        for (index = 0; index < nr_pages; index++) {
 284                if (is_vmalloc_addr(p))
 285                        pages[index] = vmalloc_to_page(p);
 286                else
 287                        pages[index] = kmap_to_page((void *)p);
 288                if (!pages[index]) {
 289                        kfree(pages);
 290                        return -EFAULT;
 291                }
 292                p += PAGE_SIZE;
 293        }
 294
 295        /*
 296         * The temporary pages list is used to code share the merging algorithm
 297         * in sg_alloc_table_from_pages
 298         */
 299        rc = sg_alloc_table_from_pages(&sgt, pages, index, offset_in_page(buf),
 300                                       count, GFP_KERNEL);
 301        kfree(pages);
 302        if (rc)
 303                return rc;
 304
 305        rc = fpga_mgr_buf_load_sg(mgr, info, &sgt);
 306        sg_free_table(&sgt);
 307
 308        return rc;
 309}
 310
 311static int fpga_dmabuf_load(struct fpga_manager *mgr,
 312                            struct fpga_image_info *info)
 313{
 314        struct dma_buf_attachment *attach;
 315        struct sg_table *sgt;
 316        int ret;
 317
 318        /* create attachment for dmabuf with the user device */
 319        attach = dma_buf_attach(mgr->dmabuf, &mgr->dev);
 320        if (IS_ERR(attach)) {
 321                pr_err("failed to attach dmabuf\n");
 322                ret = PTR_ERR(attach);
 323                goto fail_put;
 324        }
 325
 326        sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
 327        if (IS_ERR(sgt)) {
 328                ret = PTR_ERR(sgt);
 329                goto fail_detach;
 330        }
 331
 332        info->sgt = sgt;
 333        ret = fpga_mgr_buf_load_sg(mgr, info, info->sgt);
 334        dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
 335
 336fail_detach:
 337        dma_buf_detach(mgr->dmabuf, attach);
 338fail_put:
 339        dma_buf_put(mgr->dmabuf);
 340
 341        return ret;
 342}
 343
 344/**
 345 * fpga_mgr_firmware_load - request firmware and load to fpga
 346 * @mgr:        fpga manager
 347 * @info:       fpga image specific information
 348 * @image_name: name of image file on the firmware search path
 349 *
 350 * Request an FPGA image using the firmware class, then write out to the FPGA.
 351 * Update the state before each step to provide info on what step failed if
 352 * there is a failure.  This code assumes the caller got the mgr pointer
 353 * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
 354 * code.
 355 *
 356 * Return: 0 on success, negative error code otherwise.
 357 */
 358static int fpga_mgr_firmware_load(struct fpga_manager *mgr,
 359                                  struct fpga_image_info *info,
 360                                  const char *image_name)
 361{
 362        struct device *dev = &mgr->dev;
 363        const struct firmware *fw;
 364        int ret;
 365
 366        dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
 367
 368        mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
 369
 370        /* flags indicates whether to do full or partial reconfiguration */
 371        info->flags = mgr->flags;
 372        memcpy(info->key, mgr->key, ENCRYPTED_KEY_LEN);
 373
 374        ret = request_firmware(&fw, image_name, dev);
 375        if (ret) {
 376                mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
 377                dev_err(dev, "Error requesting firmware %s\n", image_name);
 378                return ret;
 379        }
 380
 381        ret = fpga_mgr_buf_load(mgr, info, fw->data, fw->size);
 382
 383        release_firmware(fw);
 384
 385        return ret;
 386}
 387
 388/**
 389 * fpga_mgr_load - load FPGA from scatter/gather table, buffer, or firmware
 390 * @mgr:        fpga manager
 391 * @info:       fpga image information.
 392 *
 393 * Load the FPGA from an image which is indicated in @info.  If successful, the
 394 * FPGA ends up in operating mode.
 395 *
 396 * Return: 0 on success, negative error code otherwise.
 397 */
 398int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info)
 399{
 400        if (mgr->flags & FPGA_MGR_CONFIG_DMA_BUF)
 401                return fpga_dmabuf_load(mgr, info);
 402        if (info->sgt)
 403                return fpga_mgr_buf_load_sg(mgr, info, info->sgt);
 404        if (info->buf && info->count)
 405                return fpga_mgr_buf_load(mgr, info, info->buf, info->count);
 406        if (info->firmware_name)
 407                return fpga_mgr_firmware_load(mgr, info, info->firmware_name);
 408        return -EINVAL;
 409}
 410EXPORT_SYMBOL_GPL(fpga_mgr_load);
 411
 412static const char * const state_str[] = {
 413        [FPGA_MGR_STATE_UNKNOWN] =              "unknown",
 414        [FPGA_MGR_STATE_POWER_OFF] =            "power off",
 415        [FPGA_MGR_STATE_POWER_UP] =             "power up",
 416        [FPGA_MGR_STATE_RESET] =                "reset",
 417
 418        /* requesting FPGA image from firmware */
 419        [FPGA_MGR_STATE_FIRMWARE_REQ] =         "firmware request",
 420        [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] =     "firmware request error",
 421
 422        /* Preparing FPGA to receive image */
 423        [FPGA_MGR_STATE_WRITE_INIT] =           "write init",
 424        [FPGA_MGR_STATE_WRITE_INIT_ERR] =       "write init error",
 425
 426        /* Writing image to FPGA */
 427        [FPGA_MGR_STATE_WRITE] =                "write",
 428        [FPGA_MGR_STATE_WRITE_ERR] =            "write error",
 429
 430        /* Finishing configuration after image has been written */
 431        [FPGA_MGR_STATE_WRITE_COMPLETE] =       "write complete",
 432        [FPGA_MGR_STATE_WRITE_COMPLETE_ERR] =   "write complete error",
 433
 434        /* FPGA reports to be in normal operating mode */
 435        [FPGA_MGR_STATE_OPERATING] =            "operating",
 436};
 437
 438static ssize_t name_show(struct device *dev,
 439                         struct device_attribute *attr, char *buf)
 440{
 441        struct fpga_manager *mgr = to_fpga_manager(dev);
 442
 443        return sprintf(buf, "%s\n", mgr->name);
 444}
 445
 446static ssize_t state_show(struct device *dev,
 447                          struct device_attribute *attr, char *buf)
 448{
 449        struct fpga_manager *mgr = to_fpga_manager(dev);
 450
 451        return sprintf(buf, "%s\n", state_str[mgr->state]);
 452}
 453
 454static ssize_t status_show(struct device *dev,
 455                           struct device_attribute *attr, char *buf)
 456{
 457        struct fpga_manager *mgr = to_fpga_manager(dev);
 458        u64 status;
 459        int len = 0;
 460
 461        if (!mgr->mops->status)
 462                return -ENOENT;
 463
 464        status = mgr->mops->status(mgr);
 465
 466        if (status & FPGA_MGR_STATUS_OPERATION_ERR)
 467                len += sprintf(buf + len, "reconfig operation error\n");
 468        if (status & FPGA_MGR_STATUS_CRC_ERR)
 469                len += sprintf(buf + len, "reconfig CRC error\n");
 470        if (status & FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR)
 471                len += sprintf(buf + len, "reconfig incompatible image\n");
 472        if (status & FPGA_MGR_STATUS_IP_PROTOCOL_ERR)
 473                len += sprintf(buf + len, "reconfig IP protocol error\n");
 474        if (status & FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR)
 475                len += sprintf(buf + len, "reconfig fifo overflow error\n");
 476        if (status & FPGA_MGR_STATUS_SECURITY_ERR)
 477                len += sprintf(buf + len, "reconfig security error\n");
 478        if (status & FPGA_MGR_STATUS_DEVICE_INIT_ERR)
 479                len += sprintf(buf + len,
 480                               "initialization has not finished\n");
 481        if (status & FPGA_MGR_STATUS_SIGNAL_ERR)
 482                len += sprintf(buf + len, "device internal signal error\n");
 483        if (status & FPGA_MGR_STATUS_HIGH_Z_STATE_ERR)
 484                len += sprintf(buf + len,
 485                               "all I/Os are placed in High-Z state\n");
 486        if (status & FPGA_MGR_STATUS_EOS_ERR)
 487                len += sprintf(buf + len,
 488                               "start-up sequence has not finished\n");
 489        if (status & FPGA_MGR_STATUS_FIRMWARE_REQ_ERR)
 490                len += sprintf(buf + len, "firmware request error\n");
 491
 492        return len;
 493}
 494
 495static ssize_t firmware_store(struct device *dev,
 496                              struct device_attribute *attr,
 497                              const char *buf, size_t count)
 498{
 499        struct fpga_manager *mgr = to_fpga_manager(dev);
 500        unsigned int len;
 501        char image_name[NAME_MAX];
 502        int ret;
 503
 504        /* struct with information about the FPGA image to program. */
 505        struct fpga_image_info info = {0};
 506
 507        /* lose terminating \n */
 508        strcpy(image_name, buf);
 509        len = strlen(image_name);
 510        if (image_name[len - 1] == '\n')
 511                image_name[len - 1] = 0;
 512
 513        ret = fpga_mgr_firmware_load(mgr, &info, image_name);
 514        if (ret)
 515                return ret;
 516
 517        return count;
 518}
 519
 520static ssize_t key_show(struct device *dev,
 521                        struct device_attribute *attr, char *buf)
 522{
 523        struct fpga_manager *mgr = to_fpga_manager(dev);
 524
 525        return snprintf(buf, ENCRYPTED_KEY_LEN + 1, "%s\n", mgr->key);
 526}
 527
 528static ssize_t key_store(struct device *dev,
 529                         struct device_attribute *attr,
 530                         const char *buf, size_t count)
 531{
 532        struct fpga_manager *mgr = to_fpga_manager(dev);
 533
 534        memcpy(mgr->key, buf, count);
 535
 536        return count;
 537}
 538
 539static ssize_t flags_show(struct device *dev,
 540                          struct device_attribute *attr, char *buf)
 541{
 542        struct fpga_manager *mgr = to_fpga_manager(dev);
 543
 544        return sprintf(buf, "%lx\n", mgr->flags);
 545}
 546
 547static ssize_t flags_store(struct device *dev,
 548                           struct device_attribute *attr,
 549                           const char *buf, size_t count)
 550{
 551        struct fpga_manager *mgr = to_fpga_manager(dev);
 552        int ret;
 553
 554        ret = kstrtol(buf, 16, &mgr->flags);
 555        if (ret)
 556                return ret;
 557
 558        return count;
 559}
 560
 561static DEVICE_ATTR_RO(name);
 562static DEVICE_ATTR_RO(state);
 563static DEVICE_ATTR_RO(status);
 564static DEVICE_ATTR_WO(firmware);
 565static DEVICE_ATTR_RW(flags);
 566static DEVICE_ATTR_RW(key);
 567
 568static struct attribute *fpga_mgr_attrs[] = {
 569        &dev_attr_name.attr,
 570        &dev_attr_state.attr,
 571        &dev_attr_status.attr,
 572        &dev_attr_firmware.attr,
 573        &dev_attr_flags.attr,
 574        &dev_attr_key.attr,
 575        NULL,
 576};
 577ATTRIBUTE_GROUPS(fpga_mgr);
 578
 579static struct fpga_manager *__fpga_mgr_get(struct device *dev)
 580{
 581        struct fpga_manager *mgr;
 582
 583        mgr = to_fpga_manager(dev);
 584
 585        if (!try_module_get(dev->parent->driver->owner))
 586                goto err_dev;
 587
 588        return mgr;
 589
 590err_dev:
 591        put_device(dev);
 592        return ERR_PTR(-ENODEV);
 593}
 594
 595static int fpga_mgr_dev_match(struct device *dev, const void *data)
 596{
 597        return dev->parent == data;
 598}
 599
 600/**
 601 * fpga_mgr_get - Given a device, get a reference to a fpga mgr.
 602 * @dev:        parent device that fpga mgr was registered with
 603 *
 604 * Return: fpga manager struct or IS_ERR() condition containing error code.
 605 */
 606struct fpga_manager *fpga_mgr_get(struct device *dev)
 607{
 608        struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev,
 609                                                   fpga_mgr_dev_match);
 610        if (!mgr_dev)
 611                return ERR_PTR(-ENODEV);
 612
 613        return __fpga_mgr_get(mgr_dev);
 614}
 615EXPORT_SYMBOL_GPL(fpga_mgr_get);
 616
 617/**
 618 * of_fpga_mgr_get - Given a device node, get a reference to a fpga mgr.
 619 *
 620 * @node:       device node
 621 *
 622 * Return: fpga manager struct or IS_ERR() condition containing error code.
 623 */
 624struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
 625{
 626        struct device *dev;
 627
 628        dev = class_find_device_by_of_node(fpga_mgr_class, node);
 629        if (!dev)
 630                return ERR_PTR(-ENODEV);
 631
 632        return __fpga_mgr_get(dev);
 633}
 634EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
 635
 636/**
 637 * fpga_mgr_put - release a reference to a fpga manager
 638 * @mgr:        fpga manager structure
 639 */
 640void fpga_mgr_put(struct fpga_manager *mgr)
 641{
 642        module_put(mgr->dev.parent->driver->owner);
 643        put_device(&mgr->dev);
 644}
 645EXPORT_SYMBOL_GPL(fpga_mgr_put);
 646
 647#ifdef CONFIG_FPGA_MGR_DEBUG_FS
 648#include <linux/debugfs.h>
 649
 650static int fpga_mgr_read(struct seq_file *s, void *data)
 651{
 652        struct fpga_manager *mgr = (struct fpga_manager *)s->private;
 653        int ret = 0;
 654
 655        if (!mgr->mops->read)
 656                return -ENOENT;
 657
 658        if (!mutex_trylock(&mgr->ref_mutex))
 659                return -EBUSY;
 660
 661        if (mgr->state != FPGA_MGR_STATE_OPERATING) {
 662                ret = -EPERM;
 663                goto err_unlock;
 664        }
 665
 666        /* Read the FPGA configuration data from the fabric */
 667        ret = mgr->mops->read(mgr, s);
 668        if (ret)
 669                dev_err(&mgr->dev, "Error while reading configuration data from FPGA\n");
 670
 671err_unlock:
 672        mutex_unlock(&mgr->ref_mutex);
 673
 674        return ret;
 675}
 676
 677static int fpga_mgr_read_open(struct inode *inode, struct file *file)
 678{
 679        return single_open(file, fpga_mgr_read, inode->i_private);
 680}
 681
 682static const struct file_operations fpga_mgr_ops_image = {
 683        .owner = THIS_MODULE,
 684        .open = fpga_mgr_read_open,
 685        .read = seq_read,
 686};
 687#endif
 688
 689static int fpga_dmabuf_fd_get(struct file *file, char __user *argp)
 690{
 691        struct fpga_manager *mgr =  (struct fpga_manager *)(file->private_data);
 692        int buffd;
 693
 694        if (copy_from_user(&buffd, argp, sizeof(buffd)))
 695                return -EFAULT;
 696
 697        mgr->dmabuf = dma_buf_get(buffd);
 698        if (IS_ERR_OR_NULL(mgr->dmabuf))
 699                return -EINVAL;
 700
 701        mgr->flags = FPGA_MGR_CONFIG_DMA_BUF;
 702
 703        return 0;
 704}
 705
 706static int fpga_device_open(struct inode *inode, struct file *file)
 707{
 708        struct miscdevice *miscdev = file->private_data;
 709        struct fpga_manager *mgr = container_of(miscdev,
 710                                                struct fpga_manager, miscdev);
 711
 712        file->private_data = mgr;
 713
 714        return 0;
 715}
 716
 717static int fpga_device_release(struct inode *inode, struct file *file)
 718{
 719        return 0;
 720}
 721
 722static long fpga_device_ioctl(struct file *file, unsigned int cmd,
 723                              unsigned long arg)
 724{
 725        char __user *argp = (char __user *)arg;
 726        int err;
 727
 728        switch (cmd) {
 729        case FPGA_IOCTL_LOAD_DMA_BUFF:
 730                err = fpga_dmabuf_fd_get(file, argp);
 731                break;
 732        default:
 733                err = -ENOTTY;
 734        }
 735
 736        return err;
 737}
 738
 739static const struct file_operations fpga_fops = {
 740        .owner          = THIS_MODULE,
 741        .open           = fpga_device_open,
 742        .release        = fpga_device_release,
 743        .unlocked_ioctl = fpga_device_ioctl,
 744        .compat_ioctl   = fpga_device_ioctl,
 745};
 746
 747/**
 748 * fpga_mgr_lock - Lock FPGA manager for exclusive use
 749 * @mgr:        fpga manager
 750 *
 751 * Given a pointer to FPGA Manager (from fpga_mgr_get() or
 752 * of_fpga_mgr_put()) attempt to get the mutex. The user should call
 753 * fpga_mgr_lock() and verify that it returns 0 before attempting to
 754 * program the FPGA.  Likewise, the user should call fpga_mgr_unlock
 755 * when done programming the FPGA.
 756 *
 757 * Return: 0 for success or -EBUSY
 758 */
 759int fpga_mgr_lock(struct fpga_manager *mgr)
 760{
 761        if (!mutex_trylock(&mgr->ref_mutex)) {
 762                dev_err(&mgr->dev, "FPGA manager is in use.\n");
 763                return -EBUSY;
 764        }
 765
 766        return 0;
 767}
 768EXPORT_SYMBOL_GPL(fpga_mgr_lock);
 769
 770/**
 771 * fpga_mgr_unlock - Unlock FPGA manager after done programming
 772 * @mgr:        fpga manager
 773 */
 774void fpga_mgr_unlock(struct fpga_manager *mgr)
 775{
 776        mutex_unlock(&mgr->ref_mutex);
 777}
 778EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
 779
 780/**
 781 * fpga_mgr_create - create and initialize a FPGA manager struct
 782 * @dev:        fpga manager device from pdev
 783 * @name:       fpga manager name
 784 * @mops:       pointer to structure of fpga manager ops
 785 * @priv:       fpga manager private data
 786 *
 787 * The caller of this function is responsible for freeing the struct with
 788 * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
 789 *
 790 * Return: pointer to struct fpga_manager or NULL
 791 */
 792struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
 793                                     const struct fpga_manager_ops *mops,
 794                                     void *priv)
 795{
 796        struct fpga_manager *mgr;
 797        int id, ret;
 798
 799        if (!mops || !mops->write_complete || !mops->state ||
 800            !mops->write_init || (!mops->write && !mops->write_sg)) {
 801                dev_err(dev, "Attempt to register without fpga_manager_ops\n");
 802                return NULL;
 803        }
 804
 805        if (!name || !strlen(name)) {
 806                dev_err(dev, "Attempt to register with no name!\n");
 807                return NULL;
 808        }
 809
 810        mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
 811        if (!mgr)
 812                return NULL;
 813
 814        id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
 815        if (id < 0)
 816                goto error_kfree;
 817
 818        mutex_init(&mgr->ref_mutex);
 819
 820        mgr->name = name;
 821        mgr->mops = mops;
 822        mgr->priv = priv;
 823
 824        device_initialize(&mgr->dev);
 825        mgr->dev.class = fpga_mgr_class;
 826        mgr->dev.groups = mops->groups;
 827        mgr->dev.parent = dev;
 828        mgr->dev.of_node = dev->of_node;
 829        mgr->dev.id = id;
 830
 831        /* Make device dma capable by inheriting from parent's */
 832        set_dma_ops(&mgr->dev, get_dma_ops(dev));
 833        ret = dma_coerce_mask_and_coherent(&mgr->dev, dma_get_mask(dev));
 834        if (ret) {
 835                dev_warn(dev,
 836                "Failed to set DMA mask %llx. Trying to continue... %x\n",
 837                dma_get_mask(dev), ret);
 838        }
 839
 840        ret = dev_set_name(&mgr->dev, "fpga%d", id);
 841        if (ret)
 842                goto error_device;
 843
 844        mgr->miscdev.minor = MISC_DYNAMIC_MINOR;
 845        mgr->miscdev.name = kobject_name(&mgr->dev.kobj);
 846        mgr->miscdev.fops = &fpga_fops;
 847        ret = misc_register(&mgr->miscdev);
 848        if (ret) {
 849                pr_err("fpga: failed to register misc device.\n");
 850                goto error_device;
 851        }
 852
 853        return mgr;
 854
 855error_device:
 856        ida_simple_remove(&fpga_mgr_ida, id);
 857error_kfree:
 858        kfree(mgr);
 859
 860        return NULL;
 861}
 862EXPORT_SYMBOL_GPL(fpga_mgr_create);
 863
 864/**
 865 * fpga_mgr_free - free a FPGA manager created with fpga_mgr_create()
 866 * @mgr:        fpga manager struct
 867 */
 868void fpga_mgr_free(struct fpga_manager *mgr)
 869{
 870        ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
 871        kfree(mgr);
 872}
 873EXPORT_SYMBOL_GPL(fpga_mgr_free);
 874
 875static void devm_fpga_mgr_release(struct device *dev, void *res)
 876{
 877        struct fpga_manager *mgr = *(struct fpga_manager **)res;
 878
 879        fpga_mgr_free(mgr);
 880}
 881
 882/**
 883 * devm_fpga_mgr_create - create and initialize a managed FPGA manager struct
 884 * @dev:        fpga manager device from pdev
 885 * @name:       fpga manager name
 886 * @mops:       pointer to structure of fpga manager ops
 887 * @priv:       fpga manager private data
 888 *
 889 * This function is intended for use in a FPGA manager driver's probe function.
 890 * After the manager driver creates the manager struct with
 891 * devm_fpga_mgr_create(), it should register it with fpga_mgr_register().  The
 892 * manager driver's remove function should call fpga_mgr_unregister().  The
 893 * manager struct allocated with this function will be freed automatically on
 894 * driver detach.  This includes the case of a probe function returning error
 895 * before calling fpga_mgr_register(), the struct will still get cleaned up.
 896 *
 897 * Return: pointer to struct fpga_manager or NULL
 898 */
 899struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
 900                                          const struct fpga_manager_ops *mops,
 901                                          void *priv)
 902{
 903        struct fpga_manager **ptr, *mgr;
 904
 905        ptr = devres_alloc(devm_fpga_mgr_release, sizeof(*ptr), GFP_KERNEL);
 906        if (!ptr)
 907                return NULL;
 908
 909        mgr = fpga_mgr_create(dev, name, mops, priv);
 910        if (!mgr) {
 911                devres_free(ptr);
 912        } else {
 913                *ptr = mgr;
 914                devres_add(dev, ptr);
 915        }
 916
 917        return mgr;
 918}
 919EXPORT_SYMBOL_GPL(devm_fpga_mgr_create);
 920
 921/**
 922 * fpga_mgr_register - register a FPGA manager
 923 * @mgr: fpga manager struct
 924 *
 925 * Return: 0 on success, negative error code otherwise.
 926 */
 927int fpga_mgr_register(struct fpga_manager *mgr)
 928{
 929        int ret;
 930#ifdef CONFIG_FPGA_MGR_DEBUG_FS
 931        struct dentry *d, *parent;
 932#endif
 933
 934        /*
 935         * Initialize framework state by requesting low level driver read state
 936         * from device.  FPGA may be in reset mode or may have been programmed
 937         * by bootloader or EEPROM.
 938         */
 939        mgr->state = mgr->mops->state(mgr);
 940
 941        ret = device_add(&mgr->dev);
 942        if (ret)
 943                goto error_device;
 944
 945#ifdef CONFIG_FPGA_MGR_DEBUG_FS
 946        mgr->dir = debugfs_create_dir("fpga", NULL);
 947        if (!mgr->dir)
 948                goto error_device;
 949
 950        parent = mgr->dir;
 951        d = debugfs_create_dir(mgr->dev.kobj.name, parent);
 952        if (!d) {
 953                debugfs_remove_recursive(parent);
 954                goto error_device;
 955        }
 956
 957        parent = d;
 958        d = debugfs_create_file("image", 0644, parent, mgr,
 959                                &fpga_mgr_ops_image);
 960        if (!d) {
 961                debugfs_remove_recursive(mgr->dir);
 962                goto error_device;
 963        }
 964#endif
 965        dev_info(&mgr->dev, "%s registered\n", mgr->name);
 966
 967        return 0;
 968
 969error_device:
 970        ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
 971
 972        return ret;
 973}
 974EXPORT_SYMBOL_GPL(fpga_mgr_register);
 975
 976/**
 977 * fpga_mgr_unregister - unregister a FPGA manager
 978 * @mgr: fpga manager struct
 979 *
 980 * This function is intended for use in a FPGA manager driver's remove function.
 981 */
 982void fpga_mgr_unregister(struct fpga_manager *mgr)
 983{
 984        dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
 985
 986#ifdef CONFIG_FPGA_MGR_DEBUG_FS
 987        debugfs_remove_recursive(mgr->dir);
 988#endif
 989
 990        /*
 991         * If the low level driver provides a method for putting fpga into
 992         * a desired state upon unregister, do it.
 993         */
 994        if (mgr->mops->fpga_remove)
 995                mgr->mops->fpga_remove(mgr);
 996
 997        device_unregister(&mgr->dev);
 998}
 999EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
1000
1001static void fpga_mgr_dev_release(struct device *dev)
1002{
1003}
1004
1005static int __init fpga_mgr_class_init(void)
1006{
1007        pr_info("FPGA manager framework\n");
1008
1009        fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
1010        if (IS_ERR(fpga_mgr_class))
1011                return PTR_ERR(fpga_mgr_class);
1012
1013        fpga_mgr_class->dev_groups = fpga_mgr_groups;
1014        fpga_mgr_class->dev_release = fpga_mgr_dev_release;
1015
1016        return 0;
1017}
1018
1019static void __exit fpga_mgr_class_exit(void)
1020{
1021        class_destroy(fpga_mgr_class);
1022        ida_destroy(&fpga_mgr_ida);
1023}
1024
1025MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
1026MODULE_DESCRIPTION("FPGA manager framework");
1027MODULE_LICENSE("GPL v2");
1028
1029subsys_initcall(fpga_mgr_class_init);
1030module_exit(fpga_mgr_class_exit);
1031