1 /* 2 * Memory subsystem initialization for Hexagon 3 * 4 * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 and 8 * only version 2 as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 18 * 02110-1301, USA. 19 */ 20 21 #include <linux/init.h> 22 #include <linux/mm.h> 23 #include <linux/bootmem.h> 24 #include <linux/memblock.h> 25 #include <asm/atomic.h> 26 #include <linux/highmem.h> 27 #include <asm/tlb.h> 28 #include <asm/sections.h> 29 #include <asm/vm_mmu.h> 30 31 /* 32 * Define a startpg just past the end of the kernel image and a lastpg 33 * that corresponds to the end of real or simulated platform memory. 34 */ 35 #define bootmem_startpg (PFN_UP(((unsigned long) _end) - PAGE_OFFSET + PHYS_OFFSET)) 36 37 unsigned long bootmem_lastpg; /* Should be set by platform code */ 38 unsigned long __phys_offset; /* physical kernel offset >> 12 */ 39 40 /* Set as variable to limit PMD copies */ 41 int max_kernel_seg = 0x303; 42 43 /* indicate pfn's of high memory */ 44 unsigned long highstart_pfn, highend_pfn; 45 46 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); 47 48 /* Default cache attribute for newly created page tables */ 49 unsigned long _dflt_cache_att = CACHEDEF; 50 51 /* 52 * The current "generation" of kernel map, which should not roll 53 * over until Hell freezes over. Actual bound in years needs to be 54 * calculated to confirm. 55 */ 56 DEFINE_SPINLOCK(kmap_gen_lock); 57 58 /* checkpatch says don't init this to 0. */ 59 unsigned long long kmap_generation; 60 61 /* 62 * mem_init - initializes memory 63 * 64 * Frees up bootmem 65 * Fixes up more stuff for HIGHMEM 66 * Calculates and displays memory available/used 67 */ 68 void __init mem_init(void) 69 { 70 /* No idea where this is actually declared. Seems to evade LXR. */ 71 free_all_bootmem(); 72 mem_init_print_info(NULL); 73 74 /* 75 * To-Do: someone somewhere should wipe out the bootmem map 76 * after we're done? 77 */ 78 79 /* 80 * This can be moved to some more virtual-memory-specific 81 * initialization hook at some point. Set the init_mm 82 * descriptors "context" value to point to the initial 83 * kernel segment table's physical address. 84 */ 85 init_mm.context.ptbase = __pa(init_mm.pgd); 86 } 87 88 /* 89 * free_initmem - frees memory used by stuff declared with __init 90 * 91 * Todo: free pages between __init_begin and __init_end; possibly 92 * some devtree related stuff as well. 93 */ 94 void __ref free_initmem(void) 95 { 96 } 97 98 /* 99 * free_initrd_mem - frees... initrd memory. 100 * @start - start of init memory 101 * @end - end of init memory 102 * 103 * Apparently has to be passed the address of the initrd memory. 104 * 105 * Wrapped by #ifdef CONFIG_BLKDEV_INITRD 106 */ 107 void free_initrd_mem(unsigned long start, unsigned long end) 108 { 109 } 110 111 void sync_icache_dcache(pte_t pte) 112 { 113 unsigned long addr; 114 struct page *page; 115 116 page = pte_page(pte); 117 addr = (unsigned long) page_address(page); 118 119 __vmcache_idsync(addr, PAGE_SIZE); 120 } 121 122 /* 123 * In order to set up page allocator "nodes", 124 * somebody has to call free_area_init() for UMA. 125 * 126 * In this mode, we only have one pg_data_t 127 * structure: contig_mem_data. 128 */ 129 void __init paging_init(void) 130 { 131 unsigned long zones_sizes[MAX_NR_ZONES] = {0, }; 132 133 /* 134 * This is not particularly well documented anywhere, but 135 * give ZONE_NORMAL all the memory, including the big holes 136 * left by the kernel+bootmem_map which are already left as reserved 137 * in the bootmem_map; free_area_init should see those bits and 138 * adjust accordingly. 139 */ 140 141 zones_sizes[ZONE_NORMAL] = max_low_pfn; 142 143 free_area_init(zones_sizes); /* sets up the zonelists and mem_map */ 144 145 /* 146 * Start of high memory area. Will probably need something more 147 * fancy if we... get more fancy. 148 */ 149 high_memory = (void *)((bootmem_lastpg + 1) << PAGE_SHIFT); 150 } 151 152 #ifndef DMA_RESERVE 153 #define DMA_RESERVE (4) 154 #endif 155 156 #define DMA_CHUNKSIZE (1<<22) 157 #define DMA_RESERVED_BYTES (DMA_RESERVE * DMA_CHUNKSIZE) 158 159 /* 160 * Pick out the memory size. We look for mem=size, 161 * where size is "size[KkMm]" 162 */ 163 static int __init early_mem(char *p) 164 { 165 unsigned long size; 166 char *endp; 167 168 size = memparse(p, &endp); 169 170 bootmem_lastpg = PFN_DOWN(size); 171 172 return 0; 173 } 174 early_param("mem", early_mem); 175 176 size_t hexagon_coherent_pool_size = (size_t) (DMA_RESERVE << 22); 177 178 void __init setup_arch_memory(void) 179 { 180 /* XXX Todo: this probably should be cleaned up */ 181 u32 *segtable = (u32 *) &swapper_pg_dir[0]; 182 u32 *segtable_end; 183 184 /* 185 * Set up boot memory allocator 186 * 187 * The Gorman book also talks about these functions. 188 * This needs to change for highmem setups. 189 */ 190 191 /* Prior to this, bootmem_lastpg is actually mem size */ 192 bootmem_lastpg += ARCH_PFN_OFFSET; 193 194 /* Memory size needs to be a multiple of 16M */ 195 bootmem_lastpg = PFN_DOWN((bootmem_lastpg << PAGE_SHIFT) & 196 ~((BIG_KERNEL_PAGE_SIZE) - 1)); 197 198 memblock_add(PHYS_OFFSET, 199 (bootmem_lastpg - ARCH_PFN_OFFSET) << PAGE_SHIFT); 200 201 /* Reserve kernel text/data/bss */ 202 memblock_reserve(PHYS_OFFSET, 203 (bootmem_startpg - ARCH_PFN_OFFSET) << PAGE_SHIFT); 204 /* 205 * Reserve the top DMA_RESERVE bytes of RAM for DMA (uncached) 206 * memory allocation 207 */ 208 max_low_pfn = bootmem_lastpg - PFN_DOWN(DMA_RESERVED_BYTES); 209 min_low_pfn = ARCH_PFN_OFFSET; 210 memblock_reserve(PFN_PHYS(max_low_pfn), DMA_RESERVED_BYTES); 211 212 printk(KERN_INFO "bootmem_startpg: 0x%08lx\n", bootmem_startpg); 213 printk(KERN_INFO "bootmem_lastpg: 0x%08lx\n", bootmem_lastpg); 214 printk(KERN_INFO "min_low_pfn: 0x%08lx\n", min_low_pfn); 215 printk(KERN_INFO "max_low_pfn: 0x%08lx\n", max_low_pfn); 216 217 /* 218 * The default VM page tables (will be) populated with 219 * VA=PA+PAGE_OFFSET mapping. We go in and invalidate entries 220 * higher than what we have memory for. 221 */ 222 223 /* this is pointer arithmetic; each entry covers 4MB */ 224 segtable = segtable + (PAGE_OFFSET >> 22); 225 226 /* this actually only goes to the end of the first gig */ 227 segtable_end = segtable + (1<<(30-22)); 228 229 /* 230 * Move forward to the start of empty pages; take into account 231 * phys_offset shift. 232 */ 233 234 segtable += (bootmem_lastpg-ARCH_PFN_OFFSET)>>(22-PAGE_SHIFT); 235 { 236 int i; 237 238 for (i = 1 ; i <= DMA_RESERVE ; i++) 239 segtable[-i] = ((segtable[-i] & __HVM_PTE_PGMASK_4MB) 240 | __HVM_PTE_R | __HVM_PTE_W | __HVM_PTE_X 241 | __HEXAGON_C_UNC << 6 242 | __HVM_PDE_S_4MB); 243 } 244 245 printk(KERN_INFO "clearing segtable from %p to %p\n", segtable, 246 segtable_end); 247 while (segtable < (segtable_end-8)) 248 *(segtable++) = __HVM_PDE_S_INVALID; 249 /* stop the pointer at the device I/O 4MB page */ 250 251 printk(KERN_INFO "segtable = %p (should be equal to _K_io_map)\n", 252 segtable); 253 254 #if 0 255 /* Other half of the early device table from vm_init_segtable. */ 256 printk(KERN_INFO "&_K_init_devicetable = 0x%08x\n", 257 (unsigned long) _K_init_devicetable-PAGE_OFFSET); 258 *segtable = ((u32) (unsigned long) _K_init_devicetable-PAGE_OFFSET) | 259 __HVM_PDE_S_4KB; 260 printk(KERN_INFO "*segtable = 0x%08x\n", *segtable); 261 #endif 262 263 /* 264 * The bootmem allocator seemingly just lives to feed memory 265 * to the paging system 266 */ 267 printk(KERN_INFO "PAGE_SIZE=%lu\n", PAGE_SIZE); 268 paging_init(); /* See Gorman Book, 2.3 */ 269 270 /* 271 * At this point, the page allocator is kind of initialized, but 272 * apparently no pages are available (just like with the bootmem 273 * allocator), and need to be freed themselves via mem_init(), 274 * which is called by start_kernel() later on in the process 275 */ 276 } 277