• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * (C) Copyright 1995 1996 Linus Torvalds
4  * (C) Copyright 2012 Regents of the University of California
5  */
6 
7 #include <linux/export.h>
8 #include <linux/mm.h>
9 #include <linux/vmalloc.h>
10 #include <linux/io.h>
11 
12 #include <asm/pgtable.h>
13 
14 /*
15  * Remap an arbitrary physical address space into the kernel virtual
16  * address space. Needed when the kernel wants to access high addresses
17  * directly.
18  *
19  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
20  * have to convert them into an offset in a page-aligned mapping, but the
21  * caller shouldn't need to know that small detail.
22  */
__ioremap_caller(phys_addr_t addr,size_t size,pgprot_t prot,void * caller)23 static void __iomem *__ioremap_caller(phys_addr_t addr, size_t size,
24 	pgprot_t prot, void *caller)
25 {
26 	phys_addr_t last_addr;
27 	unsigned long offset, vaddr;
28 	struct vm_struct *area;
29 
30 	/* Disallow wrap-around or zero size */
31 	last_addr = addr + size - 1;
32 	if (!size || last_addr < addr)
33 		return NULL;
34 
35 	/* Page-align mappings */
36 	offset = addr & (~PAGE_MASK);
37 	addr -= offset;
38 	size = PAGE_ALIGN(size + offset);
39 
40 	area = get_vm_area_caller(size, VM_IOREMAP, caller);
41 	if (!area)
42 		return NULL;
43 	vaddr = (unsigned long)area->addr;
44 
45 	if (ioremap_page_range(vaddr, vaddr + size, addr, prot)) {
46 		free_vm_area(area);
47 		return NULL;
48 	}
49 
50 	return (void __iomem *)(vaddr + offset);
51 }
52 
53 /*
54  * ioremap     -   map bus memory into CPU space
55  * @offset:    bus address of the memory
56  * @size:      size of the resource to map
57  *
58  * ioremap performs a platform specific sequence of operations to
59  * make bus memory CPU accessible via the readb/readw/readl/writeb/
60  * writew/writel functions and the other mmio helpers. The returned
61  * address is not guaranteed to be usable directly as a virtual
62  * address.
63  *
64  * Must be freed with iounmap.
65  */
ioremap(phys_addr_t offset,unsigned long size)66 void __iomem *ioremap(phys_addr_t offset, unsigned long size)
67 {
68 	return __ioremap_caller(offset, size, PAGE_KERNEL,
69 		__builtin_return_address(0));
70 }
71 EXPORT_SYMBOL(ioremap);
72 
73 
74 /**
75  * iounmap - Free a IO remapping
76  * @addr: virtual address from ioremap_*
77  *
78  * Caller must ensure there is only one unmapping for the same pointer.
79  */
iounmap(volatile void __iomem * addr)80 void iounmap(volatile void __iomem *addr)
81 {
82 	vunmap((void *)((unsigned long)addr & PAGE_MASK));
83 }
84 EXPORT_SYMBOL(iounmap);
85