Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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