1 /* 2 * Copyright (C) 2007-2008 Michal Simek <monstr@monstr.eu> 3 * Copyright (C) 2006 Atmark Techno, Inc. 4 * 5 * This file is subject to the terms and conditions of the GNU General Public 6 * License. See the file "COPYING" in the main directory of this archive 7 * for more details. 8 */ 9 10 #include <linux/dma-map-ops.h> 11 #include <linux/memblock.h> 12 #include <linux/init.h> 13 #include <linux/kernel.h> 14 #include <linux/mm.h> /* mem_init */ 15 #include <linux/initrd.h> 16 #include <linux/of_fdt.h> 17 #include <linux/pagemap.h> 18 #include <linux/pfn.h> 19 #include <linux/slab.h> 20 #include <linux/swap.h> 21 #include <linux/export.h> 22 23 #include <asm/page.h> 24 #include <asm/mmu_context.h> 25 #include <asm/pgalloc.h> 26 #include <asm/sections.h> 27 #include <asm/tlb.h> 28 #include <asm/fixmap.h> 29 30 /* Use for MMU and noMMU because of PCI generic code */ 31 int mem_init_done; 32 33 char *klimit = _end; 34 35 /* 36 * Initialize the bootmem system and give it all the memory we 37 * have available. 38 */ 39 unsigned long memory_start; 40 EXPORT_SYMBOL(memory_start); 41 unsigned long memory_size; 42 EXPORT_SYMBOL(memory_size); 43 unsigned long lowmem_size; 44 45 EXPORT_SYMBOL(min_low_pfn); 46 EXPORT_SYMBOL(max_low_pfn); 47 48 #ifdef CONFIG_HIGHMEM 49 static void __init highmem_init(void) 50 { 51 pr_debug("%x\n", (u32)PKMAP_BASE); 52 map_page(PKMAP_BASE, 0, 0); /* XXX gross */ 53 pkmap_page_table = virt_to_kpte(PKMAP_BASE); 54 } 55 #endif /* CONFIG_HIGHMEM */ 56 57 /* 58 * paging_init() sets up the page tables - in fact we've already done this. 59 */ 60 static void __init paging_init(void) 61 { 62 unsigned long zones_size[MAX_NR_ZONES]; 63 int idx; 64 65 /* Setup fixmaps */ 66 for (idx = 0; idx < __end_of_fixed_addresses; idx++) 67 clear_fixmap(idx); 68 69 /* Clean every zones */ 70 memset(zones_size, 0, sizeof(zones_size)); 71 72 #ifdef CONFIG_HIGHMEM 73 highmem_init(); 74 75 zones_size[ZONE_DMA] = max_low_pfn; 76 zones_size[ZONE_HIGHMEM] = max_pfn; 77 #else 78 zones_size[ZONE_DMA] = max_pfn; 79 #endif 80 81 /* We don't have holes in memory map */ 82 free_area_init(zones_size); 83 } 84 85 void __init setup_memory(void) 86 { 87 /* 88 * Kernel: 89 * start: base phys address of kernel - page align 90 * end: base phys address of kernel - page align 91 * 92 * min_low_pfn - the first page (mm/bootmem.c - node_boot_start) 93 * max_low_pfn 94 */ 95 96 /* memory start is from the kernel end (aligned) to higher addr */ 97 min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */ 98 max_low_pfn = ((u64)memory_start + (u64)lowmem_size) >> PAGE_SHIFT; 99 max_pfn = ((u64)memory_start + (u64)memory_size) >> PAGE_SHIFT; 100 101 pr_info("%s: min_low_pfn: %#lx\n", __func__, min_low_pfn); 102 pr_info("%s: max_low_pfn: %#lx\n", __func__, max_low_pfn); 103 pr_info("%s: max_pfn: %#lx\n", __func__, max_pfn); 104 105 paging_init(); 106 } 107 108 void __init mem_init(void) 109 { 110 mem_init_done = 1; 111 } 112 113 int page_is_ram(unsigned long pfn) 114 { 115 return pfn < max_low_pfn; 116 } 117 118 /* 119 * Check for command-line options that affect what MMU_init will do. 120 */ 121 static void __init mm_cmdline_setup(void) 122 { 123 unsigned long maxmem = 0; 124 char *p = cmd_line; 125 126 /* Look for mem= option on command line */ 127 p = strstr(cmd_line, "mem="); 128 if (p) { 129 p += 4; 130 maxmem = memparse(p, &p); 131 if (maxmem && memory_size > maxmem) { 132 memory_size = maxmem; 133 memblock.memory.regions[0].size = memory_size; 134 } 135 } 136 } 137 138 /* 139 * MMU_init_hw does the chip-specific initialization of the MMU hardware. 140 */ 141 static void __init mmu_init_hw(void) 142 { 143 /* 144 * The Zone Protection Register (ZPR) defines how protection will 145 * be applied to every page which is a member of a given zone. At 146 * present, we utilize only two of the zones. 147 * The zone index bits (of ZSEL) in the PTE are used for software 148 * indicators, except the LSB. For user access, zone 1 is used, 149 * for kernel access, zone 0 is used. We set all but zone 1 150 * to zero, allowing only kernel access as indicated in the PTE. 151 * For zone 1, we set a 01 binary (a value of 10 will not work) 152 * to allow user access as indicated in the PTE. This also allows 153 * kernel access as indicated in the PTE. 154 */ 155 __asm__ __volatile__ ("ori r11, r0, 0x10000000;" \ 156 "mts rzpr, r11;" 157 : : : "r11"); 158 } 159 160 /* 161 * MMU_init sets up the basic memory mappings for the kernel, 162 * including both RAM and possibly some I/O regions, 163 * and sets up the page tables and the MMU hardware ready to go. 164 */ 165 166 /* called from head.S */ 167 asmlinkage void __init mmu_init(void) 168 { 169 unsigned int kstart, ksize; 170 171 if ((u32) memblock.memory.regions[0].size < 0x400000) { 172 pr_emerg("Memory must be greater than 4MB\n"); 173 machine_restart(NULL); 174 } 175 176 if ((u32) memblock.memory.regions[0].size < kernel_tlb) { 177 pr_emerg("Kernel size is greater than memory node\n"); 178 machine_restart(NULL); 179 } 180 181 /* Find main memory where the kernel is */ 182 memory_start = (u32) memblock.memory.regions[0].base; 183 lowmem_size = memory_size = (u32) memblock.memory.regions[0].size; 184 185 if (lowmem_size > CONFIG_LOWMEM_SIZE) { 186 lowmem_size = CONFIG_LOWMEM_SIZE; 187 #ifndef CONFIG_HIGHMEM 188 memory_size = lowmem_size; 189 #endif 190 } 191 192 mm_cmdline_setup(); /* FIXME parse args from command line - not used */ 193 194 /* 195 * Map out the kernel text/data/bss from the available physical 196 * memory. 197 */ 198 kstart = __pa(CONFIG_KERNEL_START); /* kernel start */ 199 /* kernel size */ 200 ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START)); 201 memblock_reserve(kstart, ksize); 202 203 #if defined(CONFIG_BLK_DEV_INITRD) 204 /* Remove the init RAM disk from the available memory. */ 205 if (initrd_start) { 206 unsigned long size; 207 size = initrd_end - initrd_start; 208 memblock_reserve(__virt_to_phys(initrd_start), size); 209 } 210 #endif /* CONFIG_BLK_DEV_INITRD */ 211 212 /* Initialize the MMU hardware */ 213 mmu_init_hw(); 214 215 /* Map in all of RAM starting at CONFIG_KERNEL_START */ 216 mapin_ram(); 217 218 /* Extend vmalloc and ioremap area as big as possible */ 219 #ifdef CONFIG_HIGHMEM 220 ioremap_base = ioremap_bot = PKMAP_BASE; 221 #else 222 ioremap_base = ioremap_bot = FIXADDR_START; 223 #endif 224 225 /* Initialize the context management stuff */ 226 mmu_context_init(); 227 228 /* Shortly after that, the entire linear mapping will be available */ 229 /* This will also cause that unflatten device tree will be allocated 230 * inside 768MB limit */ 231 memblock_set_current_limit(memory_start + lowmem_size - 1); 232 233 parse_early_param(); 234 235 early_init_fdt_scan_reserved_mem(); 236 237 /* CMA initialization */ 238 dma_contiguous_reserve(memory_start + lowmem_size - 1); 239 240 memblock_dump_all(); 241 } 242 243 static const pgprot_t protection_map[16] = { 244 [VM_NONE] = PAGE_NONE, 245 [VM_READ] = PAGE_READONLY_X, 246 [VM_WRITE] = PAGE_COPY, 247 [VM_WRITE | VM_READ] = PAGE_COPY_X, 248 [VM_EXEC] = PAGE_READONLY, 249 [VM_EXEC | VM_READ] = PAGE_READONLY_X, 250 [VM_EXEC | VM_WRITE] = PAGE_COPY, 251 [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_X, 252 [VM_SHARED] = PAGE_NONE, 253 [VM_SHARED | VM_READ] = PAGE_READONLY_X, 254 [VM_SHARED | VM_WRITE] = PAGE_SHARED, 255 [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED_X, 256 [VM_SHARED | VM_EXEC] = PAGE_READONLY, 257 [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY_X, 258 [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED, 259 [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_X 260 }; 261 DECLARE_VM_GET_PAGE_PROT 262