1 /*
2 * Copyright (C) 2015 Red Hat, Inc.
3 * All Rights Reserved.
4 *
5 * Authors:
6 * Dave Airlie
7 * Alon Levy
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 #include <drm/drm_atomic_helper.h>
29 #include <drm/drm_damage_helper.h>
30 #include <drm/drm_fourcc.h>
31 #include <drm/drm_gem_framebuffer_helper.h>
32 #include <drm/drm_probe_helper.h>
33 #include <drm/drm_simple_kms_helper.h>
34
35 #include "virtgpu_drv.h"
36
37 #define XRES_MIN 32
38 #define YRES_MIN 32
39
40 #define XRES_DEF 1024
41 #define YRES_DEF 768
42
43 #define XRES_MAX 8192
44 #define YRES_MAX 8192
45
46 #define drm_connector_to_virtio_gpu_output(x) \
47 container_of(x, struct virtio_gpu_output, conn)
48
49 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
50 .set_config = drm_atomic_helper_set_config,
51 .destroy = drm_crtc_cleanup,
52
53 .page_flip = drm_atomic_helper_page_flip,
54 .reset = drm_atomic_helper_crtc_reset,
55 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
56 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
57 };
58
59 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
60 .create_handle = drm_gem_fb_create_handle,
61 .destroy = drm_gem_fb_destroy,
62 .dirty = drm_atomic_helper_dirtyfb,
63 };
64
65 static int
virtio_gpu_framebuffer_init(struct drm_device * dev,struct virtio_gpu_framebuffer * vgfb,const struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object * obj)66 virtio_gpu_framebuffer_init(struct drm_device *dev,
67 struct virtio_gpu_framebuffer *vgfb,
68 const struct drm_mode_fb_cmd2 *mode_cmd,
69 struct drm_gem_object *obj)
70 {
71 int ret;
72
73 vgfb->base.obj[0] = obj;
74
75 drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
76
77 ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
78 if (ret) {
79 vgfb->base.obj[0] = NULL;
80 return ret;
81 }
82 return 0;
83 }
84
virtio_gpu_crtc_mode_set_nofb(struct drm_crtc * crtc)85 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
86 {
87 struct drm_device *dev = crtc->dev;
88 struct virtio_gpu_device *vgdev = dev->dev_private;
89 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
90
91 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
92 crtc->mode.hdisplay,
93 crtc->mode.vdisplay, 0, 0);
94 virtio_gpu_notify(vgdev);
95 }
96
virtio_gpu_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_atomic_state * state)97 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
98 struct drm_atomic_state *state)
99 {
100 }
101
virtio_gpu_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_atomic_state * state)102 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
103 struct drm_atomic_state *state)
104 {
105 struct drm_device *dev = crtc->dev;
106 struct virtio_gpu_device *vgdev = dev->dev_private;
107 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
108
109 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
110 virtio_gpu_notify(vgdev);
111 }
112
virtio_gpu_crtc_atomic_check(struct drm_crtc * crtc,struct drm_atomic_state * state)113 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
114 struct drm_atomic_state *state)
115 {
116 return 0;
117 }
118
virtio_gpu_crtc_atomic_flush(struct drm_crtc * crtc,struct drm_atomic_state * state)119 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
120 struct drm_atomic_state *state)
121 {
122 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
123 crtc);
124 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
125
126 /*
127 * virtio-gpu can't do modeset and plane update operations
128 * independent from each other. So the actual modeset happens
129 * in the plane update callback, and here we just check
130 * whenever we must force the modeset.
131 */
132 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
133 output->needs_modeset = true;
134 }
135 }
136
137 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
138 .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
139 .atomic_check = virtio_gpu_crtc_atomic_check,
140 .atomic_flush = virtio_gpu_crtc_atomic_flush,
141 .atomic_enable = virtio_gpu_crtc_atomic_enable,
142 .atomic_disable = virtio_gpu_crtc_atomic_disable,
143 };
144
virtio_gpu_enc_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)145 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
146 struct drm_display_mode *mode,
147 struct drm_display_mode *adjusted_mode)
148 {
149 }
150
virtio_gpu_enc_enable(struct drm_encoder * encoder)151 static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
152 {
153 }
154
virtio_gpu_enc_disable(struct drm_encoder * encoder)155 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
156 {
157 }
158
virtio_gpu_conn_get_modes(struct drm_connector * connector)159 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
160 {
161 struct virtio_gpu_output *output =
162 drm_connector_to_virtio_gpu_output(connector);
163 struct drm_display_mode *mode = NULL;
164 int count, width, height;
165
166 if (output->edid) {
167 count = drm_add_edid_modes(connector, output->edid);
168 if (count)
169 return count;
170 }
171
172 width = le32_to_cpu(output->info.r.width);
173 height = le32_to_cpu(output->info.r.height);
174 count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
175
176 if (width == 0 || height == 0) {
177 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
178 } else {
179 DRM_DEBUG("add mode: %dx%d\n", width, height);
180 mode = drm_cvt_mode(connector->dev, width, height, 60,
181 false, false, false);
182 mode->type |= DRM_MODE_TYPE_PREFERRED;
183 drm_mode_probed_add(connector, mode);
184 count++;
185 }
186
187 return count;
188 }
189
virtio_gpu_conn_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)190 static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector,
191 struct drm_display_mode *mode)
192 {
193 struct virtio_gpu_output *output =
194 drm_connector_to_virtio_gpu_output(connector);
195 int width, height;
196
197 width = le32_to_cpu(output->info.r.width);
198 height = le32_to_cpu(output->info.r.height);
199
200 if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
201 return MODE_OK;
202 if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
203 return MODE_OK;
204 if (mode->hdisplay <= width && mode->hdisplay >= width - 16 &&
205 mode->vdisplay <= height && mode->vdisplay >= height - 16)
206 return MODE_OK;
207
208 DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
209 return MODE_BAD;
210 }
211
212 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
213 .mode_set = virtio_gpu_enc_mode_set,
214 .enable = virtio_gpu_enc_enable,
215 .disable = virtio_gpu_enc_disable,
216 };
217
218 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
219 .get_modes = virtio_gpu_conn_get_modes,
220 .mode_valid = virtio_gpu_conn_mode_valid,
221 };
222
virtio_gpu_conn_detect(struct drm_connector * connector,bool force)223 static enum drm_connector_status virtio_gpu_conn_detect(
224 struct drm_connector *connector,
225 bool force)
226 {
227 struct virtio_gpu_output *output =
228 drm_connector_to_virtio_gpu_output(connector);
229
230 if (output->info.enabled)
231 return connector_status_connected;
232 else
233 return connector_status_disconnected;
234 }
235
virtio_gpu_conn_destroy(struct drm_connector * connector)236 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
237 {
238 drm_connector_unregister(connector);
239 drm_connector_cleanup(connector);
240 }
241
242 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
243 .detect = virtio_gpu_conn_detect,
244 .fill_modes = drm_helper_probe_single_connector_modes,
245 .destroy = virtio_gpu_conn_destroy,
246 .reset = drm_atomic_helper_connector_reset,
247 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
248 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
249 };
250
vgdev_output_init(struct virtio_gpu_device * vgdev,int index)251 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
252 {
253 struct drm_device *dev = vgdev->ddev;
254 struct virtio_gpu_output *output = vgdev->outputs + index;
255 struct drm_connector *connector = &output->conn;
256 struct drm_encoder *encoder = &output->enc;
257 struct drm_crtc *crtc = &output->crtc;
258 struct drm_plane *primary, *cursor;
259
260 output->index = index;
261 if (index == 0) {
262 output->info.enabled = cpu_to_le32(true);
263 output->info.r.width = cpu_to_le32(XRES_DEF);
264 output->info.r.height = cpu_to_le32(YRES_DEF);
265 }
266
267 primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
268 if (IS_ERR(primary))
269 return PTR_ERR(primary);
270 cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
271 if (IS_ERR(cursor))
272 return PTR_ERR(cursor);
273 drm_crtc_init_with_planes(dev, crtc, primary, cursor,
274 &virtio_gpu_crtc_funcs, NULL);
275 drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
276
277 drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
278 DRM_MODE_CONNECTOR_VIRTUAL);
279 drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
280 if (vgdev->has_edid)
281 drm_connector_attach_edid_property(connector);
282
283 drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_VIRTUAL);
284 drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
285 encoder->possible_crtcs = 1 << index;
286
287 drm_connector_attach_encoder(connector, encoder);
288 drm_connector_register(connector);
289 return 0;
290 }
291
292 static struct drm_framebuffer *
virtio_gpu_user_framebuffer_create(struct drm_device * dev,struct drm_file * file_priv,const struct drm_mode_fb_cmd2 * mode_cmd)293 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
294 struct drm_file *file_priv,
295 const struct drm_mode_fb_cmd2 *mode_cmd)
296 {
297 struct drm_gem_object *obj = NULL;
298 struct virtio_gpu_framebuffer *virtio_gpu_fb;
299 int ret;
300
301 if (mode_cmd->pixel_format != DRM_FORMAT_HOST_XRGB8888 &&
302 mode_cmd->pixel_format != DRM_FORMAT_HOST_ARGB8888)
303 return ERR_PTR(-ENOENT);
304
305 /* lookup object associated with res handle */
306 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
307 if (!obj)
308 return ERR_PTR(-EINVAL);
309
310 virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
311 if (virtio_gpu_fb == NULL) {
312 drm_gem_object_put(obj);
313 return ERR_PTR(-ENOMEM);
314 }
315
316 ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
317 if (ret) {
318 kfree(virtio_gpu_fb);
319 drm_gem_object_put(obj);
320 return NULL;
321 }
322
323 return &virtio_gpu_fb->base;
324 }
325
326 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
327 .fb_create = virtio_gpu_user_framebuffer_create,
328 .atomic_check = drm_atomic_helper_check,
329 .atomic_commit = drm_atomic_helper_commit,
330 };
331
virtio_gpu_modeset_init(struct virtio_gpu_device * vgdev)332 int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
333 {
334 int i, ret;
335
336 ret = drmm_mode_config_init(vgdev->ddev);
337 if (ret)
338 return ret;
339
340 vgdev->ddev->mode_config.quirk_addfb_prefer_host_byte_order = true;
341 vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
342
343 /* modes will be validated against the framebuffer size */
344 vgdev->ddev->mode_config.min_width = XRES_MIN;
345 vgdev->ddev->mode_config.min_height = YRES_MIN;
346 vgdev->ddev->mode_config.max_width = XRES_MAX;
347 vgdev->ddev->mode_config.max_height = YRES_MAX;
348
349 for (i = 0 ; i < vgdev->num_scanouts; ++i)
350 vgdev_output_init(vgdev, i);
351
352 drm_mode_config_reset(vgdev->ddev);
353 return 0;
354 }
355
virtio_gpu_modeset_fini(struct virtio_gpu_device * vgdev)356 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
357 {
358 int i;
359
360 for (i = 0 ; i < vgdev->num_scanouts; ++i)
361 kfree(vgdev->outputs[i].edid);
362 }
363