mmu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. #include <linux/export.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/libfdt.h>
  24. #include <linux/mman.h>
  25. #include <linux/nodemask.h>
  26. #include <linux/memblock.h>
  27. #include <linux/fs.h>
  28. #include <linux/io.h>
  29. #include <linux/slab.h>
  30. #include <linux/stop_machine.h>
  31. #include <asm/cputype.h>
  32. #include <asm/fixmap.h>
  33. #include <asm/sections.h>
  34. #include <asm/setup.h>
  35. #include <asm/sizes.h>
  36. #include <asm/tlb.h>
  37. #include <asm/memblock.h>
  38. #include <asm/mmu_context.h>
  39. #include "mm.h"
  40. u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
  41. /*
  42. * Empty_zero_page is a special page that is used for zero-initialized data
  43. * and COW.
  44. */
  45. struct page *empty_zero_page;
  46. EXPORT_SYMBOL(empty_zero_page);
  47. pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  48. unsigned long size, pgprot_t vma_prot)
  49. {
  50. if (!pfn_valid(pfn))
  51. return pgprot_noncached(vma_prot);
  52. else if (file->f_flags & O_SYNC)
  53. return pgprot_writecombine(vma_prot);
  54. return vma_prot;
  55. }
  56. EXPORT_SYMBOL(phys_mem_access_prot);
  57. static void __init *early_alloc(unsigned long sz)
  58. {
  59. void *ptr = __va(memblock_alloc(sz, sz));
  60. BUG_ON(!ptr);
  61. memset(ptr, 0, sz);
  62. return ptr;
  63. }
  64. /*
  65. * remap a PMD into pages
  66. */
  67. static void split_pmd(pmd_t *pmd, pte_t *pte)
  68. {
  69. unsigned long pfn = pmd_pfn(*pmd);
  70. int i = 0;
  71. do {
  72. /*
  73. * Need to have the least restrictive permissions available
  74. * permissions will be fixed up later
  75. */
  76. set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
  77. pfn++;
  78. } while (pte++, i++, i < PTRS_PER_PTE);
  79. }
  80. static void alloc_init_pte(pmd_t *pmd, unsigned long addr,
  81. unsigned long end, unsigned long pfn,
  82. pgprot_t prot,
  83. void *(*alloc)(unsigned long size))
  84. {
  85. pte_t *pte;
  86. if (pmd_none(*pmd) || pmd_sect(*pmd)) {
  87. pte = alloc(PTRS_PER_PTE * sizeof(pte_t));
  88. if (pmd_sect(*pmd))
  89. split_pmd(pmd, pte);
  90. __pmd_populate(pmd, __pa(pte), PMD_TYPE_TABLE);
  91. flush_tlb_all();
  92. }
  93. BUG_ON(pmd_bad(*pmd));
  94. pte = pte_offset_kernel(pmd, addr);
  95. do {
  96. set_pte(pte, pfn_pte(pfn, prot));
  97. pfn++;
  98. } while (pte++, addr += PAGE_SIZE, addr != end);
  99. }
  100. void split_pud(pud_t *old_pud, pmd_t *pmd)
  101. {
  102. unsigned long addr = pud_pfn(*old_pud) << PAGE_SHIFT;
  103. pgprot_t prot = __pgprot(pud_val(*old_pud) ^ addr);
  104. int i = 0;
  105. do {
  106. set_pmd(pmd, __pmd(addr | pgprot_val(prot)));
  107. addr += PMD_SIZE;
  108. } while (pmd++, i++, i < PTRS_PER_PMD);
  109. }
  110. static void alloc_init_pmd(struct mm_struct *mm, pud_t *pud,
  111. unsigned long addr, unsigned long end,
  112. phys_addr_t phys, pgprot_t prot,
  113. void *(*alloc)(unsigned long size))
  114. {
  115. pmd_t *pmd;
  116. unsigned long next;
  117. /*
  118. * Check for initial section mappings in the pgd/pud and remove them.
  119. */
  120. if (pud_none(*pud) || pud_sect(*pud)) {
  121. pmd = alloc(PTRS_PER_PMD * sizeof(pmd_t));
  122. if (pud_sect(*pud)) {
  123. /*
  124. * need to have the 1G of mappings continue to be
  125. * present
  126. */
  127. split_pud(pud, pmd);
  128. }
  129. pud_populate(mm, pud, pmd);
  130. flush_tlb_all();
  131. }
  132. BUG_ON(pud_bad(*pud));
  133. pmd = pmd_offset(pud, addr);
  134. do {
  135. next = pmd_addr_end(addr, end);
  136. /* try section mapping first */
  137. if (((addr | next | phys) & ~SECTION_MASK) == 0) {
  138. pmd_t old_pmd =*pmd;
  139. set_pmd(pmd, __pmd(phys |
  140. pgprot_val(mk_sect_prot(prot))));
  141. /*
  142. * Check for previous table entries created during
  143. * boot (__create_page_tables) and flush them.
  144. */
  145. if (!pmd_none(old_pmd)) {
  146. flush_tlb_all();
  147. if (pmd_table(old_pmd)) {
  148. phys_addr_t table = __pa(pte_offset_map(&old_pmd, 0));
  149. if (!WARN_ON_ONCE(slab_is_available()))
  150. memblock_free(table, PAGE_SIZE);
  151. }
  152. }
  153. } else {
  154. alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys),
  155. prot, alloc);
  156. }
  157. phys += next - addr;
  158. } while (pmd++, addr = next, addr != end);
  159. }
  160. static inline bool use_1G_block(unsigned long addr, unsigned long next,
  161. unsigned long phys)
  162. {
  163. if (PAGE_SHIFT != 12)
  164. return false;
  165. if (((addr | next | phys) & ~PUD_MASK) != 0)
  166. return false;
  167. return true;
  168. }
  169. static void alloc_init_pud(struct mm_struct *mm, pgd_t *pgd,
  170. unsigned long addr, unsigned long end,
  171. phys_addr_t phys, pgprot_t prot,
  172. void *(*alloc)(unsigned long size))
  173. {
  174. pud_t *pud;
  175. unsigned long next;
  176. if (pgd_none(*pgd)) {
  177. pud = alloc(PTRS_PER_PUD * sizeof(pud_t));
  178. pgd_populate(mm, pgd, pud);
  179. }
  180. BUG_ON(pgd_bad(*pgd));
  181. pud = pud_offset(pgd, addr);
  182. do {
  183. next = pud_addr_end(addr, end);
  184. /*
  185. * For 4K granule only, attempt to put down a 1GB block
  186. */
  187. if (use_1G_block(addr, next, phys)) {
  188. pud_t old_pud = *pud;
  189. set_pud(pud, __pud(phys |
  190. pgprot_val(mk_sect_prot(prot))));
  191. /*
  192. * If we have an old value for a pud, it will
  193. * be pointing to a pmd table that we no longer
  194. * need (from swapper_pg_dir).
  195. *
  196. * Look up the old pmd table and free it.
  197. */
  198. if (!pud_none(old_pud)) {
  199. flush_tlb_all();
  200. if (pud_table(old_pud)) {
  201. phys_addr_t table = __pa(pmd_offset(&old_pud, 0));
  202. if (!WARN_ON_ONCE(slab_is_available()))
  203. memblock_free(table, PAGE_SIZE);
  204. }
  205. }
  206. } else {
  207. alloc_init_pmd(mm, pud, addr, next, phys, prot, alloc);
  208. }
  209. phys += next - addr;
  210. } while (pud++, addr = next, addr != end);
  211. }
  212. /*
  213. * Create the page directory entries and any necessary page tables for the
  214. * mapping specified by 'md'.
  215. */
  216. static void __create_mapping(struct mm_struct *mm, pgd_t *pgd,
  217. phys_addr_t phys, unsigned long virt,
  218. phys_addr_t size, pgprot_t prot,
  219. void *(*alloc)(unsigned long size))
  220. {
  221. unsigned long addr, length, end, next;
  222. addr = virt & PAGE_MASK;
  223. length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
  224. end = addr + length;
  225. do {
  226. next = pgd_addr_end(addr, end);
  227. alloc_init_pud(mm, pgd, addr, next, phys, prot, alloc);
  228. phys += next - addr;
  229. } while (pgd++, addr = next, addr != end);
  230. }
  231. static void *late_alloc(unsigned long size)
  232. {
  233. void *ptr;
  234. BUG_ON(size > PAGE_SIZE);
  235. ptr = (void *)__get_free_page(PGALLOC_GFP);
  236. BUG_ON(!ptr);
  237. return ptr;
  238. }
  239. static void __ref create_mapping(phys_addr_t phys, unsigned long virt,
  240. phys_addr_t size, pgprot_t prot)
  241. {
  242. if (virt < VMALLOC_START) {
  243. pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
  244. &phys, virt);
  245. return;
  246. }
  247. __create_mapping(&init_mm, pgd_offset_k(virt & PAGE_MASK), phys, virt,
  248. size, prot, early_alloc);
  249. }
  250. void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
  251. unsigned long virt, phys_addr_t size,
  252. pgprot_t prot)
  253. {
  254. __create_mapping(mm, pgd_offset(mm, virt), phys, virt, size, prot,
  255. late_alloc);
  256. }
  257. static void create_mapping_late(phys_addr_t phys, unsigned long virt,
  258. phys_addr_t size, pgprot_t prot)
  259. {
  260. if (virt < VMALLOC_START) {
  261. pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
  262. &phys, virt);
  263. return;
  264. }
  265. return __create_mapping(&init_mm, pgd_offset_k(virt & PAGE_MASK),
  266. phys, virt, size, prot, late_alloc);
  267. }
  268. #ifdef CONFIG_DEBUG_RODATA
  269. static void __init __map_memblock(phys_addr_t start, phys_addr_t end)
  270. {
  271. /*
  272. * Set up the executable regions using the existing section mappings
  273. * for now. This will get more fine grained later once all memory
  274. * is mapped
  275. */
  276. unsigned long kernel_x_start = round_down(__pa(_stext), SECTION_SIZE);
  277. unsigned long kernel_x_end = round_up(__pa(__init_end), SECTION_SIZE);
  278. if (end < kernel_x_start) {
  279. create_mapping(start, __phys_to_virt(start),
  280. end - start, PAGE_KERNEL);
  281. } else if (start >= kernel_x_end) {
  282. create_mapping(start, __phys_to_virt(start),
  283. end - start, PAGE_KERNEL);
  284. } else {
  285. if (start < kernel_x_start)
  286. create_mapping(start, __phys_to_virt(start),
  287. kernel_x_start - start,
  288. PAGE_KERNEL);
  289. create_mapping(kernel_x_start,
  290. __phys_to_virt(kernel_x_start),
  291. kernel_x_end - kernel_x_start,
  292. PAGE_KERNEL_EXEC);
  293. if (kernel_x_end < end)
  294. create_mapping(kernel_x_end,
  295. __phys_to_virt(kernel_x_end),
  296. end - kernel_x_end,
  297. PAGE_KERNEL);
  298. }
  299. }
  300. #else
  301. static void __init __map_memblock(phys_addr_t start, phys_addr_t end)
  302. {
  303. create_mapping(start, __phys_to_virt(start), end - start,
  304. PAGE_KERNEL_EXEC);
  305. }
  306. #endif
  307. static void __init map_mem(void)
  308. {
  309. struct memblock_region *reg;
  310. phys_addr_t limit;
  311. /*
  312. * Temporarily limit the memblock range. We need to do this as
  313. * create_mapping requires puds, pmds and ptes to be allocated from
  314. * memory addressable from the initial direct kernel mapping.
  315. *
  316. * The initial direct kernel mapping, located at swapper_pg_dir, gives
  317. * us PUD_SIZE (4K pages) or PMD_SIZE (64K pages) memory starting from
  318. * PHYS_OFFSET (which must be aligned to 2MB as per
  319. * Documentation/arm64/booting.txt).
  320. */
  321. if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
  322. limit = PHYS_OFFSET + PMD_SIZE;
  323. else
  324. limit = PHYS_OFFSET + PUD_SIZE;
  325. memblock_set_current_limit(limit);
  326. /* map all the memory banks */
  327. for_each_memblock(memory, reg) {
  328. phys_addr_t start = reg->base;
  329. phys_addr_t end = start + reg->size;
  330. if (start >= end)
  331. break;
  332. #ifndef CONFIG_ARM64_64K_PAGES
  333. /*
  334. * For the first memory bank align the start address and
  335. * current memblock limit to prevent create_mapping() from
  336. * allocating pte page tables from unmapped memory.
  337. * When 64K pages are enabled, the pte page table for the
  338. * first PGDIR_SIZE is already present in swapper_pg_dir.
  339. */
  340. if (start < limit)
  341. start = ALIGN(start, PMD_SIZE);
  342. if (end < limit) {
  343. limit = end & PMD_MASK;
  344. memblock_set_current_limit(limit);
  345. }
  346. #endif
  347. __map_memblock(start, end);
  348. }
  349. /* Limit no longer required. */
  350. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  351. }
  352. void __init fixup_executable(void)
  353. {
  354. #ifdef CONFIG_DEBUG_RODATA
  355. /* now that we are actually fully mapped, make the start/end more fine grained */
  356. if (!IS_ALIGNED((unsigned long)_stext, SECTION_SIZE)) {
  357. unsigned long aligned_start = round_down(__pa(_stext),
  358. SECTION_SIZE);
  359. create_mapping(aligned_start, __phys_to_virt(aligned_start),
  360. __pa(_stext) - aligned_start,
  361. PAGE_KERNEL);
  362. }
  363. if (!IS_ALIGNED((unsigned long)__init_end, SECTION_SIZE)) {
  364. unsigned long aligned_end = round_up(__pa(__init_end),
  365. SECTION_SIZE);
  366. create_mapping(__pa(__init_end), (unsigned long)__init_end,
  367. aligned_end - __pa(__init_end),
  368. PAGE_KERNEL);
  369. }
  370. #endif
  371. }
  372. #ifdef CONFIG_DEBUG_RODATA
  373. void mark_rodata_ro(void)
  374. {
  375. create_mapping_late(__pa(_stext), (unsigned long)_stext,
  376. (unsigned long)_etext - (unsigned long)_stext,
  377. PAGE_KERNEL_EXEC | PTE_RDONLY);
  378. }
  379. #endif
  380. void fixup_init(void)
  381. {
  382. create_mapping_late(__pa(__init_begin), (unsigned long)__init_begin,
  383. (unsigned long)__init_end - (unsigned long)__init_begin,
  384. PAGE_KERNEL);
  385. }
  386. /*
  387. * paging_init() sets up the page tables, initialises the zone memory
  388. * maps and sets up the zero page.
  389. */
  390. void __init paging_init(void)
  391. {
  392. void *zero_page;
  393. map_mem();
  394. fixup_executable();
  395. /* allocate the zero page. */
  396. zero_page = early_alloc(PAGE_SIZE);
  397. bootmem_init();
  398. empty_zero_page = virt_to_page(zero_page);
  399. /*
  400. * TTBR0 is only used for the identity mapping at this stage. Make it
  401. * point to zero page to avoid speculatively fetching new entries.
  402. */
  403. cpu_set_reserved_ttbr0();
  404. flush_tlb_all();
  405. cpu_set_default_tcr_t0sz();
  406. }
  407. /*
  408. * Enable the identity mapping to allow the MMU disabling.
  409. */
  410. void setup_mm_for_reboot(void)
  411. {
  412. cpu_set_reserved_ttbr0();
  413. flush_tlb_all();
  414. cpu_set_idmap_tcr_t0sz();
  415. cpu_switch_mm(idmap_pg_dir, &init_mm);
  416. }
  417. /*
  418. * Check whether a kernel address is valid (derived from arch/x86/).
  419. */
  420. int kern_addr_valid(unsigned long addr)
  421. {
  422. pgd_t *pgd;
  423. pud_t *pud;
  424. pmd_t *pmd;
  425. pte_t *pte;
  426. if ((((long)addr) >> VA_BITS) != -1UL)
  427. return 0;
  428. pgd = pgd_offset_k(addr);
  429. if (pgd_none(*pgd))
  430. return 0;
  431. pud = pud_offset(pgd, addr);
  432. if (pud_none(*pud))
  433. return 0;
  434. if (pud_sect(*pud))
  435. return pfn_valid(pud_pfn(*pud));
  436. pmd = pmd_offset(pud, addr);
  437. if (pmd_none(*pmd))
  438. return 0;
  439. if (pmd_sect(*pmd))
  440. return pfn_valid(pmd_pfn(*pmd));
  441. pte = pte_offset_kernel(pmd, addr);
  442. if (pte_none(*pte))
  443. return 0;
  444. return pfn_valid(pte_pfn(*pte));
  445. }
  446. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  447. #ifdef CONFIG_ARM64_64K_PAGES
  448. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
  449. {
  450. return vmemmap_populate_basepages(start, end, node);
  451. }
  452. #else /* !CONFIG_ARM64_64K_PAGES */
  453. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
  454. {
  455. unsigned long addr = start;
  456. unsigned long next;
  457. pgd_t *pgd;
  458. pud_t *pud;
  459. pmd_t *pmd;
  460. do {
  461. next = pmd_addr_end(addr, end);
  462. pgd = vmemmap_pgd_populate(addr, node);
  463. if (!pgd)
  464. return -ENOMEM;
  465. pud = vmemmap_pud_populate(pgd, addr, node);
  466. if (!pud)
  467. return -ENOMEM;
  468. pmd = pmd_offset(pud, addr);
  469. if (pmd_none(*pmd)) {
  470. void *p = NULL;
  471. p = vmemmap_alloc_block_buf(PMD_SIZE, node);
  472. if (!p)
  473. return -ENOMEM;
  474. set_pmd(pmd, __pmd(__pa(p) | PROT_SECT_NORMAL));
  475. } else
  476. vmemmap_verify((pte_t *)pmd, node, addr, next);
  477. } while (addr = next, addr != end);
  478. return 0;
  479. }
  480. #endif /* CONFIG_ARM64_64K_PAGES */
  481. void vmemmap_free(unsigned long start, unsigned long end)
  482. {
  483. }
  484. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  485. static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
  486. #if CONFIG_PGTABLE_LEVELS > 2
  487. static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss;
  488. #endif
  489. #if CONFIG_PGTABLE_LEVELS > 3
  490. static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss;
  491. #endif
  492. static inline pud_t * fixmap_pud(unsigned long addr)
  493. {
  494. pgd_t *pgd = pgd_offset_k(addr);
  495. BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd));
  496. return pud_offset(pgd, addr);
  497. }
  498. static inline pmd_t * fixmap_pmd(unsigned long addr)
  499. {
  500. pud_t *pud = fixmap_pud(addr);
  501. BUG_ON(pud_none(*pud) || pud_bad(*pud));
  502. return pmd_offset(pud, addr);
  503. }
  504. static inline pte_t * fixmap_pte(unsigned long addr)
  505. {
  506. pmd_t *pmd = fixmap_pmd(addr);
  507. BUG_ON(pmd_none(*pmd) || pmd_bad(*pmd));
  508. return pte_offset_kernel(pmd, addr);
  509. }
  510. void __init early_fixmap_init(void)
  511. {
  512. pgd_t *pgd;
  513. pud_t *pud;
  514. pmd_t *pmd;
  515. unsigned long addr = FIXADDR_START;
  516. pgd = pgd_offset_k(addr);
  517. pgd_populate(&init_mm, pgd, bm_pud);
  518. pud = pud_offset(pgd, addr);
  519. pud_populate(&init_mm, pud, bm_pmd);
  520. pmd = pmd_offset(pud, addr);
  521. pmd_populate_kernel(&init_mm, pmd, bm_pte);
  522. /*
  523. * The boot-ioremap range spans multiple pmds, for which
  524. * we are not preparted:
  525. */
  526. BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
  527. != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
  528. if ((pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
  529. || pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
  530. WARN_ON(1);
  531. pr_warn("pmd %p != %p, %p\n",
  532. pmd, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
  533. fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
  534. pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
  535. fix_to_virt(FIX_BTMAP_BEGIN));
  536. pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
  537. fix_to_virt(FIX_BTMAP_END));
  538. pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
  539. pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
  540. }
  541. }
  542. void __set_fixmap(enum fixed_addresses idx,
  543. phys_addr_t phys, pgprot_t flags)
  544. {
  545. unsigned long addr = __fix_to_virt(idx);
  546. pte_t *pte;
  547. BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
  548. pte = fixmap_pte(addr);
  549. if (pgprot_val(flags)) {
  550. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
  551. } else {
  552. pte_clear(&init_mm, addr, pte);
  553. flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
  554. }
  555. }
  556. void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
  557. {
  558. const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
  559. pgprot_t prot = PAGE_KERNEL | PTE_RDONLY;
  560. int granularity, size, offset;
  561. void *dt_virt;
  562. /*
  563. * Check whether the physical FDT address is set and meets the minimum
  564. * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
  565. * at least 8 bytes so that we can always access the size field of the
  566. * FDT header after mapping the first chunk, double check here if that
  567. * is indeed the case.
  568. */
  569. BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
  570. if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
  571. return NULL;
  572. /*
  573. * Make sure that the FDT region can be mapped without the need to
  574. * allocate additional translation table pages, so that it is safe
  575. * to call create_mapping() this early.
  576. *
  577. * On 64k pages, the FDT will be mapped using PTEs, so we need to
  578. * be in the same PMD as the rest of the fixmap.
  579. * On 4k pages, we'll use section mappings for the FDT so we only
  580. * have to be in the same PUD.
  581. */
  582. BUILD_BUG_ON(dt_virt_base % SZ_2M);
  583. if (IS_ENABLED(CONFIG_ARM64_64K_PAGES)) {
  584. BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> PMD_SHIFT !=
  585. __fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT);
  586. granularity = PAGE_SIZE;
  587. } else {
  588. BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> PUD_SHIFT !=
  589. __fix_to_virt(FIX_BTMAP_BEGIN) >> PUD_SHIFT);
  590. granularity = PMD_SIZE;
  591. }
  592. offset = dt_phys % granularity;
  593. dt_virt = (void *)dt_virt_base + offset;
  594. /* map the first chunk so we can read the size from the header */
  595. create_mapping(round_down(dt_phys, granularity), dt_virt_base,
  596. granularity, prot);
  597. if (fdt_check_header(dt_virt) != 0)
  598. return NULL;
  599. size = fdt_totalsize(dt_virt);
  600. if (size > MAX_FDT_SIZE)
  601. return NULL;
  602. if (offset + size > granularity)
  603. create_mapping(round_down(dt_phys, granularity), dt_virt_base,
  604. round_up(offset + size, granularity), prot);
  605. memblock_reserve(dt_phys, size);
  606. return dt_virt;
  607. }