Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / gpu / drm / tegra / fb.c
blob001cb77e2f596dd93184d21aeea877ce824d1a7c
1 /*
2 * Copyright (C) 2012-2013 Avionic Design GmbH
3 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
5 * Based on the KMS/FB CMA helpers
6 * Copyright (C) 2012 Analog Device Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/console.h>
15 #include "drm.h"
16 #include "gem.h"
18 static inline struct tegra_fb *to_tegra_fb(struct drm_framebuffer *fb)
20 return container_of(fb, struct tegra_fb, base);
23 #ifdef CONFIG_DRM_FBDEV_EMULATION
24 static inline struct tegra_fbdev *to_tegra_fbdev(struct drm_fb_helper *helper)
26 return container_of(helper, struct tegra_fbdev, base);
28 #endif
30 struct tegra_bo *tegra_fb_get_plane(struct drm_framebuffer *framebuffer,
31 unsigned int index)
33 struct tegra_fb *fb = to_tegra_fb(framebuffer);
35 if (index >= framebuffer->format->num_planes)
36 return NULL;
38 return fb->planes[index];
41 bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer)
43 struct tegra_fb *fb = to_tegra_fb(framebuffer);
45 if (fb->planes[0]->flags & TEGRA_BO_BOTTOM_UP)
46 return true;
48 return false;
51 int tegra_fb_get_tiling(struct drm_framebuffer *framebuffer,
52 struct tegra_bo_tiling *tiling)
54 struct tegra_fb *fb = to_tegra_fb(framebuffer);
55 uint64_t modifier = fb->base.modifier;
57 switch (modifier) {
58 case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED:
59 tiling->mode = TEGRA_BO_TILING_MODE_TILED;
60 tiling->value = 0;
61 break;
63 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
64 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
65 tiling->value = 0;
66 break;
68 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
69 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
70 tiling->value = 1;
71 break;
73 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
74 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
75 tiling->value = 2;
76 break;
78 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
79 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
80 tiling->value = 3;
81 break;
83 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
84 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
85 tiling->value = 4;
86 break;
88 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
89 tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
90 tiling->value = 5;
91 break;
93 default:
94 /* TODO: handle YUV formats? */
95 *tiling = fb->planes[0]->tiling;
96 break;
99 return 0;
102 static void tegra_fb_destroy(struct drm_framebuffer *framebuffer)
104 struct tegra_fb *fb = to_tegra_fb(framebuffer);
105 unsigned int i;
107 for (i = 0; i < fb->num_planes; i++) {
108 struct tegra_bo *bo = fb->planes[i];
110 if (bo) {
111 if (bo->pages)
112 vunmap(bo->vaddr);
114 drm_gem_object_put_unlocked(&bo->gem);
118 drm_framebuffer_cleanup(framebuffer);
119 kfree(fb->planes);
120 kfree(fb);
123 static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer,
124 struct drm_file *file, unsigned int *handle)
126 struct tegra_fb *fb = to_tegra_fb(framebuffer);
128 return drm_gem_handle_create(file, &fb->planes[0]->gem, handle);
131 static const struct drm_framebuffer_funcs tegra_fb_funcs = {
132 .destroy = tegra_fb_destroy,
133 .create_handle = tegra_fb_create_handle,
136 static struct tegra_fb *tegra_fb_alloc(struct drm_device *drm,
137 const struct drm_mode_fb_cmd2 *mode_cmd,
138 struct tegra_bo **planes,
139 unsigned int num_planes)
141 struct tegra_fb *fb;
142 unsigned int i;
143 int err;
145 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
146 if (!fb)
147 return ERR_PTR(-ENOMEM);
149 fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL);
150 if (!fb->planes) {
151 kfree(fb);
152 return ERR_PTR(-ENOMEM);
155 fb->num_planes = num_planes;
157 drm_helper_mode_fill_fb_struct(drm, &fb->base, mode_cmd);
159 for (i = 0; i < fb->num_planes; i++)
160 fb->planes[i] = planes[i];
162 err = drm_framebuffer_init(drm, &fb->base, &tegra_fb_funcs);
163 if (err < 0) {
164 dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
165 err);
166 kfree(fb->planes);
167 kfree(fb);
168 return ERR_PTR(err);
171 return fb;
174 struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
175 struct drm_file *file,
176 const struct drm_mode_fb_cmd2 *cmd)
178 unsigned int hsub, vsub, i;
179 struct tegra_bo *planes[4];
180 struct drm_gem_object *gem;
181 struct tegra_fb *fb;
182 int err;
184 hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
185 vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
187 for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) {
188 unsigned int width = cmd->width / (i ? hsub : 1);
189 unsigned int height = cmd->height / (i ? vsub : 1);
190 unsigned int size, bpp;
192 gem = drm_gem_object_lookup(file, cmd->handles[i]);
193 if (!gem) {
194 err = -ENXIO;
195 goto unreference;
198 bpp = drm_format_plane_cpp(cmd->pixel_format, i);
200 size = (height - 1) * cmd->pitches[i] +
201 width * bpp + cmd->offsets[i];
203 if (gem->size < size) {
204 err = -EINVAL;
205 goto unreference;
208 planes[i] = to_tegra_bo(gem);
211 fb = tegra_fb_alloc(drm, cmd, planes, i);
212 if (IS_ERR(fb)) {
213 err = PTR_ERR(fb);
214 goto unreference;
217 return &fb->base;
219 unreference:
220 while (i--)
221 drm_gem_object_put_unlocked(&planes[i]->gem);
223 return ERR_PTR(err);
226 #ifdef CONFIG_DRM_FBDEV_EMULATION
227 static struct fb_ops tegra_fb_ops = {
228 .owner = THIS_MODULE,
229 DRM_FB_HELPER_DEFAULT_OPS,
230 .fb_fillrect = drm_fb_helper_sys_fillrect,
231 .fb_copyarea = drm_fb_helper_sys_copyarea,
232 .fb_imageblit = drm_fb_helper_sys_imageblit,
235 static int tegra_fbdev_probe(struct drm_fb_helper *helper,
236 struct drm_fb_helper_surface_size *sizes)
238 struct tegra_fbdev *fbdev = to_tegra_fbdev(helper);
239 struct tegra_drm *tegra = helper->dev->dev_private;
240 struct drm_device *drm = helper->dev;
241 struct drm_mode_fb_cmd2 cmd = { 0 };
242 unsigned int bytes_per_pixel;
243 struct drm_framebuffer *fb;
244 unsigned long offset;
245 struct fb_info *info;
246 struct tegra_bo *bo;
247 size_t size;
248 int err;
250 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
252 cmd.width = sizes->surface_width;
253 cmd.height = sizes->surface_height;
254 cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
255 tegra->pitch_align);
257 cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
258 sizes->surface_depth);
260 size = cmd.pitches[0] * cmd.height;
262 bo = tegra_bo_create(drm, size, 0);
263 if (IS_ERR(bo))
264 return PTR_ERR(bo);
266 info = drm_fb_helper_alloc_fbi(helper);
267 if (IS_ERR(info)) {
268 dev_err(drm->dev, "failed to allocate framebuffer info\n");
269 drm_gem_object_put_unlocked(&bo->gem);
270 return PTR_ERR(info);
273 fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
274 if (IS_ERR(fbdev->fb)) {
275 err = PTR_ERR(fbdev->fb);
276 dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n",
277 err);
278 drm_gem_object_put_unlocked(&bo->gem);
279 return PTR_ERR(fbdev->fb);
282 fb = &fbdev->fb->base;
283 helper->fb = fb;
284 helper->fbdev = info;
286 info->par = helper;
287 info->flags = FBINFO_FLAG_DEFAULT;
288 info->fbops = &tegra_fb_ops;
290 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
291 drm_fb_helper_fill_var(info, helper, fb->width, fb->height);
293 offset = info->var.xoffset * bytes_per_pixel +
294 info->var.yoffset * fb->pitches[0];
296 if (bo->pages) {
297 bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
298 pgprot_writecombine(PAGE_KERNEL));
299 if (!bo->vaddr) {
300 dev_err(drm->dev, "failed to vmap() framebuffer\n");
301 err = -ENOMEM;
302 goto destroy;
306 drm->mode_config.fb_base = (resource_size_t)bo->paddr;
307 info->screen_base = (void __iomem *)bo->vaddr + offset;
308 info->screen_size = size;
309 info->fix.smem_start = (unsigned long)(bo->paddr + offset);
310 info->fix.smem_len = size;
312 return 0;
314 destroy:
315 drm_framebuffer_remove(fb);
316 return err;
319 static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
320 .fb_probe = tegra_fbdev_probe,
323 static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)
325 struct tegra_fbdev *fbdev;
327 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
328 if (!fbdev) {
329 dev_err(drm->dev, "failed to allocate DRM fbdev\n");
330 return ERR_PTR(-ENOMEM);
333 drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
335 return fbdev;
338 static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
340 kfree(fbdev);
343 static int tegra_fbdev_init(struct tegra_fbdev *fbdev,
344 unsigned int preferred_bpp,
345 unsigned int num_crtc,
346 unsigned int max_connectors)
348 struct drm_device *drm = fbdev->base.dev;
349 int err;
351 err = drm_fb_helper_init(drm, &fbdev->base, max_connectors);
352 if (err < 0) {
353 dev_err(drm->dev, "failed to initialize DRM FB helper: %d\n",
354 err);
355 return err;
358 err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
359 if (err < 0) {
360 dev_err(drm->dev, "failed to add connectors: %d\n", err);
361 goto fini;
364 err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
365 if (err < 0) {
366 dev_err(drm->dev, "failed to set initial configuration: %d\n",
367 err);
368 goto fini;
371 return 0;
373 fini:
374 drm_fb_helper_fini(&fbdev->base);
375 return err;
378 static void tegra_fbdev_exit(struct tegra_fbdev *fbdev)
380 drm_fb_helper_unregister_fbi(&fbdev->base);
382 if (fbdev->fb)
383 drm_framebuffer_remove(&fbdev->fb->base);
385 drm_fb_helper_fini(&fbdev->base);
386 tegra_fbdev_free(fbdev);
388 #endif
390 int tegra_drm_fb_prepare(struct drm_device *drm)
392 #ifdef CONFIG_DRM_FBDEV_EMULATION
393 struct tegra_drm *tegra = drm->dev_private;
395 tegra->fbdev = tegra_fbdev_create(drm);
396 if (IS_ERR(tegra->fbdev))
397 return PTR_ERR(tegra->fbdev);
398 #endif
400 return 0;
403 void tegra_drm_fb_free(struct drm_device *drm)
405 #ifdef CONFIG_DRM_FBDEV_EMULATION
406 struct tegra_drm *tegra = drm->dev_private;
408 tegra_fbdev_free(tegra->fbdev);
409 #endif
412 int tegra_drm_fb_init(struct drm_device *drm)
414 #ifdef CONFIG_DRM_FBDEV_EMULATION
415 struct tegra_drm *tegra = drm->dev_private;
416 int err;
418 err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc,
419 drm->mode_config.num_connector);
420 if (err < 0)
421 return err;
422 #endif
424 return 0;
427 void tegra_drm_fb_exit(struct drm_device *drm)
429 #ifdef CONFIG_DRM_FBDEV_EMULATION
430 struct tegra_drm *tegra = drm->dev_private;
432 tegra_fbdev_exit(tegra->fbdev);
433 #endif
436 void tegra_drm_fb_suspend(struct drm_device *drm)
438 #ifdef CONFIG_DRM_FBDEV_EMULATION
439 struct tegra_drm *tegra = drm->dev_private;
441 console_lock();
442 drm_fb_helper_set_suspend(&tegra->fbdev->base, 1);
443 console_unlock();
444 #endif
447 void tegra_drm_fb_resume(struct drm_device *drm)
449 #ifdef CONFIG_DRM_FBDEV_EMULATION
450 struct tegra_drm *tegra = drm->dev_private;
452 console_lock();
453 drm_fb_helper_set_suspend(&tegra->fbdev->base, 0);
454 console_unlock();
455 #endif