VirtualBox

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

Last change on this file was 107717, checked in by vboxsync, 5 weeks ago

Additions: Linux: vboxvideo: Restored .gamma_set callback which was removed in r88784 (build fix 2), bugref:4567.

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