1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2013-2015 Altera Corporation
6 * Copyright (C) 2017 Intel Corporation
8 * With code from the mailing list:
9 * Copyright (C) 2013 Xilinx, Inc.
11 #include <linux/firmware.h>
12 #include <linux/fpga/fpga-mgr.h>
13 #include <linux/idr.h>
14 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/scatterlist.h>
19 #include <linux/highmem.h>
21 static DEFINE_IDA(fpga_mgr_ida);
22 static struct class *fpga_mgr_class;
24 struct fpga_mgr_devres {
25 struct fpga_manager *mgr;
28 static inline void fpga_mgr_fpga_remove(struct fpga_manager *mgr)
30 if (mgr->mops->fpga_remove)
31 mgr->mops->fpga_remove(mgr);
34 static inline enum fpga_mgr_states fpga_mgr_state(struct fpga_manager *mgr)
37 return mgr->mops->state(mgr);
38 return FPGA_MGR_STATE_UNKNOWN;
41 static inline u64 fpga_mgr_status(struct fpga_manager *mgr)
43 if (mgr->mops->status)
44 return mgr->mops->status(mgr);
48 static inline int fpga_mgr_write(struct fpga_manager *mgr, const char *buf, size_t count)
51 return mgr->mops->write(mgr, buf, count);
56 * After all the FPGA image has been written, do the device specific steps to
57 * finish and set the FPGA into operating mode.
59 static inline int fpga_mgr_write_complete(struct fpga_manager *mgr,
60 struct fpga_image_info *info)
64 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
65 if (mgr->mops->write_complete)
66 ret = mgr->mops->write_complete(mgr, info);
68 dev_err(&mgr->dev, "Error after writing image data to FPGA\n");
69 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
72 mgr->state = FPGA_MGR_STATE_OPERATING;
77 static inline int fpga_mgr_write_init(struct fpga_manager *mgr,
78 struct fpga_image_info *info,
79 const char *buf, size_t count)
81 if (mgr->mops->write_init)
82 return mgr->mops->write_init(mgr, info, buf, count);
86 static inline int fpga_mgr_write_sg(struct fpga_manager *mgr,
89 if (mgr->mops->write_sg)
90 return mgr->mops->write_sg(mgr, sgt);
95 * fpga_image_info_alloc - Allocate an FPGA image info struct
98 * Return: struct fpga_image_info or NULL
100 struct fpga_image_info *fpga_image_info_alloc(struct device *dev)
102 struct fpga_image_info *info;
106 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
116 EXPORT_SYMBOL_GPL(fpga_image_info_alloc);
119 * fpga_image_info_free - Free an FPGA image info struct
120 * @info: FPGA image info struct to free
122 void fpga_image_info_free(struct fpga_image_info *info)
130 if (info->firmware_name)
131 devm_kfree(dev, info->firmware_name);
133 devm_kfree(dev, info);
136 EXPORT_SYMBOL_GPL(fpga_image_info_free);
139 * Call the low level driver's write_init function. This will do the
140 * device-specific things to get the FPGA into the state where it is ready to
141 * receive an FPGA image. The low level driver only gets to see the first
142 * initial_header_size bytes in the buffer.
144 static int fpga_mgr_write_init_buf(struct fpga_manager *mgr,
145 struct fpga_image_info *info,
146 const char *buf, size_t count)
150 mgr->state = FPGA_MGR_STATE_WRITE_INIT;
151 if (!mgr->mops->initial_header_size) {
152 ret = fpga_mgr_write_init(mgr, info, NULL, 0);
154 count = min(mgr->mops->initial_header_size, count);
155 ret = fpga_mgr_write_init(mgr, info, buf, count);
159 dev_err(&mgr->dev, "Error preparing FPGA for writing\n");
160 mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
167 static int fpga_mgr_write_init_sg(struct fpga_manager *mgr,
168 struct fpga_image_info *info,
169 struct sg_table *sgt)
171 struct sg_mapping_iter miter;
176 if (!mgr->mops->initial_header_size)
177 return fpga_mgr_write_init_buf(mgr, info, NULL, 0);
180 * First try to use miter to map the first fragment to access the
181 * header, this is the typical path.
183 sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
184 if (sg_miter_next(&miter) &&
185 miter.length >= mgr->mops->initial_header_size) {
186 ret = fpga_mgr_write_init_buf(mgr, info, miter.addr,
188 sg_miter_stop(&miter);
191 sg_miter_stop(&miter);
193 /* Otherwise copy the fragments into temporary memory. */
194 buf = kmalloc(mgr->mops->initial_header_size, GFP_KERNEL);
198 len = sg_copy_to_buffer(sgt->sgl, sgt->nents, buf,
199 mgr->mops->initial_header_size);
200 ret = fpga_mgr_write_init_buf(mgr, info, buf, len);
208 * fpga_mgr_buf_load_sg - load fpga from image in buffer from a scatter list
210 * @info: fpga image specific information
211 * @sgt: scatterlist table
213 * Step the low level fpga manager through the device-specific steps of getting
214 * an FPGA ready to be configured, writing the image to it, then doing whatever
215 * post-configuration steps necessary. This code assumes the caller got the
216 * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is
219 * This is the preferred entry point for FPGA programming, it does not require
220 * any contiguous kernel memory.
222 * Return: 0 on success, negative error code otherwise.
224 static int fpga_mgr_buf_load_sg(struct fpga_manager *mgr,
225 struct fpga_image_info *info,
226 struct sg_table *sgt)
230 ret = fpga_mgr_write_init_sg(mgr, info, sgt);
234 /* Write the FPGA image to the FPGA. */
235 mgr->state = FPGA_MGR_STATE_WRITE;
236 if (mgr->mops->write_sg) {
237 ret = fpga_mgr_write_sg(mgr, sgt);
239 struct sg_mapping_iter miter;
241 sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
242 while (sg_miter_next(&miter)) {
243 ret = fpga_mgr_write(mgr, miter.addr, miter.length);
247 sg_miter_stop(&miter);
251 dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
252 mgr->state = FPGA_MGR_STATE_WRITE_ERR;
256 return fpga_mgr_write_complete(mgr, info);
259 static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr,
260 struct fpga_image_info *info,
261 const char *buf, size_t count)
265 ret = fpga_mgr_write_init_buf(mgr, info, buf, count);
270 * Write the FPGA image to the FPGA.
272 mgr->state = FPGA_MGR_STATE_WRITE;
273 ret = fpga_mgr_write(mgr, buf, count);
275 dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
276 mgr->state = FPGA_MGR_STATE_WRITE_ERR;
280 return fpga_mgr_write_complete(mgr, info);
284 * fpga_mgr_buf_load - load fpga from image in buffer
286 * @info: fpga image info
287 * @buf: buffer contain fpga image
288 * @count: byte count of buf
290 * Step the low level fpga manager through the device-specific steps of getting
291 * an FPGA ready to be configured, writing the image to it, then doing whatever
292 * post-configuration steps necessary. This code assumes the caller got the
293 * mgr pointer from of_fpga_mgr_get() and checked that it is not an error code.
295 * Return: 0 on success, negative error code otherwise.
297 static int fpga_mgr_buf_load(struct fpga_manager *mgr,
298 struct fpga_image_info *info,
299 const char *buf, size_t count)
309 * This is just a fast path if the caller has already created a
310 * contiguous kernel buffer and the driver doesn't require SG, non-SG
311 * drivers will still work on the slow path.
313 if (mgr->mops->write)
314 return fpga_mgr_buf_load_mapped(mgr, info, buf, count);
317 * Convert the linear kernel pointer into a sg_table of pages for use
320 nr_pages = DIV_ROUND_UP((unsigned long)buf + count, PAGE_SIZE) -
321 (unsigned long)buf / PAGE_SIZE;
322 pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
326 p = buf - offset_in_page(buf);
327 for (index = 0; index < nr_pages; index++) {
328 if (is_vmalloc_addr(p))
329 pages[index] = vmalloc_to_page(p);
331 pages[index] = kmap_to_page((void *)p);
340 * The temporary pages list is used to code share the merging algorithm
341 * in sg_alloc_table_from_pages
343 rc = sg_alloc_table_from_pages(&sgt, pages, index, offset_in_page(buf),
349 rc = fpga_mgr_buf_load_sg(mgr, info, &sgt);
356 * fpga_mgr_firmware_load - request firmware and load to fpga
358 * @info: fpga image specific information
359 * @image_name: name of image file on the firmware search path
361 * Request an FPGA image using the firmware class, then write out to the FPGA.
362 * Update the state before each step to provide info on what step failed if
363 * there is a failure. This code assumes the caller got the mgr pointer
364 * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
367 * Return: 0 on success, negative error code otherwise.
369 static int fpga_mgr_firmware_load(struct fpga_manager *mgr,
370 struct fpga_image_info *info,
371 const char *image_name)
373 struct device *dev = &mgr->dev;
374 const struct firmware *fw;
377 dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
379 mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
381 ret = request_firmware(&fw, image_name, dev);
383 mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
384 dev_err(dev, "Error requesting firmware %s\n", image_name);
388 ret = fpga_mgr_buf_load(mgr, info, fw->data, fw->size);
390 release_firmware(fw);
396 * fpga_mgr_load - load FPGA from scatter/gather table, buffer, or firmware
398 * @info: fpga image information.
400 * Load the FPGA from an image which is indicated in @info. If successful, the
401 * FPGA ends up in operating mode.
403 * Return: 0 on success, negative error code otherwise.
405 int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info)
408 return fpga_mgr_buf_load_sg(mgr, info, info->sgt);
409 if (info->buf && info->count)
410 return fpga_mgr_buf_load(mgr, info, info->buf, info->count);
411 if (info->firmware_name)
412 return fpga_mgr_firmware_load(mgr, info, info->firmware_name);
415 EXPORT_SYMBOL_GPL(fpga_mgr_load);
417 static const char * const state_str[] = {
418 [FPGA_MGR_STATE_UNKNOWN] = "unknown",
419 [FPGA_MGR_STATE_POWER_OFF] = "power off",
420 [FPGA_MGR_STATE_POWER_UP] = "power up",
421 [FPGA_MGR_STATE_RESET] = "reset",
423 /* requesting FPGA image from firmware */
424 [FPGA_MGR_STATE_FIRMWARE_REQ] = "firmware request",
425 [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] = "firmware request error",
427 /* Preparing FPGA to receive image */
428 [FPGA_MGR_STATE_WRITE_INIT] = "write init",
429 [FPGA_MGR_STATE_WRITE_INIT_ERR] = "write init error",
431 /* Writing image to FPGA */
432 [FPGA_MGR_STATE_WRITE] = "write",
433 [FPGA_MGR_STATE_WRITE_ERR] = "write error",
435 /* Finishing configuration after image has been written */
436 [FPGA_MGR_STATE_WRITE_COMPLETE] = "write complete",
437 [FPGA_MGR_STATE_WRITE_COMPLETE_ERR] = "write complete error",
439 /* FPGA reports to be in normal operating mode */
440 [FPGA_MGR_STATE_OPERATING] = "operating",
443 static ssize_t name_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
446 struct fpga_manager *mgr = to_fpga_manager(dev);
448 return sprintf(buf, "%s\n", mgr->name);
451 static ssize_t state_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
454 struct fpga_manager *mgr = to_fpga_manager(dev);
456 return sprintf(buf, "%s\n", state_str[mgr->state]);
459 static ssize_t status_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
462 struct fpga_manager *mgr = to_fpga_manager(dev);
466 status = fpga_mgr_status(mgr);
468 if (status & FPGA_MGR_STATUS_OPERATION_ERR)
469 len += sprintf(buf + len, "reconfig operation error\n");
470 if (status & FPGA_MGR_STATUS_CRC_ERR)
471 len += sprintf(buf + len, "reconfig CRC error\n");
472 if (status & FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR)
473 len += sprintf(buf + len, "reconfig incompatible image\n");
474 if (status & FPGA_MGR_STATUS_IP_PROTOCOL_ERR)
475 len += sprintf(buf + len, "reconfig IP protocol error\n");
476 if (status & FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR)
477 len += sprintf(buf + len, "reconfig fifo overflow error\n");
482 static DEVICE_ATTR_RO(name);
483 static DEVICE_ATTR_RO(state);
484 static DEVICE_ATTR_RO(status);
486 static struct attribute *fpga_mgr_attrs[] = {
488 &dev_attr_state.attr,
489 &dev_attr_status.attr,
492 ATTRIBUTE_GROUPS(fpga_mgr);
494 static struct fpga_manager *__fpga_mgr_get(struct device *dev)
496 struct fpga_manager *mgr;
498 mgr = to_fpga_manager(dev);
500 if (!try_module_get(dev->parent->driver->owner))
507 return ERR_PTR(-ENODEV);
510 static int fpga_mgr_dev_match(struct device *dev, const void *data)
512 return dev->parent == data;
516 * fpga_mgr_get - Given a device, get a reference to an fpga mgr.
517 * @dev: parent device that fpga mgr was registered with
519 * Return: fpga manager struct or IS_ERR() condition containing error code.
521 struct fpga_manager *fpga_mgr_get(struct device *dev)
523 struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev,
526 return ERR_PTR(-ENODEV);
528 return __fpga_mgr_get(mgr_dev);
530 EXPORT_SYMBOL_GPL(fpga_mgr_get);
533 * of_fpga_mgr_get - Given a device node, get a reference to an fpga mgr.
537 * Return: fpga manager struct or IS_ERR() condition containing error code.
539 struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
543 dev = class_find_device_by_of_node(fpga_mgr_class, node);
545 return ERR_PTR(-ENODEV);
547 return __fpga_mgr_get(dev);
549 EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
552 * fpga_mgr_put - release a reference to an fpga manager
553 * @mgr: fpga manager structure
555 void fpga_mgr_put(struct fpga_manager *mgr)
557 module_put(mgr->dev.parent->driver->owner);
558 put_device(&mgr->dev);
560 EXPORT_SYMBOL_GPL(fpga_mgr_put);
563 * fpga_mgr_lock - Lock FPGA manager for exclusive use
566 * Given a pointer to FPGA Manager (from fpga_mgr_get() or
567 * of_fpga_mgr_put()) attempt to get the mutex. The user should call
568 * fpga_mgr_lock() and verify that it returns 0 before attempting to
569 * program the FPGA. Likewise, the user should call fpga_mgr_unlock
570 * when done programming the FPGA.
572 * Return: 0 for success or -EBUSY
574 int fpga_mgr_lock(struct fpga_manager *mgr)
576 if (!mutex_trylock(&mgr->ref_mutex)) {
577 dev_err(&mgr->dev, "FPGA manager is in use.\n");
583 EXPORT_SYMBOL_GPL(fpga_mgr_lock);
586 * fpga_mgr_unlock - Unlock FPGA manager after done programming
589 void fpga_mgr_unlock(struct fpga_manager *mgr)
591 mutex_unlock(&mgr->ref_mutex);
593 EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
596 * fpga_mgr_register_full - create and register an FPGA Manager device
597 * @parent: fpga manager device from pdev
598 * @info: parameters for fpga manager
600 * The caller of this function is responsible for calling fpga_mgr_unregister().
601 * Using devm_fpga_mgr_register_full() instead is recommended.
603 * Return: pointer to struct fpga_manager pointer or ERR_PTR()
605 struct fpga_manager *
606 fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info)
608 const struct fpga_manager_ops *mops = info->mops;
609 struct fpga_manager *mgr;
613 dev_err(parent, "Attempt to register without fpga_manager_ops\n");
614 return ERR_PTR(-EINVAL);
617 if (!info->name || !strlen(info->name)) {
618 dev_err(parent, "Attempt to register with no name!\n");
619 return ERR_PTR(-EINVAL);
622 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
624 return ERR_PTR(-ENOMEM);
626 id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
632 mutex_init(&mgr->ref_mutex);
634 mgr->name = info->name;
635 mgr->mops = info->mops;
636 mgr->priv = info->priv;
637 mgr->compat_id = info->compat_id;
639 mgr->dev.class = fpga_mgr_class;
640 mgr->dev.groups = mops->groups;
641 mgr->dev.parent = parent;
642 mgr->dev.of_node = parent->of_node;
645 ret = dev_set_name(&mgr->dev, "fpga%d", id);
650 * Initialize framework state by requesting low level driver read state
651 * from device. FPGA may be in reset mode or may have been programmed
652 * by bootloader or EEPROM.
654 mgr->state = fpga_mgr_state(mgr);
656 ret = device_register(&mgr->dev);
658 put_device(&mgr->dev);
665 ida_simple_remove(&fpga_mgr_ida, id);
671 EXPORT_SYMBOL_GPL(fpga_mgr_register_full);
674 * fpga_mgr_register - create and register an FPGA Manager device
675 * @parent: fpga manager device from pdev
676 * @name: fpga manager name
677 * @mops: pointer to structure of fpga manager ops
678 * @priv: fpga manager private data
680 * The caller of this function is responsible for calling fpga_mgr_unregister().
681 * Using devm_fpga_mgr_register() instead is recommended. This simple
682 * version of the register function should be sufficient for most users. The
683 * fpga_mgr_register_full() function is available for users that need to pass
684 * additional, optional parameters.
686 * Return: pointer to struct fpga_manager pointer or ERR_PTR()
688 struct fpga_manager *
689 fpga_mgr_register(struct device *parent, const char *name,
690 const struct fpga_manager_ops *mops, void *priv)
692 struct fpga_manager_info info = { 0 };
698 return fpga_mgr_register_full(parent, &info);
700 EXPORT_SYMBOL_GPL(fpga_mgr_register);
703 * fpga_mgr_unregister - unregister an FPGA manager
704 * @mgr: fpga manager struct
706 * This function is intended for use in an FPGA manager driver's remove function.
708 void fpga_mgr_unregister(struct fpga_manager *mgr)
710 dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
713 * If the low level driver provides a method for putting fpga into
714 * a desired state upon unregister, do it.
716 fpga_mgr_fpga_remove(mgr);
718 device_unregister(&mgr->dev);
720 EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
722 static void devm_fpga_mgr_unregister(struct device *dev, void *res)
724 struct fpga_mgr_devres *dr = res;
726 fpga_mgr_unregister(dr->mgr);
730 * devm_fpga_mgr_register_full - resource managed variant of fpga_mgr_register()
731 * @parent: fpga manager device from pdev
732 * @info: parameters for fpga manager
734 * Return: fpga manager pointer on success, negative error code otherwise.
736 * This is the devres variant of fpga_mgr_register_full() for which the unregister
737 * function will be called automatically when the managing device is detached.
739 struct fpga_manager *
740 devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info)
742 struct fpga_mgr_devres *dr;
743 struct fpga_manager *mgr;
745 dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
747 return ERR_PTR(-ENOMEM);
749 mgr = fpga_mgr_register_full(parent, info);
756 devres_add(parent, dr);
760 EXPORT_SYMBOL_GPL(devm_fpga_mgr_register_full);
763 * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
764 * @parent: fpga manager device from pdev
765 * @name: fpga manager name
766 * @mops: pointer to structure of fpga manager ops
767 * @priv: fpga manager private data
769 * Return: fpga manager pointer on success, negative error code otherwise.
771 * This is the devres variant of fpga_mgr_register() for which the
772 * unregister function will be called automatically when the managing
773 * device is detached.
775 struct fpga_manager *
776 devm_fpga_mgr_register(struct device *parent, const char *name,
777 const struct fpga_manager_ops *mops, void *priv)
779 struct fpga_manager_info info = { 0 };
785 return devm_fpga_mgr_register_full(parent, &info);
787 EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
789 static void fpga_mgr_dev_release(struct device *dev)
791 struct fpga_manager *mgr = to_fpga_manager(dev);
793 ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
797 static int __init fpga_mgr_class_init(void)
799 pr_info("FPGA manager framework\n");
801 fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
802 if (IS_ERR(fpga_mgr_class))
803 return PTR_ERR(fpga_mgr_class);
805 fpga_mgr_class->dev_groups = fpga_mgr_groups;
806 fpga_mgr_class->dev_release = fpga_mgr_dev_release;
811 static void __exit fpga_mgr_class_exit(void)
813 class_destroy(fpga_mgr_class);
814 ida_destroy(&fpga_mgr_ida);
817 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
818 MODULE_DESCRIPTION("FPGA manager framework");
819 MODULE_LICENSE("GPL v2");
821 subsys_initcall(fpga_mgr_class_init);
822 module_exit(fpga_mgr_class_exit);