fixmap.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Fixmap manipulation code
  4. */
  5. #include <linux/bug.h>
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/libfdt.h>
  9. #include <linux/memory.h>
  10. #include <linux/mm.h>
  11. #include <linux/sizes.h>
  12. #include <asm/fixmap.h>
  13. #include <asm/kernel-pgtable.h>
  14. #include <asm/pgalloc.h>
  15. #include <asm/tlbflush.h>
  16. /* ensure that the fixmap region does not grow down into the PCI I/O region */
  17. static_assert(FIXADDR_TOT_START > PCI_IO_END);
  18. #define NR_BM_PTE_TABLES \
  19. SPAN_NR_ENTRIES(FIXADDR_TOT_START, FIXADDR_TOP, PMD_SHIFT)
  20. #define NR_BM_PMD_TABLES \
  21. SPAN_NR_ENTRIES(FIXADDR_TOT_START, FIXADDR_TOP, PUD_SHIFT)
  22. static_assert(NR_BM_PMD_TABLES == 1);
  23. #define __BM_TABLE_IDX(addr, shift) \
  24. (((addr) >> (shift)) - (FIXADDR_TOT_START >> (shift)))
  25. #define BM_PTE_TABLE_IDX(addr) __BM_TABLE_IDX(addr, PMD_SHIFT)
  26. static pte_t bm_pte[NR_BM_PTE_TABLES][PTRS_PER_PTE] __page_aligned_bss;
  27. static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
  28. static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
  29. static inline pte_t *fixmap_pte(unsigned long addr)
  30. {
  31. return &bm_pte[BM_PTE_TABLE_IDX(addr)][pte_index(addr)];
  32. }
  33. static void __init early_fixmap_init_pte(pmd_t *pmdp, unsigned long addr)
  34. {
  35. pmd_t pmd = READ_ONCE(*pmdp);
  36. pte_t *ptep;
  37. if (pmd_none(pmd)) {
  38. ptep = bm_pte[BM_PTE_TABLE_IDX(addr)];
  39. __pmd_populate(pmdp, __pa_symbol(ptep),
  40. PMD_TYPE_TABLE | PMD_TABLE_AF);
  41. }
  42. }
  43. static void __init early_fixmap_init_pmd(pud_t *pudp, unsigned long addr,
  44. unsigned long end)
  45. {
  46. unsigned long next;
  47. pud_t pud = READ_ONCE(*pudp);
  48. pmd_t *pmdp;
  49. if (pud_none(pud))
  50. __pud_populate(pudp, __pa_symbol(bm_pmd),
  51. PUD_TYPE_TABLE | PUD_TABLE_AF);
  52. pmdp = pmd_offset_kimg(pudp, addr);
  53. do {
  54. next = pmd_addr_end(addr, end);
  55. early_fixmap_init_pte(pmdp, addr);
  56. } while (pmdp++, addr = next, addr != end);
  57. }
  58. static void __init early_fixmap_init_pud(p4d_t *p4dp, unsigned long addr,
  59. unsigned long end)
  60. {
  61. p4d_t p4d = READ_ONCE(*p4dp);
  62. pud_t *pudp;
  63. if (CONFIG_PGTABLE_LEVELS > 3 && !p4d_none(p4d) &&
  64. p4d_page_paddr(p4d) != __pa_symbol(bm_pud)) {
  65. /*
  66. * We only end up here if the kernel mapping and the fixmap
  67. * share the top level pgd entry, which should only happen on
  68. * 16k/4 levels configurations.
  69. */
  70. BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
  71. }
  72. if (p4d_none(p4d))
  73. __p4d_populate(p4dp, __pa_symbol(bm_pud),
  74. P4D_TYPE_TABLE | P4D_TABLE_AF);
  75. pudp = pud_offset_kimg(p4dp, addr);
  76. early_fixmap_init_pmd(pudp, addr, end);
  77. }
  78. /*
  79. * The p*d_populate functions call virt_to_phys implicitly so they can't be used
  80. * directly on kernel symbols (bm_p*d). This function is called too early to use
  81. * lm_alias so __p*d_populate functions must be used to populate with the
  82. * physical address from __pa_symbol.
  83. */
  84. void __init early_fixmap_init(void)
  85. {
  86. unsigned long addr = FIXADDR_TOT_START;
  87. unsigned long end = FIXADDR_TOP;
  88. pgd_t *pgdp = pgd_offset_k(addr);
  89. p4d_t *p4dp = p4d_offset_kimg(pgdp, addr);
  90. early_fixmap_init_pud(p4dp, addr, end);
  91. }
  92. /*
  93. * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
  94. * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
  95. */
  96. void __set_fixmap(enum fixed_addresses idx,
  97. phys_addr_t phys, pgprot_t flags)
  98. {
  99. unsigned long addr = __fix_to_virt(idx);
  100. pte_t *ptep;
  101. BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
  102. ptep = fixmap_pte(addr);
  103. if (pgprot_val(flags)) {
  104. __set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
  105. } else {
  106. __pte_clear(&init_mm, addr, ptep);
  107. flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
  108. }
  109. }
  110. void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
  111. {
  112. const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
  113. phys_addr_t dt_phys_base;
  114. int offset;
  115. void *dt_virt;
  116. /*
  117. * Check whether the physical FDT address is set and meets the minimum
  118. * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
  119. * at least 8 bytes so that we can always access the magic and size
  120. * fields of the FDT header after mapping the first chunk, double check
  121. * here if that is indeed the case.
  122. */
  123. BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
  124. if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
  125. return NULL;
  126. dt_phys_base = round_down(dt_phys, PAGE_SIZE);
  127. offset = dt_phys % PAGE_SIZE;
  128. dt_virt = (void *)dt_virt_base + offset;
  129. /* map the first chunk so we can read the size from the header */
  130. create_mapping_noalloc(dt_phys_base, dt_virt_base, PAGE_SIZE, prot);
  131. if (fdt_magic(dt_virt) != FDT_MAGIC)
  132. return NULL;
  133. *size = fdt_totalsize(dt_virt);
  134. if (*size > MAX_FDT_SIZE)
  135. return NULL;
  136. if (offset + *size > PAGE_SIZE) {
  137. create_mapping_noalloc(dt_phys_base, dt_virt_base,
  138. offset + *size, prot);
  139. }
  140. return dt_virt;
  141. }