1 #include <linux/init.h>
3 #include <linux/topology.h>
5 #include <asm/pci_x86.h>
8 #include <asm/pci-direct.h>
9 #include <asm/mpspec.h>
10 #include <linux/cpumask.h>
14 * This discovers the pcibus <-> node mapping on AMD K8.
15 * also get peer root bus resource for io,mmio
24 static int mp_bus_to_node[BUS_NR];
26 void set_mp_bus_to_node(int busnum, int node)
28 if (busnum >= 0 && busnum < BUS_NR)
29 mp_bus_to_node[busnum] = node;
32 int get_mp_bus_to_node(int busnum)
36 if (busnum < 0 || busnum > (BUS_NR - 1))
39 node = mp_bus_to_node[busnum];
42 * let numa_node_id to decide it later in dma_alloc_pages
43 * if there is no ram on that node
45 if (node != -1 && !node_online(node))
51 #else /* CONFIG_X86_32 */
53 static unsigned char mp_bus_to_node[BUS_NR];
55 void set_mp_bus_to_node(int busnum, int node)
57 if (busnum >= 0 && busnum < BUS_NR)
58 mp_bus_to_node[busnum] = (unsigned char) node;
61 int get_mp_bus_to_node(int busnum)
65 if (busnum < 0 || busnum > (BUS_NR - 1))
67 node = mp_bus_to_node[busnum];
71 #endif /* CONFIG_X86_32 */
73 #endif /* CONFIG_NUMA */
78 * sub bus (transparent) will use entres from 3 to store extra from root,
79 * so need to make sure have enought slot there, increase PCI_BUS_NUM_RESOURCES?
82 struct pci_root_info {
85 struct resource res[RES_NUM];
92 /* 4 at this time, it may become to 32 */
94 static int pci_root_num;
95 static struct pci_root_info pci_root_info[PCI_ROOT_NR];
97 void set_pci_bus_resources_arch_default(struct pci_bus *b)
101 struct pci_root_info *info;
103 /* if only one root bus, don't need to anything */
104 if (pci_root_num < 2)
107 for (i = 0; i < pci_root_num; i++) {
108 if (pci_root_info[i].bus_min == b->number)
112 if (i == pci_root_num)
115 info = &pci_root_info[i];
116 for (j = 0; j < info->res_num; j++) {
117 struct resource *res;
118 struct resource *root;
121 b->resource[j] = res;
122 if (res->flags & IORESOURCE_IO)
123 root = &ioport_resource;
125 root = &iomem_resource;
126 insert_resource(root, res);
137 static void __init update_range(struct res_range *range, size_t start,
143 for (j = 0; j < RANGE_NUM; j++) {
147 if (start <= range[j].start && end >= range[j].end) {
153 if (start <= range[j].start && end < range[j].end && range[j].start < end + 1) {
154 range[j].start = end + 1;
159 if (start > range[j].start && end >= range[j].end && range[j].end > start - 1) {
160 range[j].end = start - 1;
164 if (start > range[j].start && end < range[j].end) {
165 /* find the new spare */
166 for (i = 0; i < RANGE_NUM; i++) {
167 if (range[i].end == 0)
171 range[i].end = range[j].end;
172 range[i].start = end + 1;
174 printk(KERN_ERR "run of slot in ranges\n");
176 range[j].end = start - 1;
182 static void __init update_res(struct pci_root_info *info, size_t start,
183 size_t end, unsigned long flags, int merge)
186 struct resource *res;
191 /* try to merge it with old one */
192 for (i = 0; i < info->res_num; i++) {
193 size_t final_start, final_end;
194 size_t common_start, common_end;
197 if (res->flags != flags)
200 common_start = max((size_t)res->start, start);
201 common_end = min((size_t)res->end, end);
202 if (common_start > common_end + 1)
205 final_start = min((size_t)res->start, start);
206 final_end = max((size_t)res->end, end);
208 res->start = final_start;
209 res->end = final_end;
215 /* need to add that */
216 if (info->res_num >= RES_NUM)
219 res = &info->res[info->res_num];
220 res->name = info->name;
228 struct pci_hostbridge_probe {
235 static struct pci_hostbridge_probe pci_probes[] __initdata = {
236 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
237 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
238 { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
239 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
242 static u64 __initdata fam10h_mmconf_start;
243 static u64 __initdata fam10h_mmconf_end;
244 static void __init get_pci_mmcfg_amd_fam10h_range(void)
248 unsigned segn_busn_bits;
250 /* assume all cpus from fam10h have mmconf */
251 if (boot_cpu_data.x86 < 0x10)
254 address = MSR_FAM10H_MMIO_CONF_BASE;
255 rdmsrl(address, msr);
257 /* mmconfig is not enable */
258 if (!(msr & FAM10H_MMIO_CONF_ENABLE))
261 base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
263 segn_busn_bits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
264 FAM10H_MMIO_CONF_BUSRANGE_MASK;
266 fam10h_mmconf_start = base;
267 fam10h_mmconf_end = base + (1ULL<<(segn_busn_bits + 20)) - 1;
271 * early_fill_mp_bus_to_node()
272 * called before pcibios_scan_root and pci_scan_bus
273 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
274 * Registers found in the K8 northbridge
276 static int __init early_fill_mp_bus_info(void)
287 struct pci_root_info *info;
289 struct resource *res;
292 struct res_range range[RANGE_NUM];
297 for (i = 0; i < BUS_NR; i++)
298 mp_bus_to_node[i] = -1;
301 if (!early_pci_allowed())
305 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
310 bus = pci_probes[i].bus;
311 slot = pci_probes[i].slot;
312 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
314 vendor = id & 0xffff;
315 device = (id>>16) & 0xffff;
316 if (pci_probes[i].vendor == vendor &&
317 pci_probes[i].device == device) {
327 for (i = 0; i < 4; i++) {
330 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
332 /* Check if that register is enabled for bus range */
336 min_bus = (reg >> 16) & 0xff;
337 max_bus = (reg >> 24) & 0xff;
338 node = (reg >> 4) & 0x07;
340 for (j = min_bus; j <= max_bus; j++)
341 mp_bus_to_node[j] = (unsigned char) node;
343 link = (reg >> 8) & 0x03;
345 info = &pci_root_info[pci_root_num];
346 info->bus_min = min_bus;
347 info->bus_max = max_bus;
350 sprintf(info->name, "PCI Bus #%02x", min_bus);
354 /* get the default node and link for left over res */
355 reg = read_pci_config(bus, slot, 0, 0x60);
356 def_node = (reg >> 8) & 0x07;
357 reg = read_pci_config(bus, slot, 0, 0x64);
358 def_link = (reg >> 8) & 0x03;
360 memset(range, 0, sizeof(range));
361 range[0].end = 0xffff;
362 /* io port resource */
363 for (i = 0; i < 4; i++) {
364 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
368 start = reg & 0xfff000;
369 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
371 link = (reg >> 4) & 0x03;
372 end = (reg & 0xfff000) | 0xfff;
374 /* find the position */
375 for (j = 0; j < pci_root_num; j++) {
376 info = &pci_root_info[j];
377 if (info->node == node && info->link == link)
380 if (j == pci_root_num)
381 continue; /* not found */
383 info = &pci_root_info[j];
384 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
385 node, link, (u64)start, (u64)end);
387 /* kernel only handle 16 bit only */
390 update_res(info, start, end, IORESOURCE_IO, 1);
391 update_range(range, start, end);
393 /* add left over io port range to def node/link, [0, 0xffff] */
394 /* find the position */
395 for (j = 0; j < pci_root_num; j++) {
396 info = &pci_root_info[j];
397 if (info->node == def_node && info->link == def_link)
400 if (j < pci_root_num) {
401 info = &pci_root_info[j];
402 for (i = 0; i < RANGE_NUM; i++) {
406 update_res(info, range[i].start, range[i].end,
411 memset(range, 0, sizeof(range));
412 /* 0xfd00000000-0xffffffffff for HT */
413 range[0].end = (0xfdULL<<32) - 1;
415 /* need to take out [0, TOM) for RAM*/
416 address = MSR_K8_TOP_MEM1;
417 rdmsrl(address, val);
418 end = (val & 0xffffff800000ULL);
419 printk(KERN_INFO "TOM: %016lx aka %ldM\n", end, end>>20);
420 if (end < (1ULL<<32))
421 update_range(range, 0, end - 1);
424 get_pci_mmcfg_amd_fam10h_range();
425 /* need to take out mmconf range */
426 if (fam10h_mmconf_end) {
427 printk(KERN_DEBUG "Fam 10h mmconf [%llx, %llx]\n", fam10h_mmconf_start, fam10h_mmconf_end);
428 update_range(range, fam10h_mmconf_start, fam10h_mmconf_end);
432 for (i = 0; i < 8; i++) {
433 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
437 start = reg & 0xffffff00; /* 39:16 on 31:8*/
439 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
441 link = (reg >> 4) & 0x03;
442 end = (reg & 0xffffff00);
446 /* find the position */
447 for (j = 0; j < pci_root_num; j++) {
448 info = &pci_root_info[j];
449 if (info->node == node && info->link == link)
452 if (j == pci_root_num)
453 continue; /* not found */
455 info = &pci_root_info[j];
457 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
458 node, link, (u64)start, (u64)end);
460 * some sick allocation would have range overlap with fam10h
461 * mmconf range, so need to update start and end.
463 if (fam10h_mmconf_end) {
466 if (start >= fam10h_mmconf_start &&
467 start <= fam10h_mmconf_end) {
468 start = fam10h_mmconf_end + 1;
472 if (end >= fam10h_mmconf_start &&
473 end <= fam10h_mmconf_end) {
474 end = fam10h_mmconf_start - 1;
478 if (start < fam10h_mmconf_start &&
479 end > fam10h_mmconf_end) {
481 endx = fam10h_mmconf_start - 1;
482 update_res(info, start, endx, IORESOURCE_MEM, 0);
483 update_range(range, start, endx);
484 printk(KERN_CONT " ==> [%llx, %llx]", (u64)start, endx);
485 start = fam10h_mmconf_end + 1;
490 printk(KERN_CONT " %s [%llx, %llx]", endx?"and":"==>", (u64)start, (u64)end);
492 printk(KERN_CONT "%s\n", endx?"":" ==> none");
498 update_res(info, start, end, IORESOURCE_MEM, 1);
499 update_range(range, start, end);
500 printk(KERN_CONT "\n");
503 /* need to take out [4G, TOM2) for RAM*/
505 address = MSR_K8_SYSCFG;
506 rdmsrl(address, val);
507 /* TOP_MEM2 is enabled? */
510 address = MSR_K8_TOP_MEM2;
511 rdmsrl(address, val);
512 end = (val & 0xffffff800000ULL);
513 printk(KERN_INFO "TOM2: %016lx aka %ldM\n", end, end>>20);
514 update_range(range, 1ULL<<32, end - 1);
518 * add left over mmio range to def node/link ?
519 * that is tricky, just record range in from start_min to 4G
521 for (j = 0; j < pci_root_num; j++) {
522 info = &pci_root_info[j];
523 if (info->node == def_node && info->link == def_link)
526 if (j < pci_root_num) {
527 info = &pci_root_info[j];
529 for (i = 0; i < RANGE_NUM; i++) {
533 update_res(info, range[i].start, range[i].end,
538 for (i = 0; i < pci_root_num; i++) {
542 info = &pci_root_info[i];
543 res_num = info->res_num;
544 busnum = info->bus_min;
545 printk(KERN_DEBUG "bus: [%02x,%02x] on node %x link %x\n",
546 info->bus_min, info->bus_max, info->node, info->link);
547 for (j = 0; j < res_num; j++) {
549 printk(KERN_DEBUG "bus: %02x index %x %s: [%llx, %llx]\n",
551 (res->flags & IORESOURCE_IO)?"io port":"mmio",
552 res->start, res->end);
559 #else /* !CONFIG_X86_64 */
561 static int __init early_fill_mp_bus_info(void) { return 0; }
563 #endif /* !CONFIG_X86_64 */
565 /* common 32/64 bit code */
567 #define ENABLE_CF8_EXT_CFG (1ULL << 46)
569 static void enable_pci_io_ecs(void *unused)
572 rdmsrl(MSR_AMD64_NB_CFG, reg);
573 if (!(reg & ENABLE_CF8_EXT_CFG)) {
574 reg |= ENABLE_CF8_EXT_CFG;
575 wrmsrl(MSR_AMD64_NB_CFG, reg);
579 static int __cpuinit amd_cpu_notify(struct notifier_block *self,
580 unsigned long action, void *hcpu)
582 int cpu = (long)hcpu;
585 case CPU_ONLINE_FROZEN:
586 smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
594 static struct notifier_block __cpuinitdata amd_cpu_notifier = {
595 .notifier_call = amd_cpu_notify,
598 static int __init pci_io_ecs_init(void)
602 /* assume all cpus from fam10h have IO ECS */
603 if (boot_cpu_data.x86 < 0x10)
606 register_cpu_notifier(&amd_cpu_notifier);
607 for_each_online_cpu(cpu)
608 amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
610 pci_probe |= PCI_HAS_IO_ECS;
615 static int __init amd_postcore_init(void)
617 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
620 early_fill_mp_bus_info();
626 postcore_initcall(amd_postcore_init);