k8topology_64.c 4.9 KB

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