k8topology_64.c 5.0 KB

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