]>
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> | |
312fec14 DA |
43 | #include "ast_drv.h" |
44 | ||
45 | static 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; | |
52 | int bpp = (afbdev->afb.base.bits_per_pixel + 7)/8; | |
53 | int ret; | |
54 | bool unmap = false; | |
55 | ||
56 | obj = afbdev->afb.obj; | |
57 | bo = gem_to_ast_bo(obj); | |
58 | ||
59 | ret = ast_bo_reserve(bo, true); | |
60 | if (ret) { | |
61 | DRM_ERROR("failed to reserve fb bo\n"); | |
62 | return; | |
63 | } | |
64 | ||
65 | if (!bo->kmap.virtual) { | |
66 | ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap); | |
67 | if (ret) { | |
68 | DRM_ERROR("failed to kmap fb updates\n"); | |
69 | ast_bo_unreserve(bo); | |
70 | return; | |
71 | } | |
72 | unmap = true; | |
73 | } | |
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); | |
78 | ||
79 | } | |
80 | if (unmap) | |
81 | ttm_bo_kunmap(&bo->kmap); | |
82 | ||
83 | ast_bo_unreserve(bo); | |
84 | } | |
85 | ||
86 | static void ast_fillrect(struct fb_info *info, | |
87 | const struct fb_fillrect *rect) | |
88 | { | |
89 | struct ast_fbdev *afbdev = info->par; | |
90 | sys_fillrect(info, rect); | |
91 | ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width, | |
92 | rect->height); | |
93 | } | |
94 | ||
95 | static void ast_copyarea(struct fb_info *info, | |
96 | const struct fb_copyarea *area) | |
97 | { | |
98 | struct ast_fbdev *afbdev = info->par; | |
99 | sys_copyarea(info, area); | |
100 | ast_dirty_update(afbdev, area->dx, area->dy, area->width, | |
101 | area->height); | |
102 | } | |
103 | ||
104 | static void ast_imageblit(struct fb_info *info, | |
105 | const struct fb_image *image) | |
106 | { | |
107 | struct ast_fbdev *afbdev = info->par; | |
108 | sys_imageblit(info, image); | |
109 | ast_dirty_update(afbdev, image->dx, image->dy, image->width, | |
110 | image->height); | |
111 | } | |
112 | ||
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, | |
125 | }; | |
126 | ||
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) | |
130 | { | |
131 | struct drm_device *dev = afbdev->helper.dev; | |
132 | u32 bpp, depth; | |
133 | u32 size; | |
134 | struct drm_gem_object *gobj; | |
135 | ||
136 | int ret = 0; | |
137 | drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp); | |
138 | ||
139 | size = mode_cmd->pitches[0] * mode_cmd->height; | |
140 | ret = ast_gem_create(dev, size, true, &gobj); | |
141 | if (ret) | |
142 | return ret; | |
143 | ||
144 | *gobj_p = gobj; | |
145 | return ret; | |
146 | } | |
147 | ||
148 | static int astfb_create(struct ast_fbdev *afbdev, | |
149 | struct drm_fb_helper_surface_size *sizes) | |
150 | { | |
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; | |
155 | int size, ret; | |
156 | struct device *device = &dev->pdev->dev; | |
157 | void *sysram; | |
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); | |
163 | ||
164 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, | |
165 | sizes->surface_depth); | |
166 | ||
167 | size = mode_cmd.pitches[0] * mode_cmd.height; | |
168 | ||
169 | ret = astfb_create_object(afbdev, &mode_cmd, &gobj); | |
170 | if (ret) { | |
171 | DRM_ERROR("failed to create fbcon backing object %d\n", ret); | |
172 | return ret; | |
173 | } | |
174 | bo = gem_to_ast_bo(gobj); | |
175 | ||
176 | sysram = vmalloc(size); | |
177 | if (!sysram) | |
178 | return -ENOMEM; | |
179 | ||
180 | info = framebuffer_alloc(0, device); | |
181 | if (!info) { | |
182 | ret = -ENOMEM; | |
183 | goto out; | |
184 | } | |
185 | info->par = afbdev; | |
186 | ||
187 | ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj); | |
188 | if (ret) | |
189 | goto out; | |
190 | ||
191 | afbdev->sysram = sysram; | |
192 | afbdev->size = size; | |
193 | ||
194 | fb = &afbdev->afb.base; | |
195 | afbdev->helper.fb = fb; | |
196 | afbdev->helper.fbdev = info; | |
197 | ||
198 | strcpy(info->fix.id, "astdrmfb"); | |
199 | ||
200 | info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT; | |
201 | info->fbops = &astfb_ops; | |
202 | ||
203 | ret = fb_alloc_cmap(&info->cmap, 256, 0); | |
204 | if (ret) { | |
205 | ret = -ENOMEM; | |
206 | goto out; | |
207 | } | |
208 | ||
209 | info->apertures = alloc_apertures(1); | |
210 | if (!info->apertures) { | |
211 | ret = -ENOMEM; | |
212 | goto out; | |
213 | } | |
214 | info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0); | |
215 | info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0); | |
216 | ||
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); | |
219 | ||
220 | info->screen_base = sysram; | |
221 | info->screen_size = size; | |
222 | ||
223 | info->pixmap.flags = FB_PIXMAP_SYSTEM; | |
224 | ||
225 | DRM_DEBUG_KMS("allocated %dx%d\n", | |
226 | fb->width, fb->height); | |
227 | ||
228 | return 0; | |
229 | out: | |
230 | return ret; | |
231 | } | |
232 | ||
233 | static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green, | |
234 | u16 blue, int regno) | |
235 | { | |
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; | |
240 | } | |
241 | ||
242 | static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green, | |
243 | u16 *blue, int regno) | |
244 | { | |
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; | |
249 | } | |
250 | ||
251 | static int ast_find_or_create_single(struct drm_fb_helper *helper, | |
252 | struct drm_fb_helper_surface_size *sizes) | |
253 | { | |
254 | struct ast_fbdev *afbdev = (struct ast_fbdev *)helper; | |
255 | int new_fb = 0; | |
256 | int ret; | |
257 | ||
258 | if (!helper->fb) { | |
259 | ret = astfb_create(afbdev, sizes); | |
260 | if (ret) | |
261 | return ret; | |
262 | new_fb = 1; | |
263 | } | |
264 | return new_fb; | |
265 | } | |
266 | ||
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, | |
271 | }; | |
272 | ||
273 | static void ast_fbdev_destroy(struct drm_device *dev, | |
274 | struct ast_fbdev *afbdev) | |
275 | { | |
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); | |
281 | if (info->cmap.len) | |
282 | fb_dealloc_cmap(&info->cmap); | |
283 | framebuffer_release(info); | |
284 | } | |
285 | ||
286 | if (afb->obj) { | |
287 | drm_gem_object_unreference_unlocked(afb->obj); | |
288 | afb->obj = NULL; | |
289 | } | |
290 | drm_fb_helper_fini(&afbdev->helper); | |
291 | ||
292 | vfree(afbdev->sysram); | |
293 | drm_framebuffer_cleanup(&afb->base); | |
294 | } | |
295 | ||
296 | int ast_fbdev_init(struct drm_device *dev) | |
297 | { | |
298 | struct ast_private *ast = dev->dev_private; | |
299 | struct ast_fbdev *afbdev; | |
300 | int ret; | |
301 | ||
302 | afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL); | |
303 | if (!afbdev) | |
304 | return -ENOMEM; | |
305 | ||
306 | ast->fbdev = afbdev; | |
307 | afbdev->helper.funcs = &ast_fb_helper_funcs; | |
308 | ret = drm_fb_helper_init(dev, &afbdev->helper, | |
309 | 1, 1); | |
310 | if (ret) { | |
311 | kfree(afbdev); | |
312 | return ret; | |
313 | } | |
314 | ||
315 | drm_fb_helper_single_add_all_connectors(&afbdev->helper); | |
316 | drm_fb_helper_initial_config(&afbdev->helper, 32); | |
317 | return 0; | |
318 | } | |
319 | ||
320 | void ast_fbdev_fini(struct drm_device *dev) | |
321 | { | |
322 | struct ast_private *ast = dev->dev_private; | |
323 | ||
324 | if (!ast->fbdev) | |
325 | return; | |
326 | ||
327 | ast_fbdev_destroy(dev, ast->fbdev); | |
328 | kfree(ast->fbdev); | |
329 | ast->fbdev = NULL; | |
330 | } | |
331 | ||
332 | void ast_fbdev_set_suspend(struct drm_device *dev, int state) | |
333 | { | |
334 | struct ast_private *ast = dev->dev_private; | |
335 | ||
336 | if (!ast->fbdev) | |
337 | return; | |
338 | ||
339 | fb_set_suspend(ast->fbdev->helper.fbdev, state); | |
340 | } |