blob: fa46183b163bca4ff8f37a6af07c52fb41028158 [file] [log] [blame]
Keith Packard9663f2e2008-10-30 19:38:18 -07001/*
2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
3 *
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.
7 *
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.
12 *
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.
16 */
17
18#ifndef _LINUX_IO_MAPPING_H
19#define _LINUX_IO_MAPPING_H
20
21#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050023#include <linux/bug.h>
Dan Williams2584cf82015-08-10 23:07:05 -040024#include <linux/io.h>
Keith Packard9663f2e2008-10-30 19:38:18 -070025#include <asm/page.h>
Keith Packard9663f2e2008-10-30 19:38:18 -070026
27/*
28 * The io_mapping mechanism provides an abstraction for mapping
29 * individual pages from an io device to the CPU in an efficient fashion.
30 *
Paul Bolle395cf962011-08-15 02:02:26 +020031 * See Documentation/io-mapping.txt
Keith Packard9663f2e2008-10-30 19:38:18 -070032 */
33
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080034struct io_mapping {
35 resource_size_t base;
36 unsigned long size;
37 pgprot_t prot;
Chris Wilsoncafaf142016-08-19 16:54:26 +010038 void __iomem *iomem;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080039};
40
Chris Wilsoncafaf142016-08-19 16:54:26 +010041#ifdef CONFIG_HAVE_ATOMIC_IOMAP
42
43#include <asm/iomap.h>
Keith Packarde5beae12008-11-03 18:21:45 +010044/*
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.
49 */
Keith Packard9663f2e2008-10-30 19:38:18 -070050
Keith Packard9663f2e2008-10-30 19:38:18 -070051static inline struct io_mapping *
Chris Wilsoncafaf142016-08-19 16:54:26 +010052io_mapping_init_wc(struct io_mapping *iomap,
53 resource_size_t base,
54 unsigned long size)
Keith Packard9663f2e2008-10-30 19:38:18 -070055{
Venkatesh Pallipadi9e36fda2009-07-10 09:57:35 -070056 pgprot_t prot;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080057
Venkatesh Pallipadi9e36fda2009-07-10 09:57:35 -070058 if (iomap_create_wc(base, size, &prot))
Chris Wilsoncafaf142016-08-19 16:54:26 +010059 return NULL;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080060
61 iomap->base = base;
62 iomap->size = size;
Venkatesh Pallipadi9e36fda2009-07-10 09:57:35 -070063 iomap->prot = prot;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080064 return iomap;
Keith Packard9663f2e2008-10-30 19:38:18 -070065}
66
67static inline void
Chris Wilsoncafaf142016-08-19 16:54:26 +010068io_mapping_fini(struct io_mapping *mapping)
Keith Packard9663f2e2008-10-30 19:38:18 -070069{
Venkatesh Pallipadi9e36fda2009-07-10 09:57:35 -070070 iomap_free(mapping->base, mapping->size);
Keith Packard9663f2e2008-10-30 19:38:18 -070071}
72
73/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +020074static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +010075io_mapping_map_atomic_wc(struct io_mapping *mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070076 unsigned long offset)
Keith Packard9663f2e2008-10-30 19:38:18 -070077{
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080078 resource_size_t phys_addr;
79 unsigned long pfn;
80
81 BUG_ON(offset >= mapping->size);
82 phys_addr = mapping->base + offset;
83 pfn = (unsigned long) (phys_addr >> PAGE_SHIFT);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070084 return iomap_atomic_prot_pfn(pfn, mapping->prot);
Keith Packard9663f2e2008-10-30 19:38:18 -070085}
86
87static inline void
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070088io_mapping_unmap_atomic(void __iomem *vaddr)
Keith Packard9663f2e2008-10-30 19:38:18 -070089{
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070090 iounmap_atomic(vaddr);
Keith Packard9663f2e2008-10-30 19:38:18 -070091}
92
Francisco Jerez29bc17e2010-09-04 22:56:44 +020093static inline void __iomem *
Chris Wilsond8dab002016-04-28 09:56:37 +010094io_mapping_map_wc(struct io_mapping *mapping,
95 unsigned long offset,
96 unsigned long size)
Keith Packard9663f2e2008-10-30 19:38:18 -070097{
Pallipadi, Venkatesh5ce04e32009-03-01 08:53:27 -080098 resource_size_t phys_addr;
99
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800100 BUG_ON(offset >= mapping->size);
Pallipadi, Venkatesh5ce04e32009-03-01 08:53:27 -0800101 phys_addr = mapping->base + offset;
102
Chris Wilsond8dab002016-04-28 09:56:37 +0100103 return ioremap_wc(phys_addr, size);
Keith Packard9663f2e2008-10-30 19:38:18 -0700104}
105
106static inline void
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200107io_mapping_unmap(void __iomem *vaddr)
Keith Packard9663f2e2008-10-30 19:38:18 -0700108{
109 iounmap(vaddr);
110}
Keith Packarde5beae12008-11-03 18:21:45 +0100111
112#else
113
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200114#include <linux/uaccess.h>
Chris Wilsonbcaaa0c2016-08-23 16:50:24 +0100115#include <asm/pgtable.h>
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800116
Keith Packarde5beae12008-11-03 18:21:45 +0100117/* Create the io_mapping object*/
118static inline struct io_mapping *
Chris Wilsoncafaf142016-08-19 16:54:26 +0100119io_mapping_init_wc(struct io_mapping *iomap,
120 resource_size_t base,
121 unsigned long size)
Keith Packarde5beae12008-11-03 18:21:45 +0100122{
Michael J. Ruhl77f6d932020-07-23 21:15:46 -0700123 iomap->iomem = ioremap_wc(base, size);
124 if (!iomap->iomem)
125 return NULL;
126
Chris Wilsoncafaf142016-08-19 16:54:26 +0100127 iomap->base = base;
128 iomap->size = size;
Daniel Vetter35124382016-08-23 22:15:02 +0200129#if defined(pgprot_noncached_wc) /* archs can't agree on a name ... */
130 iomap->prot = pgprot_noncached_wc(PAGE_KERNEL);
131#elif defined(pgprot_writecombine)
Chris Wilsonbcaaa0c2016-08-23 16:50:24 +0100132 iomap->prot = pgprot_writecombine(PAGE_KERNEL);
Daniel Vetter35124382016-08-23 22:15:02 +0200133#else
134 iomap->prot = pgprot_noncached(PAGE_KERNEL);
135#endif
Chris Wilsoncafaf142016-08-19 16:54:26 +0100136
137 return iomap;
Keith Packarde5beae12008-11-03 18:21:45 +0100138}
139
140static inline void
Chris Wilsoncafaf142016-08-19 16:54:26 +0100141io_mapping_fini(struct io_mapping *mapping)
Keith Packarde5beae12008-11-03 18:21:45 +0100142{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100143 iounmap(mapping->iomem);
144}
145
146/* Non-atomic map/unmap */
147static inline void __iomem *
148io_mapping_map_wc(struct io_mapping *mapping,
149 unsigned long offset,
150 unsigned long size)
151{
152 return mapping->iomem + offset;
153}
154
155static inline void
156io_mapping_unmap(void __iomem *vaddr)
157{
Keith Packarde5beae12008-11-03 18:21:45 +0100158}
159
160/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200161static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100162io_mapping_map_atomic_wc(struct io_mapping *mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700163 unsigned long offset)
Keith Packarde5beae12008-11-03 18:21:45 +0100164{
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +0200165 preempt_disable();
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200166 pagefault_disable();
Chris Wilsoncafaf142016-08-19 16:54:26 +0100167 return io_mapping_map_wc(mapping, offset, PAGE_SIZE);
Keith Packarde5beae12008-11-03 18:21:45 +0100168}
169
170static inline void
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700171io_mapping_unmap_atomic(void __iomem *vaddr)
Keith Packarde5beae12008-11-03 18:21:45 +0100172{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100173 io_mapping_unmap(vaddr);
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200174 pagefault_enable();
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +0200175 preempt_enable();
Keith Packarde5beae12008-11-03 18:21:45 +0100176}
177
Chris Wilsoncafaf142016-08-19 16:54:26 +0100178#endif /* HAVE_ATOMIC_IOMAP */
179
180static inline struct io_mapping *
181io_mapping_create_wc(resource_size_t base,
182 unsigned long size)
Keith Packarde5beae12008-11-03 18:21:45 +0100183{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100184 struct io_mapping *iomap;
185
186 iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
187 if (!iomap)
188 return NULL;
189
190 if (!io_mapping_init_wc(iomap, base, size)) {
191 kfree(iomap);
192 return NULL;
193 }
194
195 return iomap;
Keith Packarde5beae12008-11-03 18:21:45 +0100196}
197
198static inline void
Chris Wilsoncafaf142016-08-19 16:54:26 +0100199io_mapping_free(struct io_mapping *iomap)
Keith Packarde5beae12008-11-03 18:21:45 +0100200{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100201 io_mapping_fini(iomap);
202 kfree(iomap);
Keith Packarde5beae12008-11-03 18:21:45 +0100203}
204
Keith Packard9663f2e2008-10-30 19:38:18 -0700205#endif /* _LINUX_IO_MAPPING_H */