VirtualBox

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

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

Additions/linux/vboxvideo: Pass a new framebuffer to vbox_crtc_do_set_base
bugref:8282: Additions/linux: track kernel changes to vboxvideo in our own tree

This modifies vbox_crtc_do_set_base() to take a new framebuffer to
be activated, instead of the existing framebuffer attached to the crtc.
This change allows the function to be given the new framebuffer from
a page-flip request.


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

Adjusted by the VirtualBox team.

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