k8topology_64.c 5.0 KB

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