Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/xtensa/mm/init.c |
| 3 | * |
| 4 | * Derived from MIPS, PPC. |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | * |
| 10 | * Copyright (C) 2001 - 2005 Tensilica Inc. |
| 11 | * |
| 12 | * Chris Zankel <chris@zankel.net> |
| 13 | * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com> |
| 14 | * Marc Gauthier |
| 15 | * Kevin Chea |
| 16 | */ |
| 17 | |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/errno.h> |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 20 | #include <linux/bootmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/gfp.h> |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 22 | #include <linux/swap.h> |
Chris Zankel | 6656920 | 2007-08-22 10:14:51 -0700 | [diff] [blame] | 23 | #include <linux/mman.h> |
| 24 | #include <linux/nodemask.h> |
| 25 | #include <linux/mm.h> |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 26 | |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 27 | #include <asm/bootparam.h> |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 28 | #include <asm/page.h> |
Geert Uytterhoeven | f022d0f | 2012-06-20 12:52:59 -0700 | [diff] [blame] | 29 | #include <asm/sections.h> |
Max Filippov | 9ba067f | 2014-03-23 03:17:43 +0400 | [diff] [blame^] | 30 | #include <asm/sysmem.h> |
| 31 | |
| 32 | struct sysmem_info sysmem __initdata; |
| 33 | |
| 34 | int __init add_sysmem_bank(unsigned long start, unsigned long end) |
| 35 | { |
| 36 | if (sysmem.nr_banks >= SYSMEM_BANKS_MAX) { |
| 37 | pr_warn("Ignoring memory bank 0x%08lx size %ldKB\n", |
| 38 | start, end - start); |
| 39 | return -EINVAL; |
| 40 | } |
| 41 | sysmem.bank[sysmem.nr_banks].start = PAGE_ALIGN(start); |
| 42 | sysmem.bank[sysmem.nr_banks].end = end & PAGE_MASK; |
| 43 | sysmem.nr_banks++; |
| 44 | |
| 45 | return 0; |
| 46 | } |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * mem_reserve(start, end, must_exist) |
| 50 | * |
| 51 | * Reserve some memory from the memory pool. |
| 52 | * |
| 53 | * Parameters: |
| 54 | * start Start of region, |
| 55 | * end End of region, |
| 56 | * must_exist Must exist in memory pool. |
| 57 | * |
| 58 | * Returns: |
| 59 | * 0 (memory area couldn't be mapped) |
| 60 | * -1 (success) |
| 61 | */ |
| 62 | |
| 63 | int __init mem_reserve(unsigned long start, unsigned long end, int must_exist) |
| 64 | { |
| 65 | int i; |
| 66 | |
| 67 | if (start == end) |
| 68 | return 0; |
| 69 | |
| 70 | start = start & PAGE_MASK; |
| 71 | end = PAGE_ALIGN(end); |
| 72 | |
| 73 | for (i = 0; i < sysmem.nr_banks; i++) |
| 74 | if (start < sysmem.bank[i].end |
| 75 | && end >= sysmem.bank[i].start) |
| 76 | break; |
| 77 | |
| 78 | if (i == sysmem.nr_banks) { |
| 79 | if (must_exist) |
| 80 | printk (KERN_WARNING "mem_reserve: [0x%0lx, 0x%0lx) " |
| 81 | "not in any region!\n", start, end); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | if (start > sysmem.bank[i].start) { |
| 86 | if (end < sysmem.bank[i].end) { |
| 87 | /* split entry */ |
| 88 | if (sysmem.nr_banks >= SYSMEM_BANKS_MAX) |
| 89 | panic("meminfo overflow\n"); |
| 90 | sysmem.bank[sysmem.nr_banks].start = end; |
| 91 | sysmem.bank[sysmem.nr_banks].end = sysmem.bank[i].end; |
| 92 | sysmem.nr_banks++; |
| 93 | } |
| 94 | sysmem.bank[i].end = start; |
Chris Zankel | c4c4594 | 2012-11-28 16:53:51 -0800 | [diff] [blame] | 95 | |
| 96 | } else if (end < sysmem.bank[i].end) { |
| 97 | sysmem.bank[i].start = end; |
| 98 | |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 99 | } else { |
Chris Zankel | c4c4594 | 2012-11-28 16:53:51 -0800 | [diff] [blame] | 100 | /* remove entry */ |
| 101 | sysmem.nr_banks--; |
| 102 | sysmem.bank[i].start = sysmem.bank[sysmem.nr_banks].start; |
| 103 | sysmem.bank[i].end = sysmem.bank[sysmem.nr_banks].end; |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 104 | } |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /* |
Max Filippov | e9d6dca | 2014-01-31 05:38:21 +0400 | [diff] [blame] | 110 | * Initialize the bootmem system and give it all low memory we have available. |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 111 | */ |
| 112 | |
| 113 | void __init bootmem_init(void) |
| 114 | { |
| 115 | unsigned long pfn; |
| 116 | unsigned long bootmap_start, bootmap_size; |
| 117 | int i; |
| 118 | |
| 119 | max_low_pfn = max_pfn = 0; |
| 120 | min_low_pfn = ~0; |
| 121 | |
| 122 | for (i=0; i < sysmem.nr_banks; i++) { |
| 123 | pfn = PAGE_ALIGN(sysmem.bank[i].start) >> PAGE_SHIFT; |
| 124 | if (pfn < min_low_pfn) |
| 125 | min_low_pfn = pfn; |
| 126 | pfn = PAGE_ALIGN(sysmem.bank[i].end - 1) >> PAGE_SHIFT; |
| 127 | if (pfn > max_pfn) |
| 128 | max_pfn = pfn; |
| 129 | } |
| 130 | |
| 131 | if (min_low_pfn > max_pfn) |
| 132 | panic("No memory found!\n"); |
| 133 | |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 134 | max_low_pfn = max_pfn < MAX_MEM_PFN >> PAGE_SHIFT ? |
| 135 | max_pfn : MAX_MEM_PFN >> PAGE_SHIFT; |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 136 | |
| 137 | /* Find an area to use for the bootmem bitmap. */ |
| 138 | |
Johannes Weiner | 264da9f | 2009-03-04 16:21:29 +0100 | [diff] [blame] | 139 | bootmap_size = bootmem_bootmap_pages(max_low_pfn - min_low_pfn); |
| 140 | bootmap_size <<= PAGE_SHIFT; |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 141 | bootmap_start = ~0; |
| 142 | |
| 143 | for (i=0; i<sysmem.nr_banks; i++) |
| 144 | if (sysmem.bank[i].end - sysmem.bank[i].start >= bootmap_size) { |
| 145 | bootmap_start = sysmem.bank[i].start; |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | if (bootmap_start == ~0UL) |
| 150 | panic("Cannot find %ld bytes for bootmap\n", bootmap_size); |
| 151 | |
| 152 | /* Reserve the bootmem bitmap area */ |
| 153 | |
| 154 | mem_reserve(bootmap_start, bootmap_start + bootmap_size, 1); |
Johannes Weiner | 0bef42e | 2009-03-04 16:21:29 +0100 | [diff] [blame] | 155 | bootmap_size = init_bootmem_node(NODE_DATA(0), |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 156 | bootmap_start >> PAGE_SHIFT, |
Johannes Weiner | 0bef42e | 2009-03-04 16:21:29 +0100 | [diff] [blame] | 157 | min_low_pfn, |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 158 | max_low_pfn); |
| 159 | |
| 160 | /* Add all remaining memory pieces into the bootmem map */ |
| 161 | |
Max Filippov | e9d6dca | 2014-01-31 05:38:21 +0400 | [diff] [blame] | 162 | for (i = 0; i < sysmem.nr_banks; i++) { |
| 163 | if (sysmem.bank[i].start >> PAGE_SHIFT < max_low_pfn) { |
| 164 | unsigned long end = min(max_low_pfn << PAGE_SHIFT, |
| 165 | sysmem.bank[i].end); |
| 166 | free_bootmem(sysmem.bank[i].start, |
| 167 | end - sysmem.bank[i].start); |
| 168 | } |
| 169 | } |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 170 | |
| 171 | } |
| 172 | |
| 173 | |
Johannes Weiner | e5083a6 | 2009-03-04 16:21:31 +0100 | [diff] [blame] | 174 | void __init zones_init(void) |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 175 | { |
| 176 | unsigned long zones_size[MAX_NR_ZONES]; |
| 177 | int i; |
| 178 | |
| 179 | /* All pages are DMA-able, so we put them all in the DMA zone. */ |
| 180 | |
Johannes Weiner | c947a58 | 2009-03-04 16:21:30 +0100 | [diff] [blame] | 181 | zones_size[ZONE_DMA] = max_low_pfn - ARCH_PFN_OFFSET; |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 182 | for (i = 1; i < MAX_NR_ZONES; i++) |
| 183 | zones_size[i] = 0; |
| 184 | |
| 185 | #ifdef CONFIG_HIGHMEM |
| 186 | zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn; |
| 187 | #endif |
| 188 | |
Johannes Weiner | c947a58 | 2009-03-04 16:21:30 +0100 | [diff] [blame] | 189 | free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 193 | * Initialize memory pages. |
| 194 | */ |
| 195 | |
| 196 | void __init mem_init(void) |
| 197 | { |
Jiang Liu | 808c2c3 | 2013-07-03 15:04:20 -0700 | [diff] [blame] | 198 | max_mapnr = max_low_pfn - ARCH_PFN_OFFSET; |
Johannes Weiner | c947a58 | 2009-03-04 16:21:30 +0100 | [diff] [blame] | 199 | high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 200 | |
Chris Zankel | 288a60c | 2005-09-22 21:44:23 -0700 | [diff] [blame] | 201 | #ifdef CONFIG_HIGHMEM |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 202 | #error HIGHGMEM not implemented in init.c |
| 203 | #endif |
| 204 | |
Jiang Liu | 0c98853 | 2013-07-03 15:03:24 -0700 | [diff] [blame] | 205 | free_all_bootmem(); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 206 | |
Jiang Liu | 808c2c3 | 2013-07-03 15:04:20 -0700 | [diff] [blame] | 207 | mem_init_print_info(NULL); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 210 | #ifdef CONFIG_BLK_DEV_INITRD |
| 211 | extern int initrd_is_mapped; |
| 212 | |
| 213 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 214 | { |
Jiang Liu | 7acb2c2 | 2013-04-29 15:06:55 -0700 | [diff] [blame] | 215 | if (initrd_is_mapped) |
Jiang Liu | dbe67df | 2013-07-03 15:02:51 -0700 | [diff] [blame] | 216 | free_reserved_area((void *)start, (void *)end, -1, "initrd"); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 217 | } |
| 218 | #endif |
| 219 | |
| 220 | void free_initmem(void) |
| 221 | { |
Jiang Liu | dbe67df | 2013-07-03 15:02:51 -0700 | [diff] [blame] | 222 | free_initmem_default(-1); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 223 | } |