k8topology_64.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. unsigned char nodeids[8];
  78. int i, j, nb, found = 0;
  79. u32 nodeid, reg;
  80. if (!early_pci_allowed())
  81. return -1;
  82. nb = find_northbridge();
  83. if (nb < 0)
  84. return nb;
  85. printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb);
  86. reg = read_pci_config(0, nb, 0, 0x60);
  87. numnodes = ((reg >> 4) & 0xF) + 1;
  88. if (numnodes <= 1)
  89. return -1;
  90. printk(KERN_INFO "Number of nodes %d\n", numnodes);
  91. memset(&nodes, 0, sizeof(nodes));
  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. nodeids[i] = nodeid;
  99. if ((base & 3) == 0) {
  100. if (i < numnodes)
  101. printk("Skipping disabled node %d\n", i);
  102. continue;
  103. }
  104. if (nodeid >= numnodes) {
  105. printk("Ignoring excess node %d (%lx:%lx)\n", nodeid,
  106. base, limit);
  107. continue;
  108. }
  109. if (!limit) {
  110. printk(KERN_INFO "Skipping node entry %d (base %lx)\n",
  111. i, base);
  112. continue;
  113. }
  114. if ((base >> 8) & 3 || (limit >> 8) & 3) {
  115. printk(KERN_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, node_possible_map)) {
  120. printk(KERN_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 > max_pfn << PAGE_SHIFT)
  129. limit = max_pfn << PAGE_SHIFT;
  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. printk(KERN_ERR "Empty node %d\n", nodeid);
  140. continue;
  141. }
  142. if (limit < base) {
  143. printk(KERN_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. printk(KERN_ERR "Node map not sorted %lx,%lx\n",
  150. prevbase, base);
  151. return -1;
  152. }
  153. printk(KERN_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. e820_register_active_regions(nodeid,
  159. nodes[nodeid].start >> PAGE_SHIFT,
  160. nodes[nodeid].end >> PAGE_SHIFT);
  161. prevbase = base;
  162. node_set(nodeid, node_possible_map);
  163. }
  164. if (!found)
  165. return -1;
  166. memnode_shift = compute_hash_shift(nodes, 8, NULL);
  167. if (memnode_shift < 0) {
  168. printk(KERN_ERR "No NUMA node hash function found. Contact maintainer\n");
  169. return -1;
  170. }
  171. printk(KERN_INFO "Using node hash shift of %d\n", memnode_shift);
  172. /* use the coreid bits from early_identify_cpu */
  173. bits = boot_cpu_data.x86_coreid_bits;
  174. cores = (1<<bits);
  175. apicid_base = 0;
  176. /* need to get boot_cpu_id early for system with apicid lifting */
  177. early_get_boot_cpu_id();
  178. if (boot_cpu_physical_apicid > 0) {
  179. printk(KERN_INFO "BSP APIC ID: %02x\n",
  180. boot_cpu_physical_apicid);
  181. apicid_base = boot_cpu_physical_apicid;
  182. }
  183. for (i = 0; i < 8; i++) {
  184. if (nodes[i].start != nodes[i].end) {
  185. nodeid = nodeids[i];
  186. for (j = apicid_base; j < cores + apicid_base; j++)
  187. apicid_to_node[(nodeid << bits) + j] = i;
  188. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  189. }
  190. }
  191. numa_init_array();
  192. return 0;
  193. }