1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright © 2008 Keith Packard <keithp@keithp.com>
6 #ifndef _LINUX_IO_MAPPING_H
7 #define _LINUX_IO_MAPPING_H
9 #include <linux/types.h>
10 #include <linux/slab.h>
11 #include <linux/bug.h>
13 #include <linux/pgtable.h>
17 * The io_mapping mechanism provides an abstraction for mapping
18 * individual pages from an io device to the CPU in an efficient fashion.
20 * See Documentation/driver-api/io-mapping.rst
30 #ifdef CONFIG_HAVE_ATOMIC_IOMAP
32 #include <linux/pfn.h>
33 #include <asm/iomap.h>
35 * For small address space machines, mapping large objects
36 * into the kernel virtual space isn't practical. Where
37 * available, use fixmap support to dynamically map pages
38 * of the object at run time.
41 static inline struct io_mapping *
42 io_mapping_init_wc(struct io_mapping *iomap,
48 if (iomap_create_wc(base, size, &prot))
58 io_mapping_fini(struct io_mapping *mapping)
60 iomap_free(mapping->base, mapping->size);
63 /* Atomic map/unmap */
64 static inline void __iomem *
65 io_mapping_map_atomic_wc(struct io_mapping *mapping,
68 resource_size_t phys_addr;
70 BUG_ON(offset >= mapping->size);
71 phys_addr = mapping->base + offset;
74 return __iomap_local_pfn_prot(PHYS_PFN(phys_addr), mapping->prot);
78 io_mapping_unmap_atomic(void __iomem *vaddr)
80 kunmap_local_indexed((void __force *)vaddr);
85 static inline void __iomem *
86 io_mapping_map_local_wc(struct io_mapping *mapping, unsigned long offset)
88 resource_size_t phys_addr;
90 BUG_ON(offset >= mapping->size);
91 phys_addr = mapping->base + offset;
92 return __iomap_local_pfn_prot(PHYS_PFN(phys_addr), mapping->prot);
95 static inline void io_mapping_unmap_local(void __iomem *vaddr)
97 kunmap_local_indexed((void __force *)vaddr);
100 static inline void __iomem *
101 io_mapping_map_wc(struct io_mapping *mapping,
102 unsigned long offset,
105 resource_size_t phys_addr;
107 BUG_ON(offset >= mapping->size);
108 phys_addr = mapping->base + offset;
110 return ioremap_wc(phys_addr, size);
114 io_mapping_unmap(void __iomem *vaddr)
119 #else /* HAVE_ATOMIC_IOMAP */
121 #include <linux/uaccess.h>
123 /* Create the io_mapping object*/
124 static inline struct io_mapping *
125 io_mapping_init_wc(struct io_mapping *iomap,
126 resource_size_t base,
129 iomap->iomem = ioremap_wc(base, size);
135 #if defined(pgprot_noncached_wc) /* archs can't agree on a name ... */
136 iomap->prot = pgprot_noncached_wc(PAGE_KERNEL);
137 #elif defined(pgprot_writecombine)
138 iomap->prot = pgprot_writecombine(PAGE_KERNEL);
140 iomap->prot = pgprot_noncached(PAGE_KERNEL);
147 io_mapping_fini(struct io_mapping *mapping)
149 iounmap(mapping->iomem);
152 /* Non-atomic map/unmap */
153 static inline void __iomem *
154 io_mapping_map_wc(struct io_mapping *mapping,
155 unsigned long offset,
158 return mapping->iomem + offset;
162 io_mapping_unmap(void __iomem *vaddr)
166 /* Atomic map/unmap */
167 static inline void __iomem *
168 io_mapping_map_atomic_wc(struct io_mapping *mapping,
169 unsigned long offset)
173 return io_mapping_map_wc(mapping, offset, PAGE_SIZE);
177 io_mapping_unmap_atomic(void __iomem *vaddr)
179 io_mapping_unmap(vaddr);
184 static inline void __iomem *
185 io_mapping_map_local_wc(struct io_mapping *mapping, unsigned long offset)
187 return io_mapping_map_wc(mapping, offset, PAGE_SIZE);
190 static inline void io_mapping_unmap_local(void __iomem *vaddr)
192 io_mapping_unmap(vaddr);
195 #endif /* !HAVE_ATOMIC_IOMAP */
197 static inline struct io_mapping *
198 io_mapping_create_wc(resource_size_t base,
201 struct io_mapping *iomap;
203 iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
207 if (!io_mapping_init_wc(iomap, base, size)) {
216 io_mapping_free(struct io_mapping *iomap)
218 io_mapping_fini(iomap);
222 #endif /* _LINUX_IO_MAPPING_H */
224 int io_mapping_map_user(struct io_mapping *iomap, struct vm_area_struct *vma,
225 unsigned long addr, unsigned long pfn, unsigned long size);