blob: a3e78ccabd655ef871c74b66b6a277eee29290d6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King0c668982006-09-27 15:40:28 +01002 * linux/arch/arm/mm/pgd.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Russell King90072052005-10-28 14:48:37 +01004 * Copyright (C) 1998-2005 Russell King
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/highmem.h>
Catalin Marinasda028772011-11-22 17:30:29 +000013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/pgalloc.h>
16#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/tlbflush.h>
18
Russell King1b2e2b72006-08-21 17:06:38 +010019#include "mm.h"
20
Catalin Marinasda028772011-11-22 17:30:29 +000021#ifdef CONFIG_ARM_LPAE
22#define __pgd_alloc() kmalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL)
23#define __pgd_free(pgd) kfree(pgd)
24#else
25#define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2)
26#define __pgd_free(pgd) free_pages((unsigned long)pgd, 2)
27#endif
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
30 * need to get a 16k page for level 1
31 */
Russell Kingb0d03742010-11-21 11:00:56 +000032pgd_t *pgd_alloc(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 pgd_t *new_pgd, *init_pgd;
Russell King516295e2010-11-21 16:27:49 +000035 pud_t *new_pud, *init_pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 pmd_t *new_pmd, *init_pmd;
37 pte_t *new_pte, *init_pte;
38
Catalin Marinasda028772011-11-22 17:30:29 +000039 new_pgd = __pgd_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (!new_pgd)
41 goto no_pgd;
42
Russell Kinge926f442010-11-21 11:55:37 +000043 memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Russell Kinga343e602005-06-27 14:08:56 +010045 /*
46 * Copy over the kernel and IO PGD entries
47 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 init_pgd = pgd_offset_k(0);
Russell Kinge926f442010-11-21 11:55:37 +000049 memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD,
50 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
Russell Kinga343e602005-06-27 14:08:56 +010051
52 clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Catalin Marinasda028772011-11-22 17:30:29 +000054#ifdef CONFIG_ARM_LPAE
55 /*
56 * Allocate PMD table for modules and pkmap mappings.
57 */
58 new_pud = pud_alloc(mm, new_pgd + pgd_index(MODULES_VADDR),
59 MODULES_VADDR);
60 if (!new_pud)
61 goto no_pud;
62
63 new_pmd = pmd_alloc(mm, new_pud, 0);
64 if (!new_pmd)
65 goto no_pmd;
66#endif
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (!vectors_high()) {
69 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * On ARM, first page must always be allocated since it
Catalin Marinasda028772011-11-22 17:30:29 +000071 * contains the machine vectors. The vectors are always high
72 * with LPAE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Russell King516295e2010-11-21 16:27:49 +000074 new_pud = pud_alloc(mm, new_pgd, 0);
75 if (!new_pud)
76 goto no_pud;
77
78 new_pmd = pmd_alloc(mm, new_pud, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (!new_pmd)
80 goto no_pmd;
81
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -080082 new_pte = pte_alloc_map(mm, NULL, new_pmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (!new_pte)
84 goto no_pte;
85
Russell King516295e2010-11-21 16:27:49 +000086 init_pud = pud_offset(init_pgd, 0);
87 init_pmd = pmd_offset(init_pud, 0);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -070088 init_pte = pte_offset_map(init_pmd, 0);
Russell Kingad1ae2f2006-12-13 14:34:43 +000089 set_pte_ext(new_pte, *init_pte, 0);
Peter Zijlstraece0e2b2010-10-26 14:21:52 -070090 pte_unmap(init_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 pte_unmap(new_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return new_pgd;
95
96no_pte:
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080097 pmd_free(mm, new_pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098no_pmd:
Russell King516295e2010-11-21 16:27:49 +000099 pud_free(mm, new_pud);
100no_pud:
Catalin Marinasda028772011-11-22 17:30:29 +0000101 __pgd_free(new_pgd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102no_pgd:
103 return NULL;
104}
105
Russell King6e4beb52010-11-21 23:48:55 +0000106void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Russell King6e4beb52010-11-21 23:48:55 +0000108 pgd_t *pgd;
Russell King516295e2010-11-21 16:27:49 +0000109 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 pmd_t *pmd;
Uwe Kleine-König0c82d832008-02-27 13:44:59 +0100111 pgtable_t pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Russell King6e4beb52010-11-21 23:48:55 +0000113 if (!pgd_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return;
115
Russell King6e4beb52010-11-21 23:48:55 +0000116 pgd = pgd_base + pgd_index(0);
117 if (pgd_none_or_clear_bad(pgd))
118 goto no_pgd;
119
Russell King516295e2010-11-21 16:27:49 +0000120 pud = pud_offset(pgd, 0);
121 if (pud_none_or_clear_bad(pud))
122 goto no_pud;
123
124 pmd = pmd_offset(pud, 0);
Russell King6e4beb52010-11-21 23:48:55 +0000125 if (pmd_none_or_clear_bad(pmd))
126 goto no_pmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Uwe Kleine-König0c82d832008-02-27 13:44:59 +0100128 pte = pmd_pgtable(*pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 pmd_clear(pmd);
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800130 pte_free(mm, pte);
Russell King6e4beb52010-11-21 23:48:55 +0000131no_pmd:
Russell King516295e2010-11-21 16:27:49 +0000132 pud_clear(pud);
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800133 pmd_free(mm, pmd);
Russell King516295e2010-11-21 16:27:49 +0000134no_pud:
135 pgd_clear(pgd);
136 pud_free(mm, pud);
Russell King6e4beb52010-11-21 23:48:55 +0000137no_pgd:
Catalin Marinasda028772011-11-22 17:30:29 +0000138#ifdef CONFIG_ARM_LPAE
139 /*
140 * Free modules/pkmap or identity pmd tables.
141 */
142 for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) {
143 if (pgd_none_or_clear_bad(pgd))
144 continue;
145 if (pgd_val(*pgd) & L_PGD_SWAPPER)
146 continue;
147 pud = pud_offset(pgd, 0);
148 if (pud_none_or_clear_bad(pud))
149 continue;
150 pmd = pmd_offset(pud, 0);
151 pud_clear(pud);
152 pmd_free(mm, pmd);
153 pgd_clear(pgd);
154 pud_free(mm, pud);
155 }
156#endif
157 __pgd_free(pgd_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}