]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - ubuntu/vbox/vboxvideo/vbox_mode.c
UBUNTU: ubuntu: vbox -- update to 5.2.0-dfsg-2
[mirror_ubuntu-bionic-kernel.git] / ubuntu / vbox / vboxvideo / vbox_mode.c
CommitLineData
056a1eb7 1/*
6d209b23
SF
2 * Copyright (C) 2013-2017 Oracle Corporation
3 * This file is based on ast_mode.c
056a1eb7
SF
4 * Copyright 2012 Red Hat Inc.
5 * Parts based on xf86-video-ast
6 * Copyright (c) 2005 ASPEED Technology Inc.
7 *
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:
15 *
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.
23 *
24 * The above copyright notice and this permission notice (including the
25 * next paragraph) shall be included in all copies or substantial portions
26 * of the Software.
27 *
28 */
29/*
30 * Authors: Dave Airlie <airlied@redhat.com>
6d209b23
SF
31 * Michael Thayer <michael.thayer@oracle.com,
32 * Hans de Goede <hdegoede@redhat.com>
056a1eb7
SF
33 */
34#include "vbox_drv.h"
056a1eb7
SF
35#include <linux/export.h>
36#include <drm/drm_crtc_helper.h>
37#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) || defined(RHEL_73)
6d209b23 38#include <drm/drm_plane_helper.h>
056a1eb7
SF
39#endif
40
6d209b23
SF
41#include "vboxvideo.h"
42#include "hgsmi_channels.h"
43
056a1eb7 44static int vbox_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv,
6d209b23
SF
45 u32 handle, u32 width, u32 height,
46 s32 hot_x, s32 hot_y);
056a1eb7
SF
47static int vbox_cursor_move(struct drm_crtc *crtc, int x, int y);
48
6d209b23
SF
49/**
50 * Set a graphics mode. Poke any required values into registers, do an HGSMI
056a1eb7
SF
51 * mode set and tell the host we support advanced graphics functions.
52 */
53static void vbox_do_modeset(struct drm_crtc *crtc,
6d209b23
SF
54 const struct drm_display_mode *mode)
55{
56 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
57 struct vbox_private *vbox;
58 int width, height, bpp, pitch;
59 unsigned int crtc_id;
60 u16 flags;
61 s32 x_offset, y_offset;
62
63 vbox = crtc->dev->dev_private;
64 width = mode->hdisplay ? mode->hdisplay : 640;
65 height = mode->vdisplay ? mode->vdisplay : 480;
66 crtc_id = vbox_crtc->crtc_id;
056a1eb7 67#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
6d209b23
SF
68 bpp = crtc->enabled ? CRTC_FB(crtc)->format->cpp[0] * 8 : 32;
69 pitch = crtc->enabled ? CRTC_FB(crtc)->pitches[0] : width * bpp / 8;
056a1eb7 70#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
6d209b23
SF
71 bpp = crtc->enabled ? CRTC_FB(crtc)->bits_per_pixel : 32;
72 pitch = crtc->enabled ? CRTC_FB(crtc)->pitches[0] : width * bpp / 8;
056a1eb7 73#else
6d209b23
SF
74 bpp = crtc->enabled ? CRTC_FB(crtc)->bits_per_pixel : 32;
75 pitch = crtc->enabled ? CRTC_FB(crtc)->pitch : width * bpp / 8;
056a1eb7 76#endif
6d209b23
SF
77 x_offset = vbox->single_framebuffer ? crtc->x : vbox_crtc->x_hint;
78 y_offset = vbox->single_framebuffer ? crtc->y : vbox_crtc->y_hint;
79
80 /*
81 * This is the old way of setting graphics modes. It assumed one screen
82 * and a frame-buffer at the start of video RAM. On older versions of
83 * VirtualBox, certain parts of the code still assume that the first
84 * screen is programmed this way, so try to fake it.
85 */
86 if (vbox_crtc->crtc_id == 0 && crtc->enabled &&
87 vbox_crtc->fb_offset / pitch < 0xffff - crtc->y &&
88 vbox_crtc->fb_offset % (bpp / 8) == 0)
89 VBoxVideoSetModeRegisters(
90 width, height, pitch * 8 / bpp,
056a1eb7 91#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
6d209b23 92 CRTC_FB(crtc)->format->cpp[0] * 8,
056a1eb7 93#else
6d209b23 94 CRTC_FB(crtc)->bits_per_pixel,
056a1eb7 95#endif
6d209b23
SF
96 0,
97 vbox_crtc->fb_offset % pitch / bpp * 8 + crtc->x,
98 vbox_crtc->fb_offset / pitch + crtc->y);
99
100 flags = VBVA_SCREEN_F_ACTIVE;
101 flags |= (crtc->enabled && !vbox_crtc->blanked) ?
102 0 : VBVA_SCREEN_F_BLANK;
103 flags |= vbox_crtc->disconnected ? VBVA_SCREEN_F_DISABLED : 0;
104 hgsmi_process_display_info(vbox->guest_pool, vbox_crtc->crtc_id,
105 x_offset, y_offset,
106 crtc->x * bpp / 8 + crtc->y * pitch,
107 pitch, width, height,
108 vbox_crtc->blanked ? 0 : bpp, flags);
056a1eb7
SF
109}
110
111static int vbox_set_view(struct drm_crtc *crtc)
112{
6d209b23
SF
113 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
114 struct vbox_private *vbox = crtc->dev->dev_private;
115 void *p;
116
117 /*
118 * Tell the host about the view. This design originally targeted the
119 * Windows XP driver architecture and assumed that each screen would
120 * have a dedicated frame buffer with the command buffer following it,
121 * the whole being a "view". The host works out which screen a command
122 * buffer belongs to by checking whether it is in the first view, then
123 * whether it is in the second and so on. The first match wins. We
124 * cheat around this by making the first view be the managed memory
125 * plus the first command buffer, the second the same plus the second
126 * buffer and so on.
127 */
128 p = hgsmi_buffer_alloc(vbox->guest_pool, sizeof(VBVAINFOVIEW),
129 HGSMI_CH_VBVA, VBVA_INFO_VIEW);
130 if (p) {
131 VBVAINFOVIEW *pInfo = (VBVAINFOVIEW *) p;
132
133 pInfo->view_index = vbox_crtc->crtc_id;
134 pInfo->u32ViewOffset = vbox_crtc->fb_offset;
135 pInfo->u32ViewSize =
136 vbox->available_vram_size - vbox_crtc->fb_offset +
137 vbox_crtc->crtc_id * VBVA_MIN_BUFFER_SIZE;
138 pInfo->u32MaxScreenSize =
139 vbox->available_vram_size - vbox_crtc->fb_offset;
140 hgsmi_buffer_submit(vbox->guest_pool, p);
141 hgsmi_buffer_free(vbox->guest_pool, p);
142 } else {
143 return -ENOMEM;
144 }
145
146 return 0;
056a1eb7
SF
147}
148
149static void vbox_crtc_load_lut(struct drm_crtc *crtc)
150{
056a1eb7
SF
151}
152
153static void vbox_crtc_dpms(struct drm_crtc *crtc, int mode)
154{
6d209b23
SF
155 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
156 struct vbox_private *vbox = crtc->dev->dev_private;
157
158 switch (mode) {
159 case DRM_MODE_DPMS_ON:
160 vbox_crtc->blanked = false;
161 break;
162 case DRM_MODE_DPMS_STANDBY:
163 case DRM_MODE_DPMS_SUSPEND:
164 case DRM_MODE_DPMS_OFF:
165 vbox_crtc->blanked = true;
166 break;
167 }
168
169 mutex_lock(&vbox->hw_mutex);
170 vbox_do_modeset(crtc, &crtc->hwmode);
171 mutex_unlock(&vbox->hw_mutex);
056a1eb7
SF
172}
173
174static bool vbox_crtc_mode_fixup(struct drm_crtc *crtc,
6d209b23
SF
175 const struct drm_display_mode *mode,
176 struct drm_display_mode *adjusted_mode)
056a1eb7 177{
6d209b23
SF
178 return true;
179}
180
181/*
182 * Try to map the layout of virtual screens to the range of the input device.
183 * Return true if we need to re-set the crtc modes due to screen offset
184 * changes.
185 */
186static bool vbox_set_up_input_mapping(struct vbox_private *vbox)
187{
188 struct drm_crtc *crtci;
189 struct drm_connector *connectori;
190 struct drm_framebuffer *fb1 = NULL;
191 bool single_framebuffer = true;
192 bool old_single_framebuffer = vbox->single_framebuffer;
193 u16 width = 0, height = 0;
194
195 /*
196 * Are we using an X.Org-style single large frame-buffer for all crtcs?
197 * If so then screen layout can be deduced from the crtc offsets.
198 * Same fall-back if this is the fbdev frame-buffer.
199 */
200 list_for_each_entry(crtci, &vbox->dev->mode_config.crtc_list, head) {
201 if (!fb1) {
202 fb1 = CRTC_FB(crtci);
203 if (to_vbox_framebuffer(fb1) == &vbox->fbdev->afb)
204 break;
205 } else if (CRTC_FB(crtci) && fb1 != CRTC_FB(crtci)) {
206 single_framebuffer = false;
207 }
208 }
209 if (single_framebuffer) {
210 list_for_each_entry(crtci, &vbox->dev->mode_config.crtc_list,
211 head) {
212 if (to_vbox_crtc(crtci)->crtc_id == 0) {
213 vbox->single_framebuffer = true;
214 vbox->input_mapping_width =
215 CRTC_FB(crtci)->width;
216 vbox->input_mapping_height =
217 CRTC_FB(crtci)->height;
218 return old_single_framebuffer !=
219 vbox->single_framebuffer;
220 }
221 }
222 }
223 /* Otherwise calculate the total span of all screens. */
224 list_for_each_entry(connectori, &vbox->dev->mode_config.connector_list,
225 head) {
226 struct vbox_connector *vbox_connector =
227 to_vbox_connector(connectori);
228 struct vbox_crtc *vbox_crtc = vbox_connector->vbox_crtc;
229
230 width = max_t(u16, width, vbox_crtc->x_hint +
231 vbox_connector->mode_hint.width);
232 height = max_t(u16, height, vbox_crtc->y_hint +
233 vbox_connector->mode_hint.height);
234 }
235
236 vbox->single_framebuffer = false;
237 vbox->input_mapping_width = width;
238 vbox->input_mapping_height = height;
239
240 return old_single_framebuffer != vbox->single_framebuffer;
056a1eb7
SF
241}
242
056a1eb7 243static int vbox_crtc_do_set_base(struct drm_crtc *crtc,
6d209b23
SF
244 struct drm_framebuffer *old_fb, int x, int y)
245{
246 struct vbox_private *vbox = crtc->dev->dev_private;
247 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
248 struct drm_gem_object *obj;
249 struct vbox_framebuffer *vbox_fb;
250 struct vbox_bo *bo;
251 int ret;
252 u64 gpu_addr;
253
254 /* Unpin the previous fb. */
255 if (old_fb) {
256 vbox_fb = to_vbox_framebuffer(old_fb);
257 obj = vbox_fb->obj;
258 bo = gem_to_vbox_bo(obj);
259 ret = vbox_bo_reserve(bo, false);
260 if (ret)
261 return ret;
262
263 vbox_bo_unpin(bo);
264 vbox_bo_unreserve(bo);
265 }
266
267 vbox_fb = to_vbox_framebuffer(CRTC_FB(crtc));
268 obj = vbox_fb->obj;
269 bo = gem_to_vbox_bo(obj);
270
271 ret = vbox_bo_reserve(bo, false);
272 if (ret)
273 return ret;
274
275 ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
276 if (ret) {
277 vbox_bo_unreserve(bo);
278 return ret;
279 }
280
281 if (&vbox->fbdev->afb == vbox_fb)
282 vbox_fbdev_set_base(vbox, gpu_addr);
283 vbox_bo_unreserve(bo);
284
285 /* vbox_set_start_address_crt1(crtc, (u32)gpu_addr); */
286 vbox_crtc->fb_offset = gpu_addr;
287 if (vbox_set_up_input_mapping(vbox)) {
288 struct drm_crtc *crtci;
289
290 list_for_each_entry(crtci, &vbox->dev->mode_config.crtc_list,
291 head) {
292 vbox_set_view(crtc);
293 vbox_do_modeset(crtci, &crtci->mode);
294 }
295 }
296
297 return 0;
056a1eb7
SF
298}
299
300static int vbox_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
6d209b23 301 struct drm_framebuffer *old_fb)
056a1eb7 302{
6d209b23 303 return vbox_crtc_do_set_base(crtc, old_fb, x, y);
056a1eb7
SF
304}
305
306static int vbox_crtc_mode_set(struct drm_crtc *crtc,
6d209b23
SF
307 struct drm_display_mode *mode,
308 struct drm_display_mode *adjusted_mode,
309 int x, int y, struct drm_framebuffer *old_fb)
310{
311 struct vbox_private *vbox = crtc->dev->dev_private;
312 int rc = 0;
313
314 vbox_crtc_mode_set_base(crtc, x, y, old_fb);
315
316 mutex_lock(&vbox->hw_mutex);
317 rc = vbox_set_view(crtc);
318 if (!rc)
319 vbox_do_modeset(crtc, mode);
320 hgsmi_update_input_mapping(vbox->guest_pool, 0, 0,
321 vbox->input_mapping_width,
322 vbox->input_mapping_height);
323 mutex_unlock(&vbox->hw_mutex);
324
325 return rc;
056a1eb7
SF
326}
327
328static void vbox_crtc_disable(struct drm_crtc *crtc)
329{
056a1eb7
SF
330}
331
332static void vbox_crtc_prepare(struct drm_crtc *crtc)
333{
056a1eb7
SF
334}
335
336static void vbox_crtc_commit(struct drm_crtc *crtc)
337{
056a1eb7
SF
338}
339
056a1eb7 340static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = {
6d209b23
SF
341 .dpms = vbox_crtc_dpms,
342 .mode_fixup = vbox_crtc_mode_fixup,
343 .mode_set = vbox_crtc_mode_set,
344 /* .mode_set_base = vbox_crtc_mode_set_base, */
345 .disable = vbox_crtc_disable,
346 .load_lut = vbox_crtc_load_lut,
347 .prepare = vbox_crtc_prepare,
348 .commit = vbox_crtc_commit,
056a1eb7
SF
349};
350
351static void vbox_crtc_reset(struct drm_crtc *crtc)
352{
056a1eb7
SF
353}
354
056a1eb7
SF
355static void vbox_crtc_destroy(struct drm_crtc *crtc)
356{
6d209b23
SF
357 drm_crtc_cleanup(crtc);
358 kfree(crtc);
056a1eb7
SF
359}
360
361static const struct drm_crtc_funcs vbox_crtc_funcs = {
6d209b23
SF
362 .cursor_move = vbox_cursor_move,
363 .cursor_set2 = vbox_cursor_set2,
364 .reset = vbox_crtc_reset,
365 .set_config = drm_crtc_helper_set_config,
366 /* .gamma_set = vbox_crtc_gamma_set, */
367 .destroy = vbox_crtc_destroy,
056a1eb7
SF
368};
369
6d209b23 370static struct vbox_crtc *vbox_crtc_init(struct drm_device *dev, unsigned int i)
056a1eb7 371{
6d209b23
SF
372 struct vbox_crtc *vbox_crtc;
373
374 vbox_crtc = kzalloc(sizeof(*vbox_crtc), GFP_KERNEL);
375 if (!vbox_crtc)
376 return NULL;
056a1eb7 377
6d209b23 378 vbox_crtc->crtc_id = i;
056a1eb7 379
6d209b23
SF
380 drm_crtc_init(dev, &vbox_crtc->base, &vbox_crtc_funcs);
381 drm_mode_crtc_set_gamma_size(&vbox_crtc->base, 256);
382 drm_crtc_helper_add(&vbox_crtc->base, &vbox_crtc_helper_funcs);
056a1eb7 383
6d209b23 384 return vbox_crtc;
056a1eb7
SF
385}
386
387static void vbox_encoder_destroy(struct drm_encoder *encoder)
388{
6d209b23
SF
389 drm_encoder_cleanup(encoder);
390 kfree(encoder);
056a1eb7
SF
391}
392
393#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0) && !defined(RHEL_73)
6d209b23 394static struct drm_encoder *drm_encoder_find(struct drm_device *dev, u32 id)
056a1eb7 395{
6d209b23
SF
396 struct drm_mode_object *mo;
397
398 mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_ENCODER);
399 return mo ? obj_to_encoder(mo) : NULL;
056a1eb7
SF
400}
401#endif
402
6d209b23
SF
403static struct drm_encoder *vbox_best_single_encoder(struct drm_connector
404 *connector)
056a1eb7 405{
6d209b23 406 int enc_id = connector->encoder_ids[0];
056a1eb7 407
6d209b23
SF
408 /* pick the encoder ids */
409 if (enc_id)
410 return drm_encoder_find(connector->dev, enc_id);
056a1eb7 411
6d209b23
SF
412 return NULL;
413}
056a1eb7
SF
414
415static const struct drm_encoder_funcs vbox_enc_funcs = {
6d209b23 416 .destroy = vbox_encoder_destroy,
056a1eb7
SF
417};
418
419static void vbox_encoder_dpms(struct drm_encoder *encoder, int mode)
420{
056a1eb7
SF
421}
422
423static bool vbox_mode_fixup(struct drm_encoder *encoder,
6d209b23
SF
424 const struct drm_display_mode *mode,
425 struct drm_display_mode *adjusted_mode)
056a1eb7 426{
6d209b23 427 return true;
056a1eb7
SF
428}
429
430static void vbox_encoder_mode_set(struct drm_encoder *encoder,
6d209b23
SF
431 struct drm_display_mode *mode,
432 struct drm_display_mode *adjusted_mode)
056a1eb7
SF
433{
434}
435
436static void vbox_encoder_prepare(struct drm_encoder *encoder)
437{
056a1eb7
SF
438}
439
440static void vbox_encoder_commit(struct drm_encoder *encoder)
441{
056a1eb7
SF
442}
443
056a1eb7 444static const struct drm_encoder_helper_funcs vbox_enc_helper_funcs = {
6d209b23
SF
445 .dpms = vbox_encoder_dpms,
446 .mode_fixup = vbox_mode_fixup,
447 .prepare = vbox_encoder_prepare,
448 .commit = vbox_encoder_commit,
449 .mode_set = vbox_encoder_mode_set,
056a1eb7
SF
450};
451
6d209b23
SF
452static struct drm_encoder *vbox_encoder_init(struct drm_device *dev,
453 unsigned int i)
056a1eb7 454{
6d209b23 455 struct vbox_encoder *vbox_encoder;
056a1eb7 456
6d209b23
SF
457 vbox_encoder = kzalloc(sizeof(*vbox_encoder), GFP_KERNEL);
458 if (!vbox_encoder)
459 return NULL;
056a1eb7 460
6d209b23
SF
461 drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs,
462 DRM_MODE_ENCODER_DAC
056a1eb7 463#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) || defined(RHEL_73)
6d209b23 464 , NULL
056a1eb7 465#endif
6d209b23
SF
466 );
467 drm_encoder_helper_add(&vbox_encoder->base, &vbox_enc_helper_funcs);
056a1eb7 468
6d209b23
SF
469 vbox_encoder->base.possible_crtcs = 1 << i;
470 return &vbox_encoder->base;
056a1eb7
SF
471}
472
6d209b23
SF
473/**
474 * Generate EDID data with a mode-unique serial number for the virtual
056a1eb7
SF
475 * monitor to try to persuade Unity that different modes correspond to
476 * different monitors and it should not try to force the same resolution on
6d209b23
SF
477 * them.
478 */
056a1eb7 479static void vbox_set_edid(struct drm_connector *connector, int width,
6d209b23
SF
480 int height)
481{
482 enum { EDID_SIZE = 128 };
483 unsigned char edid[EDID_SIZE] = {
484 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
485 0x58, 0x58, /* manufacturer (VBX) */
486 0x00, 0x00, /* product code */
487 0x00, 0x00, 0x00, 0x00, /* serial number goes here */
488 0x01, /* week of manufacture */
489 0x00, /* year of manufacture */
490 0x01, 0x03, /* EDID version */
491 0x80, /* capabilities - digital */
492 0x00, /* horiz. res in cm, zero for projectors */
493 0x00, /* vert. res in cm */
494 0x78, /* display gamma (120 == 2.2). */
495 0xEE, /* features (standby, suspend, off, RGB, std */
496 /* colour space, preferred timing mode) */
497 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
498 /* chromaticity for standard colour space. */
499 0x00, 0x00, 0x00, /* no default timings */
500 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
501 0x01, 0x01,
502 0x01, 0x01, 0x01, 0x01, /* no standard timings */
503 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
504 0x02, 0x02,
505 /* descriptor block 1 goes below */
506 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
507 /* descriptor block 2, monitor ranges */
508 0x00, 0x00, 0x00, 0xFD, 0x00,
509 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
510 0x20, 0x20,
511 /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
512 0x20,
513 /* descriptor block 3, monitor name */
514 0x00, 0x00, 0x00, 0xFC, 0x00,
515 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r',
516 '\n',
517 /* descriptor block 4: dummy data */
518 0x00, 0x00, 0x00, 0x10, 0x00,
519 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
520 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
521 0x20,
522 0x00, /* number of extensions */
523 0x00 /* checksum goes here */
524 };
525 int clock = (width + 6) * (height + 6) * 60 / 10000;
526 unsigned int i, sum = 0;
527
528 edid[12] = width & 0xff;
529 edid[13] = width >> 8;
530 edid[14] = height & 0xff;
531 edid[15] = height >> 8;
532 edid[54] = clock & 0xff;
533 edid[55] = clock >> 8;
534 edid[56] = width & 0xff;
535 edid[58] = (width >> 4) & 0xf0;
536 edid[59] = height & 0xff;
537 edid[61] = (height >> 4) & 0xf0;
538 for (i = 0; i < EDID_SIZE - 1; ++i)
539 sum += edid[i];
540 edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF;
541 drm_mode_connector_update_edid_property(connector, (struct edid *)edid);
056a1eb7
SF
542}
543
544static int vbox_get_modes(struct drm_connector *connector)
545{
6d209b23
SF
546 struct vbox_connector *vbox_connector = NULL;
547 struct drm_display_mode *mode = NULL;
548 struct vbox_private *vbox = NULL;
549 unsigned int num_modes = 0;
550 int preferred_width, preferred_height;
551
552 vbox_connector = to_vbox_connector(connector);
553 vbox = connector->dev->dev_private;
554 /*
555 * Heuristic: we do not want to tell the host that we support dynamic
556 * resizing unless we feel confident that the user space client using
557 * the video driver can handle hot-plug events. So the first time modes
558 * are queried after a "master" switch we tell the host that we do not,
559 * and immediately after we send the client a hot-plug notification as
560 * a test to see if they will respond and query again.
561 * That is also the reason why capabilities are reported to the host at
562 * this place in the code rather than elsewhere.
563 * We need to report the flags location before reporting the IRQ
564 * capability.
565 */
566 hgsmi_report_flags_location(vbox->guest_pool, GUEST_HEAP_OFFSET(vbox) +
567 HOST_FLAGS_OFFSET);
568 if (vbox_connector->vbox_crtc->crtc_id == 0)
569 vbox_report_caps(vbox);
570 if (!vbox->initial_mode_queried) {
571 if (vbox_connector->vbox_crtc->crtc_id == 0) {
572 vbox->initial_mode_queried = true;
573 vbox_report_hotplug(vbox);
574 }
575 return drm_add_modes_noedid(connector, 800, 600);
576 }
577 num_modes = drm_add_modes_noedid(connector, 2560, 1600);
578 preferred_width = vbox_connector->mode_hint.width ?
579 vbox_connector->mode_hint.width : 1024;
580 preferred_height = vbox_connector->mode_hint.height ?
581 vbox_connector->mode_hint.height : 768;
582 mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height,
583 60, false, false, false);
584 if (mode) {
585 mode->type |= DRM_MODE_TYPE_PREFERRED;
586 drm_mode_probed_add(connector, mode);
587 ++num_modes;
588 }
589 vbox_set_edid(connector, preferred_width, preferred_height);
590
591#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0) || defined(RHEL_73)
592 if (vbox_connector->vbox_crtc->x_hint != -1)
593 drm_object_property_set_value(&connector->base,
594 vbox->dev->mode_config.suggested_x_property,
595 vbox_connector->vbox_crtc->x_hint);
596 else
597 drm_object_property_set_value(&connector->base,
598 vbox->dev->mode_config.suggested_x_property, 0);
599
600 if (vbox_connector->vbox_crtc->y_hint != -1)
601 drm_object_property_set_value(&connector->base,
602 vbox->dev->mode_config.suggested_y_property,
603 vbox_connector->vbox_crtc->y_hint);
604 else
605 drm_object_property_set_value(&connector->base,
606 vbox->dev->mode_config.suggested_y_property, 0);
607#endif
608
609 return num_modes;
056a1eb7
SF
610}
611
612static int vbox_mode_valid(struct drm_connector *connector,
6d209b23 613 struct drm_display_mode *mode)
056a1eb7 614{
6d209b23 615 return MODE_OK;
056a1eb7
SF
616}
617
618static void vbox_connector_destroy(struct drm_connector *connector)
619{
6d209b23 620 struct vbox_connector *vbox_connector = NULL;
056a1eb7 621
6d209b23 622 vbox_connector = to_vbox_connector(connector);
056a1eb7 623#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0) && !defined(RHEL_73)
6d209b23 624 drm_sysfs_connector_remove(connector);
056a1eb7 625#else
6d209b23 626 drm_connector_unregister(connector);
056a1eb7 627#endif
6d209b23
SF
628 drm_connector_cleanup(connector);
629 kfree(connector);
056a1eb7
SF
630}
631
632static enum drm_connector_status
633vbox_connector_detect(struct drm_connector *connector, bool force)
634{
6d209b23
SF
635 struct vbox_connector *vbox_connector = NULL;
636
637 (void)force;
638 vbox_connector = to_vbox_connector(connector);
056a1eb7 639
6d209b23
SF
640 return vbox_connector->mode_hint.disconnected ?
641 connector_status_disconnected : connector_status_connected;
056a1eb7
SF
642}
643
6d209b23
SF
644static int vbox_fill_modes(struct drm_connector *connector, u32 max_x,
645 u32 max_y)
056a1eb7 646{
6d209b23
SF
647 struct vbox_connector *vbox_connector;
648 struct drm_device *dev;
649 struct drm_display_mode *mode, *iterator;
056a1eb7 650
6d209b23
SF
651 vbox_connector = to_vbox_connector(connector);
652 dev = vbox_connector->base.dev;
653 list_for_each_entry_safe(mode, iterator, &connector->modes, head) {
654 list_del(&mode->head);
655 drm_mode_destroy(dev, mode);
656 }
657
658 return drm_helper_probe_single_connector_modes(connector, max_x, max_y);
056a1eb7
SF
659}
660
661static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = {
6d209b23
SF
662 .mode_valid = vbox_mode_valid,
663 .get_modes = vbox_get_modes,
664 .best_encoder = vbox_best_single_encoder,
056a1eb7
SF
665};
666
667static const struct drm_connector_funcs vbox_connector_funcs = {
6d209b23
SF
668 .dpms = drm_helper_connector_dpms,
669 .detect = vbox_connector_detect,
670 .fill_modes = vbox_fill_modes,
671 .destroy = vbox_connector_destroy,
056a1eb7
SF
672};
673
674static int vbox_connector_init(struct drm_device *dev,
6d209b23
SF
675 struct vbox_crtc *vbox_crtc,
676 struct drm_encoder *encoder)
056a1eb7 677{
6d209b23
SF
678 struct vbox_connector *vbox_connector;
679 struct drm_connector *connector;
056a1eb7 680
6d209b23
SF
681 vbox_connector = kzalloc(sizeof(*vbox_connector), GFP_KERNEL);
682 if (!vbox_connector)
683 return -ENOMEM;
056a1eb7 684
6d209b23
SF
685 connector = &vbox_connector->base;
686 vbox_connector->vbox_crtc = vbox_crtc;
056a1eb7 687
6d209b23
SF
688 drm_connector_init(dev, connector, &vbox_connector_funcs,
689 DRM_MODE_CONNECTOR_VGA);
690 drm_connector_helper_add(connector, &vbox_connector_helper_funcs);
056a1eb7 691
6d209b23
SF
692 connector->interlace_allowed = 0;
693 connector->doublescan_allowed = 0;
056a1eb7
SF
694
695#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0) || defined(RHEL_73)
6d209b23
SF
696 drm_mode_create_suggested_offset_properties(dev);
697 drm_object_attach_property(&connector->base,
698 dev->mode_config.suggested_x_property, 0);
699 drm_object_attach_property(&connector->base,
700 dev->mode_config.suggested_y_property, 0);
056a1eb7
SF
701#endif
702#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0) && !defined(RHEL_73)
6d209b23 703 drm_sysfs_connector_add(connector);
056a1eb7 704#else
6d209b23 705 drm_connector_register(connector);
056a1eb7
SF
706#endif
707
6d209b23 708 drm_mode_connector_attach_encoder(connector, encoder);
056a1eb7 709
6d209b23 710 return 0;
056a1eb7
SF
711}
712
713int vbox_mode_init(struct drm_device *dev)
714{
6d209b23
SF
715 struct vbox_private *vbox = dev->dev_private;
716 struct drm_encoder *encoder;
717 struct vbox_crtc *vbox_crtc;
718 unsigned int i;
719
720 /* vbox_cursor_init(dev); */
721 for (i = 0; i < vbox->num_crtcs; ++i) {
722 vbox_crtc = vbox_crtc_init(dev, i);
723 if (!vbox_crtc)
724 return -ENOMEM;
725 encoder = vbox_encoder_init(dev, i);
726 if (!encoder)
727 return -ENOMEM;
728 vbox_connector_init(dev, vbox_crtc, encoder);
729 }
730
731 return 0;
056a1eb7
SF
732}
733
734void vbox_mode_fini(struct drm_device *dev)
735{
6d209b23 736 /* vbox_cursor_fini(dev); */
056a1eb7
SF
737}
738
6d209b23
SF
739/**
740 * Copy the ARGB image and generate the mask, which is needed in case the host
741 * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set
742 * if the corresponding alpha value in the ARGB image is greater than 0xF0.
743 */
744static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height,
745 size_t mask_size)
056a1eb7 746{
6d209b23
SF
747 size_t line_size = (width + 7) / 8;
748 u32 i, j;
056a1eb7 749
6d209b23
SF
750 memcpy(dst + mask_size, src, width * height * 4);
751 for (i = 0; i < height; ++i)
752 for (j = 0; j < width; ++j)
753 if (((u32 *)src)[i * width + j] > 0xf0000000)
754 dst[i * line_size + j / 8] |= (0x80 >> (j % 8));
056a1eb7
SF
755}
756
757static int vbox_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv,
6d209b23
SF
758 u32 handle, u32 width, u32 height,
759 s32 hot_x, s32 hot_y)
760{
761 struct vbox_private *vbox = crtc->dev->dev_private;
762 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
763 struct drm_gem_object *obj;
764 struct vbox_bo *bo;
765 int ret, rc;
766 struct ttm_bo_kmap_obj uobj_map;
767 u8 *src;
768 u8 *dst = NULL;
769 u32 caps = 0;
770 size_t data_size, mask_size;
771 bool src_isiomem;
772
773 /*
774 * Re-set this regularly as in 5.0.20 and earlier the information was
775 * lost on save and restore.
776 */
777 hgsmi_update_input_mapping(vbox->guest_pool, 0, 0,
778 vbox->input_mapping_width,
779 vbox->input_mapping_height);
780 if (!handle) {
781 bool cursor_enabled = false;
782 struct drm_crtc *crtci;
783
784 /* Hide cursor. */
785 vbox_crtc->cursor_enabled = false;
786 list_for_each_entry(crtci, &vbox->dev->mode_config.crtc_list,
787 head)
788 if (to_vbox_crtc(crtci)->cursor_enabled)
789 cursor_enabled = true;
790
791 if (!cursor_enabled)
792 hgsmi_update_pointer_shape(vbox->guest_pool, 0, 0, 0,
793 0, 0, NULL, 0);
794 return 0;
795 }
796 vbox_crtc->cursor_enabled = true;
797 if (width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT ||
798 width == 0 || height == 0)
799 return -EINVAL;
800 rc = hgsmi_query_conf(vbox->guest_pool,
801 VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps);
802 ret = rc == VINF_SUCCESS ? 0 : rc == VERR_NO_MEMORY ? -ENOMEM : -EINVAL;
803 if (ret)
804 return ret;
805
806 if (!(caps & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE))
807 /*
808 * -EINVAL means cursor_set2() not supported, -EAGAIN means
809 * retry at once.
810 */
811 return -EBUSY;
056a1eb7
SF
812
813#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) || defined(RHEL_74)
6d209b23 814 obj = drm_gem_object_lookup(file_priv, handle);
056a1eb7 815#else
6d209b23 816 obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
056a1eb7 817#endif
6d209b23
SF
818 if (obj) {
819 bo = gem_to_vbox_bo(obj);
820 ret = vbox_bo_reserve(bo, false);
821 if (!ret) {
822 /*
823 * The mask must be calculated based on the alpha
824 * channel, one bit per ARGB word, and must be 32-bit
825 * padded.
826 */
827 mask_size = ((width + 7) / 8 * height + 3) & ~3;
828 data_size = width * height * 4 + mask_size;
829 vbox->cursor_hot_x = min_t(u32, max(hot_x, 0), width);
830 vbox->cursor_hot_y = min_t(u32, max(hot_y, 0), height);
831 vbox->cursor_width = width;
832 vbox->cursor_height = height;
833 vbox->cursor_data_size = data_size;
834 dst = vbox->cursor_data;
835 ret =
836 ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages,
837 &uobj_map);
838 if (!ret) {
839 src =
840 ttm_kmap_obj_virtual(&uobj_map,
841 &src_isiomem);
842 if (!src_isiomem) {
843 u32 flags =
844 VBOX_MOUSE_POINTER_VISIBLE |
845 VBOX_MOUSE_POINTER_SHAPE |
846 VBOX_MOUSE_POINTER_ALPHA;
847 copy_cursor_image(src, dst, width,
848 height, mask_size);
849 rc = hgsmi_update_pointer_shape(
850 vbox->guest_pool, flags,
851 vbox->cursor_hot_x,
852 vbox->cursor_hot_y,
853 width, height, dst, data_size);
854 ret =
855 rc == VINF_SUCCESS ? 0 : rc ==
856 VERR_NO_MEMORY ? -ENOMEM : rc ==
857 VERR_NOT_SUPPORTED ? -EBUSY :
858 -EINVAL;
859 } else {
860 DRM_ERROR("src cursor bo should be in main memory\n");
861 }
862 ttm_bo_kunmap(&uobj_map);
863 } else {
864 vbox->cursor_data_size = 0;
865 }
866 vbox_bo_unreserve(bo);
867 }
868 drm_gem_object_unreference_unlocked(obj);
869 } else {
870 DRM_ERROR("Cannot find cursor object %x for crtc\n", handle);
871 ret = -ENOENT;
872 }
873
874 return ret;
875}
876
877static int vbox_cursor_move(struct drm_crtc *crtc, int x, int y)
878{
879 struct vbox_private *vbox = crtc->dev->dev_private;
880 u32 flags = VBOX_MOUSE_POINTER_VISIBLE |
881 VBOX_MOUSE_POINTER_SHAPE | VBOX_MOUSE_POINTER_ALPHA;
882 s32 crtc_x =
883 vbox->single_framebuffer ? crtc->x : to_vbox_crtc(crtc)->x_hint;
884 s32 crtc_y =
885 vbox->single_framebuffer ? crtc->y : to_vbox_crtc(crtc)->y_hint;
886 u32 host_x, host_y;
887 u32 hot_x = 0;
888 u32 hot_y = 0;
889 int rc;
890
891 /*
892 * We compare these to unsigned later and don't
893 * need to handle negative.
894 */
895 if (x + crtc_x < 0 || y + crtc_y < 0 || vbox->cursor_data_size == 0)
896 return 0;
897
898 rc = hgsmi_cursor_position(vbox->guest_pool, true, x + crtc_x,
899 y + crtc_y, &host_x, &host_y);
900 /* Work around a bug after save and restore in 5.0.20 and earlier. */
901 if (RT_FAILURE(rc) || (host_x == 0 && host_y == 0))
902 return rc == VINF_SUCCESS ? 0
903 : rc == VERR_NO_MEMORY ? -ENOMEM : -EINVAL;
904 if (x + crtc_x < host_x)
905 hot_x = min(host_x - x - crtc_x, vbox->cursor_width);
906 if (y + crtc_y < host_y)
907 hot_y = min(host_y - y - crtc_y, vbox->cursor_height);
908 if (hot_x == vbox->cursor_hot_x && hot_y == vbox->cursor_hot_y)
909 return 0;
910 vbox->cursor_hot_x = hot_x;
911 vbox->cursor_hot_y = hot_y;
912 rc = hgsmi_update_pointer_shape(vbox->guest_pool, flags, hot_x, hot_y,
913 vbox->cursor_width,
914 vbox->cursor_height, vbox->cursor_data,
915 vbox->cursor_data_size);
916 return rc == VINF_SUCCESS ? 0 : rc == VERR_NO_MEMORY ? -ENOMEM : rc ==
917 VERR_NOT_SUPPORTED ? -EBUSY : -EINVAL;
056a1eb7 918}