1 | /* $Id: vbox_fb.c 69748 2017-11-18 20:56:56Z 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_fb.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 <airlied@redhat.com>
|
---|
32 | * Michael Thayer <michael.thayer@oracle.com,
|
---|
33 | */
|
---|
34 | /* Include from most specific to most general to be able to override things. */
|
---|
35 | #include "vbox_drv.h"
|
---|
36 | #include <VBoxVideo.h>
|
---|
37 |
|
---|
38 | #include <linux/module.h>
|
---|
39 | #include <linux/kernel.h>
|
---|
40 | #include <linux/errno.h>
|
---|
41 | #include <linux/string.h>
|
---|
42 | #include <linux/mm.h>
|
---|
43 | #include <linux/tty.h>
|
---|
44 | #include <linux/sysrq.h>
|
---|
45 | #include <linux/delay.h>
|
---|
46 | #include <linux/fb.h>
|
---|
47 | #include <linux/init.h>
|
---|
48 |
|
---|
49 | #include <drm/drmP.h>
|
---|
50 | #include <drm/drm_crtc.h>
|
---|
51 | #include <drm/drm_fb_helper.h>
|
---|
52 | #include <drm/drm_crtc_helper.h>
|
---|
53 | #include "vbox_drv.h"
|
---|
54 |
|
---|
55 | #define VBOX_DIRTY_DELAY (HZ / 30)
|
---|
56 | /**
|
---|
57 | * Tell the host about dirty rectangles to update.
|
---|
58 | */
|
---|
59 | static void vbox_dirty_update(struct vbox_fbdev *fbdev,
|
---|
60 | int x, int y, int width, int height)
|
---|
61 | {
|
---|
62 | struct drm_gem_object *obj;
|
---|
63 | struct vbox_bo *bo;
|
---|
64 | int ret = -EBUSY;
|
---|
65 | bool store_for_later = false;
|
---|
66 | int x2, y2;
|
---|
67 | unsigned long flags;
|
---|
68 | struct drm_clip_rect rect;
|
---|
69 |
|
---|
70 | obj = fbdev->afb.obj;
|
---|
71 | bo = gem_to_vbox_bo(obj);
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * try and reserve the BO, if we fail with busy
|
---|
75 | * then the BO is being moved and we should
|
---|
76 | * store up the damage until later.
|
---|
77 | */
|
---|
78 | if (drm_can_sleep())
|
---|
79 | ret = vbox_bo_reserve(bo, true);
|
---|
80 | if (ret) {
|
---|
81 | if (ret != -EBUSY)
|
---|
82 | return;
|
---|
83 |
|
---|
84 | store_for_later = true;
|
---|
85 | }
|
---|
86 |
|
---|
87 | x2 = x + width - 1;
|
---|
88 | y2 = y + height - 1;
|
---|
89 | spin_lock_irqsave(&fbdev->dirty_lock, flags);
|
---|
90 |
|
---|
91 | if (fbdev->y1 < y)
|
---|
92 | y = fbdev->y1;
|
---|
93 | if (fbdev->y2 > y2)
|
---|
94 | y2 = fbdev->y2;
|
---|
95 | if (fbdev->x1 < x)
|
---|
96 | x = fbdev->x1;
|
---|
97 | if (fbdev->x2 > x2)
|
---|
98 | x2 = fbdev->x2;
|
---|
99 |
|
---|
100 | if (store_for_later) {
|
---|
101 | fbdev->x1 = x;
|
---|
102 | fbdev->x2 = x2;
|
---|
103 | fbdev->y1 = y;
|
---|
104 | fbdev->y2 = y2;
|
---|
105 | spin_unlock_irqrestore(&fbdev->dirty_lock, flags);
|
---|
106 | return;
|
---|
107 | }
|
---|
108 |
|
---|
109 | fbdev->x1 = INT_MAX;
|
---|
110 | fbdev->y1 = INT_MAX;
|
---|
111 | fbdev->x2 = 0;
|
---|
112 | fbdev->y2 = 0;
|
---|
113 |
|
---|
114 | spin_unlock_irqrestore(&fbdev->dirty_lock, flags);
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Not sure why the original code subtracted 1 here, but I will keep
|
---|
118 | * it that way to avoid unnecessary differences.
|
---|
119 | */
|
---|
120 | rect.x1 = x;
|
---|
121 | rect.x2 = x2 + 1;
|
---|
122 | rect.y1 = y;
|
---|
123 | rect.y2 = y2 + 1;
|
---|
124 | vbox_framebuffer_dirty_rectangles(&fbdev->afb.base, &rect, 1);
|
---|
125 |
|
---|
126 | vbox_bo_unreserve(bo);
|
---|
127 | }
|
---|
128 |
|
---|
129 | #ifdef CONFIG_FB_DEFERRED_IO
|
---|
130 | static void vbox_deferred_io(struct fb_info *info, struct list_head *pagelist)
|
---|
131 | {
|
---|
132 | struct vbox_fbdev *fbdev = info->par;
|
---|
133 | unsigned long start, end, min, max;
|
---|
134 | struct page *page;
|
---|
135 | int y1, y2;
|
---|
136 |
|
---|
137 | min = ULONG_MAX;
|
---|
138 | max = 0;
|
---|
139 | list_for_each_entry(page, pagelist, lru) {
|
---|
140 | start = page->index << PAGE_SHIFT;
|
---|
141 | end = start + PAGE_SIZE - 1;
|
---|
142 | min = min(min, start);
|
---|
143 | max = max(max, end);
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (min < max) {
|
---|
147 | y1 = min / info->fix.line_length;
|
---|
148 | y2 = (max / info->fix.line_length) + 1;
|
---|
149 | DRM_INFO("%s: Calling dirty update: 0, %d, %d, %d\n",
|
---|
150 | __func__, y1, info->var.xres, y2 - y1 - 1);
|
---|
151 | vbox_dirty_update(fbdev, 0, y1, info->var.xres, y2 - y1 - 1);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | static struct fb_deferred_io vbox_defio = {
|
---|
156 | .delay = VBOX_DIRTY_DELAY,
|
---|
157 | .deferred_io = vbox_deferred_io,
|
---|
158 | };
|
---|
159 | #endif
|
---|
160 |
|
---|
161 | static void vbox_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
|
---|
162 | {
|
---|
163 | struct vbox_fbdev *fbdev = info->par;
|
---|
164 |
|
---|
165 | sys_fillrect(info, rect);
|
---|
166 | vbox_dirty_update(fbdev, rect->dx, rect->dy, rect->width, rect->height);
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void vbox_copyarea(struct fb_info *info, const struct fb_copyarea *area)
|
---|
170 | {
|
---|
171 | struct vbox_fbdev *fbdev = info->par;
|
---|
172 |
|
---|
173 | sys_copyarea(info, area);
|
---|
174 | vbox_dirty_update(fbdev, area->dx, area->dy, area->width, area->height);
|
---|
175 | }
|
---|
176 |
|
---|
177 | static void vbox_imageblit(struct fb_info *info, const struct fb_image *image)
|
---|
178 | {
|
---|
179 | struct vbox_fbdev *fbdev = info->par;
|
---|
180 |
|
---|
181 | sys_imageblit(info, image);
|
---|
182 | vbox_dirty_update(fbdev, image->dx, image->dy, image->width,
|
---|
183 | image->height);
|
---|
184 | }
|
---|
185 |
|
---|
186 | static struct fb_ops vboxfb_ops = {
|
---|
187 | .owner = THIS_MODULE,
|
---|
188 | .fb_check_var = drm_fb_helper_check_var,
|
---|
189 | .fb_set_par = drm_fb_helper_set_par,
|
---|
190 | .fb_fillrect = vbox_fillrect,
|
---|
191 | .fb_copyarea = vbox_copyarea,
|
---|
192 | .fb_imageblit = vbox_imageblit,
|
---|
193 | .fb_pan_display = drm_fb_helper_pan_display,
|
---|
194 | .fb_blank = drm_fb_helper_blank,
|
---|
195 | .fb_setcmap = drm_fb_helper_setcmap,
|
---|
196 | .fb_debug_enter = drm_fb_helper_debug_enter,
|
---|
197 | .fb_debug_leave = drm_fb_helper_debug_leave,
|
---|
198 | };
|
---|
199 |
|
---|
200 | static int vboxfb_create_object(struct vbox_fbdev *fbdev,
|
---|
201 | struct DRM_MODE_FB_CMD *mode_cmd,
|
---|
202 | struct drm_gem_object **gobj_p)
|
---|
203 | {
|
---|
204 | struct drm_device *dev = fbdev->helper.dev;
|
---|
205 | u32 size;
|
---|
206 | struct drm_gem_object *gobj;
|
---|
207 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
|
---|
208 | u32 pitch = mode_cmd->pitch;
|
---|
209 | #else
|
---|
210 | u32 pitch = mode_cmd->pitches[0];
|
---|
211 | #endif
|
---|
212 |
|
---|
213 | int ret = 0;
|
---|
214 |
|
---|
215 | size = pitch * mode_cmd->height;
|
---|
216 | ret = vbox_gem_create(dev, size, true, &gobj);
|
---|
217 | if (ret)
|
---|
218 | return ret;
|
---|
219 |
|
---|
220 | *gobj_p = gobj;
|
---|
221 | return ret;
|
---|
222 | }
|
---|
223 |
|
---|
224 | static int vboxfb_create(struct drm_fb_helper *helper,
|
---|
225 | struct drm_fb_helper_surface_size *sizes)
|
---|
226 | {
|
---|
227 | struct vbox_fbdev *fbdev =
|
---|
228 | container_of(helper, struct vbox_fbdev, helper);
|
---|
229 | struct drm_device *dev = fbdev->helper.dev;
|
---|
230 | struct DRM_MODE_FB_CMD mode_cmd;
|
---|
231 | struct drm_framebuffer *fb;
|
---|
232 | struct fb_info *info;
|
---|
233 | struct device *device = &dev->pdev->dev;
|
---|
234 | struct drm_gem_object *gobj = NULL;
|
---|
235 | struct vbox_bo *bo = NULL;
|
---|
236 | int size, ret;
|
---|
237 | u32 pitch;
|
---|
238 |
|
---|
239 | mode_cmd.width = sizes->surface_width;
|
---|
240 | mode_cmd.height = sizes->surface_height;
|
---|
241 | pitch = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
|
---|
242 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
|
---|
243 | mode_cmd.bpp = sizes->surface_bpp;
|
---|
244 | mode_cmd.depth = sizes->surface_depth;
|
---|
245 | mode_cmd.pitch = pitch;
|
---|
246 | #else
|
---|
247 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
|
---|
248 | sizes->surface_depth);
|
---|
249 | mode_cmd.pitches[0] = pitch;
|
---|
250 | #endif
|
---|
251 |
|
---|
252 | size = pitch * mode_cmd.height;
|
---|
253 |
|
---|
254 | ret = vboxfb_create_object(fbdev, &mode_cmd, &gobj);
|
---|
255 | if (ret) {
|
---|
256 | DRM_ERROR("failed to create fbcon backing object %d\n", ret);
|
---|
257 | return ret;
|
---|
258 | }
|
---|
259 |
|
---|
260 | ret = vbox_framebuffer_init(dev, &fbdev->afb, &mode_cmd, gobj);
|
---|
261 | if (ret)
|
---|
262 | return ret;
|
---|
263 |
|
---|
264 | bo = gem_to_vbox_bo(gobj);
|
---|
265 |
|
---|
266 | ret = vbox_bo_reserve(bo, false);
|
---|
267 | if (ret)
|
---|
268 | return ret;
|
---|
269 |
|
---|
270 | ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL);
|
---|
271 | if (ret) {
|
---|
272 | vbox_bo_unreserve(bo);
|
---|
273 | return ret;
|
---|
274 | }
|
---|
275 |
|
---|
276 | ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
|
---|
277 | vbox_bo_unreserve(bo);
|
---|
278 | if (ret) {
|
---|
279 | DRM_ERROR("failed to kmap fbcon\n");
|
---|
280 | return ret;
|
---|
281 | }
|
---|
282 |
|
---|
283 | info = framebuffer_alloc(0, device);
|
---|
284 | if (!info)
|
---|
285 | return -ENOMEM;
|
---|
286 | info->par = fbdev;
|
---|
287 |
|
---|
288 | fbdev->size = size;
|
---|
289 |
|
---|
290 | fb = &fbdev->afb.base;
|
---|
291 | fbdev->helper.fb = fb;
|
---|
292 | fbdev->helper.fbdev = info;
|
---|
293 |
|
---|
294 | strcpy(info->fix.id, "vboxdrmfb");
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * The last flag forces a mode set on VT switches even if the kernel
|
---|
298 | * does not think it is needed.
|
---|
299 | */
|
---|
300 | info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT |
|
---|
301 | FBINFO_MISC_ALWAYS_SETPAR;
|
---|
302 | info->fbops = &vboxfb_ops;
|
---|
303 |
|
---|
304 | ret = fb_alloc_cmap(&info->cmap, 256, 0);
|
---|
305 | if (ret)
|
---|
306 | return -ENOMEM;
|
---|
307 |
|
---|
308 | /*
|
---|
309 | * This seems to be done for safety checking that the framebuffer
|
---|
310 | * is not registered twice by different drivers.
|
---|
311 | */
|
---|
312 | info->apertures = alloc_apertures(1);
|
---|
313 | if (!info->apertures)
|
---|
314 | return -ENOMEM;
|
---|
315 | info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
|
---|
316 | info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
|
---|
317 | info->fix.smem_start = 0;
|
---|
318 | info->fix.smem_len = size;
|
---|
319 |
|
---|
320 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
|
---|
321 | drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
|
---|
322 | #else
|
---|
323 | drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
|
---|
324 | #endif
|
---|
325 | drm_fb_helper_fill_var(info, &fbdev->helper, sizes->fb_width,
|
---|
326 | sizes->fb_height);
|
---|
327 |
|
---|
328 | info->screen_base = bo->kmap.virtual;
|
---|
329 | info->screen_size = size;
|
---|
330 |
|
---|
331 | #ifdef CONFIG_FB_DEFERRED_IO
|
---|
332 | info->fbdefio = &vbox_defio;
|
---|
333 | fb_deferred_io_init(info);
|
---|
334 | #endif
|
---|
335 |
|
---|
336 | info->pixmap.flags = FB_PIXMAP_SYSTEM;
|
---|
337 |
|
---|
338 | DRM_DEBUG_KMS("allocated %dx%d\n", fb->width, fb->height);
|
---|
339 |
|
---|
340 | return 0;
|
---|
341 | }
|
---|
342 |
|
---|
343 | static struct drm_fb_helper_funcs vbox_fb_helper_funcs = {
|
---|
344 | .fb_probe = vboxfb_create,
|
---|
345 | };
|
---|
346 |
|
---|
347 | static void vbox_fbdev_destroy(struct drm_device *dev, struct vbox_fbdev *fbdev)
|
---|
348 | {
|
---|
349 | struct fb_info *info;
|
---|
350 | struct vbox_framebuffer *afb = &fbdev->afb;
|
---|
351 |
|
---|
352 | if (fbdev->helper.fbdev) {
|
---|
353 | info = fbdev->helper.fbdev;
|
---|
354 | unregister_framebuffer(info);
|
---|
355 | if (info->cmap.len)
|
---|
356 | fb_dealloc_cmap(&info->cmap);
|
---|
357 | framebuffer_release(info);
|
---|
358 | }
|
---|
359 |
|
---|
360 | if (afb->obj) {
|
---|
361 | struct vbox_bo *bo = gem_to_vbox_bo(afb->obj);
|
---|
362 |
|
---|
363 | if (!vbox_bo_reserve(bo, false)) {
|
---|
364 | if (bo->kmap.virtual)
|
---|
365 | ttm_bo_kunmap(&bo->kmap);
|
---|
366 | /*
|
---|
367 | * QXL does this, but is it really needed before
|
---|
368 | * freeing?
|
---|
369 | */
|
---|
370 | if (bo->pin_count)
|
---|
371 | vbox_bo_unpin(bo);
|
---|
372 | vbox_bo_unreserve(bo);
|
---|
373 | }
|
---|
374 | drm_gem_object_unreference_unlocked(afb->obj);
|
---|
375 | afb->obj = NULL;
|
---|
376 | }
|
---|
377 | drm_fb_helper_fini(&fbdev->helper);
|
---|
378 |
|
---|
379 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
|
---|
380 | drm_framebuffer_unregister_private(&afb->base);
|
---|
381 | #endif
|
---|
382 | drm_framebuffer_cleanup(&afb->base);
|
---|
383 | }
|
---|
384 |
|
---|
385 | int vbox_fbdev_init(struct drm_device *dev)
|
---|
386 | {
|
---|
387 | struct vbox_private *vbox = dev->dev_private;
|
---|
388 | struct vbox_fbdev *fbdev;
|
---|
389 | int ret;
|
---|
390 |
|
---|
391 | fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
|
---|
392 | if (!fbdev)
|
---|
393 | return -ENOMEM;
|
---|
394 |
|
---|
395 | vbox->fbdev = fbdev;
|
---|
396 | spin_lock_init(&fbdev->dirty_lock);
|
---|
397 |
|
---|
398 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0) && !defined(RHEL_73)
|
---|
399 | fbdev->helper.funcs = &vbox_fb_helper_funcs;
|
---|
400 | #else
|
---|
401 | drm_fb_helper_prepare(dev, &fbdev->helper, &vbox_fb_helper_funcs);
|
---|
402 | #endif
|
---|
403 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
|
---|
404 | ret = drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs);
|
---|
405 | #else
|
---|
406 | ret =
|
---|
407 | drm_fb_helper_init(dev, &fbdev->helper, vbox->num_crtcs,
|
---|
408 | vbox->num_crtcs);
|
---|
409 | #endif
|
---|
410 | if (ret)
|
---|
411 | goto free;
|
---|
412 |
|
---|
413 | ret = drm_fb_helper_single_add_all_connectors(&fbdev->helper);
|
---|
414 | if (ret)
|
---|
415 | goto fini;
|
---|
416 |
|
---|
417 | /* disable all the possible outputs/crtcs before entering KMS mode */
|
---|
418 | drm_helper_disable_unused_functions(dev);
|
---|
419 |
|
---|
420 | ret = drm_fb_helper_initial_config(&fbdev->helper, 32);
|
---|
421 | if (ret)
|
---|
422 | goto fini;
|
---|
423 |
|
---|
424 | return 0;
|
---|
425 |
|
---|
426 | fini:
|
---|
427 | drm_fb_helper_fini(&fbdev->helper);
|
---|
428 | free:
|
---|
429 | kfree(fbdev);
|
---|
430 | vbox->fbdev = NULL;
|
---|
431 |
|
---|
432 | return ret;
|
---|
433 | }
|
---|
434 |
|
---|
435 | void vbox_fbdev_fini(struct drm_device *dev)
|
---|
436 | {
|
---|
437 | struct vbox_private *vbox = dev->dev_private;
|
---|
438 |
|
---|
439 | if (!vbox->fbdev)
|
---|
440 | return;
|
---|
441 |
|
---|
442 | vbox_fbdev_destroy(dev, vbox->fbdev);
|
---|
443 | kfree(vbox->fbdev);
|
---|
444 | vbox->fbdev = NULL;
|
---|
445 | }
|
---|
446 |
|
---|
447 | void vbox_fbdev_set_suspend(struct drm_device *dev, int state)
|
---|
448 | {
|
---|
449 | struct vbox_private *vbox = dev->dev_private;
|
---|
450 |
|
---|
451 | if (!vbox->fbdev)
|
---|
452 | return;
|
---|
453 |
|
---|
454 | fb_set_suspend(vbox->fbdev->helper.fbdev, state);
|
---|
455 | }
|
---|