2 * linux/arch/unicore32/mm/mmu.c
4 * Code specific to PKUnity SoC and UniCore ISA
6 * Copyright (C) 2001-2010 GUAN Xue-tao
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/mman.h>
17 #include <linux/nodemask.h>
18 #include <linux/memblock.h>
22 #include <asm/cputype.h>
23 #include <asm/sections.h>
24 #include <asm/setup.h>
25 #include <asm/sizes.h>
27 #include <asm/memblock.h>
34 * empty_zero_page is a special page that is used for
35 * zero-initialized data and COW.
37 struct page *empty_zero_page;
38 EXPORT_SYMBOL(empty_zero_page);
41 * The pmd table for the upper-most set of pages.
46 EXPORT_SYMBOL(pgprot_user);
48 pgprot_t pgprot_kernel;
49 EXPORT_SYMBOL(pgprot_kernel);
51 static int __init noalign_setup(char *__unused)
53 cr_alignment &= ~CR_A;
54 cr_no_alignment &= ~CR_A;
58 __setup("noalign", noalign_setup);
60 void adjust_cr(unsigned long mask, unsigned long set)
68 local_irq_save(flags);
70 cr_no_alignment = (cr_no_alignment & ~mask) | set;
71 cr_alignment = (cr_alignment & ~mask) | set;
73 set_cr((get_cr() & ~mask) | set);
75 local_irq_restore(flags);
79 unsigned long virtual;
85 #define PROT_PTE_DEVICE (PTE_PRESENT | PTE_YOUNG | \
86 PTE_DIRTY | PTE_READ | PTE_WRITE)
87 #define PROT_SECT_DEVICE (PMD_TYPE_SECT | PMD_PRESENT | \
88 PMD_SECT_READ | PMD_SECT_WRITE)
90 static struct mem_type mem_types[] = {
91 [MT_DEVICE] = { /* Strongly ordered */
92 .prot_pte = PROT_PTE_DEVICE,
93 .prot_l1 = PMD_TYPE_TABLE | PMD_PRESENT,
94 .prot_sect = PROT_SECT_DEVICE,
97 * MT_KUSER: pte for vecpage -- cacheable,
98 * and sect for unigfx mmap -- noncacheable
101 .prot_pte = PTE_PRESENT | PTE_YOUNG | PTE_DIRTY |
102 PTE_CACHEABLE | PTE_READ | PTE_EXEC,
103 .prot_l1 = PMD_TYPE_TABLE | PMD_PRESENT,
104 .prot_sect = PROT_SECT_DEVICE,
106 [MT_HIGH_VECTORS] = {
107 .prot_pte = PTE_PRESENT | PTE_YOUNG | PTE_DIRTY |
108 PTE_CACHEABLE | PTE_READ | PTE_WRITE |
110 .prot_l1 = PMD_TYPE_TABLE | PMD_PRESENT,
113 .prot_pte = PTE_PRESENT | PTE_YOUNG | PTE_DIRTY |
114 PTE_WRITE | PTE_EXEC,
115 .prot_l1 = PMD_TYPE_TABLE | PMD_PRESENT,
116 .prot_sect = PMD_TYPE_SECT | PMD_PRESENT | PMD_SECT_CACHEABLE |
117 PMD_SECT_READ | PMD_SECT_WRITE | PMD_SECT_EXEC,
120 .prot_sect = PMD_TYPE_SECT | PMD_PRESENT | PMD_SECT_CACHEABLE |
125 const struct mem_type *get_mem_type(unsigned int type)
127 return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
129 EXPORT_SYMBOL(get_mem_type);
132 * Adjust the PMD section entries according to the CPU in use.
134 static void __init build_mem_type_table(void)
136 pgprot_user = __pgprot(PTE_PRESENT | PTE_YOUNG | PTE_CACHEABLE);
137 pgprot_kernel = __pgprot(PTE_PRESENT | PTE_YOUNG |
138 PTE_DIRTY | PTE_READ | PTE_WRITE |
139 PTE_EXEC | PTE_CACHEABLE);
142 #define vectors_base() (vectors_high() ? 0xffff0000 : 0)
144 static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr,
147 if (pmd_none(*pmd)) {
148 pte_t *pte = memblock_alloc(PTRS_PER_PTE * sizeof(pte_t),
149 PTRS_PER_PTE * sizeof(pte_t));
150 __pmd_populate(pmd, __pa(pte) | prot);
152 BUG_ON(pmd_bad(*pmd));
153 return pte_offset_kernel(pmd, addr);
156 static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
157 unsigned long end, unsigned long pfn,
158 const struct mem_type *type)
160 pte_t *pte = early_pte_alloc(pmd, addr, type->prot_l1);
162 set_pte(pte, pfn_pte(pfn, __pgprot(type->prot_pte)));
164 } while (pte++, addr += PAGE_SIZE, addr != end);
167 static void __init alloc_init_section(pgd_t *pgd, unsigned long addr,
168 unsigned long end, unsigned long phys,
169 const struct mem_type *type)
171 pmd_t *pmd = pmd_offset((pud_t *)pgd, addr);
174 * Try a section mapping - end, addr and phys must all be aligned
175 * to a section boundary.
177 if (((addr | end | phys) & ~SECTION_MASK) == 0) {
181 set_pmd(pmd, __pmd(phys | type->prot_sect));
182 phys += SECTION_SIZE;
183 } while (pmd++, addr += SECTION_SIZE, addr != end);
188 * No need to loop; pte's aren't interested in the
189 * individual L1 entries.
191 alloc_init_pte(pmd, addr, end, __phys_to_pfn(phys), type);
196 * Create the page directory entries and any necessary
197 * page tables for the mapping specified by `md'. We
198 * are able to cope here with varying sizes and address
199 * offsets, and we take full advantage of sections.
201 static void __init create_mapping(struct map_desc *md)
203 unsigned long phys, addr, length, end;
204 const struct mem_type *type;
207 if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
208 printk(KERN_WARNING "BUG: not creating mapping for "
209 "0x%08llx at 0x%08lx in user region\n",
210 __pfn_to_phys((u64)md->pfn), md->virtual);
214 if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
215 md->virtual >= PAGE_OFFSET && md->virtual < VMALLOC_END) {
216 printk(KERN_WARNING "BUG: mapping for 0x%08llx at 0x%08lx "
217 "overlaps vmalloc space\n",
218 __pfn_to_phys((u64)md->pfn), md->virtual);
221 type = &mem_types[md->type];
223 addr = md->virtual & PAGE_MASK;
224 phys = (unsigned long)__pfn_to_phys(md->pfn);
225 length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
227 if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
228 printk(KERN_WARNING "BUG: map for 0x%08lx at 0x%08lx can not "
229 "be mapped using pages, ignoring.\n",
230 __pfn_to_phys(md->pfn), addr);
234 pgd = pgd_offset_k(addr);
237 unsigned long next = pgd_addr_end(addr, end);
239 alloc_init_section(pgd, addr, next, phys, type);
243 } while (pgd++, addr != end);
246 static void * __initdata vmalloc_min = (void *)(VMALLOC_END - SZ_128M);
249 * vmalloc=size forces the vmalloc area to be exactly 'size'
250 * bytes. This can be used to increase (or decrease) the vmalloc
251 * area - the default is 128m.
253 static int __init early_vmalloc(char *arg)
255 unsigned long vmalloc_reserve = memparse(arg, NULL);
257 if (vmalloc_reserve < SZ_16M) {
258 vmalloc_reserve = SZ_16M;
260 "vmalloc area too small, limiting to %luMB\n",
261 vmalloc_reserve >> 20);
264 if (vmalloc_reserve > VMALLOC_END - (PAGE_OFFSET + SZ_32M)) {
265 vmalloc_reserve = VMALLOC_END - (PAGE_OFFSET + SZ_32M);
267 "vmalloc area is too big, limiting to %luMB\n",
268 vmalloc_reserve >> 20);
271 vmalloc_min = (void *)(VMALLOC_END - vmalloc_reserve);
274 early_param("vmalloc", early_vmalloc);
276 static phys_addr_t lowmem_limit __initdata = SZ_1G;
278 static void __init sanity_check_meminfo(void)
282 lowmem_limit = __pa(vmalloc_min - 1) + 1;
283 memblock_set_current_limit(lowmem_limit);
285 for (i = 0, j = 0; i < meminfo.nr_banks; i++) {
286 struct membank *bank = &meminfo.bank[j];
287 *bank = meminfo.bank[i];
290 meminfo.nr_banks = j;
293 static inline void prepare_page_table(void)
299 * Clear out all the mappings below the kernel image.
301 for (addr = 0; addr < MODULES_VADDR; addr += PGDIR_SIZE)
302 pmd_clear(pmd_off_k(addr));
304 for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
305 pmd_clear(pmd_off_k(addr));
308 * Find the end of the first block of lowmem.
310 end = memblock.memory.regions[0].base + memblock.memory.regions[0].size;
311 if (end >= lowmem_limit)
315 * Clear out all the kernel space mappings, except for the first
316 * memory bank, up to the end of the vmalloc region.
318 for (addr = __phys_to_virt(end);
319 addr < VMALLOC_END; addr += PGDIR_SIZE)
320 pmd_clear(pmd_off_k(addr));
324 * Reserve the special regions of memory
326 void __init uc32_mm_memblock_reserve(void)
329 * Reserve the page tables. These are already in use,
330 * and can only be in node 0.
332 memblock_reserve(__pa(swapper_pg_dir), PTRS_PER_PGD * sizeof(pgd_t));
336 * Set up device the mappings. Since we clear out the page tables for all
337 * mappings above VMALLOC_END, we will remove any debug device mappings.
338 * This means you have to be careful how you debug this function, or any
339 * called function. This means you can't use any function or debugging
340 * method which may touch any device, otherwise the kernel _will_ crash.
342 static void __init devicemaps_init(void)
349 * Allocate the vector page early.
351 vectors = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
353 for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
354 pmd_clear(pmd_off_k(addr));
357 * Create a mapping for the machine vectors at the high-vectors
358 * location (0xffff0000). If we aren't using high-vectors, also
359 * create a mapping at the low-vectors virtual address.
361 map.pfn = __phys_to_pfn(virt_to_phys(vectors));
362 map.virtual = VECTORS_BASE;
363 map.length = PAGE_SIZE;
364 map.type = MT_HIGH_VECTORS;
365 create_mapping(&map);
368 * Create a mapping for the kuser page at the special
369 * location (0xbfff0000) to the same vectors location.
371 map.pfn = __phys_to_pfn(virt_to_phys(vectors));
372 map.virtual = KUSER_VECPAGE_BASE;
373 map.length = PAGE_SIZE;
375 create_mapping(&map);
378 * Finally flush the caches and tlb to ensure that we're in a
379 * consistent state wrt the writebuffer. This also ensures that
380 * any write-allocated cache lines in the vector page are written
381 * back. After this point, we can start to touch devices again.
383 local_flush_tlb_all();
387 static void __init map_lowmem(void)
389 struct memblock_region *reg;
391 /* Map all the lowmem memory banks. */
392 for_each_memblock(memory, reg) {
393 phys_addr_t start = reg->base;
394 phys_addr_t end = start + reg->size;
397 if (end > lowmem_limit)
402 map.pfn = __phys_to_pfn(start);
403 map.virtual = __phys_to_virt(start);
404 map.length = end - start;
405 map.type = MT_MEMORY;
407 create_mapping(&map);
412 * paging_init() sets up the page tables, initialises the zone memory
413 * maps, and sets up the zero page, bad page and bad page tables.
415 void __init paging_init(void)
419 build_mem_type_table();
420 sanity_check_meminfo();
421 prepare_page_table();
425 top_pmd = pmd_off_k(0xffff0000);
427 /* allocate the zero page. */
428 zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
432 empty_zero_page = virt_to_page(zero_page);
433 __flush_dcache_page(NULL, empty_zero_page);
437 * In order to soft-boot, we need to insert a 1:1 mapping in place of
438 * the user-mode pages. This will then ensure that we have predictable
439 * results when turning the mmu off
441 void setup_mm_for_reboot(void)
443 unsigned long base_pmdval;
448 * We need to access to user-mode page tables here. For kernel threads
449 * we don't have any user-mode mappings so we use the context that we
452 pgd = current->active_mm->pgd;
454 base_pmdval = PMD_SECT_WRITE | PMD_SECT_READ | PMD_TYPE_SECT;
456 for (i = 0; i < FIRST_USER_PGD_NR + USER_PTRS_PER_PGD; i++, pgd++) {
457 unsigned long pmdval = (i << PGDIR_SHIFT) | base_pmdval;
460 pmd = pmd_off(pgd, i << PGDIR_SHIFT);
461 set_pmd(pmd, __pmd(pmdval));
462 flush_pmd_entry(pmd);
465 local_flush_tlb_all();
469 * Take care of architecture specific things when placing a new PTE into
470 * a page table, or changing an existing PTE. Basically, there are two
471 * things that we need to take care of:
473 * 1. If PG_dcache_clean is not set for the page, we need to ensure
474 * that any cache entries for the kernels virtual memory
475 * range are written back to the page.
476 * 2. If we have multiple shared mappings of the same space in
477 * an object, we need to deal with the cache aliasing issues.
479 * Note that the pte lock will be held.
481 void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr,
484 unsigned long pfn = pte_pfn(*ptep);
485 struct address_space *mapping;
492 * The zero page is never written to, so never has any dirty
493 * cache lines, and therefore never needs to be flushed.
495 page = pfn_to_page(pfn);
496 if (page == ZERO_PAGE(0))
499 mapping = page_mapping_file(page);
500 if (!test_and_set_bit(PG_dcache_clean, &page->flags))
501 __flush_dcache_page(mapping, page);
503 if (vma->vm_flags & VM_EXEC)
504 __flush_icache_all();