blob: 589fd923c5508cb5dee94b2949aace2a9af21df6 [file] [log] [blame]
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +03001// SPDX-License-Identifier: GPL-2.0
2
3/*
4 * Xen dma-buf functionality for gntdev.
5 *
Oleksandr Andrushchenkoa240d6e2018-07-20 12:01:49 +03006 * DMA buffer implementation is based on drivers/gpu/drm/drm_prime.c.
7 *
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +03008 * Copyright (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
9 */
10
11#include <linux/kernel.h>
12#include <linux/errno.h>
Oleksandr Andrushchenkoa240d6e2018-07-20 12:01:49 +030013#include <linux/dma-buf.h>
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +030014#include <linux/slab.h>
15#include <linux/types.h>
16#include <linux/uaccess.h>
17
18#include <xen/xen.h>
19#include <xen/grant_table.h>
20
21#include "gntdev-common.h"
22#include "gntdev-dmabuf.h"
23
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +030024#ifndef GRANT_INVALID_REF
25/*
26 * Note on usage of grant reference 0 as invalid grant reference:
27 * grant reference 0 is valid, but never exposed to a driver,
28 * because of the fact it is already in use/reserved by the PV console.
29 */
30#define GRANT_INVALID_REF 0
31#endif
32
Oleksandr Andrushchenkoa240d6e2018-07-20 12:01:49 +030033struct gntdev_dmabuf {
34 struct gntdev_dmabuf_priv *priv;
35 struct dma_buf *dmabuf;
36 struct list_head next;
37 int fd;
38
39 union {
40 struct {
41 /* Exported buffers are reference counted. */
42 struct kref refcount;
43
44 struct gntdev_priv *priv;
45 struct gntdev_grant_map *map;
46 } exp;
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +030047 struct {
48 /* Granted references of the imported buffer. */
49 grant_ref_t *refs;
50 /* Scatter-gather table of the imported buffer. */
51 struct sg_table *sgt;
52 /* dma-buf attachment of the imported buffer. */
53 struct dma_buf_attachment *attach;
54 } imp;
Oleksandr Andrushchenkoa240d6e2018-07-20 12:01:49 +030055 } u;
56
57 /* Number of pages this buffer has. */
58 int nr_pages;
59 /* Pages of this buffer. */
60 struct page **pages;
61};
62
63struct gntdev_dmabuf_wait_obj {
64 struct list_head next;
65 struct gntdev_dmabuf *gntdev_dmabuf;
66 struct completion completion;
67};
68
69struct gntdev_dmabuf_attachment {
70 struct sg_table *sgt;
71 enum dma_data_direction dir;
72};
73
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +030074struct gntdev_dmabuf_priv {
75 /* List of exported DMA buffers. */
76 struct list_head exp_list;
77 /* List of wait objects. */
78 struct list_head exp_wait_list;
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +030079 /* List of imported DMA buffers. */
80 struct list_head imp_list;
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +030081 /* This is the lock which protects dma_buf_xxx lists. */
82 struct mutex lock;
83};
84
85/* DMA buffer export support. */
86
87/* Implementation of wait for exported DMA buffer to be released. */
88
Oleksandr Andrushchenkoa240d6e2018-07-20 12:01:49 +030089static void dmabuf_exp_release(struct kref *kref);
90
91static struct gntdev_dmabuf_wait_obj *
92dmabuf_exp_wait_obj_new(struct gntdev_dmabuf_priv *priv,
93 struct gntdev_dmabuf *gntdev_dmabuf)
94{
95 struct gntdev_dmabuf_wait_obj *obj;
96
97 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
98 if (!obj)
99 return ERR_PTR(-ENOMEM);
100
101 init_completion(&obj->completion);
102 obj->gntdev_dmabuf = gntdev_dmabuf;
103
104 mutex_lock(&priv->lock);
105 list_add(&obj->next, &priv->exp_wait_list);
106 /* Put our reference and wait for gntdev_dmabuf's release to fire. */
107 kref_put(&gntdev_dmabuf->u.exp.refcount, dmabuf_exp_release);
108 mutex_unlock(&priv->lock);
109 return obj;
110}
111
112static void dmabuf_exp_wait_obj_free(struct gntdev_dmabuf_priv *priv,
113 struct gntdev_dmabuf_wait_obj *obj)
114{
115 mutex_lock(&priv->lock);
116 list_del(&obj->next);
117 mutex_unlock(&priv->lock);
118 kfree(obj);
119}
120
121static int dmabuf_exp_wait_obj_wait(struct gntdev_dmabuf_wait_obj *obj,
122 u32 wait_to_ms)
123{
124 if (wait_for_completion_timeout(&obj->completion,
125 msecs_to_jiffies(wait_to_ms)) <= 0)
126 return -ETIMEDOUT;
127
128 return 0;
129}
130
131static void dmabuf_exp_wait_obj_signal(struct gntdev_dmabuf_priv *priv,
132 struct gntdev_dmabuf *gntdev_dmabuf)
133{
134 struct gntdev_dmabuf_wait_obj *obj;
135
136 list_for_each_entry(obj, &priv->exp_wait_list, next)
137 if (obj->gntdev_dmabuf == gntdev_dmabuf) {
138 pr_debug("Found gntdev_dmabuf in the wait list, wake\n");
139 complete_all(&obj->completion);
140 break;
141 }
142}
143
144static struct gntdev_dmabuf *
145dmabuf_exp_wait_obj_get_dmabuf(struct gntdev_dmabuf_priv *priv, int fd)
146{
147 struct gntdev_dmabuf *gntdev_dmabuf, *ret = ERR_PTR(-ENOENT);
148
149 mutex_lock(&priv->lock);
150 list_for_each_entry(gntdev_dmabuf, &priv->exp_list, next)
151 if (gntdev_dmabuf->fd == fd) {
152 pr_debug("Found gntdev_dmabuf in the wait list\n");
153 kref_get(&gntdev_dmabuf->u.exp.refcount);
154 ret = gntdev_dmabuf;
155 break;
156 }
157 mutex_unlock(&priv->lock);
158 return ret;
159}
160
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300161static int dmabuf_exp_wait_released(struct gntdev_dmabuf_priv *priv, int fd,
162 int wait_to_ms)
163{
Oleksandr Andrushchenkoa240d6e2018-07-20 12:01:49 +0300164 struct gntdev_dmabuf *gntdev_dmabuf;
165 struct gntdev_dmabuf_wait_obj *obj;
166 int ret;
167
168 pr_debug("Will wait for dma-buf with fd %d\n", fd);
169 /*
170 * Try to find the DMA buffer: if not found means that
171 * either the buffer has already been released or file descriptor
172 * provided is wrong.
173 */
174 gntdev_dmabuf = dmabuf_exp_wait_obj_get_dmabuf(priv, fd);
175 if (IS_ERR(gntdev_dmabuf))
176 return PTR_ERR(gntdev_dmabuf);
177
178 /*
179 * gntdev_dmabuf still exists and is reference count locked by us now,
180 * so prepare to wait: allocate wait object and add it to the wait list,
181 * so we can find it on release.
182 */
183 obj = dmabuf_exp_wait_obj_new(priv, gntdev_dmabuf);
184 if (IS_ERR(obj))
185 return PTR_ERR(obj);
186
187 ret = dmabuf_exp_wait_obj_wait(obj, wait_to_ms);
188 dmabuf_exp_wait_obj_free(priv, obj);
189 return ret;
190}
191
192/* DMA buffer export support. */
193
194static struct sg_table *
195dmabuf_pages_to_sgt(struct page **pages, unsigned int nr_pages)
196{
197 struct sg_table *sgt;
198 int ret;
199
200 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
201 if (!sgt) {
202 ret = -ENOMEM;
203 goto out;
204 }
205
206 ret = sg_alloc_table_from_pages(sgt, pages, nr_pages, 0,
207 nr_pages << PAGE_SHIFT,
208 GFP_KERNEL);
209 if (ret)
210 goto out;
211
212 return sgt;
213
214out:
215 kfree(sgt);
216 return ERR_PTR(ret);
217}
218
219static int dmabuf_exp_ops_attach(struct dma_buf *dma_buf,
220 struct device *target_dev,
221 struct dma_buf_attachment *attach)
222{
223 struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach;
224
225 gntdev_dmabuf_attach = kzalloc(sizeof(*gntdev_dmabuf_attach),
226 GFP_KERNEL);
227 if (!gntdev_dmabuf_attach)
228 return -ENOMEM;
229
230 gntdev_dmabuf_attach->dir = DMA_NONE;
231 attach->priv = gntdev_dmabuf_attach;
232 return 0;
233}
234
235static void dmabuf_exp_ops_detach(struct dma_buf *dma_buf,
236 struct dma_buf_attachment *attach)
237{
238 struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach = attach->priv;
239
240 if (gntdev_dmabuf_attach) {
241 struct sg_table *sgt = gntdev_dmabuf_attach->sgt;
242
243 if (sgt) {
244 if (gntdev_dmabuf_attach->dir != DMA_NONE)
245 dma_unmap_sg_attrs(attach->dev, sgt->sgl,
246 sgt->nents,
247 gntdev_dmabuf_attach->dir,
248 DMA_ATTR_SKIP_CPU_SYNC);
249 sg_free_table(sgt);
250 }
251
252 kfree(sgt);
253 kfree(gntdev_dmabuf_attach);
254 attach->priv = NULL;
255 }
256}
257
258static struct sg_table *
259dmabuf_exp_ops_map_dma_buf(struct dma_buf_attachment *attach,
260 enum dma_data_direction dir)
261{
262 struct gntdev_dmabuf_attachment *gntdev_dmabuf_attach = attach->priv;
263 struct gntdev_dmabuf *gntdev_dmabuf = attach->dmabuf->priv;
264 struct sg_table *sgt;
265
266 pr_debug("Mapping %d pages for dev %p\n", gntdev_dmabuf->nr_pages,
267 attach->dev);
268
269 if (dir == DMA_NONE || !gntdev_dmabuf_attach)
270 return ERR_PTR(-EINVAL);
271
272 /* Return the cached mapping when possible. */
273 if (gntdev_dmabuf_attach->dir == dir)
274 return gntdev_dmabuf_attach->sgt;
275
276 /*
277 * Two mappings with different directions for the same attachment are
278 * not allowed.
279 */
280 if (gntdev_dmabuf_attach->dir != DMA_NONE)
281 return ERR_PTR(-EBUSY);
282
283 sgt = dmabuf_pages_to_sgt(gntdev_dmabuf->pages,
284 gntdev_dmabuf->nr_pages);
285 if (!IS_ERR(sgt)) {
286 if (!dma_map_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
287 DMA_ATTR_SKIP_CPU_SYNC)) {
288 sg_free_table(sgt);
289 kfree(sgt);
290 sgt = ERR_PTR(-ENOMEM);
291 } else {
292 gntdev_dmabuf_attach->sgt = sgt;
293 gntdev_dmabuf_attach->dir = dir;
294 }
295 }
296 if (IS_ERR(sgt))
297 pr_debug("Failed to map sg table for dev %p\n", attach->dev);
298 return sgt;
299}
300
301static void dmabuf_exp_ops_unmap_dma_buf(struct dma_buf_attachment *attach,
302 struct sg_table *sgt,
303 enum dma_data_direction dir)
304{
305 /* Not implemented. The unmap is done at dmabuf_exp_ops_detach(). */
306}
307
308static void dmabuf_exp_release(struct kref *kref)
309{
310 struct gntdev_dmabuf *gntdev_dmabuf =
311 container_of(kref, struct gntdev_dmabuf, u.exp.refcount);
312
313 dmabuf_exp_wait_obj_signal(gntdev_dmabuf->priv, gntdev_dmabuf);
314 list_del(&gntdev_dmabuf->next);
315 kfree(gntdev_dmabuf);
316}
317
318static void dmabuf_exp_remove_map(struct gntdev_priv *priv,
319 struct gntdev_grant_map *map)
320{
321 mutex_lock(&priv->lock);
322 list_del(&map->next);
323 gntdev_put_map(NULL /* already removed */, map);
324 mutex_unlock(&priv->lock);
325}
326
327static void dmabuf_exp_ops_release(struct dma_buf *dma_buf)
328{
329 struct gntdev_dmabuf *gntdev_dmabuf = dma_buf->priv;
330 struct gntdev_dmabuf_priv *priv = gntdev_dmabuf->priv;
331
332 dmabuf_exp_remove_map(gntdev_dmabuf->u.exp.priv,
333 gntdev_dmabuf->u.exp.map);
334 mutex_lock(&priv->lock);
335 kref_put(&gntdev_dmabuf->u.exp.refcount, dmabuf_exp_release);
336 mutex_unlock(&priv->lock);
337}
338
339static void *dmabuf_exp_ops_kmap(struct dma_buf *dma_buf,
340 unsigned long page_num)
341{
342 /* Not implemented. */
343 return NULL;
344}
345
346static void dmabuf_exp_ops_kunmap(struct dma_buf *dma_buf,
347 unsigned long page_num, void *addr)
348{
349 /* Not implemented. */
350}
351
352static int dmabuf_exp_ops_mmap(struct dma_buf *dma_buf,
353 struct vm_area_struct *vma)
354{
355 /* Not implemented. */
356 return 0;
357}
358
359static const struct dma_buf_ops dmabuf_exp_ops = {
360 .attach = dmabuf_exp_ops_attach,
361 .detach = dmabuf_exp_ops_detach,
362 .map_dma_buf = dmabuf_exp_ops_map_dma_buf,
363 .unmap_dma_buf = dmabuf_exp_ops_unmap_dma_buf,
364 .release = dmabuf_exp_ops_release,
365 .map = dmabuf_exp_ops_kmap,
366 .unmap = dmabuf_exp_ops_kunmap,
367 .mmap = dmabuf_exp_ops_mmap,
368};
369
370struct gntdev_dmabuf_export_args {
371 struct gntdev_priv *priv;
372 struct gntdev_grant_map *map;
373 struct gntdev_dmabuf_priv *dmabuf_priv;
374 struct device *dev;
375 int count;
376 struct page **pages;
377 u32 fd;
378};
379
380static int dmabuf_exp_from_pages(struct gntdev_dmabuf_export_args *args)
381{
382 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
383 struct gntdev_dmabuf *gntdev_dmabuf;
384 int ret;
385
386 gntdev_dmabuf = kzalloc(sizeof(*gntdev_dmabuf), GFP_KERNEL);
387 if (!gntdev_dmabuf)
388 return -ENOMEM;
389
390 kref_init(&gntdev_dmabuf->u.exp.refcount);
391
392 gntdev_dmabuf->priv = args->dmabuf_priv;
393 gntdev_dmabuf->nr_pages = args->count;
394 gntdev_dmabuf->pages = args->pages;
395 gntdev_dmabuf->u.exp.priv = args->priv;
396 gntdev_dmabuf->u.exp.map = args->map;
397
398 exp_info.exp_name = KBUILD_MODNAME;
399 if (args->dev->driver && args->dev->driver->owner)
400 exp_info.owner = args->dev->driver->owner;
401 else
402 exp_info.owner = THIS_MODULE;
403 exp_info.ops = &dmabuf_exp_ops;
404 exp_info.size = args->count << PAGE_SHIFT;
405 exp_info.flags = O_RDWR;
406 exp_info.priv = gntdev_dmabuf;
407
408 gntdev_dmabuf->dmabuf = dma_buf_export(&exp_info);
409 if (IS_ERR(gntdev_dmabuf->dmabuf)) {
410 ret = PTR_ERR(gntdev_dmabuf->dmabuf);
411 gntdev_dmabuf->dmabuf = NULL;
412 goto fail;
413 }
414
415 ret = dma_buf_fd(gntdev_dmabuf->dmabuf, O_CLOEXEC);
416 if (ret < 0)
417 goto fail;
418
419 gntdev_dmabuf->fd = ret;
420 args->fd = ret;
421
422 pr_debug("Exporting DMA buffer with fd %d\n", ret);
423
424 mutex_lock(&args->dmabuf_priv->lock);
425 list_add(&gntdev_dmabuf->next, &args->dmabuf_priv->exp_list);
426 mutex_unlock(&args->dmabuf_priv->lock);
427 return 0;
428
429fail:
430 if (gntdev_dmabuf->dmabuf)
431 dma_buf_put(gntdev_dmabuf->dmabuf);
432 kfree(gntdev_dmabuf);
433 return ret;
434}
435
436static struct gntdev_grant_map *
437dmabuf_exp_alloc_backing_storage(struct gntdev_priv *priv, int dmabuf_flags,
438 int count)
439{
440 struct gntdev_grant_map *map;
441
442 if (unlikely(count <= 0))
443 return ERR_PTR(-EINVAL);
444
445 if ((dmabuf_flags & GNTDEV_DMA_FLAG_WC) &&
446 (dmabuf_flags & GNTDEV_DMA_FLAG_COHERENT)) {
447 pr_debug("Wrong dma-buf flags: 0x%x\n", dmabuf_flags);
448 return ERR_PTR(-EINVAL);
449 }
450
451 map = gntdev_alloc_map(priv, count, dmabuf_flags);
452 if (!map)
453 return ERR_PTR(-ENOMEM);
454
455 if (unlikely(gntdev_account_mapped_pages(count))) {
456 pr_debug("can't map %d pages: over limit\n", count);
457 gntdev_put_map(NULL, map);
458 return ERR_PTR(-ENOMEM);
459 }
460 return map;
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300461}
462
463static int dmabuf_exp_from_refs(struct gntdev_priv *priv, int flags,
464 int count, u32 domid, u32 *refs, u32 *fd)
465{
Oleksandr Andrushchenkoa240d6e2018-07-20 12:01:49 +0300466 struct gntdev_grant_map *map;
467 struct gntdev_dmabuf_export_args args;
468 int i, ret;
469
470 map = dmabuf_exp_alloc_backing_storage(priv, flags, count);
471 if (IS_ERR(map))
472 return PTR_ERR(map);
473
474 for (i = 0; i < count; i++) {
475 map->grants[i].domid = domid;
476 map->grants[i].ref = refs[i];
477 }
478
479 mutex_lock(&priv->lock);
480 gntdev_add_map(priv, map);
481 mutex_unlock(&priv->lock);
482
483 map->flags |= GNTMAP_host_map;
484#if defined(CONFIG_X86)
485 map->flags |= GNTMAP_device_map;
486#endif
487
488 ret = gntdev_map_grant_pages(map);
489 if (ret < 0)
490 goto out;
491
492 args.priv = priv;
493 args.map = map;
494 args.dev = priv->dma_dev;
495 args.dmabuf_priv = priv->dmabuf_priv;
496 args.count = map->count;
497 args.pages = map->pages;
498 args.fd = -1; /* Shut up unnecessary gcc warning for i386 */
499
500 ret = dmabuf_exp_from_pages(&args);
501 if (ret < 0)
502 goto out;
503
504 *fd = args.fd;
505 return 0;
506
507out:
508 dmabuf_exp_remove_map(priv, map);
509 return ret;
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300510}
511
512/* DMA buffer import support. */
513
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +0300514static int
515dmabuf_imp_grant_foreign_access(struct page **pages, u32 *refs,
516 int count, int domid)
517{
518 grant_ref_t priv_gref_head;
519 int i, ret;
520
521 ret = gnttab_alloc_grant_references(count, &priv_gref_head);
522 if (ret < 0) {
523 pr_debug("Cannot allocate grant references, ret %d\n", ret);
524 return ret;
525 }
526
527 for (i = 0; i < count; i++) {
528 int cur_ref;
529
530 cur_ref = gnttab_claim_grant_reference(&priv_gref_head);
531 if (cur_ref < 0) {
532 ret = cur_ref;
533 pr_debug("Cannot claim grant reference, ret %d\n", ret);
534 goto out;
535 }
536
537 gnttab_grant_foreign_access_ref(cur_ref, domid,
538 xen_page_to_gfn(pages[i]), 0);
539 refs[i] = cur_ref;
540 }
541
542 return 0;
543
544out:
545 gnttab_free_grant_references(priv_gref_head);
546 return ret;
547}
548
549static void dmabuf_imp_end_foreign_access(u32 *refs, int count)
550{
551 int i;
552
553 for (i = 0; i < count; i++)
554 if (refs[i] != GRANT_INVALID_REF)
555 gnttab_end_foreign_access(refs[i], 0, 0UL);
556}
557
558static void dmabuf_imp_free_storage(struct gntdev_dmabuf *gntdev_dmabuf)
559{
560 kfree(gntdev_dmabuf->pages);
561 kfree(gntdev_dmabuf->u.imp.refs);
562 kfree(gntdev_dmabuf);
563}
564
565static struct gntdev_dmabuf *dmabuf_imp_alloc_storage(int count)
566{
567 struct gntdev_dmabuf *gntdev_dmabuf;
568 int i;
569
570 gntdev_dmabuf = kzalloc(sizeof(*gntdev_dmabuf), GFP_KERNEL);
571 if (!gntdev_dmabuf)
Colin Ian King2789e832018-07-31 15:02:25 +0100572 goto fail_no_free;
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +0300573
574 gntdev_dmabuf->u.imp.refs = kcalloc(count,
575 sizeof(gntdev_dmabuf->u.imp.refs[0]),
576 GFP_KERNEL);
577 if (!gntdev_dmabuf->u.imp.refs)
578 goto fail;
579
580 gntdev_dmabuf->pages = kcalloc(count,
581 sizeof(gntdev_dmabuf->pages[0]),
582 GFP_KERNEL);
583 if (!gntdev_dmabuf->pages)
584 goto fail;
585
586 gntdev_dmabuf->nr_pages = count;
587
588 for (i = 0; i < count; i++)
589 gntdev_dmabuf->u.imp.refs[i] = GRANT_INVALID_REF;
590
591 return gntdev_dmabuf;
592
593fail:
594 dmabuf_imp_free_storage(gntdev_dmabuf);
Colin Ian King2789e832018-07-31 15:02:25 +0100595fail_no_free:
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +0300596 return ERR_PTR(-ENOMEM);
597}
598
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300599static struct gntdev_dmabuf *
600dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, struct device *dev,
601 int fd, int count, int domid)
602{
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +0300603 struct gntdev_dmabuf *gntdev_dmabuf, *ret;
604 struct dma_buf *dma_buf;
605 struct dma_buf_attachment *attach;
606 struct sg_table *sgt;
607 struct sg_page_iter sg_iter;
608 int i;
609
610 dma_buf = dma_buf_get(fd);
611 if (IS_ERR(dma_buf))
612 return ERR_CAST(dma_buf);
613
614 gntdev_dmabuf = dmabuf_imp_alloc_storage(count);
615 if (IS_ERR(gntdev_dmabuf)) {
616 ret = gntdev_dmabuf;
617 goto fail_put;
618 }
619
620 gntdev_dmabuf->priv = priv;
621 gntdev_dmabuf->fd = fd;
622
623 attach = dma_buf_attach(dma_buf, dev);
624 if (IS_ERR(attach)) {
625 ret = ERR_CAST(attach);
626 goto fail_free_obj;
627 }
628
629 gntdev_dmabuf->u.imp.attach = attach;
630
631 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
632 if (IS_ERR(sgt)) {
633 ret = ERR_CAST(sgt);
634 goto fail_detach;
635 }
636
637 /* Check number of pages that imported buffer has. */
638 if (attach->dmabuf->size != gntdev_dmabuf->nr_pages << PAGE_SHIFT) {
639 ret = ERR_PTR(-EINVAL);
640 pr_debug("DMA buffer has %zu pages, user-space expects %d\n",
641 attach->dmabuf->size, gntdev_dmabuf->nr_pages);
642 goto fail_unmap;
643 }
644
645 gntdev_dmabuf->u.imp.sgt = sgt;
646
647 /* Now convert sgt to array of pages and check for page validity. */
648 i = 0;
649 for_each_sg_page(sgt->sgl, &sg_iter, sgt->nents, 0) {
650 struct page *page = sg_page_iter_page(&sg_iter);
651 /*
652 * Check if page is valid: this can happen if we are given
653 * a page from VRAM or other resources which are not backed
654 * by a struct page.
655 */
656 if (!pfn_valid(page_to_pfn(page))) {
657 ret = ERR_PTR(-EINVAL);
658 goto fail_unmap;
659 }
660
661 gntdev_dmabuf->pages[i++] = page;
662 }
663
664 ret = ERR_PTR(dmabuf_imp_grant_foreign_access(gntdev_dmabuf->pages,
665 gntdev_dmabuf->u.imp.refs,
666 count, domid));
667 if (IS_ERR(ret))
668 goto fail_end_access;
669
670 pr_debug("Imported DMA buffer with fd %d\n", fd);
671
672 mutex_lock(&priv->lock);
673 list_add(&gntdev_dmabuf->next, &priv->imp_list);
674 mutex_unlock(&priv->lock);
675
676 return gntdev_dmabuf;
677
678fail_end_access:
679 dmabuf_imp_end_foreign_access(gntdev_dmabuf->u.imp.refs, count);
680fail_unmap:
681 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
682fail_detach:
683 dma_buf_detach(dma_buf, attach);
684fail_free_obj:
685 dmabuf_imp_free_storage(gntdev_dmabuf);
686fail_put:
687 dma_buf_put(dma_buf);
688 return ret;
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300689}
690
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +0300691/*
692 * Find the hyper dma-buf by its file descriptor and remove
693 * it from the buffer's list.
694 */
695static struct gntdev_dmabuf *
696dmabuf_imp_find_unlink(struct gntdev_dmabuf_priv *priv, int fd)
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300697{
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +0300698 struct gntdev_dmabuf *q, *gntdev_dmabuf, *ret = ERR_PTR(-ENOENT);
699
700 mutex_lock(&priv->lock);
701 list_for_each_entry_safe(gntdev_dmabuf, q, &priv->imp_list, next) {
702 if (gntdev_dmabuf->fd == fd) {
703 pr_debug("Found gntdev_dmabuf in the import list\n");
704 ret = gntdev_dmabuf;
705 list_del(&gntdev_dmabuf->next);
706 break;
707 }
708 }
709 mutex_unlock(&priv->lock);
710 return ret;
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300711}
712
713static int dmabuf_imp_release(struct gntdev_dmabuf_priv *priv, u32 fd)
714{
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +0300715 struct gntdev_dmabuf *gntdev_dmabuf;
716 struct dma_buf_attachment *attach;
717 struct dma_buf *dma_buf;
718
719 gntdev_dmabuf = dmabuf_imp_find_unlink(priv, fd);
720 if (IS_ERR(gntdev_dmabuf))
721 return PTR_ERR(gntdev_dmabuf);
722
723 pr_debug("Releasing DMA buffer with fd %d\n", fd);
724
725 dmabuf_imp_end_foreign_access(gntdev_dmabuf->u.imp.refs,
726 gntdev_dmabuf->nr_pages);
727
728 attach = gntdev_dmabuf->u.imp.attach;
729
730 if (gntdev_dmabuf->u.imp.sgt)
731 dma_buf_unmap_attachment(attach, gntdev_dmabuf->u.imp.sgt,
732 DMA_BIDIRECTIONAL);
733 dma_buf = attach->dmabuf;
734 dma_buf_detach(attach->dmabuf, attach);
735 dma_buf_put(dma_buf);
736
737 dmabuf_imp_free_storage(gntdev_dmabuf);
738 return 0;
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300739}
740
741/* DMA buffer IOCTL support. */
742
743long gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_priv *priv, int use_ptemod,
744 struct ioctl_gntdev_dmabuf_exp_from_refs __user *u)
745{
746 struct ioctl_gntdev_dmabuf_exp_from_refs op;
747 u32 *refs;
748 long ret;
749
750 if (use_ptemod) {
751 pr_debug("Cannot provide dma-buf: use_ptemode %d\n",
752 use_ptemod);
753 return -EINVAL;
754 }
755
756 if (copy_from_user(&op, u, sizeof(op)) != 0)
757 return -EFAULT;
758
759 if (unlikely(op.count <= 0))
760 return -EINVAL;
761
762 refs = kcalloc(op.count, sizeof(*refs), GFP_KERNEL);
763 if (!refs)
764 return -ENOMEM;
765
766 if (copy_from_user(refs, u->refs, sizeof(*refs) * op.count) != 0) {
767 ret = -EFAULT;
768 goto out;
769 }
770
771 ret = dmabuf_exp_from_refs(priv, op.flags, op.count,
772 op.domid, refs, &op.fd);
773 if (ret)
774 goto out;
775
776 if (copy_to_user(u, &op, sizeof(op)) != 0)
777 ret = -EFAULT;
778
779out:
780 kfree(refs);
781 return ret;
782}
783
784long gntdev_ioctl_dmabuf_exp_wait_released(struct gntdev_priv *priv,
785 struct ioctl_gntdev_dmabuf_exp_wait_released __user *u)
786{
787 struct ioctl_gntdev_dmabuf_exp_wait_released op;
788
789 if (copy_from_user(&op, u, sizeof(op)) != 0)
790 return -EFAULT;
791
792 return dmabuf_exp_wait_released(priv->dmabuf_priv, op.fd,
793 op.wait_to_ms);
794}
795
796long gntdev_ioctl_dmabuf_imp_to_refs(struct gntdev_priv *priv,
797 struct ioctl_gntdev_dmabuf_imp_to_refs __user *u)
798{
799 struct ioctl_gntdev_dmabuf_imp_to_refs op;
800 struct gntdev_dmabuf *gntdev_dmabuf;
801 long ret;
802
803 if (copy_from_user(&op, u, sizeof(op)) != 0)
804 return -EFAULT;
805
806 if (unlikely(op.count <= 0))
807 return -EINVAL;
808
809 gntdev_dmabuf = dmabuf_imp_to_refs(priv->dmabuf_priv,
810 priv->dma_dev, op.fd,
811 op.count, op.domid);
812 if (IS_ERR(gntdev_dmabuf))
813 return PTR_ERR(gntdev_dmabuf);
814
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +0300815 if (copy_to_user(u->refs, gntdev_dmabuf->u.imp.refs,
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300816 sizeof(*u->refs) * op.count) != 0) {
817 ret = -EFAULT;
818 goto out_release;
819 }
820 return 0;
821
822out_release:
823 dmabuf_imp_release(priv->dmabuf_priv, op.fd);
824 return ret;
825}
826
827long gntdev_ioctl_dmabuf_imp_release(struct gntdev_priv *priv,
828 struct ioctl_gntdev_dmabuf_imp_release __user *u)
829{
830 struct ioctl_gntdev_dmabuf_imp_release op;
831
832 if (copy_from_user(&op, u, sizeof(op)) != 0)
833 return -EFAULT;
834
835 return dmabuf_imp_release(priv->dmabuf_priv, op.fd);
836}
837
838struct gntdev_dmabuf_priv *gntdev_dmabuf_init(void)
839{
840 struct gntdev_dmabuf_priv *priv;
841
842 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
843 if (!priv)
844 return ERR_PTR(-ENOMEM);
845
Oleksandr Andrushchenkoa240d6e2018-07-20 12:01:49 +0300846 mutex_init(&priv->lock);
847 INIT_LIST_HEAD(&priv->exp_list);
848 INIT_LIST_HEAD(&priv->exp_wait_list);
Oleksandr Andrushchenkobf8dc552018-07-20 12:01:50 +0300849 INIT_LIST_HEAD(&priv->imp_list);
Oleksandr Andrushchenkoa240d6e2018-07-20 12:01:49 +0300850
Oleksandr Andrushchenko932d6562018-07-20 12:01:48 +0300851 return priv;
852}
853
854void gntdev_dmabuf_fini(struct gntdev_dmabuf_priv *priv)
855{
856 kfree(priv);
857}