Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 
0003 #include <linux/io.h>
0004 #include <linux/slab.h>
0005 #include <linux/vmalloc.h>
0006 
0007 #include <mm/mmu_decl.h>
0008 
0009 void __iomem *ioremap_wt(phys_addr_t addr, unsigned long size)
0010 {
0011     pgprot_t prot = pgprot_cached_wthru(PAGE_KERNEL);
0012 
0013     return __ioremap_caller(addr, size, prot, __builtin_return_address(0));
0014 }
0015 EXPORT_SYMBOL(ioremap_wt);
0016 
0017 void __iomem *
0018 __ioremap_caller(phys_addr_t addr, unsigned long size, pgprot_t prot, void *caller)
0019 {
0020     unsigned long v;
0021     phys_addr_t p, offset;
0022     int err;
0023 
0024     /*
0025      * Choose an address to map it to.
0026      * Once the vmalloc system is running, we use it.
0027      * Before then, we use space going down from IOREMAP_TOP
0028      * (ioremap_bot records where we're up to).
0029      */
0030     p = addr & PAGE_MASK;
0031     offset = addr & ~PAGE_MASK;
0032     size = PAGE_ALIGN(addr + size) - p;
0033 
0034     /*
0035      * If the address lies within the first 16 MB, assume it's in ISA
0036      * memory space
0037      */
0038     if (p < 16 * 1024 * 1024)
0039         p += _ISA_MEM_BASE;
0040 
0041 #ifndef CONFIG_CRASH_DUMP
0042     /*
0043      * Don't allow anybody to remap normal RAM that we're using.
0044      * mem_init() sets high_memory so only do the check after that.
0045      */
0046     if (slab_is_available() && p <= virt_to_phys(high_memory - 1) &&
0047         page_is_ram(__phys_to_pfn(p))) {
0048         pr_warn("%s(): phys addr 0x%llx is RAM lr %ps\n", __func__,
0049             (unsigned long long)p, __builtin_return_address(0));
0050         return NULL;
0051     }
0052 #endif
0053 
0054     if (size == 0)
0055         return NULL;
0056 
0057     /*
0058      * Is it already mapped?  Perhaps overlapped by a previous
0059      * mapping.
0060      */
0061     v = p_block_mapped(p);
0062     if (v)
0063         return (void __iomem *)v + offset;
0064 
0065     if (slab_is_available())
0066         return do_ioremap(p, offset, size, prot, caller);
0067 
0068     /*
0069      * Should check if it is a candidate for a BAT mapping
0070      */
0071     pr_warn("ioremap() called early from %pS. Use early_ioremap() instead\n", caller);
0072 
0073     err = early_ioremap_range(ioremap_bot - size - PAGE_SIZE, p, size, prot);
0074     if (err)
0075         return NULL;
0076     ioremap_bot -= size + PAGE_SIZE;
0077 
0078     return (void __iomem *)ioremap_bot + offset;
0079 }
0080 
0081 void iounmap(volatile void __iomem *addr)
0082 {
0083     /*
0084      * If mapped by BATs then there is nothing to do.
0085      * Calling vfree() generates a benign warning.
0086      */
0087     if (v_block_mapped((unsigned long)addr))
0088         return;
0089 
0090     if (addr > high_memory && (unsigned long)addr < ioremap_bot)
0091         vunmap((void *)(PAGE_MASK & (unsigned long)addr));
0092 }
0093 EXPORT_SYMBOL(iounmap);