srat.c 5.1 KB

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