ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / arm / mm / mm-armv.c
1 /*
2  *  linux/arch/arm/mm/mm-armv.c
3  *
4  *  Copyright (C) 1998-2002 Russell King
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 as
8  * published by the Free Software Foundation.
9  *
10  *  Page table sludge for ARM v3 and v4 processor architectures.
11  */
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/mm.h>
15 #include <linux/init.h>
16 #include <linux/bootmem.h>
17 #include <linux/highmem.h>
18
19 #include <asm/pgalloc.h>
20 #include <asm/page.h>
21 #include <asm/rmap.h>
22 #include <asm/io.h>
23 #include <asm/setup.h>
24 #include <asm/tlbflush.h>
25
26 #include <asm/mach/map.h>
27
28 #define CPOLICY_UNCACHED        0
29 #define CPOLICY_BUFFERED        1
30 #define CPOLICY_WRITETHROUGH    2
31 #define CPOLICY_WRITEBACK       3
32 #define CPOLICY_WRITEALLOC      4
33
34 static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
35 static unsigned int ecc_mask __initdata = 0;
36 pgprot_t pgprot_kernel;
37
38 EXPORT_SYMBOL(pgprot_kernel);
39
40 struct cachepolicy {
41         const char      policy[16];
42         unsigned int    cr_mask;
43         unsigned int    pmd;
44         unsigned int    pte;
45 };
46
47 static struct cachepolicy cache_policies[] __initdata = {
48         {
49                 .policy         = "uncached",
50                 .cr_mask        = CR_W|CR_C,
51                 .pmd            = PMD_SECT_UNCACHED,
52                 .pte            = 0,
53         }, {
54                 .policy         = "buffered",
55                 .cr_mask        = CR_C,
56                 .pmd            = PMD_SECT_BUFFERED,
57                 .pte            = PTE_BUFFERABLE,
58         }, {
59                 .policy         = "writethrough",
60                 .cr_mask        = 0,
61                 .pmd            = PMD_SECT_WT,
62                 .pte            = PTE_CACHEABLE,
63         }, {
64                 .policy         = "writeback",
65                 .cr_mask        = 0,
66                 .pmd            = PMD_SECT_WB,
67                 .pte            = PTE_BUFFERABLE|PTE_CACHEABLE,
68         }, {
69                 .policy         = "writealloc",
70                 .cr_mask        = 0,
71                 .pmd            = PMD_SECT_WBWA,
72                 .pte            = PTE_BUFFERABLE|PTE_CACHEABLE,
73         }
74 };
75
76 /*
77  * These are useful for identifing cache coherency
78  * problems by allowing the cache or the cache and
79  * writebuffer to be turned off.  (Note: the write
80  * buffer should not be on and the cache off).
81  */
82 static void __init early_cachepolicy(char **p)
83 {
84         int i;
85
86         for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
87                 int len = strlen(cache_policies[i].policy);
88
89                 if (memcmp(*p, cache_policies[i].policy, len) == 0) {
90                         cachepolicy = i;
91                         cr_alignment &= ~cache_policies[i].cr_mask;
92                         cr_no_alignment &= ~cache_policies[i].cr_mask;
93                         *p += len;
94                         break;
95                 }
96         }
97         if (i == ARRAY_SIZE(cache_policies))
98                 printk(KERN_ERR "ERROR: unknown or unsupported cache policy\n");
99         flush_cache_all();
100         set_cr(cr_alignment);
101 }
102
103 static void __init early_nocache(char **__unused)
104 {
105         char *p = "buffered";
106         printk(KERN_WARNING "nocache is deprecated; use cachepolicy=%s\n", p);
107         early_cachepolicy(&p);
108 }
109
110 static void __init early_nowrite(char **__unused)
111 {
112         char *p = "uncached";
113         printk(KERN_WARNING "nowb is deprecated; use cachepolicy=%s\n", p);
114         early_cachepolicy(&p);
115 }
116
117 static void __init early_ecc(char **p)
118 {
119         if (memcmp(*p, "on", 2) == 0) {
120                 ecc_mask = PMD_PROTECTION;
121                 *p += 2;
122         } else if (memcmp(*p, "off", 3) == 0) {
123                 ecc_mask = 0;
124                 *p += 3;
125         }
126 }
127
128 __early_param("nocache", early_nocache);
129 __early_param("nowb", early_nowrite);
130 __early_param("cachepolicy=", early_cachepolicy);
131 __early_param("ecc=", early_ecc);
132
133 static int __init noalign_setup(char *__unused)
134 {
135         cr_alignment &= ~CR_A;
136         cr_no_alignment &= ~CR_A;
137         set_cr(cr_alignment);
138         return 1;
139 }
140
141 __setup("noalign", noalign_setup);
142
143 #define FIRST_KERNEL_PGD_NR     (FIRST_USER_PGD_NR + USER_PTRS_PER_PGD)
144
145 /*
146  * need to get a 16k page for level 1
147  */
148 pgd_t *get_pgd_slow(struct mm_struct *mm)
149 {
150         pgd_t *new_pgd, *init_pgd;
151         pmd_t *new_pmd, *init_pmd;
152         pte_t *new_pte, *init_pte;
153
154         new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2);
155         if (!new_pgd)
156                 goto no_pgd;
157
158         memzero(new_pgd, FIRST_KERNEL_PGD_NR * sizeof(pgd_t));
159
160         init_pgd = pgd_offset_k(0);
161
162         if (vectors_base() == 0) {
163                 /*
164                  * This lock is here just to satisfy pmd_alloc and pte_lock
165                  */
166                 spin_lock(&mm->page_table_lock);
167
168                 /*
169                  * On ARM, first page must always be allocated since it
170                  * contains the machine vectors.
171                  */
172                 new_pmd = pmd_alloc(mm, new_pgd, 0);
173                 if (!new_pmd)
174                         goto no_pmd;
175
176                 new_pte = pte_alloc_map(mm, new_pmd, 0);
177                 if (!new_pte)
178                         goto no_pte;
179
180                 init_pmd = pmd_offset(init_pgd, 0);
181                 init_pte = pte_offset_map_nested(init_pmd, 0);
182                 set_pte(new_pte, *init_pte);
183                 pte_unmap_nested(init_pte);
184                 pte_unmap(new_pte);
185
186                 spin_unlock(&mm->page_table_lock);
187         }
188
189         /*
190          * Copy over the kernel and IO PGD entries
191          */
192         memcpy(new_pgd + FIRST_KERNEL_PGD_NR, init_pgd + FIRST_KERNEL_PGD_NR,
193                        (PTRS_PER_PGD - FIRST_KERNEL_PGD_NR) * sizeof(pgd_t));
194
195         clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
196
197         return new_pgd;
198
199 no_pte:
200         spin_unlock(&mm->page_table_lock);
201         pmd_free(new_pmd);
202         free_pages((unsigned long)new_pgd, 2);
203         return NULL;
204
205 no_pmd:
206         spin_unlock(&mm->page_table_lock);
207         free_pages((unsigned long)new_pgd, 2);
208         return NULL;
209
210 no_pgd:
211         return NULL;
212 }
213
214 void free_pgd_slow(pgd_t *pgd)
215 {
216         pmd_t *pmd;
217         struct page *pte;
218
219         if (!pgd)
220                 return;
221
222         /* pgd is always present and good */
223         pmd = (pmd_t *)pgd;
224         if (pmd_none(*pmd))
225                 goto free;
226         if (pmd_bad(*pmd)) {
227                 pmd_ERROR(*pmd);
228                 pmd_clear(pmd);
229                 goto free;
230         }
231
232         pte = pmd_page(*pmd);
233         pmd_clear(pmd);
234         pgtable_remove_rmap(pte);
235         pte_free(pte);
236         pmd_free(pmd);
237 free:
238         free_pages((unsigned long) pgd, 2);
239 }
240
241 /*
242  * Create a SECTION PGD between VIRT and PHYS in domain
243  * DOMAIN with protection PROT
244  */
245 static inline void
246 alloc_init_section(unsigned long virt, unsigned long phys, int prot)
247 {
248         pmd_t *pmdp;
249
250         pmdp = pmd_offset(pgd_offset_k(virt), virt);
251         if (virt & (1 << 20))
252                 pmdp++;
253
254         set_pmd(pmdp, __pmd(phys | prot));
255 }
256
257 /*
258  * Add a PAGE mapping between VIRT and PHYS in domain
259  * DOMAIN with protection PROT.  Note that due to the
260  * way we map the PTEs, we must allocate two PTE_SIZE'd
261  * blocks - one for the Linux pte table, and one for
262  * the hardware pte table.
263  */
264 static inline void
265 alloc_init_page(unsigned long virt, unsigned long phys, unsigned int prot_l1, pgprot_t prot)
266 {
267         pmd_t *pmdp;
268         pte_t *ptep;
269
270         pmdp = pmd_offset(pgd_offset_k(virt), virt);
271
272         if (pmd_none(*pmdp)) {
273                 unsigned long pmdval;
274                 ptep = alloc_bootmem_low_pages(2 * PTRS_PER_PTE *
275                                                sizeof(pte_t));
276
277                 pmdval = __pa(ptep) | prot_l1;
278                 pmdp[0] = __pmd(pmdval);
279                 pmdp[1] = __pmd(pmdval + 256 * sizeof(pte_t));
280                 flush_pmd_entry(pmdp);
281         }
282         ptep = pte_offset_kernel(pmdp, virt);
283
284         set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
285 }
286
287 /*
288  * Clear any PGD mapping.  On a two-level page table system,
289  * the clearance is done by the middle-level functions (pmd)
290  * rather than the top-level (pgd) functions.
291  */
292 static inline void clear_mapping(unsigned long virt)
293 {
294         pmd_clear(pmd_offset(pgd_offset_k(virt), virt));
295 }
296
297 struct mem_types {
298         unsigned int    prot_pte;
299         unsigned int    prot_l1;
300         unsigned int    prot_sect;
301         unsigned int    domain;
302 };
303
304 static struct mem_types mem_types[] __initdata = {
305         [MT_DEVICE] = {
306                 .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
307                                 L_PTE_WRITE,
308                 .prot_l1   = PMD_TYPE_TABLE | PMD_BIT4,
309                 .prot_sect = PMD_TYPE_SECT | PMD_BIT4 | PMD_SECT_UNCACHED |
310                                 PMD_SECT_AP_WRITE,
311                 .domain    = DOMAIN_IO,
312         },
313         [MT_CACHECLEAN] = {
314                 .prot_sect = PMD_TYPE_SECT | PMD_BIT4,
315                 .domain    = DOMAIN_KERNEL,
316         },
317         [MT_MINICLEAN] = {
318                 .prot_sect = PMD_TYPE_SECT | PMD_BIT4 | PMD_SECT_MINICACHE,
319                 .domain    = DOMAIN_KERNEL,
320         },
321         [MT_VECTORS] = {
322                 .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
323                                 L_PTE_EXEC,
324                 .prot_l1   = PMD_TYPE_TABLE | PMD_BIT4,
325                 .domain    = DOMAIN_USER,
326         },
327         [MT_MEMORY] = {
328                 .prot_sect = PMD_TYPE_SECT | PMD_BIT4 | PMD_SECT_AP_WRITE,
329                 .domain    = DOMAIN_KERNEL,
330         }
331 };
332
333 /*
334  * Adjust the PMD section entries according to the CPU in use.
335  */
336 static void __init build_mem_type_table(void)
337 {
338         struct cachepolicy *cp;
339         unsigned int cr = get_cr();
340         int cpu_arch = cpu_architecture();
341         int i;
342
343 #if defined(CONFIG_CPU_DCACHE_DISABLE)
344         if (cachepolicy > CPOLICY_BUFFERED)
345                 cachepolicy = CPOLICY_BUFFERED;
346 #elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
347         if (cachepolicy > CPOLICY_WRITETHROUGH)
348                 cachepolicy = CPOLICY_WRITETHROUGH;
349 #endif
350         if (cpu_arch < CPU_ARCH_ARMv5) {
351                 if (cachepolicy >= CPOLICY_WRITEALLOC)
352                         cachepolicy = CPOLICY_WRITEBACK;
353                 ecc_mask = 0;
354         }
355
356         /*
357          * ARMv6 and above have extended page tables.
358          */
359         if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
360                 /*
361                  * bit 4 becomes XN which we must clear for the
362                  * kernel memory mapping.
363                  */
364                 mem_types[MT_MEMORY].prot_sect &= ~PMD_BIT4;
365                 /*
366                  * Mark cache clean areas read only from SVC mode
367                  * and no access from userspace.
368                  */
369                 mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
370                 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
371         }
372
373         cp = &cache_policies[cachepolicy];
374
375         if (cpu_arch >= CPU_ARCH_ARMv5) {
376                 mem_types[MT_VECTORS].prot_pte |= cp->pte & PTE_CACHEABLE;
377         } else {
378                 mem_types[MT_VECTORS].prot_pte |= cp->pte;
379                 mem_types[MT_MINICLEAN].prot_sect &= ~PMD_SECT_TEX(1);
380         }
381
382         mem_types[MT_VECTORS].prot_l1 |= ecc_mask;
383         mem_types[MT_MEMORY].prot_sect |= ecc_mask | cp->pmd;
384
385         for (i = 0; i < 16; i++) {
386                 unsigned long v = pgprot_val(protection_map[i]);
387                 v &= (~(PTE_BUFFERABLE|PTE_CACHEABLE)) | cp->pte;
388                 protection_map[i] = __pgprot(v);
389         }
390
391         pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
392                                  L_PTE_DIRTY | L_PTE_WRITE |
393                                  L_PTE_EXEC | cp->pte);
394
395         switch (cp->pmd) {
396         case PMD_SECT_WT:
397                 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
398                 break;
399         case PMD_SECT_WB:
400         case PMD_SECT_WBWA:
401                 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
402                 break;
403         }
404         printk("Memory policy: ECC %sabled, Data cache %s\n",
405                 ecc_mask ? "en" : "dis", cp->policy);
406 }
407
408 /*
409  * Create the page directory entries and any necessary
410  * page tables for the mapping specified by `md'.  We
411  * are able to cope here with varying sizes and address
412  * offsets, and we take full advantage of sections.
413  */
414 static void __init create_mapping(struct map_desc *md)
415 {
416         unsigned long virt, length;
417         int prot_sect, prot_l1, domain;
418         pgprot_t prot_pte;
419         long off;
420
421         if (md->virtual != vectors_base() && md->virtual < PAGE_OFFSET) {
422                 printk(KERN_WARNING "BUG: not creating mapping for "
423                        "0x%08lx at 0x%08lx in user region\n",
424                        md->physical, md->virtual);
425                 return;
426         }
427
428         if (md->type == MT_DEVICE &&
429             md->virtual >= PAGE_OFFSET && md->virtual < VMALLOC_END) {
430                 printk(KERN_WARNING "BUG: mapping for 0x%08lx at 0x%08lx "
431                        "overlaps vmalloc space\n",
432                        md->physical, md->virtual);
433         }
434
435         domain    = mem_types[md->type].domain;
436         prot_pte  = __pgprot(mem_types[md->type].prot_pte);
437         prot_l1   = mem_types[md->type].prot_l1 | PMD_DOMAIN(domain);
438         prot_sect = mem_types[md->type].prot_sect | PMD_DOMAIN(domain);
439
440         virt   = md->virtual;
441         off    = md->physical - virt;
442         length = md->length;
443
444         if (mem_types[md->type].prot_l1 == 0 &&
445             (virt & 0xfffff || (virt + off) & 0xfffff || (virt + length) & 0xfffff)) {
446                 printk(KERN_WARNING "BUG: map for 0x%08lx at 0x%08lx can not "
447                        "be mapped using pages, ignoring.\n",
448                        md->physical, md->virtual);
449                 return;
450         }
451
452         while ((virt & 0xfffff || (virt + off) & 0xfffff) && length >= PAGE_SIZE) {
453                 alloc_init_page(virt, virt + off, prot_l1, prot_pte);
454
455                 virt   += PAGE_SIZE;
456                 length -= PAGE_SIZE;
457         }
458
459         while (length >= (PGDIR_SIZE / 2)) {
460                 alloc_init_section(virt, virt + off, prot_sect);
461
462                 virt   += (PGDIR_SIZE / 2);
463                 length -= (PGDIR_SIZE / 2);
464         }
465
466         while (length >= PAGE_SIZE) {
467                 alloc_init_page(virt, virt + off, prot_l1, prot_pte);
468
469                 virt   += PAGE_SIZE;
470                 length -= PAGE_SIZE;
471         }
472 }
473
474 /*
475  * In order to soft-boot, we need to insert a 1:1 mapping in place of
476  * the user-mode pages.  This will then ensure that we have predictable
477  * results when turning the mmu off
478  */
479 void setup_mm_for_reboot(char mode)
480 {
481         unsigned long pmdval;
482         pgd_t *pgd;
483         pmd_t *pmd;
484         int i;
485
486         if (current->mm && current->mm->pgd)
487                 pgd = current->mm->pgd;
488         else
489                 pgd = init_mm.pgd;
490
491         for (i = 0; i < FIRST_USER_PGD_NR + USER_PTRS_PER_PGD; i++) {
492                 pmdval = (i << PGDIR_SHIFT) |
493                          PMD_SECT_AP_WRITE | PMD_SECT_AP_READ |
494                          PMD_BIT4 | PMD_TYPE_SECT;
495                 pmd = pmd_offset(pgd + i, i << PGDIR_SHIFT);
496                 set_pmd(pmd, __pmd(pmdval));
497         }
498 }
499
500 /*
501  * Setup initial mappings.  We use the page we allocated for zero page to hold
502  * the mappings, which will get overwritten by the vectors in traps_init().
503  * The mappings must be in virtual address order.
504  */
505 void __init memtable_init(struct meminfo *mi)
506 {
507         struct map_desc *init_maps, *p, *q;
508         unsigned long address = 0;
509         int i;
510
511         build_mem_type_table();
512
513         init_maps = p = alloc_bootmem_low_pages(PAGE_SIZE);
514
515         for (i = 0; i < mi->nr_banks; i++) {
516                 if (mi->bank[i].size == 0)
517                         continue;
518
519                 p->physical   = mi->bank[i].start;
520                 p->virtual    = __phys_to_virt(p->physical);
521                 p->length     = mi->bank[i].size;
522                 p->type       = MT_MEMORY;
523                 p ++;
524         }
525
526 #ifdef FLUSH_BASE
527         p->physical   = FLUSH_BASE_PHYS;
528         p->virtual    = FLUSH_BASE;
529         p->length     = PGDIR_SIZE;
530         p->type       = MT_CACHECLEAN;
531         p ++;
532 #endif
533
534 #ifdef FLUSH_BASE_MINICACHE
535         p->physical   = FLUSH_BASE_PHYS + PGDIR_SIZE;
536         p->virtual    = FLUSH_BASE_MINICACHE;
537         p->length     = PGDIR_SIZE;
538         p->type       = MT_MINICLEAN;
539         p ++;
540 #endif
541
542         /*
543          * Go through the initial mappings, but clear out any
544          * pgdir entries that are not in the description.
545          */
546         q = init_maps;
547         do {
548                 if (address < q->virtual || q == p) {
549                         clear_mapping(address);
550                         address += PGDIR_SIZE;
551                 } else {
552                         create_mapping(q);
553
554                         address = q->virtual + q->length;
555                         address = (address + PGDIR_SIZE - 1) & PGDIR_MASK;
556
557                         q ++;
558                 }
559         } while (address != 0);
560
561         /*
562          * Create a mapping for the machine vectors at virtual address 0
563          * or 0xffff0000.  We should always try the high mapping.
564          */
565         init_maps->physical   = virt_to_phys(init_maps);
566         init_maps->virtual    = vectors_base();
567         init_maps->length     = PAGE_SIZE;
568         init_maps->type       = MT_VECTORS;
569
570         create_mapping(init_maps);
571
572         flush_cache_all();
573         flush_tlb_all();
574 }
575
576 /*
577  * Create the architecture specific mappings
578  */
579 void __init iotable_init(struct map_desc *io_desc, int nr)
580 {
581         int i;
582
583         for (i = 0; i < nr; i++)
584                 create_mapping(io_desc + i);
585 }
586
587 static inline void
588 free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
589 {
590         struct page *start_pg, *end_pg;
591         unsigned long pg, pgend;
592
593         /*
594          * Convert start_pfn/end_pfn to a struct page pointer.
595          */
596         start_pg = pfn_to_page(start_pfn);
597         end_pg = pfn_to_page(end_pfn);
598
599         /*
600          * Convert to physical addresses, and
601          * round start upwards and end downwards.
602          */
603         pg = PAGE_ALIGN(__pa(start_pg));
604         pgend = __pa(end_pg) & PAGE_MASK;
605
606         /*
607          * If there are free pages between these,
608          * free the section of the memmap array.
609          */
610         if (pg < pgend)
611                 free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
612 }
613
614 static inline void free_unused_memmap_node(int node, struct meminfo *mi)
615 {
616         unsigned long bank_start, prev_bank_end = 0;
617         unsigned int i;
618
619         /*
620          * [FIXME] This relies on each bank being in address order.  This
621          * may not be the case, especially if the user has provided the
622          * information on the command line.
623          */
624         for (i = 0; i < mi->nr_banks; i++) {
625                 if (mi->bank[i].size == 0 || mi->bank[i].node != node)
626                         continue;
627
628                 bank_start = mi->bank[i].start >> PAGE_SHIFT;
629                 if (bank_start < prev_bank_end) {
630                         printk(KERN_ERR "MEM: unordered memory banks.  "
631                                 "Not freeing memmap.\n");
632                         break;
633                 }
634
635                 /*
636                  * If we had a previous bank, and there is a space
637                  * between the current bank and the previous, free it.
638                  */
639                 if (prev_bank_end && prev_bank_end != bank_start)
640                         free_memmap(node, prev_bank_end, bank_start);
641
642                 prev_bank_end = PAGE_ALIGN(mi->bank[i].start +
643                                            mi->bank[i].size) >> PAGE_SHIFT;
644         }
645 }
646
647 /*
648  * The mem_map array can get very big.  Free
649  * the unused area of the memory map.
650  */
651 void __init create_memmap_holes(struct meminfo *mi)
652 {
653         int node;
654
655         for (node = 0; node < numnodes; node++)
656                 free_unused_memmap_node(node, mi);
657 }