1 #include <linux/init.h>
7 #include <asm/pci-direct.h>
8 #include <asm/mpspec.h>
9 #include <linux/cpumask.h>
10 #include <linux/topology.h>
13 * This discovers the pcibus <-> node mapping on AMD K8.
14 * also get peer root bus resource for io,mmio
19 * sub bus (transparent) will use entres from 3 to store extra from root,
20 * so need to make sure have enought slot there, increase PCI_BUS_NUM_RESOURCES?
23 struct pci_root_info {
26 struct resource res[RES_NUM];
33 /* 4 at this time, it may become to 32 */
35 static int pci_root_num;
36 static struct pci_root_info pci_root_info[PCI_ROOT_NR];
42 static int mp_bus_to_node[BUS_NR];
44 void set_mp_bus_to_node(int busnum, int node)
46 if (busnum >= 0 && busnum < BUS_NR)
47 mp_bus_to_node[busnum] = node;
50 int get_mp_bus_to_node(int busnum)
54 if (busnum < 0 || busnum > (BUS_NR - 1))
57 node = mp_bus_to_node[busnum];
60 * let numa_node_id to decide it later in dma_alloc_pages
61 * if there is no ram on that node
63 if (node != -1 && !node_online(node))
70 void set_pci_bus_resources_arch_default(struct pci_bus *b)
74 struct pci_root_info *info;
76 /* if only one root bus, don't need to anything */
80 for (i = 0; i < pci_root_num; i++) {
81 if (pci_root_info[i].bus_min == b->number)
85 if (i == pci_root_num)
88 info = &pci_root_info[i];
89 for (j = 0; j < info->res_num; j++) {
91 struct resource *root;
95 if (res->flags & IORESOURCE_IO)
96 root = &ioport_resource;
98 root = &iomem_resource;
99 insert_resource(root, res);
110 static void __init update_range(struct res_range *range, size_t start,
116 for (j = 0; j < RANGE_NUM; j++) {
120 if (start <= range[j].start && end >= range[j].end) {
126 if (start <= range[j].start && end < range[j].end && range[j].start < end + 1) {
127 range[j].start = end + 1;
132 if (start > range[j].start && end >= range[j].end && range[j].end > start - 1) {
133 range[j].end = start - 1;
137 if (start > range[j].start && end < range[j].end) {
138 /* find the new spare */
139 for (i = 0; i < RANGE_NUM; i++) {
140 if (range[i].end == 0)
144 range[i].end = range[j].end;
145 range[i].start = end + 1;
147 printk(KERN_ERR "run of slot in ranges\n");
149 range[j].end = start - 1;
155 static void __init update_res(struct pci_root_info *info, size_t start,
156 size_t end, unsigned long flags, int merge)
159 struct resource *res;
164 /* try to merge it with old one */
165 for (i = 0; i < info->res_num; i++) {
166 size_t final_start, final_end;
167 size_t common_start, common_end;
170 if (res->flags != flags)
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)
178 final_start = min((size_t)res->start, start);
179 final_end = max((size_t)res->end, end);
181 res->start = final_start;
182 res->end = final_end;
188 /* need to add that */
189 if (info->res_num >= RES_NUM)
192 res = &info->res[info->res_num];
193 res->name = info->name;
201 struct pci_hostbridge_probe {
208 static 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 },
215 static u64 __initdata fam10h_mmconf_start;
216 static u64 __initdata fam10h_mmconf_end;
217 static void __init get_pci_mmcfg_amd_fam10h_range(void)
221 unsigned segn_busn_bits;
223 /* assume all cpus from fam10h have mmconf */
224 if (boot_cpu_data.x86 < 0x10)
227 address = MSR_FAM10H_MMIO_CONF_BASE;
228 rdmsrl(address, msr);
230 /* mmconfig is not enable */
231 if (!(msr & FAM10H_MMIO_CONF_ENABLE))
234 base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
236 segn_busn_bits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
237 FAM10H_MMIO_CONF_BUSRANGE_MASK;
239 fam10h_mmconf_start = base;
240 fam10h_mmconf_end = base + (1ULL<<(segn_busn_bits + 20)) - 1;
244 * early_fill_mp_bus_to_node()
245 * called before pcibios_scan_root and pci_scan_bus
246 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
247 * Registers found in the K8 northbridge
249 static int __init early_fill_mp_bus_info(void)
260 struct pci_root_info *info;
262 struct resource *res;
265 struct res_range range[RANGE_NUM];
270 for (i = 0; i < BUS_NR; i++)
271 mp_bus_to_node[i] = -1;
274 if (!early_pci_allowed())
278 for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
283 bus = pci_probes[i].bus;
284 slot = pci_probes[i].slot;
285 id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
287 vendor = id & 0xffff;
288 device = (id>>16) & 0xffff;
289 if (pci_probes[i].vendor == vendor &&
290 pci_probes[i].device == device) {
300 for (i = 0; i < 4; i++) {
303 reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
305 /* Check if that register is enabled for bus range */
309 min_bus = (reg >> 16) & 0xff;
310 max_bus = (reg >> 24) & 0xff;
311 node = (reg >> 4) & 0x07;
313 for (j = min_bus; j <= max_bus; j++)
314 mp_bus_to_node[j] = (unsigned char) node;
316 link = (reg >> 8) & 0x03;
318 info = &pci_root_info[pci_root_num];
319 info->bus_min = min_bus;
320 info->bus_max = max_bus;
323 sprintf(info->name, "PCI Bus #%02x", min_bus);
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;
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));
341 start = reg & 0xfff000;
342 reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
344 link = (reg >> 4) & 0x03;
345 end = (reg & 0xfff000) | 0xfff;
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)
353 if (j == pci_root_num)
354 continue; /* not found */
356 info = &pci_root_info[j];
357 printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
358 node, link, (u64)start, (u64)end);
360 /* kernel only handle 16 bit only */
363 update_res(info, start, end, IORESOURCE_IO, 1);
364 update_range(range, start, end);
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)
373 if (j < pci_root_num) {
374 info = &pci_root_info[j];
375 for (i = 0; i < RANGE_NUM; i++) {
379 update_res(info, range[i].start, range[i].end,
384 memset(range, 0, sizeof(range));
385 /* 0xfd00000000-0xffffffffff for HT */
386 range[0].end = (0xfdULL<<32) - 1;
388 /* need to take out [0, TOM) for RAM*/
389 address = MSR_K8_TOP_MEM1;
390 rdmsrl(address, val);
391 end = (val & 0xffffff800000ULL);
392 printk(KERN_INFO "TOM: %016lx aka %ldM\n", end, end>>20);
393 if (end < (1ULL<<32))
394 update_range(range, 0, end - 1);
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);
405 for (i = 0; i < 8; i++) {
406 reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
410 start = reg & 0xffffff00; /* 39:16 on 31:8*/
412 reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
414 link = (reg >> 4) & 0x03;
415 end = (reg & 0xffffff00);
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)
425 if (j == pci_root_num)
426 continue; /* not found */
428 info = &pci_root_info[j];
430 printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
431 node, link, (u64)start, (u64)end);
433 * some sick allocation would have range overlap with fam10h
434 * mmconf range, so need to update start and end.
436 if (fam10h_mmconf_end) {
439 if (start >= fam10h_mmconf_start &&
440 start <= fam10h_mmconf_end) {
441 start = fam10h_mmconf_end + 1;
445 if (end >= fam10h_mmconf_start &&
446 end <= fam10h_mmconf_end) {
447 end = fam10h_mmconf_start - 1;
451 if (start < fam10h_mmconf_start &&
452 end > fam10h_mmconf_end) {
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;
463 printk(KERN_CONT " %s [%llx, %llx]", endx?"and":"==>", (u64)start, (u64)end);
465 printk(KERN_CONT "%s\n", endx?"":" ==> none");
471 update_res(info, start, end, IORESOURCE_MEM, 1);
472 update_range(range, start, end);
473 printk(KERN_CONT "\n");
476 /* need to take out [4G, TOM2) for RAM*/
478 address = MSR_K8_SYSCFG;
479 rdmsrl(address, val);
480 /* TOP_MEM2 is enabled? */
483 address = MSR_K8_TOP_MEM2;
484 rdmsrl(address, val);
485 end = (val & 0xffffff800000ULL);
486 printk(KERN_INFO "TOM2: %016lx aka %ldM\n", end, end>>20);
487 update_range(range, 1ULL<<32, end - 1);
491 * add left over mmio range to def node/link ?
492 * that is tricky, just record range in from start_min to 4G
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)
499 if (j < pci_root_num) {
500 info = &pci_root_info[j];
502 for (i = 0; i < RANGE_NUM; i++) {
506 update_res(info, range[i].start, range[i].end,
511 for (i = 0; i < pci_root_num; i++) {
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++) {
522 printk(KERN_DEBUG "bus: %02x index %x %s: [%llx, %llx]\n",
524 (res->flags & IORESOURCE_IO)?"io port":"mmio",
525 res->start, res->end);
532 postcore_initcall(early_fill_mp_bus_info);
536 /* common 32/64 bit code */
538 #define ENABLE_CF8_EXT_CFG (1ULL << 46)
540 static void enable_pci_io_ecs_per_cpu(void *unused)
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);
550 static int __init enable_pci_io_ecs(void)
552 /* assume all cpus from fam10h have IO ECS */
553 if (boot_cpu_data.x86 < 0x10)
555 on_each_cpu(enable_pci_io_ecs_per_cpu, NULL, 1, 1);
556 pci_probe |= PCI_HAS_IO_ECS;
560 postcore_initcall(enable_pci_io_ecs);