discontig.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000, 2003 Silicon Graphics, Inc. All rights reserved.
  4. * Copyright (c) 2001 Intel Corp.
  5. * Copyright (c) 2001 Tony Luck <[email protected]>
  6. * Copyright (c) 2002 NEC Corp.
  7. * Copyright (c) 2002 Kimio Suganuma <[email protected]>
  8. * Copyright (c) 2004 Silicon Graphics, Inc
  9. * Russ Anderson <[email protected]>
  10. * Jesse Barnes <[email protected]>
  11. * Jack Steiner <[email protected]>
  12. */
  13. /*
  14. * Platform initialization for Discontig Memory
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/nmi.h>
  19. #include <linux/swap.h>
  20. #include <linux/memblock.h>
  21. #include <linux/acpi.h>
  22. #include <linux/efi.h>
  23. #include <linux/nodemask.h>
  24. #include <linux/slab.h>
  25. #include <asm/efi.h>
  26. #include <asm/tlb.h>
  27. #include <asm/meminit.h>
  28. #include <asm/numa.h>
  29. #include <asm/sections.h>
  30. /*
  31. * Track per-node information needed to setup the boot memory allocator, the
  32. * per-node areas, and the real VM.
  33. */
  34. struct early_node_data {
  35. struct ia64_node_data *node_data;
  36. unsigned long pernode_addr;
  37. unsigned long pernode_size;
  38. unsigned long min_pfn;
  39. unsigned long max_pfn;
  40. };
  41. static struct early_node_data mem_data[MAX_NUMNODES] __initdata;
  42. static nodemask_t memory_less_mask __initdata;
  43. pg_data_t *pgdat_list[MAX_NUMNODES];
  44. /*
  45. * To prevent cache aliasing effects, align per-node structures so that they
  46. * start at addresses that are strided by node number.
  47. */
  48. #define MAX_NODE_ALIGN_OFFSET (32 * 1024 * 1024)
  49. #define NODEDATA_ALIGN(addr, node) \
  50. ((((addr) + 1024*1024-1) & ~(1024*1024-1)) + \
  51. (((node)*PERCPU_PAGE_SIZE) & (MAX_NODE_ALIGN_OFFSET - 1)))
  52. /**
  53. * build_node_maps - callback to setup mem_data structs for each node
  54. * @start: physical start of range
  55. * @len: length of range
  56. * @node: node where this range resides
  57. *
  58. * Detect extents of each piece of memory that we wish to
  59. * treat as a virtually contiguous block (i.e. each node). Each such block
  60. * must start on an %IA64_GRANULE_SIZE boundary, so we round the address down
  61. * if necessary. Any non-existent pages will simply be part of the virtual
  62. * memmap.
  63. */
  64. static int __init build_node_maps(unsigned long start, unsigned long len,
  65. int node)
  66. {
  67. unsigned long spfn, epfn, end = start + len;
  68. epfn = GRANULEROUNDUP(end) >> PAGE_SHIFT;
  69. spfn = GRANULEROUNDDOWN(start) >> PAGE_SHIFT;
  70. if (!mem_data[node].min_pfn) {
  71. mem_data[node].min_pfn = spfn;
  72. mem_data[node].max_pfn = epfn;
  73. } else {
  74. mem_data[node].min_pfn = min(spfn, mem_data[node].min_pfn);
  75. mem_data[node].max_pfn = max(epfn, mem_data[node].max_pfn);
  76. }
  77. return 0;
  78. }
  79. /**
  80. * early_nr_cpus_node - return number of cpus on a given node
  81. * @node: node to check
  82. *
  83. * Count the number of cpus on @node. We can't use nr_cpus_node() yet because
  84. * acpi_boot_init() (which builds the node_to_cpu_mask array) hasn't been
  85. * called yet. Note that node 0 will also count all non-existent cpus.
  86. */
  87. static int early_nr_cpus_node(int node)
  88. {
  89. int cpu, n = 0;
  90. for_each_possible_early_cpu(cpu)
  91. if (node == node_cpuid[cpu].nid)
  92. n++;
  93. return n;
  94. }
  95. /**
  96. * compute_pernodesize - compute size of pernode data
  97. * @node: the node id.
  98. */
  99. static unsigned long compute_pernodesize(int node)
  100. {
  101. unsigned long pernodesize = 0, cpus;
  102. cpus = early_nr_cpus_node(node);
  103. pernodesize += PERCPU_PAGE_SIZE * cpus;
  104. pernodesize += node * L1_CACHE_BYTES;
  105. pernodesize += L1_CACHE_ALIGN(sizeof(pg_data_t));
  106. pernodesize += L1_CACHE_ALIGN(sizeof(struct ia64_node_data));
  107. pernodesize += L1_CACHE_ALIGN(sizeof(pg_data_t));
  108. pernodesize = PAGE_ALIGN(pernodesize);
  109. return pernodesize;
  110. }
  111. /**
  112. * per_cpu_node_setup - setup per-cpu areas on each node
  113. * @cpu_data: per-cpu area on this node
  114. * @node: node to setup
  115. *
  116. * Copy the static per-cpu data into the region we just set aside and then
  117. * setup __per_cpu_offset for each CPU on this node. Return a pointer to
  118. * the end of the area.
  119. */
  120. static void *per_cpu_node_setup(void *cpu_data, int node)
  121. {
  122. #ifdef CONFIG_SMP
  123. int cpu;
  124. for_each_possible_early_cpu(cpu) {
  125. void *src = cpu == 0 ? __cpu0_per_cpu : __phys_per_cpu_start;
  126. if (node != node_cpuid[cpu].nid)
  127. continue;
  128. memcpy(__va(cpu_data), src, __per_cpu_end - __per_cpu_start);
  129. __per_cpu_offset[cpu] = (char *)__va(cpu_data) -
  130. __per_cpu_start;
  131. /*
  132. * percpu area for cpu0 is moved from the __init area
  133. * which is setup by head.S and used till this point.
  134. * Update ar.k3. This move is ensures that percpu
  135. * area for cpu0 is on the correct node and its
  136. * virtual address isn't insanely far from other
  137. * percpu areas which is important for congruent
  138. * percpu allocator.
  139. */
  140. if (cpu == 0)
  141. ia64_set_kr(IA64_KR_PER_CPU_DATA,
  142. (unsigned long)cpu_data -
  143. (unsigned long)__per_cpu_start);
  144. cpu_data += PERCPU_PAGE_SIZE;
  145. }
  146. #endif
  147. return cpu_data;
  148. }
  149. #ifdef CONFIG_SMP
  150. /**
  151. * setup_per_cpu_areas - setup percpu areas
  152. *
  153. * Arch code has already allocated and initialized percpu areas. All
  154. * this function has to do is to teach the determined layout to the
  155. * dynamic percpu allocator, which happens to be more complex than
  156. * creating whole new ones using helpers.
  157. */
  158. void __init setup_per_cpu_areas(void)
  159. {
  160. struct pcpu_alloc_info *ai;
  161. struct pcpu_group_info *gi;
  162. unsigned int *cpu_map;
  163. void *base;
  164. unsigned long base_offset;
  165. unsigned int cpu;
  166. ssize_t static_size, reserved_size, dyn_size;
  167. int node, prev_node, unit, nr_units;
  168. ai = pcpu_alloc_alloc_info(MAX_NUMNODES, nr_cpu_ids);
  169. if (!ai)
  170. panic("failed to allocate pcpu_alloc_info");
  171. cpu_map = ai->groups[0].cpu_map;
  172. /* determine base */
  173. base = (void *)ULONG_MAX;
  174. for_each_possible_cpu(cpu)
  175. base = min(base,
  176. (void *)(__per_cpu_offset[cpu] + __per_cpu_start));
  177. base_offset = (void *)__per_cpu_start - base;
  178. /* build cpu_map, units are grouped by node */
  179. unit = 0;
  180. for_each_node(node)
  181. for_each_possible_cpu(cpu)
  182. if (node == node_cpuid[cpu].nid)
  183. cpu_map[unit++] = cpu;
  184. nr_units = unit;
  185. /* set basic parameters */
  186. static_size = __per_cpu_end - __per_cpu_start;
  187. reserved_size = PERCPU_MODULE_RESERVE;
  188. dyn_size = PERCPU_PAGE_SIZE - static_size - reserved_size;
  189. if (dyn_size < 0)
  190. panic("percpu area overflow static=%zd reserved=%zd\n",
  191. static_size, reserved_size);
  192. ai->static_size = static_size;
  193. ai->reserved_size = reserved_size;
  194. ai->dyn_size = dyn_size;
  195. ai->unit_size = PERCPU_PAGE_SIZE;
  196. ai->atom_size = PAGE_SIZE;
  197. ai->alloc_size = PERCPU_PAGE_SIZE;
  198. /*
  199. * CPUs are put into groups according to node. Walk cpu_map
  200. * and create new groups at node boundaries.
  201. */
  202. prev_node = NUMA_NO_NODE;
  203. ai->nr_groups = 0;
  204. for (unit = 0; unit < nr_units; unit++) {
  205. cpu = cpu_map[unit];
  206. node = node_cpuid[cpu].nid;
  207. if (node == prev_node) {
  208. gi->nr_units++;
  209. continue;
  210. }
  211. prev_node = node;
  212. gi = &ai->groups[ai->nr_groups++];
  213. gi->nr_units = 1;
  214. gi->base_offset = __per_cpu_offset[cpu] + base_offset;
  215. gi->cpu_map = &cpu_map[unit];
  216. }
  217. pcpu_setup_first_chunk(ai, base);
  218. pcpu_free_alloc_info(ai);
  219. }
  220. #endif
  221. /**
  222. * fill_pernode - initialize pernode data.
  223. * @node: the node id.
  224. * @pernode: physical address of pernode data
  225. * @pernodesize: size of the pernode data
  226. */
  227. static void __init fill_pernode(int node, unsigned long pernode,
  228. unsigned long pernodesize)
  229. {
  230. void *cpu_data;
  231. int cpus = early_nr_cpus_node(node);
  232. mem_data[node].pernode_addr = pernode;
  233. mem_data[node].pernode_size = pernodesize;
  234. memset(__va(pernode), 0, pernodesize);
  235. cpu_data = (void *)pernode;
  236. pernode += PERCPU_PAGE_SIZE * cpus;
  237. pernode += node * L1_CACHE_BYTES;
  238. pgdat_list[node] = __va(pernode);
  239. pernode += L1_CACHE_ALIGN(sizeof(pg_data_t));
  240. mem_data[node].node_data = __va(pernode);
  241. pernode += L1_CACHE_ALIGN(sizeof(struct ia64_node_data));
  242. pernode += L1_CACHE_ALIGN(sizeof(pg_data_t));
  243. cpu_data = per_cpu_node_setup(cpu_data, node);
  244. return;
  245. }
  246. /**
  247. * find_pernode_space - allocate memory for memory map and per-node structures
  248. * @start: physical start of range
  249. * @len: length of range
  250. * @node: node where this range resides
  251. *
  252. * This routine reserves space for the per-cpu data struct, the list of
  253. * pg_data_ts and the per-node data struct. Each node will have something like
  254. * the following in the first chunk of addr. space large enough to hold it.
  255. *
  256. * ________________________
  257. * | |
  258. * |~~~~~~~~~~~~~~~~~~~~~~~~| <-- NODEDATA_ALIGN(start, node) for the first
  259. * | PERCPU_PAGE_SIZE * | start and length big enough
  260. * | cpus_on_this_node | Node 0 will also have entries for all non-existent cpus.
  261. * |------------------------|
  262. * | local pg_data_t * |
  263. * |------------------------|
  264. * | local ia64_node_data |
  265. * |------------------------|
  266. * | ??? |
  267. * |________________________|
  268. *
  269. * Once this space has been set aside, the bootmem maps are initialized. We
  270. * could probably move the allocation of the per-cpu and ia64_node_data space
  271. * outside of this function and use alloc_bootmem_node(), but doing it here
  272. * is straightforward and we get the alignments we want so...
  273. */
  274. static int __init find_pernode_space(unsigned long start, unsigned long len,
  275. int node)
  276. {
  277. unsigned long spfn, epfn;
  278. unsigned long pernodesize = 0, pernode;
  279. spfn = start >> PAGE_SHIFT;
  280. epfn = (start + len) >> PAGE_SHIFT;
  281. /*
  282. * Make sure this memory falls within this node's usable memory
  283. * since we may have thrown some away in build_maps().
  284. */
  285. if (spfn < mem_data[node].min_pfn || epfn > mem_data[node].max_pfn)
  286. return 0;
  287. /* Don't setup this node's local space twice... */
  288. if (mem_data[node].pernode_addr)
  289. return 0;
  290. /*
  291. * Calculate total size needed, incl. what's necessary
  292. * for good alignment and alias prevention.
  293. */
  294. pernodesize = compute_pernodesize(node);
  295. pernode = NODEDATA_ALIGN(start, node);
  296. /* Is this range big enough for what we want to store here? */
  297. if (start + len > (pernode + pernodesize))
  298. fill_pernode(node, pernode, pernodesize);
  299. return 0;
  300. }
  301. /**
  302. * reserve_pernode_space - reserve memory for per-node space
  303. *
  304. * Reserve the space used by the bootmem maps & per-node space in the boot
  305. * allocator so that when we actually create the real mem maps we don't
  306. * use their memory.
  307. */
  308. static void __init reserve_pernode_space(void)
  309. {
  310. unsigned long base, size;
  311. int node;
  312. for_each_online_node(node) {
  313. if (node_isset(node, memory_less_mask))
  314. continue;
  315. /* Now the per-node space */
  316. size = mem_data[node].pernode_size;
  317. base = __pa(mem_data[node].pernode_addr);
  318. memblock_reserve(base, size);
  319. }
  320. }
  321. static void scatter_node_data(void)
  322. {
  323. pg_data_t **dst;
  324. int node;
  325. /*
  326. * for_each_online_node() can't be used at here.
  327. * node_online_map is not set for hot-added nodes at this time,
  328. * because we are halfway through initialization of the new node's
  329. * structures. If for_each_online_node() is used, a new node's
  330. * pg_data_ptrs will be not initialized. Instead of using it,
  331. * pgdat_list[] is checked.
  332. */
  333. for_each_node(node) {
  334. if (pgdat_list[node]) {
  335. dst = LOCAL_DATA_ADDR(pgdat_list[node])->pg_data_ptrs;
  336. memcpy(dst, pgdat_list, sizeof(pgdat_list));
  337. }
  338. }
  339. }
  340. /**
  341. * initialize_pernode_data - fixup per-cpu & per-node pointers
  342. *
  343. * Each node's per-node area has a copy of the global pg_data_t list, so
  344. * we copy that to each node here, as well as setting the per-cpu pointer
  345. * to the local node data structure.
  346. */
  347. static void __init initialize_pernode_data(void)
  348. {
  349. int cpu, node;
  350. scatter_node_data();
  351. #ifdef CONFIG_SMP
  352. /* Set the node_data pointer for each per-cpu struct */
  353. for_each_possible_early_cpu(cpu) {
  354. node = node_cpuid[cpu].nid;
  355. per_cpu(ia64_cpu_info, cpu).node_data =
  356. mem_data[node].node_data;
  357. }
  358. #else
  359. {
  360. struct cpuinfo_ia64 *cpu0_cpu_info;
  361. cpu = 0;
  362. node = node_cpuid[cpu].nid;
  363. cpu0_cpu_info = (struct cpuinfo_ia64 *)(__phys_per_cpu_start +
  364. ((char *)&ia64_cpu_info - __per_cpu_start));
  365. cpu0_cpu_info->node_data = mem_data[node].node_data;
  366. }
  367. #endif /* CONFIG_SMP */
  368. }
  369. /**
  370. * memory_less_node_alloc - * attempt to allocate memory on the best NUMA slit
  371. * node but fall back to any other node when __alloc_bootmem_node fails
  372. * for best.
  373. * @nid: node id
  374. * @pernodesize: size of this node's pernode data
  375. */
  376. static void __init *memory_less_node_alloc(int nid, unsigned long pernodesize)
  377. {
  378. void *ptr = NULL;
  379. u8 best = 0xff;
  380. int bestnode = NUMA_NO_NODE, node, anynode = 0;
  381. for_each_online_node(node) {
  382. if (node_isset(node, memory_less_mask))
  383. continue;
  384. else if (node_distance(nid, node) < best) {
  385. best = node_distance(nid, node);
  386. bestnode = node;
  387. }
  388. anynode = node;
  389. }
  390. if (bestnode == NUMA_NO_NODE)
  391. bestnode = anynode;
  392. ptr = memblock_alloc_try_nid(pernodesize, PERCPU_PAGE_SIZE,
  393. __pa(MAX_DMA_ADDRESS),
  394. MEMBLOCK_ALLOC_ACCESSIBLE,
  395. bestnode);
  396. if (!ptr)
  397. panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%lx\n",
  398. __func__, pernodesize, PERCPU_PAGE_SIZE, bestnode,
  399. __pa(MAX_DMA_ADDRESS));
  400. return ptr;
  401. }
  402. /**
  403. * memory_less_nodes - allocate and initialize CPU only nodes pernode
  404. * information.
  405. */
  406. static void __init memory_less_nodes(void)
  407. {
  408. unsigned long pernodesize;
  409. void *pernode;
  410. int node;
  411. for_each_node_mask(node, memory_less_mask) {
  412. pernodesize = compute_pernodesize(node);
  413. pernode = memory_less_node_alloc(node, pernodesize);
  414. fill_pernode(node, __pa(pernode), pernodesize);
  415. }
  416. return;
  417. }
  418. /**
  419. * find_memory - walk the EFI memory map and setup the bootmem allocator
  420. *
  421. * Called early in boot to setup the bootmem allocator, and to
  422. * allocate the per-cpu and per-node structures.
  423. */
  424. void __init find_memory(void)
  425. {
  426. int node;
  427. reserve_memory();
  428. efi_memmap_walk(filter_memory, register_active_ranges);
  429. if (num_online_nodes() == 0) {
  430. printk(KERN_ERR "node info missing!\n");
  431. node_set_online(0);
  432. }
  433. nodes_or(memory_less_mask, memory_less_mask, node_online_map);
  434. min_low_pfn = -1;
  435. max_low_pfn = 0;
  436. /* These actually end up getting called by call_pernode_memory() */
  437. efi_memmap_walk(filter_rsvd_memory, build_node_maps);
  438. efi_memmap_walk(filter_rsvd_memory, find_pernode_space);
  439. efi_memmap_walk(find_max_min_low_pfn, NULL);
  440. for_each_online_node(node)
  441. if (mem_data[node].min_pfn)
  442. node_clear(node, memory_less_mask);
  443. reserve_pernode_space();
  444. memory_less_nodes();
  445. initialize_pernode_data();
  446. max_pfn = max_low_pfn;
  447. find_initrd();
  448. }
  449. #ifdef CONFIG_SMP
  450. /**
  451. * per_cpu_init - setup per-cpu variables
  452. *
  453. * find_pernode_space() does most of this already, we just need to set
  454. * local_per_cpu_offset
  455. */
  456. void *per_cpu_init(void)
  457. {
  458. int cpu;
  459. static int first_time = 1;
  460. if (first_time) {
  461. first_time = 0;
  462. for_each_possible_early_cpu(cpu)
  463. per_cpu(local_per_cpu_offset, cpu) = __per_cpu_offset[cpu];
  464. }
  465. return __per_cpu_start + __per_cpu_offset[smp_processor_id()];
  466. }
  467. #endif /* CONFIG_SMP */
  468. /**
  469. * call_pernode_memory - use SRAT to call callback functions with node info
  470. * @start: physical start of range
  471. * @len: length of range
  472. * @arg: function to call for each range
  473. *
  474. * efi_memmap_walk() knows nothing about layout of memory across nodes. Find
  475. * out to which node a block of memory belongs. Ignore memory that we cannot
  476. * identify, and split blocks that run across multiple nodes.
  477. *
  478. * Take this opportunity to round the start address up and the end address
  479. * down to page boundaries.
  480. */
  481. void call_pernode_memory(unsigned long start, unsigned long len, void *arg)
  482. {
  483. unsigned long rs, re, end = start + len;
  484. void (*func)(unsigned long, unsigned long, int);
  485. int i;
  486. start = PAGE_ALIGN(start);
  487. end &= PAGE_MASK;
  488. if (start >= end)
  489. return;
  490. func = arg;
  491. if (!num_node_memblks) {
  492. /* No SRAT table, so assume one node (node 0) */
  493. if (start < end)
  494. (*func)(start, end - start, 0);
  495. return;
  496. }
  497. for (i = 0; i < num_node_memblks; i++) {
  498. rs = max(start, node_memblk[i].start_paddr);
  499. re = min(end, node_memblk[i].start_paddr +
  500. node_memblk[i].size);
  501. if (rs < re)
  502. (*func)(rs, re - rs, node_memblk[i].nid);
  503. if (re == end)
  504. break;
  505. }
  506. }
  507. /**
  508. * paging_init - setup page tables
  509. *
  510. * paging_init() sets up the page tables for each node of the system and frees
  511. * the bootmem allocator memory for general use.
  512. */
  513. void __init paging_init(void)
  514. {
  515. unsigned long max_dma;
  516. unsigned long max_zone_pfns[MAX_NR_ZONES];
  517. max_dma = virt_to_phys((void *) MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  518. sparse_init();
  519. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  520. max_zone_pfns[ZONE_DMA32] = max_dma;
  521. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  522. free_area_init(max_zone_pfns);
  523. zero_page_memmap_ptr = virt_to_page(ia64_imva(empty_zero_page));
  524. }
  525. pg_data_t * __init arch_alloc_nodedata(int nid)
  526. {
  527. unsigned long size = compute_pernodesize(nid);
  528. return memblock_alloc(size, SMP_CACHE_BYTES);
  529. }
  530. void arch_refresh_nodedata(int update_node, pg_data_t *update_pgdat)
  531. {
  532. pgdat_list[update_node] = update_pgdat;
  533. scatter_node_data();
  534. }
  535. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  536. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
  537. struct vmem_altmap *altmap)
  538. {
  539. return vmemmap_populate_basepages(start, end, node, NULL);
  540. }
  541. void vmemmap_free(unsigned long start, unsigned long end,
  542. struct vmem_altmap *altmap)
  543. {
  544. }
  545. #endif