Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / gpu / drm / ast / ast_fb.c
blobc017a9330a18991cbd05eaa9be7cfeef557de98e
1 /*
2 * Copyright 2012 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
22 * of the Software.
26 * Authors: Dave Airlie <airlied@redhat.com>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/string.h>
32 #include <linux/mm.h>
33 #include <linux/tty.h>
34 #include <linux/sysrq.h>
35 #include <linux/delay.h>
36 #include <linux/fb.h>
37 #include <linux/init.h>
40 #include <drm/drmP.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_fb_helper.h>
43 #include <drm/drm_crtc_helper.h>
44 #include "ast_drv.h"
46 static void ast_dirty_update(struct ast_fbdev *afbdev,
47 int x, int y, int width, int height)
49 int i;
50 struct drm_gem_object *obj;
51 struct ast_bo *bo;
52 int src_offset, dst_offset;
53 int bpp = (afbdev->afb.base.bits_per_pixel + 7)/8;
54 int ret = -EBUSY;
55 bool unmap = false;
56 bool store_for_later = false;
57 int x2, y2;
58 unsigned long flags;
60 obj = afbdev->afb.obj;
61 bo = gem_to_ast_bo(obj);
64 * try and reserve the BO, if we fail with busy
65 * then the BO is being moved and we should
66 * store up the damage until later.
68 if (drm_can_sleep())
69 ret = ast_bo_reserve(bo, true);
70 if (ret) {
71 if (ret != -EBUSY)
72 return;
74 store_for_later = true;
77 x2 = x + width - 1;
78 y2 = y + height - 1;
79 spin_lock_irqsave(&afbdev->dirty_lock, flags);
81 if (afbdev->y1 < y)
82 y = afbdev->y1;
83 if (afbdev->y2 > y2)
84 y2 = afbdev->y2;
85 if (afbdev->x1 < x)
86 x = afbdev->x1;
87 if (afbdev->x2 > x2)
88 x2 = afbdev->x2;
90 if (store_for_later) {
91 afbdev->x1 = x;
92 afbdev->x2 = x2;
93 afbdev->y1 = y;
94 afbdev->y2 = y2;
95 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
96 return;
99 afbdev->x1 = afbdev->y1 = INT_MAX;
100 afbdev->x2 = afbdev->y2 = 0;
101 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
103 if (!bo->kmap.virtual) {
104 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
105 if (ret) {
106 DRM_ERROR("failed to kmap fb updates\n");
107 ast_bo_unreserve(bo);
108 return;
110 unmap = true;
112 for (i = y; i <= y2; i++) {
113 /* assume equal stride for now */
114 src_offset = dst_offset = i * afbdev->afb.base.pitches[0] + (x * bpp);
115 memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, (x2 - x + 1) * bpp);
118 if (unmap)
119 ttm_bo_kunmap(&bo->kmap);
121 ast_bo_unreserve(bo);
124 static void ast_fillrect(struct fb_info *info,
125 const struct fb_fillrect *rect)
127 struct ast_fbdev *afbdev = info->par;
128 drm_fb_helper_sys_fillrect(info, rect);
129 ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
130 rect->height);
133 static void ast_copyarea(struct fb_info *info,
134 const struct fb_copyarea *area)
136 struct ast_fbdev *afbdev = info->par;
137 drm_fb_helper_sys_copyarea(info, area);
138 ast_dirty_update(afbdev, area->dx, area->dy, area->width,
139 area->height);
142 static void ast_imageblit(struct fb_info *info,
143 const struct fb_image *image)
145 struct ast_fbdev *afbdev = info->par;
146 drm_fb_helper_sys_imageblit(info, image);
147 ast_dirty_update(afbdev, image->dx, image->dy, image->width,
148 image->height);
151 static struct fb_ops astfb_ops = {
152 .owner = THIS_MODULE,
153 .fb_check_var = drm_fb_helper_check_var,
154 .fb_set_par = drm_fb_helper_set_par,
155 .fb_fillrect = ast_fillrect,
156 .fb_copyarea = ast_copyarea,
157 .fb_imageblit = ast_imageblit,
158 .fb_pan_display = drm_fb_helper_pan_display,
159 .fb_blank = drm_fb_helper_blank,
160 .fb_setcmap = drm_fb_helper_setcmap,
161 .fb_debug_enter = drm_fb_helper_debug_enter,
162 .fb_debug_leave = drm_fb_helper_debug_leave,
165 static int astfb_create_object(struct ast_fbdev *afbdev,
166 const struct drm_mode_fb_cmd2 *mode_cmd,
167 struct drm_gem_object **gobj_p)
169 struct drm_device *dev = afbdev->helper.dev;
170 u32 size;
171 struct drm_gem_object *gobj;
172 int ret = 0;
174 size = mode_cmd->pitches[0] * mode_cmd->height;
175 ret = ast_gem_create(dev, size, true, &gobj);
176 if (ret)
177 return ret;
179 *gobj_p = gobj;
180 return ret;
183 static int astfb_create(struct drm_fb_helper *helper,
184 struct drm_fb_helper_surface_size *sizes)
186 struct ast_fbdev *afbdev =
187 container_of(helper, struct ast_fbdev, helper);
188 struct drm_device *dev = afbdev->helper.dev;
189 struct drm_mode_fb_cmd2 mode_cmd;
190 struct drm_framebuffer *fb;
191 struct fb_info *info;
192 int size, ret;
193 void *sysram;
194 struct drm_gem_object *gobj = NULL;
195 struct ast_bo *bo = NULL;
196 mode_cmd.width = sizes->surface_width;
197 mode_cmd.height = sizes->surface_height;
198 mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
200 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
201 sizes->surface_depth);
203 size = mode_cmd.pitches[0] * mode_cmd.height;
205 ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
206 if (ret) {
207 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
208 return ret;
210 bo = gem_to_ast_bo(gobj);
212 sysram = vmalloc(size);
213 if (!sysram)
214 return -ENOMEM;
216 info = drm_fb_helper_alloc_fbi(helper);
217 if (IS_ERR(info)) {
218 ret = PTR_ERR(info);
219 goto err_free_vram;
221 info->par = afbdev;
223 ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
224 if (ret)
225 goto err_release_fbi;
227 afbdev->sysram = sysram;
228 afbdev->size = size;
230 fb = &afbdev->afb.base;
231 afbdev->helper.fb = fb;
233 strcpy(info->fix.id, "astdrmfb");
235 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
236 info->fbops = &astfb_ops;
238 info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
239 info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
241 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
242 drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height);
244 info->screen_base = sysram;
245 info->screen_size = size;
247 info->pixmap.flags = FB_PIXMAP_SYSTEM;
249 DRM_DEBUG_KMS("allocated %dx%d\n",
250 fb->width, fb->height);
252 return 0;
254 err_release_fbi:
255 drm_fb_helper_release_fbi(helper);
256 err_free_vram:
257 vfree(afbdev->sysram);
258 return ret;
261 static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
262 u16 blue, int regno)
264 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
265 ast_crtc->lut_r[regno] = red >> 8;
266 ast_crtc->lut_g[regno] = green >> 8;
267 ast_crtc->lut_b[regno] = blue >> 8;
270 static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
271 u16 *blue, int regno)
273 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
274 *red = ast_crtc->lut_r[regno] << 8;
275 *green = ast_crtc->lut_g[regno] << 8;
276 *blue = ast_crtc->lut_b[regno] << 8;
279 static const struct drm_fb_helper_funcs ast_fb_helper_funcs = {
280 .gamma_set = ast_fb_gamma_set,
281 .gamma_get = ast_fb_gamma_get,
282 .fb_probe = astfb_create,
285 static void ast_fbdev_destroy(struct drm_device *dev,
286 struct ast_fbdev *afbdev)
288 struct ast_framebuffer *afb = &afbdev->afb;
290 drm_fb_helper_unregister_fbi(&afbdev->helper);
291 drm_fb_helper_release_fbi(&afbdev->helper);
293 if (afb->obj) {
294 drm_gem_object_unreference_unlocked(afb->obj);
295 afb->obj = NULL;
297 drm_fb_helper_fini(&afbdev->helper);
299 vfree(afbdev->sysram);
300 drm_framebuffer_unregister_private(&afb->base);
301 drm_framebuffer_cleanup(&afb->base);
304 int ast_fbdev_init(struct drm_device *dev)
306 struct ast_private *ast = dev->dev_private;
307 struct ast_fbdev *afbdev;
308 int ret;
310 afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
311 if (!afbdev)
312 return -ENOMEM;
314 ast->fbdev = afbdev;
315 spin_lock_init(&afbdev->dirty_lock);
317 drm_fb_helper_prepare(dev, &afbdev->helper, &ast_fb_helper_funcs);
319 ret = drm_fb_helper_init(dev, &afbdev->helper,
320 1, 1);
321 if (ret)
322 goto free;
324 ret = drm_fb_helper_single_add_all_connectors(&afbdev->helper);
325 if (ret)
326 goto fini;
328 /* disable all the possible outputs/crtcs before entering KMS mode */
329 drm_helper_disable_unused_functions(dev);
331 ret = drm_fb_helper_initial_config(&afbdev->helper, 32);
332 if (ret)
333 goto fini;
335 return 0;
337 fini:
338 drm_fb_helper_fini(&afbdev->helper);
339 free:
340 kfree(afbdev);
341 return ret;
344 void ast_fbdev_fini(struct drm_device *dev)
346 struct ast_private *ast = dev->dev_private;
348 if (!ast->fbdev)
349 return;
351 ast_fbdev_destroy(dev, ast->fbdev);
352 kfree(ast->fbdev);
353 ast->fbdev = NULL;
356 void ast_fbdev_set_suspend(struct drm_device *dev, int state)
358 struct ast_private *ast = dev->dev_private;
360 if (!ast->fbdev)
361 return;
363 drm_fb_helper_set_suspend(&ast->fbdev->helper, state);
366 void ast_fbdev_set_base(struct ast_private *ast, unsigned long gpu_addr)
368 ast->fbdev->helper.fbdev->fix.smem_start =
369 ast->fbdev->helper.fbdev->apertures->ranges[0].base + gpu_addr;
370 ast->fbdev->helper.fbdev->fix.smem_len = ast->vram_size - gpu_addr;