blob: 05be8db58a8cee783bde63b5f207d7e8137fd7fc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2004 Matthew Wilcox <matthew@wil.cx>
3 * Copyright (C) 2004 Intel Corp.
4 *
5 * This code is released under the GNU General Public License version 2.
6 */
7
8/*
9 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
10 */
11
12#include <linux/pci.h>
13#include <linux/init.h>
Greg Kroah-Hartman54549392005-06-23 17:35:56 -070014#include <linux/acpi.h>
Arjan van de Ven946f2ee2006-04-07 19:49:30 +020015#include <asm/e820.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "pci.h"
17
Chuck Ebbertead2bfe2006-06-15 04:41:52 -040018/* aperture is up to 256MB but BIOS may reserve less */
19#define MMCONFIG_APER_MIN (2 * 1024*1024)
20#define MMCONFIG_APER_MAX (256 * 1024*1024)
Arjan van de Ven946f2ee2006-04-07 19:49:30 +020021
Andi Kleen8c30b1a742006-04-07 19:50:12 +020022/* Assume systems with more busses have correct MCFG */
23#define MAX_CHECK_BUS 16
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG))
26
27/* The base address of the last MMCONFIG device accessed */
28static u32 mmcfg_last_accessed_device;
29
Andi Kleen8c30b1a742006-04-07 19:50:12 +020030static DECLARE_BITMAP(fallback_slots, MAX_CHECK_BUS*32);
Andi Kleend6ece542005-12-12 22:17:11 -080031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * Functions for accessing PCI configuration space with MMCONFIG accesses
34 */
Andi Kleend6ece542005-12-12 22:17:11 -080035static u32 get_base_addr(unsigned int seg, int bus, unsigned devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070037 int cfg_num = -1;
38 struct acpi_table_mcfg_config *cfg;
39
Andi Kleen8c30b1a742006-04-07 19:50:12 +020040 if (seg == 0 && bus < MAX_CHECK_BUS &&
41 test_bit(PCI_SLOT(devfn) + 32*bus, fallback_slots))
Andi Kleend6ece542005-12-12 22:17:11 -080042 return 0;
43
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070044 while (1) {
45 ++cfg_num;
46 if (cfg_num >= pci_mmcfg_config_num) {
Andi Kleen31030392006-01-27 02:03:50 +010047 break;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070048 }
49 cfg = &pci_mmcfg_config[cfg_num];
50 if (cfg->pci_segment_group_number != seg)
51 continue;
52 if ((cfg->start_bus_number <= bus) &&
53 (cfg->end_bus_number >= bus))
54 return cfg->base_address;
55 }
Andi Kleen31030392006-01-27 02:03:50 +010056
57 /* Handle more broken MCFG tables on Asus etc.
58 They only contain a single entry for bus 0-0. Assume
59 this applies to all busses. */
60 cfg = &pci_mmcfg_config[0];
61 if (pci_mmcfg_config_num == 1 &&
62 cfg->pci_segment_group_number == 0 &&
63 (cfg->start_bus_number | cfg->end_bus_number) == 0)
64 return cfg->base_address;
65
66 /* Fall back to type 0 */
67 return 0;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070068}
69
Andi Kleen928cf8c2005-12-12 22:17:10 -080070static inline void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070071{
Andi Kleen928cf8c2005-12-12 22:17:10 -080072 u32 dev_base = base | (bus << 20) | (devfn << 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (dev_base != mmcfg_last_accessed_device) {
74 mmcfg_last_accessed_device = dev_base;
75 set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
76 }
77}
78
79static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
80 unsigned int devfn, int reg, int len, u32 *value)
81{
82 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -080083 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Andi Kleenecc16ba2006-04-11 12:54:48 +020085 if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
Andi Kleen49c93e82006-04-07 19:50:15 +020086 *value = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return -EINVAL;
Andi Kleen49c93e82006-04-07 19:50:15 +020088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Andi Kleend6ece542005-12-12 22:17:11 -080090 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -080091 if (!base)
92 return pci_conf1_read(seg,bus,devfn,reg,len,value);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 spin_lock_irqsave(&pci_config_lock, flags);
95
Andi Kleen928cf8c2005-12-12 22:17:10 -080096 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 switch (len) {
99 case 1:
100 *value = readb(mmcfg_virt_addr + reg);
101 break;
102 case 2:
103 *value = readw(mmcfg_virt_addr + reg);
104 break;
105 case 4:
106 *value = readl(mmcfg_virt_addr + reg);
107 break;
108 }
109
110 spin_unlock_irqrestore(&pci_config_lock, flags);
111
112 return 0;
113}
114
115static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
116 unsigned int devfn, int reg, int len, u32 value)
117{
118 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -0800119 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if ((bus > 255) || (devfn > 255) || (reg > 4095))
122 return -EINVAL;
123
Andi Kleend6ece542005-12-12 22:17:11 -0800124 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -0800125 if (!base)
126 return pci_conf1_write(seg,bus,devfn,reg,len,value);
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 spin_lock_irqsave(&pci_config_lock, flags);
129
Andi Kleen928cf8c2005-12-12 22:17:10 -0800130 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 switch (len) {
133 case 1:
134 writeb(value, mmcfg_virt_addr + reg);
135 break;
136 case 2:
137 writew(value, mmcfg_virt_addr + reg);
138 break;
139 case 4:
140 writel(value, mmcfg_virt_addr + reg);
141 break;
142 }
143
144 spin_unlock_irqrestore(&pci_config_lock, flags);
145
146 return 0;
147}
148
149static struct pci_raw_ops pci_mmcfg = {
150 .read = pci_mmcfg_read,
151 .write = pci_mmcfg_write,
152};
153
Andi Kleende09bdd2006-09-26 10:52:40 +0200154
155static __init void pci_mmcfg_insert_resources(void)
156{
157#define PCI_MMCFG_RESOURCE_NAME_LEN 19
158 int i;
159 struct resource *res;
160 char *names;
161 unsigned num_buses;
162
163 res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
164 pci_mmcfg_config_num, GFP_KERNEL);
165
166 if (!res) {
167 printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
168 return;
169 }
170
171 names = (void *)&res[pci_mmcfg_config_num];
172 for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
173 num_buses = pci_mmcfg_config[i].end_bus_number -
174 pci_mmcfg_config[i].start_bus_number + 1;
175 res->name = names;
176 snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN, "PCI MMCONFIG %u",
177 pci_mmcfg_config[i].pci_segment_group_number);
178 res->start = pci_mmcfg_config[i].base_address;
179 res->end = res->start + (num_buses << 20) - 1;
180 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
181 insert_resource(&iomem_resource, res);
182 names += PCI_MMCFG_RESOURCE_NAME_LEN;
183 }
184}
185
Andi Kleend6ece542005-12-12 22:17:11 -0800186/* K8 systems have some devices (typically in the builtin northbridge)
187 that are only accessible using type1
188 Normally this can be expressed in the MCFG by not listing them
189 and assigning suitable _SEGs, but this isn't implemented in some BIOS.
190 Instead try to discover all devices on bus 0 that are unreachable using MM
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200191 and fallback for them. */
Andi Kleend6ece542005-12-12 22:17:11 -0800192static __init void unreachable_devices(void)
193{
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200194 int i, k;
Andi Kleend6ece542005-12-12 22:17:11 -0800195 unsigned long flags;
196
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200197 for (k = 0; k < MAX_CHECK_BUS; k++) {
198 for (i = 0; i < 32; i++) {
199 u32 val1;
200 u32 addr;
Andi Kleend6ece542005-12-12 22:17:11 -0800201
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200202 pci_conf1_read(0, k, PCI_DEVFN(i, 0), 0, 4, &val1);
203 if (val1 == 0xffffffff)
204 continue;
Andi Kleend6ece542005-12-12 22:17:11 -0800205
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200206 /* Locking probably not needed, but safer */
207 spin_lock_irqsave(&pci_config_lock, flags);
208 addr = get_base_addr(0, k, PCI_DEVFN(i, 0));
209 if (addr != 0)
210 pci_exp_set_dev_base(addr, k, PCI_DEVFN(i, 0));
211 if (addr == 0 ||
212 readl((u32 __iomem *)mmcfg_virt_addr) != val1) {
Daniel Ritzfd4dc272006-08-22 07:29:09 -0700213 set_bit(i + 32*k, fallback_slots);
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200214 printk(KERN_NOTICE
215 "PCI: No mmconfig possible on %x:%x\n", k, i);
216 }
217 spin_unlock_irqrestore(&pci_config_lock, flags);
218 }
Andi Kleend6ece542005-12-12 22:17:11 -0800219 }
220}
221
Andi Kleende09bdd2006-09-26 10:52:40 +0200222
223
Andi Kleen5e544d62006-09-26 10:52:40 +0200224void __init pci_mmcfg_init(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Linus Torvalds79e453d2006-09-19 08:15:22 -0700226 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
Andi Kleen92c05fc2006-03-23 14:35:12 -0800227 return;
Greg Kroah-Hartman54549392005-06-23 17:35:56 -0700228
229 acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
230 if ((pci_mmcfg_config_num == 0) ||
231 (pci_mmcfg_config == NULL) ||
232 (pci_mmcfg_config[0].base_address == 0))
Andi Kleen92c05fc2006-03-23 14:35:12 -0800233 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Andi Kleen9abd7922006-09-26 10:52:40 +0200235 /* Only do this check when type 1 works. If it doesn't work
236 assume we run on a Mac and always use MCFG */
237 if (type == 1 && !e820_all_mapped(pci_mmcfg_config[0].base_address,
Linus Torvalds79e453d2006-09-19 08:15:22 -0700238 pci_mmcfg_config[0].base_address + MMCONFIG_APER_MIN,
239 E820_RESERVED)) {
240 printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %x is not E820-reserved\n",
241 pci_mmcfg_config[0].base_address);
242 printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
243 return;
244 }
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 printk(KERN_INFO "PCI: Using MMCONFIG\n");
247 raw_pci_ops = &pci_mmcfg;
248 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
249
Andi Kleend6ece542005-12-12 22:17:11 -0800250 unreachable_devices();
Andi Kleende09bdd2006-09-26 10:52:40 +0200251 pci_mmcfg_insert_resources();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}