blob: 1a59a31bb7406d48445da0c9eb392281e1fb9827 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
3 *
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>
Greg Kroah-Hartman54549392005-06-23 17:35:56 -070010#include <linux/acpi.h>
Andi Kleend6ece542005-12-12 22:17:11 -080011#include <linux/bitmap.h>
Arjan van de Ven946f2ee2006-04-07 19:49:30 +020012#include <asm/e820.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "pci.h"
15
16#define MMCONFIG_APER_SIZE (256*1024*1024)
Andi Kleen8c30b1a742006-04-07 19:50:12 +020017/* Verify the first 16 busses. We assume that systems with more busses
18 get MCFG right. */
19#define MAX_CHECK_BUS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Andi Kleen8c30b1a742006-04-07 19:50:12 +020021static DECLARE_BITMAP(fallback_slots, 32*MAX_CHECK_BUS);
Andi Kleend6ece542005-12-12 22:17:11 -080022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/* Static virtual mapping of the MMCONFIG aperture */
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070024struct mmcfg_virt {
25 struct acpi_table_mcfg_config *cfg;
Al Viro8b8a4e32005-12-15 09:17:44 +000026 char __iomem *virt;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070027};
28static struct mmcfg_virt *pci_mmcfg_virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Al Viro8b8a4e32005-12-15 09:17:44 +000030static char __iomem *get_virt(unsigned int seg, unsigned bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070032 int cfg_num = -1;
33 struct acpi_table_mcfg_config *cfg;
34
35 while (1) {
36 ++cfg_num;
Andi Kleen31030392006-01-27 02:03:50 +010037 if (cfg_num >= pci_mmcfg_config_num)
38 break;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070039 cfg = pci_mmcfg_virt[cfg_num].cfg;
40 if (cfg->pci_segment_group_number != seg)
41 continue;
42 if ((cfg->start_bus_number <= bus) &&
43 (cfg->end_bus_number >= bus))
44 return pci_mmcfg_virt[cfg_num].virt;
45 }
Andi Kleen31030392006-01-27 02:03:50 +010046
47 /* Handle more broken MCFG tables on Asus etc.
48 They only contain a single entry for bus 0-0. Assume
49 this applies to all busses. */
50 cfg = &pci_mmcfg_config[0];
51 if (pci_mmcfg_config_num == 1 &&
52 cfg->pci_segment_group_number == 0 &&
53 (cfg->start_bus_number | cfg->end_bus_number) == 0)
Andi Kleen1de6bf32006-02-03 21:51:29 +010054 return pci_mmcfg_virt[0].virt;
Andi Kleen31030392006-01-27 02:03:50 +010055
56 /* Fall back to type 0 */
Al Virocc598532006-02-03 20:28:01 -050057 return NULL;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070058}
59
Al Viro8b8a4e32005-12-15 09:17:44 +000060static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070061{
Al Viro8b8a4e32005-12-15 09:17:44 +000062 char __iomem *addr;
Andi Kleen8c30b1a742006-04-07 19:50:12 +020063 if (seg == 0 && bus < MAX_CHECK_BUS &&
64 test_bit(32*bus + PCI_SLOT(devfn), fallback_slots))
Andi Kleend6ece542005-12-12 22:17:11 -080065 return NULL;
66 addr = get_virt(seg, bus);
Andi Kleen928cf8c2005-12-12 22:17:10 -080067 if (!addr)
68 return NULL;
69 return addr + ((bus << 20) | (devfn << 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
73 unsigned int devfn, int reg, int len, u32 *value)
74{
Al Viro8b8a4e32005-12-15 09:17:44 +000075 char __iomem *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Andi Kleen928cf8c2005-12-12 22:17:10 -080077 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
Andi Kleenecc16ba2006-04-11 12:54:48 +020078 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
Andi Kleen49c93e82006-04-07 19:50:15 +020079 *value = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EINVAL;
Andi Kleen49c93e82006-04-07 19:50:15 +020081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Andi Kleen928cf8c2005-12-12 22:17:10 -080083 addr = pci_dev_base(seg, bus, devfn);
84 if (!addr)
85 return pci_conf1_read(seg,bus,devfn,reg,len,value);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 switch (len) {
88 case 1:
89 *value = readb(addr + reg);
90 break;
91 case 2:
92 *value = readw(addr + reg);
93 break;
94 case 4:
95 *value = readl(addr + reg);
96 break;
97 }
98
99 return 0;
100}
101
102static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
103 unsigned int devfn, int reg, int len, u32 value)
104{
Al Viro8b8a4e32005-12-15 09:17:44 +0000105 char __iomem *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Andi Kleen928cf8c2005-12-12 22:17:10 -0800107 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
109 return -EINVAL;
110
Andi Kleen928cf8c2005-12-12 22:17:10 -0800111 addr = pci_dev_base(seg, bus, devfn);
112 if (!addr)
113 return pci_conf1_write(seg,bus,devfn,reg,len,value);
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 switch (len) {
116 case 1:
117 writeb(value, addr + reg);
118 break;
119 case 2:
120 writew(value, addr + reg);
121 break;
122 case 4:
123 writel(value, addr + reg);
124 break;
125 }
126
127 return 0;
128}
129
130static struct pci_raw_ops pci_mmcfg = {
131 .read = pci_mmcfg_read,
132 .write = pci_mmcfg_write,
133};
134
Andi Kleend6ece542005-12-12 22:17:11 -0800135/* K8 systems have some devices (typically in the builtin northbridge)
136 that are only accessible using type1
137 Normally this can be expressed in the MCFG by not listing them
138 and assigning suitable _SEGs, but this isn't implemented in some BIOS.
139 Instead try to discover all devices on bus 0 that are unreachable using MM
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200140 and fallback for them. */
Andi Kleend6ece542005-12-12 22:17:11 -0800141static __init void unreachable_devices(void)
142{
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200143 int i, k;
144 /* Use the max bus number from ACPI here? */
145 for (k = 0; i < MAX_CHECK_BUS; k++) {
146 for (i = 0; i < 32; i++) {
147 u32 val1;
148 char __iomem *addr;
Andi Kleend6ece542005-12-12 22:17:11 -0800149
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200150 pci_conf1_read(0, k, PCI_DEVFN(i,0), 0, 4, &val1);
151 if (val1 == 0xffffffff)
152 continue;
153 addr = pci_dev_base(0, k, PCI_DEVFN(i, 0));
154 if (addr == NULL|| readl(addr) != val1) {
155 set_bit(i + 32*k, fallback_slots);
156 printk(KERN_NOTICE
157 "PCI: No mmconfig possible on device %x:%x\n",
158 k, i);
159 }
Andi Kleend6ece542005-12-12 22:17:11 -0800160 }
161 }
162}
163
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800164void __init pci_mmcfg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700166 int i;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800169 return;
Greg Kroah-Hartman54549392005-06-23 17:35:56 -0700170
171 acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
172 if ((pci_mmcfg_config_num == 0) ||
173 (pci_mmcfg_config == NULL) ||
174 (pci_mmcfg_config[0].base_address == 0))
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800175 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Arjan van de Ven946f2ee2006-04-07 19:49:30 +0200177 if (!e820_all_mapped(pci_mmcfg_config[0].base_address,
178 pci_mmcfg_config[0].base_address + MMCONFIG_APER_SIZE,
179 E820_RESERVED)) {
180 printk(KERN_ERR "PCI: BIOS Bug: MCFG area is not E820-reserved\n");
181 printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
182 return;
183 }
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* RED-PEN i386 doesn't do _nocache right now */
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700186 pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) * pci_mmcfg_config_num, GFP_KERNEL);
187 if (pci_mmcfg_virt == NULL) {
188 printk("PCI: Can not allocate memory for mmconfig structures\n");
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800189 return;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700190 }
191 for (i = 0; i < pci_mmcfg_config_num; ++i) {
192 pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
193 pci_mmcfg_virt[i].virt = ioremap_nocache(pci_mmcfg_config[i].base_address, MMCONFIG_APER_SIZE);
194 if (!pci_mmcfg_virt[i].virt) {
195 printk("PCI: Cannot map mmconfig aperture for segment %d\n",
196 pci_mmcfg_config[i].pci_segment_group_number);
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800197 return;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700198 }
199 printk(KERN_INFO "PCI: Using MMCONFIG at %x\n", pci_mmcfg_config[i].base_address);
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Andi Kleend6ece542005-12-12 22:17:11 -0800202 unreachable_devices();
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 raw_pci_ops = &pci_mmcfg;
205 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}