xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/vboxvideo/vbox_mode.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: vbox_mode.c,v 1.2 2021/12/18 23:45:44 riastradh Exp $	*/
2 
3 // SPDX-License-Identifier: MIT
4 /*
5  * Copyright (C) 2013-2017 Oracle Corporation
6  * This file is based on ast_mode.c
7  * Copyright 2012 Red Hat Inc.
8  * Parts based on xf86-video-ast
9  * Copyright (c) 2005 ASPEED Technology Inc.
10  * Authors: Dave Airlie <airlied@redhat.com>
11  *          Michael Thayer <michael.thayer@oracle.com,
12  *          Hans de Goede <hdegoede@redhat.com>
13  */
14 #include <sys/cdefs.h>
15 __KERNEL_RCSID(0, "$NetBSD: vbox_mode.c,v 1.2 2021/12/18 23:45:44 riastradh Exp $");
16 
17 #include <linux/export.h>
18 
19 #include <drm/drm_atomic.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_fb_helper.h>
22 #include <drm/drm_fourcc.h>
23 #include <drm/drm_gem_framebuffer_helper.h>
24 #include <drm/drm_plane_helper.h>
25 #include <drm/drm_probe_helper.h>
26 #include <drm/drm_vblank.h>
27 
28 #include "hgsmi_channels.h"
29 #include "vbox_drv.h"
30 #include "vboxvideo.h"
31 
32 /*
33  * Set a graphics mode.  Poke any required values into registers, do an HGSMI
34  * mode set and tell the host we support advanced graphics functions.
35  */
vbox_do_modeset(struct drm_crtc * crtc)36 static void vbox_do_modeset(struct drm_crtc *crtc)
37 {
38 	struct drm_framebuffer *fb = crtc->primary->state->fb;
39 	struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
40 	struct vbox_private *vbox;
41 	int width, height, bpp, pitch;
42 	u16 flags;
43 	s32 x_offset, y_offset;
44 
45 	vbox = crtc->dev->dev_private;
46 	width = vbox_crtc->width ? vbox_crtc->width : 640;
47 	height = vbox_crtc->height ? vbox_crtc->height : 480;
48 	bpp = fb ? fb->format->cpp[0] * 8 : 32;
49 	pitch = fb ? fb->pitches[0] : width * bpp / 8;
50 	x_offset = vbox->single_framebuffer ? vbox_crtc->x : vbox_crtc->x_hint;
51 	y_offset = vbox->single_framebuffer ? vbox_crtc->y : vbox_crtc->y_hint;
52 
53 	/*
54 	 * This is the old way of setting graphics modes.  It assumed one screen
55 	 * and a frame-buffer at the start of video RAM.  On older versions of
56 	 * VirtualBox, certain parts of the code still assume that the first
57 	 * screen is programmed this way, so try to fake it.
58 	 */
59 	if (vbox_crtc->crtc_id == 0 && fb &&
60 	    vbox_crtc->fb_offset / pitch < 0xffff - crtc->y &&
61 	    vbox_crtc->fb_offset % (bpp / 8) == 0) {
62 		vbox_write_ioport(VBE_DISPI_INDEX_XRES, width);
63 		vbox_write_ioport(VBE_DISPI_INDEX_YRES, height);
64 		vbox_write_ioport(VBE_DISPI_INDEX_VIRT_WIDTH, pitch * 8 / bpp);
65 		vbox_write_ioport(VBE_DISPI_INDEX_BPP, bpp);
66 		vbox_write_ioport(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED);
67 		vbox_write_ioport(VBE_DISPI_INDEX_X_OFFSET,
68 			vbox_crtc->fb_offset % pitch / bpp * 8 + vbox_crtc->x);
69 		vbox_write_ioport(VBE_DISPI_INDEX_Y_OFFSET,
70 				  vbox_crtc->fb_offset / pitch + vbox_crtc->y);
71 	}
72 
73 	flags = VBVA_SCREEN_F_ACTIVE;
74 	flags |= (fb && crtc->state->enable) ? 0 : VBVA_SCREEN_F_BLANK;
75 	flags |= vbox_crtc->disconnected ? VBVA_SCREEN_F_DISABLED : 0;
76 	hgsmi_process_display_info(vbox->guest_pool, vbox_crtc->crtc_id,
77 				   x_offset, y_offset,
78 				   vbox_crtc->x * bpp / 8 +
79 							vbox_crtc->y * pitch,
80 				   pitch, width, height, bpp, flags);
81 }
82 
vbox_set_view(struct drm_crtc * crtc)83 static int vbox_set_view(struct drm_crtc *crtc)
84 {
85 	struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
86 	struct vbox_private *vbox = crtc->dev->dev_private;
87 	struct vbva_infoview *p;
88 
89 	/*
90 	 * Tell the host about the view.  This design originally targeted the
91 	 * Windows XP driver architecture and assumed that each screen would
92 	 * have a dedicated frame buffer with the command buffer following it,
93 	 * the whole being a "view".  The host works out which screen a command
94 	 * buffer belongs to by checking whether it is in the first view, then
95 	 * whether it is in the second and so on.  The first match wins.  We
96 	 * cheat around this by making the first view be the managed memory
97 	 * plus the first command buffer, the second the same plus the second
98 	 * buffer and so on.
99 	 */
100 	p = hgsmi_buffer_alloc(vbox->guest_pool, sizeof(*p),
101 			       HGSMI_CH_VBVA, VBVA_INFO_VIEW);
102 	if (!p)
103 		return -ENOMEM;
104 
105 	p->view_index = vbox_crtc->crtc_id;
106 	p->view_offset = vbox_crtc->fb_offset;
107 	p->view_size = vbox->available_vram_size - vbox_crtc->fb_offset +
108 		       vbox_crtc->crtc_id * VBVA_MIN_BUFFER_SIZE;
109 	p->max_screen_size = vbox->available_vram_size - vbox_crtc->fb_offset;
110 
111 	hgsmi_buffer_submit(vbox->guest_pool, p);
112 	hgsmi_buffer_free(vbox->guest_pool, p);
113 
114 	return 0;
115 }
116 
117 /*
118  * Try to map the layout of virtual screens to the range of the input device.
119  * Return true if we need to re-set the crtc modes due to screen offset
120  * changes.
121  */
vbox_set_up_input_mapping(struct vbox_private * vbox)122 static bool vbox_set_up_input_mapping(struct vbox_private *vbox)
123 {
124 	struct drm_crtc *crtci;
125 	struct drm_connector *connectori;
126 	struct drm_framebuffer *fb, *fb1 = NULL;
127 	bool single_framebuffer = true;
128 	bool old_single_framebuffer = vbox->single_framebuffer;
129 	u16 width = 0, height = 0;
130 
131 	/*
132 	 * Are we using an X.Org-style single large frame-buffer for all crtcs?
133 	 * If so then screen layout can be deduced from the crtc offsets.
134 	 * Same fall-back if this is the fbdev frame-buffer.
135 	 */
136 	list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
137 		fb = crtci->primary->state->fb;
138 		if (!fb)
139 			continue;
140 
141 		if (!fb1) {
142 			fb1 = fb;
143 			if (fb1 == vbox->ddev.fb_helper->fb)
144 				break;
145 		} else if (fb != fb1) {
146 			single_framebuffer = false;
147 		}
148 	}
149 	if (!fb1)
150 		return false;
151 
152 	if (single_framebuffer) {
153 		vbox->single_framebuffer = true;
154 		vbox->input_mapping_width = fb1->width;
155 		vbox->input_mapping_height = fb1->height;
156 		return old_single_framebuffer != vbox->single_framebuffer;
157 	}
158 	/* Otherwise calculate the total span of all screens. */
159 	list_for_each_entry(connectori, &vbox->ddev.mode_config.connector_list,
160 			    head) {
161 		struct vbox_connector *vbox_connector =
162 		    to_vbox_connector(connectori);
163 		struct vbox_crtc *vbox_crtc = vbox_connector->vbox_crtc;
164 
165 		width = max_t(u16, width, vbox_crtc->x_hint +
166 					  vbox_connector->mode_hint.width);
167 		height = max_t(u16, height, vbox_crtc->y_hint +
168 					    vbox_connector->mode_hint.height);
169 	}
170 
171 	vbox->single_framebuffer = false;
172 	vbox->input_mapping_width = width;
173 	vbox->input_mapping_height = height;
174 
175 	return old_single_framebuffer != vbox->single_framebuffer;
176 }
177 
vbox_crtc_set_base_and_mode(struct drm_crtc * crtc,struct drm_framebuffer * fb,int x,int y)178 static void vbox_crtc_set_base_and_mode(struct drm_crtc *crtc,
179 					struct drm_framebuffer *fb,
180 					int x, int y)
181 {
182 	struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(fb->obj[0]);
183 	struct vbox_private *vbox = crtc->dev->dev_private;
184 	struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
185 	bool needs_modeset = drm_atomic_crtc_needs_modeset(crtc->state);
186 
187 	mutex_lock(&vbox->hw_mutex);
188 
189 	if (crtc->state->enable) {
190 		vbox_crtc->width = crtc->state->mode.hdisplay;
191 		vbox_crtc->height = crtc->state->mode.vdisplay;
192 	}
193 
194 	vbox_crtc->x = x;
195 	vbox_crtc->y = y;
196 	vbox_crtc->fb_offset = drm_gem_vram_offset(gbo);
197 
198 	/* vbox_do_modeset() checks vbox->single_framebuffer so update it now */
199 	if (needs_modeset && vbox_set_up_input_mapping(vbox)) {
200 		struct drm_crtc *crtci;
201 
202 		list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list,
203 				    head) {
204 			if (crtci == crtc)
205 				continue;
206 			vbox_do_modeset(crtci);
207 		}
208 	}
209 
210 	vbox_set_view(crtc);
211 	vbox_do_modeset(crtc);
212 
213 	if (needs_modeset)
214 		hgsmi_update_input_mapping(vbox->guest_pool, 0, 0,
215 					   vbox->input_mapping_width,
216 					   vbox->input_mapping_height);
217 
218 	mutex_unlock(&vbox->hw_mutex);
219 }
220 
vbox_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_crtc_state * old_crtc_state)221 static void vbox_crtc_atomic_enable(struct drm_crtc *crtc,
222 				    struct drm_crtc_state *old_crtc_state)
223 {
224 }
225 
vbox_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_crtc_state * old_crtc_state)226 static void vbox_crtc_atomic_disable(struct drm_crtc *crtc,
227 				     struct drm_crtc_state *old_crtc_state)
228 {
229 }
230 
vbox_crtc_atomic_flush(struct drm_crtc * crtc,struct drm_crtc_state * old_crtc_state)231 static void vbox_crtc_atomic_flush(struct drm_crtc *crtc,
232 				   struct drm_crtc_state *old_crtc_state)
233 {
234 	struct drm_pending_vblank_event *event;
235 	unsigned long flags;
236 
237 	if (crtc->state && crtc->state->event) {
238 		event = crtc->state->event;
239 		crtc->state->event = NULL;
240 
241 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
242 		drm_crtc_send_vblank_event(crtc, event);
243 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
244 	}
245 }
246 
247 static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = {
248 	.atomic_enable = vbox_crtc_atomic_enable,
249 	.atomic_disable = vbox_crtc_atomic_disable,
250 	.atomic_flush = vbox_crtc_atomic_flush,
251 };
252 
vbox_crtc_destroy(struct drm_crtc * crtc)253 static void vbox_crtc_destroy(struct drm_crtc *crtc)
254 {
255 	drm_crtc_cleanup(crtc);
256 	kfree(crtc);
257 }
258 
259 static const struct drm_crtc_funcs vbox_crtc_funcs = {
260 	.set_config = drm_atomic_helper_set_config,
261 	.page_flip = drm_atomic_helper_page_flip,
262 	/* .gamma_set = vbox_crtc_gamma_set, */
263 	.destroy = vbox_crtc_destroy,
264 	.reset = drm_atomic_helper_crtc_reset,
265 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
266 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
267 };
268 
vbox_primary_atomic_check(struct drm_plane * plane,struct drm_plane_state * new_state)269 static int vbox_primary_atomic_check(struct drm_plane *plane,
270 				     struct drm_plane_state *new_state)
271 {
272 	struct drm_crtc_state *crtc_state = NULL;
273 
274 	if (new_state->crtc) {
275 		crtc_state = drm_atomic_get_existing_crtc_state(
276 					    new_state->state, new_state->crtc);
277 		if (WARN_ON(!crtc_state))
278 			return -EINVAL;
279 	}
280 
281 	return drm_atomic_helper_check_plane_state(new_state, crtc_state,
282 						   DRM_PLANE_HELPER_NO_SCALING,
283 						   DRM_PLANE_HELPER_NO_SCALING,
284 						   false, true);
285 }
286 
vbox_primary_atomic_update(struct drm_plane * plane,struct drm_plane_state * old_state)287 static void vbox_primary_atomic_update(struct drm_plane *plane,
288 				       struct drm_plane_state *old_state)
289 {
290 	struct drm_crtc *crtc = plane->state->crtc;
291 	struct drm_framebuffer *fb = plane->state->fb;
292 	struct vbox_private *vbox = fb->dev->dev_private;
293 	struct drm_mode_rect *clips;
294 	uint32_t num_clips, i;
295 
296 	vbox_crtc_set_base_and_mode(crtc, fb,
297 				    plane->state->src_x >> 16,
298 				    plane->state->src_y >> 16);
299 
300 	/* Send information about dirty rectangles to VBVA. */
301 
302 	clips = drm_plane_get_damage_clips(plane->state);
303 	num_clips = drm_plane_get_damage_clips_count(plane->state);
304 
305 	if (!num_clips)
306 		return;
307 
308 	mutex_lock(&vbox->hw_mutex);
309 
310 	for (i = 0; i < num_clips; ++i, ++clips) {
311 		struct vbva_cmd_hdr cmd_hdr;
312 		unsigned int crtc_id = to_vbox_crtc(crtc)->crtc_id;
313 
314 		cmd_hdr.x = (s16)clips->x1;
315 		cmd_hdr.y = (s16)clips->y1;
316 		cmd_hdr.w = (u16)clips->x2 - clips->x1;
317 		cmd_hdr.h = (u16)clips->y2 - clips->y1;
318 
319 		if (!vbva_buffer_begin_update(&vbox->vbva_info[crtc_id],
320 					      vbox->guest_pool))
321 			continue;
322 
323 		vbva_write(&vbox->vbva_info[crtc_id], vbox->guest_pool,
324 			   &cmd_hdr, sizeof(cmd_hdr));
325 		vbva_buffer_end_update(&vbox->vbva_info[crtc_id]);
326 	}
327 
328 	mutex_unlock(&vbox->hw_mutex);
329 }
330 
vbox_primary_atomic_disable(struct drm_plane * plane,struct drm_plane_state * old_state)331 static void vbox_primary_atomic_disable(struct drm_plane *plane,
332 					struct drm_plane_state *old_state)
333 {
334 	struct drm_crtc *crtc = old_state->crtc;
335 
336 	/* vbox_do_modeset checks plane->state->fb and will disable if NULL */
337 	vbox_crtc_set_base_and_mode(crtc, old_state->fb,
338 				    old_state->src_x >> 16,
339 				    old_state->src_y >> 16);
340 }
341 
vbox_cursor_atomic_check(struct drm_plane * plane,struct drm_plane_state * new_state)342 static int vbox_cursor_atomic_check(struct drm_plane *plane,
343 				    struct drm_plane_state *new_state)
344 {
345 	struct drm_crtc_state *crtc_state = NULL;
346 	u32 width = new_state->crtc_w;
347 	u32 height = new_state->crtc_h;
348 	int ret;
349 
350 	if (new_state->crtc) {
351 		crtc_state = drm_atomic_get_existing_crtc_state(
352 					    new_state->state, new_state->crtc);
353 		if (WARN_ON(!crtc_state))
354 			return -EINVAL;
355 	}
356 
357 	ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
358 						  DRM_PLANE_HELPER_NO_SCALING,
359 						  DRM_PLANE_HELPER_NO_SCALING,
360 						  true, true);
361 	if (ret)
362 		return ret;
363 
364 	if (!new_state->fb)
365 		return 0;
366 
367 	if (width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT ||
368 	    width == 0 || height == 0)
369 		return -EINVAL;
370 
371 	return 0;
372 }
373 
374 /*
375  * Copy the ARGB image and generate the mask, which is needed in case the host
376  * does not support ARGB cursors.  The mask is a 1BPP bitmap with the bit set
377  * if the corresponding alpha value in the ARGB image is greater than 0xF0.
378  */
copy_cursor_image(u8 * src,u8 * dst,u32 width,u32 height,size_t mask_size)379 static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height,
380 			      size_t mask_size)
381 {
382 	size_t line_size = (width + 7) / 8;
383 	u32 i, j;
384 
385 	memcpy(dst + mask_size, src, width * height * 4);
386 	for (i = 0; i < height; ++i)
387 		for (j = 0; j < width; ++j)
388 			if (((u32 *)src)[i * width + j] > 0xf0000000)
389 				dst[i * line_size + j / 8] |= (0x80 >> (j % 8));
390 }
391 
vbox_cursor_atomic_update(struct drm_plane * plane,struct drm_plane_state * old_state)392 static void vbox_cursor_atomic_update(struct drm_plane *plane,
393 				      struct drm_plane_state *old_state)
394 {
395 	struct vbox_private *vbox =
396 		container_of(plane->dev, struct vbox_private, ddev);
397 	struct vbox_crtc *vbox_crtc = to_vbox_crtc(plane->state->crtc);
398 	struct drm_framebuffer *fb = plane->state->fb;
399 	struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(fb->obj[0]);
400 	u32 width = plane->state->crtc_w;
401 	u32 height = plane->state->crtc_h;
402 	size_t data_size, mask_size;
403 	u32 flags;
404 	u8 *src;
405 
406 	/*
407 	 * VirtualBox uses the host windowing system to draw the cursor so
408 	 * moves are a no-op, we only need to upload new cursor sprites.
409 	 */
410 	if (fb == old_state->fb)
411 		return;
412 
413 	mutex_lock(&vbox->hw_mutex);
414 
415 	vbox_crtc->cursor_enabled = true;
416 
417 	/* pinning is done in prepare/cleanup framebuffer */
418 	src = drm_gem_vram_kmap(gbo, true, NULL);
419 	if (IS_ERR(src)) {
420 		mutex_unlock(&vbox->hw_mutex);
421 		DRM_WARN("Could not kmap cursor bo, skipping update\n");
422 		return;
423 	}
424 
425 	/*
426 	 * The mask must be calculated based on the alpha
427 	 * channel, one bit per ARGB word, and must be 32-bit
428 	 * padded.
429 	 */
430 	mask_size = ((width + 7) / 8 * height + 3) & ~3;
431 	data_size = width * height * 4 + mask_size;
432 
433 	copy_cursor_image(src, vbox->cursor_data, width, height, mask_size);
434 	drm_gem_vram_kunmap(gbo);
435 
436 	flags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE |
437 		VBOX_MOUSE_POINTER_ALPHA;
438 	hgsmi_update_pointer_shape(vbox->guest_pool, flags,
439 				   min_t(u32, max(fb->hot_x, 0), width),
440 				   min_t(u32, max(fb->hot_y, 0), height),
441 				   width, height, vbox->cursor_data, data_size);
442 
443 	mutex_unlock(&vbox->hw_mutex);
444 }
445 
vbox_cursor_atomic_disable(struct drm_plane * plane,struct drm_plane_state * old_state)446 static void vbox_cursor_atomic_disable(struct drm_plane *plane,
447 				       struct drm_plane_state *old_state)
448 {
449 	struct vbox_private *vbox =
450 		container_of(plane->dev, struct vbox_private, ddev);
451 	struct vbox_crtc *vbox_crtc = to_vbox_crtc(old_state->crtc);
452 	bool cursor_enabled = false;
453 	struct drm_crtc *crtci;
454 
455 	mutex_lock(&vbox->hw_mutex);
456 
457 	vbox_crtc->cursor_enabled = false;
458 
459 	list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
460 		if (to_vbox_crtc(crtci)->cursor_enabled)
461 			cursor_enabled = true;
462 	}
463 
464 	if (!cursor_enabled)
465 		hgsmi_update_pointer_shape(vbox->guest_pool, 0, 0, 0,
466 					   0, 0, NULL, 0);
467 
468 	mutex_unlock(&vbox->hw_mutex);
469 }
470 
471 static const u32 vbox_cursor_plane_formats[] = {
472 	DRM_FORMAT_ARGB8888,
473 };
474 
475 static const struct drm_plane_helper_funcs vbox_cursor_helper_funcs = {
476 	.atomic_check	= vbox_cursor_atomic_check,
477 	.atomic_update	= vbox_cursor_atomic_update,
478 	.atomic_disable	= vbox_cursor_atomic_disable,
479 	.prepare_fb	= drm_gem_vram_plane_helper_prepare_fb,
480 	.cleanup_fb	= drm_gem_vram_plane_helper_cleanup_fb,
481 };
482 
483 static const struct drm_plane_funcs vbox_cursor_plane_funcs = {
484 	.update_plane	= drm_atomic_helper_update_plane,
485 	.disable_plane	= drm_atomic_helper_disable_plane,
486 	.destroy	= drm_primary_helper_destroy,
487 	.reset		= drm_atomic_helper_plane_reset,
488 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
489 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
490 };
491 
492 static const u32 vbox_primary_plane_formats[] = {
493 	DRM_FORMAT_XRGB8888,
494 	DRM_FORMAT_ARGB8888,
495 };
496 
497 static const struct drm_plane_helper_funcs vbox_primary_helper_funcs = {
498 	.atomic_check = vbox_primary_atomic_check,
499 	.atomic_update = vbox_primary_atomic_update,
500 	.atomic_disable = vbox_primary_atomic_disable,
501 	.prepare_fb	= drm_gem_vram_plane_helper_prepare_fb,
502 	.cleanup_fb	= drm_gem_vram_plane_helper_cleanup_fb,
503 };
504 
505 static const struct drm_plane_funcs vbox_primary_plane_funcs = {
506 	.update_plane	= drm_atomic_helper_update_plane,
507 	.disable_plane	= drm_atomic_helper_disable_plane,
508 	.destroy	= drm_primary_helper_destroy,
509 	.reset		= drm_atomic_helper_plane_reset,
510 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
511 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
512 };
513 
vbox_create_plane(struct vbox_private * vbox,unsigned int possible_crtcs,enum drm_plane_type type)514 static struct drm_plane *vbox_create_plane(struct vbox_private *vbox,
515 					   unsigned int possible_crtcs,
516 					   enum drm_plane_type type)
517 {
518 	const struct drm_plane_helper_funcs *helper_funcs = NULL;
519 	const struct drm_plane_funcs *funcs;
520 	struct drm_plane *plane;
521 	const u32 *formats;
522 	int num_formats;
523 	int err;
524 
525 	if (type == DRM_PLANE_TYPE_PRIMARY) {
526 		funcs = &vbox_primary_plane_funcs;
527 		formats = vbox_primary_plane_formats;
528 		helper_funcs = &vbox_primary_helper_funcs;
529 		num_formats = ARRAY_SIZE(vbox_primary_plane_formats);
530 	} else if (type == DRM_PLANE_TYPE_CURSOR) {
531 		funcs = &vbox_cursor_plane_funcs;
532 		formats = vbox_cursor_plane_formats;
533 		helper_funcs = &vbox_cursor_helper_funcs;
534 		num_formats = ARRAY_SIZE(vbox_cursor_plane_formats);
535 	} else {
536 		return ERR_PTR(-EINVAL);
537 	}
538 
539 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
540 	if (!plane)
541 		return ERR_PTR(-ENOMEM);
542 
543 	err = drm_universal_plane_init(&vbox->ddev, plane, possible_crtcs,
544 				       funcs, formats, num_formats,
545 				       NULL, type, NULL);
546 	if (err)
547 		goto free_plane;
548 
549 	drm_plane_helper_add(plane, helper_funcs);
550 
551 	return plane;
552 
553 free_plane:
554 	kfree(plane);
555 	return ERR_PTR(-EINVAL);
556 }
557 
vbox_crtc_init(struct drm_device * dev,unsigned int i)558 static struct vbox_crtc *vbox_crtc_init(struct drm_device *dev, unsigned int i)
559 {
560 	struct vbox_private *vbox =
561 		container_of(dev, struct vbox_private, ddev);
562 	struct drm_plane *cursor = NULL;
563 	struct vbox_crtc *vbox_crtc;
564 	struct drm_plane *primary;
565 	u32 caps = 0;
566 	int ret;
567 
568 	ret = hgsmi_query_conf(vbox->guest_pool,
569 			       VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps);
570 	if (ret)
571 		return ERR_PTR(ret);
572 
573 	vbox_crtc = kzalloc(sizeof(*vbox_crtc), GFP_KERNEL);
574 	if (!vbox_crtc)
575 		return ERR_PTR(-ENOMEM);
576 
577 	primary = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_PRIMARY);
578 	if (IS_ERR(primary)) {
579 		ret = PTR_ERR(primary);
580 		goto free_mem;
581 	}
582 
583 	if ((caps & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)) {
584 		cursor = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_CURSOR);
585 		if (IS_ERR(cursor)) {
586 			ret = PTR_ERR(cursor);
587 			goto clean_primary;
588 		}
589 	} else {
590 		DRM_WARN("VirtualBox host is too old, no cursor support\n");
591 	}
592 
593 	vbox_crtc->crtc_id = i;
594 
595 	ret = drm_crtc_init_with_planes(dev, &vbox_crtc->base, primary, cursor,
596 					&vbox_crtc_funcs, NULL);
597 	if (ret)
598 		goto clean_cursor;
599 
600 	drm_mode_crtc_set_gamma_size(&vbox_crtc->base, 256);
601 	drm_crtc_helper_add(&vbox_crtc->base, &vbox_crtc_helper_funcs);
602 
603 	return vbox_crtc;
604 
605 clean_cursor:
606 	if (cursor) {
607 		drm_plane_cleanup(cursor);
608 		kfree(cursor);
609 	}
610 clean_primary:
611 	drm_plane_cleanup(primary);
612 	kfree(primary);
613 free_mem:
614 	kfree(vbox_crtc);
615 	return ERR_PTR(ret);
616 }
617 
vbox_encoder_destroy(struct drm_encoder * encoder)618 static void vbox_encoder_destroy(struct drm_encoder *encoder)
619 {
620 	drm_encoder_cleanup(encoder);
621 	kfree(encoder);
622 }
623 
624 static const struct drm_encoder_funcs vbox_enc_funcs = {
625 	.destroy = vbox_encoder_destroy,
626 };
627 
vbox_encoder_init(struct drm_device * dev,unsigned int i)628 static struct drm_encoder *vbox_encoder_init(struct drm_device *dev,
629 					     unsigned int i)
630 {
631 	struct vbox_encoder *vbox_encoder;
632 
633 	vbox_encoder = kzalloc(sizeof(*vbox_encoder), GFP_KERNEL);
634 	if (!vbox_encoder)
635 		return NULL;
636 
637 	drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs,
638 			 DRM_MODE_ENCODER_DAC, NULL);
639 
640 	vbox_encoder->base.possible_crtcs = 1 << i;
641 	return &vbox_encoder->base;
642 }
643 
644 /*
645  * Generate EDID data with a mode-unique serial number for the virtual
646  * monitor to try to persuade Unity that different modes correspond to
647  * different monitors and it should not try to force the same resolution on
648  * them.
649  */
vbox_set_edid(struct drm_connector * connector,int width,int height)650 static void vbox_set_edid(struct drm_connector *connector, int width,
651 			  int height)
652 {
653 	enum { EDID_SIZE = 128 };
654 	unsigned char edid[EDID_SIZE] = {
655 		0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,	/* header */
656 		0x58, 0x58,	/* manufacturer (VBX) */
657 		0x00, 0x00,	/* product code */
658 		0x00, 0x00, 0x00, 0x00,	/* serial number goes here */
659 		0x01,		/* week of manufacture */
660 		0x00,		/* year of manufacture */
661 		0x01, 0x03,	/* EDID version */
662 		0x80,		/* capabilities - digital */
663 		0x00,		/* horiz. res in cm, zero for projectors */
664 		0x00,		/* vert. res in cm */
665 		0x78,		/* display gamma (120 == 2.2). */
666 		0xEE,		/* features (standby, suspend, off, RGB, std */
667 				/* colour space, preferred timing mode) */
668 		0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
669 		/* chromaticity for standard colour space. */
670 		0x00, 0x00, 0x00,	/* no default timings */
671 		0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
672 		    0x01, 0x01,
673 		0x01, 0x01, 0x01, 0x01,	/* no standard timings */
674 		0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
675 		    0x02, 0x02,
676 		/* descriptor block 1 goes below */
677 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
678 		/* descriptor block 2, monitor ranges */
679 		0x00, 0x00, 0x00, 0xFD, 0x00,
680 		0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
681 		    0x20, 0x20,
682 		/* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
683 		0x20,
684 		/* descriptor block 3, monitor name */
685 		0x00, 0x00, 0x00, 0xFC, 0x00,
686 		'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r',
687 		'\n',
688 		/* descriptor block 4: dummy data */
689 		0x00, 0x00, 0x00, 0x10, 0x00,
690 		0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
691 		0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
692 		0x20,
693 		0x00,		/* number of extensions */
694 		0x00		/* checksum goes here */
695 	};
696 	int clock = (width + 6) * (height + 6) * 60 / 10000;
697 	unsigned int i, sum = 0;
698 
699 	edid[12] = width & 0xff;
700 	edid[13] = width >> 8;
701 	edid[14] = height & 0xff;
702 	edid[15] = height >> 8;
703 	edid[54] = clock & 0xff;
704 	edid[55] = clock >> 8;
705 	edid[56] = width & 0xff;
706 	edid[58] = (width >> 4) & 0xf0;
707 	edid[59] = height & 0xff;
708 	edid[61] = (height >> 4) & 0xf0;
709 	for (i = 0; i < EDID_SIZE - 1; ++i)
710 		sum += edid[i];
711 	edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF;
712 	drm_connector_update_edid_property(connector, (struct edid *)edid);
713 }
714 
vbox_get_modes(struct drm_connector * connector)715 static int vbox_get_modes(struct drm_connector *connector)
716 {
717 	struct vbox_connector *vbox_connector = NULL;
718 	struct drm_display_mode *mode = NULL;
719 	struct vbox_private *vbox = NULL;
720 	unsigned int num_modes = 0;
721 	int preferred_width, preferred_height;
722 
723 	vbox_connector = to_vbox_connector(connector);
724 	vbox = connector->dev->dev_private;
725 
726 	hgsmi_report_flags_location(vbox->guest_pool, GUEST_HEAP_OFFSET(vbox) +
727 				    HOST_FLAGS_OFFSET);
728 	if (vbox_connector->vbox_crtc->crtc_id == 0)
729 		vbox_report_caps(vbox);
730 
731 	num_modes = drm_add_modes_noedid(connector, 2560, 1600);
732 	preferred_width = vbox_connector->mode_hint.width ?
733 			  vbox_connector->mode_hint.width : 1024;
734 	preferred_height = vbox_connector->mode_hint.height ?
735 			   vbox_connector->mode_hint.height : 768;
736 	mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height,
737 			    60, false, false, false);
738 	if (mode) {
739 		mode->type |= DRM_MODE_TYPE_PREFERRED;
740 		drm_mode_probed_add(connector, mode);
741 		++num_modes;
742 	}
743 	vbox_set_edid(connector, preferred_width, preferred_height);
744 
745 	if (vbox_connector->vbox_crtc->x_hint != -1)
746 		drm_object_property_set_value(&connector->base,
747 			vbox->ddev.mode_config.suggested_x_property,
748 			vbox_connector->vbox_crtc->x_hint);
749 	else
750 		drm_object_property_set_value(&connector->base,
751 			vbox->ddev.mode_config.suggested_x_property, 0);
752 
753 	if (vbox_connector->vbox_crtc->y_hint != -1)
754 		drm_object_property_set_value(&connector->base,
755 			vbox->ddev.mode_config.suggested_y_property,
756 			vbox_connector->vbox_crtc->y_hint);
757 	else
758 		drm_object_property_set_value(&connector->base,
759 			vbox->ddev.mode_config.suggested_y_property, 0);
760 
761 	return num_modes;
762 }
763 
vbox_connector_destroy(struct drm_connector * connector)764 static void vbox_connector_destroy(struct drm_connector *connector)
765 {
766 	drm_connector_unregister(connector);
767 	drm_connector_cleanup(connector);
768 	kfree(connector);
769 }
770 
771 static enum drm_connector_status
vbox_connector_detect(struct drm_connector * connector,bool force)772 vbox_connector_detect(struct drm_connector *connector, bool force)
773 {
774 	struct vbox_connector *vbox_connector;
775 
776 	vbox_connector = to_vbox_connector(connector);
777 
778 	return vbox_connector->mode_hint.disconnected ?
779 	    connector_status_disconnected : connector_status_connected;
780 }
781 
vbox_fill_modes(struct drm_connector * connector,u32 max_x,u32 max_y)782 static int vbox_fill_modes(struct drm_connector *connector, u32 max_x,
783 			   u32 max_y)
784 {
785 	struct vbox_connector *vbox_connector;
786 	struct drm_device *dev;
787 	struct drm_display_mode *mode, *iterator;
788 
789 	vbox_connector = to_vbox_connector(connector);
790 	dev = vbox_connector->base.dev;
791 	list_for_each_entry_safe(mode, iterator, &connector->modes, head) {
792 		list_del(&mode->head);
793 		drm_mode_destroy(dev, mode);
794 	}
795 
796 	return drm_helper_probe_single_connector_modes(connector, max_x, max_y);
797 }
798 
799 static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = {
800 	.get_modes = vbox_get_modes,
801 };
802 
803 static const struct drm_connector_funcs vbox_connector_funcs = {
804 	.detect = vbox_connector_detect,
805 	.fill_modes = vbox_fill_modes,
806 	.destroy = vbox_connector_destroy,
807 	.reset = drm_atomic_helper_connector_reset,
808 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
809 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
810 };
811 
vbox_connector_init(struct drm_device * dev,struct vbox_crtc * vbox_crtc,struct drm_encoder * encoder)812 static int vbox_connector_init(struct drm_device *dev,
813 			       struct vbox_crtc *vbox_crtc,
814 			       struct drm_encoder *encoder)
815 {
816 	struct vbox_connector *vbox_connector;
817 	struct drm_connector *connector;
818 
819 	vbox_connector = kzalloc(sizeof(*vbox_connector), GFP_KERNEL);
820 	if (!vbox_connector)
821 		return -ENOMEM;
822 
823 	connector = &vbox_connector->base;
824 	vbox_connector->vbox_crtc = vbox_crtc;
825 
826 	drm_connector_init(dev, connector, &vbox_connector_funcs,
827 			   DRM_MODE_CONNECTOR_VGA);
828 	drm_connector_helper_add(connector, &vbox_connector_helper_funcs);
829 
830 	connector->interlace_allowed = 0;
831 	connector->doublescan_allowed = 0;
832 
833 	drm_mode_create_suggested_offset_properties(dev);
834 	drm_object_attach_property(&connector->base,
835 				   dev->mode_config.suggested_x_property, 0);
836 	drm_object_attach_property(&connector->base,
837 				   dev->mode_config.suggested_y_property, 0);
838 
839 	drm_connector_attach_encoder(connector, encoder);
840 
841 	return 0;
842 }
843 
844 static const struct drm_mode_config_funcs vbox_mode_funcs = {
845 	.fb_create = drm_gem_fb_create_with_dirty,
846 	.atomic_check = drm_atomic_helper_check,
847 	.atomic_commit = drm_atomic_helper_commit,
848 };
849 
vbox_mode_init(struct vbox_private * vbox)850 int vbox_mode_init(struct vbox_private *vbox)
851 {
852 	struct drm_device *dev = &vbox->ddev;
853 	struct drm_encoder *encoder;
854 	struct vbox_crtc *vbox_crtc;
855 	unsigned int i;
856 	int ret;
857 
858 	drm_mode_config_init(dev);
859 
860 	dev->mode_config.funcs = (void *)&vbox_mode_funcs;
861 	dev->mode_config.min_width = 0;
862 	dev->mode_config.min_height = 0;
863 	dev->mode_config.preferred_depth = 24;
864 	dev->mode_config.max_width = VBE_DISPI_MAX_XRES;
865 	dev->mode_config.max_height = VBE_DISPI_MAX_YRES;
866 
867 	for (i = 0; i < vbox->num_crtcs; ++i) {
868 		vbox_crtc = vbox_crtc_init(dev, i);
869 		if (IS_ERR(vbox_crtc)) {
870 			ret = PTR_ERR(vbox_crtc);
871 			goto err_drm_mode_cleanup;
872 		}
873 		encoder = vbox_encoder_init(dev, i);
874 		if (!encoder) {
875 			ret = -ENOMEM;
876 			goto err_drm_mode_cleanup;
877 		}
878 		ret = vbox_connector_init(dev, vbox_crtc, encoder);
879 		if (ret)
880 			goto err_drm_mode_cleanup;
881 	}
882 
883 	drm_mode_config_reset(dev);
884 	return 0;
885 
886 err_drm_mode_cleanup:
887 	drm_mode_config_cleanup(dev);
888 	return ret;
889 }
890 
vbox_mode_fini(struct vbox_private * vbox)891 void vbox_mode_fini(struct vbox_private *vbox)
892 {
893 	drm_mode_config_cleanup(&vbox->ddev);
894 }
895