VirtualBox

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

Last change on this file since 88212 was 87092, checked in by vboxsync, 4 years ago

Additions/linux/drm: Adjustment for Linux 5.10.

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