]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/vboxvideo/vbox_fb.c
staging: vboxvideo: Cleanup header use
[linux.git] / drivers / staging / vboxvideo / vbox_fb.c
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright (C) 2013-2017 Oracle Corporation
4  * This file is based on ast_fb.c
5  * Copyright 2012 Red Hat Inc.
6  * Authors: Dave Airlie <airlied@redhat.com>
7  *          Michael Thayer <michael.thayer@oracle.com,
8  */
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/mm.h>
14 #include <linux/tty.h>
15 #include <linux/sysrq.h>
16 #include <linux/delay.h>
17 #include <linux/fb.h>
18 #include <linux/init.h>
19
20 #include <drm/drm_crtc.h>
21 #include <drm/drm_fb_helper.h>
22 #include <drm/drm_crtc_helper.h>
23
24 #include "vbox_drv.h"
25 #include "vboxvideo.h"
26
27 #ifdef CONFIG_DRM_KMS_FB_HELPER
28 static struct fb_deferred_io vbox_defio = {
29         .delay = HZ / 30,
30         .deferred_io = drm_fb_helper_deferred_io,
31 };
32 #endif
33
34 static struct fb_ops vboxfb_ops = {
35         .owner = THIS_MODULE,
36         .fb_check_var = drm_fb_helper_check_var,
37         .fb_set_par = drm_fb_helper_set_par,
38         .fb_fillrect = drm_fb_helper_sys_fillrect,
39         .fb_copyarea = drm_fb_helper_sys_copyarea,
40         .fb_imageblit = drm_fb_helper_sys_imageblit,
41         .fb_pan_display = drm_fb_helper_pan_display,
42         .fb_blank = drm_fb_helper_blank,
43         .fb_setcmap = drm_fb_helper_setcmap,
44         .fb_debug_enter = drm_fb_helper_debug_enter,
45         .fb_debug_leave = drm_fb_helper_debug_leave,
46 };
47
48 int vboxfb_create(struct drm_fb_helper *helper,
49                   struct drm_fb_helper_surface_size *sizes)
50 {
51         struct vbox_private *vbox =
52                 container_of(helper, struct vbox_private, fb_helper);
53         struct pci_dev *pdev = vbox->ddev.pdev;
54         struct DRM_MODE_FB_CMD mode_cmd;
55         struct drm_framebuffer *fb;
56         struct fb_info *info;
57         struct drm_gem_object *gobj;
58         struct vbox_bo *bo;
59         int size, ret;
60         u64 gpu_addr;
61         u32 pitch;
62
63         mode_cmd.width = sizes->surface_width;
64         mode_cmd.height = sizes->surface_height;
65         pitch = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
66         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
67                                                           sizes->surface_depth);
68         mode_cmd.pitches[0] = pitch;
69
70         size = pitch * mode_cmd.height;
71
72         ret = vbox_gem_create(vbox, size, true, &gobj);
73         if (ret) {
74                 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
75                 return ret;
76         }
77
78         ret = vbox_framebuffer_init(vbox, &vbox->afb, &mode_cmd, gobj);
79         if (ret)
80                 return ret;
81
82         bo = gem_to_vbox_bo(gobj);
83
84         ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM);
85         if (ret)
86                 return ret;
87
88         info = drm_fb_helper_alloc_fbi(helper);
89         if (IS_ERR(info))
90                 return PTR_ERR(info);
91
92         info->screen_size = size;
93         info->screen_base = (char __iomem *)vbox_bo_kmap(bo);
94         if (IS_ERR(info->screen_base))
95                 return PTR_ERR(info->screen_base);
96
97         info->par = helper;
98
99         fb = &vbox->afb.base;
100         helper->fb = fb;
101
102         strcpy(info->fix.id, "vboxdrmfb");
103
104         /*
105          * The last flag forces a mode set on VT switches even if the kernel
106          * does not think it is needed.
107          */
108         info->flags = FBINFO_DEFAULT | FBINFO_MISC_ALWAYS_SETPAR;
109         info->fbops = &vboxfb_ops;
110
111         /*
112          * This seems to be done for safety checking that the framebuffer
113          * is not registered twice by different drivers.
114          */
115         info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
116         info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
117
118         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
119         drm_fb_helper_fill_var(info, helper, sizes->fb_width,
120                                sizes->fb_height);
121
122         gpu_addr = vbox_bo_gpu_offset(bo);
123         info->fix.smem_start = info->apertures->ranges[0].base + gpu_addr;
124         info->fix.smem_len = vbox->available_vram_size - gpu_addr;
125
126 #ifdef CONFIG_DRM_KMS_FB_HELPER
127         info->fbdefio = &vbox_defio;
128         fb_deferred_io_init(info);
129 #endif
130
131         info->pixmap.flags = FB_PIXMAP_SYSTEM;
132
133         DRM_DEBUG_KMS("allocated %dx%d\n", fb->width, fb->height);
134
135         return 0;
136 }
137
138 void vbox_fbdev_fini(struct vbox_private *vbox)
139 {
140         struct vbox_framebuffer *afb = &vbox->afb;
141
142 #ifdef CONFIG_DRM_KMS_FB_HELPER
143         if (vbox->fb_helper.fbdev && vbox->fb_helper.fbdev->fbdefio)
144                 fb_deferred_io_cleanup(vbox->fb_helper.fbdev);
145 #endif
146
147         drm_fb_helper_unregister_fbi(&vbox->fb_helper);
148
149         if (afb->obj) {
150                 struct vbox_bo *bo = gem_to_vbox_bo(afb->obj);
151
152                 vbox_bo_kunmap(bo);
153
154                 if (bo->pin_count)
155                         vbox_bo_unpin(bo);
156
157                 drm_gem_object_put_unlocked(afb->obj);
158                 afb->obj = NULL;
159         }
160         drm_fb_helper_fini(&vbox->fb_helper);
161
162         drm_framebuffer_unregister_private(&afb->base);
163         drm_framebuffer_cleanup(&afb->base);
164 }