VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/drm/vbox_mode.c@ 64400

Last change on this file since 64400 was 64400, checked in by vboxsync, 8 years ago

bugref:8614: Additions/common/VBoxVideo: make the code more self-contained: remove dependency on -RTErrConvertToErrno() in Linux vboxvideo.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette