xref: /linux/drivers/gpu/drm/virtio/virtgpu_display.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
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 "virtgpu_drv.h"
29 #include <drm/drm_crtc_helper.h>
30 #include <drm/drm_atomic_helper.h>
31 
32 #define XRES_MIN   320
33 #define YRES_MIN   200
34 
35 #define XRES_DEF  1024
36 #define YRES_DEF   768
37 
38 #define XRES_MAX  8192
39 #define YRES_MAX  8192
40 
41 static void virtio_gpu_crtc_gamma_set(struct drm_crtc *crtc,
42 				      u16 *red, u16 *green, u16 *blue,
43 				      uint32_t start, uint32_t size)
44 {
45 	/* TODO */
46 }
47 
48 static void
49 virtio_gpu_hide_cursor(struct virtio_gpu_device *vgdev,
50 		       struct virtio_gpu_output *output)
51 {
52 	output->cursor.hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR);
53 	output->cursor.resource_id = 0;
54 	virtio_gpu_cursor_ping(vgdev, output);
55 }
56 
57 static int virtio_gpu_crtc_cursor_set(struct drm_crtc *crtc,
58 				      struct drm_file *file_priv,
59 				      uint32_t handle,
60 				      uint32_t width,
61 				      uint32_t height,
62 				      int32_t hot_x, int32_t hot_y)
63 {
64 	struct virtio_gpu_device *vgdev = crtc->dev->dev_private;
65 	struct virtio_gpu_output *output =
66 		container_of(crtc, struct virtio_gpu_output, crtc);
67 	struct drm_gem_object *gobj = NULL;
68 	struct virtio_gpu_object *qobj = NULL;
69 	struct virtio_gpu_fence *fence = NULL;
70 	int ret = 0;
71 
72 	if (handle == 0) {
73 		virtio_gpu_hide_cursor(vgdev, output);
74 		return 0;
75 	}
76 
77 	/* lookup the cursor */
78 	gobj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
79 	if (gobj == NULL)
80 		return -ENOENT;
81 
82 	qobj = gem_to_virtio_gpu_obj(gobj);
83 
84 	if (!qobj->hw_res_handle) {
85 		ret = -EINVAL;
86 		goto out;
87 	}
88 
89 	virtio_gpu_cmd_transfer_to_host_2d(vgdev, qobj->hw_res_handle, 0,
90 					   cpu_to_le32(64),
91 					   cpu_to_le32(64),
92 					   0, 0, &fence);
93 
94 	output->cursor.hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR);
95 	output->cursor.resource_id = cpu_to_le32(qobj->hw_res_handle);
96 	output->cursor.hot_x = cpu_to_le32(hot_x);
97 	output->cursor.hot_y = cpu_to_le32(hot_y);
98 	virtio_gpu_cursor_ping(vgdev, output);
99 	ret = 0;
100 
101 out:
102 	drm_gem_object_unreference_unlocked(gobj);
103 	return ret;
104 }
105 
106 static int virtio_gpu_crtc_cursor_move(struct drm_crtc *crtc,
107 				    int x, int y)
108 {
109 	struct virtio_gpu_device *vgdev = crtc->dev->dev_private;
110 	struct virtio_gpu_output *output =
111 		container_of(crtc, struct virtio_gpu_output, crtc);
112 
113 	output->cursor.hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_MOVE_CURSOR);
114 	output->cursor.pos.x = cpu_to_le32(x);
115 	output->cursor.pos.y = cpu_to_le32(y);
116 	virtio_gpu_cursor_ping(vgdev, output);
117 	return 0;
118 }
119 
120 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
121 	.cursor_set2            = virtio_gpu_crtc_cursor_set,
122 	.cursor_move            = virtio_gpu_crtc_cursor_move,
123 	.gamma_set              = virtio_gpu_crtc_gamma_set,
124 	.set_config             = drm_atomic_helper_set_config,
125 	.destroy                = drm_crtc_cleanup,
126 
127 #if 0 /* not (yet) working without vblank support according to docs */
128 	.page_flip              = drm_atomic_helper_page_flip,
129 #endif
130 	.reset                  = drm_atomic_helper_crtc_reset,
131 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
132 	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
133 };
134 
135 static void virtio_gpu_user_framebuffer_destroy(struct drm_framebuffer *fb)
136 {
137 	struct virtio_gpu_framebuffer *virtio_gpu_fb
138 		= to_virtio_gpu_framebuffer(fb);
139 
140 	if (virtio_gpu_fb->obj)
141 		drm_gem_object_unreference_unlocked(virtio_gpu_fb->obj);
142 	drm_framebuffer_cleanup(fb);
143 	kfree(virtio_gpu_fb);
144 }
145 
146 static int
147 virtio_gpu_framebuffer_surface_dirty(struct drm_framebuffer *fb,
148 				     struct drm_file *file_priv,
149 				     unsigned flags, unsigned color,
150 				     struct drm_clip_rect *clips,
151 				     unsigned num_clips)
152 {
153 	struct virtio_gpu_framebuffer *virtio_gpu_fb
154 		= to_virtio_gpu_framebuffer(fb);
155 
156 	return virtio_gpu_surface_dirty(virtio_gpu_fb, clips, num_clips);
157 }
158 
159 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
160 	.destroy = virtio_gpu_user_framebuffer_destroy,
161 	.dirty = virtio_gpu_framebuffer_surface_dirty,
162 };
163 
164 int
165 virtio_gpu_framebuffer_init(struct drm_device *dev,
166 			    struct virtio_gpu_framebuffer *vgfb,
167 			    struct drm_mode_fb_cmd2 *mode_cmd,
168 			    struct drm_gem_object *obj)
169 {
170 	int ret;
171 	struct virtio_gpu_object *bo;
172 	vgfb->obj = obj;
173 
174 	bo = gem_to_virtio_gpu_obj(obj);
175 
176 	ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
177 	if (ret) {
178 		vgfb->obj = NULL;
179 		return ret;
180 	}
181 	drm_helper_mode_fill_fb_struct(&vgfb->base, mode_cmd);
182 
183 	spin_lock_init(&vgfb->dirty_lock);
184 	vgfb->x1 = vgfb->y1 = INT_MAX;
185 	vgfb->x2 = vgfb->y2 = 0;
186 	return 0;
187 }
188 
189 static bool virtio_gpu_crtc_mode_fixup(struct drm_crtc *crtc,
190 				       const struct drm_display_mode *mode,
191 				       struct drm_display_mode *adjusted_mode)
192 {
193 	return true;
194 }
195 
196 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
197 {
198 	struct drm_device *dev = crtc->dev;
199 	struct virtio_gpu_device *vgdev = dev->dev_private;
200 	struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
201 
202 	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
203 				   crtc->mode.hdisplay,
204 				   crtc->mode.vdisplay, 0, 0);
205 }
206 
207 static void virtio_gpu_crtc_enable(struct drm_crtc *crtc)
208 {
209 }
210 
211 static void virtio_gpu_crtc_disable(struct drm_crtc *crtc)
212 {
213 	struct drm_device *dev = crtc->dev;
214 	struct virtio_gpu_device *vgdev = dev->dev_private;
215 	struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
216 
217 	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
218 }
219 
220 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
221 					struct drm_crtc_state *state)
222 {
223 	return 0;
224 }
225 
226 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
227 	.enable        = virtio_gpu_crtc_enable,
228 	.disable       = virtio_gpu_crtc_disable,
229 	.mode_fixup    = virtio_gpu_crtc_mode_fixup,
230 	.mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
231 	.atomic_check  = virtio_gpu_crtc_atomic_check,
232 };
233 
234 static bool virtio_gpu_enc_mode_fixup(struct drm_encoder *encoder,
235 				      const struct drm_display_mode *mode,
236 				      struct drm_display_mode *adjusted_mode)
237 {
238 	return true;
239 }
240 
241 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
242 				    struct drm_display_mode *mode,
243 				    struct drm_display_mode *adjusted_mode)
244 {
245 }
246 
247 static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
248 {
249 }
250 
251 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
252 {
253 }
254 
255 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
256 {
257 	struct virtio_gpu_output *output =
258 		drm_connector_to_virtio_gpu_output(connector);
259 	struct drm_display_mode *mode = NULL;
260 	int count, width, height;
261 
262 	width  = le32_to_cpu(output->info.r.width);
263 	height = le32_to_cpu(output->info.r.height);
264 	count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
265 
266 	if (width == 0 || height == 0) {
267 		width = XRES_DEF;
268 		height = YRES_DEF;
269 		drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
270 	} else {
271 		DRM_DEBUG("add mode: %dx%d\n", width, height);
272 		mode = drm_cvt_mode(connector->dev, width, height, 60,
273 				    false, false, false);
274 		mode->type |= DRM_MODE_TYPE_PREFERRED;
275 		drm_mode_probed_add(connector, mode);
276 		count++;
277 	}
278 
279 	return count;
280 }
281 
282 static int virtio_gpu_conn_mode_valid(struct drm_connector *connector,
283 				      struct drm_display_mode *mode)
284 {
285 	struct virtio_gpu_output *output =
286 		drm_connector_to_virtio_gpu_output(connector);
287 	int width, height;
288 
289 	width  = le32_to_cpu(output->info.r.width);
290 	height = le32_to_cpu(output->info.r.height);
291 
292 	if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
293 		return MODE_OK;
294 	if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
295 		return MODE_OK;
296 	if (mode->hdisplay <= width  && mode->hdisplay >= width - 16 &&
297 	    mode->vdisplay <= height && mode->vdisplay >= height - 16)
298 		return MODE_OK;
299 
300 	DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
301 	return MODE_BAD;
302 }
303 
304 static struct drm_encoder*
305 virtio_gpu_best_encoder(struct drm_connector *connector)
306 {
307 	struct virtio_gpu_output *virtio_gpu_output =
308 		drm_connector_to_virtio_gpu_output(connector);
309 
310 	return &virtio_gpu_output->enc;
311 }
312 
313 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
314 	.mode_fixup = virtio_gpu_enc_mode_fixup,
315 	.mode_set   = virtio_gpu_enc_mode_set,
316 	.enable     = virtio_gpu_enc_enable,
317 	.disable    = virtio_gpu_enc_disable,
318 };
319 
320 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
321 	.get_modes    = virtio_gpu_conn_get_modes,
322 	.mode_valid   = virtio_gpu_conn_mode_valid,
323 	.best_encoder = virtio_gpu_best_encoder,
324 };
325 
326 static void virtio_gpu_conn_save(struct drm_connector *connector)
327 {
328 	DRM_DEBUG("\n");
329 }
330 
331 static void virtio_gpu_conn_restore(struct drm_connector *connector)
332 {
333 	DRM_DEBUG("\n");
334 }
335 
336 static enum drm_connector_status virtio_gpu_conn_detect(
337 			struct drm_connector *connector,
338 			bool force)
339 {
340 	struct virtio_gpu_output *output =
341 		drm_connector_to_virtio_gpu_output(connector);
342 
343 	if (output->info.enabled)
344 		return connector_status_connected;
345 	else
346 		return connector_status_disconnected;
347 }
348 
349 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
350 {
351 	struct virtio_gpu_output *virtio_gpu_output =
352 		drm_connector_to_virtio_gpu_output(connector);
353 
354 	drm_connector_unregister(connector);
355 	drm_connector_cleanup(connector);
356 	kfree(virtio_gpu_output);
357 }
358 
359 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
360 	.dpms = drm_atomic_helper_connector_dpms,
361 	.save = virtio_gpu_conn_save,
362 	.restore = virtio_gpu_conn_restore,
363 	.detect = virtio_gpu_conn_detect,
364 	.fill_modes = drm_helper_probe_single_connector_modes,
365 	.destroy = virtio_gpu_conn_destroy,
366 	.reset = drm_atomic_helper_connector_reset,
367 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
368 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
369 };
370 
371 static const struct drm_encoder_funcs virtio_gpu_enc_funcs = {
372 	.destroy = drm_encoder_cleanup,
373 };
374 
375 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
376 {
377 	struct drm_device *dev = vgdev->ddev;
378 	struct virtio_gpu_output *output = vgdev->outputs + index;
379 	struct drm_connector *connector = &output->conn;
380 	struct drm_encoder *encoder = &output->enc;
381 	struct drm_crtc *crtc = &output->crtc;
382 	struct drm_plane *plane;
383 
384 	output->index = index;
385 	if (index == 0) {
386 		output->info.enabled = cpu_to_le32(true);
387 		output->info.r.width = cpu_to_le32(XRES_DEF);
388 		output->info.r.height = cpu_to_le32(YRES_DEF);
389 	}
390 
391 	plane = virtio_gpu_plane_init(vgdev, index);
392 	if (IS_ERR(plane))
393 		return PTR_ERR(plane);
394 	drm_crtc_init_with_planes(dev, crtc, plane, NULL,
395 				  &virtio_gpu_crtc_funcs);
396 	drm_mode_crtc_set_gamma_size(crtc, 256);
397 	drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
398 	plane->crtc = crtc;
399 
400 	drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
401 			   DRM_MODE_CONNECTOR_VIRTUAL);
402 	drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
403 
404 	drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs,
405 			 DRM_MODE_ENCODER_VIRTUAL);
406 	drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
407 	encoder->possible_crtcs = 1 << index;
408 
409 	drm_mode_connector_attach_encoder(connector, encoder);
410 	drm_connector_register(connector);
411 	return 0;
412 }
413 
414 static struct drm_framebuffer *
415 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
416 				   struct drm_file *file_priv,
417 				   struct drm_mode_fb_cmd2 *mode_cmd)
418 {
419 	struct drm_gem_object *obj = NULL;
420 	struct virtio_gpu_framebuffer *virtio_gpu_fb;
421 	int ret;
422 
423 	/* lookup object associated with res handle */
424 	obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
425 	if (!obj)
426 		return ERR_PTR(-EINVAL);
427 
428 	virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
429 	if (virtio_gpu_fb == NULL)
430 		return ERR_PTR(-ENOMEM);
431 
432 	ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
433 	if (ret) {
434 		kfree(virtio_gpu_fb);
435 		if (obj)
436 			drm_gem_object_unreference_unlocked(obj);
437 		return NULL;
438 	}
439 
440 	return &virtio_gpu_fb->base;
441 }
442 
443 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
444 	.fb_create = virtio_gpu_user_framebuffer_create,
445 	.atomic_check = drm_atomic_helper_check,
446 	.atomic_commit = drm_atomic_helper_commit,
447 };
448 
449 int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
450 {
451 	int i;
452 
453 	drm_mode_config_init(vgdev->ddev);
454 	vgdev->ddev->mode_config.funcs = (void *)&virtio_gpu_mode_funcs;
455 
456 	/* modes will be validated against the framebuffer size */
457 	vgdev->ddev->mode_config.min_width = XRES_MIN;
458 	vgdev->ddev->mode_config.min_height = YRES_MIN;
459 	vgdev->ddev->mode_config.max_width = XRES_MAX;
460 	vgdev->ddev->mode_config.max_height = YRES_MAX;
461 
462 	for (i = 0 ; i < vgdev->num_scanouts; ++i)
463 		vgdev_output_init(vgdev, i);
464 
465         drm_mode_config_reset(vgdev->ddev);
466 	return 0;
467 }
468 
469 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
470 {
471 	virtio_gpu_fbdev_fini(vgdev);
472 	drm_mode_config_cleanup(vgdev->ddev);
473 }
474