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
;
25 * fpga_image_info_alloc - Allocate a FPGA image info struct
28 * Return: struct fpga_image_info or NULL
30 struct fpga_image_info
*fpga_image_info_alloc(struct device
*dev
)
32 struct fpga_image_info
*info
;
36 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
46 EXPORT_SYMBOL_GPL(fpga_image_info_alloc
);
49 * fpga_image_info_free - Free a FPGA image info struct
50 * @info: FPGA image info struct to free
52 void fpga_image_info_free(struct fpga_image_info
*info
)
60 if (info
->firmware_name
)
61 devm_kfree(dev
, info
->firmware_name
);
63 devm_kfree(dev
, info
);
66 EXPORT_SYMBOL_GPL(fpga_image_info_free
);
69 * Call the low level driver's write_init function. This will do the
70 * device-specific things to get the FPGA into the state where it is ready to
71 * receive an FPGA image. The low level driver only gets to see the first
72 * initial_header_size bytes in the buffer.
74 static int fpga_mgr_write_init_buf(struct fpga_manager
*mgr
,
75 struct fpga_image_info
*info
,
76 const char *buf
, size_t count
)
80 mgr
->state
= FPGA_MGR_STATE_WRITE_INIT
;
81 if (!mgr
->mops
->initial_header_size
)
82 ret
= mgr
->mops
->write_init(mgr
, info
, NULL
, 0);
84 ret
= mgr
->mops
->write_init(
85 mgr
, info
, buf
, min(mgr
->mops
->initial_header_size
, count
));
88 dev_err(&mgr
->dev
, "Error preparing FPGA for writing\n");
89 mgr
->state
= FPGA_MGR_STATE_WRITE_INIT_ERR
;
96 static int fpga_mgr_write_init_sg(struct fpga_manager
*mgr
,
97 struct fpga_image_info
*info
,
100 struct sg_mapping_iter miter
;
105 if (!mgr
->mops
->initial_header_size
)
106 return fpga_mgr_write_init_buf(mgr
, info
, NULL
, 0);
109 * First try to use miter to map the first fragment to access the
110 * header, this is the typical path.
112 sg_miter_start(&miter
, sgt
->sgl
, sgt
->nents
, SG_MITER_FROM_SG
);
113 if (sg_miter_next(&miter
) &&
114 miter
.length
>= mgr
->mops
->initial_header_size
) {
115 ret
= fpga_mgr_write_init_buf(mgr
, info
, miter
.addr
,
117 sg_miter_stop(&miter
);
120 sg_miter_stop(&miter
);
122 /* Otherwise copy the fragments into temporary memory. */
123 buf
= kmalloc(mgr
->mops
->initial_header_size
, GFP_KERNEL
);
127 len
= sg_copy_to_buffer(sgt
->sgl
, sgt
->nents
, buf
,
128 mgr
->mops
->initial_header_size
);
129 ret
= fpga_mgr_write_init_buf(mgr
, info
, buf
, len
);
137 * After all the FPGA image has been written, do the device specific steps to
138 * finish and set the FPGA into operating mode.
140 static int fpga_mgr_write_complete(struct fpga_manager
*mgr
,
141 struct fpga_image_info
*info
)
145 mgr
->state
= FPGA_MGR_STATE_WRITE_COMPLETE
;
146 ret
= mgr
->mops
->write_complete(mgr
, info
);
148 dev_err(&mgr
->dev
, "Error after writing image data to FPGA\n");
149 mgr
->state
= FPGA_MGR_STATE_WRITE_COMPLETE_ERR
;
152 mgr
->state
= FPGA_MGR_STATE_OPERATING
;
158 * fpga_mgr_buf_load_sg - load fpga from image in buffer from a scatter list
160 * @info: fpga image specific information
161 * @sgt: scatterlist table
163 * Step the low level fpga manager through the device-specific steps of getting
164 * an FPGA ready to be configured, writing the image to it, then doing whatever
165 * post-configuration steps necessary. This code assumes the caller got the
166 * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is
169 * This is the preferred entry point for FPGA programming, it does not require
170 * any contiguous kernel memory.
172 * Return: 0 on success, negative error code otherwise.
174 static int fpga_mgr_buf_load_sg(struct fpga_manager
*mgr
,
175 struct fpga_image_info
*info
,
176 struct sg_table
*sgt
)
180 ret
= fpga_mgr_write_init_sg(mgr
, info
, sgt
);
184 /* Write the FPGA image to the FPGA. */
185 mgr
->state
= FPGA_MGR_STATE_WRITE
;
186 if (mgr
->mops
->write_sg
) {
187 ret
= mgr
->mops
->write_sg(mgr
, sgt
);
189 struct sg_mapping_iter miter
;
191 sg_miter_start(&miter
, sgt
->sgl
, sgt
->nents
, SG_MITER_FROM_SG
);
192 while (sg_miter_next(&miter
)) {
193 ret
= mgr
->mops
->write(mgr
, miter
.addr
, miter
.length
);
197 sg_miter_stop(&miter
);
201 dev_err(&mgr
->dev
, "Error while writing image data to FPGA\n");
202 mgr
->state
= FPGA_MGR_STATE_WRITE_ERR
;
206 return fpga_mgr_write_complete(mgr
, info
);
209 static int fpga_mgr_buf_load_mapped(struct fpga_manager
*mgr
,
210 struct fpga_image_info
*info
,
211 const char *buf
, size_t count
)
215 ret
= fpga_mgr_write_init_buf(mgr
, info
, buf
, count
);
220 * Write the FPGA image to the FPGA.
222 mgr
->state
= FPGA_MGR_STATE_WRITE
;
223 ret
= mgr
->mops
->write(mgr
, buf
, count
);
225 dev_err(&mgr
->dev
, "Error while writing image data to FPGA\n");
226 mgr
->state
= FPGA_MGR_STATE_WRITE_ERR
;
230 return fpga_mgr_write_complete(mgr
, info
);
234 * fpga_mgr_buf_load - load fpga from image in buffer
236 * @info: fpga image info
237 * @buf: buffer contain fpga image
238 * @count: byte count of buf
240 * Step the low level fpga manager through the device-specific steps of getting
241 * an FPGA ready to be configured, writing the image to it, then doing whatever
242 * post-configuration steps necessary. This code assumes the caller got the
243 * mgr pointer from of_fpga_mgr_get() and checked that it is not an error code.
245 * Return: 0 on success, negative error code otherwise.
247 static int fpga_mgr_buf_load(struct fpga_manager
*mgr
,
248 struct fpga_image_info
*info
,
249 const char *buf
, size_t count
)
259 * This is just a fast path if the caller has already created a
260 * contiguous kernel buffer and the driver doesn't require SG, non-SG
261 * drivers will still work on the slow path.
263 if (mgr
->mops
->write
)
264 return fpga_mgr_buf_load_mapped(mgr
, info
, buf
, count
);
267 * Convert the linear kernel pointer into a sg_table of pages for use
270 nr_pages
= DIV_ROUND_UP((unsigned long)buf
+ count
, PAGE_SIZE
) -
271 (unsigned long)buf
/ PAGE_SIZE
;
272 pages
= kmalloc_array(nr_pages
, sizeof(struct page
*), GFP_KERNEL
);
276 p
= buf
- offset_in_page(buf
);
277 for (index
= 0; index
< nr_pages
; index
++) {
278 if (is_vmalloc_addr(p
))
279 pages
[index
] = vmalloc_to_page(p
);
281 pages
[index
] = kmap_to_page((void *)p
);
290 * The temporary pages list is used to code share the merging algorithm
291 * in sg_alloc_table_from_pages
293 rc
= sg_alloc_table_from_pages(&sgt
, pages
, index
, offset_in_page(buf
),
299 rc
= fpga_mgr_buf_load_sg(mgr
, info
, &sgt
);
306 * fpga_mgr_firmware_load - request firmware and load to fpga
308 * @info: fpga image specific information
309 * @image_name: name of image file on the firmware search path
311 * Request an FPGA image using the firmware class, then write out to the FPGA.
312 * Update the state before each step to provide info on what step failed if
313 * there is a failure. This code assumes the caller got the mgr pointer
314 * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
317 * Return: 0 on success, negative error code otherwise.
319 static int fpga_mgr_firmware_load(struct fpga_manager
*mgr
,
320 struct fpga_image_info
*info
,
321 const char *image_name
)
323 struct device
*dev
= &mgr
->dev
;
324 const struct firmware
*fw
;
327 dev_info(dev
, "writing %s to %s\n", image_name
, mgr
->name
);
329 mgr
->state
= FPGA_MGR_STATE_FIRMWARE_REQ
;
331 ret
= request_firmware(&fw
, image_name
, dev
);
333 mgr
->state
= FPGA_MGR_STATE_FIRMWARE_REQ_ERR
;
334 dev_err(dev
, "Error requesting firmware %s\n", image_name
);
338 ret
= fpga_mgr_buf_load(mgr
, info
, fw
->data
, fw
->size
);
340 release_firmware(fw
);
346 * fpga_mgr_load - load FPGA from scatter/gather table, buffer, or firmware
348 * @info: fpga image information.
350 * Load the FPGA from an image which is indicated in @info. If successful, the
351 * FPGA ends up in operating mode.
353 * Return: 0 on success, negative error code otherwise.
355 int fpga_mgr_load(struct fpga_manager
*mgr
, struct fpga_image_info
*info
)
358 return fpga_mgr_buf_load_sg(mgr
, info
, info
->sgt
);
359 if (info
->buf
&& info
->count
)
360 return fpga_mgr_buf_load(mgr
, info
, info
->buf
, info
->count
);
361 if (info
->firmware_name
)
362 return fpga_mgr_firmware_load(mgr
, info
, info
->firmware_name
);
365 EXPORT_SYMBOL_GPL(fpga_mgr_load
);
367 static const char * const state_str
[] = {
368 [FPGA_MGR_STATE_UNKNOWN
] = "unknown",
369 [FPGA_MGR_STATE_POWER_OFF
] = "power off",
370 [FPGA_MGR_STATE_POWER_UP
] = "power up",
371 [FPGA_MGR_STATE_RESET
] = "reset",
373 /* requesting FPGA image from firmware */
374 [FPGA_MGR_STATE_FIRMWARE_REQ
] = "firmware request",
375 [FPGA_MGR_STATE_FIRMWARE_REQ_ERR
] = "firmware request error",
377 /* Preparing FPGA to receive image */
378 [FPGA_MGR_STATE_WRITE_INIT
] = "write init",
379 [FPGA_MGR_STATE_WRITE_INIT_ERR
] = "write init error",
381 /* Writing image to FPGA */
382 [FPGA_MGR_STATE_WRITE
] = "write",
383 [FPGA_MGR_STATE_WRITE_ERR
] = "write error",
385 /* Finishing configuration after image has been written */
386 [FPGA_MGR_STATE_WRITE_COMPLETE
] = "write complete",
387 [FPGA_MGR_STATE_WRITE_COMPLETE_ERR
] = "write complete error",
389 /* FPGA reports to be in normal operating mode */
390 [FPGA_MGR_STATE_OPERATING
] = "operating",
393 static ssize_t
name_show(struct device
*dev
,
394 struct device_attribute
*attr
, char *buf
)
396 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
398 return sprintf(buf
, "%s\n", mgr
->name
);
401 static ssize_t
state_show(struct device
*dev
,
402 struct device_attribute
*attr
, char *buf
)
404 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
406 return sprintf(buf
, "%s\n", state_str
[mgr
->state
]);
409 static ssize_t
status_show(struct device
*dev
,
410 struct device_attribute
*attr
, char *buf
)
412 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
416 if (!mgr
->mops
->status
)
419 status
= mgr
->mops
->status(mgr
);
421 if (status
& FPGA_MGR_STATUS_OPERATION_ERR
)
422 len
+= sprintf(buf
+ len
, "reconfig operation error\n");
423 if (status
& FPGA_MGR_STATUS_CRC_ERR
)
424 len
+= sprintf(buf
+ len
, "reconfig CRC error\n");
425 if (status
& FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR
)
426 len
+= sprintf(buf
+ len
, "reconfig incompatible image\n");
427 if (status
& FPGA_MGR_STATUS_IP_PROTOCOL_ERR
)
428 len
+= sprintf(buf
+ len
, "reconfig IP protocol error\n");
429 if (status
& FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR
)
430 len
+= sprintf(buf
+ len
, "reconfig fifo overflow error\n");
435 static DEVICE_ATTR_RO(name
);
436 static DEVICE_ATTR_RO(state
);
437 static DEVICE_ATTR_RO(status
);
439 static struct attribute
*fpga_mgr_attrs
[] = {
441 &dev_attr_state
.attr
,
442 &dev_attr_status
.attr
,
445 ATTRIBUTE_GROUPS(fpga_mgr
);
447 static struct fpga_manager
*__fpga_mgr_get(struct device
*dev
)
449 struct fpga_manager
*mgr
;
451 mgr
= to_fpga_manager(dev
);
453 if (!try_module_get(dev
->parent
->driver
->owner
))
460 return ERR_PTR(-ENODEV
);
463 static int fpga_mgr_dev_match(struct device
*dev
, const void *data
)
465 return dev
->parent
== data
;
469 * fpga_mgr_get - Given a device, get a reference to a fpga mgr.
470 * @dev: parent device that fpga mgr was registered with
472 * Return: fpga manager struct or IS_ERR() condition containing error code.
474 struct fpga_manager
*fpga_mgr_get(struct device
*dev
)
476 struct device
*mgr_dev
= class_find_device(fpga_mgr_class
, NULL
, dev
,
479 return ERR_PTR(-ENODEV
);
481 return __fpga_mgr_get(mgr_dev
);
483 EXPORT_SYMBOL_GPL(fpga_mgr_get
);
485 static int fpga_mgr_of_node_match(struct device
*dev
, const void *data
)
487 return dev
->of_node
== data
;
491 * of_fpga_mgr_get - Given a device node, get a reference to a fpga mgr.
495 * Return: fpga manager struct or IS_ERR() condition containing error code.
497 struct fpga_manager
*of_fpga_mgr_get(struct device_node
*node
)
501 dev
= class_find_device(fpga_mgr_class
, NULL
, node
,
502 fpga_mgr_of_node_match
);
504 return ERR_PTR(-ENODEV
);
506 return __fpga_mgr_get(dev
);
508 EXPORT_SYMBOL_GPL(of_fpga_mgr_get
);
511 * fpga_mgr_put - release a reference to a fpga manager
512 * @mgr: fpga manager structure
514 void fpga_mgr_put(struct fpga_manager
*mgr
)
516 module_put(mgr
->dev
.parent
->driver
->owner
);
517 put_device(&mgr
->dev
);
519 EXPORT_SYMBOL_GPL(fpga_mgr_put
);
522 * fpga_mgr_lock - Lock FPGA manager for exclusive use
525 * Given a pointer to FPGA Manager (from fpga_mgr_get() or
526 * of_fpga_mgr_put()) attempt to get the mutex. The user should call
527 * fpga_mgr_lock() and verify that it returns 0 before attempting to
528 * program the FPGA. Likewise, the user should call fpga_mgr_unlock
529 * when done programming the FPGA.
531 * Return: 0 for success or -EBUSY
533 int fpga_mgr_lock(struct fpga_manager
*mgr
)
535 if (!mutex_trylock(&mgr
->ref_mutex
)) {
536 dev_err(&mgr
->dev
, "FPGA manager is in use.\n");
542 EXPORT_SYMBOL_GPL(fpga_mgr_lock
);
545 * fpga_mgr_unlock - Unlock FPGA manager after done programming
548 void fpga_mgr_unlock(struct fpga_manager
*mgr
)
550 mutex_unlock(&mgr
->ref_mutex
);
552 EXPORT_SYMBOL_GPL(fpga_mgr_unlock
);
555 * fpga_mgr_create - create and initialize a FPGA manager struct
556 * @dev: fpga manager device from pdev
557 * @name: fpga manager name
558 * @mops: pointer to structure of fpga manager ops
559 * @priv: fpga manager private data
561 * Return: pointer to struct fpga_manager or NULL
563 struct fpga_manager
*fpga_mgr_create(struct device
*dev
, const char *name
,
564 const struct fpga_manager_ops
*mops
,
567 struct fpga_manager
*mgr
;
570 if (!mops
|| !mops
->write_complete
|| !mops
->state
||
571 !mops
->write_init
|| (!mops
->write
&& !mops
->write_sg
) ||
572 (mops
->write
&& mops
->write_sg
)) {
573 dev_err(dev
, "Attempt to register without fpga_manager_ops\n");
577 if (!name
|| !strlen(name
)) {
578 dev_err(dev
, "Attempt to register with no name!\n");
582 mgr
= kzalloc(sizeof(*mgr
), GFP_KERNEL
);
586 id
= ida_simple_get(&fpga_mgr_ida
, 0, 0, GFP_KERNEL
);
592 mutex_init(&mgr
->ref_mutex
);
598 device_initialize(&mgr
->dev
);
599 mgr
->dev
.class = fpga_mgr_class
;
600 mgr
->dev
.groups
= mops
->groups
;
601 mgr
->dev
.parent
= dev
;
602 mgr
->dev
.of_node
= dev
->of_node
;
605 ret
= dev_set_name(&mgr
->dev
, "fpga%d", id
);
612 ida_simple_remove(&fpga_mgr_ida
, id
);
618 EXPORT_SYMBOL_GPL(fpga_mgr_create
);
621 * fpga_mgr_free - deallocate a FPGA manager
622 * @mgr: fpga manager struct created by fpga_mgr_create
624 void fpga_mgr_free(struct fpga_manager
*mgr
)
626 ida_simple_remove(&fpga_mgr_ida
, mgr
->dev
.id
);
629 EXPORT_SYMBOL_GPL(fpga_mgr_free
);
632 * fpga_mgr_register - register a FPGA manager
633 * @mgr: fpga manager struct created by fpga_mgr_create
635 * Return: 0 on success, negative error code otherwise.
637 int fpga_mgr_register(struct fpga_manager
*mgr
)
642 * Initialize framework state by requesting low level driver read state
643 * from device. FPGA may be in reset mode or may have been programmed
644 * by bootloader or EEPROM.
646 mgr
->state
= mgr
->mops
->state(mgr
);
648 ret
= device_add(&mgr
->dev
);
652 dev_info(&mgr
->dev
, "%s registered\n", mgr
->name
);
657 ida_simple_remove(&fpga_mgr_ida
, mgr
->dev
.id
);
661 EXPORT_SYMBOL_GPL(fpga_mgr_register
);
664 * fpga_mgr_unregister - unregister and free a FPGA manager
665 * @mgr: fpga manager struct
667 void fpga_mgr_unregister(struct fpga_manager
*mgr
)
669 dev_info(&mgr
->dev
, "%s %s\n", __func__
, mgr
->name
);
672 * If the low level driver provides a method for putting fpga into
673 * a desired state upon unregister, do it.
675 if (mgr
->mops
->fpga_remove
)
676 mgr
->mops
->fpga_remove(mgr
);
678 device_unregister(&mgr
->dev
);
680 EXPORT_SYMBOL_GPL(fpga_mgr_unregister
);
682 static void fpga_mgr_dev_release(struct device
*dev
)
684 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
689 static int __init
fpga_mgr_class_init(void)
691 pr_info("FPGA manager framework\n");
693 fpga_mgr_class
= class_create(THIS_MODULE
, "fpga_manager");
694 if (IS_ERR(fpga_mgr_class
))
695 return PTR_ERR(fpga_mgr_class
);
697 fpga_mgr_class
->dev_groups
= fpga_mgr_groups
;
698 fpga_mgr_class
->dev_release
= fpga_mgr_dev_release
;
703 static void __exit
fpga_mgr_class_exit(void)
705 class_destroy(fpga_mgr_class
);
706 ida_destroy(&fpga_mgr_ida
);
709 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
710 MODULE_DESCRIPTION("FPGA manager framework");
711 MODULE_LICENSE("GPL v2");
713 subsys_initcall(fpga_mgr_class_init
);
714 module_exit(fpga_mgr_class_exit
);