1 /* $Id: vbox_mode.c $ */
3 * VirtualBox Additions Linux kernel video driver
7 * Copyright (C) 2013-2016 Oracle Corporation
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 * --------------------------------------------------------------------
18 * This code is based on
20 * with the following copyright and permission notice:
22 * Copyright 2012 Red Hat Inc.
23 * Parts based on xf86-video-ast
24 * Copyright (c) 2005 ASPEED Technology Inc.
26 * Permission is hereby granted, free of charge, to any person obtaining a
27 * copy of this software and associated documentation files (the
28 * "Software"), to deal in the Software without restriction, including
29 * without limitation the rights to use, copy, modify, merge, publish,
30 * distribute, sub license, and/or sell copies of the Software, and to
31 * permit persons to whom the Software is furnished to do so, subject to
32 * the following conditions:
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
37 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
38 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
39 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
40 * USE OR OTHER DEALINGS IN THE SOFTWARE.
42 * The above copyright notice and this permission notice (including the
43 * next paragraph) shall be included in all copies or substantial portions
48 * Authors: Dave Airlie <airlied@redhat.com>
52 #include <VBox/VBoxVideo.h>
54 #include <linux/export.h>
55 #include <drm/drm_crtc_helper.h>
56 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
57 # include <drm/drm_plane_helper.h>
60 static int vbox_cursor_set2(struct drm_crtc
*crtc
, struct drm_file
*file_priv
,
61 uint32_t handle
, uint32_t width
, uint32_t height
,
62 int32_t hot_x
, int32_t hot_y
);
63 static int vbox_cursor_move(struct drm_crtc
*crtc
, int x
, int y
);
65 /** Set a graphics mode. Poke any required values into registers, do an HGSMI
66 * mode set and tell the host we support advanced graphics functions.
68 static void vbox_do_modeset(struct drm_crtc
*crtc
,
69 const struct drm_display_mode
*mode
)
71 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
72 struct vbox_private
*vbox
;
73 int width
, height
, bpp
, pitch
;
77 LogFunc(("vboxvideo: %d: vbox_crtc=%p, CRTC_FB(crtc)=%p\n", __LINE__
,
78 vbox_crtc
, CRTC_FB(crtc
)));
79 vbox
= crtc
->dev
->dev_private
;
80 width
= mode
->hdisplay
? mode
->hdisplay
: 640;
81 height
= mode
->vdisplay
? mode
->vdisplay
: 480;
82 crtc_id
= vbox_crtc
->crtc_id
;
83 bpp
= crtc
->enabled
? CRTC_FB(crtc
)->bits_per_pixel
: 32;
84 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
85 pitch
= crtc
->enabled
? CRTC_FB(crtc
)->pitch
: width
* bpp
/ 8;
87 pitch
= crtc
->enabled
? CRTC_FB(crtc
)->pitches
[0] : width
* bpp
/ 8;
89 /* This is the old way of setting graphics modes. It assumed one screen
90 * and a frame-buffer at the start of video RAM. On older versions of
91 * VirtualBox, certain parts of the code still assume that the first
92 * screen is programmed this way, so try to fake it. */
93 if ( vbox_crtc
->crtc_id
== 0
95 && vbox_crtc
->fb_offset
/ pitch
< 0xffff - crtc
->y
96 && vbox_crtc
->fb_offset
% (bpp
/ 8) == 0)
97 VBoxVideoSetModeRegisters(width
, height
, pitch
* 8 / bpp
,
98 CRTC_FB(crtc
)->bits_per_pixel
, 0,
99 vbox_crtc
->fb_offset
% pitch
/ bpp
* 8 + crtc
->x
,
100 vbox_crtc
->fb_offset
/ pitch
+ crtc
->y
);
101 flags
= VBVA_SCREEN_F_ACTIVE
;
102 flags
|= (crtc
->enabled
&& !vbox_crtc
->blanked
? 0 : VBVA_SCREEN_F_BLANK
);
103 flags
|= (vbox_crtc
->disconnected
? VBVA_SCREEN_F_DISABLED
: 0);
104 VBoxHGSMIProcessDisplayInfo(&vbox
->submit_info
, vbox_crtc
->crtc_id
,
106 crtc
->x
* bpp
/ 8 + crtc
->y
* pitch
,
107 pitch
, width
, height
,
108 vbox_crtc
->blanked
? 0 : bpp
, flags
);
109 VBoxHGSMIReportFlagsLocation(&vbox
->submit_info
, vbox
->vram_map_start
110 + vbox
->host_flags_offset
);
111 LogFunc(("vboxvideo: %d\n", __LINE__
));
114 static int vbox_set_view(struct drm_crtc
*crtc
)
116 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
117 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
120 LogFunc(("vboxvideo: %d: vbox_crtc=%p\n", __LINE__
, vbox_crtc
));
121 /* Tell the host about the view. This design originally targeted the
122 * Windows XP driver architecture and assumed that each screen would have
123 * a dedicated frame buffer with the command buffer following it, the whole
124 * being a "view". The host works out which screen a command buffer belongs
125 * to by checking whether it is in the first view, then whether it is in the
126 * second and so on. The first match wins. We cheat around this by making
127 * the first view be the managed memory plus the first command buffer, the
128 * second the same plus the second buffer and so on. */
129 p
= VBoxHGSMIBufferAlloc(&vbox
->submit_info
, sizeof(VBVAINFOVIEW
), HGSMI_CH_VBVA
,
133 VBVAINFOVIEW
*pInfo
= (VBVAINFOVIEW
*)p
;
134 pInfo
->u32ViewIndex
= vbox_crtc
->crtc_id
;
135 pInfo
->u32ViewOffset
= vbox_crtc
->fb_offset
;
136 pInfo
->u32ViewSize
= vbox
->available_vram_size
- vbox_crtc
->fb_offset
137 + vbox_crtc
->crtc_id
* VBVA_MIN_BUFFER_SIZE
;
138 pInfo
->u32MaxScreenSize
= vbox
->available_vram_size
- vbox_crtc
->fb_offset
;
139 VBoxHGSMIBufferSubmit(&vbox
->submit_info
, p
);
140 VBoxHGSMIBufferFree(&vbox
->submit_info
, p
);
144 LogFunc(("vboxvideo: %d: p=%p\n", __LINE__
, p
));
148 static void vbox_crtc_load_lut(struct drm_crtc
*crtc
)
153 static void vbox_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
155 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
156 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
158 LogFunc(("vboxvideo: %d: vbox_crtc=%p, mode=%d\n", __LINE__
, vbox_crtc
,
161 case DRM_MODE_DPMS_ON
:
162 vbox_crtc
->blanked
= false;
164 case DRM_MODE_DPMS_STANDBY
:
165 case DRM_MODE_DPMS_SUSPEND
:
166 case DRM_MODE_DPMS_OFF
:
167 vbox_crtc
->blanked
= true;
170 mutex_lock(&vbox
->hw_mutex
);
171 vbox_do_modeset(crtc
, &crtc
->hwmode
);
172 mutex_unlock(&vbox
->hw_mutex
);
173 LogFunc(("vboxvideo: %d\n", __LINE__
));
176 static bool vbox_crtc_mode_fixup(struct drm_crtc
*crtc
,
177 const struct drm_display_mode
*mode
,
178 struct drm_display_mode
*adjusted_mode
)
183 /* We move buffers which are not in active use out of VRAM to save memory. */
184 static int vbox_crtc_do_set_base(struct drm_crtc
*crtc
,
185 struct drm_framebuffer
*fb
,
186 int x
, int y
, int atomic
)
188 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
189 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
190 struct drm_gem_object
*obj
;
191 struct vbox_framebuffer
*vbox_fb
;
196 LogFunc(("vboxvideo: %d: fb=%p, vbox_crtc=%p\n", __LINE__
, fb
, vbox_crtc
));
197 /* push the previous fb to system ram */
199 vbox_fb
= to_vbox_framebuffer(fb
);
201 bo
= gem_to_vbox_bo(obj
);
202 ret
= vbox_bo_reserve(bo
, false);
205 vbox_bo_push_sysram(bo
);
206 vbox_bo_unreserve(bo
);
209 vbox_fb
= to_vbox_framebuffer(CRTC_FB(crtc
));
211 bo
= gem_to_vbox_bo(obj
);
213 ret
= vbox_bo_reserve(bo
, false);
217 ret
= vbox_bo_pin(bo
, TTM_PL_FLAG_VRAM
, &gpu_addr
);
219 vbox_bo_unreserve(bo
);
223 if (&vbox
->fbdev
->afb
== vbox_fb
) {
224 /* if pushing console in kmap it */
225 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &bo
->kmap
);
227 DRM_ERROR("failed to kmap fbcon\n");
229 vbox_fbdev_set_base(vbox
, gpu_addr
);
231 vbox_bo_unreserve(bo
);
233 /* vbox_set_start_address_crt1(crtc, (u32)gpu_addr); */
234 vbox_crtc
->fb_offset
= gpu_addr
;
235 if (vbox_crtc
->crtc_id
== 0) {
236 vbox
->input_mapping_width
= CRTC_FB(crtc
)->width
;
237 vbox
->input_mapping_height
= CRTC_FB(crtc
)->height
;
239 LogFunc(("vboxvideo: %d: vbox_fb=%p, obj=%p, bo=%p, gpu_addr=%u\n",
240 __LINE__
, vbox_fb
, obj
, bo
, (unsigned)gpu_addr
));
244 static int vbox_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
245 struct drm_framebuffer
*old_fb
)
247 LogFunc(("vboxvideo: %d\n", __LINE__
));
248 return vbox_crtc_do_set_base(crtc
, old_fb
, x
, y
, 0);
251 static int vbox_crtc_mode_set(struct drm_crtc
*crtc
,
252 struct drm_display_mode
*mode
,
253 struct drm_display_mode
*adjusted_mode
,
255 struct drm_framebuffer
*old_fb
)
257 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
260 LogFunc(("vboxvideo: %d: vbox=%p\n", __LINE__
, vbox
));
261 vbox_crtc_mode_set_base(crtc
, x
, y
, old_fb
);
262 mutex_lock(&vbox
->hw_mutex
);
263 rc
= vbox_set_view(crtc
);
265 vbox_do_modeset(crtc
, mode
);
266 /* Note that the input mapping is always relative to the first screen. */
267 VBoxHGSMIUpdateInputMapping(&vbox
->submit_info
, 0, 0,
268 vbox
->input_mapping_width
,
269 vbox
->input_mapping_height
);
270 mutex_unlock(&vbox
->hw_mutex
);
271 LogFunc(("vboxvideo: %d\n", __LINE__
));
275 static void vbox_crtc_disable(struct drm_crtc
*crtc
)
280 static void vbox_crtc_prepare(struct drm_crtc
*crtc
)
285 static void vbox_crtc_commit(struct drm_crtc
*crtc
)
291 static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs
= {
292 .dpms
= vbox_crtc_dpms
,
293 .mode_fixup
= vbox_crtc_mode_fixup
,
294 .mode_set
= vbox_crtc_mode_set
,
295 /* .mode_set_base = vbox_crtc_mode_set_base, */
296 .disable
= vbox_crtc_disable
,
297 .load_lut
= vbox_crtc_load_lut
,
298 .prepare
= vbox_crtc_prepare
,
299 .commit
= vbox_crtc_commit
,
303 static void vbox_crtc_reset(struct drm_crtc
*crtc
)
309 static void vbox_crtc_destroy(struct drm_crtc
*crtc
)
311 drm_crtc_cleanup(crtc
);
315 static const struct drm_crtc_funcs vbox_crtc_funcs
= {
316 .cursor_move
= vbox_cursor_move
,
317 .cursor_set2
= vbox_cursor_set2
,
318 .reset
= vbox_crtc_reset
,
319 .set_config
= drm_crtc_helper_set_config
,
320 /* .gamma_set = vbox_crtc_gamma_set, */
321 .destroy
= vbox_crtc_destroy
,
324 static struct vbox_crtc
*vbox_crtc_init(struct drm_device
*dev
, unsigned i
)
326 struct vbox_crtc
*vbox_crtc
;
328 LogFunc(("vboxvideo: %d\n", __LINE__
));
329 vbox_crtc
= kzalloc(sizeof(struct vbox_crtc
), GFP_KERNEL
);
332 vbox_crtc
->crtc_id
= i
;
334 drm_crtc_init(dev
, &vbox_crtc
->base
, &vbox_crtc_funcs
);
335 drm_mode_crtc_set_gamma_size(&vbox_crtc
->base
, 256);
336 drm_crtc_helper_add(&vbox_crtc
->base
, &vbox_crtc_helper_funcs
);
337 LogFunc(("vboxvideo: %d: crtc=%p\n", __LINE__
, vbox_crtc
));
342 static void vbox_encoder_destroy(struct drm_encoder
*encoder
)
344 LogFunc(("vboxvideo: %d: encoder=%p\n", __LINE__
, encoder
));
345 drm_encoder_cleanup(encoder
);
349 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
350 static struct drm_encoder
*drm_encoder_find(struct drm_device
*dev
, uint32_t id
)
352 struct drm_mode_object
*mo
;
353 mo
= drm_mode_object_find(dev
, id
, DRM_MODE_OBJECT_ENCODER
);
354 return mo
? obj_to_encoder(mo
) : NULL
;
358 static struct drm_encoder
*vbox_best_single_encoder(struct drm_connector
*connector
)
360 int enc_id
= connector
->encoder_ids
[0];
362 LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__
, connector
));
363 /* pick the encoder ids */
365 return drm_encoder_find(connector
->dev
, enc_id
);
366 LogFunc(("vboxvideo: %d\n", __LINE__
));
371 static const struct drm_encoder_funcs vbox_enc_funcs
= {
372 .destroy
= vbox_encoder_destroy
,
375 static void vbox_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
380 static bool vbox_mode_fixup(struct drm_encoder
*encoder
,
381 const struct drm_display_mode
*mode
,
382 struct drm_display_mode
*adjusted_mode
)
387 static void vbox_encoder_mode_set(struct drm_encoder
*encoder
,
388 struct drm_display_mode
*mode
,
389 struct drm_display_mode
*adjusted_mode
)
393 static void vbox_encoder_prepare(struct drm_encoder
*encoder
)
398 static void vbox_encoder_commit(struct drm_encoder
*encoder
)
404 static const struct drm_encoder_helper_funcs vbox_enc_helper_funcs
= {
405 .dpms
= vbox_encoder_dpms
,
406 .mode_fixup
= vbox_mode_fixup
,
407 .prepare
= vbox_encoder_prepare
,
408 .commit
= vbox_encoder_commit
,
409 .mode_set
= vbox_encoder_mode_set
,
412 static struct drm_encoder
*vbox_encoder_init(struct drm_device
*dev
, unsigned i
)
414 struct vbox_encoder
*vbox_encoder
;
416 LogFunc(("vboxvideo: %d: dev=%d\n", __LINE__
));
417 vbox_encoder
= kzalloc(sizeof(struct vbox_encoder
), GFP_KERNEL
);
421 drm_encoder_init(dev
, &vbox_encoder
->base
, &vbox_enc_funcs
,
423 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
427 drm_encoder_helper_add(&vbox_encoder
->base
, &vbox_enc_helper_funcs
);
429 vbox_encoder
->base
.possible_crtcs
= 1 << i
;
430 LogFunc(("vboxvideo: %d: vbox_encoder=%p\n", __LINE__
, vbox_encoder
));
431 return &vbox_encoder
->base
;
434 /** Generate EDID data with a mode-unique serial number for the virtual
435 * monitor to try to persuade Unity that different modes correspond to
436 * different monitors and it should not try to force the same resolution on
438 static void vbox_set_edid(struct drm_connector
*connector
, int width
,
441 enum { EDID_SIZE
= 128 };
442 unsigned char edid
[EDID_SIZE
] = {
443 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
444 0x58, 0x58, /* manufacturer (VBX) */
445 0x00, 0x00, /* product code */
446 0x00, 0x00,0x00, 0x00, /* serial number goes here */
447 0x01, /* week of manufacture */
448 0x00, /* year of manufacture */
449 0x01, 0x03, /* EDID version */
450 0x80, /* capabilities - digital */
451 0x00, /* horiz. res in cm, zero for projectors */
452 0x00, /* vert. res in cm */
453 0x78, /* display gamma (120 == 2.2). */
454 0xEE, /* features (standby, suspend, off, RGB, standard colour space,
455 * preferred timing mode) */
456 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
457 /* chromaticity for standard colour space. */
458 0x00, 0x00, 0x00, /* no default timings */
459 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
460 0x01, 0x01, 0x01, 0x01, /* no standard timings */
461 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02, 0x02, 0x02,
462 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* descriptor block 1 goes here */
463 0x00, 0x00, 0x00, 0xFD, 0x00, /* descriptor block 2, monitor ranges */
464 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
465 0x20, /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
466 0x00, 0x00, 0x00, 0xFC, 0x00, /* descriptor block 3, monitor name */
467 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r', '\n',
468 0x00, 0x00, 0x00, 0x10, 0x00, /* descriptor block 4: dummy data */
469 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
471 0x00, /* number of extensions */
472 0x00 /* checksum goes here */
474 int clock
= (width
+ 6) * (height
+ 6) * 60 / 10000;
478 edid
[12] = width
& 0xff;
479 edid
[13] = width
>> 8;
480 edid
[14] = height
& 0xff;
481 edid
[15] = height
>> 8;
482 edid
[54] = clock
& 0xff;
483 edid
[55] = clock
>> 8;
484 edid
[56] = width
& 0xff;
485 edid
[58] = (width
>> 4) & 0xf0;
486 edid
[59] = height
& 0xff;
487 edid
[61] = (height
>> 4) & 0xf0;
488 for (i
= 0; i
< EDID_SIZE
- 1; ++i
)
490 edid
[EDID_SIZE
- 1] = (0x100 - (sum
& 0xFF)) & 0xFF;
491 drm_mode_connector_update_edid_property(connector
, (struct edid
*)edid
);
494 static int vbox_get_modes(struct drm_connector
*connector
)
496 struct vbox_connector
*vbox_connector
= NULL
;
497 struct drm_display_mode
*mode
= NULL
;
498 struct vbox_private
*vbox
= NULL
;
499 unsigned num_modes
= 0;
500 int preferred_width
, preferred_height
;
502 LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__
, connector
));
503 vbox_connector
= to_vbox_connector(connector
);
504 vbox
= connector
->dev
->dev_private
;
505 if (vbox_connector
->vbox_crtc
->crtc_id
== 0)
506 vbox_enable_caps(vbox
);
507 if (!vbox
->initial_mode_queried
) {
508 if (vbox_connector
->vbox_crtc
->crtc_id
== 0) {
509 vbox
->initial_mode_queried
= true;
510 vbox_report_hotplug(vbox
);
512 return drm_add_modes_noedid(connector
, 800, 600);
514 num_modes
= drm_add_modes_noedid(connector
, 2560, 1600);
515 preferred_width
= vbox_connector
->mode_hint
.width
? vbox_connector
->mode_hint
.width
: 1024;
516 preferred_height
= vbox_connector
->mode_hint
.height
? vbox_connector
->mode_hint
.height
: 768;
517 mode
= drm_cvt_mode(connector
->dev
, preferred_width
, preferred_height
, 60, false,
521 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
522 drm_mode_probed_add(connector
, mode
);
525 vbox_set_edid(connector
, preferred_width
, preferred_height
);
529 static int vbox_mode_valid(struct drm_connector
*connector
,
530 struct drm_display_mode
*mode
)
535 static void vbox_connector_destroy(struct drm_connector
*connector
)
537 struct vbox_connector
*vbox_connector
= NULL
;
539 LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__
, connector
));
540 vbox_connector
= to_vbox_connector(connector
);
541 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
542 drm_sysfs_connector_remove(connector
);
544 drm_connector_unregister(connector
);
546 drm_connector_cleanup(connector
);
550 static enum drm_connector_status
551 vbox_connector_detect(struct drm_connector
*connector
, bool force
)
553 struct vbox_connector
*vbox_connector
= NULL
;
556 LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__
, connector
));
557 vbox_connector
= to_vbox_connector(connector
);
558 return vbox_connector
->mode_hint
.disconnected
?
559 connector_status_disconnected
: connector_status_connected
;
562 static int vbox_fill_modes(struct drm_connector
*connector
, uint32_t max_x
, uint32_t max_y
)
564 struct vbox_connector
*vbox_connector
;
565 struct drm_device
*dev
;
566 struct drm_display_mode
*mode
, *iterator
;
568 LogFunc(("vboxvideo: %d: connector=%p, max_x=%lu, max_y = %lu\n", __LINE__
,
569 connector
, (unsigned long)max_x
, (unsigned long)max_y
));
570 vbox_connector
= to_vbox_connector(connector
);
571 dev
= vbox_connector
->base
.dev
;
572 list_for_each_entry_safe(mode
, iterator
, &connector
->modes
, head
)
574 list_del(&mode
->head
);
575 drm_mode_destroy(dev
, mode
);
577 return drm_helper_probe_single_connector_modes(connector
, max_x
, max_y
);
580 static const struct drm_connector_helper_funcs vbox_connector_helper_funcs
= {
581 .mode_valid
= vbox_mode_valid
,
582 .get_modes
= vbox_get_modes
,
583 .best_encoder
= vbox_best_single_encoder
,
586 static const struct drm_connector_funcs vbox_connector_funcs
= {
587 .dpms
= drm_helper_connector_dpms
,
588 .detect
= vbox_connector_detect
,
589 .fill_modes
= vbox_fill_modes
,
590 .destroy
= vbox_connector_destroy
,
593 static int vbox_connector_init(struct drm_device
*dev
,
594 struct vbox_crtc
*vbox_crtc
,
595 struct drm_encoder
*encoder
)
597 struct vbox_connector
*vbox_connector
;
598 struct drm_connector
*connector
;
600 LogFunc(("vboxvideo: %d: dev=%p, encoder=%p\n", __LINE__
, dev
,
602 vbox_connector
= kzalloc(sizeof(struct vbox_connector
), GFP_KERNEL
);
606 connector
= &vbox_connector
->base
;
607 vbox_connector
->vbox_crtc
= vbox_crtc
;
609 drm_connector_init(dev
, connector
, &vbox_connector_funcs
,
610 DRM_MODE_CONNECTOR_VGA
);
611 drm_connector_helper_add(connector
, &vbox_connector_helper_funcs
);
613 connector
->interlace_allowed
= 0;
614 connector
->doublescan_allowed
= 0;
616 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
617 drm_mode_create_suggested_offset_properties(dev
);
618 drm_object_attach_property(&connector
->base
,
619 dev
->mode_config
.suggested_x_property
, 0);
620 drm_object_attach_property(&connector
->base
,
621 dev
->mode_config
.suggested_y_property
, 0);
623 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
624 drm_sysfs_connector_add(connector
);
626 drm_connector_register(connector
);
629 drm_mode_connector_attach_encoder(connector
, encoder
);
631 LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__
, connector
));
635 int vbox_mode_init(struct drm_device
*dev
)
637 struct vbox_private
*vbox
= dev
->dev_private
;
638 struct drm_encoder
*encoder
;
639 struct vbox_crtc
*vbox_crtc
;
641 /* vbox_cursor_init(dev); */
642 LogFunc(("vboxvideo: %d: dev=%p\n", __LINE__
, dev
));
643 for (i
= 0; i
< vbox
->num_crtcs
; ++i
)
645 vbox_crtc
= vbox_crtc_init(dev
, i
);
648 encoder
= vbox_encoder_init(dev
, i
);
651 vbox_connector_init(dev
, vbox_crtc
, encoder
);
656 void vbox_mode_fini(struct drm_device
*dev
)
658 /* vbox_cursor_fini(dev); */
662 /** Copy the ARGB image and generate the mask, which is needed in case the host
663 * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set
664 * if the corresponding alpha value in the ARGB image is greater than 0xF0. */
665 static void copy_cursor_image(u8
*src
, u8
*dst
, int width
, int height
,
669 size_t line_size
= (width
+ 7) / 8;
671 memcpy(dst
+ mask_size
, src
, width
* height
* 4);
672 for (i
= 0; i
< height
; ++i
)
673 for (j
= 0; j
< width
; ++j
)
674 if (((uint32_t *)src
)[i
* width
+ j
] > 0xf0000000)
675 dst
[i
* line_size
+ j
/ 8] |= (0x80 >> (j
% 8));
678 static int vbox_cursor_set2(struct drm_crtc
*crtc
, struct drm_file
*file_priv
,
679 uint32_t handle
, uint32_t width
, uint32_t height
,
680 int32_t hot_x
, int32_t hot_y
)
682 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
683 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
684 struct drm_gem_object
*obj
;
687 struct ttm_bo_kmap_obj uobj_map
;
691 size_t data_size
, mask_size
;
694 /* Re-set this regularly as in 5.0.20 and earlier the information was lost
695 * on save and restore. */
696 VBoxHGSMIUpdateInputMapping(&vbox
->submit_info
, 0, 0,
697 vbox
->input_mapping_width
,
698 vbox
->input_mapping_height
);
700 bool cursor_enabled
= false;
701 struct drm_crtc
*crtci
;
704 vbox_crtc
->cursor_enabled
= false;
705 list_for_each_entry(crtci
, &vbox
->dev
->mode_config
.crtc_list
, head
)
706 if (to_vbox_crtc(crtci
)->cursor_enabled
)
707 cursor_enabled
= true;
709 VBoxHGSMIUpdatePointerShape(&vbox
->submit_info
, 0, 0, 0, 0, 0, NULL
, 0);
712 vbox_crtc
->cursor_enabled
= true;
713 if ( width
> VBOX_MAX_CURSOR_WIDTH
|| height
> VBOX_MAX_CURSOR_HEIGHT
714 || width
== 0 || height
== 0)
716 rc
= VBoxQueryConfHGSMI(&vbox
->submit_info
,
717 VBOX_VBVA_CONF32_CURSOR_CAPABILITIES
, &caps
);
718 ret
= -RTErrConvertToErrno(rc
);
721 if ( caps
& VMMDEV_MOUSE_HOST_CANNOT_HWPOINTER
722 || !(caps
& VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE
))
725 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
726 obj
= drm_gem_object_lookup(file_priv
, handle
);
728 obj
= drm_gem_object_lookup(crtc
->dev
, file_priv
, handle
);
732 bo
= gem_to_vbox_bo(obj
);
733 ret
= vbox_bo_reserve(bo
, false);
736 /* The mask must be calculated based on the alpha channel, one bit
737 * per ARGB word, and must be 32-bit padded. */
738 mask_size
= ((width
+ 7) / 8 * height
+ 3) & ~3;
739 data_size
= width
* height
* 4 + mask_size
;
740 vbox
->cursor_hot_x
= min((uint32_t)max(hot_x
, 0), width
);
741 vbox
->cursor_hot_y
= min((uint32_t)max(hot_y
, 0), height
);
742 vbox
->cursor_width
= width
;
743 vbox
->cursor_height
= height
;
744 vbox
->cursor_data_size
= data_size
;
745 dst
= vbox
->cursor_data
;
746 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &uobj_map
);
749 src
= ttm_kmap_obj_virtual(&uobj_map
, &src_isiomem
);
752 uint32_t flags
= VBOX_MOUSE_POINTER_VISIBLE
753 | VBOX_MOUSE_POINTER_SHAPE
754 | VBOX_MOUSE_POINTER_ALPHA
;
755 copy_cursor_image(src
, dst
, width
, height
, mask_size
);
756 rc
= VBoxHGSMIUpdatePointerShape(&vbox
->submit_info
, flags
,
761 ret
= -RTErrConvertToErrno(rc
);
764 DRM_ERROR("src cursor bo should be in main memory\n");
765 ttm_bo_kunmap(&uobj_map
);
768 vbox
->cursor_data_size
= 0;
769 vbox_bo_unreserve(bo
);
771 drm_gem_object_unreference_unlocked(obj
);
775 DRM_ERROR("Cannot find cursor object %x for crtc\n", handle
);
781 static int vbox_cursor_move(struct drm_crtc
*crtc
,
784 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
785 uint32_t flags
= VBOX_MOUSE_POINTER_VISIBLE
786 | VBOX_MOUSE_POINTER_SHAPE
787 | VBOX_MOUSE_POINTER_ALPHA
;
788 uint32_t host_x
, host_y
;
793 /* We compare these to unsigned later and don't need to handle negative. */
794 if (x
+ crtc
->x
< 0 || y
+ crtc
->y
< 0 || vbox
->cursor_data_size
== 0)
796 rc
= VBoxHGSMICursorPosition(&vbox
->submit_info
, true, x
+ crtc
->x
,
797 y
+ crtc
->y
, &host_x
, &host_y
);
798 /* Work around a bug after save and restore in 5.0.20 and earlier. */
799 if (RT_FAILURE(rc
) || (host_x
== 0 && host_y
== 0))
800 return -RTErrConvertToErrno(rc
);
801 if (x
+ crtc
->x
< host_x
)
802 hot_x
= min(host_x
- x
- crtc
->x
, vbox
->cursor_width
);
803 if (y
+ crtc
->y
< host_y
)
804 hot_y
= min(host_y
- y
- crtc
->y
, vbox
->cursor_height
);
805 if (hot_x
== vbox
->cursor_hot_x
&& hot_y
== vbox
->cursor_hot_y
)
807 vbox
->cursor_hot_x
= hot_x
;
808 vbox
->cursor_hot_y
= hot_y
;
809 rc
= VBoxHGSMIUpdatePointerShape(&vbox
->submit_info
, flags
, hot_x
, hot_y
,
810 vbox
->cursor_width
, vbox
->cursor_height
,
812 vbox
->cursor_data_size
);
813 return -RTErrConvertToErrno(rc
);