blob: e2616a266e13c50cea634238b2dbbb98c3cbb062 [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;
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090029static int mmcfg_last_accessed_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Andi Kleen8c30b1a742006-04-07 19:50:12 +020031static DECLARE_BITMAP(fallback_slots, MAX_CHECK_BUS*32);
Andi Kleend6ece542005-12-12 22:17:11 -080032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * Functions for accessing PCI configuration space with MMCONFIG accesses
35 */
Andi Kleend6ece542005-12-12 22:17:11 -080036static u32 get_base_addr(unsigned int seg, int bus, unsigned devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070038 int cfg_num = -1;
39 struct acpi_table_mcfg_config *cfg;
40
Andi Kleen8c30b1a742006-04-07 19:50:12 +020041 if (seg == 0 && bus < MAX_CHECK_BUS &&
42 test_bit(PCI_SLOT(devfn) + 32*bus, fallback_slots))
Andi Kleend6ece542005-12-12 22:17:11 -080043 return 0;
44
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070045 while (1) {
46 ++cfg_num;
47 if (cfg_num >= pci_mmcfg_config_num) {
Andi Kleen31030392006-01-27 02:03:50 +010048 break;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070049 }
50 cfg = &pci_mmcfg_config[cfg_num];
51 if (cfg->pci_segment_group_number != seg)
52 continue;
53 if ((cfg->start_bus_number <= bus) &&
54 (cfg->end_bus_number >= bus))
55 return cfg->base_address;
56 }
Andi Kleen31030392006-01-27 02:03:50 +010057
58 /* Handle more broken MCFG tables on Asus etc.
59 They only contain a single entry for bus 0-0. Assume
60 this applies to all busses. */
61 cfg = &pci_mmcfg_config[0];
62 if (pci_mmcfg_config_num == 1 &&
63 cfg->pci_segment_group_number == 0 &&
64 (cfg->start_bus_number | cfg->end_bus_number) == 0)
65 return cfg->base_address;
66
67 /* Fall back to type 0 */
68 return 0;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070069}
70
Andrew Mortonbe5b7a82006-09-30 23:27:10 -070071/*
72 * This is always called under pci_config_lock
73 */
74static void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070075{
Andi Kleen928cf8c2005-12-12 22:17:10 -080076 u32 dev_base = base | (bus << 20) | (devfn << 12);
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090077 int cpu = smp_processor_id();
78 if (dev_base != mmcfg_last_accessed_device ||
79 cpu != mmcfg_last_accessed_cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 mmcfg_last_accessed_device = dev_base;
OGAWA Hirofumi8d1c4812006-12-23 10:00:43 +090081 mmcfg_last_accessed_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
83 }
84}
85
86static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
87 unsigned int devfn, int reg, int len, u32 *value)
88{
89 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -080090 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Andi Kleenecc16ba2006-04-11 12:54:48 +020092 if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
Andi Kleen49c93e82006-04-07 19:50:15 +020093 *value = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return -EINVAL;
Andi Kleen49c93e82006-04-07 19:50:15 +020095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Andi Kleend6ece542005-12-12 22:17:11 -080097 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -080098 if (!base)
99 return pci_conf1_read(seg,bus,devfn,reg,len,value);
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 spin_lock_irqsave(&pci_config_lock, flags);
102
Andi Kleen928cf8c2005-12-12 22:17:10 -0800103 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 switch (len) {
106 case 1:
107 *value = readb(mmcfg_virt_addr + reg);
108 break;
109 case 2:
110 *value = readw(mmcfg_virt_addr + reg);
111 break;
112 case 4:
113 *value = readl(mmcfg_virt_addr + reg);
114 break;
115 }
116
117 spin_unlock_irqrestore(&pci_config_lock, flags);
118
119 return 0;
120}
121
122static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
123 unsigned int devfn, int reg, int len, u32 value)
124{
125 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -0800126 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if ((bus > 255) || (devfn > 255) || (reg > 4095))
129 return -EINVAL;
130
Andi Kleend6ece542005-12-12 22:17:11 -0800131 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -0800132 if (!base)
133 return pci_conf1_write(seg,bus,devfn,reg,len,value);
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 spin_lock_irqsave(&pci_config_lock, flags);
136
Andi Kleen928cf8c2005-12-12 22:17:10 -0800137 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 switch (len) {
140 case 1:
141 writeb(value, mmcfg_virt_addr + reg);
142 break;
143 case 2:
144 writew(value, mmcfg_virt_addr + reg);
145 break;
146 case 4:
147 writel(value, mmcfg_virt_addr + reg);
148 break;
149 }
150
151 spin_unlock_irqrestore(&pci_config_lock, flags);
152
153 return 0;
154}
155
156static struct pci_raw_ops pci_mmcfg = {
157 .read = pci_mmcfg_read,
158 .write = pci_mmcfg_write,
159};
160
Andi Kleend6ece542005-12-12 22:17:11 -0800161/* K8 systems have some devices (typically in the builtin northbridge)
162 that are only accessible using type1
163 Normally this can be expressed in the MCFG by not listing them
164 and assigning suitable _SEGs, but this isn't implemented in some BIOS.
165 Instead try to discover all devices on bus 0 that are unreachable using MM
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200166 and fallback for them. */
Andi Kleend6ece542005-12-12 22:17:11 -0800167static __init void unreachable_devices(void)
168{
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200169 int i, k;
Andi Kleend6ece542005-12-12 22:17:11 -0800170 unsigned long flags;
171
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200172 for (k = 0; k < MAX_CHECK_BUS; k++) {
173 for (i = 0; i < 32; i++) {
174 u32 val1;
175 u32 addr;
Andi Kleend6ece542005-12-12 22:17:11 -0800176
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200177 pci_conf1_read(0, k, PCI_DEVFN(i, 0), 0, 4, &val1);
178 if (val1 == 0xffffffff)
179 continue;
Andi Kleend6ece542005-12-12 22:17:11 -0800180
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200181 /* Locking probably not needed, but safer */
182 spin_lock_irqsave(&pci_config_lock, flags);
183 addr = get_base_addr(0, k, PCI_DEVFN(i, 0));
184 if (addr != 0)
185 pci_exp_set_dev_base(addr, k, PCI_DEVFN(i, 0));
186 if (addr == 0 ||
187 readl((u32 __iomem *)mmcfg_virt_addr) != val1) {
Daniel Ritzfd4dc272006-08-22 07:29:09 -0700188 set_bit(i + 32*k, fallback_slots);
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200189 printk(KERN_NOTICE
190 "PCI: No mmconfig possible on %x:%x\n", k, i);
191 }
192 spin_unlock_irqrestore(&pci_config_lock, flags);
193 }
Andi Kleend6ece542005-12-12 22:17:11 -0800194 }
195}
196
Andi Kleen5e544d62006-09-26 10:52:40 +0200197void __init pci_mmcfg_init(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Linus Torvalds79e453d2006-09-19 08:15:22 -0700199 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
Andi Kleen92c05fc2006-03-23 14:35:12 -0800200 return;
Greg Kroah-Hartman54549392005-06-23 17:35:56 -0700201
202 acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
203 if ((pci_mmcfg_config_num == 0) ||
204 (pci_mmcfg_config == NULL) ||
205 (pci_mmcfg_config[0].base_address == 0))
Andi Kleen92c05fc2006-03-23 14:35:12 -0800206 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Andi Kleen9abd7922006-09-26 10:52:40 +0200208 /* Only do this check when type 1 works. If it doesn't work
209 assume we run on a Mac and always use MCFG */
210 if (type == 1 && !e820_all_mapped(pci_mmcfg_config[0].base_address,
Linus Torvalds79e453d2006-09-19 08:15:22 -0700211 pci_mmcfg_config[0].base_address + MMCONFIG_APER_MIN,
212 E820_RESERVED)) {
213 printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %x is not E820-reserved\n",
214 pci_mmcfg_config[0].base_address);
215 printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
216 return;
217 }
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 printk(KERN_INFO "PCI: Using MMCONFIG\n");
220 raw_pci_ops = &pci_mmcfg;
221 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
222
Andi Kleend6ece542005-12-12 22:17:11 -0800223 unreachable_devices();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}