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 x86_pci_root_bus_res_quirks(struct pci_bus *b)
101 struct pci_root_info *info;
103 /* don't go for it if _CRS is used already */
104 if (b->resource[0] != &ioport_resource ||
105 b->resource[1] != &iomem_resource)
108 /* if only one root bus, don't need to anything */
109 if (pci_root_num < 2)
112 for (i = 0; i < pci_root_num; i++) {
113 if (pci_root_info[i].bus_min == b->number)
117 if (i == pci_root_num)
120 printk(KERN_DEBUG "PCI: peer root bus %02x res updated from pci conf\n",
123 info = &pci_root_info[i];
124 for (j = 0; j < info->res_num; j++) {
125 struct resource *res;
126 struct resource *root;
129 b->resource[j] = res;
130 if (res->flags & IORESOURCE_IO)
131 root = &ioport_resource;
133 root = &iomem_resource;
134 insert_resource(root, res);
145 static void __init update_range(struct res_range *range, size_t start,
151 for (j = 0; j < RANGE_NUM; j++) {
155 if (start <= range[j].start && end >= range[j].end) {
161 if (start <= range[j].start && end < range[j].end && range[j].start < end + 1) {
162 range[j].start = end + 1;
167 if (start > range[j].start && end >= range[j].end && range[j].end > start - 1) {
168 range[j].end = start - 1;
172 if (start > range[j].start && end < range[j].end) {
173 /* find the new spare */
174 for (i = 0; i < RANGE_NUM; i++) {
175 if (range[i].end == 0)
179 range[i].end = range[j].end;
180 range[i].start = end + 1;
182 printk(KERN_ERR "run of slot in ranges\n");
184 range[j].end = start - 1;
190 static void __init update_res(struct pci_root_info *info, size_t start,
191 size_t end, unsigned long flags, int merge)
194 struct resource *res;
199 /* try to merge it with old one */
200 for (i = 0; i < info->res_num; i++) {
201 size_t final_start, final_end;
202 size_t common_start, common_end;
205 if (res->flags != flags)
208 common_start = max((size_t)res->start, start);
209 common_end = min((size_t)res->end, end);
210 if (common_start > common_end + 1)
213 final_start = min((size_t)res->start, start);
214 final_end = max((size_t)res->end, end);
216 res->start = final_start;
217 res->end = final_end;
223 /* need to add that */
224 if (info->res_num >= RES_NUM)
227 res = &info->res[info->res_num];
228 res->name = info->name;
236 struct pci_hostbridge_probe {
243 static struct pci_hostbridge_probe pci_probes[] __initdata = {
244 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
245 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
246 { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
247 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
250 static u64 __initdata fam10h_mmconf_start;
251 static u64 __initdata fam10h_mmconf_end;
252 static void __init get_pci_mmcfg_amd_fam10h_range(void)
256 unsigned segn_busn_bits;
258 /* assume all cpus from fam10h have mmconf */
259 if (boot_cpu_data.x86 < 0x10)
262 address = MSR_FAM10H_MMIO_CONF_BASE;
263 rdmsrl(address, msr);
265 /* mmconfig is not enable */
266 if (!(msr & FAM10H_MMIO_CONF_ENABLE))
269 base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
271 segn_busn_bits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
272 FAM10H_MMIO_CONF_BUSRANGE_MASK;
274 fam10h_mmconf_start = base;
275 fam10h_mmconf_end = base + (1ULL<<(segn_busn_bits + 20)) - 1;
279 * early_fill_mp_bus_to_node()
280 * called before pcibios_scan_root and pci_scan_bus
281 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
282 * Registers found in the K8 northbridge
284 static int __init early_fill_mp_bus_info(void)
295 struct pci_root_info *info;
297 struct resource *res;
300 struct res_range range[RANGE_NUM];
305 for (i = 0; i < BUS_NR; i++)
306 mp_bus_to_node[i] = -1;
309 if (!early_pci_allowed())
313 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
318 bus = pci_probes[i].bus;
319 slot = pci_probes[i].slot;
320 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
322 vendor = id & 0xffff;
323 device = (id>>16) & 0xffff;
324 if (pci_probes[i].vendor == vendor &&
325 pci_probes[i].device == device) {
335 for (i = 0; i < 4; i++) {
338 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
340 /* Check if that register is enabled for bus range */
344 min_bus = (reg >> 16) & 0xff;
345 max_bus = (reg >> 24) & 0xff;
346 node = (reg >> 4) & 0x07;
348 for (j = min_bus; j <= max_bus; j++)
349 mp_bus_to_node[j] = (unsigned char) node;
351 link = (reg >> 8) & 0x03;
353 info = &pci_root_info[pci_root_num];
354 info->bus_min = min_bus;
355 info->bus_max = max_bus;
358 sprintf(info->name, "PCI Bus #%02x", min_bus);
362 /* get the default node and link for left over res */
363 reg = read_pci_config(bus, slot, 0, 0x60);
364 def_node = (reg >> 8) & 0x07;
365 reg = read_pci_config(bus, slot, 0, 0x64);
366 def_link = (reg >> 8) & 0x03;
368 memset(range, 0, sizeof(range));
369 range[0].end = 0xffff;
370 /* io port resource */
371 for (i = 0; i < 4; i++) {
372 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
376 start = reg & 0xfff000;
377 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
379 link = (reg >> 4) & 0x03;
380 end = (reg & 0xfff000) | 0xfff;
382 /* find the position */
383 for (j = 0; j < pci_root_num; j++) {
384 info = &pci_root_info[j];
385 if (info->node == node && info->link == link)
388 if (j == pci_root_num)
389 continue; /* not found */
391 info = &pci_root_info[j];
392 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
393 node, link, (u64)start, (u64)end);
395 /* kernel only handle 16 bit only */
398 update_res(info, start, end, IORESOURCE_IO, 1);
399 update_range(range, start, end);
401 /* add left over io port range to def node/link, [0, 0xffff] */
402 /* find the position */
403 for (j = 0; j < pci_root_num; j++) {
404 info = &pci_root_info[j];
405 if (info->node == def_node && info->link == def_link)
408 if (j < pci_root_num) {
409 info = &pci_root_info[j];
410 for (i = 0; i < RANGE_NUM; i++) {
414 update_res(info, range[i].start, range[i].end,
419 memset(range, 0, sizeof(range));
420 /* 0xfd00000000-0xffffffffff for HT */
421 range[0].end = (0xfdULL<<32) - 1;
423 /* need to take out [0, TOM) for RAM*/
424 address = MSR_K8_TOP_MEM1;
425 rdmsrl(address, val);
426 end = (val & 0xffffff800000ULL);
427 printk(KERN_INFO "TOM: %016lx aka %ldM\n", end, end>>20);
428 if (end < (1ULL<<32))
429 update_range(range, 0, end - 1);
432 get_pci_mmcfg_amd_fam10h_range();
433 /* need to take out mmconf range */
434 if (fam10h_mmconf_end) {
435 printk(KERN_DEBUG "Fam 10h mmconf [%llx, %llx]\n", fam10h_mmconf_start, fam10h_mmconf_end);
436 update_range(range, fam10h_mmconf_start, fam10h_mmconf_end);
440 for (i = 0; i < 8; i++) {
441 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
445 start = reg & 0xffffff00; /* 39:16 on 31:8*/
447 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
449 link = (reg >> 4) & 0x03;
450 end = (reg & 0xffffff00);
454 /* find the position */
455 for (j = 0; j < pci_root_num; j++) {
456 info = &pci_root_info[j];
457 if (info->node == node && info->link == link)
460 if (j == pci_root_num)
461 continue; /* not found */
463 info = &pci_root_info[j];
465 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
466 node, link, (u64)start, (u64)end);
468 * some sick allocation would have range overlap with fam10h
469 * mmconf range, so need to update start and end.
471 if (fam10h_mmconf_end) {
474 if (start >= fam10h_mmconf_start &&
475 start <= fam10h_mmconf_end) {
476 start = fam10h_mmconf_end + 1;
480 if (end >= fam10h_mmconf_start &&
481 end <= fam10h_mmconf_end) {
482 end = fam10h_mmconf_start - 1;
486 if (start < fam10h_mmconf_start &&
487 end > fam10h_mmconf_end) {
489 endx = fam10h_mmconf_start - 1;
490 update_res(info, start, endx, IORESOURCE_MEM, 0);
491 update_range(range, start, endx);
492 printk(KERN_CONT " ==> [%llx, %llx]", (u64)start, endx);
493 start = fam10h_mmconf_end + 1;
498 printk(KERN_CONT " %s [%llx, %llx]", endx?"and":"==>", (u64)start, (u64)end);
500 printk(KERN_CONT "%s\n", endx?"":" ==> none");
506 update_res(info, start, end, IORESOURCE_MEM, 1);
507 update_range(range, start, end);
508 printk(KERN_CONT "\n");
511 /* need to take out [4G, TOM2) for RAM*/
513 address = MSR_K8_SYSCFG;
514 rdmsrl(address, val);
515 /* TOP_MEM2 is enabled? */
518 address = MSR_K8_TOP_MEM2;
519 rdmsrl(address, val);
520 end = (val & 0xffffff800000ULL);
521 printk(KERN_INFO "TOM2: %016lx aka %ldM\n", end, end>>20);
522 update_range(range, 1ULL<<32, end - 1);
526 * add left over mmio range to def node/link ?
527 * that is tricky, just record range in from start_min to 4G
529 for (j = 0; j < pci_root_num; j++) {
530 info = &pci_root_info[j];
531 if (info->node == def_node && info->link == def_link)
534 if (j < pci_root_num) {
535 info = &pci_root_info[j];
537 for (i = 0; i < RANGE_NUM; i++) {
541 update_res(info, range[i].start, range[i].end,
546 for (i = 0; i < pci_root_num; i++) {
550 info = &pci_root_info[i];
551 res_num = info->res_num;
552 busnum = info->bus_min;
553 printk(KERN_DEBUG "bus: [%02x,%02x] on node %x link %x\n",
554 info->bus_min, info->bus_max, info->node, info->link);
555 for (j = 0; j < res_num; j++) {
557 printk(KERN_DEBUG "bus: %02x index %x %s: [%llx, %llx]\n",
559 (res->flags & IORESOURCE_IO)?"io port":"mmio",
560 res->start, res->end);
567 #else /* !CONFIG_X86_64 */
569 static int __init early_fill_mp_bus_info(void) { return 0; }
571 #endif /* !CONFIG_X86_64 */
573 /* common 32/64 bit code */
575 #define ENABLE_CF8_EXT_CFG (1ULL << 46)
577 static void enable_pci_io_ecs(void *unused)
580 rdmsrl(MSR_AMD64_NB_CFG, reg);
581 if (!(reg & ENABLE_CF8_EXT_CFG)) {
582 reg |= ENABLE_CF8_EXT_CFG;
583 wrmsrl(MSR_AMD64_NB_CFG, reg);
587 static int __cpuinit amd_cpu_notify(struct notifier_block *self,
588 unsigned long action, void *hcpu)
590 int cpu = (long)hcpu;
593 case CPU_ONLINE_FROZEN:
594 smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
602 static struct notifier_block __cpuinitdata amd_cpu_notifier = {
603 .notifier_call = amd_cpu_notify,
606 static int __init pci_io_ecs_init(void)
610 /* assume all cpus from fam10h have IO ECS */
611 if (boot_cpu_data.x86 < 0x10)
614 register_cpu_notifier(&amd_cpu_notifier);
615 for_each_online_cpu(cpu)
616 amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
618 pci_probe |= PCI_HAS_IO_ECS;
623 static int __init amd_postcore_init(void)
625 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
628 early_fill_mp_bus_info();
634 postcore_initcall(amd_postcore_init);