David Stevens | a030893 | 2020-08-18 16:13:41 +0900 | [diff] [blame] | 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 | |
David Stevens | 9fe2f89 | 2020-08-19 12:10:11 +0900 | [diff] [blame] | 8 | #include <linux/module.h> |
David Stevens | a030893 | 2020-08-18 16:13:41 +0900 | [diff] [blame] | 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 || |
David Stevens | a030893 | 2020-08-18 16:13:41 +0900 | [diff] [blame] | 28 | !virtio_ops->get_uuid) { |
| 29 | return ERR_PTR(-EINVAL); |
| 30 | } |
| 31 | |
Sami Tolvanen | 9194764 | 2021-01-28 11:35:05 -0800 | [diff] [blame] | 32 | if (!(IS_ENABLED(CONFIG_CFI_CLANG) && IS_ENABLED(CONFIG_MODULES)) && |
| 33 | exp_info->ops->attach != &virtio_dma_buf_attach) |
| 34 | return ERR_PTR(-EINVAL); |
| 35 | |
David Stevens | a030893 | 2020-08-18 16:13:41 +0900 | [diff] [blame] | 36 | return dma_buf_export(exp_info); |
| 37 | } |
| 38 | EXPORT_SYMBOL(virtio_dma_buf_export); |
| 39 | |
| 40 | /** |
| 41 | * virtio_dma_buf_attach - mandatory attach callback for virtio dma-bufs |
| 42 | */ |
| 43 | int virtio_dma_buf_attach(struct dma_buf *dma_buf, |
| 44 | struct dma_buf_attachment *attach) |
| 45 | { |
| 46 | int ret; |
| 47 | const struct virtio_dma_buf_ops *ops = |
| 48 | container_of(dma_buf->ops, |
| 49 | const struct virtio_dma_buf_ops, ops); |
| 50 | |
| 51 | if (ops->device_attach) { |
| 52 | ret = ops->device_attach(dma_buf, attach); |
| 53 | if (ret) |
| 54 | return ret; |
| 55 | } |
| 56 | return 0; |
| 57 | } |
| 58 | EXPORT_SYMBOL(virtio_dma_buf_attach); |
| 59 | |
| 60 | /** |
| 61 | * is_virtio_dma_buf - returns true if the given dma-buf is a virtio dma-buf |
| 62 | * @dma_buf: buffer to query |
| 63 | */ |
| 64 | bool is_virtio_dma_buf(struct dma_buf *dma_buf) |
| 65 | { |
Sami Tolvanen | 9194764 | 2021-01-28 11:35:05 -0800 | [diff] [blame] | 66 | if (IS_ENABLED(CONFIG_CFI_CLANG) && IS_ENABLED(CONFIG_MODULES)) |
| 67 | return true; |
| 68 | |
David Stevens | a030893 | 2020-08-18 16:13:41 +0900 | [diff] [blame] | 69 | return dma_buf->ops->attach == &virtio_dma_buf_attach; |
| 70 | } |
| 71 | EXPORT_SYMBOL(is_virtio_dma_buf); |
| 72 | |
| 73 | /** |
| 74 | * virtio_dma_buf_get_uuid - gets a virtio dma-buf's exported object's uuid |
| 75 | * @dma_buf: [in] buffer to query |
| 76 | * @uuid: [out] the uuid |
| 77 | * |
| 78 | * Returns: 0 on success, negative on failure. |
| 79 | */ |
| 80 | int virtio_dma_buf_get_uuid(struct dma_buf *dma_buf, |
| 81 | uuid_t *uuid) |
| 82 | { |
| 83 | const struct virtio_dma_buf_ops *ops = |
| 84 | container_of(dma_buf->ops, |
| 85 | const struct virtio_dma_buf_ops, ops); |
| 86 | |
| 87 | if (!is_virtio_dma_buf(dma_buf)) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | return ops->get_uuid(dma_buf, uuid); |
| 91 | } |
| 92 | EXPORT_SYMBOL(virtio_dma_buf_get_uuid); |
David Stevens | 9fe2f89 | 2020-08-19 12:10:11 +0900 | [diff] [blame] | 93 | |
| 94 | MODULE_LICENSE("GPL"); |