blob: 5c60ae524c454952316f9d8f8864cf8e52ce8cc4 [file] [log] [blame]
Dave Airlie312fec12012-02-29 13:40:04 +00001/*
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/fb.h>
37#include <linux/init.h>
38
39
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drmP.h>
41#include <drm/drm_crtc.h>
42#include <drm/drm_fb_helper.h>
Daniel Vetter76a39db2013-01-20 23:12:54 +010043#include <drm/drm_crtc_helper.h>
Dave Airlie312fec12012-02-29 13:40:04 +000044#include "ast_drv.h"
45
46static void ast_dirty_update(struct ast_fbdev *afbdev,
47 int x, int y, int width, int height)
48{
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;
Maarten Lankhorst8ade2b82013-06-27 13:38:26 +020054 int ret = -EBUSY;
Dave Airlie312fec12012-02-29 13:40:04 +000055 bool unmap = false;
Dave Airlie306373b2013-05-02 02:40:25 -040056 bool store_for_later = false;
57 int x2, y2;
58 unsigned long flags;
Dave Airlie312fec12012-02-29 13:40:04 +000059
60 obj = afbdev->afb.obj;
61 bo = gem_to_ast_bo(obj);
62
Dave Airlie306373b2013-05-02 02:40:25 -040063 /*
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.
67 */
Dave Airlie8b7ad1b2014-02-05 14:47:45 +100068 if (drm_can_sleep())
Maarten Lankhorst8ade2b82013-06-27 13:38:26 +020069 ret = ast_bo_reserve(bo, true);
Dave Airlie312fec12012-02-29 13:40:04 +000070 if (ret) {
Dave Airlie306373b2013-05-02 02:40:25 -040071 if (ret != -EBUSY)
72 return;
73
74 store_for_later = true;
75 }
76
77 x2 = x + width - 1;
78 y2 = y + height - 1;
79 spin_lock_irqsave(&afbdev->dirty_lock, flags);
80
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;
89
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);
Dave Airlie312fec12012-02-29 13:40:04 +000096 return;
97 }
98
Dave Airlie306373b2013-05-02 02:40:25 -040099 afbdev->x1 = afbdev->y1 = INT_MAX;
100 afbdev->x2 = afbdev->y2 = 0;
101 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
102
Dave Airlie312fec12012-02-29 13:40:04 +0000103 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;
109 }
110 unmap = true;
111 }
Dave Airlie306373b2013-05-02 02:40:25 -0400112 for (i = y; i <= y2; i++) {
Dave Airlie312fec12012-02-29 13:40:04 +0000113 /* assume equal stride for now */
114 src_offset = dst_offset = i * afbdev->afb.base.pitches[0] + (x * bpp);
Dave Airlie306373b2013-05-02 02:40:25 -0400115 memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, (x2 - x + 1) * bpp);
Dave Airlie312fec12012-02-29 13:40:04 +0000116
117 }
118 if (unmap)
119 ttm_bo_kunmap(&bo->kmap);
120
121 ast_bo_unreserve(bo);
122}
123
124static void ast_fillrect(struct fb_info *info,
125 const struct fb_fillrect *rect)
126{
127 struct ast_fbdev *afbdev = info->par;
128 sys_fillrect(info, rect);
129 ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
130 rect->height);
131}
132
133static void ast_copyarea(struct fb_info *info,
134 const struct fb_copyarea *area)
135{
136 struct ast_fbdev *afbdev = info->par;
137 sys_copyarea(info, area);
138 ast_dirty_update(afbdev, area->dx, area->dy, area->width,
139 area->height);
140}
141
142static void ast_imageblit(struct fb_info *info,
143 const struct fb_image *image)
144{
145 struct ast_fbdev *afbdev = info->par;
146 sys_imageblit(info, image);
147 ast_dirty_update(afbdev, image->dx, image->dy, image->width,
148 image->height);
149}
150
151static 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,
163};
164
165static int astfb_create_object(struct ast_fbdev *afbdev,
166 struct drm_mode_fb_cmd2 *mode_cmd,
167 struct drm_gem_object **gobj_p)
168{
169 struct drm_device *dev = afbdev->helper.dev;
170 u32 bpp, depth;
171 u32 size;
172 struct drm_gem_object *gobj;
173
174 int ret = 0;
175 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
176
177 size = mode_cmd->pitches[0] * mode_cmd->height;
178 ret = ast_gem_create(dev, size, true, &gobj);
179 if (ret)
180 return ret;
181
182 *gobj_p = gobj;
183 return ret;
184}
185
Daniel Vettercd5428a2013-01-21 23:42:49 +0100186static int astfb_create(struct drm_fb_helper *helper,
Dave Airlie312fec12012-02-29 13:40:04 +0000187 struct drm_fb_helper_surface_size *sizes)
188{
Fabian Frederick0d634f62014-09-14 18:40:21 +0200189 struct ast_fbdev *afbdev =
190 container_of(helper, struct ast_fbdev, helper);
Dave Airlie312fec12012-02-29 13:40:04 +0000191 struct drm_device *dev = afbdev->helper.dev;
192 struct drm_mode_fb_cmd2 mode_cmd;
193 struct drm_framebuffer *fb;
194 struct fb_info *info;
195 int size, ret;
196 struct device *device = &dev->pdev->dev;
197 void *sysram;
198 struct drm_gem_object *gobj = NULL;
199 struct ast_bo *bo = NULL;
200 mode_cmd.width = sizes->surface_width;
201 mode_cmd.height = sizes->surface_height;
202 mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
203
204 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
205 sizes->surface_depth);
206
207 size = mode_cmd.pitches[0] * mode_cmd.height;
208
209 ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
210 if (ret) {
211 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
212 return ret;
213 }
214 bo = gem_to_ast_bo(gobj);
215
216 sysram = vmalloc(size);
217 if (!sysram)
218 return -ENOMEM;
219
220 info = framebuffer_alloc(0, device);
221 if (!info) {
222 ret = -ENOMEM;
223 goto out;
224 }
225 info->par = afbdev;
226
227 ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
228 if (ret)
229 goto out;
230
231 afbdev->sysram = sysram;
232 afbdev->size = size;
233
234 fb = &afbdev->afb.base;
235 afbdev->helper.fb = fb;
236 afbdev->helper.fbdev = info;
237
238 strcpy(info->fix.id, "astdrmfb");
239
240 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
241 info->fbops = &astfb_ops;
242
243 ret = fb_alloc_cmap(&info->cmap, 256, 0);
244 if (ret) {
245 ret = -ENOMEM;
246 goto out;
247 }
248
249 info->apertures = alloc_apertures(1);
250 if (!info->apertures) {
251 ret = -ENOMEM;
252 goto out;
253 }
254 info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
255 info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
256
257 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
258 drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height);
259
260 info->screen_base = sysram;
261 info->screen_size = size;
262
263 info->pixmap.flags = FB_PIXMAP_SYSTEM;
264
265 DRM_DEBUG_KMS("allocated %dx%d\n",
266 fb->width, fb->height);
267
268 return 0;
269out:
270 return ret;
271}
272
273static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
274 u16 blue, int regno)
275{
276 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
277 ast_crtc->lut_r[regno] = red >> 8;
278 ast_crtc->lut_g[regno] = green >> 8;
279 ast_crtc->lut_b[regno] = blue >> 8;
280}
281
282static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
283 u16 *blue, int regno)
284{
285 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
286 *red = ast_crtc->lut_r[regno] << 8;
287 *green = ast_crtc->lut_g[regno] << 8;
288 *blue = ast_crtc->lut_b[regno] << 8;
289}
290
Thierry Reding3a493872014-06-27 17:19:23 +0200291static const struct drm_fb_helper_funcs ast_fb_helper_funcs = {
Dave Airlie312fec12012-02-29 13:40:04 +0000292 .gamma_set = ast_fb_gamma_set,
293 .gamma_get = ast_fb_gamma_get,
Daniel Vettercd5428a2013-01-21 23:42:49 +0100294 .fb_probe = astfb_create,
Dave Airlie312fec12012-02-29 13:40:04 +0000295};
296
297static void ast_fbdev_destroy(struct drm_device *dev,
298 struct ast_fbdev *afbdev)
299{
300 struct fb_info *info;
301 struct ast_framebuffer *afb = &afbdev->afb;
302 if (afbdev->helper.fbdev) {
303 info = afbdev->helper.fbdev;
304 unregister_framebuffer(info);
305 if (info->cmap.len)
306 fb_dealloc_cmap(&info->cmap);
307 framebuffer_release(info);
308 }
309
310 if (afb->obj) {
311 drm_gem_object_unreference_unlocked(afb->obj);
312 afb->obj = NULL;
313 }
314 drm_fb_helper_fini(&afbdev->helper);
315
316 vfree(afbdev->sysram);
Daniel Vetter36206362012-12-10 20:42:17 +0100317 drm_framebuffer_unregister_private(&afb->base);
Dave Airlie312fec12012-02-29 13:40:04 +0000318 drm_framebuffer_cleanup(&afb->base);
319}
320
321int ast_fbdev_init(struct drm_device *dev)
322{
323 struct ast_private *ast = dev->dev_private;
324 struct ast_fbdev *afbdev;
325 int ret;
326
327 afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
328 if (!afbdev)
329 return -ENOMEM;
330
331 ast->fbdev = afbdev;
Dave Airlie306373b2013-05-02 02:40:25 -0400332 spin_lock_init(&afbdev->dirty_lock);
Thierry Reding10a23102014-06-27 17:19:24 +0200333
334 drm_fb_helper_prepare(dev, &afbdev->helper, &ast_fb_helper_funcs);
335
Dave Airlie312fec12012-02-29 13:40:04 +0000336 ret = drm_fb_helper_init(dev, &afbdev->helper,
337 1, 1);
338 if (ret) {
339 kfree(afbdev);
340 return ret;
341 }
342
343 drm_fb_helper_single_add_all_connectors(&afbdev->helper);
Daniel Vetter76a39db2013-01-20 23:12:54 +0100344
345 /* disable all the possible outputs/crtcs before entering KMS mode */
346 drm_helper_disable_unused_functions(dev);
347
Dave Airlie312fec12012-02-29 13:40:04 +0000348 drm_fb_helper_initial_config(&afbdev->helper, 32);
349 return 0;
350}
351
352void ast_fbdev_fini(struct drm_device *dev)
353{
354 struct ast_private *ast = dev->dev_private;
355
356 if (!ast->fbdev)
357 return;
358
359 ast_fbdev_destroy(dev, ast->fbdev);
360 kfree(ast->fbdev);
361 ast->fbdev = NULL;
362}
363
364void ast_fbdev_set_suspend(struct drm_device *dev, int state)
365{
366 struct ast_private *ast = dev->dev_private;
367
368 if (!ast->fbdev)
369 return;
370
371 fb_set_suspend(ast->fbdev->helper.fbdev, state);
372}