Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * dma-bufs for virtio exported objects
0004  *
0005  * Copyright (C) 2020 Google, Inc.
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/virtio_dma_buf.h>
0010 
0011 /**
0012  * virtio_dma_buf_export - Creates a new dma-buf for a virtio exported object
0013  * @exp_info: [in] see dma_buf_export(). ops MUST refer to a dma_buf_ops
0014  *  struct embedded in a virtio_dma_buf_ops.
0015  *
0016  * This wraps dma_buf_export() to allow virtio drivers to create a dma-buf
0017  * for an virtio exported object that can be queried by other virtio drivers
0018  * for the object's UUID.
0019  */
0020 struct dma_buf *virtio_dma_buf_export
0021     (const struct dma_buf_export_info *exp_info)
0022 {
0023     const struct virtio_dma_buf_ops *virtio_ops =
0024         container_of(exp_info->ops,
0025                  const struct virtio_dma_buf_ops, ops);
0026 
0027     if (!exp_info->ops ||
0028         exp_info->ops->attach != &virtio_dma_buf_attach ||
0029         !virtio_ops->get_uuid) {
0030         return ERR_PTR(-EINVAL);
0031     }
0032 
0033     return dma_buf_export(exp_info);
0034 }
0035 EXPORT_SYMBOL(virtio_dma_buf_export);
0036 
0037 /**
0038  * virtio_dma_buf_attach - mandatory attach callback for virtio dma-bufs
0039  */
0040 int virtio_dma_buf_attach(struct dma_buf *dma_buf,
0041               struct dma_buf_attachment *attach)
0042 {
0043     int ret;
0044     const struct virtio_dma_buf_ops *ops =
0045         container_of(dma_buf->ops,
0046                  const struct virtio_dma_buf_ops, ops);
0047 
0048     if (ops->device_attach) {
0049         ret = ops->device_attach(dma_buf, attach);
0050         if (ret)
0051             return ret;
0052     }
0053     return 0;
0054 }
0055 EXPORT_SYMBOL(virtio_dma_buf_attach);
0056 
0057 /**
0058  * is_virtio_dma_buf - returns true if the given dma-buf is a virtio dma-buf
0059  * @dma_buf: buffer to query
0060  */
0061 bool is_virtio_dma_buf(struct dma_buf *dma_buf)
0062 {
0063     return dma_buf->ops->attach == &virtio_dma_buf_attach;
0064 }
0065 EXPORT_SYMBOL(is_virtio_dma_buf);
0066 
0067 /**
0068  * virtio_dma_buf_get_uuid - gets a virtio dma-buf's exported object's uuid
0069  * @dma_buf: [in] buffer to query
0070  * @uuid: [out] the uuid
0071  *
0072  * Returns: 0 on success, negative on failure.
0073  */
0074 int virtio_dma_buf_get_uuid(struct dma_buf *dma_buf,
0075                 uuid_t *uuid)
0076 {
0077     const struct virtio_dma_buf_ops *ops =
0078         container_of(dma_buf->ops,
0079                  const struct virtio_dma_buf_ops, ops);
0080 
0081     if (!is_virtio_dma_buf(dma_buf))
0082         return -EINVAL;
0083 
0084     return ops->get_uuid(dma_buf, uuid);
0085 }
0086 EXPORT_SYMBOL(virtio_dma_buf_get_uuid);
0087 
0088 MODULE_LICENSE("GPL");
0089 MODULE_IMPORT_NS(DMA_BUF);