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 /** 8f381d571SChristophe Leroy * Low level function to establish the page tables for an IO mapping 9f381d571SChristophe Leroy */ 10f381d571SChristophe Leroy void __iomem *__ioremap_at(phys_addr_t pa, void *ea, unsigned long size, pgprot_t prot) 11f381d571SChristophe Leroy { 12*163918fcSChristophe Leroy int ret; 13*163918fcSChristophe Leroy unsigned long va = (unsigned long)ea; 14*163918fcSChristophe Leroy 15f381d571SChristophe Leroy /* We don't support the 4K PFN hack with ioremap */ 16f381d571SChristophe Leroy if (pgprot_val(prot) & H_PAGE_4K_PFN) 17f381d571SChristophe Leroy return NULL; 18f381d571SChristophe Leroy 19f381d571SChristophe Leroy if ((ea + size) >= (void *)IOREMAP_END) { 20f381d571SChristophe Leroy pr_warn("Outside the supported range\n"); 21f381d571SChristophe Leroy return NULL; 22f381d571SChristophe Leroy } 23f381d571SChristophe Leroy 24f381d571SChristophe Leroy WARN_ON(pa & ~PAGE_MASK); 25f381d571SChristophe Leroy WARN_ON(((unsigned long)ea) & ~PAGE_MASK); 26f381d571SChristophe Leroy WARN_ON(size & ~PAGE_MASK); 27f381d571SChristophe Leroy 28*163918fcSChristophe Leroy if (slab_is_available()) { 29*163918fcSChristophe Leroy ret = ioremap_page_range(va, va + size, pa, prot); 30*163918fcSChristophe Leroy if (ret) 31*163918fcSChristophe Leroy unmap_kernel_range(va, size); 32*163918fcSChristophe Leroy } else { 33*163918fcSChristophe Leroy ret = early_ioremap_range(va, pa, size, prot); 34*163918fcSChristophe Leroy } 35*163918fcSChristophe Leroy 36*163918fcSChristophe Leroy if (ret) 37f381d571SChristophe Leroy return NULL; 38f381d571SChristophe Leroy 39f381d571SChristophe Leroy return (void __iomem *)ea; 40f381d571SChristophe Leroy } 41f381d571SChristophe Leroy EXPORT_SYMBOL(__ioremap_at); 42f381d571SChristophe Leroy 43f381d571SChristophe Leroy /** 44f381d571SChristophe Leroy * Low level function to tear down the page tables for an IO mapping. This is 45f381d571SChristophe Leroy * used for mappings that are manipulated manually, like partial unmapping of 46f381d571SChristophe Leroy * PCI IOs or ISA space. 47f381d571SChristophe Leroy */ 48f381d571SChristophe Leroy void __iounmap_at(void *ea, unsigned long size) 49f381d571SChristophe Leroy { 50f381d571SChristophe Leroy WARN_ON(((unsigned long)ea) & ~PAGE_MASK); 51f381d571SChristophe Leroy WARN_ON(size & ~PAGE_MASK); 52f381d571SChristophe Leroy 53f381d571SChristophe Leroy unmap_kernel_range((unsigned long)ea, size); 54f381d571SChristophe Leroy } 55f381d571SChristophe Leroy EXPORT_SYMBOL(__iounmap_at); 56f381d571SChristophe Leroy 57f381d571SChristophe Leroy void __iomem *__ioremap_caller(phys_addr_t addr, unsigned long size, 58f381d571SChristophe Leroy pgprot_t prot, void *caller) 59f381d571SChristophe Leroy { 604a45b746SChristophe Leroy phys_addr_t paligned, offset; 61f381d571SChristophe Leroy void __iomem *ret; 62*163918fcSChristophe Leroy int err; 63f381d571SChristophe Leroy 644a45b746SChristophe Leroy /* We don't support the 4K PFN hack with ioremap */ 654a45b746SChristophe Leroy if (pgprot_val(prot) & H_PAGE_4K_PFN) 664a45b746SChristophe Leroy return NULL; 674a45b746SChristophe Leroy 68f381d571SChristophe Leroy /* 69f381d571SChristophe Leroy * Choose an address to map it to. Once the vmalloc system is running, 70f381d571SChristophe Leroy * we use it. Before that, we map using addresses going up from 71f381d571SChristophe Leroy * ioremap_bot. vmalloc will use the addresses from IOREMAP_BASE 72f381d571SChristophe Leroy * through ioremap_bot. 73f381d571SChristophe Leroy */ 74f381d571SChristophe Leroy paligned = addr & PAGE_MASK; 754a45b746SChristophe Leroy offset = addr & ~PAGE_MASK; 76f381d571SChristophe Leroy size = PAGE_ALIGN(addr + size) - paligned; 77f381d571SChristophe Leroy 78f381d571SChristophe Leroy if (size == 0 || paligned == 0) 79f381d571SChristophe Leroy return NULL; 80f381d571SChristophe Leroy 81*163918fcSChristophe Leroy if (slab_is_available()) 824a45b746SChristophe Leroy return do_ioremap(paligned, offset, size, prot, caller); 83f381d571SChristophe Leroy 84*163918fcSChristophe Leroy err = early_ioremap_range(ioremap_bot, paligned, size, prot); 85*163918fcSChristophe Leroy if (err) 86*163918fcSChristophe Leroy return NULL; 87*163918fcSChristophe Leroy 88*163918fcSChristophe Leroy ret = (void __iomem *)ioremap_bot + offset; 89*163918fcSChristophe Leroy ioremap_bot += size; 90*163918fcSChristophe Leroy 91f381d571SChristophe Leroy return ret; 92f381d571SChristophe Leroy } 93f381d571SChristophe Leroy 94f381d571SChristophe Leroy /* 95f381d571SChristophe Leroy * Unmap an IO region and remove it from vmalloc'd list. 96f381d571SChristophe Leroy * Access to IO memory should be serialized by driver. 97f381d571SChristophe Leroy */ 98f381d571SChristophe Leroy void iounmap(volatile void __iomem *token) 99f381d571SChristophe Leroy { 100f381d571SChristophe Leroy void *addr; 101f381d571SChristophe Leroy 102f381d571SChristophe Leroy if (!slab_is_available()) 103f381d571SChristophe Leroy return; 104f381d571SChristophe Leroy 105f381d571SChristophe Leroy addr = (void *)((unsigned long __force)PCI_FIX_ADDR(token) & PAGE_MASK); 106f381d571SChristophe Leroy 107f381d571SChristophe Leroy if ((unsigned long)addr < ioremap_bot) { 108f381d571SChristophe Leroy pr_warn("Attempt to iounmap early bolted mapping at 0x%p\n", addr); 109f381d571SChristophe Leroy return; 110f381d571SChristophe Leroy } 111f381d571SChristophe Leroy vunmap(addr); 112f381d571SChristophe Leroy } 113f381d571SChristophe Leroy EXPORT_SYMBOL(iounmap); 114