pci.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __POWERNV_PCI_H
  3. #define __POWERNV_PCI_H
  4. #include <linux/compiler.h> /* for __printf */
  5. #include <linux/iommu.h>
  6. #include <asm/iommu.h>
  7. #include <asm/msi_bitmap.h>
  8. struct pci_dn;
  9. enum pnv_phb_type {
  10. PNV_PHB_IODA1,
  11. PNV_PHB_IODA2,
  12. PNV_PHB_NPU_OCAPI,
  13. };
  14. /* Precise PHB model for error management */
  15. enum pnv_phb_model {
  16. PNV_PHB_MODEL_UNKNOWN,
  17. PNV_PHB_MODEL_P7IOC,
  18. PNV_PHB_MODEL_PHB3,
  19. };
  20. #define PNV_PCI_DIAG_BUF_SIZE 8192
  21. #define PNV_IODA_PE_DEV (1 << 0) /* PE has single PCI device */
  22. #define PNV_IODA_PE_BUS (1 << 1) /* PE has primary PCI bus */
  23. #define PNV_IODA_PE_BUS_ALL (1 << 2) /* PE has subordinate buses */
  24. #define PNV_IODA_PE_MASTER (1 << 3) /* Master PE in compound case */
  25. #define PNV_IODA_PE_SLAVE (1 << 4) /* Slave PE in compound case */
  26. #define PNV_IODA_PE_VF (1 << 5) /* PE for one VF */
  27. /*
  28. * A brief note on PNV_IODA_PE_BUS_ALL
  29. *
  30. * This is needed because of the behaviour of PCIe-to-PCI bridges. The PHB uses
  31. * the Requester ID field of the PCIe request header to determine the device
  32. * (and PE) that initiated a DMA. In legacy PCI individual memory read/write
  33. * requests aren't tagged with the RID. To work around this the PCIe-to-PCI
  34. * bridge will use (secondary_bus_no << 8) | 0x00 as the RID on the PCIe side.
  35. *
  36. * PCIe-to-X bridges have a similar issue even though PCI-X requests also have
  37. * a RID in the transaction header. The PCIe-to-X bridge is permitted to "take
  38. * ownership" of a transaction by a PCI-X device when forwarding it to the PCIe
  39. * side of the bridge.
  40. *
  41. * To work around these problems we use the BUS_ALL flag since every subordinate
  42. * bus of the bridge should go into the same PE.
  43. */
  44. /* Indicates operations are frozen for a PE: MMIO in PESTA & DMA in PESTB. */
  45. #define PNV_IODA_STOPPED_STATE 0x8000000000000000
  46. /* Data associated with a PE, including IOMMU tracking etc.. */
  47. struct pnv_phb;
  48. struct pnv_ioda_pe {
  49. unsigned long flags;
  50. struct pnv_phb *phb;
  51. int device_count;
  52. /* A PE can be associated with a single device or an
  53. * entire bus (& children). In the former case, pdev
  54. * is populated, in the later case, pbus is.
  55. */
  56. #ifdef CONFIG_PCI_IOV
  57. struct pci_dev *parent_dev;
  58. #endif
  59. struct pci_dev *pdev;
  60. struct pci_bus *pbus;
  61. /* Effective RID (device RID for a device PE and base bus
  62. * RID with devfn 0 for a bus PE)
  63. */
  64. unsigned int rid;
  65. /* PE number */
  66. unsigned int pe_number;
  67. /* "Base" iommu table, ie, 4K TCEs, 32-bit DMA */
  68. struct iommu_table_group table_group;
  69. /* 64-bit TCE bypass region */
  70. bool tce_bypass_enabled;
  71. uint64_t tce_bypass_base;
  72. /*
  73. * Used to track whether we've done DMA setup for this PE or not. We
  74. * want to defer allocating TCE tables, etc until we've added a
  75. * non-bridge device to the PE.
  76. */
  77. bool dma_setup_done;
  78. /* MSIs. MVE index is identical for 32 and 64 bit MSI
  79. * and -1 if not supported. (It's actually identical to the
  80. * PE number)
  81. */
  82. int mve_number;
  83. /* PEs in compound case */
  84. struct pnv_ioda_pe *master;
  85. struct list_head slaves;
  86. /* Link in list of PE#s */
  87. struct list_head list;
  88. };
  89. #define PNV_PHB_FLAG_EEH (1 << 0)
  90. struct pnv_phb {
  91. struct pci_controller *hose;
  92. enum pnv_phb_type type;
  93. enum pnv_phb_model model;
  94. u64 hub_id;
  95. u64 opal_id;
  96. int flags;
  97. void __iomem *regs;
  98. u64 regs_phys;
  99. spinlock_t lock;
  100. #ifdef CONFIG_DEBUG_FS
  101. int has_dbgfs;
  102. struct dentry *dbgfs;
  103. #endif
  104. unsigned int msi_base;
  105. struct msi_bitmap msi_bmp;
  106. int (*init_m64)(struct pnv_phb *phb);
  107. int (*get_pe_state)(struct pnv_phb *phb, int pe_no);
  108. void (*freeze_pe)(struct pnv_phb *phb, int pe_no);
  109. int (*unfreeze_pe)(struct pnv_phb *phb, int pe_no, int opt);
  110. struct {
  111. /* Global bridge info */
  112. unsigned int total_pe_num;
  113. unsigned int reserved_pe_idx;
  114. unsigned int root_pe_idx;
  115. /* 32-bit MMIO window */
  116. unsigned int m32_size;
  117. unsigned int m32_segsize;
  118. unsigned int m32_pci_base;
  119. /* 64-bit MMIO window */
  120. unsigned int m64_bar_idx;
  121. unsigned long m64_size;
  122. unsigned long m64_segsize;
  123. unsigned long m64_base;
  124. #define MAX_M64_BARS 64
  125. unsigned long m64_bar_alloc;
  126. /* IO ports */
  127. unsigned int io_size;
  128. unsigned int io_segsize;
  129. unsigned int io_pci_base;
  130. /* PE allocation */
  131. struct mutex pe_alloc_mutex;
  132. unsigned long *pe_alloc;
  133. struct pnv_ioda_pe *pe_array;
  134. /* M32 & IO segment maps */
  135. unsigned int *m64_segmap;
  136. unsigned int *m32_segmap;
  137. unsigned int *io_segmap;
  138. /* DMA32 segment maps - IODA1 only */
  139. unsigned int dma32_count;
  140. unsigned int *dma32_segmap;
  141. /* IRQ chip */
  142. int irq_chip_init;
  143. struct irq_chip irq_chip;
  144. /* Sorted list of used PE's based
  145. * on the sequence of creation
  146. */
  147. struct list_head pe_list;
  148. struct mutex pe_list_mutex;
  149. /* Reverse map of PEs, indexed by {bus, devfn} */
  150. unsigned int pe_rmap[0x10000];
  151. } ioda;
  152. /* PHB and hub diagnostics */
  153. unsigned int diag_data_size;
  154. u8 *diag_data;
  155. };
  156. /* IODA PE management */
  157. static inline bool pnv_pci_is_m64(struct pnv_phb *phb, struct resource *r)
  158. {
  159. /*
  160. * WARNING: We cannot rely on the resource flags. The Linux PCI
  161. * allocation code sometimes decides to put a 64-bit prefetchable
  162. * BAR in the 32-bit window, so we have to compare the addresses.
  163. *
  164. * For simplicity we only test resource start.
  165. */
  166. return (r->start >= phb->ioda.m64_base &&
  167. r->start < (phb->ioda.m64_base + phb->ioda.m64_size));
  168. }
  169. static inline bool pnv_pci_is_m64_flags(unsigned long resource_flags)
  170. {
  171. unsigned long flags = (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
  172. return (resource_flags & flags) == flags;
  173. }
  174. int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe);
  175. int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe);
  176. void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe);
  177. void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe);
  178. struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb, int count);
  179. void pnv_ioda_free_pe(struct pnv_ioda_pe *pe);
  180. #ifdef CONFIG_PCI_IOV
  181. /*
  182. * For SR-IOV we want to put each VF's MMIO resource in to a separate PE.
  183. * This requires a bit of acrobatics with the MMIO -> PE configuration
  184. * and this structure is used to keep track of it all.
  185. */
  186. struct pnv_iov_data {
  187. /* number of VFs enabled */
  188. u16 num_vfs;
  189. /* pointer to the array of VF PEs. num_vfs long*/
  190. struct pnv_ioda_pe *vf_pe_arr;
  191. /* Did we map the VF BAR with single-PE IODA BARs? */
  192. bool m64_single_mode[PCI_SRIOV_NUM_BARS];
  193. /*
  194. * True if we're using any segmented windows. In that case we need
  195. * shift the start of the IOV resource the segment corresponding to
  196. * the allocated PE.
  197. */
  198. bool need_shift;
  199. /*
  200. * Bit mask used to track which m64 windows are used to map the
  201. * SR-IOV BARs for this device.
  202. */
  203. DECLARE_BITMAP(used_m64_bar_mask, MAX_M64_BARS);
  204. /*
  205. * If we map the SR-IOV BARs with a segmented window then
  206. * parts of that window will be "claimed" by other PEs.
  207. *
  208. * "holes" here is used to reserve the leading portion
  209. * of the window that is used by other (non VF) PEs.
  210. */
  211. struct resource holes[PCI_SRIOV_NUM_BARS];
  212. };
  213. static inline struct pnv_iov_data *pnv_iov_get(struct pci_dev *pdev)
  214. {
  215. return pdev->dev.archdata.iov_data;
  216. }
  217. void pnv_pci_ioda_fixup_iov(struct pci_dev *pdev);
  218. resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev, int resno);
  219. int pnv_pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs);
  220. int pnv_pcibios_sriov_disable(struct pci_dev *pdev);
  221. #endif /* CONFIG_PCI_IOV */
  222. extern struct pci_ops pnv_pci_ops;
  223. void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
  224. unsigned char *log_buff);
  225. int pnv_pci_cfg_read(struct pci_dn *pdn,
  226. int where, int size, u32 *val);
  227. int pnv_pci_cfg_write(struct pci_dn *pdn,
  228. int where, int size, u32 val);
  229. extern struct iommu_table *pnv_pci_table_alloc(int nid);
  230. extern void pnv_pci_init_ioda_hub(struct device_node *np);
  231. extern void pnv_pci_init_ioda2_phb(struct device_node *np);
  232. extern void pnv_pci_init_npu2_opencapi_phb(struct device_node *np);
  233. extern void pnv_pci_reset_secondary_bus(struct pci_dev *dev);
  234. extern int pnv_eeh_phb_reset(struct pci_controller *hose, int option);
  235. extern struct pnv_ioda_pe *pnv_pci_bdfn_to_pe(struct pnv_phb *phb, u16 bdfn);
  236. extern struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev);
  237. extern void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq);
  238. extern unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
  239. __u64 window_size, __u32 levels);
  240. extern int pnv_eeh_post_init(void);
  241. __printf(3, 4)
  242. extern void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
  243. const char *fmt, ...);
  244. #define pe_err(pe, fmt, ...) \
  245. pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
  246. #define pe_warn(pe, fmt, ...) \
  247. pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
  248. #define pe_info(pe, fmt, ...) \
  249. pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
  250. /* pci-ioda-tce.c */
  251. #define POWERNV_IOMMU_DEFAULT_LEVELS 2
  252. #define POWERNV_IOMMU_MAX_LEVELS 5
  253. extern int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
  254. unsigned long uaddr, enum dma_data_direction direction,
  255. unsigned long attrs);
  256. extern void pnv_tce_free(struct iommu_table *tbl, long index, long npages);
  257. extern int pnv_tce_xchg(struct iommu_table *tbl, long index,
  258. unsigned long *hpa, enum dma_data_direction *direction);
  259. extern __be64 *pnv_tce_useraddrptr(struct iommu_table *tbl, long index,
  260. bool alloc);
  261. extern unsigned long pnv_tce_get(struct iommu_table *tbl, long index);
  262. extern long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
  263. __u32 page_shift, __u64 window_size, __u32 levels,
  264. bool alloc_userspace_copy, struct iommu_table *tbl);
  265. extern void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);
  266. extern long pnv_pci_link_table_and_group(int node, int num,
  267. struct iommu_table *tbl,
  268. struct iommu_table_group *table_group);
  269. extern void pnv_pci_unlink_table_and_group(struct iommu_table *tbl,
  270. struct iommu_table_group *table_group);
  271. extern void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
  272. void *tce_mem, u64 tce_size,
  273. u64 dma_offset, unsigned int page_shift);
  274. extern unsigned long pnv_ioda_parse_tce_sizes(struct pnv_phb *phb);
  275. static inline struct pnv_phb *pci_bus_to_pnvhb(struct pci_bus *bus)
  276. {
  277. struct pci_controller *hose = bus->sysdata;
  278. if (hose)
  279. return hose->private_data;
  280. return NULL;
  281. }
  282. #endif /* __POWERNV_PCI_H */