xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/virtio/virtgpu_object.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1*41ec0267Sriastradh /*	$NetBSD: virtgpu_object.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $	*/
2efa246c0Sriastradh 
3efa246c0Sriastradh /*
4efa246c0Sriastradh  * Copyright (C) 2015 Red Hat, Inc.
5efa246c0Sriastradh  * All Rights Reserved.
6efa246c0Sriastradh  *
7efa246c0Sriastradh  * Permission is hereby granted, free of charge, to any person obtaining
8efa246c0Sriastradh  * a copy of this software and associated documentation files (the
9efa246c0Sriastradh  * "Software"), to deal in the Software without restriction, including
10efa246c0Sriastradh  * without limitation the rights to use, copy, modify, merge, publish,
11efa246c0Sriastradh  * distribute, sublicense, and/or sell copies of the Software, and to
12efa246c0Sriastradh  * permit persons to whom the Software is furnished to do so, subject to
13efa246c0Sriastradh  * the following conditions:
14efa246c0Sriastradh  *
15efa246c0Sriastradh  * The above copyright notice and this permission notice (including the
16efa246c0Sriastradh  * next paragraph) shall be included in all copies or substantial
17efa246c0Sriastradh  * portions of the Software.
18efa246c0Sriastradh  *
19efa246c0Sriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20efa246c0Sriastradh  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21efa246c0Sriastradh  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22efa246c0Sriastradh  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23efa246c0Sriastradh  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24efa246c0Sriastradh  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25efa246c0Sriastradh  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26efa246c0Sriastradh  */
27efa246c0Sriastradh 
28efa246c0Sriastradh #include <sys/cdefs.h>
29*41ec0267Sriastradh __KERNEL_RCSID(0, "$NetBSD: virtgpu_object.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $");
30*41ec0267Sriastradh 
31*41ec0267Sriastradh #include <linux/moduleparam.h>
32efa246c0Sriastradh 
33efa246c0Sriastradh #include "virtgpu_drv.h"
34efa246c0Sriastradh 
35*41ec0267Sriastradh static int virtio_gpu_virglrenderer_workaround = 1;
36*41ec0267Sriastradh module_param_named(virglhack, virtio_gpu_virglrenderer_workaround, int, 0400);
37*41ec0267Sriastradh 
virtio_gpu_resource_id_get(struct virtio_gpu_device * vgdev,uint32_t * resid)38*41ec0267Sriastradh static int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
39*41ec0267Sriastradh 				       uint32_t *resid)
40efa246c0Sriastradh {
41*41ec0267Sriastradh 	if (virtio_gpu_virglrenderer_workaround) {
42*41ec0267Sriastradh 		/*
43*41ec0267Sriastradh 		 * Hack to avoid re-using resource IDs.
44*41ec0267Sriastradh 		 *
45*41ec0267Sriastradh 		 * virglrenderer versions up to (and including) 0.7.0
46*41ec0267Sriastradh 		 * can't deal with that.  virglrenderer commit
47*41ec0267Sriastradh 		 * "f91a9dd35715 Fix unlinking resources from hash
48*41ec0267Sriastradh 		 * table." (Feb 2019) fixes the bug.
49*41ec0267Sriastradh 		 */
50*41ec0267Sriastradh 		static int handle;
51*41ec0267Sriastradh 		handle++;
52*41ec0267Sriastradh 		*resid = handle + 1;
53*41ec0267Sriastradh 	} else {
54*41ec0267Sriastradh 		int handle = ida_alloc(&vgdev->resource_ida, GFP_KERNEL);
55*41ec0267Sriastradh 		if (handle < 0)
56*41ec0267Sriastradh 			return handle;
57*41ec0267Sriastradh 		*resid = handle + 1;
58*41ec0267Sriastradh 	}
59*41ec0267Sriastradh 	return 0;
60efa246c0Sriastradh }
61efa246c0Sriastradh 
virtio_gpu_resource_id_put(struct virtio_gpu_device * vgdev,uint32_t id)62*41ec0267Sriastradh static void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t id)
63efa246c0Sriastradh {
64*41ec0267Sriastradh 	if (!virtio_gpu_virglrenderer_workaround) {
65*41ec0267Sriastradh 		ida_free(&vgdev->resource_ida, id - 1);
66*41ec0267Sriastradh 	}
67*41ec0267Sriastradh }
68efa246c0Sriastradh 
virtio_gpu_free_object(struct drm_gem_object * obj)69*41ec0267Sriastradh static void virtio_gpu_free_object(struct drm_gem_object *obj)
70*41ec0267Sriastradh {
71*41ec0267Sriastradh 	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
72*41ec0267Sriastradh 	struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
73efa246c0Sriastradh 
74*41ec0267Sriastradh 	if (bo->pages)
75*41ec0267Sriastradh 		virtio_gpu_object_detach(vgdev, bo);
76*41ec0267Sriastradh 	if (bo->created)
77*41ec0267Sriastradh 		virtio_gpu_cmd_unref_resource(vgdev, bo->hw_res_handle);
78*41ec0267Sriastradh 	virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
79*41ec0267Sriastradh 
80*41ec0267Sriastradh 	drm_gem_shmem_free_object(obj);
81*41ec0267Sriastradh }
82*41ec0267Sriastradh 
83*41ec0267Sriastradh static const struct drm_gem_object_funcs virtio_gpu_gem_funcs = {
84*41ec0267Sriastradh 	.free = virtio_gpu_free_object,
85*41ec0267Sriastradh 	.open = virtio_gpu_gem_object_open,
86*41ec0267Sriastradh 	.close = virtio_gpu_gem_object_close,
87*41ec0267Sriastradh 
88*41ec0267Sriastradh 	.print_info = drm_gem_shmem_print_info,
89*41ec0267Sriastradh 	.pin = drm_gem_shmem_pin,
90*41ec0267Sriastradh 	.unpin = drm_gem_shmem_unpin,
91*41ec0267Sriastradh 	.get_sg_table = drm_gem_shmem_get_sg_table,
92*41ec0267Sriastradh 	.vmap = drm_gem_shmem_vmap,
93*41ec0267Sriastradh 	.vunmap = drm_gem_shmem_vunmap,
94*41ec0267Sriastradh 	.mmap = &drm_gem_shmem_mmap,
95*41ec0267Sriastradh };
96*41ec0267Sriastradh 
virtio_gpu_create_object(struct drm_device * dev,size_t size)97*41ec0267Sriastradh struct drm_gem_object *virtio_gpu_create_object(struct drm_device *dev,
98*41ec0267Sriastradh 						size_t size)
99*41ec0267Sriastradh {
100*41ec0267Sriastradh 	struct virtio_gpu_object *bo;
101*41ec0267Sriastradh 
102*41ec0267Sriastradh 	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
103*41ec0267Sriastradh 	if (!bo)
104*41ec0267Sriastradh 		return NULL;
105*41ec0267Sriastradh 
106*41ec0267Sriastradh 	bo->base.base.funcs = &virtio_gpu_gem_funcs;
107*41ec0267Sriastradh 	return &bo->base.base;
108efa246c0Sriastradh }
109efa246c0Sriastradh 
virtio_gpu_object_create(struct virtio_gpu_device * vgdev,struct virtio_gpu_object_params * params,struct virtio_gpu_object ** bo_ptr,struct virtio_gpu_fence * fence)110efa246c0Sriastradh int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
111*41ec0267Sriastradh 			     struct virtio_gpu_object_params *params,
112*41ec0267Sriastradh 			     struct virtio_gpu_object **bo_ptr,
113*41ec0267Sriastradh 			     struct virtio_gpu_fence *fence)
114efa246c0Sriastradh {
115*41ec0267Sriastradh 	struct virtio_gpu_object_array *objs = NULL;
116*41ec0267Sriastradh 	struct drm_gem_shmem_object *shmem_obj;
117efa246c0Sriastradh 	struct virtio_gpu_object *bo;
118efa246c0Sriastradh 	int ret;
119efa246c0Sriastradh 
120efa246c0Sriastradh 	*bo_ptr = NULL;
121efa246c0Sriastradh 
122*41ec0267Sriastradh 	params->size = roundup(params->size, PAGE_SIZE);
123*41ec0267Sriastradh 	shmem_obj = drm_gem_shmem_create(vgdev->ddev, params->size);
124*41ec0267Sriastradh 	if (IS_ERR(shmem_obj))
125*41ec0267Sriastradh 		return PTR_ERR(shmem_obj);
126*41ec0267Sriastradh 	bo = gem_to_virtio_gpu_obj(&shmem_obj->base);
127efa246c0Sriastradh 
128*41ec0267Sriastradh 	ret = virtio_gpu_resource_id_get(vgdev, &bo->hw_res_handle);
129*41ec0267Sriastradh 	if (ret < 0)
130*41ec0267Sriastradh 		goto err_free_gem;
131*41ec0267Sriastradh 
132*41ec0267Sriastradh 	bo->dumb = params->dumb;
133*41ec0267Sriastradh 
134*41ec0267Sriastradh 	if (fence) {
135*41ec0267Sriastradh 		ret = -ENOMEM;
136*41ec0267Sriastradh 		objs = virtio_gpu_array_alloc(1);
137*41ec0267Sriastradh 		if (!objs)
138*41ec0267Sriastradh 			goto err_put_id;
139*41ec0267Sriastradh 		virtio_gpu_array_add_obj(objs, &bo->base.base);
140*41ec0267Sriastradh 
141*41ec0267Sriastradh 		ret = virtio_gpu_array_lock_resv(objs);
142*41ec0267Sriastradh 		if (ret != 0)
143*41ec0267Sriastradh 			goto err_put_objs;
144*41ec0267Sriastradh 	}
145*41ec0267Sriastradh 
146*41ec0267Sriastradh 	if (params->virgl) {
147*41ec0267Sriastradh 		virtio_gpu_cmd_resource_create_3d(vgdev, bo, params,
148*41ec0267Sriastradh 						  objs, fence);
149*41ec0267Sriastradh 	} else {
150*41ec0267Sriastradh 		virtio_gpu_cmd_create_resource(vgdev, bo, params,
151*41ec0267Sriastradh 					       objs, fence);
152*41ec0267Sriastradh 	}
153*41ec0267Sriastradh 
154*41ec0267Sriastradh 	ret = virtio_gpu_object_attach(vgdev, bo, NULL);
155efa246c0Sriastradh 	if (ret != 0) {
156*41ec0267Sriastradh 		virtio_gpu_free_object(&shmem_obj->base);
157efa246c0Sriastradh 		return ret;
158efa246c0Sriastradh 	}
159efa246c0Sriastradh 
160efa246c0Sriastradh 	*bo_ptr = bo;
161efa246c0Sriastradh 	return 0;
162*41ec0267Sriastradh 
163*41ec0267Sriastradh err_put_objs:
164*41ec0267Sriastradh 	virtio_gpu_array_put_free(objs);
165*41ec0267Sriastradh err_put_id:
166*41ec0267Sriastradh 	virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
167*41ec0267Sriastradh err_free_gem:
168*41ec0267Sriastradh 	drm_gem_shmem_free_object(&shmem_obj->base);
169*41ec0267Sriastradh 	return ret;
170efa246c0Sriastradh }
171