xref: /linux/arch/arm/mm/pgd.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
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>
11*5a0e3ad6STejun 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 #define FIRST_KERNEL_PGD_NR	(FIRST_USER_PGD_NR + USER_PTRS_PER_PGD)
210c668984SRussell King 
220c668984SRussell King /*
230c668984SRussell King  * need to get a 16k page for level 1
240c668984SRussell King  */
250c668984SRussell King pgd_t *get_pgd_slow(struct mm_struct *mm)
260c668984SRussell King {
270c668984SRussell King 	pgd_t *new_pgd, *init_pgd;
280c668984SRussell King 	pmd_t *new_pmd, *init_pmd;
290c668984SRussell King 	pte_t *new_pte, *init_pte;
300c668984SRussell King 
310c668984SRussell King 	new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2);
320c668984SRussell King 	if (!new_pgd)
330c668984SRussell King 		goto no_pgd;
340c668984SRussell King 
3559f0cb0fSRussell King 	memset(new_pgd, 0, FIRST_KERNEL_PGD_NR * sizeof(pgd_t));
360c668984SRussell King 
370c668984SRussell King 	/*
380c668984SRussell King 	 * Copy over the kernel and IO PGD entries
390c668984SRussell King 	 */
400c668984SRussell King 	init_pgd = pgd_offset_k(0);
410c668984SRussell King 	memcpy(new_pgd + FIRST_KERNEL_PGD_NR, init_pgd + FIRST_KERNEL_PGD_NR,
420c668984SRussell King 		       (PTRS_PER_PGD - FIRST_KERNEL_PGD_NR) * sizeof(pgd_t));
430c668984SRussell King 
440c668984SRussell King 	clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
450c668984SRussell King 
460c668984SRussell King 	if (!vectors_high()) {
470c668984SRussell King 		/*
480c668984SRussell King 		 * On ARM, first page must always be allocated since it
490c668984SRussell King 		 * contains the machine vectors.
500c668984SRussell King 		 */
510c668984SRussell King 		new_pmd = pmd_alloc(mm, new_pgd, 0);
520c668984SRussell King 		if (!new_pmd)
530c668984SRussell King 			goto no_pmd;
540c668984SRussell King 
550c668984SRussell King 		new_pte = pte_alloc_map(mm, new_pmd, 0);
560c668984SRussell King 		if (!new_pte)
570c668984SRussell King 			goto no_pte;
580c668984SRussell King 
590c668984SRussell King 		init_pmd = pmd_offset(init_pgd, 0);
600c668984SRussell King 		init_pte = pte_offset_map_nested(init_pmd, 0);
61ad1ae2feSRussell King 		set_pte_ext(new_pte, *init_pte, 0);
620c668984SRussell King 		pte_unmap_nested(init_pte);
630c668984SRussell King 		pte_unmap(new_pte);
640c668984SRussell King 	}
650c668984SRussell King 
660c668984SRussell King 	return new_pgd;
670c668984SRussell King 
680c668984SRussell King no_pte:
695e541973SBenjamin Herrenschmidt 	pmd_free(mm, new_pmd);
700c668984SRussell King no_pmd:
710c668984SRussell King 	free_pages((unsigned long)new_pgd, 2);
720c668984SRussell King no_pgd:
730c668984SRussell King 	return NULL;
740c668984SRussell King }
750c668984SRussell King 
765e541973SBenjamin Herrenschmidt void free_pgd_slow(struct mm_struct *mm, pgd_t *pgd)
770c668984SRussell King {
780c668984SRussell King 	pmd_t *pmd;
790c82d83cSUwe Kleine-König 	pgtable_t pte;
800c668984SRussell King 
810c668984SRussell King 	if (!pgd)
820c668984SRussell King 		return;
830c668984SRussell King 
840c668984SRussell King 	/* pgd is always present and good */
850c668984SRussell King 	pmd = pmd_off(pgd, 0);
860c668984SRussell King 	if (pmd_none(*pmd))
870c668984SRussell King 		goto free;
880c668984SRussell King 	if (pmd_bad(*pmd)) {
890c668984SRussell King 		pmd_ERROR(*pmd);
900c668984SRussell King 		pmd_clear(pmd);
910c668984SRussell King 		goto free;
920c668984SRussell King 	}
930c668984SRussell King 
940c82d83cSUwe Kleine-König 	pte = pmd_pgtable(*pmd);
950c668984SRussell King 	pmd_clear(pmd);
965e541973SBenjamin Herrenschmidt 	pte_free(mm, pte);
975e541973SBenjamin Herrenschmidt 	pmd_free(mm, pmd);
980c668984SRussell King free:
990c668984SRussell King 	free_pages((unsigned long) pgd, 2);
1000c668984SRussell King }
101