io-mapping.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <asm/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. #ifdef CONFIG_HAVE_ATOMIC_IOMAP
  31. #include <asm/iomap.h>
  32. struct io_mapping {
  33. resource_size_t base;
  34. unsigned long size;
  35. pgprot_t prot;
  36. };
  37. /*
  38. * For small address space machines, mapping large objects
  39. * into the kernel virtual space isn't practical. Where
  40. * available, use fixmap support to dynamically map pages
  41. * of the object at run time.
  42. */
  43. static inline struct io_mapping *
  44. io_mapping_create_wc(resource_size_t base, unsigned long size)
  45. {
  46. struct io_mapping *iomap;
  47. pgprot_t prot;
  48. iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
  49. if (!iomap)
  50. goto out_err;
  51. if (iomap_create_wc(base, size, &prot))
  52. goto out_free;
  53. iomap->base = base;
  54. iomap->size = size;
  55. iomap->prot = prot;
  56. return iomap;
  57. out_free:
  58. kfree(iomap);
  59. out_err:
  60. return NULL;
  61. }
  62. static inline void
  63. io_mapping_free(struct io_mapping *mapping)
  64. {
  65. iomap_free(mapping->base, mapping->size);
  66. kfree(mapping);
  67. }
  68. /* Atomic map/unmap */
  69. static inline void __iomem *
  70. io_mapping_map_atomic_wc(struct io_mapping *mapping,
  71. unsigned long offset)
  72. {
  73. resource_size_t phys_addr;
  74. unsigned long pfn;
  75. BUG_ON(offset >= mapping->size);
  76. phys_addr = mapping->base + offset;
  77. pfn = (unsigned long) (phys_addr >> PAGE_SHIFT);
  78. return iomap_atomic_prot_pfn(pfn, mapping->prot);
  79. }
  80. static inline void
  81. io_mapping_unmap_atomic(void __iomem *vaddr)
  82. {
  83. iounmap_atomic(vaddr);
  84. }
  85. static inline void __iomem *
  86. io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
  87. {
  88. resource_size_t phys_addr;
  89. BUG_ON(offset >= mapping->size);
  90. phys_addr = mapping->base + offset;
  91. return ioremap_wc(phys_addr, PAGE_SIZE);
  92. }
  93. static inline void
  94. io_mapping_unmap(void __iomem *vaddr)
  95. {
  96. iounmap(vaddr);
  97. }
  98. #else
  99. #include <linux/uaccess.h>
  100. /* this struct isn't actually defined anywhere */
  101. struct io_mapping;
  102. /* Create the io_mapping object*/
  103. static inline struct io_mapping *
  104. io_mapping_create_wc(resource_size_t base, unsigned long size)
  105. {
  106. return (struct io_mapping __force *) ioremap_wc(base, size);
  107. }
  108. static inline void
  109. io_mapping_free(struct io_mapping *mapping)
  110. {
  111. iounmap((void __force __iomem *) mapping);
  112. }
  113. /* Atomic map/unmap */
  114. static inline void __iomem *
  115. io_mapping_map_atomic_wc(struct io_mapping *mapping,
  116. unsigned long offset)
  117. {
  118. pagefault_disable();
  119. return ((char __force __iomem *) mapping) + offset;
  120. }
  121. static inline void
  122. io_mapping_unmap_atomic(void __iomem *vaddr)
  123. {
  124. pagefault_enable();
  125. }
  126. /* Non-atomic map/unmap */
  127. static inline void __iomem *
  128. io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
  129. {
  130. return ((char __force __iomem *) mapping) + offset;
  131. }
  132. static inline void
  133. io_mapping_unmap(void __iomem *vaddr)
  134. {
  135. }
  136. #endif /* HAVE_ATOMIC_IOMAP */
  137. #endif /* _LINUX_IO_MAPPING_H */