Skip to content

Commit ebf877a

Browse files
atull-alteragregkh
authored andcommitted
fpga: mgr: separate getting/locking FPGA manager
Previously when the user gets a FPGA manager, it was locked and nobody else could use it for programming. This commit makes it straightforward to save a reference to an FPGA manager and only lock it when programming the FPGA. Add functions that get an FPGA manager's mutex for exclusive use: * fpga_mgr_lock * fpga_mgr_unlock The following functions no longer lock an FPGA manager's mutex: * of_fpga_mgr_get * fpga_mgr_get * fpga_mgr_put Signed-off-by: Alan Tull <atull@kernel.org> Acked-by: Moritz Fischer <mdf@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5cf0c7f commit ebf877a

4 files changed

Lines changed: 77 additions & 27 deletions

File tree

‎Documentation/fpga/fpga-mgr.txt‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,20 @@ To get/put a reference to a FPGA manager:
4848
struct fpga_manager *fpga_mgr_get(struct device *dev);
4949
void fpga_mgr_put(struct fpga_manager *mgr);
5050

51-
Given a DT node or device, get an exclusive reference to a FPGA manager.
52-
fpga_mgr_put releases the reference.
51+
Given a DT node or device, get a reference to a FPGA manager. This pointer
52+
can be saved until you are ready to program the FPGA. fpga_mgr_put releases
53+
the reference.
54+
55+
56+
To get exclusive control of a FPGA manager:
57+
-------------------------------------------
58+
59+
int fpga_mgr_lock(struct fpga_manager *mgr);
60+
void fpga_mgr_unlock(struct fpga_manager *mgr);
61+
62+
The user should call fpga_mgr_lock and verify that it returns 0 before
63+
attempting to program the FPGA. Likewise, the user should call
64+
fpga_mgr_unlock when done programming the FPGA.
5365

5466

5567
To register or unregister the low level FPGA-specific driver:
@@ -67,13 +79,21 @@ device."
6779

6880
How to write an image buffer to a supported FPGA
6981
================================================
70-
/* Include to get the API */
7182
#include <linux/fpga/fpga-mgr.h>
7283

7384
struct fpga_manager *mgr;
7485
struct fpga_image_info *info;
7586
int ret;
7687

88+
/*
89+
* Get a reference to FPGA manager. The manager is not locked, so you can
90+
* hold onto this reference without it preventing programming.
91+
*
92+
* This example uses the device node of the manager. Alternatively, use
93+
* fpga_mgr_get(dev) instead if you have the device.
94+
*/
95+
mgr = of_fpga_mgr_get(mgr_node);
96+
7797
/* struct with information about the FPGA image to program. */
7898
info = fpga_image_info_alloc(dev);
7999

@@ -99,17 +119,14 @@ if (image is in a scatter gather table) {
99119

100120
}
101121

102-
/*
103-
* Get a reference to FPGA manager. This example uses the device node of the
104-
* manager. You could use fpga_mgr_get() instead if you have the device instead
105-
* of the device node.
106-
*/
107-
mgr = of_fpga_mgr_get(mgr_node);
122+
/* Get exclusive control of FPGA manager */
123+
ret = fpga_mgr_lock(mgr);
108124

109125
/* Load the buffer to the FPGA */
110126
ret = fpga_mgr_buf_load(mgr, &info, buf, count);
111127

112128
/* Release the FPGA manager */
129+
fpga_mgr_unlock(mgr);
113130
fpga_mgr_put(mgr);
114131

115132
/* Deallocate the image info if you're done with it */

‎drivers/fpga/fpga-mgr.c‎

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -410,28 +410,19 @@ ATTRIBUTE_GROUPS(fpga_mgr);
410410
static struct fpga_manager *__fpga_mgr_get(struct device *dev)
411411
{
412412
struct fpga_manager *mgr;
413-
int ret = -ENODEV;
414413

415414
mgr = to_fpga_manager(dev);
416415
if (!mgr)
417416
goto err_dev;
418417

419-
/* Get exclusive use of fpga manager */
420-
if (!mutex_trylock(&mgr->ref_mutex)) {
421-
ret = -EBUSY;
422-
goto err_dev;
423-
}
424-
425418
if (!try_module_get(dev->parent->driver->owner))
426-
goto err_ll_mod;
419+
goto err_dev;
427420

428421
return mgr;
429422

430-
err_ll_mod:
431-
mutex_unlock(&mgr->ref_mutex);
432423
err_dev:
433424
put_device(dev);
434-
return ERR_PTR(ret);
425+
return ERR_PTR(-ENODEV);
435426
}
436427

437428
static int fpga_mgr_dev_match(struct device *dev, const void *data)
@@ -440,10 +431,10 @@ static int fpga_mgr_dev_match(struct device *dev, const void *data)
440431
}
441432

442433
/**
443-
* fpga_mgr_get - get an exclusive reference to a fpga mgr
434+
* fpga_mgr_get - get a reference to a fpga mgr
444435
* @dev: parent device that fpga mgr was registered with
445436
*
446-
* Given a device, get an exclusive reference to a fpga mgr.
437+
* Given a device, get a reference to a fpga mgr.
447438
*
448439
* Return: fpga manager struct or IS_ERR() condition containing error code.
449440
*/
@@ -464,10 +455,10 @@ static int fpga_mgr_of_node_match(struct device *dev, const void *data)
464455
}
465456

466457
/**
467-
* of_fpga_mgr_get - get an exclusive reference to a fpga mgr
458+
* of_fpga_mgr_get - get a reference to a fpga mgr
468459
* @node: device node
469460
*
470-
* Given a device node, get an exclusive reference to a fpga mgr.
461+
* Given a device node, get a reference to a fpga mgr.
471462
*
472463
* Return: fpga manager struct or IS_ERR() condition containing error code.
473464
*/
@@ -491,11 +482,40 @@ EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
491482
void fpga_mgr_put(struct fpga_manager *mgr)
492483
{
493484
module_put(mgr->dev.parent->driver->owner);
494-
mutex_unlock(&mgr->ref_mutex);
495485
put_device(&mgr->dev);
496486
}
497487
EXPORT_SYMBOL_GPL(fpga_mgr_put);
498488

489+
/**
490+
* fpga_mgr_lock - Lock FPGA manager for exclusive use
491+
* @mgr: fpga manager
492+
*
493+
* Given a pointer to FPGA Manager (from fpga_mgr_get() or
494+
* of_fpga_mgr_put()) attempt to get the mutex.
495+
*
496+
* Return: 0 for success or -EBUSY
497+
*/
498+
int fpga_mgr_lock(struct fpga_manager *mgr)
499+
{
500+
if (!mutex_trylock(&mgr->ref_mutex)) {
501+
dev_err(&mgr->dev, "FPGA manager is in use.\n");
502+
return -EBUSY;
503+
}
504+
505+
return 0;
506+
}
507+
EXPORT_SYMBOL_GPL(fpga_mgr_lock);
508+
509+
/**
510+
* fpga_mgr_unlock - Unlock FPGA manager
511+
* @mgr: fpga manager
512+
*/
513+
void fpga_mgr_unlock(struct fpga_manager *mgr)
514+
{
515+
mutex_unlock(&mgr->ref_mutex);
516+
}
517+
EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
518+
499519
/**
500520
* fpga_mgr_register - register a low level fpga manager driver
501521
* @dev: fpga manager device from pdev

‎drivers/fpga/fpga-region.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static void fpga_region_put(struct fpga_region *region)
125125
}
126126

127127
/**
128-
* fpga_region_get_manager - get exclusive reference for FPGA manager
128+
* fpga_region_get_manager - get reference for FPGA manager
129129
* @region: FPGA region
130130
*
131131
* Get FPGA Manager from "fpga-mgr" property or from ancestor region.
@@ -233,6 +233,7 @@ static int fpga_region_get_bridges(struct fpga_region *region,
233233
static int fpga_region_program_fpga(struct fpga_region *region,
234234
struct device_node *overlay)
235235
{
236+
struct device *dev = &region->dev;
236237
struct fpga_manager *mgr;
237238
int ret;
238239

@@ -249,10 +250,16 @@ static int fpga_region_program_fpga(struct fpga_region *region,
249250
goto err_put_region;
250251
}
251252

253+
ret = fpga_mgr_lock(mgr);
254+
if (ret) {
255+
dev_err(dev, "FPGA manager is busy\n");
256+
goto err_put_mgr;
257+
}
258+
252259
ret = fpga_region_get_bridges(region, overlay);
253260
if (ret) {
254261
pr_err("failed to get fpga region bridges\n");
255-
goto err_put_mgr;
262+
goto err_unlock_mgr;
256263
}
257264

258265
ret = fpga_bridges_disable(&region->bridge_list);
@@ -273,13 +280,16 @@ static int fpga_region_program_fpga(struct fpga_region *region,
273280
goto err_put_br;
274281
}
275282

283+
fpga_mgr_unlock(mgr);
276284
fpga_mgr_put(mgr);
277285
fpga_region_put(region);
278286

279287
return 0;
280288

281289
err_put_br:
282290
fpga_bridges_put(&region->bridge_list);
291+
err_unlock_mgr:
292+
fpga_mgr_unlock(mgr);
283293
err_put_mgr:
284294
fpga_mgr_put(mgr);
285295
err_put_region:

‎include/linux/fpga/fpga-mgr.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ void fpga_image_info_free(struct fpga_image_info *info);
155155

156156
int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info);
157157

158+
int fpga_mgr_lock(struct fpga_manager *mgr);
159+
void fpga_mgr_unlock(struct fpga_manager *mgr);
160+
158161
struct fpga_manager *of_fpga_mgr_get(struct device_node *node);
159162

160163
struct fpga_manager *fpga_mgr_get(struct device *dev);

0 commit comments

Comments
 (0)