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