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