amdtopology.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * AMD NUMA support.
  3. * Discover the memory map and associated nodes.
  4. *
  5. * This version reads it directly from the AMD northbridge.
  6. *
  7. * Copyright 2002,2003 Andi Kleen, SuSE Labs.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/string.h>
  12. #include <linux/nodemask.h>
  13. #include <linux/memblock.h>
  14. #include <linux/bootmem.h>
  15. #include <asm/io.h>
  16. #include <linux/pci_ids.h>
  17. #include <linux/acpi.h>
  18. #include <asm/types.h>
  19. #include <asm/mmzone.h>
  20. #include <asm/proto.h>
  21. #include <asm/e820.h>
  22. #include <asm/pci-direct.h>
  23. #include <asm/numa.h>
  24. #include <asm/mpspec.h>
  25. #include <asm/apic.h>
  26. #include <asm/amd_nb.h>
  27. static unsigned char __initdata nodeids[8];
  28. static __init int find_northbridge(void)
  29. {
  30. int num;
  31. for (num = 0; num < 32; num++) {
  32. u32 header;
  33. header = read_pci_config(0, num, 0, 0x00);
  34. if (header != (PCI_VENDOR_ID_AMD | (0x1100<<16)) &&
  35. header != (PCI_VENDOR_ID_AMD | (0x1200<<16)) &&
  36. header != (PCI_VENDOR_ID_AMD | (0x1300<<16)))
  37. continue;
  38. header = read_pci_config(0, num, 1, 0x00);
  39. if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)) &&
  40. header != (PCI_VENDOR_ID_AMD | (0x1201<<16)) &&
  41. header != (PCI_VENDOR_ID_AMD | (0x1301<<16)))
  42. continue;
  43. return num;
  44. }
  45. return -ENOENT;
  46. }
  47. int __init amd_numa_init(void)
  48. {
  49. u64 start = PFN_PHYS(0);
  50. u64 end = PFN_PHYS(max_pfn);
  51. unsigned numnodes;
  52. u64 prevbase;
  53. int i, j, nb;
  54. u32 nodeid, reg;
  55. unsigned int bits, cores, apicid_base;
  56. if (!early_pci_allowed())
  57. return -EINVAL;
  58. nb = find_northbridge();
  59. if (nb < 0)
  60. return nb;
  61. pr_info("Scanning NUMA topology in Northbridge %d\n", nb);
  62. reg = read_pci_config(0, nb, 0, 0x60);
  63. numnodes = ((reg >> 4) & 0xF) + 1;
  64. if (numnodes <= 1)
  65. return -ENOENT;
  66. pr_info("Number of physical nodes %d\n", numnodes);
  67. prevbase = 0;
  68. for (i = 0; i < 8; i++) {
  69. u64 base, limit;
  70. base = read_pci_config(0, nb, 1, 0x40 + i*8);
  71. limit = read_pci_config(0, nb, 1, 0x44 + i*8);
  72. nodeids[i] = nodeid = limit & 7;
  73. if ((base & 3) == 0) {
  74. if (i < numnodes)
  75. pr_info("Skipping disabled node %d\n", i);
  76. continue;
  77. }
  78. if (nodeid >= numnodes) {
  79. pr_info("Ignoring excess node %d (%Lx:%Lx)\n", nodeid,
  80. base, limit);
  81. continue;
  82. }
  83. if (!limit) {
  84. pr_info("Skipping node entry %d (base %Lx)\n",
  85. i, base);
  86. continue;
  87. }
  88. if ((base >> 8) & 3 || (limit >> 8) & 3) {
  89. pr_err("Node %d using interleaving mode %Lx/%Lx\n",
  90. nodeid, (base >> 8) & 3, (limit >> 8) & 3);
  91. return -EINVAL;
  92. }
  93. if (node_isset(nodeid, numa_nodes_parsed)) {
  94. pr_info("Node %d already present, skipping\n",
  95. nodeid);
  96. continue;
  97. }
  98. limit >>= 16;
  99. limit++;
  100. limit <<= 24;
  101. if (limit > end)
  102. limit = end;
  103. if (limit <= base)
  104. continue;
  105. base >>= 16;
  106. base <<= 24;
  107. if (base < start)
  108. base = start;
  109. if (limit > end)
  110. limit = end;
  111. if (limit == base) {
  112. pr_err("Empty node %d\n", nodeid);
  113. continue;
  114. }
  115. if (limit < base) {
  116. pr_err("Node %d bogus settings %Lx-%Lx.\n",
  117. nodeid, base, limit);
  118. continue;
  119. }
  120. /* Could sort here, but pun for now. Should not happen anyroads. */
  121. if (prevbase > base) {
  122. pr_err("Node map not sorted %Lx,%Lx\n",
  123. prevbase, base);
  124. return -EINVAL;
  125. }
  126. pr_info("Node %d MemBase %016Lx Limit %016Lx\n",
  127. nodeid, base, limit);
  128. prevbase = base;
  129. numa_add_memblk(nodeid, base, limit);
  130. node_set(nodeid, numa_nodes_parsed);
  131. }
  132. if (!nodes_weight(numa_nodes_parsed))
  133. return -ENOENT;
  134. /*
  135. * We seem to have valid NUMA configuration. Map apicids to nodes
  136. * using the coreid bits from early_identify_cpu.
  137. */
  138. bits = boot_cpu_data.x86_coreid_bits;
  139. cores = 1 << bits;
  140. apicid_base = 0;
  141. /*
  142. * get boot-time SMP configuration:
  143. */
  144. early_get_smp_config();
  145. if (boot_cpu_physical_apicid > 0) {
  146. pr_info("BSP APIC ID: %02x\n", boot_cpu_physical_apicid);
  147. apicid_base = boot_cpu_physical_apicid;
  148. }
  149. for_each_node_mask(i, numa_nodes_parsed)
  150. for (j = apicid_base; j < cores + apicid_base; j++)
  151. set_apicid_to_node((i << bits) + j, i);
  152. return 0;
  153. }