18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	Low-Level PCI Access for i386 machines.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *	(c) 1999 Martin Mares <mj@ucw.cz>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/ioport.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#undef DEBUG
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#ifdef DEBUG
138c2ecf20Sopenharmony_ci#define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
148c2ecf20Sopenharmony_ci#else
158c2ecf20Sopenharmony_ci#define DBG(fmt, ...)				\
168c2ecf20Sopenharmony_cido {						\
178c2ecf20Sopenharmony_ci	if (0)					\
188c2ecf20Sopenharmony_ci		printk(fmt, ##__VA_ARGS__);	\
198c2ecf20Sopenharmony_ci} while (0)
208c2ecf20Sopenharmony_ci#endif
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define PCI_PROBE_BIOS		0x0001
238c2ecf20Sopenharmony_ci#define PCI_PROBE_CONF1		0x0002
248c2ecf20Sopenharmony_ci#define PCI_PROBE_CONF2		0x0004
258c2ecf20Sopenharmony_ci#define PCI_PROBE_MMCONF	0x0008
268c2ecf20Sopenharmony_ci#define PCI_PROBE_MASK		0x000f
278c2ecf20Sopenharmony_ci#define PCI_PROBE_NOEARLY	0x0010
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define PCI_NO_CHECKS		0x0400
308c2ecf20Sopenharmony_ci#define PCI_USE_PIRQ_MASK	0x0800
318c2ecf20Sopenharmony_ci#define PCI_ASSIGN_ROMS		0x1000
328c2ecf20Sopenharmony_ci#define PCI_BIOS_IRQ_SCAN	0x2000
338c2ecf20Sopenharmony_ci#define PCI_ASSIGN_ALL_BUSSES	0x4000
348c2ecf20Sopenharmony_ci#define PCI_CAN_SKIP_ISA_ALIGN	0x8000
358c2ecf20Sopenharmony_ci#define PCI_USE__CRS		0x10000
368c2ecf20Sopenharmony_ci#define PCI_CHECK_ENABLE_AMD_MMCONF	0x20000
378c2ecf20Sopenharmony_ci#define PCI_HAS_IO_ECS		0x40000
388c2ecf20Sopenharmony_ci#define PCI_NOASSIGN_ROMS	0x80000
398c2ecf20Sopenharmony_ci#define PCI_ROOT_NO_CRS		0x100000
408c2ecf20Sopenharmony_ci#define PCI_NOASSIGN_BARS	0x200000
418c2ecf20Sopenharmony_ci#define PCI_BIG_ROOT_WINDOW	0x400000
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciextern unsigned int pci_probe;
448c2ecf20Sopenharmony_ciextern unsigned long pirq_table_addr;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cienum pci_bf_sort_state {
478c2ecf20Sopenharmony_ci	pci_bf_sort_default,
488c2ecf20Sopenharmony_ci	pci_force_nobf,
498c2ecf20Sopenharmony_ci	pci_force_bf,
508c2ecf20Sopenharmony_ci	pci_dmi_bf,
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* pci-i386.c */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_civoid pcibios_resource_survey(void);
568c2ecf20Sopenharmony_civoid pcibios_set_cache_line_size(void);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* pci-pc.c */
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciextern int pcibios_last_bus;
618c2ecf20Sopenharmony_ciextern struct pci_ops pci_root_ops;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_civoid pcibios_scan_specific_bus(int busn);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* pci-irq.c */
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistruct irq_info {
688c2ecf20Sopenharmony_ci	u8 bus, devfn;			/* Bus, device and function */
698c2ecf20Sopenharmony_ci	struct {
708c2ecf20Sopenharmony_ci		u8 link;		/* IRQ line ID, chipset dependent,
718c2ecf20Sopenharmony_ci					   0 = not routed */
728c2ecf20Sopenharmony_ci		u16 bitmap;		/* Available IRQs */
738c2ecf20Sopenharmony_ci	} __attribute__((packed)) irq[4];
748c2ecf20Sopenharmony_ci	u8 slot;			/* Slot number, 0=onboard */
758c2ecf20Sopenharmony_ci	u8 rfu;
768c2ecf20Sopenharmony_ci} __attribute__((packed));
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistruct irq_routing_table {
798c2ecf20Sopenharmony_ci	u32 signature;			/* PIRQ_SIGNATURE should be here */
808c2ecf20Sopenharmony_ci	u16 version;			/* PIRQ_VERSION */
818c2ecf20Sopenharmony_ci	u16 size;			/* Table size in bytes */
828c2ecf20Sopenharmony_ci	u8 rtr_bus, rtr_devfn;		/* Where the interrupt router lies */
838c2ecf20Sopenharmony_ci	u16 exclusive_irqs;		/* IRQs devoted exclusively to
848c2ecf20Sopenharmony_ci					   PCI usage */
858c2ecf20Sopenharmony_ci	u16 rtr_vendor, rtr_device;	/* Vendor and device ID of
868c2ecf20Sopenharmony_ci					   interrupt router */
878c2ecf20Sopenharmony_ci	u32 miniport_data;		/* Crap */
888c2ecf20Sopenharmony_ci	u8 rfu[11];
898c2ecf20Sopenharmony_ci	u8 checksum;			/* Modulo 256 checksum must give 0 */
908c2ecf20Sopenharmony_ci	struct irq_info slots[0];
918c2ecf20Sopenharmony_ci} __attribute__((packed));
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciextern unsigned int pcibios_irq_mask;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ciextern raw_spinlock_t pci_config_lock;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ciextern int (*pcibios_enable_irq)(struct pci_dev *dev);
988c2ecf20Sopenharmony_ciextern void (*pcibios_disable_irq)(struct pci_dev *dev);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ciextern bool mp_should_keep_irq(struct device *dev);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistruct pci_raw_ops {
1038c2ecf20Sopenharmony_ci	int (*read)(unsigned int domain, unsigned int bus, unsigned int devfn,
1048c2ecf20Sopenharmony_ci						int reg, int len, u32 *val);
1058c2ecf20Sopenharmony_ci	int (*write)(unsigned int domain, unsigned int bus, unsigned int devfn,
1068c2ecf20Sopenharmony_ci						int reg, int len, u32 val);
1078c2ecf20Sopenharmony_ci};
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciextern const struct pci_raw_ops *raw_pci_ops;
1108c2ecf20Sopenharmony_ciextern const struct pci_raw_ops *raw_pci_ext_ops;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ciextern const struct pci_raw_ops pci_mmcfg;
1138c2ecf20Sopenharmony_ciextern const struct pci_raw_ops pci_direct_conf1;
1148c2ecf20Sopenharmony_ciextern bool port_cf9_safe;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/* arch_initcall level */
1178c2ecf20Sopenharmony_ci#ifdef CONFIG_PCI_DIRECT
1188c2ecf20Sopenharmony_ciextern int pci_direct_probe(void);
1198c2ecf20Sopenharmony_ciextern void pci_direct_init(int type);
1208c2ecf20Sopenharmony_ci#else
1218c2ecf20Sopenharmony_cistatic inline int pci_direct_probe(void) { return -1; }
1228c2ecf20Sopenharmony_cistatic inline  void pci_direct_init(int type) { }
1238c2ecf20Sopenharmony_ci#endif
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#ifdef CONFIG_PCI_BIOS
1268c2ecf20Sopenharmony_ciextern void pci_pcbios_init(void);
1278c2ecf20Sopenharmony_ci#else
1288c2ecf20Sopenharmony_cistatic inline void pci_pcbios_init(void) { }
1298c2ecf20Sopenharmony_ci#endif
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ciextern void __init dmi_check_pciprobe(void);
1328c2ecf20Sopenharmony_ciextern void __init dmi_check_skip_isa_align(void);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/* some common used subsys_initcalls */
1358c2ecf20Sopenharmony_ci#ifdef CONFIG_PCI
1368c2ecf20Sopenharmony_ciextern int __init pci_acpi_init(void);
1378c2ecf20Sopenharmony_ci#else
1388c2ecf20Sopenharmony_cistatic inline int  __init pci_acpi_init(void)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	return -EINVAL;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci#endif
1438c2ecf20Sopenharmony_ciextern void __init pcibios_irq_init(void);
1448c2ecf20Sopenharmony_ciextern int __init pcibios_init(void);
1458c2ecf20Sopenharmony_ciextern int pci_legacy_init(void);
1468c2ecf20Sopenharmony_ciextern void pcibios_fixup_irqs(void);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/* pci-mmconfig.c */
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/* "PCI MMCONFIG %04x [bus %02x-%02x]" */
1518c2ecf20Sopenharmony_ci#define PCI_MMCFG_RESOURCE_NAME_LEN (22 + 4 + 2 + 2)
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistruct pci_mmcfg_region {
1548c2ecf20Sopenharmony_ci	struct list_head list;
1558c2ecf20Sopenharmony_ci	struct resource res;
1568c2ecf20Sopenharmony_ci	u64 address;
1578c2ecf20Sopenharmony_ci	char __iomem *virt;
1588c2ecf20Sopenharmony_ci	u16 segment;
1598c2ecf20Sopenharmony_ci	u8 start_bus;
1608c2ecf20Sopenharmony_ci	u8 end_bus;
1618c2ecf20Sopenharmony_ci	char name[PCI_MMCFG_RESOURCE_NAME_LEN];
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ciextern int __init pci_mmcfg_arch_init(void);
1658c2ecf20Sopenharmony_ciextern void __init pci_mmcfg_arch_free(void);
1668c2ecf20Sopenharmony_ciextern int pci_mmcfg_arch_map(struct pci_mmcfg_region *cfg);
1678c2ecf20Sopenharmony_ciextern void pci_mmcfg_arch_unmap(struct pci_mmcfg_region *cfg);
1688c2ecf20Sopenharmony_ciextern int pci_mmconfig_insert(struct device *dev, u16 seg, u8 start, u8 end,
1698c2ecf20Sopenharmony_ci			       phys_addr_t addr);
1708c2ecf20Sopenharmony_ciextern int pci_mmconfig_delete(u16 seg, u8 start, u8 end);
1718c2ecf20Sopenharmony_ciextern struct pci_mmcfg_region *pci_mmconfig_lookup(int segment, int bus);
1728c2ecf20Sopenharmony_ciextern struct pci_mmcfg_region *__init pci_mmconfig_add(int segment, int start,
1738c2ecf20Sopenharmony_ci							int end, u64 addr);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ciextern struct list_head pci_mmcfg_list;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci#define PCI_MMCFG_BUS_OFFSET(bus)      ((bus) << 20)
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/*
1808c2ecf20Sopenharmony_ci * On AMD Fam10h CPUs, all PCI MMIO configuration space accesses must use
1818c2ecf20Sopenharmony_ci * %eax.  No other source or target registers may be used.  The following
1828c2ecf20Sopenharmony_ci * mmio_config_* accessors enforce this.  See "BIOS and Kernel Developer's
1838c2ecf20Sopenharmony_ci * Guide (BKDG) For AMD Family 10h Processors", rev. 3.48, sec 2.11.1,
1848c2ecf20Sopenharmony_ci * "MMIO Configuration Coding Requirements".
1858c2ecf20Sopenharmony_ci */
1868c2ecf20Sopenharmony_cistatic inline unsigned char mmio_config_readb(void __iomem *pos)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	u8 val;
1898c2ecf20Sopenharmony_ci	asm volatile("movb (%1),%%al" : "=a" (val) : "r" (pos));
1908c2ecf20Sopenharmony_ci	return val;
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic inline unsigned short mmio_config_readw(void __iomem *pos)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	u16 val;
1968c2ecf20Sopenharmony_ci	asm volatile("movw (%1),%%ax" : "=a" (val) : "r" (pos));
1978c2ecf20Sopenharmony_ci	return val;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic inline unsigned int mmio_config_readl(void __iomem *pos)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	u32 val;
2038c2ecf20Sopenharmony_ci	asm volatile("movl (%1),%%eax" : "=a" (val) : "r" (pos));
2048c2ecf20Sopenharmony_ci	return val;
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic inline void mmio_config_writeb(void __iomem *pos, u8 val)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	asm volatile("movb %%al,(%1)" : : "a" (val), "r" (pos) : "memory");
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic inline void mmio_config_writew(void __iomem *pos, u16 val)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	asm volatile("movw %%ax,(%1)" : : "a" (val), "r" (pos) : "memory");
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic inline void mmio_config_writel(void __iomem *pos, u32 val)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	asm volatile("movl %%eax,(%1)" : : "a" (val), "r" (pos) : "memory");
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci#ifdef CONFIG_PCI
2238c2ecf20Sopenharmony_ci# ifdef CONFIG_ACPI
2248c2ecf20Sopenharmony_ci#  define x86_default_pci_init		pci_acpi_init
2258c2ecf20Sopenharmony_ci# else
2268c2ecf20Sopenharmony_ci#  define x86_default_pci_init		pci_legacy_init
2278c2ecf20Sopenharmony_ci# endif
2288c2ecf20Sopenharmony_ci# define x86_default_pci_init_irq	pcibios_irq_init
2298c2ecf20Sopenharmony_ci# define x86_default_pci_fixup_irqs	pcibios_fixup_irqs
2308c2ecf20Sopenharmony_ci#else
2318c2ecf20Sopenharmony_ci# define x86_default_pci_init		NULL
2328c2ecf20Sopenharmony_ci# define x86_default_pci_init_irq	NULL
2338c2ecf20Sopenharmony_ci# define x86_default_pci_fixup_irqs	NULL
2348c2ecf20Sopenharmony_ci#endif
235