blob: 4f855b242dfdf2724fc4f3e03b0a150e2cd283f3 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
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/virtio.h>
27#include <linux/virtio_config.h>
28
29#include <drm/drm_file.h>
30
31#include "virtgpu_drv.h"
32
33static void virtio_gpu_config_changed_work_func(struct work_struct *work)
34{
35 struct virtio_gpu_device *vgdev =
36 container_of(work, struct virtio_gpu_device,
37 config_changed_work);
38 u32 events_read, events_clear = 0;
39
40 /* read the config space */
41 virtio_cread(vgdev->vdev, struct virtio_gpu_config,
42 events_read, &events_read);
43 if (events_read & VIRTIO_GPU_EVENT_DISPLAY) {
44 if (vgdev->has_edid)
45 virtio_gpu_cmd_get_edids(vgdev);
46 virtio_gpu_cmd_get_display_info(vgdev);
47 drm_helper_hpd_irq_event(vgdev->ddev);
48 events_clear |= VIRTIO_GPU_EVENT_DISPLAY;
49 }
50 virtio_cwrite(vgdev->vdev, struct virtio_gpu_config,
51 events_clear, &events_clear);
52}
53
54static int virtio_gpu_context_create(struct virtio_gpu_device *vgdev,
55 uint32_t nlen, const char *name)
56{
57 int handle = ida_alloc(&vgdev->ctx_id_ida, GFP_KERNEL);
58
59 if (handle < 0)
60 return handle;
61 handle += 1;
62 virtio_gpu_cmd_context_create(vgdev, handle, nlen, name);
63 return handle;
64}
65
66static void virtio_gpu_context_destroy(struct virtio_gpu_device *vgdev,
67 uint32_t ctx_id)
68{
69 virtio_gpu_cmd_context_destroy(vgdev, ctx_id);
70 ida_free(&vgdev->ctx_id_ida, ctx_id - 1);
71}
72
73static void virtio_gpu_init_vq(struct virtio_gpu_queue *vgvq,
74 void (*work_func)(struct work_struct *work))
75{
76 spin_lock_init(&vgvq->qlock);
77 init_waitqueue_head(&vgvq->ack_queue);
78 INIT_WORK(&vgvq->dequeue_work, work_func);
79}
80
81static void virtio_gpu_get_capsets(struct virtio_gpu_device *vgdev,
82 int num_capsets)
83{
84 int i, ret;
85
86 vgdev->capsets = kcalloc(num_capsets,
87 sizeof(struct virtio_gpu_drv_capset),
88 GFP_KERNEL);
89 if (!vgdev->capsets) {
90 DRM_ERROR("failed to allocate cap sets\n");
91 return;
92 }
93 for (i = 0; i < num_capsets; i++) {
94 virtio_gpu_cmd_get_capset_info(vgdev, i);
95 ret = wait_event_timeout(vgdev->resp_wq,
96 vgdev->capsets[i].id > 0, 5 * HZ);
97 if (ret == 0) {
98 DRM_ERROR("timed out waiting for cap set %d\n", i);
99 spin_lock(&vgdev->display_info_lock);
100 kfree(vgdev->capsets);
101 vgdev->capsets = NULL;
102 spin_unlock(&vgdev->display_info_lock);
103 return;
104 }
105 DRM_INFO("cap set %d: id %d, max-version %d, max-size %d\n",
106 i, vgdev->capsets[i].id,
107 vgdev->capsets[i].max_version,
108 vgdev->capsets[i].max_size);
109 }
110 vgdev->num_capsets = num_capsets;
111}
112
113int virtio_gpu_init(struct drm_device *dev)
114{
115 static vq_callback_t *callbacks[] = {
116 virtio_gpu_ctrl_ack, virtio_gpu_cursor_ack
117 };
118 static const char * const names[] = { "control", "cursor" };
119
120 struct virtio_gpu_device *vgdev;
121 /* this will expand later */
122 struct virtqueue *vqs[2];
123 u32 num_scanouts, num_capsets;
124 int ret;
125
126 if (!virtio_has_feature(dev_to_virtio(dev->dev), VIRTIO_F_VERSION_1))
127 return -ENODEV;
128
129 vgdev = kzalloc(sizeof(struct virtio_gpu_device), GFP_KERNEL);
130 if (!vgdev)
131 return -ENOMEM;
132
133 vgdev->ddev = dev;
134 dev->dev_private = vgdev;
135 vgdev->vdev = dev_to_virtio(dev->dev);
136 vgdev->dev = dev->dev;
137
138 spin_lock_init(&vgdev->display_info_lock);
139 ida_init(&vgdev->ctx_id_ida);
140 ida_init(&vgdev->resource_ida);
141 init_waitqueue_head(&vgdev->resp_wq);
142 virtio_gpu_init_vq(&vgdev->ctrlq, virtio_gpu_dequeue_ctrl_func);
143 virtio_gpu_init_vq(&vgdev->cursorq, virtio_gpu_dequeue_cursor_func);
144
145 vgdev->fence_drv.context = dma_fence_context_alloc(1);
146 spin_lock_init(&vgdev->fence_drv.lock);
147 INIT_LIST_HEAD(&vgdev->fence_drv.fences);
148 INIT_LIST_HEAD(&vgdev->cap_cache);
149 INIT_WORK(&vgdev->config_changed_work,
150 virtio_gpu_config_changed_work_func);
151
152#ifdef __LITTLE_ENDIAN
153 if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_VIRGL))
154 vgdev->has_virgl_3d = true;
155 DRM_INFO("virgl 3d acceleration %s\n",
156 vgdev->has_virgl_3d ? "enabled" : "not supported by host");
157#else
158 DRM_INFO("virgl 3d acceleration not supported by guest\n");
159#endif
160 if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_EDID)) {
161 vgdev->has_edid = true;
162 DRM_INFO("EDID support available.\n");
163 }
164
165 ret = virtio_find_vqs(vgdev->vdev, 2, vqs, callbacks, names, NULL);
166 if (ret) {
167 DRM_ERROR("failed to find virt queues\n");
168 goto err_vqs;
169 }
170 vgdev->ctrlq.vq = vqs[0];
171 vgdev->cursorq.vq = vqs[1];
172 ret = virtio_gpu_alloc_vbufs(vgdev);
173 if (ret) {
174 DRM_ERROR("failed to alloc vbufs\n");
175 goto err_vbufs;
176 }
177
178 ret = virtio_gpu_ttm_init(vgdev);
179 if (ret) {
180 DRM_ERROR("failed to init ttm %d\n", ret);
181 goto err_ttm;
182 }
183
184 /* get display info */
185 virtio_cread(vgdev->vdev, struct virtio_gpu_config,
186 num_scanouts, &num_scanouts);
187 vgdev->num_scanouts = min_t(uint32_t, num_scanouts,
188 VIRTIO_GPU_MAX_SCANOUTS);
189 if (!vgdev->num_scanouts) {
190 DRM_ERROR("num_scanouts is zero\n");
191 ret = -EINVAL;
192 goto err_scanouts;
193 }
194 DRM_INFO("number of scanouts: %d\n", num_scanouts);
195
196 virtio_cread(vgdev->vdev, struct virtio_gpu_config,
197 num_capsets, &num_capsets);
198 DRM_INFO("number of cap sets: %d\n", num_capsets);
199
200 virtio_gpu_modeset_init(vgdev);
201
202 virtio_device_ready(vgdev->vdev);
203 vgdev->vqs_ready = true;
204
205 if (num_capsets)
206 virtio_gpu_get_capsets(vgdev, num_capsets);
207 if (vgdev->has_edid)
208 virtio_gpu_cmd_get_edids(vgdev);
209 virtio_gpu_cmd_get_display_info(vgdev);
210 wait_event_timeout(vgdev->resp_wq, !vgdev->display_info_pending,
211 5 * HZ);
212 return 0;
213
214err_scanouts:
215 virtio_gpu_ttm_fini(vgdev);
216err_ttm:
217 virtio_gpu_free_vbufs(vgdev);
218err_vbufs:
219 vgdev->vdev->config->del_vqs(vgdev->vdev);
220err_vqs:
221 dev->dev_private = NULL;
222 kfree(vgdev);
223 return ret;
224}
225
226static void virtio_gpu_cleanup_cap_cache(struct virtio_gpu_device *vgdev)
227{
228 struct virtio_gpu_drv_cap_cache *cache_ent, *tmp;
229
230 list_for_each_entry_safe(cache_ent, tmp, &vgdev->cap_cache, head) {
231 kfree(cache_ent->caps_cache);
232 kfree(cache_ent);
233 }
234}
235
236void virtio_gpu_deinit(struct drm_device *dev)
237{
238 struct virtio_gpu_device *vgdev = dev->dev_private;
239
240 vgdev->vqs_ready = false;
241 flush_work(&vgdev->ctrlq.dequeue_work);
242 flush_work(&vgdev->cursorq.dequeue_work);
243 flush_work(&vgdev->config_changed_work);
244 vgdev->vdev->config->reset(vgdev->vdev);
245 vgdev->vdev->config->del_vqs(vgdev->vdev);
246
247 virtio_gpu_modeset_fini(vgdev);
248 virtio_gpu_ttm_fini(vgdev);
249 virtio_gpu_free_vbufs(vgdev);
250 virtio_gpu_cleanup_cap_cache(vgdev);
251 kfree(vgdev->capsets);
252 kfree(vgdev);
253}
254
255int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)
256{
257 struct virtio_gpu_device *vgdev = dev->dev_private;
258 struct virtio_gpu_fpriv *vfpriv;
259 int id;
260 char dbgname[TASK_COMM_LEN];
261
262 /* can't create contexts without 3d renderer */
263 if (!vgdev->has_virgl_3d)
264 return 0;
265
266 /* allocate a virt GPU context for this opener */
267 vfpriv = kzalloc(sizeof(*vfpriv), GFP_KERNEL);
268 if (!vfpriv)
269 return -ENOMEM;
270
271 get_task_comm(dbgname, current);
272 id = virtio_gpu_context_create(vgdev, strlen(dbgname), dbgname);
273 if (id < 0) {
274 kfree(vfpriv);
275 return id;
276 }
277
278 vfpriv->ctx_id = id;
279 file->driver_priv = vfpriv;
280 return 0;
281}
282
283void virtio_gpu_driver_postclose(struct drm_device *dev, struct drm_file *file)
284{
285 struct virtio_gpu_device *vgdev = dev->dev_private;
286 struct virtio_gpu_fpriv *vfpriv;
287
288 if (!vgdev->has_virgl_3d)
289 return;
290
291 vfpriv = file->driver_priv;
292
293 virtio_gpu_context_destroy(vgdev, vfpriv->ctx_id);
294 kfree(vfpriv);
295 file->driver_priv = NULL;
296}