Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Russell King | 0c66898 | 2006-09-27 15:40:28 +0100 | [diff] [blame] | 2 | * linux/arch/arm/mm/pgd.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 4 | * Copyright (C) 1998-2005 Russell King |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/highmem.h> |
Catalin Marinas | da02877 | 2011-11-22 17:30:29 +0000 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Russell King | 15d07dc | 2012-03-28 18:30:01 +0100 | [diff] [blame] | 15 | #include <asm/cp15.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/pgalloc.h> |
| 17 | #include <asm/page.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/tlbflush.h> |
| 19 | |
Russell King | 1b2e2b7 | 2006-08-21 17:06:38 +0100 | [diff] [blame] | 20 | #include "mm.h" |
| 21 | |
Catalin Marinas | da02877 | 2011-11-22 17:30:29 +0000 | [diff] [blame] | 22 | #ifdef CONFIG_ARM_LPAE |
| 23 | #define __pgd_alloc() kmalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL) |
| 24 | #define __pgd_free(pgd) kfree(pgd) |
| 25 | #else |
Russell King | 8c65da6 | 2013-11-30 12:52:31 +0000 | [diff] [blame] | 26 | #define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_REPEAT, 2) |
Catalin Marinas | da02877 | 2011-11-22 17:30:29 +0000 | [diff] [blame] | 27 | #define __pgd_free(pgd) free_pages((unsigned long)pgd, 2) |
| 28 | #endif |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | /* |
| 31 | * need to get a 16k page for level 1 |
| 32 | */ |
Russell King | b0d0374 | 2010-11-21 11:00:56 +0000 | [diff] [blame] | 33 | pgd_t *pgd_alloc(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
| 35 | pgd_t *new_pgd, *init_pgd; |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 36 | pud_t *new_pud, *init_pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | pmd_t *new_pmd, *init_pmd; |
| 38 | pte_t *new_pte, *init_pte; |
| 39 | |
Catalin Marinas | da02877 | 2011-11-22 17:30:29 +0000 | [diff] [blame] | 40 | new_pgd = __pgd_alloc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | if (!new_pgd) |
| 42 | goto no_pgd; |
| 43 | |
Russell King | e926f44 | 2010-11-21 11:55:37 +0000 | [diff] [blame] | 44 | memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Russell King | a343e60 | 2005-06-27 14:08:56 +0100 | [diff] [blame] | 46 | /* |
| 47 | * Copy over the kernel and IO PGD entries |
| 48 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | init_pgd = pgd_offset_k(0); |
Russell King | e926f44 | 2010-11-21 11:55:37 +0000 | [diff] [blame] | 50 | memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD, |
| 51 | (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); |
Russell King | a343e60 | 2005-06-27 14:08:56 +0100 | [diff] [blame] | 52 | |
| 53 | clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Catalin Marinas | da02877 | 2011-11-22 17:30:29 +0000 | [diff] [blame] | 55 | #ifdef CONFIG_ARM_LPAE |
| 56 | /* |
| 57 | * Allocate PMD table for modules and pkmap mappings. |
| 58 | */ |
| 59 | new_pud = pud_alloc(mm, new_pgd + pgd_index(MODULES_VADDR), |
| 60 | MODULES_VADDR); |
| 61 | if (!new_pud) |
| 62 | goto no_pud; |
| 63 | |
| 64 | new_pmd = pmd_alloc(mm, new_pud, 0); |
| 65 | if (!new_pmd) |
| 66 | goto no_pmd; |
| 67 | #endif |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | if (!vectors_high()) { |
| 70 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | * On ARM, first page must always be allocated since it |
Catalin Marinas | da02877 | 2011-11-22 17:30:29 +0000 | [diff] [blame] | 72 | * contains the machine vectors. The vectors are always high |
| 73 | * with LPAE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 75 | new_pud = pud_alloc(mm, new_pgd, 0); |
| 76 | if (!new_pud) |
| 77 | goto no_pud; |
| 78 | |
| 79 | new_pmd = pmd_alloc(mm, new_pud, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if (!new_pmd) |
| 81 | goto no_pmd; |
| 82 | |
Andrea Arcangeli | 8ac1f83 | 2011-01-13 15:46:43 -0800 | [diff] [blame] | 83 | new_pte = pte_alloc_map(mm, NULL, new_pmd, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | if (!new_pte) |
| 85 | goto no_pte; |
| 86 | |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 87 | init_pud = pud_offset(init_pgd, 0); |
| 88 | init_pmd = pmd_offset(init_pud, 0); |
Peter Zijlstra | ece0e2b | 2010-10-26 14:21:52 -0700 | [diff] [blame] | 89 | init_pte = pte_offset_map(init_pmd, 0); |
Russell King | d8aa712 | 2013-11-28 21:43:40 +0000 | [diff] [blame] | 90 | set_pte_ext(new_pte + 0, init_pte[0], 0); |
| 91 | set_pte_ext(new_pte + 1, init_pte[1], 0); |
Peter Zijlstra | ece0e2b | 2010-10-26 14:21:52 -0700 | [diff] [blame] | 92 | pte_unmap(init_pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | pte_unmap(new_pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return new_pgd; |
| 97 | |
| 98 | no_pte: |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 99 | pmd_free(mm, new_pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | no_pmd: |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 101 | pud_free(mm, new_pud); |
| 102 | no_pud: |
Catalin Marinas | da02877 | 2011-11-22 17:30:29 +0000 | [diff] [blame] | 103 | __pgd_free(new_pgd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | no_pgd: |
| 105 | return NULL; |
| 106 | } |
| 107 | |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 108 | void pgd_free(struct mm_struct *mm, pgd_t *pgd_base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 110 | pgd_t *pgd; |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 111 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | pmd_t *pmd; |
Uwe Kleine-König | 0c82d83 | 2008-02-27 13:44:59 +0100 | [diff] [blame] | 113 | pgtable_t pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 115 | if (!pgd_base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return; |
| 117 | |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 118 | pgd = pgd_base + pgd_index(0); |
| 119 | if (pgd_none_or_clear_bad(pgd)) |
| 120 | goto no_pgd; |
| 121 | |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 122 | pud = pud_offset(pgd, 0); |
| 123 | if (pud_none_or_clear_bad(pud)) |
| 124 | goto no_pud; |
| 125 | |
| 126 | pmd = pmd_offset(pud, 0); |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 127 | if (pmd_none_or_clear_bad(pmd)) |
| 128 | goto no_pmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Uwe Kleine-König | 0c82d83 | 2008-02-27 13:44:59 +0100 | [diff] [blame] | 130 | pte = pmd_pgtable(*pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | pmd_clear(pmd); |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 132 | pte_free(mm, pte); |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 133 | no_pmd: |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 134 | pud_clear(pud); |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 135 | pmd_free(mm, pmd); |
Russell King | 516295e | 2010-11-21 16:27:49 +0000 | [diff] [blame] | 136 | no_pud: |
| 137 | pgd_clear(pgd); |
| 138 | pud_free(mm, pud); |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 139 | no_pgd: |
Catalin Marinas | da02877 | 2011-11-22 17:30:29 +0000 | [diff] [blame] | 140 | #ifdef CONFIG_ARM_LPAE |
| 141 | /* |
| 142 | * Free modules/pkmap or identity pmd tables. |
| 143 | */ |
| 144 | for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) { |
| 145 | if (pgd_none_or_clear_bad(pgd)) |
| 146 | continue; |
| 147 | if (pgd_val(*pgd) & L_PGD_SWAPPER) |
| 148 | continue; |
| 149 | pud = pud_offset(pgd, 0); |
| 150 | if (pud_none_or_clear_bad(pud)) |
| 151 | continue; |
| 152 | pmd = pmd_offset(pud, 0); |
| 153 | pud_clear(pud); |
| 154 | pmd_free(mm, pmd); |
| 155 | pgd_clear(pgd); |
| 156 | pud_free(mm, pud); |
| 157 | } |
| 158 | #endif |
| 159 | __pgd_free(pgd_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |