From patchwork Thu Sep 12 09:41:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomasz Figa X-Patchwork-Id: 11142679 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 26ED716B1 for ; Thu, 12 Sep 2019 09:41:32 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0F02220856 for ; Thu, 12 Sep 2019 09:41:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0F02220856 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 942CB6EC52; Thu, 12 Sep 2019 09:41:30 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4EBC66EC52 for ; Thu, 12 Sep 2019 09:41:29 +0000 (UTC) Received: by mail-pl1-x643.google.com with SMTP id m9so11557236pls.8 for ; Thu, 12 Sep 2019 02:41:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=K8HzPj4CjTGqPIMExQrkFd76q28JYrYuyRqYSNyq/4c=; b=sbI4IorPcf2PNyx9iVTN5phnixnkb7hyu9r8AeK+jiXVOpQO7fdAAMXbQCeTVkCWAH 8Tk4zsj9N3qJSAKLyVYRtiYevU+pscrYqdr7DBbkDIUC0BMvSGP46FGoOhaKMuz4gyzr Lzzn5huSDzhjB11pH2LPX2A4ebT9lyJhzfZ++vPnnLiRWoePVeAlVowQdEBZuZ5owHjx J8DIf6vg99yzgVQ7QcrxeBAlN1E1gZI7O/hoZsQqbRjUKW7yRnL79kQgiQA3YCO+VC17 CFNyb6hrUpydyBRTDgAl3NQXM0mJR7bISisNRhm2jixVAYqoXRHNbV9DQb/zfV4eyox4 V+mw== X-Gm-Message-State: APjAAAXappqnL9hkDtR0sAPv1gyV4+ZjKopl7Hx0LSqCNdzQIwxj/DFe 9f9ovtaitkHimWHGfaCyfxK3ajB1xHyN0Q== X-Google-Smtp-Source: APXvYqzQKWP7aloYWrXlntiae1FVI/aefxFeH7mogABRe7PTrChaxwCSGskZ9qq5ospmeBNmBWjUbg== X-Received: by 2002:a17:902:7b83:: with SMTP id w3mr12476006pll.3.1568281288738; Thu, 12 Sep 2019 02:41:28 -0700 (PDT) Received: from tfiga.tok.corp.google.com ([2401:fa00:4:4:6d27:f13:a0fa:d4b6]) by smtp.gmail.com with ESMTPSA id o195sm5695859pfg.21.2019.09.12.02.41.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2019 02:41:27 -0700 (PDT) From: Tomasz Figa To: David Airlie , Gerd Hoffmann , Daniel Vetter Subject: [RFC PATCH] drm/virtio: Export resource handles via DMA-buf API Date: Thu, 12 Sep 2019 18:41:21 +0900 Message-Id: <20190912094121.228435-1-tfiga@chromium.org> X-Mailer: git-send-email 2.23.0.237.gc6a4ce50a0-goog MIME-Version: 1.0 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=K8HzPj4CjTGqPIMExQrkFd76q28JYrYuyRqYSNyq/4c=; b=krDbwbOe6wjzVfF+U/pzAmXMrxhr3mUnguYMgh1Z7TqK9V/mvbvDLpDDq2aMYgpeWB T/ZsDxX0c8C5Pdg+UWgvKE8kCigOpvO+SYxItjUi61wMEXRogSlArgu7AfUDs3G+5/Q5 aj60bXXpSg9XxqhtXX9cleM+9cHxoUU7fLFHU= X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: zachr@chromium.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, virtualization@lists.linux-foundation.org, keiichiw@chromium.org, stevensd@chromium.org, marcheu@chromium.org, Tomasz Figa , posciak@chromium.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This patch is an early RFC to judge the direction we are following in our virtualization efforts in Chrome OS. The purpose is to start a discussion on how to handle buffer sharing between multiple virtio devices. On a side note, we are also working on a virtio video decoder interface and implementation, with a V4L2 driver for Linux. Those will be posted for review in the near future as well. Any feedback will be appreciated! Thanks in advance. === With the range of use cases for virtualization expanding, there is going to be more virtio devices added to the ecosystem. Devices such as video decoders, encoders, cameras, etc. typically work together with the display and GPU in a pipeline manner, which can only be implemented efficiently by sharing the buffers between producers and consumers. Existing buffer management framework in Linux, such as the videobuf2 framework in V4L2, implements all the DMA-buf handling inside generic code and do not expose any low level information about the buffers to the drivers. To seamlessly enable buffer sharing with drivers using such frameworks, make the virtio-gpu driver expose the resource handle as the DMA address of the buffer returned from the DMA-buf mapping operation. Arguably, the resource handle is a kind of DMA address already, as it is the buffer identifier that the device needs to access the backing memory, which is exactly the same role a DMA address provides for native devices. A virtio driver that does memory management fully on its own would have code similar to following. The code is identical to what a regular driver for real hardware would do to import a DMA-buf. static int virtio_foo_get_resource_handle(struct virtio_foo *foo, struct dma_buf *dma_buf, u32 *id) { struct dma_buf_attachment *attach; struct sg_table *sgt; int ret = 0; attach = dma_buf_attach(dma_buf, foo->dev); if (IS_ERR(attach)) return PTR_ERR(attach); sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); if (IS_ERR(sgt)) { ret = PTR_ERR(sgt); goto err_detach; } if (sgt->nents != 1) { ret = -EINVAL; goto err_unmap; } *id = sg_dma_address(sgt->sgl); err_unmap: dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL); err_detach: dma_buf_detach(dma_buf, attach); return ret; } On the other hand, a virtio driver that uses an existing kernel framework to manage buffers would not need to explicitly handle anything at all, as the framework part responsible for importing DMA-bufs would already do the work. For example, a V4L2 driver using the videobuf2 framework would just call thee vb2_dma_contig_plane_dma_addr() function to get what the above open-coded function would return. Signed-off-by: Tomasz Figa --- drivers/gpu/drm/virtio/virtgpu_drv.c | 2 + drivers/gpu/drm/virtio/virtgpu_drv.h | 4 ++ drivers/gpu/drm/virtio/virtgpu_prime.c | 81 ++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.c b/drivers/gpu/drm/virtio/virtgpu_drv.c index 0fc32fa0b3c0..ac095f813134 100644 --- a/drivers/gpu/drm/virtio/virtgpu_drv.c +++ b/drivers/gpu/drm/virtio/virtgpu_drv.c @@ -210,6 +210,8 @@ static struct drm_driver driver = { #endif .prime_handle_to_fd = drm_gem_prime_handle_to_fd, .prime_fd_to_handle = drm_gem_prime_fd_to_handle, + .gem_prime_export = virtgpu_gem_prime_export, + .gem_prime_import = virtgpu_gem_prime_import, .gem_prime_get_sg_table = virtgpu_gem_prime_get_sg_table, .gem_prime_import_sg_table = virtgpu_gem_prime_import_sg_table, .gem_prime_vmap = virtgpu_gem_prime_vmap, diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h index e28829661724..687cfce91885 100644 --- a/drivers/gpu/drm/virtio/virtgpu_drv.h +++ b/drivers/gpu/drm/virtio/virtgpu_drv.h @@ -367,6 +367,10 @@ void virtio_gpu_object_free_sg_table(struct virtio_gpu_object *bo); int virtio_gpu_object_wait(struct virtio_gpu_object *bo, bool no_wait); /* virtgpu_prime.c */ +struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj, + int flags); +struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev, + struct dma_buf *buf); struct sg_table *virtgpu_gem_prime_get_sg_table(struct drm_gem_object *obj); struct drm_gem_object *virtgpu_gem_prime_import_sg_table( struct drm_device *dev, struct dma_buf_attachment *attach, diff --git a/drivers/gpu/drm/virtio/virtgpu_prime.c b/drivers/gpu/drm/virtio/virtgpu_prime.c index dc642a884b88..562eb1a2ed5b 100644 --- a/drivers/gpu/drm/virtio/virtgpu_prime.c +++ b/drivers/gpu/drm/virtio/virtgpu_prime.c @@ -22,6 +22,9 @@ * Authors: Andreas Pokorny */ +#include +#include + #include #include "virtgpu_drv.h" @@ -30,6 +33,84 @@ * device that might share buffers with virtgpu */ +static struct sg_table * +virtgpu_gem_map_dma_buf(struct dma_buf_attachment *attach, + enum dma_data_direction dir) +{ + struct drm_gem_object *obj = attach->dmabuf->priv; + struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); + struct sg_table *sgt; + int ret; + + sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); + if (!sgt) + return ERR_PTR(-ENOMEM); + + ret = sg_alloc_table(sgt, 1, GFP_KERNEL); + if (ret) { + kfree(sgt); + return ERR_PTR(-ENOMEM); + } + + sg_dma_address(sgt->sgl) = bo->hw_res_handle; + sg_dma_len(sgt->sgl) = obj->size; + sgt->nents = 1; + + return sgt; +} + +static void virtgpu_gem_unmap_dma_buf(struct dma_buf_attachment *attach, + struct sg_table *sgt, + enum dma_data_direction dir) +{ + sg_free_table(sgt); + kfree(sgt); +} + +static const struct dma_buf_ops virtgpu_dmabuf_ops = { + .cache_sgt_mapping = true, + .attach = drm_gem_map_attach, + .detach = drm_gem_map_detach, + .map_dma_buf = virtgpu_gem_map_dma_buf, + .unmap_dma_buf = virtgpu_gem_unmap_dma_buf, + .release = drm_gem_dmabuf_release, + .mmap = drm_gem_dmabuf_mmap, + .vmap = drm_gem_dmabuf_vmap, + .vunmap = drm_gem_dmabuf_vunmap, +}; + +struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj, + int flags) +{ + struct dma_buf *buf; + + buf = drm_gem_prime_export(obj, flags); + if (!IS_ERR(buf)) + buf->ops = &virtgpu_dmabuf_ops; + + return buf; +} + +struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev, + struct dma_buf *buf) +{ + struct drm_gem_object *obj; + + if (buf->ops == &virtgpu_dmabuf_ops) { + obj = buf->priv; + if (obj->dev == dev) { + /* + * Importing dmabuf exported from our own gem increases + * refcount on gem itself instead of f_count of dmabuf. + */ + drm_gem_object_get(obj); + return obj; + } + } + + return drm_gem_prime_import(dev, buf); +} + struct sg_table *virtgpu_gem_prime_get_sg_table(struct drm_gem_object *obj) { struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);