2 * Copyright (C) 2013-2017 Oracle Corporation
3 * This file is based on ast_mode.c
4 * Copyright 2012 Red Hat Inc.
5 * Parts based on xf86-video-ast
6 * Copyright (c) 2005 ASPEED Technology Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * The above copyright notice and this permission notice (including the
25 * next paragraph) shall be included in all copies or substantial portions
30 * Authors: Dave Airlie <airlied@redhat.com>
31 * Michael Thayer <michael.thayer@oracle.com,
32 * Hans de Goede <hdegoede@redhat.com>
34 #include <linux/export.h>
35 #include <drm/drm_atomic.h>
36 #include <drm/drm_crtc_helper.h>
37 #include <drm/drm_plane_helper.h>
38 #include <drm/drm_atomic_helper.h>
41 #include "vboxvideo.h"
42 #include "hgsmi_channels.h"
45 * Set a graphics mode. Poke any required values into registers, do an HGSMI
46 * mode set and tell the host we support advanced graphics functions.
48 static void vbox_do_modeset(struct drm_crtc *crtc)
50 struct drm_framebuffer *fb = crtc->primary->state->fb;
51 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
52 struct vbox_private *vbox;
53 int width, height, bpp, pitch;
55 s32 x_offset, y_offset;
57 vbox = crtc->dev->dev_private;
58 width = vbox_crtc->width ? vbox_crtc->width : 640;
59 height = vbox_crtc->height ? vbox_crtc->height : 480;
60 bpp = fb ? fb->format->cpp[0] * 8 : 32;
61 pitch = fb ? fb->pitches[0] : width * bpp / 8;
62 x_offset = vbox->single_framebuffer ? vbox_crtc->x : vbox_crtc->x_hint;
63 y_offset = vbox->single_framebuffer ? vbox_crtc->y : vbox_crtc->y_hint;
66 * This is the old way of setting graphics modes. It assumed one screen
67 * and a frame-buffer at the start of video RAM. On older versions of
68 * VirtualBox, certain parts of the code still assume that the first
69 * screen is programmed this way, so try to fake it.
71 if (vbox_crtc->crtc_id == 0 && fb &&
72 vbox_crtc->fb_offset / pitch < 0xffff - crtc->y &&
73 vbox_crtc->fb_offset % (bpp / 8) == 0) {
74 vbox_write_ioport(VBE_DISPI_INDEX_XRES, width);
75 vbox_write_ioport(VBE_DISPI_INDEX_YRES, height);
76 vbox_write_ioport(VBE_DISPI_INDEX_VIRT_WIDTH, pitch * 8 / bpp);
77 vbox_write_ioport(VBE_DISPI_INDEX_BPP, bpp);
78 vbox_write_ioport(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED);
80 VBE_DISPI_INDEX_X_OFFSET,
81 vbox_crtc->fb_offset % pitch / bpp * 8 + vbox_crtc->x);
82 vbox_write_ioport(VBE_DISPI_INDEX_Y_OFFSET,
83 vbox_crtc->fb_offset / pitch + vbox_crtc->y);
86 flags = VBVA_SCREEN_F_ACTIVE;
87 flags |= (fb && crtc->state->active) ? 0 : VBVA_SCREEN_F_BLANK;
88 flags |= vbox_crtc->disconnected ? VBVA_SCREEN_F_DISABLED : 0;
89 hgsmi_process_display_info(vbox->guest_pool, vbox_crtc->crtc_id,
91 vbox_crtc->x * bpp / 8 +
93 pitch, width, height, bpp, flags);
96 static int vbox_set_view(struct drm_crtc *crtc)
98 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
99 struct vbox_private *vbox = crtc->dev->dev_private;
100 struct vbva_infoview *p;
103 * Tell the host about the view. This design originally targeted the
104 * Windows XP driver architecture and assumed that each screen would
105 * have a dedicated frame buffer with the command buffer following it,
106 * the whole being a "view". The host works out which screen a command
107 * buffer belongs to by checking whether it is in the first view, then
108 * whether it is in the second and so on. The first match wins. We
109 * cheat around this by making the first view be the managed memory
110 * plus the first command buffer, the second the same plus the second
113 p = hgsmi_buffer_alloc(vbox->guest_pool, sizeof(*p),
114 HGSMI_CH_VBVA, VBVA_INFO_VIEW);
118 p->view_index = vbox_crtc->crtc_id;
119 p->view_offset = vbox_crtc->fb_offset;
120 p->view_size = vbox->available_vram_size - vbox_crtc->fb_offset +
121 vbox_crtc->crtc_id * VBVA_MIN_BUFFER_SIZE;
122 p->max_screen_size = vbox->available_vram_size - vbox_crtc->fb_offset;
124 hgsmi_buffer_submit(vbox->guest_pool, p);
125 hgsmi_buffer_free(vbox->guest_pool, p);
131 * Try to map the layout of virtual screens to the range of the input device.
132 * Return true if we need to re-set the crtc modes due to screen offset
135 static bool vbox_set_up_input_mapping(struct vbox_private *vbox)
137 struct drm_crtc *crtci;
138 struct drm_connector *connectori;
139 struct drm_framebuffer *fb, *fb1 = NULL;
140 bool single_framebuffer = true;
141 bool old_single_framebuffer = vbox->single_framebuffer;
142 u16 width = 0, height = 0;
145 * Are we using an X.Org-style single large frame-buffer for all crtcs?
146 * If so then screen layout can be deduced from the crtc offsets.
147 * Same fall-back if this is the fbdev frame-buffer.
149 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
150 fb = crtci->primary->state->fb;
156 if (to_vbox_framebuffer(fb1) == &vbox->fbdev->afb)
158 } else if (fb != fb1) {
159 single_framebuffer = false;
165 if (single_framebuffer) {
166 vbox->single_framebuffer = true;
167 vbox->input_mapping_width = fb1->width;
168 vbox->input_mapping_height = fb1->height;
169 return old_single_framebuffer != vbox->single_framebuffer;
171 /* Otherwise calculate the total span of all screens. */
172 list_for_each_entry(connectori, &vbox->ddev.mode_config.connector_list,
174 struct vbox_connector *vbox_connector =
175 to_vbox_connector(connectori);
176 struct vbox_crtc *vbox_crtc = vbox_connector->vbox_crtc;
178 width = max_t(u16, width, vbox_crtc->x_hint +
179 vbox_connector->mode_hint.width);
180 height = max_t(u16, height, vbox_crtc->y_hint +
181 vbox_connector->mode_hint.height);
184 vbox->single_framebuffer = false;
185 vbox->input_mapping_width = width;
186 vbox->input_mapping_height = height;
188 return old_single_framebuffer != vbox->single_framebuffer;
191 static void vbox_crtc_set_base_and_mode(struct drm_crtc *crtc,
192 struct drm_framebuffer *fb,
193 struct drm_display_mode *mode,
196 struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(fb)->obj);
197 struct vbox_private *vbox = crtc->dev->dev_private;
198 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
199 bool needs_modeset = drm_atomic_crtc_needs_modeset(crtc->state);
201 mutex_lock(&vbox->hw_mutex);
203 vbox_crtc->width = mode->hdisplay;
204 vbox_crtc->height = mode->vdisplay;
207 vbox_crtc->fb_offset = vbox_bo_gpu_offset(bo);
209 /* vbox_do_modeset() checks vbox->single_framebuffer so update it now */
210 if (needs_modeset && vbox_set_up_input_mapping(vbox)) {
211 struct drm_crtc *crtci;
213 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list,
217 vbox_do_modeset(crtci);
222 vbox_do_modeset(crtc);
225 hgsmi_update_input_mapping(vbox->guest_pool, 0, 0,
226 vbox->input_mapping_width,
227 vbox->input_mapping_height);
229 mutex_unlock(&vbox->hw_mutex);
232 static void vbox_crtc_disable(struct drm_crtc *crtc)
236 static void vbox_crtc_commit(struct drm_crtc *crtc)
240 static void vbox_crtc_atomic_flush(struct drm_crtc *crtc,
241 struct drm_crtc_state *old_crtc_state)
243 struct drm_pending_vblank_event *event;
246 if (crtc->state && crtc->state->event) {
247 event = crtc->state->event;
248 crtc->state->event = NULL;
250 spin_lock_irqsave(&crtc->dev->event_lock, flags);
251 drm_crtc_send_vblank_event(crtc, event);
252 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
256 static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = {
257 .disable = vbox_crtc_disable,
258 .commit = vbox_crtc_commit,
259 .atomic_flush = vbox_crtc_atomic_flush,
262 static void vbox_crtc_destroy(struct drm_crtc *crtc)
264 drm_crtc_cleanup(crtc);
268 static const struct drm_crtc_funcs vbox_crtc_funcs = {
269 .set_config = drm_atomic_helper_set_config,
270 .page_flip = drm_atomic_helper_page_flip,
271 /* .gamma_set = vbox_crtc_gamma_set, */
272 .destroy = vbox_crtc_destroy,
273 .reset = drm_atomic_helper_crtc_reset,
274 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
275 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
278 static int vbox_primary_atomic_check(struct drm_plane *plane,
279 struct drm_plane_state *new_state)
284 static void vbox_primary_atomic_update(struct drm_plane *plane,
285 struct drm_plane_state *old_state)
287 struct drm_crtc *crtc = plane->state->crtc;
288 struct drm_framebuffer *fb = plane->state->fb;
290 vbox_crtc_set_base_and_mode(crtc, fb, &crtc->state->mode,
291 plane->state->src_x >> 16,
292 plane->state->src_y >> 16);
295 void vbox_primary_atomic_disable(struct drm_plane *plane,
296 struct drm_plane_state *old_state)
298 struct drm_crtc *crtc = old_state->crtc;
300 /* vbox_do_modeset checks plane->state->fb and will disable if NULL */
301 vbox_crtc_set_base_and_mode(crtc, old_state->fb, &crtc->state->mode,
302 old_state->src_x >> 16,
303 old_state->src_y >> 16);
306 static int vbox_primary_prepare_fb(struct drm_plane *plane,
307 struct drm_plane_state *new_state)
315 bo = gem_to_vbox_bo(to_vbox_framebuffer(new_state->fb)->obj);
316 ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM);
318 DRM_WARN("Error %d pinning new fb, out of video mem?\n", ret);
323 static void vbox_primary_cleanup_fb(struct drm_plane *plane,
324 struct drm_plane_state *old_state)
331 bo = gem_to_vbox_bo(to_vbox_framebuffer(old_state->fb)->obj);
335 static int vbox_cursor_atomic_check(struct drm_plane *plane,
336 struct drm_plane_state *new_state)
338 u32 width = new_state->crtc_w;
339 u32 height = new_state->crtc_h;
344 if (width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT ||
345 width == 0 || height == 0)
352 * Copy the ARGB image and generate the mask, which is needed in case the host
353 * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set
354 * if the corresponding alpha value in the ARGB image is greater than 0xF0.
356 static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height,
359 size_t line_size = (width + 7) / 8;
362 memcpy(dst + mask_size, src, width * height * 4);
363 for (i = 0; i < height; ++i)
364 for (j = 0; j < width; ++j)
365 if (((u32 *)src)[i * width + j] > 0xf0000000)
366 dst[i * line_size + j / 8] |= (0x80 >> (j % 8));
369 static void vbox_cursor_atomic_update(struct drm_plane *plane,
370 struct drm_plane_state *old_state)
372 struct vbox_private *vbox =
373 container_of(plane->dev, struct vbox_private, ddev);
374 struct vbox_crtc *vbox_crtc = to_vbox_crtc(plane->state->crtc);
375 struct drm_framebuffer *fb = plane->state->fb;
376 struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(fb)->obj);
377 u32 width = plane->state->crtc_w;
378 u32 height = plane->state->crtc_h;
379 size_t data_size, mask_size;
384 * VirtualBox uses the host windowing system to draw the cursor so
385 * moves are a no-op, we only need to upload new cursor sprites.
387 if (fb == old_state->fb)
390 mutex_lock(&vbox->hw_mutex);
392 vbox_crtc->cursor_enabled = true;
394 /* pinning is done in prepare/cleanup framebuffer */
395 src = vbox_bo_kmap(bo);
397 DRM_WARN("Could not kmap cursor bo, skipping update\n");
402 * The mask must be calculated based on the alpha
403 * channel, one bit per ARGB word, and must be 32-bit
406 mask_size = ((width + 7) / 8 * height + 3) & ~3;
407 data_size = width * height * 4 + mask_size;
409 copy_cursor_image(src, vbox->cursor_data, width, height, mask_size);
412 flags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE |
413 VBOX_MOUSE_POINTER_ALPHA;
414 hgsmi_update_pointer_shape(vbox->guest_pool, flags,
415 min_t(u32, max(fb->hot_x, 0), width),
416 min_t(u32, max(fb->hot_y, 0), height),
417 width, height, vbox->cursor_data, data_size);
419 mutex_unlock(&vbox->hw_mutex);
422 void vbox_cursor_atomic_disable(struct drm_plane *plane,
423 struct drm_plane_state *old_state)
425 struct vbox_private *vbox =
426 container_of(plane->dev, struct vbox_private, ddev);
427 struct vbox_crtc *vbox_crtc = to_vbox_crtc(old_state->crtc);
428 bool cursor_enabled = false;
429 struct drm_crtc *crtci;
431 mutex_lock(&vbox->hw_mutex);
433 vbox_crtc->cursor_enabled = false;
435 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
436 if (to_vbox_crtc(crtci)->cursor_enabled)
437 cursor_enabled = true;
441 hgsmi_update_pointer_shape(vbox->guest_pool, 0, 0, 0,
444 mutex_unlock(&vbox->hw_mutex);
447 static int vbox_cursor_prepare_fb(struct drm_plane *plane,
448 struct drm_plane_state *new_state)
455 bo = gem_to_vbox_bo(to_vbox_framebuffer(new_state->fb)->obj);
456 return vbox_bo_pin(bo, TTM_PL_FLAG_SYSTEM);
459 static void vbox_cursor_cleanup_fb(struct drm_plane *plane,
460 struct drm_plane_state *old_state)
464 if (!plane->state->fb)
467 bo = gem_to_vbox_bo(to_vbox_framebuffer(plane->state->fb)->obj);
471 static const uint32_t vbox_cursor_plane_formats[] = {
475 static const struct drm_plane_helper_funcs vbox_cursor_helper_funcs = {
476 .atomic_check = vbox_cursor_atomic_check,
477 .atomic_update = vbox_cursor_atomic_update,
478 .atomic_disable = vbox_cursor_atomic_disable,
479 .prepare_fb = vbox_cursor_prepare_fb,
480 .cleanup_fb = vbox_cursor_cleanup_fb,
483 static const struct drm_plane_funcs vbox_cursor_plane_funcs = {
484 .update_plane = drm_atomic_helper_update_plane,
485 .disable_plane = drm_atomic_helper_disable_plane,
486 .destroy = drm_primary_helper_destroy,
487 .reset = drm_atomic_helper_plane_reset,
488 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
489 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
492 static const uint32_t vbox_primary_plane_formats[] = {
497 static const struct drm_plane_helper_funcs vbox_primary_helper_funcs = {
498 .atomic_check = vbox_primary_atomic_check,
499 .atomic_update = vbox_primary_atomic_update,
500 .atomic_disable = vbox_primary_atomic_disable,
501 .prepare_fb = vbox_primary_prepare_fb,
502 .cleanup_fb = vbox_primary_cleanup_fb,
505 static const struct drm_plane_funcs vbox_primary_plane_funcs = {
506 .update_plane = drm_atomic_helper_update_plane,
507 .disable_plane = drm_atomic_helper_disable_plane,
508 .destroy = drm_primary_helper_destroy,
509 .reset = drm_atomic_helper_plane_reset,
510 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
511 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
514 static struct drm_plane *vbox_create_plane(struct vbox_private *vbox,
515 unsigned int possible_crtcs,
516 enum drm_plane_type type)
518 const struct drm_plane_helper_funcs *helper_funcs = NULL;
519 const struct drm_plane_funcs *funcs;
520 struct drm_plane *plane;
521 const uint32_t *formats;
525 if (type == DRM_PLANE_TYPE_PRIMARY) {
526 funcs = &vbox_primary_plane_funcs;
527 formats = vbox_primary_plane_formats;
528 helper_funcs = &vbox_primary_helper_funcs;
529 num_formats = ARRAY_SIZE(vbox_primary_plane_formats);
530 } else if (type == DRM_PLANE_TYPE_CURSOR) {
531 funcs = &vbox_cursor_plane_funcs;
532 formats = vbox_cursor_plane_formats;
533 helper_funcs = &vbox_cursor_helper_funcs;
534 num_formats = ARRAY_SIZE(vbox_cursor_plane_formats);
536 return ERR_PTR(-EINVAL);
539 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
541 return ERR_PTR(-ENOMEM);
543 err = drm_universal_plane_init(&vbox->ddev, plane, possible_crtcs,
544 funcs, formats, num_formats,
549 drm_plane_helper_add(plane, helper_funcs);
555 return ERR_PTR(-EINVAL);
558 static struct vbox_crtc *vbox_crtc_init(struct drm_device *dev, unsigned int i)
560 struct vbox_private *vbox =
561 container_of(dev, struct vbox_private, ddev);
562 struct drm_plane *cursor = NULL;
563 struct vbox_crtc *vbox_crtc;
564 struct drm_plane *primary;
568 ret = hgsmi_query_conf(vbox->guest_pool,
569 VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps);
573 vbox_crtc = kzalloc(sizeof(*vbox_crtc), GFP_KERNEL);
575 return ERR_PTR(-ENOMEM);
577 primary = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_PRIMARY);
578 if (IS_ERR(primary)) {
579 ret = PTR_ERR(primary);
583 if ((caps & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)) {
584 cursor = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_CURSOR);
585 if (IS_ERR(cursor)) {
586 ret = PTR_ERR(cursor);
590 DRM_WARN("VirtualBox host is too old, no cursor support\n");
593 vbox_crtc->crtc_id = i;
595 ret = drm_crtc_init_with_planes(dev, &vbox_crtc->base, primary, cursor,
596 &vbox_crtc_funcs, NULL);
600 drm_mode_crtc_set_gamma_size(&vbox_crtc->base, 256);
601 drm_crtc_helper_add(&vbox_crtc->base, &vbox_crtc_helper_funcs);
607 drm_plane_cleanup(cursor);
611 drm_plane_cleanup(primary);
618 static void vbox_encoder_destroy(struct drm_encoder *encoder)
620 drm_encoder_cleanup(encoder);
624 static struct drm_encoder *vbox_best_single_encoder(struct drm_connector
627 int enc_id = connector->encoder_ids[0];
629 /* pick the encoder ids */
631 return drm_encoder_find(connector->dev, NULL, enc_id);
636 static const struct drm_encoder_funcs vbox_enc_funcs = {
637 .destroy = vbox_encoder_destroy,
640 static struct drm_encoder *vbox_encoder_init(struct drm_device *dev,
643 struct vbox_encoder *vbox_encoder;
645 vbox_encoder = kzalloc(sizeof(*vbox_encoder), GFP_KERNEL);
649 drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs,
650 DRM_MODE_ENCODER_DAC, NULL);
652 vbox_encoder->base.possible_crtcs = 1 << i;
653 return &vbox_encoder->base;
657 * Generate EDID data with a mode-unique serial number for the virtual
658 * monitor to try to persuade Unity that different modes correspond to
659 * different monitors and it should not try to force the same resolution on
662 static void vbox_set_edid(struct drm_connector *connector, int width,
665 enum { EDID_SIZE = 128 };
666 unsigned char edid[EDID_SIZE] = {
667 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
668 0x58, 0x58, /* manufacturer (VBX) */
669 0x00, 0x00, /* product code */
670 0x00, 0x00, 0x00, 0x00, /* serial number goes here */
671 0x01, /* week of manufacture */
672 0x00, /* year of manufacture */
673 0x01, 0x03, /* EDID version */
674 0x80, /* capabilities - digital */
675 0x00, /* horiz. res in cm, zero for projectors */
676 0x00, /* vert. res in cm */
677 0x78, /* display gamma (120 == 2.2). */
678 0xEE, /* features (standby, suspend, off, RGB, std */
679 /* colour space, preferred timing mode) */
680 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
681 /* chromaticity for standard colour space. */
682 0x00, 0x00, 0x00, /* no default timings */
683 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
685 0x01, 0x01, 0x01, 0x01, /* no standard timings */
686 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
688 /* descriptor block 1 goes below */
689 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
690 /* descriptor block 2, monitor ranges */
691 0x00, 0x00, 0x00, 0xFD, 0x00,
692 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
694 /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
696 /* descriptor block 3, monitor name */
697 0x00, 0x00, 0x00, 0xFC, 0x00,
698 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r',
700 /* descriptor block 4: dummy data */
701 0x00, 0x00, 0x00, 0x10, 0x00,
702 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
703 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
705 0x00, /* number of extensions */
706 0x00 /* checksum goes here */
708 int clock = (width + 6) * (height + 6) * 60 / 10000;
709 unsigned int i, sum = 0;
711 edid[12] = width & 0xff;
712 edid[13] = width >> 8;
713 edid[14] = height & 0xff;
714 edid[15] = height >> 8;
715 edid[54] = clock & 0xff;
716 edid[55] = clock >> 8;
717 edid[56] = width & 0xff;
718 edid[58] = (width >> 4) & 0xf0;
719 edid[59] = height & 0xff;
720 edid[61] = (height >> 4) & 0xf0;
721 for (i = 0; i < EDID_SIZE - 1; ++i)
723 edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF;
724 drm_connector_update_edid_property(connector, (struct edid *)edid);
727 static int vbox_get_modes(struct drm_connector *connector)
729 struct vbox_connector *vbox_connector = NULL;
730 struct drm_display_mode *mode = NULL;
731 struct vbox_private *vbox = NULL;
732 unsigned int num_modes = 0;
733 int preferred_width, preferred_height;
735 vbox_connector = to_vbox_connector(connector);
736 vbox = connector->dev->dev_private;
738 * Heuristic: we do not want to tell the host that we support dynamic
739 * resizing unless we feel confident that the user space client using
740 * the video driver can handle hot-plug events. So the first time modes
741 * are queried after a "master" switch we tell the host that we do not,
742 * and immediately after we send the client a hot-plug notification as
743 * a test to see if they will respond and query again.
744 * That is also the reason why capabilities are reported to the host at
745 * this place in the code rather than elsewhere.
746 * We need to report the flags location before reporting the IRQ
749 hgsmi_report_flags_location(vbox->guest_pool, GUEST_HEAP_OFFSET(vbox) +
751 if (vbox_connector->vbox_crtc->crtc_id == 0)
752 vbox_report_caps(vbox);
753 if (!vbox->initial_mode_queried) {
754 if (vbox_connector->vbox_crtc->crtc_id == 0) {
755 vbox->initial_mode_queried = true;
756 vbox_report_hotplug(vbox);
758 return drm_add_modes_noedid(connector, 800, 600);
760 num_modes = drm_add_modes_noedid(connector, 2560, 1600);
761 preferred_width = vbox_connector->mode_hint.width ?
762 vbox_connector->mode_hint.width : 1024;
763 preferred_height = vbox_connector->mode_hint.height ?
764 vbox_connector->mode_hint.height : 768;
765 mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height,
766 60, false, false, false);
768 mode->type |= DRM_MODE_TYPE_PREFERRED;
769 drm_mode_probed_add(connector, mode);
772 vbox_set_edid(connector, preferred_width, preferred_height);
774 if (vbox_connector->vbox_crtc->x_hint != -1)
775 drm_object_property_set_value(&connector->base,
776 vbox->ddev.mode_config.suggested_x_property,
777 vbox_connector->vbox_crtc->x_hint);
779 drm_object_property_set_value(&connector->base,
780 vbox->ddev.mode_config.suggested_x_property, 0);
782 if (vbox_connector->vbox_crtc->y_hint != -1)
783 drm_object_property_set_value(&connector->base,
784 vbox->ddev.mode_config.suggested_y_property,
785 vbox_connector->vbox_crtc->y_hint);
787 drm_object_property_set_value(&connector->base,
788 vbox->ddev.mode_config.suggested_y_property, 0);
793 static enum drm_mode_status vbox_mode_valid(struct drm_connector *connector,
794 struct drm_display_mode *mode)
799 static void vbox_connector_destroy(struct drm_connector *connector)
801 drm_connector_unregister(connector);
802 drm_connector_cleanup(connector);
806 static enum drm_connector_status
807 vbox_connector_detect(struct drm_connector *connector, bool force)
809 struct vbox_connector *vbox_connector;
811 vbox_connector = to_vbox_connector(connector);
813 return vbox_connector->mode_hint.disconnected ?
814 connector_status_disconnected : connector_status_connected;
817 static int vbox_fill_modes(struct drm_connector *connector, u32 max_x,
820 struct vbox_connector *vbox_connector;
821 struct drm_device *dev;
822 struct drm_display_mode *mode, *iterator;
824 vbox_connector = to_vbox_connector(connector);
825 dev = vbox_connector->base.dev;
826 list_for_each_entry_safe(mode, iterator, &connector->modes, head) {
827 list_del(&mode->head);
828 drm_mode_destroy(dev, mode);
831 return drm_helper_probe_single_connector_modes(connector, max_x, max_y);
834 static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = {
835 .mode_valid = vbox_mode_valid,
836 .get_modes = vbox_get_modes,
837 .best_encoder = vbox_best_single_encoder,
840 static const struct drm_connector_funcs vbox_connector_funcs = {
841 .detect = vbox_connector_detect,
842 .fill_modes = vbox_fill_modes,
843 .destroy = vbox_connector_destroy,
844 .reset = drm_atomic_helper_connector_reset,
845 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
846 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
849 static int vbox_connector_init(struct drm_device *dev,
850 struct vbox_crtc *vbox_crtc,
851 struct drm_encoder *encoder)
853 struct vbox_connector *vbox_connector;
854 struct drm_connector *connector;
856 vbox_connector = kzalloc(sizeof(*vbox_connector), GFP_KERNEL);
860 connector = &vbox_connector->base;
861 vbox_connector->vbox_crtc = vbox_crtc;
863 drm_connector_init(dev, connector, &vbox_connector_funcs,
864 DRM_MODE_CONNECTOR_VGA);
865 drm_connector_helper_add(connector, &vbox_connector_helper_funcs);
867 connector->interlace_allowed = 0;
868 connector->doublescan_allowed = 0;
870 drm_mode_create_suggested_offset_properties(dev);
871 drm_object_attach_property(&connector->base,
872 dev->mode_config.suggested_x_property, 0);
873 drm_object_attach_property(&connector->base,
874 dev->mode_config.suggested_y_property, 0);
876 drm_connector_attach_encoder(connector, encoder);
881 static struct drm_framebuffer *vbox_user_framebuffer_create(
882 struct drm_device *dev,
883 struct drm_file *filp,
884 const struct drm_mode_fb_cmd2 *mode_cmd)
886 struct vbox_private *vbox =
887 container_of(dev, struct vbox_private, ddev);
888 struct drm_gem_object *obj;
889 struct vbox_framebuffer *vbox_fb;
892 obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
894 return ERR_PTR(-ENOENT);
896 vbox_fb = kzalloc(sizeof(*vbox_fb), GFP_KERNEL);
900 ret = vbox_framebuffer_init(vbox, vbox_fb, mode_cmd, obj);
902 goto err_free_vbox_fb;
904 return &vbox_fb->base;
909 drm_gem_object_put_unlocked(obj);
913 static const struct drm_mode_config_funcs vbox_mode_funcs = {
914 .fb_create = vbox_user_framebuffer_create,
915 .atomic_check = drm_atomic_helper_check,
916 .atomic_commit = drm_atomic_helper_commit,
919 int vbox_mode_init(struct vbox_private *vbox)
921 struct drm_device *dev = &vbox->ddev;
922 struct drm_encoder *encoder;
923 struct vbox_crtc *vbox_crtc;
927 drm_mode_config_init(dev);
929 dev->mode_config.funcs = (void *)&vbox_mode_funcs;
930 dev->mode_config.min_width = 0;
931 dev->mode_config.min_height = 0;
932 dev->mode_config.preferred_depth = 24;
933 dev->mode_config.max_width = VBE_DISPI_MAX_XRES;
934 dev->mode_config.max_height = VBE_DISPI_MAX_YRES;
936 for (i = 0; i < vbox->num_crtcs; ++i) {
937 vbox_crtc = vbox_crtc_init(dev, i);
938 if (IS_ERR(vbox_crtc)) {
939 ret = PTR_ERR(vbox_crtc);
940 goto err_drm_mode_cleanup;
942 encoder = vbox_encoder_init(dev, i);
945 goto err_drm_mode_cleanup;
947 ret = vbox_connector_init(dev, vbox_crtc, encoder);
949 goto err_drm_mode_cleanup;
952 drm_mode_config_reset(dev);
955 err_drm_mode_cleanup:
956 drm_mode_config_cleanup(dev);
960 void vbox_mode_fini(struct vbox_private *vbox)
962 drm_mode_config_cleanup(&vbox->ddev);