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>
37 #include <linux/init.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_fb_helper.h>
45 static void ast_dirty_update(struct ast_fbdev *afbdev,
46 int x, int y, int width, int height)
49 struct drm_gem_object *obj;
51 int src_offset, dst_offset;
52 int bpp = (afbdev->afb.base.bits_per_pixel + 7)/8;
56 obj = afbdev->afb.obj;
57 bo = gem_to_ast_bo(obj);
59 ret = ast_bo_reserve(bo, true);
61 DRM_ERROR("failed to reserve fb bo\n");
65 if (!bo->kmap.virtual) {
66 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
68 DRM_ERROR("failed to kmap fb updates\n");
74 for (i = y; i < y + height; i++) {
75 /* assume equal stride for now */
76 src_offset = dst_offset = i * afbdev->afb.base.pitches[0] + (x * bpp);
77 memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, width * bpp);
81 ttm_bo_kunmap(&bo->kmap);
86 static void ast_fillrect(struct fb_info *info,
87 const struct fb_fillrect *rect)
89 struct ast_fbdev *afbdev = info->par;
90 sys_fillrect(info, rect);
91 ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
95 static void ast_copyarea(struct fb_info *info,
96 const struct fb_copyarea *area)
98 struct ast_fbdev *afbdev = info->par;
99 sys_copyarea(info, area);
100 ast_dirty_update(afbdev, area->dx, area->dy, area->width,
104 static void ast_imageblit(struct fb_info *info,
105 const struct fb_image *image)
107 struct ast_fbdev *afbdev = info->par;
108 sys_imageblit(info, image);
109 ast_dirty_update(afbdev, image->dx, image->dy, image->width,
113 static struct fb_ops astfb_ops = {
114 .owner = THIS_MODULE,
115 .fb_check_var = drm_fb_helper_check_var,
116 .fb_set_par = drm_fb_helper_set_par,
117 .fb_fillrect = ast_fillrect,
118 .fb_copyarea = ast_copyarea,
119 .fb_imageblit = ast_imageblit,
120 .fb_pan_display = drm_fb_helper_pan_display,
121 .fb_blank = drm_fb_helper_blank,
122 .fb_setcmap = drm_fb_helper_setcmap,
123 .fb_debug_enter = drm_fb_helper_debug_enter,
124 .fb_debug_leave = drm_fb_helper_debug_leave,
127 static int astfb_create_object(struct ast_fbdev *afbdev,
128 struct drm_mode_fb_cmd2 *mode_cmd,
129 struct drm_gem_object **gobj_p)
131 struct drm_device *dev = afbdev->helper.dev;
134 struct drm_gem_object *gobj;
137 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
139 size = mode_cmd->pitches[0] * mode_cmd->height;
140 ret = ast_gem_create(dev, size, true, &gobj);
148 static int astfb_create(struct ast_fbdev *afbdev,
149 struct drm_fb_helper_surface_size *sizes)
151 struct drm_device *dev = afbdev->helper.dev;
152 struct drm_mode_fb_cmd2 mode_cmd;
153 struct drm_framebuffer *fb;
154 struct fb_info *info;
156 struct device *device = &dev->pdev->dev;
158 struct drm_gem_object *gobj = NULL;
159 struct ast_bo *bo = NULL;
160 mode_cmd.width = sizes->surface_width;
161 mode_cmd.height = sizes->surface_height;
162 mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
164 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
165 sizes->surface_depth);
167 size = mode_cmd.pitches[0] * mode_cmd.height;
169 ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
171 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
174 bo = gem_to_ast_bo(gobj);
176 sysram = vmalloc(size);
180 info = framebuffer_alloc(0, device);
187 ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
191 afbdev->sysram = sysram;
194 fb = &afbdev->afb.base;
195 afbdev->helper.fb = fb;
196 afbdev->helper.fbdev = info;
198 strcpy(info->fix.id, "astdrmfb");
200 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
201 info->fbops = &astfb_ops;
203 ret = fb_alloc_cmap(&info->cmap, 256, 0);
209 info->apertures = alloc_apertures(1);
210 if (!info->apertures) {
214 info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
215 info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
217 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
218 drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height);
220 info->screen_base = sysram;
221 info->screen_size = size;
223 info->pixmap.flags = FB_PIXMAP_SYSTEM;
225 DRM_DEBUG_KMS("allocated %dx%d\n",
226 fb->width, fb->height);
233 static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
236 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
237 ast_crtc->lut_r[regno] = red >> 8;
238 ast_crtc->lut_g[regno] = green >> 8;
239 ast_crtc->lut_b[regno] = blue >> 8;
242 static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
243 u16 *blue, int regno)
245 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
246 *red = ast_crtc->lut_r[regno] << 8;
247 *green = ast_crtc->lut_g[regno] << 8;
248 *blue = ast_crtc->lut_b[regno] << 8;
251 static int ast_find_or_create_single(struct drm_fb_helper *helper,
252 struct drm_fb_helper_surface_size *sizes)
254 struct ast_fbdev *afbdev = (struct ast_fbdev *)helper;
259 ret = astfb_create(afbdev, sizes);
267 static struct drm_fb_helper_funcs ast_fb_helper_funcs = {
268 .gamma_set = ast_fb_gamma_set,
269 .gamma_get = ast_fb_gamma_get,
270 .fb_probe = ast_find_or_create_single,
273 static void ast_fbdev_destroy(struct drm_device *dev,
274 struct ast_fbdev *afbdev)
276 struct fb_info *info;
277 struct ast_framebuffer *afb = &afbdev->afb;
278 if (afbdev->helper.fbdev) {
279 info = afbdev->helper.fbdev;
280 unregister_framebuffer(info);
282 fb_dealloc_cmap(&info->cmap);
283 framebuffer_release(info);
287 drm_gem_object_unreference_unlocked(afb->obj);
290 drm_fb_helper_fini(&afbdev->helper);
292 vfree(afbdev->sysram);
293 drm_framebuffer_cleanup(&afb->base);
296 int ast_fbdev_init(struct drm_device *dev)
298 struct ast_private *ast = dev->dev_private;
299 struct ast_fbdev *afbdev;
302 afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
307 afbdev->helper.funcs = &ast_fb_helper_funcs;
308 ret = drm_fb_helper_init(dev, &afbdev->helper,
315 drm_fb_helper_single_add_all_connectors(&afbdev->helper);
316 drm_fb_helper_initial_config(&afbdev->helper, 32);
320 void ast_fbdev_fini(struct drm_device *dev)
322 struct ast_private *ast = dev->dev_private;
327 ast_fbdev_destroy(dev, ast->fbdev);
332 void ast_fbdev_set_suspend(struct drm_device *dev, int state)
334 struct ast_private *ast = dev->dev_private;
339 fb_set_suspend(ast->fbdev->helper.fbdev, state);