xref: /linux/drivers/fpga/dfl.h (revision e08b9e6d87cca2bd8f427d109d22970906aaf6f8)
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>
25543be3d8SWu Hao #include <linux/platform_device.h>
26543be3d8SWu Hao #include <linux/slab.h>
27543be3d8SWu Hao #include <linux/uuid.h>
28543be3d8SWu Hao #include <linux/fpga/fpga-region.h>
29543be3d8SWu Hao 
30543be3d8SWu Hao /* maximum supported number of ports */
31543be3d8SWu Hao #define MAX_DFL_FPGA_PORT_NUM 4
32543be3d8SWu Hao /* plus one for fme device */
33543be3d8SWu Hao #define MAX_DFL_FEATURE_DEV_NUM    (MAX_DFL_FPGA_PORT_NUM + 1)
34543be3d8SWu Hao 
3515bbb300SWu Hao /* Reserved 0xfe for Header Group Register and 0xff for AFU */
3615bbb300SWu Hao #define FEATURE_ID_FIU_HEADER		0xfe
37543be3d8SWu Hao #define FEATURE_ID_AFU			0xff
38543be3d8SWu Hao 
39543be3d8SWu Hao #define FME_FEATURE_ID_HEADER		FEATURE_ID_FIU_HEADER
40543be3d8SWu Hao #define FME_FEATURE_ID_THERMAL_MGMT	0x1
41543be3d8SWu Hao #define FME_FEATURE_ID_POWER_MGMT	0x2
42543be3d8SWu Hao #define FME_FEATURE_ID_GLOBAL_IPERF	0x3
43543be3d8SWu Hao #define FME_FEATURE_ID_GLOBAL_ERR	0x4
44543be3d8SWu Hao #define FME_FEATURE_ID_PR_MGMT		0x5
45543be3d8SWu Hao #define FME_FEATURE_ID_HSSI		0x6
46543be3d8SWu Hao #define FME_FEATURE_ID_GLOBAL_DPERF	0x7
47543be3d8SWu Hao 
48543be3d8SWu Hao #define PORT_FEATURE_ID_HEADER		FEATURE_ID_FIU_HEADER
49543be3d8SWu Hao #define PORT_FEATURE_ID_AFU		FEATURE_ID_AFU
50543be3d8SWu Hao #define PORT_FEATURE_ID_ERROR		0x10
51543be3d8SWu Hao #define PORT_FEATURE_ID_UMSG		0x11
52543be3d8SWu Hao #define PORT_FEATURE_ID_UINT		0x12
53543be3d8SWu Hao #define PORT_FEATURE_ID_STP		0x13
54543be3d8SWu Hao 
55543be3d8SWu Hao /*
56543be3d8SWu Hao  * Device Feature Header Register Set
57543be3d8SWu Hao  *
58543be3d8SWu Hao  * For FIUs, they all have DFH + GUID + NEXT_AFU as common header registers.
59543be3d8SWu Hao  * For AFUs, they have DFH + GUID as common header registers.
60543be3d8SWu Hao  * For private features, they only have DFH register as common header.
61543be3d8SWu Hao  */
62543be3d8SWu Hao #define DFH			0x0
63543be3d8SWu Hao #define GUID_L			0x8
64543be3d8SWu Hao #define GUID_H			0x10
65543be3d8SWu Hao #define NEXT_AFU		0x18
66543be3d8SWu Hao 
67543be3d8SWu Hao #define DFH_SIZE		0x8
68543be3d8SWu Hao 
69543be3d8SWu Hao /* Device Feature Header Register Bitfield */
70543be3d8SWu Hao #define DFH_ID			GENMASK_ULL(11, 0)	/* Feature ID */
71543be3d8SWu Hao #define DFH_ID_FIU_FME		0
72543be3d8SWu Hao #define DFH_ID_FIU_PORT		1
73543be3d8SWu Hao #define DFH_REVISION		GENMASK_ULL(15, 12)	/* Feature revision */
74543be3d8SWu Hao #define DFH_NEXT_HDR_OFST	GENMASK_ULL(39, 16)	/* Offset to next DFH */
75543be3d8SWu Hao #define DFH_EOL			BIT_ULL(40)		/* End of list */
76543be3d8SWu Hao #define DFH_TYPE		GENMASK_ULL(63, 60)	/* Feature type */
77543be3d8SWu Hao #define DFH_TYPE_AFU		1
78543be3d8SWu Hao #define DFH_TYPE_PRIVATE	3
79543be3d8SWu Hao #define DFH_TYPE_FIU		4
80543be3d8SWu Hao 
81543be3d8SWu Hao /* Next AFU Register Bitfield */
82543be3d8SWu Hao #define NEXT_AFU_NEXT_DFH_OFST	GENMASK_ULL(23, 0)	/* Offset to next AFU */
83543be3d8SWu Hao 
84543be3d8SWu Hao /* FME Header Register Set */
85543be3d8SWu Hao #define FME_HDR_DFH		DFH
86543be3d8SWu Hao #define FME_HDR_GUID_L		GUID_L
87543be3d8SWu Hao #define FME_HDR_GUID_H		GUID_H
88543be3d8SWu Hao #define FME_HDR_NEXT_AFU	NEXT_AFU
89543be3d8SWu Hao #define FME_HDR_CAP		0x30
90543be3d8SWu Hao #define FME_HDR_PORT_OFST(n)	(0x38 + ((n) * 0x8))
91543be3d8SWu Hao #define FME_HDR_BITSTREAM_ID	0x60
92543be3d8SWu Hao #define FME_HDR_BITSTREAM_MD	0x68
93543be3d8SWu Hao 
94543be3d8SWu Hao /* FME Fab Capability Register Bitfield */
95543be3d8SWu Hao #define FME_CAP_FABRIC_VERID	GENMASK_ULL(7, 0)	/* Fabric version ID */
96543be3d8SWu Hao #define FME_CAP_SOCKET_ID	BIT_ULL(8)		/* Socket ID */
97543be3d8SWu Hao #define FME_CAP_PCIE0_LINK_AVL	BIT_ULL(12)		/* PCIE0 Link */
98543be3d8SWu Hao #define FME_CAP_PCIE1_LINK_AVL	BIT_ULL(13)		/* PCIE1 Link */
99543be3d8SWu Hao #define FME_CAP_COHR_LINK_AVL	BIT_ULL(14)		/* Coherent Link */
100543be3d8SWu Hao #define FME_CAP_IOMMU_AVL	BIT_ULL(16)		/* IOMMU available */
101543be3d8SWu Hao #define FME_CAP_NUM_PORTS	GENMASK_ULL(19, 17)	/* Number of ports */
102543be3d8SWu Hao #define FME_CAP_ADDR_WIDTH	GENMASK_ULL(29, 24)	/* Address bus width */
103543be3d8SWu Hao #define FME_CAP_CACHE_SIZE	GENMASK_ULL(43, 32)	/* cache size in KB */
104543be3d8SWu Hao #define FME_CAP_CACHE_ASSOC	GENMASK_ULL(47, 44)	/* Associativity */
105543be3d8SWu Hao 
106543be3d8SWu Hao /* FME Port Offset Register Bitfield */
107543be3d8SWu Hao /* Offset to port device feature header */
108543be3d8SWu Hao #define FME_PORT_OFST_DFH_OFST	GENMASK_ULL(23, 0)
109543be3d8SWu Hao /* PCI Bar ID for this port */
110543be3d8SWu Hao #define FME_PORT_OFST_BAR_ID	GENMASK_ULL(34, 32)
111543be3d8SWu Hao /* AFU MMIO access permission. 1 - VF, 0 - PF. */
112543be3d8SWu Hao #define FME_PORT_OFST_ACC_CTRL	BIT_ULL(55)
113543be3d8SWu Hao #define FME_PORT_OFST_ACC_PF	0
114543be3d8SWu Hao #define FME_PORT_OFST_ACC_VF	1
115543be3d8SWu Hao #define FME_PORT_OFST_IMP	BIT_ULL(60)
116543be3d8SWu Hao 
1178d021039SXu Yilun /* FME Error Capability Register */
1188d021039SXu Yilun #define FME_ERROR_CAP		0x70
1198d021039SXu Yilun 
1208d021039SXu Yilun /* FME Error Capability Register Bitfield */
1218d021039SXu Yilun #define FME_ERROR_CAP_SUPP_INT	BIT_ULL(0)		/* Interrupt Support */
1228d021039SXu Yilun #define FME_ERROR_CAP_INT_VECT	GENMASK_ULL(12, 1)	/* Interrupt vector */
1238d021039SXu Yilun 
124543be3d8SWu Hao /* PORT Header Register Set */
125543be3d8SWu Hao #define PORT_HDR_DFH		DFH
126543be3d8SWu Hao #define PORT_HDR_GUID_L		GUID_L
127543be3d8SWu Hao #define PORT_HDR_GUID_H		GUID_H
128543be3d8SWu Hao #define PORT_HDR_NEXT_AFU	NEXT_AFU
129543be3d8SWu Hao #define PORT_HDR_CAP		0x30
130543be3d8SWu Hao #define PORT_HDR_CTRL		0x38
131d2ad5ac1SWu Hao #define PORT_HDR_STS		0x40
132f09991adSWu Hao #define PORT_HDR_USRCLK_CMD0	0x50
133f09991adSWu Hao #define PORT_HDR_USRCLK_CMD1	0x58
134f09991adSWu Hao #define PORT_HDR_USRCLK_STS0	0x60
135f09991adSWu Hao #define PORT_HDR_USRCLK_STS1	0x68
136543be3d8SWu Hao 
137543be3d8SWu Hao /* Port Capability Register Bitfield */
138543be3d8SWu Hao #define PORT_CAP_PORT_NUM	GENMASK_ULL(1, 0)	/* ID of this port */
139543be3d8SWu Hao #define PORT_CAP_MMIO_SIZE	GENMASK_ULL(23, 8)	/* MMIO size in KB */
140543be3d8SWu Hao #define PORT_CAP_SUPP_INT_NUM	GENMASK_ULL(35, 32)	/* Interrupts num */
141543be3d8SWu Hao 
142543be3d8SWu Hao /* Port Control Register Bitfield */
143543be3d8SWu Hao #define PORT_CTRL_SFTRST	BIT_ULL(0)		/* Port soft reset */
144543be3d8SWu Hao /* Latency tolerance reporting. '1' >= 40us, '0' < 40us.*/
145543be3d8SWu Hao #define PORT_CTRL_LATENCY	BIT_ULL(2)
146543be3d8SWu Hao #define PORT_CTRL_SFTRST_ACK	BIT_ULL(4)		/* HW ack for reset */
147d2ad5ac1SWu Hao 
148d2ad5ac1SWu Hao /* Port Status Register Bitfield */
149d2ad5ac1SWu Hao #define PORT_STS_AP2_EVT	BIT_ULL(13)		/* AP2 event detected */
150d2ad5ac1SWu Hao #define PORT_STS_AP1_EVT	BIT_ULL(12)		/* AP1 event detected */
151d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE	GENMASK_ULL(11, 8)	/* AFU power states */
152d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_NORM 0
153d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_AP1	1			/* 50% throttling */
154d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_AP2	2			/* 90% throttling */
155d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_AP6	6			/* 100% throttling */
156d2ad5ac1SWu Hao 
1578d021039SXu Yilun /* Port Error Capability Register */
1588d021039SXu Yilun #define PORT_ERROR_CAP		0x38
1598d021039SXu Yilun 
1608d021039SXu Yilun /* Port Error Capability Register Bitfield */
1618d021039SXu Yilun #define PORT_ERROR_CAP_SUPP_INT	BIT_ULL(0)		/* Interrupt Support */
1628d021039SXu Yilun #define PORT_ERROR_CAP_INT_VECT	GENMASK_ULL(12, 1)	/* Interrupt vector */
1638d021039SXu Yilun 
1648d021039SXu Yilun /* Port Uint Capability Register */
1658d021039SXu Yilun #define PORT_UINT_CAP		0x8
1668d021039SXu Yilun 
1678d021039SXu Yilun /* Port Uint Capability Register Bitfield */
1688d021039SXu Yilun #define PORT_UINT_CAP_INT_NUM	GENMASK_ULL(11, 0)	/* Interrupts num */
1698d021039SXu Yilun #define PORT_UINT_CAP_FST_VECT	GENMASK_ULL(23, 12)	/* First Vector */
1708d021039SXu Yilun 
1716e8fd6e4SWu Hao /**
1726e8fd6e4SWu Hao  * struct dfl_fpga_port_ops - port ops
1736e8fd6e4SWu Hao  *
1746e8fd6e4SWu Hao  * @name: name of this port ops, to match with port platform device.
1756e8fd6e4SWu Hao  * @owner: pointer to the module which owns this port ops.
1766e8fd6e4SWu Hao  * @node: node to link port ops to global list.
1776e8fd6e4SWu Hao  * @get_id: get port id from hardware.
1786e8fd6e4SWu Hao  * @enable_set: enable/disable the port.
1796e8fd6e4SWu Hao  */
1806e8fd6e4SWu Hao struct dfl_fpga_port_ops {
1816e8fd6e4SWu Hao 	const char *name;
1826e8fd6e4SWu Hao 	struct module *owner;
1836e8fd6e4SWu Hao 	struct list_head node;
1846e8fd6e4SWu Hao 	int (*get_id)(struct platform_device *pdev);
1856e8fd6e4SWu Hao 	int (*enable_set)(struct platform_device *pdev, bool enable);
1866e8fd6e4SWu Hao };
1876e8fd6e4SWu Hao 
1886e8fd6e4SWu Hao void dfl_fpga_port_ops_add(struct dfl_fpga_port_ops *ops);
1896e8fd6e4SWu Hao void dfl_fpga_port_ops_del(struct dfl_fpga_port_ops *ops);
1906e8fd6e4SWu Hao struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct platform_device *pdev);
1916e8fd6e4SWu Hao void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops *ops);
192d06b004bSWu Hao int dfl_fpga_check_port_id(struct platform_device *pdev, void *pport_id);
193543be3d8SWu Hao 
194543be3d8SWu Hao /**
19515bbb300SWu Hao  * struct dfl_feature_id - dfl private feature id
1965b57d02aSXiao Guangrong  *
19715bbb300SWu Hao  * @id: unique dfl private feature id.
19815bbb300SWu Hao  */
19915bbb300SWu Hao struct dfl_feature_id {
2008a5de2deSXu Yilun 	u16 id;
20115bbb300SWu Hao };
20215bbb300SWu Hao 
20315bbb300SWu Hao /**
20415bbb300SWu Hao  * struct dfl_feature_driver - dfl private feature driver
20515bbb300SWu Hao  *
20615bbb300SWu Hao  * @id_table: id_table for dfl private features supported by this driver.
20715bbb300SWu Hao  * @ops: ops of this dfl private feature driver.
2085b57d02aSXiao Guangrong  */
2095b57d02aSXiao Guangrong struct dfl_feature_driver {
21015bbb300SWu Hao 	const struct dfl_feature_id *id_table;
2115b57d02aSXiao Guangrong 	const struct dfl_feature_ops *ops;
2125b57d02aSXiao Guangrong };
2135b57d02aSXiao Guangrong 
2145b57d02aSXiao Guangrong /**
2158d021039SXu Yilun  * struct dfl_feature_irq_ctx - dfl private feature interrupt context
2168d021039SXu Yilun  *
2178d021039SXu Yilun  * @irq: Linux IRQ number of this interrupt.
218322b598bSXu Yilun  * @trigger: eventfd context to signal when interrupt happens.
219322b598bSXu Yilun  * @name: irq name needed when requesting irq.
2208d021039SXu Yilun  */
2218d021039SXu Yilun struct dfl_feature_irq_ctx {
2228d021039SXu Yilun 	int irq;
223322b598bSXu Yilun 	struct eventfd_ctx *trigger;
224322b598bSXu Yilun 	char *name;
2258d021039SXu Yilun };
2268d021039SXu Yilun 
2278d021039SXu Yilun /**
228543be3d8SWu Hao  * struct dfl_feature - sub feature of the feature devices
229543be3d8SWu Hao  *
230322b598bSXu Yilun  * @dev: ptr to pdev of the feature device which has the sub feature.
231543be3d8SWu Hao  * @id: sub feature id.
232543be3d8SWu Hao  * @resource_index: each sub feature has one mmio resource for its registers.
233543be3d8SWu Hao  *		    this index is used to find its mmio resource from the
234543be3d8SWu Hao  *		    feature dev (platform device)'s reources.
235543be3d8SWu Hao  * @ioaddr: mapped mmio resource address.
2368d021039SXu Yilun  * @irq_ctx: interrupt context list.
2378d021039SXu Yilun  * @nr_irqs: number of interrupt contexts.
2385b57d02aSXiao Guangrong  * @ops: ops of this sub feature.
2399ba3a0aaSXu Yilun  * @ddev: ptr to the dfl device of this sub feature.
240724142f8SWu Hao  * @priv: priv data of this feature.
241543be3d8SWu Hao  */
242543be3d8SWu Hao struct dfl_feature {
243322b598bSXu Yilun 	struct platform_device *dev;
2448a5de2deSXu Yilun 	u16 id;
245543be3d8SWu Hao 	int resource_index;
246543be3d8SWu Hao 	void __iomem *ioaddr;
2478d021039SXu Yilun 	struct dfl_feature_irq_ctx *irq_ctx;
2488d021039SXu Yilun 	unsigned int nr_irqs;
2495b57d02aSXiao Guangrong 	const struct dfl_feature_ops *ops;
2509ba3a0aaSXu Yilun 	struct dfl_device *ddev;
251724142f8SWu Hao 	void *priv;
252543be3d8SWu Hao };
253543be3d8SWu Hao 
25469bb18ddSWu Hao #define FEATURE_DEV_ID_UNUSED	(-1)
25569bb18ddSWu Hao 
256543be3d8SWu Hao /**
257543be3d8SWu Hao  * struct dfl_feature_platform_data - platform data for feature devices
258543be3d8SWu Hao  *
259543be3d8SWu Hao  * @node: node to link feature devs to container device's port_dev_list.
260543be3d8SWu Hao  * @lock: mutex to protect platform data.
261b16c5147SWu Hao  * @cdev: cdev of feature dev.
262543be3d8SWu Hao  * @dev: ptr to platform device linked with this platform data.
263543be3d8SWu Hao  * @dfl_cdev: ptr to container device.
26469bb18ddSWu Hao  * @id: id used for this feature device.
265543be3d8SWu Hao  * @disable_count: count for port disable.
266b6862193SXu Yilun  * @excl_open: set on feature device exclusive open.
267b6862193SXu Yilun  * @open_count: count for feature device open.
268543be3d8SWu Hao  * @num: number for sub features.
2695b57d02aSXiao Guangrong  * @private: ptr to feature dev private data.
270543be3d8SWu Hao  * @features: sub features of this feature dev.
271543be3d8SWu Hao  */
272543be3d8SWu Hao struct dfl_feature_platform_data {
273543be3d8SWu Hao 	struct list_head node;
274543be3d8SWu Hao 	struct mutex lock;
275b16c5147SWu Hao 	struct cdev cdev;
276543be3d8SWu Hao 	struct platform_device *dev;
277543be3d8SWu Hao 	struct dfl_fpga_cdev *dfl_cdev;
27869bb18ddSWu Hao 	int id;
279543be3d8SWu Hao 	unsigned int disable_count;
280b6862193SXu Yilun 	bool excl_open;
281b6862193SXu Yilun 	int open_count;
2825b57d02aSXiao Guangrong 	void *private;
283543be3d8SWu Hao 	int num;
2845a538815SGustavo A. R. Silva 	struct dfl_feature features[];
285543be3d8SWu Hao };
286543be3d8SWu Hao 
2875b57d02aSXiao Guangrong static inline
288b6862193SXu Yilun int dfl_feature_dev_use_begin(struct dfl_feature_platform_data *pdata,
289b6862193SXu Yilun 			      bool excl)
2905b57d02aSXiao Guangrong {
291b6862193SXu Yilun 	if (pdata->excl_open)
2925b57d02aSXiao Guangrong 		return -EBUSY;
2935b57d02aSXiao Guangrong 
294b6862193SXu Yilun 	if (excl) {
295b6862193SXu Yilun 		if (pdata->open_count)
296b6862193SXu Yilun 			return -EBUSY;
297b6862193SXu Yilun 
298b6862193SXu Yilun 		pdata->excl_open = true;
299b6862193SXu Yilun 	}
300b6862193SXu Yilun 	pdata->open_count++;
301b6862193SXu Yilun 
3025b57d02aSXiao Guangrong 	return 0;
3035b57d02aSXiao Guangrong }
3045b57d02aSXiao Guangrong 
3055b57d02aSXiao Guangrong static inline
3065b57d02aSXiao Guangrong void dfl_feature_dev_use_end(struct dfl_feature_platform_data *pdata)
3075b57d02aSXiao Guangrong {
308b6862193SXu Yilun 	pdata->excl_open = false;
309b6862193SXu Yilun 
310b6862193SXu Yilun 	if (WARN_ON(pdata->open_count <= 0))
311b6862193SXu Yilun 		return;
312b6862193SXu Yilun 
313b6862193SXu Yilun 	pdata->open_count--;
314b6862193SXu Yilun }
315b6862193SXu Yilun 
316b6862193SXu Yilun static inline
317b6862193SXu Yilun int dfl_feature_dev_use_count(struct dfl_feature_platform_data *pdata)
318b6862193SXu Yilun {
319b6862193SXu Yilun 	return pdata->open_count;
3205b57d02aSXiao Guangrong }
3215b57d02aSXiao Guangrong 
3225b57d02aSXiao Guangrong static inline
3235b57d02aSXiao Guangrong void dfl_fpga_pdata_set_private(struct dfl_feature_platform_data *pdata,
3245b57d02aSXiao Guangrong 				void *private)
3255b57d02aSXiao Guangrong {
3265b57d02aSXiao Guangrong 	pdata->private = private;
3275b57d02aSXiao Guangrong }
3285b57d02aSXiao Guangrong 
3295b57d02aSXiao Guangrong static inline
3305b57d02aSXiao Guangrong void *dfl_fpga_pdata_get_private(struct dfl_feature_platform_data *pdata)
3315b57d02aSXiao Guangrong {
3325b57d02aSXiao Guangrong 	return pdata->private;
3335b57d02aSXiao Guangrong }
3345b57d02aSXiao Guangrong 
3355b57d02aSXiao Guangrong struct dfl_feature_ops {
3365b57d02aSXiao Guangrong 	int (*init)(struct platform_device *pdev, struct dfl_feature *feature);
3375b57d02aSXiao Guangrong 	void (*uinit)(struct platform_device *pdev,
3385b57d02aSXiao Guangrong 		      struct dfl_feature *feature);
3395b57d02aSXiao Guangrong 	long (*ioctl)(struct platform_device *pdev, struct dfl_feature *feature,
3405b57d02aSXiao Guangrong 		      unsigned int cmd, unsigned long arg);
3415b57d02aSXiao Guangrong };
3425b57d02aSXiao Guangrong 
343543be3d8SWu Hao #define DFL_FPGA_FEATURE_DEV_FME		"dfl-fme"
344543be3d8SWu Hao #define DFL_FPGA_FEATURE_DEV_PORT		"dfl-port"
345543be3d8SWu Hao 
3465b57d02aSXiao Guangrong void dfl_fpga_dev_feature_uinit(struct platform_device *pdev);
3475b57d02aSXiao Guangrong int dfl_fpga_dev_feature_init(struct platform_device *pdev,
3485b57d02aSXiao Guangrong 			      struct dfl_feature_driver *feature_drvs);
3495b57d02aSXiao Guangrong 
350b16c5147SWu Hao int dfl_fpga_dev_ops_register(struct platform_device *pdev,
351b16c5147SWu Hao 			      const struct file_operations *fops,
352b16c5147SWu Hao 			      struct module *owner);
353b16c5147SWu Hao void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
354b16c5147SWu Hao 
3555b57d02aSXiao Guangrong static inline
3565b57d02aSXiao Guangrong struct platform_device *dfl_fpga_inode_to_feature_dev(struct inode *inode)
3575b57d02aSXiao Guangrong {
3585b57d02aSXiao Guangrong 	struct dfl_feature_platform_data *pdata;
3595b57d02aSXiao Guangrong 
3605b57d02aSXiao Guangrong 	pdata = container_of(inode->i_cdev, struct dfl_feature_platform_data,
3615b57d02aSXiao Guangrong 			     cdev);
3625b57d02aSXiao Guangrong 	return pdata->dev;
3635b57d02aSXiao Guangrong }
3645b57d02aSXiao Guangrong 
365543be3d8SWu Hao #define dfl_fpga_dev_for_each_feature(pdata, feature)			    \
366543be3d8SWu Hao 	for ((feature) = (pdata)->features;				    \
367543be3d8SWu Hao 	   (feature) < (pdata)->features + (pdata)->num; (feature)++)
368543be3d8SWu Hao 
369543be3d8SWu Hao static inline
3708a5de2deSXu Yilun struct dfl_feature *dfl_get_feature_by_id(struct device *dev, u16 id)
371543be3d8SWu Hao {
372543be3d8SWu Hao 	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
373543be3d8SWu Hao 	struct dfl_feature *feature;
374543be3d8SWu Hao 
375543be3d8SWu Hao 	dfl_fpga_dev_for_each_feature(pdata, feature)
376543be3d8SWu Hao 		if (feature->id == id)
377543be3d8SWu Hao 			return feature;
378543be3d8SWu Hao 
379543be3d8SWu Hao 	return NULL;
380543be3d8SWu Hao }
381543be3d8SWu Hao 
382543be3d8SWu Hao static inline
3838a5de2deSXu Yilun void __iomem *dfl_get_feature_ioaddr_by_id(struct device *dev, u16 id)
384543be3d8SWu Hao {
385543be3d8SWu Hao 	struct dfl_feature *feature = dfl_get_feature_by_id(dev, id);
386543be3d8SWu Hao 
387543be3d8SWu Hao 	if (feature && feature->ioaddr)
388543be3d8SWu Hao 		return feature->ioaddr;
389543be3d8SWu Hao 
390543be3d8SWu Hao 	WARN_ON(1);
391543be3d8SWu Hao 	return NULL;
392543be3d8SWu Hao }
393543be3d8SWu Hao 
3948a5de2deSXu Yilun static inline bool is_dfl_feature_present(struct device *dev, u16 id)
3955b57d02aSXiao Guangrong {
3965b57d02aSXiao Guangrong 	return !!dfl_get_feature_ioaddr_by_id(dev, id);
3975b57d02aSXiao Guangrong }
3985b57d02aSXiao Guangrong 
3995b57d02aSXiao Guangrong static inline
4005b57d02aSXiao Guangrong struct device *dfl_fpga_pdata_to_parent(struct dfl_feature_platform_data *pdata)
4015b57d02aSXiao Guangrong {
4025b57d02aSXiao Guangrong 	return pdata->dev->dev.parent->parent;
4035b57d02aSXiao Guangrong }
4045b57d02aSXiao Guangrong 
405543be3d8SWu Hao static inline bool dfl_feature_is_fme(void __iomem *base)
406543be3d8SWu Hao {
407543be3d8SWu Hao 	u64 v = readq(base + DFH);
408543be3d8SWu Hao 
409543be3d8SWu Hao 	return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
410543be3d8SWu Hao 		(FIELD_GET(DFH_ID, v) == DFH_ID_FIU_FME);
411543be3d8SWu Hao }
412543be3d8SWu Hao 
413543be3d8SWu Hao static inline bool dfl_feature_is_port(void __iomem *base)
414543be3d8SWu Hao {
415543be3d8SWu Hao 	u64 v = readq(base + DFH);
416543be3d8SWu Hao 
417543be3d8SWu Hao 	return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
418543be3d8SWu Hao 		(FIELD_GET(DFH_ID, v) == DFH_ID_FIU_PORT);
419543be3d8SWu Hao }
420543be3d8SWu Hao 
421f09991adSWu Hao static inline u8 dfl_feature_revision(void __iomem *base)
422f09991adSWu Hao {
423f09991adSWu Hao 	return (u8)FIELD_GET(DFH_REVISION, readq(base + DFH));
424f09991adSWu Hao }
425f09991adSWu Hao 
426543be3d8SWu Hao /**
427543be3d8SWu Hao  * struct dfl_fpga_enum_info - DFL FPGA enumeration information
428543be3d8SWu Hao  *
429543be3d8SWu Hao  * @dev: parent device.
430543be3d8SWu Hao  * @dfls: list of device feature lists.
4318d021039SXu Yilun  * @nr_irqs: number of irqs for all feature devices.
4328d021039SXu Yilun  * @irq_table: Linux IRQ numbers for all irqs, indexed by hw irq numbers.
433543be3d8SWu Hao  */
434543be3d8SWu Hao struct dfl_fpga_enum_info {
435543be3d8SWu Hao 	struct device *dev;
436543be3d8SWu Hao 	struct list_head dfls;
4378d021039SXu Yilun 	unsigned int nr_irqs;
4388d021039SXu Yilun 	int *irq_table;
439543be3d8SWu Hao };
440543be3d8SWu Hao 
441543be3d8SWu Hao /**
442543be3d8SWu Hao  * struct dfl_fpga_enum_dfl - DFL FPGA enumeration device feature list info
443543be3d8SWu Hao  *
444543be3d8SWu Hao  * @start: base address of this device feature list.
445543be3d8SWu Hao  * @len: size of this device feature list.
446543be3d8SWu Hao  * @node: node in list of device feature lists.
447543be3d8SWu Hao  */
448543be3d8SWu Hao struct dfl_fpga_enum_dfl {
449543be3d8SWu Hao 	resource_size_t start;
450543be3d8SWu Hao 	resource_size_t len;
451543be3d8SWu Hao 	struct list_head node;
452543be3d8SWu Hao };
453543be3d8SWu Hao 
454543be3d8SWu Hao struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev);
455543be3d8SWu Hao int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
45689eb35e8SXu Yilun 			       resource_size_t start, resource_size_t len);
4578d021039SXu Yilun int dfl_fpga_enum_info_add_irq(struct dfl_fpga_enum_info *info,
4588d021039SXu Yilun 			       unsigned int nr_irqs, int *irq_table);
459543be3d8SWu Hao void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info);
460543be3d8SWu Hao 
461543be3d8SWu Hao /**
462543be3d8SWu Hao  * struct dfl_fpga_cdev - container device of DFL based FPGA
463543be3d8SWu Hao  *
464543be3d8SWu Hao  * @parent: parent device of this container device.
465543be3d8SWu Hao  * @region: base fpga region.
466543be3d8SWu Hao  * @fme_dev: FME feature device under this container device.
467543be3d8SWu Hao  * @lock: mutex lock to protect the port device list.
468543be3d8SWu Hao  * @port_dev_list: list of all port feature devices under this container device.
46969bb18ddSWu Hao  * @released_port_num: released port number under this container device.
470543be3d8SWu Hao  */
471543be3d8SWu Hao struct dfl_fpga_cdev {
472543be3d8SWu Hao 	struct device *parent;
473543be3d8SWu Hao 	struct fpga_region *region;
474543be3d8SWu Hao 	struct device *fme_dev;
475543be3d8SWu Hao 	struct mutex lock;
476543be3d8SWu Hao 	struct list_head port_dev_list;
47769bb18ddSWu Hao 	int released_port_num;
478543be3d8SWu Hao };
479543be3d8SWu Hao 
480543be3d8SWu Hao struct dfl_fpga_cdev *
481543be3d8SWu Hao dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info);
482543be3d8SWu Hao void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev);
483543be3d8SWu Hao 
4845d56e117SWu Hao /*
4855d56e117SWu Hao  * need to drop the device reference with put_device() after use port platform
4865d56e117SWu Hao  * device returned by __dfl_fpga_cdev_find_port and dfl_fpga_cdev_find_port
4875d56e117SWu Hao  * functions.
4885d56e117SWu Hao  */
4895d56e117SWu Hao struct platform_device *
4905d56e117SWu Hao __dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
4915d56e117SWu Hao 			  int (*match)(struct platform_device *, void *));
4925d56e117SWu Hao 
4935d56e117SWu Hao static inline struct platform_device *
4945d56e117SWu Hao dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
4955d56e117SWu Hao 			int (*match)(struct platform_device *, void *))
4965d56e117SWu Hao {
4975d56e117SWu Hao 	struct platform_device *pdev;
4985d56e117SWu Hao 
4995d56e117SWu Hao 	mutex_lock(&cdev->lock);
5005d56e117SWu Hao 	pdev = __dfl_fpga_cdev_find_port(cdev, data, match);
5015d56e117SWu Hao 	mutex_unlock(&cdev->lock);
5025d56e117SWu Hao 
5035d56e117SWu Hao 	return pdev;
5045d56e117SWu Hao }
50569bb18ddSWu Hao 
50669bb18ddSWu Hao int dfl_fpga_cdev_release_port(struct dfl_fpga_cdev *cdev, int port_id);
50769bb18ddSWu Hao int dfl_fpga_cdev_assign_port(struct dfl_fpga_cdev *cdev, int port_id);
508bdd4f307SWu Hao void dfl_fpga_cdev_config_ports_pf(struct dfl_fpga_cdev *cdev);
509bdd4f307SWu Hao int dfl_fpga_cdev_config_ports_vf(struct dfl_fpga_cdev *cdev, int num_vf);
510322b598bSXu Yilun int dfl_fpga_set_irq_triggers(struct dfl_feature *feature, unsigned int start,
511322b598bSXu Yilun 			      unsigned int count, int32_t *fds);
512322b598bSXu Yilun long dfl_feature_ioctl_get_num_irqs(struct platform_device *pdev,
513322b598bSXu Yilun 				    struct dfl_feature *feature,
514322b598bSXu Yilun 				    unsigned long arg);
515322b598bSXu Yilun long dfl_feature_ioctl_set_irq(struct platform_device *pdev,
516322b598bSXu Yilun 			       struct dfl_feature *feature,
517322b598bSXu Yilun 			       unsigned long arg);
518322b598bSXu Yilun 
5199ba3a0aaSXu Yilun /**
5209ba3a0aaSXu Yilun  * enum dfl_id_type - define the DFL FIU types
5219ba3a0aaSXu Yilun  */
5229ba3a0aaSXu Yilun enum dfl_id_type {
523*e08b9e6dSXu Yilun 	FME_ID = 0,
524*e08b9e6dSXu Yilun 	PORT_ID = 1,
5259ba3a0aaSXu Yilun 	DFL_ID_MAX,
5269ba3a0aaSXu Yilun };
5279ba3a0aaSXu Yilun 
5289ba3a0aaSXu Yilun /**
5299ba3a0aaSXu Yilun  * struct dfl_device_id -  dfl device identifier
530*e08b9e6dSXu Yilun  * @type: DFL FIU type of the device. See enum dfl_id_type.
531*e08b9e6dSXu Yilun  * @feature_id: feature identifier local to its DFL FIU type.
5329ba3a0aaSXu Yilun  * @driver_data: driver specific data.
5339ba3a0aaSXu Yilun  */
5349ba3a0aaSXu Yilun struct dfl_device_id {
535*e08b9e6dSXu Yilun 	u16 type;
5369ba3a0aaSXu Yilun 	u16 feature_id;
5379ba3a0aaSXu Yilun 	unsigned long driver_data;
5389ba3a0aaSXu Yilun };
5399ba3a0aaSXu Yilun 
5409ba3a0aaSXu Yilun /**
5419ba3a0aaSXu Yilun  * struct dfl_device - represent an dfl device on dfl bus
5429ba3a0aaSXu Yilun  *
5439ba3a0aaSXu Yilun  * @dev: generic device interface.
5449ba3a0aaSXu Yilun  * @id: id of the dfl device.
5459ba3a0aaSXu Yilun  * @type: type of DFL FIU of the device. See enum dfl_id_type.
546*e08b9e6dSXu Yilun  * @feature_id: feature identifier local to its DFL FIU type.
5479ba3a0aaSXu Yilun  * @mmio_res: mmio resource of this dfl device.
5489ba3a0aaSXu Yilun  * @irqs: list of Linux IRQ numbers of this dfl device.
5499ba3a0aaSXu Yilun  * @num_irqs: number of IRQs supported by this dfl device.
5509ba3a0aaSXu Yilun  * @cdev: pointer to DFL FPGA container device this dfl device belongs to.
5519ba3a0aaSXu Yilun  * @id_entry: matched id entry in dfl driver's id table.
5529ba3a0aaSXu Yilun  */
5539ba3a0aaSXu Yilun struct dfl_device {
5549ba3a0aaSXu Yilun 	struct device dev;
5559ba3a0aaSXu Yilun 	int id;
556*e08b9e6dSXu Yilun 	u16 type;
5579ba3a0aaSXu Yilun 	u16 feature_id;
5589ba3a0aaSXu Yilun 	struct resource mmio_res;
5599ba3a0aaSXu Yilun 	int *irqs;
5609ba3a0aaSXu Yilun 	unsigned int num_irqs;
5619ba3a0aaSXu Yilun 	struct dfl_fpga_cdev *cdev;
5629ba3a0aaSXu Yilun 	const struct dfl_device_id *id_entry;
5639ba3a0aaSXu Yilun };
5649ba3a0aaSXu Yilun 
5659ba3a0aaSXu Yilun /**
5669ba3a0aaSXu Yilun  * struct dfl_driver - represent an dfl device driver
5679ba3a0aaSXu Yilun  *
5689ba3a0aaSXu Yilun  * @drv: driver model structure.
5699ba3a0aaSXu Yilun  * @id_table: pointer to table of device IDs the driver is interested in.
5709ba3a0aaSXu Yilun  *	      { } member terminated.
5719ba3a0aaSXu Yilun  * @probe: mandatory callback for device binding.
5729ba3a0aaSXu Yilun  * @remove: callback for device unbinding.
5739ba3a0aaSXu Yilun  */
5749ba3a0aaSXu Yilun struct dfl_driver {
5759ba3a0aaSXu Yilun 	struct device_driver drv;
5769ba3a0aaSXu Yilun 	const struct dfl_device_id *id_table;
5779ba3a0aaSXu Yilun 
5789ba3a0aaSXu Yilun 	int (*probe)(struct dfl_device *dfl_dev);
5799ba3a0aaSXu Yilun 	void (*remove)(struct dfl_device *dfl_dev);
5809ba3a0aaSXu Yilun };
5819ba3a0aaSXu Yilun 
5829ba3a0aaSXu Yilun #define to_dfl_dev(d) container_of(d, struct dfl_device, dev)
5839ba3a0aaSXu Yilun #define to_dfl_drv(d) container_of(d, struct dfl_driver, drv)
5849ba3a0aaSXu Yilun 
5859ba3a0aaSXu Yilun /*
5869ba3a0aaSXu Yilun  * use a macro to avoid include chaining to get THIS_MODULE.
5879ba3a0aaSXu Yilun  */
5889ba3a0aaSXu Yilun #define dfl_driver_register(drv) \
5899ba3a0aaSXu Yilun 	__dfl_driver_register(drv, THIS_MODULE)
5909ba3a0aaSXu Yilun int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner);
5919ba3a0aaSXu Yilun void dfl_driver_unregister(struct dfl_driver *dfl_drv);
5929ba3a0aaSXu Yilun 
5939ba3a0aaSXu Yilun /*
5949ba3a0aaSXu Yilun  * module_dfl_driver() - Helper macro for drivers that don't do
5959ba3a0aaSXu Yilun  * anything special in module init/exit.  This eliminates a lot of
5969ba3a0aaSXu Yilun  * boilerplate.  Each module may only use this macro once, and
5979ba3a0aaSXu Yilun  * calling it replaces module_init() and module_exit().
5989ba3a0aaSXu Yilun  */
5999ba3a0aaSXu Yilun #define module_dfl_driver(__dfl_driver) \
6009ba3a0aaSXu Yilun 	module_driver(__dfl_driver, dfl_driver_register, \
6019ba3a0aaSXu Yilun 		      dfl_driver_unregister)
6029ba3a0aaSXu Yilun 
603543be3d8SWu Hao #endif /* __FPGA_DFL_H */
604