pgd.c 3.66 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
Russell King's avatar
Russell King committed
2
 *  linux/arch/arm/mm/pgd.c
Linus Torvalds's avatar
Linus Torvalds committed
3
 *
4
 *  Copyright (C) 1998-2005 Russell King
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/mm.h>
11
#include <linux/gfp.h>
Linus Torvalds's avatar
Linus Torvalds committed
12
#include <linux/highmem.h>
13
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
14

15
#include <asm/cp15.h>
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18 19
#include <asm/pgalloc.h>
#include <asm/page.h>
#include <asm/tlbflush.h>

20 21
#include "mm.h"

22
#ifdef CONFIG_ARM_LPAE
23
#define __pgd_alloc()	kmalloc_array(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL)
24 25
#define __pgd_free(pgd)	kfree(pgd)
#else
26
#define __pgd_alloc()	(pgd_t *)__get_free_pages(GFP_KERNEL, 2)
27 28 29
#define __pgd_free(pgd)	free_pages((unsigned long)pgd, 2)
#endif

Linus Torvalds's avatar
Linus Torvalds committed
30 31 32
/*
 * need to get a 16k page for level 1
 */
33
pgd_t *pgd_alloc(struct mm_struct *mm)
Linus Torvalds's avatar
Linus Torvalds committed
34 35
{
	pgd_t *new_pgd, *init_pgd;
36
	pud_t *new_pud, *init_pud;
Linus Torvalds's avatar
Linus Torvalds committed
37 38 39
	pmd_t *new_pmd, *init_pmd;
	pte_t *new_pte, *init_pte;

40
	new_pgd = __pgd_alloc();
Linus Torvalds's avatar
Linus Torvalds committed
41 42 43
	if (!new_pgd)
		goto no_pgd;

44
	memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
Linus Torvalds's avatar
Linus Torvalds committed
45

46 47 48
	/*
	 * Copy over the kernel and IO PGD entries
	 */
Linus Torvalds's avatar
Linus Torvalds committed
49
	init_pgd = pgd_offset_k(0);
50 51
	memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD,
		       (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
52 53

	clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
Linus Torvalds's avatar
Linus Torvalds committed
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68
#ifdef CONFIG_ARM_LPAE
	/*
	 * Allocate PMD table for modules and pkmap mappings.
	 */
	new_pud = pud_alloc(mm, new_pgd + pgd_index(MODULES_VADDR),
			    MODULES_VADDR);
	if (!new_pud)
		goto no_pud;

	new_pmd = pmd_alloc(mm, new_pud, 0);
	if (!new_pmd)
		goto no_pmd;
#endif

Linus Torvalds's avatar
Linus Torvalds committed
69 70 71
	if (!vectors_high()) {
		/*
		 * On ARM, first page must always be allocated since it
72 73
		 * contains the machine vectors. The vectors are always high
		 * with LPAE.
Linus Torvalds's avatar
Linus Torvalds committed
74
		 */
75 76 77 78 79
		new_pud = pud_alloc(mm, new_pgd, 0);
		if (!new_pud)
			goto no_pud;

		new_pmd = pmd_alloc(mm, new_pud, 0);
Linus Torvalds's avatar
Linus Torvalds committed
80 81 82
		if (!new_pmd)
			goto no_pmd;

83
		new_pte = pte_alloc_map(mm, new_pmd, 0);
Linus Torvalds's avatar
Linus Torvalds committed
84 85 86
		if (!new_pte)
			goto no_pte;

87 88 89 90 91 92 93 94 95 96
#ifndef CONFIG_ARM_LPAE
		/*
		 * Modify the PTE pointer to have the correct domain.  This
		 * needs to be the vectors domain to avoid the low vectors
		 * being unmapped.
		 */
		pmd_val(*new_pmd) &= ~PMD_DOMAIN_MASK;
		pmd_val(*new_pmd) |= PMD_DOMAIN(DOMAIN_VECTORS);
#endif

97 98
		init_pud = pud_offset(init_pgd, 0);
		init_pmd = pmd_offset(init_pud, 0);
Peter Zijlstra's avatar
Peter Zijlstra committed
99
		init_pte = pte_offset_map(init_pmd, 0);
100 101
		set_pte_ext(new_pte + 0, init_pte[0], 0);
		set_pte_ext(new_pte + 1, init_pte[1], 0);
Peter Zijlstra's avatar
Peter Zijlstra committed
102
		pte_unmap(init_pte);
Linus Torvalds's avatar
Linus Torvalds committed
103 104 105 106 107 108
		pte_unmap(new_pte);
	}

	return new_pgd;

no_pte:
109
	pmd_free(mm, new_pmd);
110
	mm_dec_nr_pmds(mm);
Linus Torvalds's avatar
Linus Torvalds committed
111
no_pmd:
112 113
	pud_free(mm, new_pud);
no_pud:
114
	__pgd_free(new_pgd);
Linus Torvalds's avatar
Linus Torvalds committed
115 116 117 118
no_pgd:
	return NULL;
}

119
void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
Linus Torvalds's avatar
Linus Torvalds committed
120
{
121
	pgd_t *pgd;
122
	pud_t *pud;
Linus Torvalds's avatar
Linus Torvalds committed
123
	pmd_t *pmd;
124
	pgtable_t pte;
Linus Torvalds's avatar
Linus Torvalds committed
125

126
	if (!pgd_base)
Linus Torvalds's avatar
Linus Torvalds committed
127 128
		return;

129 130 131 132
	pgd = pgd_base + pgd_index(0);
	if (pgd_none_or_clear_bad(pgd))
		goto no_pgd;

133 134 135 136 137
	pud = pud_offset(pgd, 0);
	if (pud_none_or_clear_bad(pud))
		goto no_pud;

	pmd = pmd_offset(pud, 0);
138 139
	if (pmd_none_or_clear_bad(pmd))
		goto no_pmd;
Linus Torvalds's avatar
Linus Torvalds committed
140

141
	pte = pmd_pgtable(*pmd);
Linus Torvalds's avatar
Linus Torvalds committed
142
	pmd_clear(pmd);
143
	pte_free(mm, pte);
144
	mm_dec_nr_ptes(mm);
145
no_pmd:
146
	pud_clear(pud);
147
	pmd_free(mm, pmd);
148
	mm_dec_nr_pmds(mm);
149 150 151
no_pud:
	pgd_clear(pgd);
	pud_free(mm, pud);
152
no_pgd:
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#ifdef CONFIG_ARM_LPAE
	/*
	 * Free modules/pkmap or identity pmd tables.
	 */
	for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) {
		if (pgd_none_or_clear_bad(pgd))
			continue;
		if (pgd_val(*pgd) & L_PGD_SWAPPER)
			continue;
		pud = pud_offset(pgd, 0);
		if (pud_none_or_clear_bad(pud))
			continue;
		pmd = pmd_offset(pud, 0);
		pud_clear(pud);
		pmd_free(mm, pmd);
168
		mm_dec_nr_pmds(mm);
169 170 171 172 173
		pgd_clear(pgd);
		pud_free(mm, pud);
	}
#endif
	__pgd_free(pgd_base);
Linus Torvalds's avatar
Linus Torvalds committed
174
}