xref: /linux/arch/arm/mm/pgd.c (revision e926f4495e202500a6265987277fab217e235f08)
10c668984SRussell King /*
20c668984SRussell King  *  linux/arch/arm/mm/pgd.c
30c668984SRussell King  *
40c668984SRussell King  *  Copyright (C) 1998-2005 Russell King
50c668984SRussell King  *
60c668984SRussell King  * This program is free software; you can redistribute it and/or modify
70c668984SRussell King  * it under the terms of the GNU General Public License version 2 as
80c668984SRussell King  * published by the Free Software Foundation.
90c668984SRussell King  */
100c668984SRussell King #include <linux/mm.h>
115a0e3ad6STejun Heo #include <linux/gfp.h>
120c668984SRussell King #include <linux/highmem.h>
130c668984SRussell King 
140c668984SRussell King #include <asm/pgalloc.h>
150c668984SRussell King #include <asm/page.h>
160c668984SRussell King #include <asm/tlbflush.h>
170c668984SRussell King 
180c668984SRussell King #include "mm.h"
190c668984SRussell King 
200c668984SRussell King /*
210c668984SRussell King  * need to get a 16k page for level 1
220c668984SRussell King  */
23b0d03745SRussell King pgd_t *pgd_alloc(struct mm_struct *mm)
240c668984SRussell King {
250c668984SRussell King 	pgd_t *new_pgd, *init_pgd;
260c668984SRussell King 	pmd_t *new_pmd, *init_pmd;
270c668984SRussell King 	pte_t *new_pte, *init_pte;
280c668984SRussell King 
290c668984SRussell King 	new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2);
300c668984SRussell King 	if (!new_pgd)
310c668984SRussell King 		goto no_pgd;
320c668984SRussell King 
33*e926f449SRussell King 	memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
340c668984SRussell King 
350c668984SRussell King 	/*
360c668984SRussell King 	 * Copy over the kernel and IO PGD entries
370c668984SRussell King 	 */
380c668984SRussell King 	init_pgd = pgd_offset_k(0);
39*e926f449SRussell King 	memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD,
40*e926f449SRussell King 		       (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
410c668984SRussell King 
420c668984SRussell King 	clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
430c668984SRussell King 
440c668984SRussell King 	if (!vectors_high()) {
450c668984SRussell King 		/*
460c668984SRussell King 		 * On ARM, first page must always be allocated since it
470c668984SRussell King 		 * contains the machine vectors.
480c668984SRussell King 		 */
490c668984SRussell King 		new_pmd = pmd_alloc(mm, new_pgd, 0);
500c668984SRussell King 		if (!new_pmd)
510c668984SRussell King 			goto no_pmd;
520c668984SRussell King 
530c668984SRussell King 		new_pte = pte_alloc_map(mm, new_pmd, 0);
540c668984SRussell King 		if (!new_pte)
550c668984SRussell King 			goto no_pte;
560c668984SRussell King 
570c668984SRussell King 		init_pmd = pmd_offset(init_pgd, 0);
58ece0e2b6SPeter Zijlstra 		init_pte = pte_offset_map(init_pmd, 0);
59ad1ae2feSRussell King 		set_pte_ext(new_pte, *init_pte, 0);
60ece0e2b6SPeter Zijlstra 		pte_unmap(init_pte);
610c668984SRussell King 		pte_unmap(new_pte);
620c668984SRussell King 	}
630c668984SRussell King 
640c668984SRussell King 	return new_pgd;
650c668984SRussell King 
660c668984SRussell King no_pte:
675e541973SBenjamin Herrenschmidt 	pmd_free(mm, new_pmd);
680c668984SRussell King no_pmd:
690c668984SRussell King 	free_pages((unsigned long)new_pgd, 2);
700c668984SRussell King no_pgd:
710c668984SRussell King 	return NULL;
720c668984SRussell King }
730c668984SRussell King 
746e4beb5eSRussell King void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
750c668984SRussell King {
766e4beb5eSRussell King 	pgd_t *pgd;
770c668984SRussell King 	pmd_t *pmd;
780c82d83cSUwe Kleine-König 	pgtable_t pte;
790c668984SRussell King 
806e4beb5eSRussell King 	if (!pgd_base)
810c668984SRussell King 		return;
820c668984SRussell King 
836e4beb5eSRussell King 	pgd = pgd_base + pgd_index(0);
846e4beb5eSRussell King 	if (pgd_none_or_clear_bad(pgd))
856e4beb5eSRussell King 		goto no_pgd;
866e4beb5eSRussell King 
876e4beb5eSRussell King 	pmd = pmd_offset(pgd, 0);
886e4beb5eSRussell King 	if (pmd_none_or_clear_bad(pmd))
896e4beb5eSRussell King 		goto no_pmd;
900c668984SRussell King 
910c82d83cSUwe Kleine-König 	pte = pmd_pgtable(*pmd);
920c668984SRussell King 	pmd_clear(pmd);
935e541973SBenjamin Herrenschmidt 	pte_free(mm, pte);
946e4beb5eSRussell King no_pmd:
956e4beb5eSRussell King 	pgd_clear(pgd);
965e541973SBenjamin Herrenschmidt 	pmd_free(mm, pmd);
976e4beb5eSRussell King no_pgd:
986e4beb5eSRussell King 	free_pages((unsigned long) pgd_base, 2);
990c668984SRussell King }
100