1b8b572e1SStephen Rothwell #ifndef _ASM_POWERPC_PCI_BRIDGE_H
2b8b572e1SStephen Rothwell #define _ASM_POWERPC_PCI_BRIDGE_H
3b8b572e1SStephen Rothwell #ifdef __KERNEL__
4b8b572e1SStephen Rothwell /*
5b8b572e1SStephen Rothwell  * This program is free software; you can redistribute it and/or
6b8b572e1SStephen Rothwell  * modify it under the terms of the GNU General Public License
7b8b572e1SStephen Rothwell  * as published by the Free Software Foundation; either version
8b8b572e1SStephen Rothwell  * 2 of the License, or (at your option) any later version.
9b8b572e1SStephen Rothwell  */
10b8b572e1SStephen Rothwell #include <linux/pci.h>
11b8b572e1SStephen Rothwell #include <linux/list.h>
12b8b572e1SStephen Rothwell #include <linux/ioport.h>
13f4ffd5e5SRob Herring #include <asm-generic/pci-bridge.h>
14b8b572e1SStephen Rothwell 
15b8b572e1SStephen Rothwell struct device_node;
16b8b572e1SStephen Rothwell 
17b8b572e1SStephen Rothwell /*
18e02def5bSDaniel Axtens  * PCI controller operations
19e02def5bSDaniel Axtens  */
20e02def5bSDaniel Axtens struct pci_controller_ops {
21e02def5bSDaniel Axtens 	void		(*dma_dev_setup)(struct pci_dev *dev);
22b122c954SDaniel Axtens 	void		(*dma_bus_setup)(struct pci_bus *bus);
23ff9df8c8SDaniel Axtens 
24ff9df8c8SDaniel Axtens 	int		(*probe_mode)(struct pci_bus *);
25b31e79f8SDaniel Axtens 
26b31e79f8SDaniel Axtens 	/* Called when pci_enable_device() is called. Returns true to
27b31e79f8SDaniel Axtens 	 * allow assignment/enabling of the device. */
28b31e79f8SDaniel Axtens 	bool		(*enable_device_hook)(struct pci_dev *);
29542070baSDaniel Axtens 
30abeeed6dSMichael Neuling 	void		(*disable_device)(struct pci_dev *);
31abeeed6dSMichael Neuling 
3210e79630SMichael Neuling 	void		(*release_device)(struct pci_dev *);
3310e79630SMichael Neuling 
34542070baSDaniel Axtens 	/* Called during PCI resource reassignment */
35542070baSDaniel Axtens 	resource_size_t (*window_alignment)(struct pci_bus *, unsigned long type);
36cd16c7baSDaniel Axtens 	void		(*reset_secondary_bus)(struct pci_dev *dev);
37e059b105SDaniel Axtens 
38e059b105SDaniel Axtens #ifdef CONFIG_PCI_MSI
39e059b105SDaniel Axtens 	int		(*setup_msi_irqs)(struct pci_dev *dev,
40e059b105SDaniel Axtens 					  int nvec, int type);
41e059b105SDaniel Axtens 	void		(*teardown_msi_irqs)(struct pci_dev *dev);
42e059b105SDaniel Axtens #endif
433405c257SDaniel Axtens 
443405c257SDaniel Axtens 	int             (*dma_set_mask)(struct pci_dev *dev, u64 dma_mask);
4553522982SAndrew Donnellan 	u64		(*dma_get_required_mask)(struct pci_dev *dev);
467a8e6bbfSMichael Neuling 
477a8e6bbfSMichael Neuling 	void		(*shutdown)(struct pci_controller *);
48e02def5bSDaniel Axtens };
49e02def5bSDaniel Axtens 
50e02def5bSDaniel Axtens /*
51b8b572e1SStephen Rothwell  * Structure of a PCI controller (host bridge)
52b8b572e1SStephen Rothwell  */
53b8b572e1SStephen Rothwell struct pci_controller {
54b8b572e1SStephen Rothwell 	struct pci_bus *bus;
55b8b572e1SStephen Rothwell 	char is_dynamic;
56b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
57b8b572e1SStephen Rothwell 	int node;
58b8b572e1SStephen Rothwell #endif
59b8b572e1SStephen Rothwell 	struct device_node *dn;
60b8b572e1SStephen Rothwell 	struct list_head list_node;
61b8b572e1SStephen Rothwell 	struct device *parent;
62b8b572e1SStephen Rothwell 
63b8b572e1SStephen Rothwell 	int first_busno;
64b8b572e1SStephen Rothwell 	int last_busno;
65b8b572e1SStephen Rothwell 	int self_busno;
66be8e60d8SYinghai Lu 	struct resource busn;
67b8b572e1SStephen Rothwell 
68b8b572e1SStephen Rothwell 	void __iomem *io_base_virt;
69b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
70b8b572e1SStephen Rothwell 	void *io_base_alloc;
71b8b572e1SStephen Rothwell #endif
72b8b572e1SStephen Rothwell 	resource_size_t io_base_phys;
73b8b572e1SStephen Rothwell 	resource_size_t pci_io_size;
74b8b572e1SStephen Rothwell 
75e9f82cb7SBenjamin Herrenschmidt 	/* Some machines have a special region to forward the ISA
76e9f82cb7SBenjamin Herrenschmidt 	 * "memory" cycles such as VGA memory regions. Left to 0
77e9f82cb7SBenjamin Herrenschmidt 	 * if unsupported
78e9f82cb7SBenjamin Herrenschmidt 	 */
79e9f82cb7SBenjamin Herrenschmidt 	resource_size_t	isa_mem_phys;
80e9f82cb7SBenjamin Herrenschmidt 	resource_size_t	isa_mem_size;
81e9f82cb7SBenjamin Herrenschmidt 
82e02def5bSDaniel Axtens 	struct pci_controller_ops controller_ops;
83b8b572e1SStephen Rothwell 	struct pci_ops *ops;
84b8b572e1SStephen Rothwell 	unsigned int __iomem *cfg_addr;
85b8b572e1SStephen Rothwell 	void __iomem *cfg_data;
86b8b572e1SStephen Rothwell 
87b8b572e1SStephen Rothwell 	/*
88b8b572e1SStephen Rothwell 	 * Used for variants of PCI indirect handling and possible quirks:
89b8b572e1SStephen Rothwell 	 *  SET_CFG_TYPE - used on 4xx or any PHB that does explicit type0/1
90b8b572e1SStephen Rothwell 	 *  EXT_REG - provides access to PCI-e extended registers
9125985edcSLucas De Marchi 	 *  SURPRESS_PRIMARY_BUS - we suppress the setting of PCI_PRIMARY_BUS
92b8b572e1SStephen Rothwell 	 *   on Freescale PCI-e controllers since they used the PCI_PRIMARY_BUS
93b8b572e1SStephen Rothwell 	 *   to determine which bus number to match on when generating type0
94b8b572e1SStephen Rothwell 	 *   config cycles
95b8b572e1SStephen Rothwell 	 *  NO_PCIE_LINK - the Freescale PCI-e controllers have issues with
96b8b572e1SStephen Rothwell 	 *   hanging if we don't have link and try to do config cycles to
97b8b572e1SStephen Rothwell 	 *   anything but the PHB.  Only allow talking to the PHB if this is
98b8b572e1SStephen Rothwell 	 *   set.
99b8b572e1SStephen Rothwell 	 *  BIG_ENDIAN - cfg_addr is a big endian register
100b8b572e1SStephen Rothwell 	 *  BROKEN_MRM - the 440EPx/GRx chips have an errata that causes hangs on
101b8b572e1SStephen Rothwell 	 *   the PLB4.  Effectively disable MRM commands by setting this.
10234642bbbSKumar Gala 	 *  FSL_CFG_REG_LINK - Freescale controller version in which the PCIe
10334642bbbSKumar Gala 	 *   link status is in a RC PCIe cfg register (vs being a SoC register)
104b8b572e1SStephen Rothwell 	 */
105b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_SET_CFG_TYPE		0x00000001
106b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_EXT_REG		0x00000002
107b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS	0x00000004
108b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_NO_PCIE_LINK		0x00000008
109b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_BIG_ENDIAN		0x00000010
110b8b572e1SStephen Rothwell #define PPC_INDIRECT_TYPE_BROKEN_MRM		0x00000020
11134642bbbSKumar Gala #define PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK	0x00000040
112b8b572e1SStephen Rothwell 	u32 indirect_type;
113b8b572e1SStephen Rothwell 	/* Currently, we limit ourselves to 1 IO range and 3 mem
114b8b572e1SStephen Rothwell 	 * ranges since the common pci_bus structure can't handle more
115b8b572e1SStephen Rothwell 	 */
116b8b572e1SStephen Rothwell 	struct resource	io_resource;
117b8b572e1SStephen Rothwell 	struct resource mem_resources[3];
1183fd47f06SBenjamin Herrenschmidt 	resource_size_t mem_offset[3];
119b8b572e1SStephen Rothwell 	int global_number;		/* PCI domain number */
12089d93347SBecky Bruce 
12189d93347SBecky Bruce 	resource_size_t dma_window_base_cur;
12289d93347SBecky Bruce 	resource_size_t dma_window_size;
12389d93347SBecky Bruce 
124b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64
125b8b572e1SStephen Rothwell 	unsigned long buid;
126cca87d30SGavin Shan 	struct pci_dn *pci_data;
12734642bbbSKumar Gala #endif	/* CONFIG_PPC64 */
128b8b572e1SStephen Rothwell 
129b8b572e1SStephen Rothwell 	void *private_data;
130b8b572e1SStephen Rothwell };
131b8b572e1SStephen Rothwell 
132b8b572e1SStephen Rothwell /* These are used for config access before all the PCI probing
133b8b572e1SStephen Rothwell    has been done. */
134b8b572e1SStephen Rothwell extern int early_read_config_byte(struct pci_controller *hose, int bus,
135b8b572e1SStephen Rothwell 			int dev_fn, int where, u8 *val);
136b8b572e1SStephen Rothwell extern int early_read_config_word(struct pci_controller *hose, int bus,
137b8b572e1SStephen Rothwell 			int dev_fn, int where, u16 *val);
138b8b572e1SStephen Rothwell extern int early_read_config_dword(struct pci_controller *hose, int bus,
139b8b572e1SStephen Rothwell 			int dev_fn, int where, u32 *val);
140b8b572e1SStephen Rothwell extern int early_write_config_byte(struct pci_controller *hose, int bus,
141b8b572e1SStephen Rothwell 			int dev_fn, int where, u8 val);
142b8b572e1SStephen Rothwell extern int early_write_config_word(struct pci_controller *hose, int bus,
143b8b572e1SStephen Rothwell 			int dev_fn, int where, u16 val);
144b8b572e1SStephen Rothwell extern int early_write_config_dword(struct pci_controller *hose, int bus,
145b8b572e1SStephen Rothwell 			int dev_fn, int where, u32 val);
146b8b572e1SStephen Rothwell 
147b8b572e1SStephen Rothwell extern int early_find_capability(struct pci_controller *hose, int bus,
148b8b572e1SStephen Rothwell 				 int dev_fn, int cap);
149b8b572e1SStephen Rothwell 
150b8b572e1SStephen Rothwell extern void setup_indirect_pci(struct pci_controller* hose,
151b8b572e1SStephen Rothwell 			       resource_size_t cfg_addr,
152b8b572e1SStephen Rothwell 			       resource_size_t cfg_data, u32 flags);
15389c2dd62SKumar Gala 
15450d8f87dSRojhalat Ibrahim extern int indirect_read_config(struct pci_bus *bus, unsigned int devfn,
15550d8f87dSRojhalat Ibrahim 				int offset, int len, u32 *val);
15650d8f87dSRojhalat Ibrahim 
1576d5f6a0eSKim Phillips extern int __indirect_read_config(struct pci_controller *hose,
1586d5f6a0eSKim Phillips 				  unsigned char bus_number, unsigned int devfn,
1596d5f6a0eSKim Phillips 				  int offset, int len, u32 *val);
1606d5f6a0eSKim Phillips 
16150d8f87dSRojhalat Ibrahim extern int indirect_write_config(struct pci_bus *bus, unsigned int devfn,
16250d8f87dSRojhalat Ibrahim 				 int offset, int len, u32 val);
16350d8f87dSRojhalat Ibrahim 
16489c2dd62SKumar Gala static inline struct pci_controller *pci_bus_to_host(const struct pci_bus *bus)
16589c2dd62SKumar Gala {
16689c2dd62SKumar Gala 	return bus->sysdata;
16789c2dd62SKumar Gala }
16889c2dd62SKumar Gala 
16998d9f30cSBenjamin Herrenschmidt #ifndef CONFIG_PPC64
17098d9f30cSBenjamin Herrenschmidt 
17198d9f30cSBenjamin Herrenschmidt extern int pci_device_from_OF_node(struct device_node *node,
17298d9f30cSBenjamin Herrenschmidt 				   u8 *bus, u8 *devfn);
17398d9f30cSBenjamin Herrenschmidt extern void pci_create_OF_bus_map(void);
17498d9f30cSBenjamin Herrenschmidt 
17589c2dd62SKumar Gala static inline int isa_vaddr_is_ioport(void __iomem *address)
17689c2dd62SKumar Gala {
17789c2dd62SKumar Gala 	/* No specific ISA handling on ppc32 at this stage, it
17889c2dd62SKumar Gala 	 * all goes through PCI
17989c2dd62SKumar Gala 	 */
18089c2dd62SKumar Gala 	return 0;
18189c2dd62SKumar Gala }
18289c2dd62SKumar Gala 
183b8b572e1SStephen Rothwell #else	/* CONFIG_PPC64 */
184b8b572e1SStephen Rothwell 
185b8b572e1SStephen Rothwell /*
186b8b572e1SStephen Rothwell  * PCI stuff, for nodes representing PCI devices, pointed to
187b8b572e1SStephen Rothwell  * by device_node->data.
188b8b572e1SStephen Rothwell  */
189b8b572e1SStephen Rothwell struct iommu_table;
190b8b572e1SStephen Rothwell 
191b8b572e1SStephen Rothwell struct pci_dn {
192cca87d30SGavin Shan 	int     flags;
193a8b2f828SGavin Shan #define PCI_DN_FLAG_IOV_VF	0x01
194cca87d30SGavin Shan 
195b8b572e1SStephen Rothwell 	int	busno;			/* pci bus number */
196b8b572e1SStephen Rothwell 	int	devfn;			/* pci device and function number */
197c035ff1dSGavin Shan 	int	vendor_id;		/* Vendor ID */
198c035ff1dSGavin Shan 	int	device_id;		/* Device ID */
199c035ff1dSGavin Shan 	int	class_code;		/* Device class code */
200b8b572e1SStephen Rothwell 
201cca87d30SGavin Shan 	struct  pci_dn *parent;
202b8b572e1SStephen Rothwell 	struct  pci_controller *phb;	/* for pci devices */
203b348aa65SAlexey Kardashevskiy 	struct	iommu_table_group *table_group;	/* for phb's or bridges */
204b8b572e1SStephen Rothwell 	struct	device_node *node;	/* back-pointer to the device_node */
205b8b572e1SStephen Rothwell 
206b8b572e1SStephen Rothwell 	int	pci_ext_config_space;	/* for pci devices */
207b8b572e1SStephen Rothwell 
20894973b24SAlistair Popple 	struct	pci_dev *pcidev;	/* back-pointer to the pci device */
209184cd4a3SBenjamin Herrenschmidt #ifdef CONFIG_EEH
2102a0352faSGavin Shan 	struct eeh_dev *edev;		/* eeh device */
211b8b572e1SStephen Rothwell #endif
212184cd4a3SBenjamin Herrenschmidt #define IODA_INVALID_PE		(-1)
213184cd4a3SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
214184cd4a3SBenjamin Herrenschmidt 	int	pe_number;
2156e628c7dSWei Yang #ifdef CONFIG_PCI_IOV
2166e628c7dSWei Yang 	u16     vfs_expanded;		/* number of VFs IOV BAR expanded */
217781a868fSWei Yang 	u16     num_vfs;		/* number of VFs enabled*/
218781a868fSWei Yang 	int     offset;			/* PE# for the first VF PE */
219ee8222feSWei Yang 	bool    m64_single_mode;	/* Use M64 BAR in Single Mode */
220781a868fSWei Yang #define IODA_INVALID_M64        (-1)
221ee8222feSWei Yang 	int     (*m64_map)[PCI_SRIOV_NUM_BARS];
2226e628c7dSWei Yang #endif /* CONFIG_PCI_IOV */
223184cd4a3SBenjamin Herrenschmidt #endif
224cca87d30SGavin Shan 	struct list_head child_list;
225cca87d30SGavin Shan 	struct list_head list;
226b8b572e1SStephen Rothwell };
227b8b572e1SStephen Rothwell 
228b8b572e1SStephen Rothwell /* Get the pointer to a device_node's pci_dn */
229b8b572e1SStephen Rothwell #define PCI_DN(dn)	((struct pci_dn *) (dn)->data)
230b8b572e1SStephen Rothwell 
231cca87d30SGavin Shan extern struct pci_dn *pci_get_pdn_by_devfn(struct pci_bus *bus,
232cca87d30SGavin Shan 					   int devfn);
233b72c1f65SBenjamin Herrenschmidt extern struct pci_dn *pci_get_pdn(struct pci_dev *pdev);
234a8b2f828SGavin Shan extern struct pci_dn *add_dev_pci_data(struct pci_dev *pdev);
235a8b2f828SGavin Shan extern void remove_dev_pci_data(struct pci_dev *pdev);
2362eb4afb6SKumar Gala extern void *update_dn_pci_info(struct device_node *dn, void *data);
237b8b572e1SStephen Rothwell 
238b8b572e1SStephen Rothwell static inline int pci_device_from_OF_node(struct device_node *np,
239b8b572e1SStephen Rothwell 					  u8 *bus, u8 *devfn)
240b8b572e1SStephen Rothwell {
241b8b572e1SStephen Rothwell 	if (!PCI_DN(np))
242b8b572e1SStephen Rothwell 		return -ENODEV;
243b8b572e1SStephen Rothwell 	*bus = PCI_DN(np)->busno;
244b8b572e1SStephen Rothwell 	*devfn = PCI_DN(np)->devfn;
245b8b572e1SStephen Rothwell 	return 0;
246b8b572e1SStephen Rothwell }
247b8b572e1SStephen Rothwell 
2482a0352faSGavin Shan #if defined(CONFIG_EEH)
249e8e9b34cSGavin Shan static inline struct eeh_dev *pdn_to_eeh_dev(struct pci_dn *pdn)
250e8e9b34cSGavin Shan {
251e8e9b34cSGavin Shan 	return pdn ? pdn->edev : NULL;
252e8e9b34cSGavin Shan }
253f8f7d63fSGavin Shan #else
254e8e9b34cSGavin Shan #define pdn_to_eeh_dev(x)	(NULL)
2552a0352faSGavin Shan #endif
2562a0352faSGavin Shan 
257b8b572e1SStephen Rothwell /** Find the bus corresponding to the indicated device node */
258b8b572e1SStephen Rothwell extern struct pci_bus *pcibios_find_pci_bus(struct device_node *dn);
259b8b572e1SStephen Rothwell 
260b8b572e1SStephen Rothwell /** Remove all of the PCI devices under this bus */
261b8b572e1SStephen Rothwell extern void pcibios_remove_pci_devices(struct pci_bus *bus);
262b8b572e1SStephen Rothwell 
263b8b572e1SStephen Rothwell /** Discover new pci devices under this bus, and add them */
264b8b572e1SStephen Rothwell extern void pcibios_add_pci_devices(struct pci_bus *bus);
265b8b572e1SStephen Rothwell 
266b8b572e1SStephen Rothwell 
267b8b572e1SStephen Rothwell extern void isa_bridge_find_early(struct pci_controller *hose);
268b8b572e1SStephen Rothwell 
269b8b572e1SStephen Rothwell static inline int isa_vaddr_is_ioport(void __iomem *address)
270b8b572e1SStephen Rothwell {
271b8b572e1SStephen Rothwell 	/* Check if address hits the reserved legacy IO range */
272b8b572e1SStephen Rothwell 	unsigned long ea = (unsigned long)address;
273b8b572e1SStephen Rothwell 	return ea >= ISA_IO_BASE && ea < ISA_IO_END;
274b8b572e1SStephen Rothwell }
275b8b572e1SStephen Rothwell 
276b8b572e1SStephen Rothwell extern int pcibios_unmap_io_space(struct pci_bus *bus);
277b8b572e1SStephen Rothwell extern int pcibios_map_io_space(struct pci_bus *bus);
278b8b572e1SStephen Rothwell 
279b8b572e1SStephen Rothwell #ifdef CONFIG_NUMA
280b8b572e1SStephen Rothwell #define PHB_SET_NODE(PHB, NODE)		((PHB)->node = (NODE))
281b8b572e1SStephen Rothwell #else
282b8b572e1SStephen Rothwell #define PHB_SET_NODE(PHB, NODE)		((PHB)->node = -1)
283b8b572e1SStephen Rothwell #endif
284b8b572e1SStephen Rothwell 
285b8b572e1SStephen Rothwell #endif	/* CONFIG_PPC64 */
286b8b572e1SStephen Rothwell 
287b8b572e1SStephen Rothwell /* Get the PCI host controller for an OF device */
288b8b572e1SStephen Rothwell extern struct pci_controller *pci_find_hose_for_OF_device(
289b8b572e1SStephen Rothwell 			struct device_node* node);
290b8b572e1SStephen Rothwell 
291b8b572e1SStephen Rothwell /* Fill up host controller resources from the OF node */
292b8b572e1SStephen Rothwell extern void pci_process_bridge_OF_ranges(struct pci_controller *hose,
293b8b572e1SStephen Rothwell 			struct device_node *dev, int primary);
294b8b572e1SStephen Rothwell 
295b8b572e1SStephen Rothwell /* Allocate & free a PCI host bridge structure */
296b8b572e1SStephen Rothwell extern struct pci_controller *pcibios_alloc_controller(struct device_node *dev);
297b8b572e1SStephen Rothwell extern void pcibios_free_controller(struct pci_controller *phb);
298b8b572e1SStephen Rothwell 
299b8b572e1SStephen Rothwell #ifdef CONFIG_PCI
300b8b572e1SStephen Rothwell extern int pcibios_vaddr_is_ioport(void __iomem *address);
301b8b572e1SStephen Rothwell #else
302b8b572e1SStephen Rothwell static inline int pcibios_vaddr_is_ioport(void __iomem *address)
303b8b572e1SStephen Rothwell {
304b8b572e1SStephen Rothwell 	return 0;
305b8b572e1SStephen Rothwell }
306b8b572e1SStephen Rothwell #endif	/* CONFIG_PCI */
307b8b572e1SStephen Rothwell 
308b8b572e1SStephen Rothwell #endif	/* __KERNEL__ */
309b8b572e1SStephen Rothwell #endif	/* _ASM_POWERPC_PCI_BRIDGE_H */
310