VirtualBox

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

Last change on this file since 67175 was 67175, checked in by vboxsync, 8 years ago

bugref:8524: Additions/linux: play nicely with distribution-installed Additions
[PATCH 2/3] additions/linux/drm: Remove unnecessary wrapping of guest_pool in a struct
Signed-off-by: Hans de Goede <hdegoede@…>

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