k8topology_64.c 5.0 KB

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