blob: e44e84f0156c06a8ba512b11aa38081ef323f703 [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>
Keith Packard9663f2e2008-10-30 19:38:18 -070023#include <asm/io.h>
24#include <asm/page.h>
Keith Packard9663f2e2008-10-30 19:38:18 -070025
26/*
27 * The io_mapping mechanism provides an abstraction for mapping
28 * individual pages from an io device to the CPU in an efficient fashion.
29 *
Paul Bolle395cf962011-08-15 02:02:26 +020030 * See Documentation/io-mapping.txt
Keith Packard9663f2e2008-10-30 19:38:18 -070031 */
32
Keith Packarde5beae12008-11-03 18:21:45 +010033#ifdef CONFIG_HAVE_ATOMIC_IOMAP
Keith Packard9663f2e2008-10-30 19:38:18 -070034
Dave Airlie31ce4bf2010-08-12 11:47:50 +100035#include <asm/iomap.h>
36
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080037struct io_mapping {
38 resource_size_t base;
39 unsigned long size;
40 pgprot_t prot;
41};
42
Keith Packarde5beae12008-11-03 18:21:45 +010043/*
44 * For small address space machines, mapping large objects
45 * into the kernel virtual space isn't practical. Where
46 * available, use fixmap support to dynamically map pages
47 * of the object at run time.
48 */
Keith Packard9663f2e2008-10-30 19:38:18 -070049
Keith Packard9663f2e2008-10-30 19:38:18 -070050static inline struct io_mapping *
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080051io_mapping_create_wc(resource_size_t base, unsigned long size)
Keith Packard9663f2e2008-10-30 19:38:18 -070052{
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080053 struct io_mapping *iomap;
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070054 pgprot_t prot;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080055
56 iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
57 if (!iomap)
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070058 goto out_err;
59
60 if (iomap_create_wc(base, size, &prot))
61 goto out_free;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080062
63 iomap->base = base;
64 iomap->size = size;
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070065 iomap->prot = prot;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080066 return iomap;
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070067
68out_free:
69 kfree(iomap);
70out_err:
71 return NULL;
Keith Packard9663f2e2008-10-30 19:38:18 -070072}
73
74static inline void
75io_mapping_free(struct io_mapping *mapping)
76{
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070077 iomap_free(mapping->base, mapping->size);
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080078 kfree(mapping);
Keith Packard9663f2e2008-10-30 19:38:18 -070079}
80
81/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +020082static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +010083io_mapping_map_atomic_wc(struct io_mapping *mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070084 unsigned long offset)
Keith Packard9663f2e2008-10-30 19:38:18 -070085{
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080086 resource_size_t phys_addr;
87 unsigned long pfn;
88
89 BUG_ON(offset >= mapping->size);
90 phys_addr = mapping->base + offset;
91 pfn = (unsigned long) (phys_addr >> PAGE_SHIFT);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070092 return iomap_atomic_prot_pfn(pfn, mapping->prot);
Keith Packard9663f2e2008-10-30 19:38:18 -070093}
94
95static inline void
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070096io_mapping_unmap_atomic(void __iomem *vaddr)
Keith Packard9663f2e2008-10-30 19:38:18 -070097{
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070098 iounmap_atomic(vaddr);
Keith Packard9663f2e2008-10-30 19:38:18 -070099}
100
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200101static inline void __iomem *
Keith Packard9663f2e2008-10-30 19:38:18 -0700102io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
103{
Pallipadi, Venkatesh5ce04e32009-03-01 08:53:27 -0800104 resource_size_t phys_addr;
105
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800106 BUG_ON(offset >= mapping->size);
Pallipadi, Venkatesh5ce04e32009-03-01 08:53:27 -0800107 phys_addr = mapping->base + offset;
108
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800109 return ioremap_wc(phys_addr, PAGE_SIZE);
Keith Packard9663f2e2008-10-30 19:38:18 -0700110}
111
112static inline void
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200113io_mapping_unmap(void __iomem *vaddr)
Keith Packard9663f2e2008-10-30 19:38:18 -0700114{
115 iounmap(vaddr);
116}
Keith Packarde5beae12008-11-03 18:21:45 +0100117
118#else
119
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200120#include <linux/uaccess.h>
121
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800122/* this struct isn't actually defined anywhere */
123struct io_mapping;
124
Keith Packarde5beae12008-11-03 18:21:45 +0100125/* Create the io_mapping object*/
126static inline struct io_mapping *
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800127io_mapping_create_wc(resource_size_t base, unsigned long size)
Keith Packarde5beae12008-11-03 18:21:45 +0100128{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200129 return (struct io_mapping __force *) ioremap_wc(base, size);
Keith Packarde5beae12008-11-03 18:21:45 +0100130}
131
132static inline void
133io_mapping_free(struct io_mapping *mapping)
134{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200135 iounmap((void __force __iomem *) mapping);
Keith Packarde5beae12008-11-03 18:21:45 +0100136}
137
138/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200139static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100140io_mapping_map_atomic_wc(struct io_mapping *mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700141 unsigned long offset)
Keith Packarde5beae12008-11-03 18:21:45 +0100142{
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200143 pagefault_disable();
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200144 return ((char __force __iomem *) mapping) + offset;
Keith Packarde5beae12008-11-03 18:21:45 +0100145}
146
147static inline void
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700148io_mapping_unmap_atomic(void __iomem *vaddr)
Keith Packarde5beae12008-11-03 18:21:45 +0100149{
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200150 pagefault_enable();
Keith Packarde5beae12008-11-03 18:21:45 +0100151}
152
153/* Non-atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200154static inline void __iomem *
Keith Packarde5beae12008-11-03 18:21:45 +0100155io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
156{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200157 return ((char __force __iomem *) mapping) + offset;
Keith Packarde5beae12008-11-03 18:21:45 +0100158}
159
160static inline void
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200161io_mapping_unmap(void __iomem *vaddr)
Keith Packarde5beae12008-11-03 18:21:45 +0100162{
163}
164
165#endif /* HAVE_ATOMIC_IOMAP */
Keith Packard9663f2e2008-10-30 19:38:18 -0700166
167#endif /* _LINUX_IO_MAPPING_H */