1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
261305a96SBenjamin Herrenschmidt #ifndef __POWERNV_PCI_H
361305a96SBenjamin Herrenschmidt #define __POWERNV_PCI_H
461305a96SBenjamin Herrenschmidt 
51e496391SJoe Perches #include <linux/compiler.h>		/* for __printf */
6f456834aSIan Munsie #include <linux/iommu.h>
7f456834aSIan Munsie #include <asm/iommu.h>
8f456834aSIan Munsie #include <asm/msi_bitmap.h>
9f456834aSIan Munsie 
1061305a96SBenjamin Herrenschmidt struct pci_dn;
1161305a96SBenjamin Herrenschmidt 
1261305a96SBenjamin Herrenschmidt enum pnv_phb_type {
132de50e96SRussell Currey 	PNV_PHB_IODA1		= 0,
142de50e96SRussell Currey 	PNV_PHB_IODA2		= 1,
157f2c39e9SFrederic Barrat 	PNV_PHB_NPU_NVLINK	= 2,
167f2c39e9SFrederic Barrat 	PNV_PHB_NPU_OCAPI	= 3,
1761305a96SBenjamin Herrenschmidt };
1861305a96SBenjamin Herrenschmidt 
19cee72d5bSBenjamin Herrenschmidt /* Precise PHB model for error management */
20cee72d5bSBenjamin Herrenschmidt enum pnv_phb_model {
21cee72d5bSBenjamin Herrenschmidt 	PNV_PHB_MODEL_UNKNOWN,
22cee72d5bSBenjamin Herrenschmidt 	PNV_PHB_MODEL_P7IOC,
23aa0c033fSGavin Shan 	PNV_PHB_MODEL_PHB3,
245d2aa710SAlistair Popple 	PNV_PHB_MODEL_NPU,
25616badd2SAlistair Popple 	PNV_PHB_MODEL_NPU2,
26cee72d5bSBenjamin Herrenschmidt };
27cee72d5bSBenjamin Herrenschmidt 
285c9d6d75SGavin Shan #define PNV_PCI_DIAG_BUF_SIZE	8192
297ebdf956SGavin Shan #define PNV_IODA_PE_DEV		(1 << 0)	/* PE has single PCI device	*/
307ebdf956SGavin Shan #define PNV_IODA_PE_BUS		(1 << 1)	/* PE has primary PCI bus	*/
317ebdf956SGavin Shan #define PNV_IODA_PE_BUS_ALL	(1 << 2)	/* PE has subordinate buses	*/
32262af557SGuo Chao #define PNV_IODA_PE_MASTER	(1 << 3)	/* Master PE in compound case	*/
33262af557SGuo Chao #define PNV_IODA_PE_SLAVE	(1 << 4)	/* Slave PE in compound case	*/
34781a868fSWei Yang #define PNV_IODA_PE_VF		(1 << 5)	/* PE for one VF 		*/
35cee72d5bSBenjamin Herrenschmidt 
369d0879a2SOliver O'Halloran /*
379d0879a2SOliver O'Halloran  * A brief note on PNV_IODA_PE_BUS_ALL
389d0879a2SOliver O'Halloran  *
399d0879a2SOliver O'Halloran  * This is needed because of the behaviour of PCIe-to-PCI bridges. The PHB uses
409d0879a2SOliver O'Halloran  * the Requester ID field of the PCIe request header to determine the device
419d0879a2SOliver O'Halloran  * (and PE) that initiated a DMA. In legacy PCI individual memory read/write
429d0879a2SOliver O'Halloran  * requests aren't tagged with the RID. To work around this the PCIe-to-PCI
439d0879a2SOliver O'Halloran  * bridge will use (secondary_bus_no << 8) | 0x00 as the RID on the PCIe side.
449d0879a2SOliver O'Halloran  *
459d0879a2SOliver O'Halloran  * PCIe-to-X bridges have a similar issue even though PCI-X requests also have
469d0879a2SOliver O'Halloran  * a RID in the transaction header. The PCIe-to-X bridge is permitted to "take
479d0879a2SOliver O'Halloran  * ownership" of a transaction by a PCI-X device when forwarding it to the PCIe
489d0879a2SOliver O'Halloran  * side of the bridge.
499d0879a2SOliver O'Halloran  *
509d0879a2SOliver O'Halloran  * To work around these problems we use the BUS_ALL flag since every subordinate
519d0879a2SOliver O'Halloran  * bus of the bridge should go into the same PE.
529d0879a2SOliver O'Halloran  */
539d0879a2SOliver O'Halloran 
5431bbd45aSRussell Currey /* Indicates operations are frozen for a PE: MMIO in PESTA & DMA in PESTB. */
5531bbd45aSRussell Currey #define PNV_IODA_STOPPED_STATE	0x8000000000000000
5631bbd45aSRussell Currey 
57184cd4a3SBenjamin Herrenschmidt /* Data associated with a PE, including IOMMU tracking etc.. */
584cce9550SGavin Shan struct pnv_phb;
59184cd4a3SBenjamin Herrenschmidt struct pnv_ioda_pe {
607ebdf956SGavin Shan 	unsigned long		flags;
614cce9550SGavin Shan 	struct pnv_phb		*phb;
62c5f7700bSGavin Shan 	int			device_count;
637ebdf956SGavin Shan 
64184cd4a3SBenjamin Herrenschmidt 	/* A PE can be associated with a single device or an
65184cd4a3SBenjamin Herrenschmidt 	 * entire bus (& children). In the former case, pdev
66184cd4a3SBenjamin Herrenschmidt 	 * is populated, in the later case, pbus is.
67184cd4a3SBenjamin Herrenschmidt 	 */
68781a868fSWei Yang #ifdef CONFIG_PCI_IOV
69781a868fSWei Yang 	struct pci_dev          *parent_dev;
70781a868fSWei Yang #endif
71184cd4a3SBenjamin Herrenschmidt 	struct pci_dev		*pdev;
72184cd4a3SBenjamin Herrenschmidt 	struct pci_bus		*pbus;
73184cd4a3SBenjamin Herrenschmidt 
74184cd4a3SBenjamin Herrenschmidt 	/* Effective RID (device RID for a device PE and base bus
75184cd4a3SBenjamin Herrenschmidt 	 * RID with devfn 0 for a bus PE)
76184cd4a3SBenjamin Herrenschmidt 	 */
77184cd4a3SBenjamin Herrenschmidt 	unsigned int		rid;
78184cd4a3SBenjamin Herrenschmidt 
79184cd4a3SBenjamin Herrenschmidt 	/* PE number */
80184cd4a3SBenjamin Herrenschmidt 	unsigned int		pe_number;
81184cd4a3SBenjamin Herrenschmidt 
82184cd4a3SBenjamin Herrenschmidt 	/* "Base" iommu table, ie, 4K TCEs, 32-bit DMA */
83b348aa65SAlexey Kardashevskiy 	struct iommu_table_group table_group;
840bd97167SAlexey Kardashevskiy 	struct npu_comp		*npucomp;
85184cd4a3SBenjamin Herrenschmidt 
86cd15b048SBenjamin Herrenschmidt 	/* 64-bit TCE bypass region */
87cd15b048SBenjamin Herrenschmidt 	bool			tce_bypass_enabled;
88cd15b048SBenjamin Herrenschmidt 	uint64_t		tce_bypass_base;
89184cd4a3SBenjamin Herrenschmidt 
9001e12629SOliver O'Halloran 	/*
9101e12629SOliver O'Halloran 	 * Used to track whether we've done DMA setup for this PE or not. We
9201e12629SOliver O'Halloran 	 * want to defer allocating TCE tables, etc until we've added a
9301e12629SOliver O'Halloran 	 * non-bridge device to the PE.
9401e12629SOliver O'Halloran 	 */
9501e12629SOliver O'Halloran 	bool			dma_setup_done;
9601e12629SOliver O'Halloran 
97184cd4a3SBenjamin Herrenschmidt 	/* MSIs. MVE index is identical for for 32 and 64 bit MSI
98184cd4a3SBenjamin Herrenschmidt 	 * and -1 if not supported. (It's actually identical to the
99184cd4a3SBenjamin Herrenschmidt 	 * PE number)
100184cd4a3SBenjamin Herrenschmidt 	 */
101184cd4a3SBenjamin Herrenschmidt 	int			mve_number;
102184cd4a3SBenjamin Herrenschmidt 
103262af557SGuo Chao 	/* PEs in compound case */
104262af557SGuo Chao 	struct pnv_ioda_pe	*master;
105262af557SGuo Chao 	struct list_head	slaves;
106262af557SGuo Chao 
107184cd4a3SBenjamin Herrenschmidt 	/* Link in list of PE#s */
1087ebdf956SGavin Shan 	struct list_head	list;
109184cd4a3SBenjamin Herrenschmidt };
110184cd4a3SBenjamin Herrenschmidt 
111f5bc6b70SGavin Shan #define PNV_PHB_FLAG_EEH	(1 << 0)
112f5bc6b70SGavin Shan 
11361305a96SBenjamin Herrenschmidt struct pnv_phb {
11461305a96SBenjamin Herrenschmidt 	struct pci_controller	*hose;
11561305a96SBenjamin Herrenschmidt 	enum pnv_phb_type	type;
116cee72d5bSBenjamin Herrenschmidt 	enum pnv_phb_model	model;
1178747f363SGavin Shan 	u64			hub_id;
11861305a96SBenjamin Herrenschmidt 	u64			opal_id;
119f5bc6b70SGavin Shan 	int			flags;
12061305a96SBenjamin Herrenschmidt 	void __iomem		*regs;
121fd141d1aSBenjamin Herrenschmidt 	u64			regs_phys;
122db1266c8SGavin Shan 	int			initialized;
12361305a96SBenjamin Herrenschmidt 	spinlock_t		lock;
12461305a96SBenjamin Herrenschmidt 
12537c367f2SGavin Shan #ifdef CONFIG_DEBUG_FS
1267f52a526SGavin Shan 	int			has_dbgfs;
12737c367f2SGavin Shan 	struct dentry		*dbgfs;
12837c367f2SGavin Shan #endif
12937c367f2SGavin Shan 
130c1a2562aSBenjamin Herrenschmidt 	unsigned int		msi_base;
131c1a2562aSBenjamin Herrenschmidt 	unsigned int		msi32_support;
132fb1b55d6SGavin Shan 	struct msi_bitmap	msi_bmp;
133c1a2562aSBenjamin Herrenschmidt 	int (*msi_setup)(struct pnv_phb *phb, struct pci_dev *dev,
134137436c9SGavin Shan 			 unsigned int hwirq, unsigned int virq,
135137436c9SGavin Shan 			 unsigned int is_64, struct msi_msg *msg);
136262af557SGuo Chao 	int (*init_m64)(struct pnv_phb *phb);
13749dec922SGavin Shan 	int (*get_pe_state)(struct pnv_phb *phb, int pe_no);
13849dec922SGavin Shan 	void (*freeze_pe)(struct pnv_phb *phb, int pe_no);
13949dec922SGavin Shan 	int (*unfreeze_pe)(struct pnv_phb *phb, int pe_no, int opt);
14061305a96SBenjamin Herrenschmidt 
141184cd4a3SBenjamin Herrenschmidt 	struct {
142184cd4a3SBenjamin Herrenschmidt 		/* Global bridge info */
14392b8f137SGavin Shan 		unsigned int		total_pe_num;
14492b8f137SGavin Shan 		unsigned int		reserved_pe_idx;
14563803c39SGavin Shan 		unsigned int		root_pe_idx;
146262af557SGuo Chao 
147262af557SGuo Chao 		/* 32-bit MMIO window */
148184cd4a3SBenjamin Herrenschmidt 		unsigned int		m32_size;
149184cd4a3SBenjamin Herrenschmidt 		unsigned int		m32_segsize;
150184cd4a3SBenjamin Herrenschmidt 		unsigned int		m32_pci_base;
151262af557SGuo Chao 
152262af557SGuo Chao 		/* 64-bit MMIO window */
153262af557SGuo Chao 		unsigned int		m64_bar_idx;
154262af557SGuo Chao 		unsigned long		m64_size;
155262af557SGuo Chao 		unsigned long		m64_segsize;
156262af557SGuo Chao 		unsigned long		m64_base;
157262af557SGuo Chao 		unsigned long		m64_bar_alloc;
158262af557SGuo Chao 
159262af557SGuo Chao 		/* IO ports */
160184cd4a3SBenjamin Herrenschmidt 		unsigned int		io_size;
161184cd4a3SBenjamin Herrenschmidt 		unsigned int		io_segsize;
162184cd4a3SBenjamin Herrenschmidt 		unsigned int		io_pci_base;
163184cd4a3SBenjamin Herrenschmidt 
16413ce7598SGavin Shan 		/* PE allocation */
165781a868fSWei Yang 		struct mutex		pe_alloc_mutex;
16613ce7598SGavin Shan 		unsigned long		*pe_alloc;
16713ce7598SGavin Shan 		struct pnv_ioda_pe	*pe_array;
168184cd4a3SBenjamin Herrenschmidt 
169184cd4a3SBenjamin Herrenschmidt 		/* M32 & IO segment maps */
17093289d8cSGavin Shan 		unsigned int		*m64_segmap;
171184cd4a3SBenjamin Herrenschmidt 		unsigned int		*m32_segmap;
172184cd4a3SBenjamin Herrenschmidt 		unsigned int		*io_segmap;
173184cd4a3SBenjamin Herrenschmidt 
1742b923ed1SGavin Shan 		/* DMA32 segment maps - IODA1 only */
1752b923ed1SGavin Shan 		unsigned int		dma32_count;
1762b923ed1SGavin Shan 		unsigned int		*dma32_segmap;
1772b923ed1SGavin Shan 
178137436c9SGavin Shan 		/* IRQ chip */
179137436c9SGavin Shan 		int			irq_chip_init;
180137436c9SGavin Shan 		struct irq_chip		irq_chip;
181137436c9SGavin Shan 
1827ebdf956SGavin Shan 		/* Sorted list of used PE's based
1837ebdf956SGavin Shan 		 * on the sequence of creation
1847ebdf956SGavin Shan 		 */
1857ebdf956SGavin Shan 		struct list_head	pe_list;
186781a868fSWei Yang 		struct mutex            pe_list_mutex;
1877ebdf956SGavin Shan 
188c127562aSGavin Shan 		/* Reverse map of PEs, indexed by {bus, devfn} */
189c127562aSGavin Shan 		unsigned int		pe_rmap[0x10000];
190184cd4a3SBenjamin Herrenschmidt 	} ioda;
191cee72d5bSBenjamin Herrenschmidt 
1925cb1f8fdSRussell Currey 	/* PHB and hub diagnostics */
1935cb1f8fdSRussell Currey 	unsigned int		diag_data_size;
1945cb1f8fdSRussell Currey 	u8			*diag_data;
19561305a96SBenjamin Herrenschmidt };
19661305a96SBenjamin Herrenschmidt 
19761305a96SBenjamin Herrenschmidt extern struct pci_ops pnv_pci_ops;
19861305a96SBenjamin Herrenschmidt 
19993aef2a7SGavin Shan void pnv_pci_dump_phb_diag_data(struct pci_controller *hose,
20093aef2a7SGavin Shan 				unsigned char *log_buff);
2013532a741SGavin Shan int pnv_pci_cfg_read(struct pci_dn *pdn,
2029bf41be6SGavin Shan 		     int where, int size, u32 *val);
2033532a741SGavin Shan int pnv_pci_cfg_write(struct pci_dn *pdn,
2049bf41be6SGavin Shan 		      int where, int size, u32 val);
2050eaf4defSAlexey Kardashevskiy extern struct iommu_table *pnv_pci_table_alloc(int nid);
2060eaf4defSAlexey Kardashevskiy 
207184cd4a3SBenjamin Herrenschmidt extern void pnv_pci_init_ioda_hub(struct device_node *np);
208aa0c033fSGavin Shan extern void pnv_pci_init_ioda2_phb(struct device_node *np);
2095d2aa710SAlistair Popple extern void pnv_pci_init_npu_phb(struct device_node *np);
2107f2c39e9SFrederic Barrat extern void pnv_pci_init_npu2_opencapi_phb(struct device_node *np);
2110e759bd7SAlexey Kardashevskiy extern void pnv_npu2_map_lpar(struct pnv_ioda_pe *gpe, unsigned long msr);
212d92a208dSGavin Shan extern void pnv_pci_reset_secondary_bus(struct pci_dev *dev);
213cadf364dSGavin Shan extern int pnv_eeh_phb_reset(struct pci_controller *hose, int option);
21473ed148aSBenjamin Herrenschmidt 
21592ae0353SDaniel Axtens extern int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type);
21692ae0353SDaniel Axtens extern void pnv_teardown_msi_irqs(struct pci_dev *pdev);
217a8d7d5fcSOliver O'Halloran extern struct pnv_ioda_pe *pnv_pci_bdfn_to_pe(struct pnv_phb *phb, u16 bdfn);
218f456834aSIan Munsie extern struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev);
219f456834aSIan Munsie extern void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq);
2200bd97167SAlexey Kardashevskiy extern unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
2210bd97167SAlexey Kardashevskiy 		__u64 window_size, __u32 levels);
222b9fde58dSBenjamin Herrenschmidt extern int pnv_eeh_post_init(void);
22392ae0353SDaniel Axtens 
2241e496391SJoe Perches __printf(3, 4)
2257d623e42SAlexey Kardashevskiy extern void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
2267d623e42SAlexey Kardashevskiy 			    const char *fmt, ...);
2277d623e42SAlexey Kardashevskiy #define pe_err(pe, fmt, ...)					\
2287d623e42SAlexey Kardashevskiy 	pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
2297d623e42SAlexey Kardashevskiy #define pe_warn(pe, fmt, ...)					\
2307d623e42SAlexey Kardashevskiy 	pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
2317d623e42SAlexey Kardashevskiy #define pe_info(pe, fmt, ...)					\
2327d623e42SAlexey Kardashevskiy 	pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
2337d623e42SAlexey Kardashevskiy 
2345d2aa710SAlistair Popple /* Nvlink functions */
235f9f83456SAlexey Kardashevskiy extern void pnv_npu_try_dma_set_bypass(struct pci_dev *gpdev, bool bypass);
2366b3d12a9SAlistair Popple extern void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_phb *phb, bool rm);
23703b7bf34SOliver O'Halloran extern void pnv_pci_npu_setup_iommu_groups(void);
2384361b034SIan Munsie 
239191c2287SAlexey Kardashevskiy /* pci-ioda-tce.c */
240c37c792dSAlexey Kardashevskiy #define POWERNV_IOMMU_DEFAULT_LEVELS	2
241191c2287SAlexey Kardashevskiy #define POWERNV_IOMMU_MAX_LEVELS	5
242191c2287SAlexey Kardashevskiy 
243191c2287SAlexey Kardashevskiy extern int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
244191c2287SAlexey Kardashevskiy 		unsigned long uaddr, enum dma_data_direction direction,
245191c2287SAlexey Kardashevskiy 		unsigned long attrs);
246191c2287SAlexey Kardashevskiy extern void pnv_tce_free(struct iommu_table *tbl, long index, long npages);
247191c2287SAlexey Kardashevskiy extern int pnv_tce_xchg(struct iommu_table *tbl, long index,
248a68bd126SAlexey Kardashevskiy 		unsigned long *hpa, enum dma_data_direction *direction,
249a68bd126SAlexey Kardashevskiy 		bool alloc);
250a68bd126SAlexey Kardashevskiy extern __be64 *pnv_tce_useraddrptr(struct iommu_table *tbl, long index,
251a68bd126SAlexey Kardashevskiy 		bool alloc);
252191c2287SAlexey Kardashevskiy extern unsigned long pnv_tce_get(struct iommu_table *tbl, long index);
253191c2287SAlexey Kardashevskiy 
254191c2287SAlexey Kardashevskiy extern long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
255191c2287SAlexey Kardashevskiy 		__u32 page_shift, __u64 window_size, __u32 levels,
256090bad39SAlexey Kardashevskiy 		bool alloc_userspace_copy, struct iommu_table *tbl);
257191c2287SAlexey Kardashevskiy extern void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);
258191c2287SAlexey Kardashevskiy 
259191c2287SAlexey Kardashevskiy extern long pnv_pci_link_table_and_group(int node, int num,
260191c2287SAlexey Kardashevskiy 		struct iommu_table *tbl,
261191c2287SAlexey Kardashevskiy 		struct iommu_table_group *table_group);
262191c2287SAlexey Kardashevskiy extern void pnv_pci_unlink_table_and_group(struct iommu_table *tbl,
263191c2287SAlexey Kardashevskiy 		struct iommu_table_group *table_group);
264191c2287SAlexey Kardashevskiy extern void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
265191c2287SAlexey Kardashevskiy 		void *tce_mem, u64 tce_size,
266191c2287SAlexey Kardashevskiy 		u64 dma_offset, unsigned int page_shift);
267191c2287SAlexey Kardashevskiy 
26896e2006aSOliver O'Halloran extern unsigned long pnv_ioda_parse_tce_sizes(struct pnv_phb *phb);
26996e2006aSOliver O'Halloran 
2705609ffddSOliver O'Halloran static inline struct pnv_phb *pci_bus_to_pnvhb(struct pci_bus *bus)
2715609ffddSOliver O'Halloran {
2725609ffddSOliver O'Halloran 	struct pci_controller *hose = bus->sysdata;
2735609ffddSOliver O'Halloran 
2745609ffddSOliver O'Halloran 	if (hose)
2755609ffddSOliver O'Halloran 		return hose->private_data;
2765609ffddSOliver O'Halloran 
2775609ffddSOliver O'Halloran 	return NULL;
2785609ffddSOliver O'Halloran }
2795609ffddSOliver O'Halloran 
28061305a96SBenjamin Herrenschmidt #endif /* __POWERNV_PCI_H */
281