VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/drm/vbox_main.c@ 60688

Last change on this file since 60688 was 60352, checked in by vboxsync, 9 years ago

bugref:8087: Additions/x11: support non-root X server: make sure that screens start out at a resolution of 800x600 in case the windowing system on top cannot handle hot-plug and resizing. Make sure that this does not get remembered as the next start-up resolution. Also remove support for mixing VESA and the kernel graphics driver: we no longer load the kernel driver if VESA is active.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.6 KB
Line 
1/* $Id: vbox_main.c 60352 2016-04-06 11:07:00Z 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.215389.xyz. 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_main.c
20 * with the following copyright and permission notice:
21 *
22 * Copyright 2012 Red Hat Inc.
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a
25 * copy of this software and associated documentation files (the
26 * "Software"), to deal in the Software without restriction, including
27 * without limitation the rights to use, copy, modify, merge, publish,
28 * distribute, sub license, and/or sell copies of the Software, and to
29 * permit persons to whom the Software is furnished to do so, subject to
30 * the following conditions:
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
35 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
36 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
37 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
38 * USE OR OTHER DEALINGS IN THE SOFTWARE.
39 *
40 * The above copyright notice and this permission notice (including the
41 * next paragraph) shall be included in all copies or substantial portions
42 * of the Software.
43 *
44 */
45/*
46 * Authors: Dave Airlie <airlied@redhat.com>
47 */
48#include "vbox_drv.h"
49
50#include <VBox/VBoxVideoGuest.h>
51#include <VBox/VBoxVideo.h>
52
53#include <drm/drm_fb_helper.h>
54#include <drm/drm_crtc_helper.h>
55
56static void vbox_user_framebuffer_destroy(struct drm_framebuffer *fb)
57{
58 struct vbox_framebuffer *vbox_fb = to_vbox_framebuffer(fb);
59 if (vbox_fb->obj)
60 drm_gem_object_unreference_unlocked(vbox_fb->obj);
61
62 LogFunc(("vboxvideo: %d: vbox_fb=%p, vbox_fb->obj=%p\n", __LINE__,
63 vbox_fb, vbox_fb->obj));
64 drm_framebuffer_cleanup(fb);
65 kfree(fb);
66}
67
68void vbox_enable_accel(struct vbox_private *vbox)
69{
70 unsigned i;
71 struct VBVABUFFER *vbva;
72
73 AssertLogRelReturnVoid(vbox->vbva_info != NULL);
74 for (i = 0; i < vbox->num_crtcs; ++i) {
75 if (vbox->vbva_info[i].pVBVA == NULL) {
76 LogFunc(("vboxvideo: enabling VBVA.\n"));
77 vbva = (struct VBVABUFFER *) ( ((uint8_t *)vbox->vram)
78 + vbox->vram_size
79 + i * VBVA_MIN_BUFFER_SIZE);
80 if (!VBoxVBVAEnable(&vbox->vbva_info[i], &vbox->submit_info, vbva, i))
81 AssertReleaseMsgFailed(("VBoxVBVAEnable failed - heap allocation error, very old host or driver error.\n"));
82 }
83 }
84}
85
86void vbox_disable_accel(struct vbox_private *vbox)
87{
88 unsigned i;
89
90 for (i = 0; i < vbox->num_crtcs; ++i)
91 VBoxVBVADisable(&vbox->vbva_info[i], &vbox->submit_info, i);
92}
93
94void vbox_enable_caps(struct vbox_private *vbox)
95{
96 uint32_t caps = VBVACAPS_DISABLE_CURSOR_INTEGRATION
97 | VBVACAPS_IRQ
98 | VBVACAPS_USE_VBVA_ONLY;
99 if (vbox->initial_mode_queried)
100 caps |= VBVACAPS_VIDEO_MODE_HINTS;
101 VBoxHGSMISendCapsInfo(&vbox->submit_info, caps);
102}
103
104/** Send information about dirty rectangles to VBVA. If necessary we enable
105 * VBVA first, as this is normally disabled after a change of master in case
106 * the new master does not send dirty rectangle information (is this even
107 * allowed?) */
108void vbox_framebuffer_dirty_rectangles(struct drm_framebuffer *fb,
109 struct drm_clip_rect *rects,
110 unsigned num_rects)
111{
112 struct vbox_private *vbox = fb->dev->dev_private;
113 unsigned i;
114
115 LogFunc(("vboxvideo: %d: fb=%p, num_rects=%u, vbox=%p\n", __LINE__, fb,
116 num_rects, vbox));
117 vbox_enable_accel(vbox);
118 mutex_lock(&vbox->hw_mutex);
119 for (i = 0; i < num_rects; ++i)
120 {
121 struct drm_crtc *crtc;
122 list_for_each_entry(crtc, &fb->dev->mode_config.crtc_list, head)
123 {
124 unsigned crtc_id = to_vbox_crtc(crtc)->crtc_id;
125 VBVACMDHDR cmd_hdr;
126
127 if ( CRTC_FB(crtc) != fb
128 || rects[i].x1 > crtc->x
129 + crtc->hwmode.hdisplay
130 || rects[i].y1 > crtc->y
131 + crtc->hwmode.vdisplay
132 || rects[i].x2 < crtc->x
133 || rects[i].y2 < crtc->y)
134 continue;
135 cmd_hdr.x = (int16_t)rects[i].x1;
136 cmd_hdr.y = (int16_t)rects[i].y1;
137 cmd_hdr.w = (uint16_t)rects[i].x2 - rects[i].x1;
138 cmd_hdr.h = (uint16_t)rects[i].y2 - rects[i].y1;
139 if (VBoxVBVABufferBeginUpdate(&vbox->vbva_info[crtc_id],
140 &vbox->submit_info))
141 {
142 VBoxVBVAWrite(&vbox->vbva_info[crtc_id], &vbox->submit_info, &cmd_hdr,
143 sizeof(cmd_hdr));
144 VBoxVBVABufferEndUpdate(&vbox->vbva_info[crtc_id]);
145 }
146 }
147 }
148 mutex_unlock(&vbox->hw_mutex);
149 LogFunc(("vboxvideo: %d\n", __LINE__));
150}
151
152static int vbox_user_framebuffer_dirty(struct drm_framebuffer *fb,
153 struct drm_file *file_priv,
154 unsigned flags, unsigned color,
155 struct drm_clip_rect *rects,
156 unsigned num_rects)
157{
158 LogFunc(("vboxvideo: %d, flags=%u\n", __LINE__, flags));
159 vbox_framebuffer_dirty_rectangles(fb, rects, num_rects);
160 return 0;
161}
162
163static const struct drm_framebuffer_funcs vbox_fb_funcs = {
164 .destroy = vbox_user_framebuffer_destroy,
165 .dirty = vbox_user_framebuffer_dirty,
166};
167
168
169int vbox_framebuffer_init(struct drm_device *dev,
170 struct vbox_framebuffer *vbox_fb,
171#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
172 const
173#endif
174 struct DRM_MODE_FB_CMD *mode_cmd,
175 struct drm_gem_object *obj)
176{
177 int ret;
178
179 LogFunc(("vboxvideo: %d: dev=%p, vbox_fb=%p, obj=%p\n", __LINE__, dev,
180 vbox_fb, obj));
181 drm_helper_mode_fill_fb_struct(&vbox_fb->base, mode_cmd);
182 vbox_fb->obj = obj;
183 ret = drm_framebuffer_init(dev, &vbox_fb->base, &vbox_fb_funcs);
184 if (ret) {
185 DRM_ERROR("framebuffer init failed %d\n", ret);
186 LogFunc(("vboxvideo: %d\n", __LINE__));
187 return ret;
188 }
189 LogFunc(("vboxvideo: %d\n", __LINE__));
190 return 0;
191}
192
193static struct drm_framebuffer *
194vbox_user_framebuffer_create(struct drm_device *dev,
195 struct drm_file *filp,
196#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
197 const
198#endif
199 struct drm_mode_fb_cmd2 *mode_cmd)
200{
201 struct drm_gem_object *obj;
202 struct vbox_framebuffer *vbox_fb;
203 int ret;
204
205 LogFunc(("vboxvideo: %d\n", __LINE__));
206 obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
207 if (obj == NULL)
208 return ERR_PTR(-ENOENT);
209
210 vbox_fb = kzalloc(sizeof(*vbox_fb), GFP_KERNEL);
211 if (!vbox_fb) {
212 drm_gem_object_unreference_unlocked(obj);
213 return ERR_PTR(-ENOMEM);
214 }
215
216 ret = vbox_framebuffer_init(dev, vbox_fb, mode_cmd, obj);
217 if (ret) {
218 drm_gem_object_unreference_unlocked(obj);
219 kfree(vbox_fb);
220 return ERR_PTR(ret);
221 }
222 LogFunc(("vboxvideo: %d\n", __LINE__));
223 return &vbox_fb->base;
224}
225
226static const struct drm_mode_config_funcs vbox_mode_funcs = {
227 .fb_create = vbox_user_framebuffer_create,
228};
229
230static void vbox_accel_fini(struct vbox_private *vbox)
231{
232 if (vbox->vbva_info)
233 {
234 vbox_disable_accel(vbox);
235 kfree(vbox->vbva_info);
236 vbox->vbva_info = NULL;
237 }
238}
239
240static int vbox_accel_init(struct vbox_private *vbox)
241{
242 unsigned i;
243 LogFunc(("vboxvideo: %d: vbox=%p, vbox->num_crtcs=%u, vbox->vbva_info=%p\n",
244 __LINE__, vbox, (unsigned)vbox->num_crtcs, vbox->vbva_info));
245 if (!vbox->vbva_info)
246 {
247 vbox->vbva_info = kzalloc( sizeof(struct VBVABUFFERCONTEXT)
248 * vbox->num_crtcs,
249 GFP_KERNEL);
250 if (!vbox->vbva_info)
251 return -ENOMEM;
252 }
253 /* Take a command buffer for each screen from the end of usable VRAM. */
254 vbox->vram_size -= vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE;
255 for (i = 0; i < vbox->num_crtcs; ++i)
256 VBoxVBVASetupBufferContext(&vbox->vbva_info[i],
257 vbox->vram_size + i * VBVA_MIN_BUFFER_SIZE,
258 VBVA_MIN_BUFFER_SIZE);
259 LogFunc(("vboxvideo: %d: vbox->vbva_info=%p, vbox->vram_size=%u\n",
260 __LINE__, vbox->vbva_info, (unsigned)vbox->vram_size));
261 return 0;
262}
263
264/** Allocation function for the HGSMI heap and data. */
265static DECLCALLBACK(void *) alloc_hgsmi_environ(void *environ, HGSMISIZE size)
266{
267 NOREF(environ);
268 return kmalloc(size, GFP_KERNEL);
269}
270
271
272/** Free function for the HGSMI heap and data. */
273static DECLCALLBACK(void) free_hgsmi_environ(void *environ, void *ptr)
274{
275 NOREF(environ);
276 kfree(ptr);
277}
278
279
280/** Pointers to the HGSMI heap and data manipulation functions. */
281static HGSMIENV hgsmi_environ =
282{
283 NULL,
284 alloc_hgsmi_environ,
285 free_hgsmi_environ
286};
287
288
289/** Do we support the 4.3 plus mode hint reporting interface? */
290static bool have_hgsmi_mode_hints(struct vbox_private *vbox)
291{
292 uint32_t have_hints, have_cursor;
293
294 return RT_SUCCESS(VBoxQueryConfHGSMI(&vbox->submit_info, VBOX_VBVA_CONF32_MODE_HINT_REPORTING, &have_hints))
295 && RT_SUCCESS(VBoxQueryConfHGSMI(&vbox->submit_info, VBOX_VBVA_CONF32_GUEST_CURSOR_REPORTING, &have_cursor))
296 && have_hints == VINF_SUCCESS
297 && have_cursor == VINF_SUCCESS;
298}
299
300
301/** Set up our heaps and data exchange buffers in VRAM before handing the rest
302 * to the memory manager. */
303static int vbox_hw_init(struct vbox_private *vbox)
304{
305 uint32_t base_offset, guest_heap_offset, guest_heap_size, host_flags_offset;
306 void *guest_heap;
307
308 vbox->full_vram_size = VBoxVideoGetVRAMSize();
309 vbox->any_pitch = VBoxVideoAnyWidthAllowed();
310 DRM_INFO("VRAM %08x\n", vbox->full_vram_size);
311 VBoxHGSMIGetBaseMappingInfo(vbox->full_vram_size, &base_offset, NULL,
312 &guest_heap_offset, &guest_heap_size, &host_flags_offset);
313 guest_heap = ((uint8_t *)vbox->vram) + base_offset + guest_heap_offset;
314 vbox->host_flags_offset = base_offset + host_flags_offset;
315 if (RT_FAILURE(VBoxHGSMISetupGuestContext(&vbox->submit_info, guest_heap,
316 guest_heap_size,
317 base_offset + guest_heap_offset,
318 &hgsmi_environ)))
319 return -ENOMEM;
320 /* Reduce available VRAM size to reflect the guest heap. */
321 vbox->vram_size = base_offset;
322 /* Linux drm represents monitors as a 32-bit array. */
323 vbox->num_crtcs = RT_MIN(VBoxHGSMIGetMonitorCount(&vbox->submit_info), 32);
324 if (!have_hgsmi_mode_hints(vbox))
325 return -ENOTSUPP;
326 vbox->last_mode_hints = kzalloc(sizeof(VBVAMODEHINT) * vbox->num_crtcs, GFP_KERNEL);
327 if (!vbox->last_mode_hints)
328 return -ENOMEM;
329 return vbox_accel_init(vbox);
330}
331
332static void vbox_hw_fini(struct vbox_private *vbox)
333{
334 vbox_accel_fini(vbox);
335 if (vbox->last_mode_hints)
336 kfree(vbox->last_mode_hints);
337 vbox->last_mode_hints = NULL;
338}
339
340int vbox_driver_load(struct drm_device *dev, unsigned long flags)
341{
342 struct vbox_private *vbox;
343 int ret = 0;
344
345 LogFunc(("vboxvideo: %d: dev=%p\n", __LINE__, dev));
346 if (!VBoxHGSMIIsSupported())
347 return -ENODEV;
348 vbox = kzalloc(sizeof(struct vbox_private), GFP_KERNEL);
349 if (!vbox)
350 return -ENOMEM;
351
352 dev->dev_private = vbox;
353 vbox->dev = dev;
354
355 mutex_init(&vbox->hw_mutex);
356 /* I hope this won't interfere with the memory manager. */
357 vbox->vram = pci_iomap(dev->pdev, 0, 0);
358 if (!vbox->vram) {
359 ret = -EIO;
360 goto out_free;
361 }
362
363 ret = vbox_hw_init(vbox);
364 if (ret)
365 goto out_free;
366
367 ret = vbox_mm_init(vbox);
368 if (ret)
369 goto out_free;
370
371 drm_mode_config_init(dev);
372
373 dev->mode_config.funcs = (void *)&vbox_mode_funcs;
374 dev->mode_config.min_width = 64;
375 dev->mode_config.min_height = 64;
376 dev->mode_config.preferred_depth = 24;
377 dev->mode_config.max_width = VBE_DISPI_MAX_XRES;
378 dev->mode_config.max_height = VBE_DISPI_MAX_YRES;
379
380 ret = vbox_mode_init(dev);
381 if (ret)
382 goto out_free;
383
384 ret = vbox_irq_init(vbox);
385 if (ret)
386 goto out_free;
387
388 ret = vbox_fbdev_init(dev);
389 if (ret)
390 goto out_free;
391 LogFunc(("vboxvideo: %d: vbox=%p, vbox->vram=%p, vbox->full_vram_size=%u\n",
392 __LINE__, vbox, vbox->vram, (unsigned)vbox->full_vram_size));
393 return 0;
394out_free:
395 vbox_driver_unload(dev);
396 LogFunc(("vboxvideo: %d: ret=%d\n", __LINE__, ret));
397 return ret;
398}
399
400int vbox_driver_unload(struct drm_device *dev)
401{
402 struct vbox_private *vbox = dev->dev_private;
403
404 LogFunc(("vboxvideo: %d\n", __LINE__));
405 vbox_fbdev_fini(dev);
406 vbox_irq_fini(vbox);
407 vbox_mode_fini(dev);
408 if (dev->mode_config.funcs)
409 drm_mode_config_cleanup(dev);
410
411 vbox_hw_fini(vbox);
412 vbox_mm_fini(vbox);
413 if (vbox->vram)
414 pci_iounmap(dev->pdev, vbox->vram);
415 kfree(vbox);
416 dev->dev_private = NULL;
417 LogFunc(("vboxvideo: %d\n", __LINE__));
418 return 0;
419}
420
421/** @note this is described in the DRM framework documentation. AST does not
422 * have it, but we get an oops on driver unload if it is not present. */
423void vbox_driver_lastclose(struct drm_device *dev)
424{
425 struct vbox_private *vbox = dev->dev_private;
426
427#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
428 if (vbox->fbdev)
429 drm_fb_helper_restore_fbdev_mode_unlocked(&vbox->fbdev->helper);
430#else
431 mutex_lock(&dev->mode_config.mutex);
432 if (vbox->fbdev)
433 drm_fb_helper_restore_fbdev_mode(&vbox->fbdev->helper);
434 mutex_unlock(&dev->mode_config.mutex);
435#endif
436}
437
438int vbox_gem_create(struct drm_device *dev,
439 u32 size, bool iskernel,
440 struct drm_gem_object **obj)
441{
442 struct vbox_bo *vboxbo;
443 int ret;
444
445 LogFunc(("vboxvideo: %d: dev=%p, size=%u, iskernel=%u\n", __LINE__,
446 dev, (unsigned)size, (unsigned)iskernel));
447 *obj = NULL;
448
449 size = roundup(size, PAGE_SIZE);
450 if (size == 0)
451 return -EINVAL;
452
453 ret = vbox_bo_create(dev, size, 0, 0, &vboxbo);
454 if (ret) {
455 if (ret != -ERESTARTSYS)
456 DRM_ERROR("failed to allocate GEM object\n");
457 return ret;
458 }
459 *obj = &vboxbo->gem;
460 LogFunc(("vboxvideo: %d: obj=%p\n", __LINE__, obj));
461 return 0;
462}
463
464int vbox_dumb_create(struct drm_file *file,
465 struct drm_device *dev,
466 struct drm_mode_create_dumb *args)
467{
468 int ret;
469 struct drm_gem_object *gobj;
470 u32 handle;
471
472 LogFunc(("vboxvideo: %d: args->width=%u, args->height=%u, args->bpp=%u\n",
473 __LINE__, (unsigned)args->width, (unsigned)args->height,
474 (unsigned)args->bpp));
475 args->pitch = args->width * ((args->bpp + 7) / 8);
476 args->size = args->pitch * args->height;
477
478 ret = vbox_gem_create(dev, args->size, false,
479 &gobj);
480 if (ret)
481 return ret;
482
483 ret = drm_gem_handle_create(file, gobj, &handle);
484 drm_gem_object_unreference_unlocked(gobj);
485 if (ret)
486 return ret;
487
488 args->handle = handle;
489 LogFunc(("vboxvideo: %d: args->handle=%u\n", __LINE__,
490 (unsigned)args->handle));
491 return 0;
492}
493
494#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
495int vbox_dumb_destroy(struct drm_file *file,
496 struct drm_device *dev,
497 uint32_t handle)
498{
499 LogFunc(("vboxvideo: %d: dev=%p, handle=%u\n", __LINE__, dev,
500 (unsigned)handle));
501 return drm_gem_handle_delete(file, handle);
502}
503#endif
504
505static void vbox_bo_unref(struct vbox_bo **bo)
506{
507 struct ttm_buffer_object *tbo;
508
509 if ((*bo) == NULL)
510 return;
511
512 LogFunc(("vboxvideo: %d: bo=%p\n", __LINE__, bo));
513 tbo = &((*bo)->bo);
514 ttm_bo_unref(&tbo);
515 if (tbo == NULL)
516 *bo = NULL;
517
518}
519void vbox_gem_free_object(struct drm_gem_object *obj)
520{
521 struct vbox_bo *vbox_bo = gem_to_vbox_bo(obj);
522
523 LogFunc(("vboxvideo: %d: vbox_bo=%p\n", __LINE__, vbox_bo));
524 vbox_bo_unref(&vbox_bo);
525}
526
527
528static inline u64 vbox_bo_mmap_offset(struct vbox_bo *bo)
529{
530#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
531 return bo->bo.addr_space_offset;
532#else
533 return drm_vma_node_offset_addr(&bo->bo.vma_node);
534#endif
535}
536int
537vbox_dumb_mmap_offset(struct drm_file *file,
538 struct drm_device *dev,
539 uint32_t handle,
540 uint64_t *offset)
541{
542 struct drm_gem_object *obj;
543 int ret;
544 struct vbox_bo *bo;
545
546 LogFunc(("vboxvideo: %d: dev=%p, handle=%u\n", __LINE__,
547 dev, (unsigned)handle));
548 mutex_lock(&dev->struct_mutex);
549 obj = drm_gem_object_lookup(dev, file, handle);
550 if (obj == NULL) {
551 ret = -ENOENT;
552 goto out_unlock;
553 }
554
555 bo = gem_to_vbox_bo(obj);
556 *offset = vbox_bo_mmap_offset(bo);
557
558 drm_gem_object_unreference(obj);
559 ret = 0;
560 LogFunc(("vboxvideo: %d: bo=%p, *offset=%llu\n", __LINE__,
561 bo, (unsigned long long)*offset));
562out_unlock:
563 mutex_unlock(&dev->struct_mutex);
564 return ret;
565
566}
Note: See TracBrowser for help on using the repository browser.

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