iova.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/dma-mapping.h>
  16. /* iova structure */
  17. struct iova {
  18. struct rb_node node;
  19. unsigned long pfn_hi; /* IOMMU dish out addr hi */
  20. unsigned long pfn_lo; /* IOMMU dish out addr lo */
  21. };
  22. /* holds all the iova translations for a domain */
  23. struct iova_domain {
  24. spinlock_t iova_rbtree_lock; /* Lock to protect update of rbtree */
  25. struct rb_root rbroot; /* iova domain rbtree root */
  26. struct rb_node *cached32_node; /* Save last alloced node */
  27. unsigned long granule; /* pfn granularity for this domain */
  28. unsigned long start_pfn; /* Lower limit for this domain */
  29. unsigned long dma_32bit_pfn;
  30. };
  31. static inline unsigned long iova_size(struct iova *iova)
  32. {
  33. return iova->pfn_hi - iova->pfn_lo + 1;
  34. }
  35. static inline unsigned long iova_shift(struct iova_domain *iovad)
  36. {
  37. return __ffs(iovad->granule);
  38. }
  39. static inline unsigned long iova_mask(struct iova_domain *iovad)
  40. {
  41. return iovad->granule - 1;
  42. }
  43. static inline size_t iova_offset(struct iova_domain *iovad, dma_addr_t iova)
  44. {
  45. return iova & iova_mask(iovad);
  46. }
  47. static inline size_t iova_align(struct iova_domain *iovad, size_t size)
  48. {
  49. return ALIGN(size, iovad->granule);
  50. }
  51. static inline dma_addr_t iova_dma_addr(struct iova_domain *iovad, struct iova *iova)
  52. {
  53. return (dma_addr_t)iova->pfn_lo << iova_shift(iovad);
  54. }
  55. static inline unsigned long iova_pfn(struct iova_domain *iovad, dma_addr_t iova)
  56. {
  57. return iova >> iova_shift(iovad);
  58. }
  59. int iova_cache_get(void);
  60. void iova_cache_put(void);
  61. struct iova *alloc_iova_mem(void);
  62. void free_iova_mem(struct iova *iova);
  63. void free_iova(struct iova_domain *iovad, unsigned long pfn);
  64. void __free_iova(struct iova_domain *iovad, struct iova *iova);
  65. struct iova *alloc_iova(struct iova_domain *iovad, unsigned long size,
  66. unsigned long limit_pfn,
  67. bool size_aligned);
  68. struct iova *reserve_iova(struct iova_domain *iovad, unsigned long pfn_lo,
  69. unsigned long pfn_hi);
  70. void copy_reserved_iova(struct iova_domain *from, struct iova_domain *to);
  71. void init_iova_domain(struct iova_domain *iovad, unsigned long granule,
  72. unsigned long start_pfn, unsigned long pfn_32bit);
  73. struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn);
  74. void put_iova_domain(struct iova_domain *iovad);
  75. struct iova *split_and_remove_iova(struct iova_domain *iovad,
  76. struct iova *iova, unsigned long pfn_lo, unsigned long pfn_hi);
  77. #endif