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>
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
);
30 struct tegra_bo
*tegra_fb_get_plane(struct drm_framebuffer
*framebuffer
,
33 struct tegra_fb
*fb
= to_tegra_fb(framebuffer
);
35 if (index
>= framebuffer
->format
->num_planes
)
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
)
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
;
58 case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED
:
59 tiling
->mode
= TEGRA_BO_TILING_MODE_TILED
;
63 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
64 tiling
->mode
= TEGRA_BO_TILING_MODE_BLOCK
;
68 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
69 tiling
->mode
= TEGRA_BO_TILING_MODE_BLOCK
;
73 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
74 tiling
->mode
= TEGRA_BO_TILING_MODE_BLOCK
;
78 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
79 tiling
->mode
= TEGRA_BO_TILING_MODE_BLOCK
;
83 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
84 tiling
->mode
= TEGRA_BO_TILING_MODE_BLOCK
;
88 case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
89 tiling
->mode
= TEGRA_BO_TILING_MODE_BLOCK
;
94 /* TODO: handle YUV formats? */
95 *tiling
= fb
->planes
[0]->tiling
;
102 static void tegra_fb_destroy(struct drm_framebuffer
*framebuffer
)
104 struct tegra_fb
*fb
= to_tegra_fb(framebuffer
);
107 for (i
= 0; i
< fb
->num_planes
; i
++) {
108 struct tegra_bo
*bo
= fb
->planes
[i
];
114 drm_gem_object_put_unlocked(&bo
->gem
);
118 drm_framebuffer_cleanup(framebuffer
);
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
)
145 fb
= kzalloc(sizeof(*fb
), GFP_KERNEL
);
147 return ERR_PTR(-ENOMEM
);
149 fb
->planes
= kzalloc(num_planes
* sizeof(*planes
), GFP_KERNEL
);
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
);
164 dev_err(drm
->dev
, "failed to initialize framebuffer: %d\n",
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
;
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
]);
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
) {
208 planes
[i
] = to_tegra_bo(gem
);
211 fb
= tegra_fb_alloc(drm
, cmd
, planes
, i
);
221 drm_gem_object_put_unlocked(&planes
[i
]->gem
);
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
;
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
,
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);
266 info
= drm_fb_helper_alloc_fbi(helper
);
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",
278 drm_gem_object_put_unlocked(&bo
->gem
);
279 return PTR_ERR(fbdev
->fb
);
282 fb
= &fbdev
->fb
->base
;
284 helper
->fbdev
= info
;
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];
297 bo
->vaddr
= vmap(bo
->pages
, bo
->num_pages
, VM_MAP
,
298 pgprot_writecombine(PAGE_KERNEL
));
300 dev_err(drm
->dev
, "failed to vmap() framebuffer\n");
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
;
315 drm_framebuffer_remove(fb
);
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
);
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
);
338 static void tegra_fbdev_free(struct tegra_fbdev
*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
;
351 err
= drm_fb_helper_init(drm
, &fbdev
->base
, max_connectors
);
353 dev_err(drm
->dev
, "failed to initialize DRM FB helper: %d\n",
358 err
= drm_fb_helper_single_add_all_connectors(&fbdev
->base
);
360 dev_err(drm
->dev
, "failed to add connectors: %d\n", err
);
364 err
= drm_fb_helper_initial_config(&fbdev
->base
, preferred_bpp
);
366 dev_err(drm
->dev
, "failed to set initial configuration: %d\n",
374 drm_fb_helper_fini(&fbdev
->base
);
378 static void tegra_fbdev_exit(struct tegra_fbdev
*fbdev
)
380 drm_fb_helper_unregister_fbi(&fbdev
->base
);
383 drm_framebuffer_remove(&fbdev
->fb
->base
);
385 drm_fb_helper_fini(&fbdev
->base
);
386 tegra_fbdev_free(fbdev
);
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
);
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
);
412 int tegra_drm_fb_init(struct drm_device
*drm
)
414 #ifdef CONFIG_DRM_FBDEV_EMULATION
415 struct tegra_drm
*tegra
= drm
->dev_private
;
418 err
= tegra_fbdev_init(tegra
->fbdev
, 32, drm
->mode_config
.num_crtc
,
419 drm
->mode_config
.num_connector
);
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
);
436 void tegra_drm_fb_suspend(struct drm_device
*drm
)
438 #ifdef CONFIG_DRM_FBDEV_EMULATION
439 struct tegra_drm
*tegra
= drm
->dev_private
;
442 drm_fb_helper_set_suspend(&tegra
->fbdev
->base
, 1);
447 void tegra_drm_fb_resume(struct drm_device
*drm
)
449 #ifdef CONFIG_DRM_FBDEV_EMULATION
450 struct tegra_drm
*tegra
= drm
->dev_private
;
453 drm_fb_helper_set_suspend(&tegra
->fbdev
->base
, 0);