1 /* SPDX-License-Identifier: GPL-2.0 */
3 * PCI Endpoint *Controller* (EPC) header file
5 * Copyright (C) 2017 Texas Instruments
9 #ifndef __LINUX_PCI_EPC_H
10 #define __LINUX_PCI_EPC_H
12 #include <linux/pci-epf.h>
16 enum pci_epc_interface_type {
17 UNKNOWN_INTERFACE = -1,
22 static inline const char *
23 pci_epc_interface_string(enum pci_epc_interface_type type)
26 case PRIMARY_INTERFACE:
28 case SECONDARY_INTERFACE:
31 return "UNKNOWN interface";
36 * struct pci_epc_ops - set of function pointers for performing EPC operations
37 * @write_header: ops to populate configuration space header
38 * @set_bar: ops to configure the BAR
39 * @clear_bar: ops to reset the BAR
40 * @map_addr: ops to map CPU address to PCI address
41 * @unmap_addr: ops to unmap CPU address and PCI address
42 * @set_msi: ops to set the requested number of MSI interrupts in the MSI
44 * @get_msi: ops to get the number of MSI interrupts allocated by the RC from
45 * the MSI capability register
46 * @set_msix: ops to set the requested number of MSI-X interrupts in the
47 * MSI-X capability register
48 * @get_msix: ops to get the number of MSI-X interrupts allocated by the RC
49 * from the MSI-X capability register
50 * @raise_irq: ops to raise a legacy, MSI or MSI-X interrupt
51 * @map_msi_irq: ops to map physical address to MSI address and return MSI data
52 * @start: ops to start the PCI link
53 * @stop: ops to stop the PCI link
54 * @get_features: ops to get the features supported by the EPC
55 * @owner: the module owner containing the ops
58 int (*write_header)(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
59 struct pci_epf_header *hdr);
60 int (*set_bar)(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
61 struct pci_epf_bar *epf_bar);
62 void (*clear_bar)(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
63 struct pci_epf_bar *epf_bar);
64 int (*map_addr)(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
65 phys_addr_t addr, u64 pci_addr, size_t size);
66 void (*unmap_addr)(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
68 int (*set_msi)(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
70 int (*get_msi)(struct pci_epc *epc, u8 func_no, u8 vfunc_no);
71 int (*set_msix)(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
72 u16 interrupts, enum pci_barno, u32 offset);
73 int (*get_msix)(struct pci_epc *epc, u8 func_no, u8 vfunc_no);
74 int (*raise_irq)(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
75 unsigned int type, u16 interrupt_num);
76 int (*map_msi_irq)(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
77 phys_addr_t phys_addr, u8 interrupt_num,
78 u32 entry_size, u32 *msi_data,
79 u32 *msi_addr_offset);
80 int (*start)(struct pci_epc *epc);
81 void (*stop)(struct pci_epc *epc);
82 const struct pci_epc_features* (*get_features)(struct pci_epc *epc,
83 u8 func_no, u8 vfunc_no);
88 * struct pci_epc_mem_window - address window of the endpoint controller
89 * @phys_base: physical base address of the PCI address window
90 * @size: the size of the PCI address window
91 * @page_size: size of each page
93 struct pci_epc_mem_window {
94 phys_addr_t phys_base;
100 * struct pci_epc_mem - address space of the endpoint controller
101 * @window: address window of the endpoint controller
102 * @bitmap: bitmap to manage the PCI address space
103 * @pages: number of bits representing the address region
104 * @lock: mutex to protect bitmap
107 struct pci_epc_mem_window window;
108 unsigned long *bitmap;
110 /* mutex to protect against concurrent access for memory allocation*/
115 * struct pci_epc - represents the PCI EPC device
116 * @dev: PCI EPC device
117 * @pci_epf: list of endpoint functions present in this EPC device
118 * @list_lock: Mutex for protecting pci_epf list
119 * @ops: function pointers for performing endpoint operations
120 * @windows: array of address space of the endpoint controller
121 * @mem: first window of the endpoint controller, which corresponds to
122 * default address space of the endpoint controller supporting
124 * @num_windows: number of windows supported by device
125 * @max_functions: max number of functions that can be configured in this EPC
126 * @max_vfs: Array indicating the maximum number of virtual functions that can
127 * be associated with each physical function
128 * @group: configfs group representing the PCI EPC device
129 * @lock: mutex to protect pci_epc ops
130 * @function_num_map: bitmap to manage physical function number
134 struct list_head pci_epf;
135 struct mutex list_lock;
136 const struct pci_epc_ops *ops;
137 struct pci_epc_mem **windows;
138 struct pci_epc_mem *mem;
139 unsigned int num_windows;
142 struct config_group *group;
143 /* mutex to protect against concurrent access of EP controller */
145 unsigned long function_num_map;
149 * struct pci_epc_features - features supported by a EPC device per function
150 * @linkup_notifier: indicate if the EPC device can notify EPF driver on link up
151 * @core_init_notifier: indicate cores that can notify about their availability
153 * @msi_capable: indicate if the endpoint function has MSI capability
154 * @msix_capable: indicate if the endpoint function has MSI-X capability
155 * @reserved_bar: bitmap to indicate reserved BAR unavailable to function driver
156 * @bar_fixed_64bit: bitmap to indicate fixed 64bit BARs
157 * @bar_fixed_size: Array specifying the size supported by each BAR
158 * @align: alignment size required for BAR buffer allocation
160 struct pci_epc_features {
161 unsigned int linkup_notifier : 1;
162 unsigned int core_init_notifier : 1;
163 unsigned int msi_capable : 1;
164 unsigned int msix_capable : 1;
167 u64 bar_fixed_size[PCI_STD_NUM_BARS];
171 #define to_pci_epc(device) container_of((device), struct pci_epc, dev)
173 #define pci_epc_create(dev, ops) \
174 __pci_epc_create((dev), (ops), THIS_MODULE)
175 #define devm_pci_epc_create(dev, ops) \
176 __devm_pci_epc_create((dev), (ops), THIS_MODULE)
178 static inline void epc_set_drvdata(struct pci_epc *epc, void *data)
180 dev_set_drvdata(&epc->dev, data);
183 static inline void *epc_get_drvdata(struct pci_epc *epc)
185 return dev_get_drvdata(&epc->dev);
189 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
190 struct module *owner);
192 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
193 struct module *owner);
194 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc);
195 void pci_epc_destroy(struct pci_epc *epc);
196 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
197 enum pci_epc_interface_type type);
198 void pci_epc_linkup(struct pci_epc *epc);
199 void pci_epc_linkdown(struct pci_epc *epc);
200 void pci_epc_init_notify(struct pci_epc *epc);
201 void pci_epc_bme_notify(struct pci_epc *epc);
202 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
203 enum pci_epc_interface_type type);
204 int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
205 struct pci_epf_header *hdr);
206 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
207 struct pci_epf_bar *epf_bar);
208 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
209 struct pci_epf_bar *epf_bar);
210 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
211 phys_addr_t phys_addr,
212 u64 pci_addr, size_t size);
213 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
214 phys_addr_t phys_addr);
215 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
217 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no);
218 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
219 u16 interrupts, enum pci_barno, u32 offset);
220 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no);
221 int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
222 phys_addr_t phys_addr, u8 interrupt_num,
223 u32 entry_size, u32 *msi_data, u32 *msi_addr_offset);
224 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
225 unsigned int type, u16 interrupt_num);
226 int pci_epc_start(struct pci_epc *epc);
227 void pci_epc_stop(struct pci_epc *epc);
228 const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
229 u8 func_no, u8 vfunc_no);
231 pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features);
232 enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
233 *epc_features, enum pci_barno bar);
234 struct pci_epc *pci_epc_get(const char *epc_name);
235 void pci_epc_put(struct pci_epc *epc);
237 int pci_epc_mem_init(struct pci_epc *epc, phys_addr_t base,
238 size_t size, size_t page_size);
239 int pci_epc_multi_mem_init(struct pci_epc *epc,
240 struct pci_epc_mem_window *window,
241 unsigned int num_windows);
242 void pci_epc_mem_exit(struct pci_epc *epc);
243 void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
244 phys_addr_t *phys_addr, size_t size);
245 void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr,
246 void __iomem *virt_addr, size_t size);
247 #endif /* __LINUX_PCI_EPC_H */