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 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
84 bpp
= crtc
->enabled
? CRTC_FB(crtc
)->format
->cpp
[0] * 8 : 32;
85 pitch
= crtc
->enabled
? CRTC_FB(crtc
)->pitches
[0] : width
* bpp
/ 8;
86 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
87 bpp
= crtc
->enabled
? CRTC_FB(crtc
)->bits_per_pixel
: 32;
88 pitch
= crtc
->enabled
? CRTC_FB(crtc
)->pitches
[0] : width
* bpp
/ 8;
90 bpp
= crtc
->enabled
? CRTC_FB(crtc
)->bits_per_pixel
: 32;
91 pitch
= crtc
->enabled
? CRTC_FB(crtc
)->pitch
: width
* bpp
/ 8;
93 /* This is the old way of setting graphics modes. It assumed one screen
94 * and a frame-buffer at the start of video RAM. On older versions of
95 * VirtualBox, certain parts of the code still assume that the first
96 * screen is programmed this way, so try to fake it. */
97 if ( vbox_crtc
->crtc_id
== 0
99 && vbox_crtc
->fb_offset
/ pitch
< 0xffff - crtc
->y
100 && vbox_crtc
->fb_offset
% (bpp
/ 8) == 0)
101 VBoxVideoSetModeRegisters(width
, height
, pitch
* 8 / bpp
,
102 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
103 CRTC_FB(crtc
)->format
->cpp
[0] * 8,
105 CRTC_FB(crtc
)->bits_per_pixel
,
108 vbox_crtc
->fb_offset
% pitch
/ bpp
* 8 + crtc
->x
,
109 vbox_crtc
->fb_offset
/ pitch
+ crtc
->y
);
110 flags
= VBVA_SCREEN_F_ACTIVE
;
111 flags
|= (crtc
->enabled
&& !vbox_crtc
->blanked
? 0 : VBVA_SCREEN_F_BLANK
);
112 flags
|= (vbox_crtc
->disconnected
? VBVA_SCREEN_F_DISABLED
: 0);
113 VBoxHGSMIProcessDisplayInfo(&vbox
->submit_info
, vbox_crtc
->crtc_id
,
115 crtc
->x
* bpp
/ 8 + crtc
->y
* pitch
,
116 pitch
, width
, height
,
117 vbox_crtc
->blanked
? 0 : bpp
, flags
);
118 LogFunc(("vboxvideo: %d\n", __LINE__
));
121 static int vbox_set_view(struct drm_crtc
*crtc
)
123 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
124 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
127 LogFunc(("vboxvideo: %d: vbox_crtc=%p\n", __LINE__
, vbox_crtc
));
128 /* Tell the host about the view. This design originally targeted the
129 * Windows XP driver architecture and assumed that each screen would have
130 * a dedicated frame buffer with the command buffer following it, the whole
131 * being a "view". The host works out which screen a command buffer belongs
132 * to by checking whether it is in the first view, then whether it is in the
133 * second and so on. The first match wins. We cheat around this by making
134 * the first view be the managed memory plus the first command buffer, the
135 * second the same plus the second buffer and so on. */
136 p
= VBoxHGSMIBufferAlloc(&vbox
->submit_info
, sizeof(VBVAINFOVIEW
), HGSMI_CH_VBVA
,
140 VBVAINFOVIEW
*pInfo
= (VBVAINFOVIEW
*)p
;
141 pInfo
->u32ViewIndex
= vbox_crtc
->crtc_id
;
142 pInfo
->u32ViewOffset
= vbox_crtc
->fb_offset
;
143 pInfo
->u32ViewSize
= vbox
->available_vram_size
- vbox_crtc
->fb_offset
144 + vbox_crtc
->crtc_id
* VBVA_MIN_BUFFER_SIZE
;
145 pInfo
->u32MaxScreenSize
= vbox
->available_vram_size
- vbox_crtc
->fb_offset
;
146 VBoxHGSMIBufferSubmit(&vbox
->submit_info
, p
);
147 VBoxHGSMIBufferFree(&vbox
->submit_info
, p
);
151 LogFunc(("vboxvideo: %d: p=%p\n", __LINE__
, p
));
155 static void vbox_crtc_load_lut(struct drm_crtc
*crtc
)
160 static void vbox_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
162 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
163 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
165 LogFunc(("vboxvideo: %d: vbox_crtc=%p, mode=%d\n", __LINE__
, vbox_crtc
,
168 case DRM_MODE_DPMS_ON
:
169 vbox_crtc
->blanked
= false;
171 case DRM_MODE_DPMS_STANDBY
:
172 case DRM_MODE_DPMS_SUSPEND
:
173 case DRM_MODE_DPMS_OFF
:
174 vbox_crtc
->blanked
= true;
177 mutex_lock(&vbox
->hw_mutex
);
178 vbox_do_modeset(crtc
, &crtc
->hwmode
);
179 mutex_unlock(&vbox
->hw_mutex
);
180 LogFunc(("vboxvideo: %d\n", __LINE__
));
183 static bool vbox_crtc_mode_fixup(struct drm_crtc
*crtc
,
184 const struct drm_display_mode
*mode
,
185 struct drm_display_mode
*adjusted_mode
)
190 /* We move buffers which are not in active use out of VRAM to save memory. */
191 static int vbox_crtc_do_set_base(struct drm_crtc
*crtc
,
192 struct drm_framebuffer
*fb
,
193 int x
, int y
, int atomic
)
195 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
196 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
197 struct drm_gem_object
*obj
;
198 struct vbox_framebuffer
*vbox_fb
;
203 LogFunc(("vboxvideo: %d: fb=%p, vbox_crtc=%p\n", __LINE__
, fb
, vbox_crtc
));
204 /* push the previous fb to system ram */
206 vbox_fb
= to_vbox_framebuffer(fb
);
208 bo
= gem_to_vbox_bo(obj
);
209 ret
= vbox_bo_reserve(bo
, false);
212 vbox_bo_push_sysram(bo
);
213 vbox_bo_unreserve(bo
);
216 vbox_fb
= to_vbox_framebuffer(CRTC_FB(crtc
));
218 bo
= gem_to_vbox_bo(obj
);
220 ret
= vbox_bo_reserve(bo
, false);
224 ret
= vbox_bo_pin(bo
, TTM_PL_FLAG_VRAM
, &gpu_addr
);
226 vbox_bo_unreserve(bo
);
230 if (&vbox
->fbdev
->afb
== vbox_fb
) {
231 /* if pushing console in kmap it */
232 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &bo
->kmap
);
234 DRM_ERROR("failed to kmap fbcon\n");
236 vbox_fbdev_set_base(vbox
, gpu_addr
);
238 vbox_bo_unreserve(bo
);
240 /* vbox_set_start_address_crt1(crtc, (u32)gpu_addr); */
241 vbox_crtc
->fb_offset
= gpu_addr
;
242 if (vbox_crtc
->crtc_id
== 0) {
243 vbox
->input_mapping_width
= CRTC_FB(crtc
)->width
;
244 vbox
->input_mapping_height
= CRTC_FB(crtc
)->height
;
246 LogFunc(("vboxvideo: %d: vbox_fb=%p, obj=%p, bo=%p, gpu_addr=%u\n",
247 __LINE__
, vbox_fb
, obj
, bo
, (unsigned)gpu_addr
));
251 static int vbox_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
252 struct drm_framebuffer
*old_fb
)
254 LogFunc(("vboxvideo: %d\n", __LINE__
));
255 return vbox_crtc_do_set_base(crtc
, old_fb
, x
, y
, 0);
258 static int vbox_crtc_mode_set(struct drm_crtc
*crtc
,
259 struct drm_display_mode
*mode
,
260 struct drm_display_mode
*adjusted_mode
,
262 struct drm_framebuffer
*old_fb
)
264 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
267 LogFunc(("vboxvideo: %d: vbox=%p\n", __LINE__
, vbox
));
268 vbox_crtc_mode_set_base(crtc
, x
, y
, old_fb
);
269 mutex_lock(&vbox
->hw_mutex
);
270 rc
= vbox_set_view(crtc
);
272 vbox_do_modeset(crtc
, mode
);
273 /* Note that the input mapping is always relative to the first screen. */
274 VBoxHGSMIUpdateInputMapping(&vbox
->submit_info
, 0, 0,
275 vbox
->input_mapping_width
,
276 vbox
->input_mapping_height
);
277 mutex_unlock(&vbox
->hw_mutex
);
278 LogFunc(("vboxvideo: %d\n", __LINE__
));
282 static void vbox_crtc_disable(struct drm_crtc
*crtc
)
287 static void vbox_crtc_prepare(struct drm_crtc
*crtc
)
292 static void vbox_crtc_commit(struct drm_crtc
*crtc
)
298 static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs
= {
299 .dpms
= vbox_crtc_dpms
,
300 .mode_fixup
= vbox_crtc_mode_fixup
,
301 .mode_set
= vbox_crtc_mode_set
,
302 /* .mode_set_base = vbox_crtc_mode_set_base, */
303 .disable
= vbox_crtc_disable
,
304 .load_lut
= vbox_crtc_load_lut
,
305 .prepare
= vbox_crtc_prepare
,
306 .commit
= vbox_crtc_commit
,
310 static void vbox_crtc_reset(struct drm_crtc
*crtc
)
316 static void vbox_crtc_destroy(struct drm_crtc
*crtc
)
318 drm_crtc_cleanup(crtc
);
322 static const struct drm_crtc_funcs vbox_crtc_funcs
= {
323 .cursor_move
= vbox_cursor_move
,
324 .cursor_set2
= vbox_cursor_set2
,
325 .reset
= vbox_crtc_reset
,
326 .set_config
= drm_crtc_helper_set_config
,
327 /* .gamma_set = vbox_crtc_gamma_set, */
328 .destroy
= vbox_crtc_destroy
,
331 static struct vbox_crtc
*vbox_crtc_init(struct drm_device
*dev
, unsigned i
)
333 struct vbox_crtc
*vbox_crtc
;
335 LogFunc(("vboxvideo: %d\n", __LINE__
));
336 vbox_crtc
= kzalloc(sizeof(struct vbox_crtc
), GFP_KERNEL
);
339 vbox_crtc
->crtc_id
= i
;
341 drm_crtc_init(dev
, &vbox_crtc
->base
, &vbox_crtc_funcs
);
342 drm_mode_crtc_set_gamma_size(&vbox_crtc
->base
, 256);
343 drm_crtc_helper_add(&vbox_crtc
->base
, &vbox_crtc_helper_funcs
);
344 LogFunc(("vboxvideo: %d: crtc=%p\n", __LINE__
, vbox_crtc
));
349 static void vbox_encoder_destroy(struct drm_encoder
*encoder
)
351 LogFunc(("vboxvideo: %d: encoder=%p\n", __LINE__
, encoder
));
352 drm_encoder_cleanup(encoder
);
356 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
357 static struct drm_encoder
*drm_encoder_find(struct drm_device
*dev
, uint32_t id
)
359 struct drm_mode_object
*mo
;
360 mo
= drm_mode_object_find(dev
, id
, DRM_MODE_OBJECT_ENCODER
);
361 return mo
? obj_to_encoder(mo
) : NULL
;
365 static struct drm_encoder
*vbox_best_single_encoder(struct drm_connector
*connector
)
367 int enc_id
= connector
->encoder_ids
[0];
369 LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__
, connector
));
370 /* pick the encoder ids */
372 return drm_encoder_find(connector
->dev
, enc_id
);
373 LogFunc(("vboxvideo: %d\n", __LINE__
));
378 static const struct drm_encoder_funcs vbox_enc_funcs
= {
379 .destroy
= vbox_encoder_destroy
,
382 static void vbox_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
387 static bool vbox_mode_fixup(struct drm_encoder
*encoder
,
388 const struct drm_display_mode
*mode
,
389 struct drm_display_mode
*adjusted_mode
)
394 static void vbox_encoder_mode_set(struct drm_encoder
*encoder
,
395 struct drm_display_mode
*mode
,
396 struct drm_display_mode
*adjusted_mode
)
400 static void vbox_encoder_prepare(struct drm_encoder
*encoder
)
405 static void vbox_encoder_commit(struct drm_encoder
*encoder
)
411 static const struct drm_encoder_helper_funcs vbox_enc_helper_funcs
= {
412 .dpms
= vbox_encoder_dpms
,
413 .mode_fixup
= vbox_mode_fixup
,
414 .prepare
= vbox_encoder_prepare
,
415 .commit
= vbox_encoder_commit
,
416 .mode_set
= vbox_encoder_mode_set
,
419 static struct drm_encoder
*vbox_encoder_init(struct drm_device
*dev
, unsigned i
)
421 struct vbox_encoder
*vbox_encoder
;
423 LogFunc(("vboxvideo: %d: dev=%d\n", __LINE__
));
424 vbox_encoder
= kzalloc(sizeof(struct vbox_encoder
), GFP_KERNEL
);
428 drm_encoder_init(dev
, &vbox_encoder
->base
, &vbox_enc_funcs
,
430 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
434 drm_encoder_helper_add(&vbox_encoder
->base
, &vbox_enc_helper_funcs
);
436 vbox_encoder
->base
.possible_crtcs
= 1 << i
;
437 LogFunc(("vboxvideo: %d: vbox_encoder=%p\n", __LINE__
, vbox_encoder
));
438 return &vbox_encoder
->base
;
441 /** Generate EDID data with a mode-unique serial number for the virtual
442 * monitor to try to persuade Unity that different modes correspond to
443 * different monitors and it should not try to force the same resolution on
445 static void vbox_set_edid(struct drm_connector
*connector
, int width
,
448 enum { EDID_SIZE
= 128 };
449 unsigned char edid
[EDID_SIZE
] = {
450 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
451 0x58, 0x58, /* manufacturer (VBX) */
452 0x00, 0x00, /* product code */
453 0x00, 0x00,0x00, 0x00, /* serial number goes here */
454 0x01, /* week of manufacture */
455 0x00, /* year of manufacture */
456 0x01, 0x03, /* EDID version */
457 0x80, /* capabilities - digital */
458 0x00, /* horiz. res in cm, zero for projectors */
459 0x00, /* vert. res in cm */
460 0x78, /* display gamma (120 == 2.2). */
461 0xEE, /* features (standby, suspend, off, RGB, standard colour space,
462 * preferred timing mode) */
463 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
464 /* chromaticity for standard colour space. */
465 0x00, 0x00, 0x00, /* no default timings */
466 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
467 0x01, 0x01, 0x01, 0x01, /* no standard timings */
468 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02, 0x02, 0x02,
469 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* descriptor block 1 goes here */
470 0x00, 0x00, 0x00, 0xFD, 0x00, /* descriptor block 2, monitor ranges */
471 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
472 0x20, /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
473 0x00, 0x00, 0x00, 0xFC, 0x00, /* descriptor block 3, monitor name */
474 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r', '\n',
475 0x00, 0x00, 0x00, 0x10, 0x00, /* descriptor block 4: dummy data */
476 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
478 0x00, /* number of extensions */
479 0x00 /* checksum goes here */
481 int clock
= (width
+ 6) * (height
+ 6) * 60 / 10000;
485 edid
[12] = width
& 0xff;
486 edid
[13] = width
>> 8;
487 edid
[14] = height
& 0xff;
488 edid
[15] = height
>> 8;
489 edid
[54] = clock
& 0xff;
490 edid
[55] = clock
>> 8;
491 edid
[56] = width
& 0xff;
492 edid
[58] = (width
>> 4) & 0xf0;
493 edid
[59] = height
& 0xff;
494 edid
[61] = (height
>> 4) & 0xf0;
495 for (i
= 0; i
< EDID_SIZE
- 1; ++i
)
497 edid
[EDID_SIZE
- 1] = (0x100 - (sum
& 0xFF)) & 0xFF;
498 drm_mode_connector_update_edid_property(connector
, (struct edid
*)edid
);
501 static int vbox_get_modes(struct drm_connector
*connector
)
503 struct vbox_connector
*vbox_connector
= NULL
;
504 struct drm_display_mode
*mode
= NULL
;
505 struct vbox_private
*vbox
= NULL
;
506 unsigned num_modes
= 0;
507 int preferred_width
, preferred_height
;
509 LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__
, connector
));
510 vbox_connector
= to_vbox_connector(connector
);
511 vbox
= connector
->dev
->dev_private
;
512 /* Heuristic: we do not want to tell the host that we support dynamic
513 * resizing unless we feel confident that the user space client using
514 * the video driver can handle hot-plug events. So the first time modes
515 * are queried after a "master" switch we tell the host that we do not,
516 * and immediately after we send the client a hot-plug notification as
517 * a test to see if they will respond and query again.
518 * That is also the reason why capabilities are reported to the host at
519 * this place in the code rather than elsewhere.
520 * We need to report the flags location before reporting the IRQ
522 VBoxHGSMIReportFlagsLocation(&vbox
->submit_info
, vbox
->vram_map_start
523 + vbox
->host_flags_offset
);
524 if (vbox_connector
->vbox_crtc
->crtc_id
== 0)
525 vbox_report_caps(vbox
);
526 if (!vbox
->initial_mode_queried
) {
527 if (vbox_connector
->vbox_crtc
->crtc_id
== 0) {
528 vbox
->initial_mode_queried
= true;
529 vbox_report_hotplug(vbox
);
531 return drm_add_modes_noedid(connector
, 800, 600);
533 num_modes
= drm_add_modes_noedid(connector
, 2560, 1600);
534 preferred_width
= vbox_connector
->mode_hint
.width
? vbox_connector
->mode_hint
.width
: 1024;
535 preferred_height
= vbox_connector
->mode_hint
.height
? vbox_connector
->mode_hint
.height
: 768;
536 mode
= drm_cvt_mode(connector
->dev
, preferred_width
, preferred_height
, 60, false,
540 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
541 drm_mode_probed_add(connector
, mode
);
544 vbox_set_edid(connector
, preferred_width
, preferred_height
);
548 static int vbox_mode_valid(struct drm_connector
*connector
,
549 struct drm_display_mode
*mode
)
554 static void vbox_connector_destroy(struct drm_connector
*connector
)
556 struct vbox_connector
*vbox_connector
= NULL
;
558 LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__
, connector
));
559 vbox_connector
= to_vbox_connector(connector
);
560 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
561 drm_sysfs_connector_remove(connector
);
563 drm_connector_unregister(connector
);
565 drm_connector_cleanup(connector
);
569 static enum drm_connector_status
570 vbox_connector_detect(struct drm_connector
*connector
, bool force
)
572 struct vbox_connector
*vbox_connector
= NULL
;
575 LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__
, connector
));
576 vbox_connector
= to_vbox_connector(connector
);
577 return vbox_connector
->mode_hint
.disconnected
?
578 connector_status_disconnected
: connector_status_connected
;
581 static int vbox_fill_modes(struct drm_connector
*connector
, uint32_t max_x
, uint32_t max_y
)
583 struct vbox_connector
*vbox_connector
;
584 struct drm_device
*dev
;
585 struct drm_display_mode
*mode
, *iterator
;
587 LogFunc(("vboxvideo: %d: connector=%p, max_x=%lu, max_y = %lu\n", __LINE__
,
588 connector
, (unsigned long)max_x
, (unsigned long)max_y
));
589 vbox_connector
= to_vbox_connector(connector
);
590 dev
= vbox_connector
->base
.dev
;
591 list_for_each_entry_safe(mode
, iterator
, &connector
->modes
, head
)
593 list_del(&mode
->head
);
594 drm_mode_destroy(dev
, mode
);
596 return drm_helper_probe_single_connector_modes(connector
, max_x
, max_y
);
599 static const struct drm_connector_helper_funcs vbox_connector_helper_funcs
= {
600 .mode_valid
= vbox_mode_valid
,
601 .get_modes
= vbox_get_modes
,
602 .best_encoder
= vbox_best_single_encoder
,
605 static const struct drm_connector_funcs vbox_connector_funcs
= {
606 .dpms
= drm_helper_connector_dpms
,
607 .detect
= vbox_connector_detect
,
608 .fill_modes
= vbox_fill_modes
,
609 .destroy
= vbox_connector_destroy
,
612 static int vbox_connector_init(struct drm_device
*dev
,
613 struct vbox_crtc
*vbox_crtc
,
614 struct drm_encoder
*encoder
)
616 struct vbox_connector
*vbox_connector
;
617 struct drm_connector
*connector
;
619 LogFunc(("vboxvideo: %d: dev=%p, encoder=%p\n", __LINE__
, dev
,
621 vbox_connector
= kzalloc(sizeof(struct vbox_connector
), GFP_KERNEL
);
625 connector
= &vbox_connector
->base
;
626 vbox_connector
->vbox_crtc
= vbox_crtc
;
628 drm_connector_init(dev
, connector
, &vbox_connector_funcs
,
629 DRM_MODE_CONNECTOR_VGA
);
630 drm_connector_helper_add(connector
, &vbox_connector_helper_funcs
);
632 connector
->interlace_allowed
= 0;
633 connector
->doublescan_allowed
= 0;
635 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
636 drm_mode_create_suggested_offset_properties(dev
);
637 drm_object_attach_property(&connector
->base
,
638 dev
->mode_config
.suggested_x_property
, 0);
639 drm_object_attach_property(&connector
->base
,
640 dev
->mode_config
.suggested_y_property
, 0);
642 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
643 drm_sysfs_connector_add(connector
);
645 drm_connector_register(connector
);
648 drm_mode_connector_attach_encoder(connector
, encoder
);
650 LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__
, connector
));
654 int vbox_mode_init(struct drm_device
*dev
)
656 struct vbox_private
*vbox
= dev
->dev_private
;
657 struct drm_encoder
*encoder
;
658 struct vbox_crtc
*vbox_crtc
;
660 /* vbox_cursor_init(dev); */
661 LogFunc(("vboxvideo: %d: dev=%p\n", __LINE__
, dev
));
662 for (i
= 0; i
< vbox
->num_crtcs
; ++i
)
664 vbox_crtc
= vbox_crtc_init(dev
, i
);
667 encoder
= vbox_encoder_init(dev
, i
);
670 vbox_connector_init(dev
, vbox_crtc
, encoder
);
675 void vbox_mode_fini(struct drm_device
*dev
)
677 /* vbox_cursor_fini(dev); */
681 /** Copy the ARGB image and generate the mask, which is needed in case the host
682 * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set
683 * if the corresponding alpha value in the ARGB image is greater than 0xF0. */
684 static void copy_cursor_image(u8
*src
, u8
*dst
, int width
, int height
,
688 size_t line_size
= (width
+ 7) / 8;
690 memcpy(dst
+ mask_size
, src
, width
* height
* 4);
691 for (i
= 0; i
< height
; ++i
)
692 for (j
= 0; j
< width
; ++j
)
693 if (((uint32_t *)src
)[i
* width
+ j
] > 0xf0000000)
694 dst
[i
* line_size
+ j
/ 8] |= (0x80 >> (j
% 8));
697 static int vbox_cursor_set2(struct drm_crtc
*crtc
, struct drm_file
*file_priv
,
698 uint32_t handle
, uint32_t width
, uint32_t height
,
699 int32_t hot_x
, int32_t hot_y
)
701 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
702 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
703 struct drm_gem_object
*obj
;
706 struct ttm_bo_kmap_obj uobj_map
;
710 size_t data_size
, mask_size
;
713 /* Re-set this regularly as in 5.0.20 and earlier the information was lost
714 * on save and restore. */
715 VBoxHGSMIUpdateInputMapping(&vbox
->submit_info
, 0, 0,
716 vbox
->input_mapping_width
,
717 vbox
->input_mapping_height
);
719 bool cursor_enabled
= false;
720 struct drm_crtc
*crtci
;
723 vbox_crtc
->cursor_enabled
= false;
724 list_for_each_entry(crtci
, &vbox
->dev
->mode_config
.crtc_list
, head
)
725 if (to_vbox_crtc(crtci
)->cursor_enabled
)
726 cursor_enabled
= true;
728 VBoxHGSMIUpdatePointerShape(&vbox
->submit_info
, 0, 0, 0, 0, 0, NULL
, 0);
731 vbox_crtc
->cursor_enabled
= true;
732 if ( width
> VBOX_MAX_CURSOR_WIDTH
|| height
> VBOX_MAX_CURSOR_HEIGHT
733 || width
== 0 || height
== 0)
735 rc
= VBoxQueryConfHGSMI(&vbox
->submit_info
,
736 VBOX_VBVA_CONF32_CURSOR_CAPABILITIES
, &caps
);
737 ret
= -RTErrConvertToErrno(rc
);
740 if ( caps
& VMMDEV_MOUSE_HOST_CANNOT_HWPOINTER
741 || !(caps
& VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE
))
742 /* -EINVAL means cursor_set2() not supported, -EAGAIN means
746 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
747 obj
= drm_gem_object_lookup(file_priv
, handle
);
749 obj
= drm_gem_object_lookup(crtc
->dev
, file_priv
, handle
);
753 bo
= gem_to_vbox_bo(obj
);
754 ret
= vbox_bo_reserve(bo
, false);
757 /* The mask must be calculated based on the alpha channel, one bit
758 * per ARGB word, and must be 32-bit padded. */
759 mask_size
= ((width
+ 7) / 8 * height
+ 3) & ~3;
760 data_size
= width
* height
* 4 + mask_size
;
761 vbox
->cursor_hot_x
= min((uint32_t)max(hot_x
, 0), width
);
762 vbox
->cursor_hot_y
= min((uint32_t)max(hot_y
, 0), height
);
763 vbox
->cursor_width
= width
;
764 vbox
->cursor_height
= height
;
765 vbox
->cursor_data_size
= data_size
;
766 dst
= vbox
->cursor_data
;
767 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &uobj_map
);
770 src
= ttm_kmap_obj_virtual(&uobj_map
, &src_isiomem
);
773 uint32_t flags
= VBOX_MOUSE_POINTER_VISIBLE
774 | VBOX_MOUSE_POINTER_SHAPE
775 | VBOX_MOUSE_POINTER_ALPHA
;
776 copy_cursor_image(src
, dst
, width
, height
, mask_size
);
777 rc
= VBoxHGSMIUpdatePointerShape(&vbox
->submit_info
, flags
,
782 ret
= -RTErrConvertToErrno(rc
);
785 DRM_ERROR("src cursor bo should be in main memory\n");
786 ttm_bo_kunmap(&uobj_map
);
789 vbox
->cursor_data_size
= 0;
790 vbox_bo_unreserve(bo
);
792 drm_gem_object_unreference_unlocked(obj
);
796 DRM_ERROR("Cannot find cursor object %x for crtc\n", handle
);
802 static int vbox_cursor_move(struct drm_crtc
*crtc
,
805 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
806 uint32_t flags
= VBOX_MOUSE_POINTER_VISIBLE
807 | VBOX_MOUSE_POINTER_SHAPE
808 | VBOX_MOUSE_POINTER_ALPHA
;
809 uint32_t host_x
, host_y
;
814 /* We compare these to unsigned later and don't need to handle negative. */
815 if (x
+ crtc
->x
< 0 || y
+ crtc
->y
< 0 || vbox
->cursor_data_size
== 0)
817 rc
= VBoxHGSMICursorPosition(&vbox
->submit_info
, true, x
+ crtc
->x
,
818 y
+ crtc
->y
, &host_x
, &host_y
);
819 /* Work around a bug after save and restore in 5.0.20 and earlier. */
820 if (RT_FAILURE(rc
) || (host_x
== 0 && host_y
== 0))
821 return -RTErrConvertToErrno(rc
);
822 if (x
+ crtc
->x
< host_x
)
823 hot_x
= min(host_x
- x
- crtc
->x
, vbox
->cursor_width
);
824 if (y
+ crtc
->y
< host_y
)
825 hot_y
= min(host_y
- y
- crtc
->y
, vbox
->cursor_height
);
826 if (hot_x
== vbox
->cursor_hot_x
&& hot_y
== vbox
->cursor_hot_y
)
828 vbox
->cursor_hot_x
= hot_x
;
829 vbox
->cursor_hot_y
= hot_y
;
830 rc
= VBoxHGSMIUpdatePointerShape(&vbox
->submit_info
, flags
, hot_x
, hot_y
,
831 vbox
->cursor_width
, vbox
->cursor_height
,
833 vbox
->cursor_data_size
);
834 return -RTErrConvertToErrno(rc
);