amdtopology_64.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/module.h>
  13. #include <linux/nodemask.h>
  14. #include <linux/memblock.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 struct bootnode __initdata nodes[8];
  28. static unsigned char __initdata nodeids[8];
  29. static nodemask_t __initdata nodes_parsed = NODE_MASK_NONE;
  30. static __init int find_northbridge(void)
  31. {
  32. int num;
  33. for (num = 0; num < 32; num++) {
  34. u32 header;
  35. header = read_pci_config(0, num, 0, 0x00);
  36. if (header != (PCI_VENDOR_ID_AMD | (0x1100<<16)) &&
  37. header != (PCI_VENDOR_ID_AMD | (0x1200<<16)) &&
  38. header != (PCI_VENDOR_ID_AMD | (0x1300<<16)))
  39. continue;
  40. header = read_pci_config(0, num, 1, 0x00);
  41. if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)) &&
  42. header != (PCI_VENDOR_ID_AMD | (0x1201<<16)) &&
  43. header != (PCI_VENDOR_ID_AMD | (0x1301<<16)))
  44. continue;
  45. return num;
  46. }
  47. return -1;
  48. }
  49. static __init void early_get_boot_cpu_id(void)
  50. {
  51. /*
  52. * need to get the APIC ID of the BSP so can use that to
  53. * create apicid_to_node in amd_scan_nodes()
  54. */
  55. #ifdef CONFIG_X86_MPPARSE
  56. /*
  57. * get boot-time SMP configuration:
  58. */
  59. if (smp_found_config)
  60. early_get_smp_config();
  61. #endif
  62. }
  63. int __init amd_numa_init(unsigned long start_pfn, unsigned long end_pfn)
  64. {
  65. unsigned long start = PFN_PHYS(start_pfn);
  66. unsigned long end = PFN_PHYS(end_pfn);
  67. unsigned numnodes;
  68. unsigned long prevbase;
  69. int i, nb, found = 0;
  70. u32 nodeid, reg;
  71. if (!early_pci_allowed())
  72. return -1;
  73. nb = find_northbridge();
  74. if (nb < 0)
  75. return nb;
  76. pr_info("Scanning NUMA topology in Northbridge %d\n", nb);
  77. reg = read_pci_config(0, nb, 0, 0x60);
  78. numnodes = ((reg >> 4) & 0xF) + 1;
  79. if (numnodes <= 1)
  80. return -1;
  81. pr_info("Number of physical nodes %d\n", numnodes);
  82. prevbase = 0;
  83. for (i = 0; i < 8; i++) {
  84. unsigned long base, limit;
  85. base = read_pci_config(0, nb, 1, 0x40 + i*8);
  86. limit = read_pci_config(0, nb, 1, 0x44 + i*8);
  87. nodeids[i] = nodeid = limit & 7;
  88. if ((base & 3) == 0) {
  89. if (i < numnodes)
  90. pr_info("Skipping disabled node %d\n", i);
  91. continue;
  92. }
  93. if (nodeid >= numnodes) {
  94. pr_info("Ignoring excess node %d (%lx:%lx)\n", nodeid,
  95. base, limit);
  96. continue;
  97. }
  98. if (!limit) {
  99. pr_info("Skipping node entry %d (base %lx)\n",
  100. i, base);
  101. continue;
  102. }
  103. if ((base >> 8) & 3 || (limit >> 8) & 3) {
  104. pr_err("Node %d using interleaving mode %lx/%lx\n",
  105. nodeid, (base >> 8) & 3, (limit >> 8) & 3);
  106. return -1;
  107. }
  108. if (node_isset(nodeid, nodes_parsed)) {
  109. pr_info("Node %d already present, skipping\n",
  110. nodeid);
  111. continue;
  112. }
  113. limit >>= 16;
  114. limit <<= 24;
  115. limit |= (1<<24)-1;
  116. limit++;
  117. if (limit > end)
  118. limit = end;
  119. if (limit <= base)
  120. continue;
  121. base >>= 16;
  122. base <<= 24;
  123. if (base < start)
  124. base = start;
  125. if (limit > end)
  126. limit = end;
  127. if (limit == base) {
  128. pr_err("Empty node %d\n", nodeid);
  129. continue;
  130. }
  131. if (limit < base) {
  132. pr_err("Node %d bogus settings %lx-%lx.\n",
  133. nodeid, base, limit);
  134. continue;
  135. }
  136. /* Could sort here, but pun for now. Should not happen anyroads. */
  137. if (prevbase > base) {
  138. pr_err("Node map not sorted %lx,%lx\n",
  139. prevbase, base);
  140. return -1;
  141. }
  142. pr_info("Node %d MemBase %016lx Limit %016lx\n",
  143. nodeid, base, limit);
  144. found++;
  145. nodes[nodeid].start = base;
  146. nodes[nodeid].end = limit;
  147. prevbase = base;
  148. node_set(nodeid, nodes_parsed);
  149. }
  150. if (!found)
  151. return -1;
  152. return 0;
  153. }
  154. #ifdef CONFIG_NUMA_EMU
  155. static s16 fake_apicid_to_node[MAX_LOCAL_APIC] __initdata = {
  156. [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
  157. };
  158. void __init amd_get_nodes(struct bootnode *physnodes)
  159. {
  160. int i;
  161. for_each_node_mask(i, nodes_parsed) {
  162. physnodes[i].start = nodes[i].start;
  163. physnodes[i].end = nodes[i].end;
  164. }
  165. }
  166. static int __init find_node_by_addr(unsigned long addr)
  167. {
  168. int ret = NUMA_NO_NODE;
  169. int i;
  170. for (i = 0; i < 8; i++)
  171. if (addr >= nodes[i].start && addr < nodes[i].end) {
  172. ret = i;
  173. break;
  174. }
  175. return ret;
  176. }
  177. /*
  178. * For NUMA emulation, fake proximity domain (_PXM) to node id mappings must be
  179. * setup to represent the physical topology but reflect the emulated
  180. * environment. For each emulated node, the real node which it appears on is
  181. * found and a fake pxm to nid mapping is created which mirrors the actual
  182. * locality. node_distance() then represents the correct distances between
  183. * emulated nodes by using the fake acpi mappings to pxms.
  184. */
  185. void __init amd_fake_nodes(const struct bootnode *nodes, int nr_nodes)
  186. {
  187. unsigned int bits;
  188. unsigned int cores;
  189. unsigned int apicid_base = 0;
  190. int i;
  191. bits = boot_cpu_data.x86_coreid_bits;
  192. cores = 1 << bits;
  193. early_get_boot_cpu_id();
  194. if (boot_cpu_physical_apicid > 0)
  195. apicid_base = boot_cpu_physical_apicid;
  196. for (i = 0; i < nr_nodes; i++) {
  197. int index;
  198. int nid;
  199. int j;
  200. nid = find_node_by_addr(nodes[i].start);
  201. if (nid == NUMA_NO_NODE)
  202. continue;
  203. index = nodeids[nid] << bits;
  204. if (fake_apicid_to_node[index + apicid_base] == NUMA_NO_NODE)
  205. for (j = apicid_base; j < cores + apicid_base; j++)
  206. fake_apicid_to_node[index + j] = i;
  207. #ifdef CONFIG_ACPI_NUMA
  208. __acpi_map_pxm_to_node(nid, i);
  209. #endif
  210. }
  211. memcpy(apicid_to_node, fake_apicid_to_node, sizeof(apicid_to_node));
  212. }
  213. #endif /* CONFIG_NUMA_EMU */
  214. int __init amd_scan_nodes(void)
  215. {
  216. unsigned int bits;
  217. unsigned int cores;
  218. unsigned int apicid_base;
  219. int i;
  220. BUG_ON(nodes_empty(nodes_parsed));
  221. node_possible_map = nodes_parsed;
  222. memnode_shift = compute_hash_shift(nodes, 8, NULL);
  223. if (memnode_shift < 0) {
  224. pr_err("No NUMA node hash function found. Contact maintainer\n");
  225. return -1;
  226. }
  227. pr_info("Using node hash shift of %d\n", memnode_shift);
  228. /* use the coreid bits from early_identify_cpu */
  229. bits = boot_cpu_data.x86_coreid_bits;
  230. cores = (1<<bits);
  231. apicid_base = 0;
  232. /* get the APIC ID of the BSP early for systems with apicid lifting */
  233. early_get_boot_cpu_id();
  234. if (boot_cpu_physical_apicid > 0) {
  235. pr_info("BSP APIC ID: %02x\n", boot_cpu_physical_apicid);
  236. apicid_base = boot_cpu_physical_apicid;
  237. }
  238. for_each_node_mask(i, node_possible_map) {
  239. int j;
  240. memblock_x86_register_active_regions(i,
  241. nodes[i].start >> PAGE_SHIFT,
  242. nodes[i].end >> PAGE_SHIFT);
  243. for (j = apicid_base; j < cores + apicid_base; j++)
  244. apicid_to_node[(i << bits) + j] = i;
  245. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  246. }
  247. numa_init_array();
  248. return 0;
  249. }