io-mapping.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  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. #ifndef _LINUX_IO_MAPPING_H
  18. #define _LINUX_IO_MAPPING_H
  19. #include <linux/types.h>
  20. #include <linux/slab.h>
  21. #include <linux/bug.h>
  22. #include <linux/io.h>
  23. #include <asm/page.h>
  24. /*
  25. * The io_mapping mechanism provides an abstraction for mapping
  26. * individual pages from an io device to the CPU in an efficient fashion.
  27. *
  28. * See Documentation/io-mapping.txt
  29. */
  30. struct io_mapping {
  31. resource_size_t base;
  32. unsigned long size;
  33. pgprot_t prot;
  34. void __iomem *iomem;
  35. };
  36. #ifdef CONFIG_HAVE_ATOMIC_IOMAP
  37. #include <asm/iomap.h>
  38. /*
  39. * For small address space machines, mapping large objects
  40. * into the kernel virtual space isn't practical. Where
  41. * available, use fixmap support to dynamically map pages
  42. * of the object at run time.
  43. */
  44. static inline struct io_mapping *
  45. io_mapping_init_wc(struct io_mapping *iomap,
  46. resource_size_t base,
  47. unsigned long size)
  48. {
  49. pgprot_t prot;
  50. if (iomap_create_wc(base, size, &prot))
  51. return NULL;
  52. iomap->base = base;
  53. iomap->size = size;
  54. iomap->prot = prot;
  55. return iomap;
  56. }
  57. static inline void
  58. io_mapping_fini(struct io_mapping *mapping)
  59. {
  60. iomap_free(mapping->base, mapping->size);
  61. }
  62. /* Atomic map/unmap */
  63. static inline void __iomem *
  64. io_mapping_map_atomic_wc(struct io_mapping *mapping,
  65. unsigned long offset)
  66. {
  67. resource_size_t phys_addr;
  68. unsigned long pfn;
  69. BUG_ON(offset >= mapping->size);
  70. phys_addr = mapping->base + offset;
  71. pfn = (unsigned long) (phys_addr >> PAGE_SHIFT);
  72. return iomap_atomic_prot_pfn(pfn, mapping->prot);
  73. }
  74. static inline void
  75. io_mapping_unmap_atomic(void __iomem *vaddr)
  76. {
  77. iounmap_atomic(vaddr);
  78. }
  79. static inline void __iomem *
  80. io_mapping_map_wc(struct io_mapping *mapping,
  81. unsigned long offset,
  82. unsigned long size)
  83. {
  84. resource_size_t phys_addr;
  85. BUG_ON(offset >= mapping->size);
  86. phys_addr = mapping->base + offset;
  87. return ioremap_wc(phys_addr, size);
  88. }
  89. static inline void
  90. io_mapping_unmap(void __iomem *vaddr)
  91. {
  92. iounmap(vaddr);
  93. }
  94. #else
  95. #include <linux/uaccess.h>
  96. #include <asm/pgtable.h>
  97. /* Create the io_mapping object*/
  98. static inline struct io_mapping *
  99. io_mapping_init_wc(struct io_mapping *iomap,
  100. resource_size_t base,
  101. unsigned long size)
  102. {
  103. iomap->base = base;
  104. iomap->size = size;
  105. iomap->iomem = ioremap_wc(base, size);
  106. #if defined(pgprot_noncached_wc) /* archs can't agree on a name ... */
  107. iomap->prot = pgprot_noncached_wc(PAGE_KERNEL);
  108. #elif defined(pgprot_writecombine)
  109. iomap->prot = pgprot_writecombine(PAGE_KERNEL);
  110. #else
  111. iomap->prot = pgprot_noncached(PAGE_KERNEL);
  112. #endif
  113. return iomap;
  114. }
  115. static inline void
  116. io_mapping_fini(struct io_mapping *mapping)
  117. {
  118. iounmap(mapping->iomem);
  119. }
  120. /* Non-atomic map/unmap */
  121. static inline void __iomem *
  122. io_mapping_map_wc(struct io_mapping *mapping,
  123. unsigned long offset,
  124. unsigned long size)
  125. {
  126. return mapping->iomem + offset;
  127. }
  128. static inline void
  129. io_mapping_unmap(void __iomem *vaddr)
  130. {
  131. }
  132. /* Atomic map/unmap */
  133. static inline void __iomem *
  134. io_mapping_map_atomic_wc(struct io_mapping *mapping,
  135. unsigned long offset)
  136. {
  137. preempt_disable();
  138. pagefault_disable();
  139. return io_mapping_map_wc(mapping, offset, PAGE_SIZE);
  140. }
  141. static inline void
  142. io_mapping_unmap_atomic(void __iomem *vaddr)
  143. {
  144. io_mapping_unmap(vaddr);
  145. pagefault_enable();
  146. preempt_enable();
  147. }
  148. #endif /* HAVE_ATOMIC_IOMAP */
  149. static inline struct io_mapping *
  150. io_mapping_create_wc(resource_size_t base,
  151. unsigned long size)
  152. {
  153. struct io_mapping *iomap;
  154. iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
  155. if (!iomap)
  156. return NULL;
  157. if (!io_mapping_init_wc(iomap, base, size)) {
  158. kfree(iomap);
  159. return NULL;
  160. }
  161. return iomap;
  162. }
  163. static inline void
  164. io_mapping_free(struct io_mapping *iomap)
  165. {
  166. io_mapping_fini(iomap);
  167. kfree(iomap);
  168. }
  169. #endif /* _LINUX_IO_MAPPING_H */