Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
74588d8b HS |
2 | /* |
3 | * Re-map IO memory to kernel address space so that we can access it. | |
4 | * This is needed for high PCI addresses that aren't mapped in the | |
5 | * 640k-1MB IO memory area on PC's | |
6 | * | |
7 | * (C) Copyright 1995 1996 Linus Torvalds | |
8 | */ | |
74588d8b HS |
9 | #include <linux/vmalloc.h> |
10 | #include <linux/mm.h> | |
53fa6645 | 11 | #include <linux/io.h> |
8bc3bcc9 | 12 | #include <linux/export.h> |
74588d8b | 13 | |
abc5992b KW |
14 | void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size, |
15 | unsigned long prot) | |
80b0ca98 CH |
16 | { |
17 | unsigned long offset, vaddr; | |
18 | phys_addr_t last_addr; | |
19 | struct vm_struct *area; | |
20 | ||
21 | /* Disallow wrap-around or zero size */ | |
abc5992b KW |
22 | last_addr = phys_addr + size - 1; |
23 | if (!size || last_addr < phys_addr) | |
80b0ca98 CH |
24 | return NULL; |
25 | ||
26 | /* Page-align mappings */ | |
abc5992b KW |
27 | offset = phys_addr & (~PAGE_MASK); |
28 | phys_addr -= offset; | |
80b0ca98 CH |
29 | size = PAGE_ALIGN(size + offset); |
30 | ||
18e780b4 KW |
31 | if (!ioremap_allowed(phys_addr, size, prot)) |
32 | return NULL; | |
33 | ||
80b0ca98 CH |
34 | area = get_vm_area_caller(size, VM_IOREMAP, |
35 | __builtin_return_address(0)); | |
36 | if (!area) | |
37 | return NULL; | |
38 | vaddr = (unsigned long)area->addr; | |
a14fff1c | 39 | area->phys_addr = phys_addr; |
80b0ca98 | 40 | |
abc5992b KW |
41 | if (ioremap_page_range(vaddr, vaddr + size, phys_addr, |
42 | __pgprot(prot))) { | |
80b0ca98 CH |
43 | free_vm_area(area); |
44 | return NULL; | |
45 | } | |
46 | ||
47 | return (void __iomem *)(vaddr + offset); | |
48 | } | |
49 | EXPORT_SYMBOL(ioremap_prot); | |
50 | ||
51 | void iounmap(volatile void __iomem *addr) | |
52 | { | |
18e780b4 KW |
53 | void *vaddr = (void *)((unsigned long)addr & PAGE_MASK); |
54 | ||
55 | if (!iounmap_allowed(vaddr)) | |
56 | return; | |
57 | ||
58 | if (is_vmalloc_addr(vaddr)) | |
59 | vunmap(vaddr); | |
80b0ca98 CH |
60 | } |
61 | EXPORT_SYMBOL(iounmap); |