1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for FPGA Device Feature List (DFL) Support
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
8 * Kang Luwei <luwei.kang@intel.com>
9 * Zhang Yi <yi.z.zhang@intel.com>
10 * Wu Hao <hao.wu@intel.com>
11 * Xiao Guangrong <guangrong.xiao@linux.intel.com>
13 #include <linux/module.h>
17 static DEFINE_MUTEX(dfl_id_mutex
);
20 * when adding a new feature dev support in DFL framework, it's required to
21 * add a new item in enum dfl_id_type and provide related information in below
22 * dfl_devs table which is indexed by dfl_id_type, e.g. name string used for
23 * platform device creation (define name strings in dfl.h, as they could be
24 * reused by platform device drivers).
26 * if the new feature dev needs chardev support, then it's required to add
27 * a new item in dfl_chardevs table and configure dfl_devs[i].devt_type as
28 * index to dfl_chardevs table. If no chardev support just set devt_type
29 * as one invalid index (DFL_FPGA_DEVT_MAX).
32 FME_ID
, /* fme id allocation and mapping */
33 PORT_ID
, /* port id allocation and mapping */
37 enum dfl_fpga_devt_type
{
44 * dfl_dev_info - dfl feature device information.
45 * @name: name string of the feature platform device.
46 * @dfh_id: id value in Device Feature Header (DFH) register by DFL spec.
47 * @id: idr id of the feature dev.
48 * @devt_type: index to dfl_chrdevs[].
54 enum dfl_fpga_devt_type devt_type
;
57 /* it is indexed by dfl_id_type */
58 static struct dfl_dev_info dfl_devs
[] = {
59 {.name
= DFL_FPGA_FEATURE_DEV_FME
, .dfh_id
= DFH_ID_FIU_FME
,
60 .devt_type
= DFL_FPGA_DEVT_FME
},
61 {.name
= DFL_FPGA_FEATURE_DEV_PORT
, .dfh_id
= DFH_ID_FIU_PORT
,
62 .devt_type
= DFL_FPGA_DEVT_PORT
},
66 * dfl_chardev_info - chardev information of dfl feature device
67 * @name: nmae string of the char device.
68 * @devt: devt of the char device.
70 struct dfl_chardev_info
{
75 /* indexed by enum dfl_fpga_devt_type */
76 static struct dfl_chardev_info dfl_chrdevs
[] = {
77 {.name
= DFL_FPGA_FEATURE_DEV_FME
},
78 {.name
= DFL_FPGA_FEATURE_DEV_PORT
},
81 static void dfl_ids_init(void)
85 for (i
= 0; i
< ARRAY_SIZE(dfl_devs
); i
++)
86 idr_init(&dfl_devs
[i
].id
);
89 static void dfl_ids_destroy(void)
93 for (i
= 0; i
< ARRAY_SIZE(dfl_devs
); i
++)
94 idr_destroy(&dfl_devs
[i
].id
);
97 static int dfl_id_alloc(enum dfl_id_type type
, struct device
*dev
)
101 WARN_ON(type
>= DFL_ID_MAX
);
102 mutex_lock(&dfl_id_mutex
);
103 id
= idr_alloc(&dfl_devs
[type
].id
, dev
, 0, 0, GFP_KERNEL
);
104 mutex_unlock(&dfl_id_mutex
);
109 static void dfl_id_free(enum dfl_id_type type
, int id
)
111 WARN_ON(type
>= DFL_ID_MAX
);
112 mutex_lock(&dfl_id_mutex
);
113 idr_remove(&dfl_devs
[type
].id
, id
);
114 mutex_unlock(&dfl_id_mutex
);
117 static enum dfl_id_type
feature_dev_id_type(struct platform_device
*pdev
)
121 for (i
= 0; i
< ARRAY_SIZE(dfl_devs
); i
++)
122 if (!strcmp(dfl_devs
[i
].name
, pdev
->name
))
128 static enum dfl_id_type
dfh_id_to_type(u32 id
)
132 for (i
= 0; i
< ARRAY_SIZE(dfl_devs
); i
++)
133 if (dfl_devs
[i
].dfh_id
== id
)
140 * introduce a global port_ops list, it allows port drivers to register ops
141 * in such list, then other feature devices (e.g. FME), could use the port
142 * functions even related port platform device is hidden. Below is one example,
143 * in virtualization case of PCIe-based FPGA DFL device, when SRIOV is
144 * enabled, port (and it's AFU) is turned into VF and port platform device
145 * is hidden from system but it's still required to access port to finish FPGA
146 * reconfiguration function in FME.
149 static DEFINE_MUTEX(dfl_port_ops_mutex
);
150 static LIST_HEAD(dfl_port_ops_list
);
153 * dfl_fpga_port_ops_get - get matched port ops from the global list
154 * @pdev: platform device to match with associated port ops.
155 * Return: matched port ops on success, NULL otherwise.
157 * Please note that must dfl_fpga_port_ops_put after use the port_ops.
159 struct dfl_fpga_port_ops
*dfl_fpga_port_ops_get(struct platform_device
*pdev
)
161 struct dfl_fpga_port_ops
*ops
= NULL
;
163 mutex_lock(&dfl_port_ops_mutex
);
164 if (list_empty(&dfl_port_ops_list
))
167 list_for_each_entry(ops
, &dfl_port_ops_list
, node
) {
168 /* match port_ops using the name of platform device */
169 if (!strcmp(pdev
->name
, ops
->name
)) {
170 if (!try_module_get(ops
->owner
))
178 mutex_unlock(&dfl_port_ops_mutex
);
181 EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_get
);
184 * dfl_fpga_port_ops_put - put port ops
187 void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops
*ops
)
189 if (ops
&& ops
->owner
)
190 module_put(ops
->owner
);
192 EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_put
);
195 * dfl_fpga_port_ops_add - add port_ops to global list
196 * @ops: port ops to add.
198 void dfl_fpga_port_ops_add(struct dfl_fpga_port_ops
*ops
)
200 mutex_lock(&dfl_port_ops_mutex
);
201 list_add_tail(&ops
->node
, &dfl_port_ops_list
);
202 mutex_unlock(&dfl_port_ops_mutex
);
204 EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_add
);
207 * dfl_fpga_port_ops_del - remove port_ops from global list
208 * @ops: port ops to del.
210 void dfl_fpga_port_ops_del(struct dfl_fpga_port_ops
*ops
)
212 mutex_lock(&dfl_port_ops_mutex
);
213 list_del(&ops
->node
);
214 mutex_unlock(&dfl_port_ops_mutex
);
216 EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_del
);
219 * dfl_fpga_check_port_id - check the port id
220 * @pdev: port platform device.
221 * @pport_id: port id to compare.
223 * Return: 1 if port device matches with given port id, otherwise 0.
225 int dfl_fpga_check_port_id(struct platform_device
*pdev
, void *pport_id
)
227 struct dfl_fpga_port_ops
*port_ops
= dfl_fpga_port_ops_get(pdev
);
230 if (!port_ops
|| !port_ops
->get_id
)
233 port_id
= port_ops
->get_id(pdev
);
234 dfl_fpga_port_ops_put(port_ops
);
236 return port_id
== *(int *)pport_id
;
238 EXPORT_SYMBOL_GPL(dfl_fpga_check_port_id
);
241 * dfl_fpga_dev_feature_uinit - uinit for sub features of dfl feature device
242 * @pdev: feature device.
244 void dfl_fpga_dev_feature_uinit(struct platform_device
*pdev
)
246 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
247 struct dfl_feature
*feature
;
249 dfl_fpga_dev_for_each_feature(pdata
, feature
)
251 feature
->ops
->uinit(pdev
, feature
);
255 EXPORT_SYMBOL_GPL(dfl_fpga_dev_feature_uinit
);
257 static int dfl_feature_instance_init(struct platform_device
*pdev
,
258 struct dfl_feature_platform_data
*pdata
,
259 struct dfl_feature
*feature
,
260 struct dfl_feature_driver
*drv
)
264 ret
= drv
->ops
->init(pdev
, feature
);
268 feature
->ops
= drv
->ops
;
274 * dfl_fpga_dev_feature_init - init for sub features of dfl feature device
275 * @pdev: feature device.
276 * @feature_drvs: drvs for sub features.
278 * This function will match sub features with given feature drvs list and
279 * use matched drv to init related sub feature.
281 * Return: 0 on success, negative error code otherwise.
283 int dfl_fpga_dev_feature_init(struct platform_device
*pdev
,
284 struct dfl_feature_driver
*feature_drvs
)
286 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
287 struct dfl_feature_driver
*drv
= feature_drvs
;
288 struct dfl_feature
*feature
;
292 dfl_fpga_dev_for_each_feature(pdata
, feature
) {
293 /* match feature and drv using id */
294 if (feature
->id
== drv
->id
) {
295 ret
= dfl_feature_instance_init(pdev
, pdata
,
306 dfl_fpga_dev_feature_uinit(pdev
);
309 EXPORT_SYMBOL_GPL(dfl_fpga_dev_feature_init
);
311 static void dfl_chardev_uinit(void)
315 for (i
= 0; i
< DFL_FPGA_DEVT_MAX
; i
++)
316 if (MAJOR(dfl_chrdevs
[i
].devt
)) {
317 unregister_chrdev_region(dfl_chrdevs
[i
].devt
,
319 dfl_chrdevs
[i
].devt
= MKDEV(0, 0);
323 static int dfl_chardev_init(void)
327 for (i
= 0; i
< DFL_FPGA_DEVT_MAX
; i
++) {
328 ret
= alloc_chrdev_region(&dfl_chrdevs
[i
].devt
, 0, MINORMASK
,
329 dfl_chrdevs
[i
].name
);
341 static dev_t
dfl_get_devt(enum dfl_fpga_devt_type type
, int id
)
343 if (type
>= DFL_FPGA_DEVT_MAX
)
346 return MKDEV(MAJOR(dfl_chrdevs
[type
].devt
), id
);
350 * dfl_fpga_dev_ops_register - register cdev ops for feature dev
352 * @pdev: feature dev.
353 * @fops: file operations for feature dev's cdev.
354 * @owner: owning module/driver.
356 * Return: 0 on success, negative error code otherwise.
358 int dfl_fpga_dev_ops_register(struct platform_device
*pdev
,
359 const struct file_operations
*fops
,
360 struct module
*owner
)
362 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
364 cdev_init(&pdata
->cdev
, fops
);
365 pdata
->cdev
.owner
= owner
;
368 * set parent to the feature device so that its refcount is
369 * decreased after the last refcount of cdev is gone, that
370 * makes sure the feature device is valid during device
373 pdata
->cdev
.kobj
.parent
= &pdev
->dev
.kobj
;
375 return cdev_add(&pdata
->cdev
, pdev
->dev
.devt
, 1);
377 EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_register
);
380 * dfl_fpga_dev_ops_unregister - unregister cdev ops for feature dev
381 * @pdev: feature dev.
383 void dfl_fpga_dev_ops_unregister(struct platform_device
*pdev
)
385 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
387 cdev_del(&pdata
->cdev
);
389 EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_unregister
);
392 * struct build_feature_devs_info - info collected during feature dev build.
394 * @dev: device to enumerate.
395 * @cdev: the container device for all feature devices.
396 * @feature_dev: current feature device.
397 * @ioaddr: header register region address of feature device in enumeration.
398 * @sub_features: a sub features linked list for feature device in enumeration.
399 * @feature_num: number of sub features for feature device in enumeration.
401 struct build_feature_devs_info
{
403 struct dfl_fpga_cdev
*cdev
;
404 struct platform_device
*feature_dev
;
405 void __iomem
*ioaddr
;
406 struct list_head sub_features
;
411 * struct dfl_feature_info - sub feature info collected during feature dev build
413 * @fid: id of this sub feature.
414 * @mmio_res: mmio resource of this sub feature.
415 * @ioaddr: mapped base address of mmio resource.
416 * @node: node in sub_features linked list.
418 struct dfl_feature_info
{
420 struct resource mmio_res
;
421 void __iomem
*ioaddr
;
422 struct list_head node
;
425 static void dfl_fpga_cdev_add_port_dev(struct dfl_fpga_cdev
*cdev
,
426 struct platform_device
*port
)
428 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&port
->dev
);
430 mutex_lock(&cdev
->lock
);
431 list_add(&pdata
->node
, &cdev
->port_dev_list
);
432 get_device(&pdata
->dev
->dev
);
433 mutex_unlock(&cdev
->lock
);
437 * register current feature device, it is called when we need to switch to
438 * another feature parsing or we have parsed all features on given device
441 static int build_info_commit_dev(struct build_feature_devs_info
*binfo
)
443 struct platform_device
*fdev
= binfo
->feature_dev
;
444 struct dfl_feature_platform_data
*pdata
;
445 struct dfl_feature_info
*finfo
, *p
;
452 * we do not need to care for the memory which is associated with
453 * the platform device. After calling platform_device_unregister(),
454 * it will be automatically freed by device's release() callback,
455 * platform_device_release().
457 pdata
= kzalloc(dfl_feature_platform_data_size(binfo
->feature_num
),
463 pdata
->num
= binfo
->feature_num
;
464 pdata
->dfl_cdev
= binfo
->cdev
;
465 mutex_init(&pdata
->lock
);
468 * the count should be initialized to 0 to make sure
469 *__fpga_port_enable() following __fpga_port_disable()
470 * works properly for port device.
471 * and it should always be 0 for fme device.
473 WARN_ON(pdata
->disable_count
);
475 fdev
->dev
.platform_data
= pdata
;
477 /* each sub feature has one MMIO resource */
478 fdev
->num_resources
= binfo
->feature_num
;
479 fdev
->resource
= kcalloc(binfo
->feature_num
, sizeof(*fdev
->resource
),
484 /* fill features and resource information for feature dev */
485 list_for_each_entry_safe(finfo
, p
, &binfo
->sub_features
, node
) {
486 struct dfl_feature
*feature
= &pdata
->features
[index
];
488 /* save resource information for each feature */
489 feature
->id
= finfo
->fid
;
490 feature
->resource_index
= index
;
491 feature
->ioaddr
= finfo
->ioaddr
;
492 fdev
->resource
[index
++] = finfo
->mmio_res
;
494 list_del(&finfo
->node
);
498 ret
= platform_device_add(binfo
->feature_dev
);
500 if (feature_dev_id_type(binfo
->feature_dev
) == PORT_ID
)
501 dfl_fpga_cdev_add_port_dev(binfo
->cdev
,
504 binfo
->cdev
->fme_dev
=
505 get_device(&binfo
->feature_dev
->dev
);
507 * reset it to avoid build_info_free() freeing their resource.
509 * The resource of successfully registered feature devices
510 * will be freed by platform_device_unregister(). See the
511 * comments in build_info_create_dev().
513 binfo
->feature_dev
= NULL
;
520 build_info_create_dev(struct build_feature_devs_info
*binfo
,
521 enum dfl_id_type type
, void __iomem
*ioaddr
)
523 struct platform_device
*fdev
;
526 if (type
>= DFL_ID_MAX
)
529 /* we will create a new device, commit current device first */
530 ret
= build_info_commit_dev(binfo
);
535 * we use -ENODEV as the initialization indicator which indicates
536 * whether the id need to be reclaimed
538 fdev
= platform_device_alloc(dfl_devs
[type
].name
, -ENODEV
);
542 binfo
->feature_dev
= fdev
;
543 binfo
->feature_num
= 0;
544 binfo
->ioaddr
= ioaddr
;
545 INIT_LIST_HEAD(&binfo
->sub_features
);
547 fdev
->id
= dfl_id_alloc(type
, &fdev
->dev
);
551 fdev
->dev
.parent
= &binfo
->cdev
->region
->dev
;
552 fdev
->dev
.devt
= dfl_get_devt(dfl_devs
[type
].devt_type
, fdev
->id
);
557 static void build_info_free(struct build_feature_devs_info
*binfo
)
559 struct dfl_feature_info
*finfo
, *p
;
562 * it is a valid id, free it. See comments in
563 * build_info_create_dev()
565 if (binfo
->feature_dev
&& binfo
->feature_dev
->id
>= 0) {
566 dfl_id_free(feature_dev_id_type(binfo
->feature_dev
),
567 binfo
->feature_dev
->id
);
569 list_for_each_entry_safe(finfo
, p
, &binfo
->sub_features
, node
) {
570 list_del(&finfo
->node
);
575 platform_device_put(binfo
->feature_dev
);
577 devm_kfree(binfo
->dev
, binfo
);
580 static inline u32
feature_size(void __iomem
*start
)
582 u64 v
= readq(start
+ DFH
);
583 u32 ofst
= FIELD_GET(DFH_NEXT_HDR_OFST
, v
);
584 /* workaround for private features with invalid size, use 4K instead */
585 return ofst
? ofst
: 4096;
588 static u64
feature_id(void __iomem
*start
)
590 u64 v
= readq(start
+ DFH
);
591 u16 id
= FIELD_GET(DFH_ID
, v
);
592 u8 type
= FIELD_GET(DFH_TYPE
, v
);
594 if (type
== DFH_TYPE_FIU
)
595 return FEATURE_ID_FIU_HEADER
;
596 else if (type
== DFH_TYPE_PRIVATE
)
598 else if (type
== DFH_TYPE_AFU
)
599 return FEATURE_ID_AFU
;
606 * when create sub feature instances, for private features, it doesn't need
607 * to provide resource size and feature id as they could be read from DFH
608 * register. For afu sub feature, its register region only contains user
609 * defined registers, so never trust any information from it, just use the
610 * resource size information provided by its parent FIU.
613 create_feature_instance(struct build_feature_devs_info
*binfo
,
614 struct dfl_fpga_enum_dfl
*dfl
, resource_size_t ofst
,
615 resource_size_t size
, u64 fid
)
617 struct dfl_feature_info
*finfo
;
619 /* read feature size and id if inputs are invalid */
620 size
= size
? size
: feature_size(dfl
->ioaddr
+ ofst
);
621 fid
= fid
? fid
: feature_id(dfl
->ioaddr
+ ofst
);
623 if (dfl
->len
- ofst
< size
)
626 finfo
= kzalloc(sizeof(*finfo
), GFP_KERNEL
);
631 finfo
->mmio_res
.start
= dfl
->start
+ ofst
;
632 finfo
->mmio_res
.end
= finfo
->mmio_res
.start
+ size
- 1;
633 finfo
->mmio_res
.flags
= IORESOURCE_MEM
;
634 finfo
->ioaddr
= dfl
->ioaddr
+ ofst
;
636 list_add_tail(&finfo
->node
, &binfo
->sub_features
);
637 binfo
->feature_num
++;
642 static int parse_feature_port_afu(struct build_feature_devs_info
*binfo
,
643 struct dfl_fpga_enum_dfl
*dfl
,
644 resource_size_t ofst
)
646 u64 v
= readq(binfo
->ioaddr
+ PORT_HDR_CAP
);
647 u32 size
= FIELD_GET(PORT_CAP_MMIO_SIZE
, v
) << 10;
651 return create_feature_instance(binfo
, dfl
, ofst
, size
, FEATURE_ID_AFU
);
654 static int parse_feature_afu(struct build_feature_devs_info
*binfo
,
655 struct dfl_fpga_enum_dfl
*dfl
,
656 resource_size_t ofst
)
658 if (!binfo
->feature_dev
) {
659 dev_err(binfo
->dev
, "this AFU does not belong to any FIU.\n");
663 switch (feature_dev_id_type(binfo
->feature_dev
)) {
665 return parse_feature_port_afu(binfo
, dfl
, ofst
);
667 dev_info(binfo
->dev
, "AFU belonging to FIU %s is not supported yet.\n",
668 binfo
->feature_dev
->name
);
674 static int parse_feature_fiu(struct build_feature_devs_info
*binfo
,
675 struct dfl_fpga_enum_dfl
*dfl
,
676 resource_size_t ofst
)
682 v
= readq(dfl
->ioaddr
+ ofst
+ DFH
);
683 id
= FIELD_GET(DFH_ID
, v
);
685 /* create platform device for dfl feature dev */
686 ret
= build_info_create_dev(binfo
, dfh_id_to_type(id
),
691 ret
= create_feature_instance(binfo
, dfl
, ofst
, 0, 0);
695 * find and parse FIU's child AFU via its NEXT_AFU register.
696 * please note that only Port has valid NEXT_AFU pointer per spec.
698 v
= readq(dfl
->ioaddr
+ ofst
+ NEXT_AFU
);
700 offset
= FIELD_GET(NEXT_AFU_NEXT_DFH_OFST
, v
);
702 return parse_feature_afu(binfo
, dfl
, ofst
+ offset
);
704 dev_dbg(binfo
->dev
, "No AFUs detected on FIU %d\n", id
);
709 static int parse_feature_private(struct build_feature_devs_info
*binfo
,
710 struct dfl_fpga_enum_dfl
*dfl
,
711 resource_size_t ofst
)
713 if (!binfo
->feature_dev
) {
714 dev_err(binfo
->dev
, "the private feature %llx does not belong to any AFU.\n",
715 (unsigned long long)feature_id(dfl
->ioaddr
+ ofst
));
719 return create_feature_instance(binfo
, dfl
, ofst
, 0, 0);
723 * parse_feature - parse a feature on given device feature list
725 * @binfo: build feature devices information.
726 * @dfl: device feature list to parse
727 * @ofst: offset to feature header on this device feature list
729 static int parse_feature(struct build_feature_devs_info
*binfo
,
730 struct dfl_fpga_enum_dfl
*dfl
, resource_size_t ofst
)
735 v
= readq(dfl
->ioaddr
+ ofst
+ DFH
);
736 type
= FIELD_GET(DFH_TYPE
, v
);
740 return parse_feature_afu(binfo
, dfl
, ofst
);
741 case DFH_TYPE_PRIVATE
:
742 return parse_feature_private(binfo
, dfl
, ofst
);
744 return parse_feature_fiu(binfo
, dfl
, ofst
);
747 "Feature Type %x is not supported.\n", type
);
753 static int parse_feature_list(struct build_feature_devs_info
*binfo
,
754 struct dfl_fpga_enum_dfl
*dfl
)
756 void __iomem
*start
= dfl
->ioaddr
;
757 void __iomem
*end
= dfl
->ioaddr
+ dfl
->len
;
762 /* walk through the device feature list via DFH's next DFH pointer. */
763 for (; start
< end
; start
+= ofst
) {
764 if (end
- start
< DFH_SIZE
) {
765 dev_err(binfo
->dev
, "The region is too small to contain a feature.\n");
769 ret
= parse_feature(binfo
, dfl
, start
- dfl
->ioaddr
);
773 v
= readq(start
+ DFH
);
774 ofst
= FIELD_GET(DFH_NEXT_HDR_OFST
, v
);
776 /* stop parsing if EOL(End of List) is set or offset is 0 */
777 if ((v
& DFH_EOL
) || !ofst
)
781 /* commit current feature device when reach the end of list */
782 return build_info_commit_dev(binfo
);
785 struct dfl_fpga_enum_info
*dfl_fpga_enum_info_alloc(struct device
*dev
)
787 struct dfl_fpga_enum_info
*info
;
791 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
798 INIT_LIST_HEAD(&info
->dfls
);
802 EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_alloc
);
804 void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info
*info
)
806 struct dfl_fpga_enum_dfl
*tmp
, *dfl
;
814 /* remove all device feature lists in the list. */
815 list_for_each_entry_safe(dfl
, tmp
, &info
->dfls
, node
) {
816 list_del(&dfl
->node
);
817 devm_kfree(dev
, dfl
);
820 devm_kfree(dev
, info
);
823 EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_free
);
826 * dfl_fpga_enum_info_add_dfl - add info of a device feature list to enum info
828 * @info: ptr to dfl_fpga_enum_info
829 * @start: mmio resource address of the device feature list.
830 * @len: mmio resource length of the device feature list.
831 * @ioaddr: mapped mmio resource address of the device feature list.
833 * One FPGA device may have one or more Device Feature Lists (DFLs), use this
834 * function to add information of each DFL to common data structure for next
837 * Return: 0 on success, negative error code otherwise.
839 int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info
*info
,
840 resource_size_t start
, resource_size_t len
,
841 void __iomem
*ioaddr
)
843 struct dfl_fpga_enum_dfl
*dfl
;
845 dfl
= devm_kzalloc(info
->dev
, sizeof(*dfl
), GFP_KERNEL
);
851 dfl
->ioaddr
= ioaddr
;
853 list_add_tail(&dfl
->node
, &info
->dfls
);
857 EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_add_dfl
);
859 static int remove_feature_dev(struct device
*dev
, void *data
)
861 struct platform_device
*pdev
= to_platform_device(dev
);
862 enum dfl_id_type type
= feature_dev_id_type(pdev
);
865 platform_device_unregister(pdev
);
867 dfl_id_free(type
, id
);
872 static void remove_feature_devs(struct dfl_fpga_cdev
*cdev
)
874 device_for_each_child(&cdev
->region
->dev
, NULL
, remove_feature_dev
);
878 * dfl_fpga_feature_devs_enumerate - enumerate feature devices
879 * @info: information for enumeration.
881 * This function creates a container device (base FPGA region), enumerates
882 * feature devices based on the enumeration info and creates platform devices
883 * under the container device.
885 * Return: dfl_fpga_cdev struct on success, -errno on failure
887 struct dfl_fpga_cdev
*
888 dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info
*info
)
890 struct build_feature_devs_info
*binfo
;
891 struct dfl_fpga_enum_dfl
*dfl
;
892 struct dfl_fpga_cdev
*cdev
;
896 return ERR_PTR(-ENODEV
);
898 cdev
= devm_kzalloc(info
->dev
, sizeof(*cdev
), GFP_KERNEL
);
900 return ERR_PTR(-ENOMEM
);
902 cdev
->region
= fpga_region_create(info
->dev
, NULL
, NULL
);
908 cdev
->parent
= info
->dev
;
909 mutex_init(&cdev
->lock
);
910 INIT_LIST_HEAD(&cdev
->port_dev_list
);
912 ret
= fpga_region_register(cdev
->region
);
914 goto free_region_exit
;
916 /* create and init build info for enumeration */
917 binfo
= devm_kzalloc(info
->dev
, sizeof(*binfo
), GFP_KERNEL
);
920 goto unregister_region_exit
;
923 binfo
->dev
= info
->dev
;
927 * start enumeration for all feature devices based on Device Feature
930 list_for_each_entry(dfl
, &info
->dfls
, node
) {
931 ret
= parse_feature_list(binfo
, dfl
);
933 remove_feature_devs(cdev
);
934 build_info_free(binfo
);
935 goto unregister_region_exit
;
939 build_info_free(binfo
);
943 unregister_region_exit
:
944 fpga_region_unregister(cdev
->region
);
946 fpga_region_free(cdev
->region
);
948 devm_kfree(info
->dev
, cdev
);
951 EXPORT_SYMBOL_GPL(dfl_fpga_feature_devs_enumerate
);
954 * dfl_fpga_feature_devs_remove - remove all feature devices
955 * @cdev: fpga container device.
957 * Remove the container device and all feature devices under given container
960 void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev
*cdev
)
962 struct dfl_feature_platform_data
*pdata
, *ptmp
;
964 remove_feature_devs(cdev
);
966 mutex_lock(&cdev
->lock
);
968 /* the fme should be unregistered. */
969 WARN_ON(device_is_registered(cdev
->fme_dev
));
970 put_device(cdev
->fme_dev
);
973 list_for_each_entry_safe(pdata
, ptmp
, &cdev
->port_dev_list
, node
) {
974 struct platform_device
*port_dev
= pdata
->dev
;
976 /* the port should be unregistered. */
977 WARN_ON(device_is_registered(&port_dev
->dev
));
978 list_del(&pdata
->node
);
979 put_device(&port_dev
->dev
);
981 mutex_unlock(&cdev
->lock
);
983 fpga_region_unregister(cdev
->region
);
984 devm_kfree(cdev
->parent
, cdev
);
986 EXPORT_SYMBOL_GPL(dfl_fpga_feature_devs_remove
);
989 * __dfl_fpga_cdev_find_port - find a port under given container device
991 * @cdev: container device
992 * @data: data passed to match function
993 * @match: match function used to find specific port from the port device list
995 * Find a port device under container device. This function needs to be
996 * invoked with lock held.
998 * Return: pointer to port's platform device if successful, NULL otherwise.
1000 * NOTE: you will need to drop the device reference with put_device() after use.
1002 struct platform_device
*
1003 __dfl_fpga_cdev_find_port(struct dfl_fpga_cdev
*cdev
, void *data
,
1004 int (*match
)(struct platform_device
*, void *))
1006 struct dfl_feature_platform_data
*pdata
;
1007 struct platform_device
*port_dev
;
1009 list_for_each_entry(pdata
, &cdev
->port_dev_list
, node
) {
1010 port_dev
= pdata
->dev
;
1012 if (match(port_dev
, data
) && get_device(&port_dev
->dev
))
1018 EXPORT_SYMBOL_GPL(__dfl_fpga_cdev_find_port
);
1020 static int __init
dfl_fpga_init(void)
1026 ret
= dfl_chardev_init();
1033 static void __exit
dfl_fpga_exit(void)
1035 dfl_chardev_uinit();
1039 module_init(dfl_fpga_init
);
1040 module_exit(dfl_fpga_exit
);
1042 MODULE_DESCRIPTION("FPGA Device Feature List (DFL) Support");
1043 MODULE_AUTHOR("Intel Corporation");
1044 MODULE_LICENSE("GPL v2");