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