iova.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2006, Intel Corporation.
  3. *
  4. * This file is released under the GPLv2.
  5. *
  6. * Copyright (C) 2006-2008 Intel Corporation
  7. * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  8. *
  9. */
  10. #ifndef _IOVA_H_
  11. #define _IOVA_H_
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/atomic.h>
  16. #include <linux/dma-mapping.h>
  17. /* iova structure */
  18. struct iova {
  19. struct rb_node node;
  20. unsigned long pfn_hi; /* Highest allocated pfn */
  21. unsigned long pfn_lo; /* Lowest allocated pfn */
  22. };
  23. struct iova_magazine;
  24. struct iova_cpu_rcache;
  25. #define IOVA_RANGE_CACHE_MAX_SIZE 6 /* log of max cached IOVA range size (in pages) */
  26. #define MAX_GLOBAL_MAGS 32 /* magazines per bin */
  27. struct iova_rcache {
  28. spinlock_t lock;
  29. unsigned long depot_size;
  30. struct iova_magazine *depot[MAX_GLOBAL_MAGS];
  31. struct iova_cpu_rcache __percpu *cpu_rcaches;
  32. };
  33. struct iova_domain;
  34. /* Call-Back from IOVA code into IOMMU drivers */
  35. typedef void (* iova_flush_cb)(struct iova_domain *domain);
  36. /* Destructor for per-entry data */
  37. typedef void (* iova_entry_dtor)(unsigned long data);
  38. /* Number of entries per Flush Queue */
  39. #define IOVA_FQ_SIZE 256
  40. /* Timeout (in ms) after which entries are flushed from the Flush-Queue */
  41. #define IOVA_FQ_TIMEOUT 10
  42. /* Flush Queue entry for defered flushing */
  43. struct iova_fq_entry {
  44. unsigned long iova_pfn;
  45. unsigned long pages;
  46. unsigned long data;
  47. u64 counter; /* Flush counter when this entrie was added */
  48. };
  49. /* Per-CPU Flush Queue structure */
  50. struct iova_fq {
  51. struct iova_fq_entry entries[IOVA_FQ_SIZE];
  52. unsigned head, tail;
  53. spinlock_t lock;
  54. };
  55. /* holds all the iova translations for a domain */
  56. struct iova_domain {
  57. spinlock_t iova_rbtree_lock; /* Lock to protect update of rbtree */
  58. struct rb_root rbroot; /* iova domain rbtree root */
  59. struct rb_node *cached_node; /* Save last alloced node */
  60. struct rb_node *cached32_node; /* Save last 32-bit alloced node */
  61. unsigned long granule; /* pfn granularity for this domain */
  62. unsigned long start_pfn; /* Lower limit for this domain */
  63. unsigned long dma_32bit_pfn;
  64. unsigned long max32_alloc_size; /* Size of last failed allocation */
  65. struct iova anchor; /* rbtree lookup anchor */
  66. struct iova_rcache rcaches[IOVA_RANGE_CACHE_MAX_SIZE]; /* IOVA range caches */
  67. iova_flush_cb flush_cb; /* Call-Back function to flush IOMMU
  68. TLBs */
  69. iova_entry_dtor entry_dtor; /* IOMMU driver specific destructor for
  70. iova entry */
  71. struct iova_fq __percpu *fq; /* Flush Queue */
  72. atomic64_t fq_flush_start_cnt; /* Number of TLB flushes that
  73. have been started */
  74. atomic64_t fq_flush_finish_cnt; /* Number of TLB flushes that
  75. have been finished */
  76. struct timer_list fq_timer; /* Timer to regularily empty the
  77. flush-queues */
  78. atomic_t fq_timer_on; /* 1 when timer is active, 0
  79. when not */
  80. };
  81. static inline unsigned long iova_size(struct iova *iova)
  82. {
  83. return iova->pfn_hi - iova->pfn_lo + 1;
  84. }
  85. static inline unsigned long iova_shift(struct iova_domain *iovad)
  86. {
  87. return __ffs(iovad->granule);
  88. }
  89. static inline unsigned long iova_mask(struct iova_domain *iovad)
  90. {
  91. return iovad->granule - 1;
  92. }
  93. static inline size_t iova_offset(struct iova_domain *iovad, dma_addr_t iova)
  94. {
  95. return iova & iova_mask(iovad);
  96. }
  97. static inline size_t iova_align(struct iova_domain *iovad, size_t size)
  98. {
  99. return ALIGN(size, iovad->granule);
  100. }
  101. static inline dma_addr_t iova_dma_addr(struct iova_domain *iovad, struct iova *iova)
  102. {
  103. return (dma_addr_t)iova->pfn_lo << iova_shift(iovad);
  104. }
  105. static inline unsigned long iova_pfn(struct iova_domain *iovad, dma_addr_t iova)
  106. {
  107. return iova >> iova_shift(iovad);
  108. }
  109. #if IS_ENABLED(CONFIG_IOMMU_IOVA)
  110. int iova_cache_get(void);
  111. void iova_cache_put(void);
  112. struct iova *alloc_iova_mem(void);
  113. void free_iova_mem(struct iova *iova);
  114. void free_iova(struct iova_domain *iovad, unsigned long pfn);
  115. void __free_iova(struct iova_domain *iovad, struct iova *iova);
  116. struct iova *alloc_iova(struct iova_domain *iovad, unsigned long size,
  117. unsigned long limit_pfn,
  118. bool size_aligned);
  119. void free_iova_fast(struct iova_domain *iovad, unsigned long pfn,
  120. unsigned long size);
  121. void queue_iova(struct iova_domain *iovad,
  122. unsigned long pfn, unsigned long pages,
  123. unsigned long data);
  124. unsigned long alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
  125. unsigned long limit_pfn, bool flush_rcache);
  126. struct iova *reserve_iova(struct iova_domain *iovad, unsigned long pfn_lo,
  127. unsigned long pfn_hi);
  128. void copy_reserved_iova(struct iova_domain *from, struct iova_domain *to);
  129. void init_iova_domain(struct iova_domain *iovad, unsigned long granule,
  130. unsigned long start_pfn);
  131. int init_iova_flush_queue(struct iova_domain *iovad,
  132. iova_flush_cb flush_cb, iova_entry_dtor entry_dtor);
  133. struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn);
  134. void put_iova_domain(struct iova_domain *iovad);
  135. struct iova *split_and_remove_iova(struct iova_domain *iovad,
  136. struct iova *iova, unsigned long pfn_lo, unsigned long pfn_hi);
  137. void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad);
  138. #else
  139. static inline int iova_cache_get(void)
  140. {
  141. return -ENOTSUPP;
  142. }
  143. static inline void iova_cache_put(void)
  144. {
  145. }
  146. static inline struct iova *alloc_iova_mem(void)
  147. {
  148. return NULL;
  149. }
  150. static inline void free_iova_mem(struct iova *iova)
  151. {
  152. }
  153. static inline void free_iova(struct iova_domain *iovad, unsigned long pfn)
  154. {
  155. }
  156. static inline void __free_iova(struct iova_domain *iovad, struct iova *iova)
  157. {
  158. }
  159. static inline struct iova *alloc_iova(struct iova_domain *iovad,
  160. unsigned long size,
  161. unsigned long limit_pfn,
  162. bool size_aligned)
  163. {
  164. return NULL;
  165. }
  166. static inline void free_iova_fast(struct iova_domain *iovad,
  167. unsigned long pfn,
  168. unsigned long size)
  169. {
  170. }
  171. static inline void queue_iova(struct iova_domain *iovad,
  172. unsigned long pfn, unsigned long pages,
  173. unsigned long data)
  174. {
  175. }
  176. static inline unsigned long alloc_iova_fast(struct iova_domain *iovad,
  177. unsigned long size,
  178. unsigned long limit_pfn,
  179. bool flush_rcache)
  180. {
  181. return 0;
  182. }
  183. static inline struct iova *reserve_iova(struct iova_domain *iovad,
  184. unsigned long pfn_lo,
  185. unsigned long pfn_hi)
  186. {
  187. return NULL;
  188. }
  189. static inline void copy_reserved_iova(struct iova_domain *from,
  190. struct iova_domain *to)
  191. {
  192. }
  193. static inline void init_iova_domain(struct iova_domain *iovad,
  194. unsigned long granule,
  195. unsigned long start_pfn)
  196. {
  197. }
  198. static inline int init_iova_flush_queue(struct iova_domain *iovad,
  199. iova_flush_cb flush_cb,
  200. iova_entry_dtor entry_dtor)
  201. {
  202. return -ENODEV;
  203. }
  204. static inline struct iova *find_iova(struct iova_domain *iovad,
  205. unsigned long pfn)
  206. {
  207. return NULL;
  208. }
  209. static inline void put_iova_domain(struct iova_domain *iovad)
  210. {
  211. }
  212. static inline struct iova *split_and_remove_iova(struct iova_domain *iovad,
  213. struct iova *iova,
  214. unsigned long pfn_lo,
  215. unsigned long pfn_hi)
  216. {
  217. return NULL;
  218. }
  219. static inline void free_cpu_cached_iovas(unsigned int cpu,
  220. struct iova_domain *iovad)
  221. {
  222. }
  223. #endif
  224. #endif