1 // SPDX-License-Identifier: GPL-2.0
3 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
5 * This is an 64bit optimized version that always keeps the full mmconfig
6 * space mapped. This allows lockless config space operation.
10 #include <linux/init.h>
11 #include <linux/acpi.h>
12 #include <linux/bitmap.h>
13 #include <linux/rcupdate.h>
14 #include <asm/e820/api.h>
15 #include <asm/pci_x86.h>
17 #define PREFIX "PCI: "
19 static char __iomem
*pci_dev_base(unsigned int seg
, unsigned int bus
, unsigned int devfn
)
21 struct pci_mmcfg_region
*cfg
= pci_mmconfig_lookup(seg
, bus
);
24 return cfg
->virt
+ (PCI_MMCFG_BUS_OFFSET(bus
) | (devfn
<< 12));
28 static int pci_mmcfg_read(unsigned int seg
, unsigned int bus
,
29 unsigned int devfn
, int reg
, int len
, u32
*value
)
33 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
34 if (unlikely((bus
> 255) || (devfn
> 255) || (reg
> 4095))) {
40 addr
= pci_dev_base(seg
, bus
, devfn
);
48 *value
= mmio_config_readb(addr
+ reg
);
51 *value
= mmio_config_readw(addr
+ reg
);
54 *value
= mmio_config_readl(addr
+ reg
);
62 static int pci_mmcfg_write(unsigned int seg
, unsigned int bus
,
63 unsigned int devfn
, int reg
, int len
, u32 value
)
67 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
68 if (unlikely((bus
> 255) || (devfn
> 255) || (reg
> 4095)))
72 addr
= pci_dev_base(seg
, bus
, devfn
);
80 mmio_config_writeb(addr
+ reg
, value
);
83 mmio_config_writew(addr
+ reg
, value
);
86 mmio_config_writel(addr
+ reg
, value
);
94 const struct pci_raw_ops pci_mmcfg
= {
95 .read
= pci_mmcfg_read
,
96 .write
= pci_mmcfg_write
,
99 static void __iomem
*mcfg_ioremap(struct pci_mmcfg_region
*cfg
)
105 start
= cfg
->address
+ PCI_MMCFG_BUS_OFFSET(cfg
->start_bus
);
106 num_buses
= cfg
->end_bus
- cfg
->start_bus
+ 1;
107 size
= PCI_MMCFG_BUS_OFFSET(num_buses
);
108 addr
= ioremap_nocache(start
, size
);
110 addr
-= PCI_MMCFG_BUS_OFFSET(cfg
->start_bus
);
114 int __init
pci_mmcfg_arch_init(void)
116 struct pci_mmcfg_region
*cfg
;
118 list_for_each_entry(cfg
, &pci_mmcfg_list
, list
)
119 if (pci_mmcfg_arch_map(cfg
)) {
120 pci_mmcfg_arch_free();
124 raw_pci_ext_ops
= &pci_mmcfg
;
129 void __init
pci_mmcfg_arch_free(void)
131 struct pci_mmcfg_region
*cfg
;
133 list_for_each_entry(cfg
, &pci_mmcfg_list
, list
)
134 pci_mmcfg_arch_unmap(cfg
);
137 int pci_mmcfg_arch_map(struct pci_mmcfg_region
*cfg
)
139 cfg
->virt
= mcfg_ioremap(cfg
);
141 pr_err(PREFIX
"can't map MMCONFIG at %pR\n", &cfg
->res
);
148 void pci_mmcfg_arch_unmap(struct pci_mmcfg_region
*cfg
)
150 if (cfg
&& cfg
->virt
) {
151 iounmap(cfg
->virt
+ PCI_MMCFG_BUS_OFFSET(cfg
->start_bus
));