drm/ast: Replace mapping code with drm_gem_vram_{kmap/kunmap}()
[sfrench/cifs-2.6.git] / drivers / gpu / drm / ast / ast_fb.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
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:
11  *
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.
19  *
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.
23  *
24  */
25 /*
26  * Authors: Dave Airlie <airlied@redhat.com>
27  */
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/init.h>
37
38
39 #include <drm/drmP.h>
40 #include <drm/drm_crtc.h>
41 #include <drm/drm_fb_helper.h>
42 #include <drm/drm_util.h>
43 #include <drm/drm_crtc_helper.h>
44
45 #include "ast_drv.h"
46
47 static void ast_dirty_update(struct ast_fbdev *afbdev,
48                              int x, int y, int width, int height)
49 {
50         int i;
51         struct drm_gem_object *obj;
52         struct drm_gem_vram_object *gbo;
53         int src_offset, dst_offset;
54         int bpp = afbdev->afb.base.format->cpp[0];
55         int ret = -EBUSY;
56         u8 *dst;
57         bool unmap = false;
58         bool store_for_later = false;
59         int x2, y2;
60         unsigned long flags;
61
62         obj = afbdev->afb.obj;
63         gbo = drm_gem_vram_of_gem(obj);
64
65         /*
66          * try and reserve the BO, if we fail with busy
67          * then the BO is being moved and we should
68          * store up the damage until later.
69          */
70         if (drm_can_sleep())
71                 ret = drm_gem_vram_reserve(gbo, true);
72         if (ret) {
73                 if (ret != -EBUSY)
74                         return;
75
76                 store_for_later = true;
77         }
78
79         x2 = x + width - 1;
80         y2 = y + height - 1;
81         spin_lock_irqsave(&afbdev->dirty_lock, flags);
82
83         if (afbdev->y1 < y)
84                 y = afbdev->y1;
85         if (afbdev->y2 > y2)
86                 y2 = afbdev->y2;
87         if (afbdev->x1 < x)
88                 x = afbdev->x1;
89         if (afbdev->x2 > x2)
90                 x2 = afbdev->x2;
91
92         if (store_for_later) {
93                 afbdev->x1 = x;
94                 afbdev->x2 = x2;
95                 afbdev->y1 = y;
96                 afbdev->y2 = y2;
97                 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
98                 return;
99         }
100
101         afbdev->x1 = afbdev->y1 = INT_MAX;
102         afbdev->x2 = afbdev->y2 = 0;
103         spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
104
105         dst = drm_gem_vram_kmap(gbo, false, NULL);
106         if (IS_ERR(dst)) {
107                 DRM_ERROR("failed to kmap fb updates\n");
108                 goto out;
109         } else if (!dst) {
110                 dst = drm_gem_vram_kmap(gbo, true, NULL);
111                 if (IS_ERR(dst)) {
112                         DRM_ERROR("failed to kmap fb updates\n");
113                         goto out;
114                 }
115                 unmap = true;
116         }
117
118         for (i = y; i <= y2; i++) {
119                 /* assume equal stride for now */
120                 src_offset = dst_offset =
121                         i * afbdev->afb.base.pitches[0] + (x * bpp);
122                 memcpy_toio(dst + dst_offset, afbdev->sysram + src_offset,
123                             (x2 - x + 1) * bpp);
124         }
125
126         if (unmap)
127                 drm_gem_vram_kunmap(gbo);
128
129 out:
130         drm_gem_vram_unreserve(gbo);
131 }
132
133 static void ast_fillrect(struct fb_info *info,
134                          const struct fb_fillrect *rect)
135 {
136         struct ast_fbdev *afbdev = info->par;
137         drm_fb_helper_sys_fillrect(info, rect);
138         ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
139                          rect->height);
140 }
141
142 static void ast_copyarea(struct fb_info *info,
143                          const struct fb_copyarea *area)
144 {
145         struct ast_fbdev *afbdev = info->par;
146         drm_fb_helper_sys_copyarea(info, area);
147         ast_dirty_update(afbdev, area->dx, area->dy, area->width,
148                          area->height);
149 }
150
151 static void ast_imageblit(struct fb_info *info,
152                           const struct fb_image *image)
153 {
154         struct ast_fbdev *afbdev = info->par;
155         drm_fb_helper_sys_imageblit(info, image);
156         ast_dirty_update(afbdev, image->dx, image->dy, image->width,
157                          image->height);
158 }
159
160 static struct fb_ops astfb_ops = {
161         .owner = THIS_MODULE,
162         .fb_check_var = drm_fb_helper_check_var,
163         .fb_set_par = drm_fb_helper_set_par,
164         .fb_fillrect = ast_fillrect,
165         .fb_copyarea = ast_copyarea,
166         .fb_imageblit = ast_imageblit,
167         .fb_pan_display = drm_fb_helper_pan_display,
168         .fb_blank = drm_fb_helper_blank,
169         .fb_setcmap = drm_fb_helper_setcmap,
170         .fb_debug_enter = drm_fb_helper_debug_enter,
171         .fb_debug_leave = drm_fb_helper_debug_leave,
172 };
173
174 static int astfb_create_object(struct ast_fbdev *afbdev,
175                                const struct drm_mode_fb_cmd2 *mode_cmd,
176                                struct drm_gem_object **gobj_p)
177 {
178         struct drm_device *dev = afbdev->helper.dev;
179         u32 size;
180         struct drm_gem_object *gobj;
181         int ret = 0;
182
183         size = mode_cmd->pitches[0] * mode_cmd->height;
184         ret = ast_gem_create(dev, size, true, &gobj);
185         if (ret)
186                 return ret;
187
188         *gobj_p = gobj;
189         return ret;
190 }
191
192 static int astfb_create(struct drm_fb_helper *helper,
193                         struct drm_fb_helper_surface_size *sizes)
194 {
195         struct ast_fbdev *afbdev =
196                 container_of(helper, struct ast_fbdev, helper);
197         struct drm_device *dev = afbdev->helper.dev;
198         struct drm_mode_fb_cmd2 mode_cmd;
199         struct drm_framebuffer *fb;
200         struct fb_info *info;
201         int size, ret;
202         void *sysram;
203         struct drm_gem_object *gobj = NULL;
204         mode_cmd.width = sizes->surface_width;
205         mode_cmd.height = sizes->surface_height;
206         mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
207
208         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
209                                                           sizes->surface_depth);
210
211         size = mode_cmd.pitches[0] * mode_cmd.height;
212
213         ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
214         if (ret) {
215                 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
216                 return ret;
217         }
218
219         sysram = vmalloc(size);
220         if (!sysram)
221                 return -ENOMEM;
222
223         info = drm_fb_helper_alloc_fbi(helper);
224         if (IS_ERR(info)) {
225                 ret = PTR_ERR(info);
226                 goto out;
227         }
228         ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
229         if (ret)
230                 goto out;
231
232         afbdev->sysram = sysram;
233         afbdev->size = size;
234
235         fb = &afbdev->afb.base;
236         afbdev->helper.fb = fb;
237
238         info->fbops = &astfb_ops;
239
240         info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
241         info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
242
243         drm_fb_helper_fill_info(info, &afbdev->helper, sizes);
244
245         info->screen_base = sysram;
246         info->screen_size = size;
247
248         info->pixmap.flags = FB_PIXMAP_SYSTEM;
249
250         DRM_DEBUG_KMS("allocated %dx%d\n",
251                       fb->width, fb->height);
252
253         return 0;
254
255 out:
256         vfree(sysram);
257         return ret;
258 }
259
260 static const struct drm_fb_helper_funcs ast_fb_helper_funcs = {
261         .fb_probe = astfb_create,
262 };
263
264 static void ast_fbdev_destroy(struct drm_device *dev,
265                               struct ast_fbdev *afbdev)
266 {
267         struct ast_framebuffer *afb = &afbdev->afb;
268
269         drm_helper_force_disable_all(dev);
270         drm_fb_helper_unregister_fbi(&afbdev->helper);
271
272         if (afb->obj) {
273                 drm_gem_object_put_unlocked(afb->obj);
274                 afb->obj = NULL;
275         }
276         drm_fb_helper_fini(&afbdev->helper);
277
278         vfree(afbdev->sysram);
279         drm_framebuffer_unregister_private(&afb->base);
280         drm_framebuffer_cleanup(&afb->base);
281 }
282
283 int ast_fbdev_init(struct drm_device *dev)
284 {
285         struct ast_private *ast = dev->dev_private;
286         struct ast_fbdev *afbdev;
287         int ret;
288
289         afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
290         if (!afbdev)
291                 return -ENOMEM;
292
293         ast->fbdev = afbdev;
294         spin_lock_init(&afbdev->dirty_lock);
295
296         drm_fb_helper_prepare(dev, &afbdev->helper, &ast_fb_helper_funcs);
297
298         ret = drm_fb_helper_init(dev, &afbdev->helper, 1);
299         if (ret)
300                 goto free;
301
302         ret = drm_fb_helper_single_add_all_connectors(&afbdev->helper);
303         if (ret)
304                 goto fini;
305
306         /* disable all the possible outputs/crtcs before entering KMS mode */
307         drm_helper_disable_unused_functions(dev);
308
309         ret = drm_fb_helper_initial_config(&afbdev->helper, 32);
310         if (ret)
311                 goto fini;
312
313         return 0;
314
315 fini:
316         drm_fb_helper_fini(&afbdev->helper);
317 free:
318         kfree(afbdev);
319         return ret;
320 }
321
322 void ast_fbdev_fini(struct drm_device *dev)
323 {
324         struct ast_private *ast = dev->dev_private;
325
326         if (!ast->fbdev)
327                 return;
328
329         ast_fbdev_destroy(dev, ast->fbdev);
330         kfree(ast->fbdev);
331         ast->fbdev = NULL;
332 }
333
334 void ast_fbdev_set_suspend(struct drm_device *dev, int state)
335 {
336         struct ast_private *ast = dev->dev_private;
337
338         if (!ast->fbdev)
339                 return;
340
341         drm_fb_helper_set_suspend(&ast->fbdev->helper, state);
342 }
343
344 void ast_fbdev_set_base(struct ast_private *ast, unsigned long gpu_addr)
345 {
346         ast->fbdev->helper.fbdev->fix.smem_start =
347                 ast->fbdev->helper.fbdev->apertures->ranges[0].base + gpu_addr;
348         ast->fbdev->helper.fbdev->fix.smem_len = ast->vram_size - gpu_addr;
349 }