Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * mmconfig.c - Low-level direct PCI config space access via MMCONFIG | |
15a58ed1 | 3 | * |
1da177e4 LT |
4 | * This is an 64bit optimized version that always keeps the full mmconfig |
5 | * space mapped. This allows lockless config space operation. | |
6 | */ | |
7 | ||
8 | #include <linux/pci.h> | |
9 | #include <linux/init.h> | |
54549391 | 10 | #include <linux/acpi.h> |
d6ece549 | 11 | #include <linux/bitmap.h> |
946f2ee5 | 12 | #include <asm/e820.h> |
82487711 | 13 | #include <asm/pci_x86.h> |
1da177e4 | 14 | |
1da177e4 | 15 | /* Static virtual mapping of the MMCONFIG aperture */ |
1cde8a16 | 16 | struct mmcfg_virt { |
15a58ed1 | 17 | struct acpi_mcfg_allocation *cfg; |
8b8a4e33 | 18 | char __iomem *virt; |
1cde8a16 GKH |
19 | }; |
20 | static struct mmcfg_virt *pci_mmcfg_virt; | |
1da177e4 | 21 | |
8b8a4e33 | 22 | static char __iomem *get_virt(unsigned int seg, unsigned bus) |
1da177e4 | 23 | { |
15a58ed1 | 24 | struct acpi_mcfg_allocation *cfg; |
429d512e | 25 | int cfg_num; |
1cde8a16 | 26 | |
429d512e | 27 | for (cfg_num = 0; cfg_num < pci_mmcfg_config_num; cfg_num++) { |
1cde8a16 | 28 | cfg = pci_mmcfg_virt[cfg_num].cfg; |
429d512e OH |
29 | if (cfg->pci_segment == seg && |
30 | (cfg->start_bus_number <= bus) && | |
1cde8a16 GKH |
31 | (cfg->end_bus_number >= bus)) |
32 | return pci_mmcfg_virt[cfg_num].virt; | |
33 | } | |
3103039c | 34 | |
3103039c | 35 | /* Fall back to type 0 */ |
cc59853b | 36 | return NULL; |
1cde8a16 GKH |
37 | } |
38 | ||
8b8a4e33 | 39 | static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn) |
1cde8a16 | 40 | { |
8b8a4e33 | 41 | char __iomem *addr; |
a0ca9909 | 42 | |
d6ece549 | 43 | addr = get_virt(seg, bus); |
928cf8c6 AK |
44 | if (!addr) |
45 | return NULL; | |
46 | return addr + ((bus << 20) | (devfn << 12)); | |
1da177e4 LT |
47 | } |
48 | ||
49 | static int pci_mmcfg_read(unsigned int seg, unsigned int bus, | |
50 | unsigned int devfn, int reg, int len, u32 *value) | |
51 | { | |
8b8a4e33 | 52 | char __iomem *addr; |
1da177e4 | 53 | |
928cf8c6 | 54 | /* Why do we have this when nobody checks it. How about a BUG()!? -AK */ |
ecc16ba9 | 55 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) { |
a0ca9909 | 56 | err: *value = -1; |
1da177e4 | 57 | return -EINVAL; |
49c93e84 | 58 | } |
1da177e4 | 59 | |
928cf8c6 AK |
60 | addr = pci_dev_base(seg, bus, devfn); |
61 | if (!addr) | |
a0ca9909 | 62 | goto err; |
928cf8c6 | 63 | |
1da177e4 LT |
64 | switch (len) { |
65 | case 1: | |
3320ad99 | 66 | *value = mmio_config_readb(addr + reg); |
1da177e4 LT |
67 | break; |
68 | case 2: | |
3320ad99 | 69 | *value = mmio_config_readw(addr + reg); |
1da177e4 LT |
70 | break; |
71 | case 4: | |
3320ad99 | 72 | *value = mmio_config_readl(addr + reg); |
1da177e4 LT |
73 | break; |
74 | } | |
75 | ||
76 | return 0; | |
77 | } | |
78 | ||
79 | static int pci_mmcfg_write(unsigned int seg, unsigned int bus, | |
80 | unsigned int devfn, int reg, int len, u32 value) | |
81 | { | |
8b8a4e33 | 82 | char __iomem *addr; |
1da177e4 | 83 | |
928cf8c6 | 84 | /* Why do we have this when nobody checks it. How about a BUG()!? -AK */ |
1da177e4 LT |
85 | if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) |
86 | return -EINVAL; | |
87 | ||
928cf8c6 AK |
88 | addr = pci_dev_base(seg, bus, devfn); |
89 | if (!addr) | |
a0ca9909 | 90 | return -EINVAL; |
928cf8c6 | 91 | |
1da177e4 LT |
92 | switch (len) { |
93 | case 1: | |
3320ad99 | 94 | mmio_config_writeb(addr + reg, value); |
1da177e4 LT |
95 | break; |
96 | case 2: | |
3320ad99 | 97 | mmio_config_writew(addr + reg, value); |
1da177e4 LT |
98 | break; |
99 | case 4: | |
3320ad99 | 100 | mmio_config_writel(addr + reg, value); |
1da177e4 LT |
101 | break; |
102 | } | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
107 | static struct pci_raw_ops pci_mmcfg = { | |
108 | .read = pci_mmcfg_read, | |
109 | .write = pci_mmcfg_write, | |
110 | }; | |
111 | ||
44de0203 OH |
112 | static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg) |
113 | { | |
114 | void __iomem *addr; | |
115 | u32 size; | |
116 | ||
117 | size = (cfg->end_bus_number + 1) << 20; | |
118 | addr = ioremap_nocache(cfg->address, size); | |
119 | if (addr) { | |
120 | printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n", | |
121 | cfg->address, cfg->address + size - 1); | |
122 | } | |
123 | return addr; | |
124 | } | |
125 | ||
b7867394 | 126 | int __init pci_mmcfg_arch_init(void) |
1da177e4 | 127 | { |
1cde8a16 | 128 | int i; |
0b64ad71 | 129 | pci_mmcfg_virt = kzalloc(sizeof(*pci_mmcfg_virt) * |
b7867394 | 130 | pci_mmcfg_config_num, GFP_KERNEL); |
1cde8a16 | 131 | if (pci_mmcfg_virt == NULL) { |
3095fc0c | 132 | printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n"); |
b7867394 | 133 | return 0; |
1cde8a16 | 134 | } |
b7867394 | 135 | |
1cde8a16 GKH |
136 | for (i = 0; i < pci_mmcfg_config_num; ++i) { |
137 | pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i]; | |
faed197b | 138 | pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]); |
1cde8a16 | 139 | if (!pci_mmcfg_virt[i].virt) { |
3095fc0c DJ |
140 | printk(KERN_ERR "PCI: Cannot map mmconfig aperture for " |
141 | "segment %d\n", | |
15a58ed1 | 142 | pci_mmcfg_config[i].pci_segment); |
0b64ad71 | 143 | pci_mmcfg_arch_free(); |
b7867394 | 144 | return 0; |
1cde8a16 | 145 | } |
1cde8a16 | 146 | } |
b6ce068a | 147 | raw_pci_ext_ops = &pci_mmcfg; |
b7867394 | 148 | return 1; |
1da177e4 | 149 | } |
0b64ad71 YL |
150 | |
151 | void __init pci_mmcfg_arch_free(void) | |
152 | { | |
153 | int i; | |
154 | ||
155 | if (pci_mmcfg_virt == NULL) | |
156 | return; | |
157 | ||
158 | for (i = 0; i < pci_mmcfg_config_num; ++i) { | |
159 | if (pci_mmcfg_virt[i].virt) { | |
160 | iounmap(pci_mmcfg_virt[i].virt); | |
161 | pci_mmcfg_virt[i].virt = NULL; | |
162 | pci_mmcfg_virt[i].cfg = NULL; | |
163 | } | |
164 | } | |
165 | ||
166 | kfree(pci_mmcfg_virt); | |
167 | pci_mmcfg_virt = NULL; | |
168 | } |