ib_umem_odp.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
  2. /*
  3. * Copyright (c) 2014 Mellanox Technologies. All rights reserved.
  4. */
  5. #ifndef IB_UMEM_ODP_H
  6. #define IB_UMEM_ODP_H
  7. #include <rdma/ib_umem.h>
  8. #include <rdma/ib_verbs.h>
  9. struct ib_umem_odp {
  10. struct ib_umem umem;
  11. struct mmu_interval_notifier notifier;
  12. struct pid *tgid;
  13. /* An array of the pfns included in the on-demand paging umem. */
  14. unsigned long *pfn_list;
  15. /*
  16. * An array with DMA addresses mapped for pfns in pfn_list.
  17. * The lower two bits designate access permissions.
  18. * See ODP_READ_ALLOWED_BIT and ODP_WRITE_ALLOWED_BIT.
  19. */
  20. dma_addr_t *dma_list;
  21. /*
  22. * The umem_mutex protects the page_list and dma_list fields of an ODP
  23. * umem, allowing only a single thread to map/unmap pages. The mutex
  24. * also protects access to the mmu notifier counters.
  25. */
  26. struct mutex umem_mutex;
  27. void *private; /* for the HW driver to use. */
  28. int npages;
  29. /*
  30. * An implicit odp umem cannot be DMA mapped, has 0 length, and serves
  31. * only as an anchor for the driver to hold onto the per_mm. FIXME:
  32. * This should be removed and drivers should work with the per_mm
  33. * directly.
  34. */
  35. bool is_implicit_odp;
  36. unsigned int page_shift;
  37. };
  38. static inline struct ib_umem_odp *to_ib_umem_odp(struct ib_umem *umem)
  39. {
  40. return container_of(umem, struct ib_umem_odp, umem);
  41. }
  42. /* Returns the first page of an ODP umem. */
  43. static inline unsigned long ib_umem_start(struct ib_umem_odp *umem_odp)
  44. {
  45. return umem_odp->notifier.interval_tree.start;
  46. }
  47. /* Returns the address of the page after the last one of an ODP umem. */
  48. static inline unsigned long ib_umem_end(struct ib_umem_odp *umem_odp)
  49. {
  50. return umem_odp->notifier.interval_tree.last + 1;
  51. }
  52. static inline size_t ib_umem_odp_num_pages(struct ib_umem_odp *umem_odp)
  53. {
  54. return (ib_umem_end(umem_odp) - ib_umem_start(umem_odp)) >>
  55. umem_odp->page_shift;
  56. }
  57. /*
  58. * The lower 2 bits of the DMA address signal the R/W permissions for
  59. * the entry. To upgrade the permissions, provide the appropriate
  60. * bitmask to the map_dma_pages function.
  61. *
  62. * Be aware that upgrading a mapped address might result in change of
  63. * the DMA address for the page.
  64. */
  65. #define ODP_READ_ALLOWED_BIT (1<<0ULL)
  66. #define ODP_WRITE_ALLOWED_BIT (1<<1ULL)
  67. #define ODP_DMA_ADDR_MASK (~(ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT))
  68. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  69. struct ib_umem_odp *
  70. ib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size,
  71. int access, const struct mmu_interval_notifier_ops *ops);
  72. struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_device *device,
  73. int access);
  74. struct ib_umem_odp *
  75. ib_umem_odp_alloc_child(struct ib_umem_odp *root_umem, unsigned long addr,
  76. size_t size,
  77. const struct mmu_interval_notifier_ops *ops);
  78. void ib_umem_odp_release(struct ib_umem_odp *umem_odp);
  79. int ib_umem_odp_map_dma_and_lock(struct ib_umem_odp *umem_odp, u64 start_offset,
  80. u64 bcnt, u64 access_mask, bool fault);
  81. void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset,
  82. u64 bound);
  83. #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
  84. static inline struct ib_umem_odp *
  85. ib_umem_odp_get(struct ib_device *device, unsigned long addr, size_t size,
  86. int access, const struct mmu_interval_notifier_ops *ops)
  87. {
  88. return ERR_PTR(-EINVAL);
  89. }
  90. static inline void ib_umem_odp_release(struct ib_umem_odp *umem_odp) {}
  91. #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
  92. #endif /* IB_UMEM_ODP_H */