pci-epc.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * PCI Endpoint *Controller* (EPC) header file
  4. *
  5. * Copyright (C) 2017 Texas Instruments
  6. * Author: Kishon Vijay Abraham I <[email protected]>
  7. */
  8. #ifndef __LINUX_PCI_EPC_H
  9. #define __LINUX_PCI_EPC_H
  10. #include <linux/pci-epf.h>
  11. struct pci_epc;
  12. enum pci_epc_interface_type {
  13. UNKNOWN_INTERFACE = -1,
  14. PRIMARY_INTERFACE,
  15. SECONDARY_INTERFACE,
  16. };
  17. enum pci_epc_irq_type {
  18. PCI_EPC_IRQ_UNKNOWN,
  19. PCI_EPC_IRQ_LEGACY,
  20. PCI_EPC_IRQ_MSI,
  21. PCI_EPC_IRQ_MSIX,
  22. };
  23. static inline const char *
  24. pci_epc_interface_string(enum pci_epc_interface_type type)
  25. {
  26. switch (type) {
  27. case PRIMARY_INTERFACE:
  28. return "primary";
  29. case SECONDARY_INTERFACE:
  30. return "secondary";
  31. default:
  32. return "UNKNOWN interface";
  33. }
  34. }
  35. /**
  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
  43. * capability register
  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
  56. */
  57. struct pci_epc_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,
  67. phys_addr_t addr);
  68. int (*set_msi)(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  69. u8 interrupts);
  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. enum pci_epc_irq_type 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);
  84. struct module *owner;
  85. };
  86. /**
  87. * struct pci_epc_mem_window - address window of the endpoint controller
  88. * @phys_base: physical base address of the PCI address window
  89. * @size: the size of the PCI address window
  90. * @page_size: size of each page
  91. */
  92. struct pci_epc_mem_window {
  93. phys_addr_t phys_base;
  94. size_t size;
  95. size_t page_size;
  96. };
  97. /**
  98. * struct pci_epc_mem - address space of the endpoint controller
  99. * @window: address window of the endpoint controller
  100. * @bitmap: bitmap to manage the PCI address space
  101. * @pages: number of bits representing the address region
  102. * @lock: mutex to protect bitmap
  103. */
  104. struct pci_epc_mem {
  105. struct pci_epc_mem_window window;
  106. unsigned long *bitmap;
  107. int pages;
  108. /* mutex to protect against concurrent access for memory allocation*/
  109. struct mutex lock;
  110. };
  111. /**
  112. * struct pci_epc - represents the PCI EPC device
  113. * @dev: PCI EPC device
  114. * @pci_epf: list of endpoint functions present in this EPC device
  115. * @ops: function pointers for performing endpoint operations
  116. * @windows: array of address space of the endpoint controller
  117. * @mem: first window of the endpoint controller, which corresponds to
  118. * default address space of the endpoint controller supporting
  119. * single window.
  120. * @num_windows: number of windows supported by device
  121. * @max_functions: max number of functions that can be configured in this EPC
  122. * @max_vfs: Array indicating the maximum number of virtual functions that can
  123. * be associated with each physical function
  124. * @group: configfs group representing the PCI EPC device
  125. * @lock: mutex to protect pci_epc ops
  126. * @function_num_map: bitmap to manage physical function number
  127. * @notifier: used to notify EPF of any EPC events (like linkup)
  128. */
  129. struct pci_epc {
  130. struct device dev;
  131. struct list_head pci_epf;
  132. const struct pci_epc_ops *ops;
  133. struct pci_epc_mem **windows;
  134. struct pci_epc_mem *mem;
  135. unsigned int num_windows;
  136. u8 max_functions;
  137. u8 *max_vfs;
  138. struct config_group *group;
  139. /* mutex to protect against concurrent access of EP controller */
  140. struct mutex lock;
  141. unsigned long function_num_map;
  142. struct atomic_notifier_head notifier;
  143. };
  144. /**
  145. * struct pci_epc_features - features supported by a EPC device per function
  146. * @linkup_notifier: indicate if the EPC device can notify EPF driver on link up
  147. * @core_init_notifier: indicate cores that can notify about their availability
  148. * for initialization
  149. * @msi_capable: indicate if the endpoint function has MSI capability
  150. * @msix_capable: indicate if the endpoint function has MSI-X capability
  151. * @reserved_bar: bitmap to indicate reserved BAR unavailable to function driver
  152. * @bar_fixed_64bit: bitmap to indicate fixed 64bit BARs
  153. * @bar_fixed_size: Array specifying the size supported by each BAR
  154. * @align: alignment size required for BAR buffer allocation
  155. */
  156. struct pci_epc_features {
  157. unsigned int linkup_notifier : 1;
  158. unsigned int core_init_notifier : 1;
  159. unsigned int msi_capable : 1;
  160. unsigned int msix_capable : 1;
  161. u8 reserved_bar;
  162. u8 bar_fixed_64bit;
  163. u64 bar_fixed_size[PCI_STD_NUM_BARS];
  164. size_t align;
  165. };
  166. #define to_pci_epc(device) container_of((device), struct pci_epc, dev)
  167. #define pci_epc_create(dev, ops) \
  168. __pci_epc_create((dev), (ops), THIS_MODULE)
  169. #define devm_pci_epc_create(dev, ops) \
  170. __devm_pci_epc_create((dev), (ops), THIS_MODULE)
  171. static inline void epc_set_drvdata(struct pci_epc *epc, void *data)
  172. {
  173. dev_set_drvdata(&epc->dev, data);
  174. }
  175. static inline void *epc_get_drvdata(struct pci_epc *epc)
  176. {
  177. return dev_get_drvdata(&epc->dev);
  178. }
  179. static inline int
  180. pci_epc_register_notifier(struct pci_epc *epc, struct notifier_block *nb)
  181. {
  182. return atomic_notifier_chain_register(&epc->notifier, nb);
  183. }
  184. struct pci_epc *
  185. __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
  186. struct module *owner);
  187. struct pci_epc *
  188. __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
  189. struct module *owner);
  190. void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc);
  191. void pci_epc_destroy(struct pci_epc *epc);
  192. int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
  193. enum pci_epc_interface_type type);
  194. void pci_epc_linkup(struct pci_epc *epc);
  195. void pci_epc_init_notify(struct pci_epc *epc);
  196. void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
  197. enum pci_epc_interface_type type);
  198. int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  199. struct pci_epf_header *hdr);
  200. int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  201. struct pci_epf_bar *epf_bar);
  202. void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  203. struct pci_epf_bar *epf_bar);
  204. int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  205. phys_addr_t phys_addr,
  206. u64 pci_addr, size_t size);
  207. void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  208. phys_addr_t phys_addr);
  209. int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  210. u8 interrupts);
  211. int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no);
  212. int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  213. u16 interrupts, enum pci_barno, u32 offset);
  214. int pci_epc_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no);
  215. int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  216. phys_addr_t phys_addr, u8 interrupt_num,
  217. u32 entry_size, u32 *msi_data, u32 *msi_addr_offset);
  218. int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  219. enum pci_epc_irq_type type, u16 interrupt_num);
  220. int pci_epc_start(struct pci_epc *epc);
  221. void pci_epc_stop(struct pci_epc *epc);
  222. const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
  223. u8 func_no, u8 vfunc_no);
  224. enum pci_barno
  225. pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features);
  226. enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
  227. *epc_features, enum pci_barno bar);
  228. struct pci_epc *pci_epc_get(const char *epc_name);
  229. void pci_epc_put(struct pci_epc *epc);
  230. int pci_epc_mem_init(struct pci_epc *epc, phys_addr_t base,
  231. size_t size, size_t page_size);
  232. int pci_epc_multi_mem_init(struct pci_epc *epc,
  233. struct pci_epc_mem_window *window,
  234. unsigned int num_windows);
  235. void pci_epc_mem_exit(struct pci_epc *epc);
  236. void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
  237. phys_addr_t *phys_addr, size_t size);
  238. void pci_epc_mem_free_addr(struct pci_epc *epc, phys_addr_t phys_addr,
  239. void __iomem *virt_addr, size_t size);
  240. #endif /* __LINUX_PCI_EPC_H */