PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel-hero/arch/x86/mm/srat_64.c

https://github.com/teknologist/HTC-Hero-Teknologist-Kernel
C | 522 lines | 401 code | 63 blank | 58 comment | 95 complexity | 9fdb7a2eaa1fa8bf3975af543938ebd0 MD5 | raw file
  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/mm.h>
  19. #include <asm/proto.h>
  20. #include <asm/numa.h>
  21. #include <asm/e820.h>
  22. #include <asm/genapic.h>
  23. int acpi_numa __initdata;
  24. static struct acpi_table_slit *acpi_slit;
  25. static nodemask_t nodes_parsed __initdata;
  26. static struct bootnode nodes[MAX_NUMNODES] __initdata;
  27. static struct bootnode nodes_add[MAX_NUMNODES];
  28. static int found_add_area __initdata;
  29. int hotadd_percent __initdata = 0;
  30. static int num_node_memblks __initdata;
  31. static struct bootnode node_memblk_range[NR_NODE_MEMBLKS] __initdata;
  32. static int memblk_nodeid[NR_NODE_MEMBLKS] __initdata;
  33. /* Too small nodes confuse the VM badly. Usually they result
  34. from BIOS bugs. */
  35. #define NODE_MIN_SIZE (4*1024*1024)
  36. static __init int setup_node(int pxm)
  37. {
  38. return acpi_map_pxm_to_node(pxm);
  39. }
  40. static __init int conflicting_memblks(unsigned long start, unsigned long end)
  41. {
  42. int i;
  43. for (i = 0; i < num_node_memblks; i++) {
  44. struct bootnode *nd = &node_memblk_range[i];
  45. if (nd->start == nd->end)
  46. continue;
  47. if (nd->end > start && nd->start < end)
  48. return memblk_nodeid[i];
  49. if (nd->end == end && nd->start == start)
  50. return memblk_nodeid[i];
  51. }
  52. return -1;
  53. }
  54. static __init void cutoff_node(int i, unsigned long start, unsigned long end)
  55. {
  56. struct bootnode *nd = &nodes[i];
  57. if (found_add_area)
  58. return;
  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. nd->end = end;
  66. if (nd->start > nd->end)
  67. nd->start = nd->end;
  68. }
  69. }
  70. static __init void bad_srat(void)
  71. {
  72. int i;
  73. printk(KERN_ERR "SRAT: SRAT not used.\n");
  74. acpi_numa = -1;
  75. found_add_area = 0;
  76. for (i = 0; i < MAX_LOCAL_APIC; i++)
  77. apicid_to_node[i] = NUMA_NO_NODE;
  78. for (i = 0; i < MAX_NUMNODES; i++)
  79. nodes_add[i].start = nodes[i].end = 0;
  80. remove_all_active_ranges();
  81. }
  82. static __init inline int srat_disabled(void)
  83. {
  84. return numa_off || acpi_numa < 0;
  85. }
  86. /* Callback for SLIT parsing */
  87. void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
  88. {
  89. unsigned length;
  90. unsigned long phys;
  91. length = slit->header.length;
  92. phys = find_e820_area(0, max_pfn_mapped<<PAGE_SHIFT, length,
  93. PAGE_SIZE);
  94. if (phys == -1L)
  95. panic(" Can not save slit!\n");
  96. acpi_slit = __va(phys);
  97. memcpy(acpi_slit, slit, length);
  98. reserve_early(phys, phys + length, "ACPI SLIT");
  99. }
  100. /* Callback for Proximity Domain -> LAPIC mapping */
  101. void __init
  102. acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
  103. {
  104. int pxm, node;
  105. int apic_id;
  106. if (srat_disabled())
  107. return;
  108. if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
  109. bad_srat();
  110. return;
  111. }
  112. if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
  113. return;
  114. pxm = pa->proximity_domain_lo;
  115. node = setup_node(pxm);
  116. if (node < 0) {
  117. printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
  118. bad_srat();
  119. return;
  120. }
  121. if (is_uv_system())
  122. apic_id = (pa->apic_id << 8) | pa->local_sapic_eid;
  123. else
  124. apic_id = pa->apic_id;
  125. apicid_to_node[apic_id] = node;
  126. acpi_numa = 1;
  127. printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n",
  128. pxm, apic_id, node);
  129. }
  130. static int update_end_of_memory(unsigned long end) {return -1;}
  131. static int hotadd_enough_memory(struct bootnode *nd) {return 1;}
  132. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  133. static inline int save_add_info(void) {return 1;}
  134. #else
  135. static inline int save_add_info(void) {return 0;}
  136. #endif
  137. /*
  138. * Update nodes_add and decide if to include add are in the zone.
  139. * Both SPARSE and RESERVE need nodes_add information.
  140. * This code supports one contiguous hot add area per node.
  141. */
  142. static int __init
  143. reserve_hotadd(int node, unsigned long start, unsigned long end)
  144. {
  145. unsigned long s_pfn = start >> PAGE_SHIFT;
  146. unsigned long e_pfn = end >> PAGE_SHIFT;
  147. int ret = 0, changed = 0;
  148. struct bootnode *nd = &nodes_add[node];
  149. /* I had some trouble with strange memory hotadd regions breaking
  150. the boot. Be very strict here and reject anything unexpected.
  151. If you want working memory hotadd write correct SRATs.
  152. The node size check is a basic sanity check to guard against
  153. mistakes */
  154. if ((signed long)(end - start) < NODE_MIN_SIZE) {
  155. printk(KERN_ERR "SRAT: Hotplug area too small\n");
  156. return -1;
  157. }
  158. /* This check might be a bit too strict, but I'm keeping it for now. */
  159. if (absent_pages_in_range(s_pfn, e_pfn) != e_pfn - s_pfn) {
  160. printk(KERN_ERR
  161. "SRAT: Hotplug area %lu -> %lu has existing memory\n",
  162. s_pfn, e_pfn);
  163. return -1;
  164. }
  165. if (!hotadd_enough_memory(&nodes_add[node])) {
  166. printk(KERN_ERR "SRAT: Hotplug area too large\n");
  167. return -1;
  168. }
  169. /* Looks good */
  170. if (nd->start == nd->end) {
  171. nd->start = start;
  172. nd->end = end;
  173. changed = 1;
  174. } else {
  175. if (nd->start == end) {
  176. nd->start = start;
  177. changed = 1;
  178. }
  179. if (nd->end == start) {
  180. nd->end = end;
  181. changed = 1;
  182. }
  183. if (!changed)
  184. printk(KERN_ERR "SRAT: Hotplug zone not continuous. Partly ignored\n");
  185. }
  186. ret = update_end_of_memory(nd->end);
  187. if (changed)
  188. printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n", nd->start, nd->end);
  189. return ret;
  190. }
  191. /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
  192. void __init
  193. acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
  194. {
  195. struct bootnode *nd, oldnode;
  196. unsigned long start, end;
  197. int node, pxm;
  198. int i;
  199. if (srat_disabled())
  200. return;
  201. if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
  202. bad_srat();
  203. return;
  204. }
  205. if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
  206. return;
  207. if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && !save_add_info())
  208. return;
  209. start = ma->base_address;
  210. end = start + ma->length;
  211. pxm = ma->proximity_domain;
  212. node = setup_node(pxm);
  213. if (node < 0) {
  214. printk(KERN_ERR "SRAT: Too many proximity domains.\n");
  215. bad_srat();
  216. return;
  217. }
  218. i = conflicting_memblks(start, end);
  219. if (i == node) {
  220. printk(KERN_WARNING
  221. "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
  222. pxm, start, end, nodes[i].start, nodes[i].end);
  223. } else if (i >= 0) {
  224. printk(KERN_ERR
  225. "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
  226. pxm, start, end, node_to_pxm(i),
  227. nodes[i].start, nodes[i].end);
  228. bad_srat();
  229. return;
  230. }
  231. nd = &nodes[node];
  232. oldnode = *nd;
  233. if (!node_test_and_set(node, nodes_parsed)) {
  234. nd->start = start;
  235. nd->end = end;
  236. } else {
  237. if (start < nd->start)
  238. nd->start = start;
  239. if (nd->end < end)
  240. nd->end = end;
  241. }
  242. printk(KERN_INFO "SRAT: Node %u PXM %u %lx-%lx\n", node, pxm,
  243. start, end);
  244. e820_register_active_regions(node, start >> PAGE_SHIFT,
  245. end >> PAGE_SHIFT);
  246. push_node_boundaries(node, nd->start >> PAGE_SHIFT,
  247. nd->end >> PAGE_SHIFT);
  248. if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) &&
  249. (reserve_hotadd(node, start, end) < 0)) {
  250. /* Ignore hotadd region. Undo damage */
  251. printk(KERN_NOTICE "SRAT: Hotplug region ignored\n");
  252. *nd = oldnode;
  253. if ((nd->start | nd->end) == 0)
  254. node_clear(node, nodes_parsed);
  255. }
  256. node_memblk_range[num_node_memblks].start = start;
  257. node_memblk_range[num_node_memblks].end = end;
  258. memblk_nodeid[num_node_memblks] = node;
  259. num_node_memblks++;
  260. }
  261. /* Sanity check to catch more bad SRATs (they are amazingly common).
  262. Make sure the PXMs cover all memory. */
  263. static int __init nodes_cover_memory(const struct bootnode *nodes)
  264. {
  265. int i;
  266. unsigned long pxmram, e820ram;
  267. pxmram = 0;
  268. for_each_node_mask(i, nodes_parsed) {
  269. unsigned long s = nodes[i].start >> PAGE_SHIFT;
  270. unsigned long e = nodes[i].end >> PAGE_SHIFT;
  271. pxmram += e - s;
  272. pxmram -= absent_pages_in_range(s, e);
  273. if ((long)pxmram < 0)
  274. pxmram = 0;
  275. }
  276. e820ram = max_pfn - absent_pages_in_range(0, max_pfn);
  277. /* We seem to lose 3 pages somewhere. Allow a bit of slack. */
  278. if ((long)(e820ram - pxmram) >= 1*1024*1024) {
  279. printk(KERN_ERR
  280. "SRAT: PXMs only cover %luMB of your %luMB e820 RAM. Not used.\n",
  281. (pxmram << PAGE_SHIFT) >> 20,
  282. (e820ram << PAGE_SHIFT) >> 20);
  283. return 0;
  284. }
  285. return 1;
  286. }
  287. static void __init unparse_node(int node)
  288. {
  289. int i;
  290. node_clear(node, nodes_parsed);
  291. for (i = 0; i < MAX_LOCAL_APIC; i++) {
  292. if (apicid_to_node[i] == node)
  293. apicid_to_node[i] = NUMA_NO_NODE;
  294. }
  295. }
  296. void __init acpi_numa_arch_fixup(void) {}
  297. /* Use the information discovered above to actually set up the nodes. */
  298. int __init acpi_scan_nodes(unsigned long start, unsigned long end)
  299. {
  300. int i;
  301. if (acpi_numa <= 0)
  302. return -1;
  303. /* First clean up the node list */
  304. for (i = 0; i < MAX_NUMNODES; i++) {
  305. cutoff_node(i, start, end);
  306. /*
  307. * don't confuse VM with a node that doesn't have the
  308. * minimum memory.
  309. */
  310. if (nodes[i].end &&
  311. (nodes[i].end - nodes[i].start) < NODE_MIN_SIZE) {
  312. unparse_node(i);
  313. node_set_offline(i);
  314. }
  315. }
  316. if (!nodes_cover_memory(nodes)) {
  317. bad_srat();
  318. return -1;
  319. }
  320. memnode_shift = compute_hash_shift(node_memblk_range, num_node_memblks,
  321. memblk_nodeid);
  322. if (memnode_shift < 0) {
  323. printk(KERN_ERR
  324. "SRAT: No NUMA node hash function found. Contact maintainer\n");
  325. bad_srat();
  326. return -1;
  327. }
  328. node_possible_map = nodes_parsed;
  329. /* Finally register nodes */
  330. for_each_node_mask(i, node_possible_map)
  331. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  332. /* Try again in case setup_node_bootmem missed one due
  333. to missing bootmem */
  334. for_each_node_mask(i, node_possible_map)
  335. if (!node_online(i))
  336. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  337. for (i = 0; i < NR_CPUS; i++) {
  338. int node = early_cpu_to_node(i);
  339. if (node == NUMA_NO_NODE)
  340. continue;
  341. if (!node_isset(node, node_possible_map))
  342. numa_clear_node(i);
  343. }
  344. numa_init_array();
  345. return 0;
  346. }
  347. #ifdef CONFIG_NUMA_EMU
  348. static int fake_node_to_pxm_map[MAX_NUMNODES] __initdata = {
  349. [0 ... MAX_NUMNODES-1] = PXM_INVAL
  350. };
  351. static s16 fake_apicid_to_node[MAX_LOCAL_APIC] __initdata = {
  352. [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
  353. };
  354. static int __init find_node_by_addr(unsigned long addr)
  355. {
  356. int ret = NUMA_NO_NODE;
  357. int i;
  358. for_each_node_mask(i, nodes_parsed) {
  359. /*
  360. * Find the real node that this emulated node appears on. For
  361. * the sake of simplicity, we only use a real node's starting
  362. * address to determine which emulated node it appears on.
  363. */
  364. if (addr >= nodes[i].start && addr < nodes[i].end) {
  365. ret = i;
  366. break;
  367. }
  368. }
  369. return ret;
  370. }
  371. /*
  372. * In NUMA emulation, we need to setup proximity domain (_PXM) to node ID
  373. * mappings that respect the real ACPI topology but reflect our emulated
  374. * environment. For each emulated node, we find which real node it appears on
  375. * and create PXM to NID mappings for those fake nodes which mirror that
  376. * locality. SLIT will now represent the correct distances between emulated
  377. * nodes as a result of the real topology.
  378. */
  379. void __init acpi_fake_nodes(const struct bootnode *fake_nodes, int num_nodes)
  380. {
  381. int i, j;
  382. printk(KERN_INFO "Faking PXM affinity for fake nodes on real "
  383. "topology.\n");
  384. for (i = 0; i < num_nodes; i++) {
  385. int nid, pxm;
  386. nid = find_node_by_addr(fake_nodes[i].start);
  387. if (nid == NUMA_NO_NODE)
  388. continue;
  389. pxm = node_to_pxm(nid);
  390. if (pxm == PXM_INVAL)
  391. continue;
  392. fake_node_to_pxm_map[i] = pxm;
  393. /*
  394. * For each apicid_to_node mapping that exists for this real
  395. * node, it must now point to the fake node ID.
  396. */
  397. for (j = 0; j < MAX_LOCAL_APIC; j++)
  398. if (apicid_to_node[j] == nid)
  399. fake_apicid_to_node[j] = i;
  400. }
  401. for (i = 0; i < num_nodes; i++)
  402. __acpi_map_pxm_to_node(fake_node_to_pxm_map[i], i);
  403. memcpy(apicid_to_node, fake_apicid_to_node, sizeof(apicid_to_node));
  404. nodes_clear(nodes_parsed);
  405. for (i = 0; i < num_nodes; i++)
  406. if (fake_nodes[i].start != fake_nodes[i].end)
  407. node_set(i, nodes_parsed);
  408. WARN_ON(!nodes_cover_memory(fake_nodes));
  409. }
  410. static int null_slit_node_compare(int a, int b)
  411. {
  412. return node_to_pxm(a) == node_to_pxm(b);
  413. }
  414. #else
  415. static int null_slit_node_compare(int a, int b)
  416. {
  417. return a == b;
  418. }
  419. #endif /* CONFIG_NUMA_EMU */
  420. void __init srat_reserve_add_area(int nodeid)
  421. {
  422. if (found_add_area && nodes_add[nodeid].end) {
  423. u64 total_mb;
  424. printk(KERN_INFO "SRAT: Reserving hot-add memory space "
  425. "for node %d at %Lx-%Lx\n",
  426. nodeid, nodes_add[nodeid].start, nodes_add[nodeid].end);
  427. total_mb = (nodes_add[nodeid].end - nodes_add[nodeid].start)
  428. >> PAGE_SHIFT;
  429. total_mb *= sizeof(struct page);
  430. total_mb >>= 20;
  431. printk(KERN_INFO "SRAT: This will cost you %Lu MB of "
  432. "pre-allocated memory.\n", (unsigned long long)total_mb);
  433. reserve_bootmem_node(NODE_DATA(nodeid), nodes_add[nodeid].start,
  434. nodes_add[nodeid].end - nodes_add[nodeid].start,
  435. BOOTMEM_DEFAULT);
  436. }
  437. }
  438. int __node_distance(int a, int b)
  439. {
  440. int index;
  441. if (!acpi_slit)
  442. return null_slit_node_compare(a, b) ? LOCAL_DISTANCE :
  443. REMOTE_DISTANCE;
  444. index = acpi_slit->locality_count * node_to_pxm(a);
  445. return acpi_slit->entry[index + node_to_pxm(b)];
  446. }
  447. EXPORT_SYMBOL(__node_distance);
  448. #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || defined(CONFIG_ACPI_HOTPLUG_MEMORY)
  449. int memory_add_physaddr_to_nid(u64 start)
  450. {
  451. int i, ret = 0;
  452. for_each_node(i)
  453. if (nodes_add[i].start <= start && nodes_add[i].end > start)
  454. ret = i;
  455. return ret;
  456. }
  457. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  458. #endif