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
;
29 * fpga_image_info_alloc - Allocate a FPGA image info struct
32 * Return: struct fpga_image_info or NULL
34 struct fpga_image_info
*fpga_image_info_alloc(struct device
*dev
)
36 struct fpga_image_info
*info
;
40 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
50 EXPORT_SYMBOL_GPL(fpga_image_info_alloc
);
53 * fpga_image_info_free - Free a FPGA image info struct
54 * @info: FPGA image info struct to free
56 void fpga_image_info_free(struct fpga_image_info
*info
)
64 if (info
->firmware_name
)
65 devm_kfree(dev
, info
->firmware_name
);
67 devm_kfree(dev
, info
);
70 EXPORT_SYMBOL_GPL(fpga_image_info_free
);
73 * Call the low level driver's write_init function. This will do the
74 * device-specific things to get the FPGA into the state where it is ready to
75 * receive an FPGA image. The low level driver only gets to see the first
76 * initial_header_size bytes in the buffer.
78 static int fpga_mgr_write_init_buf(struct fpga_manager
*mgr
,
79 struct fpga_image_info
*info
,
80 const char *buf
, size_t count
)
84 mgr
->state
= FPGA_MGR_STATE_WRITE_INIT
;
85 if (!mgr
->mops
->initial_header_size
)
86 ret
= mgr
->mops
->write_init(mgr
, info
, NULL
, 0);
88 ret
= mgr
->mops
->write_init(
89 mgr
, info
, buf
, min(mgr
->mops
->initial_header_size
, count
));
92 dev_err(&mgr
->dev
, "Error preparing FPGA for writing\n");
93 mgr
->state
= FPGA_MGR_STATE_WRITE_INIT_ERR
;
100 static int fpga_mgr_write_init_sg(struct fpga_manager
*mgr
,
101 struct fpga_image_info
*info
,
102 struct sg_table
*sgt
)
104 struct sg_mapping_iter miter
;
109 if (!mgr
->mops
->initial_header_size
)
110 return fpga_mgr_write_init_buf(mgr
, info
, NULL
, 0);
113 * First try to use miter to map the first fragment to access the
114 * header, this is the typical path.
116 sg_miter_start(&miter
, sgt
->sgl
, sgt
->nents
, SG_MITER_FROM_SG
);
117 if (sg_miter_next(&miter
) &&
118 miter
.length
>= mgr
->mops
->initial_header_size
) {
119 ret
= fpga_mgr_write_init_buf(mgr
, info
, miter
.addr
,
121 sg_miter_stop(&miter
);
124 sg_miter_stop(&miter
);
126 /* Otherwise copy the fragments into temporary memory. */
127 buf
= kmalloc(mgr
->mops
->initial_header_size
, GFP_KERNEL
);
131 len
= sg_copy_to_buffer(sgt
->sgl
, sgt
->nents
, buf
,
132 mgr
->mops
->initial_header_size
);
133 ret
= fpga_mgr_write_init_buf(mgr
, info
, buf
, len
);
141 * After all the FPGA image has been written, do the device specific steps to
142 * finish and set the FPGA into operating mode.
144 static int fpga_mgr_write_complete(struct fpga_manager
*mgr
,
145 struct fpga_image_info
*info
)
149 mgr
->state
= FPGA_MGR_STATE_WRITE_COMPLETE
;
150 ret
= mgr
->mops
->write_complete(mgr
, info
);
152 dev_err(&mgr
->dev
, "Error after writing image data to FPGA\n");
153 mgr
->state
= FPGA_MGR_STATE_WRITE_COMPLETE_ERR
;
156 mgr
->state
= FPGA_MGR_STATE_OPERATING
;
162 * fpga_mgr_buf_load_sg - load fpga from image in buffer from a scatter list
164 * @info: fpga image specific information
165 * @sgt: scatterlist table
167 * Step the low level fpga manager through the device-specific steps of getting
168 * an FPGA ready to be configured, writing the image to it, then doing whatever
169 * post-configuration steps necessary. This code assumes the caller got the
170 * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is
173 * This is the preferred entry point for FPGA programming, it does not require
174 * any contiguous kernel memory.
176 * Return: 0 on success, negative error code otherwise.
178 static int fpga_mgr_buf_load_sg(struct fpga_manager
*mgr
,
179 struct fpga_image_info
*info
,
180 struct sg_table
*sgt
)
184 ret
= fpga_mgr_write_init_sg(mgr
, info
, sgt
);
188 /* Write the FPGA image to the FPGA. */
189 mgr
->state
= FPGA_MGR_STATE_WRITE
;
190 if (mgr
->mops
->write_sg
) {
191 ret
= mgr
->mops
->write_sg(mgr
, sgt
);
193 struct sg_mapping_iter miter
;
195 sg_miter_start(&miter
, sgt
->sgl
, sgt
->nents
, SG_MITER_FROM_SG
);
196 while (sg_miter_next(&miter
)) {
197 ret
= mgr
->mops
->write(mgr
, miter
.addr
, miter
.length
);
201 sg_miter_stop(&miter
);
205 dev_err(&mgr
->dev
, "Error while writing image data to FPGA\n");
206 mgr
->state
= FPGA_MGR_STATE_WRITE_ERR
;
210 return fpga_mgr_write_complete(mgr
, info
);
213 static int fpga_mgr_buf_load_mapped(struct fpga_manager
*mgr
,
214 struct fpga_image_info
*info
,
215 const char *buf
, size_t count
)
219 ret
= fpga_mgr_write_init_buf(mgr
, info
, buf
, count
);
224 * Write the FPGA image to the FPGA.
226 mgr
->state
= FPGA_MGR_STATE_WRITE
;
227 ret
= mgr
->mops
->write(mgr
, buf
, count
);
229 dev_err(&mgr
->dev
, "Error while writing image data to FPGA\n");
230 mgr
->state
= FPGA_MGR_STATE_WRITE_ERR
;
234 return fpga_mgr_write_complete(mgr
, info
);
238 * fpga_mgr_buf_load - load fpga from image in buffer
240 * @info: fpga image info
241 * @buf: buffer contain fpga image
242 * @count: byte count of buf
244 * Step the low level fpga manager through the device-specific steps of getting
245 * an FPGA ready to be configured, writing the image to it, then doing whatever
246 * post-configuration steps necessary. This code assumes the caller got the
247 * mgr pointer from of_fpga_mgr_get() and checked that it is not an error code.
249 * Return: 0 on success, negative error code otherwise.
251 static int fpga_mgr_buf_load(struct fpga_manager
*mgr
,
252 struct fpga_image_info
*info
,
253 const char *buf
, size_t count
)
263 * This is just a fast path if the caller has already created a
264 * contiguous kernel buffer and the driver doesn't require SG, non-SG
265 * drivers will still work on the slow path.
267 if (mgr
->mops
->write
)
268 return fpga_mgr_buf_load_mapped(mgr
, info
, buf
, count
);
271 * Convert the linear kernel pointer into a sg_table of pages for use
274 nr_pages
= DIV_ROUND_UP((unsigned long)buf
+ count
, PAGE_SIZE
) -
275 (unsigned long)buf
/ PAGE_SIZE
;
276 pages
= kmalloc_array(nr_pages
, sizeof(struct page
*), GFP_KERNEL
);
280 p
= buf
- offset_in_page(buf
);
281 for (index
= 0; index
< nr_pages
; index
++) {
282 if (is_vmalloc_addr(p
))
283 pages
[index
] = vmalloc_to_page(p
);
285 pages
[index
] = kmap_to_page((void *)p
);
294 * The temporary pages list is used to code share the merging algorithm
295 * in sg_alloc_table_from_pages
297 rc
= sg_alloc_table_from_pages(&sgt
, pages
, index
, offset_in_page(buf
),
303 rc
= fpga_mgr_buf_load_sg(mgr
, info
, &sgt
);
310 * fpga_mgr_firmware_load - request firmware and load to fpga
312 * @info: fpga image specific information
313 * @image_name: name of image file on the firmware search path
315 * Request an FPGA image using the firmware class, then write out to the FPGA.
316 * Update the state before each step to provide info on what step failed if
317 * there is a failure. This code assumes the caller got the mgr pointer
318 * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
321 * Return: 0 on success, negative error code otherwise.
323 static int fpga_mgr_firmware_load(struct fpga_manager
*mgr
,
324 struct fpga_image_info
*info
,
325 const char *image_name
)
327 struct device
*dev
= &mgr
->dev
;
328 const struct firmware
*fw
;
331 dev_info(dev
, "writing %s to %s\n", image_name
, mgr
->name
);
333 mgr
->state
= FPGA_MGR_STATE_FIRMWARE_REQ
;
335 ret
= request_firmware(&fw
, image_name
, dev
);
337 mgr
->state
= FPGA_MGR_STATE_FIRMWARE_REQ_ERR
;
338 dev_err(dev
, "Error requesting firmware %s\n", image_name
);
342 ret
= fpga_mgr_buf_load(mgr
, info
, fw
->data
, fw
->size
);
344 release_firmware(fw
);
350 * fpga_mgr_load - load FPGA from scatter/gather table, buffer, or firmware
352 * @info: fpga image information.
354 * Load the FPGA from an image which is indicated in @info. If successful, the
355 * FPGA ends up in operating mode.
357 * Return: 0 on success, negative error code otherwise.
359 int fpga_mgr_load(struct fpga_manager
*mgr
, struct fpga_image_info
*info
)
362 return fpga_mgr_buf_load_sg(mgr
, info
, info
->sgt
);
363 if (info
->buf
&& info
->count
)
364 return fpga_mgr_buf_load(mgr
, info
, info
->buf
, info
->count
);
365 if (info
->firmware_name
)
366 return fpga_mgr_firmware_load(mgr
, info
, info
->firmware_name
);
369 EXPORT_SYMBOL_GPL(fpga_mgr_load
);
371 static const char * const state_str
[] = {
372 [FPGA_MGR_STATE_UNKNOWN
] = "unknown",
373 [FPGA_MGR_STATE_POWER_OFF
] = "power off",
374 [FPGA_MGR_STATE_POWER_UP
] = "power up",
375 [FPGA_MGR_STATE_RESET
] = "reset",
377 /* requesting FPGA image from firmware */
378 [FPGA_MGR_STATE_FIRMWARE_REQ
] = "firmware request",
379 [FPGA_MGR_STATE_FIRMWARE_REQ_ERR
] = "firmware request error",
381 /* Preparing FPGA to receive image */
382 [FPGA_MGR_STATE_WRITE_INIT
] = "write init",
383 [FPGA_MGR_STATE_WRITE_INIT_ERR
] = "write init error",
385 /* Writing image to FPGA */
386 [FPGA_MGR_STATE_WRITE
] = "write",
387 [FPGA_MGR_STATE_WRITE_ERR
] = "write error",
389 /* Finishing configuration after image has been written */
390 [FPGA_MGR_STATE_WRITE_COMPLETE
] = "write complete",
391 [FPGA_MGR_STATE_WRITE_COMPLETE_ERR
] = "write complete error",
393 /* FPGA reports to be in normal operating mode */
394 [FPGA_MGR_STATE_OPERATING
] = "operating",
397 static ssize_t
name_show(struct device
*dev
,
398 struct device_attribute
*attr
, char *buf
)
400 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
402 return sprintf(buf
, "%s\n", mgr
->name
);
405 static ssize_t
state_show(struct device
*dev
,
406 struct device_attribute
*attr
, char *buf
)
408 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
410 return sprintf(buf
, "%s\n", state_str
[mgr
->state
]);
413 static ssize_t
status_show(struct device
*dev
,
414 struct device_attribute
*attr
, char *buf
)
416 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
420 if (!mgr
->mops
->status
)
423 status
= mgr
->mops
->status(mgr
);
425 if (status
& FPGA_MGR_STATUS_OPERATION_ERR
)
426 len
+= sprintf(buf
+ len
, "reconfig operation error\n");
427 if (status
& FPGA_MGR_STATUS_CRC_ERR
)
428 len
+= sprintf(buf
+ len
, "reconfig CRC error\n");
429 if (status
& FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR
)
430 len
+= sprintf(buf
+ len
, "reconfig incompatible image\n");
431 if (status
& FPGA_MGR_STATUS_IP_PROTOCOL_ERR
)
432 len
+= sprintf(buf
+ len
, "reconfig IP protocol error\n");
433 if (status
& FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR
)
434 len
+= sprintf(buf
+ len
, "reconfig fifo overflow error\n");
439 static DEVICE_ATTR_RO(name
);
440 static DEVICE_ATTR_RO(state
);
441 static DEVICE_ATTR_RO(status
);
443 static struct attribute
*fpga_mgr_attrs
[] = {
445 &dev_attr_state
.attr
,
446 &dev_attr_status
.attr
,
449 ATTRIBUTE_GROUPS(fpga_mgr
);
451 static struct fpga_manager
*__fpga_mgr_get(struct device
*dev
)
453 struct fpga_manager
*mgr
;
455 mgr
= to_fpga_manager(dev
);
457 if (!try_module_get(dev
->parent
->driver
->owner
))
464 return ERR_PTR(-ENODEV
);
467 static int fpga_mgr_dev_match(struct device
*dev
, const void *data
)
469 return dev
->parent
== data
;
473 * fpga_mgr_get - Given a device, get a reference to a fpga mgr.
474 * @dev: parent device that fpga mgr was registered with
476 * Return: fpga manager struct or IS_ERR() condition containing error code.
478 struct fpga_manager
*fpga_mgr_get(struct device
*dev
)
480 struct device
*mgr_dev
= class_find_device(fpga_mgr_class
, NULL
, dev
,
483 return ERR_PTR(-ENODEV
);
485 return __fpga_mgr_get(mgr_dev
);
487 EXPORT_SYMBOL_GPL(fpga_mgr_get
);
490 * of_fpga_mgr_get - Given a device node, get a reference to a fpga mgr.
494 * Return: fpga manager struct or IS_ERR() condition containing error code.
496 struct fpga_manager
*of_fpga_mgr_get(struct device_node
*node
)
500 dev
= class_find_device_by_of_node(fpga_mgr_class
, node
);
502 return ERR_PTR(-ENODEV
);
504 return __fpga_mgr_get(dev
);
506 EXPORT_SYMBOL_GPL(of_fpga_mgr_get
);
509 * fpga_mgr_put - release a reference to a fpga manager
510 * @mgr: fpga manager structure
512 void fpga_mgr_put(struct fpga_manager
*mgr
)
514 module_put(mgr
->dev
.parent
->driver
->owner
);
515 put_device(&mgr
->dev
);
517 EXPORT_SYMBOL_GPL(fpga_mgr_put
);
520 * fpga_mgr_lock - Lock FPGA manager for exclusive use
523 * Given a pointer to FPGA Manager (from fpga_mgr_get() or
524 * of_fpga_mgr_put()) attempt to get the mutex. The user should call
525 * fpga_mgr_lock() and verify that it returns 0 before attempting to
526 * program the FPGA. Likewise, the user should call fpga_mgr_unlock
527 * when done programming the FPGA.
529 * Return: 0 for success or -EBUSY
531 int fpga_mgr_lock(struct fpga_manager
*mgr
)
533 if (!mutex_trylock(&mgr
->ref_mutex
)) {
534 dev_err(&mgr
->dev
, "FPGA manager is in use.\n");
540 EXPORT_SYMBOL_GPL(fpga_mgr_lock
);
543 * fpga_mgr_unlock - Unlock FPGA manager after done programming
546 void fpga_mgr_unlock(struct fpga_manager
*mgr
)
548 mutex_unlock(&mgr
->ref_mutex
);
550 EXPORT_SYMBOL_GPL(fpga_mgr_unlock
);
553 * fpga_mgr_create - create and initialize a FPGA manager struct
554 * @dev: fpga manager device from pdev
555 * @name: fpga manager name
556 * @mops: pointer to structure of fpga manager ops
557 * @priv: fpga manager private data
559 * The caller of this function is responsible for freeing the struct with
560 * fpga_mgr_free(). Using devm_fpga_mgr_create() instead is recommended.
562 * Return: pointer to struct fpga_manager or NULL
564 struct fpga_manager
*fpga_mgr_create(struct device
*dev
, const char *name
,
565 const struct fpga_manager_ops
*mops
,
568 struct fpga_manager
*mgr
;
571 if (!mops
|| !mops
->write_complete
|| !mops
->state
||
572 !mops
->write_init
|| (!mops
->write
&& !mops
->write_sg
) ||
573 (mops
->write
&& mops
->write_sg
)) {
574 dev_err(dev
, "Attempt to register without fpga_manager_ops\n");
578 if (!name
|| !strlen(name
)) {
579 dev_err(dev
, "Attempt to register with no name!\n");
583 mgr
= kzalloc(sizeof(*mgr
), GFP_KERNEL
);
587 id
= ida_simple_get(&fpga_mgr_ida
, 0, 0, GFP_KERNEL
);
591 mutex_init(&mgr
->ref_mutex
);
597 device_initialize(&mgr
->dev
);
598 mgr
->dev
.class = fpga_mgr_class
;
599 mgr
->dev
.groups
= mops
->groups
;
600 mgr
->dev
.parent
= dev
;
601 mgr
->dev
.of_node
= dev
->of_node
;
604 ret
= dev_set_name(&mgr
->dev
, "fpga%d", id
);
611 ida_simple_remove(&fpga_mgr_ida
, id
);
617 EXPORT_SYMBOL_GPL(fpga_mgr_create
);
620 * fpga_mgr_free - free a FPGA manager created with fpga_mgr_create()
621 * @mgr: fpga manager struct
623 void fpga_mgr_free(struct fpga_manager
*mgr
)
625 ida_simple_remove(&fpga_mgr_ida
, mgr
->dev
.id
);
628 EXPORT_SYMBOL_GPL(fpga_mgr_free
);
630 static void devm_fpga_mgr_release(struct device
*dev
, void *res
)
632 struct fpga_mgr_devres
*dr
= res
;
634 fpga_mgr_free(dr
->mgr
);
638 * devm_fpga_mgr_create - create and initialize a managed FPGA manager struct
639 * @dev: fpga manager device from pdev
640 * @name: fpga manager name
641 * @mops: pointer to structure of fpga manager ops
642 * @priv: fpga manager private data
644 * This function is intended for use in a FPGA manager driver's probe function.
645 * After the manager driver creates the manager struct with
646 * devm_fpga_mgr_create(), it should register it with fpga_mgr_register(). The
647 * manager driver's remove function should call fpga_mgr_unregister(). The
648 * manager struct allocated with this function will be freed automatically on
649 * driver detach. This includes the case of a probe function returning error
650 * before calling fpga_mgr_register(), the struct will still get cleaned up.
652 * Return: pointer to struct fpga_manager or NULL
654 struct fpga_manager
*devm_fpga_mgr_create(struct device
*dev
, const char *name
,
655 const struct fpga_manager_ops
*mops
,
658 struct fpga_mgr_devres
*dr
;
660 dr
= devres_alloc(devm_fpga_mgr_release
, sizeof(*dr
), GFP_KERNEL
);
664 dr
->mgr
= fpga_mgr_create(dev
, name
, mops
, priv
);
674 EXPORT_SYMBOL_GPL(devm_fpga_mgr_create
);
677 * fpga_mgr_register - register a FPGA manager
678 * @mgr: fpga manager struct
680 * Return: 0 on success, negative error code otherwise.
682 int fpga_mgr_register(struct fpga_manager
*mgr
)
687 * Initialize framework state by requesting low level driver read state
688 * from device. FPGA may be in reset mode or may have been programmed
689 * by bootloader or EEPROM.
691 mgr
->state
= mgr
->mops
->state(mgr
);
693 ret
= device_add(&mgr
->dev
);
697 dev_info(&mgr
->dev
, "%s registered\n", mgr
->name
);
702 ida_simple_remove(&fpga_mgr_ida
, mgr
->dev
.id
);
706 EXPORT_SYMBOL_GPL(fpga_mgr_register
);
709 * fpga_mgr_unregister - unregister a FPGA manager
710 * @mgr: fpga manager struct
712 * This function is intended for use in a FPGA manager driver's remove function.
714 void fpga_mgr_unregister(struct fpga_manager
*mgr
)
716 dev_info(&mgr
->dev
, "%s %s\n", __func__
, mgr
->name
);
719 * If the low level driver provides a method for putting fpga into
720 * a desired state upon unregister, do it.
722 if (mgr
->mops
->fpga_remove
)
723 mgr
->mops
->fpga_remove(mgr
);
725 device_unregister(&mgr
->dev
);
727 EXPORT_SYMBOL_GPL(fpga_mgr_unregister
);
729 static int fpga_mgr_devres_match(struct device
*dev
, void *res
,
732 struct fpga_mgr_devres
*dr
= res
;
734 return match_data
== dr
->mgr
;
737 static void devm_fpga_mgr_unregister(struct device
*dev
, void *res
)
739 struct fpga_mgr_devres
*dr
= res
;
741 fpga_mgr_unregister(dr
->mgr
);
745 * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
746 * @dev: managing device for this FPGA manager
747 * @mgr: fpga manager struct
749 * This is the devres variant of fpga_mgr_register() for which the unregister
750 * function will be called automatically when the managing device is detached.
752 int devm_fpga_mgr_register(struct device
*dev
, struct fpga_manager
*mgr
)
754 struct fpga_mgr_devres
*dr
;
758 * Make sure that the struct fpga_manager * that is passed in is
761 if (WARN_ON(!devres_find(dev
, devm_fpga_mgr_release
,
762 fpga_mgr_devres_match
, mgr
)))
765 dr
= devres_alloc(devm_fpga_mgr_unregister
, sizeof(*dr
), GFP_KERNEL
);
769 ret
= fpga_mgr_register(mgr
);
780 EXPORT_SYMBOL_GPL(devm_fpga_mgr_register
);
782 static void fpga_mgr_dev_release(struct device
*dev
)
786 static int __init
fpga_mgr_class_init(void)
788 pr_info("FPGA manager framework\n");
790 fpga_mgr_class
= class_create(THIS_MODULE
, "fpga_manager");
791 if (IS_ERR(fpga_mgr_class
))
792 return PTR_ERR(fpga_mgr_class
);
794 fpga_mgr_class
->dev_groups
= fpga_mgr_groups
;
795 fpga_mgr_class
->dev_release
= fpga_mgr_dev_release
;
800 static void __exit
fpga_mgr_class_exit(void)
802 class_destroy(fpga_mgr_class
);
803 ida_destroy(&fpga_mgr_ida
);
806 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
807 MODULE_DESCRIPTION("FPGA manager framework");
808 MODULE_LICENSE("GPL v2");
810 subsys_initcall(fpga_mgr_class_init
);
811 module_exit(fpga_mgr_class_exit
);