VirtualBox

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

最後變更 在這個檔案從75837是 75402,由 vboxsync 提交於 6 年 前

Addiitons/linux/vboxvideo: make vboxvideo work with EL7.6 standard kernel.
bugref:4567: Linux kernel driver maintenance.
Contributed by Robert Conde. Many thanks!

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette