io-mapping.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. iomap->prot = pgprot_writecombine(PAGE_KERNEL);
  107. return iomap;
  108. }
  109. static inline void
  110. io_mapping_fini(struct io_mapping *mapping)
  111. {
  112. iounmap(mapping->iomem);
  113. }
  114. /* Non-atomic map/unmap */
  115. static inline void __iomem *
  116. io_mapping_map_wc(struct io_mapping *mapping,
  117. unsigned long offset,
  118. unsigned long size)
  119. {
  120. return mapping->iomem + offset;
  121. }
  122. static inline void
  123. io_mapping_unmap(void __iomem *vaddr)
  124. {
  125. }
  126. /* Atomic map/unmap */
  127. static inline void __iomem *
  128. io_mapping_map_atomic_wc(struct io_mapping *mapping,
  129. unsigned long offset)
  130. {
  131. preempt_disable();
  132. pagefault_disable();
  133. return io_mapping_map_wc(mapping, offset, PAGE_SIZE);
  134. }
  135. static inline void
  136. io_mapping_unmap_atomic(void __iomem *vaddr)
  137. {
  138. io_mapping_unmap(vaddr);
  139. pagefault_enable();
  140. preempt_enable();
  141. }
  142. #endif /* HAVE_ATOMIC_IOMAP */
  143. static inline struct io_mapping *
  144. io_mapping_create_wc(resource_size_t base,
  145. unsigned long size)
  146. {
  147. struct io_mapping *iomap;
  148. iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
  149. if (!iomap)
  150. return NULL;
  151. if (!io_mapping_init_wc(iomap, base, size)) {
  152. kfree(iomap);
  153. return NULL;
  154. }
  155. return iomap;
  156. }
  157. static inline void
  158. io_mapping_free(struct io_mapping *iomap)
  159. {
  160. io_mapping_fini(iomap);
  161. kfree(iomap);
  162. }
  163. #endif /* _LINUX_IO_MAPPING_H */