2013-09-09 08:02:56 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Red Hat, Inc.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial
|
|
|
|
* portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-02-07 15:46:37 +08:00
|
|
|
#include <linux/dma-mapping.h>
|
2019-08-28 16:55:16 +08:00
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
|
2013-09-09 08:02:56 +08:00
|
|
|
#include "virtgpu_drv.h"
|
|
|
|
|
2019-08-22 18:26:14 +08:00
|
|
|
static int virtio_gpu_virglrenderer_workaround = 1;
|
|
|
|
module_param_named(virglhack, virtio_gpu_virglrenderer_workaround, int, 0400);
|
|
|
|
|
2020-09-24 08:32:05 +08:00
|
|
|
int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev, uint32_t *resid)
|
2018-10-19 14:18:47 +08:00
|
|
|
{
|
2019-08-22 18:26:14 +08:00
|
|
|
if (virtio_gpu_virglrenderer_workaround) {
|
|
|
|
/*
|
|
|
|
* Hack to avoid re-using resource IDs.
|
|
|
|
*
|
|
|
|
* virglrenderer versions up to (and including) 0.7.0
|
|
|
|
* can't deal with that. virglrenderer commit
|
|
|
|
* "f91a9dd35715 Fix unlinking resources from hash
|
|
|
|
* table." (Feb 2019) fixes the bug.
|
|
|
|
*/
|
2020-02-21 06:53:19 +08:00
|
|
|
static atomic_t seqno = ATOMIC_INIT(0);
|
|
|
|
int handle = atomic_inc_return(&seqno);
|
2019-08-22 18:26:14 +08:00
|
|
|
*resid = handle + 1;
|
|
|
|
} else {
|
|
|
|
int handle = ida_alloc(&vgdev->resource_ida, GFP_KERNEL);
|
|
|
|
if (handle < 0)
|
|
|
|
return handle;
|
|
|
|
*resid = handle + 1;
|
|
|
|
}
|
2018-10-31 00:53:51 +08:00
|
|
|
return 0;
|
2018-10-19 14:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t id)
|
|
|
|
{
|
2019-08-22 18:26:14 +08:00
|
|
|
if (!virtio_gpu_virglrenderer_workaround) {
|
|
|
|
ida_free(&vgdev->resource_ida, id - 1);
|
|
|
|
}
|
2018-10-19 14:18:47 +08:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:46:36 +08:00
|
|
|
void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
|
|
|
|
{
|
|
|
|
struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
|
|
|
|
|
2020-03-05 09:32:12 +08:00
|
|
|
virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
|
|
|
|
if (virtio_gpu_is_shmem(bo)) {
|
|
|
|
struct virtio_gpu_object_shmem *shmem = to_virtio_gpu_shmem(bo);
|
|
|
|
|
|
|
|
if (shmem->pages) {
|
|
|
|
if (shmem->mapped) {
|
2020-04-28 19:10:19 +08:00
|
|
|
dma_unmap_sgtable(vgdev->vdev->dev.parent,
|
|
|
|
shmem->pages, DMA_TO_DEVICE, 0);
|
2020-03-05 09:32:12 +08:00
|
|
|
shmem->mapped = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sg_free_table(shmem->pages);
|
2020-07-22 13:18:51 +08:00
|
|
|
kfree(shmem->pages);
|
2020-03-05 09:32:12 +08:00
|
|
|
shmem->pages = NULL;
|
2021-11-08 17:31:49 +08:00
|
|
|
drm_gem_shmem_unpin(&bo->base);
|
2020-02-07 15:46:37 +08:00
|
|
|
}
|
2020-03-05 09:32:12 +08:00
|
|
|
|
2021-11-08 17:31:49 +08:00
|
|
|
drm_gem_shmem_free(&bo->base);
|
2020-09-24 08:32:07 +08:00
|
|
|
} else if (virtio_gpu_is_vram(bo)) {
|
|
|
|
struct virtio_gpu_object_vram *vram = to_virtio_gpu_vram(bo);
|
|
|
|
|
|
|
|
spin_lock(&vgdev->host_visible_lock);
|
|
|
|
if (drm_mm_node_allocated(&vram->vram_node))
|
|
|
|
drm_mm_remove_node(&vram->vram_node);
|
|
|
|
|
|
|
|
spin_unlock(&vgdev->host_visible_lock);
|
|
|
|
|
|
|
|
drm_gem_free_mmap_offset(&vram->base.base.base);
|
|
|
|
drm_gem_object_release(&vram->base.base.base);
|
|
|
|
kfree(vram);
|
2020-02-07 15:46:37 +08:00
|
|
|
}
|
2020-02-07 15:46:36 +08:00
|
|
|
}
|
|
|
|
|
2019-08-29 18:32:57 +08:00
|
|
|
static void virtio_gpu_free_object(struct drm_gem_object *obj)
|
2013-09-09 08:02:56 +08:00
|
|
|
{
|
2019-08-29 18:32:57 +08:00
|
|
|
struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
|
|
|
|
struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
|
2013-09-09 08:02:56 +08:00
|
|
|
|
2020-02-07 15:46:36 +08:00
|
|
|
if (bo->created) {
|
|
|
|
virtio_gpu_cmd_unref_resource(vgdev, bo);
|
2020-02-14 20:55:35 +08:00
|
|
|
virtio_gpu_notify(vgdev);
|
2020-02-07 15:46:36 +08:00
|
|
|
/* completion handler calls virtio_gpu_cleanup_object() */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
virtio_gpu_cleanup_object(bo);
|
2013-09-09 08:02:56 +08:00
|
|
|
}
|
|
|
|
|
2020-02-27 08:25:55 +08:00
|
|
|
static const struct drm_gem_object_funcs virtio_gpu_shmem_funcs = {
|
2019-08-29 18:32:57 +08:00
|
|
|
.free = virtio_gpu_free_object,
|
|
|
|
.open = virtio_gpu_gem_object_open,
|
|
|
|
.close = virtio_gpu_gem_object_close,
|
2021-11-08 17:31:48 +08:00
|
|
|
.print_info = drm_gem_shmem_object_print_info,
|
2020-09-23 18:21:55 +08:00
|
|
|
.export = virtgpu_gem_prime_export,
|
2021-11-08 17:31:48 +08:00
|
|
|
.pin = drm_gem_shmem_object_pin,
|
|
|
|
.unpin = drm_gem_shmem_object_unpin,
|
|
|
|
.get_sg_table = drm_gem_shmem_object_get_sg_table,
|
|
|
|
.vmap = drm_gem_shmem_object_vmap,
|
|
|
|
.vunmap = drm_gem_shmem_object_vunmap,
|
|
|
|
.mmap = drm_gem_shmem_object_mmap,
|
2022-02-09 23:56:33 +08:00
|
|
|
.vm_ops = &drm_gem_shmem_vm_ops,
|
2019-08-29 18:32:57 +08:00
|
|
|
};
|
|
|
|
|
2020-03-05 09:32:12 +08:00
|
|
|
bool virtio_gpu_is_shmem(struct virtio_gpu_object *bo)
|
2020-02-27 08:25:55 +08:00
|
|
|
{
|
2020-03-05 09:32:12 +08:00
|
|
|
return bo->base.base.funcs == &virtio_gpu_shmem_funcs;
|
2020-02-27 08:25:55 +08:00
|
|
|
}
|
|
|
|
|
2019-08-29 18:32:57 +08:00
|
|
|
struct drm_gem_object *virtio_gpu_create_object(struct drm_device *dev,
|
|
|
|
size_t size)
|
2013-09-09 08:02:56 +08:00
|
|
|
{
|
2020-03-19 18:04:21 +08:00
|
|
|
struct virtio_gpu_object_shmem *shmem;
|
|
|
|
struct drm_gem_shmem_object *dshmem;
|
2019-08-29 18:32:57 +08:00
|
|
|
|
2020-03-19 18:04:21 +08:00
|
|
|
shmem = kzalloc(sizeof(*shmem), GFP_KERNEL);
|
|
|
|
if (!shmem)
|
2021-11-30 17:52:55 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2013-09-09 08:02:56 +08:00
|
|
|
|
2020-03-19 18:04:21 +08:00
|
|
|
dshmem = &shmem->base.base;
|
|
|
|
dshmem->base.funcs = &virtio_gpu_shmem_funcs;
|
|
|
|
return &dshmem->base;
|
2013-09-09 08:02:56 +08:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:46:38 +08:00
|
|
|
static int virtio_gpu_object_shmem_init(struct virtio_gpu_device *vgdev,
|
|
|
|
struct virtio_gpu_object *bo,
|
|
|
|
struct virtio_gpu_mem_entry **ents,
|
|
|
|
unsigned int *nents)
|
|
|
|
{
|
2020-06-25 07:17:04 +08:00
|
|
|
bool use_dma_api = !virtio_has_dma_quirk(vgdev->vdev);
|
2020-03-05 09:32:11 +08:00
|
|
|
struct virtio_gpu_object_shmem *shmem = to_virtio_gpu_shmem(bo);
|
2020-02-07 15:46:38 +08:00
|
|
|
struct scatterlist *sg;
|
|
|
|
int si, ret;
|
|
|
|
|
2021-11-08 17:31:49 +08:00
|
|
|
ret = drm_gem_shmem_pin(&bo->base);
|
2020-02-07 15:46:38 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-06-16 07:05:00 +08:00
|
|
|
/*
|
|
|
|
* virtio_gpu uses drm_gem_shmem_get_sg_table instead of
|
|
|
|
* drm_gem_shmem_get_pages_sgt because virtio has it's own set of
|
|
|
|
* dma-ops. This is discouraged for other drivers, but should be fine
|
|
|
|
* since virtio_gpu doesn't support dma-buf import from other devices.
|
|
|
|
*/
|
2021-11-08 17:31:49 +08:00
|
|
|
shmem->pages = drm_gem_shmem_get_sg_table(&bo->base);
|
2020-03-05 09:32:11 +08:00
|
|
|
if (!shmem->pages) {
|
2021-11-08 17:31:49 +08:00
|
|
|
drm_gem_shmem_unpin(&bo->base);
|
2020-02-07 15:46:38 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_dma_api) {
|
2020-04-28 19:10:19 +08:00
|
|
|
ret = dma_map_sgtable(vgdev->vdev->dev.parent,
|
|
|
|
shmem->pages, DMA_TO_DEVICE, 0);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
*nents = shmem->mapped = shmem->pages->nents;
|
2020-02-07 15:46:38 +08:00
|
|
|
} else {
|
2020-04-28 19:10:19 +08:00
|
|
|
*nents = shmem->pages->orig_nents;
|
2020-02-07 15:46:38 +08:00
|
|
|
}
|
|
|
|
|
2020-11-05 09:47:44 +08:00
|
|
|
*ents = kvmalloc_array(*nents,
|
|
|
|
sizeof(struct virtio_gpu_mem_entry),
|
|
|
|
GFP_KERNEL);
|
2020-02-07 15:46:38 +08:00
|
|
|
if (!(*ents)) {
|
|
|
|
DRM_ERROR("failed to allocate ent list\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2020-04-28 19:10:19 +08:00
|
|
|
if (use_dma_api) {
|
|
|
|
for_each_sgtable_dma_sg(shmem->pages, sg, si) {
|
|
|
|
(*ents)[si].addr = cpu_to_le64(sg_dma_address(sg));
|
|
|
|
(*ents)[si].length = cpu_to_le32(sg_dma_len(sg));
|
|
|
|
(*ents)[si].padding = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for_each_sgtable_sg(shmem->pages, sg, si) {
|
|
|
|
(*ents)[si].addr = cpu_to_le64(sg_phys(sg));
|
|
|
|
(*ents)[si].length = cpu_to_le32(sg->length);
|
|
|
|
(*ents)[si].padding = 0;
|
|
|
|
}
|
2020-02-07 15:46:38 +08:00
|
|
|
}
|
2020-04-28 19:10:19 +08:00
|
|
|
|
2020-02-07 15:46:38 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-09 08:02:56 +08:00
|
|
|
int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
|
2019-03-18 19:33:29 +08:00
|
|
|
struct virtio_gpu_object_params *params,
|
2019-03-18 19:33:32 +08:00
|
|
|
struct virtio_gpu_object **bo_ptr,
|
|
|
|
struct virtio_gpu_fence *fence)
|
2013-09-09 08:02:56 +08:00
|
|
|
{
|
2019-08-29 18:32:52 +08:00
|
|
|
struct virtio_gpu_object_array *objs = NULL;
|
2019-08-29 18:32:57 +08:00
|
|
|
struct drm_gem_shmem_object *shmem_obj;
|
2013-09-09 08:02:56 +08:00
|
|
|
struct virtio_gpu_object *bo;
|
2020-02-07 15:46:38 +08:00
|
|
|
struct virtio_gpu_mem_entry *ents;
|
|
|
|
unsigned int nents;
|
2013-09-09 08:02:56 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
*bo_ptr = NULL;
|
|
|
|
|
2019-08-29 18:32:57 +08:00
|
|
|
params->size = roundup(params->size, PAGE_SIZE);
|
|
|
|
shmem_obj = drm_gem_shmem_create(vgdev->ddev, params->size);
|
|
|
|
if (IS_ERR(shmem_obj))
|
|
|
|
return PTR_ERR(shmem_obj);
|
|
|
|
bo = gem_to_virtio_gpu_obj(&shmem_obj->base);
|
2013-09-09 08:02:56 +08:00
|
|
|
|
2018-10-31 00:53:51 +08:00
|
|
|
ret = virtio_gpu_resource_id_get(vgdev, &bo->hw_res_handle);
|
2019-08-29 18:32:52 +08:00
|
|
|
if (ret < 0)
|
|
|
|
goto err_free_gem;
|
|
|
|
|
2019-03-18 19:33:32 +08:00
|
|
|
bo->dumb = params->dumb;
|
|
|
|
|
2019-08-29 18:32:52 +08:00
|
|
|
if (fence) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
objs = virtio_gpu_array_alloc(1);
|
|
|
|
if (!objs)
|
|
|
|
goto err_put_id;
|
2019-08-29 18:32:57 +08:00
|
|
|
virtio_gpu_array_add_obj(objs, &bo->base.base);
|
2019-08-29 18:32:52 +08:00
|
|
|
|
|
|
|
ret = virtio_gpu_array_lock_resv(objs);
|
|
|
|
if (ret != 0)
|
|
|
|
goto err_put_objs;
|
|
|
|
}
|
|
|
|
|
2020-09-24 08:31:56 +08:00
|
|
|
ret = virtio_gpu_object_shmem_init(vgdev, bo, &ents, &nents);
|
|
|
|
if (ret != 0) {
|
2021-03-05 23:18:19 +08:00
|
|
|
virtio_gpu_array_put_free(objs);
|
2020-09-24 08:31:56 +08:00
|
|
|
virtio_gpu_free_object(&shmem_obj->base);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-24 08:32:13 +08:00
|
|
|
if (params->blob) {
|
2021-04-13 13:26:14 +08:00
|
|
|
if (params->blob_mem == VIRTGPU_BLOB_MEM_GUEST)
|
|
|
|
bo->guest_blob = true;
|
|
|
|
|
2020-09-24 08:32:13 +08:00
|
|
|
virtio_gpu_cmd_resource_create_blob(vgdev, bo, params,
|
|
|
|
ents, nents);
|
|
|
|
} else if (params->virgl) {
|
2019-08-29 18:32:52 +08:00
|
|
|
virtio_gpu_cmd_resource_create_3d(vgdev, bo, params,
|
|
|
|
objs, fence);
|
2020-09-24 08:31:56 +08:00
|
|
|
virtio_gpu_object_attach(vgdev, bo, ents, nents);
|
2019-03-18 19:33:32 +08:00
|
|
|
} else {
|
2019-08-29 18:32:52 +08:00
|
|
|
virtio_gpu_cmd_create_resource(vgdev, bo, params,
|
|
|
|
objs, fence);
|
2020-09-24 08:31:56 +08:00
|
|
|
virtio_gpu_object_attach(vgdev, bo, ents, nents);
|
2019-03-18 19:33:32 +08:00
|
|
|
}
|
2013-09-09 08:02:56 +08:00
|
|
|
|
|
|
|
*bo_ptr = bo;
|
|
|
|
return 0;
|
2019-08-29 18:32:52 +08:00
|
|
|
|
|
|
|
err_put_objs:
|
|
|
|
virtio_gpu_array_put_free(objs);
|
|
|
|
err_put_id:
|
|
|
|
virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
|
|
|
|
err_free_gem:
|
2021-11-08 17:31:49 +08:00
|
|
|
drm_gem_shmem_free(shmem_obj);
|
2019-08-29 18:32:52 +08:00
|
|
|
return ret;
|
2013-09-09 08:02:56 +08:00
|
|
|
}
|