1*f381d571SChristophe Leroy // SPDX-License-Identifier: GPL-2.0-or-later 2*f381d571SChristophe Leroy 3*f381d571SChristophe Leroy #include <linux/io.h> 4*f381d571SChristophe Leroy #include <linux/slab.h> 5*f381d571SChristophe Leroy #include <linux/vmalloc.h> 6*f381d571SChristophe Leroy 7*f381d571SChristophe Leroy int __weak ioremap_range(unsigned long ea, phys_addr_t pa, unsigned long size, 8*f381d571SChristophe Leroy pgprot_t prot, int nid) 9*f381d571SChristophe Leroy { 10*f381d571SChristophe Leroy unsigned long i; 11*f381d571SChristophe Leroy 12*f381d571SChristophe Leroy for (i = 0; i < size; i += PAGE_SIZE) { 13*f381d571SChristophe Leroy int err = map_kernel_page(ea + i, pa + i, prot); 14*f381d571SChristophe Leroy if (err) { 15*f381d571SChristophe Leroy if (slab_is_available()) 16*f381d571SChristophe Leroy unmap_kernel_range(ea, size); 17*f381d571SChristophe Leroy else 18*f381d571SChristophe Leroy WARN_ON_ONCE(1); /* Should clean up */ 19*f381d571SChristophe Leroy return err; 20*f381d571SChristophe Leroy } 21*f381d571SChristophe Leroy } 22*f381d571SChristophe Leroy 23*f381d571SChristophe Leroy return 0; 24*f381d571SChristophe Leroy } 25*f381d571SChristophe Leroy 26*f381d571SChristophe Leroy /** 27*f381d571SChristophe Leroy * Low level function to establish the page tables for an IO mapping 28*f381d571SChristophe Leroy */ 29*f381d571SChristophe Leroy void __iomem *__ioremap_at(phys_addr_t pa, void *ea, unsigned long size, pgprot_t prot) 30*f381d571SChristophe Leroy { 31*f381d571SChristophe Leroy /* We don't support the 4K PFN hack with ioremap */ 32*f381d571SChristophe Leroy if (pgprot_val(prot) & H_PAGE_4K_PFN) 33*f381d571SChristophe Leroy return NULL; 34*f381d571SChristophe Leroy 35*f381d571SChristophe Leroy if ((ea + size) >= (void *)IOREMAP_END) { 36*f381d571SChristophe Leroy pr_warn("Outside the supported range\n"); 37*f381d571SChristophe Leroy return NULL; 38*f381d571SChristophe Leroy } 39*f381d571SChristophe Leroy 40*f381d571SChristophe Leroy WARN_ON(pa & ~PAGE_MASK); 41*f381d571SChristophe Leroy WARN_ON(((unsigned long)ea) & ~PAGE_MASK); 42*f381d571SChristophe Leroy WARN_ON(size & ~PAGE_MASK); 43*f381d571SChristophe Leroy 44*f381d571SChristophe Leroy if (ioremap_range((unsigned long)ea, pa, size, prot, NUMA_NO_NODE)) 45*f381d571SChristophe Leroy return NULL; 46*f381d571SChristophe Leroy 47*f381d571SChristophe Leroy return (void __iomem *)ea; 48*f381d571SChristophe Leroy } 49*f381d571SChristophe Leroy EXPORT_SYMBOL(__ioremap_at); 50*f381d571SChristophe Leroy 51*f381d571SChristophe Leroy /** 52*f381d571SChristophe Leroy * Low level function to tear down the page tables for an IO mapping. This is 53*f381d571SChristophe Leroy * used for mappings that are manipulated manually, like partial unmapping of 54*f381d571SChristophe Leroy * PCI IOs or ISA space. 55*f381d571SChristophe Leroy */ 56*f381d571SChristophe Leroy void __iounmap_at(void *ea, unsigned long size) 57*f381d571SChristophe Leroy { 58*f381d571SChristophe Leroy WARN_ON(((unsigned long)ea) & ~PAGE_MASK); 59*f381d571SChristophe Leroy WARN_ON(size & ~PAGE_MASK); 60*f381d571SChristophe Leroy 61*f381d571SChristophe Leroy unmap_kernel_range((unsigned long)ea, size); 62*f381d571SChristophe Leroy } 63*f381d571SChristophe Leroy EXPORT_SYMBOL(__iounmap_at); 64*f381d571SChristophe Leroy 65*f381d571SChristophe Leroy void __iomem *__ioremap_caller(phys_addr_t addr, unsigned long size, 66*f381d571SChristophe Leroy pgprot_t prot, void *caller) 67*f381d571SChristophe Leroy { 68*f381d571SChristophe Leroy phys_addr_t paligned; 69*f381d571SChristophe Leroy void __iomem *ret; 70*f381d571SChristophe Leroy 71*f381d571SChristophe Leroy /* 72*f381d571SChristophe Leroy * Choose an address to map it to. Once the vmalloc system is running, 73*f381d571SChristophe Leroy * we use it. Before that, we map using addresses going up from 74*f381d571SChristophe Leroy * ioremap_bot. vmalloc will use the addresses from IOREMAP_BASE 75*f381d571SChristophe Leroy * through ioremap_bot. 76*f381d571SChristophe Leroy */ 77*f381d571SChristophe Leroy paligned = addr & PAGE_MASK; 78*f381d571SChristophe Leroy size = PAGE_ALIGN(addr + size) - paligned; 79*f381d571SChristophe Leroy 80*f381d571SChristophe Leroy if (size == 0 || paligned == 0) 81*f381d571SChristophe Leroy return NULL; 82*f381d571SChristophe Leroy 83*f381d571SChristophe Leroy if (slab_is_available()) { 84*f381d571SChristophe Leroy struct vm_struct *area; 85*f381d571SChristophe Leroy 86*f381d571SChristophe Leroy area = __get_vm_area_caller(size, VM_IOREMAP, ioremap_bot, 87*f381d571SChristophe Leroy IOREMAP_END, caller); 88*f381d571SChristophe Leroy if (area == NULL) 89*f381d571SChristophe Leroy return NULL; 90*f381d571SChristophe Leroy 91*f381d571SChristophe Leroy area->phys_addr = paligned; 92*f381d571SChristophe Leroy ret = __ioremap_at(paligned, area->addr, size, prot); 93*f381d571SChristophe Leroy } else { 94*f381d571SChristophe Leroy ret = __ioremap_at(paligned, (void *)ioremap_bot, size, prot); 95*f381d571SChristophe Leroy if (ret) 96*f381d571SChristophe Leroy ioremap_bot += size; 97*f381d571SChristophe Leroy } 98*f381d571SChristophe Leroy 99*f381d571SChristophe Leroy if (ret) 100*f381d571SChristophe Leroy ret += addr & ~PAGE_MASK; 101*f381d571SChristophe Leroy return ret; 102*f381d571SChristophe Leroy } 103*f381d571SChristophe Leroy 104*f381d571SChristophe Leroy /* 105*f381d571SChristophe Leroy * Unmap an IO region and remove it from vmalloc'd list. 106*f381d571SChristophe Leroy * Access to IO memory should be serialized by driver. 107*f381d571SChristophe Leroy */ 108*f381d571SChristophe Leroy void iounmap(volatile void __iomem *token) 109*f381d571SChristophe Leroy { 110*f381d571SChristophe Leroy void *addr; 111*f381d571SChristophe Leroy 112*f381d571SChristophe Leroy if (!slab_is_available()) 113*f381d571SChristophe Leroy return; 114*f381d571SChristophe Leroy 115*f381d571SChristophe Leroy addr = (void *)((unsigned long __force)PCI_FIX_ADDR(token) & PAGE_MASK); 116*f381d571SChristophe Leroy 117*f381d571SChristophe Leroy if ((unsigned long)addr < ioremap_bot) { 118*f381d571SChristophe Leroy pr_warn("Attempt to iounmap early bolted mapping at 0x%p\n", addr); 119*f381d571SChristophe Leroy return; 120*f381d571SChristophe Leroy } 121*f381d571SChristophe Leroy vunmap(addr); 122*f381d571SChristophe Leroy } 123*f381d571SChristophe Leroy EXPORT_SYMBOL(iounmap); 124