Commit | Line | Data |
---|---|---|
312fec14 DA |
1 | /* |
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 | ||
760285e7 DH |
40 | #include <drm/drmP.h> |
41 | #include <drm/drm_crtc.h> | |
42 | #include <drm/drm_fb_helper.h> | |
76a39dbf | 43 | #include <drm/drm_crtc_helper.h> |
312fec14 DA |
44 | #include "ast_drv.h" |
45 | ||
46 | static 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; | |
55 | bool unmap = false; | |
306373b6 DA |
56 | bool store_for_later = false; |
57 | int x2, y2; | |
58 | unsigned long flags; | |
312fec14 DA |
59 | |
60 | obj = afbdev->afb.obj; | |
61 | bo = gem_to_ast_bo(obj); | |
62 | ||
306373b6 DA |
63 | /* |
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 | */ | |
312fec14 DA |
68 | ret = ast_bo_reserve(bo, true); |
69 | if (ret) { | |
306373b6 DA |
70 | 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); | |
312fec14 DA |
95 | return; |
96 | } | |
97 | ||
306373b6 DA |
98 | afbdev->x1 = afbdev->y1 = INT_MAX; |
99 | afbdev->x2 = afbdev->y2 = 0; | |
100 | spin_unlock_irqrestore(&afbdev->dirty_lock, flags); | |
101 | ||
312fec14 DA |
102 | 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 | } | |
306373b6 | 111 | for (i = y; i <= y2; i++) { |
312fec14 DA |
112 | /* assume equal stride for now */ |
113 | src_offset = dst_offset = i * afbdev->afb.base.pitches[0] + (x * bpp); | |
306373b6 | 114 | memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, (x2 - x + 1) * bpp); |
312fec14 DA |
115 | |
116 | } | |
117 | if (unmap) | |
118 | ttm_bo_kunmap(&bo->kmap); | |
119 | ||
120 | ast_bo_unreserve(bo); | |
121 | } | |
122 | ||
123 | static void ast_fillrect(struct fb_info *info, | |
124 | const struct fb_fillrect *rect) | |
125 | { | |
126 | struct ast_fbdev *afbdev = info->par; | |
127 | sys_fillrect(info, rect); | |
128 | ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width, | |
129 | rect->height); | |
130 | } | |
131 | ||
132 | static void ast_copyarea(struct fb_info *info, | |
133 | const struct fb_copyarea *area) | |
134 | { | |
135 | struct ast_fbdev *afbdev = info->par; | |
136 | sys_copyarea(info, area); | |
137 | ast_dirty_update(afbdev, area->dx, area->dy, area->width, | |
138 | area->height); | |
139 | } | |
140 | ||
141 | static void ast_imageblit(struct fb_info *info, | |
142 | const struct fb_image *image) | |
143 | { | |
144 | struct ast_fbdev *afbdev = info->par; | |
145 | sys_imageblit(info, image); | |
146 | ast_dirty_update(afbdev, image->dx, image->dy, image->width, | |
147 | image->height); | |
148 | } | |
149 | ||
150 | static 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 | ||
164 | static int astfb_create_object(struct ast_fbdev *afbdev, | |
165 | struct drm_mode_fb_cmd2 *mode_cmd, | |
166 | struct drm_gem_object **gobj_p) | |
167 | { | |
168 | struct drm_device *dev = afbdev->helper.dev; | |
169 | u32 bpp, depth; | |
170 | u32 size; | |
171 | struct drm_gem_object *gobj; | |
172 | ||
173 | int ret = 0; | |
174 | drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp); | |
175 | ||
176 | size = mode_cmd->pitches[0] * mode_cmd->height; | |
177 | ret = ast_gem_create(dev, size, true, &gobj); | |
178 | if (ret) | |
179 | return ret; | |
180 | ||
181 | *gobj_p = gobj; | |
182 | return ret; | |
183 | } | |
184 | ||
cd5428a5 | 185 | static int astfb_create(struct drm_fb_helper *helper, |
312fec14 DA |
186 | struct drm_fb_helper_surface_size *sizes) |
187 | { | |
cd5428a5 | 188 | struct ast_fbdev *afbdev = (struct ast_fbdev *)helper; |
312fec14 DA |
189 | struct drm_device *dev = afbdev->helper.dev; |
190 | struct drm_mode_fb_cmd2 mode_cmd; | |
191 | struct drm_framebuffer *fb; | |
192 | struct fb_info *info; | |
193 | int size, ret; | |
194 | struct device *device = &dev->pdev->dev; | |
195 | void *sysram; | |
196 | struct drm_gem_object *gobj = NULL; | |
197 | struct ast_bo *bo = NULL; | |
198 | mode_cmd.width = sizes->surface_width; | |
199 | mode_cmd.height = sizes->surface_height; | |
200 | mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8); | |
201 | ||
202 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, | |
203 | sizes->surface_depth); | |
204 | ||
205 | size = mode_cmd.pitches[0] * mode_cmd.height; | |
206 | ||
207 | ret = astfb_create_object(afbdev, &mode_cmd, &gobj); | |
208 | if (ret) { | |
209 | DRM_ERROR("failed to create fbcon backing object %d\n", ret); | |
210 | return ret; | |
211 | } | |
212 | bo = gem_to_ast_bo(gobj); | |
213 | ||
214 | sysram = vmalloc(size); | |
215 | if (!sysram) | |
216 | return -ENOMEM; | |
217 | ||
218 | info = framebuffer_alloc(0, device); | |
219 | if (!info) { | |
220 | ret = -ENOMEM; | |
221 | goto out; | |
222 | } | |
223 | info->par = afbdev; | |
224 | ||
225 | ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj); | |
226 | if (ret) | |
227 | goto out; | |
228 | ||
229 | afbdev->sysram = sysram; | |
230 | afbdev->size = size; | |
231 | ||
232 | fb = &afbdev->afb.base; | |
233 | afbdev->helper.fb = fb; | |
234 | afbdev->helper.fbdev = info; | |
235 | ||
236 | strcpy(info->fix.id, "astdrmfb"); | |
237 | ||
238 | info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT; | |
239 | info->fbops = &astfb_ops; | |
240 | ||
241 | ret = fb_alloc_cmap(&info->cmap, 256, 0); | |
242 | if (ret) { | |
243 | ret = -ENOMEM; | |
244 | goto out; | |
245 | } | |
246 | ||
247 | info->apertures = alloc_apertures(1); | |
248 | if (!info->apertures) { | |
249 | ret = -ENOMEM; | |
250 | goto out; | |
251 | } | |
252 | info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0); | |
253 | info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0); | |
254 | ||
255 | drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth); | |
256 | drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height); | |
257 | ||
258 | info->screen_base = sysram; | |
259 | info->screen_size = size; | |
260 | ||
261 | info->pixmap.flags = FB_PIXMAP_SYSTEM; | |
262 | ||
263 | DRM_DEBUG_KMS("allocated %dx%d\n", | |
264 | fb->width, fb->height); | |
265 | ||
266 | return 0; | |
267 | out: | |
268 | return ret; | |
269 | } | |
270 | ||
271 | static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green, | |
272 | u16 blue, int regno) | |
273 | { | |
274 | struct ast_crtc *ast_crtc = to_ast_crtc(crtc); | |
275 | ast_crtc->lut_r[regno] = red >> 8; | |
276 | ast_crtc->lut_g[regno] = green >> 8; | |
277 | ast_crtc->lut_b[regno] = blue >> 8; | |
278 | } | |
279 | ||
280 | static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green, | |
281 | u16 *blue, int regno) | |
282 | { | |
283 | struct ast_crtc *ast_crtc = to_ast_crtc(crtc); | |
284 | *red = ast_crtc->lut_r[regno] << 8; | |
285 | *green = ast_crtc->lut_g[regno] << 8; | |
286 | *blue = ast_crtc->lut_b[regno] << 8; | |
287 | } | |
288 | ||
312fec14 DA |
289 | static struct drm_fb_helper_funcs ast_fb_helper_funcs = { |
290 | .gamma_set = ast_fb_gamma_set, | |
291 | .gamma_get = ast_fb_gamma_get, | |
cd5428a5 | 292 | .fb_probe = astfb_create, |
312fec14 DA |
293 | }; |
294 | ||
295 | static void ast_fbdev_destroy(struct drm_device *dev, | |
296 | struct ast_fbdev *afbdev) | |
297 | { | |
298 | struct fb_info *info; | |
299 | struct ast_framebuffer *afb = &afbdev->afb; | |
300 | if (afbdev->helper.fbdev) { | |
301 | info = afbdev->helper.fbdev; | |
302 | unregister_framebuffer(info); | |
303 | if (info->cmap.len) | |
304 | fb_dealloc_cmap(&info->cmap); | |
305 | framebuffer_release(info); | |
306 | } | |
307 | ||
308 | if (afb->obj) { | |
309 | drm_gem_object_unreference_unlocked(afb->obj); | |
310 | afb->obj = NULL; | |
311 | } | |
312 | drm_fb_helper_fini(&afbdev->helper); | |
313 | ||
314 | vfree(afbdev->sysram); | |
36206361 | 315 | drm_framebuffer_unregister_private(&afb->base); |
312fec14 DA |
316 | drm_framebuffer_cleanup(&afb->base); |
317 | } | |
318 | ||
319 | int ast_fbdev_init(struct drm_device *dev) | |
320 | { | |
321 | struct ast_private *ast = dev->dev_private; | |
322 | struct ast_fbdev *afbdev; | |
323 | int ret; | |
324 | ||
325 | afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL); | |
326 | if (!afbdev) | |
327 | return -ENOMEM; | |
328 | ||
329 | ast->fbdev = afbdev; | |
330 | afbdev->helper.funcs = &ast_fb_helper_funcs; | |
306373b6 | 331 | spin_lock_init(&afbdev->dirty_lock); |
312fec14 DA |
332 | ret = drm_fb_helper_init(dev, &afbdev->helper, |
333 | 1, 1); | |
334 | if (ret) { | |
335 | kfree(afbdev); | |
336 | return ret; | |
337 | } | |
338 | ||
339 | drm_fb_helper_single_add_all_connectors(&afbdev->helper); | |
76a39dbf DV |
340 | |
341 | /* disable all the possible outputs/crtcs before entering KMS mode */ | |
342 | drm_helper_disable_unused_functions(dev); | |
343 | ||
312fec14 DA |
344 | drm_fb_helper_initial_config(&afbdev->helper, 32); |
345 | return 0; | |
346 | } | |
347 | ||
348 | void ast_fbdev_fini(struct drm_device *dev) | |
349 | { | |
350 | struct ast_private *ast = dev->dev_private; | |
351 | ||
352 | if (!ast->fbdev) | |
353 | return; | |
354 | ||
355 | ast_fbdev_destroy(dev, ast->fbdev); | |
356 | kfree(ast->fbdev); | |
357 | ast->fbdev = NULL; | |
358 | } | |
359 | ||
360 | void ast_fbdev_set_suspend(struct drm_device *dev, int state) | |
361 | { | |
362 | struct ast_private *ast = dev->dev_private; | |
363 | ||
364 | if (!ast->fbdev) | |
365 | return; | |
366 | ||
367 | fb_set_suspend(ast->fbdev->helper.fbdev, state); | |
368 | } |