^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Re-map IO memory to kernel address space so that we can access it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * This is needed for high PCI addresses that aren't mapped in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * 640k-1MB IO memory area on PC's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * (C) Copyright 1995 1996 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "pgalloc-track.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static int __read_mostly ioremap_p4d_capable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int __read_mostly ioremap_pud_capable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static int __read_mostly ioremap_pmd_capable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int __read_mostly ioremap_huge_disabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int __init set_nohugeiomap(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) ioremap_huge_disabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) early_param("nohugeiomap", set_nohugeiomap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) void __init ioremap_huge_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (!ioremap_huge_disabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (arch_ioremap_p4d_supported())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ioremap_p4d_capable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (arch_ioremap_pud_supported())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) ioremap_pud_capable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (arch_ioremap_pmd_supported())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) ioremap_pmd_capable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static inline int ioremap_p4d_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return ioremap_p4d_capable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static inline int ioremap_pud_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return ioremap_pud_capable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static inline int ioremap_pmd_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return ioremap_pmd_capable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static inline int ioremap_p4d_enabled(void) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static inline int ioremap_pud_enabled(void) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static inline int ioremap_pmd_enabled(void) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int ioremap_pte_range(pmd_t *pmd, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned long end, phys_addr_t phys_addr, pgprot_t prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) pgtbl_mod_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) u64 pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) pfn = phys_addr >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) pte = pte_alloc_kernel_track(pmd, addr, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) BUG_ON(!pte_none(*pte));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pfn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) } while (pte++, addr += PAGE_SIZE, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *mask |= PGTBL_PTE_MODIFIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int ioremap_try_huge_pmd(pmd_t *pmd, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) unsigned long end, phys_addr_t phys_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!ioremap_pmd_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if ((end - addr) != PMD_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (!IS_ALIGNED(addr, PMD_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!IS_ALIGNED(phys_addr, PMD_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (pmd_present(*pmd) && !pmd_free_pte_page(pmd, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return pmd_set_huge(pmd, phys_addr, prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static inline int ioremap_pmd_range(pud_t *pud, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) unsigned long end, phys_addr_t phys_addr, pgprot_t prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pgtbl_mod_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) pmd = pmd_alloc_track(&init_mm, pud, addr, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (!pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) next = pmd_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (ioremap_try_huge_pmd(pmd, addr, next, phys_addr, prot)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) *mask |= PGTBL_PMD_MODIFIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (ioremap_pte_range(pmd, addr, next, phys_addr, prot, mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) } while (pmd++, phys_addr += (next - addr), addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static int ioremap_try_huge_pud(pud_t *pud, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned long end, phys_addr_t phys_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!ioremap_pud_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if ((end - addr) != PUD_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!IS_ALIGNED(addr, PUD_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!IS_ALIGNED(phys_addr, PUD_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (pud_present(*pud) && !pud_free_pmd_page(pud, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return pud_set_huge(pud, phys_addr, prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static inline int ioremap_pud_range(p4d_t *p4d, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned long end, phys_addr_t phys_addr, pgprot_t prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) pgtbl_mod_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) pud = pud_alloc_track(&init_mm, p4d, addr, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!pud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) next = pud_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (ioremap_try_huge_pud(pud, addr, next, phys_addr, prot)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) *mask |= PGTBL_PUD_MODIFIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (ioremap_pmd_range(pud, addr, next, phys_addr, prot, mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) } while (pud++, phys_addr += (next - addr), addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static int ioremap_try_huge_p4d(p4d_t *p4d, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) unsigned long end, phys_addr_t phys_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!ioremap_p4d_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if ((end - addr) != P4D_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (!IS_ALIGNED(addr, P4D_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (!IS_ALIGNED(phys_addr, P4D_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (p4d_present(*p4d) && !p4d_free_pud_page(p4d, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return p4d_set_huge(p4d, phys_addr, prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static inline int ioremap_p4d_range(pgd_t *pgd, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) unsigned long end, phys_addr_t phys_addr, pgprot_t prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) pgtbl_mod_mask *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) p4d_t *p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) p4d = p4d_alloc_track(&init_mm, pgd, addr, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (!p4d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) next = p4d_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (ioremap_try_huge_p4d(p4d, addr, next, phys_addr, prot)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) *mask |= PGTBL_P4D_MODIFIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (ioremap_pud_range(p4d, addr, next, phys_addr, prot, mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) } while (p4d++, phys_addr += (next - addr), addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int ioremap_page_range(unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) pgd_t *pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) pgtbl_mod_mask mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) BUG_ON(addr >= end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) start = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) pgd = pgd_offset_k(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) next = pgd_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) err = ioremap_p4d_range(pgd, addr, next, phys_addr, prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) } while (pgd++, phys_addr += (next - addr), addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) flush_cache_vmap(start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) arch_sync_kernel_mappings(start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) #ifdef CONFIG_GENERIC_IOREMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) void __iomem *ioremap_prot(phys_addr_t addr, size_t size, unsigned long prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) unsigned long offset, vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) phys_addr_t last_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct vm_struct *area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /* Disallow wrap-around or zero size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) last_addr = addr + size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (!size || last_addr < addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /* Page-align mappings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) offset = addr & (~PAGE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) addr -= offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) size = PAGE_ALIGN(size + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) area = get_vm_area_caller(size, VM_IOREMAP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) __builtin_return_address(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (!area)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) vaddr = (unsigned long)area->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (ioremap_page_range(vaddr, vaddr + size, addr, __pgprot(prot))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) free_vm_area(area);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return (void __iomem *)(vaddr + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) EXPORT_SYMBOL(ioremap_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) void iounmap(volatile void __iomem *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) vunmap((void *)((unsigned long)addr & PAGE_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) EXPORT_SYMBOL(iounmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) #endif /* CONFIG_GENERIC_IOREMAP */