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
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>
33 #include <linux/tty.h>
34 #include <linux/sysrq.h>
35 #include <linux/delay.h>
36 #include <linux/init.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>
47 static void ast_dirty_update(struct ast_fbdev *afbdev,
48 int x, int y, int width, int height)
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];
58 bool store_for_later = false;
62 obj = afbdev->afb.obj;
63 gbo = drm_gem_vram_of_gem(obj);
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.
71 ret = drm_gem_vram_reserve(gbo, true);
76 store_for_later = true;
81 spin_lock_irqsave(&afbdev->dirty_lock, flags);
92 if (store_for_later) {
97 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
101 afbdev->x1 = afbdev->y1 = INT_MAX;
102 afbdev->x2 = afbdev->y2 = 0;
103 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
105 dst = drm_gem_vram_kmap(gbo, false, NULL);
107 DRM_ERROR("failed to kmap fb updates\n");
110 dst = drm_gem_vram_kmap(gbo, true, NULL);
112 DRM_ERROR("failed to kmap fb updates\n");
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,
127 drm_gem_vram_kunmap(gbo);
130 drm_gem_vram_unreserve(gbo);
133 static void ast_fillrect(struct fb_info *info,
134 const struct fb_fillrect *rect)
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,
142 static void ast_copyarea(struct fb_info *info,
143 const struct fb_copyarea *area)
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,
151 static void ast_imageblit(struct fb_info *info,
152 const struct fb_image *image)
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,
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,
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)
178 struct drm_device *dev = afbdev->helper.dev;
180 struct drm_gem_object *gobj;
183 size = mode_cmd->pitches[0] * mode_cmd->height;
184 ret = ast_gem_create(dev, size, true, &gobj);
192 static int astfb_create(struct drm_fb_helper *helper,
193 struct drm_fb_helper_surface_size *sizes)
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;
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);
208 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
209 sizes->surface_depth);
211 size = mode_cmd.pitches[0] * mode_cmd.height;
213 ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
215 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
219 sysram = vmalloc(size);
223 info = drm_fb_helper_alloc_fbi(helper);
228 ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
232 afbdev->sysram = sysram;
235 fb = &afbdev->afb.base;
236 afbdev->helper.fb = fb;
238 info->fbops = &astfb_ops;
240 info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
241 info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
243 drm_fb_helper_fill_info(info, &afbdev->helper, sizes);
245 info->screen_base = sysram;
246 info->screen_size = size;
248 info->pixmap.flags = FB_PIXMAP_SYSTEM;
250 DRM_DEBUG_KMS("allocated %dx%d\n",
251 fb->width, fb->height);
260 static const struct drm_fb_helper_funcs ast_fb_helper_funcs = {
261 .fb_probe = astfb_create,
264 static void ast_fbdev_destroy(struct drm_device *dev,
265 struct ast_fbdev *afbdev)
267 struct ast_framebuffer *afb = &afbdev->afb;
269 drm_helper_force_disable_all(dev);
270 drm_fb_helper_unregister_fbi(&afbdev->helper);
273 drm_gem_object_put_unlocked(afb->obj);
276 drm_fb_helper_fini(&afbdev->helper);
278 vfree(afbdev->sysram);
279 drm_framebuffer_unregister_private(&afb->base);
280 drm_framebuffer_cleanup(&afb->base);
283 int ast_fbdev_init(struct drm_device *dev)
285 struct ast_private *ast = dev->dev_private;
286 struct ast_fbdev *afbdev;
289 afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
294 spin_lock_init(&afbdev->dirty_lock);
296 drm_fb_helper_prepare(dev, &afbdev->helper, &ast_fb_helper_funcs);
298 ret = drm_fb_helper_init(dev, &afbdev->helper, 1);
302 ret = drm_fb_helper_single_add_all_connectors(&afbdev->helper);
306 /* disable all the possible outputs/crtcs before entering KMS mode */
307 drm_helper_disable_unused_functions(dev);
309 ret = drm_fb_helper_initial_config(&afbdev->helper, 32);
316 drm_fb_helper_fini(&afbdev->helper);
322 void ast_fbdev_fini(struct drm_device *dev)
324 struct ast_private *ast = dev->dev_private;
329 ast_fbdev_destroy(dev, ast->fbdev);
334 void ast_fbdev_set_suspend(struct drm_device *dev, int state)
336 struct ast_private *ast = dev->dev_private;
341 drm_fb_helper_set_suspend(&ast->fbdev->helper, state);
344 void ast_fbdev_set_base(struct ast_private *ast, unsigned long gpu_addr)
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;