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 23*6da2ec56SKees Cook #define __pgd_alloc() kmalloc_array(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL) 24da028779SCatalin Marinas #define __pgd_free(pgd) kfree(pgd) 25da028779SCatalin Marinas #else 26397b080bSMichal Hocko #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 833ed3a4f0SKirill A. Shutemov new_pte = pte_alloc_map(mm, new_pmd, 0); 840c668984SRussell King if (!new_pte) 850c668984SRussell King goto no_pte; 860c668984SRussell King 87a02d8dfdSRussell King #ifndef CONFIG_ARM_LPAE 88a02d8dfdSRussell King /* 89a02d8dfdSRussell King * Modify the PTE pointer to have the correct domain. This 90a02d8dfdSRussell King * needs to be the vectors domain to avoid the low vectors 91a02d8dfdSRussell King * being unmapped. 92a02d8dfdSRussell King */ 93a02d8dfdSRussell King pmd_val(*new_pmd) &= ~PMD_DOMAIN_MASK; 94a02d8dfdSRussell King pmd_val(*new_pmd) |= PMD_DOMAIN(DOMAIN_VECTORS); 95a02d8dfdSRussell King #endif 96a02d8dfdSRussell King 97516295e5SRussell King init_pud = pud_offset(init_pgd, 0); 98516295e5SRussell King init_pmd = pmd_offset(init_pud, 0); 99ece0e2b6SPeter Zijlstra init_pte = pte_offset_map(init_pmd, 0); 100d8aa712cSRussell King set_pte_ext(new_pte + 0, init_pte[0], 0); 101d8aa712cSRussell King set_pte_ext(new_pte + 1, init_pte[1], 0); 102ece0e2b6SPeter Zijlstra pte_unmap(init_pte); 1030c668984SRussell King pte_unmap(new_pte); 1040c668984SRussell King } 1050c668984SRussell King 1060c668984SRussell King return new_pgd; 1070c668984SRussell King 1080c668984SRussell King no_pte: 1095e541973SBenjamin Herrenschmidt pmd_free(mm, new_pmd); 110b30fe6c7SKirill A. Shutemov mm_dec_nr_pmds(mm); 1110c668984SRussell King no_pmd: 112516295e5SRussell King pud_free(mm, new_pud); 113516295e5SRussell King no_pud: 114da028779SCatalin Marinas __pgd_free(new_pgd); 1150c668984SRussell King no_pgd: 1160c668984SRussell King return NULL; 1170c668984SRussell King } 1180c668984SRussell King 1196e4beb5eSRussell King void pgd_free(struct mm_struct *mm, pgd_t *pgd_base) 1200c668984SRussell King { 1216e4beb5eSRussell King pgd_t *pgd; 122516295e5SRussell King pud_t *pud; 1230c668984SRussell King pmd_t *pmd; 1240c82d83cSUwe Kleine-König pgtable_t pte; 1250c668984SRussell King 1266e4beb5eSRussell King if (!pgd_base) 1270c668984SRussell King return; 1280c668984SRussell King 1296e4beb5eSRussell King pgd = pgd_base + pgd_index(0); 1306e4beb5eSRussell King if (pgd_none_or_clear_bad(pgd)) 1316e4beb5eSRussell King goto no_pgd; 1326e4beb5eSRussell King 133516295e5SRussell King pud = pud_offset(pgd, 0); 134516295e5SRussell King if (pud_none_or_clear_bad(pud)) 135516295e5SRussell King goto no_pud; 136516295e5SRussell King 137516295e5SRussell King pmd = pmd_offset(pud, 0); 1386e4beb5eSRussell King if (pmd_none_or_clear_bad(pmd)) 1396e4beb5eSRussell King goto no_pmd; 1400c668984SRussell King 1410c82d83cSUwe Kleine-König pte = pmd_pgtable(*pmd); 1420c668984SRussell King pmd_clear(pmd); 1435e541973SBenjamin Herrenschmidt pte_free(mm, pte); 144c4812909SKirill A. Shutemov mm_dec_nr_ptes(mm); 1456e4beb5eSRussell King no_pmd: 146516295e5SRussell King pud_clear(pud); 1475e541973SBenjamin Herrenschmidt pmd_free(mm, pmd); 148b30fe6c7SKirill A. Shutemov mm_dec_nr_pmds(mm); 149516295e5SRussell King no_pud: 150516295e5SRussell King pgd_clear(pgd); 151516295e5SRussell King pud_free(mm, pud); 1526e4beb5eSRussell King no_pgd: 153da028779SCatalin Marinas #ifdef CONFIG_ARM_LPAE 154da028779SCatalin Marinas /* 155da028779SCatalin Marinas * Free modules/pkmap or identity pmd tables. 156da028779SCatalin Marinas */ 157da028779SCatalin Marinas for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) { 158da028779SCatalin Marinas if (pgd_none_or_clear_bad(pgd)) 159da028779SCatalin Marinas continue; 160da028779SCatalin Marinas if (pgd_val(*pgd) & L_PGD_SWAPPER) 161da028779SCatalin Marinas continue; 162da028779SCatalin Marinas pud = pud_offset(pgd, 0); 163da028779SCatalin Marinas if (pud_none_or_clear_bad(pud)) 164da028779SCatalin Marinas continue; 165da028779SCatalin Marinas pmd = pmd_offset(pud, 0); 166da028779SCatalin Marinas pud_clear(pud); 167da028779SCatalin Marinas pmd_free(mm, pmd); 168b30fe6c7SKirill A. Shutemov mm_dec_nr_pmds(mm); 169da028779SCatalin Marinas pgd_clear(pgd); 170da028779SCatalin Marinas pud_free(mm, pud); 171da028779SCatalin Marinas } 172da028779SCatalin Marinas #endif 173da028779SCatalin Marinas __pgd_free(pgd_base); 1740c668984SRussell King } 175