xref: /linux/drivers/fpga/dfl.h (revision afeea2758b4f1210361ce2a91d8fa3e7df606ad2)
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>
259326eecdSXu 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 */
770926d8d5SBasheer Ahmed Muddebihal #define DFH_VERSION		GENMASK_ULL(59, 52)	/* DFH version */
78543be3d8SWu Hao #define DFH_TYPE		GENMASK_ULL(63, 60)	/* Feature type */
79543be3d8SWu Hao #define DFH_TYPE_AFU		1
80543be3d8SWu Hao #define DFH_TYPE_PRIVATE	3
81543be3d8SWu Hao #define DFH_TYPE_FIU		4
82543be3d8SWu Hao 
830926d8d5SBasheer Ahmed Muddebihal /*
840926d8d5SBasheer Ahmed Muddebihal  * DFHv1 Register Offset definitons
850926d8d5SBasheer Ahmed Muddebihal  * In DHFv1, DFH + GUID + CSR_START + CSR_SIZE_GROUP + PARAM_HDR + PARAM_DATA
860926d8d5SBasheer Ahmed Muddebihal  * as common header registers
870926d8d5SBasheer Ahmed Muddebihal  */
880926d8d5SBasheer Ahmed Muddebihal #define DFHv1_CSR_ADDR		0x18  /* CSR Register start address */
890926d8d5SBasheer Ahmed Muddebihal #define DFHv1_CSR_SIZE_GRP	0x20  /* Size of Reg Block and Group/tag */
900926d8d5SBasheer Ahmed Muddebihal #define DFHv1_PARAM_HDR		0x28  /* Optional First Param header */
910926d8d5SBasheer Ahmed Muddebihal 
920926d8d5SBasheer Ahmed Muddebihal /*
930926d8d5SBasheer Ahmed Muddebihal  * CSR Rel Bit, 1'b0 = relative (offset from feature DFH start),
940926d8d5SBasheer Ahmed Muddebihal  * 1'b1 = absolute (ARM or other non-PCIe use)
950926d8d5SBasheer Ahmed Muddebihal  */
960926d8d5SBasheer Ahmed Muddebihal #define DFHv1_CSR_ADDR_REL	BIT_ULL(0)
970926d8d5SBasheer Ahmed Muddebihal 
980926d8d5SBasheer Ahmed Muddebihal /* CSR Header Register Bit Definitions */
990926d8d5SBasheer Ahmed Muddebihal #define DFHv1_CSR_ADDR_MASK       GENMASK_ULL(63, 1)  /* 63:1 of CSR address */
1000926d8d5SBasheer Ahmed Muddebihal 
1010926d8d5SBasheer Ahmed Muddebihal /* CSR SIZE Goup Register Bit Definitions */
1020926d8d5SBasheer Ahmed Muddebihal #define DFHv1_CSR_SIZE_GRP_INSTANCE_ID	GENMASK_ULL(15, 0)	/* Enumeration instantiated IP */
1030926d8d5SBasheer Ahmed Muddebihal #define DFHv1_CSR_SIZE_GRP_GROUPING_ID	GENMASK_ULL(30, 16)	/* Group Features/interfaces */
1040926d8d5SBasheer Ahmed Muddebihal #define DFHv1_CSR_SIZE_GRP_HAS_PARAMS	BIT_ULL(31)		/* Presence of Parameters */
1050926d8d5SBasheer Ahmed Muddebihal #define DFHv1_CSR_SIZE_GRP_SIZE		GENMASK_ULL(63, 32)	/* Size of CSR Block in bytes */
1060926d8d5SBasheer Ahmed Muddebihal 
1070926d8d5SBasheer Ahmed Muddebihal /* PARAM Header Register Bit Definitions */
1080926d8d5SBasheer Ahmed Muddebihal #define DFHv1_PARAM_HDR_ID		GENMASK_ULL(15, 0) /* Id of this Param  */
1090926d8d5SBasheer Ahmed Muddebihal #define DFHv1_PARAM_HDR_VER		GENMASK_ULL(31, 16) /* Version Param */
1100926d8d5SBasheer Ahmed Muddebihal #define DFHv1_PARAM_HDR_NEXT_OFFSET	GENMASK_ULL(63, 35) /* Offset of next Param */
1110926d8d5SBasheer Ahmed Muddebihal #define DFHv1_PARAM_HDR_NEXT_EOP	BIT_ULL(32)
1120926d8d5SBasheer Ahmed Muddebihal #define DFHv1_PARAM_DATA		0x08  /* Offset of Param data from Param header */
1130926d8d5SBasheer Ahmed Muddebihal 
1144747ab89SMatthew Gerlach #define DFHv1_PARAM_ID_MSI_X		0x1
1154747ab89SMatthew Gerlach #define DFHv1_PARAM_MSI_X_NUMV		GENMASK_ULL(63, 32)
1164747ab89SMatthew Gerlach #define DFHv1_PARAM_MSI_X_STARTV	GENMASK_ULL(31, 0)
1174747ab89SMatthew Gerlach 
118543be3d8SWu Hao /* Next AFU Register Bitfield */
119543be3d8SWu Hao #define NEXT_AFU_NEXT_DFH_OFST	GENMASK_ULL(23, 0)	/* Offset to next AFU */
120543be3d8SWu Hao 
121543be3d8SWu Hao /* FME Header Register Set */
122543be3d8SWu Hao #define FME_HDR_DFH		DFH
123543be3d8SWu Hao #define FME_HDR_GUID_L		GUID_L
124543be3d8SWu Hao #define FME_HDR_GUID_H		GUID_H
125543be3d8SWu Hao #define FME_HDR_NEXT_AFU	NEXT_AFU
126543be3d8SWu Hao #define FME_HDR_CAP		0x30
127543be3d8SWu Hao #define FME_HDR_PORT_OFST(n)	(0x38 + ((n) * 0x8))
128ae23f746SMatthew Gerlach #define FME_PORT_OFST_BAR_SKIP	7
129543be3d8SWu Hao #define FME_HDR_BITSTREAM_ID	0x60
130543be3d8SWu Hao #define FME_HDR_BITSTREAM_MD	0x68
131543be3d8SWu Hao 
132543be3d8SWu Hao /* FME Fab Capability Register Bitfield */
133543be3d8SWu Hao #define FME_CAP_FABRIC_VERID	GENMASK_ULL(7, 0)	/* Fabric version ID */
134543be3d8SWu Hao #define FME_CAP_SOCKET_ID	BIT_ULL(8)		/* Socket ID */
135543be3d8SWu Hao #define FME_CAP_PCIE0_LINK_AVL	BIT_ULL(12)		/* PCIE0 Link */
136543be3d8SWu Hao #define FME_CAP_PCIE1_LINK_AVL	BIT_ULL(13)		/* PCIE1 Link */
137543be3d8SWu Hao #define FME_CAP_COHR_LINK_AVL	BIT_ULL(14)		/* Coherent Link */
138543be3d8SWu Hao #define FME_CAP_IOMMU_AVL	BIT_ULL(16)		/* IOMMU available */
139543be3d8SWu Hao #define FME_CAP_NUM_PORTS	GENMASK_ULL(19, 17)	/* Number of ports */
140543be3d8SWu Hao #define FME_CAP_ADDR_WIDTH	GENMASK_ULL(29, 24)	/* Address bus width */
141543be3d8SWu Hao #define FME_CAP_CACHE_SIZE	GENMASK_ULL(43, 32)	/* cache size in KB */
142543be3d8SWu Hao #define FME_CAP_CACHE_ASSOC	GENMASK_ULL(47, 44)	/* Associativity */
143543be3d8SWu Hao 
144543be3d8SWu Hao /* FME Port Offset Register Bitfield */
145543be3d8SWu Hao /* Offset to port device feature header */
146543be3d8SWu Hao #define FME_PORT_OFST_DFH_OFST	GENMASK_ULL(23, 0)
147543be3d8SWu Hao /* PCI Bar ID for this port */
148543be3d8SWu Hao #define FME_PORT_OFST_BAR_ID	GENMASK_ULL(34, 32)
149543be3d8SWu Hao /* AFU MMIO access permission. 1 - VF, 0 - PF. */
150543be3d8SWu Hao #define FME_PORT_OFST_ACC_CTRL	BIT_ULL(55)
151543be3d8SWu Hao #define FME_PORT_OFST_ACC_PF	0
152543be3d8SWu Hao #define FME_PORT_OFST_ACC_VF	1
153543be3d8SWu Hao #define FME_PORT_OFST_IMP	BIT_ULL(60)
154543be3d8SWu Hao 
1558d021039SXu Yilun /* FME Error Capability Register */
1568d021039SXu Yilun #define FME_ERROR_CAP		0x70
1578d021039SXu Yilun 
1588d021039SXu Yilun /* FME Error Capability Register Bitfield */
1598d021039SXu Yilun #define FME_ERROR_CAP_SUPP_INT	BIT_ULL(0)		/* Interrupt Support */
1608d021039SXu Yilun #define FME_ERROR_CAP_INT_VECT	GENMASK_ULL(12, 1)	/* Interrupt vector */
1618d021039SXu Yilun 
162543be3d8SWu Hao /* PORT Header Register Set */
163543be3d8SWu Hao #define PORT_HDR_DFH		DFH
164543be3d8SWu Hao #define PORT_HDR_GUID_L		GUID_L
165543be3d8SWu Hao #define PORT_HDR_GUID_H		GUID_H
166543be3d8SWu Hao #define PORT_HDR_NEXT_AFU	NEXT_AFU
167543be3d8SWu Hao #define PORT_HDR_CAP		0x30
168543be3d8SWu Hao #define PORT_HDR_CTRL		0x38
169d2ad5ac1SWu Hao #define PORT_HDR_STS		0x40
170f09991adSWu Hao #define PORT_HDR_USRCLK_CMD0	0x50
171f09991adSWu Hao #define PORT_HDR_USRCLK_CMD1	0x58
172f09991adSWu Hao #define PORT_HDR_USRCLK_STS0	0x60
173f09991adSWu Hao #define PORT_HDR_USRCLK_STS1	0x68
174543be3d8SWu Hao 
175543be3d8SWu Hao /* Port Capability Register Bitfield */
176543be3d8SWu Hao #define PORT_CAP_PORT_NUM	GENMASK_ULL(1, 0)	/* ID of this port */
177543be3d8SWu Hao #define PORT_CAP_MMIO_SIZE	GENMASK_ULL(23, 8)	/* MMIO size in KB */
178543be3d8SWu Hao #define PORT_CAP_SUPP_INT_NUM	GENMASK_ULL(35, 32)	/* Interrupts num */
179543be3d8SWu Hao 
180543be3d8SWu Hao /* Port Control Register Bitfield */
181543be3d8SWu Hao #define PORT_CTRL_SFTRST	BIT_ULL(0)		/* Port soft reset */
182543be3d8SWu Hao /* Latency tolerance reporting. '1' >= 40us, '0' < 40us.*/
183543be3d8SWu Hao #define PORT_CTRL_LATENCY	BIT_ULL(2)
184543be3d8SWu Hao #define PORT_CTRL_SFTRST_ACK	BIT_ULL(4)		/* HW ack for reset */
185d2ad5ac1SWu Hao 
186d2ad5ac1SWu Hao /* Port Status Register Bitfield */
187d2ad5ac1SWu Hao #define PORT_STS_AP2_EVT	BIT_ULL(13)		/* AP2 event detected */
188d2ad5ac1SWu Hao #define PORT_STS_AP1_EVT	BIT_ULL(12)		/* AP1 event detected */
189d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE	GENMASK_ULL(11, 8)	/* AFU power states */
190d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_NORM 0
191d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_AP1	1			/* 50% throttling */
192d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_AP2	2			/* 90% throttling */
193d2ad5ac1SWu Hao #define PORT_STS_PWR_STATE_AP6	6			/* 100% throttling */
194d2ad5ac1SWu Hao 
1958d021039SXu Yilun /* Port Error Capability Register */
1968d021039SXu Yilun #define PORT_ERROR_CAP		0x38
1978d021039SXu Yilun 
1988d021039SXu Yilun /* Port Error Capability Register Bitfield */
1998d021039SXu Yilun #define PORT_ERROR_CAP_SUPP_INT	BIT_ULL(0)		/* Interrupt Support */
2008d021039SXu Yilun #define PORT_ERROR_CAP_INT_VECT	GENMASK_ULL(12, 1)	/* Interrupt vector */
2018d021039SXu Yilun 
2028d021039SXu Yilun /* Port Uint Capability Register */
2038d021039SXu Yilun #define PORT_UINT_CAP		0x8
2048d021039SXu Yilun 
2058d021039SXu Yilun /* Port Uint Capability Register Bitfield */
2068d021039SXu Yilun #define PORT_UINT_CAP_INT_NUM	GENMASK_ULL(11, 0)	/* Interrupts num */
2078d021039SXu Yilun #define PORT_UINT_CAP_FST_VECT	GENMASK_ULL(23, 12)	/* First Vector */
2088d021039SXu Yilun 
2096e8fd6e4SWu Hao /**
2106e8fd6e4SWu Hao  * struct dfl_fpga_port_ops - port ops
2116e8fd6e4SWu Hao  *
2126e8fd6e4SWu Hao  * @name: name of this port ops, to match with port platform device.
2136e8fd6e4SWu Hao  * @owner: pointer to the module which owns this port ops.
2146e8fd6e4SWu Hao  * @node: node to link port ops to global list.
2156e8fd6e4SWu Hao  * @get_id: get port id from hardware.
2166e8fd6e4SWu Hao  * @enable_set: enable/disable the port.
2176e8fd6e4SWu Hao  */
2186e8fd6e4SWu Hao struct dfl_fpga_port_ops {
2196e8fd6e4SWu Hao 	const char *name;
2206e8fd6e4SWu Hao 	struct module *owner;
2216e8fd6e4SWu Hao 	struct list_head node;
2226e8fd6e4SWu Hao 	int (*get_id)(struct platform_device *pdev);
2236e8fd6e4SWu Hao 	int (*enable_set)(struct platform_device *pdev, bool enable);
2246e8fd6e4SWu Hao };
2256e8fd6e4SWu Hao 
2266e8fd6e4SWu Hao void dfl_fpga_port_ops_add(struct dfl_fpga_port_ops *ops);
2276e8fd6e4SWu Hao void dfl_fpga_port_ops_del(struct dfl_fpga_port_ops *ops);
2286e8fd6e4SWu Hao struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct platform_device *pdev);
2296e8fd6e4SWu Hao void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops *ops);
230d06b004bSWu Hao int dfl_fpga_check_port_id(struct platform_device *pdev, void *pport_id);
231543be3d8SWu Hao 
232543be3d8SWu Hao /**
23315bbb300SWu Hao  * struct dfl_feature_id - dfl private feature id
2345b57d02aSXiao Guangrong  *
23515bbb300SWu Hao  * @id: unique dfl private feature id.
23615bbb300SWu Hao  */
23715bbb300SWu Hao struct dfl_feature_id {
2388a5de2deSXu Yilun 	u16 id;
23915bbb300SWu Hao };
24015bbb300SWu Hao 
24115bbb300SWu Hao /**
24215bbb300SWu Hao  * struct dfl_feature_driver - dfl private feature driver
24315bbb300SWu Hao  *
24415bbb300SWu Hao  * @id_table: id_table for dfl private features supported by this driver.
24515bbb300SWu Hao  * @ops: ops of this dfl private feature driver.
2465b57d02aSXiao Guangrong  */
2475b57d02aSXiao Guangrong struct dfl_feature_driver {
24815bbb300SWu Hao 	const struct dfl_feature_id *id_table;
2495b57d02aSXiao Guangrong 	const struct dfl_feature_ops *ops;
2505b57d02aSXiao Guangrong };
2515b57d02aSXiao Guangrong 
2525b57d02aSXiao Guangrong /**
2538d021039SXu Yilun  * struct dfl_feature_irq_ctx - dfl private feature interrupt context
2548d021039SXu Yilun  *
2558d021039SXu Yilun  * @irq: Linux IRQ number of this interrupt.
256322b598bSXu Yilun  * @trigger: eventfd context to signal when interrupt happens.
257322b598bSXu Yilun  * @name: irq name needed when requesting irq.
2588d021039SXu Yilun  */
2598d021039SXu Yilun struct dfl_feature_irq_ctx {
2608d021039SXu Yilun 	int irq;
261322b598bSXu Yilun 	struct eventfd_ctx *trigger;
262322b598bSXu Yilun 	char *name;
2638d021039SXu Yilun };
2648d021039SXu Yilun 
2658d021039SXu Yilun /**
266543be3d8SWu Hao  * struct dfl_feature - sub feature of the feature devices
267543be3d8SWu Hao  *
268322b598bSXu Yilun  * @dev: ptr to pdev of the feature device which has the sub feature.
269543be3d8SWu Hao  * @id: sub feature id.
270*693fed98SLinus Torvalds  * @revision: revision of this sub feature.
271543be3d8SWu Hao  * @resource_index: each sub feature has one mmio resource for its registers.
272543be3d8SWu Hao  *		    this index is used to find its mmio resource from the
273580e3137STom Rix  *		    feature dev (platform device)'s resources.
274543be3d8SWu Hao  * @ioaddr: mapped mmio resource address.
2758d021039SXu Yilun  * @irq_ctx: interrupt context list.
2768d021039SXu Yilun  * @nr_irqs: number of interrupt contexts.
2775b57d02aSXiao Guangrong  * @ops: ops of this sub feature.
2789ba3a0aaSXu Yilun  * @ddev: ptr to the dfl device of this sub feature.
279724142f8SWu Hao  * @priv: priv data of this feature.
2804747ab89SMatthew Gerlach  * @dfh_version: version of the DFH
2814747ab89SMatthew Gerlach  * @param_size: size of dfh parameters
2824747ab89SMatthew Gerlach  * @params: point to memory copy of dfh parameters
283543be3d8SWu Hao  */
284543be3d8SWu Hao struct dfl_feature {
285322b598bSXu Yilun 	struct platform_device *dev;
2868a5de2deSXu Yilun 	u16 id;
2871604986cSMartin Hundebøll 	u8 revision;
288543be3d8SWu Hao 	int resource_index;
289543be3d8SWu Hao 	void __iomem *ioaddr;
2908d021039SXu Yilun 	struct dfl_feature_irq_ctx *irq_ctx;
2918d021039SXu Yilun 	unsigned int nr_irqs;
2925b57d02aSXiao Guangrong 	const struct dfl_feature_ops *ops;
2939ba3a0aaSXu Yilun 	struct dfl_device *ddev;
294724142f8SWu Hao 	void *priv;
2954747ab89SMatthew Gerlach 	u8 dfh_version;
2964747ab89SMatthew Gerlach 	unsigned int param_size;
2974747ab89SMatthew Gerlach 	void *params;
298543be3d8SWu Hao };
299543be3d8SWu Hao 
30069bb18ddSWu Hao #define FEATURE_DEV_ID_UNUSED	(-1)
30169bb18ddSWu Hao 
302543be3d8SWu Hao /**
303543be3d8SWu Hao  * struct dfl_feature_platform_data - platform data for feature devices
304543be3d8SWu Hao  *
305543be3d8SWu Hao  * @node: node to link feature devs to container device's port_dev_list.
306543be3d8SWu Hao  * @lock: mutex to protect platform data.
307b16c5147SWu Hao  * @cdev: cdev of feature dev.
308543be3d8SWu Hao  * @dev: ptr to platform device linked with this platform data.
309543be3d8SWu Hao  * @dfl_cdev: ptr to container device.
31069bb18ddSWu Hao  * @id: id used for this feature device.
311543be3d8SWu Hao  * @disable_count: count for port disable.
312b6862193SXu Yilun  * @excl_open: set on feature device exclusive open.
313b6862193SXu Yilun  * @open_count: count for feature device open.
314543be3d8SWu Hao  * @num: number for sub features.
3155b57d02aSXiao Guangrong  * @private: ptr to feature dev private data.
316543be3d8SWu Hao  * @features: sub features of this feature dev.
317543be3d8SWu Hao  */
318543be3d8SWu Hao struct dfl_feature_platform_data {
319543be3d8SWu Hao 	struct list_head node;
320543be3d8SWu Hao 	struct mutex lock;
321b16c5147SWu Hao 	struct cdev cdev;
322543be3d8SWu Hao 	struct platform_device *dev;
323543be3d8SWu Hao 	struct dfl_fpga_cdev *dfl_cdev;
32469bb18ddSWu Hao 	int id;
325543be3d8SWu Hao 	unsigned int disable_count;
326b6862193SXu Yilun 	bool excl_open;
327b6862193SXu Yilun 	int open_count;
3285b57d02aSXiao Guangrong 	void *private;
329543be3d8SWu Hao 	int num;
3305a538815SGustavo A. R. Silva 	struct dfl_feature features[];
331543be3d8SWu Hao };
332543be3d8SWu Hao 
3335b57d02aSXiao Guangrong static inline
334b6862193SXu Yilun int dfl_feature_dev_use_begin(struct dfl_feature_platform_data *pdata,
335b6862193SXu Yilun 			      bool excl)
3365b57d02aSXiao Guangrong {
337b6862193SXu Yilun 	if (pdata->excl_open)
3385b57d02aSXiao Guangrong 		return -EBUSY;
3395b57d02aSXiao Guangrong 
340b6862193SXu Yilun 	if (excl) {
341b6862193SXu Yilun 		if (pdata->open_count)
342b6862193SXu Yilun 			return -EBUSY;
343b6862193SXu Yilun 
344b6862193SXu Yilun 		pdata->excl_open = true;
345b6862193SXu Yilun 	}
346b6862193SXu Yilun 	pdata->open_count++;
347b6862193SXu Yilun 
3485b57d02aSXiao Guangrong 	return 0;
3495b57d02aSXiao Guangrong }
3505b57d02aSXiao Guangrong 
3515b57d02aSXiao Guangrong static inline
3525b57d02aSXiao Guangrong void dfl_feature_dev_use_end(struct dfl_feature_platform_data *pdata)
3535b57d02aSXiao Guangrong {
354b6862193SXu Yilun 	pdata->excl_open = false;
355b6862193SXu Yilun 
356b6862193SXu Yilun 	if (WARN_ON(pdata->open_count <= 0))
357b6862193SXu Yilun 		return;
358b6862193SXu Yilun 
359b6862193SXu Yilun 	pdata->open_count--;
360b6862193SXu Yilun }
361b6862193SXu Yilun 
362b6862193SXu Yilun static inline
363b6862193SXu Yilun int dfl_feature_dev_use_count(struct dfl_feature_platform_data *pdata)
364b6862193SXu Yilun {
365b6862193SXu Yilun 	return pdata->open_count;
3665b57d02aSXiao Guangrong }
3675b57d02aSXiao Guangrong 
3685b57d02aSXiao Guangrong static inline
3695b57d02aSXiao Guangrong void dfl_fpga_pdata_set_private(struct dfl_feature_platform_data *pdata,
3705b57d02aSXiao Guangrong 				void *private)
3715b57d02aSXiao Guangrong {
3725b57d02aSXiao Guangrong 	pdata->private = private;
3735b57d02aSXiao Guangrong }
3745b57d02aSXiao Guangrong 
3755b57d02aSXiao Guangrong static inline
3765b57d02aSXiao Guangrong void *dfl_fpga_pdata_get_private(struct dfl_feature_platform_data *pdata)
3775b57d02aSXiao Guangrong {
3785b57d02aSXiao Guangrong 	return pdata->private;
3795b57d02aSXiao Guangrong }
3805b57d02aSXiao Guangrong 
3815b57d02aSXiao Guangrong struct dfl_feature_ops {
3825b57d02aSXiao Guangrong 	int (*init)(struct platform_device *pdev, struct dfl_feature *feature);
3835b57d02aSXiao Guangrong 	void (*uinit)(struct platform_device *pdev,
3845b57d02aSXiao Guangrong 		      struct dfl_feature *feature);
3855b57d02aSXiao Guangrong 	long (*ioctl)(struct platform_device *pdev, struct dfl_feature *feature,
3865b57d02aSXiao Guangrong 		      unsigned int cmd, unsigned long arg);
3875b57d02aSXiao Guangrong };
3885b57d02aSXiao Guangrong 
389543be3d8SWu Hao #define DFL_FPGA_FEATURE_DEV_FME		"dfl-fme"
390543be3d8SWu Hao #define DFL_FPGA_FEATURE_DEV_PORT		"dfl-port"
391543be3d8SWu Hao 
3925b57d02aSXiao Guangrong void dfl_fpga_dev_feature_uinit(struct platform_device *pdev);
3935b57d02aSXiao Guangrong int dfl_fpga_dev_feature_init(struct platform_device *pdev,
3945b57d02aSXiao Guangrong 			      struct dfl_feature_driver *feature_drvs);
3955b57d02aSXiao Guangrong 
396b16c5147SWu Hao int dfl_fpga_dev_ops_register(struct platform_device *pdev,
397b16c5147SWu Hao 			      const struct file_operations *fops,
398b16c5147SWu Hao 			      struct module *owner);
399b16c5147SWu Hao void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
400b16c5147SWu Hao 
4015b57d02aSXiao Guangrong static inline
4025b57d02aSXiao Guangrong struct platform_device *dfl_fpga_inode_to_feature_dev(struct inode *inode)
4035b57d02aSXiao Guangrong {
4045b57d02aSXiao Guangrong 	struct dfl_feature_platform_data *pdata;
4055b57d02aSXiao Guangrong 
4065b57d02aSXiao Guangrong 	pdata = container_of(inode->i_cdev, struct dfl_feature_platform_data,
4075b57d02aSXiao Guangrong 			     cdev);
4085b57d02aSXiao Guangrong 	return pdata->dev;
4095b57d02aSXiao Guangrong }
4105b57d02aSXiao Guangrong 
411543be3d8SWu Hao #define dfl_fpga_dev_for_each_feature(pdata, feature)			    \
412543be3d8SWu Hao 	for ((feature) = (pdata)->features;				    \
413543be3d8SWu Hao 	   (feature) < (pdata)->features + (pdata)->num; (feature)++)
414543be3d8SWu Hao 
415543be3d8SWu Hao static inline
4168a5de2deSXu Yilun struct dfl_feature *dfl_get_feature_by_id(struct device *dev, u16 id)
417543be3d8SWu Hao {
418543be3d8SWu Hao 	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
419543be3d8SWu Hao 	struct dfl_feature *feature;
420543be3d8SWu Hao 
421543be3d8SWu Hao 	dfl_fpga_dev_for_each_feature(pdata, feature)
422543be3d8SWu Hao 		if (feature->id == id)
423543be3d8SWu Hao 			return feature;
424543be3d8SWu Hao 
425543be3d8SWu Hao 	return NULL;
426543be3d8SWu Hao }
427543be3d8SWu Hao 
428543be3d8SWu Hao static inline
4298a5de2deSXu Yilun void __iomem *dfl_get_feature_ioaddr_by_id(struct device *dev, u16 id)
430543be3d8SWu Hao {
431543be3d8SWu Hao 	struct dfl_feature *feature = dfl_get_feature_by_id(dev, id);
432543be3d8SWu Hao 
433543be3d8SWu Hao 	if (feature && feature->ioaddr)
434543be3d8SWu Hao 		return feature->ioaddr;
435543be3d8SWu Hao 
436543be3d8SWu Hao 	WARN_ON(1);
437543be3d8SWu Hao 	return NULL;
438543be3d8SWu Hao }
439543be3d8SWu Hao 
4405b57d02aSXiao Guangrong static inline
4415b57d02aSXiao Guangrong struct device *dfl_fpga_pdata_to_parent(struct dfl_feature_platform_data *pdata)
4425b57d02aSXiao Guangrong {
4435b57d02aSXiao Guangrong 	return pdata->dev->dev.parent->parent;
4445b57d02aSXiao Guangrong }
4455b57d02aSXiao Guangrong 
446543be3d8SWu Hao static inline bool dfl_feature_is_fme(void __iomem *base)
447543be3d8SWu Hao {
448543be3d8SWu Hao 	u64 v = readq(base + DFH);
449543be3d8SWu Hao 
450543be3d8SWu Hao 	return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
451543be3d8SWu Hao 		(FIELD_GET(DFH_ID, v) == DFH_ID_FIU_FME);
452543be3d8SWu Hao }
453543be3d8SWu Hao 
454543be3d8SWu Hao static inline bool dfl_feature_is_port(void __iomem *base)
455543be3d8SWu Hao {
456543be3d8SWu Hao 	u64 v = readq(base + DFH);
457543be3d8SWu Hao 
458543be3d8SWu Hao 	return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
459543be3d8SWu Hao 		(FIELD_GET(DFH_ID, v) == DFH_ID_FIU_PORT);
460543be3d8SWu Hao }
461543be3d8SWu Hao 
462f09991adSWu Hao static inline u8 dfl_feature_revision(void __iomem *base)
463f09991adSWu Hao {
464f09991adSWu Hao 	return (u8)FIELD_GET(DFH_REVISION, readq(base + DFH));
465f09991adSWu Hao }
466f09991adSWu Hao 
467543be3d8SWu Hao /**
468543be3d8SWu Hao  * struct dfl_fpga_enum_info - DFL FPGA enumeration information
469543be3d8SWu Hao  *
470543be3d8SWu Hao  * @dev: parent device.
471543be3d8SWu Hao  * @dfls: list of device feature lists.
4728d021039SXu Yilun  * @nr_irqs: number of irqs for all feature devices.
4738d021039SXu Yilun  * @irq_table: Linux IRQ numbers for all irqs, indexed by hw irq numbers.
474543be3d8SWu Hao  */
475543be3d8SWu Hao struct dfl_fpga_enum_info {
476543be3d8SWu Hao 	struct device *dev;
477543be3d8SWu Hao 	struct list_head dfls;
4788d021039SXu Yilun 	unsigned int nr_irqs;
4798d021039SXu Yilun 	int *irq_table;
480543be3d8SWu Hao };
481543be3d8SWu Hao 
482543be3d8SWu Hao /**
483543be3d8SWu Hao  * struct dfl_fpga_enum_dfl - DFL FPGA enumeration device feature list info
484543be3d8SWu Hao  *
485543be3d8SWu Hao  * @start: base address of this device feature list.
486543be3d8SWu Hao  * @len: size of this device feature list.
487543be3d8SWu Hao  * @node: node in list of device feature lists.
488543be3d8SWu Hao  */
489543be3d8SWu Hao struct dfl_fpga_enum_dfl {
490543be3d8SWu Hao 	resource_size_t start;
491543be3d8SWu Hao 	resource_size_t len;
492543be3d8SWu Hao 	struct list_head node;
493543be3d8SWu Hao };
494543be3d8SWu Hao 
495543be3d8SWu Hao struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev);
496543be3d8SWu Hao int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
49789eb35e8SXu Yilun 			       resource_size_t start, resource_size_t len);
4988d021039SXu Yilun int dfl_fpga_enum_info_add_irq(struct dfl_fpga_enum_info *info,
4998d021039SXu Yilun 			       unsigned int nr_irqs, int *irq_table);
500543be3d8SWu Hao void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info);
501543be3d8SWu Hao 
502543be3d8SWu Hao /**
503543be3d8SWu Hao  * struct dfl_fpga_cdev - container device of DFL based FPGA
504543be3d8SWu Hao  *
505543be3d8SWu Hao  * @parent: parent device of this container device.
506543be3d8SWu Hao  * @region: base fpga region.
507543be3d8SWu Hao  * @fme_dev: FME feature device under this container device.
508543be3d8SWu Hao  * @lock: mutex lock to protect the port device list.
509543be3d8SWu Hao  * @port_dev_list: list of all port feature devices under this container device.
51069bb18ddSWu Hao  * @released_port_num: released port number under this container device.
511543be3d8SWu Hao  */
512543be3d8SWu Hao struct dfl_fpga_cdev {
513543be3d8SWu Hao 	struct device *parent;
514543be3d8SWu Hao 	struct fpga_region *region;
515543be3d8SWu Hao 	struct device *fme_dev;
516543be3d8SWu Hao 	struct mutex lock;
517543be3d8SWu Hao 	struct list_head port_dev_list;
51869bb18ddSWu Hao 	int released_port_num;
519543be3d8SWu Hao };
520543be3d8SWu Hao 
521543be3d8SWu Hao struct dfl_fpga_cdev *
522543be3d8SWu Hao dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info);
523543be3d8SWu Hao void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev);
524543be3d8SWu Hao 
5255d56e117SWu Hao /*
5265d56e117SWu Hao  * need to drop the device reference with put_device() after use port platform
5275d56e117SWu Hao  * device returned by __dfl_fpga_cdev_find_port and dfl_fpga_cdev_find_port
5285d56e117SWu Hao  * functions.
5295d56e117SWu Hao  */
5305d56e117SWu Hao struct platform_device *
5315d56e117SWu Hao __dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
5325d56e117SWu Hao 			  int (*match)(struct platform_device *, void *));
5335d56e117SWu Hao 
5345d56e117SWu Hao static inline struct platform_device *
5355d56e117SWu Hao dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
5365d56e117SWu Hao 			int (*match)(struct platform_device *, void *))
5375d56e117SWu Hao {
5385d56e117SWu Hao 	struct platform_device *pdev;
5395d56e117SWu Hao 
5405d56e117SWu Hao 	mutex_lock(&cdev->lock);
5415d56e117SWu Hao 	pdev = __dfl_fpga_cdev_find_port(cdev, data, match);
5425d56e117SWu Hao 	mutex_unlock(&cdev->lock);
5435d56e117SWu Hao 
5445d56e117SWu Hao 	return pdev;
5455d56e117SWu Hao }
54669bb18ddSWu Hao 
54769bb18ddSWu Hao int dfl_fpga_cdev_release_port(struct dfl_fpga_cdev *cdev, int port_id);
54869bb18ddSWu Hao int dfl_fpga_cdev_assign_port(struct dfl_fpga_cdev *cdev, int port_id);
549bdd4f307SWu Hao void dfl_fpga_cdev_config_ports_pf(struct dfl_fpga_cdev *cdev);
550bdd4f307SWu Hao int dfl_fpga_cdev_config_ports_vf(struct dfl_fpga_cdev *cdev, int num_vf);
551322b598bSXu Yilun int dfl_fpga_set_irq_triggers(struct dfl_feature *feature, unsigned int start,
552322b598bSXu Yilun 			      unsigned int count, int32_t *fds);
553322b598bSXu Yilun long dfl_feature_ioctl_get_num_irqs(struct platform_device *pdev,
554322b598bSXu Yilun 				    struct dfl_feature *feature,
555322b598bSXu Yilun 				    unsigned long arg);
556322b598bSXu Yilun long dfl_feature_ioctl_set_irq(struct platform_device *pdev,
557322b598bSXu Yilun 			       struct dfl_feature *feature,
558322b598bSXu Yilun 			       unsigned long arg);
559322b598bSXu Yilun 
560543be3d8SWu Hao #endif /* __FPGA_DFL_H */
561