Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / arch / arm / mm / pgd.c
1 /*
2  *  linux/arch/arm/mm/pgd.c
3  *
4  *  Copyright (C) 1998-2005 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 #include <linux/mm.h>
11 #include <linux/gfp.h>
12 #include <linux/highmem.h>
13 #include <linux/slab.h>
14
15 #include <asm/pgalloc.h>
16 #include <asm/page.h>
17 #include <asm/tlbflush.h>
18
19 #include "mm.h"
20
21 /*
22  * need to get a 16k page for level 1
23  */
24 pgd_t *pgd_alloc(struct mm_struct *mm)
25 {
26         pgd_t *new_pgd, *init_pgd;
27         pud_t *new_pud, *init_pud;
28         pmd_t *new_pmd, *init_pmd;
29         pte_t *new_pte, *init_pte;
30
31         new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2);
32         if (!new_pgd)
33                 goto no_pgd;
34
35         memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
36
37         /*
38          * Copy over the kernel and IO PGD entries
39          */
40         init_pgd = pgd_offset_k(0);
41         memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD,
42                        (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
43
44         clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
45
46         if (!vectors_high()) {
47                 /*
48                  * On ARM, first page must always be allocated since it
49                  * contains the machine vectors.
50                  */
51                 new_pud = pud_alloc(mm, new_pgd, 0);
52                 if (!new_pud)
53                         goto no_pud;
54
55                 new_pmd = pmd_alloc(mm, new_pud, 0);
56                 if (!new_pmd)
57                         goto no_pmd;
58
59                 new_pte = pte_alloc_map(mm, NULL, new_pmd, 0);
60                 if (!new_pte)
61                         goto no_pte;
62
63                 init_pud = pud_offset(init_pgd, 0);
64                 init_pmd = pmd_offset(init_pud, 0);
65                 init_pte = pte_offset_map(init_pmd, 0);
66                 set_pte_ext(new_pte, *init_pte, 0);
67                 pte_unmap(init_pte);
68                 pte_unmap(new_pte);
69         }
70
71 #ifdef CONFIG_ARM_HUGETLB_SUPPORT
72         /* reset the hugepage linux pte pointer
73          * for new mm_struct when we do the fork
74          */
75         mm->context.huge_linux_pte = NULL;
76 #endif
77         return new_pgd;
78
79 no_pte:
80         pmd_free(mm, new_pmd);
81 no_pmd:
82         pud_free(mm, new_pud);
83 no_pud:
84         free_pages((unsigned long)new_pgd, 2);
85 no_pgd:
86 #ifdef CONFIG_ARM_HUGETLB_SUPPORT
87         /* free huge linux pte table */
88         if (mm->context.huge_linux_pte != NULL)
89                 kfree(mm->context.huge_linux_pte);
90 #endif
91         return NULL;
92 }
93
94 void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
95 {
96         pgd_t *pgd;
97         pud_t *pud;
98         pmd_t *pmd;
99         pgtable_t pte;
100
101         if (!pgd_base)
102                 return;
103
104         pgd = pgd_base + pgd_index(0);
105         if (pgd_none_or_clear_bad(pgd))
106                 goto no_pgd;
107
108         pud = pud_offset(pgd, 0);
109         if (pud_none_or_clear_bad(pud))
110                 goto no_pud;
111
112         pmd = pmd_offset(pud, 0);
113         if (pmd_none_or_clear_bad(pmd))
114                 goto no_pmd;
115
116         pte = pmd_pgtable(*pmd);
117         pmd_clear(pmd);
118         pte_free(mm, pte);
119 no_pmd:
120         pud_clear(pud);
121         pmd_free(mm, pmd);
122 no_pud:
123         pgd_clear(pgd);
124         pud_free(mm, pud);
125 no_pgd:
126         free_pages((unsigned long) pgd_base, 2);
127 }