2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 #ifndef _LINUX_IO_MAPPING_H
19 #define _LINUX_IO_MAPPING_H
21 #include <linux/types.h>
22 #include <linux/slab.h>
23 #include <linux/bug.h>
28 * The io_mapping mechanism provides an abstraction for mapping
29 * individual pages from an io device to the CPU in an efficient fashion.
31 * See Documentation/io-mapping.txt
41 #ifdef CONFIG_HAVE_ATOMIC_IOMAP
43 #include <asm/iomap.h>
45 * For small address space machines, mapping large objects
46 * into the kernel virtual space isn't practical. Where
47 * available, use fixmap support to dynamically map pages
48 * of the object at run time.
51 static inline struct io_mapping
*
52 io_mapping_init_wc(struct io_mapping
*iomap
,
58 if (iomap_create_wc(base
, size
, &prot
))
68 io_mapping_fini(struct io_mapping
*mapping
)
70 iomap_free(mapping
->base
, mapping
->size
);
73 /* Atomic map/unmap */
74 static inline void __iomem
*
75 io_mapping_map_atomic_wc(struct io_mapping
*mapping
,
78 resource_size_t phys_addr
;
81 BUG_ON(offset
>= mapping
->size
);
82 phys_addr
= mapping
->base
+ offset
;
83 pfn
= (unsigned long) (phys_addr
>> PAGE_SHIFT
);
84 return iomap_atomic_prot_pfn(pfn
, mapping
->prot
);
88 io_mapping_unmap_atomic(void __iomem
*vaddr
)
90 iounmap_atomic(vaddr
);
93 static inline void __iomem
*
94 io_mapping_map_wc(struct io_mapping
*mapping
,
98 resource_size_t phys_addr
;
100 BUG_ON(offset
>= mapping
->size
);
101 phys_addr
= mapping
->base
+ offset
;
103 return ioremap_wc(phys_addr
, size
);
107 io_mapping_unmap(void __iomem
*vaddr
)
114 #include <linux/uaccess.h>
115 #include <asm/pgtable.h>
117 /* Create the io_mapping object*/
118 static inline struct io_mapping
*
119 io_mapping_init_wc(struct io_mapping
*iomap
,
120 resource_size_t base
,
125 iomap
->iomem
= ioremap_wc(base
, size
);
126 #if defined(pgprot_noncached_wc) /* archs can't agree on a name ... */
127 iomap
->prot
= pgprot_noncached_wc(PAGE_KERNEL
);
128 #elif defined(pgprot_writecombine)
129 iomap
->prot
= pgprot_writecombine(PAGE_KERNEL
);
131 iomap
->prot
= pgprot_noncached(PAGE_KERNEL
);
138 io_mapping_fini(struct io_mapping
*mapping
)
140 iounmap(mapping
->iomem
);
143 /* Non-atomic map/unmap */
144 static inline void __iomem
*
145 io_mapping_map_wc(struct io_mapping
*mapping
,
146 unsigned long offset
,
149 return mapping
->iomem
+ offset
;
153 io_mapping_unmap(void __iomem
*vaddr
)
157 /* Atomic map/unmap */
158 static inline void __iomem
*
159 io_mapping_map_atomic_wc(struct io_mapping
*mapping
,
160 unsigned long offset
)
164 return io_mapping_map_wc(mapping
, offset
, PAGE_SIZE
);
168 io_mapping_unmap_atomic(void __iomem
*vaddr
)
170 io_mapping_unmap(vaddr
);
175 #endif /* HAVE_ATOMIC_IOMAP */
177 static inline struct io_mapping
*
178 io_mapping_create_wc(resource_size_t base
,
181 struct io_mapping
*iomap
;
183 iomap
= kmalloc(sizeof(*iomap
), GFP_KERNEL
);
187 if (!io_mapping_init_wc(iomap
, base
, size
)) {
196 io_mapping_free(struct io_mapping
*iomap
)
198 io_mapping_fini(iomap
);
202 #endif /* _LINUX_IO_MAPPING_H */