blob: faabb6e87f12461de8c25bf4ec4f14f75216edaa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +03003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Chuck Ebbertead2bfe2006-06-15 04:41:52 -040016/* aperture is up to 256MB but BIOS may reserve less */
17#define MMCONFIG_APER_MIN (2 * 1024*1024)
18#define MMCONFIG_APER_MAX (256 * 1024*1024)
19
Andi Kleen8c30b1a742006-04-07 19:50:12 +020020/* Verify the first 16 busses. We assume that systems with more busses
21 get MCFG right. */
22#define MAX_CHECK_BUS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Andi Kleen8c30b1a742006-04-07 19:50:12 +020024static DECLARE_BITMAP(fallback_slots, 32*MAX_CHECK_BUS);
Andi Kleend6ece542005-12-12 22:17:11 -080025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Static virtual mapping of the MMCONFIG aperture */
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070027struct mmcfg_virt {
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +030028 struct acpi_mcfg_allocation *cfg;
Al Viro8b8a4e32005-12-15 09:17:44 +000029 char __iomem *virt;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070030};
31static struct mmcfg_virt *pci_mmcfg_virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Al Viro8b8a4e32005-12-15 09:17:44 +000033static char __iomem *get_virt(unsigned int seg, unsigned bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070035 int cfg_num = -1;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +030036 struct acpi_mcfg_allocation *cfg;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070037
38 while (1) {
39 ++cfg_num;
Andi Kleen31030392006-01-27 02:03:50 +010040 if (cfg_num >= pci_mmcfg_config_num)
41 break;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070042 cfg = pci_mmcfg_virt[cfg_num].cfg;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +030043 if (cfg->pci_segment != seg)
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070044 continue;
45 if ((cfg->start_bus_number <= bus) &&
46 (cfg->end_bus_number >= bus))
47 return pci_mmcfg_virt[cfg_num].virt;
48 }
Andi Kleen31030392006-01-27 02:03:50 +010049
50 /* Handle more broken MCFG tables on Asus etc.
51 They only contain a single entry for bus 0-0. Assume
52 this applies to all busses. */
53 cfg = &pci_mmcfg_config[0];
54 if (pci_mmcfg_config_num == 1 &&
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +030055 cfg->pci_segment == 0 &&
Andi Kleen31030392006-01-27 02:03:50 +010056 (cfg->start_bus_number | cfg->end_bus_number) == 0)
Andi Kleen1de6bf32006-02-03 21:51:29 +010057 return pci_mmcfg_virt[0].virt;
Andi Kleen31030392006-01-27 02:03:50 +010058
59 /* Fall back to type 0 */
Al Virocc598532006-02-03 20:28:01 -050060 return NULL;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070061}
62
Al Viro8b8a4e32005-12-15 09:17:44 +000063static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -070064{
Al Viro8b8a4e32005-12-15 09:17:44 +000065 char __iomem *addr;
Andi Kleen8c30b1a742006-04-07 19:50:12 +020066 if (seg == 0 && bus < MAX_CHECK_BUS &&
67 test_bit(32*bus + PCI_SLOT(devfn), fallback_slots))
Andi Kleend6ece542005-12-12 22:17:11 -080068 return NULL;
69 addr = get_virt(seg, bus);
Andi Kleen928cf8c2005-12-12 22:17:10 -080070 if (!addr)
71 return NULL;
72 return addr + ((bus << 20) | (devfn << 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
76 unsigned int devfn, int reg, int len, u32 *value)
77{
Al Viro8b8a4e32005-12-15 09:17:44 +000078 char __iomem *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Andi Kleen928cf8c2005-12-12 22:17:10 -080080 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
Andi Kleenecc16ba2006-04-11 12:54:48 +020081 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
Andi Kleen49c93e82006-04-07 19:50:15 +020082 *value = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return -EINVAL;
Andi Kleen49c93e82006-04-07 19:50:15 +020084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Andi Kleen928cf8c2005-12-12 22:17:10 -080086 addr = pci_dev_base(seg, bus, devfn);
87 if (!addr)
88 return pci_conf1_read(seg,bus,devfn,reg,len,value);
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 switch (len) {
91 case 1:
92 *value = readb(addr + reg);
93 break;
94 case 2:
95 *value = readw(addr + reg);
96 break;
97 case 4:
98 *value = readl(addr + reg);
99 break;
100 }
101
102 return 0;
103}
104
105static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
106 unsigned int devfn, int reg, int len, u32 value)
107{
Al Viro8b8a4e32005-12-15 09:17:44 +0000108 char __iomem *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Andi Kleen928cf8c2005-12-12 22:17:10 -0800110 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
112 return -EINVAL;
113
Andi Kleen928cf8c2005-12-12 22:17:10 -0800114 addr = pci_dev_base(seg, bus, devfn);
115 if (!addr)
116 return pci_conf1_write(seg,bus,devfn,reg,len,value);
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 switch (len) {
119 case 1:
120 writeb(value, addr + reg);
121 break;
122 case 2:
123 writew(value, addr + reg);
124 break;
125 case 4:
126 writel(value, addr + reg);
127 break;
128 }
129
130 return 0;
131}
132
133static struct pci_raw_ops pci_mmcfg = {
134 .read = pci_mmcfg_read,
135 .write = pci_mmcfg_write,
136};
137
Andi Kleend6ece542005-12-12 22:17:11 -0800138/* K8 systems have some devices (typically in the builtin northbridge)
139 that are only accessible using type1
140 Normally this can be expressed in the MCFG by not listing them
141 and assigning suitable _SEGs, but this isn't implemented in some BIOS.
142 Instead try to discover all devices on bus 0 that are unreachable using MM
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200143 and fallback for them. */
Andi Kleend6ece542005-12-12 22:17:11 -0800144static __init void unreachable_devices(void)
145{
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200146 int i, k;
147 /* Use the max bus number from ACPI here? */
Andi Kleen44b940c2006-04-11 12:54:51 +0200148 for (k = 0; k < MAX_CHECK_BUS; k++) {
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200149 for (i = 0; i < 32; i++) {
150 u32 val1;
151 char __iomem *addr;
Andi Kleend6ece542005-12-12 22:17:11 -0800152
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200153 pci_conf1_read(0, k, PCI_DEVFN(i,0), 0, 4, &val1);
154 if (val1 == 0xffffffff)
155 continue;
156 addr = pci_dev_base(0, k, PCI_DEVFN(i, 0));
157 if (addr == NULL|| readl(addr) != val1) {
158 set_bit(i + 32*k, fallback_slots);
Brice Goglin40bee2e2006-09-26 10:52:35 +0200159 printk(KERN_NOTICE "PCI: No mmconfig possible"
160 " on device %02x:%02x\n", k, i);
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200161 }
Andi Kleend6ece542005-12-12 22:17:11 -0800162 }
163 }
164}
165
Andi Kleen5e544d62006-09-26 10:52:40 +0200166void __init pci_mmcfg_init(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700168 int i;
169
Linus Torvalds79e453d2006-09-19 08:15:22 -0700170 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800171 return;
Greg Kroah-Hartman54549392005-06-23 17:35:56 -0700172
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300173 acpi_table_parse(ACPI_SIG_MCFG, acpi_parse_mcfg);
Greg Kroah-Hartman54549392005-06-23 17:35:56 -0700174 if ((pci_mmcfg_config_num == 0) ||
175 (pci_mmcfg_config == NULL) ||
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300176 (pci_mmcfg_config[0].address == 0))
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800177 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Andi Kleen9abd7922006-09-26 10:52:40 +0200179 /* Only do this check when type 1 works. If it doesn't work
180 assume we run on a Mac and always use MCFG */
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300181 if (type == 1 && !e820_all_mapped(pci_mmcfg_config[0].address,
182 pci_mmcfg_config[0].address + MMCONFIG_APER_MIN,
Linus Torvalds79e453d2006-09-19 08:15:22 -0700183 E820_RESERVED)) {
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300184 printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %lx is not E820-reserved\n",
185 (unsigned long)pci_mmcfg_config[0].address);
Linus Torvalds79e453d2006-09-19 08:15:22 -0700186 printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
187 return;
188 }
189
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700190 pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) * pci_mmcfg_config_num, GFP_KERNEL);
191 if (pci_mmcfg_virt == NULL) {
Dave Jones3095fc02006-10-20 14:45:33 -0700192 printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800193 return;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700194 }
195 for (i = 0; i < pci_mmcfg_config_num; ++i) {
196 pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300197 pci_mmcfg_virt[i].virt = ioremap_nocache(pci_mmcfg_config[i].address,
Chuck Ebbertead2bfe2006-06-15 04:41:52 -0400198 MMCONFIG_APER_MAX);
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700199 if (!pci_mmcfg_virt[i].virt) {
Dave Jones3095fc02006-10-20 14:45:33 -0700200 printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
201 "segment %d\n",
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300202 pci_mmcfg_config[i].pci_segment);
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800203 return;
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700204 }
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300205 printk(KERN_INFO "PCI: Using MMCONFIG at %lx\n",
206 (unsigned long)pci_mmcfg_config[i].address);
Greg Kroah-Hartman1cde8a12005-06-23 17:35:56 -0700207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Andi Kleend6ece542005-12-12 22:17:11 -0800209 unreachable_devices();
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 raw_pci_ops = &pci_mmcfg;
212 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}