blob: 7fb59279373823339f6fdda86158952e3e6fac45 [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,
84 unsigned long offset,
85 int slot)
Keith Packard9663f2e2008-10-30 19:38:18 -070086{
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080087 resource_size_t phys_addr;
88 unsigned long pfn;
89
90 BUG_ON(offset >= mapping->size);
91 phys_addr = mapping->base + offset;
92 pfn = (unsigned long) (phys_addr >> PAGE_SHIFT);
Chris Wilsonfca3ec02010-08-04 14:34:24 +010093 return iomap_atomic_prot_pfn(pfn, slot, mapping->prot);
Keith Packard9663f2e2008-10-30 19:38:18 -070094}
95
96static inline void
Francisco Jerez29bc17e2010-09-04 22:56:44 +020097io_mapping_unmap_atomic(void __iomem *vaddr, int slot)
Keith Packard9663f2e2008-10-30 19:38:18 -070098{
Chris Wilsonfca3ec02010-08-04 14:34:24 +010099 iounmap_atomic(vaddr, slot);
Keith Packard9663f2e2008-10-30 19:38:18 -0700100}
101
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200102static inline void __iomem *
Keith Packard9663f2e2008-10-30 19:38:18 -0700103io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
104{
Pallipadi, Venkatesh5ce04e32009-03-01 08:53:27 -0800105 resource_size_t phys_addr;
106
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800107 BUG_ON(offset >= mapping->size);
Pallipadi, Venkatesh5ce04e32009-03-01 08:53:27 -0800108 phys_addr = mapping->base + offset;
109
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800110 return ioremap_wc(phys_addr, PAGE_SIZE);
Keith Packard9663f2e2008-10-30 19:38:18 -0700111}
112
113static inline void
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200114io_mapping_unmap(void __iomem *vaddr)
Keith Packard9663f2e2008-10-30 19:38:18 -0700115{
116 iounmap(vaddr);
117}
Keith Packarde5beae12008-11-03 18:21:45 +0100118
119#else
120
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800121/* this struct isn't actually defined anywhere */
122struct io_mapping;
123
Keith Packarde5beae12008-11-03 18:21:45 +0100124/* Create the io_mapping object*/
125static inline struct io_mapping *
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800126io_mapping_create_wc(resource_size_t base, unsigned long size)
Keith Packarde5beae12008-11-03 18:21:45 +0100127{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200128 return (struct io_mapping __force *) ioremap_wc(base, size);
Keith Packarde5beae12008-11-03 18:21:45 +0100129}
130
131static inline void
132io_mapping_free(struct io_mapping *mapping)
133{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200134 iounmap((void __force __iomem *) mapping);
Keith Packarde5beae12008-11-03 18:21:45 +0100135}
136
137/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200138static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100139io_mapping_map_atomic_wc(struct io_mapping *mapping,
140 unsigned long offset,
141 int slot)
Keith Packarde5beae12008-11-03 18:21:45 +0100142{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200143 return ((char __force __iomem *) mapping) + offset;
Keith Packarde5beae12008-11-03 18:21:45 +0100144}
145
146static inline void
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200147io_mapping_unmap_atomic(void __iomem *vaddr, int slot)
Keith Packarde5beae12008-11-03 18:21:45 +0100148{
149}
150
151/* Non-atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200152static inline void __iomem *
Keith Packarde5beae12008-11-03 18:21:45 +0100153io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
154{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200155 return ((char __force __iomem *) mapping) + offset;
Keith Packarde5beae12008-11-03 18:21:45 +0100156}
157
158static inline void
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200159io_mapping_unmap(void __iomem *vaddr)
Keith Packarde5beae12008-11-03 18:21:45 +0100160{
161}
162
163#endif /* HAVE_ATOMIC_IOMAP */
Keith Packard9663f2e2008-10-30 19:38:18 -0700164
165#endif /* _LINUX_IO_MAPPING_H */