Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Based on arch/arm/mm/mmu.c |
| 3 | * |
| 4 | * Copyright (C) 1995-2005 Russell King |
| 5 | * Copyright (C) 2012 ARM Ltd. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/export.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/init.h> |
Ard Biesheuvel | 61bd93c | 2015-06-01 13:40:32 +0200 | [diff] [blame] | 24 | #include <linux/libfdt.h> |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 25 | #include <linux/mman.h> |
| 26 | #include <linux/nodemask.h> |
| 27 | #include <linux/memblock.h> |
| 28 | #include <linux/fs.h> |
Catalin Marinas | 2475ff9 | 2012-10-23 14:55:08 +0100 | [diff] [blame] | 29 | #include <linux/io.h> |
Catalin Marinas | 4108935 | 2015-01-29 17:33:35 +0000 | [diff] [blame] | 30 | #include <linux/slab.h> |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 31 | #include <linux/stop_machine.h> |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 32 | |
Mark Rutland | 21ab99c | 2016-01-25 11:44:56 +0000 | [diff] [blame] | 33 | #include <asm/barrier.h> |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 34 | #include <asm/cputype.h> |
Laura Abbott | af86e59 | 2014-11-21 21:50:42 +0000 | [diff] [blame] | 35 | #include <asm/fixmap.h> |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame^] | 36 | #include <asm/kasan.h> |
Suzuki K. Poulose | b433dce | 2015-10-19 14:19:28 +0100 | [diff] [blame] | 37 | #include <asm/kernel-pgtable.h> |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 38 | #include <asm/sections.h> |
| 39 | #include <asm/setup.h> |
| 40 | #include <asm/sizes.h> |
| 41 | #include <asm/tlb.h> |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 42 | #include <asm/memblock.h> |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 43 | #include <asm/mmu_context.h> |
| 44 | |
| 45 | #include "mm.h" |
| 46 | |
Ard Biesheuvel | dd006da | 2015-03-19 16:42:27 +0000 | [diff] [blame] | 47 | u64 idmap_t0sz = TCR_T0SZ(VA_BITS); |
| 48 | |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 49 | /* |
| 50 | * Empty_zero_page is a special page that is used for zero-initialized data |
| 51 | * and COW. |
| 52 | */ |
Mark Rutland | 5227cfa | 2016-01-25 11:44:57 +0000 | [diff] [blame] | 53 | unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss; |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 54 | EXPORT_SYMBOL(empty_zero_page); |
| 55 | |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 56 | pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, |
| 57 | unsigned long size, pgprot_t vma_prot) |
| 58 | { |
| 59 | if (!pfn_valid(pfn)) |
| 60 | return pgprot_noncached(vma_prot); |
| 61 | else if (file->f_flags & O_SYNC) |
| 62 | return pgprot_writecombine(vma_prot); |
| 63 | return vma_prot; |
| 64 | } |
| 65 | EXPORT_SYMBOL(phys_mem_access_prot); |
| 66 | |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 67 | static phys_addr_t __init early_pgtable_alloc(void) |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 68 | { |
Suzuki K. Poulose | 7142392 | 2015-11-20 17:45:40 +0000 | [diff] [blame] | 69 | phys_addr_t phys; |
| 70 | void *ptr; |
| 71 | |
Mark Rutland | 21ab99c | 2016-01-25 11:44:56 +0000 | [diff] [blame] | 72 | phys = memblock_alloc(PAGE_SIZE, PAGE_SIZE); |
Suzuki K. Poulose | 7142392 | 2015-11-20 17:45:40 +0000 | [diff] [blame] | 73 | BUG_ON(!phys); |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE |
| 77 | * slot will be free, so we can (ab)use the FIX_PTE slot to initialise |
| 78 | * any level of table. |
| 79 | */ |
| 80 | ptr = pte_set_fixmap(phys); |
| 81 | |
Mark Rutland | 21ab99c | 2016-01-25 11:44:56 +0000 | [diff] [blame] | 82 | memset(ptr, 0, PAGE_SIZE); |
| 83 | |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 84 | /* |
| 85 | * Implicit barriers also ensure the zeroed page is visible to the page |
| 86 | * table walker |
| 87 | */ |
| 88 | pte_clear_fixmap(); |
| 89 | |
| 90 | return phys; |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 93 | /* |
| 94 | * remap a PMD into pages |
| 95 | */ |
| 96 | static void split_pmd(pmd_t *pmd, pte_t *pte) |
| 97 | { |
| 98 | unsigned long pfn = pmd_pfn(*pmd); |
| 99 | int i = 0; |
| 100 | |
| 101 | do { |
| 102 | /* |
| 103 | * Need to have the least restrictive permissions available |
Catalin Marinas | 667c275 | 2015-11-26 15:42:41 +0000 | [diff] [blame] | 104 | * permissions will be fixed up later |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 105 | */ |
Catalin Marinas | 667c275 | 2015-11-26 15:42:41 +0000 | [diff] [blame] | 106 | set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC)); |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 107 | pfn++; |
| 108 | } while (pte++, i++, i < PTRS_PER_PTE); |
| 109 | } |
| 110 | |
| 111 | static void alloc_init_pte(pmd_t *pmd, unsigned long addr, |
Catalin Marinas | 667c275 | 2015-11-26 15:42:41 +0000 | [diff] [blame] | 112 | unsigned long end, unsigned long pfn, |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 113 | pgprot_t prot, |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 114 | phys_addr_t (*pgtable_alloc)(void)) |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 115 | { |
| 116 | pte_t *pte; |
| 117 | |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 118 | if (pmd_none(*pmd) || pmd_sect(*pmd)) { |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 119 | phys_addr_t pte_phys = pgtable_alloc(); |
| 120 | pte = pte_set_fixmap(pte_phys); |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 121 | if (pmd_sect(*pmd)) |
| 122 | split_pmd(pmd, pte); |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 123 | __pmd_populate(pmd, pte_phys, PMD_TYPE_TABLE); |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 124 | flush_tlb_all(); |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 125 | pte_clear_fixmap(); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 126 | } |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 127 | BUG_ON(pmd_bad(*pmd)); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 128 | |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 129 | pte = pte_set_fixmap_offset(pmd, addr); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 130 | do { |
Catalin Marinas | 667c275 | 2015-11-26 15:42:41 +0000 | [diff] [blame] | 131 | set_pte(pte, pfn_pte(pfn, prot)); |
| 132 | pfn++; |
| 133 | } while (pte++, addr += PAGE_SIZE, addr != end); |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 134 | |
| 135 | pte_clear_fixmap(); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Jisheng Zhang | 9a17a21 | 2015-11-12 20:04:43 +0800 | [diff] [blame] | 138 | static void split_pud(pud_t *old_pud, pmd_t *pmd) |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 139 | { |
| 140 | unsigned long addr = pud_pfn(*old_pud) << PAGE_SHIFT; |
| 141 | pgprot_t prot = __pgprot(pud_val(*old_pud) ^ addr); |
| 142 | int i = 0; |
| 143 | |
| 144 | do { |
Ard Biesheuvel | 1e43ba9 | 2015-06-30 18:04:49 +0200 | [diff] [blame] | 145 | set_pmd(pmd, __pmd(addr | pgprot_val(prot))); |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 146 | addr += PMD_SIZE; |
| 147 | } while (pmd++, i++, i < PTRS_PER_PMD); |
| 148 | } |
| 149 | |
Mark Rutland | 11509a3 | 2016-01-25 11:45:10 +0000 | [diff] [blame] | 150 | static void alloc_init_pmd(pud_t *pud, unsigned long addr, unsigned long end, |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 151 | phys_addr_t phys, pgprot_t prot, |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 152 | phys_addr_t (*pgtable_alloc)(void)) |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 153 | { |
| 154 | pmd_t *pmd; |
| 155 | unsigned long next; |
| 156 | |
| 157 | /* |
| 158 | * Check for initial section mappings in the pgd/pud and remove them. |
| 159 | */ |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 160 | if (pud_none(*pud) || pud_sect(*pud)) { |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 161 | phys_addr_t pmd_phys = pgtable_alloc(); |
| 162 | pmd = pmd_set_fixmap(pmd_phys); |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 163 | if (pud_sect(*pud)) { |
| 164 | /* |
| 165 | * need to have the 1G of mappings continue to be |
| 166 | * present |
| 167 | */ |
| 168 | split_pud(pud, pmd); |
| 169 | } |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 170 | __pud_populate(pud, pmd_phys, PUD_TYPE_TABLE); |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 171 | flush_tlb_all(); |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 172 | pmd_clear_fixmap(); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 173 | } |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 174 | BUG_ON(pud_bad(*pud)); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 175 | |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 176 | pmd = pmd_set_fixmap_offset(pud, addr); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 177 | do { |
| 178 | next = pmd_addr_end(addr, end); |
| 179 | /* try section mapping first */ |
Catalin Marinas | a55f992 | 2014-02-04 16:01:31 +0000 | [diff] [blame] | 180 | if (((addr | next | phys) & ~SECTION_MASK) == 0) { |
| 181 | pmd_t old_pmd =*pmd; |
Ard Biesheuvel | 8ce837c | 2014-10-20 15:42:07 +0200 | [diff] [blame] | 182 | set_pmd(pmd, __pmd(phys | |
| 183 | pgprot_val(mk_sect_prot(prot)))); |
Catalin Marinas | a55f992 | 2014-02-04 16:01:31 +0000 | [diff] [blame] | 184 | /* |
| 185 | * Check for previous table entries created during |
| 186 | * boot (__create_page_tables) and flush them. |
| 187 | */ |
zhichang.yuan | 523d6e9 | 2014-12-09 07:26:47 +0000 | [diff] [blame] | 188 | if (!pmd_none(old_pmd)) { |
Catalin Marinas | a55f992 | 2014-02-04 16:01:31 +0000 | [diff] [blame] | 189 | flush_tlb_all(); |
zhichang.yuan | 523d6e9 | 2014-12-09 07:26:47 +0000 | [diff] [blame] | 190 | if (pmd_table(old_pmd)) { |
Mark Rutland | 316b39db | 2016-01-25 11:45:05 +0000 | [diff] [blame] | 191 | phys_addr_t table = pmd_page_paddr(old_pmd); |
Catalin Marinas | 4108935 | 2015-01-29 17:33:35 +0000 | [diff] [blame] | 192 | if (!WARN_ON_ONCE(slab_is_available())) |
| 193 | memblock_free(table, PAGE_SIZE); |
zhichang.yuan | 523d6e9 | 2014-12-09 07:26:47 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
Catalin Marinas | a55f992 | 2014-02-04 16:01:31 +0000 | [diff] [blame] | 196 | } else { |
Catalin Marinas | 667c275 | 2015-11-26 15:42:41 +0000 | [diff] [blame] | 197 | alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys), |
Mark Rutland | 21ab99c | 2016-01-25 11:44:56 +0000 | [diff] [blame] | 198 | prot, pgtable_alloc); |
Catalin Marinas | a55f992 | 2014-02-04 16:01:31 +0000 | [diff] [blame] | 199 | } |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 200 | phys += next - addr; |
| 201 | } while (pmd++, addr = next, addr != end); |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 202 | |
| 203 | pmd_clear_fixmap(); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 206 | static inline bool use_1G_block(unsigned long addr, unsigned long next, |
| 207 | unsigned long phys) |
| 208 | { |
| 209 | if (PAGE_SHIFT != 12) |
| 210 | return false; |
| 211 | |
| 212 | if (((addr | next | phys) & ~PUD_MASK) != 0) |
| 213 | return false; |
| 214 | |
| 215 | return true; |
| 216 | } |
| 217 | |
Mark Rutland | 11509a3 | 2016-01-25 11:45:10 +0000 | [diff] [blame] | 218 | static void alloc_init_pud(pgd_t *pgd, unsigned long addr, unsigned long end, |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 219 | phys_addr_t phys, pgprot_t prot, |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 220 | phys_addr_t (*pgtable_alloc)(void)) |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 221 | { |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 222 | pud_t *pud; |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 223 | unsigned long next; |
| 224 | |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 225 | if (pgd_none(*pgd)) { |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 226 | phys_addr_t pud_phys = pgtable_alloc(); |
| 227 | __pgd_populate(pgd, pud_phys, PUD_TYPE_TABLE); |
Jungseok Lee | c79b954 | 2014-05-12 18:40:51 +0900 | [diff] [blame] | 228 | } |
| 229 | BUG_ON(pgd_bad(*pgd)); |
| 230 | |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 231 | pud = pud_set_fixmap_offset(pgd, addr); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 232 | do { |
| 233 | next = pud_addr_end(addr, end); |
Steve Capper | 206a2a7 | 2014-05-06 14:02:27 +0100 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | * For 4K granule only, attempt to put down a 1GB block |
| 237 | */ |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 238 | if (use_1G_block(addr, next, phys)) { |
Steve Capper | 206a2a7 | 2014-05-06 14:02:27 +0100 | [diff] [blame] | 239 | pud_t old_pud = *pud; |
Ard Biesheuvel | 8ce837c | 2014-10-20 15:42:07 +0200 | [diff] [blame] | 240 | set_pud(pud, __pud(phys | |
| 241 | pgprot_val(mk_sect_prot(prot)))); |
Steve Capper | 206a2a7 | 2014-05-06 14:02:27 +0100 | [diff] [blame] | 242 | |
| 243 | /* |
| 244 | * If we have an old value for a pud, it will |
| 245 | * be pointing to a pmd table that we no longer |
| 246 | * need (from swapper_pg_dir). |
| 247 | * |
| 248 | * Look up the old pmd table and free it. |
| 249 | */ |
| 250 | if (!pud_none(old_pud)) { |
Steve Capper | 206a2a7 | 2014-05-06 14:02:27 +0100 | [diff] [blame] | 251 | flush_tlb_all(); |
zhichang.yuan | 523d6e9 | 2014-12-09 07:26:47 +0000 | [diff] [blame] | 252 | if (pud_table(old_pud)) { |
Mark Rutland | 316b39db | 2016-01-25 11:45:05 +0000 | [diff] [blame] | 253 | phys_addr_t table = pud_page_paddr(old_pud); |
Catalin Marinas | 4108935 | 2015-01-29 17:33:35 +0000 | [diff] [blame] | 254 | if (!WARN_ON_ONCE(slab_is_available())) |
| 255 | memblock_free(table, PAGE_SIZE); |
zhichang.yuan | 523d6e9 | 2014-12-09 07:26:47 +0000 | [diff] [blame] | 256 | } |
Steve Capper | 206a2a7 | 2014-05-06 14:02:27 +0100 | [diff] [blame] | 257 | } |
| 258 | } else { |
Mark Rutland | 11509a3 | 2016-01-25 11:45:10 +0000 | [diff] [blame] | 259 | alloc_init_pmd(pud, addr, next, phys, prot, |
Mark Rutland | 21ab99c | 2016-01-25 11:44:56 +0000 | [diff] [blame] | 260 | pgtable_alloc); |
Steve Capper | 206a2a7 | 2014-05-06 14:02:27 +0100 | [diff] [blame] | 261 | } |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 262 | phys += next - addr; |
| 263 | } while (pud++, addr = next, addr != end); |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 264 | |
| 265 | pud_clear_fixmap(); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Create the page directory entries and any necessary page tables for the |
| 270 | * mapping specified by 'md'. |
| 271 | */ |
Mark Rutland | 11509a3 | 2016-01-25 11:45:10 +0000 | [diff] [blame] | 272 | static void init_pgd(pgd_t *pgd, phys_addr_t phys, unsigned long virt, |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 273 | phys_addr_t size, pgprot_t prot, |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 274 | phys_addr_t (*pgtable_alloc)(void)) |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 275 | { |
| 276 | unsigned long addr, length, end, next; |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 277 | |
Mark Rutland | cc5d2b3 | 2015-11-23 13:26:19 +0000 | [diff] [blame] | 278 | /* |
| 279 | * If the virtual and physical address don't have the same offset |
| 280 | * within a page, we cannot map the region as the caller expects. |
| 281 | */ |
| 282 | if (WARN_ON((phys ^ virt) & ~PAGE_MASK)) |
| 283 | return; |
| 284 | |
Mark Rutland | 9c4e08a | 2015-11-23 13:26:20 +0000 | [diff] [blame] | 285 | phys &= PAGE_MASK; |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 286 | addr = virt & PAGE_MASK; |
| 287 | length = PAGE_ALIGN(size + (virt & ~PAGE_MASK)); |
| 288 | |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 289 | end = addr + length; |
| 290 | do { |
| 291 | next = pgd_addr_end(addr, end); |
Mark Rutland | 11509a3 | 2016-01-25 11:45:10 +0000 | [diff] [blame] | 292 | alloc_init_pud(pgd, addr, next, phys, prot, pgtable_alloc); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 293 | phys += next - addr; |
| 294 | } while (pgd++, addr = next, addr != end); |
| 295 | } |
| 296 | |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 297 | static phys_addr_t late_pgtable_alloc(void) |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 298 | { |
Mark Rutland | 21ab99c | 2016-01-25 11:44:56 +0000 | [diff] [blame] | 299 | void *ptr = (void *)__get_free_page(PGALLOC_GFP); |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 300 | BUG_ON(!ptr); |
Mark Rutland | 21ab99c | 2016-01-25 11:44:56 +0000 | [diff] [blame] | 301 | |
| 302 | /* Ensure the zeroed page is visible to the page table walker */ |
| 303 | dsb(ishst); |
Mark Rutland | f471044 | 2016-01-25 11:45:08 +0000 | [diff] [blame] | 304 | return __pa(ptr); |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Mark Rutland | 11509a3 | 2016-01-25 11:45:10 +0000 | [diff] [blame] | 307 | static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, |
| 308 | unsigned long virt, phys_addr_t size, |
| 309 | pgprot_t prot, |
| 310 | phys_addr_t (*alloc)(void)) |
| 311 | { |
| 312 | init_pgd(pgd_offset_raw(pgdir, virt), phys, virt, size, prot, alloc); |
| 313 | } |
| 314 | |
Mark Rutland | c53e0ba | 2015-07-28 10:31:06 +0100 | [diff] [blame] | 315 | static void __init create_mapping(phys_addr_t phys, unsigned long virt, |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 316 | phys_addr_t size, pgprot_t prot) |
Mark Salter | d7ecbdd | 2014-03-12 12:28:06 -0400 | [diff] [blame] | 317 | { |
| 318 | if (virt < VMALLOC_START) { |
| 319 | pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n", |
| 320 | &phys, virt); |
| 321 | return; |
| 322 | } |
Mark Rutland | 11509a3 | 2016-01-25 11:45:10 +0000 | [diff] [blame] | 323 | __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, |
| 324 | early_pgtable_alloc); |
Mark Salter | d7ecbdd | 2014-03-12 12:28:06 -0400 | [diff] [blame] | 325 | } |
| 326 | |
Ard Biesheuvel | 8ce837c | 2014-10-20 15:42:07 +0200 | [diff] [blame] | 327 | void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys, |
| 328 | unsigned long virt, phys_addr_t size, |
| 329 | pgprot_t prot) |
| 330 | { |
Mark Rutland | 11509a3 | 2016-01-25 11:45:10 +0000 | [diff] [blame] | 331 | __create_pgd_mapping(mm->pgd, phys, virt, size, prot, |
| 332 | late_pgtable_alloc); |
Mark Salter | d7ecbdd | 2014-03-12 12:28:06 -0400 | [diff] [blame] | 333 | } |
| 334 | |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 335 | static void create_mapping_late(phys_addr_t phys, unsigned long virt, |
| 336 | phys_addr_t size, pgprot_t prot) |
| 337 | { |
| 338 | if (virt < VMALLOC_START) { |
| 339 | pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n", |
| 340 | &phys, virt); |
| 341 | return; |
| 342 | } |
| 343 | |
Mark Rutland | 11509a3 | 2016-01-25 11:45:10 +0000 | [diff] [blame] | 344 | __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, |
| 345 | late_pgtable_alloc); |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame^] | 348 | static void __init __map_memblock(pgd_t *pgd, phys_addr_t start, phys_addr_t end) |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 349 | { |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 350 | |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame^] | 351 | unsigned long kernel_start = __pa(_stext); |
| 352 | unsigned long kernel_end = __pa(_end); |
| 353 | |
| 354 | /* |
| 355 | * The kernel itself is mapped at page granularity. Map all other |
| 356 | * memory, making sure we don't overwrite the existing kernel mappings. |
| 357 | */ |
| 358 | |
| 359 | /* No overlap with the kernel. */ |
| 360 | if (end < kernel_start || start >= kernel_end) { |
| 361 | __create_pgd_mapping(pgd, start, __phys_to_virt(start), |
| 362 | end - start, PAGE_KERNEL, |
| 363 | early_pgtable_alloc); |
| 364 | return; |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame^] | 367 | /* |
| 368 | * This block overlaps the kernel mapping. Map the portion(s) which |
| 369 | * don't overlap. |
| 370 | */ |
| 371 | if (start < kernel_start) |
| 372 | __create_pgd_mapping(pgd, start, |
| 373 | __phys_to_virt(start), |
| 374 | kernel_start - start, PAGE_KERNEL, |
| 375 | early_pgtable_alloc); |
| 376 | if (kernel_end < end) |
| 377 | __create_pgd_mapping(pgd, kernel_end, |
| 378 | __phys_to_virt(kernel_end), |
| 379 | end - kernel_end, PAGE_KERNEL, |
| 380 | early_pgtable_alloc); |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 381 | } |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 382 | |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame^] | 383 | static void __init map_mem(pgd_t *pgd) |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 384 | { |
| 385 | struct memblock_region *reg; |
Steve Capper | f6bc87c | 2013-04-30 11:00:33 +0100 | [diff] [blame] | 386 | |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 387 | /* map all the memory banks */ |
| 388 | for_each_memblock(memory, reg) { |
| 389 | phys_addr_t start = reg->base; |
| 390 | phys_addr_t end = start + reg->size; |
| 391 | |
| 392 | if (start >= end) |
| 393 | break; |
Ard Biesheuvel | 68709f4 | 2015-11-30 13:28:16 +0100 | [diff] [blame] | 394 | if (memblock_is_nomap(reg)) |
| 395 | continue; |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 396 | |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame^] | 397 | __map_memblock(pgd, start, end); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 401 | #ifdef CONFIG_DEBUG_RODATA |
| 402 | void mark_rodata_ro(void) |
| 403 | { |
| 404 | create_mapping_late(__pa(_stext), (unsigned long)_stext, |
| 405 | (unsigned long)_etext - (unsigned long)_stext, |
Laura Abbott | 0b2aa5b | 2015-11-12 12:21:10 -0800 | [diff] [blame] | 406 | PAGE_KERNEL_ROX); |
Laura Abbott | da14170 | 2015-01-21 17:36:06 -0800 | [diff] [blame] | 407 | |
| 408 | } |
| 409 | #endif |
| 410 | |
| 411 | void fixup_init(void) |
| 412 | { |
| 413 | create_mapping_late(__pa(__init_begin), (unsigned long)__init_begin, |
| 414 | (unsigned long)__init_end - (unsigned long)__init_begin, |
| 415 | PAGE_KERNEL); |
| 416 | } |
| 417 | |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame^] | 418 | static void __init map_kernel_chunk(pgd_t *pgd, void *va_start, void *va_end, |
| 419 | pgprot_t prot) |
| 420 | { |
| 421 | phys_addr_t pa_start = __pa(va_start); |
| 422 | unsigned long size = va_end - va_start; |
| 423 | |
| 424 | BUG_ON(!PAGE_ALIGNED(pa_start)); |
| 425 | BUG_ON(!PAGE_ALIGNED(size)); |
| 426 | |
| 427 | __create_pgd_mapping(pgd, pa_start, (unsigned long)va_start, size, prot, |
| 428 | early_pgtable_alloc); |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * Create fine-grained mappings for the kernel. |
| 433 | */ |
| 434 | static void __init map_kernel(pgd_t *pgd) |
| 435 | { |
| 436 | |
| 437 | map_kernel_chunk(pgd, _stext, _etext, PAGE_KERNEL_EXEC); |
| 438 | map_kernel_chunk(pgd, __init_begin, __init_end, PAGE_KERNEL_EXEC); |
| 439 | map_kernel_chunk(pgd, _data, _end, PAGE_KERNEL); |
| 440 | |
| 441 | /* |
| 442 | * The fixmap falls in a separate pgd to the kernel, and doesn't live |
| 443 | * in the carveout for the swapper_pg_dir. We can simply re-use the |
| 444 | * existing dir for the fixmap. |
| 445 | */ |
| 446 | set_pgd(pgd_offset_raw(pgd, FIXADDR_START), *pgd_offset_k(FIXADDR_START)); |
| 447 | |
| 448 | kasan_copy_shadow(pgd); |
| 449 | } |
| 450 | |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 451 | /* |
| 452 | * paging_init() sets up the page tables, initialises the zone memory |
| 453 | * maps and sets up the zero page. |
| 454 | */ |
| 455 | void __init paging_init(void) |
| 456 | { |
Mark Rutland | 068a17a | 2016-01-25 11:45:12 +0000 | [diff] [blame^] | 457 | phys_addr_t pgd_phys = early_pgtable_alloc(); |
| 458 | pgd_t *pgd = pgd_set_fixmap(pgd_phys); |
| 459 | |
| 460 | map_kernel(pgd); |
| 461 | map_mem(pgd); |
| 462 | |
| 463 | /* |
| 464 | * We want to reuse the original swapper_pg_dir so we don't have to |
| 465 | * communicate the new address to non-coherent secondaries in |
| 466 | * secondary_entry, and so cpu_switch_mm can generate the address with |
| 467 | * adrp+add rather than a load from some global variable. |
| 468 | * |
| 469 | * To do this we need to go via a temporary pgd. |
| 470 | */ |
| 471 | cpu_replace_ttbr1(__va(pgd_phys)); |
| 472 | memcpy(swapper_pg_dir, pgd, PAGE_SIZE); |
| 473 | cpu_replace_ttbr1(swapper_pg_dir); |
| 474 | |
| 475 | pgd_clear_fixmap(); |
| 476 | memblock_free(pgd_phys, PAGE_SIZE); |
| 477 | |
| 478 | /* |
| 479 | * We only reuse the PGD from the swapper_pg_dir, not the pud + pmd |
| 480 | * allocated with it. |
| 481 | */ |
| 482 | memblock_free(__pa(swapper_pg_dir) + PAGE_SIZE, |
| 483 | SWAPPER_DIR_SIZE - PAGE_SIZE); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 484 | |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 485 | bootmem_init(); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 489 | * Check whether a kernel address is valid (derived from arch/x86/). |
| 490 | */ |
| 491 | int kern_addr_valid(unsigned long addr) |
| 492 | { |
| 493 | pgd_t *pgd; |
| 494 | pud_t *pud; |
| 495 | pmd_t *pmd; |
| 496 | pte_t *pte; |
| 497 | |
| 498 | if ((((long)addr) >> VA_BITS) != -1UL) |
| 499 | return 0; |
| 500 | |
| 501 | pgd = pgd_offset_k(addr); |
| 502 | if (pgd_none(*pgd)) |
| 503 | return 0; |
| 504 | |
| 505 | pud = pud_offset(pgd, addr); |
| 506 | if (pud_none(*pud)) |
| 507 | return 0; |
| 508 | |
Steve Capper | 206a2a7 | 2014-05-06 14:02:27 +0100 | [diff] [blame] | 509 | if (pud_sect(*pud)) |
| 510 | return pfn_valid(pud_pfn(*pud)); |
| 511 | |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 512 | pmd = pmd_offset(pud, addr); |
| 513 | if (pmd_none(*pmd)) |
| 514 | return 0; |
| 515 | |
Dave Anderson | da6e4cb | 2014-04-15 18:53:24 +0100 | [diff] [blame] | 516 | if (pmd_sect(*pmd)) |
| 517 | return pfn_valid(pmd_pfn(*pmd)); |
| 518 | |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 519 | pte = pte_offset_kernel(pmd, addr); |
| 520 | if (pte_none(*pte)) |
| 521 | return 0; |
| 522 | |
| 523 | return pfn_valid(pte_pfn(*pte)); |
| 524 | } |
| 525 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
Suzuki K. Poulose | b433dce | 2015-10-19 14:19:28 +0100 | [diff] [blame] | 526 | #if !ARM64_SWAPPER_USES_SECTION_MAPS |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 527 | int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node) |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 528 | { |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 529 | return vmemmap_populate_basepages(start, end, node); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 530 | } |
Suzuki K. Poulose | b433dce | 2015-10-19 14:19:28 +0100 | [diff] [blame] | 531 | #else /* !ARM64_SWAPPER_USES_SECTION_MAPS */ |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 532 | int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node) |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 533 | { |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 534 | unsigned long addr = start; |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 535 | unsigned long next; |
| 536 | pgd_t *pgd; |
| 537 | pud_t *pud; |
| 538 | pmd_t *pmd; |
| 539 | |
| 540 | do { |
| 541 | next = pmd_addr_end(addr, end); |
| 542 | |
| 543 | pgd = vmemmap_pgd_populate(addr, node); |
| 544 | if (!pgd) |
| 545 | return -ENOMEM; |
| 546 | |
| 547 | pud = vmemmap_pud_populate(pgd, addr, node); |
| 548 | if (!pud) |
| 549 | return -ENOMEM; |
| 550 | |
| 551 | pmd = pmd_offset(pud, addr); |
| 552 | if (pmd_none(*pmd)) { |
| 553 | void *p = NULL; |
| 554 | |
| 555 | p = vmemmap_alloc_block_buf(PMD_SIZE, node); |
| 556 | if (!p) |
| 557 | return -ENOMEM; |
| 558 | |
Catalin Marinas | a501e32 | 2014-04-03 15:57:15 +0100 | [diff] [blame] | 559 | set_pmd(pmd, __pmd(__pa(p) | PROT_SECT_NORMAL)); |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 560 | } else |
| 561 | vmemmap_verify((pte_t *)pmd, node, addr, next); |
| 562 | } while (addr = next, addr != end); |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | #endif /* CONFIG_ARM64_64K_PAGES */ |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 567 | void vmemmap_free(unsigned long start, unsigned long end) |
Tang Chen | 0197518 | 2013-02-22 16:33:08 -0800 | [diff] [blame] | 568 | { |
| 569 | } |
Catalin Marinas | c1cc155 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 570 | #endif /* CONFIG_SPARSEMEM_VMEMMAP */ |
Laura Abbott | af86e59 | 2014-11-21 21:50:42 +0000 | [diff] [blame] | 571 | |
| 572 | static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss; |
Kirill A. Shutemov | 9f25e6a | 2015-04-14 15:45:39 -0700 | [diff] [blame] | 573 | #if CONFIG_PGTABLE_LEVELS > 2 |
Laura Abbott | af86e59 | 2014-11-21 21:50:42 +0000 | [diff] [blame] | 574 | static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss; |
| 575 | #endif |
Kirill A. Shutemov | 9f25e6a | 2015-04-14 15:45:39 -0700 | [diff] [blame] | 576 | #if CONFIG_PGTABLE_LEVELS > 3 |
Laura Abbott | af86e59 | 2014-11-21 21:50:42 +0000 | [diff] [blame] | 577 | static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss; |
| 578 | #endif |
| 579 | |
| 580 | static inline pud_t * fixmap_pud(unsigned long addr) |
| 581 | { |
| 582 | pgd_t *pgd = pgd_offset_k(addr); |
| 583 | |
| 584 | BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd)); |
| 585 | |
| 586 | return pud_offset(pgd, addr); |
| 587 | } |
| 588 | |
| 589 | static inline pmd_t * fixmap_pmd(unsigned long addr) |
| 590 | { |
| 591 | pud_t *pud = fixmap_pud(addr); |
| 592 | |
| 593 | BUG_ON(pud_none(*pud) || pud_bad(*pud)); |
| 594 | |
| 595 | return pmd_offset(pud, addr); |
| 596 | } |
| 597 | |
| 598 | static inline pte_t * fixmap_pte(unsigned long addr) |
| 599 | { |
| 600 | pmd_t *pmd = fixmap_pmd(addr); |
| 601 | |
| 602 | BUG_ON(pmd_none(*pmd) || pmd_bad(*pmd)); |
| 603 | |
| 604 | return pte_offset_kernel(pmd, addr); |
| 605 | } |
| 606 | |
| 607 | void __init early_fixmap_init(void) |
| 608 | { |
| 609 | pgd_t *pgd; |
| 610 | pud_t *pud; |
| 611 | pmd_t *pmd; |
| 612 | unsigned long addr = FIXADDR_START; |
| 613 | |
| 614 | pgd = pgd_offset_k(addr); |
| 615 | pgd_populate(&init_mm, pgd, bm_pud); |
| 616 | pud = pud_offset(pgd, addr); |
| 617 | pud_populate(&init_mm, pud, bm_pmd); |
| 618 | pmd = pmd_offset(pud, addr); |
| 619 | pmd_populate_kernel(&init_mm, pmd, bm_pte); |
| 620 | |
| 621 | /* |
| 622 | * The boot-ioremap range spans multiple pmds, for which |
| 623 | * we are not preparted: |
| 624 | */ |
| 625 | BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT) |
| 626 | != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT)); |
| 627 | |
| 628 | if ((pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN))) |
| 629 | || pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) { |
| 630 | WARN_ON(1); |
| 631 | pr_warn("pmd %p != %p, %p\n", |
| 632 | pmd, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)), |
| 633 | fixmap_pmd(fix_to_virt(FIX_BTMAP_END))); |
| 634 | pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n", |
| 635 | fix_to_virt(FIX_BTMAP_BEGIN)); |
| 636 | pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n", |
| 637 | fix_to_virt(FIX_BTMAP_END)); |
| 638 | |
| 639 | pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END); |
| 640 | pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | void __set_fixmap(enum fixed_addresses idx, |
| 645 | phys_addr_t phys, pgprot_t flags) |
| 646 | { |
| 647 | unsigned long addr = __fix_to_virt(idx); |
| 648 | pte_t *pte; |
| 649 | |
Mark Rutland | b63dbef | 2015-03-04 13:27:35 +0000 | [diff] [blame] | 650 | BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); |
Laura Abbott | af86e59 | 2014-11-21 21:50:42 +0000 | [diff] [blame] | 651 | |
| 652 | pte = fixmap_pte(addr); |
| 653 | |
| 654 | if (pgprot_val(flags)) { |
| 655 | set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags)); |
| 656 | } else { |
| 657 | pte_clear(&init_mm, addr, pte); |
| 658 | flush_tlb_kernel_range(addr, addr+PAGE_SIZE); |
| 659 | } |
| 660 | } |
Ard Biesheuvel | 61bd93c | 2015-06-01 13:40:32 +0200 | [diff] [blame] | 661 | |
| 662 | void *__init fixmap_remap_fdt(phys_addr_t dt_phys) |
| 663 | { |
| 664 | const u64 dt_virt_base = __fix_to_virt(FIX_FDT); |
Ard Biesheuvel | fb226c3 | 2015-11-09 09:55:46 +0100 | [diff] [blame] | 665 | pgprot_t prot = PAGE_KERNEL_RO; |
Suzuki K. Poulose | b433dce | 2015-10-19 14:19:28 +0100 | [diff] [blame] | 666 | int size, offset; |
Ard Biesheuvel | 61bd93c | 2015-06-01 13:40:32 +0200 | [diff] [blame] | 667 | void *dt_virt; |
| 668 | |
| 669 | /* |
| 670 | * Check whether the physical FDT address is set and meets the minimum |
| 671 | * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be |
| 672 | * at least 8 bytes so that we can always access the size field of the |
| 673 | * FDT header after mapping the first chunk, double check here if that |
| 674 | * is indeed the case. |
| 675 | */ |
| 676 | BUILD_BUG_ON(MIN_FDT_ALIGN < 8); |
| 677 | if (!dt_phys || dt_phys % MIN_FDT_ALIGN) |
| 678 | return NULL; |
| 679 | |
| 680 | /* |
| 681 | * Make sure that the FDT region can be mapped without the need to |
| 682 | * allocate additional translation table pages, so that it is safe |
| 683 | * to call create_mapping() this early. |
| 684 | * |
| 685 | * On 64k pages, the FDT will be mapped using PTEs, so we need to |
| 686 | * be in the same PMD as the rest of the fixmap. |
| 687 | * On 4k pages, we'll use section mappings for the FDT so we only |
| 688 | * have to be in the same PUD. |
| 689 | */ |
| 690 | BUILD_BUG_ON(dt_virt_base % SZ_2M); |
| 691 | |
Suzuki K. Poulose | b433dce | 2015-10-19 14:19:28 +0100 | [diff] [blame] | 692 | BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT != |
| 693 | __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT); |
Ard Biesheuvel | 61bd93c | 2015-06-01 13:40:32 +0200 | [diff] [blame] | 694 | |
Suzuki K. Poulose | b433dce | 2015-10-19 14:19:28 +0100 | [diff] [blame] | 695 | offset = dt_phys % SWAPPER_BLOCK_SIZE; |
Ard Biesheuvel | 61bd93c | 2015-06-01 13:40:32 +0200 | [diff] [blame] | 696 | dt_virt = (void *)dt_virt_base + offset; |
| 697 | |
| 698 | /* map the first chunk so we can read the size from the header */ |
Suzuki K. Poulose | b433dce | 2015-10-19 14:19:28 +0100 | [diff] [blame] | 699 | create_mapping(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base, |
| 700 | SWAPPER_BLOCK_SIZE, prot); |
Ard Biesheuvel | 61bd93c | 2015-06-01 13:40:32 +0200 | [diff] [blame] | 701 | |
| 702 | if (fdt_check_header(dt_virt) != 0) |
| 703 | return NULL; |
| 704 | |
| 705 | size = fdt_totalsize(dt_virt); |
| 706 | if (size > MAX_FDT_SIZE) |
| 707 | return NULL; |
| 708 | |
Suzuki K. Poulose | b433dce | 2015-10-19 14:19:28 +0100 | [diff] [blame] | 709 | if (offset + size > SWAPPER_BLOCK_SIZE) |
| 710 | create_mapping(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base, |
| 711 | round_up(offset + size, SWAPPER_BLOCK_SIZE), prot); |
Ard Biesheuvel | 61bd93c | 2015-06-01 13:40:32 +0200 | [diff] [blame] | 712 | |
| 713 | memblock_reserve(dt_phys, size); |
| 714 | |
| 715 | return dt_virt; |
| 716 | } |