VirtualBox

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

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

Additions/linux/vboxvideo: attempt to fix a crash in Linux Mint guests.
bugref:4567: Linux kernel module maintanance.
ticketref:18443: Kernel Oops in vboxvideo.

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