1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
3 * Copyright (c) 2020 Intel Corporation. All rights reserved.
6 #include <linux/dma-buf.h>
7 #include <linux/dma-resv.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/module.h>
13 MODULE_IMPORT_NS("DMA_BUF");
15 int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf
*umem_dmabuf
)
18 struct scatterlist
*sg
;
19 unsigned long start
, end
, cur
= 0;
20 unsigned int nmap
= 0;
24 dma_resv_assert_held(umem_dmabuf
->attach
->dmabuf
->resv
);
26 if (umem_dmabuf
->revoked
)
32 sgt
= dma_buf_map_attachment(umem_dmabuf
->attach
,
37 /* modify the sg list in-place to match umem address and length */
39 start
= ALIGN_DOWN(umem_dmabuf
->umem
.address
, PAGE_SIZE
);
40 end
= ALIGN(umem_dmabuf
->umem
.address
+ umem_dmabuf
->umem
.length
,
42 for_each_sgtable_dma_sg(sgt
, sg
, i
) {
43 if (start
< cur
+ sg_dma_len(sg
) && cur
< end
)
45 if (cur
<= start
&& start
< cur
+ sg_dma_len(sg
)) {
46 unsigned long offset
= start
- cur
;
48 umem_dmabuf
->first_sg
= sg
;
49 umem_dmabuf
->first_sg_offset
= offset
;
50 sg_dma_address(sg
) += offset
;
51 sg_dma_len(sg
) -= offset
;
54 if (cur
< end
&& end
<= cur
+ sg_dma_len(sg
)) {
55 unsigned long trim
= cur
+ sg_dma_len(sg
) - end
;
57 umem_dmabuf
->last_sg
= sg
;
58 umem_dmabuf
->last_sg_trim
= trim
;
59 sg_dma_len(sg
) -= trim
;
62 cur
+= sg_dma_len(sg
);
65 umem_dmabuf
->umem
.sgt_append
.sgt
.sgl
= umem_dmabuf
->first_sg
;
66 umem_dmabuf
->umem
.sgt_append
.sgt
.nents
= nmap
;
67 umem_dmabuf
->sgt
= sgt
;
71 * Although the sg list is valid now, the content of the pages
72 * may be not up-to-date. Wait for the exporter to finish
75 ret
= dma_resv_wait_timeout(umem_dmabuf
->attach
->dmabuf
->resv
,
76 DMA_RESV_USAGE_KERNEL
,
77 false, MAX_SCHEDULE_TIMEOUT
);
84 EXPORT_SYMBOL(ib_umem_dmabuf_map_pages
);
86 void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf
*umem_dmabuf
)
88 dma_resv_assert_held(umem_dmabuf
->attach
->dmabuf
->resv
);
90 if (!umem_dmabuf
->sgt
)
93 /* retore the original sg list */
94 if (umem_dmabuf
->first_sg
) {
95 sg_dma_address(umem_dmabuf
->first_sg
) -=
96 umem_dmabuf
->first_sg_offset
;
97 sg_dma_len(umem_dmabuf
->first_sg
) +=
98 umem_dmabuf
->first_sg_offset
;
99 umem_dmabuf
->first_sg
= NULL
;
100 umem_dmabuf
->first_sg_offset
= 0;
102 if (umem_dmabuf
->last_sg
) {
103 sg_dma_len(umem_dmabuf
->last_sg
) +=
104 umem_dmabuf
->last_sg_trim
;
105 umem_dmabuf
->last_sg
= NULL
;
106 umem_dmabuf
->last_sg_trim
= 0;
109 dma_buf_unmap_attachment(umem_dmabuf
->attach
, umem_dmabuf
->sgt
,
112 umem_dmabuf
->sgt
= NULL
;
114 EXPORT_SYMBOL(ib_umem_dmabuf_unmap_pages
);
116 static struct ib_umem_dmabuf
*
117 ib_umem_dmabuf_get_with_dma_device(struct ib_device
*device
,
118 struct device
*dma_device
,
119 unsigned long offset
, size_t size
,
121 const struct dma_buf_attach_ops
*ops
)
123 struct dma_buf
*dmabuf
;
124 struct ib_umem_dmabuf
*umem_dmabuf
;
125 struct ib_umem
*umem
;
127 struct ib_umem_dmabuf
*ret
= ERR_PTR(-EINVAL
);
129 if (check_add_overflow(offset
, (unsigned long)size
, &end
))
132 if (unlikely(!ops
|| !ops
->move_notify
))
135 dmabuf
= dma_buf_get(fd
);
137 return ERR_CAST(dmabuf
);
139 if (dmabuf
->size
< end
)
140 goto out_release_dmabuf
;
142 umem_dmabuf
= kzalloc(sizeof(*umem_dmabuf
), GFP_KERNEL
);
144 ret
= ERR_PTR(-ENOMEM
);
145 goto out_release_dmabuf
;
148 umem
= &umem_dmabuf
->umem
;
149 umem
->ibdev
= device
;
151 umem
->address
= offset
;
152 umem
->writable
= ib_access_writable(access
);
155 if (!ib_umem_num_pages(umem
))
158 umem_dmabuf
->attach
= dma_buf_dynamic_attach(
163 if (IS_ERR(umem_dmabuf
->attach
)) {
164 ret
= ERR_CAST(umem_dmabuf
->attach
);
177 struct ib_umem_dmabuf
*ib_umem_dmabuf_get(struct ib_device
*device
,
178 unsigned long offset
, size_t size
,
180 const struct dma_buf_attach_ops
*ops
)
182 return ib_umem_dmabuf_get_with_dma_device(device
, device
->dma_device
,
183 offset
, size
, fd
, access
, ops
);
185 EXPORT_SYMBOL(ib_umem_dmabuf_get
);
188 ib_umem_dmabuf_unsupported_move_notify(struct dma_buf_attachment
*attach
)
190 struct ib_umem_dmabuf
*umem_dmabuf
= attach
->importer_priv
;
192 ibdev_warn_ratelimited(umem_dmabuf
->umem
.ibdev
,
193 "Invalidate callback should not be called when memory is pinned\n");
196 static struct dma_buf_attach_ops ib_umem_dmabuf_attach_pinned_ops
= {
197 .allow_peer2peer
= true,
198 .move_notify
= ib_umem_dmabuf_unsupported_move_notify
,
201 struct ib_umem_dmabuf
*
202 ib_umem_dmabuf_get_pinned_with_dma_device(struct ib_device
*device
,
203 struct device
*dma_device
,
204 unsigned long offset
, size_t size
,
207 struct ib_umem_dmabuf
*umem_dmabuf
;
210 umem_dmabuf
= ib_umem_dmabuf_get_with_dma_device(device
, dma_device
, offset
,
212 &ib_umem_dmabuf_attach_pinned_ops
);
213 if (IS_ERR(umem_dmabuf
))
216 dma_resv_lock(umem_dmabuf
->attach
->dmabuf
->resv
, NULL
);
217 err
= dma_buf_pin(umem_dmabuf
->attach
);
220 umem_dmabuf
->pinned
= 1;
222 err
= ib_umem_dmabuf_map_pages(umem_dmabuf
);
225 dma_resv_unlock(umem_dmabuf
->attach
->dmabuf
->resv
);
230 dma_buf_unpin(umem_dmabuf
->attach
);
232 dma_resv_unlock(umem_dmabuf
->attach
->dmabuf
->resv
);
233 ib_umem_release(&umem_dmabuf
->umem
);
236 EXPORT_SYMBOL(ib_umem_dmabuf_get_pinned_with_dma_device
);
238 struct ib_umem_dmabuf
*ib_umem_dmabuf_get_pinned(struct ib_device
*device
,
239 unsigned long offset
,
243 return ib_umem_dmabuf_get_pinned_with_dma_device(device
, device
->dma_device
,
244 offset
, size
, fd
, access
);
246 EXPORT_SYMBOL(ib_umem_dmabuf_get_pinned
);
248 void ib_umem_dmabuf_revoke(struct ib_umem_dmabuf
*umem_dmabuf
)
250 struct dma_buf
*dmabuf
= umem_dmabuf
->attach
->dmabuf
;
252 dma_resv_lock(dmabuf
->resv
, NULL
);
253 if (umem_dmabuf
->revoked
)
255 ib_umem_dmabuf_unmap_pages(umem_dmabuf
);
256 if (umem_dmabuf
->pinned
) {
257 dma_buf_unpin(umem_dmabuf
->attach
);
258 umem_dmabuf
->pinned
= 0;
260 umem_dmabuf
->revoked
= 1;
262 dma_resv_unlock(dmabuf
->resv
);
264 EXPORT_SYMBOL(ib_umem_dmabuf_revoke
);
266 void ib_umem_dmabuf_release(struct ib_umem_dmabuf
*umem_dmabuf
)
268 struct dma_buf
*dmabuf
= umem_dmabuf
->attach
->dmabuf
;
270 ib_umem_dmabuf_revoke(umem_dmabuf
);
272 dma_buf_detach(dmabuf
, umem_dmabuf
->attach
);