blob: 132405f1538916961b147e0cfc2da3caa3ef917c [file] [log] [blame]
Dave Airliedc5698e2013-09-09 10:02:56 +10001/*
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
31static int virtio_gpu_fbdev = 1;
32
33MODULE_PARM_DESC(fbdev, "Disable/Enable framebuffer device & console");
34module_param_named(fbdev, virtio_gpu_fbdev, int, 0400);
35
36static 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
55static 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
63int 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 if (virtio_gpu_fbdev)
141 virtio_gpu_fbdev_init(vgdev);
142 virtio_gpu_cmd_get_display_info(vgdev);
143
144 return 0;
145
146err_modeset:
147err_scanouts:
148 virtio_gpu_ttm_fini(vgdev);
149err_ttm:
150 virtio_gpu_free_vbufs(vgdev);
151err_vbufs:
152 vgdev->vdev->config->del_vqs(vgdev->vdev);
153err_vqs:
154 kfree(vgdev);
155 return ret;
156}
157
158int virtio_gpu_driver_unload(struct drm_device *dev)
159{
160 struct virtio_gpu_device *vgdev = dev->dev_private;
161
162 vgdev->vqs_ready = false;
163 flush_work(&vgdev->ctrlq.dequeue_work);
164 flush_work(&vgdev->cursorq.dequeue_work);
165 flush_work(&vgdev->config_changed_work);
166 vgdev->vdev->config->del_vqs(vgdev->vdev);
167
168 virtio_gpu_modeset_fini(vgdev);
169 virtio_gpu_ttm_fini(vgdev);
170 virtio_gpu_free_vbufs(vgdev);
171 kfree(vgdev);
172 return 0;
173}