File Index
Files and directories
Sign in
FILE

vbox_mode.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/vboxvideo/vbox_mode.c C
  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_print.h>
 26#include <drm/drm_probe_helper.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 */
 36static 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 = to_vbox_dev(crtc->dev);
 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
 83static int vbox_set_view(struct drm_crtc *crtc)
 84{
 85	struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
 86	struct vbox_private *vbox = to_vbox_dev(crtc->dev);
 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 */
122static 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
178static 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 = to_vbox_dev(crtc->dev);
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
221static void vbox_crtc_atomic_enable(struct drm_crtc *crtc,
222				    struct drm_atomic_state *state)
223{
224}
225
226static void vbox_crtc_atomic_disable(struct drm_crtc *crtc,
227				     struct drm_atomic_state *state)
228{
229}
230
231static void vbox_crtc_atomic_flush(struct drm_crtc *crtc,
232				   struct drm_atomic_state *state)
233{
234}
235
236static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = {
237	.atomic_enable = vbox_crtc_atomic_enable,
238	.atomic_disable = vbox_crtc_atomic_disable,
239	.atomic_flush = vbox_crtc_atomic_flush,
240};
241
242static void vbox_crtc_destroy(struct drm_crtc *crtc)
243{
244	drm_crtc_cleanup(crtc);
245	kfree(crtc);
246}
247
248static const struct drm_crtc_funcs vbox_crtc_funcs = {
249	.set_config = drm_atomic_helper_set_config,
250	.page_flip = drm_atomic_helper_page_flip,
251	/* .gamma_set = vbox_crtc_gamma_set, */
252	.destroy = vbox_crtc_destroy,
253	.reset = drm_atomic_helper_crtc_reset,
254	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
255	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
256};
257
258static int vbox_primary_atomic_check(struct drm_plane *plane,
259				     struct drm_atomic_state *state)
260{
261	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
262									   plane);
263	struct drm_crtc_state *crtc_state = NULL;
264
265	if (new_state->crtc) {
266		crtc_state = drm_atomic_get_new_crtc_state(state,
267							   new_state->crtc);
268		if (WARN_ON(!crtc_state))
269			return -EINVAL;
270	}
271
272	return drm_atomic_helper_check_plane_state(new_state, crtc_state,
273						   DRM_PLANE_NO_SCALING,
274						   DRM_PLANE_NO_SCALING,
275						   false, true);
276}
277
278static void vbox_primary_atomic_update(struct drm_plane *plane,
279				       struct drm_atomic_state *state)
280{
281	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
282									   plane);
283	struct drm_crtc *crtc = new_state->crtc;
284	struct drm_framebuffer *fb = new_state->fb;
285	struct vbox_private *vbox = to_vbox_dev(fb->dev);
286	struct drm_mode_rect *clips;
287	uint32_t num_clips, i;
288
289	vbox_crtc_set_base_and_mode(crtc, fb,
290				    new_state->src_x >> 16,
291				    new_state->src_y >> 16);
292
293	/* Send information about dirty rectangles to VBVA. */
294
295	clips = drm_plane_get_damage_clips(new_state);
296	num_clips = drm_plane_get_damage_clips_count(new_state);
297
298	if (!num_clips)
299		return;
300
301	mutex_lock(&vbox->hw_mutex);
302
303	for (i = 0; i < num_clips; ++i, ++clips) {
304		struct vbva_cmd_hdr cmd_hdr;
305		unsigned int crtc_id = to_vbox_crtc(crtc)->crtc_id;
306
307		cmd_hdr.x = (s16)clips->x1;
308		cmd_hdr.y = (s16)clips->y1;
309		cmd_hdr.w = (u16)clips->x2 - clips->x1;
310		cmd_hdr.h = (u16)clips->y2 - clips->y1;
311
312		if (!vbva_buffer_begin_update(&vbox->vbva_info[crtc_id],
313					      vbox->guest_pool))
314			continue;
315
316		vbva_write(&vbox->vbva_info[crtc_id], vbox->guest_pool,
317			   &cmd_hdr, sizeof(cmd_hdr));
318		vbva_buffer_end_update(&vbox->vbva_info[crtc_id]);
319	}
320
321	mutex_unlock(&vbox->hw_mutex);
322}
323
324static void vbox_primary_atomic_disable(struct drm_plane *plane,
325					struct drm_atomic_state *state)
326{
327	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
328									   plane);
329	struct drm_crtc *crtc = old_state->crtc;
330
331	/* vbox_do_modeset checks plane->state->fb and will disable if NULL */
332	vbox_crtc_set_base_and_mode(crtc, old_state->fb,
333				    old_state->src_x >> 16,
334				    old_state->src_y >> 16);
335}
336
337static int vbox_cursor_atomic_check(struct drm_plane *plane,
338				    struct drm_atomic_state *state)
339{
340	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
341									   plane);
342	struct drm_crtc_state *crtc_state = NULL;
343	u32 width = new_state->crtc_w;
344	u32 height = new_state->crtc_h;
345	int ret;
346
347	if (new_state->crtc) {
348		crtc_state = drm_atomic_get_new_crtc_state(state,
349							   new_state->crtc);
350		if (WARN_ON(!crtc_state))
351			return -EINVAL;
352	}
353
354	ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
355						  DRM_PLANE_NO_SCALING,
356						  DRM_PLANE_NO_SCALING,
357						  true, true);
358	if (ret)
359		return ret;
360
361	if (!new_state->fb)
362		return 0;
363
364	if (width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT ||
365	    width == 0 || height == 0)
366		return -EINVAL;
367
368	return 0;
369}
370
371/*
372 * Copy the ARGB image and generate the mask, which is needed in case the host
373 * does not support ARGB cursors.  The mask is a 1BPP bitmap with the bit set
374 * if the corresponding alpha value in the ARGB image is greater than 0xF0.
375 */
376static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height,
377			      size_t mask_size)
378{
379	size_t line_size = (width + 7) / 8;
380	u32 i, j;
381
382	memcpy(dst + mask_size, src, width * height * 4);
383	for (i = 0; i < height; ++i)
384		for (j = 0; j < width; ++j)
385			if (((u32 *)src)[i * width + j] > 0xf0000000)
386				dst[i * line_size + j / 8] |= (0x80 >> (j % 8));
387}
388
389static void vbox_cursor_atomic_update(struct drm_plane *plane,
390				      struct drm_atomic_state *state)
391{
392	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
393									   plane);
394	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
395									   plane);
396	struct vbox_private *vbox =
397		container_of(plane->dev, struct vbox_private, ddev);
398	struct vbox_crtc *vbox_crtc = to_vbox_crtc(new_state->crtc);
399	struct drm_framebuffer *fb = new_state->fb;
400	u32 width = new_state->crtc_w;
401	u32 height = new_state->crtc_h;
402	struct drm_shadow_plane_state *shadow_plane_state =
403		to_drm_shadow_plane_state(new_state);
404	struct iosys_map map = shadow_plane_state->data[0];
405	u8 *src = map.vaddr; /* TODO: Use mapping abstraction properly */
406	size_t data_size, mask_size;
407	u32 flags;
408
409	/*
410	 * VirtualBox uses the host windowing system to draw the cursor so
411	 * moves are a no-op, we only need to upload new cursor sprites.
412	 */
413	if (fb == old_state->fb)
414		return;
415
416	mutex_lock(&vbox->hw_mutex);
417
418	vbox_crtc->cursor_enabled = true;
419
420	/*
421	 * The mask must be calculated based on the alpha
422	 * channel, one bit per ARGB word, and must be 32-bit
423	 * padded.
424	 */
425	mask_size = ((width + 7) / 8 * height + 3) & ~3;
426	data_size = width * height * 4 + mask_size;
427
428	copy_cursor_image(src, vbox->cursor_data, width, height, mask_size);
429
430	flags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE |
431		VBOX_MOUSE_POINTER_ALPHA;
432	hgsmi_update_pointer_shape(vbox->guest_pool, flags,
433				   min_t(u32, max(new_state->hotspot_x, 0), width),
434				   min_t(u32, max(new_state->hotspot_y, 0), height),
435				   width, height, vbox->cursor_data, data_size);
436
437	mutex_unlock(&vbox->hw_mutex);
438}
439
440static void vbox_cursor_atomic_disable(struct drm_plane *plane,
441				       struct drm_atomic_state *state)
442{
443	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
444									   plane);
445	struct vbox_private *vbox =
446		container_of(plane->dev, struct vbox_private, ddev);
447	struct vbox_crtc *vbox_crtc = to_vbox_crtc(old_state->crtc);
448	bool cursor_enabled = false;
449	struct drm_crtc *crtci;
450
451	mutex_lock(&vbox->hw_mutex);
452
453	vbox_crtc->cursor_enabled = false;
454
455	list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
456		if (to_vbox_crtc(crtci)->cursor_enabled)
457			cursor_enabled = true;
458	}
459
460	if (!cursor_enabled)
461		hgsmi_update_pointer_shape(vbox->guest_pool, 0, 0, 0,
462					   0, 0, NULL, 0);
463
464	mutex_unlock(&vbox->hw_mutex);
465}
466
467static const u32 vbox_cursor_plane_formats[] = {
468	DRM_FORMAT_ARGB8888,
469};
470
471static const struct drm_plane_helper_funcs vbox_cursor_helper_funcs = {
472	.atomic_check	= vbox_cursor_atomic_check,
473	.atomic_update	= vbox_cursor_atomic_update,
474	.atomic_disable	= vbox_cursor_atomic_disable,
475	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
476};
477
478static const struct drm_plane_funcs vbox_cursor_plane_funcs = {
479	.update_plane	= drm_atomic_helper_update_plane,
480	.disable_plane	= drm_atomic_helper_disable_plane,
481	.destroy	= drm_plane_helper_destroy,
482	DRM_GEM_SHADOW_PLANE_FUNCS,
483};
484
485static const u32 vbox_primary_plane_formats[] = {
486	DRM_FORMAT_XRGB8888,
487	DRM_FORMAT_ARGB8888,
488};
489
490static const struct drm_plane_helper_funcs vbox_primary_helper_funcs = {
491	.atomic_check = vbox_primary_atomic_check,
492	.atomic_update = vbox_primary_atomic_update,
493	.atomic_disable = vbox_primary_atomic_disable,
494	DRM_GEM_VRAM_PLANE_HELPER_FUNCS,
495};
496
497static const struct drm_plane_funcs vbox_primary_plane_funcs = {
498	.update_plane	= drm_atomic_helper_update_plane,
499	.disable_plane	= drm_atomic_helper_disable_plane,
500	.destroy	= drm_plane_helper_destroy,
501	.reset		= drm_atomic_helper_plane_reset,
502	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
503	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
504};
505
506static struct drm_plane *vbox_create_plane(struct vbox_private *vbox,
507					   unsigned int possible_crtcs,
508					   enum drm_plane_type type)
509{
510	const struct drm_plane_helper_funcs *helper_funcs = NULL;
511	const struct drm_plane_funcs *funcs;
512	struct drm_plane *plane;
513	const u32 *formats;
514	int num_formats;
515	int err;
516
517	if (type == DRM_PLANE_TYPE_PRIMARY) {
518		funcs = &vbox_primary_plane_funcs;
519		formats = vbox_primary_plane_formats;
520		helper_funcs = &vbox_primary_helper_funcs;
521		num_formats = ARRAY_SIZE(vbox_primary_plane_formats);
522	} else if (type == DRM_PLANE_TYPE_CURSOR) {
523		funcs = &vbox_cursor_plane_funcs;
524		formats = vbox_cursor_plane_formats;
525		helper_funcs = &vbox_cursor_helper_funcs;
526		num_formats = ARRAY_SIZE(vbox_cursor_plane_formats);
527	} else {
528		return ERR_PTR(-EINVAL);
529	}
530
531	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
532	if (!plane)
533		return ERR_PTR(-ENOMEM);
534
535	err = drm_universal_plane_init(&vbox->ddev, plane, possible_crtcs,
536				       funcs, formats, num_formats,
537				       NULL, type, NULL);
538	if (err)
539		goto free_plane;
540
541	drm_plane_helper_add(plane, helper_funcs);
542
543	return plane;
544
545free_plane:
546	kfree(plane);
547	return ERR_PTR(-EINVAL);
548}
549
550static struct vbox_crtc *vbox_crtc_init(struct drm_device *dev, unsigned int i)
551{
552	struct vbox_private *vbox =
553		container_of(dev, struct vbox_private, ddev);
554	struct drm_plane *cursor = NULL;
555	struct vbox_crtc *vbox_crtc;
556	struct drm_plane *primary;
557	u32 caps = 0;
558	int ret;
559
560	ret = hgsmi_query_conf(vbox->guest_pool,
561			       VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps);
562	if (ret)
563		return ERR_PTR(ret);
564
565	vbox_crtc = kzalloc(sizeof(*vbox_crtc), GFP_KERNEL);
566	if (!vbox_crtc)
567		return ERR_PTR(-ENOMEM);
568
569	primary = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_PRIMARY);
570	if (IS_ERR(primary)) {
571		ret = PTR_ERR(primary);
572		goto free_mem;
573	}
574
575	if ((caps & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)) {
576		cursor = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_CURSOR);
577		if (IS_ERR(cursor)) {
578			ret = PTR_ERR(cursor);
579			goto clean_primary;
580		}
581	} else {
582		DRM_WARN("VirtualBox host is too old, no cursor support\n");
583	}
584
585	vbox_crtc->crtc_id = i;
586
587	ret = drm_crtc_init_with_planes(dev, &vbox_crtc->base, primary, cursor,
588					&vbox_crtc_funcs, NULL);
589	if (ret)
590		goto clean_cursor;
591
592	drm_mode_crtc_set_gamma_size(&vbox_crtc->base, 256);
593	drm_crtc_helper_add(&vbox_crtc->base, &vbox_crtc_helper_funcs);
594
595	return vbox_crtc;
596
597clean_cursor:
598	if (cursor) {
599		drm_plane_cleanup(cursor);
600		kfree(cursor);
601	}
602clean_primary:
603	drm_plane_cleanup(primary);
604	kfree(primary);
605free_mem:
606	kfree(vbox_crtc);
607	return ERR_PTR(ret);
608}
609
610static void vbox_encoder_destroy(struct drm_encoder *encoder)
611{
612	drm_encoder_cleanup(encoder);
613	kfree(encoder);
614}
615
616static const struct drm_encoder_funcs vbox_enc_funcs = {
617	.destroy = vbox_encoder_destroy,
618};
619
620static struct drm_encoder *vbox_encoder_init(struct drm_device *dev,
621					     unsigned int i)
622{
623	struct vbox_encoder *vbox_encoder;
624
625	vbox_encoder = kzalloc(sizeof(*vbox_encoder), GFP_KERNEL);
626	if (!vbox_encoder)
627		return NULL;
628
629	drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs,
630			 DRM_MODE_ENCODER_DAC, NULL);
631
632	vbox_encoder->base.possible_crtcs = 1 << i;
633	return &vbox_encoder->base;
634}
635
636/*
637 * Generate EDID data with a mode-unique serial number for the virtual
638 * monitor to try to persuade Unity that different modes correspond to
639 * different monitors and it should not try to force the same resolution on
640 * them.
641 */
642static void vbox_set_edid(struct drm_connector *connector, int width,
643			  int height)
644{
645	enum { EDID_SIZE = 128 };
646	unsigned char edid[EDID_SIZE] = {
647		0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,	/* header */
648		0x58, 0x58,	/* manufacturer (VBX) */
649		0x00, 0x00,	/* product code */
650		0x00, 0x00, 0x00, 0x00,	/* serial number goes here */
651		0x01,		/* week of manufacture */
652		0x00,		/* year of manufacture */
653		0x01, 0x03,	/* EDID version */
654		0x80,		/* capabilities - digital */
655		0x00,		/* horiz. res in cm, zero for projectors */
656		0x00,		/* vert. res in cm */
657		0x78,		/* display gamma (120 == 2.2). */
658		0xEE,		/* features (standby, suspend, off, RGB, std */
659				/* colour space, preferred timing mode) */
660		0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
661		/* chromaticity for standard colour space. */
662		0x00, 0x00, 0x00,	/* no default timings */
663		0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
664		    0x01, 0x01,
665		0x01, 0x01, 0x01, 0x01,	/* no standard timings */
666		0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
667		    0x02, 0x02,
668		/* descriptor block 1 goes below */
669		0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
670		/* descriptor block 2, monitor ranges */
671		0x00, 0x00, 0x00, 0xFD, 0x00,
672		0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
673		    0x20, 0x20,
674		/* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
675		0x20,
676		/* descriptor block 3, monitor name */
677		0x00, 0x00, 0x00, 0xFC, 0x00,
678		'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r',
679		'\n',
680		/* descriptor block 4: dummy data */
681		0x00, 0x00, 0x00, 0x10, 0x00,
682		0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
683		0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
684		0x20,
685		0x00,		/* number of extensions */
686		0x00		/* checksum goes here */
687	};
688	int clock = (width + 6) * (height + 6) * 60 / 10000;
689	unsigned int i, sum = 0;
690
691	edid[12] = width & 0xff;
692	edid[13] = width >> 8;
693	edid[14] = height & 0xff;
694	edid[15] = height >> 8;
695	edid[54] = clock & 0xff;
696	edid[55] = clock >> 8;
697	edid[56] = width & 0xff;
698	edid[58] = (width >> 4) & 0xf0;
699	edid[59] = height & 0xff;
700	edid[61] = (height >> 4) & 0xf0;
701	for (i = 0; i < EDID_SIZE - 1; ++i)
702		sum += edid[i];
703	edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF;
704	drm_connector_update_edid_property(connector, (struct edid *)edid);
705}
706
707static int vbox_get_modes(struct drm_connector *connector)
708{
709	struct vbox_connector *vbox_connector = NULL;
710	struct drm_display_mode *mode = NULL;
711	struct vbox_private *vbox = NULL;
712	unsigned int num_modes = 0;
713	int preferred_width, preferred_height;
714
715	vbox_connector = to_vbox_connector(connector);
716	vbox = to_vbox_dev(connector->dev);
717
718	hgsmi_report_flags_location(vbox->guest_pool, GUEST_HEAP_OFFSET(vbox) +
719				    HOST_FLAGS_OFFSET);
720	if (vbox_connector->vbox_crtc->crtc_id == 0)
721		vbox_report_caps(vbox);
722
723	num_modes = drm_add_modes_noedid(connector, 2560, 1600);
724	preferred_width = vbox_connector->mode_hint.width ?
725			  vbox_connector->mode_hint.width : 1024;
726	preferred_height = vbox_connector->mode_hint.height ?
727			   vbox_connector->mode_hint.height : 768;
728	mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height,
729			    60, false, false, false);
730	if (mode) {
731		mode->type |= DRM_MODE_TYPE_PREFERRED;
732		drm_mode_probed_add(connector, mode);
733		++num_modes;
734	}
735	vbox_set_edid(connector, preferred_width, preferred_height);
736
737	if (vbox_connector->vbox_crtc->x_hint != -1)
738		drm_object_property_set_value(&connector->base,
739			vbox->ddev.mode_config.suggested_x_property,
740			vbox_connector->vbox_crtc->x_hint);
741	else
742		drm_object_property_set_value(&connector->base,
743			vbox->ddev.mode_config.suggested_x_property, 0);
744
745	if (vbox_connector->vbox_crtc->y_hint != -1)
746		drm_object_property_set_value(&connector->base,
747			vbox->ddev.mode_config.suggested_y_property,
748			vbox_connector->vbox_crtc->y_hint);
749	else
750		drm_object_property_set_value(&connector->base,
751			vbox->ddev.mode_config.suggested_y_property, 0);
752
753	return num_modes;
754}
755
756static void vbox_connector_destroy(struct drm_connector *connector)
757{
758	drm_connector_unregister(connector);
759	drm_connector_cleanup(connector);
760	kfree(connector);
761}
762
763static enum drm_connector_status
764vbox_connector_detect(struct drm_connector *connector, bool force)
765{
766	struct vbox_connector *vbox_connector;
767
768	vbox_connector = to_vbox_connector(connector);
769
770	return vbox_connector->mode_hint.disconnected ?
771	    connector_status_disconnected : connector_status_connected;
772}
773
774static int vbox_fill_modes(struct drm_connector *connector, u32 max_x,
775			   u32 max_y)
776{
777	struct vbox_connector *vbox_connector;
778	struct drm_device *dev;
779	struct drm_display_mode *mode, *iterator;
780
781	vbox_connector = to_vbox_connector(connector);
782	dev = vbox_connector->base.dev;
783	list_for_each_entry_safe(mode, iterator, &connector->modes, head) {
784		list_del(&mode->head);
785		drm_mode_destroy(dev, mode);
786	}
787
788	return drm_helper_probe_single_connector_modes(connector, max_x, max_y);
789}
790
791static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = {
792	.get_modes = vbox_get_modes,
793};
794
795static const struct drm_connector_funcs vbox_connector_funcs = {
796	.detect = vbox_connector_detect,
797	.fill_modes = vbox_fill_modes,
798	.destroy = vbox_connector_destroy,
799	.reset = drm_atomic_helper_connector_reset,
800	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
801	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
802};
803
804static int vbox_connector_init(struct drm_device *dev,
805			       struct vbox_crtc *vbox_crtc,
806			       struct drm_encoder *encoder)
807{
808	struct vbox_connector *vbox_connector;
809	struct drm_connector *connector;
810
811	vbox_connector = kzalloc(sizeof(*vbox_connector), GFP_KERNEL);
812	if (!vbox_connector)
813		return -ENOMEM;
814
815	connector = &vbox_connector->base;
816	vbox_connector->vbox_crtc = vbox_crtc;
817
818	drm_connector_init(dev, connector, &vbox_connector_funcs,
819			   DRM_MODE_CONNECTOR_VGA);
820	drm_connector_helper_add(connector, &vbox_connector_helper_funcs);
821
822	connector->interlace_allowed = 0;
823	connector->doublescan_allowed = 0;
824
825	drm_mode_create_suggested_offset_properties(dev);
826	drm_object_attach_property(&connector->base,
827				   dev->mode_config.suggested_x_property, 0);
828	drm_object_attach_property(&connector->base,
829				   dev->mode_config.suggested_y_property, 0);
830
831	drm_connector_attach_encoder(connector, encoder);
832
833	return 0;
834}
835
836static const struct drm_mode_config_funcs vbox_mode_funcs = {
837	.fb_create = drm_gem_fb_create_with_dirty,
838	.mode_valid = drm_vram_helper_mode_valid,
839	.atomic_check = drm_atomic_helper_check,
840	.atomic_commit = drm_atomic_helper_commit,
841};
842
843int vbox_mode_init(struct vbox_private *vbox)
844{
845	struct drm_device *dev = &vbox->ddev;
846	struct drm_encoder *encoder;
847	struct vbox_crtc *vbox_crtc;
848	unsigned int i;
849	int ret;
850
851	drm_mode_config_init(dev);
852
853	dev->mode_config.funcs = (void *)&vbox_mode_funcs;
854	dev->mode_config.min_width = 0;
855	dev->mode_config.min_height = 0;
856	dev->mode_config.preferred_depth = 24;
857	dev->mode_config.max_width = VBE_DISPI_MAX_XRES;
858	dev->mode_config.max_height = VBE_DISPI_MAX_YRES;
859
860	for (i = 0; i < vbox->num_crtcs; ++i) {
861		vbox_crtc = vbox_crtc_init(dev, i);
862		if (IS_ERR(vbox_crtc)) {
863			ret = PTR_ERR(vbox_crtc);
864			goto err_drm_mode_cleanup;
865		}
866		encoder = vbox_encoder_init(dev, i);
867		if (!encoder) {
868			ret = -ENOMEM;
869			goto err_drm_mode_cleanup;
870		}
871		ret = vbox_connector_init(dev, vbox_crtc, encoder);
872		if (ret)
873			goto err_drm_mode_cleanup;
874	}
875
876	drm_mode_config_reset(dev);
877	return 0;
878
879err_drm_mode_cleanup:
880	drm_mode_config_cleanup(dev);
881	return ret;
882}
883
884void vbox_mode_fini(struct vbox_private *vbox)
885{
886	drm_mode_config_cleanup(&vbox->ddev);
887}
~/ Read-only file preview.