io-mapping.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. if (!is_io_mapping_possible(base, size))
  46. return NULL;
  47. iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
  48. if (!iomap)
  49. return NULL;
  50. iomap->base = base;
  51. iomap->size = size;
  52. iomap->prot = pgprot_writecombine(__pgprot(__PAGE_KERNEL));
  53. return iomap;
  54. }
  55. static inline void
  56. io_mapping_free(struct io_mapping *mapping)
  57. {
  58. kfree(mapping);
  59. }
  60. /* Atomic map/unmap */
  61. static inline void *
  62. io_mapping_map_atomic_wc(struct io_mapping *mapping, unsigned long offset)
  63. {
  64. resource_size_t phys_addr;
  65. unsigned long pfn;
  66. BUG_ON(offset >= mapping->size);
  67. phys_addr = mapping->base + offset;
  68. pfn = (unsigned long) (phys_addr >> PAGE_SHIFT);
  69. return iomap_atomic_prot_pfn(pfn, KM_USER0, mapping->prot);
  70. }
  71. static inline void
  72. io_mapping_unmap_atomic(void *vaddr)
  73. {
  74. iounmap_atomic(vaddr, KM_USER0);
  75. }
  76. static inline void *
  77. io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
  78. {
  79. resource_size_t phys_addr;
  80. BUG_ON(offset >= mapping->size);
  81. phys_addr = mapping->base + offset;
  82. return ioremap_wc(phys_addr, PAGE_SIZE);
  83. }
  84. static inline void
  85. io_mapping_unmap(void *vaddr)
  86. {
  87. iounmap(vaddr);
  88. }
  89. #else
  90. /* this struct isn't actually defined anywhere */
  91. struct io_mapping;
  92. /* Create the io_mapping object*/
  93. static inline struct io_mapping *
  94. io_mapping_create_wc(resource_size_t base, unsigned long size)
  95. {
  96. return (struct io_mapping *) ioremap_wc(base, size);
  97. }
  98. static inline void
  99. io_mapping_free(struct io_mapping *mapping)
  100. {
  101. iounmap(mapping);
  102. }
  103. /* Atomic map/unmap */
  104. static inline void *
  105. io_mapping_map_atomic_wc(struct io_mapping *mapping, unsigned long offset)
  106. {
  107. return ((char *) mapping) + offset;
  108. }
  109. static inline void
  110. io_mapping_unmap_atomic(void *vaddr)
  111. {
  112. }
  113. /* Non-atomic map/unmap */
  114. static inline void *
  115. io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
  116. {
  117. return ((char *) mapping) + offset;
  118. }
  119. static inline void
  120. io_mapping_unmap(void *vaddr)
  121. {
  122. }
  123. #endif /* HAVE_ATOMIC_IOMAP */
  124. #endif /* _LINUX_IO_MAPPING_H */