18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/mm.h>
88c2ecf20Sopenharmony_ci#include <linux/memblock.h>
98c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
108c2ecf20Sopenharmony_ci#include <linux/initrd.h>
118c2ecf20Sopenharmony_ci#endif
128c2ecf20Sopenharmony_ci#include <linux/of_fdt.h>
138c2ecf20Sopenharmony_ci#include <linux/swap.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/highmem.h>
168c2ecf20Sopenharmony_ci#include <asm/page.h>
178c2ecf20Sopenharmony_ci#include <asm/sections.h>
188c2ecf20Sopenharmony_ci#include <asm/arcregs.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cipgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
218c2ecf20Sopenharmony_cichar empty_zero_page[PAGE_SIZE] __aligned(PAGE_SIZE);
228c2ecf20Sopenharmony_ciEXPORT_SYMBOL(empty_zero_page);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic const unsigned long low_mem_start = CONFIG_LINUX_RAM_BASE;
258c2ecf20Sopenharmony_cistatic unsigned long low_mem_sz;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHMEM
288c2ecf20Sopenharmony_cistatic unsigned long min_high_pfn, max_high_pfn;
298c2ecf20Sopenharmony_cistatic phys_addr_t high_mem_start;
308c2ecf20Sopenharmony_cistatic phys_addr_t high_mem_sz;
318c2ecf20Sopenharmony_ci#endif
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#ifdef CONFIG_DISCONTIGMEM
348c2ecf20Sopenharmony_cistruct pglist_data node_data[MAX_NUMNODES] __read_mostly;
358c2ecf20Sopenharmony_ciEXPORT_SYMBOL(node_data);
368c2ecf20Sopenharmony_ci#endif
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cilong __init arc_get_mem_sz(void)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	return low_mem_sz;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* User can over-ride above with "mem=nnn[KkMm]" in cmdline */
448c2ecf20Sopenharmony_cistatic int __init setup_mem_sz(char *str)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	low_mem_sz = memparse(str, NULL) & PAGE_MASK;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	/* early console might not be setup yet - it will show up later */
498c2ecf20Sopenharmony_ci	pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(low_mem_sz));
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	return 0;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ciearly_param("mem", setup_mem_sz);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_civoid __init early_init_dt_add_memory_arch(u64 base, u64 size)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	int in_use = 0;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if (!low_mem_sz) {
608c2ecf20Sopenharmony_ci		if (base != low_mem_start)
618c2ecf20Sopenharmony_ci			panic("CONFIG_LINUX_RAM_BASE != DT memory { }");
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci		low_mem_sz = size;
648c2ecf20Sopenharmony_ci		in_use = 1;
658c2ecf20Sopenharmony_ci		memblock_add_node(base, size, 0);
668c2ecf20Sopenharmony_ci	} else {
678c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHMEM
688c2ecf20Sopenharmony_ci		high_mem_start = base;
698c2ecf20Sopenharmony_ci		high_mem_sz = size;
708c2ecf20Sopenharmony_ci		in_use = 1;
718c2ecf20Sopenharmony_ci		memblock_add_node(base, size, 1);
728c2ecf20Sopenharmony_ci		memblock_reserve(base, size);
738c2ecf20Sopenharmony_ci#endif
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	pr_info("Memory @ %llx [%lldM] %s\n",
778c2ecf20Sopenharmony_ci		base, TO_MB(size), !in_use ? "Not used":"");
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cibool arch_has_descending_max_zone_pfns(void)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	return !IS_ENABLED(CONFIG_ARC_HAS_PAE40);
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * First memory setup routine called from setup_arch()
878c2ecf20Sopenharmony_ci * 1. setup swapper's mm @init_mm
888c2ecf20Sopenharmony_ci * 2. Count the pages we have and setup bootmem allocator
898c2ecf20Sopenharmony_ci * 3. zone setup
908c2ecf20Sopenharmony_ci */
918c2ecf20Sopenharmony_civoid __init setup_arch_memory(void)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	init_mm.start_code = (unsigned long)_text;
968c2ecf20Sopenharmony_ci	init_mm.end_code = (unsigned long)_etext;
978c2ecf20Sopenharmony_ci	init_mm.end_data = (unsigned long)_edata;
988c2ecf20Sopenharmony_ci	init_mm.brk = (unsigned long)_end;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	/* first page of system - kernel .vector starts here */
1018c2ecf20Sopenharmony_ci	min_low_pfn = ARCH_PFN_OFFSET;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	/* Last usable page of low mem */
1048c2ecf20Sopenharmony_ci	max_low_pfn = max_pfn = PFN_DOWN(low_mem_start + low_mem_sz);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#ifdef CONFIG_FLATMEM
1078c2ecf20Sopenharmony_ci	/* pfn_valid() uses this */
1088c2ecf20Sopenharmony_ci	max_mapnr = max_low_pfn - min_low_pfn;
1098c2ecf20Sopenharmony_ci#endif
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	/*------------- bootmem allocator setup -----------------------*/
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/*
1148c2ecf20Sopenharmony_ci	 * seed the bootmem allocator after any DT memory node parsing or
1158c2ecf20Sopenharmony_ci	 * "mem=xxx" cmdline overrides have potentially updated @arc_mem_sz
1168c2ecf20Sopenharmony_ci	 *
1178c2ecf20Sopenharmony_ci	 * Only low mem is added, otherwise we have crashes when allocating
1188c2ecf20Sopenharmony_ci	 * mem_map[] itself. NO_BOOTMEM allocates mem_map[] at the end of
1198c2ecf20Sopenharmony_ci	 * avail memory, ending in highmem with a > 32-bit address. However
1208c2ecf20Sopenharmony_ci	 * it then tries to memset it with a truncaed 32-bit handle, causing
1218c2ecf20Sopenharmony_ci	 * the crash
1228c2ecf20Sopenharmony_ci	 */
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	memblock_reserve(CONFIG_LINUX_LINK_BASE,
1258c2ecf20Sopenharmony_ci			 __pa(_end) - CONFIG_LINUX_LINK_BASE);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
1288c2ecf20Sopenharmony_ci	if (phys_initrd_size) {
1298c2ecf20Sopenharmony_ci		memblock_reserve(phys_initrd_start, phys_initrd_size);
1308c2ecf20Sopenharmony_ci		initrd_start = (unsigned long)__va(phys_initrd_start);
1318c2ecf20Sopenharmony_ci		initrd_end = initrd_start + phys_initrd_size;
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci#endif
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	early_init_fdt_reserve_self();
1368c2ecf20Sopenharmony_ci	early_init_fdt_scan_reserved_mem();
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	memblock_dump_all();
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	/*----------------- node/zones setup --------------------------*/
1418c2ecf20Sopenharmony_ci	max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHMEM
1448c2ecf20Sopenharmony_ci	/*
1458c2ecf20Sopenharmony_ci	 * Populate a new node with highmem
1468c2ecf20Sopenharmony_ci	 *
1478c2ecf20Sopenharmony_ci	 * On ARC (w/o PAE) HIGHMEM addresses are actually smaller (0 based)
1488c2ecf20Sopenharmony_ci	 * than addresses in normal ala low memory (0x8000_0000 based).
1498c2ecf20Sopenharmony_ci	 * Even with PAE, the huge peripheral space hole would waste a lot of
1508c2ecf20Sopenharmony_ci	 * mem with single mem_map[]. This warrants a mem_map per region design.
1518c2ecf20Sopenharmony_ci	 * Thus HIGHMEM on ARC is imlemented with DISCONTIGMEM.
1528c2ecf20Sopenharmony_ci	 *
1538c2ecf20Sopenharmony_ci	 * DISCONTIGMEM in turns requires multiple nodes. node 0 above is
1548c2ecf20Sopenharmony_ci	 * populated with normal memory zone while node 1 only has highmem
1558c2ecf20Sopenharmony_ci	 */
1568c2ecf20Sopenharmony_ci	node_set_online(1);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	min_high_pfn = PFN_DOWN(high_mem_start);
1598c2ecf20Sopenharmony_ci	max_high_pfn = PFN_DOWN(high_mem_start + high_mem_sz);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	/*
1628c2ecf20Sopenharmony_ci	 * max_high_pfn should be ok here for both HIGHMEM and HIGHMEM+PAE.
1638c2ecf20Sopenharmony_ci	 * For HIGHMEM without PAE max_high_pfn should be less than
1648c2ecf20Sopenharmony_ci	 * min_low_pfn to guarantee that these two regions don't overlap.
1658c2ecf20Sopenharmony_ci	 * For PAE case highmem is greater than lowmem, so it is natural
1668c2ecf20Sopenharmony_ci	 * to use max_high_pfn.
1678c2ecf20Sopenharmony_ci	 *
1688c2ecf20Sopenharmony_ci	 * In both cases, holes should be handled by pfn_valid().
1698c2ecf20Sopenharmony_ci	 */
1708c2ecf20Sopenharmony_ci	max_zone_pfn[ZONE_HIGHMEM] = max_high_pfn;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	high_memory = (void *)(min_high_pfn << PAGE_SHIFT);
1738c2ecf20Sopenharmony_ci	kmap_init();
1748c2ecf20Sopenharmony_ci#endif
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	free_area_init(max_zone_pfn);
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic void __init highmem_init(void)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHMEM
1828c2ecf20Sopenharmony_ci	unsigned long tmp;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	memblock_free(high_mem_start, high_mem_sz);
1858c2ecf20Sopenharmony_ci	for (tmp = min_high_pfn; tmp < max_high_pfn; tmp++)
1868c2ecf20Sopenharmony_ci		free_highmem_page(pfn_to_page(tmp));
1878c2ecf20Sopenharmony_ci#endif
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci/*
1918c2ecf20Sopenharmony_ci * mem_init - initializes memory
1928c2ecf20Sopenharmony_ci *
1938c2ecf20Sopenharmony_ci * Frees up bootmem
1948c2ecf20Sopenharmony_ci * Calculates and displays memory available/used
1958c2ecf20Sopenharmony_ci */
1968c2ecf20Sopenharmony_civoid __init mem_init(void)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	memblock_free_all();
1998c2ecf20Sopenharmony_ci	highmem_init();
2008c2ecf20Sopenharmony_ci	mem_init_print_info(NULL);
2018c2ecf20Sopenharmony_ci}
202