arch: simplify several early memory allocations
[linux-2.6-block.git] / arch / unicore32 / mm / mmu.c
1 /*
2  * linux/arch/unicore32/mm/mmu.c
3  *
4  * Code specific to PKUnity SoC and UniCore ISA
5  *
6  * Copyright (C) 2001-2010 GUAN Xue-tao
7  *
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.
11  */
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>
19 #include <linux/fs.h>
20 #include <linux/io.h>
21
22 #include <asm/cputype.h>
23 #include <asm/sections.h>
24 #include <asm/setup.h>
25 #include <asm/sizes.h>
26 #include <asm/tlb.h>
27 #include <asm/memblock.h>
28
29 #include <mach/map.h>
30
31 #include "mm.h"
32
33 /*
34  * empty_zero_page is a special page that is used for
35  * zero-initialized data and COW.
36  */
37 struct page *empty_zero_page;
38 EXPORT_SYMBOL(empty_zero_page);
39
40 /*
41  * The pmd table for the upper-most set of pages.
42  */
43 pmd_t *top_pmd;
44
45 pgprot_t pgprot_user;
46 EXPORT_SYMBOL(pgprot_user);
47
48 pgprot_t pgprot_kernel;
49 EXPORT_SYMBOL(pgprot_kernel);
50
51 static int __init noalign_setup(char *__unused)
52 {
53         cr_alignment &= ~CR_A;
54         cr_no_alignment &= ~CR_A;
55         set_cr(cr_alignment);
56         return 1;
57 }
58 __setup("noalign", noalign_setup);
59
60 void adjust_cr(unsigned long mask, unsigned long set)
61 {
62         unsigned long flags;
63
64         mask &= ~CR_A;
65
66         set &= mask;
67
68         local_irq_save(flags);
69
70         cr_no_alignment = (cr_no_alignment & ~mask) | set;
71         cr_alignment = (cr_alignment & ~mask) | set;
72
73         set_cr((get_cr() & ~mask) | set);
74
75         local_irq_restore(flags);
76 }
77
78 struct map_desc {
79         unsigned long virtual;
80         unsigned long pfn;
81         unsigned long length;
82         unsigned int type;
83 };
84
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)
89
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,
95         },
96         /*
97          * MT_KUSER: pte for vecpage -- cacheable,
98          *       and sect for unigfx mmap -- noncacheable
99          */
100         [MT_KUSER] = {
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,
105         },
106         [MT_HIGH_VECTORS] = {
107                 .prot_pte  = PTE_PRESENT | PTE_YOUNG | PTE_DIRTY |
108                                 PTE_CACHEABLE | PTE_READ | PTE_WRITE |
109                                 PTE_EXEC,
110                 .prot_l1   = PMD_TYPE_TABLE | PMD_PRESENT,
111         },
112         [MT_MEMORY] = {
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,
118         },
119         [MT_ROM] = {
120                 .prot_sect = PMD_TYPE_SECT | PMD_PRESENT | PMD_SECT_CACHEABLE |
121                                 PMD_SECT_READ,
122         },
123 };
124
125 const struct mem_type *get_mem_type(unsigned int type)
126 {
127         return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
128 }
129 EXPORT_SYMBOL(get_mem_type);
130
131 /*
132  * Adjust the PMD section entries according to the CPU in use.
133  */
134 static void __init build_mem_type_table(void)
135 {
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);
140 }
141
142 #define vectors_base()  (vectors_high() ? 0xffff0000 : 0)
143
144 static void __init *early_alloc(unsigned long sz)
145 {
146         return memblock_alloc(sz, sz);
147 }
148
149 static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr,
150                 unsigned long prot)
151 {
152         if (pmd_none(*pmd)) {
153                 pte_t *pte = early_alloc(PTRS_PER_PTE * sizeof(pte_t));
154                 __pmd_populate(pmd, __pa(pte) | prot);
155         }
156         BUG_ON(pmd_bad(*pmd));
157         return pte_offset_kernel(pmd, addr);
158 }
159
160 static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
161                                   unsigned long end, unsigned long pfn,
162                                   const struct mem_type *type)
163 {
164         pte_t *pte = early_pte_alloc(pmd, addr, type->prot_l1);
165         do {
166                 set_pte(pte, pfn_pte(pfn, __pgprot(type->prot_pte)));
167                 pfn++;
168         } while (pte++, addr += PAGE_SIZE, addr != end);
169 }
170
171 static void __init alloc_init_section(pgd_t *pgd, unsigned long addr,
172                                       unsigned long end, unsigned long phys,
173                                       const struct mem_type *type)
174 {
175         pmd_t *pmd = pmd_offset((pud_t *)pgd, addr);
176
177         /*
178          * Try a section mapping - end, addr and phys must all be aligned
179          * to a section boundary.
180          */
181         if (((addr | end | phys) & ~SECTION_MASK) == 0) {
182                 pmd_t *p = pmd;
183
184                 do {
185                         set_pmd(pmd, __pmd(phys | type->prot_sect));
186                         phys += SECTION_SIZE;
187                 } while (pmd++, addr += SECTION_SIZE, addr != end);
188
189                 flush_pmd_entry(p);
190         } else {
191                 /*
192                  * No need to loop; pte's aren't interested in the
193                  * individual L1 entries.
194                  */
195                 alloc_init_pte(pmd, addr, end, __phys_to_pfn(phys), type);
196         }
197 }
198
199 /*
200  * Create the page directory entries and any necessary
201  * page tables for the mapping specified by `md'.  We
202  * are able to cope here with varying sizes and address
203  * offsets, and we take full advantage of sections.
204  */
205 static void __init create_mapping(struct map_desc *md)
206 {
207         unsigned long phys, addr, length, end;
208         const struct mem_type *type;
209         pgd_t *pgd;
210
211         if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
212                 printk(KERN_WARNING "BUG: not creating mapping for "
213                        "0x%08llx at 0x%08lx in user region\n",
214                        __pfn_to_phys((u64)md->pfn), md->virtual);
215                 return;
216         }
217
218         if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
219             md->virtual >= PAGE_OFFSET && md->virtual < VMALLOC_END) {
220                 printk(KERN_WARNING "BUG: mapping for 0x%08llx at 0x%08lx "
221                        "overlaps vmalloc space\n",
222                        __pfn_to_phys((u64)md->pfn), md->virtual);
223         }
224
225         type = &mem_types[md->type];
226
227         addr = md->virtual & PAGE_MASK;
228         phys = (unsigned long)__pfn_to_phys(md->pfn);
229         length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
230
231         if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
232                 printk(KERN_WARNING "BUG: map for 0x%08lx at 0x%08lx can not "
233                        "be mapped using pages, ignoring.\n",
234                        __pfn_to_phys(md->pfn), addr);
235                 return;
236         }
237
238         pgd = pgd_offset_k(addr);
239         end = addr + length;
240         do {
241                 unsigned long next = pgd_addr_end(addr, end);
242
243                 alloc_init_section(pgd, addr, next, phys, type);
244
245                 phys += next - addr;
246                 addr = next;
247         } while (pgd++, addr != end);
248 }
249
250 static void * __initdata vmalloc_min = (void *)(VMALLOC_END - SZ_128M);
251
252 /*
253  * vmalloc=size forces the vmalloc area to be exactly 'size'
254  * bytes. This can be used to increase (or decrease) the vmalloc
255  * area - the default is 128m.
256  */
257 static int __init early_vmalloc(char *arg)
258 {
259         unsigned long vmalloc_reserve = memparse(arg, NULL);
260
261         if (vmalloc_reserve < SZ_16M) {
262                 vmalloc_reserve = SZ_16M;
263                 printk(KERN_WARNING
264                         "vmalloc area too small, limiting to %luMB\n",
265                         vmalloc_reserve >> 20);
266         }
267
268         if (vmalloc_reserve > VMALLOC_END - (PAGE_OFFSET + SZ_32M)) {
269                 vmalloc_reserve = VMALLOC_END - (PAGE_OFFSET + SZ_32M);
270                 printk(KERN_WARNING
271                         "vmalloc area is too big, limiting to %luMB\n",
272                         vmalloc_reserve >> 20);
273         }
274
275         vmalloc_min = (void *)(VMALLOC_END - vmalloc_reserve);
276         return 0;
277 }
278 early_param("vmalloc", early_vmalloc);
279
280 static phys_addr_t lowmem_limit __initdata = SZ_1G;
281
282 static void __init sanity_check_meminfo(void)
283 {
284         int i, j;
285
286         lowmem_limit = __pa(vmalloc_min - 1) + 1;
287         memblock_set_current_limit(lowmem_limit);
288
289         for (i = 0, j = 0; i < meminfo.nr_banks; i++) {
290                 struct membank *bank = &meminfo.bank[j];
291                 *bank = meminfo.bank[i];
292                 j++;
293         }
294         meminfo.nr_banks = j;
295 }
296
297 static inline void prepare_page_table(void)
298 {
299         unsigned long addr;
300         phys_addr_t end;
301
302         /*
303          * Clear out all the mappings below the kernel image.
304          */
305         for (addr = 0; addr < MODULES_VADDR; addr += PGDIR_SIZE)
306                 pmd_clear(pmd_off_k(addr));
307
308         for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
309                 pmd_clear(pmd_off_k(addr));
310
311         /*
312          * Find the end of the first block of lowmem.
313          */
314         end = memblock.memory.regions[0].base + memblock.memory.regions[0].size;
315         if (end >= lowmem_limit)
316                 end = lowmem_limit;
317
318         /*
319          * Clear out all the kernel space mappings, except for the first
320          * memory bank, up to the end of the vmalloc region.
321          */
322         for (addr = __phys_to_virt(end);
323              addr < VMALLOC_END; addr += PGDIR_SIZE)
324                 pmd_clear(pmd_off_k(addr));
325 }
326
327 /*
328  * Reserve the special regions of memory
329  */
330 void __init uc32_mm_memblock_reserve(void)
331 {
332         /*
333          * Reserve the page tables.  These are already in use,
334          * and can only be in node 0.
335          */
336         memblock_reserve(__pa(swapper_pg_dir), PTRS_PER_PGD * sizeof(pgd_t));
337 }
338
339 /*
340  * Set up device the mappings.  Since we clear out the page tables for all
341  * mappings above VMALLOC_END, we will remove any debug device mappings.
342  * This means you have to be careful how you debug this function, or any
343  * called function.  This means you can't use any function or debugging
344  * method which may touch any device, otherwise the kernel _will_ crash.
345  */
346 static void __init devicemaps_init(void)
347 {
348         struct map_desc map;
349         unsigned long addr;
350         void *vectors;
351
352         /*
353          * Allocate the vector page early.
354          */
355         vectors = early_alloc(PAGE_SIZE);
356
357         for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
358                 pmd_clear(pmd_off_k(addr));
359
360         /*
361          * Create a mapping for the machine vectors at the high-vectors
362          * location (0xffff0000).  If we aren't using high-vectors, also
363          * create a mapping at the low-vectors virtual address.
364          */
365         map.pfn = __phys_to_pfn(virt_to_phys(vectors));
366         map.virtual = VECTORS_BASE;
367         map.length = PAGE_SIZE;
368         map.type = MT_HIGH_VECTORS;
369         create_mapping(&map);
370
371         /*
372          * Create a mapping for the kuser page at the special
373          * location (0xbfff0000) to the same vectors location.
374          */
375         map.pfn = __phys_to_pfn(virt_to_phys(vectors));
376         map.virtual = KUSER_VECPAGE_BASE;
377         map.length = PAGE_SIZE;
378         map.type = MT_KUSER;
379         create_mapping(&map);
380
381         /*
382          * Finally flush the caches and tlb to ensure that we're in a
383          * consistent state wrt the writebuffer.  This also ensures that
384          * any write-allocated cache lines in the vector page are written
385          * back.  After this point, we can start to touch devices again.
386          */
387         local_flush_tlb_all();
388         flush_cache_all();
389 }
390
391 static void __init map_lowmem(void)
392 {
393         struct memblock_region *reg;
394
395         /* Map all the lowmem memory banks. */
396         for_each_memblock(memory, reg) {
397                 phys_addr_t start = reg->base;
398                 phys_addr_t end = start + reg->size;
399                 struct map_desc map;
400
401                 if (end > lowmem_limit)
402                         end = lowmem_limit;
403                 if (start >= end)
404                         break;
405
406                 map.pfn = __phys_to_pfn(start);
407                 map.virtual = __phys_to_virt(start);
408                 map.length = end - start;
409                 map.type = MT_MEMORY;
410
411                 create_mapping(&map);
412         }
413 }
414
415 /*
416  * paging_init() sets up the page tables, initialises the zone memory
417  * maps, and sets up the zero page, bad page and bad page tables.
418  */
419 void __init paging_init(void)
420 {
421         void *zero_page;
422
423         build_mem_type_table();
424         sanity_check_meminfo();
425         prepare_page_table();
426         map_lowmem();
427         devicemaps_init();
428
429         top_pmd = pmd_off_k(0xffff0000);
430
431         /* allocate the zero page. */
432         zero_page = early_alloc(PAGE_SIZE);
433
434         bootmem_init();
435
436         empty_zero_page = virt_to_page(zero_page);
437         __flush_dcache_page(NULL, empty_zero_page);
438 }
439
440 /*
441  * In order to soft-boot, we need to insert a 1:1 mapping in place of
442  * the user-mode pages.  This will then ensure that we have predictable
443  * results when turning the mmu off
444  */
445 void setup_mm_for_reboot(void)
446 {
447         unsigned long base_pmdval;
448         pgd_t *pgd;
449         int i;
450
451         /*
452          * We need to access to user-mode page tables here. For kernel threads
453          * we don't have any user-mode mappings so we use the context that we
454          * "borrowed".
455          */
456         pgd = current->active_mm->pgd;
457
458         base_pmdval = PMD_SECT_WRITE | PMD_SECT_READ | PMD_TYPE_SECT;
459
460         for (i = 0; i < FIRST_USER_PGD_NR + USER_PTRS_PER_PGD; i++, pgd++) {
461                 unsigned long pmdval = (i << PGDIR_SHIFT) | base_pmdval;
462                 pmd_t *pmd;
463
464                 pmd = pmd_off(pgd, i << PGDIR_SHIFT);
465                 set_pmd(pmd, __pmd(pmdval));
466                 flush_pmd_entry(pmd);
467         }
468
469         local_flush_tlb_all();
470 }
471
472 /*
473  * Take care of architecture specific things when placing a new PTE into
474  * a page table, or changing an existing PTE.  Basically, there are two
475  * things that we need to take care of:
476  *
477  *  1. If PG_dcache_clean is not set for the page, we need to ensure
478  *     that any cache entries for the kernels virtual memory
479  *     range are written back to the page.
480  *  2. If we have multiple shared mappings of the same space in
481  *     an object, we need to deal with the cache aliasing issues.
482  *
483  * Note that the pte lock will be held.
484  */
485 void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr,
486         pte_t *ptep)
487 {
488         unsigned long pfn = pte_pfn(*ptep);
489         struct address_space *mapping;
490         struct page *page;
491
492         if (!pfn_valid(pfn))
493                 return;
494
495         /*
496          * The zero page is never written to, so never has any dirty
497          * cache lines, and therefore never needs to be flushed.
498          */
499         page = pfn_to_page(pfn);
500         if (page == ZERO_PAGE(0))
501                 return;
502
503         mapping = page_mapping_file(page);
504         if (!test_and_set_bit(PG_dcache_clean, &page->flags))
505                 __flush_dcache_page(mapping, page);
506         if (mapping)
507                 if (vma->vm_flags & VM_EXEC)
508                         __flush_icache_all();
509 }