blob: 8cdcc2a199ad1641c3ca652263922fbdcddf6f6a [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 *
30 * See Documentation/io_mapping.txt
31 */
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 Pallipadi9e36fda2009-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 Pallipadi9e36fda2009-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 Pallipadi9e36fda2009-07-10 09:57:35 -070065 iomap->prot = prot;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080066 return iomap;
Venkatesh Pallipadi9e36fda2009-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 Pallipadi9e36fda2009-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
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800120/* this struct isn't actually defined anywhere */
121struct io_mapping;
122
Keith Packarde5beae12008-11-03 18:21:45 +0100123/* Create the io_mapping object*/
124static inline struct io_mapping *
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800125io_mapping_create_wc(resource_size_t base, unsigned long size)
Keith Packarde5beae12008-11-03 18:21:45 +0100126{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200127 return (struct io_mapping __force *) ioremap_wc(base, size);
Keith Packarde5beae12008-11-03 18:21:45 +0100128}
129
130static inline void
131io_mapping_free(struct io_mapping *mapping)
132{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200133 iounmap((void __force __iomem *) mapping);
Keith Packarde5beae12008-11-03 18:21:45 +0100134}
135
136/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200137static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100138io_mapping_map_atomic_wc(struct io_mapping *mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700139 unsigned long offset)
Keith Packarde5beae12008-11-03 18:21:45 +0100140{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200141 return ((char __force __iomem *) mapping) + offset;
Keith Packarde5beae12008-11-03 18:21:45 +0100142}
143
144static inline void
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700145io_mapping_unmap_atomic(void __iomem *vaddr)
Keith Packarde5beae12008-11-03 18:21:45 +0100146{
147}
148
149/* Non-atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200150static inline void __iomem *
Keith Packarde5beae12008-11-03 18:21:45 +0100151io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
152{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200153 return ((char __force __iomem *) mapping) + offset;
Keith Packarde5beae12008-11-03 18:21:45 +0100154}
155
156static inline void
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200157io_mapping_unmap(void __iomem *vaddr)
Keith Packarde5beae12008-11-03 18:21:45 +0100158{
159}
160
161#endif /* HAVE_ATOMIC_IOMAP */
Keith Packard9663f2e2008-10-30 19:38:18 -0700162
163#endif /* _LINUX_IO_MAPPING_H */