blob: 13b7dd83faa9ca7e6e05157d002be0199f3bd667 [file] [log] [blame]
Dave Airlie414c4532012-04-17 15:01:25 +01001/*
2 * Copyright 2010 Matt Turner.
3 * Copyright 2012 Red Hat
4 *
5 * This file is subject to the terms and conditions of the GNU General
6 * Public License version 2. See the file COPYING in the main
7 * directory of this archive for more details.
8 *
9 * Authors: Matthew Garrett
10 * Matt Turner
11 * Dave Airlie
12 */
13#include <linux/module.h>
David Howells760285e2012-10-02 18:01:07 +010014#include <drm/drmP.h>
15#include <drm/drm_fb_helper.h>
Daniel Vetter76a39db2013-01-20 23:12:54 +010016#include <drm/drm_crtc_helper.h>
Dave Airlie414c4532012-04-17 15:01:25 +010017
18#include <linux/fb.h>
19
20#include "mgag200_drv.h"
21
22static void mga_dirty_update(struct mga_fbdev *mfbdev,
23 int x, int y, int width, int height)
24{
25 int i;
26 struct drm_gem_object *obj;
27 struct mgag200_bo *bo;
28 int src_offset, dst_offset;
29 int bpp = (mfbdev->mfb.base.bits_per_pixel + 7)/8;
Maarten Lankhorsta06b9a72013-06-27 13:38:25 +020030 int ret = -EBUSY;
Dave Airlie414c4532012-04-17 15:01:25 +010031 bool unmap = false;
Dave Airlie64171952013-05-02 00:52:01 -040032 bool store_for_later = false;
33 int x2, y2;
34 unsigned long flags;
Dave Airlie414c4532012-04-17 15:01:25 +010035
36 obj = mfbdev->mfb.obj;
37 bo = gem_to_mga_bo(obj);
38
Dave Airlie64171952013-05-02 00:52:01 -040039 /*
40 * try and reserve the BO, if we fail with busy
41 * then the BO is being moved and we should
42 * store up the damage until later.
43 */
Dave Airlie8b7ad1b2014-02-05 14:47:45 +100044 if (drm_can_sleep())
Maarten Lankhorsta06b9a72013-06-27 13:38:25 +020045 ret = mgag200_bo_reserve(bo, true);
Dave Airlie414c4532012-04-17 15:01:25 +010046 if (ret) {
Dave Airlie64171952013-05-02 00:52:01 -040047 if (ret != -EBUSY)
48 return;
49
50 store_for_later = true;
51 }
52
53 x2 = x + width - 1;
54 y2 = y + height - 1;
55 spin_lock_irqsave(&mfbdev->dirty_lock, flags);
56
57 if (mfbdev->y1 < y)
58 y = mfbdev->y1;
59 if (mfbdev->y2 > y2)
60 y2 = mfbdev->y2;
61 if (mfbdev->x1 < x)
62 x = mfbdev->x1;
63 if (mfbdev->x2 > x2)
64 x2 = mfbdev->x2;
65
66 if (store_for_later) {
67 mfbdev->x1 = x;
68 mfbdev->x2 = x2;
69 mfbdev->y1 = y;
70 mfbdev->y2 = y2;
71 spin_unlock_irqrestore(&mfbdev->dirty_lock, flags);
Dave Airlie414c4532012-04-17 15:01:25 +010072 return;
73 }
74
Dave Airlie64171952013-05-02 00:52:01 -040075 mfbdev->x1 = mfbdev->y1 = INT_MAX;
76 mfbdev->x2 = mfbdev->y2 = 0;
77 spin_unlock_irqrestore(&mfbdev->dirty_lock, flags);
78
Dave Airlie414c4532012-04-17 15:01:25 +010079 if (!bo->kmap.virtual) {
80 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
81 if (ret) {
82 DRM_ERROR("failed to kmap fb updates\n");
83 mgag200_bo_unreserve(bo);
84 return;
85 }
86 unmap = true;
87 }
Dave Airlie64171952013-05-02 00:52:01 -040088 for (i = y; i <= y2; i++) {
Dave Airlie414c4532012-04-17 15:01:25 +010089 /* assume equal stride for now */
90 src_offset = dst_offset = i * mfbdev->mfb.base.pitches[0] + (x * bpp);
Dave Airlie64171952013-05-02 00:52:01 -040091 memcpy_toio(bo->kmap.virtual + src_offset, mfbdev->sysram + src_offset, (x2 - x + 1) * bpp);
Dave Airlie414c4532012-04-17 15:01:25 +010092
93 }
94 if (unmap)
95 ttm_bo_kunmap(&bo->kmap);
96
97 mgag200_bo_unreserve(bo);
98}
99
100static void mga_fillrect(struct fb_info *info,
101 const struct fb_fillrect *rect)
102{
103 struct mga_fbdev *mfbdev = info->par;
104 sys_fillrect(info, rect);
105 mga_dirty_update(mfbdev, rect->dx, rect->dy, rect->width,
106 rect->height);
107}
108
109static void mga_copyarea(struct fb_info *info,
110 const struct fb_copyarea *area)
111{
112 struct mga_fbdev *mfbdev = info->par;
113 sys_copyarea(info, area);
114 mga_dirty_update(mfbdev, area->dx, area->dy, area->width,
115 area->height);
116}
117
118static void mga_imageblit(struct fb_info *info,
119 const struct fb_image *image)
120{
121 struct mga_fbdev *mfbdev = info->par;
122 sys_imageblit(info, image);
123 mga_dirty_update(mfbdev, image->dx, image->dy, image->width,
124 image->height);
125}
126
127
128static struct fb_ops mgag200fb_ops = {
129 .owner = THIS_MODULE,
130 .fb_check_var = drm_fb_helper_check_var,
131 .fb_set_par = drm_fb_helper_set_par,
132 .fb_fillrect = mga_fillrect,
133 .fb_copyarea = mga_copyarea,
134 .fb_imageblit = mga_imageblit,
135 .fb_pan_display = drm_fb_helper_pan_display,
136 .fb_blank = drm_fb_helper_blank,
137 .fb_setcmap = drm_fb_helper_setcmap,
138};
139
140static int mgag200fb_create_object(struct mga_fbdev *afbdev,
141 struct drm_mode_fb_cmd2 *mode_cmd,
142 struct drm_gem_object **gobj_p)
143{
144 struct drm_device *dev = afbdev->helper.dev;
Dave Airlie414c4532012-04-17 15:01:25 +0100145 u32 size;
146 struct drm_gem_object *gobj;
Dave Airlie414c4532012-04-17 15:01:25 +0100147 int ret = 0;
Dave Airlie414c4532012-04-17 15:01:25 +0100148
149 size = mode_cmd->pitches[0] * mode_cmd->height;
150 ret = mgag200_gem_create(dev, size, true, &gobj);
151 if (ret)
152 return ret;
153
154 *gobj_p = gobj;
155 return ret;
156}
157
Daniel Vettercd5428a2013-01-21 23:42:49 +0100158static int mgag200fb_create(struct drm_fb_helper *helper,
Dave Airlie414c4532012-04-17 15:01:25 +0100159 struct drm_fb_helper_surface_size *sizes)
160{
Daniel Vettercd5428a2013-01-21 23:42:49 +0100161 struct mga_fbdev *mfbdev = (struct mga_fbdev *)helper;
Dave Airlie414c4532012-04-17 15:01:25 +0100162 struct drm_device *dev = mfbdev->helper.dev;
163 struct drm_mode_fb_cmd2 mode_cmd;
164 struct mga_device *mdev = dev->dev_private;
165 struct fb_info *info;
166 struct drm_framebuffer *fb;
167 struct drm_gem_object *gobj = NULL;
168 struct device *device = &dev->pdev->dev;
169 struct mgag200_bo *bo;
170 int ret;
171 void *sysram;
172 int size;
173
174 mode_cmd.width = sizes->surface_width;
175 mode_cmd.height = sizes->surface_height;
176 mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
177
178 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
179 sizes->surface_depth);
180 size = mode_cmd.pitches[0] * mode_cmd.height;
181
182 ret = mgag200fb_create_object(mfbdev, &mode_cmd, &gobj);
183 if (ret) {
184 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
185 return ret;
186 }
187 bo = gem_to_mga_bo(gobj);
188
189 sysram = vmalloc(size);
190 if (!sysram)
191 return -ENOMEM;
192
193 info = framebuffer_alloc(0, device);
194 if (info == NULL)
195 return -ENOMEM;
196
197 info->par = mfbdev;
198
199 ret = mgag200_framebuffer_init(dev, &mfbdev->mfb, &mode_cmd, gobj);
200 if (ret)
201 return ret;
202
203 mfbdev->sysram = sysram;
204 mfbdev->size = size;
205
206 fb = &mfbdev->mfb.base;
207
208 /* setup helper */
209 mfbdev->helper.fb = fb;
210 mfbdev->helper.fbdev = info;
211
212 ret = fb_alloc_cmap(&info->cmap, 256, 0);
213 if (ret) {
214 DRM_ERROR("%s: can't allocate color map\n", info->fix.id);
215 ret = -ENOMEM;
216 goto out;
217 }
218
219 strcpy(info->fix.id, "mgadrmfb");
220
221 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
222 info->fbops = &mgag200fb_ops;
223
224 /* setup aperture base/size for vesafb takeover */
225 info->apertures = alloc_apertures(1);
226 if (!info->apertures) {
227 ret = -ENOMEM;
228 goto out;
229 }
230 info->apertures->ranges[0].base = mdev->dev->mode_config.fb_base;
231 info->apertures->ranges[0].size = mdev->mc.vram_size;
232
233 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
234 drm_fb_helper_fill_var(info, &mfbdev->helper, sizes->fb_width,
235 sizes->fb_height);
236
237 info->screen_base = sysram;
238 info->screen_size = size;
239 info->pixmap.flags = FB_PIXMAP_SYSTEM;
240
241 DRM_DEBUG_KMS("allocated %dx%d\n",
242 fb->width, fb->height);
243 return 0;
244out:
245 return ret;
246}
247
Dave Airlie414c4532012-04-17 15:01:25 +0100248static int mga_fbdev_destroy(struct drm_device *dev,
249 struct mga_fbdev *mfbdev)
250{
251 struct fb_info *info;
252 struct mga_framebuffer *mfb = &mfbdev->mfb;
253
254 if (mfbdev->helper.fbdev) {
255 info = mfbdev->helper.fbdev;
256
257 unregister_framebuffer(info);
258 if (info->cmap.len)
259 fb_dealloc_cmap(&info->cmap);
260 framebuffer_release(info);
261 }
262
263 if (mfb->obj) {
264 drm_gem_object_unreference_unlocked(mfb->obj);
265 mfb->obj = NULL;
266 }
267 drm_fb_helper_fini(&mfbdev->helper);
268 vfree(mfbdev->sysram);
Daniel Vetter36206362012-12-10 20:42:17 +0100269 drm_framebuffer_unregister_private(&mfb->base);
Dave Airlie414c4532012-04-17 15:01:25 +0100270 drm_framebuffer_cleanup(&mfb->base);
271
272 return 0;
273}
274
275static struct drm_fb_helper_funcs mga_fb_helper_funcs = {
276 .gamma_set = mga_crtc_fb_gamma_set,
277 .gamma_get = mga_crtc_fb_gamma_get,
Daniel Vettercd5428a2013-01-21 23:42:49 +0100278 .fb_probe = mgag200fb_create,
Dave Airlie414c4532012-04-17 15:01:25 +0100279};
280
281int mgag200_fbdev_init(struct mga_device *mdev)
282{
283 struct mga_fbdev *mfbdev;
284 int ret;
Dave Airlie918be882014-01-21 01:47:46 -0500285 int bpp_sel = 32;
286
287 /* prefer 16bpp on low end gpus with limited VRAM */
288 if (IS_G200_SE(mdev) && mdev->mc.vram_size < (2048*1024))
289 bpp_sel = 16;
Dave Airlie414c4532012-04-17 15:01:25 +0100290
Christopher Harveyc2ed8842013-04-05 16:15:30 +0000291 mfbdev = devm_kzalloc(mdev->dev->dev, sizeof(struct mga_fbdev), GFP_KERNEL);
Dave Airlie414c4532012-04-17 15:01:25 +0100292 if (!mfbdev)
293 return -ENOMEM;
294
295 mdev->mfbdev = mfbdev;
296 mfbdev->helper.funcs = &mga_fb_helper_funcs;
Dave Airlie64171952013-05-02 00:52:01 -0400297 spin_lock_init(&mfbdev->dirty_lock);
Dave Airlie414c4532012-04-17 15:01:25 +0100298
299 ret = drm_fb_helper_init(mdev->dev, &mfbdev->helper,
300 mdev->num_crtc, MGAG200FB_CONN_LIMIT);
Christopher Harveyc2ed8842013-04-05 16:15:30 +0000301 if (ret)
Dave Airlie414c4532012-04-17 15:01:25 +0100302 return ret;
Christopher Harveyc2ed8842013-04-05 16:15:30 +0000303
Dave Airlie414c4532012-04-17 15:01:25 +0100304 drm_fb_helper_single_add_all_connectors(&mfbdev->helper);
Daniel Vetter76a39db2013-01-20 23:12:54 +0100305
306 /* disable all the possible outputs/crtcs before entering KMS mode */
307 drm_helper_disable_unused_functions(mdev->dev);
308
Dave Airlie918be882014-01-21 01:47:46 -0500309 drm_fb_helper_initial_config(&mfbdev->helper, bpp_sel);
Dave Airlie414c4532012-04-17 15:01:25 +0100310
311 return 0;
312}
313
314void mgag200_fbdev_fini(struct mga_device *mdev)
315{
316 if (!mdev->mfbdev)
317 return;
318
319 mga_fbdev_destroy(mdev->dev, mdev->mfbdev);
Dave Airlie414c4532012-04-17 15:01:25 +0100320}