xref: /linux/drivers/gpu/drm/virtio/virtgpu_kms.c (revision 6198770a1fe01938921f8a383b777ddb881c94b6)
1dc5698e8SDave Airlie /*
2dc5698e8SDave Airlie  * Copyright (C) 2015 Red Hat, Inc.
3dc5698e8SDave Airlie  * All Rights Reserved.
4dc5698e8SDave Airlie  *
5dc5698e8SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining
6dc5698e8SDave Airlie  * a copy of this software and associated documentation files (the
7dc5698e8SDave Airlie  * "Software"), to deal in the Software without restriction, including
8dc5698e8SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
9dc5698e8SDave Airlie  * distribute, sublicense, and/or sell copies of the Software, and to
10dc5698e8SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
11dc5698e8SDave Airlie  * the following conditions:
12dc5698e8SDave Airlie  *
13dc5698e8SDave Airlie  * The above copyright notice and this permission notice (including the
14dc5698e8SDave Airlie  * next paragraph) shall be included in all copies or substantial
15dc5698e8SDave Airlie  * portions of the Software.
16dc5698e8SDave Airlie  *
17dc5698e8SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18dc5698e8SDave Airlie  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19dc5698e8SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20dc5698e8SDave Airlie  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21dc5698e8SDave Airlie  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22dc5698e8SDave Airlie  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23dc5698e8SDave Airlie  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24dc5698e8SDave Airlie  */
25dc5698e8SDave Airlie 
26dc5698e8SDave Airlie #include <linux/virtio.h>
27dc5698e8SDave Airlie #include <linux/virtio_config.h>
285038a567SStephen Rothwell #include <linux/virtio_ring.h>
29a3d63977SSam Ravnborg 
30a3d63977SSam Ravnborg #include <drm/drm_file.h>
31a3d63977SSam Ravnborg 
32dc5698e8SDave Airlie #include "virtgpu_drv.h"
33dc5698e8SDave Airlie 
34dc5698e8SDave Airlie static void virtio_gpu_config_changed_work_func(struct work_struct *work)
35dc5698e8SDave Airlie {
36dc5698e8SDave Airlie 	struct virtio_gpu_device *vgdev =
37dc5698e8SDave Airlie 		container_of(work, struct virtio_gpu_device,
38dc5698e8SDave Airlie 			     config_changed_work);
39dc5698e8SDave Airlie 	u32 events_read, events_clear = 0;
40dc5698e8SDave Airlie 
41dc5698e8SDave Airlie 	/* read the config space */
42115a71d8SMichael S. Tsirkin 	virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
43dc5698e8SDave Airlie 			events_read, &events_read);
44dc5698e8SDave Airlie 	if (events_read & VIRTIO_GPU_EVENT_DISPLAY) {
45b4b01b49SGerd Hoffmann 		if (vgdev->has_edid)
46b4b01b49SGerd Hoffmann 			virtio_gpu_cmd_get_edids(vgdev);
47dc5698e8SDave Airlie 		virtio_gpu_cmd_get_display_info(vgdev);
48234489eaSGerd Hoffmann 		virtio_gpu_notify(vgdev);
49dc5698e8SDave Airlie 		drm_helper_hpd_irq_event(vgdev->ddev);
50dc5698e8SDave Airlie 		events_clear |= VIRTIO_GPU_EVENT_DISPLAY;
51dc5698e8SDave Airlie 	}
52115a71d8SMichael S. Tsirkin 	virtio_cwrite_le(vgdev->vdev, struct virtio_gpu_config,
53dc5698e8SDave Airlie 			 events_clear, &events_clear);
54dc5698e8SDave Airlie }
55dc5698e8SDave Airlie 
56dc5698e8SDave Airlie static void virtio_gpu_init_vq(struct virtio_gpu_queue *vgvq,
57dc5698e8SDave Airlie 			       void (*work_func)(struct work_struct *work))
58dc5698e8SDave Airlie {
59dc5698e8SDave Airlie 	spin_lock_init(&vgvq->qlock);
60dc5698e8SDave Airlie 	init_waitqueue_head(&vgvq->ack_queue);
61dc5698e8SDave Airlie 	INIT_WORK(&vgvq->dequeue_work, work_func);
62dc5698e8SDave Airlie }
63dc5698e8SDave Airlie 
6462fb7a5eSGerd Hoffmann static void virtio_gpu_get_capsets(struct virtio_gpu_device *vgdev,
6562fb7a5eSGerd Hoffmann 				   int num_capsets)
6662fb7a5eSGerd Hoffmann {
6762fb7a5eSGerd Hoffmann 	int i, ret;
681925d6a7SGurchetan Singh 	bool invalid_capset_id = false;
6962fb7a5eSGerd Hoffmann 
7062fb7a5eSGerd Hoffmann 	vgdev->capsets = kcalloc(num_capsets,
7162fb7a5eSGerd Hoffmann 				 sizeof(struct virtio_gpu_drv_capset),
7262fb7a5eSGerd Hoffmann 				 GFP_KERNEL);
7362fb7a5eSGerd Hoffmann 	if (!vgdev->capsets) {
7462fb7a5eSGerd Hoffmann 		DRM_ERROR("failed to allocate cap sets\n");
7562fb7a5eSGerd Hoffmann 		return;
7662fb7a5eSGerd Hoffmann 	}
7762fb7a5eSGerd Hoffmann 	for (i = 0; i < num_capsets; i++) {
7862fb7a5eSGerd Hoffmann 		virtio_gpu_cmd_get_capset_info(vgdev, i);
7997452907SGerd Hoffmann 		virtio_gpu_notify(vgdev);
8062fb7a5eSGerd Hoffmann 		ret = wait_event_timeout(vgdev->resp_wq,
8162fb7a5eSGerd Hoffmann 					 vgdev->capsets[i].id > 0, 5 * HZ);
821925d6a7SGurchetan Singh 		/*
831925d6a7SGurchetan Singh 		 * Capability ids are defined in the virtio-gpu spec and are
841925d6a7SGurchetan Singh 		 * between 1 to 63, inclusive.
851925d6a7SGurchetan Singh 		 */
861925d6a7SGurchetan Singh 		if (!vgdev->capsets[i].id ||
871925d6a7SGurchetan Singh 		    vgdev->capsets[i].id > MAX_CAPSET_ID)
881925d6a7SGurchetan Singh 			invalid_capset_id = true;
891925d6a7SGurchetan Singh 
901925d6a7SGurchetan Singh 		if (ret == 0)
9162fb7a5eSGerd Hoffmann 			DRM_ERROR("timed out waiting for cap set %d\n", i);
921925d6a7SGurchetan Singh 		else if (invalid_capset_id)
931925d6a7SGurchetan Singh 			DRM_ERROR("invalid capset id %u", vgdev->capsets[i].id);
941925d6a7SGurchetan Singh 
951925d6a7SGurchetan Singh 		if (ret == 0 || invalid_capset_id) {
96e219688fSDoug Horn 			spin_lock(&vgdev->display_info_lock);
9762fb7a5eSGerd Hoffmann 			kfree(vgdev->capsets);
9862fb7a5eSGerd Hoffmann 			vgdev->capsets = NULL;
99e219688fSDoug Horn 			spin_unlock(&vgdev->display_info_lock);
10062fb7a5eSGerd Hoffmann 			return;
10162fb7a5eSGerd Hoffmann 		}
1021925d6a7SGurchetan Singh 
1031925d6a7SGurchetan Singh 		vgdev->capset_id_mask |= 1 << vgdev->capsets[i].id;
10462fb7a5eSGerd Hoffmann 		DRM_INFO("cap set %d: id %d, max-version %d, max-size %d\n",
10562fb7a5eSGerd Hoffmann 			 i, vgdev->capsets[i].id,
10662fb7a5eSGerd Hoffmann 			 vgdev->capsets[i].max_version,
10762fb7a5eSGerd Hoffmann 			 vgdev->capsets[i].max_size);
10862fb7a5eSGerd Hoffmann 	}
1091925d6a7SGurchetan Singh 
11062fb7a5eSGerd Hoffmann 	vgdev->num_capsets = num_capsets;
11162fb7a5eSGerd Hoffmann }
11262fb7a5eSGerd Hoffmann 
113d516e75cSEzequiel Garcia int virtio_gpu_init(struct drm_device *dev)
114dc5698e8SDave Airlie {
115dc5698e8SDave Airlie 	static vq_callback_t *callbacks[] = {
116dc5698e8SDave Airlie 		virtio_gpu_ctrl_ack, virtio_gpu_cursor_ack
117dc5698e8SDave Airlie 	};
118f7ad26ffSStefan Hajnoczi 	static const char * const names[] = { "control", "cursor" };
119dc5698e8SDave Airlie 
120dc5698e8SDave Airlie 	struct virtio_gpu_device *vgdev;
121dc5698e8SDave Airlie 	/* this will expand later */
122dc5698e8SDave Airlie 	struct virtqueue *vqs[2];
12362fb7a5eSGerd Hoffmann 	u32 num_scanouts, num_capsets;
1241fb97413SGurchetan Singh 	int ret = 0;
125dc5698e8SDave Airlie 
12618e51064SDaniel Vetter 	if (!virtio_has_feature(dev_to_virtio(dev->dev), VIRTIO_F_VERSION_1))
127dc5698e8SDave Airlie 		return -ENODEV;
128dc5698e8SDave Airlie 
129dc5698e8SDave Airlie 	vgdev = kzalloc(sizeof(struct virtio_gpu_device), GFP_KERNEL);
130dc5698e8SDave Airlie 	if (!vgdev)
131dc5698e8SDave Airlie 		return -ENOMEM;
132dc5698e8SDave Airlie 
133dc5698e8SDave Airlie 	vgdev->ddev = dev;
134dc5698e8SDave Airlie 	dev->dev_private = vgdev;
13518e51064SDaniel Vetter 	vgdev->vdev = dev_to_virtio(dev->dev);
136dc5698e8SDave Airlie 	vgdev->dev = dev->dev;
137dc5698e8SDave Airlie 
138dc5698e8SDave Airlie 	spin_lock_init(&vgdev->display_info_lock);
139c84adb30SDavid Stevens 	spin_lock_init(&vgdev->resource_export_lock);
14016845c5dSGerd Hoffmann 	spin_lock_init(&vgdev->host_visible_lock);
1411938d1aeSMatthew Wilcox 	ida_init(&vgdev->ctx_id_ida);
1421938d1aeSMatthew Wilcox 	ida_init(&vgdev->resource_ida);
143dc5698e8SDave Airlie 	init_waitqueue_head(&vgdev->resp_wq);
144dc5698e8SDave Airlie 	virtio_gpu_init_vq(&vgdev->ctrlq, virtio_gpu_dequeue_ctrl_func);
145dc5698e8SDave Airlie 	virtio_gpu_init_vq(&vgdev->cursorq, virtio_gpu_dequeue_cursor_func);
146dc5698e8SDave Airlie 
147f54d1867SChris Wilson 	vgdev->fence_drv.context = dma_fence_context_alloc(1);
148dc5698e8SDave Airlie 	spin_lock_init(&vgdev->fence_drv.lock);
149dc5698e8SDave Airlie 	INIT_LIST_HEAD(&vgdev->fence_drv.fences);
15062fb7a5eSGerd Hoffmann 	INIT_LIST_HEAD(&vgdev->cap_cache);
151dc5698e8SDave Airlie 	INIT_WORK(&vgdev->config_changed_work,
152dc5698e8SDave Airlie 		  virtio_gpu_config_changed_work_func);
153dc5698e8SDave Airlie 
154f0c6cef7SGerd Hoffmann 	INIT_WORK(&vgdev->obj_free_work,
155f0c6cef7SGerd Hoffmann 		  virtio_gpu_array_put_free_work);
156f0c6cef7SGerd Hoffmann 	INIT_LIST_HEAD(&vgdev->obj_free_list);
157f0c6cef7SGerd Hoffmann 	spin_lock_init(&vgdev->obj_free_lock);
158f0c6cef7SGerd Hoffmann 
159ff2ac58aSLaurent Vivier #ifdef __LITTLE_ENDIAN
16062fb7a5eSGerd Hoffmann 	if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_VIRGL))
16162fb7a5eSGerd Hoffmann 		vgdev->has_virgl_3d = true;
162ff2ac58aSLaurent Vivier #endif
163b4b01b49SGerd Hoffmann 	if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_EDID)) {
164b4b01b49SGerd Hoffmann 		vgdev->has_edid = true;
165b4b01b49SGerd Hoffmann 	}
1665edbb560SGerd Hoffmann 	if (virtio_has_feature(vgdev->vdev, VIRTIO_RING_F_INDIRECT_DESC)) {
1675edbb560SGerd Hoffmann 		vgdev->has_indirect = true;
1685edbb560SGerd Hoffmann 	}
169c84adb30SDavid Stevens 	if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_RESOURCE_UUID)) {
170c84adb30SDavid Stevens 		vgdev->has_resource_assign_uuid = true;
171c84adb30SDavid Stevens 	}
1726815cfe6SGerd Hoffmann 	if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_RESOURCE_BLOB)) {
1736815cfe6SGerd Hoffmann 		vgdev->has_resource_blob = true;
1746815cfe6SGerd Hoffmann 	}
1756076a971SGerd Hoffmann 	if (virtio_get_shm_region(vgdev->vdev, &vgdev->host_visible_region,
1766076a971SGerd Hoffmann 				  VIRTIO_GPU_SHM_ID_HOST_VISIBLE)) {
1776076a971SGerd Hoffmann 		if (!devm_request_mem_region(&vgdev->vdev->dev,
1786076a971SGerd Hoffmann 					     vgdev->host_visible_region.addr,
1796076a971SGerd Hoffmann 					     vgdev->host_visible_region.len,
1806076a971SGerd Hoffmann 					     dev_name(&vgdev->vdev->dev))) {
1816076a971SGerd Hoffmann 			DRM_ERROR("Could not reserve host visible region\n");
182eb988a2eSDan Carpenter 			ret = -EBUSY;
1836076a971SGerd Hoffmann 			goto err_vqs;
1846076a971SGerd Hoffmann 		}
18562fb7a5eSGerd Hoffmann 
1866076a971SGerd Hoffmann 		DRM_INFO("Host memory window: 0x%lx +0x%lx\n",
1876076a971SGerd Hoffmann 			 (unsigned long)vgdev->host_visible_region.addr,
1886076a971SGerd Hoffmann 			 (unsigned long)vgdev->host_visible_region.len);
1896076a971SGerd Hoffmann 		vgdev->has_host_visible = true;
19016845c5dSGerd Hoffmann 		drm_mm_init(&vgdev->host_visible_mm,
19116845c5dSGerd Hoffmann 			    (unsigned long)vgdev->host_visible_region.addr,
19216845c5dSGerd Hoffmann 			    (unsigned long)vgdev->host_visible_region.len);
1936076a971SGerd Hoffmann 	}
194*6198770aSAnthoine Bourgeois 	if (virtio_has_feature(vgdev->vdev, VIRTIO_GPU_F_CONTEXT_INIT)) {
195*6198770aSAnthoine Bourgeois 		vgdev->has_context_init = true;
196*6198770aSAnthoine Bourgeois 	}
1976076a971SGerd Hoffmann 
198*6198770aSAnthoine Bourgeois 	DRM_INFO("features: %cvirgl %cedid %cresource_blob %chost_visible",
1999e370dfeSGerd Hoffmann 		 vgdev->has_virgl_3d    ? '+' : '-',
2006815cfe6SGerd Hoffmann 		 vgdev->has_edid        ? '+' : '-',
2016076a971SGerd Hoffmann 		 vgdev->has_resource_blob ? '+' : '-',
2026076a971SGerd Hoffmann 		 vgdev->has_host_visible ? '+' : '-');
2039e370dfeSGerd Hoffmann 
204*6198770aSAnthoine Bourgeois 	DRM_INFO("features: %ccontext_init\n",
205*6198770aSAnthoine Bourgeois 		 vgdev->has_context_init ? '+' : '-');
206*6198770aSAnthoine Bourgeois 
2079b2bbdb2SMichael S. Tsirkin 	ret = virtio_find_vqs(vgdev->vdev, 2, vqs, callbacks, names, NULL);
208dc5698e8SDave Airlie 	if (ret) {
209dc5698e8SDave Airlie 		DRM_ERROR("failed to find virt queues\n");
210dc5698e8SDave Airlie 		goto err_vqs;
211dc5698e8SDave Airlie 	}
212dc5698e8SDave Airlie 	vgdev->ctrlq.vq = vqs[0];
213dc5698e8SDave Airlie 	vgdev->cursorq.vq = vqs[1];
214dc5698e8SDave Airlie 	ret = virtio_gpu_alloc_vbufs(vgdev);
215dc5698e8SDave Airlie 	if (ret) {
216dc5698e8SDave Airlie 		DRM_ERROR("failed to alloc vbufs\n");
217dc5698e8SDave Airlie 		goto err_vbufs;
218dc5698e8SDave Airlie 	}
219dc5698e8SDave Airlie 
220dc5698e8SDave Airlie 	/* get display info */
221115a71d8SMichael S. Tsirkin 	virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
222dc5698e8SDave Airlie 			num_scanouts, &num_scanouts);
223dc5698e8SDave Airlie 	vgdev->num_scanouts = min_t(uint32_t, num_scanouts,
224dc5698e8SDave Airlie 				    VIRTIO_GPU_MAX_SCANOUTS);
225dc5698e8SDave Airlie 	if (!vgdev->num_scanouts) {
226dc5698e8SDave Airlie 		DRM_ERROR("num_scanouts is zero\n");
227dc5698e8SDave Airlie 		ret = -EINVAL;
228dc5698e8SDave Airlie 		goto err_scanouts;
229dc5698e8SDave Airlie 	}
23062fb7a5eSGerd Hoffmann 	DRM_INFO("number of scanouts: %d\n", num_scanouts);
23162fb7a5eSGerd Hoffmann 
232115a71d8SMichael S. Tsirkin 	virtio_cread_le(vgdev->vdev, struct virtio_gpu_config,
23362fb7a5eSGerd Hoffmann 			num_capsets, &num_capsets);
23462fb7a5eSGerd Hoffmann 	DRM_INFO("number of cap sets: %d\n", num_capsets);
235dc5698e8SDave Airlie 
236d6005d3dSGerd Hoffmann 	ret = virtio_gpu_modeset_init(vgdev);
237d6005d3dSGerd Hoffmann 	if (ret) {
238d6005d3dSGerd Hoffmann 		DRM_ERROR("modeset init failed\n");
239d6005d3dSGerd Hoffmann 		goto err_scanouts;
240d6005d3dSGerd Hoffmann 	}
241dc5698e8SDave Airlie 
242dc5698e8SDave Airlie 	virtio_device_ready(vgdev->vdev);
243dc5698e8SDave Airlie 
24462fb7a5eSGerd Hoffmann 	if (num_capsets)
24562fb7a5eSGerd Hoffmann 		virtio_gpu_get_capsets(vgdev, num_capsets);
246b4b01b49SGerd Hoffmann 	if (vgdev->has_edid)
247b4b01b49SGerd Hoffmann 		virtio_gpu_cmd_get_edids(vgdev);
248441012afSDave Airlie 	virtio_gpu_cmd_get_display_info(vgdev);
249234489eaSGerd Hoffmann 	virtio_gpu_notify(vgdev);
250441012afSDave Airlie 	wait_event_timeout(vgdev->resp_wq, !vgdev->display_info_pending,
251441012afSDave Airlie 			   5 * HZ);
252dc5698e8SDave Airlie 	return 0;
253dc5698e8SDave Airlie 
254dc5698e8SDave Airlie err_scanouts:
255dc5698e8SDave Airlie 	virtio_gpu_free_vbufs(vgdev);
256dc5698e8SDave Airlie err_vbufs:
257dc5698e8SDave Airlie 	vgdev->vdev->config->del_vqs(vgdev->vdev);
258dc5698e8SDave Airlie err_vqs:
259cec7f177SXie Yongji 	dev->dev_private = NULL;
260dc5698e8SDave Airlie 	kfree(vgdev);
261dc5698e8SDave Airlie 	return ret;
262dc5698e8SDave Airlie }
263dc5698e8SDave Airlie 
26462fb7a5eSGerd Hoffmann static void virtio_gpu_cleanup_cap_cache(struct virtio_gpu_device *vgdev)
26562fb7a5eSGerd Hoffmann {
26662fb7a5eSGerd Hoffmann 	struct virtio_gpu_drv_cap_cache *cache_ent, *tmp;
26762fb7a5eSGerd Hoffmann 
26862fb7a5eSGerd Hoffmann 	list_for_each_entry_safe(cache_ent, tmp, &vgdev->cap_cache, head) {
26962fb7a5eSGerd Hoffmann 		kfree(cache_ent->caps_cache);
27062fb7a5eSGerd Hoffmann 		kfree(cache_ent);
27162fb7a5eSGerd Hoffmann 	}
27262fb7a5eSGerd Hoffmann }
27362fb7a5eSGerd Hoffmann 
274d516e75cSEzequiel Garcia void virtio_gpu_deinit(struct drm_device *dev)
275dc5698e8SDave Airlie {
276dc5698e8SDave Airlie 	struct virtio_gpu_device *vgdev = dev->dev_private;
277dc5698e8SDave Airlie 
278f0c6cef7SGerd Hoffmann 	flush_work(&vgdev->obj_free_work);
279dc5698e8SDave Airlie 	flush_work(&vgdev->ctrlq.dequeue_work);
280dc5698e8SDave Airlie 	flush_work(&vgdev->cursorq.dequeue_work);
281dc5698e8SDave Airlie 	flush_work(&vgdev->config_changed_work);
282edde9fc5SEzequiel Garcia 	vgdev->vdev->config->reset(vgdev->vdev);
283dc5698e8SDave Airlie 	vgdev->vdev->config->del_vqs(vgdev->vdev);
284b1df3a2bSGerd Hoffmann }
285b1df3a2bSGerd Hoffmann 
286b1df3a2bSGerd Hoffmann void virtio_gpu_release(struct drm_device *dev)
287b1df3a2bSGerd Hoffmann {
288b1df3a2bSGerd Hoffmann 	struct virtio_gpu_device *vgdev = dev->dev_private;
289dc5698e8SDave Airlie 
29017f46f48SXie Yongji 	if (!vgdev)
29117f46f48SXie Yongji 		return;
29217f46f48SXie Yongji 
293dc5698e8SDave Airlie 	virtio_gpu_modeset_fini(vgdev);
294dc5698e8SDave Airlie 	virtio_gpu_free_vbufs(vgdev);
29562fb7a5eSGerd Hoffmann 	virtio_gpu_cleanup_cap_cache(vgdev);
29616845c5dSGerd Hoffmann 
29716845c5dSGerd Hoffmann 	if (vgdev->has_host_visible)
29816845c5dSGerd Hoffmann 		drm_mm_takedown(&vgdev->host_visible_mm);
29916845c5dSGerd Hoffmann 
30062fb7a5eSGerd Hoffmann 	kfree(vgdev->capsets);
301dc5698e8SDave Airlie 	kfree(vgdev);
302dc5698e8SDave Airlie }
30362fb7a5eSGerd Hoffmann 
30462fb7a5eSGerd Hoffmann int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)
30562fb7a5eSGerd Hoffmann {
30662fb7a5eSGerd Hoffmann 	struct virtio_gpu_device *vgdev = dev->dev_private;
30762fb7a5eSGerd Hoffmann 	struct virtio_gpu_fpriv *vfpriv;
30840cadeddSGurchetan Singh 	int handle;
30962fb7a5eSGerd Hoffmann 
31062fb7a5eSGerd Hoffmann 	/* can't create contexts without 3d renderer */
31162fb7a5eSGerd Hoffmann 	if (!vgdev->has_virgl_3d)
31262fb7a5eSGerd Hoffmann 		return 0;
31362fb7a5eSGerd Hoffmann 
31462fb7a5eSGerd Hoffmann 	/* allocate a virt GPU context for this opener */
31562fb7a5eSGerd Hoffmann 	vfpriv = kzalloc(sizeof(*vfpriv), GFP_KERNEL);
31662fb7a5eSGerd Hoffmann 	if (!vfpriv)
31762fb7a5eSGerd Hoffmann 		return -ENOMEM;
31862fb7a5eSGerd Hoffmann 
319d2a983b2SGurchetan Singh 	mutex_init(&vfpriv->context_lock);
320d2a983b2SGurchetan Singh 
32140cadeddSGurchetan Singh 	handle = ida_alloc(&vgdev->ctx_id_ida, GFP_KERNEL);
32240cadeddSGurchetan Singh 	if (handle < 0) {
323040b595aSColin Ian King 		kfree(vfpriv);
32440cadeddSGurchetan Singh 		return handle;
325040b595aSColin Ian King 	}
32662fb7a5eSGerd Hoffmann 
32740cadeddSGurchetan Singh 	vfpriv->ctx_id = handle + 1;
32862fb7a5eSGerd Hoffmann 	file->driver_priv = vfpriv;
32962fb7a5eSGerd Hoffmann 	return 0;
33062fb7a5eSGerd Hoffmann }
33162fb7a5eSGerd Hoffmann 
33262fb7a5eSGerd Hoffmann void virtio_gpu_driver_postclose(struct drm_device *dev, struct drm_file *file)
33362fb7a5eSGerd Hoffmann {
33462fb7a5eSGerd Hoffmann 	struct virtio_gpu_device *vgdev = dev->dev_private;
33545c5d2a4SGurchetan Singh 	struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
33662fb7a5eSGerd Hoffmann 
33762fb7a5eSGerd Hoffmann 	if (!vgdev->has_virgl_3d)
33862fb7a5eSGerd Hoffmann 		return;
33962fb7a5eSGerd Hoffmann 
34045c5d2a4SGurchetan Singh 	if (vfpriv->context_created) {
34145c5d2a4SGurchetan Singh 		virtio_gpu_cmd_context_destroy(vgdev, vfpriv->ctx_id);
34245c5d2a4SGurchetan Singh 		virtio_gpu_notify(vgdev);
34345c5d2a4SGurchetan Singh 	}
34462fb7a5eSGerd Hoffmann 
34545c5d2a4SGurchetan Singh 	ida_free(&vgdev->ctx_id_ida, vfpriv->ctx_id - 1);
346d2a983b2SGurchetan Singh 	mutex_destroy(&vfpriv->context_lock);
34762fb7a5eSGerd Hoffmann 	kfree(vfpriv);
34862fb7a5eSGerd Hoffmann 	file->driver_priv = NULL;
34962fb7a5eSGerd Hoffmann }
350