]>
Commit | Line | Data |
---|---|---|
1c248b7d ID |
1 | /* exynos_drm_fbdev.c |
2 | * | |
3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. | |
4 | * Authors: | |
5 | * Inki Dae <inki.dae@samsung.com> | |
6 | * Joonyoung Shim <jy0922.shim@samsung.com> | |
7 | * Seung-Woo Kim <sw0312.kim@samsung.com> | |
8 | * | |
d81aecb5 ID |
9 | * This program is free software; you can redistribute it and/or modify it |
10 | * under the terms of the GNU General Public License as published by the | |
11 | * Free Software Foundation; either version 2 of the License, or (at your | |
12 | * option) any later version. | |
1c248b7d ID |
13 | */ |
14 | ||
760285e7 DH |
15 | #include <drm/drmP.h> |
16 | #include <drm/drm_crtc.h> | |
17 | #include <drm/drm_fb_helper.h> | |
18 | #include <drm/drm_crtc_helper.h> | |
1c248b7d ID |
19 | |
20 | #include "exynos_drm_drv.h" | |
21 | #include "exynos_drm_fb.h" | |
2c871127 | 22 | #include "exynos_drm_gem.h" |
c704f1b4 | 23 | #include "exynos_drm_iommu.h" |
1c248b7d ID |
24 | |
25 | #define MAX_CONNECTOR 4 | |
26 | #define PREFERRED_BPP 32 | |
27 | ||
28 | #define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\ | |
29 | drm_fb_helper) | |
30 | ||
31 | struct exynos_drm_fbdev { | |
e1533c08 JS |
32 | struct drm_fb_helper drm_fb_helper; |
33 | struct exynos_drm_gem_obj *exynos_gem_obj; | |
1c248b7d ID |
34 | }; |
35 | ||
dd265850 P |
36 | static int exynos_drm_fb_mmap(struct fb_info *info, |
37 | struct vm_area_struct *vma) | |
38 | { | |
39 | struct drm_fb_helper *helper = info->par; | |
40 | struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper); | |
41 | struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj; | |
42 | struct exynos_drm_gem_buf *buffer = exynos_gem_obj->buffer; | |
43 | unsigned long vm_size; | |
44 | int ret; | |
45 | ||
46 | DRM_DEBUG_KMS("%s\n", __func__); | |
47 | ||
48 | vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP; | |
49 | ||
50 | vm_size = vma->vm_end - vma->vm_start; | |
51 | ||
52 | if (vm_size > buffer->size) | |
53 | return -EINVAL; | |
54 | ||
4744ad24 | 55 | ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->pages, |
dd265850 P |
56 | buffer->dma_addr, buffer->size, &buffer->dma_attrs); |
57 | if (ret < 0) { | |
58 | DRM_ERROR("failed to mmap.\n"); | |
59 | return ret; | |
60 | } | |
61 | ||
62 | return 0; | |
63 | } | |
64 | ||
1c248b7d ID |
65 | static struct fb_ops exynos_drm_fb_ops = { |
66 | .owner = THIS_MODULE, | |
dd265850 | 67 | .fb_mmap = exynos_drm_fb_mmap, |
1c248b7d ID |
68 | .fb_fillrect = cfb_fillrect, |
69 | .fb_copyarea = cfb_copyarea, | |
70 | .fb_imageblit = cfb_imageblit, | |
71 | .fb_check_var = drm_fb_helper_check_var, | |
83b316fd | 72 | .fb_set_par = drm_fb_helper_set_par, |
1c248b7d ID |
73 | .fb_blank = drm_fb_helper_blank, |
74 | .fb_pan_display = drm_fb_helper_pan_display, | |
75 | .fb_setcmap = drm_fb_helper_setcmap, | |
76 | }; | |
77 | ||
19c8b834 | 78 | static int exynos_drm_fbdev_update(struct drm_fb_helper *helper, |
aa6b2b6c | 79 | struct drm_framebuffer *fb) |
1c248b7d ID |
80 | { |
81 | struct fb_info *fbi = helper->fbdev; | |
82 | struct drm_device *dev = helper->dev; | |
2c871127 | 83 | struct exynos_drm_gem_buf *buffer; |
aa6b2b6c | 84 | unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3); |
19c8b834 | 85 | unsigned long offset; |
1c248b7d ID |
86 | |
87 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
88 | ||
01f2c773 | 89 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth); |
aa6b2b6c | 90 | drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height); |
1c248b7d | 91 | |
229d3534 SWK |
92 | /* RGB formats use only one buffer */ |
93 | buffer = exynos_drm_fb_buffer(fb, 0); | |
2c871127 ID |
94 | if (!buffer) { |
95 | DRM_LOG_KMS("buffer is null.\n"); | |
19c8b834 ID |
96 | return -EFAULT; |
97 | } | |
1c248b7d | 98 | |
4744ad24 ID |
99 | /* map pages with kernel virtual space. */ |
100 | if (!buffer->kvaddr) { | |
c704f1b4 ID |
101 | if (is_drm_iommu_supported(dev)) { |
102 | unsigned int nr_pages = buffer->size >> PAGE_SHIFT; | |
103 | ||
104 | buffer->kvaddr = vmap(buffer->pages, nr_pages, VM_MAP, | |
4744ad24 | 105 | pgprot_writecombine(PAGE_KERNEL)); |
c704f1b4 ID |
106 | } else { |
107 | phys_addr_t dma_addr = buffer->dma_addr; | |
108 | if (dma_addr) | |
109 | buffer->kvaddr = phys_to_virt(dma_addr); | |
110 | else | |
111 | buffer->kvaddr = (void __iomem *)NULL; | |
112 | } | |
4744ad24 ID |
113 | if (!buffer->kvaddr) { |
114 | DRM_ERROR("failed to map pages to kernel space.\n"); | |
115 | return -EIO; | |
116 | } | |
117 | } | |
118 | ||
01ed8126 ID |
119 | /* buffer count to framebuffer always is 1 at booting time. */ |
120 | exynos_drm_fb_set_buf_cnt(fb, 1); | |
121 | ||
19c8b834 | 122 | offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3); |
01f2c773 | 123 | offset += fbi->var.yoffset * fb->pitches[0]; |
1c248b7d | 124 | |
2c871127 ID |
125 | dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr; |
126 | fbi->screen_base = buffer->kvaddr + offset; | |
c704f1b4 ID |
127 | if (is_drm_iommu_supported(dev)) |
128 | fbi->fix.smem_start = (unsigned long) | |
640631d0 | 129 | (page_to_phys(sg_page(buffer->sgt->sgl)) + offset); |
c704f1b4 ID |
130 | else |
131 | fbi->fix.smem_start = (unsigned long)buffer->dma_addr; | |
132 | ||
1c248b7d | 133 | fbi->screen_size = size; |
1c248b7d | 134 | fbi->fix.smem_len = size; |
19c8b834 ID |
135 | |
136 | return 0; | |
1c248b7d ID |
137 | } |
138 | ||
139 | static int exynos_drm_fbdev_create(struct drm_fb_helper *helper, | |
140 | struct drm_fb_helper_surface_size *sizes) | |
141 | { | |
142 | struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper); | |
e1533c08 | 143 | struct exynos_drm_gem_obj *exynos_gem_obj; |
1c248b7d ID |
144 | struct drm_device *dev = helper->dev; |
145 | struct fb_info *fbi; | |
a794d57d | 146 | struct drm_mode_fb_cmd2 mode_cmd = { 0 }; |
1c248b7d | 147 | struct platform_device *pdev = dev->platformdev; |
e1533c08 | 148 | unsigned long size; |
1c248b7d ID |
149 | int ret; |
150 | ||
151 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
152 | ||
153 | DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n", | |
154 | sizes->surface_width, sizes->surface_height, | |
155 | sizes->surface_bpp); | |
156 | ||
157 | mode_cmd.width = sizes->surface_width; | |
158 | mode_cmd.height = sizes->surface_height; | |
a794d57d JS |
159 | mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3); |
160 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, | |
161 | sizes->surface_depth); | |
1c248b7d ID |
162 | |
163 | mutex_lock(&dev->struct_mutex); | |
164 | ||
165 | fbi = framebuffer_alloc(0, &pdev->dev); | |
166 | if (!fbi) { | |
167 | DRM_ERROR("failed to allocate fb info.\n"); | |
168 | ret = -ENOMEM; | |
169 | goto out; | |
170 | } | |
171 | ||
e1533c08 | 172 | size = mode_cmd.pitches[0] * mode_cmd.height; |
2b35892e ID |
173 | |
174 | /* 0 means to allocate physically continuous memory */ | |
175 | exynos_gem_obj = exynos_drm_gem_create(dev, 0, size); | |
e1533c08 JS |
176 | if (IS_ERR(exynos_gem_obj)) { |
177 | ret = PTR_ERR(exynos_gem_obj); | |
662aa6d7 | 178 | goto err_release_framebuffer; |
e1533c08 JS |
179 | } |
180 | ||
181 | exynos_fbdev->exynos_gem_obj = exynos_gem_obj; | |
182 | ||
183 | helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd, | |
184 | &exynos_gem_obj->base); | |
185 | if (IS_ERR_OR_NULL(helper->fb)) { | |
1c248b7d | 186 | DRM_ERROR("failed to create drm framebuffer.\n"); |
e1533c08 | 187 | ret = PTR_ERR(helper->fb); |
662aa6d7 | 188 | goto err_destroy_gem; |
1c248b7d ID |
189 | } |
190 | ||
1c248b7d ID |
191 | helper->fbdev = fbi; |
192 | ||
193 | fbi->par = helper; | |
194 | fbi->flags = FBINFO_FLAG_DEFAULT; | |
195 | fbi->fbops = &exynos_drm_fb_ops; | |
196 | ||
197 | ret = fb_alloc_cmap(&fbi->cmap, 256, 0); | |
198 | if (ret) { | |
199 | DRM_ERROR("failed to allocate cmap.\n"); | |
662aa6d7 | 200 | goto err_destroy_framebuffer; |
1c248b7d ID |
201 | } |
202 | ||
aa6b2b6c | 203 | ret = exynos_drm_fbdev_update(helper, helper->fb); |
662aa6d7 ID |
204 | if (ret < 0) |
205 | goto err_dealloc_cmap; | |
206 | ||
207 | mutex_unlock(&dev->struct_mutex); | |
208 | return ret; | |
209 | ||
210 | err_dealloc_cmap: | |
211 | fb_dealloc_cmap(&fbi->cmap); | |
212 | err_destroy_framebuffer: | |
213 | drm_framebuffer_cleanup(helper->fb); | |
214 | err_destroy_gem: | |
215 | exynos_drm_gem_destroy(exynos_gem_obj); | |
216 | err_release_framebuffer: | |
217 | framebuffer_release(fbi); | |
1c248b7d ID |
218 | |
219 | /* | |
220 | * if failed, all resources allocated above would be released by | |
221 | * drm_mode_config_cleanup() when drm_load() had been called prior | |
222 | * to any specific driver such as fimd or hdmi driver. | |
223 | */ | |
224 | out: | |
225 | mutex_unlock(&dev->struct_mutex); | |
226 | return ret; | |
227 | } | |
228 | ||
1c248b7d ID |
229 | static int exynos_drm_fbdev_probe(struct drm_fb_helper *helper, |
230 | struct drm_fb_helper_surface_size *sizes) | |
231 | { | |
232 | int ret = 0; | |
233 | ||
234 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
235 | ||
bc41eae2 ID |
236 | /* |
237 | * with !helper->fb, it means that this funcion is called first time | |
238 | * and after that, the helper->fb would be used as clone mode. | |
239 | */ | |
1c248b7d ID |
240 | if (!helper->fb) { |
241 | ret = exynos_drm_fbdev_create(helper, sizes); | |
242 | if (ret < 0) { | |
243 | DRM_ERROR("failed to create fbdev.\n"); | |
244 | return ret; | |
245 | } | |
246 | ||
247 | /* | |
248 | * fb_helper expects a value more than 1 if succeed | |
249 | * because register_framebuffer() should be called. | |
250 | */ | |
251 | ret = 1; | |
1c248b7d ID |
252 | } |
253 | ||
254 | return ret; | |
255 | } | |
256 | ||
257 | static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = { | |
258 | .fb_probe = exynos_drm_fbdev_probe, | |
259 | }; | |
260 | ||
261 | int exynos_drm_fbdev_init(struct drm_device *dev) | |
262 | { | |
263 | struct exynos_drm_fbdev *fbdev; | |
264 | struct exynos_drm_private *private = dev->dev_private; | |
265 | struct drm_fb_helper *helper; | |
266 | unsigned int num_crtc; | |
267 | int ret; | |
268 | ||
269 | DRM_DEBUG_KMS("%s\n", __FILE__); | |
270 | ||
271 | if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector) | |
272 | return 0; | |
273 | ||
274 | fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL); | |
275 | if (!fbdev) { | |
276 | DRM_ERROR("failed to allocate drm fbdev.\n"); | |
277 | return -ENOMEM; | |
278 | } | |
279 | ||
280 | private->fb_helper = helper = &fbdev->drm_fb_helper; | |
281 | helper->funcs = &exynos_drm_fb_helper_funcs; | |
282 | ||
283 | num_crtc = dev->mode_config.num_crtc; | |
284 | ||
285 | ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR); | |
286 | if (ret < 0) { | |
287 | DRM_ERROR("failed to initialize drm fb helper.\n"); | |
288 | goto err_init; | |
289 | } | |
290 | ||
291 | ret = drm_fb_helper_single_add_all_connectors(helper); | |
292 | if (ret < 0) { | |
293 | DRM_ERROR("failed to register drm_fb_helper_connector.\n"); | |
294 | goto err_setup; | |
295 | ||
296 | } | |
297 | ||
298 | ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP); | |
299 | if (ret < 0) { | |
300 | DRM_ERROR("failed to set up hw configuration.\n"); | |
301 | goto err_setup; | |
302 | } | |
303 | ||
304 | return 0; | |
305 | ||
306 | err_setup: | |
307 | drm_fb_helper_fini(helper); | |
308 | ||
309 | err_init: | |
310 | private->fb_helper = NULL; | |
311 | kfree(fbdev); | |
312 | ||
313 | return ret; | |
314 | } | |
315 | ||
316 | static void exynos_drm_fbdev_destroy(struct drm_device *dev, | |
317 | struct drm_fb_helper *fb_helper) | |
318 | { | |
4744ad24 ID |
319 | struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper); |
320 | struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj; | |
1c248b7d ID |
321 | struct drm_framebuffer *fb; |
322 | ||
c704f1b4 | 323 | if (is_drm_iommu_supported(dev) && exynos_gem_obj->buffer->kvaddr) |
4744ad24 ID |
324 | vunmap(exynos_gem_obj->buffer->kvaddr); |
325 | ||
1c248b7d ID |
326 | /* release drm framebuffer and real buffer */ |
327 | if (fb_helper->fb && fb_helper->fb->funcs) { | |
328 | fb = fb_helper->fb; | |
f7eff60e RC |
329 | if (fb) |
330 | drm_framebuffer_remove(fb); | |
1c248b7d ID |
331 | } |
332 | ||
333 | /* release linux framebuffer */ | |
334 | if (fb_helper->fbdev) { | |
335 | struct fb_info *info; | |
336 | int ret; | |
337 | ||
338 | info = fb_helper->fbdev; | |
339 | ret = unregister_framebuffer(info); | |
340 | if (ret < 0) | |
341 | DRM_DEBUG_KMS("failed unregister_framebuffer()\n"); | |
342 | ||
343 | if (info->cmap.len) | |
344 | fb_dealloc_cmap(&info->cmap); | |
345 | ||
346 | framebuffer_release(info); | |
347 | } | |
348 | ||
349 | drm_fb_helper_fini(fb_helper); | |
350 | } | |
351 | ||
352 | void exynos_drm_fbdev_fini(struct drm_device *dev) | |
353 | { | |
354 | struct exynos_drm_private *private = dev->dev_private; | |
355 | struct exynos_drm_fbdev *fbdev; | |
356 | ||
357 | if (!private || !private->fb_helper) | |
358 | return; | |
359 | ||
360 | fbdev = to_exynos_fbdev(private->fb_helper); | |
361 | ||
e1533c08 JS |
362 | if (fbdev->exynos_gem_obj) |
363 | exynos_drm_gem_destroy(fbdev->exynos_gem_obj); | |
364 | ||
1c248b7d ID |
365 | exynos_drm_fbdev_destroy(dev, private->fb_helper); |
366 | kfree(fbdev); | |
367 | private->fb_helper = NULL; | |
368 | } | |
369 | ||
370 | void exynos_drm_fbdev_restore_mode(struct drm_device *dev) | |
371 | { | |
372 | struct exynos_drm_private *private = dev->dev_private; | |
373 | ||
374 | if (!private || !private->fb_helper) | |
375 | return; | |
376 | ||
377 | drm_fb_helper_restore_fbdev_mode(private->fb_helper); | |
378 | } |