blob: da790a1c302a2e9069d0088313e4d6f1a983afc9 [file] [log] [blame]
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 */
7
8#include "bochs.h"
9
10/* ---------------------------------------------------------------------- */
11
Gerd Hoffmann49b70a32014-11-19 12:28:11 +010012static int bochsfb_mmap(struct fb_info *info,
13 struct vm_area_struct *vma)
14{
15 struct drm_fb_helper *fb_helper = info->par;
16 struct bochs_device *bochs =
17 container_of(fb_helper, struct bochs_device, fb.helper);
18 struct bochs_bo *bo = gem_to_bochs_bo(bochs->fb.gfb.obj);
19
20 return ttm_fbdev_mmap(vma, &bo->bo);
21}
22
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +010023static struct fb_ops bochsfb_ops = {
24 .owner = THIS_MODULE,
Stefan Christd4d938c2016-11-14 00:03:15 +010025 DRM_FB_HELPER_DEFAULT_OPS,
Archit Taneja6a752972015-07-31 16:21:59 +053026 .fb_fillrect = drm_fb_helper_sys_fillrect,
27 .fb_copyarea = drm_fb_helper_sys_copyarea,
28 .fb_imageblit = drm_fb_helper_sys_imageblit,
Gerd Hoffmann49b70a32014-11-19 12:28:11 +010029 .fb_mmap = bochsfb_mmap,
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +010030};
31
32static int bochsfb_create_object(struct bochs_device *bochs,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020033 const struct drm_mode_fb_cmd2 *mode_cmd,
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +010034 struct drm_gem_object **gobj_p)
35{
36 struct drm_device *dev = bochs->dev;
37 struct drm_gem_object *gobj;
38 u32 size;
39 int ret = 0;
40
41 size = mode_cmd->pitches[0] * mode_cmd->height;
42 ret = bochs_gem_create(dev, size, true, &gobj);
43 if (ret)
44 return ret;
45
46 *gobj_p = gobj;
47 return ret;
48}
49
50static int bochsfb_create(struct drm_fb_helper *helper,
51 struct drm_fb_helper_surface_size *sizes)
52{
53 struct bochs_device *bochs =
54 container_of(helper, struct bochs_device, fb.helper);
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +010055 struct fb_info *info;
56 struct drm_framebuffer *fb;
57 struct drm_mode_fb_cmd2 mode_cmd;
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +010058 struct drm_gem_object *gobj = NULL;
59 struct bochs_bo *bo = NULL;
60 int size, ret;
61
62 if (sizes->surface_bpp != 32)
63 return -EINVAL;
64
65 mode_cmd.width = sizes->surface_width;
66 mode_cmd.height = sizes->surface_height;
67 mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
68 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
69 sizes->surface_depth);
70 size = mode_cmd.pitches[0] * mode_cmd.height;
71
72 /* alloc, pin & map bo */
73 ret = bochsfb_create_object(bochs, &mode_cmd, &gobj);
74 if (ret) {
75 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
76 return ret;
77 }
78
79 bo = gem_to_bochs_bo(gobj);
80
Christian Königdfd5e502016-04-06 11:12:03 +020081 ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +010082 if (ret)
83 return ret;
84
85 ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL);
86 if (ret) {
87 DRM_ERROR("failed to pin fbcon\n");
88 ttm_bo_unreserve(&bo->bo);
89 return ret;
90 }
91
92 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages,
93 &bo->kmap);
94 if (ret) {
95 DRM_ERROR("failed to kmap fbcon\n");
96 ttm_bo_unreserve(&bo->bo);
97 return ret;
98 }
99
100 ttm_bo_unreserve(&bo->bo);
101
102 /* init fb device */
Archit Taneja6a752972015-07-31 16:21:59 +0530103 info = drm_fb_helper_alloc_fbi(helper);
104 if (IS_ERR(info))
105 return PTR_ERR(info);
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100106
107 info->par = &bochs->fb.helper;
108
109 ret = bochs_framebuffer_init(bochs->dev, &bochs->fb.gfb, &mode_cmd, gobj);
Archit Taneja6a752972015-07-31 16:21:59 +0530110 if (ret) {
111 drm_fb_helper_release_fbi(helper);
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100112 return ret;
Archit Taneja6a752972015-07-31 16:21:59 +0530113 }
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100114
115 bochs->fb.size = size;
116
117 /* setup helper */
118 fb = &bochs->fb.gfb.base;
119 bochs->fb.helper.fb = fb;
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100120
121 strcpy(info->fix.id, "bochsdrmfb");
122
123 info->flags = FBINFO_DEFAULT;
124 info->fbops = &bochsfb_ops;
125
126 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
127 drm_fb_helper_fill_var(info, &bochs->fb.helper, sizes->fb_width,
128 sizes->fb_height);
129
130 info->screen_base = bo->kmap.virtual;
131 info->screen_size = size;
132
Gerd Hoffmann49b70a32014-11-19 12:28:11 +0100133 drm_vma_offset_remove(&bo->bo.bdev->vma_manager, &bo->bo.vma_node);
134 info->fix.smem_start = 0;
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100135 info->fix.smem_len = size;
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100136
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100137 return 0;
138}
139
140static int bochs_fbdev_destroy(struct bochs_device *bochs)
141{
142 struct bochs_framebuffer *gfb = &bochs->fb.gfb;
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100143
144 DRM_DEBUG_DRIVER("\n");
145
Archit Taneja6a752972015-07-31 16:21:59 +0530146 drm_fb_helper_unregister_fbi(&bochs->fb.helper);
147 drm_fb_helper_release_fbi(&bochs->fb.helper);
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100148
149 if (gfb->obj) {
150 drm_gem_object_unreference_unlocked(gfb->obj);
151 gfb->obj = NULL;
152 }
153
154 drm_fb_helper_fini(&bochs->fb.helper);
155 drm_framebuffer_unregister_private(&gfb->base);
156 drm_framebuffer_cleanup(&gfb->base);
157
158 return 0;
159}
160
Thierry Reding3a493872014-06-27 17:19:23 +0200161static const struct drm_fb_helper_funcs bochs_fb_helper_funcs = {
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100162 .fb_probe = bochsfb_create,
163};
164
165int bochs_fbdev_init(struct bochs_device *bochs)
166{
167 int ret;
168
Thierry Reding10a23102014-06-27 17:19:24 +0200169 drm_fb_helper_prepare(bochs->dev, &bochs->fb.helper,
170 &bochs_fb_helper_funcs);
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100171
172 ret = drm_fb_helper_init(bochs->dev, &bochs->fb.helper,
173 1, 1);
174 if (ret)
175 return ret;
176
Thierry Reding01934c22014-12-19 11:21:32 +0100177 ret = drm_fb_helper_single_add_all_connectors(&bochs->fb.helper);
178 if (ret)
179 goto fini;
180
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100181 drm_helper_disable_unused_functions(bochs->dev);
Thierry Reding01934c22014-12-19 11:21:32 +0100182
183 ret = drm_fb_helper_initial_config(&bochs->fb.helper, 32);
184 if (ret)
185 goto fini;
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100186
187 bochs->fb.initialized = true;
188 return 0;
Thierry Reding01934c22014-12-19 11:21:32 +0100189
190fini:
191 drm_fb_helper_fini(&bochs->fb.helper);
192 return ret;
Gerd Hoffmann0a6659b2013-12-17 18:04:46 +0100193}
194
195void bochs_fbdev_fini(struct bochs_device *bochs)
196{
197 if (!bochs->fb.initialized)
198 return;
199
200 bochs_fbdev_destroy(bochs);
201 bochs->fb.initialized = false;
202}