amdtopology_64.c 6.7 KB

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