xref: /linux/drivers/gpu/drm/virtio/virtgpu_kms.c (revision b85d45947951d23cb22d90caecf4c1eb81342c96)
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/virtio.h>
27 #include <linux/virtio_config.h>
28 #include <drm/drmP.h>
29 #include "virtgpu_drv.h"
30 
31 static int virtio_gpu_fbdev = 1;
32 
33 MODULE_PARM_DESC(fbdev, "Disable/Enable framebuffer device & console");
34 module_param_named(fbdev, virtio_gpu_fbdev, int, 0400);
35 
36 static void virtio_gpu_config_changed_work_func(struct work_struct *work)
37 {
38 	struct virtio_gpu_device *vgdev =
39 		container_of(work, struct virtio_gpu_device,
40 			     config_changed_work);
41 	u32 events_read, events_clear = 0;
42 
43 	/* read the config space */
44 	virtio_cread(vgdev->vdev, struct virtio_gpu_config,
45 		     events_read, &events_read);
46 	if (events_read & VIRTIO_GPU_EVENT_DISPLAY) {
47 		virtio_gpu_cmd_get_display_info(vgdev);
48 		drm_helper_hpd_irq_event(vgdev->ddev);
49 		events_clear |= VIRTIO_GPU_EVENT_DISPLAY;
50 	}
51 	virtio_cwrite(vgdev->vdev, struct virtio_gpu_config,
52 		      events_clear, &events_clear);
53 }
54 
55 static void virtio_gpu_init_vq(struct virtio_gpu_queue *vgvq,
56 			       void (*work_func)(struct work_struct *work))
57 {
58 	spin_lock_init(&vgvq->qlock);
59 	init_waitqueue_head(&vgvq->ack_queue);
60 	INIT_WORK(&vgvq->dequeue_work, work_func);
61 }
62 
63 int virtio_gpu_driver_load(struct drm_device *dev, unsigned long flags)
64 {
65 	static vq_callback_t *callbacks[] = {
66 		virtio_gpu_ctrl_ack, virtio_gpu_cursor_ack
67 	};
68 	static const char *names[] = { "control", "cursor" };
69 
70 	struct virtio_gpu_device *vgdev;
71 	/* this will expand later */
72 	struct virtqueue *vqs[2];
73 	u32 num_scanouts;
74 	int ret;
75 
76 	if (!virtio_has_feature(dev->virtdev, VIRTIO_F_VERSION_1))
77 		return -ENODEV;
78 
79 	vgdev = kzalloc(sizeof(struct virtio_gpu_device), GFP_KERNEL);
80 	if (!vgdev)
81 		return -ENOMEM;
82 
83 	vgdev->ddev = dev;
84 	dev->dev_private = vgdev;
85 	vgdev->vdev = dev->virtdev;
86 	vgdev->dev = dev->dev;
87 
88 	spin_lock_init(&vgdev->display_info_lock);
89 	spin_lock_init(&vgdev->ctx_id_idr_lock);
90 	idr_init(&vgdev->ctx_id_idr);
91 	spin_lock_init(&vgdev->resource_idr_lock);
92 	idr_init(&vgdev->resource_idr);
93 	init_waitqueue_head(&vgdev->resp_wq);
94 	virtio_gpu_init_vq(&vgdev->ctrlq, virtio_gpu_dequeue_ctrl_func);
95 	virtio_gpu_init_vq(&vgdev->cursorq, virtio_gpu_dequeue_cursor_func);
96 
97 	spin_lock_init(&vgdev->fence_drv.lock);
98 	INIT_LIST_HEAD(&vgdev->fence_drv.fences);
99 	INIT_WORK(&vgdev->config_changed_work,
100 		  virtio_gpu_config_changed_work_func);
101 
102 	ret = vgdev->vdev->config->find_vqs(vgdev->vdev, 2, vqs,
103 					    callbacks, names);
104 	if (ret) {
105 		DRM_ERROR("failed to find virt queues\n");
106 		goto err_vqs;
107 	}
108 	vgdev->ctrlq.vq = vqs[0];
109 	vgdev->cursorq.vq = vqs[1];
110 	ret = virtio_gpu_alloc_vbufs(vgdev);
111 	if (ret) {
112 		DRM_ERROR("failed to alloc vbufs\n");
113 		goto err_vbufs;
114 	}
115 
116 	ret = virtio_gpu_ttm_init(vgdev);
117 	if (ret) {
118 		DRM_ERROR("failed to init ttm %d\n", ret);
119 		goto err_ttm;
120 	}
121 
122 	/* get display info */
123 	virtio_cread(vgdev->vdev, struct virtio_gpu_config,
124 		     num_scanouts, &num_scanouts);
125 	vgdev->num_scanouts = min_t(uint32_t, num_scanouts,
126 				    VIRTIO_GPU_MAX_SCANOUTS);
127 	if (!vgdev->num_scanouts) {
128 		DRM_ERROR("num_scanouts is zero\n");
129 		ret = -EINVAL;
130 		goto err_scanouts;
131 	}
132 
133 	ret = virtio_gpu_modeset_init(vgdev);
134 	if (ret)
135 		goto err_modeset;
136 
137 	virtio_device_ready(vgdev->vdev);
138 	vgdev->vqs_ready = true;
139 
140 	virtio_gpu_cmd_get_display_info(vgdev);
141 	wait_event_timeout(vgdev->resp_wq, !vgdev->display_info_pending,
142 			   5 * HZ);
143 	if (virtio_gpu_fbdev)
144 		virtio_gpu_fbdev_init(vgdev);
145 
146 	return 0;
147 
148 err_modeset:
149 err_scanouts:
150 	virtio_gpu_ttm_fini(vgdev);
151 err_ttm:
152 	virtio_gpu_free_vbufs(vgdev);
153 err_vbufs:
154 	vgdev->vdev->config->del_vqs(vgdev->vdev);
155 err_vqs:
156 	kfree(vgdev);
157 	return ret;
158 }
159 
160 int virtio_gpu_driver_unload(struct drm_device *dev)
161 {
162 	struct virtio_gpu_device *vgdev = dev->dev_private;
163 
164 	vgdev->vqs_ready = false;
165 	flush_work(&vgdev->ctrlq.dequeue_work);
166 	flush_work(&vgdev->cursorq.dequeue_work);
167 	flush_work(&vgdev->config_changed_work);
168 	vgdev->vdev->config->del_vqs(vgdev->vdev);
169 
170 	virtio_gpu_modeset_fini(vgdev);
171 	virtio_gpu_ttm_fini(vgdev);
172 	virtio_gpu_free_vbufs(vgdev);
173 	kfree(vgdev);
174 	return 0;
175 }
176