blob: 0567df3890e18932ae53fbb67c118e401ccb278f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/init.h>
2#include <linux/pci.h>
Robert Richterd199a042008-07-02 22:50:26 +02003#include <linux/topology.h>
Robert Richter91ede002008-08-22 20:23:38 +02004#include <linux/cpu.h>
Yinghai Lu27811d82010-02-10 01:20:07 -08005#include <linux/range.h>
6
Jan Beulich24d9b702011-01-10 16:20:23 +00007#include <asm/amd_nb.h>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +05308#include <asm/pci_x86.h>
Robert Richter3a27dd12008-06-12 20:19:23 +02009
Yinghai Lu871d5f82008-02-19 03:20:09 -080010#include <asm/pci-direct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Yinghai Lu99935a72009-10-04 21:54:24 -070012#include "bus_numa.h"
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/*
15 * This discovers the pcibus <-> node mapping on AMD K8.
Yinghai Lu30a18d62008-02-19 03:21:20 -080016 * also get peer root bus resource for io,mmio
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Yinghai Lu30a18d62008-02-19 03:21:20 -080019struct pci_hostbridge_probe {
20 u32 bus;
21 u32 slot;
22 u32 vendor;
23 u32 device;
24};
25
26static struct pci_hostbridge_probe pci_probes[] __initdata = {
27 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
28 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
29 { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
30 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
31};
32
Yinghai Lu27811d82010-02-10 01:20:07 -080033#define RANGE_NUM 16
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/**
Yinghai Lu871d5f82008-02-19 03:20:09 -080036 * early_fill_mp_bus_to_node()
37 * called before pcibios_scan_root and pci_scan_bus
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
39 * Registers found in the K8 northbridge
40 */
Yinghai Lu30a18d62008-02-19 03:21:20 -080041static int __init early_fill_mp_bus_info(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Yinghai Lu30a18d62008-02-19 03:21:20 -080043 int i;
44 int j;
45 unsigned bus;
Yinghai Lu871d5f82008-02-19 03:20:09 -080046 unsigned slot;
Yinghai Lu35ddd062008-02-19 03:15:08 -080047 int node;
Yinghai Lu30a18d62008-02-19 03:21:20 -080048 int link;
49 int def_node;
50 int def_link;
51 struct pci_root_info *info;
52 u32 reg;
53 struct resource *res;
Yinghai Lu97445c32010-02-10 01:20:10 -080054 u64 start;
55 u64 end;
Yinghai Lu27811d82010-02-10 01:20:07 -080056 struct range range[RANGE_NUM];
Yinghai Lu30a18d62008-02-19 03:21:20 -080057 u64 val;
58 u32 address;
Yinghai Lu3e3da002010-02-10 01:20:09 -080059 bool found;
Bjorn Helgaas24d25db2012-01-05 14:27:19 -070060 struct resource fam10h_mmconf_res, *fam10h_mmconf;
61 u64 fam10h_mmconf_start;
62 u64 fam10h_mmconf_end;
Yinghai Lu35ddd062008-02-19 03:15:08 -080063
Yinghai Lu871d5f82008-02-19 03:20:09 -080064 if (!early_pci_allowed())
65 return -1;
66
Yinghai Lu3e3da002010-02-10 01:20:09 -080067 found = false;
Yinghai Lu30a18d62008-02-19 03:21:20 -080068 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
69 u32 id;
70 u16 device;
71 u16 vendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Yinghai Lu30a18d62008-02-19 03:21:20 -080073 bus = pci_probes[i].bus;
74 slot = pci_probes[i].slot;
75 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
Yinghai Lu35ddd062008-02-19 03:15:08 -080076
Yinghai Lu30a18d62008-02-19 03:21:20 -080077 vendor = id & 0xffff;
78 device = (id>>16) & 0xffff;
79 if (pci_probes[i].vendor == vendor &&
80 pci_probes[i].device == device) {
Yinghai Lu3e3da002010-02-10 01:20:09 -080081 found = true;
Yinghai Lu30a18d62008-02-19 03:21:20 -080082 break;
83 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
Yinghai Lu3e3da002010-02-10 01:20:09 -080086 if (!found)
Yinghai Lu30a18d62008-02-19 03:21:20 -080087 return 0;
88
89 pci_root_num = 0;
90 for (i = 0; i < 4; i++) {
91 int min_bus;
92 int max_bus;
93 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
94
95 /* Check if that register is enabled for bus range */
96 if ((reg & 7) != 3)
97 continue;
98
99 min_bus = (reg >> 16) & 0xff;
100 max_bus = (reg >> 24) & 0xff;
101 node = (reg >> 4) & 0x07;
102#ifdef CONFIG_NUMA
103 for (j = min_bus; j <= max_bus; j++)
Jesse Barnes25470892009-07-10 14:04:30 -0700104 set_mp_bus_to_node(j, node);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800105#endif
106 link = (reg >> 8) & 0x03;
107
108 info = &pci_root_info[pci_root_num];
109 info->bus_min = min_bus;
110 info->bus_max = max_bus;
111 info->node = node;
112 info->link = link;
113 sprintf(info->name, "PCI Bus #%02x", min_bus);
114 pci_root_num++;
115 }
116
117 /* get the default node and link for left over res */
118 reg = read_pci_config(bus, slot, 0, 0x60);
119 def_node = (reg >> 8) & 0x07;
120 reg = read_pci_config(bus, slot, 0, 0x64);
121 def_link = (reg >> 8) & 0x03;
122
123 memset(range, 0, sizeof(range));
Yinghai Lue9a00642010-02-10 01:20:13 -0800124 add_range(range, RANGE_NUM, 0, 0, 0xffff + 1);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800125 /* io port resource */
126 for (i = 0; i < 4; i++) {
127 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
128 if (!(reg & 3))
129 continue;
130
131 start = reg & 0xfff000;
132 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
133 node = reg & 0x07;
134 link = (reg >> 4) & 0x03;
135 end = (reg & 0xfff000) | 0xfff;
136
137 /* find the position */
138 for (j = 0; j < pci_root_num; j++) {
139 info = &pci_root_info[j];
140 if (info->node == node && info->link == link)
141 break;
142 }
143 if (j == pci_root_num)
144 continue; /* not found */
145
146 info = &pci_root_info[j];
Yinghai Lu6e184f22008-03-06 01:15:31 -0800147 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
Yinghai Lu97445c32010-02-10 01:20:10 -0800148 node, link, start, end);
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700149
150 /* kernel only handle 16 bit only */
151 if (end > 0xffff)
152 end = 0xffff;
153 update_res(info, start, end, IORESOURCE_IO, 1);
Yinghai Lue9a00642010-02-10 01:20:13 -0800154 subtract_range(range, RANGE_NUM, start, end + 1);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800155 }
156 /* add left over io port range to def node/link, [0, 0xffff] */
157 /* find the position */
158 for (j = 0; j < pci_root_num; j++) {
159 info = &pci_root_info[j];
160 if (info->node == def_node && info->link == def_link)
161 break;
162 }
163 if (j < pci_root_num) {
164 info = &pci_root_info[j];
165 for (i = 0; i < RANGE_NUM; i++) {
166 if (!range[i].end)
167 continue;
168
Yinghai Lue9a00642010-02-10 01:20:13 -0800169 update_res(info, range[i].start, range[i].end - 1,
Yinghai Lu30a18d62008-02-19 03:21:20 -0800170 IORESOURCE_IO, 1);
171 }
172 }
173
174 memset(range, 0, sizeof(range));
175 /* 0xfd00000000-0xffffffffff for HT */
Yinghai Lue9a00642010-02-10 01:20:13 -0800176 end = cap_resource((0xfdULL<<32) - 1);
177 end++;
178 add_range(range, RANGE_NUM, 0, 0, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800179
180 /* need to take out [0, TOM) for RAM*/
181 address = MSR_K8_TOP_MEM1;
182 rdmsrl(address, val);
Yinghai Lu8004dd92008-05-12 17:40:39 -0700183 end = (val & 0xffffff800000ULL);
Yinghai Lu97445c32010-02-10 01:20:10 -0800184 printk(KERN_INFO "TOM: %016llx aka %lldM\n", end, end>>20);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800185 if (end < (1ULL<<32))
Yinghai Lue9a00642010-02-10 01:20:13 -0800186 subtract_range(range, RANGE_NUM, 0, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800187
Yinghai Lu6e184f22008-03-06 01:15:31 -0800188 /* get mmconfig */
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700189 fam10h_mmconf = amd_get_mmconfig_range(&fam10h_mmconf_res);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800190 /* need to take out mmconf range */
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700191 if (fam10h_mmconf) {
192 printk(KERN_DEBUG "Fam 10h mmconf %pR\n", fam10h_mmconf);
193 fam10h_mmconf_start = fam10h_mmconf->start;
194 fam10h_mmconf_end = fam10h_mmconf->end;
Yinghai Lue9a00642010-02-10 01:20:13 -0800195 subtract_range(range, RANGE_NUM, fam10h_mmconf_start,
196 fam10h_mmconf_end + 1);
Bjorn Helgaas24d25db2012-01-05 14:27:19 -0700197 } else {
198 fam10h_mmconf_start = 0;
199 fam10h_mmconf_end = 0;
Yinghai Lu6e184f22008-03-06 01:15:31 -0800200 }
201
Yinghai Lu30a18d62008-02-19 03:21:20 -0800202 /* mmio resource */
203 for (i = 0; i < 8; i++) {
204 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
205 if (!(reg & 3))
206 continue;
207
208 start = reg & 0xffffff00; /* 39:16 on 31:8*/
209 start <<= 8;
210 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
211 node = reg & 0x07;
212 link = (reg >> 4) & 0x03;
213 end = (reg & 0xffffff00);
214 end <<= 8;
215 end |= 0xffff;
216
217 /* find the position */
218 for (j = 0; j < pci_root_num; j++) {
219 info = &pci_root_info[j];
220 if (info->node == node && info->link == link)
221 break;
222 }
223 if (j == pci_root_num)
224 continue; /* not found */
225
226 info = &pci_root_info[j];
Yinghai Lu6e184f22008-03-06 01:15:31 -0800227
228 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
Yinghai Lu97445c32010-02-10 01:20:10 -0800229 node, link, start, end);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800230 /*
231 * some sick allocation would have range overlap with fam10h
232 * mmconf range, so need to update start and end.
233 */
234 if (fam10h_mmconf_end) {
235 int changed = 0;
236 u64 endx = 0;
237 if (start >= fam10h_mmconf_start &&
238 start <= fam10h_mmconf_end) {
239 start = fam10h_mmconf_end + 1;
240 changed = 1;
241 }
242
243 if (end >= fam10h_mmconf_start &&
244 end <= fam10h_mmconf_end) {
245 end = fam10h_mmconf_start - 1;
246 changed = 1;
247 }
248
249 if (start < fam10h_mmconf_start &&
250 end > fam10h_mmconf_end) {
251 /* we got a hole */
252 endx = fam10h_mmconf_start - 1;
253 update_res(info, start, endx, IORESOURCE_MEM, 0);
Yinghai Lue9a00642010-02-10 01:20:13 -0800254 subtract_range(range, RANGE_NUM, start,
255 endx + 1);
Yinghai Lu97445c32010-02-10 01:20:10 -0800256 printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800257 start = fam10h_mmconf_end + 1;
258 changed = 1;
259 }
260 if (changed) {
261 if (start <= end) {
Yinghai Lu97445c32010-02-10 01:20:10 -0800262 printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800263 } else {
264 printk(KERN_CONT "%s\n", endx?"":" ==> none");
265 continue;
266 }
267 }
268 }
269
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -0800270 update_res(info, cap_resource(start), cap_resource(end),
271 IORESOURCE_MEM, 1);
Yinghai Lue9a00642010-02-10 01:20:13 -0800272 subtract_range(range, RANGE_NUM, start, end + 1);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800273 printk(KERN_CONT "\n");
Yinghai Lu30a18d62008-02-19 03:21:20 -0800274 }
275
276 /* need to take out [4G, TOM2) for RAM*/
277 /* SYS_CFG */
278 address = MSR_K8_SYSCFG;
279 rdmsrl(address, val);
280 /* TOP_MEM2 is enabled? */
281 if (val & (1<<21)) {
282 /* TOP_MEM2 */
283 address = MSR_K8_TOP_MEM2;
284 rdmsrl(address, val);
Yinghai Lu8004dd92008-05-12 17:40:39 -0700285 end = (val & 0xffffff800000ULL);
Yinghai Lu97445c32010-02-10 01:20:10 -0800286 printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
Yinghai Lue9a00642010-02-10 01:20:13 -0800287 subtract_range(range, RANGE_NUM, 1ULL<<32, end);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800288 }
289
290 /*
291 * add left over mmio range to def node/link ?
292 * that is tricky, just record range in from start_min to 4G
293 */
294 for (j = 0; j < pci_root_num; j++) {
295 info = &pci_root_info[j];
296 if (info->node == def_node && info->link == def_link)
297 break;
298 }
299 if (j < pci_root_num) {
300 info = &pci_root_info[j];
301
302 for (i = 0; i < RANGE_NUM; i++) {
303 if (!range[i].end)
304 continue;
305
Yinghai Lu9ad3f2c2010-02-10 01:20:11 -0800306 update_res(info, cap_resource(range[i].start),
Yinghai Lue9a00642010-02-10 01:20:13 -0800307 cap_resource(range[i].end - 1),
Yinghai Lu30a18d62008-02-19 03:21:20 -0800308 IORESOURCE_MEM, 1);
309 }
310 }
311
Yinghai Lu30a18d62008-02-19 03:21:20 -0800312 for (i = 0; i < pci_root_num; i++) {
313 int res_num;
314 int busnum;
315
316 info = &pci_root_info[i];
317 res_num = info->res_num;
318 busnum = info->bus_min;
Yinghai Lu99935a72009-10-04 21:54:24 -0700319 printk(KERN_DEBUG "bus: [%02x, %02x] on node %x link %x\n",
Yinghai Lu30a18d62008-02-19 03:21:20 -0800320 info->bus_min, info->bus_max, info->node, info->link);
321 for (j = 0; j < res_num; j++) {
322 res = &info->res[j];
Yinghai Lu284f9332010-02-10 01:20:12 -0800323 printk(KERN_DEBUG "bus: %02x index %x %pR\n",
324 busnum, j, res);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800325 }
326 }
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329}
330
Robert Richter3a27dd12008-06-12 20:19:23 +0200331#define ENABLE_CF8_EXT_CFG (1ULL << 46)
332
Jan Beulich691269f2011-02-09 08:26:53 +0000333static void __cpuinit enable_pci_io_ecs(void *unused)
Robert Richter3a27dd12008-06-12 20:19:23 +0200334{
335 u64 reg;
336 rdmsrl(MSR_AMD64_NB_CFG, reg);
337 if (!(reg & ENABLE_CF8_EXT_CFG)) {
338 reg |= ENABLE_CF8_EXT_CFG;
339 wrmsrl(MSR_AMD64_NB_CFG, reg);
340 }
341}
342
Robert Richter91ede002008-08-22 20:23:38 +0200343static int __cpuinit amd_cpu_notify(struct notifier_block *self,
344 unsigned long action, void *hcpu)
Robert Richter3a27dd12008-06-12 20:19:23 +0200345{
Robert Richter91ede002008-08-22 20:23:38 +0200346 int cpu = (long)hcpu;
Robert Richtered217632008-08-22 20:23:38 +0200347 switch (action) {
Robert Richter91ede002008-08-22 20:23:38 +0200348 case CPU_ONLINE:
349 case CPU_ONLINE_FROZEN:
350 smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
351 break;
352 default:
353 break;
354 }
355 return NOTIFY_OK;
356}
357
358static struct notifier_block __cpuinitdata amd_cpu_notifier = {
359 .notifier_call = amd_cpu_notify,
360};
361
Jan Beulich24d9b702011-01-10 16:20:23 +0000362static void __init pci_enable_pci_io_ecs(void)
363{
364#ifdef CONFIG_AMD_NB
365 unsigned int i, n;
366
367 for (n = i = 0; !n && amd_nb_bus_dev_ranges[i].dev_limit; ++i) {
368 u8 bus = amd_nb_bus_dev_ranges[i].bus;
369 u8 slot = amd_nb_bus_dev_ranges[i].dev_base;
370 u8 limit = amd_nb_bus_dev_ranges[i].dev_limit;
371
372 for (; slot < limit; ++slot) {
373 u32 val = read_pci_config(bus, slot, 3, 0);
374
375 if (!early_is_amd_nb(val))
376 continue;
377
378 val = read_pci_config(bus, slot, 3, 0x8c);
379 if (!(val & (ENABLE_CF8_EXT_CFG >> 32))) {
380 val |= ENABLE_CF8_EXT_CFG >> 32;
381 write_pci_config(bus, slot, 3, 0x8c, val);
382 }
383 ++n;
384 }
385 }
Jan Beulich24d9b702011-01-10 16:20:23 +0000386#endif
387}
388
Robert Richter91ede002008-08-22 20:23:38 +0200389static int __init pci_io_ecs_init(void)
390{
391 int cpu;
392
Robert Richter3a27dd12008-06-12 20:19:23 +0200393 /* assume all cpus from fam10h have IO ECS */
394 if (boot_cpu_data.x86 < 0x10)
395 return 0;
Robert Richter91ede002008-08-22 20:23:38 +0200396
Jan Beulich24d9b702011-01-10 16:20:23 +0000397 /* Try the PCI method first. */
398 if (early_pci_allowed())
399 pci_enable_pci_io_ecs();
400
Robert Richter91ede002008-08-22 20:23:38 +0200401 register_cpu_notifier(&amd_cpu_notifier);
402 for_each_online_cpu(cpu)
403 amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
404 (void *)(long)cpu);
Robert Richter3a27dd12008-06-12 20:19:23 +0200405 pci_probe |= PCI_HAS_IO_ECS;
Robert Richter91ede002008-08-22 20:23:38 +0200406
Robert Richter3a27dd12008-06-12 20:19:23 +0200407 return 0;
408}
409
Robert Richter9b4e27b2008-08-22 20:23:37 +0200410static int __init amd_postcore_init(void)
411{
412 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
413 return 0;
414
415 early_fill_mp_bus_info();
Robert Richter91ede002008-08-22 20:23:38 +0200416 pci_io_ecs_init();
Robert Richter9b4e27b2008-08-22 20:23:37 +0200417
418 return 0;
419}
420
421postcore_initcall(amd_postcore_init);