1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
40#include <drm/drmP.h>
41#include <drm/drm_crtc.h>
42#include <drm/drm_fb_helper.h>
43#include <drm/drm_crtc_helper.h>
44#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;
54 int ret = -EBUSY;
55 bool unmap = false;
56 bool store_for_later = false;
57 int x2, y2;
58 unsigned long flags;
59
60 obj = afbdev->afb.obj;
61 bo = gem_to_ast_bo(obj);
62
63
64
65
66
67
68 if (!in_interrupt())
69 ret = ast_bo_reserve(bo, true);
70 if (ret) {
71 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);
96 return;
97 }
98
99 afbdev->x1 = afbdev->y1 = INT_MAX;
100 afbdev->x2 = afbdev->y2 = 0;
101 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
102
103 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 }
112 for (i = y; i <= y2; i++) {
113
114 src_offset = dst_offset = i * afbdev->afb.base.pitches[0] + (x * bpp);
115 memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, (x2 - x + 1) * bpp);
116
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
186static int astfb_create(struct drm_fb_helper *helper,
187 struct drm_fb_helper_surface_size *sizes)
188{
189 struct ast_fbdev *afbdev = (struct ast_fbdev *)helper;
190 struct drm_device *dev = afbdev->helper.dev;
191 struct drm_mode_fb_cmd2 mode_cmd;
192 struct drm_framebuffer *fb;
193 struct fb_info *info;
194 int size, ret;
195 struct device *device = &dev->pdev->dev;
196 void *sysram;
197 struct drm_gem_object *gobj = NULL;
198 struct ast_bo *bo = 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 bo = gem_to_ast_bo(gobj);
214
215 sysram = vmalloc(size);
216 if (!sysram)
217 return -ENOMEM;
218
219 info = framebuffer_alloc(0, device);
220 if (!info) {
221 ret = -ENOMEM;
222 goto out;
223 }
224 info->par = afbdev;
225
226 ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
227 if (ret)
228 goto out;
229
230 afbdev->sysram = sysram;
231 afbdev->size = size;
232
233 fb = &afbdev->afb.base;
234 afbdev->helper.fb = fb;
235 afbdev->helper.fbdev = info;
236
237 strcpy(info->fix.id, "astdrmfb");
238
239 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
240 info->fbops = &astfb_ops;
241
242 ret = fb_alloc_cmap(&info->cmap, 256, 0);
243 if (ret) {
244 ret = -ENOMEM;
245 goto out;
246 }
247
248 info->apertures = alloc_apertures(1);
249 if (!info->apertures) {
250 ret = -ENOMEM;
251 goto out;
252 }
253 info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
254 info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
255
256 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
257 drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height);
258
259 info->screen_base = sysram;
260 info->screen_size = size;
261
262 info->pixmap.flags = FB_PIXMAP_SYSTEM;
263
264 DRM_DEBUG_KMS("allocated %dx%d\n",
265 fb->width, fb->height);
266
267 return 0;
268out:
269 return ret;
270}
271
272static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
273 u16 blue, int regno)
274{
275 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
276 ast_crtc->lut_r[regno] = red >> 8;
277 ast_crtc->lut_g[regno] = green >> 8;
278 ast_crtc->lut_b[regno] = blue >> 8;
279}
280
281static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
282 u16 *blue, int regno)
283{
284 struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
285 *red = ast_crtc->lut_r[regno] << 8;
286 *green = ast_crtc->lut_g[regno] << 8;
287 *blue = ast_crtc->lut_b[regno] << 8;
288}
289
290static struct drm_fb_helper_funcs ast_fb_helper_funcs = {
291 .gamma_set = ast_fb_gamma_set,
292 .gamma_get = ast_fb_gamma_get,
293 .fb_probe = astfb_create,
294};
295
296static void ast_fbdev_destroy(struct drm_device *dev,
297 struct ast_fbdev *afbdev)
298{
299 struct fb_info *info;
300 struct ast_framebuffer *afb = &afbdev->afb;
301 if (afbdev->helper.fbdev) {
302 info = afbdev->helper.fbdev;
303 unregister_framebuffer(info);
304 if (info->cmap.len)
305 fb_dealloc_cmap(&info->cmap);
306 framebuffer_release(info);
307 }
308
309 if (afb->obj) {
310 drm_gem_object_unreference_unlocked(afb->obj);
311 afb->obj = NULL;
312 }
313 drm_fb_helper_fini(&afbdev->helper);
314
315 vfree(afbdev->sysram);
316 drm_framebuffer_unregister_private(&afb->base);
317 drm_framebuffer_cleanup(&afb->base);
318}
319
320int ast_fbdev_init(struct drm_device *dev)
321{
322 struct ast_private *ast = dev->dev_private;
323 struct ast_fbdev *afbdev;
324 int ret;
325
326 afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
327 if (!afbdev)
328 return -ENOMEM;
329
330 ast->fbdev = afbdev;
331 afbdev->helper.funcs = &ast_fb_helper_funcs;
332 spin_lock_init(&afbdev->dirty_lock);
333 ret = drm_fb_helper_init(dev, &afbdev->helper,
334 1, 1);
335 if (ret) {
336 kfree(afbdev);
337 return ret;
338 }
339
340 drm_fb_helper_single_add_all_connectors(&afbdev->helper);
341
342
343 drm_helper_disable_unused_functions(dev);
344
345 drm_fb_helper_initial_config(&afbdev->helper, 32);
346 return 0;
347}
348
349void ast_fbdev_fini(struct drm_device *dev)
350{
351 struct ast_private *ast = dev->dev_private;
352
353 if (!ast->fbdev)
354 return;
355
356 ast_fbdev_destroy(dev, ast->fbdev);
357 kfree(ast->fbdev);
358 ast->fbdev = NULL;
359}
360
361void ast_fbdev_set_suspend(struct drm_device *dev, int state)
362{
363 struct ast_private *ast = dev->dev_private;
364
365 if (!ast->fbdev)
366 return;
367
368 fb_set_suspend(ast->fbdev->helper.fbdev, state);
369}
370