k8topology_64.c 5.0 KB

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