srat.c 5.4 KB

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