xref: /OK3568_Linux_fs/kernel/drivers/fpga/dfl.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Driver Header File 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 
14*4882a593Smuzhiyun #ifndef __FPGA_DFL_H
15*4882a593Smuzhiyun #define __FPGA_DFL_H
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/bitfield.h>
18*4882a593Smuzhiyun #include <linux/cdev.h>
19*4882a593Smuzhiyun #include <linux/delay.h>
20*4882a593Smuzhiyun #include <linux/eventfd.h>
21*4882a593Smuzhiyun #include <linux/fs.h>
22*4882a593Smuzhiyun #include <linux/interrupt.h>
23*4882a593Smuzhiyun #include <linux/iopoll.h>
24*4882a593Smuzhiyun #include <linux/io-64-nonatomic-lo-hi.h>
25*4882a593Smuzhiyun #include <linux/platform_device.h>
26*4882a593Smuzhiyun #include <linux/slab.h>
27*4882a593Smuzhiyun #include <linux/uuid.h>
28*4882a593Smuzhiyun #include <linux/fpga/fpga-region.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /* maximum supported number of ports */
31*4882a593Smuzhiyun #define MAX_DFL_FPGA_PORT_NUM 4
32*4882a593Smuzhiyun /* plus one for fme device */
33*4882a593Smuzhiyun #define MAX_DFL_FEATURE_DEV_NUM    (MAX_DFL_FPGA_PORT_NUM + 1)
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* Reserved 0xfe for Header Group Register and 0xff for AFU */
36*4882a593Smuzhiyun #define FEATURE_ID_FIU_HEADER		0xfe
37*4882a593Smuzhiyun #define FEATURE_ID_AFU			0xff
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #define FME_FEATURE_ID_HEADER		FEATURE_ID_FIU_HEADER
40*4882a593Smuzhiyun #define FME_FEATURE_ID_THERMAL_MGMT	0x1
41*4882a593Smuzhiyun #define FME_FEATURE_ID_POWER_MGMT	0x2
42*4882a593Smuzhiyun #define FME_FEATURE_ID_GLOBAL_IPERF	0x3
43*4882a593Smuzhiyun #define FME_FEATURE_ID_GLOBAL_ERR	0x4
44*4882a593Smuzhiyun #define FME_FEATURE_ID_PR_MGMT		0x5
45*4882a593Smuzhiyun #define FME_FEATURE_ID_HSSI		0x6
46*4882a593Smuzhiyun #define FME_FEATURE_ID_GLOBAL_DPERF	0x7
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define PORT_FEATURE_ID_HEADER		FEATURE_ID_FIU_HEADER
49*4882a593Smuzhiyun #define PORT_FEATURE_ID_AFU		FEATURE_ID_AFU
50*4882a593Smuzhiyun #define PORT_FEATURE_ID_ERROR		0x10
51*4882a593Smuzhiyun #define PORT_FEATURE_ID_UMSG		0x11
52*4882a593Smuzhiyun #define PORT_FEATURE_ID_UINT		0x12
53*4882a593Smuzhiyun #define PORT_FEATURE_ID_STP		0x13
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun  * Device Feature Header Register Set
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * For FIUs, they all have DFH + GUID + NEXT_AFU as common header registers.
59*4882a593Smuzhiyun  * For AFUs, they have DFH + GUID as common header registers.
60*4882a593Smuzhiyun  * For private features, they only have DFH register as common header.
61*4882a593Smuzhiyun  */
62*4882a593Smuzhiyun #define DFH			0x0
63*4882a593Smuzhiyun #define GUID_L			0x8
64*4882a593Smuzhiyun #define GUID_H			0x10
65*4882a593Smuzhiyun #define NEXT_AFU		0x18
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #define DFH_SIZE		0x8
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /* Device Feature Header Register Bitfield */
70*4882a593Smuzhiyun #define DFH_ID			GENMASK_ULL(11, 0)	/* Feature ID */
71*4882a593Smuzhiyun #define DFH_ID_FIU_FME		0
72*4882a593Smuzhiyun #define DFH_ID_FIU_PORT		1
73*4882a593Smuzhiyun #define DFH_REVISION		GENMASK_ULL(15, 12)	/* Feature revision */
74*4882a593Smuzhiyun #define DFH_NEXT_HDR_OFST	GENMASK_ULL(39, 16)	/* Offset to next DFH */
75*4882a593Smuzhiyun #define DFH_EOL			BIT_ULL(40)		/* End of list */
76*4882a593Smuzhiyun #define DFH_TYPE		GENMASK_ULL(63, 60)	/* Feature type */
77*4882a593Smuzhiyun #define DFH_TYPE_AFU		1
78*4882a593Smuzhiyun #define DFH_TYPE_PRIVATE	3
79*4882a593Smuzhiyun #define DFH_TYPE_FIU		4
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /* Next AFU Register Bitfield */
82*4882a593Smuzhiyun #define NEXT_AFU_NEXT_DFH_OFST	GENMASK_ULL(23, 0)	/* Offset to next AFU */
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /* FME Header Register Set */
85*4882a593Smuzhiyun #define FME_HDR_DFH		DFH
86*4882a593Smuzhiyun #define FME_HDR_GUID_L		GUID_L
87*4882a593Smuzhiyun #define FME_HDR_GUID_H		GUID_H
88*4882a593Smuzhiyun #define FME_HDR_NEXT_AFU	NEXT_AFU
89*4882a593Smuzhiyun #define FME_HDR_CAP		0x30
90*4882a593Smuzhiyun #define FME_HDR_PORT_OFST(n)	(0x38 + ((n) * 0x8))
91*4882a593Smuzhiyun #define FME_HDR_BITSTREAM_ID	0x60
92*4882a593Smuzhiyun #define FME_HDR_BITSTREAM_MD	0x68
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /* FME Fab Capability Register Bitfield */
95*4882a593Smuzhiyun #define FME_CAP_FABRIC_VERID	GENMASK_ULL(7, 0)	/* Fabric version ID */
96*4882a593Smuzhiyun #define FME_CAP_SOCKET_ID	BIT_ULL(8)		/* Socket ID */
97*4882a593Smuzhiyun #define FME_CAP_PCIE0_LINK_AVL	BIT_ULL(12)		/* PCIE0 Link */
98*4882a593Smuzhiyun #define FME_CAP_PCIE1_LINK_AVL	BIT_ULL(13)		/* PCIE1 Link */
99*4882a593Smuzhiyun #define FME_CAP_COHR_LINK_AVL	BIT_ULL(14)		/* Coherent Link */
100*4882a593Smuzhiyun #define FME_CAP_IOMMU_AVL	BIT_ULL(16)		/* IOMMU available */
101*4882a593Smuzhiyun #define FME_CAP_NUM_PORTS	GENMASK_ULL(19, 17)	/* Number of ports */
102*4882a593Smuzhiyun #define FME_CAP_ADDR_WIDTH	GENMASK_ULL(29, 24)	/* Address bus width */
103*4882a593Smuzhiyun #define FME_CAP_CACHE_SIZE	GENMASK_ULL(43, 32)	/* cache size in KB */
104*4882a593Smuzhiyun #define FME_CAP_CACHE_ASSOC	GENMASK_ULL(47, 44)	/* Associativity */
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /* FME Port Offset Register Bitfield */
107*4882a593Smuzhiyun /* Offset to port device feature header */
108*4882a593Smuzhiyun #define FME_PORT_OFST_DFH_OFST	GENMASK_ULL(23, 0)
109*4882a593Smuzhiyun /* PCI Bar ID for this port */
110*4882a593Smuzhiyun #define FME_PORT_OFST_BAR_ID	GENMASK_ULL(34, 32)
111*4882a593Smuzhiyun /* AFU MMIO access permission. 1 - VF, 0 - PF. */
112*4882a593Smuzhiyun #define FME_PORT_OFST_ACC_CTRL	BIT_ULL(55)
113*4882a593Smuzhiyun #define FME_PORT_OFST_ACC_PF	0
114*4882a593Smuzhiyun #define FME_PORT_OFST_ACC_VF	1
115*4882a593Smuzhiyun #define FME_PORT_OFST_IMP	BIT_ULL(60)
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /* FME Error Capability Register */
118*4882a593Smuzhiyun #define FME_ERROR_CAP		0x70
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /* FME Error Capability Register Bitfield */
121*4882a593Smuzhiyun #define FME_ERROR_CAP_SUPP_INT	BIT_ULL(0)		/* Interrupt Support */
122*4882a593Smuzhiyun #define FME_ERROR_CAP_INT_VECT	GENMASK_ULL(12, 1)	/* Interrupt vector */
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /* PORT Header Register Set */
125*4882a593Smuzhiyun #define PORT_HDR_DFH		DFH
126*4882a593Smuzhiyun #define PORT_HDR_GUID_L		GUID_L
127*4882a593Smuzhiyun #define PORT_HDR_GUID_H		GUID_H
128*4882a593Smuzhiyun #define PORT_HDR_NEXT_AFU	NEXT_AFU
129*4882a593Smuzhiyun #define PORT_HDR_CAP		0x30
130*4882a593Smuzhiyun #define PORT_HDR_CTRL		0x38
131*4882a593Smuzhiyun #define PORT_HDR_STS		0x40
132*4882a593Smuzhiyun #define PORT_HDR_USRCLK_CMD0	0x50
133*4882a593Smuzhiyun #define PORT_HDR_USRCLK_CMD1	0x58
134*4882a593Smuzhiyun #define PORT_HDR_USRCLK_STS0	0x60
135*4882a593Smuzhiyun #define PORT_HDR_USRCLK_STS1	0x68
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun /* Port Capability Register Bitfield */
138*4882a593Smuzhiyun #define PORT_CAP_PORT_NUM	GENMASK_ULL(1, 0)	/* ID of this port */
139*4882a593Smuzhiyun #define PORT_CAP_MMIO_SIZE	GENMASK_ULL(23, 8)	/* MMIO size in KB */
140*4882a593Smuzhiyun #define PORT_CAP_SUPP_INT_NUM	GENMASK_ULL(35, 32)	/* Interrupts num */
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /* Port Control Register Bitfield */
143*4882a593Smuzhiyun #define PORT_CTRL_SFTRST	BIT_ULL(0)		/* Port soft reset */
144*4882a593Smuzhiyun /* Latency tolerance reporting. '1' >= 40us, '0' < 40us.*/
145*4882a593Smuzhiyun #define PORT_CTRL_LATENCY	BIT_ULL(2)
146*4882a593Smuzhiyun #define PORT_CTRL_SFTRST_ACK	BIT_ULL(4)		/* HW ack for reset */
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun /* Port Status Register Bitfield */
149*4882a593Smuzhiyun #define PORT_STS_AP2_EVT	BIT_ULL(13)		/* AP2 event detected */
150*4882a593Smuzhiyun #define PORT_STS_AP1_EVT	BIT_ULL(12)		/* AP1 event detected */
151*4882a593Smuzhiyun #define PORT_STS_PWR_STATE	GENMASK_ULL(11, 8)	/* AFU power states */
152*4882a593Smuzhiyun #define PORT_STS_PWR_STATE_NORM 0
153*4882a593Smuzhiyun #define PORT_STS_PWR_STATE_AP1	1			/* 50% throttling */
154*4882a593Smuzhiyun #define PORT_STS_PWR_STATE_AP2	2			/* 90% throttling */
155*4882a593Smuzhiyun #define PORT_STS_PWR_STATE_AP6	6			/* 100% throttling */
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /* Port Error Capability Register */
158*4882a593Smuzhiyun #define PORT_ERROR_CAP		0x38
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /* Port Error Capability Register Bitfield */
161*4882a593Smuzhiyun #define PORT_ERROR_CAP_SUPP_INT	BIT_ULL(0)		/* Interrupt Support */
162*4882a593Smuzhiyun #define PORT_ERROR_CAP_INT_VECT	GENMASK_ULL(12, 1)	/* Interrupt vector */
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /* Port Uint Capability Register */
165*4882a593Smuzhiyun #define PORT_UINT_CAP		0x8
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /* Port Uint Capability Register Bitfield */
168*4882a593Smuzhiyun #define PORT_UINT_CAP_INT_NUM	GENMASK_ULL(11, 0)	/* Interrupts num */
169*4882a593Smuzhiyun #define PORT_UINT_CAP_FST_VECT	GENMASK_ULL(23, 12)	/* First Vector */
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /**
172*4882a593Smuzhiyun  * struct dfl_fpga_port_ops - port ops
173*4882a593Smuzhiyun  *
174*4882a593Smuzhiyun  * @name: name of this port ops, to match with port platform device.
175*4882a593Smuzhiyun  * @owner: pointer to the module which owns this port ops.
176*4882a593Smuzhiyun  * @node: node to link port ops to global list.
177*4882a593Smuzhiyun  * @get_id: get port id from hardware.
178*4882a593Smuzhiyun  * @enable_set: enable/disable the port.
179*4882a593Smuzhiyun  */
180*4882a593Smuzhiyun struct dfl_fpga_port_ops {
181*4882a593Smuzhiyun 	const char *name;
182*4882a593Smuzhiyun 	struct module *owner;
183*4882a593Smuzhiyun 	struct list_head node;
184*4882a593Smuzhiyun 	int (*get_id)(struct platform_device *pdev);
185*4882a593Smuzhiyun 	int (*enable_set)(struct platform_device *pdev, bool enable);
186*4882a593Smuzhiyun };
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun void dfl_fpga_port_ops_add(struct dfl_fpga_port_ops *ops);
189*4882a593Smuzhiyun void dfl_fpga_port_ops_del(struct dfl_fpga_port_ops *ops);
190*4882a593Smuzhiyun struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct platform_device *pdev);
191*4882a593Smuzhiyun void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops *ops);
192*4882a593Smuzhiyun int dfl_fpga_check_port_id(struct platform_device *pdev, void *pport_id);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun /**
195*4882a593Smuzhiyun  * struct dfl_feature_id - dfl private feature id
196*4882a593Smuzhiyun  *
197*4882a593Smuzhiyun  * @id: unique dfl private feature id.
198*4882a593Smuzhiyun  */
199*4882a593Smuzhiyun struct dfl_feature_id {
200*4882a593Smuzhiyun 	u16 id;
201*4882a593Smuzhiyun };
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun /**
204*4882a593Smuzhiyun  * struct dfl_feature_driver - dfl private feature driver
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * @id_table: id_table for dfl private features supported by this driver.
207*4882a593Smuzhiyun  * @ops: ops of this dfl private feature driver.
208*4882a593Smuzhiyun  */
209*4882a593Smuzhiyun struct dfl_feature_driver {
210*4882a593Smuzhiyun 	const struct dfl_feature_id *id_table;
211*4882a593Smuzhiyun 	const struct dfl_feature_ops *ops;
212*4882a593Smuzhiyun };
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun /**
215*4882a593Smuzhiyun  * struct dfl_feature_irq_ctx - dfl private feature interrupt context
216*4882a593Smuzhiyun  *
217*4882a593Smuzhiyun  * @irq: Linux IRQ number of this interrupt.
218*4882a593Smuzhiyun  * @trigger: eventfd context to signal when interrupt happens.
219*4882a593Smuzhiyun  * @name: irq name needed when requesting irq.
220*4882a593Smuzhiyun  */
221*4882a593Smuzhiyun struct dfl_feature_irq_ctx {
222*4882a593Smuzhiyun 	int irq;
223*4882a593Smuzhiyun 	struct eventfd_ctx *trigger;
224*4882a593Smuzhiyun 	char *name;
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /**
228*4882a593Smuzhiyun  * struct dfl_feature - sub feature of the feature devices
229*4882a593Smuzhiyun  *
230*4882a593Smuzhiyun  * @dev: ptr to pdev of the feature device which has the sub feature.
231*4882a593Smuzhiyun  * @id: sub feature id.
232*4882a593Smuzhiyun  * @resource_index: each sub feature has one mmio resource for its registers.
233*4882a593Smuzhiyun  *		    this index is used to find its mmio resource from the
234*4882a593Smuzhiyun  *		    feature dev (platform device)'s reources.
235*4882a593Smuzhiyun  * @ioaddr: mapped mmio resource address.
236*4882a593Smuzhiyun  * @irq_ctx: interrupt context list.
237*4882a593Smuzhiyun  * @nr_irqs: number of interrupt contexts.
238*4882a593Smuzhiyun  * @ops: ops of this sub feature.
239*4882a593Smuzhiyun  * @ddev: ptr to the dfl device of this sub feature.
240*4882a593Smuzhiyun  * @priv: priv data of this feature.
241*4882a593Smuzhiyun  */
242*4882a593Smuzhiyun struct dfl_feature {
243*4882a593Smuzhiyun 	struct platform_device *dev;
244*4882a593Smuzhiyun 	u16 id;
245*4882a593Smuzhiyun 	int resource_index;
246*4882a593Smuzhiyun 	void __iomem *ioaddr;
247*4882a593Smuzhiyun 	struct dfl_feature_irq_ctx *irq_ctx;
248*4882a593Smuzhiyun 	unsigned int nr_irqs;
249*4882a593Smuzhiyun 	const struct dfl_feature_ops *ops;
250*4882a593Smuzhiyun 	struct dfl_device *ddev;
251*4882a593Smuzhiyun 	void *priv;
252*4882a593Smuzhiyun };
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun #define FEATURE_DEV_ID_UNUSED	(-1)
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun /**
257*4882a593Smuzhiyun  * struct dfl_feature_platform_data - platform data for feature devices
258*4882a593Smuzhiyun  *
259*4882a593Smuzhiyun  * @node: node to link feature devs to container device's port_dev_list.
260*4882a593Smuzhiyun  * @lock: mutex to protect platform data.
261*4882a593Smuzhiyun  * @cdev: cdev of feature dev.
262*4882a593Smuzhiyun  * @dev: ptr to platform device linked with this platform data.
263*4882a593Smuzhiyun  * @dfl_cdev: ptr to container device.
264*4882a593Smuzhiyun  * @id: id used for this feature device.
265*4882a593Smuzhiyun  * @disable_count: count for port disable.
266*4882a593Smuzhiyun  * @excl_open: set on feature device exclusive open.
267*4882a593Smuzhiyun  * @open_count: count for feature device open.
268*4882a593Smuzhiyun  * @num: number for sub features.
269*4882a593Smuzhiyun  * @private: ptr to feature dev private data.
270*4882a593Smuzhiyun  * @features: sub features of this feature dev.
271*4882a593Smuzhiyun  */
272*4882a593Smuzhiyun struct dfl_feature_platform_data {
273*4882a593Smuzhiyun 	struct list_head node;
274*4882a593Smuzhiyun 	struct mutex lock;
275*4882a593Smuzhiyun 	struct cdev cdev;
276*4882a593Smuzhiyun 	struct platform_device *dev;
277*4882a593Smuzhiyun 	struct dfl_fpga_cdev *dfl_cdev;
278*4882a593Smuzhiyun 	int id;
279*4882a593Smuzhiyun 	unsigned int disable_count;
280*4882a593Smuzhiyun 	bool excl_open;
281*4882a593Smuzhiyun 	int open_count;
282*4882a593Smuzhiyun 	void *private;
283*4882a593Smuzhiyun 	int num;
284*4882a593Smuzhiyun 	struct dfl_feature features[];
285*4882a593Smuzhiyun };
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun static inline
dfl_feature_dev_use_begin(struct dfl_feature_platform_data * pdata,bool excl)288*4882a593Smuzhiyun int dfl_feature_dev_use_begin(struct dfl_feature_platform_data *pdata,
289*4882a593Smuzhiyun 			      bool excl)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	if (pdata->excl_open)
292*4882a593Smuzhiyun 		return -EBUSY;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	if (excl) {
295*4882a593Smuzhiyun 		if (pdata->open_count)
296*4882a593Smuzhiyun 			return -EBUSY;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 		pdata->excl_open = true;
299*4882a593Smuzhiyun 	}
300*4882a593Smuzhiyun 	pdata->open_count++;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	return 0;
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun static inline
dfl_feature_dev_use_end(struct dfl_feature_platform_data * pdata)306*4882a593Smuzhiyun void dfl_feature_dev_use_end(struct dfl_feature_platform_data *pdata)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	pdata->excl_open = false;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	if (WARN_ON(pdata->open_count <= 0))
311*4882a593Smuzhiyun 		return;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	pdata->open_count--;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun static inline
dfl_feature_dev_use_count(struct dfl_feature_platform_data * pdata)317*4882a593Smuzhiyun int dfl_feature_dev_use_count(struct dfl_feature_platform_data *pdata)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun 	return pdata->open_count;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun static inline
dfl_fpga_pdata_set_private(struct dfl_feature_platform_data * pdata,void * private)323*4882a593Smuzhiyun void dfl_fpga_pdata_set_private(struct dfl_feature_platform_data *pdata,
324*4882a593Smuzhiyun 				void *private)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	pdata->private = private;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun static inline
dfl_fpga_pdata_get_private(struct dfl_feature_platform_data * pdata)330*4882a593Smuzhiyun void *dfl_fpga_pdata_get_private(struct dfl_feature_platform_data *pdata)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	return pdata->private;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun struct dfl_feature_ops {
336*4882a593Smuzhiyun 	int (*init)(struct platform_device *pdev, struct dfl_feature *feature);
337*4882a593Smuzhiyun 	void (*uinit)(struct platform_device *pdev,
338*4882a593Smuzhiyun 		      struct dfl_feature *feature);
339*4882a593Smuzhiyun 	long (*ioctl)(struct platform_device *pdev, struct dfl_feature *feature,
340*4882a593Smuzhiyun 		      unsigned int cmd, unsigned long arg);
341*4882a593Smuzhiyun };
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun #define DFL_FPGA_FEATURE_DEV_FME		"dfl-fme"
344*4882a593Smuzhiyun #define DFL_FPGA_FEATURE_DEV_PORT		"dfl-port"
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun void dfl_fpga_dev_feature_uinit(struct platform_device *pdev);
347*4882a593Smuzhiyun int dfl_fpga_dev_feature_init(struct platform_device *pdev,
348*4882a593Smuzhiyun 			      struct dfl_feature_driver *feature_drvs);
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun int dfl_fpga_dev_ops_register(struct platform_device *pdev,
351*4882a593Smuzhiyun 			      const struct file_operations *fops,
352*4882a593Smuzhiyun 			      struct module *owner);
353*4882a593Smuzhiyun void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun static inline
dfl_fpga_inode_to_feature_dev(struct inode * inode)356*4882a593Smuzhiyun struct platform_device *dfl_fpga_inode_to_feature_dev(struct inode *inode)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun 	struct dfl_feature_platform_data *pdata;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	pdata = container_of(inode->i_cdev, struct dfl_feature_platform_data,
361*4882a593Smuzhiyun 			     cdev);
362*4882a593Smuzhiyun 	return pdata->dev;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun #define dfl_fpga_dev_for_each_feature(pdata, feature)			    \
366*4882a593Smuzhiyun 	for ((feature) = (pdata)->features;				    \
367*4882a593Smuzhiyun 	   (feature) < (pdata)->features + (pdata)->num; (feature)++)
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun static inline
dfl_get_feature_by_id(struct device * dev,u16 id)370*4882a593Smuzhiyun struct dfl_feature *dfl_get_feature_by_id(struct device *dev, u16 id)
371*4882a593Smuzhiyun {
372*4882a593Smuzhiyun 	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
373*4882a593Smuzhiyun 	struct dfl_feature *feature;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	dfl_fpga_dev_for_each_feature(pdata, feature)
376*4882a593Smuzhiyun 		if (feature->id == id)
377*4882a593Smuzhiyun 			return feature;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	return NULL;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun static inline
dfl_get_feature_ioaddr_by_id(struct device * dev,u16 id)383*4882a593Smuzhiyun void __iomem *dfl_get_feature_ioaddr_by_id(struct device *dev, u16 id)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun 	struct dfl_feature *feature = dfl_get_feature_by_id(dev, id);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	if (feature && feature->ioaddr)
388*4882a593Smuzhiyun 		return feature->ioaddr;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	WARN_ON(1);
391*4882a593Smuzhiyun 	return NULL;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun 
is_dfl_feature_present(struct device * dev,u16 id)394*4882a593Smuzhiyun static inline bool is_dfl_feature_present(struct device *dev, u16 id)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun 	return !!dfl_get_feature_ioaddr_by_id(dev, id);
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun static inline
dfl_fpga_pdata_to_parent(struct dfl_feature_platform_data * pdata)400*4882a593Smuzhiyun struct device *dfl_fpga_pdata_to_parent(struct dfl_feature_platform_data *pdata)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun 	return pdata->dev->dev.parent->parent;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
dfl_feature_is_fme(void __iomem * base)405*4882a593Smuzhiyun static inline bool dfl_feature_is_fme(void __iomem *base)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	u64 v = readq(base + DFH);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
410*4882a593Smuzhiyun 		(FIELD_GET(DFH_ID, v) == DFH_ID_FIU_FME);
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun 
dfl_feature_is_port(void __iomem * base)413*4882a593Smuzhiyun static inline bool dfl_feature_is_port(void __iomem *base)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	u64 v = readq(base + DFH);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
418*4882a593Smuzhiyun 		(FIELD_GET(DFH_ID, v) == DFH_ID_FIU_PORT);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
dfl_feature_revision(void __iomem * base)421*4882a593Smuzhiyun static inline u8 dfl_feature_revision(void __iomem *base)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun 	return (u8)FIELD_GET(DFH_REVISION, readq(base + DFH));
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun /**
427*4882a593Smuzhiyun  * struct dfl_fpga_enum_info - DFL FPGA enumeration information
428*4882a593Smuzhiyun  *
429*4882a593Smuzhiyun  * @dev: parent device.
430*4882a593Smuzhiyun  * @dfls: list of device feature lists.
431*4882a593Smuzhiyun  * @nr_irqs: number of irqs for all feature devices.
432*4882a593Smuzhiyun  * @irq_table: Linux IRQ numbers for all irqs, indexed by hw irq numbers.
433*4882a593Smuzhiyun  */
434*4882a593Smuzhiyun struct dfl_fpga_enum_info {
435*4882a593Smuzhiyun 	struct device *dev;
436*4882a593Smuzhiyun 	struct list_head dfls;
437*4882a593Smuzhiyun 	unsigned int nr_irqs;
438*4882a593Smuzhiyun 	int *irq_table;
439*4882a593Smuzhiyun };
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun /**
442*4882a593Smuzhiyun  * struct dfl_fpga_enum_dfl - DFL FPGA enumeration device feature list info
443*4882a593Smuzhiyun  *
444*4882a593Smuzhiyun  * @start: base address of this device feature list.
445*4882a593Smuzhiyun  * @len: size of this device feature list.
446*4882a593Smuzhiyun  * @node: node in list of device feature lists.
447*4882a593Smuzhiyun  */
448*4882a593Smuzhiyun struct dfl_fpga_enum_dfl {
449*4882a593Smuzhiyun 	resource_size_t start;
450*4882a593Smuzhiyun 	resource_size_t len;
451*4882a593Smuzhiyun 	struct list_head node;
452*4882a593Smuzhiyun };
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev);
455*4882a593Smuzhiyun int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
456*4882a593Smuzhiyun 			       resource_size_t start, resource_size_t len);
457*4882a593Smuzhiyun int dfl_fpga_enum_info_add_irq(struct dfl_fpga_enum_info *info,
458*4882a593Smuzhiyun 			       unsigned int nr_irqs, int *irq_table);
459*4882a593Smuzhiyun void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun /**
462*4882a593Smuzhiyun  * struct dfl_fpga_cdev - container device of DFL based FPGA
463*4882a593Smuzhiyun  *
464*4882a593Smuzhiyun  * @parent: parent device of this container device.
465*4882a593Smuzhiyun  * @region: base fpga region.
466*4882a593Smuzhiyun  * @fme_dev: FME feature device under this container device.
467*4882a593Smuzhiyun  * @lock: mutex lock to protect the port device list.
468*4882a593Smuzhiyun  * @port_dev_list: list of all port feature devices under this container device.
469*4882a593Smuzhiyun  * @released_port_num: released port number under this container device.
470*4882a593Smuzhiyun  */
471*4882a593Smuzhiyun struct dfl_fpga_cdev {
472*4882a593Smuzhiyun 	struct device *parent;
473*4882a593Smuzhiyun 	struct fpga_region *region;
474*4882a593Smuzhiyun 	struct device *fme_dev;
475*4882a593Smuzhiyun 	struct mutex lock;
476*4882a593Smuzhiyun 	struct list_head port_dev_list;
477*4882a593Smuzhiyun 	int released_port_num;
478*4882a593Smuzhiyun };
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun struct dfl_fpga_cdev *
481*4882a593Smuzhiyun dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info);
482*4882a593Smuzhiyun void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev);
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun /*
485*4882a593Smuzhiyun  * need to drop the device reference with put_device() after use port platform
486*4882a593Smuzhiyun  * device returned by __dfl_fpga_cdev_find_port and dfl_fpga_cdev_find_port
487*4882a593Smuzhiyun  * functions.
488*4882a593Smuzhiyun  */
489*4882a593Smuzhiyun struct platform_device *
490*4882a593Smuzhiyun __dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
491*4882a593Smuzhiyun 			  int (*match)(struct platform_device *, void *));
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun static inline struct platform_device *
dfl_fpga_cdev_find_port(struct dfl_fpga_cdev * cdev,void * data,int (* match)(struct platform_device *,void *))494*4882a593Smuzhiyun dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
495*4882a593Smuzhiyun 			int (*match)(struct platform_device *, void *))
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun 	struct platform_device *pdev;
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	mutex_lock(&cdev->lock);
500*4882a593Smuzhiyun 	pdev = __dfl_fpga_cdev_find_port(cdev, data, match);
501*4882a593Smuzhiyun 	mutex_unlock(&cdev->lock);
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	return pdev;
504*4882a593Smuzhiyun }
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun int dfl_fpga_cdev_release_port(struct dfl_fpga_cdev *cdev, int port_id);
507*4882a593Smuzhiyun int dfl_fpga_cdev_assign_port(struct dfl_fpga_cdev *cdev, int port_id);
508*4882a593Smuzhiyun void dfl_fpga_cdev_config_ports_pf(struct dfl_fpga_cdev *cdev);
509*4882a593Smuzhiyun int dfl_fpga_cdev_config_ports_vf(struct dfl_fpga_cdev *cdev, int num_vf);
510*4882a593Smuzhiyun int dfl_fpga_set_irq_triggers(struct dfl_feature *feature, unsigned int start,
511*4882a593Smuzhiyun 			      unsigned int count, int32_t *fds);
512*4882a593Smuzhiyun long dfl_feature_ioctl_get_num_irqs(struct platform_device *pdev,
513*4882a593Smuzhiyun 				    struct dfl_feature *feature,
514*4882a593Smuzhiyun 				    unsigned long arg);
515*4882a593Smuzhiyun long dfl_feature_ioctl_set_irq(struct platform_device *pdev,
516*4882a593Smuzhiyun 			       struct dfl_feature *feature,
517*4882a593Smuzhiyun 			       unsigned long arg);
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun /**
520*4882a593Smuzhiyun  * enum dfl_id_type - define the DFL FIU types
521*4882a593Smuzhiyun  */
522*4882a593Smuzhiyun enum dfl_id_type {
523*4882a593Smuzhiyun 	FME_ID,
524*4882a593Smuzhiyun 	PORT_ID,
525*4882a593Smuzhiyun 	DFL_ID_MAX,
526*4882a593Smuzhiyun };
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun /**
529*4882a593Smuzhiyun  * struct dfl_device_id -  dfl device identifier
530*4882a593Smuzhiyun  * @type: contains 4 bits DFL FIU type of the device. See enum dfl_id_type.
531*4882a593Smuzhiyun  * @feature_id: contains 12 bits feature identifier local to its DFL FIU type.
532*4882a593Smuzhiyun  * @driver_data: driver specific data.
533*4882a593Smuzhiyun  */
534*4882a593Smuzhiyun struct dfl_device_id {
535*4882a593Smuzhiyun 	u8 type;
536*4882a593Smuzhiyun 	u16 feature_id;
537*4882a593Smuzhiyun 	unsigned long driver_data;
538*4882a593Smuzhiyun };
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun /**
541*4882a593Smuzhiyun  * struct dfl_device - represent an dfl device on dfl bus
542*4882a593Smuzhiyun  *
543*4882a593Smuzhiyun  * @dev: generic device interface.
544*4882a593Smuzhiyun  * @id: id of the dfl device.
545*4882a593Smuzhiyun  * @type: type of DFL FIU of the device. See enum dfl_id_type.
546*4882a593Smuzhiyun  * @feature_id: 16 bits feature identifier local to its DFL FIU type.
547*4882a593Smuzhiyun  * @mmio_res: mmio resource of this dfl device.
548*4882a593Smuzhiyun  * @irqs: list of Linux IRQ numbers of this dfl device.
549*4882a593Smuzhiyun  * @num_irqs: number of IRQs supported by this dfl device.
550*4882a593Smuzhiyun  * @cdev: pointer to DFL FPGA container device this dfl device belongs to.
551*4882a593Smuzhiyun  * @id_entry: matched id entry in dfl driver's id table.
552*4882a593Smuzhiyun  */
553*4882a593Smuzhiyun struct dfl_device {
554*4882a593Smuzhiyun 	struct device dev;
555*4882a593Smuzhiyun 	int id;
556*4882a593Smuzhiyun 	u8 type;
557*4882a593Smuzhiyun 	u16 feature_id;
558*4882a593Smuzhiyun 	struct resource mmio_res;
559*4882a593Smuzhiyun 	int *irqs;
560*4882a593Smuzhiyun 	unsigned int num_irqs;
561*4882a593Smuzhiyun 	struct dfl_fpga_cdev *cdev;
562*4882a593Smuzhiyun 	const struct dfl_device_id *id_entry;
563*4882a593Smuzhiyun };
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun /**
566*4882a593Smuzhiyun  * struct dfl_driver - represent an dfl device driver
567*4882a593Smuzhiyun  *
568*4882a593Smuzhiyun  * @drv: driver model structure.
569*4882a593Smuzhiyun  * @id_table: pointer to table of device IDs the driver is interested in.
570*4882a593Smuzhiyun  *	      { } member terminated.
571*4882a593Smuzhiyun  * @probe: mandatory callback for device binding.
572*4882a593Smuzhiyun  * @remove: callback for device unbinding.
573*4882a593Smuzhiyun  */
574*4882a593Smuzhiyun struct dfl_driver {
575*4882a593Smuzhiyun 	struct device_driver drv;
576*4882a593Smuzhiyun 	const struct dfl_device_id *id_table;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	int (*probe)(struct dfl_device *dfl_dev);
579*4882a593Smuzhiyun 	void (*remove)(struct dfl_device *dfl_dev);
580*4882a593Smuzhiyun };
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun #define to_dfl_dev(d) container_of(d, struct dfl_device, dev)
583*4882a593Smuzhiyun #define to_dfl_drv(d) container_of(d, struct dfl_driver, drv)
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun /*
586*4882a593Smuzhiyun  * use a macro to avoid include chaining to get THIS_MODULE.
587*4882a593Smuzhiyun  */
588*4882a593Smuzhiyun #define dfl_driver_register(drv) \
589*4882a593Smuzhiyun 	__dfl_driver_register(drv, THIS_MODULE)
590*4882a593Smuzhiyun int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner);
591*4882a593Smuzhiyun void dfl_driver_unregister(struct dfl_driver *dfl_drv);
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun /*
594*4882a593Smuzhiyun  * module_dfl_driver() - Helper macro for drivers that don't do
595*4882a593Smuzhiyun  * anything special in module init/exit.  This eliminates a lot of
596*4882a593Smuzhiyun  * boilerplate.  Each module may only use this macro once, and
597*4882a593Smuzhiyun  * calling it replaces module_init() and module_exit().
598*4882a593Smuzhiyun  */
599*4882a593Smuzhiyun #define module_dfl_driver(__dfl_driver) \
600*4882a593Smuzhiyun 	module_driver(__dfl_driver, dfl_driver_register, \
601*4882a593Smuzhiyun 		      dfl_driver_unregister)
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun #endif /* __FPGA_DFL_H */
604