xref: /linux/drivers/fpga/dfl.h (revision 9326eecd9365a5b82220a4011592f7c0209566fc)
1543be3d8SWu Hao /* SPDX-License-Identifier: GPL-2.0 */
2543be3d8SWu Hao /*
3543be3d8SWu Hao  * Driver Header File for FPGA Device Feature List (DFL) Support
4543be3d8SWu Hao  *
5543be3d8SWu Hao  * Copyright (C) 2017-2018 Intel Corporation, Inc.
6543be3d8SWu Hao  *
7543be3d8SWu Hao  * Authors:
8543be3d8SWu Hao  *   Kang Luwei <luwei.kang@intel.com>
9543be3d8SWu Hao  *   Zhang Yi <yi.z.zhang@intel.com>
10543be3d8SWu Hao  *   Wu Hao <hao.wu@intel.com>
11543be3d8SWu Hao  *   Xiao Guangrong <guangrong.xiao@linux.intel.com>
12543be3d8SWu Hao  */
13543be3d8SWu Hao 
14543be3d8SWu Hao #ifndef __FPGA_DFL_H
15543be3d8SWu Hao #define __FPGA_DFL_H
16543be3d8SWu Hao 
17543be3d8SWu Hao #include <linux/bitfield.h>
18b16c5147SWu Hao #include <linux/cdev.h>
19543be3d8SWu Hao #include <linux/delay.h>
20322b598bSXu Yilun #include <linux/eventfd.h>
21543be3d8SWu Hao #include <linux/fs.h>
228d021039SXu Yilun #include <linux/interrupt.h>
23543be3d8SWu Hao #include <linux/iopoll.h>
24543be3d8SWu Hao #include <linux/io-64-nonatomic-lo-hi.h>
25*9326eecdSXu Yilun #include <linux/mod_devicetable.h>
26543be3d8SWu Hao #include <linux/platform_device.h>
27543be3d8SWu Hao #include <linux/slab.h>
28543be3d8SWu Hao #include <linux/uuid.h>
29543be3d8SWu Hao #include <linux/fpga/fpga-region.h>
30543be3d8SWu Hao 
31543be3d8SWu Hao /* maximum supported number of ports */
32543be3d8SWu Hao #define MAX_DFL_FPGA_PORT_NUM 4
33543be3d8SWu Hao /* plus one for fme device */
34543be3d8SWu Hao #define MAX_DFL_FEATURE_DEV_NUM    (MAX_DFL_FPGA_PORT_NUM + 1)
35543be3d8SWu Hao 
3615bbb300SWu Hao /* Reserved 0xfe for Header Group Register and 0xff for AFU */
3715bbb300SWu Hao #define FEATURE_ID_FIU_HEADER		0xfe
38543be3d8SWu Hao #define FEATURE_ID_AFU			0xff
39543be3d8SWu Hao 
40543be3d8SWu Hao #define FME_FEATURE_ID_HEADER		FEATURE_ID_FIU_HEADER
41543be3d8SWu Hao #define FME_FEATURE_ID_THERMAL_MGMT	0x1
42543be3d8SWu Hao #define FME_FEATURE_ID_POWER_MGMT	0x2
43543be3d8SWu Hao #define FME_FEATURE_ID_GLOBAL_IPERF	0x3
44543be3d8SWu Hao #define FME_FEATURE_ID_GLOBAL_ERR	0x4
45543be3d8SWu Hao #define FME_FEATURE_ID_PR_MGMT		0x5
46543be3d8SWu Hao #define FME_FEATURE_ID_HSSI		0x6
47543be3d8SWu Hao #define FME_FEATURE_ID_GLOBAL_DPERF	0x7
48543be3d8SWu Hao 
49543be3d8SWu Hao #define PORT_FEATURE_ID_HEADER		FEATURE_ID_FIU_HEADER
50543be3d8SWu Hao #define PORT_FEATURE_ID_AFU		FEATURE_ID_AFU
51543be3d8SWu Hao #define PORT_FEATURE_ID_ERROR		0x10
52543be3d8SWu Hao #define PORT_FEATURE_ID_UMSG		0x11
53543be3d8SWu Hao #define PORT_FEATURE_ID_UINT		0x12
54543be3d8SWu Hao #define PORT_FEATURE_ID_STP		0x13
55543be3d8SWu Hao 
56543be3d8SWu Hao /*
57543be3d8SWu Hao  * Device Feature Header Register Set
58543be3d8SWu Hao  *
59543be3d8SWu Hao  * For FIUs, they all have DFH + GUID + NEXT_AFU as common header registers.
60543be3d8SWu Hao  * For AFUs, they have DFH + GUID as common header registers.
61543be3d8SWu Hao  * For private features, they only have DFH register as common header.
62543be3d8SWu Hao  */
63543be3d8SWu Hao #define DFH			0x0
64543be3d8SWu Hao #define GUID_L			0x8
65543be3d8SWu Hao #define GUID_H			0x10
66543be3d8SWu Hao #define NEXT_AFU		0x18
67543be3d8SWu Hao 
68543be3d8SWu Hao #define DFH_SIZE		0x8
69543be3d8SWu Hao 
70543be3d8SWu Hao /* Device Feature Header Register Bitfield */
71543be3d8SWu Hao #define DFH_ID			GENMASK_ULL(11, 0)	/* Feature ID */
72543be3d8SWu Hao #define DFH_ID_FIU_FME		0
73543be3d8SWu Hao #define DFH_ID_FIU_PORT		1
74543be3d8SWu Hao #define DFH_REVISION		GENMASK_ULL(15, 12)	/* Feature revision */
75543be3d8SWu Hao #define DFH_NEXT_HDR_OFST	GENMASK_ULL(39, 16)	/* Offset to next DFH */
76543be3d8SWu Hao #define DFH_EOL			BIT_ULL(40)		/* End of list */
77543be3d8SWu Hao #define DFH_TYPE		GENMASK_ULL(63, 60)	/* Feature type */
78543be3d8SWu Hao #define DFH_TYPE_AFU		1
79543be3d8SWu Hao #define DFH_TYPE_PRIVATE	3
80543be3d8SWu Hao #define DFH_TYPE_FIU		4
81543be3d8SWu Hao 
82543be3d8SWu Hao /* Next AFU Register Bitfield */
83543be3d8SWu Hao #define NEXT_AFU_NEXT_DFH_OFST	GENMASK_ULL(23, 0)	/* Offset to next AFU */
84543be3d8SWu Hao 
85543be3d8SWu Hao /* FME Header Register Set */
86543be3d8SWu Hao #define FME_HDR_DFH		DFH
87543be3d8SWu Hao #define FME_HDR_GUID_L		GUID_L
88543be3d8SWu Hao #define FME_HDR_GUID_H		GUID_H
89543be3d8SWu Hao #define FME_HDR_NEXT_AFU	NEXT_AFU
90543be3d8SWu Hao #define FME_HDR_CAP		0x30
91543be3d8SWu Hao #define FME_HDR_PORT_OFST(n)	(0x38 + ((n) * 0x8))
92543be3d8SWu Hao #define FME_HDR_BITSTREAM_ID	0x60
93543be3d8SWu Hao #define FME_HDR_BITSTREAM_MD	0x68
94543be3d8SWu Hao 
95543be3d8SWu Hao /* FME Fab Capability Register Bitfield */
96543be3d8SWu Hao #define FME_CAP_FABRIC_VERID	GENMASK_ULL(7, 0)	/* Fabric version ID */
97543be3d8SWu Hao #define FME_CAP_SOCKET_ID	BIT_ULL(8)		/* Socket ID */
98543be3d8SWu Hao #define FME_CAP_PCIE0_LINK_AVL	BIT_ULL(12)		/* PCIE0 Link */
99543be3d8SWu Hao #define FME_CAP_PCIE1_LINK_AVL	BIT_ULL(13)		/* PCIE1 Link */
100543be3d8SWu Hao #define FME_CAP_COHR_LINK_AVL	BIT_ULL(14)		/* Coherent Link */
101543be3d8SWu Hao #define FME_CAP_IOMMU_AVL	BIT_ULL(16)		/* IOMMU available */
102543be3d8SWu Hao #define FME_CAP_NUM_PORTS	GENMASK_ULL(19, 17)	/* Number of ports */
103543be3d8SWu Hao #define FME_CAP_ADDR_WIDTH	GENMASK_ULL(29, 24)	/* Address bus width */
104543be3d8SWu Hao #define FME_CAP_CACHE_SIZE	GENMASK_ULL(43, 32)	/* cache size in KB */
105543be3d8SWu Hao #define FME_CAP_CACHE_ASSOC	GENMASK_ULL(47, 44)	/* Associativity */
106543be3d8SWu Hao 
107543be3d8SWu Hao /* FME Port Offset Register Bitfield */
108543be3d8SWu Hao /* Offset to port device feature header */
109543be3d8SWu Hao #define FME_PORT_OFST_DFH_OFST	GENMASK_ULL(23, 0)
110543be3d8SWu Hao /* PCI Bar ID for this port */
111543be3d8SWu Hao #define FME_PORT_OFST_BAR_ID	GENMASK_ULL(34, 32)
112543be3d8SWu Hao /* AFU MMIO access permission. 1 - VF, 0 - PF. */
113543be3d8SWu Hao #define FME_PORT_OFST_ACC_CTRL	BIT_ULL(55)
114543be3d8SWu Hao #define FME_PORT_OFST_ACC_PF	0
115543be3d8SWu Hao #define FME_PORT_OFST_ACC_VF	1
116543be3d8SWu Hao #define FME_PORT_OFST_IMP	BIT_ULL(60)
117543be3d8SWu Hao 
1188d021039SXu Yilun /* FME Error Capability Register */
1198d021039SXu Yilun #define FME_ERROR_CAP		0x70
1208d021039SXu Yilun 
1218d021039SXu Yilun /* FME Error Capability Register Bitfield */
1228d021039SXu Yilun #define FME_ERROR_CAP_SUPP_INT	BIT_ULL(0)		/* Interrupt Support */
1238d021039SXu Yilun #define FME_ERROR_CAP_INT_VECT	GENMASK_ULL(12, 1)	/* Interrupt vector */
1248d021039SXu Yilun 
125543be3d8SWu Hao /* PORT Header Register Set */
126543be3d8SWu Hao #define PORT_HDR_DFH		DFH
127543be3d8SWu Hao #define PORT_HDR_GUID_L		GUID_L
128543be3d8SWu Hao #define PORT_HDR_GUID_H		GUID_H
129543be3d8SWu Hao #define PORT_HDR_NEXT_AFU	NEXT_AFU
130543be3d8SWu Hao #define PORT_HDR_CAP		0x30
131543be3d8SWu Hao #define PORT_HDR_CTRL		0x38
132d2ad5ac1SWu Hao #define PORT_HDR_STS		0x40
133f09991adSWu Hao #define PORT_HDR_USRCLK_CMD0	0x50
134f09991adSWu Hao #define PORT_HDR_USRCLK_CMD1	0x58
135f09991adSWu Hao #define PORT_HDR_USRCLK_STS0	0x60
136f09991adSWu Hao #define PORT_HDR_USRCLK_STS1	0x68
137543be3d8SWu Hao 
138543be3d8SWu Hao /* Port Capability Register Bitfield */
139543be3d8SWu Hao #define PORT_CAP_PORT_NUM	GENMASK_ULL(1, 0)	/* ID of this port */
140543be3d8SWu Hao #define PORT_CAP_MMIO_SIZE	GENMASK_ULL(23, 8)	/* MMIO size in KB */
141543be3d8SWu Hao #define PORT_CAP_SUPP_INT_NUM	GENMASK_ULL(35, 32)	/* Interrupts num */
142543be3d8SWu Hao 
143543be3d8SWu Hao /* Port Control Register Bitfield */
144543be3d8SWu Hao #define PORT_CTRL_SFTRST	BIT_ULL(0)		/* Port soft reset */
145543be3d8SWu Hao /* Latency tolerance reporting. '1' >= 40us, '0' < 40us.*/
146543be3d8SWu Hao #define PORT_CTRL_LATENCY	BIT_ULL(2)
147543be3d8SWu Hao #define PORT_CTRL_SFTRST_ACK	BIT_ULL(4)		/* HW ack for reset */
148d2ad5ac1SWu Hao 
149d2ad5ac1SWu Hao /* Port Status Register Bitfield */
150d2ad5ac1SWu Hao #define PORT_STS_AP2_EVT	BIT_ULL(13)		/* AP2 event detected */
151d2ad5ac1SWu Hao #define PORT_STS_AP1_EVT	BIT_ULL(12)		/* AP1 event detected */
152d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE	GENMASK_ULL(11, 8)	/* AFU power states */
153d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_NORM 0
154d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_AP1	1			/* 50% throttling */
155d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_AP2	2			/* 90% throttling */
156d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_AP6	6			/* 100% throttling */
157d2ad5ac1SWu Hao 
1588d021039SXu Yilun /* Port Error Capability Register */
1598d021039SXu Yilun #define PORT_ERROR_CAP		0x38
1608d021039SXu Yilun 
1618d021039SXu Yilun /* Port Error Capability Register Bitfield */
1628d021039SXu Yilun #define PORT_ERROR_CAP_SUPP_INT	BIT_ULL(0)		/* Interrupt Support */
1638d021039SXu Yilun #define PORT_ERROR_CAP_INT_VECT	GENMASK_ULL(12, 1)	/* Interrupt vector */
1648d021039SXu Yilun 
1658d021039SXu Yilun /* Port Uint Capability Register */
1668d021039SXu Yilun #define PORT_UINT_CAP		0x8
1678d021039SXu Yilun 
1688d021039SXu Yilun /* Port Uint Capability Register Bitfield */
1698d021039SXu Yilun #define PORT_UINT_CAP_INT_NUM	GENMASK_ULL(11, 0)	/* Interrupts num */
1708d021039SXu Yilun #define PORT_UINT_CAP_FST_VECT	GENMASK_ULL(23, 12)	/* First Vector */
1718d021039SXu Yilun 
1726e8fd6e4SWu Hao /**
1736e8fd6e4SWu Hao  * struct dfl_fpga_port_ops - port ops
1746e8fd6e4SWu Hao  *
1756e8fd6e4SWu Hao  * @name: name of this port ops, to match with port platform device.
1766e8fd6e4SWu Hao  * @owner: pointer to the module which owns this port ops.
1776e8fd6e4SWu Hao  * @node: node to link port ops to global list.
1786e8fd6e4SWu Hao  * @get_id: get port id from hardware.
1796e8fd6e4SWu Hao  * @enable_set: enable/disable the port.
1806e8fd6e4SWu Hao  */
1816e8fd6e4SWu Hao struct dfl_fpga_port_ops {
1826e8fd6e4SWu Hao 	const char *name;
1836e8fd6e4SWu Hao 	struct module *owner;
1846e8fd6e4SWu Hao 	struct list_head node;
1856e8fd6e4SWu Hao 	int (*get_id)(struct platform_device *pdev);
1866e8fd6e4SWu Hao 	int (*enable_set)(struct platform_device *pdev, bool enable);
1876e8fd6e4SWu Hao };
1886e8fd6e4SWu Hao 
1896e8fd6e4SWu Hao void dfl_fpga_port_ops_add(struct dfl_fpga_port_ops *ops);
1906e8fd6e4SWu Hao void dfl_fpga_port_ops_del(struct dfl_fpga_port_ops *ops);
1916e8fd6e4SWu Hao struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct platform_device *pdev);
1926e8fd6e4SWu Hao void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops *ops);
193d06b004bSWu Hao int dfl_fpga_check_port_id(struct platform_device *pdev, void *pport_id);
194543be3d8SWu Hao 
195543be3d8SWu Hao /**
19615bbb300SWu Hao  * struct dfl_feature_id - dfl private feature id
1975b57d02aSXiao Guangrong  *
19815bbb300SWu Hao  * @id: unique dfl private feature id.
19915bbb300SWu Hao  */
20015bbb300SWu Hao struct dfl_feature_id {
2018a5de2deSXu Yilun 	u16 id;
20215bbb300SWu Hao };
20315bbb300SWu Hao 
20415bbb300SWu Hao /**
20515bbb300SWu Hao  * struct dfl_feature_driver - dfl private feature driver
20615bbb300SWu Hao  *
20715bbb300SWu Hao  * @id_table: id_table for dfl private features supported by this driver.
20815bbb300SWu Hao  * @ops: ops of this dfl private feature driver.
2095b57d02aSXiao Guangrong  */
2105b57d02aSXiao Guangrong struct dfl_feature_driver {
21115bbb300SWu Hao 	const struct dfl_feature_id *id_table;
2125b57d02aSXiao Guangrong 	const struct dfl_feature_ops *ops;
2135b57d02aSXiao Guangrong };
2145b57d02aSXiao Guangrong 
2155b57d02aSXiao Guangrong /**
2168d021039SXu Yilun  * struct dfl_feature_irq_ctx - dfl private feature interrupt context
2178d021039SXu Yilun  *
2188d021039SXu Yilun  * @irq: Linux IRQ number of this interrupt.
219322b598bSXu Yilun  * @trigger: eventfd context to signal when interrupt happens.
220322b598bSXu Yilun  * @name: irq name needed when requesting irq.
2218d021039SXu Yilun  */
2228d021039SXu Yilun struct dfl_feature_irq_ctx {
2238d021039SXu Yilun 	int irq;
224322b598bSXu Yilun 	struct eventfd_ctx *trigger;
225322b598bSXu Yilun 	char *name;
2268d021039SXu Yilun };
2278d021039SXu Yilun 
2288d021039SXu Yilun /**
229543be3d8SWu Hao  * struct dfl_feature - sub feature of the feature devices
230543be3d8SWu Hao  *
231322b598bSXu Yilun  * @dev: ptr to pdev of the feature device which has the sub feature.
232543be3d8SWu Hao  * @id: sub feature id.
233543be3d8SWu Hao  * @resource_index: each sub feature has one mmio resource for its registers.
234543be3d8SWu Hao  *		    this index is used to find its mmio resource from the
235543be3d8SWu Hao  *		    feature dev (platform device)'s reources.
236543be3d8SWu Hao  * @ioaddr: mapped mmio resource address.
2378d021039SXu Yilun  * @irq_ctx: interrupt context list.
2388d021039SXu Yilun  * @nr_irqs: number of interrupt contexts.
2395b57d02aSXiao Guangrong  * @ops: ops of this sub feature.
2409ba3a0aaSXu Yilun  * @ddev: ptr to the dfl device of this sub feature.
241724142f8SWu Hao  * @priv: priv data of this feature.
242543be3d8SWu Hao  */
243543be3d8SWu Hao struct dfl_feature {
244322b598bSXu Yilun 	struct platform_device *dev;
2458a5de2deSXu Yilun 	u16 id;
246543be3d8SWu Hao 	int resource_index;
247543be3d8SWu Hao 	void __iomem *ioaddr;
2488d021039SXu Yilun 	struct dfl_feature_irq_ctx *irq_ctx;
2498d021039SXu Yilun 	unsigned int nr_irqs;
2505b57d02aSXiao Guangrong 	const struct dfl_feature_ops *ops;
2519ba3a0aaSXu Yilun 	struct dfl_device *ddev;
252724142f8SWu Hao 	void *priv;
253543be3d8SWu Hao };
254543be3d8SWu Hao 
25569bb18ddSWu Hao #define FEATURE_DEV_ID_UNUSED	(-1)
25669bb18ddSWu Hao 
257543be3d8SWu Hao /**
258543be3d8SWu Hao  * struct dfl_feature_platform_data - platform data for feature devices
259543be3d8SWu Hao  *
260543be3d8SWu Hao  * @node: node to link feature devs to container device's port_dev_list.
261543be3d8SWu Hao  * @lock: mutex to protect platform data.
262b16c5147SWu Hao  * @cdev: cdev of feature dev.
263543be3d8SWu Hao  * @dev: ptr to platform device linked with this platform data.
264543be3d8SWu Hao  * @dfl_cdev: ptr to container device.
26569bb18ddSWu Hao  * @id: id used for this feature device.
266543be3d8SWu Hao  * @disable_count: count for port disable.
267b6862193SXu Yilun  * @excl_open: set on feature device exclusive open.
268b6862193SXu Yilun  * @open_count: count for feature device open.
269543be3d8SWu Hao  * @num: number for sub features.
2705b57d02aSXiao Guangrong  * @private: ptr to feature dev private data.
271543be3d8SWu Hao  * @features: sub features of this feature dev.
272543be3d8SWu Hao  */
273543be3d8SWu Hao struct dfl_feature_platform_data {
274543be3d8SWu Hao 	struct list_head node;
275543be3d8SWu Hao 	struct mutex lock;
276b16c5147SWu Hao 	struct cdev cdev;
277543be3d8SWu Hao 	struct platform_device *dev;
278543be3d8SWu Hao 	struct dfl_fpga_cdev *dfl_cdev;
27969bb18ddSWu Hao 	int id;
280543be3d8SWu Hao 	unsigned int disable_count;
281b6862193SXu Yilun 	bool excl_open;
282b6862193SXu Yilun 	int open_count;
2835b57d02aSXiao Guangrong 	void *private;
284543be3d8SWu Hao 	int num;
2855a538815SGustavo A. R. Silva 	struct dfl_feature features[];
286543be3d8SWu Hao };
287543be3d8SWu Hao 
2885b57d02aSXiao Guangrong static inline
289b6862193SXu Yilun int dfl_feature_dev_use_begin(struct dfl_feature_platform_data *pdata,
290b6862193SXu Yilun 			      bool excl)
2915b57d02aSXiao Guangrong {
292b6862193SXu Yilun 	if (pdata->excl_open)
2935b57d02aSXiao Guangrong 		return -EBUSY;
2945b57d02aSXiao Guangrong 
295b6862193SXu Yilun 	if (excl) {
296b6862193SXu Yilun 		if (pdata->open_count)
297b6862193SXu Yilun 			return -EBUSY;
298b6862193SXu Yilun 
299b6862193SXu Yilun 		pdata->excl_open = true;
300b6862193SXu Yilun 	}
301b6862193SXu Yilun 	pdata->open_count++;
302b6862193SXu Yilun 
3035b57d02aSXiao Guangrong 	return 0;
3045b57d02aSXiao Guangrong }
3055b57d02aSXiao Guangrong 
3065b57d02aSXiao Guangrong static inline
3075b57d02aSXiao Guangrong void dfl_feature_dev_use_end(struct dfl_feature_platform_data *pdata)
3085b57d02aSXiao Guangrong {
309b6862193SXu Yilun 	pdata->excl_open = false;
310b6862193SXu Yilun 
311b6862193SXu Yilun 	if (WARN_ON(pdata->open_count <= 0))
312b6862193SXu Yilun 		return;
313b6862193SXu Yilun 
314b6862193SXu Yilun 	pdata->open_count--;
315b6862193SXu Yilun }
316b6862193SXu Yilun 
317b6862193SXu Yilun static inline
318b6862193SXu Yilun int dfl_feature_dev_use_count(struct dfl_feature_platform_data *pdata)
319b6862193SXu Yilun {
320b6862193SXu Yilun 	return pdata->open_count;
3215b57d02aSXiao Guangrong }
3225b57d02aSXiao Guangrong 
3235b57d02aSXiao Guangrong static inline
3245b57d02aSXiao Guangrong void dfl_fpga_pdata_set_private(struct dfl_feature_platform_data *pdata,
3255b57d02aSXiao Guangrong 				void *private)
3265b57d02aSXiao Guangrong {
3275b57d02aSXiao Guangrong 	pdata->private = private;
3285b57d02aSXiao Guangrong }
3295b57d02aSXiao Guangrong 
3305b57d02aSXiao Guangrong static inline
3315b57d02aSXiao Guangrong void *dfl_fpga_pdata_get_private(struct dfl_feature_platform_data *pdata)
3325b57d02aSXiao Guangrong {
3335b57d02aSXiao Guangrong 	return pdata->private;
3345b57d02aSXiao Guangrong }
3355b57d02aSXiao Guangrong 
3365b57d02aSXiao Guangrong struct dfl_feature_ops {
3375b57d02aSXiao Guangrong 	int (*init)(struct platform_device *pdev, struct dfl_feature *feature);
3385b57d02aSXiao Guangrong 	void (*uinit)(struct platform_device *pdev,
3395b57d02aSXiao Guangrong 		      struct dfl_feature *feature);
3405b57d02aSXiao Guangrong 	long (*ioctl)(struct platform_device *pdev, struct dfl_feature *feature,
3415b57d02aSXiao Guangrong 		      unsigned int cmd, unsigned long arg);
3425b57d02aSXiao Guangrong };
3435b57d02aSXiao Guangrong 
344543be3d8SWu Hao #define DFL_FPGA_FEATURE_DEV_FME		"dfl-fme"
345543be3d8SWu Hao #define DFL_FPGA_FEATURE_DEV_PORT		"dfl-port"
346543be3d8SWu Hao 
3475b57d02aSXiao Guangrong void dfl_fpga_dev_feature_uinit(struct platform_device *pdev);
3485b57d02aSXiao Guangrong int dfl_fpga_dev_feature_init(struct platform_device *pdev,
3495b57d02aSXiao Guangrong 			      struct dfl_feature_driver *feature_drvs);
3505b57d02aSXiao Guangrong 
351b16c5147SWu Hao int dfl_fpga_dev_ops_register(struct platform_device *pdev,
352b16c5147SWu Hao 			      const struct file_operations *fops,
353b16c5147SWu Hao 			      struct module *owner);
354b16c5147SWu Hao void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
355b16c5147SWu Hao 
3565b57d02aSXiao Guangrong static inline
3575b57d02aSXiao Guangrong struct platform_device *dfl_fpga_inode_to_feature_dev(struct inode *inode)
3585b57d02aSXiao Guangrong {
3595b57d02aSXiao Guangrong 	struct dfl_feature_platform_data *pdata;
3605b57d02aSXiao Guangrong 
3615b57d02aSXiao Guangrong 	pdata = container_of(inode->i_cdev, struct dfl_feature_platform_data,
3625b57d02aSXiao Guangrong 			     cdev);
3635b57d02aSXiao Guangrong 	return pdata->dev;
3645b57d02aSXiao Guangrong }
3655b57d02aSXiao Guangrong 
366543be3d8SWu Hao #define dfl_fpga_dev_for_each_feature(pdata, feature)			    \
367543be3d8SWu Hao 	for ((feature) = (pdata)->features;				    \
368543be3d8SWu Hao 	   (feature) < (pdata)->features + (pdata)->num; (feature)++)
369543be3d8SWu Hao 
370543be3d8SWu Hao static inline
3718a5de2deSXu Yilun struct dfl_feature *dfl_get_feature_by_id(struct device *dev, u16 id)
372543be3d8SWu Hao {
373543be3d8SWu Hao 	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
374543be3d8SWu Hao 	struct dfl_feature *feature;
375543be3d8SWu Hao 
376543be3d8SWu Hao 	dfl_fpga_dev_for_each_feature(pdata, feature)
377543be3d8SWu Hao 		if (feature->id == id)
378543be3d8SWu Hao 			return feature;
379543be3d8SWu Hao 
380543be3d8SWu Hao 	return NULL;
381543be3d8SWu Hao }
382543be3d8SWu Hao 
383543be3d8SWu Hao static inline
3848a5de2deSXu Yilun void __iomem *dfl_get_feature_ioaddr_by_id(struct device *dev, u16 id)
385543be3d8SWu Hao {
386543be3d8SWu Hao 	struct dfl_feature *feature = dfl_get_feature_by_id(dev, id);
387543be3d8SWu Hao 
388543be3d8SWu Hao 	if (feature && feature->ioaddr)
389543be3d8SWu Hao 		return feature->ioaddr;
390543be3d8SWu Hao 
391543be3d8SWu Hao 	WARN_ON(1);
392543be3d8SWu Hao 	return NULL;
393543be3d8SWu Hao }
394543be3d8SWu Hao 
3958a5de2deSXu Yilun static inline bool is_dfl_feature_present(struct device *dev, u16 id)
3965b57d02aSXiao Guangrong {
3975b57d02aSXiao Guangrong 	return !!dfl_get_feature_ioaddr_by_id(dev, id);
3985b57d02aSXiao Guangrong }
3995b57d02aSXiao Guangrong 
4005b57d02aSXiao Guangrong static inline
4015b57d02aSXiao Guangrong struct device *dfl_fpga_pdata_to_parent(struct dfl_feature_platform_data *pdata)
4025b57d02aSXiao Guangrong {
4035b57d02aSXiao Guangrong 	return pdata->dev->dev.parent->parent;
4045b57d02aSXiao Guangrong }
4055b57d02aSXiao Guangrong 
406543be3d8SWu Hao static inline bool dfl_feature_is_fme(void __iomem *base)
407543be3d8SWu Hao {
408543be3d8SWu Hao 	u64 v = readq(base + DFH);
409543be3d8SWu Hao 
410543be3d8SWu Hao 	return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
411543be3d8SWu Hao 		(FIELD_GET(DFH_ID, v) == DFH_ID_FIU_FME);
412543be3d8SWu Hao }
413543be3d8SWu Hao 
414543be3d8SWu Hao static inline bool dfl_feature_is_port(void __iomem *base)
415543be3d8SWu Hao {
416543be3d8SWu Hao 	u64 v = readq(base + DFH);
417543be3d8SWu Hao 
418543be3d8SWu Hao 	return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
419543be3d8SWu Hao 		(FIELD_GET(DFH_ID, v) == DFH_ID_FIU_PORT);
420543be3d8SWu Hao }
421543be3d8SWu Hao 
422f09991adSWu Hao static inline u8 dfl_feature_revision(void __iomem *base)
423f09991adSWu Hao {
424f09991adSWu Hao 	return (u8)FIELD_GET(DFH_REVISION, readq(base + DFH));
425f09991adSWu Hao }
426f09991adSWu Hao 
427543be3d8SWu Hao /**
428543be3d8SWu Hao  * struct dfl_fpga_enum_info - DFL FPGA enumeration information
429543be3d8SWu Hao  *
430543be3d8SWu Hao  * @dev: parent device.
431543be3d8SWu Hao  * @dfls: list of device feature lists.
4328d021039SXu Yilun  * @nr_irqs: number of irqs for all feature devices.
4338d021039SXu Yilun  * @irq_table: Linux IRQ numbers for all irqs, indexed by hw irq numbers.
434543be3d8SWu Hao  */
435543be3d8SWu Hao struct dfl_fpga_enum_info {
436543be3d8SWu Hao 	struct device *dev;
437543be3d8SWu Hao 	struct list_head dfls;
4388d021039SXu Yilun 	unsigned int nr_irqs;
4398d021039SXu Yilun 	int *irq_table;
440543be3d8SWu Hao };
441543be3d8SWu Hao 
442543be3d8SWu Hao /**
443543be3d8SWu Hao  * struct dfl_fpga_enum_dfl - DFL FPGA enumeration device feature list info
444543be3d8SWu Hao  *
445543be3d8SWu Hao  * @start: base address of this device feature list.
446543be3d8SWu Hao  * @len: size of this device feature list.
447543be3d8SWu Hao  * @node: node in list of device feature lists.
448543be3d8SWu Hao  */
449543be3d8SWu Hao struct dfl_fpga_enum_dfl {
450543be3d8SWu Hao 	resource_size_t start;
451543be3d8SWu Hao 	resource_size_t len;
452543be3d8SWu Hao 	struct list_head node;
453543be3d8SWu Hao };
454543be3d8SWu Hao 
455543be3d8SWu Hao struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev);
456543be3d8SWu Hao int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
45789eb35e8SXu Yilun 			       resource_size_t start, resource_size_t len);
4588d021039SXu Yilun int dfl_fpga_enum_info_add_irq(struct dfl_fpga_enum_info *info,
4598d021039SXu Yilun 			       unsigned int nr_irqs, int *irq_table);
460543be3d8SWu Hao void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info);
461543be3d8SWu Hao 
462543be3d8SWu Hao /**
463543be3d8SWu Hao  * struct dfl_fpga_cdev - container device of DFL based FPGA
464543be3d8SWu Hao  *
465543be3d8SWu Hao  * @parent: parent device of this container device.
466543be3d8SWu Hao  * @region: base fpga region.
467543be3d8SWu Hao  * @fme_dev: FME feature device under this container device.
468543be3d8SWu Hao  * @lock: mutex lock to protect the port device list.
469543be3d8SWu Hao  * @port_dev_list: list of all port feature devices under this container device.
47069bb18ddSWu Hao  * @released_port_num: released port number under this container device.
471543be3d8SWu Hao  */
472543be3d8SWu Hao struct dfl_fpga_cdev {
473543be3d8SWu Hao 	struct device *parent;
474543be3d8SWu Hao 	struct fpga_region *region;
475543be3d8SWu Hao 	struct device *fme_dev;
476543be3d8SWu Hao 	struct mutex lock;
477543be3d8SWu Hao 	struct list_head port_dev_list;
47869bb18ddSWu Hao 	int released_port_num;
479543be3d8SWu Hao };
480543be3d8SWu Hao 
481543be3d8SWu Hao struct dfl_fpga_cdev *
482543be3d8SWu Hao dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info);
483543be3d8SWu Hao void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev);
484543be3d8SWu Hao 
4855d56e117SWu Hao /*
4865d56e117SWu Hao  * need to drop the device reference with put_device() after use port platform
4875d56e117SWu Hao  * device returned by __dfl_fpga_cdev_find_port and dfl_fpga_cdev_find_port
4885d56e117SWu Hao  * functions.
4895d56e117SWu Hao  */
4905d56e117SWu Hao struct platform_device *
4915d56e117SWu Hao __dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
4925d56e117SWu Hao 			  int (*match)(struct platform_device *, void *));
4935d56e117SWu Hao 
4945d56e117SWu Hao static inline struct platform_device *
4955d56e117SWu Hao dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
4965d56e117SWu Hao 			int (*match)(struct platform_device *, void *))
4975d56e117SWu Hao {
4985d56e117SWu Hao 	struct platform_device *pdev;
4995d56e117SWu Hao 
5005d56e117SWu Hao 	mutex_lock(&cdev->lock);
5015d56e117SWu Hao 	pdev = __dfl_fpga_cdev_find_port(cdev, data, match);
5025d56e117SWu Hao 	mutex_unlock(&cdev->lock);
5035d56e117SWu Hao 
5045d56e117SWu Hao 	return pdev;
5055d56e117SWu Hao }
50669bb18ddSWu Hao 
50769bb18ddSWu Hao int dfl_fpga_cdev_release_port(struct dfl_fpga_cdev *cdev, int port_id);
50869bb18ddSWu Hao int dfl_fpga_cdev_assign_port(struct dfl_fpga_cdev *cdev, int port_id);
509bdd4f307SWu Hao void dfl_fpga_cdev_config_ports_pf(struct dfl_fpga_cdev *cdev);
510bdd4f307SWu Hao int dfl_fpga_cdev_config_ports_vf(struct dfl_fpga_cdev *cdev, int num_vf);
511322b598bSXu Yilun int dfl_fpga_set_irq_triggers(struct dfl_feature *feature, unsigned int start,
512322b598bSXu Yilun 			      unsigned int count, int32_t *fds);
513322b598bSXu Yilun long dfl_feature_ioctl_get_num_irqs(struct platform_device *pdev,
514322b598bSXu Yilun 				    struct dfl_feature *feature,
515322b598bSXu Yilun 				    unsigned long arg);
516322b598bSXu Yilun long dfl_feature_ioctl_set_irq(struct platform_device *pdev,
517322b598bSXu Yilun 			       struct dfl_feature *feature,
518322b598bSXu Yilun 			       unsigned long arg);
519322b598bSXu Yilun 
5209ba3a0aaSXu Yilun /**
5219ba3a0aaSXu Yilun  * enum dfl_id_type - define the DFL FIU types
5229ba3a0aaSXu Yilun  */
5239ba3a0aaSXu Yilun enum dfl_id_type {
524e08b9e6dSXu Yilun 	FME_ID = 0,
525e08b9e6dSXu Yilun 	PORT_ID = 1,
5269ba3a0aaSXu Yilun 	DFL_ID_MAX,
5279ba3a0aaSXu Yilun };
5289ba3a0aaSXu Yilun 
5299ba3a0aaSXu Yilun /**
5309ba3a0aaSXu Yilun  * struct dfl_device - represent an dfl device on dfl bus
5319ba3a0aaSXu Yilun  *
5329ba3a0aaSXu Yilun  * @dev: generic device interface.
5339ba3a0aaSXu Yilun  * @id: id of the dfl device.
5349ba3a0aaSXu Yilun  * @type: type of DFL FIU of the device. See enum dfl_id_type.
535e08b9e6dSXu Yilun  * @feature_id: feature identifier local to its DFL FIU type.
5369ba3a0aaSXu Yilun  * @mmio_res: mmio resource of this dfl device.
5379ba3a0aaSXu Yilun  * @irqs: list of Linux IRQ numbers of this dfl device.
5389ba3a0aaSXu Yilun  * @num_irqs: number of IRQs supported by this dfl device.
5399ba3a0aaSXu Yilun  * @cdev: pointer to DFL FPGA container device this dfl device belongs to.
5409ba3a0aaSXu Yilun  * @id_entry: matched id entry in dfl driver's id table.
5419ba3a0aaSXu Yilun  */
5429ba3a0aaSXu Yilun struct dfl_device {
5439ba3a0aaSXu Yilun 	struct device dev;
5449ba3a0aaSXu Yilun 	int id;
545e08b9e6dSXu Yilun 	u16 type;
5469ba3a0aaSXu Yilun 	u16 feature_id;
5479ba3a0aaSXu Yilun 	struct resource mmio_res;
5489ba3a0aaSXu Yilun 	int *irqs;
5499ba3a0aaSXu Yilun 	unsigned int num_irqs;
5509ba3a0aaSXu Yilun 	struct dfl_fpga_cdev *cdev;
5519ba3a0aaSXu Yilun 	const struct dfl_device_id *id_entry;
5529ba3a0aaSXu Yilun };
5539ba3a0aaSXu Yilun 
5549ba3a0aaSXu Yilun /**
5559ba3a0aaSXu Yilun  * struct dfl_driver - represent an dfl device driver
5569ba3a0aaSXu Yilun  *
5579ba3a0aaSXu Yilun  * @drv: driver model structure.
5589ba3a0aaSXu Yilun  * @id_table: pointer to table of device IDs the driver is interested in.
5599ba3a0aaSXu Yilun  *	      { } member terminated.
5609ba3a0aaSXu Yilun  * @probe: mandatory callback for device binding.
5619ba3a0aaSXu Yilun  * @remove: callback for device unbinding.
5629ba3a0aaSXu Yilun  */
5639ba3a0aaSXu Yilun struct dfl_driver {
5649ba3a0aaSXu Yilun 	struct device_driver drv;
5659ba3a0aaSXu Yilun 	const struct dfl_device_id *id_table;
5669ba3a0aaSXu Yilun 
5679ba3a0aaSXu Yilun 	int (*probe)(struct dfl_device *dfl_dev);
5689ba3a0aaSXu Yilun 	void (*remove)(struct dfl_device *dfl_dev);
5699ba3a0aaSXu Yilun };
5709ba3a0aaSXu Yilun 
5719ba3a0aaSXu Yilun #define to_dfl_dev(d) container_of(d, struct dfl_device, dev)
5729ba3a0aaSXu Yilun #define to_dfl_drv(d) container_of(d, struct dfl_driver, drv)
5739ba3a0aaSXu Yilun 
5749ba3a0aaSXu Yilun /*
5759ba3a0aaSXu Yilun  * use a macro to avoid include chaining to get THIS_MODULE.
5769ba3a0aaSXu Yilun  */
5779ba3a0aaSXu Yilun #define dfl_driver_register(drv) \
5789ba3a0aaSXu Yilun 	__dfl_driver_register(drv, THIS_MODULE)
5799ba3a0aaSXu Yilun int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner);
5809ba3a0aaSXu Yilun void dfl_driver_unregister(struct dfl_driver *dfl_drv);
5819ba3a0aaSXu Yilun 
5829ba3a0aaSXu Yilun /*
5839ba3a0aaSXu Yilun  * module_dfl_driver() - Helper macro for drivers that don't do
5849ba3a0aaSXu Yilun  * anything special in module init/exit.  This eliminates a lot of
5859ba3a0aaSXu Yilun  * boilerplate.  Each module may only use this macro once, and
5869ba3a0aaSXu Yilun  * calling it replaces module_init() and module_exit().
5879ba3a0aaSXu Yilun  */
5889ba3a0aaSXu Yilun #define module_dfl_driver(__dfl_driver) \
5899ba3a0aaSXu Yilun 	module_driver(__dfl_driver, dfl_driver_register, \
5909ba3a0aaSXu Yilun 		      dfl_driver_unregister)
5919ba3a0aaSXu Yilun 
592543be3d8SWu Hao #endif /* __FPGA_DFL_H */
593