srat_64.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 <linux/bootmem.h>
  18. #include <linux/memblock.h>
  19. #include <linux/mm.h>
  20. #include <asm/proto.h>
  21. #include <asm/numa.h>
  22. #include <asm/e820.h>
  23. #include <asm/apic.h>
  24. #include <asm/uv/uv.h>
  25. int acpi_numa __initdata;
  26. static struct acpi_table_slit *acpi_slit;
  27. static struct bootnode nodes_add[MAX_NUMNODES];
  28. static __init int setup_node(int pxm)
  29. {
  30. return acpi_map_pxm_to_node(pxm);
  31. }
  32. static __init void bad_srat(void)
  33. {
  34. printk(KERN_ERR "SRAT: SRAT not used.\n");
  35. acpi_numa = -1;
  36. memset(nodes_add, 0, sizeof(nodes_add));
  37. }
  38. static __init inline int srat_disabled(void)
  39. {
  40. return acpi_numa < 0;
  41. }
  42. /* Callback for SLIT parsing */
  43. void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
  44. {
  45. int i, j;
  46. unsigned length;
  47. unsigned long phys;
  48. for (i = 0; i < slit->locality_count; i++)
  49. for (j = 0; j < slit->locality_count; j++)
  50. numa_set_distance(pxm_to_node(i), pxm_to_node(j),
  51. slit->entry[slit->locality_count * i + j]);
  52. /* acpi_slit is used only by emulation */
  53. length = slit->header.length;
  54. phys = memblock_find_in_range(0, max_pfn_mapped<<PAGE_SHIFT, length,
  55. PAGE_SIZE);
  56. if (phys == MEMBLOCK_ERROR)
  57. panic(" Can not save slit!\n");
  58. acpi_slit = __va(phys);
  59. memcpy(acpi_slit, slit, length);
  60. memblock_x86_reserve_range(phys, phys + length, "ACPI SLIT");
  61. }
  62. /* Callback for Proximity Domain -> x2APIC mapping */
  63. void __init
  64. acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
  65. {
  66. int pxm, node;
  67. int apic_id;
  68. if (srat_disabled())
  69. return;
  70. if (pa->header.length < sizeof(struct acpi_srat_x2apic_cpu_affinity)) {
  71. bad_srat();
  72. return;
  73. }
  74. if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
  75. return;
  76. pxm = pa->proximity_domain;
  77. node = setup_node(pxm);
  78. if (node < 0) {
  79. printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
  80. bad_srat();
  81. return;
  82. }
  83. apic_id = pa->apic_id;
  84. if (apic_id >= MAX_LOCAL_APIC) {
  85. printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
  86. return;
  87. }
  88. set_apicid_to_node(apic_id, node);
  89. node_set(node, numa_nodes_parsed);
  90. acpi_numa = 1;
  91. printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u\n",
  92. pxm, apic_id, node);
  93. }
  94. /* Callback for Proximity Domain -> LAPIC mapping */
  95. void __init
  96. acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
  97. {
  98. int pxm, node;
  99. int apic_id;
  100. if (srat_disabled())
  101. return;
  102. if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
  103. bad_srat();
  104. return;
  105. }
  106. if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
  107. return;
  108. pxm = pa->proximity_domain_lo;
  109. node = setup_node(pxm);
  110. if (node < 0) {
  111. printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
  112. bad_srat();
  113. return;
  114. }
  115. if (get_uv_system_type() >= UV_X2APIC)
  116. apic_id = (pa->apic_id << 8) | pa->local_sapic_eid;
  117. else
  118. apic_id = pa->apic_id;
  119. if (apic_id >= MAX_LOCAL_APIC) {
  120. printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
  121. return;
  122. }
  123. set_apicid_to_node(apic_id, node);
  124. node_set(node, numa_nodes_parsed);
  125. acpi_numa = 1;
  126. printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u\n",
  127. pxm, apic_id, node);
  128. }
  129. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  130. static inline int save_add_info(void) {return 1;}
  131. #else
  132. static inline int save_add_info(void) {return 0;}
  133. #endif
  134. /*
  135. * Update nodes_add[]
  136. * This code supports one contiguous hot add area per node
  137. */
  138. static void __init
  139. update_nodes_add(int node, unsigned long start, unsigned long end)
  140. {
  141. unsigned long s_pfn = start >> PAGE_SHIFT;
  142. unsigned long e_pfn = end >> PAGE_SHIFT;
  143. int changed = 0;
  144. struct bootnode *nd = &nodes_add[node];
  145. /* I had some trouble with strange memory hotadd regions breaking
  146. the boot. Be very strict here and reject anything unexpected.
  147. If you want working memory hotadd write correct SRATs.
  148. The node size check is a basic sanity check to guard against
  149. mistakes */
  150. if ((signed long)(end - start) < NODE_MIN_SIZE) {
  151. printk(KERN_ERR "SRAT: Hotplug area too small\n");
  152. return;
  153. }
  154. /* This check might be a bit too strict, but I'm keeping it for now. */
  155. if (absent_pages_in_range(s_pfn, e_pfn) != e_pfn - s_pfn) {
  156. printk(KERN_ERR
  157. "SRAT: Hotplug area %lu -> %lu has existing memory\n",
  158. s_pfn, e_pfn);
  159. return;
  160. }
  161. /* Looks good */
  162. if (nd->start == nd->end) {
  163. nd->start = start;
  164. nd->end = end;
  165. changed = 1;
  166. } else {
  167. if (nd->start == end) {
  168. nd->start = start;
  169. changed = 1;
  170. }
  171. if (nd->end == start) {
  172. nd->end = end;
  173. changed = 1;
  174. }
  175. if (!changed)
  176. printk(KERN_ERR "SRAT: Hotplug zone not continuous. Partly ignored\n");
  177. }
  178. if (changed) {
  179. node_set(node, numa_nodes_parsed);
  180. printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n",
  181. nd->start, nd->end);
  182. }
  183. }
  184. /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
  185. void __init
  186. acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
  187. {
  188. unsigned long start, end;
  189. int node, pxm;
  190. if (srat_disabled())
  191. return;
  192. if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
  193. bad_srat();
  194. return;
  195. }
  196. if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
  197. return;
  198. if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && !save_add_info())
  199. return;
  200. start = ma->base_address;
  201. end = start + ma->length;
  202. pxm = ma->proximity_domain;
  203. node = setup_node(pxm);
  204. if (node < 0) {
  205. printk(KERN_ERR "SRAT: Too many proximity domains.\n");
  206. bad_srat();
  207. return;
  208. }
  209. if (numa_add_memblk(node, start, end) < 0) {
  210. bad_srat();
  211. return;
  212. }
  213. printk(KERN_INFO "SRAT: Node %u PXM %u %lx-%lx\n", node, pxm,
  214. start, end);
  215. if (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)
  216. update_nodes_add(node, start, end);
  217. }
  218. void __init acpi_numa_arch_fixup(void) {}
  219. int __init x86_acpi_numa_init(void)
  220. {
  221. int ret;
  222. ret = acpi_numa_init();
  223. if (ret < 0)
  224. return ret;
  225. return srat_disabled() ? -EINVAL : 0;
  226. }
  227. #ifdef CONFIG_NUMA_EMU
  228. static int fake_node_to_pxm_map[MAX_NUMNODES] __initdata = {
  229. [0 ... MAX_NUMNODES-1] = PXM_INVAL
  230. };
  231. /*
  232. * In NUMA emulation, we need to setup proximity domain (_PXM) to node ID
  233. * mappings that respect the real ACPI topology but reflect our emulated
  234. * environment. For each emulated node, we find which real node it appears on
  235. * and create PXM to NID mappings for those fake nodes which mirror that
  236. * locality. SLIT will now represent the correct distances between emulated
  237. * nodes as a result of the real topology.
  238. */
  239. void __init acpi_fake_nodes(const struct bootnode *fake_nodes, int num_nodes)
  240. {
  241. int i;
  242. for (i = 0; i < num_nodes; i++) {
  243. int nid, pxm;
  244. nid = find_node_by_addr(fake_nodes[i].start);
  245. if (nid == NUMA_NO_NODE)
  246. continue;
  247. pxm = node_to_pxm(nid);
  248. if (pxm == PXM_INVAL)
  249. continue;
  250. fake_node_to_pxm_map[i] = pxm;
  251. }
  252. for (i = 0; i < num_nodes; i++)
  253. __acpi_map_pxm_to_node(fake_node_to_pxm_map[i], i);
  254. for (i = 0; i < num_nodes; i++)
  255. if (fake_nodes[i].start != fake_nodes[i].end)
  256. node_set(i, numa_nodes_parsed);
  257. }
  258. int acpi_emu_node_distance(int a, int b)
  259. {
  260. int index;
  261. if (!acpi_slit)
  262. return node_to_pxm(a) == node_to_pxm(b) ?
  263. LOCAL_DISTANCE : REMOTE_DISTANCE;
  264. index = acpi_slit->locality_count * node_to_pxm(a);
  265. return acpi_slit->entry[index + node_to_pxm(b)];
  266. }
  267. #endif /* CONFIG_NUMA_EMU */
  268. #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || defined(CONFIG_ACPI_HOTPLUG_MEMORY)
  269. int memory_add_physaddr_to_nid(u64 start)
  270. {
  271. int i, ret = 0;
  272. for_each_node(i)
  273. if (nodes_add[i].start <= start && nodes_add[i].end > start)
  274. ret = i;
  275. return ret;
  276. }
  277. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  278. #endif