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> 13da028779SCatalin Marinas #include <linux/slab.h> 140c668984SRussell King 1515d07dc9SRussell King #include <asm/cp15.h> 160c668984SRussell King #include <asm/pgalloc.h> 170c668984SRussell King #include <asm/page.h> 180c668984SRussell King #include <asm/tlbflush.h> 190c668984SRussell King 200c668984SRussell King #include "mm.h" 210c668984SRussell King 22da028779SCatalin Marinas #ifdef CONFIG_ARM_LPAE 23da028779SCatalin Marinas #define __pgd_alloc() kmalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL) 24da028779SCatalin Marinas #define __pgd_free(pgd) kfree(pgd) 25da028779SCatalin Marinas #else 26da028779SCatalin Marinas #define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2) 27da028779SCatalin Marinas #define __pgd_free(pgd) free_pages((unsigned long)pgd, 2) 28da028779SCatalin Marinas #endif 29da028779SCatalin Marinas 300c668984SRussell King /* 310c668984SRussell King * need to get a 16k page for level 1 320c668984SRussell King */ 33b0d03745SRussell King pgd_t *pgd_alloc(struct mm_struct *mm) 340c668984SRussell King { 350c668984SRussell King pgd_t *new_pgd, *init_pgd; 36516295e5SRussell King pud_t *new_pud, *init_pud; 370c668984SRussell King pmd_t *new_pmd, *init_pmd; 380c668984SRussell King pte_t *new_pte, *init_pte; 390c668984SRussell King 40da028779SCatalin Marinas new_pgd = __pgd_alloc(); 410c668984SRussell King if (!new_pgd) 420c668984SRussell King goto no_pgd; 430c668984SRussell King 44e926f449SRussell King memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); 450c668984SRussell King 460c668984SRussell King /* 470c668984SRussell King * Copy over the kernel and IO PGD entries 480c668984SRussell King */ 490c668984SRussell King init_pgd = pgd_offset_k(0); 50e926f449SRussell King memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD, 51e926f449SRussell King (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); 520c668984SRussell King 530c668984SRussell King clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t)); 540c668984SRussell King 55da028779SCatalin Marinas #ifdef CONFIG_ARM_LPAE 56da028779SCatalin Marinas /* 57da028779SCatalin Marinas * Allocate PMD table for modules and pkmap mappings. 58da028779SCatalin Marinas */ 59da028779SCatalin Marinas new_pud = pud_alloc(mm, new_pgd + pgd_index(MODULES_VADDR), 60da028779SCatalin Marinas MODULES_VADDR); 61da028779SCatalin Marinas if (!new_pud) 62da028779SCatalin Marinas goto no_pud; 63da028779SCatalin Marinas 64da028779SCatalin Marinas new_pmd = pmd_alloc(mm, new_pud, 0); 65da028779SCatalin Marinas if (!new_pmd) 66da028779SCatalin Marinas goto no_pmd; 67da028779SCatalin Marinas #endif 68da028779SCatalin Marinas 690c668984SRussell King if (!vectors_high()) { 700c668984SRussell King /* 710c668984SRussell King * On ARM, first page must always be allocated since it 72da028779SCatalin Marinas * contains the machine vectors. The vectors are always high 73da028779SCatalin Marinas * with LPAE. 740c668984SRussell King */ 75516295e5SRussell King new_pud = pud_alloc(mm, new_pgd, 0); 76516295e5SRussell King if (!new_pud) 77516295e5SRussell King goto no_pud; 78516295e5SRussell King 79516295e5SRussell King new_pmd = pmd_alloc(mm, new_pud, 0); 800c668984SRussell King if (!new_pmd) 810c668984SRussell King goto no_pmd; 820c668984SRussell King 838ac1f832SAndrea Arcangeli new_pte = pte_alloc_map(mm, NULL, new_pmd, 0); 840c668984SRussell King if (!new_pte) 850c668984SRussell King goto no_pte; 860c668984SRussell King 87516295e5SRussell King init_pud = pud_offset(init_pgd, 0); 88516295e5SRussell King init_pmd = pmd_offset(init_pud, 0); 89ece0e2b6SPeter Zijlstra init_pte = pte_offset_map(init_pmd, 0); 90*d8aa712cSRussell King set_pte_ext(new_pte + 0, init_pte[0], 0); 91*d8aa712cSRussell King set_pte_ext(new_pte + 1, init_pte[1], 0); 92ece0e2b6SPeter Zijlstra pte_unmap(init_pte); 930c668984SRussell King pte_unmap(new_pte); 940c668984SRussell King } 950c668984SRussell King 960c668984SRussell King return new_pgd; 970c668984SRussell King 980c668984SRussell King no_pte: 995e541973SBenjamin Herrenschmidt pmd_free(mm, new_pmd); 1000c668984SRussell King no_pmd: 101516295e5SRussell King pud_free(mm, new_pud); 102516295e5SRussell King no_pud: 103da028779SCatalin Marinas __pgd_free(new_pgd); 1040c668984SRussell King no_pgd: 1050c668984SRussell King return NULL; 1060c668984SRussell King } 1070c668984SRussell King 1086e4beb5eSRussell King void pgd_free(struct mm_struct *mm, pgd_t *pgd_base) 1090c668984SRussell King { 1106e4beb5eSRussell King pgd_t *pgd; 111516295e5SRussell King pud_t *pud; 1120c668984SRussell King pmd_t *pmd; 1130c82d83cSUwe Kleine-König pgtable_t pte; 1140c668984SRussell King 1156e4beb5eSRussell King if (!pgd_base) 1160c668984SRussell King return; 1170c668984SRussell King 1186e4beb5eSRussell King pgd = pgd_base + pgd_index(0); 1196e4beb5eSRussell King if (pgd_none_or_clear_bad(pgd)) 1206e4beb5eSRussell King goto no_pgd; 1216e4beb5eSRussell King 122516295e5SRussell King pud = pud_offset(pgd, 0); 123516295e5SRussell King if (pud_none_or_clear_bad(pud)) 124516295e5SRussell King goto no_pud; 125516295e5SRussell King 126516295e5SRussell King pmd = pmd_offset(pud, 0); 1276e4beb5eSRussell King if (pmd_none_or_clear_bad(pmd)) 1286e4beb5eSRussell King goto no_pmd; 1290c668984SRussell King 1300c82d83cSUwe Kleine-König pte = pmd_pgtable(*pmd); 1310c668984SRussell King pmd_clear(pmd); 1325e541973SBenjamin Herrenschmidt pte_free(mm, pte); 1336e4beb5eSRussell King no_pmd: 134516295e5SRussell King pud_clear(pud); 1355e541973SBenjamin Herrenschmidt pmd_free(mm, pmd); 136516295e5SRussell King no_pud: 137516295e5SRussell King pgd_clear(pgd); 138516295e5SRussell King pud_free(mm, pud); 1396e4beb5eSRussell King no_pgd: 140da028779SCatalin Marinas #ifdef CONFIG_ARM_LPAE 141da028779SCatalin Marinas /* 142da028779SCatalin Marinas * Free modules/pkmap or identity pmd tables. 143da028779SCatalin Marinas */ 144da028779SCatalin Marinas for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) { 145da028779SCatalin Marinas if (pgd_none_or_clear_bad(pgd)) 146da028779SCatalin Marinas continue; 147da028779SCatalin Marinas if (pgd_val(*pgd) & L_PGD_SWAPPER) 148da028779SCatalin Marinas continue; 149da028779SCatalin Marinas pud = pud_offset(pgd, 0); 150da028779SCatalin Marinas if (pud_none_or_clear_bad(pud)) 151da028779SCatalin Marinas continue; 152da028779SCatalin Marinas pmd = pmd_offset(pud, 0); 153da028779SCatalin Marinas pud_clear(pud); 154da028779SCatalin Marinas pmd_free(mm, pmd); 155da028779SCatalin Marinas pgd_clear(pgd); 156da028779SCatalin Marinas pud_free(mm, pud); 157da028779SCatalin Marinas } 158da028779SCatalin Marinas #endif 159da028779SCatalin Marinas __pgd_free(pgd_base); 1600c668984SRussell King } 161