VirtualBox

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

Last change on this file since 98034 was 98034, checked in by vboxsync, 23 months ago

Additions: Linux: vboxvideo: Introduce initial support for kernel 6.2, bugref:10345.

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