blob: f77d7f8b9bf6ca53f4536e5a1d38a1135c3e2d58 [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
Arjan van de Ven946f2ee2006-04-07 19:49:30 +020018#define MMCONFIG_APER_SIZE (256*1024*1024)
19
Andi Kleen8c30b1a742006-04-07 19:50:12 +020020/* Assume systems with more busses have correct MCFG */
21#define MAX_CHECK_BUS 16
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG))
24
25/* The base address of the last MMCONFIG device accessed */
26static u32 mmcfg_last_accessed_device;
27
Andi Kleen8c30b1a742006-04-07 19:50:12 +020028static DECLARE_BITMAP(fallback_slots, MAX_CHECK_BUS*32);
Andi Kleend6ece542005-12-12 22:17:11 -080029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * Functions for accessing PCI configuration space with MMCONFIG accesses
32 */
Andi Kleend6ece542005-12-12 22:17:11 -080033static u32 get_base_addr(unsigned int seg, int bus, unsigned devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070035 int cfg_num = -1;
36 struct acpi_table_mcfg_config *cfg;
37
Andi Kleen8c30b1a742006-04-07 19:50:12 +020038 if (seg == 0 && bus < MAX_CHECK_BUS &&
39 test_bit(PCI_SLOT(devfn) + 32*bus, fallback_slots))
Andi Kleend6ece542005-12-12 22:17:11 -080040 return 0;
41
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070042 while (1) {
43 ++cfg_num;
44 if (cfg_num >= pci_mmcfg_config_num) {
Andi Kleen31030392006-01-27 02:03:50 +010045 break;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070046 }
47 cfg = &pci_mmcfg_config[cfg_num];
48 if (cfg->pci_segment_group_number != seg)
49 continue;
50 if ((cfg->start_bus_number <= bus) &&
51 (cfg->end_bus_number >= bus))
52 return cfg->base_address;
53 }
Andi Kleen31030392006-01-27 02:03:50 +010054
55 /* Handle more broken MCFG tables on Asus etc.
56 They only contain a single entry for bus 0-0. Assume
57 this applies to all busses. */
58 cfg = &pci_mmcfg_config[0];
59 if (pci_mmcfg_config_num == 1 &&
60 cfg->pci_segment_group_number == 0 &&
61 (cfg->start_bus_number | cfg->end_bus_number) == 0)
62 return cfg->base_address;
63
64 /* Fall back to type 0 */
65 return 0;
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070066}
67
Andi Kleen928cf8c2005-12-12 22:17:10 -080068static inline void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
Greg Kroah-Hartmand57e26c2005-06-23 17:35:56 -070069{
Andi Kleen928cf8c2005-12-12 22:17:10 -080070 u32 dev_base = base | (bus << 20) | (devfn << 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (dev_base != mmcfg_last_accessed_device) {
72 mmcfg_last_accessed_device = dev_base;
73 set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
74 }
75}
76
77static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
78 unsigned int devfn, int reg, int len, u32 *value)
79{
80 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -080081 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Andi Kleen49c93e82006-04-07 19:50:15 +020083 if (!value || (bus > 255) || (devfn > 255) || (reg > 4095)) {
84 *value = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return -EINVAL;
Andi Kleen49c93e82006-04-07 19:50:15 +020086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Andi Kleend6ece542005-12-12 22:17:11 -080088 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -080089 if (!base)
90 return pci_conf1_read(seg,bus,devfn,reg,len,value);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 spin_lock_irqsave(&pci_config_lock, flags);
93
Andi Kleen928cf8c2005-12-12 22:17:10 -080094 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 switch (len) {
97 case 1:
98 *value = readb(mmcfg_virt_addr + reg);
99 break;
100 case 2:
101 *value = readw(mmcfg_virt_addr + reg);
102 break;
103 case 4:
104 *value = readl(mmcfg_virt_addr + reg);
105 break;
106 }
107
108 spin_unlock_irqrestore(&pci_config_lock, flags);
109
110 return 0;
111}
112
113static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
114 unsigned int devfn, int reg, int len, u32 value)
115{
116 unsigned long flags;
Andi Kleen928cf8c2005-12-12 22:17:10 -0800117 u32 base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if ((bus > 255) || (devfn > 255) || (reg > 4095))
120 return -EINVAL;
121
Andi Kleend6ece542005-12-12 22:17:11 -0800122 base = get_base_addr(seg, bus, devfn);
Andi Kleen928cf8c2005-12-12 22:17:10 -0800123 if (!base)
124 return pci_conf1_write(seg,bus,devfn,reg,len,value);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 spin_lock_irqsave(&pci_config_lock, flags);
127
Andi Kleen928cf8c2005-12-12 22:17:10 -0800128 pci_exp_set_dev_base(base, bus, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 switch (len) {
131 case 1:
132 writeb(value, mmcfg_virt_addr + reg);
133 break;
134 case 2:
135 writew(value, mmcfg_virt_addr + reg);
136 break;
137 case 4:
138 writel(value, mmcfg_virt_addr + reg);
139 break;
140 }
141
142 spin_unlock_irqrestore(&pci_config_lock, flags);
143
144 return 0;
145}
146
147static struct pci_raw_ops pci_mmcfg = {
148 .read = pci_mmcfg_read,
149 .write = pci_mmcfg_write,
150};
151
Andi Kleend6ece542005-12-12 22:17:11 -0800152/* K8 systems have some devices (typically in the builtin northbridge)
153 that are only accessible using type1
154 Normally this can be expressed in the MCFG by not listing them
155 and assigning suitable _SEGs, but this isn't implemented in some BIOS.
156 Instead try to discover all devices on bus 0 that are unreachable using MM
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200157 and fallback for them. */
Andi Kleend6ece542005-12-12 22:17:11 -0800158static __init void unreachable_devices(void)
159{
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200160 int i, k;
Andi Kleend6ece542005-12-12 22:17:11 -0800161 unsigned long flags;
162
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200163 for (k = 0; k < MAX_CHECK_BUS; k++) {
164 for (i = 0; i < 32; i++) {
165 u32 val1;
166 u32 addr;
Andi Kleend6ece542005-12-12 22:17:11 -0800167
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200168 pci_conf1_read(0, k, PCI_DEVFN(i, 0), 0, 4, &val1);
169 if (val1 == 0xffffffff)
170 continue;
Andi Kleend6ece542005-12-12 22:17:11 -0800171
Andi Kleen8c30b1a742006-04-07 19:50:12 +0200172 /* Locking probably not needed, but safer */
173 spin_lock_irqsave(&pci_config_lock, flags);
174 addr = get_base_addr(0, k, PCI_DEVFN(i, 0));
175 if (addr != 0)
176 pci_exp_set_dev_base(addr, k, PCI_DEVFN(i, 0));
177 if (addr == 0 ||
178 readl((u32 __iomem *)mmcfg_virt_addr) != val1) {
179 set_bit(i, fallback_slots);
180 printk(KERN_NOTICE
181 "PCI: No mmconfig possible on %x:%x\n", k, i);
182 }
183 spin_unlock_irqrestore(&pci_config_lock, flags);
184 }
Andi Kleend6ece542005-12-12 22:17:11 -0800185 }
186}
187
Andi Kleen92c05fc2006-03-23 14:35:12 -0800188void __init pci_mmcfg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
Andi Kleen92c05fc2006-03-23 14:35:12 -0800191 return;
Greg Kroah-Hartman54549392005-06-23 17:35:56 -0700192
193 acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
194 if ((pci_mmcfg_config_num == 0) ||
195 (pci_mmcfg_config == NULL) ||
196 (pci_mmcfg_config[0].base_address == 0))
Andi Kleen92c05fc2006-03-23 14:35:12 -0800197 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Arjan van de Ven946f2ee2006-04-07 19:49:30 +0200199 if (!e820_all_mapped(pci_mmcfg_config[0].base_address,
200 pci_mmcfg_config[0].base_address + MMCONFIG_APER_SIZE,
201 E820_RESERVED)) {
202 printk(KERN_ERR "PCI: BIOS Bug: MCFG area is not E820-reserved\n");
203 printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
204 return;
205 }
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 printk(KERN_INFO "PCI: Using MMCONFIG\n");
208 raw_pci_ops = &pci_mmcfg;
209 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
210
Andi Kleend6ece542005-12-12 22:17:11 -0800211 unreachable_devices();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}