aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/fpga
diff options
context:
space:
mode:
authorWu Hao <hao.wu@intel.com>2018-06-29 20:53:13 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2018-07-15 07:55:45 -0400
commit543be3d8c999b30e1e1c05d30c1ea3f2d922340b (patch)
treeae6401cfee61ca718ecbd2021f8d4e1359019659 /drivers/fpga
parent41a8b2c56470b7e4e3e2db93324d50bbbf60cdc4 (diff)
fpga: add device feature list support
Device Feature List (DFL) defines a feature list structure that creates a linked list of feature headers within the MMIO space to provide an extensible way of adding features. This patch introduces a kernel module to provide basic infrastructure to support FPGA devices which implement the Device Feature List. Usually there will be different features and their sub features linked into the DFL. This code provides common APIs for feature enumeration, it creates a container device (FPGA base region), walks through the DFLs and creates platform devices for feature devices (Currently it only supports two different feature devices, FPGA Management Engine (FME) and Port which the Accelerator Function Unit (AFU) connected to). In order to enumerate the DFLs, the common APIs required low level driver to provide necessary enumeration information (e.g. address for each device feature list for given device) and fill it to the dfl_fpga_enum_info data structure. Please refer to below description for APIs added for enumeration. Functions for enumeration information preparation: *dfl_fpga_enum_info_alloc allocate enumeration information data structure. *dfl_fpga_enum_info_add_dfl add a device feature list to dfl_fpga_enum_info data structure. *dfl_fpga_enum_info_free free dfl_fpga_enum_info data structure and related resources. Functions for feature device enumeration: *dfl_fpga_feature_devs_enumerate enumerate feature devices and return container device. *dfl_fpga_feature_devs_remove remove feature devices under given container device. Signed-off-by: Tim Whisonant <tim.whisonant@intel.com> Signed-off-by: Enno Luebbers <enno.luebbers@intel.com> Signed-off-by: Shiva Rao <shiva.rao@intel.com> Signed-off-by: Christopher Rauer <christopher.rauer@intel.com> Signed-off-by: Zhang Yi <yi.z.zhang@intel.com> Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com> Signed-off-by: Wu Hao <hao.wu@intel.com> Acked-by: Alan Tull <atull@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/fpga')
-rw-r--r--drivers/fpga/Kconfig16
-rw-r--r--drivers/fpga/Makefile3
-rw-r--r--drivers/fpga/dfl.c721
-rw-r--r--drivers/fpga/dfl.h279
4 files changed, 1019 insertions, 0 deletions
diff --git a/drivers/fpga/Kconfig b/drivers/fpga/Kconfig
index ee9c5420c47f..4880fd676a19 100644
--- a/drivers/fpga/Kconfig
+++ b/drivers/fpga/Kconfig
@@ -130,4 +130,20 @@ config OF_FPGA_REGION
130 Support for loading FPGA images by applying a Device Tree 130 Support for loading FPGA images by applying a Device Tree
131 overlay. 131 overlay.
132 132
133config FPGA_DFL
134 tristate "FPGA Device Feature List (DFL) support"
135 select FPGA_BRIDGE
136 select FPGA_REGION
137 help
138 Device Feature List (DFL) defines a feature list structure that
139 creates a linked list of feature headers within the MMIO space
140 to provide an extensible way of adding features for FPGA.
141 Driver can walk through the feature headers to enumerate feature
142 devices (e.g. FPGA Management Engine, Port and Accelerator
143 Function Unit) and their private features for target FPGA devices.
144
145 Select this option to enable common support for Field-Programmable
146 Gate Array (FPGA) solutions which implement Device Feature List.
147 It provides enumeration APIs and feature device infrastructure.
148
133endif # FPGA 149endif # FPGA
diff --git a/drivers/fpga/Makefile b/drivers/fpga/Makefile
index f9803dad6919..7a7a11739e00 100644
--- a/drivers/fpga/Makefile
+++ b/drivers/fpga/Makefile
@@ -28,3 +28,6 @@ obj-$(CONFIG_XILINX_PR_DECOUPLER) += xilinx-pr-decoupler.o
28# High Level Interfaces 28# High Level Interfaces
29obj-$(CONFIG_FPGA_REGION) += fpga-region.o 29obj-$(CONFIG_FPGA_REGION) += fpga-region.o
30obj-$(CONFIG_OF_FPGA_REGION) += of-fpga-region.o 30obj-$(CONFIG_OF_FPGA_REGION) += of-fpga-region.o
31
32# FPGA Device Feature List Support
33obj-$(CONFIG_FPGA_DFL) += dfl.o
diff --git a/drivers/fpga/dfl.c b/drivers/fpga/dfl.c
new file mode 100644
index 000000000000..d1dff360ea14
--- /dev/null
+++ b/drivers/fpga/dfl.c
@@ -0,0 +1,721 @@
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Driver for FPGA Device Feature List (DFL) Support
4 *
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
6 *
7 * Authors:
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>
12 */
13#include <linux/module.h>
14
15#include "dfl.h"
16
17static DEFINE_MUTEX(dfl_id_mutex);
18
19/*
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).
25 */
26enum dfl_id_type {
27 FME_ID, /* fme id allocation and mapping */
28 PORT_ID, /* port id allocation and mapping */
29 DFL_ID_MAX,
30};
31
32/**
33 * dfl_dev_info - dfl feature device information.
34 * @name: name string of the feature platform device.
35 * @dfh_id: id value in Device Feature Header (DFH) register by DFL spec.
36 * @id: idr id of the feature dev.
37 */
38struct dfl_dev_info {
39 const char *name;
40 u32 dfh_id;
41 struct idr id;
42};
43
44/* it is indexed by dfl_id_type */
45static struct dfl_dev_info dfl_devs[] = {
46 {.name = DFL_FPGA_FEATURE_DEV_FME, .dfh_id = DFH_ID_FIU_FME},
47 {.name = DFL_FPGA_FEATURE_DEV_PORT, .dfh_id = DFH_ID_FIU_PORT},
48};
49
50static void dfl_ids_init(void)
51{
52 int i;
53
54 for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
55 idr_init(&dfl_devs[i].id);
56}
57
58static void dfl_ids_destroy(void)
59{
60 int i;
61
62 for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
63 idr_destroy(&dfl_devs[i].id);
64}
65
66static int dfl_id_alloc(enum dfl_id_type type, struct device *dev)
67{
68 int id;
69
70 WARN_ON(type >= DFL_ID_MAX);
71 mutex_lock(&dfl_id_mutex);
72 id = idr_alloc(&dfl_devs[type].id, dev, 0, 0, GFP_KERNEL);
73 mutex_unlock(&dfl_id_mutex);
74
75 return id;
76}
77
78static void dfl_id_free(enum dfl_id_type type, int id)
79{
80 WARN_ON(type >= DFL_ID_MAX);
81 mutex_lock(&dfl_id_mutex);
82 idr_remove(&dfl_devs[type].id, id);
83 mutex_unlock(&dfl_id_mutex);
84}
85
86static enum dfl_id_type feature_dev_id_type(struct platform_device *pdev)
87{
88 int i;
89
90 for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
91 if (!strcmp(dfl_devs[i].name, pdev->name))
92 return i;
93
94 return DFL_ID_MAX;
95}
96
97static enum dfl_id_type dfh_id_to_type(u32 id)
98{
99 int i;
100
101 for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
102 if (dfl_devs[i].dfh_id == id)
103 return i;
104
105 return DFL_ID_MAX;
106}
107
108/**
109 * struct build_feature_devs_info - info collected during feature dev build.
110 *
111 * @dev: device to enumerate.
112 * @cdev: the container device for all feature devices.
113 * @feature_dev: current feature device.
114 * @ioaddr: header register region address of feature device in enumeration.
115 * @sub_features: a sub features linked list for feature device in enumeration.
116 * @feature_num: number of sub features for feature device in enumeration.
117 */
118struct build_feature_devs_info {
119 struct device *dev;
120 struct dfl_fpga_cdev *cdev;
121 struct platform_device *feature_dev;
122 void __iomem *ioaddr;
123 struct list_head sub_features;
124 int feature_num;
125};
126
127/**
128 * struct dfl_feature_info - sub feature info collected during feature dev build
129 *
130 * @fid: id of this sub feature.
131 * @mmio_res: mmio resource of this sub feature.
132 * @ioaddr: mapped base address of mmio resource.
133 * @node: node in sub_features linked list.
134 */
135struct dfl_feature_info {
136 u64 fid;
137 struct resource mmio_res;
138 void __iomem *ioaddr;
139 struct list_head node;
140};
141
142static void dfl_fpga_cdev_add_port_dev(struct dfl_fpga_cdev *cdev,
143 struct platform_device *port)
144{
145 struct dfl_feature_platform_data *pdata = dev_get_platdata(&port->dev);
146
147 mutex_lock(&cdev->lock);
148 list_add(&pdata->node, &cdev->port_dev_list);
149 get_device(&pdata->dev->dev);
150 mutex_unlock(&cdev->lock);
151}
152
153/*
154 * register current feature device, it is called when we need to switch to
155 * another feature parsing or we have parsed all features on given device
156 * feature list.
157 */
158static int build_info_commit_dev(struct build_feature_devs_info *binfo)
159{
160 struct platform_device *fdev = binfo->feature_dev;
161 struct dfl_feature_platform_data *pdata;
162 struct dfl_feature_info *finfo, *p;
163 int ret, index = 0;
164
165 if (!fdev)
166 return 0;
167
168 /*
169 * we do not need to care for the memory which is associated with
170 * the platform device. After calling platform_device_unregister(),
171 * it will be automatically freed by device's release() callback,
172 * platform_device_release().
173 */
174 pdata = kzalloc(dfl_feature_platform_data_size(binfo->feature_num),
175 GFP_KERNEL);
176 if (!pdata)
177 return -ENOMEM;
178
179 pdata->dev = fdev;
180 pdata->num = binfo->feature_num;
181 pdata->dfl_cdev = binfo->cdev;
182 mutex_init(&pdata->lock);
183
184 /*
185 * the count should be initialized to 0 to make sure
186 *__fpga_port_enable() following __fpga_port_disable()
187 * works properly for port device.
188 * and it should always be 0 for fme device.
189 */
190 WARN_ON(pdata->disable_count);
191
192 fdev->dev.platform_data = pdata;
193
194 /* each sub feature has one MMIO resource */
195 fdev->num_resources = binfo->feature_num;
196 fdev->resource = kcalloc(binfo->feature_num, sizeof(*fdev->resource),
197 GFP_KERNEL);
198 if (!fdev->resource)
199 return -ENOMEM;
200
201 /* fill features and resource information for feature dev */
202 list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
203 struct dfl_feature *feature = &pdata->features[index];
204
205 /* save resource information for each feature */
206 feature->id = finfo->fid;
207 feature->resource_index = index;
208 feature->ioaddr = finfo->ioaddr;
209 fdev->resource[index++] = finfo->mmio_res;
210
211 list_del(&finfo->node);
212 kfree(finfo);
213 }
214
215 ret = platform_device_add(binfo->feature_dev);
216 if (!ret) {
217 if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
218 dfl_fpga_cdev_add_port_dev(binfo->cdev,
219 binfo->feature_dev);
220 else
221 binfo->cdev->fme_dev =
222 get_device(&binfo->feature_dev->dev);
223 /*
224 * reset it to avoid build_info_free() freeing their resource.
225 *
226 * The resource of successfully registered feature devices
227 * will be freed by platform_device_unregister(). See the
228 * comments in build_info_create_dev().
229 */
230 binfo->feature_dev = NULL;
231 }
232
233 return ret;
234}
235
236static int
237build_info_create_dev(struct build_feature_devs_info *binfo,
238 enum dfl_id_type type, void __iomem *ioaddr)
239{
240 struct platform_device *fdev;
241 int ret;
242
243 if (type >= DFL_ID_MAX)
244 return -EINVAL;
245
246 /* we will create a new device, commit current device first */
247 ret = build_info_commit_dev(binfo);
248 if (ret)
249 return ret;
250
251 /*
252 * we use -ENODEV as the initialization indicator which indicates
253 * whether the id need to be reclaimed
254 */
255 fdev = platform_device_alloc(dfl_devs[type].name, -ENODEV);
256 if (!fdev)
257 return -ENOMEM;
258
259 binfo->feature_dev = fdev;
260 binfo->feature_num = 0;
261 binfo->ioaddr = ioaddr;
262 INIT_LIST_HEAD(&binfo->sub_features);
263
264 fdev->id = dfl_id_alloc(type, &fdev->dev);
265 if (fdev->id < 0)
266 return fdev->id;
267
268 fdev->dev.parent = &binfo->cdev->region->dev;
269
270 return 0;
271}
272
273static void build_info_free(struct build_feature_devs_info *binfo)
274{
275 struct dfl_feature_info *finfo, *p;
276
277 /*
278 * it is a valid id, free it. See comments in
279 * build_info_create_dev()
280 */
281 if (binfo->feature_dev && binfo->feature_dev->id >= 0) {
282 dfl_id_free(feature_dev_id_type(binfo->feature_dev),
283 binfo->feature_dev->id);
284
285 list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
286 list_del(&finfo->node);
287 kfree(finfo);
288 }
289 }
290
291 platform_device_put(binfo->feature_dev);
292
293 devm_kfree(binfo->dev, binfo);
294}
295
296static inline u32 feature_size(void __iomem *start)
297{
298 u64 v = readq(start + DFH);
299 u32 ofst = FIELD_GET(DFH_NEXT_HDR_OFST, v);
300 /* workaround for private features with invalid size, use 4K instead */
301 return ofst ? ofst : 4096;
302}
303
304static u64 feature_id(void __iomem *start)
305{
306 u64 v = readq(start + DFH);
307 u16 id = FIELD_GET(DFH_ID, v);
308 u8 type = FIELD_GET(DFH_TYPE, v);
309
310 if (type == DFH_TYPE_FIU)
311 return FEATURE_ID_FIU_HEADER;
312 else if (type == DFH_TYPE_PRIVATE)
313 return id;
314 else if (type == DFH_TYPE_AFU)
315 return FEATURE_ID_AFU;
316
317 WARN_ON(1);
318 return 0;
319}
320
321/*
322 * when create sub feature instances, for private features, it doesn't need
323 * to provide resource size and feature id as they could be read from DFH
324 * register. For afu sub feature, its register region only contains user
325 * defined registers, so never trust any information from it, just use the
326 * resource size information provided by its parent FIU.
327 */
328static int
329create_feature_instance(struct build_feature_devs_info *binfo,
330 struct dfl_fpga_enum_dfl *dfl, resource_size_t ofst,
331 resource_size_t size, u64 fid)
332{
333 struct dfl_feature_info *finfo;
334
335 /* read feature size and id if inputs are invalid */
336 size = size ? size : feature_size(dfl->ioaddr + ofst);
337 fid = fid ? fid : feature_id(dfl->ioaddr + ofst);
338
339 if (dfl->len - ofst < size)
340 return -EINVAL;
341
342 finfo = kzalloc(sizeof(*finfo), GFP_KERNEL);
343 if (!finfo)
344 return -ENOMEM;
345
346 finfo->fid = fid;
347 finfo->mmio_res.start = dfl->start + ofst;
348 finfo->mmio_res.end = finfo->mmio_res.start + size - 1;
349 finfo->mmio_res.flags = IORESOURCE_MEM;
350 finfo->ioaddr = dfl->ioaddr + ofst;
351
352 list_add_tail(&finfo->node, &binfo->sub_features);
353 binfo->feature_num++;
354
355 return 0;
356}
357
358static int parse_feature_port_afu(struct build_feature_devs_info *binfo,
359 struct dfl_fpga_enum_dfl *dfl,
360 resource_size_t ofst)
361{
362 u64 v = readq(binfo->ioaddr + PORT_HDR_CAP);
363 u32 size = FIELD_GET(PORT_CAP_MMIO_SIZE, v) << 10;
364
365 WARN_ON(!size);
366
367 return create_feature_instance(binfo, dfl, ofst, size, FEATURE_ID_AFU);
368}
369
370static int parse_feature_afu(struct build_feature_devs_info *binfo,
371 struct dfl_fpga_enum_dfl *dfl,
372 resource_size_t ofst)
373{
374 if (!binfo->feature_dev) {
375 dev_err(binfo->dev, "this AFU does not belong to any FIU.\n");
376 return -EINVAL;
377 }
378
379 switch (feature_dev_id_type(binfo->feature_dev)) {
380 case PORT_ID:
381 return parse_feature_port_afu(binfo, dfl, ofst);
382 default:
383 dev_info(binfo->dev, "AFU belonging to FIU %s is not supported yet.\n",
384 binfo->feature_dev->name);
385 }
386
387 return 0;
388}
389
390static int parse_feature_fiu(struct build_feature_devs_info *binfo,
391 struct dfl_fpga_enum_dfl *dfl,
392 resource_size_t ofst)
393{
394 u32 id, offset;
395 u64 v;
396 int ret = 0;
397
398 v = readq(dfl->ioaddr + ofst + DFH);
399 id = FIELD_GET(DFH_ID, v);
400
401 /* create platform device for dfl feature dev */
402 ret = build_info_create_dev(binfo, dfh_id_to_type(id),
403 dfl->ioaddr + ofst);
404 if (ret)
405 return ret;
406
407 ret = create_feature_instance(binfo, dfl, ofst, 0, 0);
408 if (ret)
409 return ret;
410 /*
411 * find and parse FIU's child AFU via its NEXT_AFU register.
412 * please note that only Port has valid NEXT_AFU pointer per spec.
413 */
414 v = readq(dfl->ioaddr + ofst + NEXT_AFU);
415
416 offset = FIELD_GET(NEXT_AFU_NEXT_DFH_OFST, v);
417 if (offset)
418 return parse_feature_afu(binfo, dfl, ofst + offset);
419
420 dev_dbg(binfo->dev, "No AFUs detected on FIU %d\n", id);
421
422 return ret;
423}
424
425static int parse_feature_private(struct build_feature_devs_info *binfo,
426 struct dfl_fpga_enum_dfl *dfl,
427 resource_size_t ofst)
428{
429 if (!binfo->feature_dev) {
430 dev_err(binfo->dev, "the private feature %llx does not belong to any AFU.\n",
431 (unsigned long long)feature_id(dfl->ioaddr + ofst));
432 return -EINVAL;
433 }
434
435 return create_feature_instance(binfo, dfl, ofst, 0, 0);
436}
437
438/**
439 * parse_feature - parse a feature on given device feature list
440 *
441 * @binfo: build feature devices information.
442 * @dfl: device feature list to parse
443 * @ofst: offset to feature header on this device feature list
444 */
445static int parse_feature(struct build_feature_devs_info *binfo,
446 struct dfl_fpga_enum_dfl *dfl, resource_size_t ofst)
447{
448 u64 v;
449 u32 type;
450
451 v = readq(dfl->ioaddr + ofst + DFH);
452 type = FIELD_GET(DFH_TYPE, v);
453
454 switch (type) {
455 case DFH_TYPE_AFU:
456 return parse_feature_afu(binfo, dfl, ofst);
457 case DFH_TYPE_PRIVATE:
458 return parse_feature_private(binfo, dfl, ofst);
459 case DFH_TYPE_FIU:
460 return parse_feature_fiu(binfo, dfl, ofst);
461 default:
462 dev_info(binfo->dev,
463 "Feature Type %x is not supported.\n", type);
464 }
465
466 return 0;
467}
468
469static int parse_feature_list(struct build_feature_devs_info *binfo,
470 struct dfl_fpga_enum_dfl *dfl)
471{
472 void __iomem *start = dfl->ioaddr;
473 void __iomem *end = dfl->ioaddr + dfl->len;
474 int ret = 0;
475 u32 ofst = 0;
476 u64 v;
477
478 /* walk through the device feature list via DFH's next DFH pointer. */
479 for (; start < end; start += ofst) {
480 if (end - start < DFH_SIZE) {
481 dev_err(binfo->dev, "The region is too small to contain a feature.\n");
482 return -EINVAL;
483 }
484
485 ret = parse_feature(binfo, dfl, start - dfl->ioaddr);
486 if (ret)
487 return ret;
488
489 v = readq(start + DFH);
490 ofst = FIELD_GET(DFH_NEXT_HDR_OFST, v);
491
492 /* stop parsing if EOL(End of List) is set or offset is 0 */
493 if ((v & DFH_EOL) || !ofst)
494 break;
495 }
496
497 /* commit current feature device when reach the end of list */
498 return build_info_commit_dev(binfo);
499}
500
501struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev)
502{
503 struct dfl_fpga_enum_info *info;
504
505 get_device(dev);
506
507 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
508 if (!info) {
509 put_device(dev);
510 return NULL;
511 }
512
513 info->dev = dev;
514 INIT_LIST_HEAD(&info->dfls);
515
516 return info;
517}
518EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_alloc);
519
520void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info)
521{
522 struct dfl_fpga_enum_dfl *tmp, *dfl;
523 struct device *dev;
524
525 if (!info)
526 return;
527
528 dev = info->dev;
529
530 /* remove all device feature lists in the list. */
531 list_for_each_entry_safe(dfl, tmp, &info->dfls, node) {
532 list_del(&dfl->node);
533 devm_kfree(dev, dfl);
534 }
535
536 devm_kfree(dev, info);
537 put_device(dev);
538}
539EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_free);
540
541/**
542 * dfl_fpga_enum_info_add_dfl - add info of a device feature list to enum info
543 *
544 * @info: ptr to dfl_fpga_enum_info
545 * @start: mmio resource address of the device feature list.
546 * @len: mmio resource length of the device feature list.
547 * @ioaddr: mapped mmio resource address of the device feature list.
548 *
549 * One FPGA device may have one or more Device Feature Lists (DFLs), use this
550 * function to add information of each DFL to common data structure for next
551 * step enumeration.
552 *
553 * Return: 0 on success, negative error code otherwise.
554 */
555int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
556 resource_size_t start, resource_size_t len,
557 void __iomem *ioaddr)
558{
559 struct dfl_fpga_enum_dfl *dfl;
560
561 dfl = devm_kzalloc(info->dev, sizeof(*dfl), GFP_KERNEL);
562 if (!dfl)
563 return -ENOMEM;
564
565 dfl->start = start;
566 dfl->len = len;
567 dfl->ioaddr = ioaddr;
568
569 list_add_tail(&dfl->node, &info->dfls);
570
571 return 0;
572}
573EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_add_dfl);
574
575static int remove_feature_dev(struct device *dev, void *data)
576{
577 struct platform_device *pdev = to_platform_device(dev);
578 enum dfl_id_type type = feature_dev_id_type(pdev);
579 int id = pdev->id;
580
581 platform_device_unregister(pdev);
582
583 dfl_id_free(type, id);
584
585 return 0;
586}
587
588static void remove_feature_devs(struct dfl_fpga_cdev *cdev)
589{
590 device_for_each_child(&cdev->region->dev, NULL, remove_feature_dev);
591}
592
593/**
594 * dfl_fpga_feature_devs_enumerate - enumerate feature devices
595 * @info: information for enumeration.
596 *
597 * This function creates a container device (base FPGA region), enumerates
598 * feature devices based on the enumeration info and creates platform devices
599 * under the container device.
600 *
601 * Return: dfl_fpga_cdev struct on success, -errno on failure
602 */
603struct dfl_fpga_cdev *
604dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
605{
606 struct build_feature_devs_info *binfo;
607 struct dfl_fpga_enum_dfl *dfl;
608 struct dfl_fpga_cdev *cdev;
609 int ret = 0;
610
611 if (!info->dev)
612 return ERR_PTR(-ENODEV);
613
614 cdev = devm_kzalloc(info->dev, sizeof(*cdev), GFP_KERNEL);
615 if (!cdev)
616 return ERR_PTR(-ENOMEM);
617
618 cdev->region = fpga_region_create(info->dev, NULL, NULL);
619 if (!cdev->region) {
620 ret = -ENOMEM;
621 goto free_cdev_exit;
622 }
623
624 cdev->parent = info->dev;
625 mutex_init(&cdev->lock);
626 INIT_LIST_HEAD(&cdev->port_dev_list);
627
628 ret = fpga_region_register(cdev->region);
629 if (ret)
630 goto free_region_exit;
631
632 /* create and init build info for enumeration */
633 binfo = devm_kzalloc(info->dev, sizeof(*binfo), GFP_KERNEL);
634 if (!binfo) {
635 ret = -ENOMEM;
636 goto unregister_region_exit;
637 }
638
639 binfo->dev = info->dev;
640 binfo->cdev = cdev;
641
642 /*
643 * start enumeration for all feature devices based on Device Feature
644 * Lists.
645 */
646 list_for_each_entry(dfl, &info->dfls, node) {
647 ret = parse_feature_list(binfo, dfl);
648 if (ret) {
649 remove_feature_devs(cdev);
650 build_info_free(binfo);
651 goto unregister_region_exit;
652 }
653 }
654
655 build_info_free(binfo);
656
657 return cdev;
658
659unregister_region_exit:
660 fpga_region_unregister(cdev->region);
661free_region_exit:
662 fpga_region_free(cdev->region);
663free_cdev_exit:
664 devm_kfree(info->dev, cdev);
665 return ERR_PTR(ret);
666}
667EXPORT_SYMBOL_GPL(dfl_fpga_feature_devs_enumerate);
668
669/**
670 * dfl_fpga_feature_devs_remove - remove all feature devices
671 * @cdev: fpga container device.
672 *
673 * Remove the container device and all feature devices under given container
674 * devices.
675 */
676void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev)
677{
678 struct dfl_feature_platform_data *pdata, *ptmp;
679
680 remove_feature_devs(cdev);
681
682 mutex_lock(&cdev->lock);
683 if (cdev->fme_dev) {
684 /* the fme should be unregistered. */
685 WARN_ON(device_is_registered(cdev->fme_dev));
686 put_device(cdev->fme_dev);
687 }
688
689 list_for_each_entry_safe(pdata, ptmp, &cdev->port_dev_list, node) {
690 struct platform_device *port_dev = pdata->dev;
691
692 /* the port should be unregistered. */
693 WARN_ON(device_is_registered(&port_dev->dev));
694 list_del(&pdata->node);
695 put_device(&port_dev->dev);
696 }
697 mutex_unlock(&cdev->lock);
698
699 fpga_region_unregister(cdev->region);
700 devm_kfree(cdev->parent, cdev);
701}
702EXPORT_SYMBOL_GPL(dfl_fpga_feature_devs_remove);
703
704static int __init dfl_fpga_init(void)
705{
706 dfl_ids_init();
707
708 return 0;
709}
710
711static void __exit dfl_fpga_exit(void)
712{
713 dfl_ids_destroy();
714}
715
716module_init(dfl_fpga_init);
717module_exit(dfl_fpga_exit);
718
719MODULE_DESCRIPTION("FPGA Device Feature List (DFL) Support");
720MODULE_AUTHOR("Intel Corporation");
721MODULE_LICENSE("GPL v2");
diff --git a/drivers/fpga/dfl.h b/drivers/fpga/dfl.h
new file mode 100644
index 000000000000..47ecb3bb6f61
--- /dev/null
+++ b/drivers/fpga/dfl.h
@@ -0,0 +1,279 @@
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Driver Header File for FPGA Device Feature List (DFL) Support
4 *
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
6 *
7 * Authors:
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>
12 */
13
14#ifndef __FPGA_DFL_H
15#define __FPGA_DFL_H
16
17#include <linux/bitfield.h>
18#include <linux/delay.h>
19#include <linux/fs.h>
20#include <linux/iopoll.h>
21#include <linux/io-64-nonatomic-lo-hi.h>
22#include <linux/platform_device.h>
23#include <linux/slab.h>
24#include <linux/uuid.h>
25#include <linux/fpga/fpga-region.h>
26
27/* maximum supported number of ports */
28#define MAX_DFL_FPGA_PORT_NUM 4
29/* plus one for fme device */
30#define MAX_DFL_FEATURE_DEV_NUM (MAX_DFL_FPGA_PORT_NUM + 1)
31
32/* Reserved 0x0 for Header Group Register and 0xff for AFU */
33#define FEATURE_ID_FIU_HEADER 0x0
34#define FEATURE_ID_AFU 0xff
35
36#define FME_FEATURE_ID_HEADER FEATURE_ID_FIU_HEADER
37#define FME_FEATURE_ID_THERMAL_MGMT 0x1
38#define FME_FEATURE_ID_POWER_MGMT 0x2
39#define FME_FEATURE_ID_GLOBAL_IPERF 0x3
40#define FME_FEATURE_ID_GLOBAL_ERR 0x4
41#define FME_FEATURE_ID_PR_MGMT 0x5
42#define FME_FEATURE_ID_HSSI 0x6
43#define FME_FEATURE_ID_GLOBAL_DPERF 0x7
44
45#define PORT_FEATURE_ID_HEADER FEATURE_ID_FIU_HEADER
46#define PORT_FEATURE_ID_AFU FEATURE_ID_AFU
47#define PORT_FEATURE_ID_ERROR 0x10
48#define PORT_FEATURE_ID_UMSG 0x11
49#define PORT_FEATURE_ID_UINT 0x12
50#define PORT_FEATURE_ID_STP 0x13
51
52/*
53 * Device Feature Header Register Set
54 *
55 * For FIUs, they all have DFH + GUID + NEXT_AFU as common header registers.
56 * For AFUs, they have DFH + GUID as common header registers.
57 * For private features, they only have DFH register as common header.
58 */
59#define DFH 0x0
60#define GUID_L 0x8
61#define GUID_H 0x10
62#define NEXT_AFU 0x18
63
64#define DFH_SIZE 0x8
65
66/* Device Feature Header Register Bitfield */
67#define DFH_ID GENMASK_ULL(11, 0) /* Feature ID */
68#define DFH_ID_FIU_FME 0
69#define DFH_ID_FIU_PORT 1
70#define DFH_REVISION GENMASK_ULL(15, 12) /* Feature revision */
71#define DFH_NEXT_HDR_OFST GENMASK_ULL(39, 16) /* Offset to next DFH */
72#define DFH_EOL BIT_ULL(40) /* End of list */
73#define DFH_TYPE GENMASK_ULL(63, 60) /* Feature type */
74#define DFH_TYPE_AFU 1
75#define DFH_TYPE_PRIVATE 3
76#define DFH_TYPE_FIU 4
77
78/* Next AFU Register Bitfield */
79#define NEXT_AFU_NEXT_DFH_OFST GENMASK_ULL(23, 0) /* Offset to next AFU */
80
81/* FME Header Register Set */
82#define FME_HDR_DFH DFH
83#define FME_HDR_GUID_L GUID_L
84#define FME_HDR_GUID_H GUID_H
85#define FME_HDR_NEXT_AFU NEXT_AFU
86#define FME_HDR_CAP 0x30
87#define FME_HDR_PORT_OFST(n) (0x38 + ((n) * 0x8))
88#define FME_HDR_BITSTREAM_ID 0x60
89#define FME_HDR_BITSTREAM_MD 0x68
90
91/* FME Fab Capability Register Bitfield */
92#define FME_CAP_FABRIC_VERID GENMASK_ULL(7, 0) /* Fabric version ID */
93#define FME_CAP_SOCKET_ID BIT_ULL(8) /* Socket ID */
94#define FME_CAP_PCIE0_LINK_AVL BIT_ULL(12) /* PCIE0 Link */
95#define FME_CAP_PCIE1_LINK_AVL BIT_ULL(13) /* PCIE1 Link */
96#define FME_CAP_COHR_LINK_AVL BIT_ULL(14) /* Coherent Link */
97#define FME_CAP_IOMMU_AVL BIT_ULL(16) /* IOMMU available */
98#define FME_CAP_NUM_PORTS GENMASK_ULL(19, 17) /* Number of ports */
99#define FME_CAP_ADDR_WIDTH GENMASK_ULL(29, 24) /* Address bus width */
100#define FME_CAP_CACHE_SIZE GENMASK_ULL(43, 32) /* cache size in KB */
101#define FME_CAP_CACHE_ASSOC GENMASK_ULL(47, 44) /* Associativity */
102
103/* FME Port Offset Register Bitfield */
104/* Offset to port device feature header */
105#define FME_PORT_OFST_DFH_OFST GENMASK_ULL(23, 0)
106/* PCI Bar ID for this port */
107#define FME_PORT_OFST_BAR_ID GENMASK_ULL(34, 32)
108/* AFU MMIO access permission. 1 - VF, 0 - PF. */
109#define FME_PORT_OFST_ACC_CTRL BIT_ULL(55)
110#define FME_PORT_OFST_ACC_PF 0
111#define FME_PORT_OFST_ACC_VF 1
112#define FME_PORT_OFST_IMP BIT_ULL(60)
113
114/* PORT Header Register Set */
115#define PORT_HDR_DFH DFH
116#define PORT_HDR_GUID_L GUID_L
117#define PORT_HDR_GUID_H GUID_H
118#define PORT_HDR_NEXT_AFU NEXT_AFU
119#define PORT_HDR_CAP 0x30
120#define PORT_HDR_CTRL 0x38
121
122/* Port Capability Register Bitfield */
123#define PORT_CAP_PORT_NUM GENMASK_ULL(1, 0) /* ID of this port */
124#define PORT_CAP_MMIO_SIZE GENMASK_ULL(23, 8) /* MMIO size in KB */
125#define PORT_CAP_SUPP_INT_NUM GENMASK_ULL(35, 32) /* Interrupts num */
126
127/* Port Control Register Bitfield */
128#define PORT_CTRL_SFTRST BIT_ULL(0) /* Port soft reset */
129/* Latency tolerance reporting. '1' >= 40us, '0' < 40us.*/
130#define PORT_CTRL_LATENCY BIT_ULL(2)
131#define PORT_CTRL_SFTRST_ACK BIT_ULL(4) /* HW ack for reset */
132
133/**
134 * struct dfl_feature - sub feature of the feature devices
135 *
136 * @id: sub feature id.
137 * @resource_index: each sub feature has one mmio resource for its registers.
138 * this index is used to find its mmio resource from the
139 * feature dev (platform device)'s reources.
140 * @ioaddr: mapped mmio resource address.
141 */
142struct dfl_feature {
143 u64 id;
144 int resource_index;
145 void __iomem *ioaddr;
146};
147
148/**
149 * struct dfl_feature_platform_data - platform data for feature devices
150 *
151 * @node: node to link feature devs to container device's port_dev_list.
152 * @lock: mutex to protect platform data.
153 * @dev: ptr to platform device linked with this platform data.
154 * @dfl_cdev: ptr to container device.
155 * @disable_count: count for port disable.
156 * @num: number for sub features.
157 * @features: sub features of this feature dev.
158 */
159struct dfl_feature_platform_data {
160 struct list_head node;
161 struct mutex lock;
162 struct platform_device *dev;
163 struct dfl_fpga_cdev *dfl_cdev;
164 unsigned int disable_count;
165
166 int num;
167 struct dfl_feature features[0];
168};
169
170#define DFL_FPGA_FEATURE_DEV_FME "dfl-fme"
171#define DFL_FPGA_FEATURE_DEV_PORT "dfl-port"
172
173static inline int dfl_feature_platform_data_size(const int num)
174{
175 return sizeof(struct dfl_feature_platform_data) +
176 num * sizeof(struct dfl_feature);
177}
178
179#define dfl_fpga_dev_for_each_feature(pdata, feature) \
180 for ((feature) = (pdata)->features; \
181 (feature) < (pdata)->features + (pdata)->num; (feature)++)
182
183static inline
184struct dfl_feature *dfl_get_feature_by_id(struct device *dev, u64 id)
185{
186 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
187 struct dfl_feature *feature;
188
189 dfl_fpga_dev_for_each_feature(pdata, feature)
190 if (feature->id == id)
191 return feature;
192
193 return NULL;
194}
195
196static inline
197void __iomem *dfl_get_feature_ioaddr_by_id(struct device *dev, u64 id)
198{
199 struct dfl_feature *feature = dfl_get_feature_by_id(dev, id);
200
201 if (feature && feature->ioaddr)
202 return feature->ioaddr;
203
204 WARN_ON(1);
205 return NULL;
206}
207
208static inline bool dfl_feature_is_fme(void __iomem *base)
209{
210 u64 v = readq(base + DFH);
211
212 return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
213 (FIELD_GET(DFH_ID, v) == DFH_ID_FIU_FME);
214}
215
216static inline bool dfl_feature_is_port(void __iomem *base)
217{
218 u64 v = readq(base + DFH);
219
220 return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
221 (FIELD_GET(DFH_ID, v) == DFH_ID_FIU_PORT);
222}
223
224/**
225 * struct dfl_fpga_enum_info - DFL FPGA enumeration information
226 *
227 * @dev: parent device.
228 * @dfls: list of device feature lists.
229 */
230struct dfl_fpga_enum_info {
231 struct device *dev;
232 struct list_head dfls;
233};
234
235/**
236 * struct dfl_fpga_enum_dfl - DFL FPGA enumeration device feature list info
237 *
238 * @start: base address of this device feature list.
239 * @len: size of this device feature list.
240 * @ioaddr: mapped base address of this device feature list.
241 * @node: node in list of device feature lists.
242 */
243struct dfl_fpga_enum_dfl {
244 resource_size_t start;
245 resource_size_t len;
246
247 void __iomem *ioaddr;
248
249 struct list_head node;
250};
251
252struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev);
253int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
254 resource_size_t start, resource_size_t len,
255 void __iomem *ioaddr);
256void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info);
257
258/**
259 * struct dfl_fpga_cdev - container device of DFL based FPGA
260 *
261 * @parent: parent device of this container device.
262 * @region: base fpga region.
263 * @fme_dev: FME feature device under this container device.
264 * @lock: mutex lock to protect the port device list.
265 * @port_dev_list: list of all port feature devices under this container device.
266 */
267struct dfl_fpga_cdev {
268 struct device *parent;
269 struct fpga_region *region;
270 struct device *fme_dev;
271 struct mutex lock;
272 struct list_head port_dev_list;
273};
274
275struct dfl_fpga_cdev *
276dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info);
277void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev);
278
279#endif /* __FPGA_DFL_H */