VirtualBox

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

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

bugref:8524: Additions/linux: play nicely with distribution-installed Additions
[PATCH 2/2] Additions: linux/drm: Fix compiler warning
Fix:

drivers/staging/vboxvideo/vbox_main.c: In function ‘vbox_hw_init’:
drivers/staging/vboxvideo/vbox_main.c:278:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]

int ret;

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 67269 2017-06-06 11:55:09Z 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 int ret;
277
278 vbox->full_vram_size = VBoxVideoGetVRAMSize();
279 vbox->any_pitch = VBoxVideoAnyWidthAllowed();
280
281 DRM_INFO("VRAM %08x\n", vbox->full_vram_size);
282
283 /* Map guest-heap at end of vram */
284 vbox->guest_heap = pci_iomap_range(vbox->dev->pdev, 0, GUEST_HEAP_OFFSET(vbox),
285 GUEST_HEAP_SIZE);
286 if (!vbox->guest_heap)
287 return -ENOMEM;
288
289 /* Create guest-heap mem-pool use 2^4 = 16 byte chunks */
290 vbox->guest_pool = gen_pool_create(4, -1);
291 if (!vbox->guest_pool)
292 return -ENOMEM;
293
294 ret = gen_pool_add_virt(vbox->guest_pool,
295 (unsigned long)vbox->guest_heap,
296 GUEST_HEAP_OFFSET(vbox),
297 GUEST_HEAP_USABLE_SIZE, -1);
298 if (ret)
299 return ret;
300
301 /* Reduce available VRAM size to reflect the guest heap. */
302 vbox->available_vram_size = GUEST_HEAP_OFFSET(vbox);
303 /* Linux drm represents monitors as a 32-bit array. */
304 vbox->num_crtcs = min(VBoxHGSMIGetMonitorCount(vbox->guest_pool),
305 (uint32_t)VBOX_MAX_SCREENS);
306 if (!have_hgsmi_mode_hints(vbox))
307 return -ENOTSUPP;
308 vbox->last_mode_hints = kzalloc(sizeof(VBVAMODEHINT) * vbox->num_crtcs, GFP_KERNEL);
309 if (!vbox->last_mode_hints)
310 return -ENOMEM;
311 return vbox_accel_init(vbox);
312}
313
314static void vbox_hw_fini(struct vbox_private *vbox)
315{
316 vbox_accel_fini(vbox);
317 if (vbox->last_mode_hints)
318 kfree(vbox->last_mode_hints);
319 vbox->last_mode_hints = NULL;
320}
321
322int vbox_driver_load(struct drm_device *dev, unsigned long flags)
323{
324 struct vbox_private *vbox;
325 int ret = 0;
326
327 if (!VBoxHGSMIIsSupported())
328 return -ENODEV;
329 vbox = kzalloc(sizeof(struct vbox_private), GFP_KERNEL);
330 if (!vbox)
331 return -ENOMEM;
332
333 dev->dev_private = vbox;
334 vbox->dev = dev;
335
336 mutex_init(&vbox->hw_mutex);
337
338 ret = vbox_hw_init(vbox);
339 if (ret)
340 goto out_free;
341
342 ret = vbox_mm_init(vbox);
343 if (ret)
344 goto out_free;
345
346 drm_mode_config_init(dev);
347
348 dev->mode_config.funcs = (void *)&vbox_mode_funcs;
349 dev->mode_config.min_width = 64;
350 dev->mode_config.min_height = 64;
351 dev->mode_config.preferred_depth = 24;
352 dev->mode_config.max_width = VBE_DISPI_MAX_XRES;
353 dev->mode_config.max_height = VBE_DISPI_MAX_YRES;
354
355 ret = vbox_mode_init(dev);
356 if (ret)
357 goto out_free;
358
359 ret = vbox_irq_init(vbox);
360 if (ret)
361 goto out_free;
362
363 ret = vbox_fbdev_init(dev);
364 if (ret)
365 goto out_free;
366 return 0;
367out_free:
368 vbox_driver_unload(dev);
369 return ret;
370}
371
372#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
373void vbox_driver_unload(struct drm_device *dev)
374#else
375int vbox_driver_unload(struct drm_device *dev)
376#endif
377{
378 struct vbox_private *vbox = dev->dev_private;
379
380 vbox_fbdev_fini(dev);
381 vbox_irq_fini(vbox);
382 vbox_mode_fini(dev);
383 if (dev->mode_config.funcs)
384 drm_mode_config_cleanup(dev);
385
386 vbox_hw_fini(vbox);
387 vbox_mm_fini(vbox);
388 if (vbox->guest_pool)
389 gen_pool_destroy(vbox->guest_pool);
390 if (vbox->guest_heap)
391 pci_iounmap(dev->pdev, vbox->guest_heap);
392 kfree(vbox);
393 dev->dev_private = NULL;
394#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)
395 return 0;
396#endif
397}
398
399/** @note this is described in the DRM framework documentation. AST does not
400 * have it, but we get an oops on driver unload if it is not present. */
401void vbox_driver_lastclose(struct drm_device *dev)
402{
403 struct vbox_private *vbox = dev->dev_private;
404
405#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
406 if (vbox->fbdev)
407 drm_fb_helper_restore_fbdev_mode_unlocked(&vbox->fbdev->helper);
408#else
409 drm_modeset_lock_all(dev);
410 if (vbox->fbdev)
411 drm_fb_helper_restore_fbdev_mode(&vbox->fbdev->helper);
412 drm_modeset_unlock_all(dev);
413#endif
414}
415
416int vbox_gem_create(struct drm_device *dev,
417 u32 size, bool iskernel,
418 struct drm_gem_object **obj)
419{
420 struct vbox_bo *vboxbo;
421 int ret;
422
423 *obj = NULL;
424
425 size = roundup(size, PAGE_SIZE);
426 if (size == 0)
427 return -EINVAL;
428
429 ret = vbox_bo_create(dev, size, 0, 0, &vboxbo);
430 if (ret) {
431 if (ret != -ERESTARTSYS)
432 DRM_ERROR("failed to allocate GEM object\n");
433 return ret;
434 }
435 *obj = &vboxbo->gem;
436 return 0;
437}
438
439int vbox_dumb_create(struct drm_file *file,
440 struct drm_device *dev,
441 struct drm_mode_create_dumb *args)
442{
443 int ret;
444 struct drm_gem_object *gobj;
445 u32 handle;
446
447 args->pitch = args->width * ((args->bpp + 7) / 8);
448 args->size = args->pitch * args->height;
449
450 ret = vbox_gem_create(dev, args->size, false,
451 &gobj);
452 if (ret)
453 return ret;
454
455 ret = drm_gem_handle_create(file, gobj, &handle);
456 drm_gem_object_unreference_unlocked(gobj);
457 if (ret)
458 return ret;
459
460 args->handle = handle;
461 return 0;
462}
463
464#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
465int vbox_dumb_destroy(struct drm_file *file,
466 struct drm_device *dev,
467 uint32_t handle)
468{
469 return drm_gem_handle_delete(file, handle);
470}
471#endif
472
473static void vbox_bo_unref(struct vbox_bo **bo)
474{
475 struct ttm_buffer_object *tbo;
476
477 if ((*bo) == NULL)
478 return;
479
480 tbo = &((*bo)->bo);
481 ttm_bo_unref(&tbo);
482 if (tbo == NULL)
483 *bo = NULL;
484
485}
486void vbox_gem_free_object(struct drm_gem_object *obj)
487{
488 struct vbox_bo *vbox_bo = gem_to_vbox_bo(obj);
489
490 vbox_bo_unref(&vbox_bo);
491}
492
493
494static inline u64 vbox_bo_mmap_offset(struct vbox_bo *bo)
495{
496#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
497 return bo->bo.addr_space_offset;
498#else
499 return drm_vma_node_offset_addr(&bo->bo.vma_node);
500#endif
501}
502int
503vbox_dumb_mmap_offset(struct drm_file *file,
504 struct drm_device *dev,
505 uint32_t handle,
506 uint64_t *offset)
507{
508 struct drm_gem_object *obj;
509 int ret;
510 struct vbox_bo *bo;
511
512 mutex_lock(&dev->struct_mutex);
513#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
514 obj = drm_gem_object_lookup(file, handle);
515#else
516 obj = drm_gem_object_lookup(dev, file, handle);
517#endif
518 if (obj == NULL) {
519 ret = -ENOENT;
520 goto out_unlock;
521 }
522
523 bo = gem_to_vbox_bo(obj);
524 *offset = vbox_bo_mmap_offset(bo);
525
526 drm_gem_object_unreference(obj);
527 ret = 0;
528out_unlock:
529 mutex_unlock(&dev->struct_mutex);
530 return ret;
531
532}
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