blob: 5d0ffab411a80b4a267ed24a26e2d2249c6b3f09 [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>
Dave Airlie312fec12012-02-29 13:40:04 +000036#include <linux/init.h>
37
38
David Howells760285e2012-10-02 18:01:07 +010039#include <drm/drmP.h>
40#include <drm/drm_crtc.h>
41#include <drm/drm_fb_helper.h>
Daniel Vetter76a39db2013-01-20 23:12:54 +010042#include <drm/drm_crtc_helper.h>
Dave Airlie312fec12012-02-29 13:40:04 +000043#include "ast_drv.h"
44
45static void ast_dirty_update(struct ast_fbdev *afbdev,
46 int x, int y, int width, int height)
47{
48 int i;
49 struct drm_gem_object *obj;
50 struct ast_bo *bo;
51 int src_offset, dst_offset;
Ville Syrjälä272725c2016-12-14 23:32:20 +020052 int bpp = afbdev->afb.base.format->cpp[0];
Maarten Lankhorst8ade2b82013-06-27 13:38:26 +020053 int ret = -EBUSY;
Dave Airlie312fec12012-02-29 13:40:04 +000054 bool unmap = false;
Dave Airlie306373b2013-05-02 02:40:25 -040055 bool store_for_later = false;
56 int x2, y2;
57 unsigned long flags;
Dave Airlie312fec12012-02-29 13:40:04 +000058
59 obj = afbdev->afb.obj;
60 bo = gem_to_ast_bo(obj);
61
Dave Airlie306373b2013-05-02 02:40:25 -040062 /*
63 * try and reserve the BO, if we fail with busy
64 * then the BO is being moved and we should
65 * store up the damage until later.
66 */
Dave Airlie8b7ad1b2014-02-05 14:47:45 +100067 if (drm_can_sleep())
Maarten Lankhorst8ade2b82013-06-27 13:38:26 +020068 ret = ast_bo_reserve(bo, true);
Dave Airlie312fec12012-02-29 13:40:04 +000069 if (ret) {
Dave Airlie306373b2013-05-02 02:40:25 -040070 if (ret != -EBUSY)
71 return;
72
73 store_for_later = true;
74 }
75
76 x2 = x + width - 1;
77 y2 = y + height - 1;
78 spin_lock_irqsave(&afbdev->dirty_lock, flags);
79
80 if (afbdev->y1 < y)
81 y = afbdev->y1;
82 if (afbdev->y2 > y2)
83 y2 = afbdev->y2;
84 if (afbdev->x1 < x)
85 x = afbdev->x1;
86 if (afbdev->x2 > x2)
87 x2 = afbdev->x2;
88
89 if (store_for_later) {
90 afbdev->x1 = x;
91 afbdev->x2 = x2;
92 afbdev->y1 = y;
93 afbdev->y2 = y2;
94 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
Dave Airlie312fec12012-02-29 13:40:04 +000095 return;
96 }
97
Dave Airlie306373b2013-05-02 02:40:25 -040098 afbdev->x1 = afbdev->y1 = INT_MAX;
99 afbdev->x2 = afbdev->y2 = 0;
100 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
101
Dave Airlie312fec12012-02-29 13:40:04 +0000102 if (!bo->kmap.virtual) {
103 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
104 if (ret) {
105 DRM_ERROR("failed to kmap fb updates\n");
106 ast_bo_unreserve(bo);
107 return;
108 }
109 unmap = true;
110 }
Dave Airlie306373b2013-05-02 02:40:25 -0400111 for (i = y; i <= y2; i++) {
Dave Airlie312fec12012-02-29 13:40:04 +0000112 /* assume equal stride for now */
113 src_offset = dst_offset = i * afbdev->afb.base.pitches[0] + (x * bpp);
Dave Airlie306373b2013-05-02 02:40:25 -0400114 memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, (x2 - x + 1) * bpp);
Dave Airlie312fec12012-02-29 13:40:04 +0000115
116 }
117 if (unmap)
118 ttm_bo_kunmap(&bo->kmap);
119
120 ast_bo_unreserve(bo);
121}
122
123static void ast_fillrect(struct fb_info *info,
124 const struct fb_fillrect *rect)
125{
126 struct ast_fbdev *afbdev = info->par;
Archit Taneja990e8442015-07-22 14:58:05 +0530127 drm_fb_helper_sys_fillrect(info, rect);
Dave Airlie312fec12012-02-29 13:40:04 +0000128 ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
129 rect->height);
130}
131
132static void ast_copyarea(struct fb_info *info,
133 const struct fb_copyarea *area)
134{
135 struct ast_fbdev *afbdev = info->par;
Archit Taneja990e8442015-07-22 14:58:05 +0530136 drm_fb_helper_sys_copyarea(info, area);
Dave Airlie312fec12012-02-29 13:40:04 +0000137 ast_dirty_update(afbdev, area->dx, area->dy, area->width,
138 area->height);
139}
140
141static void ast_imageblit(struct fb_info *info,
142 const struct fb_image *image)
143{
144 struct ast_fbdev *afbdev = info->par;
Archit Taneja990e8442015-07-22 14:58:05 +0530145 drm_fb_helper_sys_imageblit(info, image);
Dave Airlie312fec12012-02-29 13:40:04 +0000146 ast_dirty_update(afbdev, image->dx, image->dy, image->width,
147 image->height);
148}
149
150static struct fb_ops astfb_ops = {
151 .owner = THIS_MODULE,
152 .fb_check_var = drm_fb_helper_check_var,
153 .fb_set_par = drm_fb_helper_set_par,
154 .fb_fillrect = ast_fillrect,
155 .fb_copyarea = ast_copyarea,
156 .fb_imageblit = ast_imageblit,
157 .fb_pan_display = drm_fb_helper_pan_display,
158 .fb_blank = drm_fb_helper_blank,
159 .fb_setcmap = drm_fb_helper_setcmap,
160 .fb_debug_enter = drm_fb_helper_debug_enter,
161 .fb_debug_leave = drm_fb_helper_debug_leave,
162};
163
164static int astfb_create_object(struct ast_fbdev *afbdev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200165 const struct drm_mode_fb_cmd2 *mode_cmd,
Dave Airlie312fec12012-02-29 13:40:04 +0000166 struct drm_gem_object **gobj_p)
167{
168 struct drm_device *dev = afbdev->helper.dev;
Dave Airlie312fec12012-02-29 13:40:04 +0000169 u32 size;
170 struct drm_gem_object *gobj;
Dave Airlie312fec12012-02-29 13:40:04 +0000171 int ret = 0;
Dave Airlie312fec12012-02-29 13:40:04 +0000172
173 size = mode_cmd->pitches[0] * mode_cmd->height;
174 ret = ast_gem_create(dev, size, true, &gobj);
175 if (ret)
176 return ret;
177
178 *gobj_p = gobj;
179 return ret;
180}
181
Daniel Vettercd5428a2013-01-21 23:42:49 +0100182static int astfb_create(struct drm_fb_helper *helper,
Dave Airlie312fec12012-02-29 13:40:04 +0000183 struct drm_fb_helper_surface_size *sizes)
184{
Fabian Frederick0d634f62014-09-14 18:40:21 +0200185 struct ast_fbdev *afbdev =
186 container_of(helper, struct ast_fbdev, helper);
Dave Airlie312fec12012-02-29 13:40:04 +0000187 struct drm_device *dev = afbdev->helper.dev;
188 struct drm_mode_fb_cmd2 mode_cmd;
189 struct drm_framebuffer *fb;
190 struct fb_info *info;
191 int size, ret;
Dave Airlie312fec12012-02-29 13:40:04 +0000192 void *sysram;
193 struct drm_gem_object *gobj = NULL;
194 struct ast_bo *bo = NULL;
195 mode_cmd.width = sizes->surface_width;
196 mode_cmd.height = sizes->surface_height;
197 mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
198
199 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
200 sizes->surface_depth);
201
202 size = mode_cmd.pitches[0] * mode_cmd.height;
203
204 ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
205 if (ret) {
206 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
207 return ret;
208 }
209 bo = gem_to_ast_bo(gobj);
210
211 sysram = vmalloc(size);
212 if (!sysram)
213 return -ENOMEM;
214
Archit Taneja990e8442015-07-22 14:58:05 +0530215 info = drm_fb_helper_alloc_fbi(helper);
216 if (IS_ERR(info)) {
217 ret = PTR_ERR(info);
218 goto err_free_vram;
Dave Airlie312fec12012-02-29 13:40:04 +0000219 }
220 info->par = afbdev;
221
222 ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
223 if (ret)
Archit Taneja990e8442015-07-22 14:58:05 +0530224 goto err_release_fbi;
Dave Airlie312fec12012-02-29 13:40:04 +0000225
226 afbdev->sysram = sysram;
227 afbdev->size = size;
228
229 fb = &afbdev->afb.base;
230 afbdev->helper.fb = fb;
Dave Airlie312fec12012-02-29 13:40:04 +0000231
232 strcpy(info->fix.id, "astdrmfb");
233
234 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
235 info->fbops = &astfb_ops;
236
Dave Airlie312fec12012-02-29 13:40:04 +0000237 info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
238 info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
239
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200240 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
Dave Airlie312fec12012-02-29 13:40:04 +0000241 drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height);
242
243 info->screen_base = sysram;
244 info->screen_size = size;
245
246 info->pixmap.flags = FB_PIXMAP_SYSTEM;
247
248 DRM_DEBUG_KMS("allocated %dx%d\n",
249 fb->width, fb->height);
250
251 return 0;
Archit Taneja990e8442015-07-22 14:58:05 +0530252
253err_release_fbi:
254 drm_fb_helper_release_fbi(helper);
255err_free_vram:
Andrew Donnellan554dd692016-11-14 14:03:59 +1100256 vfree(sysram);
Dave Airlie312fec12012-02-29 13:40:04 +0000257 return ret;
258}
259
260static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
261 u16 blue, int regno)
262{
263 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
264 ast_crtc->lut_r[regno] = red >> 8;
265 ast_crtc->lut_g[regno] = green >> 8;
266 ast_crtc->lut_b[regno] = blue >> 8;
267}
268
269static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
270 u16 *blue, int regno)
271{
272 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
273 *red = ast_crtc->lut_r[regno] << 8;
274 *green = ast_crtc->lut_g[regno] << 8;
275 *blue = ast_crtc->lut_b[regno] << 8;
276}
277
Thierry Reding3a493872014-06-27 17:19:23 +0200278static const struct drm_fb_helper_funcs ast_fb_helper_funcs = {
Dave Airlie312fec12012-02-29 13:40:04 +0000279 .gamma_set = ast_fb_gamma_set,
280 .gamma_get = ast_fb_gamma_get,
Daniel Vettercd5428a2013-01-21 23:42:49 +0100281 .fb_probe = astfb_create,
Dave Airlie312fec12012-02-29 13:40:04 +0000282};
283
284static void ast_fbdev_destroy(struct drm_device *dev,
285 struct ast_fbdev *afbdev)
286{
Dave Airlie312fec12012-02-29 13:40:04 +0000287 struct ast_framebuffer *afb = &afbdev->afb;
Archit Taneja990e8442015-07-22 14:58:05 +0530288
289 drm_fb_helper_unregister_fbi(&afbdev->helper);
290 drm_fb_helper_release_fbi(&afbdev->helper);
Dave Airlie312fec12012-02-29 13:40:04 +0000291
292 if (afb->obj) {
293 drm_gem_object_unreference_unlocked(afb->obj);
294 afb->obj = NULL;
295 }
296 drm_fb_helper_fini(&afbdev->helper);
297
298 vfree(afbdev->sysram);
Daniel Vetter36206362012-12-10 20:42:17 +0100299 drm_framebuffer_unregister_private(&afb->base);
Dave Airlie312fec12012-02-29 13:40:04 +0000300 drm_framebuffer_cleanup(&afb->base);
301}
302
303int ast_fbdev_init(struct drm_device *dev)
304{
305 struct ast_private *ast = dev->dev_private;
306 struct ast_fbdev *afbdev;
307 int ret;
308
309 afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
310 if (!afbdev)
311 return -ENOMEM;
312
313 ast->fbdev = afbdev;
Dave Airlie306373b2013-05-02 02:40:25 -0400314 spin_lock_init(&afbdev->dirty_lock);
Thierry Reding10a23102014-06-27 17:19:24 +0200315
316 drm_fb_helper_prepare(dev, &afbdev->helper, &ast_fb_helper_funcs);
317
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200318 ret = drm_fb_helper_init(dev, &afbdev->helper, 1);
Thierry Reding01934c22014-12-19 11:21:32 +0100319 if (ret)
320 goto free;
Dave Airlie312fec12012-02-29 13:40:04 +0000321
Thierry Reding01934c22014-12-19 11:21:32 +0100322 ret = drm_fb_helper_single_add_all_connectors(&afbdev->helper);
323 if (ret)
324 goto fini;
Daniel Vetter76a39db2013-01-20 23:12:54 +0100325
326 /* disable all the possible outputs/crtcs before entering KMS mode */
327 drm_helper_disable_unused_functions(dev);
328
Thierry Reding01934c22014-12-19 11:21:32 +0100329 ret = drm_fb_helper_initial_config(&afbdev->helper, 32);
330 if (ret)
331 goto fini;
332
Dave Airlie312fec12012-02-29 13:40:04 +0000333 return 0;
Thierry Reding01934c22014-12-19 11:21:32 +0100334
335fini:
336 drm_fb_helper_fini(&afbdev->helper);
337free:
338 kfree(afbdev);
339 return ret;
Dave Airlie312fec12012-02-29 13:40:04 +0000340}
341
342void ast_fbdev_fini(struct drm_device *dev)
343{
344 struct ast_private *ast = dev->dev_private;
345
346 if (!ast->fbdev)
347 return;
348
349 ast_fbdev_destroy(dev, ast->fbdev);
350 kfree(ast->fbdev);
351 ast->fbdev = NULL;
352}
353
354void ast_fbdev_set_suspend(struct drm_device *dev, int state)
355{
356 struct ast_private *ast = dev->dev_private;
357
358 if (!ast->fbdev)
359 return;
360
Archit Taneja990e8442015-07-22 14:58:05 +0530361 drm_fb_helper_set_suspend(&ast->fbdev->helper, state);
Dave Airlie312fec12012-02-29 13:40:04 +0000362}
Egbert Eich28fb4cb2014-06-11 14:59:55 +0200363
364void ast_fbdev_set_base(struct ast_private *ast, unsigned long gpu_addr)
365{
366 ast->fbdev->helper.fbdev->fix.smem_start =
367 ast->fbdev->helper.fbdev->apertures->ranges[0].base + gpu_addr;
368 ast->fbdev->helper.fbdev->fix.smem_len = ast->vram_size - gpu_addr;
369}