dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / mgag200 / mgag200_fb.c
blob6893934b26c0387a390540b770ec1e49252902bb
1 /*
2 * Copyright 2010 Matt Turner.
3 * Copyright 2012 Red Hat
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.
9 * Authors: Matthew Garrett
10 * Matt Turner
11 * Dave Airlie
13 #include <linux/module.h>
14 #include <drm/drmP.h>
15 #include <drm/drm_util.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_crtc_helper.h>
19 #include "mgag200_drv.h"
21 static void mga_dirty_update(struct mga_fbdev *mfbdev,
22 int x, int y, int width, int height)
24 int i;
25 struct drm_gem_object *obj;
26 struct mgag200_bo *bo;
27 int src_offset, dst_offset;
28 int bpp = mfbdev->mfb.base.format->cpp[0];
29 int ret = -EBUSY;
30 bool unmap = false;
31 bool store_for_later = false;
32 int x2, y2;
33 unsigned long flags;
35 obj = mfbdev->mfb.obj;
36 bo = gem_to_mga_bo(obj);
39 * try and reserve the BO, if we fail with busy
40 * then the BO is being moved and we should
41 * store up the damage until later.
43 if (drm_can_sleep())
44 ret = mgag200_bo_reserve(bo, true);
45 if (ret) {
46 if (ret != -EBUSY)
47 return;
49 store_for_later = true;
52 x2 = x + width - 1;
53 y2 = y + height - 1;
54 spin_lock_irqsave(&mfbdev->dirty_lock, flags);
56 if (mfbdev->y1 < y)
57 y = mfbdev->y1;
58 if (mfbdev->y2 > y2)
59 y2 = mfbdev->y2;
60 if (mfbdev->x1 < x)
61 x = mfbdev->x1;
62 if (mfbdev->x2 > x2)
63 x2 = mfbdev->x2;
65 if (store_for_later) {
66 mfbdev->x1 = x;
67 mfbdev->x2 = x2;
68 mfbdev->y1 = y;
69 mfbdev->y2 = y2;
70 spin_unlock_irqrestore(&mfbdev->dirty_lock, flags);
71 return;
74 mfbdev->x1 = mfbdev->y1 = INT_MAX;
75 mfbdev->x2 = mfbdev->y2 = 0;
76 spin_unlock_irqrestore(&mfbdev->dirty_lock, flags);
78 if (!bo->kmap.virtual) {
79 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
80 if (ret) {
81 DRM_ERROR("failed to kmap fb updates\n");
82 mgag200_bo_unreserve(bo);
83 return;
85 unmap = true;
87 for (i = y; i <= y2; i++) {
88 /* assume equal stride for now */
89 src_offset = dst_offset = i * mfbdev->mfb.base.pitches[0] + (x * bpp);
90 memcpy_toio(bo->kmap.virtual + src_offset, mfbdev->sysram + src_offset, (x2 - x + 1) * bpp);
93 if (unmap)
94 ttm_bo_kunmap(&bo->kmap);
96 mgag200_bo_unreserve(bo);
99 static void mga_fillrect(struct fb_info *info,
100 const struct fb_fillrect *rect)
102 struct mga_fbdev *mfbdev = info->par;
103 drm_fb_helper_sys_fillrect(info, rect);
104 mga_dirty_update(mfbdev, rect->dx, rect->dy, rect->width,
105 rect->height);
108 static void mga_copyarea(struct fb_info *info,
109 const struct fb_copyarea *area)
111 struct mga_fbdev *mfbdev = info->par;
112 drm_fb_helper_sys_copyarea(info, area);
113 mga_dirty_update(mfbdev, area->dx, area->dy, area->width,
114 area->height);
117 static void mga_imageblit(struct fb_info *info,
118 const struct fb_image *image)
120 struct mga_fbdev *mfbdev = info->par;
121 drm_fb_helper_sys_imageblit(info, image);
122 mga_dirty_update(mfbdev, image->dx, image->dy, image->width,
123 image->height);
127 static struct fb_ops mgag200fb_ops = {
128 .owner = THIS_MODULE,
129 .fb_check_var = drm_fb_helper_check_var,
130 .fb_set_par = drm_fb_helper_set_par,
131 .fb_fillrect = mga_fillrect,
132 .fb_copyarea = mga_copyarea,
133 .fb_imageblit = mga_imageblit,
134 .fb_pan_display = drm_fb_helper_pan_display,
135 .fb_blank = drm_fb_helper_blank,
136 .fb_setcmap = drm_fb_helper_setcmap,
139 static int mgag200fb_create_object(struct mga_fbdev *afbdev,
140 const struct drm_mode_fb_cmd2 *mode_cmd,
141 struct drm_gem_object **gobj_p)
143 struct drm_device *dev = afbdev->helper.dev;
144 u32 size;
145 struct drm_gem_object *gobj;
146 int ret = 0;
148 size = mode_cmd->pitches[0] * mode_cmd->height;
149 ret = mgag200_gem_create(dev, size, true, &gobj);
150 if (ret)
151 return ret;
153 *gobj_p = gobj;
154 return ret;
157 static int mgag200fb_create(struct drm_fb_helper *helper,
158 struct drm_fb_helper_surface_size *sizes)
160 struct mga_fbdev *mfbdev =
161 container_of(helper, struct mga_fbdev, helper);
162 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 int ret;
169 void *sysram;
170 int size;
172 mode_cmd.width = sizes->surface_width;
173 mode_cmd.height = sizes->surface_height;
174 mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
176 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
177 sizes->surface_depth);
178 size = mode_cmd.pitches[0] * mode_cmd.height;
180 ret = mgag200fb_create_object(mfbdev, &mode_cmd, &gobj);
181 if (ret) {
182 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
183 return ret;
186 sysram = vmalloc(size);
187 if (!sysram) {
188 ret = -ENOMEM;
189 goto err_sysram;
192 info = drm_fb_helper_alloc_fbi(helper);
193 if (IS_ERR(info)) {
194 ret = PTR_ERR(info);
195 goto err_alloc_fbi;
198 info->par = mfbdev;
200 ret = mgag200_framebuffer_init(dev, &mfbdev->mfb, &mode_cmd, gobj);
201 if (ret)
202 goto err_alloc_fbi;
204 mfbdev->sysram = sysram;
205 mfbdev->size = size;
207 fb = &mfbdev->mfb.base;
209 /* setup helper */
210 mfbdev->helper.fb = fb;
212 strcpy(info->fix.id, "mgadrmfb");
214 info->fbops = &mgag200fb_ops;
216 /* setup aperture base/size for vesafb takeover */
217 info->apertures->ranges[0].base = mdev->dev->mode_config.fb_base;
218 info->apertures->ranges[0].size = mdev->mc.vram_size;
220 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
221 drm_fb_helper_fill_var(info, &mfbdev->helper, sizes->fb_width,
222 sizes->fb_height);
224 info->screen_base = sysram;
225 info->screen_size = size;
226 info->pixmap.flags = FB_PIXMAP_SYSTEM;
228 DRM_DEBUG_KMS("allocated %dx%d\n",
229 fb->width, fb->height);
231 return 0;
233 err_alloc_fbi:
234 vfree(sysram);
235 err_sysram:
236 drm_gem_object_put_unlocked(gobj);
238 return ret;
241 static int mga_fbdev_destroy(struct drm_device *dev,
242 struct mga_fbdev *mfbdev)
244 struct mga_framebuffer *mfb = &mfbdev->mfb;
246 drm_fb_helper_unregister_fbi(&mfbdev->helper);
248 if (mfb->obj) {
249 drm_gem_object_put_unlocked(mfb->obj);
250 mfb->obj = NULL;
252 drm_fb_helper_fini(&mfbdev->helper);
253 vfree(mfbdev->sysram);
254 drm_framebuffer_unregister_private(&mfb->base);
255 drm_framebuffer_cleanup(&mfb->base);
257 return 0;
260 static const struct drm_fb_helper_funcs mga_fb_helper_funcs = {
261 .fb_probe = mgag200fb_create,
264 int mgag200_fbdev_init(struct mga_device *mdev)
266 struct mga_fbdev *mfbdev;
267 int ret;
268 int bpp_sel = 32;
270 /* prefer 16bpp on low end gpus with limited VRAM */
271 if (IS_G200_SE(mdev) && mdev->mc.vram_size < (2048*1024))
272 bpp_sel = 16;
274 mfbdev = devm_kzalloc(mdev->dev->dev, sizeof(struct mga_fbdev), GFP_KERNEL);
275 if (!mfbdev)
276 return -ENOMEM;
278 mdev->mfbdev = mfbdev;
279 spin_lock_init(&mfbdev->dirty_lock);
281 drm_fb_helper_prepare(mdev->dev, &mfbdev->helper, &mga_fb_helper_funcs);
283 ret = drm_fb_helper_init(mdev->dev, &mfbdev->helper,
284 MGAG200FB_CONN_LIMIT);
285 if (ret)
286 goto err_fb_helper;
288 ret = drm_fb_helper_single_add_all_connectors(&mfbdev->helper);
289 if (ret)
290 goto err_fb_setup;
292 /* disable all the possible outputs/crtcs before entering KMS mode */
293 drm_helper_disable_unused_functions(mdev->dev);
295 ret = drm_fb_helper_initial_config(&mfbdev->helper, bpp_sel);
296 if (ret)
297 goto err_fb_setup;
299 return 0;
301 err_fb_setup:
302 drm_fb_helper_fini(&mfbdev->helper);
303 err_fb_helper:
304 mdev->mfbdev = NULL;
306 return ret;
309 void mgag200_fbdev_fini(struct mga_device *mdev)
311 if (!mdev->mfbdev)
312 return;
314 mga_fbdev_destroy(mdev->dev, mdev->mfbdev);