srat.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. if (!(end & 0xfff))
  67. end--;
  68. nd->end = end;
  69. if (nd->start > nd->end)
  70. nd->start = nd->end;
  71. }
  72. }
  73. static __init void bad_srat(void)
  74. {
  75. int i;
  76. printk(KERN_ERR "SRAT: SRAT not used.\n");
  77. acpi_numa = -1;
  78. for (i = 0; i < MAX_LOCAL_APIC; i++)
  79. apicid_to_node[i] = NUMA_NO_NODE;
  80. }
  81. static __init inline int srat_disabled(void)
  82. {
  83. return numa_off || acpi_numa < 0;
  84. }
  85. /* Callback for SLIT parsing */
  86. void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
  87. {
  88. acpi_slit = slit;
  89. }
  90. /* Callback for Proximity Domain -> LAPIC mapping */
  91. void __init
  92. acpi_numa_processor_affinity_init(struct acpi_table_processor_affinity *pa)
  93. {
  94. int pxm, node;
  95. if (srat_disabled() || pa->flags.enabled == 0)
  96. return;
  97. pxm = pa->proximity_domain;
  98. node = setup_node(pxm);
  99. if (node < 0) {
  100. printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
  101. bad_srat();
  102. return;
  103. }
  104. apicid_to_node[pa->apic_id] = node;
  105. acpi_numa = 1;
  106. printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n",
  107. pxm, pa->apic_id, node);
  108. }
  109. /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
  110. void __init
  111. acpi_numa_memory_affinity_init(struct acpi_table_memory_affinity *ma)
  112. {
  113. struct node *nd;
  114. unsigned long start, end;
  115. int node, pxm;
  116. int i;
  117. if (srat_disabled() || ma->flags.enabled == 0)
  118. return;
  119. pxm = ma->proximity_domain;
  120. node = setup_node(pxm);
  121. if (node < 0) {
  122. printk(KERN_ERR "SRAT: Too many proximity domains.\n");
  123. bad_srat();
  124. return;
  125. }
  126. start = ma->base_addr_lo | ((u64)ma->base_addr_hi << 32);
  127. end = start + (ma->length_lo | ((u64)ma->length_hi << 32));
  128. /* It is fine to add this area to the nodes data it will be used later*/
  129. if (ma->flags.hot_pluggable == 1)
  130. printk(KERN_INFO "SRAT: hot plug zone found %lx - %lx \n",
  131. start, end);
  132. i = conflicting_nodes(start, end);
  133. if (i == node) {
  134. printk(KERN_WARNING
  135. "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
  136. pxm, start, end, nodes[i].start, nodes[i].end);
  137. } else if (i >= 0) {
  138. printk(KERN_ERR
  139. "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
  140. pxm, start, end, node_to_pxm(i),
  141. nodes[i].start, nodes[i].end);
  142. bad_srat();
  143. return;
  144. }
  145. nd = &nodes[node];
  146. if (!node_test_and_set(node, nodes_parsed)) {
  147. nd->start = start;
  148. nd->end = end;
  149. } else {
  150. if (start < nd->start)
  151. nd->start = start;
  152. if (nd->end < end)
  153. nd->end = end;
  154. }
  155. if (!(nd->end & 0xfff))
  156. nd->end--;
  157. printk(KERN_INFO "SRAT: Node %u PXM %u %Lx-%Lx\n", node, pxm,
  158. nd->start, nd->end);
  159. }
  160. void __init acpi_numa_arch_fixup(void) {}
  161. /* Use the information discovered above to actually set up the nodes. */
  162. int __init acpi_scan_nodes(unsigned long start, unsigned long end)
  163. {
  164. int i;
  165. if (acpi_numa <= 0)
  166. return -1;
  167. /* First clean up the node list */
  168. for_each_node_mask(i, nodes_parsed) {
  169. cutoff_node(i, start, end);
  170. if (nodes[i].start == nodes[i].end)
  171. node_clear(i, nodes_parsed);
  172. }
  173. memnode_shift = compute_hash_shift(nodes, nodes_weight(nodes_parsed));
  174. if (memnode_shift < 0) {
  175. printk(KERN_ERR
  176. "SRAT: No NUMA node hash function found. Contact maintainer\n");
  177. bad_srat();
  178. return -1;
  179. }
  180. /* Finally register nodes */
  181. for_each_node_mask(i, nodes_parsed)
  182. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  183. for (i = 0; i < NR_CPUS; i++) {
  184. if (cpu_to_node[i] == NUMA_NO_NODE)
  185. continue;
  186. if (!node_isset(cpu_to_node[i], nodes_parsed))
  187. cpu_to_node[i] = NUMA_NO_NODE;
  188. }
  189. numa_init_array();
  190. return 0;
  191. }
  192. static int node_to_pxm(int n)
  193. {
  194. int i;
  195. if (pxm2node[n] == n)
  196. return n;
  197. for (i = 0; i < 256; i++)
  198. if (pxm2node[i] == n)
  199. return i;
  200. return 0;
  201. }
  202. int __node_distance(int a, int b)
  203. {
  204. int index;
  205. if (!acpi_slit)
  206. return a == b ? 10 : 20;
  207. index = acpi_slit->localities * node_to_pxm(a);
  208. return acpi_slit->entry[index + node_to_pxm(b)];
  209. }
  210. EXPORT_SYMBOL(__node_distance);