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