1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Driver for FPGA Device Feature List (DFL) Support
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2017-2018 Intel Corporation, Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Authors:
8*4882a593Smuzhiyun * Kang Luwei <luwei.kang@intel.com>
9*4882a593Smuzhiyun * Zhang Yi <yi.z.zhang@intel.com>
10*4882a593Smuzhiyun * Wu Hao <hao.wu@intel.com>
11*4882a593Smuzhiyun * Xiao Guangrong <guangrong.xiao@linux.intel.com>
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun #include <linux/fpga-dfl.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/uaccess.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include "dfl.h"
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun static DEFINE_MUTEX(dfl_id_mutex);
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun * when adding a new feature dev support in DFL framework, it's required to
23*4882a593Smuzhiyun * add a new item in enum dfl_id_type and provide related information in below
24*4882a593Smuzhiyun * dfl_devs table which is indexed by dfl_id_type, e.g. name string used for
25*4882a593Smuzhiyun * platform device creation (define name strings in dfl.h, as they could be
26*4882a593Smuzhiyun * reused by platform device drivers).
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * if the new feature dev needs chardev support, then it's required to add
29*4882a593Smuzhiyun * a new item in dfl_chardevs table and configure dfl_devs[i].devt_type as
30*4882a593Smuzhiyun * index to dfl_chardevs table. If no chardev support just set devt_type
31*4882a593Smuzhiyun * as one invalid index (DFL_FPGA_DEVT_MAX).
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun enum dfl_fpga_devt_type {
34*4882a593Smuzhiyun DFL_FPGA_DEVT_FME,
35*4882a593Smuzhiyun DFL_FPGA_DEVT_PORT,
36*4882a593Smuzhiyun DFL_FPGA_DEVT_MAX,
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun static struct lock_class_key dfl_pdata_keys[DFL_ID_MAX];
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun static const char *dfl_pdata_key_strings[DFL_ID_MAX] = {
42*4882a593Smuzhiyun "dfl-fme-pdata",
43*4882a593Smuzhiyun "dfl-port-pdata",
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /**
47*4882a593Smuzhiyun * dfl_dev_info - dfl feature device information.
48*4882a593Smuzhiyun * @name: name string of the feature platform device.
49*4882a593Smuzhiyun * @dfh_id: id value in Device Feature Header (DFH) register by DFL spec.
50*4882a593Smuzhiyun * @id: idr id of the feature dev.
51*4882a593Smuzhiyun * @devt_type: index to dfl_chrdevs[].
52*4882a593Smuzhiyun */
53*4882a593Smuzhiyun struct dfl_dev_info {
54*4882a593Smuzhiyun const char *name;
55*4882a593Smuzhiyun u16 dfh_id;
56*4882a593Smuzhiyun struct idr id;
57*4882a593Smuzhiyun enum dfl_fpga_devt_type devt_type;
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /* it is indexed by dfl_id_type */
61*4882a593Smuzhiyun static struct dfl_dev_info dfl_devs[] = {
62*4882a593Smuzhiyun {.name = DFL_FPGA_FEATURE_DEV_FME, .dfh_id = DFH_ID_FIU_FME,
63*4882a593Smuzhiyun .devt_type = DFL_FPGA_DEVT_FME},
64*4882a593Smuzhiyun {.name = DFL_FPGA_FEATURE_DEV_PORT, .dfh_id = DFH_ID_FIU_PORT,
65*4882a593Smuzhiyun .devt_type = DFL_FPGA_DEVT_PORT},
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /**
69*4882a593Smuzhiyun * dfl_chardev_info - chardev information of dfl feature device
70*4882a593Smuzhiyun * @name: nmae string of the char device.
71*4882a593Smuzhiyun * @devt: devt of the char device.
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun struct dfl_chardev_info {
74*4882a593Smuzhiyun const char *name;
75*4882a593Smuzhiyun dev_t devt;
76*4882a593Smuzhiyun };
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /* indexed by enum dfl_fpga_devt_type */
79*4882a593Smuzhiyun static struct dfl_chardev_info dfl_chrdevs[] = {
80*4882a593Smuzhiyun {.name = DFL_FPGA_FEATURE_DEV_FME},
81*4882a593Smuzhiyun {.name = DFL_FPGA_FEATURE_DEV_PORT},
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun
dfl_ids_init(void)84*4882a593Smuzhiyun static void dfl_ids_init(void)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun int i;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
89*4882a593Smuzhiyun idr_init(&dfl_devs[i].id);
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
dfl_ids_destroy(void)92*4882a593Smuzhiyun static void dfl_ids_destroy(void)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun int i;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
97*4882a593Smuzhiyun idr_destroy(&dfl_devs[i].id);
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
dfl_id_alloc(enum dfl_id_type type,struct device * dev)100*4882a593Smuzhiyun static int dfl_id_alloc(enum dfl_id_type type, struct device *dev)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun int id;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun WARN_ON(type >= DFL_ID_MAX);
105*4882a593Smuzhiyun mutex_lock(&dfl_id_mutex);
106*4882a593Smuzhiyun id = idr_alloc(&dfl_devs[type].id, dev, 0, 0, GFP_KERNEL);
107*4882a593Smuzhiyun mutex_unlock(&dfl_id_mutex);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun return id;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
dfl_id_free(enum dfl_id_type type,int id)112*4882a593Smuzhiyun static void dfl_id_free(enum dfl_id_type type, int id)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun WARN_ON(type >= DFL_ID_MAX);
115*4882a593Smuzhiyun mutex_lock(&dfl_id_mutex);
116*4882a593Smuzhiyun idr_remove(&dfl_devs[type].id, id);
117*4882a593Smuzhiyun mutex_unlock(&dfl_id_mutex);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
feature_dev_id_type(struct platform_device * pdev)120*4882a593Smuzhiyun static enum dfl_id_type feature_dev_id_type(struct platform_device *pdev)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun int i;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
125*4882a593Smuzhiyun if (!strcmp(dfl_devs[i].name, pdev->name))
126*4882a593Smuzhiyun return i;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun return DFL_ID_MAX;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
dfh_id_to_type(u16 id)131*4882a593Smuzhiyun static enum dfl_id_type dfh_id_to_type(u16 id)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun int i;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
136*4882a593Smuzhiyun if (dfl_devs[i].dfh_id == id)
137*4882a593Smuzhiyun return i;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun return DFL_ID_MAX;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun * introduce a global port_ops list, it allows port drivers to register ops
144*4882a593Smuzhiyun * in such list, then other feature devices (e.g. FME), could use the port
145*4882a593Smuzhiyun * functions even related port platform device is hidden. Below is one example,
146*4882a593Smuzhiyun * in virtualization case of PCIe-based FPGA DFL device, when SRIOV is
147*4882a593Smuzhiyun * enabled, port (and it's AFU) is turned into VF and port platform device
148*4882a593Smuzhiyun * is hidden from system but it's still required to access port to finish FPGA
149*4882a593Smuzhiyun * reconfiguration function in FME.
150*4882a593Smuzhiyun */
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun static DEFINE_MUTEX(dfl_port_ops_mutex);
153*4882a593Smuzhiyun static LIST_HEAD(dfl_port_ops_list);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /**
156*4882a593Smuzhiyun * dfl_fpga_port_ops_get - get matched port ops from the global list
157*4882a593Smuzhiyun * @pdev: platform device to match with associated port ops.
158*4882a593Smuzhiyun * Return: matched port ops on success, NULL otherwise.
159*4882a593Smuzhiyun *
160*4882a593Smuzhiyun * Please note that must dfl_fpga_port_ops_put after use the port_ops.
161*4882a593Smuzhiyun */
dfl_fpga_port_ops_get(struct platform_device * pdev)162*4882a593Smuzhiyun struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct platform_device *pdev)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun struct dfl_fpga_port_ops *ops = NULL;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun mutex_lock(&dfl_port_ops_mutex);
167*4882a593Smuzhiyun if (list_empty(&dfl_port_ops_list))
168*4882a593Smuzhiyun goto done;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun list_for_each_entry(ops, &dfl_port_ops_list, node) {
171*4882a593Smuzhiyun /* match port_ops using the name of platform device */
172*4882a593Smuzhiyun if (!strcmp(pdev->name, ops->name)) {
173*4882a593Smuzhiyun if (!try_module_get(ops->owner))
174*4882a593Smuzhiyun ops = NULL;
175*4882a593Smuzhiyun goto done;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun ops = NULL;
180*4882a593Smuzhiyun done:
181*4882a593Smuzhiyun mutex_unlock(&dfl_port_ops_mutex);
182*4882a593Smuzhiyun return ops;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_get);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /**
187*4882a593Smuzhiyun * dfl_fpga_port_ops_put - put port ops
188*4882a593Smuzhiyun * @ops: port ops.
189*4882a593Smuzhiyun */
dfl_fpga_port_ops_put(struct dfl_fpga_port_ops * ops)190*4882a593Smuzhiyun void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops *ops)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun if (ops && ops->owner)
193*4882a593Smuzhiyun module_put(ops->owner);
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_put);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun /**
198*4882a593Smuzhiyun * dfl_fpga_port_ops_add - add port_ops to global list
199*4882a593Smuzhiyun * @ops: port ops to add.
200*4882a593Smuzhiyun */
dfl_fpga_port_ops_add(struct dfl_fpga_port_ops * ops)201*4882a593Smuzhiyun void dfl_fpga_port_ops_add(struct dfl_fpga_port_ops *ops)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun mutex_lock(&dfl_port_ops_mutex);
204*4882a593Smuzhiyun list_add_tail(&ops->node, &dfl_port_ops_list);
205*4882a593Smuzhiyun mutex_unlock(&dfl_port_ops_mutex);
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_add);
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /**
210*4882a593Smuzhiyun * dfl_fpga_port_ops_del - remove port_ops from global list
211*4882a593Smuzhiyun * @ops: port ops to del.
212*4882a593Smuzhiyun */
dfl_fpga_port_ops_del(struct dfl_fpga_port_ops * ops)213*4882a593Smuzhiyun void dfl_fpga_port_ops_del(struct dfl_fpga_port_ops *ops)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun mutex_lock(&dfl_port_ops_mutex);
216*4882a593Smuzhiyun list_del(&ops->node);
217*4882a593Smuzhiyun mutex_unlock(&dfl_port_ops_mutex);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_del);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /**
222*4882a593Smuzhiyun * dfl_fpga_check_port_id - check the port id
223*4882a593Smuzhiyun * @pdev: port platform device.
224*4882a593Smuzhiyun * @pport_id: port id to compare.
225*4882a593Smuzhiyun *
226*4882a593Smuzhiyun * Return: 1 if port device matches with given port id, otherwise 0.
227*4882a593Smuzhiyun */
dfl_fpga_check_port_id(struct platform_device * pdev,void * pport_id)228*4882a593Smuzhiyun int dfl_fpga_check_port_id(struct platform_device *pdev, void *pport_id)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
231*4882a593Smuzhiyun struct dfl_fpga_port_ops *port_ops;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun if (pdata->id != FEATURE_DEV_ID_UNUSED)
234*4882a593Smuzhiyun return pdata->id == *(int *)pport_id;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun port_ops = dfl_fpga_port_ops_get(pdev);
237*4882a593Smuzhiyun if (!port_ops || !port_ops->get_id)
238*4882a593Smuzhiyun return 0;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun pdata->id = port_ops->get_id(pdev);
241*4882a593Smuzhiyun dfl_fpga_port_ops_put(port_ops);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun return pdata->id == *(int *)pport_id;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_check_port_id);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun static DEFINE_IDA(dfl_device_ida);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun static const struct dfl_device_id *
dfl_match_one_device(const struct dfl_device_id * id,struct dfl_device * ddev)250*4882a593Smuzhiyun dfl_match_one_device(const struct dfl_device_id *id, struct dfl_device *ddev)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun if (id->type == ddev->type && id->feature_id == ddev->feature_id)
253*4882a593Smuzhiyun return id;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun return NULL;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
dfl_bus_match(struct device * dev,struct device_driver * drv)258*4882a593Smuzhiyun static int dfl_bus_match(struct device *dev, struct device_driver *drv)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun struct dfl_device *ddev = to_dfl_dev(dev);
261*4882a593Smuzhiyun struct dfl_driver *ddrv = to_dfl_drv(drv);
262*4882a593Smuzhiyun const struct dfl_device_id *id_entry;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun id_entry = ddrv->id_table;
265*4882a593Smuzhiyun if (id_entry) {
266*4882a593Smuzhiyun while (id_entry->feature_id) {
267*4882a593Smuzhiyun if (dfl_match_one_device(id_entry, ddev)) {
268*4882a593Smuzhiyun ddev->id_entry = id_entry;
269*4882a593Smuzhiyun return 1;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun id_entry++;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun return 0;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
dfl_bus_probe(struct device * dev)278*4882a593Smuzhiyun static int dfl_bus_probe(struct device *dev)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun struct dfl_driver *ddrv = to_dfl_drv(dev->driver);
281*4882a593Smuzhiyun struct dfl_device *ddev = to_dfl_dev(dev);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun return ddrv->probe(ddev);
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
dfl_bus_remove(struct device * dev)286*4882a593Smuzhiyun static int dfl_bus_remove(struct device *dev)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun struct dfl_driver *ddrv = to_dfl_drv(dev->driver);
289*4882a593Smuzhiyun struct dfl_device *ddev = to_dfl_dev(dev);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun if (ddrv->remove)
292*4882a593Smuzhiyun ddrv->remove(ddev);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun return 0;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
dfl_bus_uevent(struct device * dev,struct kobj_uevent_env * env)297*4882a593Smuzhiyun static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun struct dfl_device *ddev = to_dfl_dev(dev);
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun /* The type has 4 valid bits and feature_id has 12 valid bits */
302*4882a593Smuzhiyun return add_uevent_var(env, "MODALIAS=dfl:t%01Xf%03X",
303*4882a593Smuzhiyun ddev->type, ddev->feature_id);
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun static ssize_t
type_show(struct device * dev,struct device_attribute * attr,char * buf)307*4882a593Smuzhiyun type_show(struct device *dev, struct device_attribute *attr, char *buf)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun struct dfl_device *ddev = to_dfl_dev(dev);
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun return sprintf(buf, "0x%x\n", ddev->type);
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun static DEVICE_ATTR_RO(type);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun static ssize_t
feature_id_show(struct device * dev,struct device_attribute * attr,char * buf)316*4882a593Smuzhiyun feature_id_show(struct device *dev, struct device_attribute *attr, char *buf)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun struct dfl_device *ddev = to_dfl_dev(dev);
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun return sprintf(buf, "0x%x\n", ddev->feature_id);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun static DEVICE_ATTR_RO(feature_id);
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun static struct attribute *dfl_dev_attrs[] = {
325*4882a593Smuzhiyun &dev_attr_type.attr,
326*4882a593Smuzhiyun &dev_attr_feature_id.attr,
327*4882a593Smuzhiyun NULL,
328*4882a593Smuzhiyun };
329*4882a593Smuzhiyun ATTRIBUTE_GROUPS(dfl_dev);
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun static struct bus_type dfl_bus_type = {
332*4882a593Smuzhiyun .name = "dfl",
333*4882a593Smuzhiyun .match = dfl_bus_match,
334*4882a593Smuzhiyun .probe = dfl_bus_probe,
335*4882a593Smuzhiyun .remove = dfl_bus_remove,
336*4882a593Smuzhiyun .uevent = dfl_bus_uevent,
337*4882a593Smuzhiyun .dev_groups = dfl_dev_groups,
338*4882a593Smuzhiyun };
339*4882a593Smuzhiyun
release_dfl_dev(struct device * dev)340*4882a593Smuzhiyun static void release_dfl_dev(struct device *dev)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun struct dfl_device *ddev = to_dfl_dev(dev);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun if (ddev->mmio_res.parent)
345*4882a593Smuzhiyun release_resource(&ddev->mmio_res);
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun ida_simple_remove(&dfl_device_ida, ddev->id);
348*4882a593Smuzhiyun kfree(ddev->irqs);
349*4882a593Smuzhiyun kfree(ddev);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun static struct dfl_device *
dfl_dev_add(struct dfl_feature_platform_data * pdata,struct dfl_feature * feature)353*4882a593Smuzhiyun dfl_dev_add(struct dfl_feature_platform_data *pdata,
354*4882a593Smuzhiyun struct dfl_feature *feature)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun struct platform_device *pdev = pdata->dev;
357*4882a593Smuzhiyun struct resource *parent_res;
358*4882a593Smuzhiyun struct dfl_device *ddev;
359*4882a593Smuzhiyun int id, i, ret;
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
362*4882a593Smuzhiyun if (!ddev)
363*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun id = ida_simple_get(&dfl_device_ida, 0, 0, GFP_KERNEL);
366*4882a593Smuzhiyun if (id < 0) {
367*4882a593Smuzhiyun dev_err(&pdev->dev, "unable to get id\n");
368*4882a593Smuzhiyun kfree(ddev);
369*4882a593Smuzhiyun return ERR_PTR(id);
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun /* freeing resources by put_device() after device_initialize() */
373*4882a593Smuzhiyun device_initialize(&ddev->dev);
374*4882a593Smuzhiyun ddev->dev.parent = &pdev->dev;
375*4882a593Smuzhiyun ddev->dev.bus = &dfl_bus_type;
376*4882a593Smuzhiyun ddev->dev.release = release_dfl_dev;
377*4882a593Smuzhiyun ddev->id = id;
378*4882a593Smuzhiyun ret = dev_set_name(&ddev->dev, "dfl_dev.%d", id);
379*4882a593Smuzhiyun if (ret)
380*4882a593Smuzhiyun goto put_dev;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun ddev->type = feature_dev_id_type(pdev);
383*4882a593Smuzhiyun ddev->feature_id = feature->id;
384*4882a593Smuzhiyun ddev->cdev = pdata->dfl_cdev;
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /* add mmio resource */
387*4882a593Smuzhiyun parent_res = &pdev->resource[feature->resource_index];
388*4882a593Smuzhiyun ddev->mmio_res.flags = IORESOURCE_MEM;
389*4882a593Smuzhiyun ddev->mmio_res.start = parent_res->start;
390*4882a593Smuzhiyun ddev->mmio_res.end = parent_res->end;
391*4882a593Smuzhiyun ddev->mmio_res.name = dev_name(&ddev->dev);
392*4882a593Smuzhiyun ret = insert_resource(parent_res, &ddev->mmio_res);
393*4882a593Smuzhiyun if (ret) {
394*4882a593Smuzhiyun dev_err(&pdev->dev, "%s failed to claim resource: %pR\n",
395*4882a593Smuzhiyun dev_name(&ddev->dev), &ddev->mmio_res);
396*4882a593Smuzhiyun goto put_dev;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun /* then add irq resource */
400*4882a593Smuzhiyun if (feature->nr_irqs) {
401*4882a593Smuzhiyun ddev->irqs = kcalloc(feature->nr_irqs,
402*4882a593Smuzhiyun sizeof(*ddev->irqs), GFP_KERNEL);
403*4882a593Smuzhiyun if (!ddev->irqs) {
404*4882a593Smuzhiyun ret = -ENOMEM;
405*4882a593Smuzhiyun goto put_dev;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun for (i = 0; i < feature->nr_irqs; i++)
409*4882a593Smuzhiyun ddev->irqs[i] = feature->irq_ctx[i].irq;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun ddev->num_irqs = feature->nr_irqs;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun ret = device_add(&ddev->dev);
415*4882a593Smuzhiyun if (ret)
416*4882a593Smuzhiyun goto put_dev;
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun dev_dbg(&pdev->dev, "add dfl_dev: %s\n", dev_name(&ddev->dev));
419*4882a593Smuzhiyun return ddev;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun put_dev:
422*4882a593Smuzhiyun /* calls release_dfl_dev() which does the clean up */
423*4882a593Smuzhiyun put_device(&ddev->dev);
424*4882a593Smuzhiyun return ERR_PTR(ret);
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
dfl_devs_remove(struct dfl_feature_platform_data * pdata)427*4882a593Smuzhiyun static void dfl_devs_remove(struct dfl_feature_platform_data *pdata)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun struct dfl_feature *feature;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun dfl_fpga_dev_for_each_feature(pdata, feature) {
432*4882a593Smuzhiyun if (feature->ddev) {
433*4882a593Smuzhiyun device_unregister(&feature->ddev->dev);
434*4882a593Smuzhiyun feature->ddev = NULL;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
dfl_devs_add(struct dfl_feature_platform_data * pdata)439*4882a593Smuzhiyun static int dfl_devs_add(struct dfl_feature_platform_data *pdata)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun struct dfl_feature *feature;
442*4882a593Smuzhiyun struct dfl_device *ddev;
443*4882a593Smuzhiyun int ret;
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun dfl_fpga_dev_for_each_feature(pdata, feature) {
446*4882a593Smuzhiyun if (feature->ioaddr)
447*4882a593Smuzhiyun continue;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun if (feature->ddev) {
450*4882a593Smuzhiyun ret = -EEXIST;
451*4882a593Smuzhiyun goto err;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun ddev = dfl_dev_add(pdata, feature);
455*4882a593Smuzhiyun if (IS_ERR(ddev)) {
456*4882a593Smuzhiyun ret = PTR_ERR(ddev);
457*4882a593Smuzhiyun goto err;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun feature->ddev = ddev;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun return 0;
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun err:
466*4882a593Smuzhiyun dfl_devs_remove(pdata);
467*4882a593Smuzhiyun return ret;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun
__dfl_driver_register(struct dfl_driver * dfl_drv,struct module * owner)470*4882a593Smuzhiyun int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun if (!dfl_drv || !dfl_drv->probe || !dfl_drv->id_table)
473*4882a593Smuzhiyun return -EINVAL;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun dfl_drv->drv.owner = owner;
476*4882a593Smuzhiyun dfl_drv->drv.bus = &dfl_bus_type;
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun return driver_register(&dfl_drv->drv);
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun EXPORT_SYMBOL(__dfl_driver_register);
481*4882a593Smuzhiyun
dfl_driver_unregister(struct dfl_driver * dfl_drv)482*4882a593Smuzhiyun void dfl_driver_unregister(struct dfl_driver *dfl_drv)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun driver_unregister(&dfl_drv->drv);
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun EXPORT_SYMBOL(dfl_driver_unregister);
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun #define is_header_feature(feature) ((feature)->id == FEATURE_ID_FIU_HEADER)
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun /**
491*4882a593Smuzhiyun * dfl_fpga_dev_feature_uinit - uinit for sub features of dfl feature device
492*4882a593Smuzhiyun * @pdev: feature device.
493*4882a593Smuzhiyun */
dfl_fpga_dev_feature_uinit(struct platform_device * pdev)494*4882a593Smuzhiyun void dfl_fpga_dev_feature_uinit(struct platform_device *pdev)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
497*4882a593Smuzhiyun struct dfl_feature *feature;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun dfl_devs_remove(pdata);
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun dfl_fpga_dev_for_each_feature(pdata, feature) {
502*4882a593Smuzhiyun if (feature->ops) {
503*4882a593Smuzhiyun if (feature->ops->uinit)
504*4882a593Smuzhiyun feature->ops->uinit(pdev, feature);
505*4882a593Smuzhiyun feature->ops = NULL;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_dev_feature_uinit);
510*4882a593Smuzhiyun
dfl_feature_instance_init(struct platform_device * pdev,struct dfl_feature_platform_data * pdata,struct dfl_feature * feature,struct dfl_feature_driver * drv)511*4882a593Smuzhiyun static int dfl_feature_instance_init(struct platform_device *pdev,
512*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata,
513*4882a593Smuzhiyun struct dfl_feature *feature,
514*4882a593Smuzhiyun struct dfl_feature_driver *drv)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun void __iomem *base;
517*4882a593Smuzhiyun int ret = 0;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun if (!is_header_feature(feature)) {
520*4882a593Smuzhiyun base = devm_platform_ioremap_resource(pdev,
521*4882a593Smuzhiyun feature->resource_index);
522*4882a593Smuzhiyun if (IS_ERR(base)) {
523*4882a593Smuzhiyun dev_err(&pdev->dev,
524*4882a593Smuzhiyun "ioremap failed for feature 0x%x!\n",
525*4882a593Smuzhiyun feature->id);
526*4882a593Smuzhiyun return PTR_ERR(base);
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun feature->ioaddr = base;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun if (drv->ops->init) {
533*4882a593Smuzhiyun ret = drv->ops->init(pdev, feature);
534*4882a593Smuzhiyun if (ret)
535*4882a593Smuzhiyun return ret;
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun feature->ops = drv->ops;
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun return ret;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun
dfl_feature_drv_match(struct dfl_feature * feature,struct dfl_feature_driver * driver)543*4882a593Smuzhiyun static bool dfl_feature_drv_match(struct dfl_feature *feature,
544*4882a593Smuzhiyun struct dfl_feature_driver *driver)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun const struct dfl_feature_id *ids = driver->id_table;
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun if (ids) {
549*4882a593Smuzhiyun while (ids->id) {
550*4882a593Smuzhiyun if (ids->id == feature->id)
551*4882a593Smuzhiyun return true;
552*4882a593Smuzhiyun ids++;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun return false;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun /**
559*4882a593Smuzhiyun * dfl_fpga_dev_feature_init - init for sub features of dfl feature device
560*4882a593Smuzhiyun * @pdev: feature device.
561*4882a593Smuzhiyun * @feature_drvs: drvs for sub features.
562*4882a593Smuzhiyun *
563*4882a593Smuzhiyun * This function will match sub features with given feature drvs list and
564*4882a593Smuzhiyun * use matched drv to init related sub feature.
565*4882a593Smuzhiyun *
566*4882a593Smuzhiyun * Return: 0 on success, negative error code otherwise.
567*4882a593Smuzhiyun */
dfl_fpga_dev_feature_init(struct platform_device * pdev,struct dfl_feature_driver * feature_drvs)568*4882a593Smuzhiyun int dfl_fpga_dev_feature_init(struct platform_device *pdev,
569*4882a593Smuzhiyun struct dfl_feature_driver *feature_drvs)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
572*4882a593Smuzhiyun struct dfl_feature_driver *drv = feature_drvs;
573*4882a593Smuzhiyun struct dfl_feature *feature;
574*4882a593Smuzhiyun int ret;
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun while (drv->ops) {
577*4882a593Smuzhiyun dfl_fpga_dev_for_each_feature(pdata, feature) {
578*4882a593Smuzhiyun if (dfl_feature_drv_match(feature, drv)) {
579*4882a593Smuzhiyun ret = dfl_feature_instance_init(pdev, pdata,
580*4882a593Smuzhiyun feature, drv);
581*4882a593Smuzhiyun if (ret)
582*4882a593Smuzhiyun goto exit;
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun drv++;
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun ret = dfl_devs_add(pdata);
589*4882a593Smuzhiyun if (ret)
590*4882a593Smuzhiyun goto exit;
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun return 0;
593*4882a593Smuzhiyun exit:
594*4882a593Smuzhiyun dfl_fpga_dev_feature_uinit(pdev);
595*4882a593Smuzhiyun return ret;
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_dev_feature_init);
598*4882a593Smuzhiyun
dfl_chardev_uinit(void)599*4882a593Smuzhiyun static void dfl_chardev_uinit(void)
600*4882a593Smuzhiyun {
601*4882a593Smuzhiyun int i;
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun for (i = 0; i < DFL_FPGA_DEVT_MAX; i++)
604*4882a593Smuzhiyun if (MAJOR(dfl_chrdevs[i].devt)) {
605*4882a593Smuzhiyun unregister_chrdev_region(dfl_chrdevs[i].devt,
606*4882a593Smuzhiyun MINORMASK + 1);
607*4882a593Smuzhiyun dfl_chrdevs[i].devt = MKDEV(0, 0);
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun
dfl_chardev_init(void)611*4882a593Smuzhiyun static int dfl_chardev_init(void)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun int i, ret;
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun for (i = 0; i < DFL_FPGA_DEVT_MAX; i++) {
616*4882a593Smuzhiyun ret = alloc_chrdev_region(&dfl_chrdevs[i].devt, 0,
617*4882a593Smuzhiyun MINORMASK + 1, dfl_chrdevs[i].name);
618*4882a593Smuzhiyun if (ret)
619*4882a593Smuzhiyun goto exit;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun return 0;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun exit:
625*4882a593Smuzhiyun dfl_chardev_uinit();
626*4882a593Smuzhiyun return ret;
627*4882a593Smuzhiyun }
628*4882a593Smuzhiyun
dfl_get_devt(enum dfl_fpga_devt_type type,int id)629*4882a593Smuzhiyun static dev_t dfl_get_devt(enum dfl_fpga_devt_type type, int id)
630*4882a593Smuzhiyun {
631*4882a593Smuzhiyun if (type >= DFL_FPGA_DEVT_MAX)
632*4882a593Smuzhiyun return 0;
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun return MKDEV(MAJOR(dfl_chrdevs[type].devt), id);
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun /**
638*4882a593Smuzhiyun * dfl_fpga_dev_ops_register - register cdev ops for feature dev
639*4882a593Smuzhiyun *
640*4882a593Smuzhiyun * @pdev: feature dev.
641*4882a593Smuzhiyun * @fops: file operations for feature dev's cdev.
642*4882a593Smuzhiyun * @owner: owning module/driver.
643*4882a593Smuzhiyun *
644*4882a593Smuzhiyun * Return: 0 on success, negative error code otherwise.
645*4882a593Smuzhiyun */
dfl_fpga_dev_ops_register(struct platform_device * pdev,const struct file_operations * fops,struct module * owner)646*4882a593Smuzhiyun int dfl_fpga_dev_ops_register(struct platform_device *pdev,
647*4882a593Smuzhiyun const struct file_operations *fops,
648*4882a593Smuzhiyun struct module *owner)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun cdev_init(&pdata->cdev, fops);
653*4882a593Smuzhiyun pdata->cdev.owner = owner;
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun /*
656*4882a593Smuzhiyun * set parent to the feature device so that its refcount is
657*4882a593Smuzhiyun * decreased after the last refcount of cdev is gone, that
658*4882a593Smuzhiyun * makes sure the feature device is valid during device
659*4882a593Smuzhiyun * file's life-cycle.
660*4882a593Smuzhiyun */
661*4882a593Smuzhiyun pdata->cdev.kobj.parent = &pdev->dev.kobj;
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun return cdev_add(&pdata->cdev, pdev->dev.devt, 1);
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_register);
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun /**
668*4882a593Smuzhiyun * dfl_fpga_dev_ops_unregister - unregister cdev ops for feature dev
669*4882a593Smuzhiyun * @pdev: feature dev.
670*4882a593Smuzhiyun */
dfl_fpga_dev_ops_unregister(struct platform_device * pdev)671*4882a593Smuzhiyun void dfl_fpga_dev_ops_unregister(struct platform_device *pdev)
672*4882a593Smuzhiyun {
673*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun cdev_del(&pdata->cdev);
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_unregister);
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun /**
680*4882a593Smuzhiyun * struct build_feature_devs_info - info collected during feature dev build.
681*4882a593Smuzhiyun *
682*4882a593Smuzhiyun * @dev: device to enumerate.
683*4882a593Smuzhiyun * @cdev: the container device for all feature devices.
684*4882a593Smuzhiyun * @nr_irqs: number of irqs for all feature devices.
685*4882a593Smuzhiyun * @irq_table: Linux IRQ numbers for all irqs, indexed by local irq index of
686*4882a593Smuzhiyun * this device.
687*4882a593Smuzhiyun * @feature_dev: current feature device.
688*4882a593Smuzhiyun * @ioaddr: header register region address of current FIU in enumeration.
689*4882a593Smuzhiyun * @start: register resource start of current FIU.
690*4882a593Smuzhiyun * @len: max register resource length of current FIU.
691*4882a593Smuzhiyun * @sub_features: a sub features linked list for feature device in enumeration.
692*4882a593Smuzhiyun * @feature_num: number of sub features for feature device in enumeration.
693*4882a593Smuzhiyun */
694*4882a593Smuzhiyun struct build_feature_devs_info {
695*4882a593Smuzhiyun struct device *dev;
696*4882a593Smuzhiyun struct dfl_fpga_cdev *cdev;
697*4882a593Smuzhiyun unsigned int nr_irqs;
698*4882a593Smuzhiyun int *irq_table;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun struct platform_device *feature_dev;
701*4882a593Smuzhiyun void __iomem *ioaddr;
702*4882a593Smuzhiyun resource_size_t start;
703*4882a593Smuzhiyun resource_size_t len;
704*4882a593Smuzhiyun struct list_head sub_features;
705*4882a593Smuzhiyun int feature_num;
706*4882a593Smuzhiyun };
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun /**
709*4882a593Smuzhiyun * struct dfl_feature_info - sub feature info collected during feature dev build
710*4882a593Smuzhiyun *
711*4882a593Smuzhiyun * @fid: id of this sub feature.
712*4882a593Smuzhiyun * @mmio_res: mmio resource of this sub feature.
713*4882a593Smuzhiyun * @ioaddr: mapped base address of mmio resource.
714*4882a593Smuzhiyun * @node: node in sub_features linked list.
715*4882a593Smuzhiyun * @irq_base: start of irq index in this sub feature.
716*4882a593Smuzhiyun * @nr_irqs: number of irqs of this sub feature.
717*4882a593Smuzhiyun */
718*4882a593Smuzhiyun struct dfl_feature_info {
719*4882a593Smuzhiyun u16 fid;
720*4882a593Smuzhiyun struct resource mmio_res;
721*4882a593Smuzhiyun void __iomem *ioaddr;
722*4882a593Smuzhiyun struct list_head node;
723*4882a593Smuzhiyun unsigned int irq_base;
724*4882a593Smuzhiyun unsigned int nr_irqs;
725*4882a593Smuzhiyun };
726*4882a593Smuzhiyun
dfl_fpga_cdev_add_port_dev(struct dfl_fpga_cdev * cdev,struct platform_device * port)727*4882a593Smuzhiyun static void dfl_fpga_cdev_add_port_dev(struct dfl_fpga_cdev *cdev,
728*4882a593Smuzhiyun struct platform_device *port)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata = dev_get_platdata(&port->dev);
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun mutex_lock(&cdev->lock);
733*4882a593Smuzhiyun list_add(&pdata->node, &cdev->port_dev_list);
734*4882a593Smuzhiyun get_device(&pdata->dev->dev);
735*4882a593Smuzhiyun mutex_unlock(&cdev->lock);
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun /*
739*4882a593Smuzhiyun * register current feature device, it is called when we need to switch to
740*4882a593Smuzhiyun * another feature parsing or we have parsed all features on given device
741*4882a593Smuzhiyun * feature list.
742*4882a593Smuzhiyun */
build_info_commit_dev(struct build_feature_devs_info * binfo)743*4882a593Smuzhiyun static int build_info_commit_dev(struct build_feature_devs_info *binfo)
744*4882a593Smuzhiyun {
745*4882a593Smuzhiyun struct platform_device *fdev = binfo->feature_dev;
746*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata;
747*4882a593Smuzhiyun struct dfl_feature_info *finfo, *p;
748*4882a593Smuzhiyun enum dfl_id_type type;
749*4882a593Smuzhiyun int ret, index = 0, res_idx = 0;
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun type = feature_dev_id_type(fdev);
752*4882a593Smuzhiyun if (WARN_ON_ONCE(type >= DFL_ID_MAX))
753*4882a593Smuzhiyun return -EINVAL;
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun /*
756*4882a593Smuzhiyun * we do not need to care for the memory which is associated with
757*4882a593Smuzhiyun * the platform device. After calling platform_device_unregister(),
758*4882a593Smuzhiyun * it will be automatically freed by device's release() callback,
759*4882a593Smuzhiyun * platform_device_release().
760*4882a593Smuzhiyun */
761*4882a593Smuzhiyun pdata = kzalloc(struct_size(pdata, features, binfo->feature_num), GFP_KERNEL);
762*4882a593Smuzhiyun if (!pdata)
763*4882a593Smuzhiyun return -ENOMEM;
764*4882a593Smuzhiyun
765*4882a593Smuzhiyun pdata->dev = fdev;
766*4882a593Smuzhiyun pdata->num = binfo->feature_num;
767*4882a593Smuzhiyun pdata->dfl_cdev = binfo->cdev;
768*4882a593Smuzhiyun pdata->id = FEATURE_DEV_ID_UNUSED;
769*4882a593Smuzhiyun mutex_init(&pdata->lock);
770*4882a593Smuzhiyun lockdep_set_class_and_name(&pdata->lock, &dfl_pdata_keys[type],
771*4882a593Smuzhiyun dfl_pdata_key_strings[type]);
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun /*
774*4882a593Smuzhiyun * the count should be initialized to 0 to make sure
775*4882a593Smuzhiyun *__fpga_port_enable() following __fpga_port_disable()
776*4882a593Smuzhiyun * works properly for port device.
777*4882a593Smuzhiyun * and it should always be 0 for fme device.
778*4882a593Smuzhiyun */
779*4882a593Smuzhiyun WARN_ON(pdata->disable_count);
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun fdev->dev.platform_data = pdata;
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun /* each sub feature has one MMIO resource */
784*4882a593Smuzhiyun fdev->num_resources = binfo->feature_num;
785*4882a593Smuzhiyun fdev->resource = kcalloc(binfo->feature_num, sizeof(*fdev->resource),
786*4882a593Smuzhiyun GFP_KERNEL);
787*4882a593Smuzhiyun if (!fdev->resource)
788*4882a593Smuzhiyun return -ENOMEM;
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun /* fill features and resource information for feature dev */
791*4882a593Smuzhiyun list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
792*4882a593Smuzhiyun struct dfl_feature *feature = &pdata->features[index++];
793*4882a593Smuzhiyun struct dfl_feature_irq_ctx *ctx;
794*4882a593Smuzhiyun unsigned int i;
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun /* save resource information for each feature */
797*4882a593Smuzhiyun feature->dev = fdev;
798*4882a593Smuzhiyun feature->id = finfo->fid;
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun /*
801*4882a593Smuzhiyun * the FIU header feature has some fundamental functions (sriov
802*4882a593Smuzhiyun * set, port enable/disable) needed for the dfl bus device and
803*4882a593Smuzhiyun * other sub features. So its mmio resource should be mapped by
804*4882a593Smuzhiyun * DFL bus device. And we should not assign it to feature
805*4882a593Smuzhiyun * devices (dfl-fme/afu) again.
806*4882a593Smuzhiyun */
807*4882a593Smuzhiyun if (is_header_feature(feature)) {
808*4882a593Smuzhiyun feature->resource_index = -1;
809*4882a593Smuzhiyun feature->ioaddr =
810*4882a593Smuzhiyun devm_ioremap_resource(binfo->dev,
811*4882a593Smuzhiyun &finfo->mmio_res);
812*4882a593Smuzhiyun if (IS_ERR(feature->ioaddr))
813*4882a593Smuzhiyun return PTR_ERR(feature->ioaddr);
814*4882a593Smuzhiyun } else {
815*4882a593Smuzhiyun feature->resource_index = res_idx;
816*4882a593Smuzhiyun fdev->resource[res_idx++] = finfo->mmio_res;
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun if (finfo->nr_irqs) {
820*4882a593Smuzhiyun ctx = devm_kcalloc(binfo->dev, finfo->nr_irqs,
821*4882a593Smuzhiyun sizeof(*ctx), GFP_KERNEL);
822*4882a593Smuzhiyun if (!ctx)
823*4882a593Smuzhiyun return -ENOMEM;
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun for (i = 0; i < finfo->nr_irqs; i++)
826*4882a593Smuzhiyun ctx[i].irq =
827*4882a593Smuzhiyun binfo->irq_table[finfo->irq_base + i];
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun feature->irq_ctx = ctx;
830*4882a593Smuzhiyun feature->nr_irqs = finfo->nr_irqs;
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun list_del(&finfo->node);
834*4882a593Smuzhiyun kfree(finfo);
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun ret = platform_device_add(binfo->feature_dev);
838*4882a593Smuzhiyun if (!ret) {
839*4882a593Smuzhiyun if (type == PORT_ID)
840*4882a593Smuzhiyun dfl_fpga_cdev_add_port_dev(binfo->cdev,
841*4882a593Smuzhiyun binfo->feature_dev);
842*4882a593Smuzhiyun else
843*4882a593Smuzhiyun binfo->cdev->fme_dev =
844*4882a593Smuzhiyun get_device(&binfo->feature_dev->dev);
845*4882a593Smuzhiyun /*
846*4882a593Smuzhiyun * reset it to avoid build_info_free() freeing their resource.
847*4882a593Smuzhiyun *
848*4882a593Smuzhiyun * The resource of successfully registered feature devices
849*4882a593Smuzhiyun * will be freed by platform_device_unregister(). See the
850*4882a593Smuzhiyun * comments in build_info_create_dev().
851*4882a593Smuzhiyun */
852*4882a593Smuzhiyun binfo->feature_dev = NULL;
853*4882a593Smuzhiyun }
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun return ret;
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun static int
build_info_create_dev(struct build_feature_devs_info * binfo,enum dfl_id_type type)859*4882a593Smuzhiyun build_info_create_dev(struct build_feature_devs_info *binfo,
860*4882a593Smuzhiyun enum dfl_id_type type)
861*4882a593Smuzhiyun {
862*4882a593Smuzhiyun struct platform_device *fdev;
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun if (type >= DFL_ID_MAX)
865*4882a593Smuzhiyun return -EINVAL;
866*4882a593Smuzhiyun
867*4882a593Smuzhiyun /*
868*4882a593Smuzhiyun * we use -ENODEV as the initialization indicator which indicates
869*4882a593Smuzhiyun * whether the id need to be reclaimed
870*4882a593Smuzhiyun */
871*4882a593Smuzhiyun fdev = platform_device_alloc(dfl_devs[type].name, -ENODEV);
872*4882a593Smuzhiyun if (!fdev)
873*4882a593Smuzhiyun return -ENOMEM;
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun binfo->feature_dev = fdev;
876*4882a593Smuzhiyun binfo->feature_num = 0;
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun INIT_LIST_HEAD(&binfo->sub_features);
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun fdev->id = dfl_id_alloc(type, &fdev->dev);
881*4882a593Smuzhiyun if (fdev->id < 0)
882*4882a593Smuzhiyun return fdev->id;
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun fdev->dev.parent = &binfo->cdev->region->dev;
885*4882a593Smuzhiyun fdev->dev.devt = dfl_get_devt(dfl_devs[type].devt_type, fdev->id);
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun return 0;
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun
build_info_free(struct build_feature_devs_info * binfo)890*4882a593Smuzhiyun static void build_info_free(struct build_feature_devs_info *binfo)
891*4882a593Smuzhiyun {
892*4882a593Smuzhiyun struct dfl_feature_info *finfo, *p;
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun /*
895*4882a593Smuzhiyun * it is a valid id, free it. See comments in
896*4882a593Smuzhiyun * build_info_create_dev()
897*4882a593Smuzhiyun */
898*4882a593Smuzhiyun if (binfo->feature_dev && binfo->feature_dev->id >= 0) {
899*4882a593Smuzhiyun dfl_id_free(feature_dev_id_type(binfo->feature_dev),
900*4882a593Smuzhiyun binfo->feature_dev->id);
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
903*4882a593Smuzhiyun list_del(&finfo->node);
904*4882a593Smuzhiyun kfree(finfo);
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun }
907*4882a593Smuzhiyun
908*4882a593Smuzhiyun platform_device_put(binfo->feature_dev);
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun devm_kfree(binfo->dev, binfo);
911*4882a593Smuzhiyun }
912*4882a593Smuzhiyun
feature_size(void __iomem * start)913*4882a593Smuzhiyun static inline u32 feature_size(void __iomem *start)
914*4882a593Smuzhiyun {
915*4882a593Smuzhiyun u64 v = readq(start + DFH);
916*4882a593Smuzhiyun u32 ofst = FIELD_GET(DFH_NEXT_HDR_OFST, v);
917*4882a593Smuzhiyun /* workaround for private features with invalid size, use 4K instead */
918*4882a593Smuzhiyun return ofst ? ofst : 4096;
919*4882a593Smuzhiyun }
920*4882a593Smuzhiyun
feature_id(void __iomem * start)921*4882a593Smuzhiyun static u16 feature_id(void __iomem *start)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun u64 v = readq(start + DFH);
924*4882a593Smuzhiyun u16 id = FIELD_GET(DFH_ID, v);
925*4882a593Smuzhiyun u8 type = FIELD_GET(DFH_TYPE, v);
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun if (type == DFH_TYPE_FIU)
928*4882a593Smuzhiyun return FEATURE_ID_FIU_HEADER;
929*4882a593Smuzhiyun else if (type == DFH_TYPE_PRIVATE)
930*4882a593Smuzhiyun return id;
931*4882a593Smuzhiyun else if (type == DFH_TYPE_AFU)
932*4882a593Smuzhiyun return FEATURE_ID_AFU;
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun WARN_ON(1);
935*4882a593Smuzhiyun return 0;
936*4882a593Smuzhiyun }
937*4882a593Smuzhiyun
parse_feature_irqs(struct build_feature_devs_info * binfo,resource_size_t ofst,u16 fid,unsigned int * irq_base,unsigned int * nr_irqs)938*4882a593Smuzhiyun static int parse_feature_irqs(struct build_feature_devs_info *binfo,
939*4882a593Smuzhiyun resource_size_t ofst, u16 fid,
940*4882a593Smuzhiyun unsigned int *irq_base, unsigned int *nr_irqs)
941*4882a593Smuzhiyun {
942*4882a593Smuzhiyun void __iomem *base = binfo->ioaddr + ofst;
943*4882a593Smuzhiyun unsigned int i, ibase, inr = 0;
944*4882a593Smuzhiyun int virq;
945*4882a593Smuzhiyun u64 v;
946*4882a593Smuzhiyun
947*4882a593Smuzhiyun /*
948*4882a593Smuzhiyun * Ideally DFL framework should only read info from DFL header, but
949*4882a593Smuzhiyun * current version DFL only provides mmio resources information for
950*4882a593Smuzhiyun * each feature in DFL Header, no field for interrupt resources.
951*4882a593Smuzhiyun * Interrupt resource information is provided by specific mmio
952*4882a593Smuzhiyun * registers of each private feature which supports interrupt. So in
953*4882a593Smuzhiyun * order to parse and assign irq resources, DFL framework has to look
954*4882a593Smuzhiyun * into specific capability registers of these private features.
955*4882a593Smuzhiyun *
956*4882a593Smuzhiyun * Once future DFL version supports generic interrupt resource
957*4882a593Smuzhiyun * information in common DFL headers, the generic interrupt parsing
958*4882a593Smuzhiyun * code will be added. But in order to be compatible to old version
959*4882a593Smuzhiyun * DFL, the driver may still fall back to these quirks.
960*4882a593Smuzhiyun */
961*4882a593Smuzhiyun switch (fid) {
962*4882a593Smuzhiyun case PORT_FEATURE_ID_UINT:
963*4882a593Smuzhiyun v = readq(base + PORT_UINT_CAP);
964*4882a593Smuzhiyun ibase = FIELD_GET(PORT_UINT_CAP_FST_VECT, v);
965*4882a593Smuzhiyun inr = FIELD_GET(PORT_UINT_CAP_INT_NUM, v);
966*4882a593Smuzhiyun break;
967*4882a593Smuzhiyun case PORT_FEATURE_ID_ERROR:
968*4882a593Smuzhiyun v = readq(base + PORT_ERROR_CAP);
969*4882a593Smuzhiyun ibase = FIELD_GET(PORT_ERROR_CAP_INT_VECT, v);
970*4882a593Smuzhiyun inr = FIELD_GET(PORT_ERROR_CAP_SUPP_INT, v);
971*4882a593Smuzhiyun break;
972*4882a593Smuzhiyun case FME_FEATURE_ID_GLOBAL_ERR:
973*4882a593Smuzhiyun v = readq(base + FME_ERROR_CAP);
974*4882a593Smuzhiyun ibase = FIELD_GET(FME_ERROR_CAP_INT_VECT, v);
975*4882a593Smuzhiyun inr = FIELD_GET(FME_ERROR_CAP_SUPP_INT, v);
976*4882a593Smuzhiyun break;
977*4882a593Smuzhiyun }
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun if (!inr) {
980*4882a593Smuzhiyun *irq_base = 0;
981*4882a593Smuzhiyun *nr_irqs = 0;
982*4882a593Smuzhiyun return 0;
983*4882a593Smuzhiyun }
984*4882a593Smuzhiyun
985*4882a593Smuzhiyun dev_dbg(binfo->dev, "feature: 0x%x, irq_base: %u, nr_irqs: %u\n",
986*4882a593Smuzhiyun fid, ibase, inr);
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun if (ibase + inr > binfo->nr_irqs) {
989*4882a593Smuzhiyun dev_err(binfo->dev,
990*4882a593Smuzhiyun "Invalid interrupt number in feature 0x%x\n", fid);
991*4882a593Smuzhiyun return -EINVAL;
992*4882a593Smuzhiyun }
993*4882a593Smuzhiyun
994*4882a593Smuzhiyun for (i = 0; i < inr; i++) {
995*4882a593Smuzhiyun virq = binfo->irq_table[ibase + i];
996*4882a593Smuzhiyun if (virq < 0 || virq > NR_IRQS) {
997*4882a593Smuzhiyun dev_err(binfo->dev,
998*4882a593Smuzhiyun "Invalid irq table entry for feature 0x%x\n",
999*4882a593Smuzhiyun fid);
1000*4882a593Smuzhiyun return -EINVAL;
1001*4882a593Smuzhiyun }
1002*4882a593Smuzhiyun }
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun *irq_base = ibase;
1005*4882a593Smuzhiyun *nr_irqs = inr;
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun return 0;
1008*4882a593Smuzhiyun }
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun /*
1011*4882a593Smuzhiyun * when create sub feature instances, for private features, it doesn't need
1012*4882a593Smuzhiyun * to provide resource size and feature id as they could be read from DFH
1013*4882a593Smuzhiyun * register. For afu sub feature, its register region only contains user
1014*4882a593Smuzhiyun * defined registers, so never trust any information from it, just use the
1015*4882a593Smuzhiyun * resource size information provided by its parent FIU.
1016*4882a593Smuzhiyun */
1017*4882a593Smuzhiyun static int
create_feature_instance(struct build_feature_devs_info * binfo,resource_size_t ofst,resource_size_t size,u16 fid)1018*4882a593Smuzhiyun create_feature_instance(struct build_feature_devs_info *binfo,
1019*4882a593Smuzhiyun resource_size_t ofst, resource_size_t size, u16 fid)
1020*4882a593Smuzhiyun {
1021*4882a593Smuzhiyun unsigned int irq_base, nr_irqs;
1022*4882a593Smuzhiyun struct dfl_feature_info *finfo;
1023*4882a593Smuzhiyun int ret;
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun /* read feature size and id if inputs are invalid */
1026*4882a593Smuzhiyun size = size ? size : feature_size(binfo->ioaddr + ofst);
1027*4882a593Smuzhiyun fid = fid ? fid : feature_id(binfo->ioaddr + ofst);
1028*4882a593Smuzhiyun
1029*4882a593Smuzhiyun if (binfo->len - ofst < size)
1030*4882a593Smuzhiyun return -EINVAL;
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun ret = parse_feature_irqs(binfo, ofst, fid, &irq_base, &nr_irqs);
1033*4882a593Smuzhiyun if (ret)
1034*4882a593Smuzhiyun return ret;
1035*4882a593Smuzhiyun
1036*4882a593Smuzhiyun finfo = kzalloc(sizeof(*finfo), GFP_KERNEL);
1037*4882a593Smuzhiyun if (!finfo)
1038*4882a593Smuzhiyun return -ENOMEM;
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun finfo->fid = fid;
1041*4882a593Smuzhiyun finfo->mmio_res.start = binfo->start + ofst;
1042*4882a593Smuzhiyun finfo->mmio_res.end = finfo->mmio_res.start + size - 1;
1043*4882a593Smuzhiyun finfo->mmio_res.flags = IORESOURCE_MEM;
1044*4882a593Smuzhiyun finfo->irq_base = irq_base;
1045*4882a593Smuzhiyun finfo->nr_irqs = nr_irqs;
1046*4882a593Smuzhiyun
1047*4882a593Smuzhiyun list_add_tail(&finfo->node, &binfo->sub_features);
1048*4882a593Smuzhiyun binfo->feature_num++;
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun return 0;
1051*4882a593Smuzhiyun }
1052*4882a593Smuzhiyun
parse_feature_port_afu(struct build_feature_devs_info * binfo,resource_size_t ofst)1053*4882a593Smuzhiyun static int parse_feature_port_afu(struct build_feature_devs_info *binfo,
1054*4882a593Smuzhiyun resource_size_t ofst)
1055*4882a593Smuzhiyun {
1056*4882a593Smuzhiyun u64 v = readq(binfo->ioaddr + PORT_HDR_CAP);
1057*4882a593Smuzhiyun u32 size = FIELD_GET(PORT_CAP_MMIO_SIZE, v) << 10;
1058*4882a593Smuzhiyun
1059*4882a593Smuzhiyun WARN_ON(!size);
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun return create_feature_instance(binfo, ofst, size, FEATURE_ID_AFU);
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun #define is_feature_dev_detected(binfo) (!!(binfo)->feature_dev)
1065*4882a593Smuzhiyun
parse_feature_afu(struct build_feature_devs_info * binfo,resource_size_t ofst)1066*4882a593Smuzhiyun static int parse_feature_afu(struct build_feature_devs_info *binfo,
1067*4882a593Smuzhiyun resource_size_t ofst)
1068*4882a593Smuzhiyun {
1069*4882a593Smuzhiyun if (!is_feature_dev_detected(binfo)) {
1070*4882a593Smuzhiyun dev_err(binfo->dev, "this AFU does not belong to any FIU.\n");
1071*4882a593Smuzhiyun return -EINVAL;
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun switch (feature_dev_id_type(binfo->feature_dev)) {
1075*4882a593Smuzhiyun case PORT_ID:
1076*4882a593Smuzhiyun return parse_feature_port_afu(binfo, ofst);
1077*4882a593Smuzhiyun default:
1078*4882a593Smuzhiyun dev_info(binfo->dev, "AFU belonging to FIU %s is not supported yet.\n",
1079*4882a593Smuzhiyun binfo->feature_dev->name);
1080*4882a593Smuzhiyun }
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun return 0;
1083*4882a593Smuzhiyun }
1084*4882a593Smuzhiyun
build_info_prepare(struct build_feature_devs_info * binfo,resource_size_t start,resource_size_t len)1085*4882a593Smuzhiyun static int build_info_prepare(struct build_feature_devs_info *binfo,
1086*4882a593Smuzhiyun resource_size_t start, resource_size_t len)
1087*4882a593Smuzhiyun {
1088*4882a593Smuzhiyun struct device *dev = binfo->dev;
1089*4882a593Smuzhiyun void __iomem *ioaddr;
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun if (!devm_request_mem_region(dev, start, len, dev_name(dev))) {
1092*4882a593Smuzhiyun dev_err(dev, "request region fail, start:%pa, len:%pa\n",
1093*4882a593Smuzhiyun &start, &len);
1094*4882a593Smuzhiyun return -EBUSY;
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun ioaddr = devm_ioremap(dev, start, len);
1098*4882a593Smuzhiyun if (!ioaddr) {
1099*4882a593Smuzhiyun dev_err(dev, "ioremap region fail, start:%pa, len:%pa\n",
1100*4882a593Smuzhiyun &start, &len);
1101*4882a593Smuzhiyun return -ENOMEM;
1102*4882a593Smuzhiyun }
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun binfo->start = start;
1105*4882a593Smuzhiyun binfo->len = len;
1106*4882a593Smuzhiyun binfo->ioaddr = ioaddr;
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun return 0;
1109*4882a593Smuzhiyun }
1110*4882a593Smuzhiyun
build_info_complete(struct build_feature_devs_info * binfo)1111*4882a593Smuzhiyun static void build_info_complete(struct build_feature_devs_info *binfo)
1112*4882a593Smuzhiyun {
1113*4882a593Smuzhiyun devm_iounmap(binfo->dev, binfo->ioaddr);
1114*4882a593Smuzhiyun devm_release_mem_region(binfo->dev, binfo->start, binfo->len);
1115*4882a593Smuzhiyun }
1116*4882a593Smuzhiyun
parse_feature_fiu(struct build_feature_devs_info * binfo,resource_size_t ofst)1117*4882a593Smuzhiyun static int parse_feature_fiu(struct build_feature_devs_info *binfo,
1118*4882a593Smuzhiyun resource_size_t ofst)
1119*4882a593Smuzhiyun {
1120*4882a593Smuzhiyun int ret = 0;
1121*4882a593Smuzhiyun u32 offset;
1122*4882a593Smuzhiyun u16 id;
1123*4882a593Smuzhiyun u64 v;
1124*4882a593Smuzhiyun
1125*4882a593Smuzhiyun if (is_feature_dev_detected(binfo)) {
1126*4882a593Smuzhiyun build_info_complete(binfo);
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun ret = build_info_commit_dev(binfo);
1129*4882a593Smuzhiyun if (ret)
1130*4882a593Smuzhiyun return ret;
1131*4882a593Smuzhiyun
1132*4882a593Smuzhiyun ret = build_info_prepare(binfo, binfo->start + ofst,
1133*4882a593Smuzhiyun binfo->len - ofst);
1134*4882a593Smuzhiyun if (ret)
1135*4882a593Smuzhiyun return ret;
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun v = readq(binfo->ioaddr + DFH);
1139*4882a593Smuzhiyun id = FIELD_GET(DFH_ID, v);
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun /* create platform device for dfl feature dev */
1142*4882a593Smuzhiyun ret = build_info_create_dev(binfo, dfh_id_to_type(id));
1143*4882a593Smuzhiyun if (ret)
1144*4882a593Smuzhiyun return ret;
1145*4882a593Smuzhiyun
1146*4882a593Smuzhiyun ret = create_feature_instance(binfo, 0, 0, 0);
1147*4882a593Smuzhiyun if (ret)
1148*4882a593Smuzhiyun return ret;
1149*4882a593Smuzhiyun /*
1150*4882a593Smuzhiyun * find and parse FIU's child AFU via its NEXT_AFU register.
1151*4882a593Smuzhiyun * please note that only Port has valid NEXT_AFU pointer per spec.
1152*4882a593Smuzhiyun */
1153*4882a593Smuzhiyun v = readq(binfo->ioaddr + NEXT_AFU);
1154*4882a593Smuzhiyun
1155*4882a593Smuzhiyun offset = FIELD_GET(NEXT_AFU_NEXT_DFH_OFST, v);
1156*4882a593Smuzhiyun if (offset)
1157*4882a593Smuzhiyun return parse_feature_afu(binfo, offset);
1158*4882a593Smuzhiyun
1159*4882a593Smuzhiyun dev_dbg(binfo->dev, "No AFUs detected on FIU %d\n", id);
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun return ret;
1162*4882a593Smuzhiyun }
1163*4882a593Smuzhiyun
parse_feature_private(struct build_feature_devs_info * binfo,resource_size_t ofst)1164*4882a593Smuzhiyun static int parse_feature_private(struct build_feature_devs_info *binfo,
1165*4882a593Smuzhiyun resource_size_t ofst)
1166*4882a593Smuzhiyun {
1167*4882a593Smuzhiyun if (!is_feature_dev_detected(binfo)) {
1168*4882a593Smuzhiyun dev_err(binfo->dev, "the private feature 0x%x does not belong to any AFU.\n",
1169*4882a593Smuzhiyun feature_id(binfo->ioaddr + ofst));
1170*4882a593Smuzhiyun return -EINVAL;
1171*4882a593Smuzhiyun }
1172*4882a593Smuzhiyun
1173*4882a593Smuzhiyun return create_feature_instance(binfo, ofst, 0, 0);
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun /**
1177*4882a593Smuzhiyun * parse_feature - parse a feature on given device feature list
1178*4882a593Smuzhiyun *
1179*4882a593Smuzhiyun * @binfo: build feature devices information.
1180*4882a593Smuzhiyun * @ofst: offset to current FIU header
1181*4882a593Smuzhiyun */
parse_feature(struct build_feature_devs_info * binfo,resource_size_t ofst)1182*4882a593Smuzhiyun static int parse_feature(struct build_feature_devs_info *binfo,
1183*4882a593Smuzhiyun resource_size_t ofst)
1184*4882a593Smuzhiyun {
1185*4882a593Smuzhiyun u64 v;
1186*4882a593Smuzhiyun u32 type;
1187*4882a593Smuzhiyun
1188*4882a593Smuzhiyun v = readq(binfo->ioaddr + ofst + DFH);
1189*4882a593Smuzhiyun type = FIELD_GET(DFH_TYPE, v);
1190*4882a593Smuzhiyun
1191*4882a593Smuzhiyun switch (type) {
1192*4882a593Smuzhiyun case DFH_TYPE_AFU:
1193*4882a593Smuzhiyun return parse_feature_afu(binfo, ofst);
1194*4882a593Smuzhiyun case DFH_TYPE_PRIVATE:
1195*4882a593Smuzhiyun return parse_feature_private(binfo, ofst);
1196*4882a593Smuzhiyun case DFH_TYPE_FIU:
1197*4882a593Smuzhiyun return parse_feature_fiu(binfo, ofst);
1198*4882a593Smuzhiyun default:
1199*4882a593Smuzhiyun dev_info(binfo->dev,
1200*4882a593Smuzhiyun "Feature Type %x is not supported.\n", type);
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun return 0;
1204*4882a593Smuzhiyun }
1205*4882a593Smuzhiyun
parse_feature_list(struct build_feature_devs_info * binfo,resource_size_t start,resource_size_t len)1206*4882a593Smuzhiyun static int parse_feature_list(struct build_feature_devs_info *binfo,
1207*4882a593Smuzhiyun resource_size_t start, resource_size_t len)
1208*4882a593Smuzhiyun {
1209*4882a593Smuzhiyun resource_size_t end = start + len;
1210*4882a593Smuzhiyun int ret = 0;
1211*4882a593Smuzhiyun u32 ofst = 0;
1212*4882a593Smuzhiyun u64 v;
1213*4882a593Smuzhiyun
1214*4882a593Smuzhiyun ret = build_info_prepare(binfo, start, len);
1215*4882a593Smuzhiyun if (ret)
1216*4882a593Smuzhiyun return ret;
1217*4882a593Smuzhiyun
1218*4882a593Smuzhiyun /* walk through the device feature list via DFH's next DFH pointer. */
1219*4882a593Smuzhiyun for (; start < end; start += ofst) {
1220*4882a593Smuzhiyun if (end - start < DFH_SIZE) {
1221*4882a593Smuzhiyun dev_err(binfo->dev, "The region is too small to contain a feature.\n");
1222*4882a593Smuzhiyun return -EINVAL;
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun
1225*4882a593Smuzhiyun ret = parse_feature(binfo, start - binfo->start);
1226*4882a593Smuzhiyun if (ret)
1227*4882a593Smuzhiyun return ret;
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun v = readq(binfo->ioaddr + start - binfo->start + DFH);
1230*4882a593Smuzhiyun ofst = FIELD_GET(DFH_NEXT_HDR_OFST, v);
1231*4882a593Smuzhiyun
1232*4882a593Smuzhiyun /* stop parsing if EOL(End of List) is set or offset is 0 */
1233*4882a593Smuzhiyun if ((v & DFH_EOL) || !ofst)
1234*4882a593Smuzhiyun break;
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun
1237*4882a593Smuzhiyun /* commit current feature device when reach the end of list */
1238*4882a593Smuzhiyun build_info_complete(binfo);
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun if (is_feature_dev_detected(binfo))
1241*4882a593Smuzhiyun ret = build_info_commit_dev(binfo);
1242*4882a593Smuzhiyun
1243*4882a593Smuzhiyun return ret;
1244*4882a593Smuzhiyun }
1245*4882a593Smuzhiyun
dfl_fpga_enum_info_alloc(struct device * dev)1246*4882a593Smuzhiyun struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev)
1247*4882a593Smuzhiyun {
1248*4882a593Smuzhiyun struct dfl_fpga_enum_info *info;
1249*4882a593Smuzhiyun
1250*4882a593Smuzhiyun get_device(dev);
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1253*4882a593Smuzhiyun if (!info) {
1254*4882a593Smuzhiyun put_device(dev);
1255*4882a593Smuzhiyun return NULL;
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun info->dev = dev;
1259*4882a593Smuzhiyun INIT_LIST_HEAD(&info->dfls);
1260*4882a593Smuzhiyun
1261*4882a593Smuzhiyun return info;
1262*4882a593Smuzhiyun }
1263*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_alloc);
1264*4882a593Smuzhiyun
dfl_fpga_enum_info_free(struct dfl_fpga_enum_info * info)1265*4882a593Smuzhiyun void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info)
1266*4882a593Smuzhiyun {
1267*4882a593Smuzhiyun struct dfl_fpga_enum_dfl *tmp, *dfl;
1268*4882a593Smuzhiyun struct device *dev;
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun if (!info)
1271*4882a593Smuzhiyun return;
1272*4882a593Smuzhiyun
1273*4882a593Smuzhiyun dev = info->dev;
1274*4882a593Smuzhiyun
1275*4882a593Smuzhiyun /* remove all device feature lists in the list. */
1276*4882a593Smuzhiyun list_for_each_entry_safe(dfl, tmp, &info->dfls, node) {
1277*4882a593Smuzhiyun list_del(&dfl->node);
1278*4882a593Smuzhiyun devm_kfree(dev, dfl);
1279*4882a593Smuzhiyun }
1280*4882a593Smuzhiyun
1281*4882a593Smuzhiyun /* remove irq table */
1282*4882a593Smuzhiyun if (info->irq_table)
1283*4882a593Smuzhiyun devm_kfree(dev, info->irq_table);
1284*4882a593Smuzhiyun
1285*4882a593Smuzhiyun devm_kfree(dev, info);
1286*4882a593Smuzhiyun put_device(dev);
1287*4882a593Smuzhiyun }
1288*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_free);
1289*4882a593Smuzhiyun
1290*4882a593Smuzhiyun /**
1291*4882a593Smuzhiyun * dfl_fpga_enum_info_add_dfl - add info of a device feature list to enum info
1292*4882a593Smuzhiyun *
1293*4882a593Smuzhiyun * @info: ptr to dfl_fpga_enum_info
1294*4882a593Smuzhiyun * @start: mmio resource address of the device feature list.
1295*4882a593Smuzhiyun * @len: mmio resource length of the device feature list.
1296*4882a593Smuzhiyun *
1297*4882a593Smuzhiyun * One FPGA device may have one or more Device Feature Lists (DFLs), use this
1298*4882a593Smuzhiyun * function to add information of each DFL to common data structure for next
1299*4882a593Smuzhiyun * step enumeration.
1300*4882a593Smuzhiyun *
1301*4882a593Smuzhiyun * Return: 0 on success, negative error code otherwise.
1302*4882a593Smuzhiyun */
dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info * info,resource_size_t start,resource_size_t len)1303*4882a593Smuzhiyun int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
1304*4882a593Smuzhiyun resource_size_t start, resource_size_t len)
1305*4882a593Smuzhiyun {
1306*4882a593Smuzhiyun struct dfl_fpga_enum_dfl *dfl;
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun dfl = devm_kzalloc(info->dev, sizeof(*dfl), GFP_KERNEL);
1309*4882a593Smuzhiyun if (!dfl)
1310*4882a593Smuzhiyun return -ENOMEM;
1311*4882a593Smuzhiyun
1312*4882a593Smuzhiyun dfl->start = start;
1313*4882a593Smuzhiyun dfl->len = len;
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun list_add_tail(&dfl->node, &info->dfls);
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun return 0;
1318*4882a593Smuzhiyun }
1319*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_add_dfl);
1320*4882a593Smuzhiyun
1321*4882a593Smuzhiyun /**
1322*4882a593Smuzhiyun * dfl_fpga_enum_info_add_irq - add irq table to enum info
1323*4882a593Smuzhiyun *
1324*4882a593Smuzhiyun * @info: ptr to dfl_fpga_enum_info
1325*4882a593Smuzhiyun * @nr_irqs: number of irqs of the DFL fpga device to be enumerated.
1326*4882a593Smuzhiyun * @irq_table: Linux IRQ numbers for all irqs, indexed by local irq index of
1327*4882a593Smuzhiyun * this device.
1328*4882a593Smuzhiyun *
1329*4882a593Smuzhiyun * One FPGA device may have several interrupts. This function adds irq
1330*4882a593Smuzhiyun * information of the DFL fpga device to enum info for next step enumeration.
1331*4882a593Smuzhiyun * This function should be called before dfl_fpga_feature_devs_enumerate().
1332*4882a593Smuzhiyun * As we only support one irq domain for all DFLs in the same enum info, adding
1333*4882a593Smuzhiyun * irq table a second time for the same enum info will return error.
1334*4882a593Smuzhiyun *
1335*4882a593Smuzhiyun * If we need to enumerate DFLs which belong to different irq domains, we
1336*4882a593Smuzhiyun * should fill more enum info and enumerate them one by one.
1337*4882a593Smuzhiyun *
1338*4882a593Smuzhiyun * Return: 0 on success, negative error code otherwise.
1339*4882a593Smuzhiyun */
dfl_fpga_enum_info_add_irq(struct dfl_fpga_enum_info * info,unsigned int nr_irqs,int * irq_table)1340*4882a593Smuzhiyun int dfl_fpga_enum_info_add_irq(struct dfl_fpga_enum_info *info,
1341*4882a593Smuzhiyun unsigned int nr_irqs, int *irq_table)
1342*4882a593Smuzhiyun {
1343*4882a593Smuzhiyun if (!nr_irqs || !irq_table)
1344*4882a593Smuzhiyun return -EINVAL;
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun if (info->irq_table)
1347*4882a593Smuzhiyun return -EEXIST;
1348*4882a593Smuzhiyun
1349*4882a593Smuzhiyun info->irq_table = devm_kmemdup(info->dev, irq_table,
1350*4882a593Smuzhiyun sizeof(int) * nr_irqs, GFP_KERNEL);
1351*4882a593Smuzhiyun if (!info->irq_table)
1352*4882a593Smuzhiyun return -ENOMEM;
1353*4882a593Smuzhiyun
1354*4882a593Smuzhiyun info->nr_irqs = nr_irqs;
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun return 0;
1357*4882a593Smuzhiyun }
1358*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_add_irq);
1359*4882a593Smuzhiyun
remove_feature_dev(struct device * dev,void * data)1360*4882a593Smuzhiyun static int remove_feature_dev(struct device *dev, void *data)
1361*4882a593Smuzhiyun {
1362*4882a593Smuzhiyun struct platform_device *pdev = to_platform_device(dev);
1363*4882a593Smuzhiyun enum dfl_id_type type = feature_dev_id_type(pdev);
1364*4882a593Smuzhiyun int id = pdev->id;
1365*4882a593Smuzhiyun
1366*4882a593Smuzhiyun platform_device_unregister(pdev);
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun dfl_id_free(type, id);
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun return 0;
1371*4882a593Smuzhiyun }
1372*4882a593Smuzhiyun
remove_feature_devs(struct dfl_fpga_cdev * cdev)1373*4882a593Smuzhiyun static void remove_feature_devs(struct dfl_fpga_cdev *cdev)
1374*4882a593Smuzhiyun {
1375*4882a593Smuzhiyun device_for_each_child(&cdev->region->dev, NULL, remove_feature_dev);
1376*4882a593Smuzhiyun }
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun /**
1379*4882a593Smuzhiyun * dfl_fpga_feature_devs_enumerate - enumerate feature devices
1380*4882a593Smuzhiyun * @info: information for enumeration.
1381*4882a593Smuzhiyun *
1382*4882a593Smuzhiyun * This function creates a container device (base FPGA region), enumerates
1383*4882a593Smuzhiyun * feature devices based on the enumeration info and creates platform devices
1384*4882a593Smuzhiyun * under the container device.
1385*4882a593Smuzhiyun *
1386*4882a593Smuzhiyun * Return: dfl_fpga_cdev struct on success, -errno on failure
1387*4882a593Smuzhiyun */
1388*4882a593Smuzhiyun struct dfl_fpga_cdev *
dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info * info)1389*4882a593Smuzhiyun dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
1390*4882a593Smuzhiyun {
1391*4882a593Smuzhiyun struct build_feature_devs_info *binfo;
1392*4882a593Smuzhiyun struct dfl_fpga_enum_dfl *dfl;
1393*4882a593Smuzhiyun struct dfl_fpga_cdev *cdev;
1394*4882a593Smuzhiyun int ret = 0;
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun if (!info->dev)
1397*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun cdev = devm_kzalloc(info->dev, sizeof(*cdev), GFP_KERNEL);
1400*4882a593Smuzhiyun if (!cdev)
1401*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun cdev->region = devm_fpga_region_create(info->dev, NULL, NULL);
1404*4882a593Smuzhiyun if (!cdev->region) {
1405*4882a593Smuzhiyun ret = -ENOMEM;
1406*4882a593Smuzhiyun goto free_cdev_exit;
1407*4882a593Smuzhiyun }
1408*4882a593Smuzhiyun
1409*4882a593Smuzhiyun cdev->parent = info->dev;
1410*4882a593Smuzhiyun mutex_init(&cdev->lock);
1411*4882a593Smuzhiyun INIT_LIST_HEAD(&cdev->port_dev_list);
1412*4882a593Smuzhiyun
1413*4882a593Smuzhiyun ret = fpga_region_register(cdev->region);
1414*4882a593Smuzhiyun if (ret)
1415*4882a593Smuzhiyun goto free_cdev_exit;
1416*4882a593Smuzhiyun
1417*4882a593Smuzhiyun /* create and init build info for enumeration */
1418*4882a593Smuzhiyun binfo = devm_kzalloc(info->dev, sizeof(*binfo), GFP_KERNEL);
1419*4882a593Smuzhiyun if (!binfo) {
1420*4882a593Smuzhiyun ret = -ENOMEM;
1421*4882a593Smuzhiyun goto unregister_region_exit;
1422*4882a593Smuzhiyun }
1423*4882a593Smuzhiyun
1424*4882a593Smuzhiyun binfo->dev = info->dev;
1425*4882a593Smuzhiyun binfo->cdev = cdev;
1426*4882a593Smuzhiyun
1427*4882a593Smuzhiyun binfo->nr_irqs = info->nr_irqs;
1428*4882a593Smuzhiyun if (info->nr_irqs)
1429*4882a593Smuzhiyun binfo->irq_table = info->irq_table;
1430*4882a593Smuzhiyun
1431*4882a593Smuzhiyun /*
1432*4882a593Smuzhiyun * start enumeration for all feature devices based on Device Feature
1433*4882a593Smuzhiyun * Lists.
1434*4882a593Smuzhiyun */
1435*4882a593Smuzhiyun list_for_each_entry(dfl, &info->dfls, node) {
1436*4882a593Smuzhiyun ret = parse_feature_list(binfo, dfl->start, dfl->len);
1437*4882a593Smuzhiyun if (ret) {
1438*4882a593Smuzhiyun remove_feature_devs(cdev);
1439*4882a593Smuzhiyun build_info_free(binfo);
1440*4882a593Smuzhiyun goto unregister_region_exit;
1441*4882a593Smuzhiyun }
1442*4882a593Smuzhiyun }
1443*4882a593Smuzhiyun
1444*4882a593Smuzhiyun build_info_free(binfo);
1445*4882a593Smuzhiyun
1446*4882a593Smuzhiyun return cdev;
1447*4882a593Smuzhiyun
1448*4882a593Smuzhiyun unregister_region_exit:
1449*4882a593Smuzhiyun fpga_region_unregister(cdev->region);
1450*4882a593Smuzhiyun free_cdev_exit:
1451*4882a593Smuzhiyun devm_kfree(info->dev, cdev);
1452*4882a593Smuzhiyun return ERR_PTR(ret);
1453*4882a593Smuzhiyun }
1454*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_feature_devs_enumerate);
1455*4882a593Smuzhiyun
1456*4882a593Smuzhiyun /**
1457*4882a593Smuzhiyun * dfl_fpga_feature_devs_remove - remove all feature devices
1458*4882a593Smuzhiyun * @cdev: fpga container device.
1459*4882a593Smuzhiyun *
1460*4882a593Smuzhiyun * Remove the container device and all feature devices under given container
1461*4882a593Smuzhiyun * devices.
1462*4882a593Smuzhiyun */
dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev * cdev)1463*4882a593Smuzhiyun void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev)
1464*4882a593Smuzhiyun {
1465*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata, *ptmp;
1466*4882a593Smuzhiyun
1467*4882a593Smuzhiyun mutex_lock(&cdev->lock);
1468*4882a593Smuzhiyun if (cdev->fme_dev)
1469*4882a593Smuzhiyun put_device(cdev->fme_dev);
1470*4882a593Smuzhiyun
1471*4882a593Smuzhiyun list_for_each_entry_safe(pdata, ptmp, &cdev->port_dev_list, node) {
1472*4882a593Smuzhiyun struct platform_device *port_dev = pdata->dev;
1473*4882a593Smuzhiyun
1474*4882a593Smuzhiyun /* remove released ports */
1475*4882a593Smuzhiyun if (!device_is_registered(&port_dev->dev)) {
1476*4882a593Smuzhiyun dfl_id_free(feature_dev_id_type(port_dev),
1477*4882a593Smuzhiyun port_dev->id);
1478*4882a593Smuzhiyun platform_device_put(port_dev);
1479*4882a593Smuzhiyun }
1480*4882a593Smuzhiyun
1481*4882a593Smuzhiyun list_del(&pdata->node);
1482*4882a593Smuzhiyun put_device(&port_dev->dev);
1483*4882a593Smuzhiyun }
1484*4882a593Smuzhiyun mutex_unlock(&cdev->lock);
1485*4882a593Smuzhiyun
1486*4882a593Smuzhiyun remove_feature_devs(cdev);
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun fpga_region_unregister(cdev->region);
1489*4882a593Smuzhiyun devm_kfree(cdev->parent, cdev);
1490*4882a593Smuzhiyun }
1491*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_feature_devs_remove);
1492*4882a593Smuzhiyun
1493*4882a593Smuzhiyun /**
1494*4882a593Smuzhiyun * __dfl_fpga_cdev_find_port - find a port under given container device
1495*4882a593Smuzhiyun *
1496*4882a593Smuzhiyun * @cdev: container device
1497*4882a593Smuzhiyun * @data: data passed to match function
1498*4882a593Smuzhiyun * @match: match function used to find specific port from the port device list
1499*4882a593Smuzhiyun *
1500*4882a593Smuzhiyun * Find a port device under container device. This function needs to be
1501*4882a593Smuzhiyun * invoked with lock held.
1502*4882a593Smuzhiyun *
1503*4882a593Smuzhiyun * Return: pointer to port's platform device if successful, NULL otherwise.
1504*4882a593Smuzhiyun *
1505*4882a593Smuzhiyun * NOTE: you will need to drop the device reference with put_device() after use.
1506*4882a593Smuzhiyun */
1507*4882a593Smuzhiyun struct platform_device *
__dfl_fpga_cdev_find_port(struct dfl_fpga_cdev * cdev,void * data,int (* match)(struct platform_device *,void *))1508*4882a593Smuzhiyun __dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
1509*4882a593Smuzhiyun int (*match)(struct platform_device *, void *))
1510*4882a593Smuzhiyun {
1511*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata;
1512*4882a593Smuzhiyun struct platform_device *port_dev;
1513*4882a593Smuzhiyun
1514*4882a593Smuzhiyun list_for_each_entry(pdata, &cdev->port_dev_list, node) {
1515*4882a593Smuzhiyun port_dev = pdata->dev;
1516*4882a593Smuzhiyun
1517*4882a593Smuzhiyun if (match(port_dev, data) && get_device(&port_dev->dev))
1518*4882a593Smuzhiyun return port_dev;
1519*4882a593Smuzhiyun }
1520*4882a593Smuzhiyun
1521*4882a593Smuzhiyun return NULL;
1522*4882a593Smuzhiyun }
1523*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__dfl_fpga_cdev_find_port);
1524*4882a593Smuzhiyun
dfl_fpga_init(void)1525*4882a593Smuzhiyun static int __init dfl_fpga_init(void)
1526*4882a593Smuzhiyun {
1527*4882a593Smuzhiyun int ret;
1528*4882a593Smuzhiyun
1529*4882a593Smuzhiyun ret = bus_register(&dfl_bus_type);
1530*4882a593Smuzhiyun if (ret)
1531*4882a593Smuzhiyun return ret;
1532*4882a593Smuzhiyun
1533*4882a593Smuzhiyun dfl_ids_init();
1534*4882a593Smuzhiyun
1535*4882a593Smuzhiyun ret = dfl_chardev_init();
1536*4882a593Smuzhiyun if (ret) {
1537*4882a593Smuzhiyun dfl_ids_destroy();
1538*4882a593Smuzhiyun bus_unregister(&dfl_bus_type);
1539*4882a593Smuzhiyun }
1540*4882a593Smuzhiyun
1541*4882a593Smuzhiyun return ret;
1542*4882a593Smuzhiyun }
1543*4882a593Smuzhiyun
1544*4882a593Smuzhiyun /**
1545*4882a593Smuzhiyun * dfl_fpga_cdev_release_port - release a port platform device
1546*4882a593Smuzhiyun *
1547*4882a593Smuzhiyun * @cdev: parent container device.
1548*4882a593Smuzhiyun * @port_id: id of the port platform device.
1549*4882a593Smuzhiyun *
1550*4882a593Smuzhiyun * This function allows user to release a port platform device. This is a
1551*4882a593Smuzhiyun * mandatory step before turn a port from PF into VF for SRIOV support.
1552*4882a593Smuzhiyun *
1553*4882a593Smuzhiyun * Return: 0 on success, negative error code otherwise.
1554*4882a593Smuzhiyun */
dfl_fpga_cdev_release_port(struct dfl_fpga_cdev * cdev,int port_id)1555*4882a593Smuzhiyun int dfl_fpga_cdev_release_port(struct dfl_fpga_cdev *cdev, int port_id)
1556*4882a593Smuzhiyun {
1557*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata;
1558*4882a593Smuzhiyun struct platform_device *port_pdev;
1559*4882a593Smuzhiyun int ret = -ENODEV;
1560*4882a593Smuzhiyun
1561*4882a593Smuzhiyun mutex_lock(&cdev->lock);
1562*4882a593Smuzhiyun port_pdev = __dfl_fpga_cdev_find_port(cdev, &port_id,
1563*4882a593Smuzhiyun dfl_fpga_check_port_id);
1564*4882a593Smuzhiyun if (!port_pdev)
1565*4882a593Smuzhiyun goto unlock_exit;
1566*4882a593Smuzhiyun
1567*4882a593Smuzhiyun if (!device_is_registered(&port_pdev->dev)) {
1568*4882a593Smuzhiyun ret = -EBUSY;
1569*4882a593Smuzhiyun goto put_dev_exit;
1570*4882a593Smuzhiyun }
1571*4882a593Smuzhiyun
1572*4882a593Smuzhiyun pdata = dev_get_platdata(&port_pdev->dev);
1573*4882a593Smuzhiyun
1574*4882a593Smuzhiyun mutex_lock(&pdata->lock);
1575*4882a593Smuzhiyun ret = dfl_feature_dev_use_begin(pdata, true);
1576*4882a593Smuzhiyun mutex_unlock(&pdata->lock);
1577*4882a593Smuzhiyun if (ret)
1578*4882a593Smuzhiyun goto put_dev_exit;
1579*4882a593Smuzhiyun
1580*4882a593Smuzhiyun platform_device_del(port_pdev);
1581*4882a593Smuzhiyun cdev->released_port_num++;
1582*4882a593Smuzhiyun put_dev_exit:
1583*4882a593Smuzhiyun put_device(&port_pdev->dev);
1584*4882a593Smuzhiyun unlock_exit:
1585*4882a593Smuzhiyun mutex_unlock(&cdev->lock);
1586*4882a593Smuzhiyun return ret;
1587*4882a593Smuzhiyun }
1588*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_cdev_release_port);
1589*4882a593Smuzhiyun
1590*4882a593Smuzhiyun /**
1591*4882a593Smuzhiyun * dfl_fpga_cdev_assign_port - assign a port platform device back
1592*4882a593Smuzhiyun *
1593*4882a593Smuzhiyun * @cdev: parent container device.
1594*4882a593Smuzhiyun * @port_id: id of the port platform device.
1595*4882a593Smuzhiyun *
1596*4882a593Smuzhiyun * This function allows user to assign a port platform device back. This is
1597*4882a593Smuzhiyun * a mandatory step after disable SRIOV support.
1598*4882a593Smuzhiyun *
1599*4882a593Smuzhiyun * Return: 0 on success, negative error code otherwise.
1600*4882a593Smuzhiyun */
dfl_fpga_cdev_assign_port(struct dfl_fpga_cdev * cdev,int port_id)1601*4882a593Smuzhiyun int dfl_fpga_cdev_assign_port(struct dfl_fpga_cdev *cdev, int port_id)
1602*4882a593Smuzhiyun {
1603*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata;
1604*4882a593Smuzhiyun struct platform_device *port_pdev;
1605*4882a593Smuzhiyun int ret = -ENODEV;
1606*4882a593Smuzhiyun
1607*4882a593Smuzhiyun mutex_lock(&cdev->lock);
1608*4882a593Smuzhiyun port_pdev = __dfl_fpga_cdev_find_port(cdev, &port_id,
1609*4882a593Smuzhiyun dfl_fpga_check_port_id);
1610*4882a593Smuzhiyun if (!port_pdev)
1611*4882a593Smuzhiyun goto unlock_exit;
1612*4882a593Smuzhiyun
1613*4882a593Smuzhiyun if (device_is_registered(&port_pdev->dev)) {
1614*4882a593Smuzhiyun ret = -EBUSY;
1615*4882a593Smuzhiyun goto put_dev_exit;
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun
1618*4882a593Smuzhiyun ret = platform_device_add(port_pdev);
1619*4882a593Smuzhiyun if (ret)
1620*4882a593Smuzhiyun goto put_dev_exit;
1621*4882a593Smuzhiyun
1622*4882a593Smuzhiyun pdata = dev_get_platdata(&port_pdev->dev);
1623*4882a593Smuzhiyun
1624*4882a593Smuzhiyun mutex_lock(&pdata->lock);
1625*4882a593Smuzhiyun dfl_feature_dev_use_end(pdata);
1626*4882a593Smuzhiyun mutex_unlock(&pdata->lock);
1627*4882a593Smuzhiyun
1628*4882a593Smuzhiyun cdev->released_port_num--;
1629*4882a593Smuzhiyun put_dev_exit:
1630*4882a593Smuzhiyun put_device(&port_pdev->dev);
1631*4882a593Smuzhiyun unlock_exit:
1632*4882a593Smuzhiyun mutex_unlock(&cdev->lock);
1633*4882a593Smuzhiyun return ret;
1634*4882a593Smuzhiyun }
1635*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_cdev_assign_port);
1636*4882a593Smuzhiyun
config_port_access_mode(struct device * fme_dev,int port_id,bool is_vf)1637*4882a593Smuzhiyun static void config_port_access_mode(struct device *fme_dev, int port_id,
1638*4882a593Smuzhiyun bool is_vf)
1639*4882a593Smuzhiyun {
1640*4882a593Smuzhiyun void __iomem *base;
1641*4882a593Smuzhiyun u64 v;
1642*4882a593Smuzhiyun
1643*4882a593Smuzhiyun base = dfl_get_feature_ioaddr_by_id(fme_dev, FME_FEATURE_ID_HEADER);
1644*4882a593Smuzhiyun
1645*4882a593Smuzhiyun v = readq(base + FME_HDR_PORT_OFST(port_id));
1646*4882a593Smuzhiyun
1647*4882a593Smuzhiyun v &= ~FME_PORT_OFST_ACC_CTRL;
1648*4882a593Smuzhiyun v |= FIELD_PREP(FME_PORT_OFST_ACC_CTRL,
1649*4882a593Smuzhiyun is_vf ? FME_PORT_OFST_ACC_VF : FME_PORT_OFST_ACC_PF);
1650*4882a593Smuzhiyun
1651*4882a593Smuzhiyun writeq(v, base + FME_HDR_PORT_OFST(port_id));
1652*4882a593Smuzhiyun }
1653*4882a593Smuzhiyun
1654*4882a593Smuzhiyun #define config_port_vf_mode(dev, id) config_port_access_mode(dev, id, true)
1655*4882a593Smuzhiyun #define config_port_pf_mode(dev, id) config_port_access_mode(dev, id, false)
1656*4882a593Smuzhiyun
1657*4882a593Smuzhiyun /**
1658*4882a593Smuzhiyun * dfl_fpga_cdev_config_ports_pf - configure ports to PF access mode
1659*4882a593Smuzhiyun *
1660*4882a593Smuzhiyun * @cdev: parent container device.
1661*4882a593Smuzhiyun *
1662*4882a593Smuzhiyun * This function is needed in sriov configuration routine. It could be used to
1663*4882a593Smuzhiyun * configure the all released ports from VF access mode to PF.
1664*4882a593Smuzhiyun */
dfl_fpga_cdev_config_ports_pf(struct dfl_fpga_cdev * cdev)1665*4882a593Smuzhiyun void dfl_fpga_cdev_config_ports_pf(struct dfl_fpga_cdev *cdev)
1666*4882a593Smuzhiyun {
1667*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata;
1668*4882a593Smuzhiyun
1669*4882a593Smuzhiyun mutex_lock(&cdev->lock);
1670*4882a593Smuzhiyun list_for_each_entry(pdata, &cdev->port_dev_list, node) {
1671*4882a593Smuzhiyun if (device_is_registered(&pdata->dev->dev))
1672*4882a593Smuzhiyun continue;
1673*4882a593Smuzhiyun
1674*4882a593Smuzhiyun config_port_pf_mode(cdev->fme_dev, pdata->id);
1675*4882a593Smuzhiyun }
1676*4882a593Smuzhiyun mutex_unlock(&cdev->lock);
1677*4882a593Smuzhiyun }
1678*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_cdev_config_ports_pf);
1679*4882a593Smuzhiyun
1680*4882a593Smuzhiyun /**
1681*4882a593Smuzhiyun * dfl_fpga_cdev_config_ports_vf - configure ports to VF access mode
1682*4882a593Smuzhiyun *
1683*4882a593Smuzhiyun * @cdev: parent container device.
1684*4882a593Smuzhiyun * @num_vfs: VF device number.
1685*4882a593Smuzhiyun *
1686*4882a593Smuzhiyun * This function is needed in sriov configuration routine. It could be used to
1687*4882a593Smuzhiyun * configure the released ports from PF access mode to VF.
1688*4882a593Smuzhiyun *
1689*4882a593Smuzhiyun * Return: 0 on success, negative error code otherwise.
1690*4882a593Smuzhiyun */
dfl_fpga_cdev_config_ports_vf(struct dfl_fpga_cdev * cdev,int num_vfs)1691*4882a593Smuzhiyun int dfl_fpga_cdev_config_ports_vf(struct dfl_fpga_cdev *cdev, int num_vfs)
1692*4882a593Smuzhiyun {
1693*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata;
1694*4882a593Smuzhiyun int ret = 0;
1695*4882a593Smuzhiyun
1696*4882a593Smuzhiyun mutex_lock(&cdev->lock);
1697*4882a593Smuzhiyun /*
1698*4882a593Smuzhiyun * can't turn multiple ports into 1 VF device, only 1 port for 1 VF
1699*4882a593Smuzhiyun * device, so if released port number doesn't match VF device number,
1700*4882a593Smuzhiyun * then reject the request with -EINVAL error code.
1701*4882a593Smuzhiyun */
1702*4882a593Smuzhiyun if (cdev->released_port_num != num_vfs) {
1703*4882a593Smuzhiyun ret = -EINVAL;
1704*4882a593Smuzhiyun goto done;
1705*4882a593Smuzhiyun }
1706*4882a593Smuzhiyun
1707*4882a593Smuzhiyun list_for_each_entry(pdata, &cdev->port_dev_list, node) {
1708*4882a593Smuzhiyun if (device_is_registered(&pdata->dev->dev))
1709*4882a593Smuzhiyun continue;
1710*4882a593Smuzhiyun
1711*4882a593Smuzhiyun config_port_vf_mode(cdev->fme_dev, pdata->id);
1712*4882a593Smuzhiyun }
1713*4882a593Smuzhiyun done:
1714*4882a593Smuzhiyun mutex_unlock(&cdev->lock);
1715*4882a593Smuzhiyun return ret;
1716*4882a593Smuzhiyun }
1717*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_cdev_config_ports_vf);
1718*4882a593Smuzhiyun
dfl_irq_handler(int irq,void * arg)1719*4882a593Smuzhiyun static irqreturn_t dfl_irq_handler(int irq, void *arg)
1720*4882a593Smuzhiyun {
1721*4882a593Smuzhiyun struct eventfd_ctx *trigger = arg;
1722*4882a593Smuzhiyun
1723*4882a593Smuzhiyun eventfd_signal(trigger, 1);
1724*4882a593Smuzhiyun return IRQ_HANDLED;
1725*4882a593Smuzhiyun }
1726*4882a593Smuzhiyun
do_set_irq_trigger(struct dfl_feature * feature,unsigned int idx,int fd)1727*4882a593Smuzhiyun static int do_set_irq_trigger(struct dfl_feature *feature, unsigned int idx,
1728*4882a593Smuzhiyun int fd)
1729*4882a593Smuzhiyun {
1730*4882a593Smuzhiyun struct platform_device *pdev = feature->dev;
1731*4882a593Smuzhiyun struct eventfd_ctx *trigger;
1732*4882a593Smuzhiyun int irq, ret;
1733*4882a593Smuzhiyun
1734*4882a593Smuzhiyun irq = feature->irq_ctx[idx].irq;
1735*4882a593Smuzhiyun
1736*4882a593Smuzhiyun if (feature->irq_ctx[idx].trigger) {
1737*4882a593Smuzhiyun free_irq(irq, feature->irq_ctx[idx].trigger);
1738*4882a593Smuzhiyun kfree(feature->irq_ctx[idx].name);
1739*4882a593Smuzhiyun eventfd_ctx_put(feature->irq_ctx[idx].trigger);
1740*4882a593Smuzhiyun feature->irq_ctx[idx].trigger = NULL;
1741*4882a593Smuzhiyun }
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun if (fd < 0)
1744*4882a593Smuzhiyun return 0;
1745*4882a593Smuzhiyun
1746*4882a593Smuzhiyun feature->irq_ctx[idx].name =
1747*4882a593Smuzhiyun kasprintf(GFP_KERNEL, "fpga-irq[%u](%s-%x)", idx,
1748*4882a593Smuzhiyun dev_name(&pdev->dev), feature->id);
1749*4882a593Smuzhiyun if (!feature->irq_ctx[idx].name)
1750*4882a593Smuzhiyun return -ENOMEM;
1751*4882a593Smuzhiyun
1752*4882a593Smuzhiyun trigger = eventfd_ctx_fdget(fd);
1753*4882a593Smuzhiyun if (IS_ERR(trigger)) {
1754*4882a593Smuzhiyun ret = PTR_ERR(trigger);
1755*4882a593Smuzhiyun goto free_name;
1756*4882a593Smuzhiyun }
1757*4882a593Smuzhiyun
1758*4882a593Smuzhiyun ret = request_irq(irq, dfl_irq_handler, 0,
1759*4882a593Smuzhiyun feature->irq_ctx[idx].name, trigger);
1760*4882a593Smuzhiyun if (!ret) {
1761*4882a593Smuzhiyun feature->irq_ctx[idx].trigger = trigger;
1762*4882a593Smuzhiyun return ret;
1763*4882a593Smuzhiyun }
1764*4882a593Smuzhiyun
1765*4882a593Smuzhiyun eventfd_ctx_put(trigger);
1766*4882a593Smuzhiyun free_name:
1767*4882a593Smuzhiyun kfree(feature->irq_ctx[idx].name);
1768*4882a593Smuzhiyun
1769*4882a593Smuzhiyun return ret;
1770*4882a593Smuzhiyun }
1771*4882a593Smuzhiyun
1772*4882a593Smuzhiyun /**
1773*4882a593Smuzhiyun * dfl_fpga_set_irq_triggers - set eventfd triggers for dfl feature interrupts
1774*4882a593Smuzhiyun *
1775*4882a593Smuzhiyun * @feature: dfl sub feature.
1776*4882a593Smuzhiyun * @start: start of irq index in this dfl sub feature.
1777*4882a593Smuzhiyun * @count: number of irqs.
1778*4882a593Smuzhiyun * @fds: eventfds to bind with irqs. unbind related irq if fds[n] is negative.
1779*4882a593Smuzhiyun * unbind "count" specified number of irqs if fds ptr is NULL.
1780*4882a593Smuzhiyun *
1781*4882a593Smuzhiyun * Bind given eventfds with irqs in this dfl sub feature. Unbind related irq if
1782*4882a593Smuzhiyun * fds[n] is negative. Unbind "count" specified number of irqs if fds ptr is
1783*4882a593Smuzhiyun * NULL.
1784*4882a593Smuzhiyun *
1785*4882a593Smuzhiyun * Return: 0 on success, negative error code otherwise.
1786*4882a593Smuzhiyun */
dfl_fpga_set_irq_triggers(struct dfl_feature * feature,unsigned int start,unsigned int count,int32_t * fds)1787*4882a593Smuzhiyun int dfl_fpga_set_irq_triggers(struct dfl_feature *feature, unsigned int start,
1788*4882a593Smuzhiyun unsigned int count, int32_t *fds)
1789*4882a593Smuzhiyun {
1790*4882a593Smuzhiyun unsigned int i;
1791*4882a593Smuzhiyun int ret = 0;
1792*4882a593Smuzhiyun
1793*4882a593Smuzhiyun /* overflow */
1794*4882a593Smuzhiyun if (unlikely(start + count < start))
1795*4882a593Smuzhiyun return -EINVAL;
1796*4882a593Smuzhiyun
1797*4882a593Smuzhiyun /* exceeds nr_irqs */
1798*4882a593Smuzhiyun if (start + count > feature->nr_irqs)
1799*4882a593Smuzhiyun return -EINVAL;
1800*4882a593Smuzhiyun
1801*4882a593Smuzhiyun for (i = 0; i < count; i++) {
1802*4882a593Smuzhiyun int fd = fds ? fds[i] : -1;
1803*4882a593Smuzhiyun
1804*4882a593Smuzhiyun ret = do_set_irq_trigger(feature, start + i, fd);
1805*4882a593Smuzhiyun if (ret) {
1806*4882a593Smuzhiyun while (i--)
1807*4882a593Smuzhiyun do_set_irq_trigger(feature, start + i, -1);
1808*4882a593Smuzhiyun break;
1809*4882a593Smuzhiyun }
1810*4882a593Smuzhiyun }
1811*4882a593Smuzhiyun
1812*4882a593Smuzhiyun return ret;
1813*4882a593Smuzhiyun }
1814*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_fpga_set_irq_triggers);
1815*4882a593Smuzhiyun
1816*4882a593Smuzhiyun /**
1817*4882a593Smuzhiyun * dfl_feature_ioctl_get_num_irqs - dfl feature _GET_IRQ_NUM ioctl interface.
1818*4882a593Smuzhiyun * @pdev: the feature device which has the sub feature
1819*4882a593Smuzhiyun * @feature: the dfl sub feature
1820*4882a593Smuzhiyun * @arg: ioctl argument
1821*4882a593Smuzhiyun *
1822*4882a593Smuzhiyun * Return: 0 on success, negative error code otherwise.
1823*4882a593Smuzhiyun */
dfl_feature_ioctl_get_num_irqs(struct platform_device * pdev,struct dfl_feature * feature,unsigned long arg)1824*4882a593Smuzhiyun long dfl_feature_ioctl_get_num_irqs(struct platform_device *pdev,
1825*4882a593Smuzhiyun struct dfl_feature *feature,
1826*4882a593Smuzhiyun unsigned long arg)
1827*4882a593Smuzhiyun {
1828*4882a593Smuzhiyun return put_user(feature->nr_irqs, (__u32 __user *)arg);
1829*4882a593Smuzhiyun }
1830*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_feature_ioctl_get_num_irqs);
1831*4882a593Smuzhiyun
1832*4882a593Smuzhiyun /**
1833*4882a593Smuzhiyun * dfl_feature_ioctl_set_irq - dfl feature _SET_IRQ ioctl interface.
1834*4882a593Smuzhiyun * @pdev: the feature device which has the sub feature
1835*4882a593Smuzhiyun * @feature: the dfl sub feature
1836*4882a593Smuzhiyun * @arg: ioctl argument
1837*4882a593Smuzhiyun *
1838*4882a593Smuzhiyun * Return: 0 on success, negative error code otherwise.
1839*4882a593Smuzhiyun */
dfl_feature_ioctl_set_irq(struct platform_device * pdev,struct dfl_feature * feature,unsigned long arg)1840*4882a593Smuzhiyun long dfl_feature_ioctl_set_irq(struct platform_device *pdev,
1841*4882a593Smuzhiyun struct dfl_feature *feature,
1842*4882a593Smuzhiyun unsigned long arg)
1843*4882a593Smuzhiyun {
1844*4882a593Smuzhiyun struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
1845*4882a593Smuzhiyun struct dfl_fpga_irq_set hdr;
1846*4882a593Smuzhiyun s32 *fds;
1847*4882a593Smuzhiyun long ret;
1848*4882a593Smuzhiyun
1849*4882a593Smuzhiyun if (!feature->nr_irqs)
1850*4882a593Smuzhiyun return -ENOENT;
1851*4882a593Smuzhiyun
1852*4882a593Smuzhiyun if (copy_from_user(&hdr, (void __user *)arg, sizeof(hdr)))
1853*4882a593Smuzhiyun return -EFAULT;
1854*4882a593Smuzhiyun
1855*4882a593Smuzhiyun if (!hdr.count || (hdr.start + hdr.count > feature->nr_irqs) ||
1856*4882a593Smuzhiyun (hdr.start + hdr.count < hdr.start))
1857*4882a593Smuzhiyun return -EINVAL;
1858*4882a593Smuzhiyun
1859*4882a593Smuzhiyun fds = memdup_user((void __user *)(arg + sizeof(hdr)),
1860*4882a593Smuzhiyun array_size(hdr.count, sizeof(s32)));
1861*4882a593Smuzhiyun if (IS_ERR(fds))
1862*4882a593Smuzhiyun return PTR_ERR(fds);
1863*4882a593Smuzhiyun
1864*4882a593Smuzhiyun mutex_lock(&pdata->lock);
1865*4882a593Smuzhiyun ret = dfl_fpga_set_irq_triggers(feature, hdr.start, hdr.count, fds);
1866*4882a593Smuzhiyun mutex_unlock(&pdata->lock);
1867*4882a593Smuzhiyun
1868*4882a593Smuzhiyun kfree(fds);
1869*4882a593Smuzhiyun return ret;
1870*4882a593Smuzhiyun }
1871*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dfl_feature_ioctl_set_irq);
1872*4882a593Smuzhiyun
dfl_fpga_exit(void)1873*4882a593Smuzhiyun static void __exit dfl_fpga_exit(void)
1874*4882a593Smuzhiyun {
1875*4882a593Smuzhiyun dfl_chardev_uinit();
1876*4882a593Smuzhiyun dfl_ids_destroy();
1877*4882a593Smuzhiyun bus_unregister(&dfl_bus_type);
1878*4882a593Smuzhiyun }
1879*4882a593Smuzhiyun
1880*4882a593Smuzhiyun module_init(dfl_fpga_init);
1881*4882a593Smuzhiyun module_exit(dfl_fpga_exit);
1882*4882a593Smuzhiyun
1883*4882a593Smuzhiyun MODULE_DESCRIPTION("FPGA Device Feature List (DFL) Support");
1884*4882a593Smuzhiyun MODULE_AUTHOR("Intel Corporation");
1885*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
1886