virtio_dma_buf.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * dma-bufs for virtio exported objects
  4. *
  5. * Copyright (C) 2020 Google, Inc.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/virtio_dma_buf.h>
  9. /**
  10. * virtio_dma_buf_export - Creates a new dma-buf for a virtio exported object
  11. * @exp_info: [in] see dma_buf_export(). ops MUST refer to a dma_buf_ops
  12. * struct embedded in a virtio_dma_buf_ops.
  13. *
  14. * This wraps dma_buf_export() to allow virtio drivers to create a dma-buf
  15. * for an virtio exported object that can be queried by other virtio drivers
  16. * for the object's UUID.
  17. */
  18. struct dma_buf *virtio_dma_buf_export
  19. (const struct dma_buf_export_info *exp_info)
  20. {
  21. const struct virtio_dma_buf_ops *virtio_ops =
  22. container_of(exp_info->ops,
  23. const struct virtio_dma_buf_ops, ops);
  24. if (!exp_info->ops ||
  25. exp_info->ops->attach != &virtio_dma_buf_attach ||
  26. !virtio_ops->get_uuid) {
  27. return ERR_PTR(-EINVAL);
  28. }
  29. return dma_buf_export(exp_info);
  30. }
  31. EXPORT_SYMBOL(virtio_dma_buf_export);
  32. /**
  33. * virtio_dma_buf_attach - mandatory attach callback for virtio dma-bufs
  34. */
  35. int virtio_dma_buf_attach(struct dma_buf *dma_buf,
  36. struct dma_buf_attachment *attach)
  37. {
  38. int ret;
  39. const struct virtio_dma_buf_ops *ops =
  40. container_of(dma_buf->ops,
  41. const struct virtio_dma_buf_ops, ops);
  42. if (ops->device_attach) {
  43. ret = ops->device_attach(dma_buf, attach);
  44. if (ret)
  45. return ret;
  46. }
  47. return 0;
  48. }
  49. EXPORT_SYMBOL(virtio_dma_buf_attach);
  50. /**
  51. * is_virtio_dma_buf - returns true if the given dma-buf is a virtio dma-buf
  52. * @dma_buf: buffer to query
  53. */
  54. bool is_virtio_dma_buf(struct dma_buf *dma_buf)
  55. {
  56. return dma_buf->ops->attach == &virtio_dma_buf_attach;
  57. }
  58. EXPORT_SYMBOL(is_virtio_dma_buf);
  59. /**
  60. * virtio_dma_buf_get_uuid - gets a virtio dma-buf's exported object's uuid
  61. * @dma_buf: [in] buffer to query
  62. * @uuid: [out] the uuid
  63. *
  64. * Returns: 0 on success, negative on failure.
  65. */
  66. int virtio_dma_buf_get_uuid(struct dma_buf *dma_buf,
  67. uuid_t *uuid)
  68. {
  69. const struct virtio_dma_buf_ops *ops =
  70. container_of(dma_buf->ops,
  71. const struct virtio_dma_buf_ops, ops);
  72. if (!is_virtio_dma_buf(dma_buf))
  73. return -EINVAL;
  74. return ops->get_uuid(dma_buf, uuid);
  75. }
  76. EXPORT_SYMBOL(virtio_dma_buf_get_uuid);
  77. MODULE_DESCRIPTION("dma-bufs for virtio exported objects");
  78. MODULE_LICENSE("GPL");
  79. MODULE_IMPORT_NS(DMA_BUF);