blob: d1dcc7e744acfa5adbf2c77cc69d16c11bd826eb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Haavard Skinnemoen74588d82006-09-30 23:29:12 -07002/*
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 */
Haavard Skinnemoen74588d82006-09-30 23:29:12 -07009#include <linux/vmalloc.h>
10#include <linux/mm.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040011#include <linux/sched.h>
Adrian Bunk53fa6642007-10-16 23:26:42 -070012#include <linux/io.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050013#include <linux/export.h>
Haavard Skinnemoen74588d82006-09-30 23:29:12 -070014#include <asm/cacheflush.h>
Haavard Skinnemoen74588d82006-09-30 23:29:12 -070015
Joerg Roedel2a681cf2020-08-06 23:22:55 -070016#include "pgalloc-track.h"
17
Toshi Kani0ddab1d2015-04-14 15:47:20 -070018#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
Nicholas Pigginbbc180a2021-04-29 22:58:26 -070019static bool __ro_after_init iomap_max_page_shift = PAGE_SHIFT;
Toshi Kani0ddab1d2015-04-14 15:47:20 -070020
21static int __init set_nohugeiomap(char *str)
22{
Nicholas Pigginbbc180a2021-04-29 22:58:26 -070023 iomap_max_page_shift = P4D_SHIFT;
Toshi Kani0ddab1d2015-04-14 15:47:20 -070024 return 0;
25}
26early_param("nohugeiomap", set_nohugeiomap);
Nicholas Pigginbbc180a2021-04-29 22:58:26 -070027#else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
28static const bool iomap_max_page_shift = PAGE_SHIFT;
Toshi Kani0ddab1d2015-04-14 15:47:20 -070029#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
30
Nicholas Piggin95f0ddf2021-04-29 22:58:23 -070031int ioremap_page_range(unsigned long addr,
32 unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
33{
Nicholas Pigginbbc180a2021-04-29 22:58:26 -070034 return vmap_range(addr, end, phys_addr, prot, iomap_max_page_shift);
Nicholas Piggin95f0ddf2021-04-29 22:58:23 -070035}
36
Christoph Hellwig80b0ca92019-08-13 11:24:04 +020037#ifdef CONFIG_GENERIC_IOREMAP
38void __iomem *ioremap_prot(phys_addr_t addr, size_t size, unsigned long prot)
39{
40 unsigned long offset, vaddr;
41 phys_addr_t last_addr;
42 struct vm_struct *area;
43
44 /* Disallow wrap-around or zero size */
45 last_addr = addr + size - 1;
46 if (!size || last_addr < addr)
47 return NULL;
48
49 /* Page-align mappings */
50 offset = addr & (~PAGE_MASK);
51 addr -= offset;
52 size = PAGE_ALIGN(size + offset);
53
54 area = get_vm_area_caller(size, VM_IOREMAP,
55 __builtin_return_address(0));
56 if (!area)
57 return NULL;
58 vaddr = (unsigned long)area->addr;
59
60 if (ioremap_page_range(vaddr, vaddr + size, addr, __pgprot(prot))) {
61 free_vm_area(area);
62 return NULL;
63 }
64
65 return (void __iomem *)(vaddr + offset);
66}
67EXPORT_SYMBOL(ioremap_prot);
68
69void iounmap(volatile void __iomem *addr)
70{
71 vunmap((void *)((unsigned long)addr & PAGE_MASK));
72}
73EXPORT_SYMBOL(iounmap);
74#endif /* CONFIG_GENERIC_IOREMAP */