srat_64.c 8.7 KB

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