1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
3 * Copyright (c) 2007 Cisco Systems. All rights reserved.
9 #include <linux/list.h>
10 #include <linux/scatterlist.h>
11 #include <linux/workqueue.h>
12 #include <rdma/ib_verbs.h>
18 struct ib_device
*ibdev
;
19 struct mm_struct
*owning_mm
;
22 unsigned long address
;
25 struct work_struct work
;
26 struct sg_table sg_head
;
28 unsigned int sg_nents
;
31 /* Returns the offset of the umem start relative to the first page. */
32 static inline int ib_umem_offset(struct ib_umem
*umem
)
34 return umem
->address
& ~PAGE_MASK
;
37 static inline unsigned long ib_umem_dma_offset(struct ib_umem
*umem
,
40 return (sg_dma_address(umem
->sg_head
.sgl
) + ib_umem_offset(umem
)) &
44 static inline size_t ib_umem_num_dma_blocks(struct ib_umem
*umem
,
47 return (size_t)((ALIGN(umem
->iova
+ umem
->length
, pgsz
) -
48 ALIGN_DOWN(umem
->iova
, pgsz
))) /
52 static inline size_t ib_umem_num_pages(struct ib_umem
*umem
)
54 return ib_umem_num_dma_blocks(umem
, PAGE_SIZE
);
57 static inline void __rdma_umem_block_iter_start(struct ib_block_iter
*biter
,
61 __rdma_block_iter_start(biter
, umem
->sg_head
.sgl
, umem
->nmap
, pgsz
);
65 * rdma_umem_for_each_dma_block - iterate over contiguous DMA blocks of the umem
66 * @umem: umem to iterate over
67 * @pgsz: Page size to split the list into
69 * pgsz must be <= PAGE_SIZE or computed by ib_umem_find_best_pgsz(). The
70 * returned DMA blocks will be aligned to pgsz and span the range:
71 * ALIGN_DOWN(umem->address, pgsz) to ALIGN(umem->address + umem->length, pgsz)
73 * Performs exactly ib_umem_num_dma_blocks() iterations.
75 #define rdma_umem_for_each_dma_block(umem, biter, pgsz) \
76 for (__rdma_umem_block_iter_start(biter, umem, pgsz); \
77 __rdma_block_iter_next(biter);)
79 #ifdef CONFIG_INFINIBAND_USER_MEM
81 struct ib_umem
*ib_umem_get(struct ib_device
*device
, unsigned long addr
,
82 size_t size
, int access
);
83 void ib_umem_release(struct ib_umem
*umem
);
84 int ib_umem_copy_from(void *dst
, struct ib_umem
*umem
, size_t offset
,
86 unsigned long ib_umem_find_best_pgsz(struct ib_umem
*umem
,
87 unsigned long pgsz_bitmap
,
90 * ib_umem_find_best_pgoff - Find best HW page size
93 * @pgsz_bitmap bitmap of HW supported page sizes
94 * @pgoff_bitmask: Mask of bits that can be represented with an offset
96 * This is very similar to ib_umem_find_best_pgsz() except instead of accepting
97 * an IOVA it accepts a bitmask specifying what address bits can be represented
100 * For instance if the HW has multiple page sizes, requires 64 byte alignemnt,
101 * and can support aligned offsets up to 4032 then pgoff_bitmask would be
104 * If the pgoff_bitmask requires either alignment in the low bit or an
105 * unavailable page size for the high bits, this function returns 0.
107 static inline unsigned long ib_umem_find_best_pgoff(struct ib_umem
*umem
,
108 unsigned long pgsz_bitmap
,
111 struct scatterlist
*sg
= umem
->sg_head
.sgl
;
114 dma_addr
= sg_dma_address(sg
) + (umem
->address
& ~PAGE_MASK
);
115 return ib_umem_find_best_pgsz(umem
, pgsz_bitmap
,
116 dma_addr
& pgoff_bitmask
);
119 #else /* CONFIG_INFINIBAND_USER_MEM */
121 #include <linux/err.h>
123 static inline struct ib_umem
*ib_umem_get(struct ib_device
*device
,
124 unsigned long addr
, size_t size
,
127 return ERR_PTR(-EINVAL
);
129 static inline void ib_umem_release(struct ib_umem
*umem
) { }
130 static inline int ib_umem_copy_from(void *dst
, struct ib_umem
*umem
, size_t offset
,
134 static inline unsigned long ib_umem_find_best_pgsz(struct ib_umem
*umem
,
135 unsigned long pgsz_bitmap
,
140 static inline unsigned long ib_umem_find_best_pgoff(struct ib_umem
*umem
,
141 unsigned long pgsz_bitmap
,
147 #endif /* CONFIG_INFINIBAND_USER_MEM */
149 #endif /* IB_UMEM_H */