io-mapping.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <asm/io.h>
  22. #include <asm/page.h>
  23. /*
  24. * The io_mapping mechanism provides an abstraction for mapping
  25. * individual pages from an io device to the CPU in an efficient fashion.
  26. *
  27. * See Documentation/io_mapping.txt
  28. */
  29. #ifdef CONFIG_HAVE_ATOMIC_IOMAP
  30. #include <asm/iomap.h>
  31. struct io_mapping {
  32. resource_size_t base;
  33. unsigned long size;
  34. pgprot_t prot;
  35. };
  36. /*
  37. * For small address space machines, mapping large objects
  38. * into the kernel virtual space isn't practical. Where
  39. * available, use fixmap support to dynamically map pages
  40. * of the object at run time.
  41. */
  42. static inline struct io_mapping *
  43. io_mapping_create_wc(resource_size_t base, unsigned long size)
  44. {
  45. struct io_mapping *iomap;
  46. pgprot_t prot;
  47. iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
  48. if (!iomap)
  49. goto out_err;
  50. if (iomap_create_wc(base, size, &prot))
  51. goto out_free;
  52. iomap->base = base;
  53. iomap->size = size;
  54. iomap->prot = prot;
  55. return iomap;
  56. out_free:
  57. kfree(iomap);
  58. out_err:
  59. return NULL;
  60. }
  61. static inline void
  62. io_mapping_free(struct io_mapping *mapping)
  63. {
  64. iomap_free(mapping->base, mapping->size);
  65. kfree(mapping);
  66. }
  67. /* Atomic map/unmap */
  68. static inline void *
  69. io_mapping_map_atomic_wc(struct io_mapping *mapping,
  70. unsigned long offset,
  71. int slot)
  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, slot, mapping->prot);
  79. }
  80. static inline void
  81. io_mapping_unmap_atomic(void *vaddr, int slot)
  82. {
  83. iounmap_atomic(vaddr, slot);
  84. }
  85. static inline void *
  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 *vaddr)
  95. {
  96. iounmap(vaddr);
  97. }
  98. #else
  99. /* this struct isn't actually defined anywhere */
  100. struct io_mapping;
  101. /* Create the io_mapping object*/
  102. static inline struct io_mapping *
  103. io_mapping_create_wc(resource_size_t base, unsigned long size)
  104. {
  105. return (struct io_mapping *) ioremap_wc(base, size);
  106. }
  107. static inline void
  108. io_mapping_free(struct io_mapping *mapping)
  109. {
  110. iounmap(mapping);
  111. }
  112. /* Atomic map/unmap */
  113. static inline void *
  114. io_mapping_map_atomic_wc(struct io_mapping *mapping,
  115. unsigned long offset,
  116. int slot)
  117. {
  118. return ((char *) mapping) + offset;
  119. }
  120. static inline void
  121. io_mapping_unmap_atomic(void *vaddr, int slot)
  122. {
  123. }
  124. /* Non-atomic map/unmap */
  125. static inline void *
  126. io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
  127. {
  128. return ((char *) mapping) + offset;
  129. }
  130. static inline void
  131. io_mapping_unmap(void *vaddr)
  132. {
  133. }
  134. #endif /* HAVE_ATOMIC_IOMAP */
  135. #endif /* _LINUX_IO_MAPPING_H */