treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / vboxvideo / vbox_mode.c
blob19612132c8a39deb2b718c4c9b1a102c75d65343
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>
12 #include <linux/export.h>
14 #include <drm/drm_atomic.h>
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_fourcc.h>
18 #include <drm/drm_gem_framebuffer_helper.h>
19 #include <drm/drm_plane_helper.h>
20 #include <drm/drm_probe_helper.h>
21 #include <drm/drm_vblank.h>
23 #include "hgsmi_channels.h"
24 #include "vbox_drv.h"
25 #include "vboxvideo.h"
28 * Set a graphics mode. Poke any required values into registers, do an HGSMI
29 * mode set and tell the host we support advanced graphics functions.
31 static void vbox_do_modeset(struct drm_crtc *crtc)
33 struct drm_framebuffer *fb = crtc->primary->state->fb;
34 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
35 struct vbox_private *vbox;
36 int width, height, bpp, pitch;
37 u16 flags;
38 s32 x_offset, y_offset;
40 vbox = crtc->dev->dev_private;
41 width = vbox_crtc->width ? vbox_crtc->width : 640;
42 height = vbox_crtc->height ? vbox_crtc->height : 480;
43 bpp = fb ? fb->format->cpp[0] * 8 : 32;
44 pitch = fb ? fb->pitches[0] : width * bpp / 8;
45 x_offset = vbox->single_framebuffer ? vbox_crtc->x : vbox_crtc->x_hint;
46 y_offset = vbox->single_framebuffer ? vbox_crtc->y : vbox_crtc->y_hint;
49 * This is the old way of setting graphics modes. It assumed one screen
50 * and a frame-buffer at the start of video RAM. On older versions of
51 * VirtualBox, certain parts of the code still assume that the first
52 * screen is programmed this way, so try to fake it.
54 if (vbox_crtc->crtc_id == 0 && fb &&
55 vbox_crtc->fb_offset / pitch < 0xffff - crtc->y &&
56 vbox_crtc->fb_offset % (bpp / 8) == 0) {
57 vbox_write_ioport(VBE_DISPI_INDEX_XRES, width);
58 vbox_write_ioport(VBE_DISPI_INDEX_YRES, height);
59 vbox_write_ioport(VBE_DISPI_INDEX_VIRT_WIDTH, pitch * 8 / bpp);
60 vbox_write_ioport(VBE_DISPI_INDEX_BPP, bpp);
61 vbox_write_ioport(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED);
62 vbox_write_ioport(VBE_DISPI_INDEX_X_OFFSET,
63 vbox_crtc->fb_offset % pitch / bpp * 8 + vbox_crtc->x);
64 vbox_write_ioport(VBE_DISPI_INDEX_Y_OFFSET,
65 vbox_crtc->fb_offset / pitch + vbox_crtc->y);
68 flags = VBVA_SCREEN_F_ACTIVE;
69 flags |= (fb && crtc->state->enable) ? 0 : VBVA_SCREEN_F_BLANK;
70 flags |= vbox_crtc->disconnected ? VBVA_SCREEN_F_DISABLED : 0;
71 hgsmi_process_display_info(vbox->guest_pool, vbox_crtc->crtc_id,
72 x_offset, y_offset,
73 vbox_crtc->x * bpp / 8 +
74 vbox_crtc->y * pitch,
75 pitch, width, height, bpp, flags);
78 static int vbox_set_view(struct drm_crtc *crtc)
80 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
81 struct vbox_private *vbox = crtc->dev->dev_private;
82 struct vbva_infoview *p;
85 * Tell the host about the view. This design originally targeted the
86 * Windows XP driver architecture and assumed that each screen would
87 * have a dedicated frame buffer with the command buffer following it,
88 * the whole being a "view". The host works out which screen a command
89 * buffer belongs to by checking whether it is in the first view, then
90 * whether it is in the second and so on. The first match wins. We
91 * cheat around this by making the first view be the managed memory
92 * plus the first command buffer, the second the same plus the second
93 * buffer and so on.
95 p = hgsmi_buffer_alloc(vbox->guest_pool, sizeof(*p),
96 HGSMI_CH_VBVA, VBVA_INFO_VIEW);
97 if (!p)
98 return -ENOMEM;
100 p->view_index = vbox_crtc->crtc_id;
101 p->view_offset = vbox_crtc->fb_offset;
102 p->view_size = vbox->available_vram_size - vbox_crtc->fb_offset +
103 vbox_crtc->crtc_id * VBVA_MIN_BUFFER_SIZE;
104 p->max_screen_size = vbox->available_vram_size - vbox_crtc->fb_offset;
106 hgsmi_buffer_submit(vbox->guest_pool, p);
107 hgsmi_buffer_free(vbox->guest_pool, p);
109 return 0;
113 * Try to map the layout of virtual screens to the range of the input device.
114 * Return true if we need to re-set the crtc modes due to screen offset
115 * changes.
117 static bool vbox_set_up_input_mapping(struct vbox_private *vbox)
119 struct drm_crtc *crtci;
120 struct drm_connector *connectori;
121 struct drm_framebuffer *fb, *fb1 = NULL;
122 bool single_framebuffer = true;
123 bool old_single_framebuffer = vbox->single_framebuffer;
124 u16 width = 0, height = 0;
127 * Are we using an X.Org-style single large frame-buffer for all crtcs?
128 * If so then screen layout can be deduced from the crtc offsets.
129 * Same fall-back if this is the fbdev frame-buffer.
131 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
132 fb = crtci->primary->state->fb;
133 if (!fb)
134 continue;
136 if (!fb1) {
137 fb1 = fb;
138 if (fb1 == vbox->ddev.fb_helper->fb)
139 break;
140 } else if (fb != fb1) {
141 single_framebuffer = false;
144 if (!fb1)
145 return false;
147 if (single_framebuffer) {
148 vbox->single_framebuffer = true;
149 vbox->input_mapping_width = fb1->width;
150 vbox->input_mapping_height = fb1->height;
151 return old_single_framebuffer != vbox->single_framebuffer;
153 /* Otherwise calculate the total span of all screens. */
154 list_for_each_entry(connectori, &vbox->ddev.mode_config.connector_list,
155 head) {
156 struct vbox_connector *vbox_connector =
157 to_vbox_connector(connectori);
158 struct vbox_crtc *vbox_crtc = vbox_connector->vbox_crtc;
160 width = max_t(u16, width, vbox_crtc->x_hint +
161 vbox_connector->mode_hint.width);
162 height = max_t(u16, height, vbox_crtc->y_hint +
163 vbox_connector->mode_hint.height);
166 vbox->single_framebuffer = false;
167 vbox->input_mapping_width = width;
168 vbox->input_mapping_height = height;
170 return old_single_framebuffer != vbox->single_framebuffer;
173 static void vbox_crtc_set_base_and_mode(struct drm_crtc *crtc,
174 struct drm_framebuffer *fb,
175 int x, int y)
177 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(fb->obj[0]);
178 struct vbox_private *vbox = crtc->dev->dev_private;
179 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
180 bool needs_modeset = drm_atomic_crtc_needs_modeset(crtc->state);
182 mutex_lock(&vbox->hw_mutex);
184 if (crtc->state->enable) {
185 vbox_crtc->width = crtc->state->mode.hdisplay;
186 vbox_crtc->height = crtc->state->mode.vdisplay;
189 vbox_crtc->x = x;
190 vbox_crtc->y = y;
191 vbox_crtc->fb_offset = drm_gem_vram_offset(gbo);
193 /* vbox_do_modeset() checks vbox->single_framebuffer so update it now */
194 if (needs_modeset && vbox_set_up_input_mapping(vbox)) {
195 struct drm_crtc *crtci;
197 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list,
198 head) {
199 if (crtci == crtc)
200 continue;
201 vbox_do_modeset(crtci);
205 vbox_set_view(crtc);
206 vbox_do_modeset(crtc);
208 if (needs_modeset)
209 hgsmi_update_input_mapping(vbox->guest_pool, 0, 0,
210 vbox->input_mapping_width,
211 vbox->input_mapping_height);
213 mutex_unlock(&vbox->hw_mutex);
216 static void vbox_crtc_atomic_enable(struct drm_crtc *crtc,
217 struct drm_crtc_state *old_crtc_state)
221 static void vbox_crtc_atomic_disable(struct drm_crtc *crtc,
222 struct drm_crtc_state *old_crtc_state)
226 static void vbox_crtc_atomic_flush(struct drm_crtc *crtc,
227 struct drm_crtc_state *old_crtc_state)
229 struct drm_pending_vblank_event *event;
230 unsigned long flags;
232 if (crtc->state && crtc->state->event) {
233 event = crtc->state->event;
234 crtc->state->event = NULL;
236 spin_lock_irqsave(&crtc->dev->event_lock, flags);
237 drm_crtc_send_vblank_event(crtc, event);
238 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
242 static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = {
243 .atomic_enable = vbox_crtc_atomic_enable,
244 .atomic_disable = vbox_crtc_atomic_disable,
245 .atomic_flush = vbox_crtc_atomic_flush,
248 static void vbox_crtc_destroy(struct drm_crtc *crtc)
250 drm_crtc_cleanup(crtc);
251 kfree(crtc);
254 static const struct drm_crtc_funcs vbox_crtc_funcs = {
255 .set_config = drm_atomic_helper_set_config,
256 .page_flip = drm_atomic_helper_page_flip,
257 /* .gamma_set = vbox_crtc_gamma_set, */
258 .destroy = vbox_crtc_destroy,
259 .reset = drm_atomic_helper_crtc_reset,
260 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
261 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
264 static int vbox_primary_atomic_check(struct drm_plane *plane,
265 struct drm_plane_state *new_state)
267 struct drm_crtc_state *crtc_state = NULL;
269 if (new_state->crtc) {
270 crtc_state = drm_atomic_get_existing_crtc_state(
271 new_state->state, new_state->crtc);
272 if (WARN_ON(!crtc_state))
273 return -EINVAL;
276 return drm_atomic_helper_check_plane_state(new_state, crtc_state,
277 DRM_PLANE_HELPER_NO_SCALING,
278 DRM_PLANE_HELPER_NO_SCALING,
279 false, true);
282 static void vbox_primary_atomic_update(struct drm_plane *plane,
283 struct drm_plane_state *old_state)
285 struct drm_crtc *crtc = plane->state->crtc;
286 struct drm_framebuffer *fb = plane->state->fb;
287 struct vbox_private *vbox = fb->dev->dev_private;
288 struct drm_mode_rect *clips;
289 uint32_t num_clips, i;
291 vbox_crtc_set_base_and_mode(crtc, fb,
292 plane->state->src_x >> 16,
293 plane->state->src_y >> 16);
295 /* Send information about dirty rectangles to VBVA. */
297 clips = drm_plane_get_damage_clips(plane->state);
298 num_clips = drm_plane_get_damage_clips_count(plane->state);
300 if (!num_clips)
301 return;
303 mutex_lock(&vbox->hw_mutex);
305 for (i = 0; i < num_clips; ++i, ++clips) {
306 struct vbva_cmd_hdr cmd_hdr;
307 unsigned int crtc_id = to_vbox_crtc(crtc)->crtc_id;
309 cmd_hdr.x = (s16)clips->x1;
310 cmd_hdr.y = (s16)clips->y1;
311 cmd_hdr.w = (u16)clips->x2 - clips->x1;
312 cmd_hdr.h = (u16)clips->y2 - clips->y1;
314 if (!vbva_buffer_begin_update(&vbox->vbva_info[crtc_id],
315 vbox->guest_pool))
316 continue;
318 vbva_write(&vbox->vbva_info[crtc_id], vbox->guest_pool,
319 &cmd_hdr, sizeof(cmd_hdr));
320 vbva_buffer_end_update(&vbox->vbva_info[crtc_id]);
323 mutex_unlock(&vbox->hw_mutex);
326 static void vbox_primary_atomic_disable(struct drm_plane *plane,
327 struct drm_plane_state *old_state)
329 struct drm_crtc *crtc = old_state->crtc;
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);
337 static int vbox_cursor_atomic_check(struct drm_plane *plane,
338 struct drm_plane_state *new_state)
340 struct drm_crtc_state *crtc_state = NULL;
341 u32 width = new_state->crtc_w;
342 u32 height = new_state->crtc_h;
343 int ret;
345 if (new_state->crtc) {
346 crtc_state = drm_atomic_get_existing_crtc_state(
347 new_state->state, new_state->crtc);
348 if (WARN_ON(!crtc_state))
349 return -EINVAL;
352 ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
353 DRM_PLANE_HELPER_NO_SCALING,
354 DRM_PLANE_HELPER_NO_SCALING,
355 true, true);
356 if (ret)
357 return ret;
359 if (!new_state->fb)
360 return 0;
362 if (width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT ||
363 width == 0 || height == 0)
364 return -EINVAL;
366 return 0;
370 * Copy the ARGB image and generate the mask, which is needed in case the host
371 * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set
372 * if the corresponding alpha value in the ARGB image is greater than 0xF0.
374 static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height,
375 size_t mask_size)
377 size_t line_size = (width + 7) / 8;
378 u32 i, j;
380 memcpy(dst + mask_size, src, width * height * 4);
381 for (i = 0; i < height; ++i)
382 for (j = 0; j < width; ++j)
383 if (((u32 *)src)[i * width + j] > 0xf0000000)
384 dst[i * line_size + j / 8] |= (0x80 >> (j % 8));
387 static void vbox_cursor_atomic_update(struct drm_plane *plane,
388 struct drm_plane_state *old_state)
390 struct vbox_private *vbox =
391 container_of(plane->dev, struct vbox_private, ddev);
392 struct vbox_crtc *vbox_crtc = to_vbox_crtc(plane->state->crtc);
393 struct drm_framebuffer *fb = plane->state->fb;
394 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(fb->obj[0]);
395 u32 width = plane->state->crtc_w;
396 u32 height = plane->state->crtc_h;
397 size_t data_size, mask_size;
398 u32 flags;
399 u8 *src;
402 * VirtualBox uses the host windowing system to draw the cursor so
403 * moves are a no-op, we only need to upload new cursor sprites.
405 if (fb == old_state->fb)
406 return;
408 mutex_lock(&vbox->hw_mutex);
410 vbox_crtc->cursor_enabled = true;
412 /* pinning is done in prepare/cleanup framebuffer */
413 src = drm_gem_vram_kmap(gbo, true, NULL);
414 if (IS_ERR(src)) {
415 mutex_unlock(&vbox->hw_mutex);
416 DRM_WARN("Could not kmap cursor bo, skipping update\n");
417 return;
421 * The mask must be calculated based on the alpha
422 * channel, one bit per ARGB word, and must be 32-bit
423 * padded.
425 mask_size = ((width + 7) / 8 * height + 3) & ~3;
426 data_size = width * height * 4 + mask_size;
428 copy_cursor_image(src, vbox->cursor_data, width, height, mask_size);
429 drm_gem_vram_kunmap(gbo);
431 flags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE |
432 VBOX_MOUSE_POINTER_ALPHA;
433 hgsmi_update_pointer_shape(vbox->guest_pool, flags,
434 min_t(u32, max(fb->hot_x, 0), width),
435 min_t(u32, max(fb->hot_y, 0), height),
436 width, height, vbox->cursor_data, data_size);
438 mutex_unlock(&vbox->hw_mutex);
441 static void vbox_cursor_atomic_disable(struct drm_plane *plane,
442 struct drm_plane_state *old_state)
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;
450 mutex_lock(&vbox->hw_mutex);
452 vbox_crtc->cursor_enabled = false;
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;
459 if (!cursor_enabled)
460 hgsmi_update_pointer_shape(vbox->guest_pool, 0, 0, 0,
461 0, 0, NULL, 0);
463 mutex_unlock(&vbox->hw_mutex);
466 static const u32 vbox_cursor_plane_formats[] = {
467 DRM_FORMAT_ARGB8888,
470 static 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 .prepare_fb = drm_gem_vram_plane_helper_prepare_fb,
475 .cleanup_fb = drm_gem_vram_plane_helper_cleanup_fb,
478 static 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_primary_helper_destroy,
482 .reset = drm_atomic_helper_plane_reset,
483 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
484 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
487 static const u32 vbox_primary_plane_formats[] = {
488 DRM_FORMAT_XRGB8888,
489 DRM_FORMAT_ARGB8888,
492 static const struct drm_plane_helper_funcs vbox_primary_helper_funcs = {
493 .atomic_check = vbox_primary_atomic_check,
494 .atomic_update = vbox_primary_atomic_update,
495 .atomic_disable = vbox_primary_atomic_disable,
496 .prepare_fb = drm_gem_vram_plane_helper_prepare_fb,
497 .cleanup_fb = drm_gem_vram_plane_helper_cleanup_fb,
500 static const struct drm_plane_funcs vbox_primary_plane_funcs = {
501 .update_plane = drm_atomic_helper_update_plane,
502 .disable_plane = drm_atomic_helper_disable_plane,
503 .destroy = drm_primary_helper_destroy,
504 .reset = drm_atomic_helper_plane_reset,
505 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
506 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
509 static struct drm_plane *vbox_create_plane(struct vbox_private *vbox,
510 unsigned int possible_crtcs,
511 enum drm_plane_type type)
513 const struct drm_plane_helper_funcs *helper_funcs = NULL;
514 const struct drm_plane_funcs *funcs;
515 struct drm_plane *plane;
516 const u32 *formats;
517 int num_formats;
518 int err;
520 if (type == DRM_PLANE_TYPE_PRIMARY) {
521 funcs = &vbox_primary_plane_funcs;
522 formats = vbox_primary_plane_formats;
523 helper_funcs = &vbox_primary_helper_funcs;
524 num_formats = ARRAY_SIZE(vbox_primary_plane_formats);
525 } else if (type == DRM_PLANE_TYPE_CURSOR) {
526 funcs = &vbox_cursor_plane_funcs;
527 formats = vbox_cursor_plane_formats;
528 helper_funcs = &vbox_cursor_helper_funcs;
529 num_formats = ARRAY_SIZE(vbox_cursor_plane_formats);
530 } else {
531 return ERR_PTR(-EINVAL);
534 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
535 if (!plane)
536 return ERR_PTR(-ENOMEM);
538 err = drm_universal_plane_init(&vbox->ddev, plane, possible_crtcs,
539 funcs, formats, num_formats,
540 NULL, type, NULL);
541 if (err)
542 goto free_plane;
544 drm_plane_helper_add(plane, helper_funcs);
546 return plane;
548 free_plane:
549 kfree(plane);
550 return ERR_PTR(-EINVAL);
553 static struct vbox_crtc *vbox_crtc_init(struct drm_device *dev, unsigned int i)
555 struct vbox_private *vbox =
556 container_of(dev, struct vbox_private, ddev);
557 struct drm_plane *cursor = NULL;
558 struct vbox_crtc *vbox_crtc;
559 struct drm_plane *primary;
560 u32 caps = 0;
561 int ret;
563 ret = hgsmi_query_conf(vbox->guest_pool,
564 VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps);
565 if (ret)
566 return ERR_PTR(ret);
568 vbox_crtc = kzalloc(sizeof(*vbox_crtc), GFP_KERNEL);
569 if (!vbox_crtc)
570 return ERR_PTR(-ENOMEM);
572 primary = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_PRIMARY);
573 if (IS_ERR(primary)) {
574 ret = PTR_ERR(primary);
575 goto free_mem;
578 if ((caps & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)) {
579 cursor = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_CURSOR);
580 if (IS_ERR(cursor)) {
581 ret = PTR_ERR(cursor);
582 goto clean_primary;
584 } else {
585 DRM_WARN("VirtualBox host is too old, no cursor support\n");
588 vbox_crtc->crtc_id = i;
590 ret = drm_crtc_init_with_planes(dev, &vbox_crtc->base, primary, cursor,
591 &vbox_crtc_funcs, NULL);
592 if (ret)
593 goto clean_cursor;
595 drm_mode_crtc_set_gamma_size(&vbox_crtc->base, 256);
596 drm_crtc_helper_add(&vbox_crtc->base, &vbox_crtc_helper_funcs);
598 return vbox_crtc;
600 clean_cursor:
601 if (cursor) {
602 drm_plane_cleanup(cursor);
603 kfree(cursor);
605 clean_primary:
606 drm_plane_cleanup(primary);
607 kfree(primary);
608 free_mem:
609 kfree(vbox_crtc);
610 return ERR_PTR(ret);
613 static void vbox_encoder_destroy(struct drm_encoder *encoder)
615 drm_encoder_cleanup(encoder);
616 kfree(encoder);
619 static const struct drm_encoder_funcs vbox_enc_funcs = {
620 .destroy = vbox_encoder_destroy,
623 static struct drm_encoder *vbox_encoder_init(struct drm_device *dev,
624 unsigned int i)
626 struct vbox_encoder *vbox_encoder;
628 vbox_encoder = kzalloc(sizeof(*vbox_encoder), GFP_KERNEL);
629 if (!vbox_encoder)
630 return NULL;
632 drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs,
633 DRM_MODE_ENCODER_DAC, NULL);
635 vbox_encoder->base.possible_crtcs = 1 << i;
636 return &vbox_encoder->base;
640 * Generate EDID data with a mode-unique serial number for the virtual
641 * monitor to try to persuade Unity that different modes correspond to
642 * different monitors and it should not try to force the same resolution on
643 * them.
645 static void vbox_set_edid(struct drm_connector *connector, int width,
646 int height)
648 enum { EDID_SIZE = 128 };
649 unsigned char edid[EDID_SIZE] = {
650 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
651 0x58, 0x58, /* manufacturer (VBX) */
652 0x00, 0x00, /* product code */
653 0x00, 0x00, 0x00, 0x00, /* serial number goes here */
654 0x01, /* week of manufacture */
655 0x00, /* year of manufacture */
656 0x01, 0x03, /* EDID version */
657 0x80, /* capabilities - digital */
658 0x00, /* horiz. res in cm, zero for projectors */
659 0x00, /* vert. res in cm */
660 0x78, /* display gamma (120 == 2.2). */
661 0xEE, /* features (standby, suspend, off, RGB, std */
662 /* colour space, preferred timing mode) */
663 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
664 /* chromaticity for standard colour space. */
665 0x00, 0x00, 0x00, /* no default timings */
666 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
667 0x01, 0x01,
668 0x01, 0x01, 0x01, 0x01, /* no standard timings */
669 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
670 0x02, 0x02,
671 /* descriptor block 1 goes below */
672 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
673 /* descriptor block 2, monitor ranges */
674 0x00, 0x00, 0x00, 0xFD, 0x00,
675 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
676 0x20, 0x20,
677 /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
678 0x20,
679 /* descriptor block 3, monitor name */
680 0x00, 0x00, 0x00, 0xFC, 0x00,
681 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r',
682 '\n',
683 /* descriptor block 4: dummy data */
684 0x00, 0x00, 0x00, 0x10, 0x00,
685 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
686 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
687 0x20,
688 0x00, /* number of extensions */
689 0x00 /* checksum goes here */
691 int clock = (width + 6) * (height + 6) * 60 / 10000;
692 unsigned int i, sum = 0;
694 edid[12] = width & 0xff;
695 edid[13] = width >> 8;
696 edid[14] = height & 0xff;
697 edid[15] = height >> 8;
698 edid[54] = clock & 0xff;
699 edid[55] = clock >> 8;
700 edid[56] = width & 0xff;
701 edid[58] = (width >> 4) & 0xf0;
702 edid[59] = height & 0xff;
703 edid[61] = (height >> 4) & 0xf0;
704 for (i = 0; i < EDID_SIZE - 1; ++i)
705 sum += edid[i];
706 edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF;
707 drm_connector_update_edid_property(connector, (struct edid *)edid);
710 static int vbox_get_modes(struct drm_connector *connector)
712 struct vbox_connector *vbox_connector = NULL;
713 struct drm_display_mode *mode = NULL;
714 struct vbox_private *vbox = NULL;
715 unsigned int num_modes = 0;
716 int preferred_width, preferred_height;
718 vbox_connector = to_vbox_connector(connector);
719 vbox = connector->dev->dev_private;
721 hgsmi_report_flags_location(vbox->guest_pool, GUEST_HEAP_OFFSET(vbox) +
722 HOST_FLAGS_OFFSET);
723 if (vbox_connector->vbox_crtc->crtc_id == 0)
724 vbox_report_caps(vbox);
726 num_modes = drm_add_modes_noedid(connector, 2560, 1600);
727 preferred_width = vbox_connector->mode_hint.width ?
728 vbox_connector->mode_hint.width : 1024;
729 preferred_height = vbox_connector->mode_hint.height ?
730 vbox_connector->mode_hint.height : 768;
731 mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height,
732 60, false, false, false);
733 if (mode) {
734 mode->type |= DRM_MODE_TYPE_PREFERRED;
735 drm_mode_probed_add(connector, mode);
736 ++num_modes;
738 vbox_set_edid(connector, preferred_width, preferred_height);
740 if (vbox_connector->vbox_crtc->x_hint != -1)
741 drm_object_property_set_value(&connector->base,
742 vbox->ddev.mode_config.suggested_x_property,
743 vbox_connector->vbox_crtc->x_hint);
744 else
745 drm_object_property_set_value(&connector->base,
746 vbox->ddev.mode_config.suggested_x_property, 0);
748 if (vbox_connector->vbox_crtc->y_hint != -1)
749 drm_object_property_set_value(&connector->base,
750 vbox->ddev.mode_config.suggested_y_property,
751 vbox_connector->vbox_crtc->y_hint);
752 else
753 drm_object_property_set_value(&connector->base,
754 vbox->ddev.mode_config.suggested_y_property, 0);
756 return num_modes;
759 static void vbox_connector_destroy(struct drm_connector *connector)
761 drm_connector_unregister(connector);
762 drm_connector_cleanup(connector);
763 kfree(connector);
766 static enum drm_connector_status
767 vbox_connector_detect(struct drm_connector *connector, bool force)
769 struct vbox_connector *vbox_connector;
771 vbox_connector = to_vbox_connector(connector);
773 return vbox_connector->mode_hint.disconnected ?
774 connector_status_disconnected : connector_status_connected;
777 static int vbox_fill_modes(struct drm_connector *connector, u32 max_x,
778 u32 max_y)
780 struct vbox_connector *vbox_connector;
781 struct drm_device *dev;
782 struct drm_display_mode *mode, *iterator;
784 vbox_connector = to_vbox_connector(connector);
785 dev = vbox_connector->base.dev;
786 list_for_each_entry_safe(mode, iterator, &connector->modes, head) {
787 list_del(&mode->head);
788 drm_mode_destroy(dev, mode);
791 return drm_helper_probe_single_connector_modes(connector, max_x, max_y);
794 static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = {
795 .get_modes = vbox_get_modes,
798 static const struct drm_connector_funcs vbox_connector_funcs = {
799 .detect = vbox_connector_detect,
800 .fill_modes = vbox_fill_modes,
801 .destroy = vbox_connector_destroy,
802 .reset = drm_atomic_helper_connector_reset,
803 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
804 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
807 static int vbox_connector_init(struct drm_device *dev,
808 struct vbox_crtc *vbox_crtc,
809 struct drm_encoder *encoder)
811 struct vbox_connector *vbox_connector;
812 struct drm_connector *connector;
814 vbox_connector = kzalloc(sizeof(*vbox_connector), GFP_KERNEL);
815 if (!vbox_connector)
816 return -ENOMEM;
818 connector = &vbox_connector->base;
819 vbox_connector->vbox_crtc = vbox_crtc;
821 drm_connector_init(dev, connector, &vbox_connector_funcs,
822 DRM_MODE_CONNECTOR_VGA);
823 drm_connector_helper_add(connector, &vbox_connector_helper_funcs);
825 connector->interlace_allowed = 0;
826 connector->doublescan_allowed = 0;
828 drm_mode_create_suggested_offset_properties(dev);
829 drm_object_attach_property(&connector->base,
830 dev->mode_config.suggested_x_property, 0);
831 drm_object_attach_property(&connector->base,
832 dev->mode_config.suggested_y_property, 0);
834 drm_connector_attach_encoder(connector, encoder);
836 return 0;
839 static const struct drm_mode_config_funcs vbox_mode_funcs = {
840 .fb_create = drm_gem_fb_create_with_dirty,
841 .atomic_check = drm_atomic_helper_check,
842 .atomic_commit = drm_atomic_helper_commit,
845 int vbox_mode_init(struct vbox_private *vbox)
847 struct drm_device *dev = &vbox->ddev;
848 struct drm_encoder *encoder;
849 struct vbox_crtc *vbox_crtc;
850 unsigned int i;
851 int ret;
853 drm_mode_config_init(dev);
855 dev->mode_config.funcs = (void *)&vbox_mode_funcs;
856 dev->mode_config.min_width = 0;
857 dev->mode_config.min_height = 0;
858 dev->mode_config.preferred_depth = 24;
859 dev->mode_config.max_width = VBE_DISPI_MAX_XRES;
860 dev->mode_config.max_height = VBE_DISPI_MAX_YRES;
862 for (i = 0; i < vbox->num_crtcs; ++i) {
863 vbox_crtc = vbox_crtc_init(dev, i);
864 if (IS_ERR(vbox_crtc)) {
865 ret = PTR_ERR(vbox_crtc);
866 goto err_drm_mode_cleanup;
868 encoder = vbox_encoder_init(dev, i);
869 if (!encoder) {
870 ret = -ENOMEM;
871 goto err_drm_mode_cleanup;
873 ret = vbox_connector_init(dev, vbox_crtc, encoder);
874 if (ret)
875 goto err_drm_mode_cleanup;
878 drm_mode_config_reset(dev);
879 return 0;
881 err_drm_mode_cleanup:
882 drm_mode_config_cleanup(dev);
883 return ret;
886 void vbox_mode_fini(struct vbox_private *vbox)
888 drm_mode_config_cleanup(&vbox->ddev);