k8topology_64.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * AMD K8 NUMA support.
  3. * Discover the memory map and associated nodes.
  4. *
  5. * This version reads it directly from the K8 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 nodemask_t __initdata nodes_parsed = NODE_MASK_NONE;
  29. static __init int find_northbridge(void)
  30. {
  31. int num;
  32. for (num = 0; num < 32; num++) {
  33. u32 header;
  34. header = read_pci_config(0, num, 0, 0x00);
  35. if (header != (PCI_VENDOR_ID_AMD | (0x1100<<16)) &&
  36. header != (PCI_VENDOR_ID_AMD | (0x1200<<16)) &&
  37. header != (PCI_VENDOR_ID_AMD | (0x1300<<16)))
  38. continue;
  39. header = read_pci_config(0, num, 1, 0x00);
  40. if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)) &&
  41. header != (PCI_VENDOR_ID_AMD | (0x1201<<16)) &&
  42. header != (PCI_VENDOR_ID_AMD | (0x1301<<16)))
  43. continue;
  44. return num;
  45. }
  46. return -1;
  47. }
  48. static __init void early_get_boot_cpu_id(void)
  49. {
  50. /*
  51. * need to get the APIC ID of the BSP so can use that to
  52. * create apicid_to_node in k8_scan_nodes()
  53. */
  54. #ifdef CONFIG_X86_MPPARSE
  55. /*
  56. * get boot-time SMP configuration:
  57. */
  58. if (smp_found_config)
  59. early_get_smp_config();
  60. #endif
  61. early_init_lapic_mapping();
  62. }
  63. int __init k8_get_nodes(struct bootnode *physnodes)
  64. {
  65. int i;
  66. int ret = 0;
  67. for_each_node_mask(i, nodes_parsed) {
  68. physnodes[ret].start = nodes[i].start;
  69. physnodes[ret].end = nodes[i].end;
  70. ret++;
  71. }
  72. return ret;
  73. }
  74. int __init k8_numa_init(unsigned long start_pfn, unsigned long end_pfn)
  75. {
  76. unsigned long start = PFN_PHYS(start_pfn);
  77. unsigned long end = PFN_PHYS(end_pfn);
  78. unsigned numnodes;
  79. unsigned long prevbase;
  80. int i, nb, found = 0;
  81. u32 nodeid, reg;
  82. if (!early_pci_allowed())
  83. return -1;
  84. nb = find_northbridge();
  85. if (nb < 0)
  86. return nb;
  87. pr_info("Scanning NUMA topology in Northbridge %d\n", nb);
  88. reg = read_pci_config(0, nb, 0, 0x60);
  89. numnodes = ((reg >> 4) & 0xF) + 1;
  90. if (numnodes <= 1)
  91. return -1;
  92. pr_info("Number of physical nodes %d\n", numnodes);
  93. prevbase = 0;
  94. for (i = 0; i < 8; i++) {
  95. unsigned long base, limit;
  96. base = read_pci_config(0, nb, 1, 0x40 + i*8);
  97. limit = read_pci_config(0, nb, 1, 0x44 + i*8);
  98. nodeid = limit & 7;
  99. if ((base & 3) == 0) {
  100. if (i < numnodes)
  101. pr_info("Skipping disabled node %d\n", i);
  102. continue;
  103. }
  104. if (nodeid >= numnodes) {
  105. pr_info("Ignoring excess node %d (%lx:%lx)\n", nodeid,
  106. base, limit);
  107. continue;
  108. }
  109. if (!limit) {
  110. pr_info("Skipping node entry %d (base %lx)\n",
  111. i, base);
  112. continue;
  113. }
  114. if ((base >> 8) & 3 || (limit >> 8) & 3) {
  115. pr_err("Node %d using interleaving mode %lx/%lx\n",
  116. nodeid, (base >> 8) & 3, (limit >> 8) & 3);
  117. return -1;
  118. }
  119. if (node_isset(nodeid, nodes_parsed)) {
  120. pr_info("Node %d already present, skipping\n",
  121. nodeid);
  122. continue;
  123. }
  124. limit >>= 16;
  125. limit <<= 24;
  126. limit |= (1<<24)-1;
  127. limit++;
  128. if (limit > end)
  129. limit = end;
  130. if (limit <= base)
  131. continue;
  132. base >>= 16;
  133. base <<= 24;
  134. if (base < start)
  135. base = start;
  136. if (limit > end)
  137. limit = end;
  138. if (limit == base) {
  139. pr_err("Empty node %d\n", nodeid);
  140. continue;
  141. }
  142. if (limit < base) {
  143. pr_err("Node %d bogus settings %lx-%lx.\n",
  144. nodeid, base, limit);
  145. continue;
  146. }
  147. /* Could sort here, but pun for now. Should not happen anyroads. */
  148. if (prevbase > base) {
  149. pr_err("Node map not sorted %lx,%lx\n",
  150. prevbase, base);
  151. return -1;
  152. }
  153. pr_info("Node %d MemBase %016lx Limit %016lx\n",
  154. nodeid, base, limit);
  155. found++;
  156. nodes[nodeid].start = base;
  157. nodes[nodeid].end = limit;
  158. prevbase = base;
  159. node_set(nodeid, nodes_parsed);
  160. }
  161. if (!found)
  162. return -1;
  163. return 0;
  164. }
  165. int __init k8_scan_nodes(void)
  166. {
  167. unsigned int bits;
  168. unsigned int cores;
  169. unsigned int apicid_base;
  170. int i;
  171. BUG_ON(nodes_empty(nodes_parsed));
  172. node_possible_map = nodes_parsed;
  173. memnode_shift = compute_hash_shift(nodes, 8, NULL);
  174. if (memnode_shift < 0) {
  175. pr_err("No NUMA node hash function found. Contact maintainer\n");
  176. return -1;
  177. }
  178. pr_info("Using node hash shift of %d\n", memnode_shift);
  179. /* use the coreid bits from early_identify_cpu */
  180. bits = boot_cpu_data.x86_coreid_bits;
  181. cores = (1<<bits);
  182. apicid_base = 0;
  183. /* get the APIC ID of the BSP early for systems with apicid lifting */
  184. early_get_boot_cpu_id();
  185. if (boot_cpu_physical_apicid > 0) {
  186. pr_info("BSP APIC ID: %02x\n", boot_cpu_physical_apicid);
  187. apicid_base = boot_cpu_physical_apicid;
  188. }
  189. for_each_node_mask(i, node_possible_map) {
  190. int j;
  191. memblock_x86_register_active_regions(i,
  192. nodes[i].start >> PAGE_SHIFT,
  193. nodes[i].end >> PAGE_SHIFT);
  194. for (j = apicid_base; j < cores + apicid_base; j++)
  195. apicid_to_node[(i << bits) + j] = i;
  196. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  197. }
  198. numa_init_array();
  199. return 0;
  200. }