1f381d571SChristophe Leroy // SPDX-License-Identifier: GPL-2.0-or-later
2f381d571SChristophe Leroy
3f381d571SChristophe Leroy #include <linux/io.h>
4f381d571SChristophe Leroy #include <linux/slab.h>
5f381d571SChristophe Leroy #include <linux/vmalloc.h>
6f381d571SChristophe Leroy
7f381d571SChristophe Leroy #include <mm/mmu_decl.h>
8f381d571SChristophe Leroy
ioremap_wt(phys_addr_t addr,unsigned long size)9f381d571SChristophe Leroy void __iomem *ioremap_wt(phys_addr_t addr, unsigned long size)
10f381d571SChristophe Leroy {
11f381d571SChristophe Leroy pgprot_t prot = pgprot_cached_wthru(PAGE_KERNEL);
12f381d571SChristophe Leroy
13f381d571SChristophe Leroy return __ioremap_caller(addr, size, prot, __builtin_return_address(0));
14f381d571SChristophe Leroy }
15f381d571SChristophe Leroy EXPORT_SYMBOL(ioremap_wt);
16f381d571SChristophe Leroy
17f381d571SChristophe Leroy void __iomem *
__ioremap_caller(phys_addr_t addr,unsigned long size,pgprot_t prot,void * caller)18f381d571SChristophe Leroy __ioremap_caller(phys_addr_t addr, unsigned long size, pgprot_t prot, void *caller)
19f381d571SChristophe Leroy {
20191e4206SChristophe Leroy unsigned long v;
214a45b746SChristophe Leroy phys_addr_t p, offset;
22f381d571SChristophe Leroy int err;
23f381d571SChristophe Leroy
24f381d571SChristophe Leroy /*
25*8d05554dSChristophe Leroy * If the address lies within the first 16 MB, assume it's in ISA
26*8d05554dSChristophe Leroy * memory space
27*8d05554dSChristophe Leroy */
28*8d05554dSChristophe Leroy if (addr < SZ_16M)
29*8d05554dSChristophe Leroy addr += _ISA_MEM_BASE;
30*8d05554dSChristophe Leroy
31*8d05554dSChristophe Leroy /*
32f381d571SChristophe Leroy * Choose an address to map it to.
33f381d571SChristophe Leroy * Once the vmalloc system is running, we use it.
34f381d571SChristophe Leroy * Before then, we use space going down from IOREMAP_TOP
35f381d571SChristophe Leroy * (ioremap_bot records where we're up to).
36f381d571SChristophe Leroy */
37f381d571SChristophe Leroy p = addr & PAGE_MASK;
384a45b746SChristophe Leroy offset = addr & ~PAGE_MASK;
39f381d571SChristophe Leroy size = PAGE_ALIGN(addr + size) - p;
40f381d571SChristophe Leroy
41f381d571SChristophe Leroy #ifndef CONFIG_CRASH_DUMP
42f381d571SChristophe Leroy /*
43f381d571SChristophe Leroy * Don't allow anybody to remap normal RAM that we're using.
44f381d571SChristophe Leroy * mem_init() sets high_memory so only do the check after that.
45f381d571SChristophe Leroy */
46f381d571SChristophe Leroy if (slab_is_available() && p <= virt_to_phys(high_memory - 1) &&
47f381d571SChristophe Leroy page_is_ram(__phys_to_pfn(p))) {
48f381d571SChristophe Leroy pr_warn("%s(): phys addr 0x%llx is RAM lr %ps\n", __func__,
49f381d571SChristophe Leroy (unsigned long long)p, __builtin_return_address(0));
50f381d571SChristophe Leroy return NULL;
51f381d571SChristophe Leroy }
52f381d571SChristophe Leroy #endif
53f381d571SChristophe Leroy
54f381d571SChristophe Leroy if (size == 0)
55f381d571SChristophe Leroy return NULL;
56f381d571SChristophe Leroy
57f381d571SChristophe Leroy /*
58f381d571SChristophe Leroy * Is it already mapped? Perhaps overlapped by a previous
59f381d571SChristophe Leroy * mapping.
60f381d571SChristophe Leroy */
61f381d571SChristophe Leroy v = p_block_mapped(p);
62f381d571SChristophe Leroy if (v)
63163918fcSChristophe Leroy return (void __iomem *)v + offset;
64f381d571SChristophe Leroy
65163918fcSChristophe Leroy if (slab_is_available())
66*8d05554dSChristophe Leroy return generic_ioremap_prot(addr, size, prot);
67f381d571SChristophe Leroy
68f381d571SChristophe Leroy /*
69f381d571SChristophe Leroy * Should check if it is a candidate for a BAT mapping
70f381d571SChristophe Leroy */
71d538aadcSChristophe Leroy pr_warn("ioremap() called early from %pS. Use early_ioremap() instead\n", caller);
72f381d571SChristophe Leroy
7357307f1bSChristophe Leroy err = early_ioremap_range(ioremap_bot - size - PAGE_SIZE, p, size, prot);
744a45b746SChristophe Leroy if (err)
75f381d571SChristophe Leroy return NULL;
7657307f1bSChristophe Leroy ioremap_bot -= size + PAGE_SIZE;
77f381d571SChristophe Leroy
78163918fcSChristophe Leroy return (void __iomem *)ioremap_bot + offset;
79f381d571SChristophe Leroy }
80f381d571SChristophe Leroy
iounmap(volatile void __iomem * addr)81f381d571SChristophe Leroy void iounmap(volatile void __iomem *addr)
82f381d571SChristophe Leroy {
83f381d571SChristophe Leroy /*
84f381d571SChristophe Leroy * If mapped by BATs then there is nothing to do.
85f381d571SChristophe Leroy * Calling vfree() generates a benign warning.
86f381d571SChristophe Leroy */
87f381d571SChristophe Leroy if (v_block_mapped((unsigned long)addr))
88f381d571SChristophe Leroy return;
89f381d571SChristophe Leroy
90*8d05554dSChristophe Leroy generic_iounmap(addr);
91f381d571SChristophe Leroy }
92f381d571SChristophe Leroy EXPORT_SYMBOL(iounmap);
93