blob: 465b3a6f1c12479f172a44342e6718b93b5bc73f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/init.h>
2#include <linux/pci.h>
Robert Richter3a27dd12008-06-12 20:19:23 +02003#include "pci.h"
4
5#ifdef CONFIG_X86_64
6
Yinghai Lu871d5f82008-02-19 03:20:09 -08007#include <asm/pci-direct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <asm/mpspec.h>
9#include <linux/cpumask.h>
Yinghai Lu871d5f82008-02-19 03:20:09 -080010#include <linux/topology.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12/*
13 * This discovers the pcibus <-> node mapping on AMD K8.
Yinghai Lu30a18d62008-02-19 03:21:20 -080014 * also get peer root bus resource for io,mmio
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
Yinghai Lu871d5f82008-02-19 03:20:09 -080017#ifdef CONFIG_NUMA
18
19#define BUS_NR 256
20
21static int mp_bus_to_node[BUS_NR];
22
23void set_mp_bus_to_node(int busnum, int node)
24{
25 if (busnum >= 0 && busnum < BUS_NR)
26 mp_bus_to_node[busnum] = node;
27}
28
29int get_mp_bus_to_node(int busnum)
30{
31 int node = -1;
32
33 if (busnum < 0 || busnum > (BUS_NR - 1))
34 return node;
35
36 node = mp_bus_to_node[busnum];
37
38 /*
39 * let numa_node_id to decide it later in dma_alloc_pages
40 * if there is no ram on that node
41 */
42 if (node != -1 && !node_online(node))
43 node = -1;
44
45 return node;
46}
Robert Richter42a4b422008-07-02 22:50:25 +020047
Yinghai Lu871d5f82008-02-19 03:20:09 -080048#endif
49
Robert Richter42a4b422008-07-02 22:50:25 +020050/*
51 * sub bus (transparent) will use entres from 3 to store extra from root,
52 * so need to make sure have enought slot there, increase PCI_BUS_NUM_RESOURCES?
53 */
54#define RES_NUM 16
55struct pci_root_info {
56 char name[12];
57 unsigned int res_num;
58 struct resource res[RES_NUM];
59 int bus_min;
60 int bus_max;
61 int node;
62 int link;
63};
64
65/* 4 at this time, it may become to 32 */
66#define PCI_ROOT_NR 4
67static int pci_root_num;
68static struct pci_root_info pci_root_info[PCI_ROOT_NR];
69
Yinghai Lu30a18d62008-02-19 03:21:20 -080070void set_pci_bus_resources_arch_default(struct pci_bus *b)
71{
72 int i;
73 int j;
74 struct pci_root_info *info;
75
Yinghai Lu4cf19462008-04-11 15:14:52 -070076 /* if only one root bus, don't need to anything */
77 if (pci_root_num < 2)
Yinghai Lu30a18d62008-02-19 03:21:20 -080078 return;
79
80 for (i = 0; i < pci_root_num; i++) {
81 if (pci_root_info[i].bus_min == b->number)
82 break;
83 }
84
85 if (i == pci_root_num)
86 return;
87
88 info = &pci_root_info[i];
89 for (j = 0; j < info->res_num; j++) {
90 struct resource *res;
91 struct resource *root;
92
93 res = &info->res[j];
94 b->resource[j] = res;
95 if (res->flags & IORESOURCE_IO)
96 root = &ioport_resource;
97 else
98 root = &iomem_resource;
99 insert_resource(root, res);
100 }
101}
102
103#define RANGE_NUM 16
104
105struct res_range {
106 size_t start;
107 size_t end;
108};
109
110static void __init update_range(struct res_range *range, size_t start,
111 size_t end)
112{
113 int i;
114 int j;
115
116 for (j = 0; j < RANGE_NUM; j++) {
117 if (!range[j].end)
118 continue;
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700119
120 if (start <= range[j].start && end >= range[j].end) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800121 range[j].start = 0;
122 range[j].end = 0;
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700123 continue;
124 }
125
126 if (start <= range[j].start && end < range[j].end && range[j].start < end + 1) {
127 range[j].start = end + 1;
128 continue;
129 }
130
131
132 if (start > range[j].start && end >= range[j].end && range[j].end > start - 1) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800133 range[j].end = start - 1;
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700134 continue;
135 }
136
137 if (start > range[j].start && end < range[j].end) {
Yinghai Lu30a18d62008-02-19 03:21:20 -0800138 /* find the new spare */
139 for (i = 0; i < RANGE_NUM; i++) {
140 if (range[i].end == 0)
141 break;
142 }
143 if (i < RANGE_NUM) {
144 range[i].end = range[j].end;
145 range[i].start = end + 1;
146 } else {
147 printk(KERN_ERR "run of slot in ranges\n");
148 }
149 range[j].end = start - 1;
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700150 continue;
Yinghai Lu30a18d62008-02-19 03:21:20 -0800151 }
152 }
153}
154
155static void __init update_res(struct pci_root_info *info, size_t start,
156 size_t end, unsigned long flags, int merge)
157{
158 int i;
159 struct resource *res;
160
161 if (!merge)
162 goto addit;
163
164 /* try to merge it with old one */
165 for (i = 0; i < info->res_num; i++) {
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700166 size_t final_start, final_end;
167 size_t common_start, common_end;
168
Yinghai Lu30a18d62008-02-19 03:21:20 -0800169 res = &info->res[i];
170 if (res->flags != flags)
171 continue;
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700172
173 common_start = max((size_t)res->start, start);
174 common_end = min((size_t)res->end, end);
175 if (common_start > common_end + 1)
176 continue;
177
178 final_start = min((size_t)res->start, start);
179 final_end = max((size_t)res->end, end);
180
181 res->start = final_start;
182 res->end = final_end;
183 return;
Yinghai Lu30a18d62008-02-19 03:21:20 -0800184 }
185
186addit:
187
188 /* need to add that */
189 if (info->res_num >= RES_NUM)
190 return;
191
192 res = &info->res[info->res_num];
193 res->name = info->name;
194 res->flags = flags;
195 res->start = start;
196 res->end = end;
197 res->child = NULL;
198 info->res_num++;
199}
200
201struct pci_hostbridge_probe {
202 u32 bus;
203 u32 slot;
204 u32 vendor;
205 u32 device;
206};
207
208static struct pci_hostbridge_probe pci_probes[] __initdata = {
209 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
210 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
211 { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
212 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
213};
214
Yinghai Lu6e184f22008-03-06 01:15:31 -0800215static u64 __initdata fam10h_mmconf_start;
216static u64 __initdata fam10h_mmconf_end;
217static void __init get_pci_mmcfg_amd_fam10h_range(void)
218{
219 u32 address;
220 u64 base, msr;
221 unsigned segn_busn_bits;
222
223 /* assume all cpus from fam10h have mmconf */
224 if (boot_cpu_data.x86 < 0x10)
225 return;
226
227 address = MSR_FAM10H_MMIO_CONF_BASE;
228 rdmsrl(address, msr);
229
230 /* mmconfig is not enable */
231 if (!(msr & FAM10H_MMIO_CONF_ENABLE))
232 return;
233
234 base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
235
236 segn_busn_bits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
237 FAM10H_MMIO_CONF_BUSRANGE_MASK;
238
239 fam10h_mmconf_start = base;
240 fam10h_mmconf_end = base + (1ULL<<(segn_busn_bits + 20)) - 1;
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/**
Yinghai Lu871d5f82008-02-19 03:20:09 -0800244 * early_fill_mp_bus_to_node()
245 * called before pcibios_scan_root and pci_scan_bus
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
247 * Registers found in the K8 northbridge
248 */
Yinghai Lu30a18d62008-02-19 03:21:20 -0800249static int __init early_fill_mp_bus_info(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Yinghai Lu30a18d62008-02-19 03:21:20 -0800251 int i;
252 int j;
253 unsigned bus;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800254 unsigned slot;
Yinghai Lu30a18d62008-02-19 03:21:20 -0800255 int found;
Yinghai Lu35ddd062008-02-19 03:15:08 -0800256 int node;
Yinghai Lu30a18d62008-02-19 03:21:20 -0800257 int link;
258 int def_node;
259 int def_link;
260 struct pci_root_info *info;
261 u32 reg;
262 struct resource *res;
263 size_t start;
264 size_t end;
265 struct res_range range[RANGE_NUM];
266 u64 val;
267 u32 address;
Yinghai Lu35ddd062008-02-19 03:15:08 -0800268
Yinghai Lu30a18d62008-02-19 03:21:20 -0800269#ifdef CONFIG_NUMA
Yinghai Lu871d5f82008-02-19 03:20:09 -0800270 for (i = 0; i < BUS_NR; i++)
271 mp_bus_to_node[i] = -1;
Yinghai Lu30a18d62008-02-19 03:21:20 -0800272#endif
Yinghai Lu871d5f82008-02-19 03:20:09 -0800273
274 if (!early_pci_allowed())
275 return -1;
276
Yinghai Lu30a18d62008-02-19 03:21:20 -0800277 found = 0;
278 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
279 u32 id;
280 u16 device;
281 u16 vendor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Yinghai Lu30a18d62008-02-19 03:21:20 -0800283 bus = pci_probes[i].bus;
284 slot = pci_probes[i].slot;
285 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
Yinghai Lu35ddd062008-02-19 03:15:08 -0800286
Yinghai Lu30a18d62008-02-19 03:21:20 -0800287 vendor = id & 0xffff;
288 device = (id>>16) & 0xffff;
289 if (pci_probes[i].vendor == vendor &&
290 pci_probes[i].device == device) {
291 found = 1;
292 break;
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
Yinghai Lu30a18d62008-02-19 03:21:20 -0800296 if (!found)
297 return 0;
298
299 pci_root_num = 0;
300 for (i = 0; i < 4; i++) {
301 int min_bus;
302 int max_bus;
303 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
304
305 /* Check if that register is enabled for bus range */
306 if ((reg & 7) != 3)
307 continue;
308
309 min_bus = (reg >> 16) & 0xff;
310 max_bus = (reg >> 24) & 0xff;
311 node = (reg >> 4) & 0x07;
312#ifdef CONFIG_NUMA
313 for (j = min_bus; j <= max_bus; j++)
314 mp_bus_to_node[j] = (unsigned char) node;
315#endif
316 link = (reg >> 8) & 0x03;
317
318 info = &pci_root_info[pci_root_num];
319 info->bus_min = min_bus;
320 info->bus_max = max_bus;
321 info->node = node;
322 info->link = link;
323 sprintf(info->name, "PCI Bus #%02x", min_bus);
324 pci_root_num++;
325 }
326
327 /* get the default node and link for left over res */
328 reg = read_pci_config(bus, slot, 0, 0x60);
329 def_node = (reg >> 8) & 0x07;
330 reg = read_pci_config(bus, slot, 0, 0x64);
331 def_link = (reg >> 8) & 0x03;
332
333 memset(range, 0, sizeof(range));
334 range[0].end = 0xffff;
335 /* io port resource */
336 for (i = 0; i < 4; i++) {
337 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
338 if (!(reg & 3))
339 continue;
340
341 start = reg & 0xfff000;
342 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
343 node = reg & 0x07;
344 link = (reg >> 4) & 0x03;
345 end = (reg & 0xfff000) | 0xfff;
346
347 /* find the position */
348 for (j = 0; j < pci_root_num; j++) {
349 info = &pci_root_info[j];
350 if (info->node == node && info->link == link)
351 break;
352 }
353 if (j == pci_root_num)
354 continue; /* not found */
355
356 info = &pci_root_info[j];
Yinghai Lu6e184f22008-03-06 01:15:31 -0800357 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
358 node, link, (u64)start, (u64)end);
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700359
360 /* kernel only handle 16 bit only */
361 if (end > 0xffff)
362 end = 0xffff;
363 update_res(info, start, end, IORESOURCE_IO, 1);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800364 update_range(range, start, end);
365 }
366 /* add left over io port range to def node/link, [0, 0xffff] */
367 /* find the position */
368 for (j = 0; j < pci_root_num; j++) {
369 info = &pci_root_info[j];
370 if (info->node == def_node && info->link == def_link)
371 break;
372 }
373 if (j < pci_root_num) {
374 info = &pci_root_info[j];
375 for (i = 0; i < RANGE_NUM; i++) {
376 if (!range[i].end)
377 continue;
378
379 update_res(info, range[i].start, range[i].end,
380 IORESOURCE_IO, 1);
381 }
382 }
383
384 memset(range, 0, sizeof(range));
385 /* 0xfd00000000-0xffffffffff for HT */
Yinghai Lu6e184f22008-03-06 01:15:31 -0800386 range[0].end = (0xfdULL<<32) - 1;
Yinghai Lu30a18d62008-02-19 03:21:20 -0800387
388 /* need to take out [0, TOM) for RAM*/
389 address = MSR_K8_TOP_MEM1;
390 rdmsrl(address, val);
Yinghai Lu8004dd92008-05-12 17:40:39 -0700391 end = (val & 0xffffff800000ULL);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800392 printk(KERN_INFO "TOM: %016lx aka %ldM\n", end, end>>20);
393 if (end < (1ULL<<32))
394 update_range(range, 0, end - 1);
395
Yinghai Lu6e184f22008-03-06 01:15:31 -0800396 /* get mmconfig */
397 get_pci_mmcfg_amd_fam10h_range();
398 /* need to take out mmconf range */
399 if (fam10h_mmconf_end) {
400 printk(KERN_DEBUG "Fam 10h mmconf [%llx, %llx]\n", fam10h_mmconf_start, fam10h_mmconf_end);
401 update_range(range, fam10h_mmconf_start, fam10h_mmconf_end);
402 }
403
Yinghai Lu30a18d62008-02-19 03:21:20 -0800404 /* mmio resource */
405 for (i = 0; i < 8; i++) {
406 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
407 if (!(reg & 3))
408 continue;
409
410 start = reg & 0xffffff00; /* 39:16 on 31:8*/
411 start <<= 8;
412 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
413 node = reg & 0x07;
414 link = (reg >> 4) & 0x03;
415 end = (reg & 0xffffff00);
416 end <<= 8;
417 end |= 0xffff;
418
419 /* find the position */
420 for (j = 0; j < pci_root_num; j++) {
421 info = &pci_root_info[j];
422 if (info->node == node && info->link == link)
423 break;
424 }
425 if (j == pci_root_num)
426 continue; /* not found */
427
428 info = &pci_root_info[j];
Yinghai Lu6e184f22008-03-06 01:15:31 -0800429
430 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
431 node, link, (u64)start, (u64)end);
432 /*
433 * some sick allocation would have range overlap with fam10h
434 * mmconf range, so need to update start and end.
435 */
436 if (fam10h_mmconf_end) {
437 int changed = 0;
438 u64 endx = 0;
439 if (start >= fam10h_mmconf_start &&
440 start <= fam10h_mmconf_end) {
441 start = fam10h_mmconf_end + 1;
442 changed = 1;
443 }
444
445 if (end >= fam10h_mmconf_start &&
446 end <= fam10h_mmconf_end) {
447 end = fam10h_mmconf_start - 1;
448 changed = 1;
449 }
450
451 if (start < fam10h_mmconf_start &&
452 end > fam10h_mmconf_end) {
453 /* we got a hole */
454 endx = fam10h_mmconf_start - 1;
455 update_res(info, start, endx, IORESOURCE_MEM, 0);
456 update_range(range, start, endx);
457 printk(KERN_CONT " ==> [%llx, %llx]", (u64)start, endx);
458 start = fam10h_mmconf_end + 1;
459 changed = 1;
460 }
461 if (changed) {
462 if (start <= end) {
463 printk(KERN_CONT " %s [%llx, %llx]", endx?"and":"==>", (u64)start, (u64)end);
464 } else {
465 printk(KERN_CONT "%s\n", endx?"":" ==> none");
466 continue;
467 }
468 }
469 }
470
Yinghai Lue8ee6f02008-04-13 01:41:58 -0700471 update_res(info, start, end, IORESOURCE_MEM, 1);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800472 update_range(range, start, end);
Yinghai Lu6e184f22008-03-06 01:15:31 -0800473 printk(KERN_CONT "\n");
Yinghai Lu30a18d62008-02-19 03:21:20 -0800474 }
475
476 /* need to take out [4G, TOM2) for RAM*/
477 /* SYS_CFG */
478 address = MSR_K8_SYSCFG;
479 rdmsrl(address, val);
480 /* TOP_MEM2 is enabled? */
481 if (val & (1<<21)) {
482 /* TOP_MEM2 */
483 address = MSR_K8_TOP_MEM2;
484 rdmsrl(address, val);
Yinghai Lu8004dd92008-05-12 17:40:39 -0700485 end = (val & 0xffffff800000ULL);
Yinghai Lu30a18d62008-02-19 03:21:20 -0800486 printk(KERN_INFO "TOM2: %016lx aka %ldM\n", end, end>>20);
487 update_range(range, 1ULL<<32, end - 1);
488 }
489
490 /*
491 * add left over mmio range to def node/link ?
492 * that is tricky, just record range in from start_min to 4G
493 */
494 for (j = 0; j < pci_root_num; j++) {
495 info = &pci_root_info[j];
496 if (info->node == def_node && info->link == def_link)
497 break;
498 }
499 if (j < pci_root_num) {
500 info = &pci_root_info[j];
501
502 for (i = 0; i < RANGE_NUM; i++) {
503 if (!range[i].end)
504 continue;
505
506 update_res(info, range[i].start, range[i].end,
507 IORESOURCE_MEM, 1);
508 }
509 }
510
Yinghai Lu30a18d62008-02-19 03:21:20 -0800511 for (i = 0; i < pci_root_num; i++) {
512 int res_num;
513 int busnum;
514
515 info = &pci_root_info[i];
516 res_num = info->res_num;
517 busnum = info->bus_min;
518 printk(KERN_DEBUG "bus: [%02x,%02x] on node %x link %x\n",
519 info->bus_min, info->bus_max, info->node, info->link);
520 for (j = 0; j < res_num; j++) {
521 res = &info->res[j];
522 printk(KERN_DEBUG "bus: %02x index %x %s: [%llx, %llx]\n",
523 busnum, j,
524 (res->flags & IORESOURCE_IO)?"io port":"mmio",
525 res->start, res->end);
526 }
527 }
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 0;
530}
531
Yinghai Lu30a18d62008-02-19 03:21:20 -0800532postcore_initcall(early_fill_mp_bus_info);
Robert Richter3a27dd12008-06-12 20:19:23 +0200533
534#endif
535
536/* common 32/64 bit code */
537
538#define ENABLE_CF8_EXT_CFG (1ULL << 46)
539
540static void enable_pci_io_ecs_per_cpu(void *unused)
541{
542 u64 reg;
543 rdmsrl(MSR_AMD64_NB_CFG, reg);
544 if (!(reg & ENABLE_CF8_EXT_CFG)) {
545 reg |= ENABLE_CF8_EXT_CFG;
546 wrmsrl(MSR_AMD64_NB_CFG, reg);
547 }
548}
549
550static int __init enable_pci_io_ecs(void)
551{
552 /* assume all cpus from fam10h have IO ECS */
553 if (boot_cpu_data.x86 < 0x10)
554 return 0;
555 on_each_cpu(enable_pci_io_ecs_per_cpu, NULL, 1, 1);
556 pci_probe |= PCI_HAS_IO_ECS;
557 return 0;
558}
559
560postcore_initcall(enable_pci_io_ecs);