1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
3 * Copyright (c) 2007 Cisco Systems. All rights reserved.
4 * Copyright (c) 2020 Intel Corporation. All rights reserved.
10 #include <linux/list.h>
11 #include <linux/scatterlist.h>
12 #include <linux/workqueue.h>
13 #include <rdma/ib_verbs.h>
17 struct dma_buf_attach_ops;
20 struct ib_device *ibdev;
21 struct mm_struct *owning_mm;
24 unsigned long address;
28 struct sg_append_table sgt_append;
31 struct ib_umem_dmabuf {
33 struct dma_buf_attachment *attach;
35 struct scatterlist *first_sg;
36 struct scatterlist *last_sg;
37 unsigned long first_sg_offset;
38 unsigned long last_sg_trim;
43 static inline struct ib_umem_dmabuf *to_ib_umem_dmabuf(struct ib_umem *umem)
45 return container_of(umem, struct ib_umem_dmabuf, umem);
48 /* Returns the offset of the umem start relative to the first page. */
49 static inline int ib_umem_offset(struct ib_umem *umem)
51 return umem->address & ~PAGE_MASK;
54 static inline unsigned long ib_umem_dma_offset(struct ib_umem *umem,
57 return (sg_dma_address(umem->sgt_append.sgt.sgl) + ib_umem_offset(umem)) &
61 static inline size_t ib_umem_num_dma_blocks(struct ib_umem *umem,
64 return (size_t)((ALIGN(umem->iova + umem->length, pgsz) -
65 ALIGN_DOWN(umem->iova, pgsz))) /
69 static inline size_t ib_umem_num_pages(struct ib_umem *umem)
71 return ib_umem_num_dma_blocks(umem, PAGE_SIZE);
74 static inline void __rdma_umem_block_iter_start(struct ib_block_iter *biter,
78 __rdma_block_iter_start(biter, umem->sgt_append.sgt.sgl,
79 umem->sgt_append.sgt.nents, pgsz);
83 * rdma_umem_for_each_dma_block - iterate over contiguous DMA blocks of the umem
84 * @umem: umem to iterate over
85 * @pgsz: Page size to split the list into
87 * pgsz must be <= PAGE_SIZE or computed by ib_umem_find_best_pgsz(). The
88 * returned DMA blocks will be aligned to pgsz and span the range:
89 * ALIGN_DOWN(umem->address, pgsz) to ALIGN(umem->address + umem->length, pgsz)
91 * Performs exactly ib_umem_num_dma_blocks() iterations.
93 #define rdma_umem_for_each_dma_block(umem, biter, pgsz) \
94 for (__rdma_umem_block_iter_start(biter, umem, pgsz); \
95 __rdma_block_iter_next(biter);)
97 #ifdef CONFIG_INFINIBAND_USER_MEM
99 struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr,
100 size_t size, int access);
101 void ib_umem_release(struct ib_umem *umem);
102 int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
104 unsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
105 unsigned long pgsz_bitmap,
109 * ib_umem_find_best_pgoff - Find best HW page size
112 * @pgsz_bitmap bitmap of HW supported page sizes
113 * @pgoff_bitmask: Mask of bits that can be represented with an offset
115 * This is very similar to ib_umem_find_best_pgsz() except instead of accepting
116 * an IOVA it accepts a bitmask specifying what address bits can be represented
117 * with a page offset.
119 * For instance if the HW has multiple page sizes, requires 64 byte alignemnt,
120 * and can support aligned offsets up to 4032 then pgoff_bitmask would be
123 * If the pgoff_bitmask requires either alignment in the low bit or an
124 * unavailable page size for the high bits, this function returns 0.
126 static inline unsigned long ib_umem_find_best_pgoff(struct ib_umem *umem,
127 unsigned long pgsz_bitmap,
130 struct scatterlist *sg = umem->sgt_append.sgt.sgl;
133 dma_addr = sg_dma_address(sg) + (umem->address & ~PAGE_MASK);
134 return ib_umem_find_best_pgsz(umem, pgsz_bitmap,
135 dma_addr & pgoff_bitmask);
138 struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
139 unsigned long offset, size_t size,
141 const struct dma_buf_attach_ops *ops);
142 struct ib_umem_dmabuf *ib_umem_dmabuf_get_pinned(struct ib_device *device,
143 unsigned long offset,
146 int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf);
147 void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf);
148 void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf);
150 #else /* CONFIG_INFINIBAND_USER_MEM */
152 #include <linux/err.h>
154 static inline struct ib_umem *ib_umem_get(struct ib_device *device,
155 unsigned long addr, size_t size,
158 return ERR_PTR(-EOPNOTSUPP);
160 static inline void ib_umem_release(struct ib_umem *umem) { }
161 static inline int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
165 static inline unsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
166 unsigned long pgsz_bitmap,
171 static inline unsigned long ib_umem_find_best_pgoff(struct ib_umem *umem,
172 unsigned long pgsz_bitmap,
178 struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
179 unsigned long offset,
182 struct dma_buf_attach_ops *ops)
184 return ERR_PTR(-EOPNOTSUPP);
186 static inline struct ib_umem_dmabuf *
187 ib_umem_dmabuf_get_pinned(struct ib_device *device, unsigned long offset,
188 size_t size, int fd, int access)
190 return ERR_PTR(-EOPNOTSUPP);
192 static inline int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf)
196 static inline void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf) { }
197 static inline void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf) { }
199 #endif /* CONFIG_INFINIBAND_USER_MEM */
200 #endif /* IB_UMEM_H */