Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/bootmem.h> |
| 12 | #include <linux/memblock.h> |
Noam Camus | d6579e9 | 2012-09-09 11:37:40 +0300 | [diff] [blame] | 13 | #ifdef CONFIG_BLK_DEV_INITRD |
| 14 | #include <linux/initrd.h> |
| 15 | #endif |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 16 | #include <linux/swap.h> |
| 17 | #include <linux/module.h> |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 18 | #include <linux/highmem.h> |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 19 | #include <asm/page.h> |
| 20 | #include <asm/pgalloc.h> |
| 21 | #include <asm/sections.h> |
| 22 | #include <asm/arcregs.h> |
| 23 | |
| 24 | pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE); |
| 25 | char empty_zero_page[PAGE_SIZE] __aligned(PAGE_SIZE); |
| 26 | EXPORT_SYMBOL(empty_zero_page); |
| 27 | |
Vineet Gupta | 6101be5 | 2015-10-28 18:48:17 +0530 | [diff] [blame] | 28 | static const unsigned long low_mem_start = CONFIG_LINUX_LINK_BASE; |
| 29 | static unsigned long low_mem_sz; |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 30 | |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 31 | #ifdef CONFIG_HIGHMEM |
| 32 | static unsigned long min_high_pfn; |
| 33 | static u64 high_mem_start; |
| 34 | static u64 high_mem_sz; |
| 35 | #endif |
| 36 | |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 37 | /* User can over-ride above with "mem=nnn[KkMm]" in cmdline */ |
| 38 | static int __init setup_mem_sz(char *str) |
| 39 | { |
Vineet Gupta | 6101be5 | 2015-10-28 18:48:17 +0530 | [diff] [blame] | 40 | low_mem_sz = memparse(str, NULL) & PAGE_MASK; |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 41 | |
| 42 | /* early console might not be setup yet - it will show up later */ |
Vineet Gupta | 6101be5 | 2015-10-28 18:48:17 +0530 | [diff] [blame] | 43 | pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(low_mem_sz)); |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | early_param("mem", setup_mem_sz); |
| 48 | |
Vineet Gupta | 999159a | 2013-01-22 17:00:52 +0530 | [diff] [blame] | 49 | void __init early_init_dt_add_memory_arch(u64 base, u64 size) |
| 50 | { |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 51 | int in_use = 0; |
Vineet Gupta | f759ee5 | 2015-01-23 18:10:26 +0530 | [diff] [blame] | 52 | |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 53 | if (!low_mem_sz) { |
| 54 | BUG_ON(base != low_mem_start); |
| 55 | low_mem_sz = size; |
| 56 | in_use = 1; |
| 57 | } else { |
| 58 | #ifdef CONFIG_HIGHMEM |
| 59 | high_mem_start = base; |
| 60 | high_mem_sz = size; |
| 61 | in_use = 1; |
| 62 | #endif |
| 63 | } |
| 64 | |
| 65 | pr_info("Memory @ %llx [%lldM] %s\n", |
| 66 | base, TO_MB(size), !in_use ? "Not used":""); |
Vineet Gupta | 999159a | 2013-01-22 17:00:52 +0530 | [diff] [blame] | 67 | } |
| 68 | |
Noam Camus | d6579e9 | 2012-09-09 11:37:40 +0300 | [diff] [blame] | 69 | #ifdef CONFIG_BLK_DEV_INITRD |
| 70 | static int __init early_initrd(char *p) |
| 71 | { |
| 72 | unsigned long start, size; |
| 73 | char *endp; |
| 74 | |
| 75 | start = memparse(p, &endp); |
| 76 | if (*endp == ',') { |
| 77 | size = memparse(endp + 1, NULL); |
| 78 | |
| 79 | initrd_start = (unsigned long)__va(start); |
| 80 | initrd_end = (unsigned long)__va(start + size); |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | early_param("initrd", early_initrd); |
| 85 | #endif |
| 86 | |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 87 | /* |
| 88 | * First memory setup routine called from setup_arch() |
| 89 | * 1. setup swapper's mm @init_mm |
| 90 | * 2. Count the pages we have and setup bootmem allocator |
| 91 | * 3. zone setup |
| 92 | */ |
| 93 | void __init setup_arch_memory(void) |
| 94 | { |
Vineet Gupta | f2e2013f7 | 2015-02-13 13:59:53 +0530 | [diff] [blame] | 95 | unsigned long zones_size[MAX_NR_ZONES]; |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 96 | unsigned long zones_holes[MAX_NR_ZONES]; |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 97 | |
| 98 | init_mm.start_code = (unsigned long)_text; |
| 99 | init_mm.end_code = (unsigned long)_etext; |
| 100 | init_mm.end_data = (unsigned long)_edata; |
| 101 | init_mm.brk = (unsigned long)_end; |
| 102 | |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 103 | /* first page of system - kernel .vector starts here */ |
Vineet Gupta | f2e2013f7 | 2015-02-13 13:59:53 +0530 | [diff] [blame] | 104 | min_low_pfn = ARCH_PFN_OFFSET; |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 105 | |
Vineet Gupta | 6101be5 | 2015-10-28 18:48:17 +0530 | [diff] [blame] | 106 | /* Last usable page of low mem */ |
| 107 | max_low_pfn = max_pfn = PFN_DOWN(low_mem_start + low_mem_sz); |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 108 | |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 109 | #ifdef CONFIG_HIGHMEM |
| 110 | min_high_pfn = PFN_DOWN(high_mem_start); |
| 111 | max_pfn = PFN_DOWN(high_mem_start + high_mem_sz); |
| 112 | #endif |
| 113 | |
| 114 | max_mapnr = max_pfn - min_low_pfn; |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 115 | |
Vineet Gupta | 6101be5 | 2015-10-28 18:48:17 +0530 | [diff] [blame] | 116 | /*------------- bootmem allocator setup -----------------------*/ |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * seed the bootmem allocator after any DT memory node parsing or |
| 120 | * "mem=xxx" cmdline overrides have potentially updated @arc_mem_sz |
| 121 | * |
| 122 | * Only low mem is added, otherwise we have crashes when allocating |
| 123 | * mem_map[] itself. NO_BOOTMEM allocates mem_map[] at the end of |
| 124 | * avail memory, ending in highmem with a > 32-bit address. However |
| 125 | * it then tries to memset it with a truncaed 32-bit handle, causing |
| 126 | * the crash |
| 127 | */ |
| 128 | |
Vineet Gupta | 6101be5 | 2015-10-28 18:48:17 +0530 | [diff] [blame] | 129 | memblock_add(low_mem_start, low_mem_sz); |
| 130 | memblock_reserve(low_mem_start, __pa(_end) - low_mem_start); |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 131 | |
Noam Camus | d6579e9 | 2012-09-09 11:37:40 +0300 | [diff] [blame] | 132 | #ifdef CONFIG_BLK_DEV_INITRD |
Noam Camus | d6579e9 | 2012-09-09 11:37:40 +0300 | [diff] [blame] | 133 | if (initrd_start) |
| 134 | memblock_reserve(__pa(initrd_start), initrd_end - initrd_start); |
| 135 | #endif |
| 136 | |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 137 | memblock_dump_all(); |
| 138 | |
Vineet Gupta | 6101be5 | 2015-10-28 18:48:17 +0530 | [diff] [blame] | 139 | /*----------------- node/zones setup --------------------------*/ |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 140 | memset(zones_size, 0, sizeof(zones_size)); |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 141 | memset(zones_holes, 0, sizeof(zones_holes)); |
| 142 | |
Vineet Gupta | 6101be5 | 2015-10-28 18:48:17 +0530 | [diff] [blame] | 143 | zones_size[ZONE_NORMAL] = max_low_pfn - min_low_pfn; |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 144 | zones_holes[ZONE_NORMAL] = 0; |
| 145 | |
| 146 | #ifdef CONFIG_HIGHMEM |
| 147 | zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn; |
| 148 | |
| 149 | /* This handles the peripheral address space hole */ |
| 150 | zones_holes[ZONE_HIGHMEM] = min_high_pfn - max_low_pfn; |
| 151 | #endif |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * We can't use the helper free_area_init(zones[]) because it uses |
| 155 | * PAGE_OFFSET to compute the @min_low_pfn which would be wrong |
| 156 | * when our kernel doesn't start at PAGE_OFFSET, i.e. |
| 157 | * PAGE_OFFSET != CONFIG_LINUX_LINK_BASE |
| 158 | */ |
| 159 | free_area_init_node(0, /* node-id */ |
| 160 | zones_size, /* num pages per zone */ |
| 161 | min_low_pfn, /* first pfn of node */ |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 162 | zones_holes); /* holes */ |
Vineet Gupta | f2e2013f7 | 2015-02-13 13:59:53 +0530 | [diff] [blame] | 163 | |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 164 | #ifdef CONFIG_HIGHMEM |
| 165 | high_memory = (void *)(min_high_pfn << PAGE_SHIFT); |
| 166 | kmap_init(); |
| 167 | #endif |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* |
| 171 | * mem_init - initializes memory |
| 172 | * |
| 173 | * Frees up bootmem |
| 174 | * Calculates and displays memory available/used |
| 175 | */ |
| 176 | void __init mem_init(void) |
| 177 | { |
Vineet Gupta | 29e3322 | 2015-10-28 19:06:10 +0530 | [diff] [blame] | 178 | #ifdef CONFIG_HIGHMEM |
| 179 | unsigned long tmp; |
| 180 | |
| 181 | reset_all_zones_managed_pages(); |
| 182 | for (tmp = min_high_pfn; tmp < max_pfn; tmp++) |
| 183 | free_highmem_page(pfn_to_page(tmp)); |
| 184 | #endif |
| 185 | |
Jiang Liu | 0c98853 | 2013-07-03 15:03:24 -0700 | [diff] [blame] | 186 | free_all_bootmem(); |
Jiang Liu | de35e1b | 2013-07-03 15:03:47 -0700 | [diff] [blame] | 187 | mem_init_print_info(NULL); |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 188 | } |
| 189 | |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 190 | /* |
| 191 | * free_initmem: Free all the __init memory. |
| 192 | */ |
| 193 | void __init_refok free_initmem(void) |
| 194 | { |
Jiang Liu | dbe67df | 2013-07-03 15:02:51 -0700 | [diff] [blame] | 195 | free_initmem_default(-1); |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | #ifdef CONFIG_BLK_DEV_INITRD |
| 199 | void __init free_initrd_mem(unsigned long start, unsigned long end) |
| 200 | { |
Jiang Liu | dbe67df | 2013-07-03 15:02:51 -0700 | [diff] [blame] | 201 | free_reserved_area((void *)start, (void *)end, -1, "initrd"); |
Vineet Gupta | c121c50 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 202 | } |
| 203 | #endif |