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>
16 * The io_mapping mechanism provides an abstraction for mapping
17 * individual pages from an io device to the CPU in an efficient fashion.
19 * See Documentation/io-mapping.txt
29 #ifdef CONFIG_HAVE_ATOMIC_IOMAP
31 #include <asm/iomap.h>
33 * For small address space machines, mapping large objects
34 * into the kernel virtual space isn't practical. Where
35 * available, use fixmap support to dynamically map pages
36 * of the object at run time.
39 static inline struct io_mapping
*
40 io_mapping_init_wc(struct io_mapping
*iomap
,
46 if (iomap_create_wc(base
, size
, &prot
))
56 io_mapping_fini(struct io_mapping
*mapping
)
58 iomap_free(mapping
->base
, mapping
->size
);
61 /* Atomic map/unmap */
62 static inline void __iomem
*
63 io_mapping_map_atomic_wc(struct io_mapping
*mapping
,
66 resource_size_t phys_addr
;
69 BUG_ON(offset
>= mapping
->size
);
70 phys_addr
= mapping
->base
+ offset
;
71 pfn
= (unsigned long) (phys_addr
>> PAGE_SHIFT
);
72 return iomap_atomic_prot_pfn(pfn
, mapping
->prot
);
76 io_mapping_unmap_atomic(void __iomem
*vaddr
)
78 iounmap_atomic(vaddr
);
81 static inline void __iomem
*
82 io_mapping_map_wc(struct io_mapping
*mapping
,
86 resource_size_t phys_addr
;
88 BUG_ON(offset
>= mapping
->size
);
89 phys_addr
= mapping
->base
+ offset
;
91 return ioremap_wc(phys_addr
, size
);
95 io_mapping_unmap(void __iomem
*vaddr
)
102 #include <linux/uaccess.h>
103 #include <asm/pgtable.h>
105 /* Create the io_mapping object*/
106 static inline struct io_mapping
*
107 io_mapping_init_wc(struct io_mapping
*iomap
,
108 resource_size_t base
,
113 iomap
->iomem
= ioremap_wc(base
, size
);
114 #if defined(pgprot_noncached_wc) /* archs can't agree on a name ... */
115 iomap
->prot
= pgprot_noncached_wc(PAGE_KERNEL
);
116 #elif defined(pgprot_writecombine)
117 iomap
->prot
= pgprot_writecombine(PAGE_KERNEL
);
119 iomap
->prot
= pgprot_noncached(PAGE_KERNEL
);
126 io_mapping_fini(struct io_mapping
*mapping
)
128 iounmap(mapping
->iomem
);
131 /* Non-atomic map/unmap */
132 static inline void __iomem
*
133 io_mapping_map_wc(struct io_mapping
*mapping
,
134 unsigned long offset
,
137 return mapping
->iomem
+ offset
;
141 io_mapping_unmap(void __iomem
*vaddr
)
145 /* Atomic map/unmap */
146 static inline void __iomem
*
147 io_mapping_map_atomic_wc(struct io_mapping
*mapping
,
148 unsigned long offset
)
152 return io_mapping_map_wc(mapping
, offset
, PAGE_SIZE
);
156 io_mapping_unmap_atomic(void __iomem
*vaddr
)
158 io_mapping_unmap(vaddr
);
163 #endif /* HAVE_ATOMIC_IOMAP */
165 static inline struct io_mapping
*
166 io_mapping_create_wc(resource_size_t base
,
169 struct io_mapping
*iomap
;
171 iomap
= kmalloc(sizeof(*iomap
), GFP_KERNEL
);
175 if (!io_mapping_init_wc(iomap
, base
, size
)) {
184 io_mapping_free(struct io_mapping
*iomap
)
186 io_mapping_fini(iomap
);
190 #endif /* _LINUX_IO_MAPPING_H */