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);
29 sgt = dma_buf_map_attachment(umem_dmabuf->attach,
34 /* modify the sg list in-place to match umem address and length */
36 start = ALIGN_DOWN(umem_dmabuf->umem.address, PAGE_SIZE);
37 end = ALIGN(umem_dmabuf->umem.address + umem_dmabuf->umem.length,
39 for_each_sgtable_dma_sg(sgt, sg, i) {
40 if (start < cur + sg_dma_len(sg) && cur < end)
42 if (cur <= start && start < cur + sg_dma_len(sg)) {
43 unsigned long offset = start - cur;
45 umem_dmabuf->first_sg = sg;
46 umem_dmabuf->first_sg_offset = offset;
47 sg_dma_address(sg) += offset;
48 sg_dma_len(sg) -= offset;
51 if (cur < end && end <= cur + sg_dma_len(sg)) {
52 unsigned long trim = cur + sg_dma_len(sg) - end;
54 umem_dmabuf->last_sg = sg;
55 umem_dmabuf->last_sg_trim = trim;
56 sg_dma_len(sg) -= trim;
59 cur += sg_dma_len(sg);
62 umem_dmabuf->umem.sgt_append.sgt.sgl = umem_dmabuf->first_sg;
63 umem_dmabuf->umem.sgt_append.sgt.nents = nmap;
64 umem_dmabuf->sgt = sgt;
68 * Although the sg list is valid now, the content of the pages
69 * may be not up-to-date. Wait for the exporter to finish
72 ret = dma_resv_wait_timeout(umem_dmabuf->attach->dmabuf->resv,
73 DMA_RESV_USAGE_KERNEL,
74 false, MAX_SCHEDULE_TIMEOUT);
81 EXPORT_SYMBOL(ib_umem_dmabuf_map_pages);
83 void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf)
85 dma_resv_assert_held(umem_dmabuf->attach->dmabuf->resv);
87 if (!umem_dmabuf->sgt)
90 /* retore the original sg list */
91 if (umem_dmabuf->first_sg) {
92 sg_dma_address(umem_dmabuf->first_sg) -=
93 umem_dmabuf->first_sg_offset;
94 sg_dma_len(umem_dmabuf->first_sg) +=
95 umem_dmabuf->first_sg_offset;
96 umem_dmabuf->first_sg = NULL;
97 umem_dmabuf->first_sg_offset = 0;
99 if (umem_dmabuf->last_sg) {
100 sg_dma_len(umem_dmabuf->last_sg) +=
101 umem_dmabuf->last_sg_trim;
102 umem_dmabuf->last_sg = NULL;
103 umem_dmabuf->last_sg_trim = 0;
106 dma_buf_unmap_attachment(umem_dmabuf->attach, umem_dmabuf->sgt,
109 umem_dmabuf->sgt = NULL;
111 EXPORT_SYMBOL(ib_umem_dmabuf_unmap_pages);
113 struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
114 unsigned long offset, size_t size,
116 const struct dma_buf_attach_ops *ops)
118 struct dma_buf *dmabuf;
119 struct ib_umem_dmabuf *umem_dmabuf;
120 struct ib_umem *umem;
122 struct ib_umem_dmabuf *ret = ERR_PTR(-EINVAL);
124 if (check_add_overflow(offset, (unsigned long)size, &end))
127 if (unlikely(!ops || !ops->move_notify))
130 dmabuf = dma_buf_get(fd);
132 return ERR_CAST(dmabuf);
134 if (dmabuf->size < end)
135 goto out_release_dmabuf;
137 umem_dmabuf = kzalloc(sizeof(*umem_dmabuf), GFP_KERNEL);
139 ret = ERR_PTR(-ENOMEM);
140 goto out_release_dmabuf;
143 umem = &umem_dmabuf->umem;
144 umem->ibdev = device;
146 umem->address = offset;
147 umem->writable = ib_access_writable(access);
150 if (!ib_umem_num_pages(umem))
153 umem_dmabuf->attach = dma_buf_dynamic_attach(
158 if (IS_ERR(umem_dmabuf->attach)) {
159 ret = ERR_CAST(umem_dmabuf->attach);
171 EXPORT_SYMBOL(ib_umem_dmabuf_get);
174 ib_umem_dmabuf_unsupported_move_notify(struct dma_buf_attachment *attach)
176 struct ib_umem_dmabuf *umem_dmabuf = attach->importer_priv;
178 ibdev_warn_ratelimited(umem_dmabuf->umem.ibdev,
179 "Invalidate callback should not be called when memory is pinned\n");
182 static struct dma_buf_attach_ops ib_umem_dmabuf_attach_pinned_ops = {
183 .allow_peer2peer = true,
184 .move_notify = ib_umem_dmabuf_unsupported_move_notify,
187 struct ib_umem_dmabuf *ib_umem_dmabuf_get_pinned(struct ib_device *device,
188 unsigned long offset,
192 struct ib_umem_dmabuf *umem_dmabuf;
195 umem_dmabuf = ib_umem_dmabuf_get(device, offset, size, fd, access,
196 &ib_umem_dmabuf_attach_pinned_ops);
197 if (IS_ERR(umem_dmabuf))
200 dma_resv_lock(umem_dmabuf->attach->dmabuf->resv, NULL);
201 err = dma_buf_pin(umem_dmabuf->attach);
204 umem_dmabuf->pinned = 1;
206 err = ib_umem_dmabuf_map_pages(umem_dmabuf);
209 dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
214 dma_buf_unpin(umem_dmabuf->attach);
216 dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
217 ib_umem_release(&umem_dmabuf->umem);
220 EXPORT_SYMBOL(ib_umem_dmabuf_get_pinned);
222 void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf)
224 struct dma_buf *dmabuf = umem_dmabuf->attach->dmabuf;
226 dma_resv_lock(dmabuf->resv, NULL);
227 ib_umem_dmabuf_unmap_pages(umem_dmabuf);
228 if (umem_dmabuf->pinned)
229 dma_buf_unpin(umem_dmabuf->attach);
230 dma_resv_unlock(dmabuf->resv);
232 dma_buf_detach(dmabuf, umem_dmabuf->attach);