xref: /linux/include/linux/pci-epf.h (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * PCI Endpoint *Function* (EPF) header file
4  *
5  * Copyright (C) 2017 Texas Instruments
6  * Author: Kishon Vijay Abraham I <kishon@ti.com>
7  */
8 
9 #ifndef __LINUX_PCI_EPF_H
10 #define __LINUX_PCI_EPF_H
11 
12 #include <linux/configfs.h>
13 #include <linux/device.h>
14 #include <linux/device-id/pci.h>
15 #include <linux/msi.h>
16 #include <linux/pci.h>
17 
18 struct pci_epf;
19 struct pci_epc_features;
20 enum pci_epc_interface_type;
21 
22 enum pci_barno {
23 	NO_BAR = -1,
24 	BAR_0,
25 	BAR_1,
26 	BAR_2,
27 	BAR_3,
28 	BAR_4,
29 	BAR_5,
30 };
31 
32 /**
33  * struct pci_epf_header - represents standard configuration header
34  * @vendorid: identifies device manufacturer
35  * @deviceid: identifies a particular device
36  * @revid: specifies a device-specific revision identifier
37  * @progif_code: identifies a specific register-level programming interface
38  * @subclass_code: identifies more specifically the function of the device
39  * @baseclass_code: broadly classifies the type of function the device performs
40  * @cache_line_size: specifies the system cacheline size in units of DWORDs
41  * @subsys_vendor_id: vendor of the add-in card or subsystem
42  * @subsys_id: ID specific to vendor
43  * @interrupt_pin: interrupt pin the device (or device function) uses
44  */
45 struct pci_epf_header {
46 	u16	vendorid;
47 	u16	deviceid;
48 	u8	revid;
49 	u8	progif_code;
50 	u8	subclass_code;
51 	u8	baseclass_code;
52 	u8	cache_line_size;
53 	u16	subsys_vendor_id;
54 	u16	subsys_id;
55 	enum pci_interrupt_pin interrupt_pin;
56 };
57 
58 /**
59  * struct pci_epf_ops - set of function pointers for performing EPF operations
60  * @bind: ops to perform when a EPC device has been bound to EPF device
61  * @unbind: ops to perform when a binding has been lost between a EPC device
62  *	    and EPF device
63  * @add_cfs: ops to initialize function-specific configfs attributes
64  */
65 struct pci_epf_ops {
66 	int	(*bind)(struct pci_epf *epf);
67 	void	(*unbind)(struct pci_epf *epf);
68 	struct config_group *(*add_cfs)(struct pci_epf *epf,
69 					struct config_group *group);
70 };
71 
72 /**
73  * struct pci_epc_event_ops - Callbacks for capturing the EPC events
74  * @epc_init: Callback for the EPC initialization complete event
75  * @epc_deinit: Callback for the EPC deinitialization event
76  * @link_up: Callback for the EPC link up event
77  * @link_down: Callback for the EPC link down event
78  * @bus_master_enable: Callback for the EPC Bus Master Enable event
79  */
80 struct pci_epc_event_ops {
81 	int (*epc_init)(struct pci_epf *epf);
82 	void (*epc_deinit)(struct pci_epf *epf);
83 	int (*link_up)(struct pci_epf *epf);
84 	int (*link_down)(struct pci_epf *epf);
85 	int (*bus_master_enable)(struct pci_epf *epf);
86 };
87 
88 /**
89  * struct pci_epf_driver - represents the PCI EPF driver
90  * @probe: ops to perform when a new EPF device has been bound to the EPF driver
91  * @remove: ops to perform when the binding between the EPF device and EPF
92  *	    driver is broken
93  * @driver: PCI EPF driver
94  * @ops: set of function pointers for performing EPF operations
95  * @owner: the owner of the module that registers the PCI EPF driver
96  * @epf_group: list of configfs group corresponding to the PCI EPF driver
97  * @id_table: identifies EPF devices for probing
98  */
99 struct pci_epf_driver {
100 	int	(*probe)(struct pci_epf *epf,
101 			 const struct pci_epf_device_id *id);
102 	void	(*remove)(struct pci_epf *epf);
103 
104 	struct device_driver	driver;
105 	const struct pci_epf_ops *ops;
106 	struct module		*owner;
107 	struct list_head	epf_group;
108 	const struct pci_epf_device_id	*id_table;
109 };
110 
111 #define to_pci_epf_driver(drv) container_of_const((drv), struct pci_epf_driver, driver)
112 
113 /**
114  * struct pci_epf_bar_submap - BAR subrange for inbound mapping
115  * @phys_addr: target physical/DMA address for this subrange
116  * @size: the size of the subrange to be mapped
117  *
118  * When pci_epf_bar.num_submap is >0, pci_epf_bar.submap describes the
119  * complete BAR layout. This allows an EPC driver to program multiple
120  * inbound translation windows for a single BAR when supported by the
121  * controller. The array order defines the BAR layout (submap[0] at offset
122  * 0, and each immediately follows the previous one).
123  */
124 struct pci_epf_bar_submap {
125 	dma_addr_t	phys_addr;
126 	size_t		size;
127 };
128 
129 /**
130  * struct pci_epf_bar - represents the BAR of EPF device
131  * @phys_addr: physical address that should be mapped to the BAR
132  * @addr: virtual address corresponding to the @phys_addr
133  * @size: the size of the address space present in BAR
134  * @mem_size: the size actually allocated to accommodate the iATU alignment
135  *            requirement
136  * @barno: BAR number
137  * @flags: flags that are set for the BAR
138  * @num_submap: number of entries in @submap
139  * @submap: array of subrange descriptors allocated by the caller. See
140  *          struct pci_epf_bar_submap for the semantics in detail.
141  */
142 struct pci_epf_bar {
143 	dma_addr_t	phys_addr;
144 	void		*addr;
145 	size_t		size;
146 	size_t		mem_size;
147 	enum pci_barno	barno;
148 	int		flags;
149 
150 	/* Optional sub-range mapping */
151 	unsigned int	num_submap;
152 	struct pci_epf_bar_submap	*submap;
153 };
154 
155 enum pci_epf_doorbell_type {
156 	PCI_EPF_DOORBELL_MSI = 0,
157 	PCI_EPF_DOORBELL_EMBEDDED,
158 };
159 
160 /**
161  * struct pci_epf_doorbell_msg - represents doorbell message
162  * @msg: Doorbell address/data pair to be mapped into BAR space.
163  *       For MSI-backed doorbells this is the MSI message, while for
164  *       "embedded" doorbells this represents an MMIO write that asserts
165  *       an interrupt on the EP side.
166  * @virq: IRQ number of this doorbell message
167  * @irq_flags: Required flags for request_irq()/request_threaded_irq().
168  *             Callers may OR-in additional flags (e.g. IRQF_ONESHOT).
169  * @type: Doorbell type.
170  * @bar: BAR number where the doorbell target is already exposed to the RC
171  *       (NO_BAR if not)
172  * @offset: offset within @bar for the doorbell target (valid iff
173  *          @bar != NO_BAR)
174  * @iova_base: Internal: base DMA address returned by dma_map_resource() for the
175  *             embedded doorbell MMIO window (used only for unmapping). Valid
176  *             when @type is PCI_EPF_DOORBELL_EMBEDDED and @iova_size is
177  *             non-zero.
178  * @iova_size: Internal: size of the dma_map_resource() mapping at @iova_base.
179  *             Zero when no mapping was created (e.g. pre-exposed fixed BAR).
180  */
181 struct pci_epf_doorbell_msg {
182 	struct msi_msg msg;
183 	int virq;
184 	unsigned long irq_flags;
185 	enum pci_epf_doorbell_type type;
186 	enum pci_barno bar;
187 	resource_size_t offset;
188 	dma_addr_t iova_base;
189 	size_t iova_size;
190 };
191 
192 /**
193  * struct pci_epf - represents the PCI EPF device
194  * @dev: the PCI EPF device
195  * @name: the name of the PCI EPF device
196  * @header: represents standard configuration header
197  * @bar: represents the BAR of EPF device
198  * @msi_interrupts: number of MSI interrupts required by this function
199  * @msix_interrupts: number of MSI-X interrupts required by this function
200  * @func_no: unique (physical) function number within this endpoint device
201  * @vfunc_no: unique virtual function number within a physical function
202  * @epc: the EPC device to which this EPF device is bound
203  * @epf_pf: the physical EPF device to which this virtual EPF device is bound
204  * @driver: the EPF driver to which this EPF device is bound
205  * @id: pointer to the EPF device ID
206  * @list: to add pci_epf as a list of PCI endpoint functions to pci_epc
207  * @lock: mutex to protect pci_epf_ops
208  * @sec_epc: the secondary EPC device to which this EPF device is bound
209  * @sec_epc_list: to add pci_epf as list of PCI endpoint functions to secondary
210  *   EPC device
211  * @sec_epc_bar: represents the BAR of EPF device associated with secondary EPC
212  * @sec_epc_func_no: unique (physical) function number within the secondary EPC
213  * @group: configfs group associated with the EPF device
214  * @is_bound: indicates if bind notification to function driver has been invoked
215  * @is_vf: true - virtual function, false - physical function
216  * @vfunction_num_map: bitmap to manage virtual function number
217  * @pci_vepf: list of virtual endpoint functions associated with this function
218  * @event_ops: callbacks for capturing the EPC events
219  * @db_msg: data for MSI from RC side
220  * @num_db: number of doorbells
221  */
222 struct pci_epf {
223 	struct device		dev;
224 	const char		*name;
225 	struct pci_epf_header	*header;
226 	struct pci_epf_bar	bar[PCI_STD_NUM_BARS];
227 	u8			msi_interrupts;
228 	u16			msix_interrupts;
229 	u8			func_no;
230 	u8			vfunc_no;
231 
232 	struct pci_epc		*epc;
233 	struct pci_epf		*epf_pf;
234 	struct pci_epf_driver	*driver;
235 	const struct pci_epf_device_id *id;
236 	struct list_head	list;
237 	/* mutex to protect against concurrent access of pci_epf_ops */
238 	struct mutex		lock;
239 
240 	/* Below members are to attach secondary EPC to an endpoint function */
241 	struct pci_epc		*sec_epc;
242 	struct list_head	sec_epc_list;
243 	struct pci_epf_bar	sec_epc_bar[PCI_STD_NUM_BARS];
244 	u8			sec_epc_func_no;
245 	struct config_group	*group;
246 	unsigned int		is_bound;
247 	unsigned int		is_vf;
248 	unsigned long		vfunction_num_map;
249 	struct list_head	pci_vepf;
250 	const struct pci_epc_event_ops *event_ops;
251 	struct pci_epf_doorbell_msg *db_msg;
252 	u16 num_db;
253 };
254 
255 /**
256  * struct pci_epf_msix_tbl - represents the MSI-X table entry structure
257  * @msg_addr: Writes to this address will trigger MSI-X interrupt in host
258  * @msg_data: Data that should be written to @msg_addr to trigger MSI-X
259  *   interrupt
260  * @vector_ctrl: Identifies if the function is prohibited from sending a message
261  *   using this MSI-X table entry
262  */
263 struct pci_epf_msix_tbl {
264 	u64 msg_addr;
265 	u32 msg_data;
266 	u32 vector_ctrl;
267 };
268 
269 #define to_pci_epf(epf_dev) container_of((epf_dev), struct pci_epf, dev)
270 
271 #define pci_epf_register_driver(driver)    \
272 		__pci_epf_register_driver((driver), THIS_MODULE)
273 
epf_set_drvdata(struct pci_epf * epf,void * data)274 static inline void epf_set_drvdata(struct pci_epf *epf, void *data)
275 {
276 	dev_set_drvdata(&epf->dev, data);
277 }
278 
epf_get_drvdata(struct pci_epf * epf)279 static inline void *epf_get_drvdata(struct pci_epf *epf)
280 {
281 	return dev_get_drvdata(&epf->dev);
282 }
283 
284 struct pci_epf *pci_epf_create(const char *name);
285 void pci_epf_destroy(struct pci_epf *epf);
286 int __pci_epf_register_driver(struct pci_epf_driver *driver,
287 			      struct module *owner);
288 void pci_epf_unregister_driver(struct pci_epf_driver *driver);
289 void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
290 			  const struct pci_epc_features *epc_features,
291 			  enum pci_epc_interface_type type);
292 void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
293 			enum pci_epc_interface_type type);
294 
295 int pci_epf_assign_bar_space(struct pci_epf *epf, size_t size,
296 			     enum pci_barno bar,
297 			     const struct pci_epc_features *epc_features,
298 			     enum pci_epc_interface_type type,
299 			     dma_addr_t bar_addr);
300 
301 int pci_epf_align_inbound_addr(struct pci_epf *epf, enum pci_barno bar,
302 			       u64 addr, dma_addr_t *base, size_t *off);
303 int pci_epf_bind(struct pci_epf *epf);
304 void pci_epf_unbind(struct pci_epf *epf);
305 int pci_epf_add_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf);
306 void pci_epf_remove_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf);
307 #endif /* __LINUX_PCI_EPF_H */
308