VirtualBox

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

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

Additions/linux/vboxvideo: fix kernel module build with openSUSE 15.0. Fix the fix.

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