Commit | Line | Data |
---|---|---|
8cfab3cf | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
5e8cb403 KVA |
2 | /** |
3 | * PCI Endpoint *Controller* (EPC) header file | |
4 | * | |
5 | * Copyright (C) 2017 Texas Instruments | |
6 | * Author: Kishon Vijay Abraham I <kishon@ti.com> | |
5e8cb403 KVA |
7 | */ |
8 | ||
9 | #ifndef __LINUX_PCI_EPC_H | |
10 | #define __LINUX_PCI_EPC_H | |
11 | ||
12 | #include <linux/pci-epf.h> | |
13 | ||
14 | struct pci_epc; | |
15 | ||
16 | enum pci_epc_irq_type { | |
17 | PCI_EPC_IRQ_UNKNOWN, | |
18 | PCI_EPC_IRQ_LEGACY, | |
19 | PCI_EPC_IRQ_MSI, | |
8963106e | 20 | PCI_EPC_IRQ_MSIX, |
5e8cb403 KVA |
21 | }; |
22 | ||
23 | /** | |
24 | * struct pci_epc_ops - set of function pointers for performing EPC operations | |
25 | * @write_header: ops to populate configuration space header | |
26 | * @set_bar: ops to configure the BAR | |
27 | * @clear_bar: ops to reset the BAR | |
28 | * @map_addr: ops to map CPU address to PCI address | |
29 | * @unmap_addr: ops to unmap CPU address and PCI address | |
30 | * @set_msi: ops to set the requested number of MSI interrupts in the MSI | |
31 | * capability register | |
32 | * @get_msi: ops to get the number of MSI interrupts allocated by the RC from | |
33 | * the MSI capability register | |
8963106e GP |
34 | * @set_msix: ops to set the requested number of MSI-X interrupts in the |
35 | * MSI-X capability register | |
36 | * @get_msix: ops to get the number of MSI-X interrupts allocated by the RC | |
37 | * from the MSI-X capability register | |
d3c70a98 | 38 | * @raise_irq: ops to raise a legacy, MSI or MSI-X interrupt |
5e8cb403 KVA |
39 | * @start: ops to start the PCI link |
40 | * @stop: ops to stop the PCI link | |
41 | * @owner: the module owner containing the ops | |
42 | */ | |
43 | struct pci_epc_ops { | |
4494738d | 44 | int (*write_header)(struct pci_epc *epc, u8 func_no, |
5e8cb403 | 45 | struct pci_epf_header *hdr); |
4494738d | 46 | int (*set_bar)(struct pci_epc *epc, u8 func_no, |
bc4a4897 | 47 | struct pci_epf_bar *epf_bar); |
4494738d | 48 | void (*clear_bar)(struct pci_epc *epc, u8 func_no, |
77d08dbd | 49 | struct pci_epf_bar *epf_bar); |
4494738d CP |
50 | int (*map_addr)(struct pci_epc *epc, u8 func_no, |
51 | phys_addr_t addr, u64 pci_addr, size_t size); | |
52 | void (*unmap_addr)(struct pci_epc *epc, u8 func_no, | |
53 | phys_addr_t addr); | |
54 | int (*set_msi)(struct pci_epc *epc, u8 func_no, u8 interrupts); | |
55 | int (*get_msi)(struct pci_epc *epc, u8 func_no); | |
8963106e GP |
56 | int (*set_msix)(struct pci_epc *epc, u8 func_no, u16 interrupts); |
57 | int (*get_msix)(struct pci_epc *epc, u8 func_no); | |
4494738d | 58 | int (*raise_irq)(struct pci_epc *epc, u8 func_no, |
d3c70a98 | 59 | enum pci_epc_irq_type type, u16 interrupt_num); |
5e8cb403 KVA |
60 | int (*start)(struct pci_epc *epc); |
61 | void (*stop)(struct pci_epc *epc); | |
41cb8d18 KVA |
62 | const struct pci_epc_features* (*get_features)(struct pci_epc *epc, |
63 | u8 func_no); | |
5e8cb403 KVA |
64 | struct module *owner; |
65 | }; | |
66 | ||
67 | /** | |
68 | * struct pci_epc_mem - address space of the endpoint controller | |
69 | * @phys_base: physical base address of the PCI address space | |
70 | * @size: the size of the PCI address space | |
71 | * @bitmap: bitmap to manage the PCI address space | |
72 | * @pages: number of bits representing the address region | |
52c9285d | 73 | * @page_size: size of each page |
5e8cb403 KVA |
74 | */ |
75 | struct pci_epc_mem { | |
76 | phys_addr_t phys_base; | |
77 | size_t size; | |
78 | unsigned long *bitmap; | |
52c9285d | 79 | size_t page_size; |
5e8cb403 KVA |
80 | int pages; |
81 | }; | |
82 | ||
83 | /** | |
84 | * struct pci_epc - represents the PCI EPC device | |
85 | * @dev: PCI EPC device | |
86 | * @pci_epf: list of endpoint functions present in this EPC device | |
87 | * @ops: function pointers for performing endpoint operations | |
88 | * @mem: address space of the endpoint controller | |
89 | * @max_functions: max number of functions that can be configured in this EPC | |
3a401a2c | 90 | * @group: configfs group representing the PCI EPC device |
5e8cb403 | 91 | * @lock: spinlock to protect pci_epc ops |
5779dd0a | 92 | * @notifier: used to notify EPF of any EPC events (like linkup) |
5e8cb403 KVA |
93 | */ |
94 | struct pci_epc { | |
95 | struct device dev; | |
96 | struct list_head pci_epf; | |
97 | const struct pci_epc_ops *ops; | |
98 | struct pci_epc_mem *mem; | |
99 | u8 max_functions; | |
3a401a2c | 100 | struct config_group *group; |
5e8cb403 KVA |
101 | /* spinlock to protect against concurrent access of EP controller */ |
102 | spinlock_t lock; | |
5779dd0a | 103 | struct atomic_notifier_head notifier; |
5e8cb403 KVA |
104 | }; |
105 | ||
41cb8d18 KVA |
106 | /** |
107 | * struct pci_epc_features - features supported by a EPC device per function | |
108 | * @linkup_notifier: indicate if the EPC device can notify EPF driver on link up | |
109 | * @msi_capable: indicate if the endpoint function has MSI capability | |
110 | * @msix_capable: indicate if the endpoint function has MSI-X capability | |
111 | * @reserved_bar: bitmap to indicate reserved BAR unavailable to function driver | |
112 | * @bar_fixed_64bit: bitmap to indicate fixed 64bit BARs | |
113 | * @bar_fixed_size: Array specifying the size supported by each BAR | |
2a9a8016 | 114 | * @align: alignment size required for BAR buffer allocation |
41cb8d18 KVA |
115 | */ |
116 | struct pci_epc_features { | |
117 | unsigned int linkup_notifier : 1; | |
118 | unsigned int msi_capable : 1; | |
119 | unsigned int msix_capable : 1; | |
120 | u8 reserved_bar; | |
121 | u8 bar_fixed_64bit; | |
c9c13ba4 | 122 | u64 bar_fixed_size[PCI_STD_NUM_BARS]; |
2a9a8016 | 123 | size_t align; |
41cb8d18 KVA |
124 | }; |
125 | ||
5e8cb403 KVA |
126 | #define to_pci_epc(device) container_of((device), struct pci_epc, dev) |
127 | ||
128 | #define pci_epc_create(dev, ops) \ | |
129 | __pci_epc_create((dev), (ops), THIS_MODULE) | |
130 | #define devm_pci_epc_create(dev, ops) \ | |
131 | __devm_pci_epc_create((dev), (ops), THIS_MODULE) | |
132 | ||
52c9285d KVA |
133 | #define pci_epc_mem_init(epc, phys_addr, size) \ |
134 | __pci_epc_mem_init((epc), (phys_addr), (size), PAGE_SIZE) | |
135 | ||
5e8cb403 KVA |
136 | static inline void epc_set_drvdata(struct pci_epc *epc, void *data) |
137 | { | |
138 | dev_set_drvdata(&epc->dev, data); | |
139 | } | |
140 | ||
141 | static inline void *epc_get_drvdata(struct pci_epc *epc) | |
142 | { | |
143 | return dev_get_drvdata(&epc->dev); | |
144 | } | |
145 | ||
5779dd0a KVA |
146 | static inline int |
147 | pci_epc_register_notifier(struct pci_epc *epc, struct notifier_block *nb) | |
148 | { | |
149 | return atomic_notifier_chain_register(&epc->notifier, nb); | |
150 | } | |
151 | ||
5e8cb403 KVA |
152 | struct pci_epc * |
153 | __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, | |
154 | struct module *owner); | |
155 | struct pci_epc * | |
156 | __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, | |
157 | struct module *owner); | |
158 | void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc); | |
159 | void pci_epc_destroy(struct pci_epc *epc); | |
160 | int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf); | |
161 | void pci_epc_linkup(struct pci_epc *epc); | |
162 | void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf); | |
4494738d CP |
163 | int pci_epc_write_header(struct pci_epc *epc, u8 func_no, |
164 | struct pci_epf_header *hdr); | |
165 | int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, | |
bc4a4897 | 166 | struct pci_epf_bar *epf_bar); |
77d08dbd NC |
167 | void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, |
168 | struct pci_epf_bar *epf_bar); | |
4494738d CP |
169 | int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, |
170 | phys_addr_t phys_addr, | |
5e8cb403 | 171 | u64 pci_addr, size_t size); |
4494738d CP |
172 | void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, |
173 | phys_addr_t phys_addr); | |
174 | int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts); | |
175 | int pci_epc_get_msi(struct pci_epc *epc, u8 func_no); | |
8963106e GP |
176 | int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts); |
177 | int pci_epc_get_msix(struct pci_epc *epc, u8 func_no); | |
4494738d | 178 | int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, |
d3c70a98 | 179 | enum pci_epc_irq_type type, u16 interrupt_num); |
5e8cb403 KVA |
180 | int pci_epc_start(struct pci_epc *epc); |
181 | void pci_epc_stop(struct pci_epc *epc); | |
41cb8d18 KVA |
182 | const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc, |
183 | u8 func_no); | |
1e9efe6c KVA |
184 | unsigned int pci_epc_get_first_free_bar(const struct pci_epc_features |
185 | *epc_features); | |
5e8cb403 KVA |
186 | struct pci_epc *pci_epc_get(const char *epc_name); |
187 | void pci_epc_put(struct pci_epc *epc); | |
188 | ||
52c9285d KVA |
189 | int __pci_epc_mem_init(struct pci_epc *epc, phys_addr_t phys_addr, size_t size, |
190 | size_t page_size); | |
5e8cb403 KVA |
191 | void pci_epc_mem_exit(struct pci_epc *epc); |
192 | void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc, | |
193 | phys_addr_t *phys_addr, size_t size); | |
194 | void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr, | |
195 | void __iomem *virt_addr, size_t size); | |
196 | #endif /* __LINUX_PCI_EPC_H */ |