1 #include <linux/init.h>
3 #include <linux/topology.h>
7 #include <asm/pci-direct.h>
8 #include <asm/mpspec.h>
9 #include <linux/cpumask.h>
13 * This discovers the pcibus <-> node mapping on AMD K8.
14 * also get peer root bus resource for io,mmio
23 static int mp_bus_to_node[BUS_NR];
25 void set_mp_bus_to_node(int busnum, int node)
27 if (busnum >= 0 && busnum < BUS_NR)
28 mp_bus_to_node[busnum] = node;
31 int get_mp_bus_to_node(int busnum)
35 if (busnum < 0 || busnum > (BUS_NR - 1))
38 node = mp_bus_to_node[busnum];
41 * let numa_node_id to decide it later in dma_alloc_pages
42 * if there is no ram on that node
44 if (node != -1 && !node_online(node))
50 #else /* CONFIG_X86_32 */
52 static unsigned char mp_bus_to_node[BUS_NR];
54 void set_mp_bus_to_node(int busnum, int node)
56 if (busnum >= 0 && busnum < BUS_NR)
57 mp_bus_to_node[busnum] = (unsigned char) node;
60 int get_mp_bus_to_node(int busnum)
64 if (busnum < 0 || busnum > (BUS_NR - 1))
66 node = mp_bus_to_node[busnum];
70 #endif /* CONFIG_X86_32 */
72 #endif /* CONFIG_NUMA */
77 * sub bus (transparent) will use entres from 3 to store extra from root,
78 * so need to make sure have enought slot there, increase PCI_BUS_NUM_RESOURCES?
81 struct pci_root_info {
84 struct resource res[RES_NUM];
91 /* 4 at this time, it may become to 32 */
93 static int pci_root_num;
94 static struct pci_root_info pci_root_info[PCI_ROOT_NR];
96 void set_pci_bus_resources_arch_default(struct pci_bus *b)
100 struct pci_root_info *info;
102 /* if only one root bus, don't need to anything */
103 if (pci_root_num < 2)
106 for (i = 0; i < pci_root_num; i++) {
107 if (pci_root_info[i].bus_min == b->number)
111 if (i == pci_root_num)
114 info = &pci_root_info[i];
115 for (j = 0; j < info->res_num; j++) {
116 struct resource *res;
117 struct resource *root;
120 b->resource[j] = res;
121 if (res->flags & IORESOURCE_IO)
122 root = &ioport_resource;
124 root = &iomem_resource;
125 insert_resource(root, res);
136 static void __init update_range(struct res_range *range, size_t start,
142 for (j = 0; j < RANGE_NUM; j++) {
146 if (start <= range[j].start && end >= range[j].end) {
152 if (start <= range[j].start && end < range[j].end && range[j].start < end + 1) {
153 range[j].start = end + 1;
158 if (start > range[j].start && end >= range[j].end && range[j].end > start - 1) {
159 range[j].end = start - 1;
163 if (start > range[j].start && end < range[j].end) {
164 /* find the new spare */
165 for (i = 0; i < RANGE_NUM; i++) {
166 if (range[i].end == 0)
170 range[i].end = range[j].end;
171 range[i].start = end + 1;
173 printk(KERN_ERR "run of slot in ranges\n");
175 range[j].end = start - 1;
181 static void __init update_res(struct pci_root_info *info, size_t start,
182 size_t end, unsigned long flags, int merge)
185 struct resource *res;
190 /* try to merge it with old one */
191 for (i = 0; i < info->res_num; i++) {
192 size_t final_start, final_end;
193 size_t common_start, common_end;
196 if (res->flags != flags)
199 common_start = max((size_t)res->start, start);
200 common_end = min((size_t)res->end, end);
201 if (common_start > common_end + 1)
204 final_start = min((size_t)res->start, start);
205 final_end = max((size_t)res->end, end);
207 res->start = final_start;
208 res->end = final_end;
214 /* need to add that */
215 if (info->res_num >= RES_NUM)
218 res = &info->res[info->res_num];
219 res->name = info->name;
227 struct pci_hostbridge_probe {
234 static struct pci_hostbridge_probe pci_probes[] __initdata = {
235 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
236 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
237 { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
238 { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
241 static u64 __initdata fam10h_mmconf_start;
242 static u64 __initdata fam10h_mmconf_end;
243 static void __init get_pci_mmcfg_amd_fam10h_range(void)
247 unsigned segn_busn_bits;
249 /* assume all cpus from fam10h have mmconf */
250 if (boot_cpu_data.x86 < 0x10)
253 address = MSR_FAM10H_MMIO_CONF_BASE;
254 rdmsrl(address, msr);
256 /* mmconfig is not enable */
257 if (!(msr & FAM10H_MMIO_CONF_ENABLE))
260 base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
262 segn_busn_bits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
263 FAM10H_MMIO_CONF_BUSRANGE_MASK;
265 fam10h_mmconf_start = base;
266 fam10h_mmconf_end = base + (1ULL<<(segn_busn_bits + 20)) - 1;
270 * early_fill_mp_bus_to_node()
271 * called before pcibios_scan_root and pci_scan_bus
272 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
273 * Registers found in the K8 northbridge
275 static int __init early_fill_mp_bus_info(void)
286 struct pci_root_info *info;
288 struct resource *res;
291 struct res_range range[RANGE_NUM];
296 for (i = 0; i < BUS_NR; i++)
297 mp_bus_to_node[i] = -1;
300 if (!early_pci_allowed())
304 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
309 bus = pci_probes[i].bus;
310 slot = pci_probes[i].slot;
311 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
313 vendor = id & 0xffff;
314 device = (id>>16) & 0xffff;
315 if (pci_probes[i].vendor == vendor &&
316 pci_probes[i].device == device) {
326 for (i = 0; i < 4; i++) {
329 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
331 /* Check if that register is enabled for bus range */
335 min_bus = (reg >> 16) & 0xff;
336 max_bus = (reg >> 24) & 0xff;
337 node = (reg >> 4) & 0x07;
339 for (j = min_bus; j <= max_bus; j++)
340 mp_bus_to_node[j] = (unsigned char) node;
342 link = (reg >> 8) & 0x03;
344 info = &pci_root_info[pci_root_num];
345 info->bus_min = min_bus;
346 info->bus_max = max_bus;
349 sprintf(info->name, "PCI Bus #%02x", min_bus);
353 /* get the default node and link for left over res */
354 reg = read_pci_config(bus, slot, 0, 0x60);
355 def_node = (reg >> 8) & 0x07;
356 reg = read_pci_config(bus, slot, 0, 0x64);
357 def_link = (reg >> 8) & 0x03;
359 memset(range, 0, sizeof(range));
360 range[0].end = 0xffff;
361 /* io port resource */
362 for (i = 0; i < 4; i++) {
363 reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
367 start = reg & 0xfff000;
368 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
370 link = (reg >> 4) & 0x03;
371 end = (reg & 0xfff000) | 0xfff;
373 /* find the position */
374 for (j = 0; j < pci_root_num; j++) {
375 info = &pci_root_info[j];
376 if (info->node == node && info->link == link)
379 if (j == pci_root_num)
380 continue; /* not found */
382 info = &pci_root_info[j];
383 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
384 node, link, (u64)start, (u64)end);
386 /* kernel only handle 16 bit only */
389 update_res(info, start, end, IORESOURCE_IO, 1);
390 update_range(range, start, end);
392 /* add left over io port range to def node/link, [0, 0xffff] */
393 /* find the position */
394 for (j = 0; j < pci_root_num; j++) {
395 info = &pci_root_info[j];
396 if (info->node == def_node && info->link == def_link)
399 if (j < pci_root_num) {
400 info = &pci_root_info[j];
401 for (i = 0; i < RANGE_NUM; i++) {
405 update_res(info, range[i].start, range[i].end,
410 memset(range, 0, sizeof(range));
411 /* 0xfd00000000-0xffffffffff for HT */
412 range[0].end = (0xfdULL<<32) - 1;
414 /* need to take out [0, TOM) for RAM*/
415 address = MSR_K8_TOP_MEM1;
416 rdmsrl(address, val);
417 end = (val & 0xffffff800000ULL);
418 printk(KERN_INFO "TOM: %016lx aka %ldM\n", end, end>>20);
419 if (end < (1ULL<<32))
420 update_range(range, 0, end - 1);
423 get_pci_mmcfg_amd_fam10h_range();
424 /* need to take out mmconf range */
425 if (fam10h_mmconf_end) {
426 printk(KERN_DEBUG "Fam 10h mmconf [%llx, %llx]\n", fam10h_mmconf_start, fam10h_mmconf_end);
427 update_range(range, fam10h_mmconf_start, fam10h_mmconf_end);
431 for (i = 0; i < 8; i++) {
432 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
436 start = reg & 0xffffff00; /* 39:16 on 31:8*/
438 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
440 link = (reg >> 4) & 0x03;
441 end = (reg & 0xffffff00);
445 /* find the position */
446 for (j = 0; j < pci_root_num; j++) {
447 info = &pci_root_info[j];
448 if (info->node == node && info->link == link)
451 if (j == pci_root_num)
452 continue; /* not found */
454 info = &pci_root_info[j];
456 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
457 node, link, (u64)start, (u64)end);
459 * some sick allocation would have range overlap with fam10h
460 * mmconf range, so need to update start and end.
462 if (fam10h_mmconf_end) {
465 if (start >= fam10h_mmconf_start &&
466 start <= fam10h_mmconf_end) {
467 start = fam10h_mmconf_end + 1;
471 if (end >= fam10h_mmconf_start &&
472 end <= fam10h_mmconf_end) {
473 end = fam10h_mmconf_start - 1;
477 if (start < fam10h_mmconf_start &&
478 end > fam10h_mmconf_end) {
480 endx = fam10h_mmconf_start - 1;
481 update_res(info, start, endx, IORESOURCE_MEM, 0);
482 update_range(range, start, endx);
483 printk(KERN_CONT " ==> [%llx, %llx]", (u64)start, endx);
484 start = fam10h_mmconf_end + 1;
489 printk(KERN_CONT " %s [%llx, %llx]", endx?"and":"==>", (u64)start, (u64)end);
491 printk(KERN_CONT "%s\n", endx?"":" ==> none");
497 update_res(info, start, end, IORESOURCE_MEM, 1);
498 update_range(range, start, end);
499 printk(KERN_CONT "\n");
502 /* need to take out [4G, TOM2) for RAM*/
504 address = MSR_K8_SYSCFG;
505 rdmsrl(address, val);
506 /* TOP_MEM2 is enabled? */
509 address = MSR_K8_TOP_MEM2;
510 rdmsrl(address, val);
511 end = (val & 0xffffff800000ULL);
512 printk(KERN_INFO "TOM2: %016lx aka %ldM\n", end, end>>20);
513 update_range(range, 1ULL<<32, end - 1);
517 * add left over mmio range to def node/link ?
518 * that is tricky, just record range in from start_min to 4G
520 for (j = 0; j < pci_root_num; j++) {
521 info = &pci_root_info[j];
522 if (info->node == def_node && info->link == def_link)
525 if (j < pci_root_num) {
526 info = &pci_root_info[j];
528 for (i = 0; i < RANGE_NUM; i++) {
532 update_res(info, range[i].start, range[i].end,
537 for (i = 0; i < pci_root_num; i++) {
541 info = &pci_root_info[i];
542 res_num = info->res_num;
543 busnum = info->bus_min;
544 printk(KERN_DEBUG "bus: [%02x,%02x] on node %x link %x\n",
545 info->bus_min, info->bus_max, info->node, info->link);
546 for (j = 0; j < res_num; j++) {
548 printk(KERN_DEBUG "bus: %02x index %x %s: [%llx, %llx]\n",
550 (res->flags & IORESOURCE_IO)?"io port":"mmio",
551 res->start, res->end);
558 postcore_initcall(early_fill_mp_bus_info);
562 /* common 32/64 bit code */
564 #define ENABLE_CF8_EXT_CFG (1ULL << 46)
566 static void enable_pci_io_ecs_per_cpu(void *unused)
569 rdmsrl(MSR_AMD64_NB_CFG, reg);
570 if (!(reg & ENABLE_CF8_EXT_CFG)) {
571 reg |= ENABLE_CF8_EXT_CFG;
572 wrmsrl(MSR_AMD64_NB_CFG, reg);
576 static int __init enable_pci_io_ecs(void)
578 /* assume all cpus from fam10h have IO ECS */
579 if (boot_cpu_data.x86 < 0x10)
581 on_each_cpu(enable_pci_io_ecs_per_cpu, NULL, 1, 1);
582 pci_probe |= PCI_HAS_IO_ECS;
586 postcore_initcall(enable_pci_io_ecs);