xref: /linux/drivers/gpu/drm/virtio/virtgpu_object.c (revision 15a1fbdcfb519c2bd291ed01c6c94e0b89537a77)
1 /*
2  * Copyright (C) 2015 Red Hat, Inc.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <linux/dma-mapping.h>
27 #include <linux/moduleparam.h>
28 
29 #include "virtgpu_drv.h"
30 
31 static int virtio_gpu_virglrenderer_workaround = 1;
32 module_param_named(virglhack, virtio_gpu_virglrenderer_workaround, int, 0400);
33 
34 static int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
35 				       uint32_t *resid)
36 {
37 	if (virtio_gpu_virglrenderer_workaround) {
38 		/*
39 		 * Hack to avoid re-using resource IDs.
40 		 *
41 		 * virglrenderer versions up to (and including) 0.7.0
42 		 * can't deal with that.  virglrenderer commit
43 		 * "f91a9dd35715 Fix unlinking resources from hash
44 		 * table." (Feb 2019) fixes the bug.
45 		 */
46 		static int handle;
47 		handle++;
48 		*resid = handle + 1;
49 	} else {
50 		int handle = ida_alloc(&vgdev->resource_ida, GFP_KERNEL);
51 		if (handle < 0)
52 			return handle;
53 		*resid = handle + 1;
54 	}
55 	return 0;
56 }
57 
58 static void virtio_gpu_resource_id_put(struct virtio_gpu_device *vgdev, uint32_t id)
59 {
60 	if (!virtio_gpu_virglrenderer_workaround) {
61 		ida_free(&vgdev->resource_ida, id - 1);
62 	}
63 }
64 
65 void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
66 {
67 	struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
68 
69 	if (bo->pages) {
70 		if (bo->mapped) {
71 			dma_unmap_sg(vgdev->vdev->dev.parent,
72 				     bo->pages->sgl, bo->mapped,
73 				     DMA_TO_DEVICE);
74 			bo->mapped = 0;
75 		}
76 		sg_free_table(bo->pages);
77 		bo->pages = NULL;
78 		drm_gem_shmem_unpin(&bo->base.base);
79 	}
80 	virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
81 	drm_gem_shmem_free_object(&bo->base.base);
82 }
83 
84 static void virtio_gpu_free_object(struct drm_gem_object *obj)
85 {
86 	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
87 	struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
88 
89 	if (bo->created) {
90 		virtio_gpu_cmd_unref_resource(vgdev, bo);
91 		virtio_gpu_notify(vgdev);
92 		/* completion handler calls virtio_gpu_cleanup_object() */
93 		return;
94 	}
95 	virtio_gpu_cleanup_object(bo);
96 }
97 
98 static const struct drm_gem_object_funcs virtio_gpu_shmem_funcs = {
99 	.free = virtio_gpu_free_object,
100 	.open = virtio_gpu_gem_object_open,
101 	.close = virtio_gpu_gem_object_close,
102 
103 	.print_info = drm_gem_shmem_print_info,
104 	.pin = drm_gem_shmem_pin,
105 	.unpin = drm_gem_shmem_unpin,
106 	.get_sg_table = drm_gem_shmem_get_sg_table,
107 	.vmap = drm_gem_shmem_vmap,
108 	.vunmap = drm_gem_shmem_vunmap,
109 	.mmap = drm_gem_shmem_mmap,
110 };
111 
112 bool virtio_gpu_is_shmem(struct drm_gem_object *obj)
113 {
114 	return obj->funcs == &virtio_gpu_shmem_funcs;
115 }
116 
117 struct drm_gem_object *virtio_gpu_create_object(struct drm_device *dev,
118 						size_t size)
119 {
120 	struct virtio_gpu_object *bo;
121 
122 	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
123 	if (!bo)
124 		return NULL;
125 
126 	bo->base.base.funcs = &virtio_gpu_shmem_funcs;
127 	return &bo->base.base;
128 }
129 
130 static int virtio_gpu_object_shmem_init(struct virtio_gpu_device *vgdev,
131 					struct virtio_gpu_object *bo,
132 					struct virtio_gpu_mem_entry **ents,
133 					unsigned int *nents)
134 {
135 	bool use_dma_api = !virtio_has_iommu_quirk(vgdev->vdev);
136 	struct scatterlist *sg;
137 	int si, ret;
138 
139 	ret = drm_gem_shmem_pin(&bo->base.base);
140 	if (ret < 0)
141 		return -EINVAL;
142 
143 	bo->pages = drm_gem_shmem_get_sg_table(&bo->base.base);
144 	if (!bo->pages) {
145 		drm_gem_shmem_unpin(&bo->base.base);
146 		return -EINVAL;
147 	}
148 
149 	if (use_dma_api) {
150 		bo->mapped = dma_map_sg(vgdev->vdev->dev.parent,
151 					bo->pages->sgl, bo->pages->nents,
152 					DMA_TO_DEVICE);
153 		*nents = bo->mapped;
154 	} else {
155 		*nents = bo->pages->nents;
156 	}
157 
158 	*ents = kmalloc_array(*nents, sizeof(struct virtio_gpu_mem_entry),
159 			      GFP_KERNEL);
160 	if (!(*ents)) {
161 		DRM_ERROR("failed to allocate ent list\n");
162 		return -ENOMEM;
163 	}
164 
165 	for_each_sg(bo->pages->sgl, sg, *nents, si) {
166 		(*ents)[si].addr = cpu_to_le64(use_dma_api
167 					       ? sg_dma_address(sg)
168 					       : sg_phys(sg));
169 		(*ents)[si].length = cpu_to_le32(sg->length);
170 		(*ents)[si].padding = 0;
171 	}
172 	return 0;
173 }
174 
175 int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
176 			     struct virtio_gpu_object_params *params,
177 			     struct virtio_gpu_object **bo_ptr,
178 			     struct virtio_gpu_fence *fence)
179 {
180 	struct virtio_gpu_object_array *objs = NULL;
181 	struct drm_gem_shmem_object *shmem_obj;
182 	struct virtio_gpu_object *bo;
183 	struct virtio_gpu_mem_entry *ents;
184 	unsigned int nents;
185 	int ret;
186 
187 	*bo_ptr = NULL;
188 
189 	params->size = roundup(params->size, PAGE_SIZE);
190 	shmem_obj = drm_gem_shmem_create(vgdev->ddev, params->size);
191 	if (IS_ERR(shmem_obj))
192 		return PTR_ERR(shmem_obj);
193 	bo = gem_to_virtio_gpu_obj(&shmem_obj->base);
194 
195 	ret = virtio_gpu_resource_id_get(vgdev, &bo->hw_res_handle);
196 	if (ret < 0)
197 		goto err_free_gem;
198 
199 	bo->dumb = params->dumb;
200 
201 	if (fence) {
202 		ret = -ENOMEM;
203 		objs = virtio_gpu_array_alloc(1);
204 		if (!objs)
205 			goto err_put_id;
206 		virtio_gpu_array_add_obj(objs, &bo->base.base);
207 
208 		ret = virtio_gpu_array_lock_resv(objs);
209 		if (ret != 0)
210 			goto err_put_objs;
211 	}
212 
213 	if (params->virgl) {
214 		virtio_gpu_cmd_resource_create_3d(vgdev, bo, params,
215 						  objs, fence);
216 	} else {
217 		virtio_gpu_cmd_create_resource(vgdev, bo, params,
218 					       objs, fence);
219 	}
220 
221 	ret = virtio_gpu_object_shmem_init(vgdev, bo, &ents, &nents);
222 	if (ret != 0) {
223 		virtio_gpu_free_object(&shmem_obj->base);
224 		return ret;
225 	}
226 
227 	ret = virtio_gpu_object_attach(vgdev, bo, ents, nents);
228 	if (ret != 0) {
229 		virtio_gpu_free_object(&shmem_obj->base);
230 		return ret;
231 	}
232 
233 	virtio_gpu_notify(vgdev);
234 	*bo_ptr = bo;
235 	return 0;
236 
237 err_put_objs:
238 	virtio_gpu_array_put_free(objs);
239 err_put_id:
240 	virtio_gpu_resource_id_put(vgdev, bo->hw_res_handle);
241 err_free_gem:
242 	drm_gem_shmem_free_object(&shmem_obj->base);
243 	return ret;
244 }
245