18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * dma-bufs for virtio exported objects 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2020 Google, Inc. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/module.h> 98c2ecf20Sopenharmony_ci#include <linux/virtio_dma_buf.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci/** 128c2ecf20Sopenharmony_ci * virtio_dma_buf_export - Creates a new dma-buf for a virtio exported object 138c2ecf20Sopenharmony_ci * @exp_info: [in] see dma_buf_export(). ops MUST refer to a dma_buf_ops 148c2ecf20Sopenharmony_ci * struct embedded in a virtio_dma_buf_ops. 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * This wraps dma_buf_export() to allow virtio drivers to create a dma-buf 178c2ecf20Sopenharmony_ci * for an virtio exported object that can be queried by other virtio drivers 188c2ecf20Sopenharmony_ci * for the object's UUID. 198c2ecf20Sopenharmony_ci */ 208c2ecf20Sopenharmony_cistruct dma_buf *virtio_dma_buf_export 218c2ecf20Sopenharmony_ci (const struct dma_buf_export_info *exp_info) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci const struct virtio_dma_buf_ops *virtio_ops = 248c2ecf20Sopenharmony_ci container_of(exp_info->ops, 258c2ecf20Sopenharmony_ci const struct virtio_dma_buf_ops, ops); 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci if (!exp_info->ops || 288c2ecf20Sopenharmony_ci exp_info->ops->attach != &virtio_dma_buf_attach || 298c2ecf20Sopenharmony_ci !virtio_ops->get_uuid) { 308c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 318c2ecf20Sopenharmony_ci } 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci return dma_buf_export(exp_info); 348c2ecf20Sopenharmony_ci} 358c2ecf20Sopenharmony_ciEXPORT_SYMBOL(virtio_dma_buf_export); 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/** 388c2ecf20Sopenharmony_ci * virtio_dma_buf_attach - mandatory attach callback for virtio dma-bufs 398c2ecf20Sopenharmony_ci */ 408c2ecf20Sopenharmony_ciint virtio_dma_buf_attach(struct dma_buf *dma_buf, 418c2ecf20Sopenharmony_ci struct dma_buf_attachment *attach) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci int ret; 448c2ecf20Sopenharmony_ci const struct virtio_dma_buf_ops *ops = 458c2ecf20Sopenharmony_ci container_of(dma_buf->ops, 468c2ecf20Sopenharmony_ci const struct virtio_dma_buf_ops, ops); 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci if (ops->device_attach) { 498c2ecf20Sopenharmony_ci ret = ops->device_attach(dma_buf, attach); 508c2ecf20Sopenharmony_ci if (ret) 518c2ecf20Sopenharmony_ci return ret; 528c2ecf20Sopenharmony_ci } 538c2ecf20Sopenharmony_ci return 0; 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(virtio_dma_buf_attach); 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/** 588c2ecf20Sopenharmony_ci * is_virtio_dma_buf - returns true if the given dma-buf is a virtio dma-buf 598c2ecf20Sopenharmony_ci * @dma_buf: buffer to query 608c2ecf20Sopenharmony_ci */ 618c2ecf20Sopenharmony_cibool is_virtio_dma_buf(struct dma_buf *dma_buf) 628c2ecf20Sopenharmony_ci{ 638c2ecf20Sopenharmony_ci return dma_buf->ops->attach == &virtio_dma_buf_attach; 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ciEXPORT_SYMBOL(is_virtio_dma_buf); 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/** 688c2ecf20Sopenharmony_ci * virtio_dma_buf_get_uuid - gets a virtio dma-buf's exported object's uuid 698c2ecf20Sopenharmony_ci * @dma_buf: [in] buffer to query 708c2ecf20Sopenharmony_ci * @uuid: [out] the uuid 718c2ecf20Sopenharmony_ci * 728c2ecf20Sopenharmony_ci * Returns: 0 on success, negative on failure. 738c2ecf20Sopenharmony_ci */ 748c2ecf20Sopenharmony_ciint virtio_dma_buf_get_uuid(struct dma_buf *dma_buf, 758c2ecf20Sopenharmony_ci uuid_t *uuid) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci const struct virtio_dma_buf_ops *ops = 788c2ecf20Sopenharmony_ci container_of(dma_buf->ops, 798c2ecf20Sopenharmony_ci const struct virtio_dma_buf_ops, ops); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci if (!is_virtio_dma_buf(dma_buf)) 828c2ecf20Sopenharmony_ci return -EINVAL; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci return ops->get_uuid(dma_buf, uuid); 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ciEXPORT_SYMBOL(virtio_dma_buf_get_uuid); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 89