io-mapping.h 3.4 KB

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