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 void __iomem *__ioremap_caller(phys_addr_t addr, unsigned long size,
0008                    pgprot_t prot, void *caller)
0009 {
0010     phys_addr_t paligned, offset;
0011     void __iomem *ret;
0012     int err;
0013 
0014     /* We don't support the 4K PFN hack with ioremap */
0015     if (pgprot_val(prot) & H_PAGE_4K_PFN)
0016         return NULL;
0017 
0018     /*
0019      * Choose an address to map it to. Once the vmalloc system is running,
0020      * we use it. Before that, we map using addresses going up from
0021      * ioremap_bot.  vmalloc will use the addresses from IOREMAP_BASE
0022      * through ioremap_bot.
0023      */
0024     paligned = addr & PAGE_MASK;
0025     offset = addr & ~PAGE_MASK;
0026     size = PAGE_ALIGN(addr + size) - paligned;
0027 
0028     if (size == 0 || paligned == 0)
0029         return NULL;
0030 
0031     if (slab_is_available())
0032         return do_ioremap(paligned, offset, size, prot, caller);
0033 
0034     pr_warn("ioremap() called early from %pS. Use early_ioremap() instead\n", caller);
0035 
0036     err = early_ioremap_range(ioremap_bot, paligned, size, prot);
0037     if (err)
0038         return NULL;
0039 
0040     ret = (void __iomem *)ioremap_bot + offset;
0041     ioremap_bot += size + PAGE_SIZE;
0042 
0043     return ret;
0044 }
0045 
0046 /*
0047  * Unmap an IO region and remove it from vmalloc'd list.
0048  * Access to IO memory should be serialized by driver.
0049  */
0050 void iounmap(volatile void __iomem *token)
0051 {
0052     void *addr;
0053 
0054     if (!slab_is_available())
0055         return;
0056 
0057     addr = (void *)((unsigned long __force)PCI_FIX_ADDR(token) & PAGE_MASK);
0058 
0059     if ((unsigned long)addr < ioremap_bot) {
0060         pr_warn("Attempt to iounmap early bolted mapping at 0x%p\n", addr);
0061         return;
0062     }
0063     vunmap(addr);
0064 }
0065 EXPORT_SYMBOL(iounmap);