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 static char __iomem
*get_virt(unsigned int seg
, unsigned bus
)
17 struct pci_mmcfg_region
*cfg
;
19 list_for_each_entry(cfg
, &pci_mmcfg_list
, list
)
20 if (cfg
->segment
== seg
&&
21 (cfg
->start_bus
<= bus
) &&
22 (cfg
->end_bus
>= bus
))
25 /* Fall back to type 0 */
29 static char __iomem
*pci_dev_base(unsigned int seg
, unsigned int bus
, unsigned int devfn
)
33 addr
= get_virt(seg
, bus
);
36 return addr
+ (PCI_MMCFG_BUS_OFFSET(bus
) | (devfn
<< 12));
39 static int pci_mmcfg_read(unsigned int seg
, unsigned int bus
,
40 unsigned int devfn
, int reg
, int len
, u32
*value
)
44 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
45 if (unlikely((bus
> 255) || (devfn
> 255) || (reg
> 4095))) {
50 addr
= pci_dev_base(seg
, bus
, devfn
);
56 *value
= mmio_config_readb(addr
+ reg
);
59 *value
= mmio_config_readw(addr
+ reg
);
62 *value
= mmio_config_readl(addr
+ reg
);
69 static int pci_mmcfg_write(unsigned int seg
, unsigned int bus
,
70 unsigned int devfn
, int reg
, int len
, u32 value
)
74 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
75 if (unlikely((bus
> 255) || (devfn
> 255) || (reg
> 4095)))
78 addr
= pci_dev_base(seg
, bus
, devfn
);
84 mmio_config_writeb(addr
+ reg
, value
);
87 mmio_config_writew(addr
+ reg
, value
);
90 mmio_config_writel(addr
+ reg
, value
);
97 static struct pci_raw_ops pci_mmcfg
= {
98 .read
= pci_mmcfg_read
,
99 .write
= pci_mmcfg_write
,
102 static void __iomem
* __init
mcfg_ioremap(struct pci_mmcfg_region
*cfg
)
108 start
= cfg
->address
+ PCI_MMCFG_BUS_OFFSET(cfg
->start_bus
);
109 num_buses
= cfg
->end_bus
- cfg
->start_bus
+ 1;
110 size
= PCI_MMCFG_BUS_OFFSET(num_buses
);
111 addr
= ioremap_nocache(start
, size
);
113 printk(KERN_INFO
"PCI: Using MMCONFIG at %Lx - %Lx\n",
114 start
, start
+ size
- 1);
115 addr
-= PCI_MMCFG_BUS_OFFSET(cfg
->start_bus
);
120 int __init
pci_mmcfg_arch_init(void)
122 struct pci_mmcfg_region
*cfg
;
124 list_for_each_entry(cfg
, &pci_mmcfg_list
, list
) {
125 cfg
->virt
= mcfg_ioremap(cfg
);
127 printk(KERN_ERR
"PCI: Cannot map mmconfig aperture for "
130 pci_mmcfg_arch_free();
134 raw_pci_ext_ops
= &pci_mmcfg
;
138 void __init
pci_mmcfg_arch_free(void)
140 struct pci_mmcfg_region
*cfg
;
142 list_for_each_entry(cfg
, &pci_mmcfg_list
, list
) {
144 iounmap(cfg
->virt
+ PCI_MMCFG_BUS_OFFSET(cfg
->start_bus
));