162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci#ifndef _ASM_POWERPC_PCI_BRIDGE_H
362306a36Sopenharmony_ci#define _ASM_POWERPC_PCI_BRIDGE_H
462306a36Sopenharmony_ci#ifdef __KERNEL__
562306a36Sopenharmony_ci/*
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci#include <linux/pci.h>
862306a36Sopenharmony_ci#include <linux/list.h>
962306a36Sopenharmony_ci#include <linux/ioport.h>
1062306a36Sopenharmony_ci#include <linux/numa.h>
1162306a36Sopenharmony_ci#include <linux/iommu.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cistruct device_node;
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * PCI controller operations
1762306a36Sopenharmony_ci */
1862306a36Sopenharmony_cistruct pci_controller_ops {
1962306a36Sopenharmony_ci	void		(*dma_dev_setup)(struct pci_dev *pdev);
2062306a36Sopenharmony_ci	void		(*dma_bus_setup)(struct pci_bus *bus);
2162306a36Sopenharmony_ci	bool		(*iommu_bypass_supported)(struct pci_dev *pdev,
2262306a36Sopenharmony_ci				u64 mask);
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci	int		(*probe_mode)(struct pci_bus *bus);
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci	/* Called when pci_enable_device() is called. Returns true to
2762306a36Sopenharmony_ci	 * allow assignment/enabling of the device. */
2862306a36Sopenharmony_ci	bool		(*enable_device_hook)(struct pci_dev *pdev);
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci	void		(*disable_device)(struct pci_dev *pdev);
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	void		(*release_device)(struct pci_dev *pdev);
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	/* Called during PCI resource reassignment */
3562306a36Sopenharmony_ci	resource_size_t (*window_alignment)(struct pci_bus *bus,
3662306a36Sopenharmony_ci					    unsigned long type);
3762306a36Sopenharmony_ci	void		(*setup_bridge)(struct pci_bus *bus,
3862306a36Sopenharmony_ci					unsigned long type);
3962306a36Sopenharmony_ci	void		(*reset_secondary_bus)(struct pci_dev *pdev);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#ifdef CONFIG_PCI_MSI
4262306a36Sopenharmony_ci	int		(*setup_msi_irqs)(struct pci_dev *pdev,
4362306a36Sopenharmony_ci					  int nvec, int type);
4462306a36Sopenharmony_ci	void		(*teardown_msi_irqs)(struct pci_dev *pdev);
4562306a36Sopenharmony_ci#endif
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	void		(*shutdown)(struct pci_controller *hose);
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	struct iommu_group *(*device_group)(struct pci_controller *hose,
5062306a36Sopenharmony_ci					    struct pci_dev *pdev);
5162306a36Sopenharmony_ci};
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci/*
5462306a36Sopenharmony_ci * Structure of a PCI controller (host bridge)
5562306a36Sopenharmony_ci */
5662306a36Sopenharmony_cistruct pci_controller {
5762306a36Sopenharmony_ci	struct pci_bus *bus;
5862306a36Sopenharmony_ci	char is_dynamic;
5962306a36Sopenharmony_ci#ifdef CONFIG_PPC64
6062306a36Sopenharmony_ci	int node;
6162306a36Sopenharmony_ci#endif
6262306a36Sopenharmony_ci	struct device_node *dn;
6362306a36Sopenharmony_ci	struct list_head list_node;
6462306a36Sopenharmony_ci	struct device *parent;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	int first_busno;
6762306a36Sopenharmony_ci	int last_busno;
6862306a36Sopenharmony_ci	int self_busno;
6962306a36Sopenharmony_ci	struct resource busn;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	void __iomem *io_base_virt;
7262306a36Sopenharmony_ci#ifdef CONFIG_PPC64
7362306a36Sopenharmony_ci	void __iomem *io_base_alloc;
7462306a36Sopenharmony_ci#endif
7562306a36Sopenharmony_ci	resource_size_t io_base_phys;
7662306a36Sopenharmony_ci	resource_size_t pci_io_size;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	/* Some machines have a special region to forward the ISA
7962306a36Sopenharmony_ci	 * "memory" cycles such as VGA memory regions. Left to 0
8062306a36Sopenharmony_ci	 * if unsupported
8162306a36Sopenharmony_ci	 */
8262306a36Sopenharmony_ci	resource_size_t	isa_mem_phys;
8362306a36Sopenharmony_ci	resource_size_t	isa_mem_size;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	struct pci_controller_ops controller_ops;
8662306a36Sopenharmony_ci	struct pci_ops *ops;
8762306a36Sopenharmony_ci	unsigned int __iomem *cfg_addr;
8862306a36Sopenharmony_ci	void __iomem *cfg_data;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	/*
9162306a36Sopenharmony_ci	 * Used for variants of PCI indirect handling and possible quirks:
9262306a36Sopenharmony_ci	 *  SET_CFG_TYPE - used on 4xx or any PHB that does explicit type0/1
9362306a36Sopenharmony_ci	 *  EXT_REG - provides access to PCI-e extended registers
9462306a36Sopenharmony_ci	 *  SURPRESS_PRIMARY_BUS - we suppress the setting of PCI_PRIMARY_BUS
9562306a36Sopenharmony_ci	 *   on Freescale PCI-e controllers since they used the PCI_PRIMARY_BUS
9662306a36Sopenharmony_ci	 *   to determine which bus number to match on when generating type0
9762306a36Sopenharmony_ci	 *   config cycles
9862306a36Sopenharmony_ci	 *  NO_PCIE_LINK - the Freescale PCI-e controllers have issues with
9962306a36Sopenharmony_ci	 *   hanging if we don't have link and try to do config cycles to
10062306a36Sopenharmony_ci	 *   anything but the PHB.  Only allow talking to the PHB if this is
10162306a36Sopenharmony_ci	 *   set.
10262306a36Sopenharmony_ci	 *  BIG_ENDIAN - cfg_addr is a big endian register
10362306a36Sopenharmony_ci	 *  BROKEN_MRM - the 440EPx/GRx chips have an errata that causes hangs on
10462306a36Sopenharmony_ci	 *   the PLB4.  Effectively disable MRM commands by setting this.
10562306a36Sopenharmony_ci	 *  FSL_CFG_REG_LINK - Freescale controller version in which the PCIe
10662306a36Sopenharmony_ci	 *   link status is in a RC PCIe cfg register (vs being a SoC register)
10762306a36Sopenharmony_ci	 */
10862306a36Sopenharmony_ci#define PPC_INDIRECT_TYPE_SET_CFG_TYPE		0x00000001
10962306a36Sopenharmony_ci#define PPC_INDIRECT_TYPE_EXT_REG		0x00000002
11062306a36Sopenharmony_ci#define PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS	0x00000004
11162306a36Sopenharmony_ci#define PPC_INDIRECT_TYPE_NO_PCIE_LINK		0x00000008
11262306a36Sopenharmony_ci#define PPC_INDIRECT_TYPE_BIG_ENDIAN		0x00000010
11362306a36Sopenharmony_ci#define PPC_INDIRECT_TYPE_BROKEN_MRM		0x00000020
11462306a36Sopenharmony_ci#define PPC_INDIRECT_TYPE_FSL_CFG_REG_LINK	0x00000040
11562306a36Sopenharmony_ci	u32 indirect_type;
11662306a36Sopenharmony_ci	/* Currently, we limit ourselves to 1 IO range and 3 mem
11762306a36Sopenharmony_ci	 * ranges since the common pci_bus structure can't handle more
11862306a36Sopenharmony_ci	 */
11962306a36Sopenharmony_ci	struct resource	io_resource;
12062306a36Sopenharmony_ci	struct resource mem_resources[3];
12162306a36Sopenharmony_ci	resource_size_t mem_offset[3];
12262306a36Sopenharmony_ci	int global_number;		/* PCI domain number */
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	resource_size_t dma_window_base_cur;
12562306a36Sopenharmony_ci	resource_size_t dma_window_size;
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci#ifdef CONFIG_PPC64
12862306a36Sopenharmony_ci	unsigned long buid;
12962306a36Sopenharmony_ci	struct pci_dn *pci_data;
13062306a36Sopenharmony_ci#endif	/* CONFIG_PPC64 */
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	void *private_data;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	/* IRQ domain hierarchy */
13562306a36Sopenharmony_ci	struct irq_domain	*dev_domain;
13662306a36Sopenharmony_ci	struct irq_domain	*msi_domain;
13762306a36Sopenharmony_ci	struct fwnode_handle	*fwnode;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	/* iommu_ops support */
14062306a36Sopenharmony_ci	struct iommu_device	iommu;
14162306a36Sopenharmony_ci};
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci/* These are used for config access before all the PCI probing
14462306a36Sopenharmony_ci   has been done. */
14562306a36Sopenharmony_ciextern int early_read_config_byte(struct pci_controller *hose, int bus,
14662306a36Sopenharmony_ci			int dev_fn, int where, u8 *val);
14762306a36Sopenharmony_ciextern int early_read_config_word(struct pci_controller *hose, int bus,
14862306a36Sopenharmony_ci			int dev_fn, int where, u16 *val);
14962306a36Sopenharmony_ciextern int early_read_config_dword(struct pci_controller *hose, int bus,
15062306a36Sopenharmony_ci			int dev_fn, int where, u32 *val);
15162306a36Sopenharmony_ciextern int early_write_config_byte(struct pci_controller *hose, int bus,
15262306a36Sopenharmony_ci			int dev_fn, int where, u8 val);
15362306a36Sopenharmony_ciextern int early_write_config_word(struct pci_controller *hose, int bus,
15462306a36Sopenharmony_ci			int dev_fn, int where, u16 val);
15562306a36Sopenharmony_ciextern int early_write_config_dword(struct pci_controller *hose, int bus,
15662306a36Sopenharmony_ci			int dev_fn, int where, u32 val);
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ciextern int early_find_capability(struct pci_controller *hose, int bus,
15962306a36Sopenharmony_ci				 int dev_fn, int cap);
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ciextern void setup_indirect_pci(struct pci_controller* hose,
16262306a36Sopenharmony_ci			       resource_size_t cfg_addr,
16362306a36Sopenharmony_ci			       resource_size_t cfg_data, u32 flags);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciextern int indirect_read_config(struct pci_bus *bus, unsigned int devfn,
16662306a36Sopenharmony_ci				int offset, int len, u32 *val);
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ciextern int __indirect_read_config(struct pci_controller *hose,
16962306a36Sopenharmony_ci				  unsigned char bus_number, unsigned int devfn,
17062306a36Sopenharmony_ci				  int offset, int len, u32 *val);
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ciextern int indirect_write_config(struct pci_bus *bus, unsigned int devfn,
17362306a36Sopenharmony_ci				 int offset, int len, u32 val);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_cistatic inline struct pci_controller *pci_bus_to_host(const struct pci_bus *bus)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	return bus->sysdata;
17862306a36Sopenharmony_ci}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci#ifdef CONFIG_PPC_PMAC
18162306a36Sopenharmony_ciextern int pci_device_from_OF_node(struct device_node *node,
18262306a36Sopenharmony_ci				   u8 *bus, u8 *devfn);
18362306a36Sopenharmony_ci#endif
18462306a36Sopenharmony_ci#ifndef CONFIG_PPC64
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci#ifdef CONFIG_PPC_PCI_OF_BUS_MAP
18762306a36Sopenharmony_ciextern void pci_create_OF_bus_map(void);
18862306a36Sopenharmony_ci#else
18962306a36Sopenharmony_cistatic inline void pci_create_OF_bus_map(void) {}
19062306a36Sopenharmony_ci#endif
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci#else	/* CONFIG_PPC64 */
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci/*
19562306a36Sopenharmony_ci * PCI stuff, for nodes representing PCI devices, pointed to
19662306a36Sopenharmony_ci * by device_node->data.
19762306a36Sopenharmony_ci */
19862306a36Sopenharmony_cistruct iommu_table;
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_cistruct pci_dn {
20162306a36Sopenharmony_ci	int     flags;
20262306a36Sopenharmony_ci#define PCI_DN_FLAG_IOV_VF	0x01
20362306a36Sopenharmony_ci#define PCI_DN_FLAG_DEAD	0x02    /* Device has been hot-removed */
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	int	busno;			/* pci bus number */
20662306a36Sopenharmony_ci	int	devfn;			/* pci device and function number */
20762306a36Sopenharmony_ci	int	vendor_id;		/* Vendor ID */
20862306a36Sopenharmony_ci	int	device_id;		/* Device ID */
20962306a36Sopenharmony_ci	int	class_code;		/* Device class code */
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	struct  pci_dn *parent;
21262306a36Sopenharmony_ci	struct  pci_controller *phb;	/* for pci devices */
21362306a36Sopenharmony_ci	struct	iommu_table_group *table_group;	/* for phb's or bridges */
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	int	pci_ext_config_space;	/* for pci devices */
21662306a36Sopenharmony_ci#ifdef CONFIG_EEH
21762306a36Sopenharmony_ci	struct eeh_dev *edev;		/* eeh device */
21862306a36Sopenharmony_ci#endif
21962306a36Sopenharmony_ci#define IODA_INVALID_PE		0xFFFFFFFF
22062306a36Sopenharmony_ci	unsigned int pe_number;
22162306a36Sopenharmony_ci#ifdef CONFIG_PCI_IOV
22262306a36Sopenharmony_ci	u16     vfs_expanded;		/* number of VFs IOV BAR expanded */
22362306a36Sopenharmony_ci	u16     num_vfs;		/* number of VFs enabled*/
22462306a36Sopenharmony_ci	unsigned int *pe_num_map;	/* PE# for the first VF PE or array */
22562306a36Sopenharmony_ci	bool    m64_single_mode;	/* Use M64 BAR in Single Mode */
22662306a36Sopenharmony_ci#define IODA_INVALID_M64        (-1)
22762306a36Sopenharmony_ci	int     (*m64_map)[PCI_SRIOV_NUM_BARS];	/* Only used on powernv */
22862306a36Sopenharmony_ci	int     last_allow_rc;			/* Only used on pseries */
22962306a36Sopenharmony_ci#endif /* CONFIG_PCI_IOV */
23062306a36Sopenharmony_ci	int	mps;			/* Maximum Payload Size */
23162306a36Sopenharmony_ci	struct list_head child_list;
23262306a36Sopenharmony_ci	struct list_head list;
23362306a36Sopenharmony_ci	struct resource holes[PCI_SRIOV_NUM_BARS];
23462306a36Sopenharmony_ci};
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci/* Get the pointer to a device_node's pci_dn */
23762306a36Sopenharmony_ci#define PCI_DN(dn)	((struct pci_dn *) (dn)->data)
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ciextern struct pci_dn *pci_get_pdn_by_devfn(struct pci_bus *bus,
24062306a36Sopenharmony_ci					   int devfn);
24162306a36Sopenharmony_ciextern struct pci_dn *pci_get_pdn(struct pci_dev *pdev);
24262306a36Sopenharmony_ciextern struct pci_dn *pci_add_device_node_info(struct pci_controller *hose,
24362306a36Sopenharmony_ci					       struct device_node *dn);
24462306a36Sopenharmony_ciextern void pci_remove_device_node_info(struct device_node *dn);
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci#ifdef CONFIG_PCI_IOV
24762306a36Sopenharmony_cistruct pci_dn *add_sriov_vf_pdns(struct pci_dev *pdev);
24862306a36Sopenharmony_civoid remove_sriov_vf_pdns(struct pci_dev *pdev);
24962306a36Sopenharmony_ci#endif
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci#if defined(CONFIG_EEH)
25262306a36Sopenharmony_cistatic inline struct eeh_dev *pdn_to_eeh_dev(struct pci_dn *pdn)
25362306a36Sopenharmony_ci{
25462306a36Sopenharmony_ci	return pdn ? pdn->edev : NULL;
25562306a36Sopenharmony_ci}
25662306a36Sopenharmony_ci#else
25762306a36Sopenharmony_ci#define pdn_to_eeh_dev(x)	(NULL)
25862306a36Sopenharmony_ci#endif
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci/** Find the bus corresponding to the indicated device node */
26162306a36Sopenharmony_ciextern struct pci_bus *pci_find_bus_by_node(struct device_node *dn);
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci/** Remove all of the PCI devices under this bus */
26462306a36Sopenharmony_ciextern void pci_hp_remove_devices(struct pci_bus *bus);
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci/** Discover new pci devices under this bus, and add them */
26762306a36Sopenharmony_ciextern void pci_hp_add_devices(struct pci_bus *bus);
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ciextern int pcibios_unmap_io_space(struct pci_bus *bus);
27062306a36Sopenharmony_ciextern int pcibios_map_io_space(struct pci_bus *bus);
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci#ifdef CONFIG_NUMA
27362306a36Sopenharmony_ci#define PHB_SET_NODE(PHB, NODE)		((PHB)->node = (NODE))
27462306a36Sopenharmony_ci#else
27562306a36Sopenharmony_ci#define PHB_SET_NODE(PHB, NODE)		((PHB)->node = NUMA_NO_NODE)
27662306a36Sopenharmony_ci#endif
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci#endif	/* CONFIG_PPC64 */
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci/* Get the PCI host controller for an OF device */
28162306a36Sopenharmony_ciextern struct pci_controller *pci_find_hose_for_OF_device(
28262306a36Sopenharmony_ci			struct device_node* node);
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ciextern struct pci_controller *pci_find_controller_for_domain(int domain_nr);
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci/* Fill up host controller resources from the OF node */
28762306a36Sopenharmony_ciextern void pci_process_bridge_OF_ranges(struct pci_controller *hose,
28862306a36Sopenharmony_ci			struct device_node *dev, int primary);
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci/* Allocate & free a PCI host bridge structure */
29162306a36Sopenharmony_ciextern struct pci_controller *pcibios_alloc_controller(struct device_node *dev);
29262306a36Sopenharmony_ciextern void pcibios_free_controller(struct pci_controller *phb);
29362306a36Sopenharmony_ciextern void pcibios_free_controller_deferred(struct pci_host_bridge *bridge);
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci#ifdef CONFIG_PCI
29662306a36Sopenharmony_ciextern int pcibios_vaddr_is_ioport(void __iomem *address);
29762306a36Sopenharmony_ci#else
29862306a36Sopenharmony_cistatic inline int pcibios_vaddr_is_ioport(void __iomem *address)
29962306a36Sopenharmony_ci{
30062306a36Sopenharmony_ci	return 0;
30162306a36Sopenharmony_ci}
30262306a36Sopenharmony_ci#endif	/* CONFIG_PCI */
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci#endif	/* __KERNEL__ */
30562306a36Sopenharmony_ci#endif	/* _ASM_POWERPC_PCI_BRIDGE_H */
306