12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2b8b572e1SStephen Rothwell #ifndef _ASM_POWERPC_PCI_BRIDGE_H
3b8b572e1SStephen Rothwell #define _ASM_POWERPC_PCI_BRIDGE_H
4b8b572e1SStephen Rothwell #ifdef __KERNEL__
5b8b572e1SStephen Rothwell /*
6b8b572e1SStephen Rothwell  */
7b8b572e1SStephen Rothwell #include <linux/pci.h>
8b8b572e1SStephen Rothwell #include <linux/list.h>
9b8b572e1SStephen Rothwell #include <linux/ioport.h>
1098fa15f3SAnshuman Khandual #include <linux/numa.h>
11*a9409044SAlexey Kardashevskiy #include <linux/iommu.h>
12b8b572e1SStephen Rothwell 
13b8b572e1SStephen Rothwell struct device_node;
14b8b572e1SStephen Rothwell 
15b8b572e1SStephen Rothwell /*
16e02def5bSDaniel Axtens  * PCI controller operations
17e02def5bSDaniel Axtens  */
18e02def5bSDaniel Axtens struct pci_controller_ops {
19062b26baSGavin Shan 	void		(*dma_dev_setup)(struct pci_dev *pdev);
20b122c954SDaniel Axtens 	void		(*dma_bus_setup)(struct pci_bus *bus);
218617a5c5SChristoph Hellwig 	bool		(*iommu_bypass_supported)(struct pci_dev *pdev,
228617a5c5SChristoph Hellwig 				u64 mask);
23ff9df8c8SDaniel Axtens 
24062b26baSGavin Shan 	int		(*probe_mode)(struct pci_bus *bus);
25b31e79f8SDaniel Axtens 
26b31e79f8SDaniel Axtens 	/* Called when pci_enable_device() is called. Returns true to
27b31e79f8SDaniel Axtens 	 * allow assignment/enabling of the device. */
28062b26baSGavin Shan 	bool		(*enable_device_hook)(struct pci_dev *pdev);
29542070baSDaniel Axtens 
30062b26baSGavin Shan 	void		(*disable_device)(struct pci_dev *pdev);
31abeeed6dSMichael Neuling 
32062b26baSGavin Shan 	void		(*release_device)(struct pci_dev *pdev);
3310e79630SMichael Neuling 
34542070baSDaniel Axtens 	/* Called during PCI resource reassignment */
35062b26baSGavin Shan 	resource_size_t (*window_alignment)(struct pci_bus *bus,
36062b26baSGavin Shan 					    unsigned long type);
37c5fcb29aSGavin Shan 	void		(*setup_bridge)(struct pci_bus *bus,
38c5fcb29aSGavin Shan 					unsigned long type);
39062b26baSGavin Shan 	void		(*reset_secondary_bus)(struct pci_dev *pdev);
40e059b105SDaniel Axtens 
41e059b105SDaniel Axtens #ifdef CONFIG_PCI_MSI
42062b26baSGavin Shan 	int		(*setup_msi_irqs)(struct pci_dev *pdev,
43e059b105SDaniel Axtens 					  int nvec, int type);
44062b26baSGavin Shan 	void		(*teardown_msi_irqs)(struct pci_dev *pdev);
45e059b105SDaniel Axtens #endif
463405c257SDaniel Axtens 
47062b26baSGavin Shan 	void		(*shutdown)(struct pci_controller *hose);
48*a9409044SAlexey Kardashevskiy 
49*a9409044SAlexey Kardashevskiy 	struct iommu_group *(*device_group)(struct pci_controller *hose,
50*a9409044SAlexey Kardashevskiy 					    struct pci_dev *pdev);
51e02def5bSDaniel Axtens };
52e02def5bSDaniel Axtens 
53e02def5bSDaniel Axtens /*
54b8b572e1SStephen Rothwell  * Structure of a PCI controller (host bridge)
55b8b572e1SStephen Rothwell  */
56b8b572e1SStephen Rothwell struct pci_controller {
57b8b572e1SStephen Rothwell 	struct pci_bus *bus;
58b8b572e1SStephen Rothwell 	char is_dynamic;
59b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
60b8b572e1SStephen Rothwell 	int node;
61b8b572e1SStephen Rothwell #endif
62b8b572e1SStephen Rothwell 	struct device_node *dn;
63b8b572e1SStephen Rothwell 	struct list_head list_node;
64b8b572e1SStephen Rothwell 	struct device *parent;
65b8b572e1SStephen Rothwell 
66b8b572e1SStephen Rothwell 	int first_busno;
67b8b572e1SStephen Rothwell 	int last_busno;
68b8b572e1SStephen Rothwell 	int self_busno;
69be8e60d8SYinghai Lu 	struct resource busn;
70b8b572e1SStephen Rothwell 
71b8b572e1SStephen Rothwell 	void __iomem *io_base_virt;
72b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
73b274014cSChristoph Hellwig 	void __iomem *io_base_alloc;
74b8b572e1SStephen Rothwell #endif
75b8b572e1SStephen Rothwell 	resource_size_t io_base_phys;
76b8b572e1SStephen Rothwell 	resource_size_t pci_io_size;
77b8b572e1SStephen Rothwell 
78e9f82cb7SBenjamin Herrenschmidt 	/* Some machines have a special region to forward the ISA
79e9f82cb7SBenjamin Herrenschmidt 	 * "memory" cycles such as VGA memory regions. Left to 0
80e9f82cb7SBenjamin Herrenschmidt 	 * if unsupported
81e9f82cb7SBenjamin Herrenschmidt 	 */
82e9f82cb7SBenjamin Herrenschmidt 	resource_size_t	isa_mem_phys;
83e9f82cb7SBenjamin Herrenschmidt 	resource_size_t	isa_mem_size;
84e9f82cb7SBenjamin Herrenschmidt 
85e02def5bSDaniel Axtens 	struct pci_controller_ops controller_ops;
86b8b572e1SStephen Rothwell 	struct pci_ops *ops;
87b8b572e1SStephen Rothwell 	unsigned int __iomem *cfg_addr;
88b8b572e1SStephen Rothwell 	void __iomem *cfg_data;
89b8b572e1SStephen Rothwell 
90b8b572e1SStephen Rothwell 	/*
91b8b572e1SStephen Rothwell 	 * Used for variants of PCI indirect handling and possible quirks:
92b8b572e1SStephen Rothwell 	 *  SET_CFG_TYPE - used on 4xx or any PHB that does explicit type0/1
93b8b572e1SStephen Rothwell 	 *  EXT_REG - provides access to PCI-e extended registers
9425985edcSLucas De Marchi 	 *  SURPRESS_PRIMARY_BUS - we suppress the setting of PCI_PRIMARY_BUS
95b8b572e1SStephen Rothwell 	 *   on Freescale PCI-e controllers since they used the PCI_PRIMARY_BUS
96b8b572e1SStephen Rothwell 	 *   to determine which bus number to match on when generating type0
97b8b572e1SStephen Rothwell 	 *   config cycles
98b8b572e1SStephen Rothwell 	 *  NO_PCIE_LINK - the Freescale PCI-e controllers have issues with
99b8b572e1SStephen Rothwell 	 *   hanging if we don't have link and try to do config cycles to
100b8b572e1SStephen Rothwell 	 *   anything but the PHB.  Only allow talking to the PHB if this is
101b8b572e1SStephen Rothwell 	 *   set.
102b8b572e1SStephen Rothwell 	 *  BIG_ENDIAN - cfg_addr is a big endian register
103b8b572e1SStephen Rothwell 	 *  BROKEN_MRM - the 440EPx/GRx chips have an errata that causes hangs on
104b8b572e1SStephen Rothwell 	 *   the PLB4.  Effectively disable MRM commands by setting this.
10534642bbbSKumar Gala 	 *  FSL_CFG_REG_LINK - Freescale controller version in which the PCIe
10634642bbbSKumar Gala 	 *   link status is in a RC PCIe cfg register (vs being a SoC register)
107b8b572e1SStephen Rothwell 	 */
108b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_SET_CFG_TYPE		0x00000001
109b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_EXT_REG		0x00000002
110b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS	0x00000004
111b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_NO_PCIE_LINK		0x00000008
112b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_BIG_ENDIAN		0x00000010
113b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_BROKEN_MRM		0x00000020
11434642bbbSKumar Gala #define PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK	0x00000040
115b8b572e1SStephen Rothwell 	u32 indirect_type;
116b8b572e1SStephen Rothwell 	/* Currently, we limit ourselves to 1 IO range and 3 mem
117b8b572e1SStephen Rothwell 	 * ranges since the common pci_bus structure can't handle more
118b8b572e1SStephen Rothwell 	 */
119b8b572e1SStephen Rothwell 	struct resource	io_resource;
120b8b572e1SStephen Rothwell 	struct resource mem_resources[3];
1213fd47f06SBenjamin Herrenschmidt 	resource_size_t mem_offset[3];
122b8b572e1SStephen Rothwell 	int global_number;		/* PCI domain number */
12389d93347SBecky Bruce 
12489d93347SBecky Bruce 	resource_size_t dma_window_base_cur;
12589d93347SBecky Bruce 	resource_size_t dma_window_size;
12689d93347SBecky Bruce 
127b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
128b8b572e1SStephen Rothwell 	unsigned long buid;
129cca87d30SGavin Shan 	struct pci_dn *pci_data;
13034642bbbSKumar Gala #endif	/* CONFIG_PPC64 */
131b8b572e1SStephen Rothwell 
132b8b572e1SStephen Rothwell 	void *private_data;
133a5f3d2c1SCédric Le Goater 
134a5f3d2c1SCédric Le Goater 	/* IRQ domain hierarchy */
135a5f3d2c1SCédric Le Goater 	struct irq_domain	*dev_domain;
136a5f3d2c1SCédric Le Goater 	struct irq_domain	*msi_domain;
137a5f3d2c1SCédric Le Goater 	struct fwnode_handle	*fwnode;
138*a9409044SAlexey Kardashevskiy 
139*a9409044SAlexey Kardashevskiy 	/* iommu_ops support */
140*a9409044SAlexey Kardashevskiy 	struct iommu_device	iommu;
141b8b572e1SStephen Rothwell };
142b8b572e1SStephen Rothwell 
143b8b572e1SStephen Rothwell /* These are used for config access before all the PCI probing
144b8b572e1SStephen Rothwell    has been done. */
145b8b572e1SStephen Rothwell extern int early_read_config_byte(struct pci_controller *hose, int bus,
146b8b572e1SStephen Rothwell 			int dev_fn, int where, u8 *val);
147b8b572e1SStephen Rothwell extern int early_read_config_word(struct pci_controller *hose, int bus,
148b8b572e1SStephen Rothwell 			int dev_fn, int where, u16 *val);
149b8b572e1SStephen Rothwell extern int early_read_config_dword(struct pci_controller *hose, int bus,
150b8b572e1SStephen Rothwell 			int dev_fn, int where, u32 *val);
151b8b572e1SStephen Rothwell extern int early_write_config_byte(struct pci_controller *hose, int bus,
152b8b572e1SStephen Rothwell 			int dev_fn, int where, u8 val);
153b8b572e1SStephen Rothwell extern int early_write_config_word(struct pci_controller *hose, int bus,
154b8b572e1SStephen Rothwell 			int dev_fn, int where, u16 val);
155b8b572e1SStephen Rothwell extern int early_write_config_dword(struct pci_controller *hose, int bus,
156b8b572e1SStephen Rothwell 			int dev_fn, int where, u32 val);
157b8b572e1SStephen Rothwell 
158b8b572e1SStephen Rothwell extern int early_find_capability(struct pci_controller *hose, int bus,
159b8b572e1SStephen Rothwell 				 int dev_fn, int cap);
160b8b572e1SStephen Rothwell 
161b8b572e1SStephen Rothwell extern void setup_indirect_pci(struct pci_controller* hose,
162b8b572e1SStephen Rothwell 			       resource_size_t cfg_addr,
163b8b572e1SStephen Rothwell 			       resource_size_t cfg_data, u32 flags);
16489c2dd62SKumar Gala 
16550d8f87dSRojhalat Ibrahim extern int indirect_read_config(struct pci_bus *bus, unsigned int devfn,
16650d8f87dSRojhalat Ibrahim 				int offset, int len, u32 *val);
16750d8f87dSRojhalat Ibrahim 
1686d5f6a0eSKim Phillips extern int __indirect_read_config(struct pci_controller *hose,
1696d5f6a0eSKim Phillips 				  unsigned char bus_number, unsigned int devfn,
1706d5f6a0eSKim Phillips 				  int offset, int len, u32 *val);
1716d5f6a0eSKim Phillips 
17250d8f87dSRojhalat Ibrahim extern int indirect_write_config(struct pci_bus *bus, unsigned int devfn,
17350d8f87dSRojhalat Ibrahim 				 int offset, int len, u32 val);
17450d8f87dSRojhalat Ibrahim 
pci_bus_to_host(const struct pci_bus * bus)17589c2dd62SKumar Gala static inline struct pci_controller *pci_bus_to_host(const struct pci_bus *bus)
17689c2dd62SKumar Gala {
17789c2dd62SKumar Gala 	return bus->sysdata;
17889c2dd62SKumar Gala }
17989c2dd62SKumar Gala 
180a2954a7eSPali Rohár #ifdef CONFIG_PPC_PMAC
18198d9f30cSBenjamin Herrenschmidt extern int pci_device_from_OF_node(struct device_node *node,
18298d9f30cSBenjamin Herrenschmidt 				   u8 *bus, u8 *devfn);
183a2954a7eSPali Rohár #endif
1840aa297e7SChristophe Leroy #ifndef CONFIG_PPC64
1850aa297e7SChristophe Leroy 
1865d2eb73aSPali Rohár #ifdef CONFIG_PPC_PCI_OF_BUS_MAP
18798d9f30cSBenjamin Herrenschmidt extern void pci_create_OF_bus_map(void);
1885d2eb73aSPali Rohár #else
pci_create_OF_bus_map(void)1895d2eb73aSPali Rohár static inline void pci_create_OF_bus_map(void) {}
19070454458SPali Rohár #endif
19198d9f30cSBenjamin Herrenschmidt 
192b8b572e1SStephen Rothwell #else	/* CONFIG_PPC64 */
193b8b572e1SStephen Rothwell 
194b8b572e1SStephen Rothwell /*
195b8b572e1SStephen Rothwell  * PCI stuff, for nodes representing PCI devices, pointed to
196b8b572e1SStephen Rothwell  * by device_node->data.
197b8b572e1SStephen Rothwell  */
198b8b572e1SStephen Rothwell struct iommu_table;
199b8b572e1SStephen Rothwell 
200b8b572e1SStephen Rothwell struct pci_dn {
201cca87d30SGavin Shan 	int     flags;
202a8b2f828SGavin Shan #define PCI_DN_FLAG_IOV_VF	0x01
2035ef753aeSOliver O'Halloran #define PCI_DN_FLAG_DEAD	0x02    /* Device has been hot-removed */
204cca87d30SGavin Shan 
205b8b572e1SStephen Rothwell 	int	busno;			/* pci bus number */
206b8b572e1SStephen Rothwell 	int	devfn;			/* pci device and function number */
207c035ff1dSGavin Shan 	int	vendor_id;		/* Vendor ID */
208c035ff1dSGavin Shan 	int	device_id;		/* Device ID */
209c035ff1dSGavin Shan 	int	class_code;		/* Device class code */
210b8b572e1SStephen Rothwell 
211cca87d30SGavin Shan 	struct  pci_dn *parent;
212b8b572e1SStephen Rothwell 	struct  pci_controller *phb;	/* for pci devices */
213b348aa65SAlexey Kardashevskiy 	struct	iommu_table_group *table_group;	/* for phb's or bridges */
214b8b572e1SStephen Rothwell 
215b8b572e1SStephen Rothwell 	int	pci_ext_config_space;	/* for pci devices */
216184cd4a3SBenjamin Herrenschmidt #ifdef CONFIG_EEH
2172a0352faSGavin Shan 	struct eeh_dev *edev;		/* eeh device */
218b8b572e1SStephen Rothwell #endif
219689ee8c9SGavin Shan #define IODA_INVALID_PE		0xFFFFFFFF
220689ee8c9SGavin Shan 	unsigned int pe_number;
2216e628c7dSWei Yang #ifdef CONFIG_PCI_IOV
2226e628c7dSWei Yang 	u16     vfs_expanded;		/* number of VFs IOV BAR expanded */
223781a868fSWei Yang 	u16     num_vfs;		/* number of VFs enabled*/
224689ee8c9SGavin Shan 	unsigned int *pe_num_map;	/* PE# for the first VF PE or array */
225ee8222feSWei Yang 	bool    m64_single_mode;	/* Use M64 BAR in Single Mode */
226781a868fSWei Yang #define IODA_INVALID_M64        (-1)
227565a744dSBryant G. Ly 	int     (*m64_map)[PCI_SRIOV_NUM_BARS];	/* Only used on powernv */
228565a744dSBryant G. Ly 	int     last_allow_rc;			/* Only used on pseries */
2296e628c7dSWei Yang #endif /* CONFIG_PCI_IOV */
2300dc2830eSWei Yang 	int	mps;			/* Maximum Payload Size */
231cca87d30SGavin Shan 	struct list_head child_list;
232cca87d30SGavin Shan 	struct list_head list;
233d6f934fdSAlexey Kardashevskiy 	struct resource holes[PCI_SRIOV_NUM_BARS];
234b8b572e1SStephen Rothwell };
235b8b572e1SStephen Rothwell 
236b8b572e1SStephen Rothwell /* Get the pointer to a device_node's pci_dn */
237b8b572e1SStephen Rothwell #define PCI_DN(dn)	((struct pci_dn *) (dn)->data)
238b8b572e1SStephen Rothwell 
239cca87d30SGavin Shan extern struct pci_dn *pci_get_pdn_by_devfn(struct pci_bus *bus,
240cca87d30SGavin Shan 					   int devfn);
241b72c1f65SBenjamin Herrenschmidt extern struct pci_dn *pci_get_pdn(struct pci_dev *pdev);
242d8f66f41SGavin Shan extern struct pci_dn *pci_add_device_node_info(struct pci_controller *hose,
243d8f66f41SGavin Shan 					       struct device_node *dn);
244de5a28acSGavin Shan extern void pci_remove_device_node_info(struct device_node *dn);
245b8b572e1SStephen Rothwell 
2468cd6aaccSOliver O'Halloran #ifdef CONFIG_PCI_IOV
2478cd6aaccSOliver O'Halloran struct pci_dn *add_sriov_vf_pdns(struct pci_dev *pdev);
2488cd6aaccSOliver O'Halloran void remove_sriov_vf_pdns(struct pci_dev *pdev);
2498cd6aaccSOliver O'Halloran #endif
2508cd6aaccSOliver O'Halloran 
2512a0352faSGavin Shan #if defined(CONFIG_EEH)
pdn_to_eeh_dev(struct pci_dn * pdn)252e8e9b34cSGavin Shan static inline struct eeh_dev *pdn_to_eeh_dev(struct pci_dn *pdn)
253e8e9b34cSGavin Shan {
254e8e9b34cSGavin Shan 	return pdn ? pdn->edev : NULL;
255e8e9b34cSGavin Shan }
256f8f7d63fSGavin Shan #else
257e8e9b34cSGavin Shan #define pdn_to_eeh_dev(x)	(NULL)
2582a0352faSGavin Shan #endif
2592a0352faSGavin Shan 
260b8b572e1SStephen Rothwell /** Find the bus corresponding to the indicated device node */
2613773dd25SGavin Shan extern struct pci_bus *pci_find_bus_by_node(struct device_node *dn);
262b8b572e1SStephen Rothwell 
263b8b572e1SStephen Rothwell /** Remove all of the PCI devices under this bus */
264bd251b89SGavin Shan extern void pci_hp_remove_devices(struct pci_bus *bus);
265b8b572e1SStephen Rothwell 
266b8b572e1SStephen Rothwell /** Discover new pci devices under this bus, and add them */
267bd251b89SGavin Shan extern void pci_hp_add_devices(struct pci_bus *bus);
268b8b572e1SStephen Rothwell 
269b8b572e1SStephen Rothwell extern int pcibios_unmap_io_space(struct pci_bus *bus);
270b8b572e1SStephen Rothwell extern int pcibios_map_io_space(struct pci_bus *bus);
271b8b572e1SStephen Rothwell 
272b8b572e1SStephen Rothwell #ifdef CONFIG_NUMA
273b8b572e1SStephen Rothwell #define PHB_SET_NODE(PHB, NODE)		((PHB)->node = (NODE))
274b8b572e1SStephen Rothwell #else
27598fa15f3SAnshuman Khandual #define PHB_SET_NODE(PHB, NODE)		((PHB)->node = NUMA_NO_NODE)
276b8b572e1SStephen Rothwell #endif
277b8b572e1SStephen Rothwell 
278b8b572e1SStephen Rothwell #endif	/* CONFIG_PPC64 */
279b8b572e1SStephen Rothwell 
280b8b572e1SStephen Rothwell /* Get the PCI host controller for an OF device */
281b8b572e1SStephen Rothwell extern struct pci_controller *pci_find_hose_for_OF_device(
282b8b572e1SStephen Rothwell 			struct device_node* node);
283b8b572e1SStephen Rothwell 
28467060cb1SOliver O'Halloran extern struct pci_controller *pci_find_controller_for_domain(int domain_nr);
28567060cb1SOliver O'Halloran 
286b8b572e1SStephen Rothwell /* Fill up host controller resources from the OF node */
287b8b572e1SStephen Rothwell extern void pci_process_bridge_OF_ranges(struct pci_controller *hose,
288b8b572e1SStephen Rothwell 			struct device_node *dev, int primary);
289b8b572e1SStephen Rothwell 
290b8b572e1SStephen Rothwell /* Allocate & free a PCI host bridge structure */
291b8b572e1SStephen Rothwell extern struct pci_controller *pcibios_alloc_controller(struct device_node *dev);
292b8b572e1SStephen Rothwell extern void pcibios_free_controller(struct pci_controller *phb);
2932dd9c11bSMauricio Faria de Oliveira extern void pcibios_free_controller_deferred(struct pci_host_bridge *bridge);
294b8b572e1SStephen Rothwell 
295b8b572e1SStephen Rothwell #ifdef CONFIG_PCI
296b8b572e1SStephen Rothwell extern int pcibios_vaddr_is_ioport(void __iomem *address);
297b8b572e1SStephen Rothwell #else
pcibios_vaddr_is_ioport(void __iomem * address)298b8b572e1SStephen Rothwell static inline int pcibios_vaddr_is_ioport(void __iomem *address)
299b8b572e1SStephen Rothwell {
300b8b572e1SStephen Rothwell 	return 0;
301b8b572e1SStephen Rothwell }
302b8b572e1SStephen Rothwell #endif	/* CONFIG_PCI */
303b8b572e1SStephen Rothwell 
304b8b572e1SStephen Rothwell #endif	/* __KERNEL__ */
305b8b572e1SStephen Rothwell #endif	/* _ASM_POWERPC_PCI_BRIDGE_H */
306