1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 282487711SJaswinder Singh Rajput /* 382487711SJaswinder Singh Rajput * Low-Level PCI Access for i386 machines. 482487711SJaswinder Singh Rajput * 582487711SJaswinder Singh Rajput * (c) 1999 Martin Mares <mj@ucw.cz> 682487711SJaswinder Singh Rajput */ 782487711SJaswinder Singh Rajput 8b9fae6a4SRandy Dunlap #include <linux/errno.h> 9b9fae6a4SRandy Dunlap #include <linux/init.h> 105520b7e7SIngo Molnar #include <linux/ioport.h> 11b9fae6a4SRandy Dunlap #include <linux/spinlock.h> 125520b7e7SIngo Molnar 1382487711SJaswinder Singh Rajput #undef DEBUG 1482487711SJaswinder Singh Rajput 1582487711SJaswinder Singh Rajput #ifdef DEBUG 16c767a54bSJoe Perches #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__) 1782487711SJaswinder Singh Rajput #else 18c767a54bSJoe Perches #define DBG(fmt, ...) \ 19c767a54bSJoe Perches do { \ 20c767a54bSJoe Perches if (0) \ 21c767a54bSJoe Perches printk(fmt, ##__VA_ARGS__); \ 22c767a54bSJoe Perches } while (0) 2382487711SJaswinder Singh Rajput #endif 2482487711SJaswinder Singh Rajput 2582487711SJaswinder Singh Rajput #define PCI_PROBE_BIOS 0x0001 2682487711SJaswinder Singh Rajput #define PCI_PROBE_CONF1 0x0002 2782487711SJaswinder Singh Rajput #define PCI_PROBE_CONF2 0x0004 2882487711SJaswinder Singh Rajput #define PCI_PROBE_MMCONF 0x0008 2982487711SJaswinder Singh Rajput #define PCI_PROBE_MASK 0x000f 3082487711SJaswinder Singh Rajput #define PCI_PROBE_NOEARLY 0x0010 3182487711SJaswinder Singh Rajput 3282487711SJaswinder Singh Rajput #define PCI_NO_CHECKS 0x0400 3382487711SJaswinder Singh Rajput #define PCI_USE_PIRQ_MASK 0x0800 3482487711SJaswinder Singh Rajput #define PCI_ASSIGN_ROMS 0x1000 3582487711SJaswinder Singh Rajput #define PCI_BIOS_IRQ_SCAN 0x2000 3682487711SJaswinder Singh Rajput #define PCI_ASSIGN_ALL_BUSSES 0x4000 3782487711SJaswinder Singh Rajput #define PCI_CAN_SKIP_ISA_ALIGN 0x8000 38236e946bSLinus Torvalds #define PCI_USE__CRS 0x10000 3982487711SJaswinder Singh Rajput #define PCI_CHECK_ENABLE_AMD_MMCONF 0x20000 4082487711SJaswinder Singh Rajput #define PCI_HAS_IO_ECS 0x40000 4182487711SJaswinder Singh Rajput #define PCI_NOASSIGN_ROMS 0x80000 427bc5e3f2SBjorn Helgaas #define PCI_ROOT_NO_CRS 0x100000 437bd1c365SMike Habeck #define PCI_NOASSIGN_BARS 0x200000 44f32ab754S=?UTF-8?q?Christian=20K=C3=B6nig?= #define PCI_BIG_ROOT_WINDOW 0x400000 45fa6dae5dSHans de Goede #define PCI_USE_E820 0x800000 46fa6dae5dSHans de Goede #define PCI_NO_E820 0x1000000 4782487711SJaswinder Singh Rajput 4882487711SJaswinder Singh Rajput extern unsigned int pci_probe; 4982487711SJaswinder Singh Rajput extern unsigned long pirq_table_addr; 5082487711SJaswinder Singh Rajput 5182487711SJaswinder Singh Rajput enum pci_bf_sort_state { 5282487711SJaswinder Singh Rajput pci_bf_sort_default, 5382487711SJaswinder Singh Rajput pci_force_nobf, 5482487711SJaswinder Singh Rajput pci_force_bf, 5582487711SJaswinder Singh Rajput pci_dmi_bf, 5682487711SJaswinder Singh Rajput }; 5782487711SJaswinder Singh Rajput 5882487711SJaswinder Singh Rajput /* pci-i386.c */ 5982487711SJaswinder Singh Rajput 6082487711SJaswinder Singh Rajput void pcibios_resource_survey(void); 6144de3395SAlex Nixon void pcibios_set_cache_line_size(void); 6282487711SJaswinder Singh Rajput 6382487711SJaswinder Singh Rajput /* pci-pc.c */ 6482487711SJaswinder Singh Rajput 6582487711SJaswinder Singh Rajput extern int pcibios_last_bus; 6682487711SJaswinder Singh Rajput extern struct pci_ops pci_root_ops; 6782487711SJaswinder Singh Rajput 685707b24aSAristeu Rozanski void pcibios_scan_specific_bus(int busn); 695707b24aSAristeu Rozanski 7082487711SJaswinder Singh Rajput /* pci-irq.c */ 7182487711SJaswinder Singh Rajput 72*a2b36ffbSHans de Goede struct pci_dev; 73*a2b36ffbSHans de Goede 7482487711SJaswinder Singh Rajput struct irq_info { 7582487711SJaswinder Singh Rajput u8 bus, devfn; /* Bus, device and function */ 7682487711SJaswinder Singh Rajput struct { 7782487711SJaswinder Singh Rajput u8 link; /* IRQ line ID, chipset dependent, 7882487711SJaswinder Singh Rajput 0 = not routed */ 7982487711SJaswinder Singh Rajput u16 bitmap; /* Available IRQs */ 8082487711SJaswinder Singh Rajput } __attribute__((packed)) irq[4]; 8182487711SJaswinder Singh Rajput u8 slot; /* Slot number, 0=onboard */ 8282487711SJaswinder Singh Rajput u8 rfu; 8382487711SJaswinder Singh Rajput } __attribute__((packed)); 8482487711SJaswinder Singh Rajput 8582487711SJaswinder Singh Rajput struct irq_routing_table { 8682487711SJaswinder Singh Rajput u32 signature; /* PIRQ_SIGNATURE should be here */ 8782487711SJaswinder Singh Rajput u16 version; /* PIRQ_VERSION */ 8882487711SJaswinder Singh Rajput u16 size; /* Table size in bytes */ 8982487711SJaswinder Singh Rajput u8 rtr_bus, rtr_devfn; /* Where the interrupt router lies */ 9082487711SJaswinder Singh Rajput u16 exclusive_irqs; /* IRQs devoted exclusively to 9182487711SJaswinder Singh Rajput PCI usage */ 9282487711SJaswinder Singh Rajput u16 rtr_vendor, rtr_device; /* Vendor and device ID of 9382487711SJaswinder Singh Rajput interrupt router */ 9482487711SJaswinder Singh Rajput u32 miniport_data; /* Crap */ 9582487711SJaswinder Singh Rajput u8 rfu[11]; 9682487711SJaswinder Singh Rajput u8 checksum; /* Modulo 256 checksum must give 0 */ 975224f790SGustavo A. R. Silva struct irq_info slots[]; 9882487711SJaswinder Singh Rajput } __attribute__((packed)); 9982487711SJaswinder Singh Rajput 100b584db0cSMaciej W. Rozycki struct irt_routing_table { 101b584db0cSMaciej W. Rozycki u32 signature; /* IRT_SIGNATURE should be here */ 102b584db0cSMaciej W. Rozycki u8 size; /* Number of entries provided */ 103b584db0cSMaciej W. Rozycki u8 used; /* Number of entries actually used */ 104b584db0cSMaciej W. Rozycki u16 exclusive_irqs; /* IRQs devoted exclusively to 105b584db0cSMaciej W. Rozycki PCI usage */ 106b584db0cSMaciej W. Rozycki struct irq_info slots[]; 107b584db0cSMaciej W. Rozycki } __attribute__((packed)); 108b584db0cSMaciej W. Rozycki 10982487711SJaswinder Singh Rajput extern unsigned int pcibios_irq_mask; 11082487711SJaswinder Singh Rajput 111d19f61f0SThomas Gleixner extern raw_spinlock_t pci_config_lock; 11282487711SJaswinder Singh Rajput 11382487711SJaswinder Singh Rajput extern int (*pcibios_enable_irq)(struct pci_dev *dev); 11482487711SJaswinder Singh Rajput extern void (*pcibios_disable_irq)(struct pci_dev *dev); 11582487711SJaswinder Singh Rajput 1166c777e87SBjorn Helgaas extern bool mp_should_keep_irq(struct device *dev); 1176c777e87SBjorn Helgaas 11882487711SJaswinder Singh Rajput struct pci_raw_ops { 11982487711SJaswinder Singh Rajput int (*read)(unsigned int domain, unsigned int bus, unsigned int devfn, 12082487711SJaswinder Singh Rajput int reg, int len, u32 *val); 12182487711SJaswinder Singh Rajput int (*write)(unsigned int domain, unsigned int bus, unsigned int devfn, 12282487711SJaswinder Singh Rajput int reg, int len, u32 val); 12382487711SJaswinder Singh Rajput }; 12482487711SJaswinder Singh Rajput 12572da0b07SJan Beulich extern const struct pci_raw_ops *raw_pci_ops; 12672da0b07SJan Beulich extern const struct pci_raw_ops *raw_pci_ext_ops; 12782487711SJaswinder Singh Rajput 128c0fa4078SJiang Liu extern const struct pci_raw_ops pci_mmcfg; 12972da0b07SJan Beulich extern const struct pci_raw_ops pci_direct_conf1; 13082487711SJaswinder Singh Rajput extern bool port_cf9_safe; 13182487711SJaswinder Singh Rajput 13282487711SJaswinder Singh Rajput /* arch_initcall level */ 133445d3595SThomas Gleixner #ifdef CONFIG_PCI_DIRECT 13482487711SJaswinder Singh Rajput extern int pci_direct_probe(void); 13582487711SJaswinder Singh Rajput extern void pci_direct_init(int type); 136445d3595SThomas Gleixner #else 137445d3595SThomas Gleixner static inline int pci_direct_probe(void) { return -1; } 138445d3595SThomas Gleixner static inline void pci_direct_init(int type) { } 139445d3595SThomas Gleixner #endif 140445d3595SThomas Gleixner 141445d3595SThomas Gleixner #ifdef CONFIG_PCI_BIOS 14282487711SJaswinder Singh Rajput extern void pci_pcbios_init(void); 143445d3595SThomas Gleixner #else 144445d3595SThomas Gleixner static inline void pci_pcbios_init(void) { } 145445d3595SThomas Gleixner #endif 146445d3595SThomas Gleixner 14782487711SJaswinder Singh Rajput extern void __init dmi_check_pciprobe(void); 14882487711SJaswinder Singh Rajput extern void __init dmi_check_skip_isa_align(void); 14982487711SJaswinder Singh Rajput 15082487711SJaswinder Singh Rajput /* some common used subsys_initcalls */ 1515d32a665SSinan Kaya #ifdef CONFIG_PCI 15282487711SJaswinder Singh Rajput extern int __init pci_acpi_init(void); 1535d32a665SSinan Kaya #else 1545d32a665SSinan Kaya static inline int __init pci_acpi_init(void) 1555d32a665SSinan Kaya { 1565d32a665SSinan Kaya return -EINVAL; 1575d32a665SSinan Kaya } 1585d32a665SSinan Kaya #endif 159ab3b3793SThomas Gleixner extern void __init pcibios_irq_init(void); 16082487711SJaswinder Singh Rajput extern int __init pcibios_init(void); 161b72d0db9SThomas Gleixner extern int pci_legacy_init(void); 1629325a28cSThomas Gleixner extern void pcibios_fixup_irqs(void); 16382487711SJaswinder Singh Rajput 16482487711SJaswinder Singh Rajput /* pci-mmconfig.c */ 16582487711SJaswinder Singh Rajput 16656ddf4d3SBjorn Helgaas /* "PCI MMCONFIG %04x [bus %02x-%02x]" */ 16756ddf4d3SBjorn Helgaas #define PCI_MMCFG_RESOURCE_NAME_LEN (22 + 4 + 2 + 2) 16856ddf4d3SBjorn Helgaas 169d215a9c8SBjorn Helgaas struct pci_mmcfg_region { 170ff097dddSBjorn Helgaas struct list_head list; 17156ddf4d3SBjorn Helgaas struct resource res; 172d215a9c8SBjorn Helgaas u64 address; 1733f0f5503SBjorn Helgaas char __iomem *virt; 174d7e6b66fSBjorn Helgaas u16 segment; 175d7e6b66fSBjorn Helgaas u8 start_bus; 176d7e6b66fSBjorn Helgaas u8 end_bus; 17756ddf4d3SBjorn Helgaas char name[PCI_MMCFG_RESOURCE_NAME_LEN]; 178d215a9c8SBjorn Helgaas }; 179d215a9c8SBjorn Helgaas 18082487711SJaswinder Singh Rajput extern int __init pci_mmcfg_arch_init(void); 18182487711SJaswinder Singh Rajput extern void __init pci_mmcfg_arch_free(void); 182a18e3690SGreg Kroah-Hartman extern int pci_mmcfg_arch_map(struct pci_mmcfg_region *cfg); 1839cf0105dSJiang Liu extern void pci_mmcfg_arch_unmap(struct pci_mmcfg_region *cfg); 184a18e3690SGreg Kroah-Hartman extern int pci_mmconfig_insert(struct device *dev, u16 seg, u8 start, u8 end, 185a18e3690SGreg Kroah-Hartman phys_addr_t addr); 1869c95111bSJiang Liu extern int pci_mmconfig_delete(u16 seg, u8 start, u8 end); 187f6e1d8ccSBjorn Helgaas extern struct pci_mmcfg_region *pci_mmconfig_lookup(int segment, int bus); 1886fa4a94eSOtavio Pontes extern struct pci_mmcfg_region *__init pci_mmconfig_add(int segment, int start, 1896fa4a94eSOtavio Pontes int end, u64 addr); 19082487711SJaswinder Singh Rajput 191ff097dddSBjorn Helgaas extern struct list_head pci_mmcfg_list; 192c4bf2f37SLen Brown 193df5eb1d6SBjorn Helgaas #define PCI_MMCFG_BUS_OFFSET(bus) ((bus) << 20) 194df5eb1d6SBjorn Helgaas 19582487711SJaswinder Singh Rajput /* 19621461775STomasz Nowicki * On AMD Fam10h CPUs, all PCI MMIO configuration space accesses must use 19721461775STomasz Nowicki * %eax. No other source or target registers may be used. The following 19821461775STomasz Nowicki * mmio_config_* accessors enforce this. See "BIOS and Kernel Developer's 19921461775STomasz Nowicki * Guide (BKDG) For AMD Family 10h Processors", rev. 3.48, sec 2.11.1, 20021461775STomasz Nowicki * "MMIO Configuration Coding Requirements". 20182487711SJaswinder Singh Rajput */ 20282487711SJaswinder Singh Rajput static inline unsigned char mmio_config_readb(void __iomem *pos) 20382487711SJaswinder Singh Rajput { 20482487711SJaswinder Singh Rajput u8 val; 20582487711SJaswinder Singh Rajput asm volatile("movb (%1),%%al" : "=a" (val) : "r" (pos)); 20682487711SJaswinder Singh Rajput return val; 20782487711SJaswinder Singh Rajput } 20882487711SJaswinder Singh Rajput 20982487711SJaswinder Singh Rajput static inline unsigned short mmio_config_readw(void __iomem *pos) 21082487711SJaswinder Singh Rajput { 21182487711SJaswinder Singh Rajput u16 val; 21282487711SJaswinder Singh Rajput asm volatile("movw (%1),%%ax" : "=a" (val) : "r" (pos)); 21382487711SJaswinder Singh Rajput return val; 21482487711SJaswinder Singh Rajput } 21582487711SJaswinder Singh Rajput 21682487711SJaswinder Singh Rajput static inline unsigned int mmio_config_readl(void __iomem *pos) 21782487711SJaswinder Singh Rajput { 21882487711SJaswinder Singh Rajput u32 val; 21982487711SJaswinder Singh Rajput asm volatile("movl (%1),%%eax" : "=a" (val) : "r" (pos)); 22082487711SJaswinder Singh Rajput return val; 22182487711SJaswinder Singh Rajput } 22282487711SJaswinder Singh Rajput 22382487711SJaswinder Singh Rajput static inline void mmio_config_writeb(void __iomem *pos, u8 val) 22482487711SJaswinder Singh Rajput { 22582487711SJaswinder Singh Rajput asm volatile("movb %%al,(%1)" : : "a" (val), "r" (pos) : "memory"); 22682487711SJaswinder Singh Rajput } 22782487711SJaswinder Singh Rajput 22882487711SJaswinder Singh Rajput static inline void mmio_config_writew(void __iomem *pos, u16 val) 22982487711SJaswinder Singh Rajput { 23082487711SJaswinder Singh Rajput asm volatile("movw %%ax,(%1)" : : "a" (val), "r" (pos) : "memory"); 23182487711SJaswinder Singh Rajput } 23282487711SJaswinder Singh Rajput 23382487711SJaswinder Singh Rajput static inline void mmio_config_writel(void __iomem *pos, u32 val) 23482487711SJaswinder Singh Rajput { 23582487711SJaswinder Singh Rajput asm volatile("movl %%eax,(%1)" : : "a" (val), "r" (pos) : "memory"); 23682487711SJaswinder Singh Rajput } 237b72d0db9SThomas Gleixner 238b72d0db9SThomas Gleixner #ifdef CONFIG_PCI 239b72d0db9SThomas Gleixner # ifdef CONFIG_ACPI 240b72d0db9SThomas Gleixner # define x86_default_pci_init pci_acpi_init 241b72d0db9SThomas Gleixner # else 242b72d0db9SThomas Gleixner # define x86_default_pci_init pci_legacy_init 243b72d0db9SThomas Gleixner # endif 244ab3b3793SThomas Gleixner # define x86_default_pci_init_irq pcibios_irq_init 2459325a28cSThomas Gleixner # define x86_default_pci_fixup_irqs pcibios_fixup_irqs 246b72d0db9SThomas Gleixner #else 247b72d0db9SThomas Gleixner # define x86_default_pci_init NULL 248ab3b3793SThomas Gleixner # define x86_default_pci_init_irq NULL 2499325a28cSThomas Gleixner # define x86_default_pci_fixup_irqs NULL 250b72d0db9SThomas Gleixner #endif 251*a2b36ffbSHans de Goede 252*a2b36ffbSHans de Goede #if defined(CONFIG_PCI) && defined(CONFIG_ACPI) 253*a2b36ffbSHans de Goede extern bool pci_use_e820; 254*a2b36ffbSHans de Goede #else 255*a2b36ffbSHans de Goede #define pci_use_e820 false 256*a2b36ffbSHans de Goede #endif 257