1 | /* $Id: vbox_mode.c 59914 2016-03-03 20:22:29Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Additions Linux kernel video driver
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | * --------------------------------------------------------------------
|
---|
17 | *
|
---|
18 | * This code is based on
|
---|
19 | * ast_mode.c
|
---|
20 | * with the following copyright and permission notice:
|
---|
21 | *
|
---|
22 | * Copyright 2012 Red Hat Inc.
|
---|
23 | * Parts based on xf86-video-ast
|
---|
24 | * Copyright (c) 2005 ASPEED Technology Inc.
|
---|
25 | *
|
---|
26 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
27 | * copy of this software and associated documentation files (the
|
---|
28 | * "Software"), to deal in the Software without restriction, including
|
---|
29 | * without limitation the rights to use, copy, modify, merge, publish,
|
---|
30 | * distribute, sub license, and/or sell copies of the Software, and to
|
---|
31 | * permit persons to whom the Software is furnished to do so, subject to
|
---|
32 | * the following conditions:
|
---|
33 | *
|
---|
34 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
35 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
36 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
---|
37 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
---|
38 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
---|
39 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
---|
40 | * USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
41 | *
|
---|
42 | * The above copyright notice and this permission notice (including the
|
---|
43 | * next paragraph) shall be included in all copies or substantial portions
|
---|
44 | * of the Software.
|
---|
45 | *
|
---|
46 | */
|
---|
47 | /*
|
---|
48 | * Authors: Dave Airlie <[email protected]>
|
---|
49 | */
|
---|
50 | #include "vbox_drv.h"
|
---|
51 |
|
---|
52 | #include <VBox/VBoxVideo.h>
|
---|
53 |
|
---|
54 | #include <linux/export.h>
|
---|
55 | #include <drm/drm_crtc_helper.h>
|
---|
56 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
|
---|
57 | # include <drm/drm_plane_helper.h>
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | static int vbox_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv,
|
---|
61 | uint32_t handle, uint32_t width, uint32_t height,
|
---|
62 | int32_t hot_x, int32_t hot_y);
|
---|
63 | static int vbox_cursor_move(struct drm_crtc *crtc, int x, int y);
|
---|
64 |
|
---|
65 | /** Set a graphics mode. Poke any required values into registers, do an HGSMI
|
---|
66 | * mode set and tell the host we support advanced graphics functions.
|
---|
67 | */
|
---|
68 | static void vbox_do_modeset(struct drm_crtc *crtc,
|
---|
69 | const struct drm_display_mode *mode)
|
---|
70 | {
|
---|
71 | struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
|
---|
72 | struct vbox_private *vbox;
|
---|
73 | int width, height, bpp, pitch;
|
---|
74 | unsigned crtc_id;
|
---|
75 | uint16_t flags;
|
---|
76 |
|
---|
77 | LogFunc(("vboxvideo: %d: vbox_crtc=%p, CRTC_FB(crtc)=%p\n", __LINE__,
|
---|
78 | vbox_crtc, CRTC_FB(crtc)));
|
---|
79 | vbox = crtc->dev->dev_private;
|
---|
80 | width = mode->hdisplay ? mode->hdisplay : 640;
|
---|
81 | height = mode->vdisplay ? mode->vdisplay : 480;
|
---|
82 | crtc_id = vbox_crtc->crtc_id;
|
---|
83 | bpp = crtc->enabled ? CRTC_FB(crtc)->bits_per_pixel : 32;
|
---|
84 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
|
---|
85 | pitch = crtc->enabled ? CRTC_FB(crtc)->pitch : width * bpp / 8;
|
---|
86 | #else
|
---|
87 | pitch = crtc->enabled ? CRTC_FB(crtc)->pitches[0] : width * bpp / 8;
|
---|
88 | #endif
|
---|
89 | /* if (vbox_crtc->crtc_id == 0 && crtc->enabled)
|
---|
90 | VBoxVideoSetModeRegisters(width, height, pitch * 8 / bpp,
|
---|
91 | CRTC_FB(crtc)->bits_per_pixel, 0,
|
---|
92 | crtc->x, crtc->y); */
|
---|
93 | flags = VBVA_SCREEN_F_ACTIVE;
|
---|
94 | flags |= (crtc->enabled ? 0 : VBVA_SCREEN_F_DISABLED);
|
---|
95 | VBoxHGSMIProcessDisplayInfo(&vbox->submit_info, vbox_crtc->crtc_id,
|
---|
96 | crtc->x, crtc->y,
|
---|
97 | crtc->x * bpp / 8 + crtc->y * pitch,
|
---|
98 | pitch, width, height,
|
---|
99 | vbox_crtc->blanked ? 0 : bpp, flags);
|
---|
100 | VBoxHGSMIReportFlagsLocation(&vbox->submit_info, vbox->host_flags_offset);
|
---|
101 | vbox_enable_caps(vbox);
|
---|
102 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
103 | }
|
---|
104 |
|
---|
105 | static int vbox_set_view(struct drm_crtc *crtc)
|
---|
106 | {
|
---|
107 | struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
|
---|
108 | struct vbox_private *vbox = crtc->dev->dev_private;
|
---|
109 | void *p;
|
---|
110 |
|
---|
111 | LogFunc(("vboxvideo: %d: vbox_crtc=%p\n", __LINE__, vbox_crtc));
|
---|
112 | /* Tell the host about the view. This design originally targeted the
|
---|
113 | * Windows XP driver architecture and assumed that each screen would have
|
---|
114 | * a dedicated frame buffer with the command buffer following it, the whole
|
---|
115 | * being a "view". The host works out which screen a command buffer belongs
|
---|
116 | * to by checking whether it is in the first view, then whether it is in the
|
---|
117 | * second and so on. The first match wins. We cheat around this by making
|
---|
118 | * the first view be the managed memory plus the first command buffer, the
|
---|
119 | * second the same plus the second buffer and so on. */
|
---|
120 | p = VBoxHGSMIBufferAlloc(&vbox->submit_info, sizeof(VBVAINFOVIEW), HGSMI_CH_VBVA,
|
---|
121 | VBVA_INFO_VIEW);
|
---|
122 | if (p)
|
---|
123 | {
|
---|
124 | VBVAINFOVIEW *pInfo = (VBVAINFOVIEW *)p;
|
---|
125 | pInfo->u32ViewIndex = vbox_crtc->crtc_id;
|
---|
126 | pInfo->u32ViewOffset = vbox_crtc->fb_offset;
|
---|
127 | pInfo->u32ViewSize = vbox->vram_size - vbox_crtc->fb_offset
|
---|
128 | + vbox_crtc->crtc_id * VBVA_MIN_BUFFER_SIZE;
|
---|
129 | pInfo->u32MaxScreenSize = vbox->vram_size - vbox_crtc->fb_offset;
|
---|
130 | VBoxHGSMIBufferSubmit(&vbox->submit_info, p);
|
---|
131 | VBoxHGSMIBufferFree(&vbox->submit_info, p);
|
---|
132 | }
|
---|
133 | else
|
---|
134 | return -ENOMEM;
|
---|
135 | LogFunc(("vboxvideo: %d: p=%p\n", __LINE__, p));
|
---|
136 | return 0;
|
---|
137 | }
|
---|
138 |
|
---|
139 | static void vbox_crtc_load_lut(struct drm_crtc *crtc)
|
---|
140 | {
|
---|
141 |
|
---|
142 | }
|
---|
143 |
|
---|
144 | static void vbox_crtc_dpms(struct drm_crtc *crtc, int mode)
|
---|
145 | {
|
---|
146 | struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
|
---|
147 | struct vbox_private *vbox = crtc->dev->dev_private;
|
---|
148 | unsigned long flags;
|
---|
149 |
|
---|
150 | LogFunc(("vboxvideo: %d: vbox_crtc=%p, mode=%d\n", __LINE__, vbox_crtc,
|
---|
151 | mode));
|
---|
152 | switch (mode) {
|
---|
153 | case DRM_MODE_DPMS_ON:
|
---|
154 | vbox_crtc->blanked = false;
|
---|
155 | break;
|
---|
156 | case DRM_MODE_DPMS_STANDBY:
|
---|
157 | case DRM_MODE_DPMS_SUSPEND:
|
---|
158 | case DRM_MODE_DPMS_OFF:
|
---|
159 | vbox_crtc->blanked = true;
|
---|
160 | break;
|
---|
161 | }
|
---|
162 | spin_lock_irqsave(&vbox->dev_lock, flags);
|
---|
163 | vbox_do_modeset(crtc, &crtc->hwmode);
|
---|
164 | spin_unlock_irqrestore(&vbox->dev_lock, flags);
|
---|
165 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
166 | }
|
---|
167 |
|
---|
168 | static bool vbox_crtc_mode_fixup(struct drm_crtc *crtc,
|
---|
169 | const struct drm_display_mode *mode,
|
---|
170 | struct drm_display_mode *adjusted_mode)
|
---|
171 | {
|
---|
172 | return true;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /* We move buffers which are not in active use out of VRAM to save memory. */
|
---|
176 | static int vbox_crtc_do_set_base(struct drm_crtc *crtc,
|
---|
177 | struct drm_framebuffer *fb,
|
---|
178 | int x, int y, int atomic)
|
---|
179 | {
|
---|
180 | struct vbox_private *vbox = crtc->dev->dev_private;
|
---|
181 | struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
|
---|
182 | struct drm_gem_object *obj;
|
---|
183 | struct vbox_framebuffer *vbox_fb;
|
---|
184 | struct vbox_bo *bo;
|
---|
185 | int ret;
|
---|
186 | u64 gpu_addr;
|
---|
187 |
|
---|
188 | LogFunc(("vboxvideo: %d: fb=%p, vbox_crtc=%p\n", __LINE__, fb, vbox_crtc));
|
---|
189 | /* push the previous fb to system ram */
|
---|
190 | if (!atomic && fb) {
|
---|
191 | vbox_fb = to_vbox_framebuffer(fb);
|
---|
192 | obj = vbox_fb->obj;
|
---|
193 | bo = gem_to_vbox_bo(obj);
|
---|
194 | ret = vbox_bo_reserve(bo, false);
|
---|
195 | if (ret)
|
---|
196 | return ret;
|
---|
197 | vbox_bo_push_sysram(bo);
|
---|
198 | vbox_bo_unreserve(bo);
|
---|
199 | }
|
---|
200 |
|
---|
201 | vbox_fb = to_vbox_framebuffer(CRTC_FB(crtc));
|
---|
202 | obj = vbox_fb->obj;
|
---|
203 | bo = gem_to_vbox_bo(obj);
|
---|
204 |
|
---|
205 | ret = vbox_bo_reserve(bo, false);
|
---|
206 | if (ret)
|
---|
207 | return ret;
|
---|
208 |
|
---|
209 | ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
|
---|
210 | if (ret) {
|
---|
211 | vbox_bo_unreserve(bo);
|
---|
212 | return ret;
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (&vbox->fbdev->afb == vbox_fb) {
|
---|
216 | /* if pushing console in kmap it */
|
---|
217 | ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
|
---|
218 | if (ret)
|
---|
219 | DRM_ERROR("failed to kmap fbcon\n");
|
---|
220 | vbox_disable_accel(vbox);
|
---|
221 | vbox_disable_caps(vbox);
|
---|
222 | }
|
---|
223 | else {
|
---|
224 | vbox_enable_accel(vbox);
|
---|
225 | vbox_enable_caps(vbox);
|
---|
226 | }
|
---|
227 | vbox_bo_unreserve(bo);
|
---|
228 |
|
---|
229 | /* vbox_set_start_address_crt1(crtc, (u32)gpu_addr); */
|
---|
230 | vbox_crtc->fb_offset = gpu_addr;
|
---|
231 |
|
---|
232 | LogFunc(("vboxvideo: %d: vbox_fb=%p, obj=%p, bo=%p, gpu_addr=%u\n",
|
---|
233 | __LINE__, vbox_fb, obj, bo, (unsigned)gpu_addr));
|
---|
234 | return 0;
|
---|
235 | }
|
---|
236 |
|
---|
237 | static int vbox_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
|
---|
238 | struct drm_framebuffer *old_fb)
|
---|
239 | {
|
---|
240 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
241 | return vbox_crtc_do_set_base(crtc, old_fb, x, y, 0);
|
---|
242 | }
|
---|
243 |
|
---|
244 | static int vbox_crtc_mode_set(struct drm_crtc *crtc,
|
---|
245 | struct drm_display_mode *mode,
|
---|
246 | struct drm_display_mode *adjusted_mode,
|
---|
247 | int x, int y,
|
---|
248 | struct drm_framebuffer *old_fb)
|
---|
249 | {
|
---|
250 | struct vbox_private *vbox = crtc->dev->dev_private;
|
---|
251 | unsigned long flags;
|
---|
252 | int rc = 0;
|
---|
253 |
|
---|
254 | LogFunc(("vboxvideo: %d: vbox=%p\n", __LINE__, vbox));
|
---|
255 | vbox_crtc_mode_set_base(crtc, x, y, old_fb);
|
---|
256 | spin_lock_irqsave(&vbox->dev_lock, flags);
|
---|
257 | rc = vbox_set_view(crtc);
|
---|
258 | if (!rc)
|
---|
259 | vbox_do_modeset(crtc, mode);
|
---|
260 | spin_unlock_irqrestore(&vbox->dev_lock, flags);
|
---|
261 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
262 | return rc;
|
---|
263 | }
|
---|
264 |
|
---|
265 | static void vbox_crtc_disable(struct drm_crtc *crtc)
|
---|
266 | {
|
---|
267 |
|
---|
268 | }
|
---|
269 |
|
---|
270 | static void vbox_crtc_prepare(struct drm_crtc *crtc)
|
---|
271 | {
|
---|
272 |
|
---|
273 | }
|
---|
274 |
|
---|
275 | static void vbox_crtc_commit(struct drm_crtc *crtc)
|
---|
276 | {
|
---|
277 |
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = {
|
---|
282 | .dpms = vbox_crtc_dpms,
|
---|
283 | .mode_fixup = vbox_crtc_mode_fixup,
|
---|
284 | .mode_set = vbox_crtc_mode_set,
|
---|
285 | /* .mode_set_base = vbox_crtc_mode_set_base, */
|
---|
286 | .disable = vbox_crtc_disable,
|
---|
287 | .load_lut = vbox_crtc_load_lut,
|
---|
288 | .prepare = vbox_crtc_prepare,
|
---|
289 | .commit = vbox_crtc_commit,
|
---|
290 |
|
---|
291 | };
|
---|
292 |
|
---|
293 | static void vbox_crtc_reset(struct drm_crtc *crtc)
|
---|
294 | {
|
---|
295 |
|
---|
296 | }
|
---|
297 |
|
---|
298 |
|
---|
299 | static void vbox_crtc_destroy(struct drm_crtc *crtc)
|
---|
300 | {
|
---|
301 | drm_crtc_cleanup(crtc);
|
---|
302 | kfree(crtc);
|
---|
303 | }
|
---|
304 |
|
---|
305 | static const struct drm_crtc_funcs vbox_crtc_funcs = {
|
---|
306 | .cursor_move = vbox_cursor_move,
|
---|
307 | #ifdef DRM_IOCTL_MODE_CURSOR2
|
---|
308 | .cursor_set2 = vbox_cursor_set2,
|
---|
309 | #endif
|
---|
310 | .reset = vbox_crtc_reset,
|
---|
311 | .set_config = drm_crtc_helper_set_config,
|
---|
312 | /* .gamma_set = vbox_crtc_gamma_set, */
|
---|
313 | .destroy = vbox_crtc_destroy,
|
---|
314 | };
|
---|
315 |
|
---|
316 | static int vbox_crtc_init(struct drm_device *dev, unsigned i)
|
---|
317 | {
|
---|
318 | struct vbox_crtc *crtc;
|
---|
319 |
|
---|
320 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
321 | crtc = kzalloc(sizeof(struct vbox_crtc), GFP_KERNEL);
|
---|
322 | if (!crtc)
|
---|
323 | return -ENOMEM;
|
---|
324 | crtc->crtc_id = i;
|
---|
325 |
|
---|
326 | drm_crtc_init(dev, &crtc->base, &vbox_crtc_funcs);
|
---|
327 | drm_mode_crtc_set_gamma_size(&crtc->base, 256);
|
---|
328 | drm_crtc_helper_add(&crtc->base, &vbox_crtc_helper_funcs);
|
---|
329 | LogFunc(("vboxvideo: %d: crtc=%p\n", __LINE__, crtc));
|
---|
330 |
|
---|
331 | return 0;
|
---|
332 | }
|
---|
333 |
|
---|
334 | static void vbox_encoder_destroy(struct drm_encoder *encoder)
|
---|
335 | {
|
---|
336 | LogFunc(("vboxvideo: %d: encoder=%p\n", __LINE__, encoder));
|
---|
337 | drm_encoder_cleanup(encoder);
|
---|
338 | kfree(encoder);
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | static struct drm_encoder *vbox_best_single_encoder(struct drm_connector *connector)
|
---|
343 | {
|
---|
344 | int enc_id = connector->encoder_ids[0];
|
---|
345 |
|
---|
346 | LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__, connector));
|
---|
347 | /* pick the encoder ids */
|
---|
348 | if (enc_id)
|
---|
349 | return drm_encoder_find(connector->dev, enc_id);
|
---|
350 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
351 | return NULL;
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | static const struct drm_encoder_funcs vbox_enc_funcs = {
|
---|
356 | .destroy = vbox_encoder_destroy,
|
---|
357 | };
|
---|
358 |
|
---|
359 | static void vbox_encoder_dpms(struct drm_encoder *encoder, int mode)
|
---|
360 | {
|
---|
361 |
|
---|
362 | }
|
---|
363 |
|
---|
364 | static bool vbox_mode_fixup(struct drm_encoder *encoder,
|
---|
365 | const struct drm_display_mode *mode,
|
---|
366 | struct drm_display_mode *adjusted_mode)
|
---|
367 | {
|
---|
368 | return true;
|
---|
369 | }
|
---|
370 |
|
---|
371 | static void vbox_encoder_mode_set(struct drm_encoder *encoder,
|
---|
372 | struct drm_display_mode *mode,
|
---|
373 | struct drm_display_mode *adjusted_mode)
|
---|
374 | {
|
---|
375 | }
|
---|
376 |
|
---|
377 | static void vbox_encoder_prepare(struct drm_encoder *encoder)
|
---|
378 | {
|
---|
379 |
|
---|
380 | }
|
---|
381 |
|
---|
382 | static void vbox_encoder_commit(struct drm_encoder *encoder)
|
---|
383 | {
|
---|
384 |
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | static const struct drm_encoder_helper_funcs vbox_enc_helper_funcs = {
|
---|
389 | .dpms = vbox_encoder_dpms,
|
---|
390 | .mode_fixup = vbox_mode_fixup,
|
---|
391 | .prepare = vbox_encoder_prepare,
|
---|
392 | .commit = vbox_encoder_commit,
|
---|
393 | .mode_set = vbox_encoder_mode_set,
|
---|
394 | };
|
---|
395 |
|
---|
396 | static struct drm_encoder *vbox_encoder_init(struct drm_device *dev, unsigned i)
|
---|
397 | {
|
---|
398 | struct vbox_encoder *vbox_encoder;
|
---|
399 |
|
---|
400 | LogFunc(("vboxvideo: %d: dev=%d\n", __LINE__));
|
---|
401 | vbox_encoder = kzalloc(sizeof(struct vbox_encoder), GFP_KERNEL);
|
---|
402 | if (!vbox_encoder)
|
---|
403 | return NULL;
|
---|
404 |
|
---|
405 | drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs,
|
---|
406 | DRM_MODE_ENCODER_DAC);
|
---|
407 | drm_encoder_helper_add(&vbox_encoder->base, &vbox_enc_helper_funcs);
|
---|
408 |
|
---|
409 | vbox_encoder->base.possible_crtcs = 1 << i;
|
---|
410 | LogFunc(("vboxvideo: %d: vbox_encoder=%p\n", __LINE__, vbox_encoder));
|
---|
411 | return &vbox_encoder->base;
|
---|
412 | }
|
---|
413 |
|
---|
414 | static int vbox_get_modes(struct drm_connector *connector)
|
---|
415 | {
|
---|
416 | struct vbox_connector *vbox_connector = NULL;
|
---|
417 | struct drm_display_mode *mode = NULL;
|
---|
418 | unsigned num_modes = 0;
|
---|
419 | int preferred_width, preferred_height;
|
---|
420 |
|
---|
421 | LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__, connector));
|
---|
422 | vbox_connector = to_vbox_connector(connector);
|
---|
423 | num_modes = drm_add_modes_noedid(connector, 2560, 1600);
|
---|
424 | preferred_width = vbox_connector->mode_hint.width ? vbox_connector->mode_hint.width : 1024;
|
---|
425 | preferred_height = vbox_connector->mode_hint.height ? vbox_connector->mode_hint.height : 768;
|
---|
426 | mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height, 60, false,
|
---|
427 | false, false);
|
---|
428 | if (mode)
|
---|
429 | {
|
---|
430 | mode->type |= DRM_MODE_TYPE_PREFERRED;
|
---|
431 | drm_mode_probed_add(connector, mode);
|
---|
432 | ++num_modes;
|
---|
433 | }
|
---|
434 | return num_modes;
|
---|
435 | }
|
---|
436 |
|
---|
437 | static int vbox_mode_valid(struct drm_connector *connector,
|
---|
438 | struct drm_display_mode *mode)
|
---|
439 | {
|
---|
440 | return MODE_OK;
|
---|
441 | }
|
---|
442 |
|
---|
443 | static void vbox_connector_destroy(struct drm_connector *connector)
|
---|
444 | {
|
---|
445 | struct vbox_connector *vbox_connector = NULL;
|
---|
446 |
|
---|
447 | LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__, connector));
|
---|
448 | vbox_connector = to_vbox_connector(connector);
|
---|
449 | device_remove_file(connector->dev->dev, &vbox_connector->sysfs_node);
|
---|
450 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
|
---|
451 | drm_sysfs_connector_remove(connector);
|
---|
452 | #else
|
---|
453 | drm_connector_unregister(connector);
|
---|
454 | #endif
|
---|
455 | drm_connector_cleanup(connector);
|
---|
456 | kfree(connector);
|
---|
457 | }
|
---|
458 |
|
---|
459 | static enum drm_connector_status
|
---|
460 | vbox_connector_detect(struct drm_connector *connector, bool force)
|
---|
461 | {
|
---|
462 | struct vbox_connector *vbox_connector = NULL;
|
---|
463 |
|
---|
464 | (void) force;
|
---|
465 | LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__, connector));
|
---|
466 | vbox_connector = to_vbox_connector(connector);
|
---|
467 | return !vbox_connector->mode_hint.disconnected;
|
---|
468 | }
|
---|
469 |
|
---|
470 | static int vbox_fill_modes(struct drm_connector *connector, uint32_t max_x, uint32_t max_y)
|
---|
471 | {
|
---|
472 | struct vbox_connector *vbox_connector;
|
---|
473 | struct drm_device *dev;
|
---|
474 | struct drm_display_mode *mode, *iterator;
|
---|
475 |
|
---|
476 | LogFunc(("vboxvideo: %d: connector=%p, max_x=%lu, max_y = %lu\n", __LINE__,
|
---|
477 | connector, (unsigned long)max_x, (unsigned long)max_y));
|
---|
478 | vbox_connector = to_vbox_connector(connector);
|
---|
479 | dev = vbox_connector->base.dev;
|
---|
480 | list_for_each_entry_safe(mode, iterator, &connector->modes, head)
|
---|
481 | {
|
---|
482 | list_del(&mode->head);
|
---|
483 | drm_mode_destroy(dev, mode);
|
---|
484 | }
|
---|
485 | return drm_helper_probe_single_connector_modes(connector, max_x, max_y);
|
---|
486 | }
|
---|
487 |
|
---|
488 | static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = {
|
---|
489 | .mode_valid = vbox_mode_valid,
|
---|
490 | .get_modes = vbox_get_modes,
|
---|
491 | .best_encoder = vbox_best_single_encoder,
|
---|
492 | };
|
---|
493 |
|
---|
494 | static const struct drm_connector_funcs vbox_connector_funcs = {
|
---|
495 | .dpms = drm_helper_connector_dpms,
|
---|
496 | .detect = vbox_connector_detect,
|
---|
497 | .fill_modes = vbox_fill_modes,
|
---|
498 | .destroy = vbox_connector_destroy,
|
---|
499 | };
|
---|
500 |
|
---|
501 | ssize_t vbox_connector_write_sysfs(struct device *dev,
|
---|
502 | struct device_attribute *pAttr,
|
---|
503 | const char *psz, size_t cch)
|
---|
504 | {
|
---|
505 | struct vbox_connector *vbox_connector;
|
---|
506 | struct vbox_private *vbox;
|
---|
507 |
|
---|
508 | LogFunc(("vboxvideo: %d: dev=%p, pAttr=%p, psz=%s, cch=%llu\n", __LINE__,
|
---|
509 | dev, pAttr, psz, (unsigned long long)cch));
|
---|
510 | vbox_connector = container_of(pAttr, struct vbox_connector,
|
---|
511 | sysfs_node);
|
---|
512 | vbox = vbox_connector->base.dev->dev_private;
|
---|
513 | drm_kms_helper_hotplug_event(vbox_connector->base.dev);
|
---|
514 | if (vbox->fbdev)
|
---|
515 | drm_fb_helper_hotplug_event(&vbox->fbdev->helper);
|
---|
516 | return cch;
|
---|
517 | }
|
---|
518 |
|
---|
519 | static int vbox_connector_init(struct drm_device *dev, unsigned cScreen,
|
---|
520 | struct drm_encoder *encoder)
|
---|
521 | {
|
---|
522 | struct vbox_connector *vbox_connector;
|
---|
523 | struct drm_connector *connector;
|
---|
524 | int rc;
|
---|
525 |
|
---|
526 | LogFunc(("vboxvideo: %d: dev=%p, encoder=%p\n", __LINE__, dev,
|
---|
527 | encoder));
|
---|
528 | vbox_connector = kzalloc(sizeof(struct vbox_connector), GFP_KERNEL);
|
---|
529 | if (!vbox_connector)
|
---|
530 | return -ENOMEM;
|
---|
531 |
|
---|
532 | connector = &vbox_connector->base;
|
---|
533 | vbox_connector->crtc_id = cScreen;
|
---|
534 |
|
---|
535 | /*
|
---|
536 | * Set up the sysfs file we use for getting video mode hints from user
|
---|
537 | * space.
|
---|
538 | */
|
---|
539 | snprintf(vbox_connector->name, sizeof(vbox_connector->name),
|
---|
540 | "vbox_screen_%u", cScreen);
|
---|
541 | vbox_connector->sysfs_node.attr.name = vbox_connector->name;
|
---|
542 | vbox_connector->sysfs_node.attr.mode = S_IWUSR;
|
---|
543 | vbox_connector->sysfs_node.show = NULL;
|
---|
544 | vbox_connector->sysfs_node.store = vbox_connector_write_sysfs;
|
---|
545 | rc = device_create_file(dev->dev, &vbox_connector->sysfs_node);
|
---|
546 | if (rc < 0)
|
---|
547 | {
|
---|
548 | kfree(vbox_connector);
|
---|
549 | return rc;
|
---|
550 | }
|
---|
551 | drm_connector_init(dev, connector, &vbox_connector_funcs,
|
---|
552 | DRM_MODE_CONNECTOR_VGA);
|
---|
553 | drm_connector_helper_add(connector, &vbox_connector_helper_funcs);
|
---|
554 |
|
---|
555 | connector->interlace_allowed = 0;
|
---|
556 | connector->doublescan_allowed = 0;
|
---|
557 |
|
---|
558 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
|
---|
559 | drm_mode_create_suggested_offset_properties(dev);
|
---|
560 | drm_object_attach_property(&connector->base,
|
---|
561 | dev->mode_config.suggested_x_property, 0);
|
---|
562 | drm_object_attach_property(&connector->base,
|
---|
563 | dev->mode_config.suggested_y_property, 0);
|
---|
564 | #endif
|
---|
565 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
|
---|
566 | drm_sysfs_connector_add(connector);
|
---|
567 | #else
|
---|
568 | drm_connector_register(connector);
|
---|
569 | #endif
|
---|
570 |
|
---|
571 | drm_mode_connector_attach_encoder(connector, encoder);
|
---|
572 |
|
---|
573 | LogFunc(("vboxvideo: %d: connector=%p\n", __LINE__, connector));
|
---|
574 | return 0;
|
---|
575 | }
|
---|
576 |
|
---|
577 | int vbox_mode_init(struct drm_device *dev)
|
---|
578 | {
|
---|
579 | struct vbox_private *vbox = dev->dev_private;
|
---|
580 | struct drm_encoder *encoder;
|
---|
581 | unsigned i;
|
---|
582 | /* vbox_cursor_init(dev); */
|
---|
583 | LogFunc(("vboxvideo: %d: dev=%p\n", __LINE__, dev));
|
---|
584 | for (i = 0; i < vbox->num_crtcs; ++i)
|
---|
585 | {
|
---|
586 | vbox_crtc_init(dev, i);
|
---|
587 | encoder = vbox_encoder_init(dev, i);
|
---|
588 | if (encoder)
|
---|
589 | vbox_connector_init(dev, i, encoder);
|
---|
590 | }
|
---|
591 | return 0;
|
---|
592 | }
|
---|
593 |
|
---|
594 | void vbox_mode_fini(struct drm_device *dev)
|
---|
595 | {
|
---|
596 | /* vbox_cursor_fini(dev); */
|
---|
597 | }
|
---|
598 |
|
---|
599 |
|
---|
600 | void vbox_refresh_modes(struct drm_device *dev)
|
---|
601 | {
|
---|
602 | struct vbox_private *vbox = dev->dev_private;
|
---|
603 | struct drm_crtc *crtci;
|
---|
604 | unsigned long flags;
|
---|
605 |
|
---|
606 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
607 | spin_lock_irqsave(&vbox->dev_lock, flags);
|
---|
608 | list_for_each_entry(crtci, &dev->mode_config.crtc_list, head)
|
---|
609 | vbox_do_modeset(crtci, &crtci->hwmode);
|
---|
610 | spin_unlock_irqrestore(&vbox->dev_lock, flags);
|
---|
611 | LogFunc(("vboxvideo: %d\n", __LINE__));
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | /** Copy the ARGB image and generate the mask, which is needed in case the host
|
---|
616 | * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set
|
---|
617 | * if the corresponding alpha value in the ARGB image is greater than 0xF0. */
|
---|
618 | static void copy_cursor_image(u8 *src, u8 *dst, int width, int height,
|
---|
619 | size_t mask_size)
|
---|
620 | {
|
---|
621 | unsigned i, j;
|
---|
622 | size_t line_size = (width + 7) / 8;
|
---|
623 |
|
---|
624 | memcpy(dst + mask_size, src, width * height * 4);
|
---|
625 | for (i = 0; i < height; ++i)
|
---|
626 | for (j = 0; j < width; ++j)
|
---|
627 | if (((uint32_t *)src)[i * width + j] > 0xf0000000)
|
---|
628 | dst[i * line_size + j / 8] |= (0x80 >> (j % 8));
|
---|
629 | }
|
---|
630 |
|
---|
631 | static int vbox_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv,
|
---|
632 | uint32_t handle, uint32_t width, uint32_t height,
|
---|
633 | int32_t hot_x, int32_t hot_y)
|
---|
634 | {
|
---|
635 | struct vbox_private *vbox = crtc->dev->dev_private;
|
---|
636 | struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
|
---|
637 | struct drm_gem_object *obj;
|
---|
638 | struct vbox_bo *bo;
|
---|
639 | int ret, rc;
|
---|
640 | struct ttm_bo_kmap_obj uobj_map;
|
---|
641 | u8 *src;
|
---|
642 | u8 *dst = NULL;
|
---|
643 | u32 caps = 0;
|
---|
644 | size_t data_size, mask_size;
|
---|
645 | bool src_isiomem;
|
---|
646 |
|
---|
647 | if (!handle) {
|
---|
648 | /* Hide cursor. */
|
---|
649 | VBoxHGSMIUpdatePointerShape(&vbox->submit_info, 0, 0, 0, 0, 0, NULL, 0);
|
---|
650 | return 0;
|
---|
651 | }
|
---|
652 | if ( width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT
|
---|
653 | || width == 0 || hot_x > width || height == 0 || hot_y > height)
|
---|
654 | return -EINVAL;
|
---|
655 | rc = VBoxQueryConfHGSMI(&vbox->submit_info,
|
---|
656 | VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps);
|
---|
657 | ret = -RTErrConvertToErrno(rc);
|
---|
658 | if (ret)
|
---|
659 | return ret;
|
---|
660 | if ( caps & VMMDEV_MOUSE_HOST_CANNOT_HWPOINTER
|
---|
661 | || !(caps & VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE))
|
---|
662 | return -EINVAL;
|
---|
663 |
|
---|
664 | obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
|
---|
665 | if (obj)
|
---|
666 | {
|
---|
667 | bo = gem_to_vbox_bo(obj);
|
---|
668 | ret = vbox_bo_reserve(bo, false);
|
---|
669 | if (!ret)
|
---|
670 | {
|
---|
671 | /* The mask must be calculated based on the alpha channel, one bit
|
---|
672 | * per ARGB word, and must be 32-bit padded. */
|
---|
673 | mask_size = ((width + 7) / 8 * height + 3) & ~3;
|
---|
674 | data_size = width * height * 4 + mask_size;
|
---|
675 | dst = kmalloc(data_size, GFP_KERNEL);
|
---|
676 | if (dst)
|
---|
677 | {
|
---|
678 | ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &uobj_map);
|
---|
679 | if (!ret)
|
---|
680 | {
|
---|
681 | src = ttm_kmap_obj_virtual(&uobj_map, &src_isiomem);
|
---|
682 | if (!src_isiomem)
|
---|
683 | {
|
---|
684 | uint32_t flags = VBOX_MOUSE_POINTER_VISIBLE
|
---|
685 | | VBOX_MOUSE_POINTER_SHAPE
|
---|
686 | | VBOX_MOUSE_POINTER_ALPHA;
|
---|
687 | copy_cursor_image(src, dst, width, height, mask_size);
|
---|
688 | rc = VBoxHGSMIUpdatePointerShape(&vbox->submit_info, flags,
|
---|
689 | hot_x, hot_y, width,
|
---|
690 | height, dst, data_size);
|
---|
691 | ret = -RTErrConvertToErrno(rc);
|
---|
692 | }
|
---|
693 | else
|
---|
694 | DRM_ERROR("src cursor bo should be in main memory\n");
|
---|
695 | ttm_bo_kunmap(&uobj_map);
|
---|
696 | }
|
---|
697 | kfree(dst);
|
---|
698 | }
|
---|
699 | vbox_bo_unreserve(bo);
|
---|
700 | }
|
---|
701 | drm_gem_object_unreference_unlocked(obj);
|
---|
702 | }
|
---|
703 | else
|
---|
704 | {
|
---|
705 | DRM_ERROR("Cannot find cursor object %x for crtc\n", handle);
|
---|
706 | ret = -ENOENT;
|
---|
707 | }
|
---|
708 | return ret;
|
---|
709 | }
|
---|
710 |
|
---|
711 | static int vbox_cursor_move(struct drm_crtc *crtc,
|
---|
712 | int x, int y)
|
---|
713 | {
|
---|
714 | return 0;
|
---|
715 | }
|
---|