VirtualBox

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

Last change on this file since 79840 was 79694, checked in by vboxsync, 6 years ago

Additions/linux/vboxvideo: apply kernel 2408898e3 and 65aac1742.
bugref:8282: Additions/linux: track kernel changes to vboxvideo in our own tree
commit 2408898e3b6c99b3ec792760989e57026cd9909d
Author: Steve Longerbeam <slongerbeam@…>
Date: Fri Jul 20 10:17:31 2018 -0700

staging: vboxvideo: Add page-flip support


Adds crtc page-flip support by passing the new requested framebuffer
to vbox_crtc_do_set_base().

...

Signed-off-by: Steve Longerbeam <steve_longerbeam@…>
Signed-off-by: Greg Kroah-Hartman <gregkh@…>

commit 65aac17423284634169489f298169c3e3f099cc7
Author: Hans de Goede <hdegoede@…>
Date: Mon Sep 10 20:30:39 2018 +0200

staging: vboxvideo: Change address of scanout buffer on page-flip


Commit 2408898e3b6c ("staging: vboxvideo: Add page-flip support") only
calls vbox_crtc_do_set_base() on page-flips, but despite that function-s
name it only pins the new fb, unpins the old fb and sets
vbox_crtc->fb_offset. It does not program the hardware to scan out at the
new vbox_crtc->fb_offset value.

...

Fixes: 2408898e3b6c ("staging: vboxvideo: Add page-flip support")
Cc: Steve Longerbeam <steve_longerbeam@…>
Signed-off-by: Hans de Goede <hdegoede@…>
Signed-off-by: Greg Kroah-Hartman <gregkh@…>

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