VirtualBox

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

Last change on this file since 85569 was 83073, checked in by vboxsync, 5 years ago

GA/Linux: ticketref:19145 kernel 5.5-rc1/rc2 - we need changes, portions contributed by Larry Finger

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.2 KB
Line 
1/* $Id: vbox_mode.c 83073 2020-02-14 11:35:05Z 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 LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) || defined(RHEL_72)
43#include <drm/drm_plane_helper.h>
44#endif
45#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0) || defined(RHEL_81)
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 LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) || defined(RHEL_75)
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 LINUX_VERSION_CODE >= KERNEL_VERSION(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 LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) || defined(RHEL_75)
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, TTM_PL_FLAG_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 LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) || defined(RHEL_75)
288 struct drm_pending_vblank_event *event,
289 uint32_t page_flip_flags,
290 struct drm_modeset_acquire_ctx *ctx)
291#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0) || defined(RHEL_70)
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 LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0) || defined(RHEL_72)
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 LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0) && !defined(RHEL_71)
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 LINUX_VERSION_CODE >= KERNEL_VERSION(5, 5, 0)
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 /* KERNEL_VERSION < 5.5 */
408 int enc_id = connector->encoder_ids[0];
409
410 /* pick the encoder ids */
411 if (enc_id)
412# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0) || \
413 (defined(CONFIG_SUSE_VERSION) && \
414 LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)) || \
415 defined(RHEL_76)
416 return drm_encoder_find(connector->dev, NULL, enc_id);
417# else
418 return drm_encoder_find(connector->dev, enc_id);
419# endif
420#endif /* KERNEL_VERSION < 5.5 */
421 return NULL;
422}
423
424static const struct drm_encoder_funcs vbox_enc_funcs = {
425 .destroy = vbox_encoder_destroy,
426};
427
428static void vbox_encoder_dpms(struct drm_encoder *encoder, int mode)
429{
430}
431
432static bool vbox_mode_fixup(struct drm_encoder *encoder,
433 const struct drm_display_mode *mode,
434 struct drm_display_mode *adjusted_mode)
435{
436 return true;
437}
438
439static void vbox_encoder_mode_set(struct drm_encoder *encoder,
440 struct drm_display_mode *mode,
441 struct drm_display_mode *adjusted_mode)
442{
443}
444
445static void vbox_encoder_prepare(struct drm_encoder *encoder)
446{
447}
448
449static void vbox_encoder_commit(struct drm_encoder *encoder)
450{
451}
452
453static const struct drm_encoder_helper_funcs vbox_enc_helper_funcs = {
454 .dpms = vbox_encoder_dpms,
455 .mode_fixup = vbox_mode_fixup,
456 .prepare = vbox_encoder_prepare,
457 .commit = vbox_encoder_commit,
458 .mode_set = vbox_encoder_mode_set,
459};
460
461static struct drm_encoder *vbox_encoder_init(struct drm_device *dev,
462 unsigned int i)
463{
464 struct vbox_encoder *vbox_encoder;
465
466 vbox_encoder = kzalloc(sizeof(*vbox_encoder), GFP_KERNEL);
467 if (!vbox_encoder)
468 return NULL;
469
470 drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs,
471#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) || defined(RHEL_73)
472 DRM_MODE_ENCODER_DAC, NULL);
473#else
474 DRM_MODE_ENCODER_DAC);
475#endif
476 drm_encoder_helper_add(&vbox_encoder->base, &vbox_enc_helper_funcs);
477
478 vbox_encoder->base.possible_crtcs = 1 << i;
479 return &vbox_encoder->base;
480}
481
482/**
483 * Generate EDID data with a mode-unique serial number for the virtual
484 * monitor to try to persuade Unity that different modes correspond to
485 * different monitors and it should not try to force the same resolution on
486 * them.
487 */
488static void vbox_set_edid(struct drm_connector *connector, int width,
489 int height)
490{
491 enum { EDID_SIZE = 128 };
492 unsigned char edid[EDID_SIZE] = {
493 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
494 0x58, 0x58, /* manufacturer (VBX) */
495 0x00, 0x00, /* product code */
496 0x00, 0x00, 0x00, 0x00, /* serial number goes here */
497 0x01, /* week of manufacture */
498 0x00, /* year of manufacture */
499 0x01, 0x03, /* EDID version */
500 0x80, /* capabilities - digital */
501 0x00, /* horiz. res in cm, zero for projectors */
502 0x00, /* vert. res in cm */
503 0x78, /* display gamma (120 == 2.2). */
504 0xEE, /* features (standby, suspend, off, RGB, std */
505 /* colour space, preferred timing mode) */
506 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
507 /* chromaticity for standard colour space. */
508 0x00, 0x00, 0x00, /* no default timings */
509 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
510 0x01, 0x01,
511 0x01, 0x01, 0x01, 0x01, /* no standard timings */
512 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
513 0x02, 0x02,
514 /* descriptor block 1 goes below */
515 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
516 /* descriptor block 2, monitor ranges */
517 0x00, 0x00, 0x00, 0xFD, 0x00,
518 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
519 0x20, 0x20,
520 /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
521 0x20,
522 /* descriptor block 3, monitor name */
523 0x00, 0x00, 0x00, 0xFC, 0x00,
524 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r',
525 '\n',
526 /* descriptor block 4: dummy data */
527 0x00, 0x00, 0x00, 0x10, 0x00,
528 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
529 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
530 0x20,
531 0x00, /* number of extensions */
532 0x00 /* checksum goes here */
533 };
534 int clock = (width + 6) * (height + 6) * 60 / 10000;
535 unsigned int i, sum = 0;
536
537 edid[12] = width & 0xff;
538 edid[13] = width >> 8;
539 edid[14] = height & 0xff;
540 edid[15] = height >> 8;
541 edid[54] = clock & 0xff;
542 edid[55] = clock >> 8;
543 edid[56] = width & 0xff;
544 edid[58] = (width >> 4) & 0xf0;
545 edid[59] = height & 0xff;
546 edid[61] = (height >> 4) & 0xf0;
547 for (i = 0; i < EDID_SIZE - 1; ++i)
548 sum += edid[i];
549 edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF;
550#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) || defined(OPENSUSE_151) || defined(RHEL_77) || defined(RHEL_81)
551 drm_connector_update_edid_property(connector, (struct edid *)edid);
552#else
553 drm_mode_connector_update_edid_property(connector, (struct edid *)edid);
554#endif
555}
556
557static int vbox_get_modes(struct drm_connector *connector)
558{
559 struct vbox_connector *vbox_connector = NULL;
560 struct drm_display_mode *mode = NULL;
561 struct vbox_private *vbox = NULL;
562 unsigned int num_modes = 0;
563 int preferred_width, preferred_height;
564
565 vbox_connector = to_vbox_connector(connector);
566 vbox = connector->dev->dev_private;
567 /*
568 * Heuristic: we do not want to tell the host that we support dynamic
569 * resizing unless we feel confident that the user space client using
570 * the video driver can handle hot-plug events. So the first time modes
571 * are queried after a "master" switch we tell the host that we do not,
572 * and immediately after we send the client a hot-plug notification as
573 * a test to see if they will respond and query again.
574 * That is also the reason why capabilities are reported to the host at
575 * this place in the code rather than elsewhere.
576 * We need to report the flags location before reporting the IRQ
577 * capability.
578 */
579 VBoxHGSMIReportFlagsLocation(vbox->guest_pool, GUEST_HEAP_OFFSET(vbox) +
580 HOST_FLAGS_OFFSET);
581 if (vbox_connector->vbox_crtc->crtc_id == 0)
582 vbox_report_caps(vbox);
583 if (!vbox->initial_mode_queried) {
584 if (vbox_connector->vbox_crtc->crtc_id == 0) {
585 vbox->initial_mode_queried = true;
586 vbox_report_hotplug(vbox);
587 }
588 return drm_add_modes_noedid(connector, 800, 600);
589 }
590 /* Also assume that a client which supports hot-plugging also knows
591 * how to update the screen in a way we can use, the only known
592 * relevent client which cannot is Plymouth in Ubuntu 14.04. */
593 vbox->need_refresh_timer = false;
594 num_modes = drm_add_modes_noedid(connector, 2560, 1600);
595 preferred_width = vbox_connector->mode_hint.width ?
596 vbox_connector->mode_hint.width : 1024;
597 preferred_height = vbox_connector->mode_hint.height ?
598 vbox_connector->mode_hint.height : 768;
599 mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height,
600 60, false, false, false);
601 if (mode) {
602 mode->type |= DRM_MODE_TYPE_PREFERRED;
603 drm_mode_probed_add(connector, mode);
604 ++num_modes;
605 }
606 vbox_set_edid(connector, preferred_width, preferred_height);
607
608#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0) || defined(RHEL_72)
609 if (vbox_connector->vbox_crtc->x_hint != -1)
610 drm_object_property_set_value(&connector->base,
611 vbox->dev->mode_config.suggested_x_property,
612 vbox_connector->vbox_crtc->x_hint);
613 else
614 drm_object_property_set_value(&connector->base,
615 vbox->dev->mode_config.suggested_x_property, 0);
616
617 if (vbox_connector->vbox_crtc->y_hint != -1)
618 drm_object_property_set_value(&connector->base,
619 vbox->dev->mode_config.suggested_y_property,
620 vbox_connector->vbox_crtc->y_hint);
621 else
622 drm_object_property_set_value(&connector->base,
623 vbox->dev->mode_config.suggested_y_property, 0);
624#endif
625
626 return num_modes;
627}
628
629#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) && !defined(RHEL_71)
630static int vbox_mode_valid(struct drm_connector *connector,
631#else
632static enum drm_mode_status vbox_mode_valid(struct drm_connector *connector,
633#endif
634 struct drm_display_mode *mode)
635{
636 return MODE_OK;
637}
638
639static void vbox_connector_destroy(struct drm_connector *connector)
640{
641#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0) && !defined(RHEL_72)
642 drm_sysfs_connector_remove(connector);
643#else
644 drm_connector_unregister(connector);
645#endif
646 drm_connector_cleanup(connector);
647 kfree(connector);
648}
649
650static enum drm_connector_status
651vbox_connector_detect(struct drm_connector *connector, bool force)
652{
653 struct vbox_connector *vbox_connector;
654
655 vbox_connector = to_vbox_connector(connector);
656
657 return vbox_connector->mode_hint.disconnected ?
658 connector_status_disconnected : connector_status_connected;
659}
660
661static int vbox_fill_modes(struct drm_connector *connector, u32 max_x,
662 u32 max_y)
663{
664 struct vbox_connector *vbox_connector;
665 struct drm_device *dev;
666 struct drm_display_mode *mode, *iterator;
667
668 vbox_connector = to_vbox_connector(connector);
669 dev = vbox_connector->base.dev;
670 list_for_each_entry_safe(mode, iterator, &connector->modes, head) {
671 list_del(&mode->head);
672 drm_mode_destroy(dev, mode);
673 }
674
675 return drm_helper_probe_single_connector_modes(connector, max_x, max_y);
676}
677
678static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = {
679 .mode_valid = vbox_mode_valid,
680 .get_modes = vbox_get_modes,
681 .best_encoder = vbox_best_single_encoder,
682};
683
684static const struct drm_connector_funcs vbox_connector_funcs = {
685 .dpms = drm_helper_connector_dpms,
686 .detect = vbox_connector_detect,
687 .fill_modes = vbox_fill_modes,
688 .destroy = vbox_connector_destroy,
689};
690
691static int vbox_connector_init(struct drm_device *dev,
692 struct vbox_crtc *vbox_crtc,
693 struct drm_encoder *encoder)
694{
695 struct vbox_connector *vbox_connector;
696 struct drm_connector *connector;
697
698 vbox_connector = kzalloc(sizeof(*vbox_connector), GFP_KERNEL);
699 if (!vbox_connector)
700 return -ENOMEM;
701
702 connector = &vbox_connector->base;
703 vbox_connector->vbox_crtc = vbox_crtc;
704
705 drm_connector_init(dev, connector, &vbox_connector_funcs,
706 DRM_MODE_CONNECTOR_VGA);
707 drm_connector_helper_add(connector, &vbox_connector_helper_funcs);
708
709 connector->interlace_allowed = 0;
710 connector->doublescan_allowed = 0;
711
712#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0) || defined(RHEL_72)
713 drm_mode_create_suggested_offset_properties(dev);
714 drm_object_attach_property(&connector->base,
715 dev->mode_config.suggested_x_property, 0);
716 drm_object_attach_property(&connector->base,
717 dev->mode_config.suggested_y_property, 0);
718#endif
719#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0) && !defined(RHEL_72)
720 drm_sysfs_connector_add(connector);
721#else
722 drm_connector_register(connector);
723#endif
724
725#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) || defined(OPENSUSE_151) || defined(RHEL_77) || defined(RHEL_81)
726 drm_connector_attach_encoder(connector, encoder);
727#else
728 drm_mode_connector_attach_encoder(connector, encoder);
729#endif
730
731 return 0;
732}
733
734int vbox_mode_init(struct drm_device *dev)
735{
736 struct vbox_private *vbox = dev->dev_private;
737 struct drm_encoder *encoder;
738 struct vbox_crtc *vbox_crtc;
739 unsigned int i;
740 int ret;
741
742 /* vbox_cursor_init(dev); */
743 for (i = 0; i < vbox->num_crtcs; ++i) {
744 vbox_crtc = vbox_crtc_init(dev, i);
745 if (!vbox_crtc)
746 return -ENOMEM;
747 encoder = vbox_encoder_init(dev, i);
748 if (!encoder)
749 return -ENOMEM;
750 ret = vbox_connector_init(dev, vbox_crtc, encoder);
751 if (ret)
752 return ret;
753 }
754
755 return 0;
756}
757
758void vbox_mode_fini(struct drm_device *dev)
759{
760 /* vbox_cursor_fini(dev); */
761}
762
763/**
764 * Copy the ARGB image and generate the mask, which is needed in case the host
765 * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set
766 * if the corresponding alpha value in the ARGB image is greater than 0xF0.
767 */
768static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height,
769 size_t mask_size)
770{
771 size_t line_size = (width + 7) / 8;
772 u32 i, j;
773
774 memcpy(dst + mask_size, src, width * height * 4);
775 for (i = 0; i < height; ++i)
776 for (j = 0; j < width; ++j)
777 if (((u32 *)src)[i * width + j] > 0xf0000000)
778 dst[i * line_size + j / 8] |= (0x80 >> (j % 8));
779}
780
781static int vbox_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv,
782 u32 handle, u32 width, u32 height,
783 s32 hot_x, s32 hot_y)
784{
785 struct vbox_private *vbox = crtc->dev->dev_private;
786 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
787 struct ttm_bo_kmap_obj uobj_map;
788 size_t data_size, mask_size;
789 struct drm_gem_object *obj;
790 u32 flags, caps = 0;
791 struct vbox_bo *bo;
792 bool src_isiomem;
793 u8 *dst = NULL;
794 u8 *src;
795 int ret;
796
797 if (!handle) {
798 bool cursor_enabled = false;
799 struct drm_crtc *crtci;
800
801 /* Hide cursor. */
802 vbox_crtc->cursor_enabled = false;
803 list_for_each_entry(crtci, &vbox->dev->mode_config.crtc_list,
804 head) {
805 if (to_vbox_crtc(crtci)->cursor_enabled)
806 cursor_enabled = true;
807 }
808
809 if (!cursor_enabled)
810 VBoxHGSMIUpdatePointerShape(vbox->guest_pool, 0, 0, 0,
811 0, 0, NULL, 0);
812 return 0;
813 }
814
815 vbox_crtc->cursor_enabled = true;
816
817 if (width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT ||
818 width == 0 || height == 0)
819 return -EINVAL;
820 ret = VBoxQueryConfHGSMI(vbox->guest_pool,
821 VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps);
822 if (ret)
823 return ret == VERR_NO_MEMORY ? -ENOMEM : -EINVAL;
824
825 if (!(caps & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)) {
826 /*
827 * -EINVAL means cursor_set2() not supported, -EAGAIN means
828 * retry at once.
829 */
830 return -EBUSY;
831 }
832
833#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) || defined(RHEL_74)
834 obj = drm_gem_object_lookup(file_priv, handle);
835#else
836 obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
837#endif
838 if (!obj) {
839 DRM_ERROR("Cannot find cursor object %x for crtc\n", handle);
840 return -ENOENT;
841 }
842
843 bo = gem_to_vbox_bo(obj);
844 ret = vbox_bo_reserve(bo, false);
845 if (ret)
846 goto out_unref_obj;
847
848 /*
849 * The mask must be calculated based on the alpha
850 * channel, one bit per ARGB word, and must be 32-bit
851 * padded.
852 */
853 mask_size = ((width + 7) / 8 * height + 3) & ~3;
854 data_size = width * height * 4 + mask_size;
855 vbox->cursor_hot_x = hot_x;
856 vbox->cursor_hot_y = hot_y;
857 vbox->cursor_width = width;
858 vbox->cursor_height = height;
859 vbox->cursor_data_size = data_size;
860 dst = vbox->cursor_data;
861
862 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &uobj_map);
863 if (ret) {
864 vbox->cursor_data_size = 0;
865 goto out_unreserve_bo;
866 }
867
868 src = ttm_kmap_obj_virtual(&uobj_map, &src_isiomem);
869 if (src_isiomem) {
870 DRM_ERROR("src cursor bo not in main memory\n");
871 ret = -EIO;
872 goto out_unmap_bo;
873 }
874
875 copy_cursor_image(src, dst, width, height, mask_size);
876
877 flags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE |
878 VBOX_MOUSE_POINTER_ALPHA;
879 ret = VBoxHGSMIUpdatePointerShape(vbox->guest_pool, flags,
880 vbox->cursor_hot_x, vbox->cursor_hot_y,
881 width, height, dst, data_size);
882 ret = ret == VINF_SUCCESS ? 0 : ret == VERR_NO_MEMORY ? -ENOMEM :
883 ret == VERR_NOT_SUPPORTED ? -EBUSY : -EINVAL;
884
885out_unmap_bo:
886 ttm_bo_kunmap(&uobj_map);
887out_unreserve_bo:
888 vbox_bo_unreserve(bo);
889out_unref_obj:
890 drm_gem_object_put_unlocked(obj);
891
892 return ret;
893}
894
895static int vbox_cursor_move(struct drm_crtc *crtc, int x, int y)
896{
897 struct vbox_private *vbox = crtc->dev->dev_private;
898 s32 crtc_x =
899 vbox->single_framebuffer ? crtc->x : to_vbox_crtc(crtc)->x_hint;
900 s32 crtc_y =
901 vbox->single_framebuffer ? crtc->y : to_vbox_crtc(crtc)->y_hint;
902 int ret;
903
904 x += vbox->cursor_hot_x;
905 y += vbox->cursor_hot_y;
906 if (x + crtc_x < 0 || y + crtc_y < 0 ||
907 x + crtc_x >= vbox->input_mapping_width ||
908 y + crtc_y >= vbox->input_mapping_width ||
909 vbox->cursor_data_size == 0)
910 return 0;
911 ret = VBoxHGSMICursorPosition(vbox->guest_pool, true, x + crtc_x,
912 y + crtc_y, NULL, NULL);
913 return ret == VINF_SUCCESS ? 0 : ret == VERR_NO_MEMORY ? -ENOMEM : ret ==
914 VERR_NOT_SUPPORTED ? -EBUSY : -EINVAL;
915}
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