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> |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * mem_reserve(start, end, must_exist) |
| 33 | * |
| 34 | * Reserve some memory from the memory pool. |
| 35 | * |
| 36 | * Parameters: |
| 37 | * start Start of region, |
| 38 | * end End of region, |
| 39 | * must_exist Must exist in memory pool. |
| 40 | * |
| 41 | * Returns: |
| 42 | * 0 (memory area couldn't be mapped) |
| 43 | * -1 (success) |
| 44 | */ |
| 45 | |
| 46 | int __init mem_reserve(unsigned long start, unsigned long end, int must_exist) |
| 47 | { |
| 48 | int i; |
| 49 | |
| 50 | if (start == end) |
| 51 | return 0; |
| 52 | |
| 53 | start = start & PAGE_MASK; |
| 54 | end = PAGE_ALIGN(end); |
| 55 | |
| 56 | for (i = 0; i < sysmem.nr_banks; i++) |
| 57 | if (start < sysmem.bank[i].end |
| 58 | && end >= sysmem.bank[i].start) |
| 59 | break; |
| 60 | |
| 61 | if (i == sysmem.nr_banks) { |
| 62 | if (must_exist) |
| 63 | printk (KERN_WARNING "mem_reserve: [0x%0lx, 0x%0lx) " |
| 64 | "not in any region!\n", start, end); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | if (start > sysmem.bank[i].start) { |
| 69 | if (end < sysmem.bank[i].end) { |
| 70 | /* split entry */ |
| 71 | if (sysmem.nr_banks >= SYSMEM_BANKS_MAX) |
| 72 | panic("meminfo overflow\n"); |
| 73 | sysmem.bank[sysmem.nr_banks].start = end; |
| 74 | sysmem.bank[sysmem.nr_banks].end = sysmem.bank[i].end; |
| 75 | sysmem.nr_banks++; |
| 76 | } |
| 77 | sysmem.bank[i].end = start; |
Chris Zankel | c4c4594 | 2012-11-28 16:53:51 -0800 | [diff] [blame] | 78 | |
| 79 | } else if (end < sysmem.bank[i].end) { |
| 80 | sysmem.bank[i].start = end; |
| 81 | |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 82 | } else { |
Chris Zankel | c4c4594 | 2012-11-28 16:53:51 -0800 | [diff] [blame] | 83 | /* remove entry */ |
| 84 | sysmem.nr_banks--; |
| 85 | sysmem.bank[i].start = sysmem.bank[sysmem.nr_banks].start; |
| 86 | sysmem.bank[i].end = sysmem.bank[sysmem.nr_banks].end; |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 87 | } |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /* |
Max Filippov | e9d6dca | 2014-01-31 05:38:21 +0400 | [diff] [blame] | 93 | * 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] | 94 | */ |
| 95 | |
| 96 | void __init bootmem_init(void) |
| 97 | { |
| 98 | unsigned long pfn; |
| 99 | unsigned long bootmap_start, bootmap_size; |
| 100 | int i; |
| 101 | |
| 102 | max_low_pfn = max_pfn = 0; |
| 103 | min_low_pfn = ~0; |
| 104 | |
| 105 | for (i=0; i < sysmem.nr_banks; i++) { |
| 106 | pfn = PAGE_ALIGN(sysmem.bank[i].start) >> PAGE_SHIFT; |
| 107 | if (pfn < min_low_pfn) |
| 108 | min_low_pfn = pfn; |
| 109 | pfn = PAGE_ALIGN(sysmem.bank[i].end - 1) >> PAGE_SHIFT; |
| 110 | if (pfn > max_pfn) |
| 111 | max_pfn = pfn; |
| 112 | } |
| 113 | |
| 114 | if (min_low_pfn > max_pfn) |
| 115 | panic("No memory found!\n"); |
| 116 | |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 117 | max_low_pfn = max_pfn < MAX_MEM_PFN >> PAGE_SHIFT ? |
| 118 | max_pfn : MAX_MEM_PFN >> PAGE_SHIFT; |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 119 | |
| 120 | /* Find an area to use for the bootmem bitmap. */ |
| 121 | |
Johannes Weiner | 264da9f | 2009-03-04 16:21:29 +0100 | [diff] [blame] | 122 | bootmap_size = bootmem_bootmap_pages(max_low_pfn - min_low_pfn); |
| 123 | bootmap_size <<= PAGE_SHIFT; |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 124 | bootmap_start = ~0; |
| 125 | |
| 126 | for (i=0; i<sysmem.nr_banks; i++) |
| 127 | if (sysmem.bank[i].end - sysmem.bank[i].start >= bootmap_size) { |
| 128 | bootmap_start = sysmem.bank[i].start; |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | if (bootmap_start == ~0UL) |
| 133 | panic("Cannot find %ld bytes for bootmap\n", bootmap_size); |
| 134 | |
| 135 | /* Reserve the bootmem bitmap area */ |
| 136 | |
| 137 | mem_reserve(bootmap_start, bootmap_start + bootmap_size, 1); |
Johannes Weiner | 0bef42e | 2009-03-04 16:21:29 +0100 | [diff] [blame] | 138 | bootmap_size = init_bootmem_node(NODE_DATA(0), |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 139 | bootmap_start >> PAGE_SHIFT, |
Johannes Weiner | 0bef42e | 2009-03-04 16:21:29 +0100 | [diff] [blame] | 140 | min_low_pfn, |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 141 | max_low_pfn); |
| 142 | |
| 143 | /* Add all remaining memory pieces into the bootmem map */ |
| 144 | |
Max Filippov | e9d6dca | 2014-01-31 05:38:21 +0400 | [diff] [blame] | 145 | for (i = 0; i < sysmem.nr_banks; i++) { |
| 146 | if (sysmem.bank[i].start >> PAGE_SHIFT < max_low_pfn) { |
| 147 | unsigned long end = min(max_low_pfn << PAGE_SHIFT, |
| 148 | sysmem.bank[i].end); |
| 149 | free_bootmem(sysmem.bank[i].start, |
| 150 | end - sysmem.bank[i].start); |
| 151 | } |
| 152 | } |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 153 | |
| 154 | } |
| 155 | |
| 156 | |
Johannes Weiner | e5083a6 | 2009-03-04 16:21:31 +0100 | [diff] [blame] | 157 | void __init zones_init(void) |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 158 | { |
| 159 | unsigned long zones_size[MAX_NR_ZONES]; |
| 160 | int i; |
| 161 | |
| 162 | /* All pages are DMA-able, so we put them all in the DMA zone. */ |
| 163 | |
Johannes Weiner | c947a58 | 2009-03-04 16:21:30 +0100 | [diff] [blame] | 164 | zones_size[ZONE_DMA] = max_low_pfn - ARCH_PFN_OFFSET; |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 165 | for (i = 1; i < MAX_NR_ZONES; i++) |
| 166 | zones_size[i] = 0; |
| 167 | |
| 168 | #ifdef CONFIG_HIGHMEM |
| 169 | zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn; |
| 170 | #endif |
| 171 | |
Johannes Weiner | c947a58 | 2009-03-04 16:21:30 +0100 | [diff] [blame] | 172 | free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 176 | * Initialize memory pages. |
| 177 | */ |
| 178 | |
| 179 | void __init mem_init(void) |
| 180 | { |
Jiang Liu | 808c2c3 | 2013-07-03 15:04:20 -0700 | [diff] [blame] | 181 | max_mapnr = max_low_pfn - ARCH_PFN_OFFSET; |
Johannes Weiner | c947a58 | 2009-03-04 16:21:30 +0100 | [diff] [blame] | 182 | high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 183 | |
Chris Zankel | 288a60c | 2005-09-22 21:44:23 -0700 | [diff] [blame] | 184 | #ifdef CONFIG_HIGHMEM |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 185 | #error HIGHGMEM not implemented in init.c |
| 186 | #endif |
| 187 | |
Jiang Liu | 0c98853 | 2013-07-03 15:03:24 -0700 | [diff] [blame] | 188 | free_all_bootmem(); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 189 | |
Jiang Liu | 808c2c3 | 2013-07-03 15:04:20 -0700 | [diff] [blame] | 190 | mem_init_print_info(NULL); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 193 | #ifdef CONFIG_BLK_DEV_INITRD |
| 194 | extern int initrd_is_mapped; |
| 195 | |
| 196 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 197 | { |
Jiang Liu | 7acb2c2 | 2013-04-29 15:06:55 -0700 | [diff] [blame] | 198 | if (initrd_is_mapped) |
Jiang Liu | dbe67df | 2013-07-03 15:02:51 -0700 | [diff] [blame] | 199 | free_reserved_area((void *)start, (void *)end, -1, "initrd"); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 200 | } |
| 201 | #endif |
| 202 | |
| 203 | void free_initmem(void) |
| 204 | { |
Jiang Liu | dbe67df | 2013-07-03 15:02:51 -0700 | [diff] [blame] | 205 | free_initmem_default(-1); |
Chris Zankel | 3f65ce4 | 2005-06-23 22:01:24 -0700 | [diff] [blame] | 206 | } |