k8topology_64.c 5.2 KB

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