VirtualBox

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

最後變更 在這個檔案從72634是 71947,由 vboxsync 提交於 7 年 前

Additions/linux/drm: update drm driver to work with EL7.5, standard kernel.
No bugref.

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

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