Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 2 | /** |
| 3 | * PCI Endpoint *Controller* (EPC) header file |
| 4 | * |
| 5 | * Copyright (C) 2017 Texas Instruments |
| 6 | * Author: Kishon Vijay Abraham I <kishon@ti.com> |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 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, |
Gustavo Pimentel | 8963106 | 2018-07-19 10:32:12 +0200 | [diff] [blame] | 20 | PCI_EPC_IRQ_MSIX, |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 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 |
Gustavo Pimentel | 8963106 | 2018-07-19 10:32:12 +0200 | [diff] [blame] | 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 |
Gustavo Pimentel | d3c70a9 | 2018-07-19 10:32:13 +0200 | [diff] [blame] | 38 | * @raise_irq: ops to raise a legacy, MSI or MSI-X interrupt |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 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 { |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 44 | int (*write_header)(struct pci_epc *epc, u8 func_no, |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 45 | struct pci_epf_header *hdr); |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 46 | int (*set_bar)(struct pci_epc *epc, u8 func_no, |
Niklas Cassel | bc4a489 | 2018-03-28 13:50:07 +0200 | [diff] [blame] | 47 | struct pci_epf_bar *epf_bar); |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 48 | void (*clear_bar)(struct pci_epc *epc, u8 func_no, |
Niklas Cassel | 77d08db | 2018-03-28 13:50:14 +0200 | [diff] [blame] | 49 | struct pci_epf_bar *epf_bar); |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 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); |
Kishon Vijay Abraham I | 83153d9 | 2020-02-25 13:47:01 +0530 | [diff] [blame] | 56 | int (*set_msix)(struct pci_epc *epc, u8 func_no, u16 interrupts, |
| 57 | enum pci_barno, u32 offset); |
Gustavo Pimentel | 8963106 | 2018-07-19 10:32:12 +0200 | [diff] [blame] | 58 | int (*get_msix)(struct pci_epc *epc, u8 func_no); |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 59 | int (*raise_irq)(struct pci_epc *epc, u8 func_no, |
Gustavo Pimentel | d3c70a9 | 2018-07-19 10:32:13 +0200 | [diff] [blame] | 60 | enum pci_epc_irq_type type, u16 interrupt_num); |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 61 | int (*start)(struct pci_epc *epc); |
| 62 | void (*stop)(struct pci_epc *epc); |
Kishon Vijay Abraham I | 41cb8d1 | 2019-01-14 16:44:59 +0530 | [diff] [blame] | 63 | const struct pci_epc_features* (*get_features)(struct pci_epc *epc, |
| 64 | u8 func_no); |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 65 | struct module *owner; |
| 66 | }; |
| 67 | |
| 68 | /** |
Lad Prabhakar | d45e3c1 | 2020-05-07 13:33:16 +0100 | [diff] [blame] | 69 | * struct pci_epc_mem_window - address window of the endpoint controller |
| 70 | * @phys_base: physical base address of the PCI address window |
| 71 | * @size: the size of the PCI address window |
| 72 | * @page_size: size of each page |
| 73 | */ |
| 74 | struct pci_epc_mem_window { |
| 75 | phys_addr_t phys_base; |
| 76 | size_t size; |
| 77 | size_t page_size; |
| 78 | }; |
| 79 | |
| 80 | /** |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 81 | * struct pci_epc_mem - address space of the endpoint controller |
Lad Prabhakar | d45e3c1 | 2020-05-07 13:33:16 +0100 | [diff] [blame] | 82 | * @window: address window of the endpoint controller |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 83 | * @bitmap: bitmap to manage the PCI address space |
| 84 | * @pages: number of bits representing the address region |
Kishon Vijay Abraham I | 04e046c | 2020-02-24 15:23:36 +0530 | [diff] [blame] | 85 | * @lock: mutex to protect bitmap |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 86 | */ |
| 87 | struct pci_epc_mem { |
Lad Prabhakar | d45e3c1 | 2020-05-07 13:33:16 +0100 | [diff] [blame] | 88 | struct pci_epc_mem_window window; |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 89 | unsigned long *bitmap; |
| 90 | int pages; |
Kishon Vijay Abraham I | 04e046c | 2020-02-24 15:23:36 +0530 | [diff] [blame] | 91 | /* mutex to protect against concurrent access for memory allocation*/ |
| 92 | struct mutex lock; |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * struct pci_epc - represents the PCI EPC device |
| 97 | * @dev: PCI EPC device |
| 98 | * @pci_epf: list of endpoint functions present in this EPC device |
| 99 | * @ops: function pointers for performing endpoint operations |
Lad Prabhakar | d45e3c1 | 2020-05-07 13:33:16 +0100 | [diff] [blame] | 100 | * @windows: array of address space of the endpoint controller |
| 101 | * @mem: first window of the endpoint controller, which corresponds to |
| 102 | * default address space of the endpoint controller supporting |
| 103 | * single window. |
| 104 | * @num_windows: number of windows supported by device |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 105 | * @max_functions: max number of functions that can be configured in this EPC |
Kishon Vijay Abraham I | 3a401a2 | 2017-03-27 15:15:01 +0530 | [diff] [blame] | 106 | * @group: configfs group representing the PCI EPC device |
Kishon Vijay Abraham I | 3d3248d | 2020-02-24 15:23:35 +0530 | [diff] [blame] | 107 | * @lock: mutex to protect pci_epc ops |
Kishon Vijay Abraham I | 2499ee8 | 2020-02-24 15:23:38 +0530 | [diff] [blame] | 108 | * @function_num_map: bitmap to manage physical function number |
Kishon Vijay Abraham I | 5779dd0 | 2020-02-24 15:23:34 +0530 | [diff] [blame] | 109 | * @notifier: used to notify EPF of any EPC events (like linkup) |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 110 | */ |
| 111 | struct pci_epc { |
| 112 | struct device dev; |
| 113 | struct list_head pci_epf; |
| 114 | const struct pci_epc_ops *ops; |
Lad Prabhakar | d45e3c1 | 2020-05-07 13:33:16 +0100 | [diff] [blame] | 115 | struct pci_epc_mem **windows; |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 116 | struct pci_epc_mem *mem; |
Lad Prabhakar | d45e3c1 | 2020-05-07 13:33:16 +0100 | [diff] [blame] | 117 | unsigned int num_windows; |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 118 | u8 max_functions; |
Kishon Vijay Abraham I | 3a401a2 | 2017-03-27 15:15:01 +0530 | [diff] [blame] | 119 | struct config_group *group; |
Kishon Vijay Abraham I | 3d3248d | 2020-02-24 15:23:35 +0530 | [diff] [blame] | 120 | /* mutex to protect against concurrent access of EP controller */ |
| 121 | struct mutex lock; |
Kishon Vijay Abraham I | 2499ee8 | 2020-02-24 15:23:38 +0530 | [diff] [blame] | 122 | unsigned long function_num_map; |
Kishon Vijay Abraham I | 5779dd0 | 2020-02-24 15:23:34 +0530 | [diff] [blame] | 123 | struct atomic_notifier_head notifier; |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 124 | }; |
| 125 | |
Kishon Vijay Abraham I | 41cb8d1 | 2019-01-14 16:44:59 +0530 | [diff] [blame] | 126 | /** |
| 127 | * struct pci_epc_features - features supported by a EPC device per function |
| 128 | * @linkup_notifier: indicate if the EPC device can notify EPF driver on link up |
| 129 | * @msi_capable: indicate if the endpoint function has MSI capability |
| 130 | * @msix_capable: indicate if the endpoint function has MSI-X capability |
| 131 | * @reserved_bar: bitmap to indicate reserved BAR unavailable to function driver |
| 132 | * @bar_fixed_64bit: bitmap to indicate fixed 64bit BARs |
| 133 | * @bar_fixed_size: Array specifying the size supported by each BAR |
Kishon Vijay Abraham I | 2a9a801 | 2019-03-25 15:09:39 +0530 | [diff] [blame] | 134 | * @align: alignment size required for BAR buffer allocation |
Kishon Vijay Abraham I | 41cb8d1 | 2019-01-14 16:44:59 +0530 | [diff] [blame] | 135 | */ |
| 136 | struct pci_epc_features { |
| 137 | unsigned int linkup_notifier : 1; |
Vidya Sagar | 3d5f7d9 | 2020-02-17 17:40:32 +0530 | [diff] [blame] | 138 | unsigned int core_init_notifier : 1; |
Kishon Vijay Abraham I | 41cb8d1 | 2019-01-14 16:44:59 +0530 | [diff] [blame] | 139 | unsigned int msi_capable : 1; |
| 140 | unsigned int msix_capable : 1; |
| 141 | u8 reserved_bar; |
| 142 | u8 bar_fixed_64bit; |
Denis Efremov | c9c13ba | 2019-09-28 02:43:08 +0300 | [diff] [blame] | 143 | u64 bar_fixed_size[PCI_STD_NUM_BARS]; |
Kishon Vijay Abraham I | 2a9a801 | 2019-03-25 15:09:39 +0530 | [diff] [blame] | 144 | size_t align; |
Kishon Vijay Abraham I | 41cb8d1 | 2019-01-14 16:44:59 +0530 | [diff] [blame] | 145 | }; |
| 146 | |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 147 | #define to_pci_epc(device) container_of((device), struct pci_epc, dev) |
| 148 | |
| 149 | #define pci_epc_create(dev, ops) \ |
| 150 | __pci_epc_create((dev), (ops), THIS_MODULE) |
| 151 | #define devm_pci_epc_create(dev, ops) \ |
| 152 | __devm_pci_epc_create((dev), (ops), THIS_MODULE) |
| 153 | |
| 154 | static inline void epc_set_drvdata(struct pci_epc *epc, void *data) |
| 155 | { |
| 156 | dev_set_drvdata(&epc->dev, data); |
| 157 | } |
| 158 | |
| 159 | static inline void *epc_get_drvdata(struct pci_epc *epc) |
| 160 | { |
| 161 | return dev_get_drvdata(&epc->dev); |
| 162 | } |
| 163 | |
Kishon Vijay Abraham I | 5779dd0 | 2020-02-24 15:23:34 +0530 | [diff] [blame] | 164 | static inline int |
| 165 | pci_epc_register_notifier(struct pci_epc *epc, struct notifier_block *nb) |
| 166 | { |
| 167 | return atomic_notifier_chain_register(&epc->notifier, nb); |
| 168 | } |
| 169 | |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 170 | struct pci_epc * |
| 171 | __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, |
| 172 | struct module *owner); |
| 173 | struct pci_epc * |
| 174 | __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, |
| 175 | struct module *owner); |
| 176 | void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc); |
| 177 | void pci_epc_destroy(struct pci_epc *epc); |
| 178 | int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf); |
| 179 | void pci_epc_linkup(struct pci_epc *epc); |
Vidya Sagar | 0ef22dc | 2020-02-17 17:40:34 +0530 | [diff] [blame] | 180 | void pci_epc_init_notify(struct pci_epc *epc); |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 181 | void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf); |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 182 | int pci_epc_write_header(struct pci_epc *epc, u8 func_no, |
| 183 | struct pci_epf_header *hdr); |
| 184 | int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, |
Niklas Cassel | bc4a489 | 2018-03-28 13:50:07 +0200 | [diff] [blame] | 185 | struct pci_epf_bar *epf_bar); |
Niklas Cassel | 77d08db | 2018-03-28 13:50:14 +0200 | [diff] [blame] | 186 | void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, |
| 187 | struct pci_epf_bar *epf_bar); |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 188 | int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, |
| 189 | phys_addr_t phys_addr, |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 190 | u64 pci_addr, size_t size); |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 191 | void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, |
| 192 | phys_addr_t phys_addr); |
| 193 | int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts); |
| 194 | int pci_epc_get_msi(struct pci_epc *epc, u8 func_no); |
Kishon Vijay Abraham I | 83153d9 | 2020-02-25 13:47:01 +0530 | [diff] [blame] | 195 | int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts, |
| 196 | enum pci_barno, u32 offset); |
Gustavo Pimentel | 8963106 | 2018-07-19 10:32:12 +0200 | [diff] [blame] | 197 | int pci_epc_get_msix(struct pci_epc *epc, u8 func_no); |
Cyrille Pitchen | 4494738 | 2018-01-30 21:56:56 +0100 | [diff] [blame] | 198 | int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, |
Gustavo Pimentel | d3c70a9 | 2018-07-19 10:32:13 +0200 | [diff] [blame] | 199 | enum pci_epc_irq_type type, u16 interrupt_num); |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 200 | int pci_epc_start(struct pci_epc *epc); |
| 201 | void pci_epc_stop(struct pci_epc *epc); |
Kishon Vijay Abraham I | 41cb8d1 | 2019-01-14 16:44:59 +0530 | [diff] [blame] | 202 | const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc, |
| 203 | u8 func_no); |
Kishon Vijay Abraham I | 1e9efe6 | 2019-01-14 16:45:05 +0530 | [diff] [blame] | 204 | unsigned int pci_epc_get_first_free_bar(const struct pci_epc_features |
| 205 | *epc_features); |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 206 | struct pci_epc *pci_epc_get(const char *epc_name); |
| 207 | void pci_epc_put(struct pci_epc *epc); |
| 208 | |
Lad Prabhakar | 975cf23e | 2020-05-07 13:33:15 +0100 | [diff] [blame] | 209 | int pci_epc_mem_init(struct pci_epc *epc, phys_addr_t base, |
| 210 | size_t size, size_t page_size); |
Lad Prabhakar | d45e3c1 | 2020-05-07 13:33:16 +0100 | [diff] [blame] | 211 | int pci_epc_multi_mem_init(struct pci_epc *epc, |
| 212 | struct pci_epc_mem_window *window, |
| 213 | unsigned int num_windows); |
Kishon Vijay Abraham I | 5e8cb40 | 2017-04-10 19:25:10 +0530 | [diff] [blame] | 214 | void pci_epc_mem_exit(struct pci_epc *epc); |
| 215 | void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc, |
| 216 | phys_addr_t *phys_addr, size_t size); |
| 217 | void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr, |
| 218 | void __iomem *virt_addr, size_t size); |
| 219 | #endif /* __LINUX_PCI_EPC_H */ |