2 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
4 * This is an 64bit optimized version that always keeps the full mmconfig
5 * space mapped. This allows lockless config space operation.
9 #include <linux/init.h>
10 #include <linux/acpi.h>
11 #include <linux/bitmap.h>
13 #include <asm/pci_x86.h>
15 #define PREFIX "PCI: "
17 static char __iomem
*pci_dev_base(unsigned int seg
, unsigned int bus
, unsigned int devfn
)
19 struct pci_mmcfg_region
*cfg
= pci_mmconfig_lookup(seg
, bus
);
22 return cfg
->virt
+ (PCI_MMCFG_BUS_OFFSET(bus
) | (devfn
<< 12));
26 static int pci_mmcfg_read(unsigned int seg
, unsigned int bus
,
27 unsigned int devfn
, int reg
, int len
, u32
*value
)
31 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
32 if (unlikely((bus
> 255) || (devfn
> 255) || (reg
> 4095))) {
37 addr
= pci_dev_base(seg
, bus
, devfn
);
43 *value
= mmio_config_readb(addr
+ reg
);
46 *value
= mmio_config_readw(addr
+ reg
);
49 *value
= mmio_config_readl(addr
+ reg
);
56 static int pci_mmcfg_write(unsigned int seg
, unsigned int bus
,
57 unsigned int devfn
, int reg
, int len
, u32 value
)
61 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
62 if (unlikely((bus
> 255) || (devfn
> 255) || (reg
> 4095)))
65 addr
= pci_dev_base(seg
, bus
, devfn
);
71 mmio_config_writeb(addr
+ reg
, value
);
74 mmio_config_writew(addr
+ reg
, value
);
77 mmio_config_writel(addr
+ reg
, value
);
84 static const struct pci_raw_ops pci_mmcfg
= {
85 .read
= pci_mmcfg_read
,
86 .write
= pci_mmcfg_write
,
89 static void __iomem
* __init
mcfg_ioremap(struct pci_mmcfg_region
*cfg
)
95 start
= cfg
->address
+ PCI_MMCFG_BUS_OFFSET(cfg
->start_bus
);
96 num_buses
= cfg
->end_bus
- cfg
->start_bus
+ 1;
97 size
= PCI_MMCFG_BUS_OFFSET(num_buses
);
98 addr
= ioremap_nocache(start
, size
);
100 addr
-= PCI_MMCFG_BUS_OFFSET(cfg
->start_bus
);
104 int __init
pci_mmcfg_arch_init(void)
106 struct pci_mmcfg_region
*cfg
;
108 list_for_each_entry(cfg
, &pci_mmcfg_list
, list
) {
109 cfg
->virt
= mcfg_ioremap(cfg
);
111 printk(KERN_ERR PREFIX
"can't map MMCONFIG at %pR\n",
113 pci_mmcfg_arch_free();
117 raw_pci_ext_ops
= &pci_mmcfg
;
121 void __init
pci_mmcfg_arch_free(void)
123 struct pci_mmcfg_region
*cfg
;
125 list_for_each_entry(cfg
, &pci_mmcfg_list
, list
) {
127 iounmap(cfg
->virt
+ PCI_MMCFG_BUS_OFFSET(cfg
->start_bus
));