Blame view

include/linux/io-mapping.h 3.65 KB
9663f2e6a   Keith Packard   resources: add io...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  /*
   * Copyright © 2008 Keith Packard <keithp@keithp.com>
   *
   * This file is free software; you can redistribute it and/or modify
   * it under the terms of version 2 of the GNU General Public License
   * as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software Foundation,
   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
   */
  
  #ifndef _LINUX_IO_MAPPING_H
  #define _LINUX_IO_MAPPING_H
  
  #include <linux/types.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
22
  #include <linux/slab.h>
9663f2e6a   Keith Packard   resources: add io...
23
24
  #include <asm/io.h>
  #include <asm/page.h>
9663f2e6a   Keith Packard   resources: add io...
25
26
27
28
29
  
  /*
   * The io_mapping mechanism provides an abstraction for mapping
   * individual pages from an io device to the CPU in an efficient fashion.
   *
395cf9691   Paul Bolle   doc: fix broken r...
30
   * See Documentation/io-mapping.txt
9663f2e6a   Keith Packard   resources: add io...
31
   */
e5beae169   Keith Packard   io mapping: clean...
32
  #ifdef CONFIG_HAVE_ATOMIC_IOMAP
31ce4bfdf   Dave Airlie   io-mapping: move ...
33
  #include <asm/iomap.h>
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
34
35
36
37
38
  struct io_mapping {
  	resource_size_t base;
  	unsigned long size;
  	pgprot_t prot;
  };
e5beae169   Keith Packard   io mapping: clean...
39
40
41
42
43
44
  /*
   * For small address space machines, mapping large objects
   * into the kernel virtual space isn't practical. Where
   * available, use fixmap support to dynamically map pages
   * of the object at run time.
   */
9663f2e6a   Keith Packard   resources: add io...
45

9663f2e6a   Keith Packard   resources: add io...
46
  static inline struct io_mapping *
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
47
  io_mapping_create_wc(resource_size_t base, unsigned long size)
9663f2e6a   Keith Packard   resources: add io...
48
  {
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
49
  	struct io_mapping *iomap;
9e36fda0b   Venkatesh Pallipadi   x86, pat: Add PAT...
50
  	pgprot_t prot;
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
51
52
53
  
  	iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
  	if (!iomap)
9e36fda0b   Venkatesh Pallipadi   x86, pat: Add PAT...
54
55
56
57
  		goto out_err;
  
  	if (iomap_create_wc(base, size, &prot))
  		goto out_free;
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
58
59
60
  
  	iomap->base = base;
  	iomap->size = size;
9e36fda0b   Venkatesh Pallipadi   x86, pat: Add PAT...
61
  	iomap->prot = prot;
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
62
  	return iomap;
9e36fda0b   Venkatesh Pallipadi   x86, pat: Add PAT...
63
64
65
66
67
  
  out_free:
  	kfree(iomap);
  out_err:
  	return NULL;
9663f2e6a   Keith Packard   resources: add io...
68
69
70
71
72
  }
  
  static inline void
  io_mapping_free(struct io_mapping *mapping)
  {
9e36fda0b   Venkatesh Pallipadi   x86, pat: Add PAT...
73
  	iomap_free(mapping->base, mapping->size);
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
74
  	kfree(mapping);
9663f2e6a   Keith Packard   resources: add io...
75
76
77
  }
  
  /* Atomic map/unmap */
29bc17ecb   Francisco Jerez   io-mapping: Fix t...
78
  static inline void __iomem *
fca3ec01e   Chris Wilson   drm,io-mapping: S...
79
  io_mapping_map_atomic_wc(struct io_mapping *mapping,
3e4d3af50   Peter Zijlstra   mm: stack based k...
80
  			 unsigned long offset)
9663f2e6a   Keith Packard   resources: add io...
81
  {
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
82
83
84
85
86
87
  	resource_size_t phys_addr;
  	unsigned long pfn;
  
  	BUG_ON(offset >= mapping->size);
  	phys_addr = mapping->base + offset;
  	pfn = (unsigned long) (phys_addr >> PAGE_SHIFT);
3e4d3af50   Peter Zijlstra   mm: stack based k...
88
  	return iomap_atomic_prot_pfn(pfn, mapping->prot);
9663f2e6a   Keith Packard   resources: add io...
89
90
91
  }
  
  static inline void
3e4d3af50   Peter Zijlstra   mm: stack based k...
92
  io_mapping_unmap_atomic(void __iomem *vaddr)
9663f2e6a   Keith Packard   resources: add io...
93
  {
3e4d3af50   Peter Zijlstra   mm: stack based k...
94
  	iounmap_atomic(vaddr);
9663f2e6a   Keith Packard   resources: add io...
95
  }
29bc17ecb   Francisco Jerez   io-mapping: Fix t...
96
  static inline void __iomem *
9663f2e6a   Keith Packard   resources: add io...
97
98
  io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
  {
5ce04e3de   Pallipadi, Venkatesh   fix warning in io...
99
  	resource_size_t phys_addr;
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
100
  	BUG_ON(offset >= mapping->size);
5ce04e3de   Pallipadi, Venkatesh   fix warning in io...
101
  	phys_addr = mapping->base + offset;
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
102
  	return ioremap_wc(phys_addr, PAGE_SIZE);
9663f2e6a   Keith Packard   resources: add io...
103
104
105
  }
  
  static inline void
29bc17ecb   Francisco Jerez   io-mapping: Fix t...
106
  io_mapping_unmap(void __iomem *vaddr)
9663f2e6a   Keith Packard   resources: add io...
107
  {
e5beae169   Keith Packard   io mapping: clean...
108
  	iounmap(vaddr);
9663f2e6a   Keith Packard   resources: add io...
109
  }
e5beae169   Keith Packard   io mapping: clean...
110
  #else
9663f2e6a   Keith Packard   resources: add io...
111

24dd85ff7   Daniel Vetter   io-mapping: ensur...
112
  #include <linux/uaccess.h>
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
113
114
  /* this struct isn't actually defined anywhere */
  struct io_mapping;
e5beae169   Keith Packard   io mapping: clean...
115
  /* Create the io_mapping object*/
9663f2e6a   Keith Packard   resources: add io...
116
  static inline struct io_mapping *
4ab0d47d0   Venkatesh Pallipadi   gpu/drm, x86, PAT...
117
  io_mapping_create_wc(resource_size_t base, unsigned long size)
9663f2e6a   Keith Packard   resources: add io...
118
  {
29bc17ecb   Francisco Jerez   io-mapping: Fix t...
119
  	return (struct io_mapping __force *) ioremap_wc(base, size);
9663f2e6a   Keith Packard   resources: add io...
120
121
122
123
124
  }
  
  static inline void
  io_mapping_free(struct io_mapping *mapping)
  {
29bc17ecb   Francisco Jerez   io-mapping: Fix t...
125
  	iounmap((void __force __iomem *) mapping);
9663f2e6a   Keith Packard   resources: add io...
126
127
128
  }
  
  /* Atomic map/unmap */
29bc17ecb   Francisco Jerez   io-mapping: Fix t...
129
  static inline void __iomem *
fca3ec01e   Chris Wilson   drm,io-mapping: S...
130
  io_mapping_map_atomic_wc(struct io_mapping *mapping,
3e4d3af50   Peter Zijlstra   mm: stack based k...
131
  			 unsigned long offset)
9663f2e6a   Keith Packard   resources: add io...
132
  {
24dd85ff7   Daniel Vetter   io-mapping: ensur...
133
  	pagefault_disable();
29bc17ecb   Francisco Jerez   io-mapping: Fix t...
134
  	return ((char __force __iomem *) mapping) + offset;
9663f2e6a   Keith Packard   resources: add io...
135
136
137
  }
  
  static inline void
3e4d3af50   Peter Zijlstra   mm: stack based k...
138
  io_mapping_unmap_atomic(void __iomem *vaddr)
9663f2e6a   Keith Packard   resources: add io...
139
  {
24dd85ff7   Daniel Vetter   io-mapping: ensur...
140
  	pagefault_enable();
9663f2e6a   Keith Packard   resources: add io...
141
  }
e5beae169   Keith Packard   io mapping: clean...
142
  /* Non-atomic map/unmap */
29bc17ecb   Francisco Jerez   io-mapping: Fix t...
143
  static inline void __iomem *
9663f2e6a   Keith Packard   resources: add io...
144
145
  io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
  {
29bc17ecb   Francisco Jerez   io-mapping: Fix t...
146
  	return ((char __force __iomem *) mapping) + offset;
9663f2e6a   Keith Packard   resources: add io...
147
148
149
  }
  
  static inline void
29bc17ecb   Francisco Jerez   io-mapping: Fix t...
150
  io_mapping_unmap(void __iomem *vaddr)
9663f2e6a   Keith Packard   resources: add io...
151
  {
9663f2e6a   Keith Packard   resources: add io...
152
  }
e5beae169   Keith Packard   io mapping: clean...
153
154
  
  #endif /* HAVE_ATOMIC_IOMAP */
9663f2e6a   Keith Packard   resources: add io...
155
156
  
  #endif /* _LINUX_IO_MAPPING_H */