gntdev-dmabuf.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xen dma-buf functionality for gntdev.
  4. *
  5. * Copyright (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/errno.h>
  9. #include <linux/slab.h>
  10. #include <linux/types.h>
  11. #include <linux/uaccess.h>
  12. #include <xen/xen.h>
  13. #include <xen/grant_table.h>
  14. #include "gntdev-common.h"
  15. #include "gntdev-dmabuf.h"
  16. struct gntdev_dmabuf_priv {
  17. /* List of exported DMA buffers. */
  18. struct list_head exp_list;
  19. /* List of wait objects. */
  20. struct list_head exp_wait_list;
  21. /* This is the lock which protects dma_buf_xxx lists. */
  22. struct mutex lock;
  23. };
  24. /* DMA buffer export support. */
  25. /* Implementation of wait for exported DMA buffer to be released. */
  26. static int dmabuf_exp_wait_released(struct gntdev_dmabuf_priv *priv, int fd,
  27. int wait_to_ms)
  28. {
  29. return -EINVAL;
  30. }
  31. static int dmabuf_exp_from_refs(struct gntdev_priv *priv, int flags,
  32. int count, u32 domid, u32 *refs, u32 *fd)
  33. {
  34. *fd = -1;
  35. return -EINVAL;
  36. }
  37. /* DMA buffer import support. */
  38. static struct gntdev_dmabuf *
  39. dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, struct device *dev,
  40. int fd, int count, int domid)
  41. {
  42. return ERR_PTR(-ENOMEM);
  43. }
  44. static u32 *dmabuf_imp_get_refs(struct gntdev_dmabuf *gntdev_dmabuf)
  45. {
  46. return NULL;
  47. }
  48. static int dmabuf_imp_release(struct gntdev_dmabuf_priv *priv, u32 fd)
  49. {
  50. return -EINVAL;
  51. }
  52. /* DMA buffer IOCTL support. */
  53. long gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_priv *priv, int use_ptemod,
  54. struct ioctl_gntdev_dmabuf_exp_from_refs __user *u)
  55. {
  56. struct ioctl_gntdev_dmabuf_exp_from_refs op;
  57. u32 *refs;
  58. long ret;
  59. if (use_ptemod) {
  60. pr_debug("Cannot provide dma-buf: use_ptemode %d\n",
  61. use_ptemod);
  62. return -EINVAL;
  63. }
  64. if (copy_from_user(&op, u, sizeof(op)) != 0)
  65. return -EFAULT;
  66. if (unlikely(op.count <= 0))
  67. return -EINVAL;
  68. refs = kcalloc(op.count, sizeof(*refs), GFP_KERNEL);
  69. if (!refs)
  70. return -ENOMEM;
  71. if (copy_from_user(refs, u->refs, sizeof(*refs) * op.count) != 0) {
  72. ret = -EFAULT;
  73. goto out;
  74. }
  75. ret = dmabuf_exp_from_refs(priv, op.flags, op.count,
  76. op.domid, refs, &op.fd);
  77. if (ret)
  78. goto out;
  79. if (copy_to_user(u, &op, sizeof(op)) != 0)
  80. ret = -EFAULT;
  81. out:
  82. kfree(refs);
  83. return ret;
  84. }
  85. long gntdev_ioctl_dmabuf_exp_wait_released(struct gntdev_priv *priv,
  86. struct ioctl_gntdev_dmabuf_exp_wait_released __user *u)
  87. {
  88. struct ioctl_gntdev_dmabuf_exp_wait_released op;
  89. if (copy_from_user(&op, u, sizeof(op)) != 0)
  90. return -EFAULT;
  91. return dmabuf_exp_wait_released(priv->dmabuf_priv, op.fd,
  92. op.wait_to_ms);
  93. }
  94. long gntdev_ioctl_dmabuf_imp_to_refs(struct gntdev_priv *priv,
  95. struct ioctl_gntdev_dmabuf_imp_to_refs __user *u)
  96. {
  97. struct ioctl_gntdev_dmabuf_imp_to_refs op;
  98. struct gntdev_dmabuf *gntdev_dmabuf;
  99. long ret;
  100. if (copy_from_user(&op, u, sizeof(op)) != 0)
  101. return -EFAULT;
  102. if (unlikely(op.count <= 0))
  103. return -EINVAL;
  104. gntdev_dmabuf = dmabuf_imp_to_refs(priv->dmabuf_priv,
  105. priv->dma_dev, op.fd,
  106. op.count, op.domid);
  107. if (IS_ERR(gntdev_dmabuf))
  108. return PTR_ERR(gntdev_dmabuf);
  109. if (copy_to_user(u->refs, dmabuf_imp_get_refs(gntdev_dmabuf),
  110. sizeof(*u->refs) * op.count) != 0) {
  111. ret = -EFAULT;
  112. goto out_release;
  113. }
  114. return 0;
  115. out_release:
  116. dmabuf_imp_release(priv->dmabuf_priv, op.fd);
  117. return ret;
  118. }
  119. long gntdev_ioctl_dmabuf_imp_release(struct gntdev_priv *priv,
  120. struct ioctl_gntdev_dmabuf_imp_release __user *u)
  121. {
  122. struct ioctl_gntdev_dmabuf_imp_release op;
  123. if (copy_from_user(&op, u, sizeof(op)) != 0)
  124. return -EFAULT;
  125. return dmabuf_imp_release(priv->dmabuf_priv, op.fd);
  126. }
  127. struct gntdev_dmabuf_priv *gntdev_dmabuf_init(void)
  128. {
  129. struct gntdev_dmabuf_priv *priv;
  130. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  131. if (!priv)
  132. return ERR_PTR(-ENOMEM);
  133. return priv;
  134. }
  135. void gntdev_dmabuf_fini(struct gntdev_dmabuf_priv *priv)
  136. {
  137. kfree(priv);
  138. }