virtio_dma_buf.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. !virtio_ops->get_uuid) {
  26. return ERR_PTR(-EINVAL);
  27. }
  28. if (!(IS_ENABLED(CONFIG_CFI_CLANG) && IS_ENABLED(CONFIG_MODULES)) &&
  29. exp_info->ops->attach != &virtio_dma_buf_attach)
  30. return ERR_PTR(-EINVAL);
  31. return dma_buf_export(exp_info);
  32. }
  33. EXPORT_SYMBOL(virtio_dma_buf_export);
  34. /**
  35. * virtio_dma_buf_attach - mandatory attach callback for virtio dma-bufs
  36. */
  37. int virtio_dma_buf_attach(struct dma_buf *dma_buf,
  38. struct dma_buf_attachment *attach)
  39. {
  40. int ret;
  41. const struct virtio_dma_buf_ops *ops =
  42. container_of(dma_buf->ops,
  43. const struct virtio_dma_buf_ops, ops);
  44. if (ops->device_attach) {
  45. ret = ops->device_attach(dma_buf, attach);
  46. if (ret)
  47. return ret;
  48. }
  49. return 0;
  50. }
  51. EXPORT_SYMBOL(virtio_dma_buf_attach);
  52. /**
  53. * is_virtio_dma_buf - returns true if the given dma-buf is a virtio dma-buf
  54. * @dma_buf: buffer to query
  55. */
  56. bool is_virtio_dma_buf(struct dma_buf *dma_buf)
  57. {
  58. if (IS_ENABLED(CONFIG_CFI_CLANG) && IS_ENABLED(CONFIG_MODULES))
  59. return true;
  60. return dma_buf->ops->attach == &virtio_dma_buf_attach;
  61. }
  62. EXPORT_SYMBOL(is_virtio_dma_buf);
  63. /**
  64. * virtio_dma_buf_get_uuid - gets a virtio dma-buf's exported object's uuid
  65. * @dma_buf: [in] buffer to query
  66. * @uuid: [out] the uuid
  67. *
  68. * Returns: 0 on success, negative on failure.
  69. */
  70. int virtio_dma_buf_get_uuid(struct dma_buf *dma_buf,
  71. uuid_t *uuid)
  72. {
  73. const struct virtio_dma_buf_ops *ops =
  74. container_of(dma_buf->ops,
  75. const struct virtio_dma_buf_ops, ops);
  76. if (!is_virtio_dma_buf(dma_buf))
  77. return -EINVAL;
  78. return ops->get_uuid(dma_buf, uuid);
  79. }
  80. EXPORT_SYMBOL(virtio_dma_buf_get_uuid);
  81. MODULE_LICENSE("GPL");
  82. MODULE_IMPORT_NS(DMA_BUF);