diff options
author | Alan Tull <atull@kernel.org> | 2017-11-15 15:20:12 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2017-11-28 10:30:37 -0500 |
commit | 5cf0c7f6502f26332b46fa87914553a4d6ae75ac (patch) | |
tree | 56814a3ec0bf179797e7d9fed89136954f937d3e /drivers | |
parent | 9c1c4b2753fea36a072e78a5efc82fca0d13b455 (diff) |
fpga: mgr: API change to replace fpga load functions with single function
fpga-mgr has three methods for programming FPGAs, depending on
whether the image is in a scatter gather list, a contiguous
buffer, or a firmware file. This makes it difficult to write
upper layers as the caller has to assume whether the FPGA image
is in a sg table, as a single buffer, or a firmware file.
This commit moves these parameters to struct fpga_image_info
and adds a single function for programming fpgas.
New functions:
* fpga_mgr_load - given fpga manager and struct fpga_image_info,
program the fpga.
* fpga_image_info_alloc - alloc a struct fpga_image_info.
* fpga_image_info_free - free a struct fpga_image_info.
These three functions are unexported:
* fpga_mgr_buf_load_sg
* fpga_mgr_buf_load
* fpga_mgr_firmware_load
Also use devm_kstrdup to copy firmware_name so we aren't making
assumptions about where it comes from when allocing/freeing the
struct fpga_image_info.
API documentation has been updated and a new document for
FPGA region has been added.
Signed-off-by: Alan Tull <atull@kernel.org>
Acked-by: Moritz Fischer <mdf@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/fpga/fpga-mgr.c | 68 | ||||
-rw-r--r-- | drivers/fpga/fpga-region.c | 43 |
2 files changed, 85 insertions, 26 deletions
diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c index 188ffefa3cc3..a8dd54945470 100644 --- a/drivers/fpga/fpga-mgr.c +++ b/drivers/fpga/fpga-mgr.c | |||
@@ -2,6 +2,7 @@ | |||
2 | * FPGA Manager Core | 2 | * FPGA Manager Core |
3 | * | 3 | * |
4 | * Copyright (C) 2013-2015 Altera Corporation | 4 | * Copyright (C) 2013-2015 Altera Corporation |
5 | * Copyright (C) 2017 Intel Corporation | ||
5 | * | 6 | * |
6 | * With code from the mailing list: | 7 | * With code from the mailing list: |
7 | * Copyright (C) 2013 Xilinx, Inc. | 8 | * Copyright (C) 2013 Xilinx, Inc. |
@@ -31,6 +32,40 @@ | |||
31 | static DEFINE_IDA(fpga_mgr_ida); | 32 | static DEFINE_IDA(fpga_mgr_ida); |
32 | static struct class *fpga_mgr_class; | 33 | static struct class *fpga_mgr_class; |
33 | 34 | ||
35 | struct fpga_image_info *fpga_image_info_alloc(struct device *dev) | ||
36 | { | ||
37 | struct fpga_image_info *info; | ||
38 | |||
39 | get_device(dev); | ||
40 | |||
41 | info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); | ||
42 | if (!info) { | ||
43 | put_device(dev); | ||
44 | return NULL; | ||
45 | } | ||
46 | |||
47 | info->dev = dev; | ||
48 | |||
49 | return info; | ||
50 | } | ||
51 | EXPORT_SYMBOL_GPL(fpga_image_info_alloc); | ||
52 | |||
53 | void fpga_image_info_free(struct fpga_image_info *info) | ||
54 | { | ||
55 | struct device *dev; | ||
56 | |||
57 | if (!info) | ||
58 | return; | ||
59 | |||
60 | dev = info->dev; | ||
61 | if (info->firmware_name) | ||
62 | devm_kfree(dev, info->firmware_name); | ||
63 | |||
64 | devm_kfree(dev, info); | ||
65 | put_device(dev); | ||
66 | } | ||
67 | EXPORT_SYMBOL_GPL(fpga_image_info_free); | ||
68 | |||
34 | /* | 69 | /* |
35 | * Call the low level driver's write_init function. This will do the | 70 | * Call the low level driver's write_init function. This will do the |
36 | * device-specific things to get the FPGA into the state where it is ready to | 71 | * device-specific things to get the FPGA into the state where it is ready to |
@@ -137,8 +172,9 @@ static int fpga_mgr_write_complete(struct fpga_manager *mgr, | |||
137 | * | 172 | * |
138 | * Return: 0 on success, negative error code otherwise. | 173 | * Return: 0 on success, negative error code otherwise. |
139 | */ | 174 | */ |
140 | int fpga_mgr_buf_load_sg(struct fpga_manager *mgr, struct fpga_image_info *info, | 175 | static int fpga_mgr_buf_load_sg(struct fpga_manager *mgr, |
141 | struct sg_table *sgt) | 176 | struct fpga_image_info *info, |
177 | struct sg_table *sgt) | ||
142 | { | 178 | { |
143 | int ret; | 179 | int ret; |
144 | 180 | ||
@@ -170,7 +206,6 @@ int fpga_mgr_buf_load_sg(struct fpga_manager *mgr, struct fpga_image_info *info, | |||
170 | 206 | ||
171 | return fpga_mgr_write_complete(mgr, info); | 207 | return fpga_mgr_write_complete(mgr, info); |
172 | } | 208 | } |
173 | EXPORT_SYMBOL_GPL(fpga_mgr_buf_load_sg); | ||
174 | 209 | ||
175 | static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr, | 210 | static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr, |
176 | struct fpga_image_info *info, | 211 | struct fpga_image_info *info, |
@@ -210,8 +245,9 @@ static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr, | |||
210 | * | 245 | * |
211 | * Return: 0 on success, negative error code otherwise. | 246 | * Return: 0 on success, negative error code otherwise. |
212 | */ | 247 | */ |
213 | int fpga_mgr_buf_load(struct fpga_manager *mgr, struct fpga_image_info *info, | 248 | static int fpga_mgr_buf_load(struct fpga_manager *mgr, |
214 | const char *buf, size_t count) | 249 | struct fpga_image_info *info, |
250 | const char *buf, size_t count) | ||
215 | { | 251 | { |
216 | struct page **pages; | 252 | struct page **pages; |
217 | struct sg_table sgt; | 253 | struct sg_table sgt; |
@@ -266,7 +302,6 @@ int fpga_mgr_buf_load(struct fpga_manager *mgr, struct fpga_image_info *info, | |||
266 | 302 | ||
267 | return rc; | 303 | return rc; |
268 | } | 304 | } |
269 | EXPORT_SYMBOL_GPL(fpga_mgr_buf_load); | ||
270 | 305 | ||
271 | /** | 306 | /** |
272 | * fpga_mgr_firmware_load - request firmware and load to fpga | 307 | * fpga_mgr_firmware_load - request firmware and load to fpga |
@@ -282,9 +317,9 @@ EXPORT_SYMBOL_GPL(fpga_mgr_buf_load); | |||
282 | * | 317 | * |
283 | * Return: 0 on success, negative error code otherwise. | 318 | * Return: 0 on success, negative error code otherwise. |
284 | */ | 319 | */ |
285 | int fpga_mgr_firmware_load(struct fpga_manager *mgr, | 320 | static int fpga_mgr_firmware_load(struct fpga_manager *mgr, |
286 | struct fpga_image_info *info, | 321 | struct fpga_image_info *info, |
287 | const char *image_name) | 322 | const char *image_name) |
288 | { | 323 | { |
289 | struct device *dev = &mgr->dev; | 324 | struct device *dev = &mgr->dev; |
290 | const struct firmware *fw; | 325 | const struct firmware *fw; |
@@ -307,7 +342,18 @@ int fpga_mgr_firmware_load(struct fpga_manager *mgr, | |||
307 | 342 | ||
308 | return ret; | 343 | return ret; |
309 | } | 344 | } |
310 | EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load); | 345 | |
346 | int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info) | ||
347 | { | ||
348 | if (info->sgt) | ||
349 | return fpga_mgr_buf_load_sg(mgr, info, info->sgt); | ||
350 | if (info->buf && info->count) | ||
351 | return fpga_mgr_buf_load(mgr, info, info->buf, info->count); | ||
352 | if (info->firmware_name) | ||
353 | return fpga_mgr_firmware_load(mgr, info, info->firmware_name); | ||
354 | return -EINVAL; | ||
355 | } | ||
356 | EXPORT_SYMBOL_GPL(fpga_mgr_load); | ||
311 | 357 | ||
312 | static const char * const state_str[] = { | 358 | static const char * const state_str[] = { |
313 | [FPGA_MGR_STATE_UNKNOWN] = "unknown", | 359 | [FPGA_MGR_STATE_UNKNOWN] = "unknown", |
@@ -578,7 +624,7 @@ static void __exit fpga_mgr_class_exit(void) | |||
578 | ida_destroy(&fpga_mgr_ida); | 624 | ida_destroy(&fpga_mgr_ida); |
579 | } | 625 | } |
580 | 626 | ||
581 | MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>"); | 627 | MODULE_AUTHOR("Alan Tull <atull@kernel.org>"); |
582 | MODULE_DESCRIPTION("FPGA manager framework"); | 628 | MODULE_DESCRIPTION("FPGA manager framework"); |
583 | MODULE_LICENSE("GPL v2"); | 629 | MODULE_LICENSE("GPL v2"); |
584 | 630 | ||
diff --git a/drivers/fpga/fpga-region.c b/drivers/fpga/fpga-region.c index 9175556215b1..120c496eb7bd 100644 --- a/drivers/fpga/fpga-region.c +++ b/drivers/fpga/fpga-region.c | |||
@@ -226,14 +226,11 @@ static int fpga_region_get_bridges(struct fpga_region *region, | |||
226 | /** | 226 | /** |
227 | * fpga_region_program_fpga - program FPGA | 227 | * fpga_region_program_fpga - program FPGA |
228 | * @region: FPGA region | 228 | * @region: FPGA region |
229 | * @firmware_name: name of FPGA image firmware file | ||
230 | * @overlay: device node of the overlay | 229 | * @overlay: device node of the overlay |
231 | * Program an FPGA using information in the device tree. | 230 | * Program an FPGA using information in the region's fpga image info. |
232 | * Function assumes that there is a firmware-name property. | ||
233 | * Return 0 for success or negative error code. | 231 | * Return 0 for success or negative error code. |
234 | */ | 232 | */ |
235 | static int fpga_region_program_fpga(struct fpga_region *region, | 233 | static int fpga_region_program_fpga(struct fpga_region *region, |
236 | const char *firmware_name, | ||
237 | struct device_node *overlay) | 234 | struct device_node *overlay) |
238 | { | 235 | { |
239 | struct fpga_manager *mgr; | 236 | struct fpga_manager *mgr; |
@@ -264,7 +261,7 @@ static int fpga_region_program_fpga(struct fpga_region *region, | |||
264 | goto err_put_br; | 261 | goto err_put_br; |
265 | } | 262 | } |
266 | 263 | ||
267 | ret = fpga_mgr_firmware_load(mgr, region->info, firmware_name); | 264 | ret = fpga_mgr_load(mgr, region->info); |
268 | if (ret) { | 265 | if (ret) { |
269 | pr_err("failed to load fpga image\n"); | 266 | pr_err("failed to load fpga image\n"); |
270 | goto err_put_br; | 267 | goto err_put_br; |
@@ -357,16 +354,15 @@ static int child_regions_with_firmware(struct device_node *overlay) | |||
357 | static int fpga_region_notify_pre_apply(struct fpga_region *region, | 354 | static int fpga_region_notify_pre_apply(struct fpga_region *region, |
358 | struct of_overlay_notify_data *nd) | 355 | struct of_overlay_notify_data *nd) |
359 | { | 356 | { |
360 | const char *firmware_name = NULL; | 357 | struct device *dev = ®ion->dev; |
361 | struct fpga_image_info *info; | 358 | struct fpga_image_info *info; |
359 | const char *firmware_name; | ||
362 | int ret; | 360 | int ret; |
363 | 361 | ||
364 | info = devm_kzalloc(®ion->dev, sizeof(*info), GFP_KERNEL); | 362 | info = fpga_image_info_alloc(dev); |
365 | if (!info) | 363 | if (!info) |
366 | return -ENOMEM; | 364 | return -ENOMEM; |
367 | 365 | ||
368 | region->info = info; | ||
369 | |||
370 | /* Reject overlay if child FPGA Regions have firmware-name property */ | 366 | /* Reject overlay if child FPGA Regions have firmware-name property */ |
371 | ret = child_regions_with_firmware(nd->overlay); | 367 | ret = child_regions_with_firmware(nd->overlay); |
372 | if (ret) | 368 | if (ret) |
@@ -382,7 +378,13 @@ static int fpga_region_notify_pre_apply(struct fpga_region *region, | |||
382 | if (of_property_read_bool(nd->overlay, "encrypted-fpga-config")) | 378 | if (of_property_read_bool(nd->overlay, "encrypted-fpga-config")) |
383 | info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM; | 379 | info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM; |
384 | 380 | ||
385 | of_property_read_string(nd->overlay, "firmware-name", &firmware_name); | 381 | if (!of_property_read_string(nd->overlay, "firmware-name", |
382 | &firmware_name)) { | ||
383 | info->firmware_name = devm_kstrdup(dev, firmware_name, | ||
384 | GFP_KERNEL); | ||
385 | if (!info->firmware_name) | ||
386 | return -ENOMEM; | ||
387 | } | ||
386 | 388 | ||
387 | of_property_read_u32(nd->overlay, "region-unfreeze-timeout-us", | 389 | of_property_read_u32(nd->overlay, "region-unfreeze-timeout-us", |
388 | &info->enable_timeout_us); | 390 | &info->enable_timeout_us); |
@@ -394,22 +396,33 @@ static int fpga_region_notify_pre_apply(struct fpga_region *region, | |||
394 | &info->config_complete_timeout_us); | 396 | &info->config_complete_timeout_us); |
395 | 397 | ||
396 | /* If FPGA was externally programmed, don't specify firmware */ | 398 | /* If FPGA was externally programmed, don't specify firmware */ |
397 | if ((info->flags & FPGA_MGR_EXTERNAL_CONFIG) && firmware_name) { | 399 | if ((info->flags & FPGA_MGR_EXTERNAL_CONFIG) && info->firmware_name) { |
398 | pr_err("error: specified firmware and external-fpga-config"); | 400 | pr_err("error: specified firmware and external-fpga-config"); |
401 | fpga_image_info_free(info); | ||
399 | return -EINVAL; | 402 | return -EINVAL; |
400 | } | 403 | } |
401 | 404 | ||
402 | /* FPGA is already configured externally. We're done. */ | 405 | /* FPGA is already configured externally. We're done. */ |
403 | if (info->flags & FPGA_MGR_EXTERNAL_CONFIG) | 406 | if (info->flags & FPGA_MGR_EXTERNAL_CONFIG) { |
407 | fpga_image_info_free(info); | ||
404 | return 0; | 408 | return 0; |
409 | } | ||
405 | 410 | ||
406 | /* If we got this far, we should be programming the FPGA */ | 411 | /* If we got this far, we should be programming the FPGA */ |
407 | if (!firmware_name) { | 412 | if (!info->firmware_name) { |
408 | pr_err("should specify firmware-name or external-fpga-config\n"); | 413 | pr_err("should specify firmware-name or external-fpga-config\n"); |
414 | fpga_image_info_free(info); | ||
409 | return -EINVAL; | 415 | return -EINVAL; |
410 | } | 416 | } |
411 | 417 | ||
412 | return fpga_region_program_fpga(region, firmware_name, nd->overlay); | 418 | region->info = info; |
419 | ret = fpga_region_program_fpga(region, nd->overlay); | ||
420 | if (ret) { | ||
421 | fpga_image_info_free(info); | ||
422 | region->info = NULL; | ||
423 | } | ||
424 | |||
425 | return ret; | ||
413 | } | 426 | } |
414 | 427 | ||
415 | /** | 428 | /** |
@@ -426,7 +439,7 @@ static void fpga_region_notify_post_remove(struct fpga_region *region, | |||
426 | { | 439 | { |
427 | fpga_bridges_disable(®ion->bridge_list); | 440 | fpga_bridges_disable(®ion->bridge_list); |
428 | fpga_bridges_put(®ion->bridge_list); | 441 | fpga_bridges_put(®ion->bridge_list); |
429 | devm_kfree(®ion->dev, region->info); | 442 | fpga_image_info_free(region->info); |
430 | region->info = NULL; | 443 | region->info = NULL; |
431 | } | 444 | } |
432 | 445 | ||