srat.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * ACPI 3.0 based NUMA setup
  3. * Copyright 2004 Andi Kleen, SuSE Labs.
  4. *
  5. * Reads the ACPI SRAT table to figure out what memory belongs to which CPUs.
  6. *
  7. * Called from acpi_numa_init while reading the SRAT and SLIT tables.
  8. * Assumes all memory regions belonging to a single proximity domain
  9. * are in one chunk. Holes between them will be included in the node.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/acpi.h>
  13. #include <linux/mmzone.h>
  14. #include <linux/bitmap.h>
  15. #include <linux/module.h>
  16. #include <linux/topology.h>
  17. #include <asm/proto.h>
  18. #include <asm/numa.h>
  19. static struct acpi_table_slit *acpi_slit;
  20. static nodemask_t nodes_parsed __initdata;
  21. static nodemask_t nodes_found __initdata;
  22. static struct node nodes[MAX_NUMNODES] __initdata;
  23. static __u8 pxm2node[256] = { [0 ... 255] = 0xff };
  24. static int node_to_pxm(int n);
  25. int pxm_to_node(int pxm)
  26. {
  27. if ((unsigned)pxm >= 256)
  28. return 0;
  29. return pxm2node[pxm];
  30. }
  31. static __init int setup_node(int pxm)
  32. {
  33. unsigned node = pxm2node[pxm];
  34. if (node == 0xff) {
  35. if (nodes_weight(nodes_found) >= MAX_NUMNODES)
  36. return -1;
  37. node = first_unset_node(nodes_found);
  38. node_set(node, nodes_found);
  39. pxm2node[pxm] = node;
  40. }
  41. return pxm2node[pxm];
  42. }
  43. static __init int conflicting_nodes(unsigned long start, unsigned long end)
  44. {
  45. int i;
  46. for_each_node_mask(i, nodes_parsed) {
  47. struct node *nd = &nodes[i];
  48. if (nd->start == nd->end)
  49. continue;
  50. if (nd->end > start && nd->start < end)
  51. return i;
  52. if (nd->end == end && nd->start == start)
  53. return i;
  54. }
  55. return -1;
  56. }
  57. static __init void cutoff_node(int i, unsigned long start, unsigned long end)
  58. {
  59. struct node *nd = &nodes[i];
  60. if (nd->start < start) {
  61. nd->start = start;
  62. if (nd->end < nd->start)
  63. nd->start = nd->end;
  64. }
  65. if (nd->end > end) {
  66. nd->end = end;
  67. if (nd->start > nd->end)
  68. nd->start = nd->end;
  69. }
  70. }
  71. static __init void bad_srat(void)
  72. {
  73. int i;
  74. printk(KERN_ERR "SRAT: SRAT not used.\n");
  75. acpi_numa = -1;
  76. for (i = 0; i < MAX_LOCAL_APIC; i++)
  77. apicid_to_node[i] = NUMA_NO_NODE;
  78. }
  79. static __init inline int srat_disabled(void)
  80. {
  81. return numa_off || acpi_numa < 0;
  82. }
  83. /* Callback for SLIT parsing */
  84. void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
  85. {
  86. acpi_slit = slit;
  87. }
  88. /* Callback for Proximity Domain -> LAPIC mapping */
  89. void __init
  90. acpi_numa_processor_affinity_init(struct acpi_table_processor_affinity *pa)
  91. {
  92. int pxm, node;
  93. if (srat_disabled() || pa->flags.enabled == 0)
  94. return;
  95. pxm = pa->proximity_domain;
  96. node = setup_node(pxm);
  97. if (node < 0) {
  98. printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
  99. bad_srat();
  100. return;
  101. }
  102. apicid_to_node[pa->apic_id] = node;
  103. acpi_numa = 1;
  104. printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n",
  105. pxm, pa->apic_id, node);
  106. }
  107. /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
  108. void __init
  109. acpi_numa_memory_affinity_init(struct acpi_table_memory_affinity *ma)
  110. {
  111. struct node *nd;
  112. unsigned long start, end;
  113. int node, pxm;
  114. int i;
  115. if (srat_disabled() || ma->flags.enabled == 0)
  116. return;
  117. pxm = ma->proximity_domain;
  118. node = setup_node(pxm);
  119. if (node < 0) {
  120. printk(KERN_ERR "SRAT: Too many proximity domains.\n");
  121. bad_srat();
  122. return;
  123. }
  124. start = ma->base_addr_lo | ((u64)ma->base_addr_hi << 32);
  125. end = start + (ma->length_lo | ((u64)ma->length_hi << 32));
  126. /* It is fine to add this area to the nodes data it will be used later*/
  127. if (ma->flags.hot_pluggable == 1)
  128. printk(KERN_INFO "SRAT: hot plug zone found %lx - %lx \n",
  129. start, end);
  130. i = conflicting_nodes(start, end);
  131. if (i == node) {
  132. printk(KERN_WARNING
  133. "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
  134. pxm, start, end, nodes[i].start, nodes[i].end);
  135. } else if (i >= 0) {
  136. printk(KERN_ERR
  137. "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
  138. pxm, start, end, node_to_pxm(i),
  139. nodes[i].start, nodes[i].end);
  140. bad_srat();
  141. return;
  142. }
  143. nd = &nodes[node];
  144. if (!node_test_and_set(node, nodes_parsed)) {
  145. nd->start = start;
  146. nd->end = end;
  147. } else {
  148. if (start < nd->start)
  149. nd->start = start;
  150. if (nd->end < end)
  151. nd->end = end;
  152. }
  153. printk(KERN_INFO "SRAT: Node %u PXM %u %Lx-%Lx\n", node, pxm,
  154. nd->start, nd->end);
  155. }
  156. void __init acpi_numa_arch_fixup(void) {}
  157. /* Use the information discovered above to actually set up the nodes. */
  158. int __init acpi_scan_nodes(unsigned long start, unsigned long end)
  159. {
  160. int i;
  161. if (acpi_numa <= 0)
  162. return -1;
  163. /* First clean up the node list */
  164. for_each_node_mask(i, nodes_parsed) {
  165. cutoff_node(i, start, end);
  166. if (nodes[i].start == nodes[i].end)
  167. node_clear(i, nodes_parsed);
  168. }
  169. memnode_shift = compute_hash_shift(nodes, nodes_weight(nodes_parsed));
  170. if (memnode_shift < 0) {
  171. printk(KERN_ERR
  172. "SRAT: No NUMA node hash function found. Contact maintainer\n");
  173. bad_srat();
  174. return -1;
  175. }
  176. /* Finally register nodes */
  177. for_each_node_mask(i, nodes_parsed)
  178. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  179. for (i = 0; i < NR_CPUS; i++) {
  180. if (cpu_to_node[i] == NUMA_NO_NODE)
  181. continue;
  182. if (!node_isset(cpu_to_node[i], nodes_parsed))
  183. numa_set_node(i, NUMA_NO_NODE);
  184. }
  185. numa_init_array();
  186. return 0;
  187. }
  188. static int node_to_pxm(int n)
  189. {
  190. int i;
  191. if (pxm2node[n] == n)
  192. return n;
  193. for (i = 0; i < 256; i++)
  194. if (pxm2node[i] == n)
  195. return i;
  196. return 0;
  197. }
  198. int __node_distance(int a, int b)
  199. {
  200. int index;
  201. if (!acpi_slit)
  202. return a == b ? 10 : 20;
  203. index = acpi_slit->localities * node_to_pxm(a);
  204. return acpi_slit->entry[index + node_to_pxm(b)];
  205. }
  206. EXPORT_SYMBOL(__node_distance);