1368c0159SJianxin Xiong // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2368c0159SJianxin Xiong /*
3368c0159SJianxin Xiong  * Copyright (c) 2020 Intel Corporation. All rights reserved.
4368c0159SJianxin Xiong  */
5368c0159SJianxin Xiong 
6368c0159SJianxin Xiong #include <linux/dma-buf.h>
7368c0159SJianxin Xiong #include <linux/dma-resv.h>
8368c0159SJianxin Xiong #include <linux/dma-mapping.h>
916b0314aSGreg Kroah-Hartman #include <linux/module.h>
10368c0159SJianxin Xiong 
11368c0159SJianxin Xiong #include "uverbs.h"
12368c0159SJianxin Xiong 
1316b0314aSGreg Kroah-Hartman MODULE_IMPORT_NS(DMA_BUF);
1416b0314aSGreg Kroah-Hartman 
ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf * umem_dmabuf)15368c0159SJianxin Xiong int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf)
16368c0159SJianxin Xiong {
17368c0159SJianxin Xiong 	struct sg_table *sgt;
18368c0159SJianxin Xiong 	struct scatterlist *sg;
19368c0159SJianxin Xiong 	unsigned long start, end, cur = 0;
20368c0159SJianxin Xiong 	unsigned int nmap = 0;
21b16de8b9SJason Gunthorpe 	long ret;
22368c0159SJianxin Xiong 	int i;
23368c0159SJianxin Xiong 
24368c0159SJianxin Xiong 	dma_resv_assert_held(umem_dmabuf->attach->dmabuf->resv);
25368c0159SJianxin Xiong 
26368c0159SJianxin Xiong 	if (umem_dmabuf->sgt)
27368c0159SJianxin Xiong 		goto wait_fence;
28368c0159SJianxin Xiong 
29*c956940aSMaor Gottlieb 	sgt = dma_buf_map_attachment(umem_dmabuf->attach,
3021c9c5c0SDmitry Osipenko 				     DMA_BIDIRECTIONAL);
31368c0159SJianxin Xiong 	if (IS_ERR(sgt))
32368c0159SJianxin Xiong 		return PTR_ERR(sgt);
33368c0159SJianxin Xiong 
34368c0159SJianxin Xiong 	/* modify the sg list in-place to match umem address and length */
35368c0159SJianxin Xiong 
36368c0159SJianxin Xiong 	start = ALIGN_DOWN(umem_dmabuf->umem.address, PAGE_SIZE);
37368c0159SJianxin Xiong 	end = ALIGN(umem_dmabuf->umem.address + umem_dmabuf->umem.length,
38368c0159SJianxin Xiong 		    PAGE_SIZE);
39368c0159SJianxin Xiong 	for_each_sgtable_dma_sg(sgt, sg, i) {
40368c0159SJianxin Xiong 		if (start < cur + sg_dma_len(sg) && cur < end)
41368c0159SJianxin Xiong 			nmap++;
42368c0159SJianxin Xiong 		if (cur <= start && start < cur + sg_dma_len(sg)) {
43368c0159SJianxin Xiong 			unsigned long offset = start - cur;
44368c0159SJianxin Xiong 
45368c0159SJianxin Xiong 			umem_dmabuf->first_sg = sg;
46368c0159SJianxin Xiong 			umem_dmabuf->first_sg_offset = offset;
47368c0159SJianxin Xiong 			sg_dma_address(sg) += offset;
48368c0159SJianxin Xiong 			sg_dma_len(sg) -= offset;
49368c0159SJianxin Xiong 			cur += offset;
50368c0159SJianxin Xiong 		}
51368c0159SJianxin Xiong 		if (cur < end && end <= cur + sg_dma_len(sg)) {
52368c0159SJianxin Xiong 			unsigned long trim = cur + sg_dma_len(sg) - end;
53368c0159SJianxin Xiong 
54368c0159SJianxin Xiong 			umem_dmabuf->last_sg = sg;
55368c0159SJianxin Xiong 			umem_dmabuf->last_sg_trim = trim;
56368c0159SJianxin Xiong 			sg_dma_len(sg) -= trim;
57368c0159SJianxin Xiong 			break;
58368c0159SJianxin Xiong 		}
59368c0159SJianxin Xiong 		cur += sg_dma_len(sg);
60368c0159SJianxin Xiong 	}
61368c0159SJianxin Xiong 
6279fbd3e1SMaor Gottlieb 	umem_dmabuf->umem.sgt_append.sgt.sgl = umem_dmabuf->first_sg;
6379fbd3e1SMaor Gottlieb 	umem_dmabuf->umem.sgt_append.sgt.nents = nmap;
64368c0159SJianxin Xiong 	umem_dmabuf->sgt = sgt;
65368c0159SJianxin Xiong 
66368c0159SJianxin Xiong wait_fence:
67368c0159SJianxin Xiong 	/*
68368c0159SJianxin Xiong 	 * Although the sg list is valid now, the content of the pages
69368c0159SJianxin Xiong 	 * may be not up-to-date. Wait for the exporter to finish
70368c0159SJianxin Xiong 	 * the migration.
71368c0159SJianxin Xiong 	 */
72b16de8b9SJason Gunthorpe 	ret = dma_resv_wait_timeout(umem_dmabuf->attach->dmabuf->resv,
7361e55c6fSChristian König 				     DMA_RESV_USAGE_KERNEL,
74f30bceabSChristian König 				     false, MAX_SCHEDULE_TIMEOUT);
75b16de8b9SJason Gunthorpe 	if (ret < 0)
76b16de8b9SJason Gunthorpe 		return ret;
77b16de8b9SJason Gunthorpe 	if (ret == 0)
78b16de8b9SJason Gunthorpe 		return -ETIMEDOUT;
79b16de8b9SJason Gunthorpe 	return 0;
80368c0159SJianxin Xiong }
81368c0159SJianxin Xiong EXPORT_SYMBOL(ib_umem_dmabuf_map_pages);
82368c0159SJianxin Xiong 
ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf * umem_dmabuf)83368c0159SJianxin Xiong void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf)
84368c0159SJianxin Xiong {
85368c0159SJianxin Xiong 	dma_resv_assert_held(umem_dmabuf->attach->dmabuf->resv);
86368c0159SJianxin Xiong 
87368c0159SJianxin Xiong 	if (!umem_dmabuf->sgt)
88368c0159SJianxin Xiong 		return;
89368c0159SJianxin Xiong 
90368c0159SJianxin Xiong 	/* retore the original sg list */
91368c0159SJianxin Xiong 	if (umem_dmabuf->first_sg) {
92368c0159SJianxin Xiong 		sg_dma_address(umem_dmabuf->first_sg) -=
93368c0159SJianxin Xiong 			umem_dmabuf->first_sg_offset;
94368c0159SJianxin Xiong 		sg_dma_len(umem_dmabuf->first_sg) +=
95368c0159SJianxin Xiong 			umem_dmabuf->first_sg_offset;
96368c0159SJianxin Xiong 		umem_dmabuf->first_sg = NULL;
97368c0159SJianxin Xiong 		umem_dmabuf->first_sg_offset = 0;
98368c0159SJianxin Xiong 	}
99368c0159SJianxin Xiong 	if (umem_dmabuf->last_sg) {
100368c0159SJianxin Xiong 		sg_dma_len(umem_dmabuf->last_sg) +=
101368c0159SJianxin Xiong 			umem_dmabuf->last_sg_trim;
102368c0159SJianxin Xiong 		umem_dmabuf->last_sg = NULL;
103368c0159SJianxin Xiong 		umem_dmabuf->last_sg_trim = 0;
104368c0159SJianxin Xiong 	}
105368c0159SJianxin Xiong 
106*c956940aSMaor Gottlieb 	dma_buf_unmap_attachment(umem_dmabuf->attach, umem_dmabuf->sgt,
107368c0159SJianxin Xiong 				 DMA_BIDIRECTIONAL);
108368c0159SJianxin Xiong 
109368c0159SJianxin Xiong 	umem_dmabuf->sgt = NULL;
110368c0159SJianxin Xiong }
111368c0159SJianxin Xiong EXPORT_SYMBOL(ib_umem_dmabuf_unmap_pages);
112368c0159SJianxin Xiong 
ib_umem_dmabuf_get(struct ib_device * device,unsigned long offset,size_t size,int fd,int access,const struct dma_buf_attach_ops * ops)113368c0159SJianxin Xiong struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
114368c0159SJianxin Xiong 					  unsigned long offset, size_t size,
115368c0159SJianxin Xiong 					  int fd, int access,
116368c0159SJianxin Xiong 					  const struct dma_buf_attach_ops *ops)
117368c0159SJianxin Xiong {
118368c0159SJianxin Xiong 	struct dma_buf *dmabuf;
119368c0159SJianxin Xiong 	struct ib_umem_dmabuf *umem_dmabuf;
120368c0159SJianxin Xiong 	struct ib_umem *umem;
121368c0159SJianxin Xiong 	unsigned long end;
122368c0159SJianxin Xiong 	struct ib_umem_dmabuf *ret = ERR_PTR(-EINVAL);
123368c0159SJianxin Xiong 
124368c0159SJianxin Xiong 	if (check_add_overflow(offset, (unsigned long)size, &end))
125368c0159SJianxin Xiong 		return ret;
126368c0159SJianxin Xiong 
127368c0159SJianxin Xiong 	if (unlikely(!ops || !ops->move_notify))
128368c0159SJianxin Xiong 		return ret;
129368c0159SJianxin Xiong 
130368c0159SJianxin Xiong 	dmabuf = dma_buf_get(fd);
131368c0159SJianxin Xiong 	if (IS_ERR(dmabuf))
132368c0159SJianxin Xiong 		return ERR_CAST(dmabuf);
133368c0159SJianxin Xiong 
134368c0159SJianxin Xiong 	if (dmabuf->size < end)
135368c0159SJianxin Xiong 		goto out_release_dmabuf;
136368c0159SJianxin Xiong 
137368c0159SJianxin Xiong 	umem_dmabuf = kzalloc(sizeof(*umem_dmabuf), GFP_KERNEL);
138368c0159SJianxin Xiong 	if (!umem_dmabuf) {
139368c0159SJianxin Xiong 		ret = ERR_PTR(-ENOMEM);
140368c0159SJianxin Xiong 		goto out_release_dmabuf;
141368c0159SJianxin Xiong 	}
142368c0159SJianxin Xiong 
143368c0159SJianxin Xiong 	umem = &umem_dmabuf->umem;
144368c0159SJianxin Xiong 	umem->ibdev = device;
145368c0159SJianxin Xiong 	umem->length = size;
146368c0159SJianxin Xiong 	umem->address = offset;
147368c0159SJianxin Xiong 	umem->writable = ib_access_writable(access);
148368c0159SJianxin Xiong 	umem->is_dmabuf = 1;
149368c0159SJianxin Xiong 
150368c0159SJianxin Xiong 	if (!ib_umem_num_pages(umem))
151368c0159SJianxin Xiong 		goto out_free_umem;
152368c0159SJianxin Xiong 
153368c0159SJianxin Xiong 	umem_dmabuf->attach = dma_buf_dynamic_attach(
154368c0159SJianxin Xiong 					dmabuf,
155368c0159SJianxin Xiong 					device->dma_device,
156368c0159SJianxin Xiong 					ops,
157368c0159SJianxin Xiong 					umem_dmabuf);
158368c0159SJianxin Xiong 	if (IS_ERR(umem_dmabuf->attach)) {
159368c0159SJianxin Xiong 		ret = ERR_CAST(umem_dmabuf->attach);
160368c0159SJianxin Xiong 		goto out_free_umem;
161368c0159SJianxin Xiong 	}
162368c0159SJianxin Xiong 	return umem_dmabuf;
163368c0159SJianxin Xiong 
164368c0159SJianxin Xiong out_free_umem:
165368c0159SJianxin Xiong 	kfree(umem_dmabuf);
166368c0159SJianxin Xiong 
167368c0159SJianxin Xiong out_release_dmabuf:
168368c0159SJianxin Xiong 	dma_buf_put(dmabuf);
169368c0159SJianxin Xiong 	return ret;
170368c0159SJianxin Xiong }
171368c0159SJianxin Xiong EXPORT_SYMBOL(ib_umem_dmabuf_get);
172368c0159SJianxin Xiong 
1731e4df4a2SGal Pressman static void
ib_umem_dmabuf_unsupported_move_notify(struct dma_buf_attachment * attach)1741e4df4a2SGal Pressman ib_umem_dmabuf_unsupported_move_notify(struct dma_buf_attachment *attach)
1751e4df4a2SGal Pressman {
1761e4df4a2SGal Pressman 	struct ib_umem_dmabuf *umem_dmabuf = attach->importer_priv;
1771e4df4a2SGal Pressman 
1781e4df4a2SGal Pressman 	ibdev_warn_ratelimited(umem_dmabuf->umem.ibdev,
1791e4df4a2SGal Pressman 			       "Invalidate callback should not be called when memory is pinned\n");
1801e4df4a2SGal Pressman }
1811e4df4a2SGal Pressman 
1821e4df4a2SGal Pressman static struct dma_buf_attach_ops ib_umem_dmabuf_attach_pinned_ops = {
1831e4df4a2SGal Pressman 	.allow_peer2peer = true,
1841e4df4a2SGal Pressman 	.move_notify = ib_umem_dmabuf_unsupported_move_notify,
1851e4df4a2SGal Pressman };
1861e4df4a2SGal Pressman 
ib_umem_dmabuf_get_pinned(struct ib_device * device,unsigned long offset,size_t size,int fd,int access)1871e4df4a2SGal Pressman struct ib_umem_dmabuf *ib_umem_dmabuf_get_pinned(struct ib_device *device,
1881e4df4a2SGal Pressman 						 unsigned long offset,
1891e4df4a2SGal Pressman 						 size_t size, int fd,
1901e4df4a2SGal Pressman 						 int access)
1911e4df4a2SGal Pressman {
1921e4df4a2SGal Pressman 	struct ib_umem_dmabuf *umem_dmabuf;
1931e4df4a2SGal Pressman 	int err;
1941e4df4a2SGal Pressman 
1951e4df4a2SGal Pressman 	umem_dmabuf = ib_umem_dmabuf_get(device, offset, size, fd, access,
1961e4df4a2SGal Pressman 					 &ib_umem_dmabuf_attach_pinned_ops);
1971e4df4a2SGal Pressman 	if (IS_ERR(umem_dmabuf))
1981e4df4a2SGal Pressman 		return umem_dmabuf;
1991e4df4a2SGal Pressman 
2001e4df4a2SGal Pressman 	dma_resv_lock(umem_dmabuf->attach->dmabuf->resv, NULL);
2011e4df4a2SGal Pressman 	err = dma_buf_pin(umem_dmabuf->attach);
2021e4df4a2SGal Pressman 	if (err)
2031e4df4a2SGal Pressman 		goto err_release;
2041e4df4a2SGal Pressman 	umem_dmabuf->pinned = 1;
2051e4df4a2SGal Pressman 
2061e4df4a2SGal Pressman 	err = ib_umem_dmabuf_map_pages(umem_dmabuf);
2071e4df4a2SGal Pressman 	if (err)
2081e4df4a2SGal Pressman 		goto err_unpin;
2091e4df4a2SGal Pressman 	dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
2101e4df4a2SGal Pressman 
2111e4df4a2SGal Pressman 	return umem_dmabuf;
2121e4df4a2SGal Pressman 
2131e4df4a2SGal Pressman err_unpin:
2141e4df4a2SGal Pressman 	dma_buf_unpin(umem_dmabuf->attach);
2151e4df4a2SGal Pressman err_release:
2161e4df4a2SGal Pressman 	dma_resv_unlock(umem_dmabuf->attach->dmabuf->resv);
2171e4df4a2SGal Pressman 	ib_umem_release(&umem_dmabuf->umem);
2181e4df4a2SGal Pressman 	return ERR_PTR(err);
2191e4df4a2SGal Pressman }
2201e4df4a2SGal Pressman EXPORT_SYMBOL(ib_umem_dmabuf_get_pinned);
2211e4df4a2SGal Pressman 
ib_umem_dmabuf_release(struct ib_umem_dmabuf * umem_dmabuf)222368c0159SJianxin Xiong void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf)
223368c0159SJianxin Xiong {
224368c0159SJianxin Xiong 	struct dma_buf *dmabuf = umem_dmabuf->attach->dmabuf;
225368c0159SJianxin Xiong 
226e6fb246cSJason Gunthorpe 	dma_resv_lock(dmabuf->resv, NULL);
227e6fb246cSJason Gunthorpe 	ib_umem_dmabuf_unmap_pages(umem_dmabuf);
2281e4df4a2SGal Pressman 	if (umem_dmabuf->pinned)
2291e4df4a2SGal Pressman 		dma_buf_unpin(umem_dmabuf->attach);
230e6fb246cSJason Gunthorpe 	dma_resv_unlock(dmabuf->resv);
231e6fb246cSJason Gunthorpe 
232368c0159SJianxin Xiong 	dma_buf_detach(dmabuf, umem_dmabuf->attach);
233368c0159SJianxin Xiong 	dma_buf_put(dmabuf);
234368c0159SJianxin Xiong 	kfree(umem_dmabuf);
235368c0159SJianxin Xiong }
236