srat.c 5.2 KB

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