2 * Copyright (C) 2015 Red Hat, Inc.
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:
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.
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.
26 #include <linux/virtio.h>
27 #include <linux/virtio_config.h>
29 #include "virtgpu_drv.h"
31 static int virtio_gpu_fbdev = 1;
33 MODULE_PARM_DESC(fbdev, "Disable/Enable framebuffer device & console");
34 module_param_named(fbdev, virtio_gpu_fbdev, int, 0400);
36 static void virtio_gpu_config_changed_work_func(struct work_struct *work)
38 struct virtio_gpu_device *vgdev =
39 container_of(work, struct virtio_gpu_device,
41 u32 events_read, events_clear = 0;
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;
51 virtio_cwrite(vgdev->vdev, struct virtio_gpu_config,
52 events_clear, &events_clear);
55 static void virtio_gpu_init_vq(struct virtio_gpu_queue *vgvq,
56 void (*work_func)(struct work_struct *work))
58 spin_lock_init(&vgvq->qlock);
59 init_waitqueue_head(&vgvq->ack_queue);
60 INIT_WORK(&vgvq->dequeue_work, work_func);
63 int virtio_gpu_driver_load(struct drm_device *dev, unsigned long flags)
65 static vq_callback_t *callbacks[] = {
66 virtio_gpu_ctrl_ack, virtio_gpu_cursor_ack
68 static const char *names[] = { "control", "cursor" };
70 struct virtio_gpu_device *vgdev;
71 /* this will expand later */
72 struct virtqueue *vqs[2];
76 if (!virtio_has_feature(dev->virtdev, VIRTIO_F_VERSION_1))
79 vgdev = kzalloc(sizeof(struct virtio_gpu_device), GFP_KERNEL);
84 dev->dev_private = vgdev;
85 vgdev->vdev = dev->virtdev;
86 vgdev->dev = dev->dev;
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);
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);
102 ret = vgdev->vdev->config->find_vqs(vgdev->vdev, 2, vqs,
105 DRM_ERROR("failed to find virt queues\n");
108 vgdev->ctrlq.vq = vqs[0];
109 vgdev->cursorq.vq = vqs[1];
110 ret = virtio_gpu_alloc_vbufs(vgdev);
112 DRM_ERROR("failed to alloc vbufs\n");
116 ret = virtio_gpu_ttm_init(vgdev);
118 DRM_ERROR("failed to init ttm %d\n", ret);
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");
133 ret = virtio_gpu_modeset_init(vgdev);
137 virtio_device_ready(vgdev->vdev);
138 vgdev->vqs_ready = true;
140 virtio_gpu_cmd_get_display_info(vgdev);
141 wait_event_timeout(vgdev->resp_wq, !vgdev->display_info_pending,
143 if (virtio_gpu_fbdev)
144 virtio_gpu_fbdev_init(vgdev);
150 virtio_gpu_ttm_fini(vgdev);
152 virtio_gpu_free_vbufs(vgdev);
154 vgdev->vdev->config->del_vqs(vgdev->vdev);
160 int virtio_gpu_driver_unload(struct drm_device *dev)
162 struct virtio_gpu_device *vgdev = dev->dev_private;
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);
170 virtio_gpu_modeset_fini(vgdev);
171 virtio_gpu_ttm_fini(vgdev);
172 virtio_gpu_free_vbufs(vgdev);