blob: ec5ae7cf43493e5b256e042fb54bfe5bdf5aec65 [file] [log] [blame]
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001/*
Arto Merilainende2ba662013-03-22 16:34:08 +02002 * Copyright (C) 2012-2013 Avionic Design GmbH
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00003 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
4 *
Arto Merilainende2ba662013-03-22 16:34:08 +02005 * Based on the KMS/FB CMA helpers
6 * Copyright (C) 2012 Analog Device Inc.
7 *
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00008 * 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.
11 */
12
Arto Merilainende2ba662013-03-22 16:34:08 +020013#include "drm.h"
14#include "gem.h"
15
16static inline struct tegra_fb *to_tegra_fb(struct drm_framebuffer *fb)
17{
18 return container_of(fb, struct tegra_fb, base);
19}
20
Thierry Reding60c2f702013-10-31 13:28:50 +010021#ifdef CONFIG_DRM_TEGRA_FBDEV
Arto Merilainende2ba662013-03-22 16:34:08 +020022static inline struct tegra_fbdev *to_tegra_fbdev(struct drm_fb_helper *helper)
23{
24 return container_of(helper, struct tegra_fbdev, base);
25}
Thierry Reding60c2f702013-10-31 13:28:50 +010026#endif
Arto Merilainende2ba662013-03-22 16:34:08 +020027
28struct tegra_bo *tegra_fb_get_plane(struct drm_framebuffer *framebuffer,
29 unsigned int index)
30{
31 struct tegra_fb *fb = to_tegra_fb(framebuffer);
32
33 if (index >= drm_format_num_planes(framebuffer->pixel_format))
34 return NULL;
35
36 return fb->planes[index];
37}
38
Thierry Redingdb7fbdf2013-10-07 09:47:58 +020039bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer)
40{
41 struct tegra_fb *fb = to_tegra_fb(framebuffer);
42
43 if (fb->planes[0]->flags & TEGRA_BO_BOTTOM_UP)
44 return true;
45
46 return false;
47}
48
Thierry Redingc134f012014-06-03 14:48:12 +020049int tegra_fb_get_tiling(struct drm_framebuffer *framebuffer,
50 struct tegra_bo_tiling *tiling)
Thierry Reding773af772013-10-04 22:34:01 +020051{
52 struct tegra_fb *fb = to_tegra_fb(framebuffer);
53
Thierry Redingc134f012014-06-03 14:48:12 +020054 /* TODO: handle YUV formats? */
55 *tiling = fb->planes[0]->tiling;
Thierry Reding773af772013-10-04 22:34:01 +020056
Thierry Redingc134f012014-06-03 14:48:12 +020057 return 0;
Thierry Reding773af772013-10-04 22:34:01 +020058}
59
Arto Merilainende2ba662013-03-22 16:34:08 +020060static void tegra_fb_destroy(struct drm_framebuffer *framebuffer)
61{
62 struct tegra_fb *fb = to_tegra_fb(framebuffer);
63 unsigned int i;
64
65 for (i = 0; i < fb->num_planes; i++) {
66 struct tegra_bo *bo = fb->planes[i];
67
Thierry Redingdf06b752014-06-26 21:41:53 +020068 if (bo) {
69 if (bo->pages && bo->vaddr)
70 vunmap(bo->vaddr);
71
Arto Merilainende2ba662013-03-22 16:34:08 +020072 drm_gem_object_unreference_unlocked(&bo->gem);
Thierry Redingdf06b752014-06-26 21:41:53 +020073 }
Arto Merilainende2ba662013-03-22 16:34:08 +020074 }
75
76 drm_framebuffer_cleanup(framebuffer);
77 kfree(fb->planes);
78 kfree(fb);
79}
80
81static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer,
82 struct drm_file *file, unsigned int *handle)
83{
84 struct tegra_fb *fb = to_tegra_fb(framebuffer);
85
86 return drm_gem_handle_create(file, &fb->planes[0]->gem, handle);
87}
88
89static struct drm_framebuffer_funcs tegra_fb_funcs = {
90 .destroy = tegra_fb_destroy,
91 .create_handle = tegra_fb_create_handle,
92};
93
94static struct tegra_fb *tegra_fb_alloc(struct drm_device *drm,
95 struct drm_mode_fb_cmd2 *mode_cmd,
96 struct tegra_bo **planes,
97 unsigned int num_planes)
98{
99 struct tegra_fb *fb;
100 unsigned int i;
101 int err;
102
103 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
104 if (!fb)
105 return ERR_PTR(-ENOMEM);
106
107 fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL);
Dan Carpenter6b7c79d2013-11-12 13:25:26 +0300108 if (!fb->planes) {
109 kfree(fb);
Arto Merilainende2ba662013-03-22 16:34:08 +0200110 return ERR_PTR(-ENOMEM);
Dan Carpenter6b7c79d2013-11-12 13:25:26 +0300111 }
Arto Merilainende2ba662013-03-22 16:34:08 +0200112
113 fb->num_planes = num_planes;
114
115 drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
116
117 for (i = 0; i < fb->num_planes; i++)
118 fb->planes[i] = planes[i];
119
120 err = drm_framebuffer_init(drm, &fb->base, &tegra_fb_funcs);
121 if (err < 0) {
122 dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
123 err);
124 kfree(fb->planes);
125 kfree(fb);
126 return ERR_PTR(err);
127 }
128
129 return fb;
130}
131
132static struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
133 struct drm_file *file,
134 struct drm_mode_fb_cmd2 *cmd)
135{
136 unsigned int hsub, vsub, i;
137 struct tegra_bo *planes[4];
138 struct drm_gem_object *gem;
139 struct tegra_fb *fb;
140 int err;
141
142 hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
143 vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
144
145 for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) {
146 unsigned int width = cmd->width / (i ? hsub : 1);
147 unsigned int height = cmd->height / (i ? vsub : 1);
148 unsigned int size, bpp;
149
150 gem = drm_gem_object_lookup(drm, file, cmd->handles[i]);
151 if (!gem) {
152 err = -ENXIO;
153 goto unreference;
154 }
155
156 bpp = drm_format_plane_cpp(cmd->pixel_format, i);
157
158 size = (height - 1) * cmd->pitches[i] +
159 width * bpp + cmd->offsets[i];
160
161 if (gem->size < size) {
162 err = -EINVAL;
163 goto unreference;
164 }
165
166 planes[i] = to_tegra_bo(gem);
167 }
168
169 fb = tegra_fb_alloc(drm, cmd, planes, i);
170 if (IS_ERR(fb)) {
171 err = PTR_ERR(fb);
172 goto unreference;
173 }
174
175 return &fb->base;
176
177unreference:
178 while (i--)
179 drm_gem_object_unreference_unlocked(&planes[i]->gem);
180
181 return ERR_PTR(err);
182}
183
Thierry Reding60c2f702013-10-31 13:28:50 +0100184#ifdef CONFIG_DRM_TEGRA_FBDEV
Arto Merilainende2ba662013-03-22 16:34:08 +0200185static struct fb_ops tegra_fb_ops = {
186 .owner = THIS_MODULE,
187 .fb_fillrect = sys_fillrect,
188 .fb_copyarea = sys_copyarea,
189 .fb_imageblit = sys_imageblit,
190 .fb_check_var = drm_fb_helper_check_var,
191 .fb_set_par = drm_fb_helper_set_par,
192 .fb_blank = drm_fb_helper_blank,
193 .fb_pan_display = drm_fb_helper_pan_display,
194 .fb_setcmap = drm_fb_helper_setcmap,
195};
196
197static int tegra_fbdev_probe(struct drm_fb_helper *helper,
198 struct drm_fb_helper_surface_size *sizes)
199{
200 struct tegra_fbdev *fbdev = to_tegra_fbdev(helper);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +0200201 struct tegra_drm *tegra = helper->dev->dev_private;
Arto Merilainende2ba662013-03-22 16:34:08 +0200202 struct drm_device *drm = helper->dev;
203 struct drm_mode_fb_cmd2 cmd = { 0 };
204 unsigned int bytes_per_pixel;
205 struct drm_framebuffer *fb;
206 unsigned long offset;
207 struct fb_info *info;
208 struct tegra_bo *bo;
209 size_t size;
210 int err;
211
212 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
213
214 cmd.width = sizes->surface_width;
215 cmd.height = sizes->surface_height;
Thierry Redingd1f3e1e2014-07-11 08:29:14 +0200216 cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
217 tegra->pitch_align);
Arto Merilainende2ba662013-03-22 16:34:08 +0200218 cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
219 sizes->surface_depth);
220
221 size = cmd.pitches[0] * cmd.height;
222
Thierry Reding773af772013-10-04 22:34:01 +0200223 bo = tegra_bo_create(drm, size, 0);
Arto Merilainende2ba662013-03-22 16:34:08 +0200224 if (IS_ERR(bo))
225 return PTR_ERR(bo);
226
227 info = framebuffer_alloc(0, drm->dev);
228 if (!info) {
229 dev_err(drm->dev, "failed to allocate framebuffer info\n");
230 tegra_bo_free_object(&bo->gem);
231 return -ENOMEM;
232 }
233
234 fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
235 if (IS_ERR(fbdev->fb)) {
236 dev_err(drm->dev, "failed to allocate DRM framebuffer\n");
237 err = PTR_ERR(fbdev->fb);
238 goto release;
239 }
240
241 fb = &fbdev->fb->base;
242 helper->fb = fb;
243 helper->fbdev = info;
244
245 info->par = helper;
246 info->flags = FBINFO_FLAG_DEFAULT;
247 info->fbops = &tegra_fb_ops;
248
249 err = fb_alloc_cmap(&info->cmap, 256, 0);
250 if (err < 0) {
251 dev_err(drm->dev, "failed to allocate color map: %d\n", err);
252 goto destroy;
253 }
254
255 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
256 drm_fb_helper_fill_var(info, helper, fb->width, fb->height);
257
258 offset = info->var.xoffset * bytes_per_pixel +
259 info->var.yoffset * fb->pitches[0];
260
Thierry Redingdf06b752014-06-26 21:41:53 +0200261 if (bo->pages) {
262 bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
263 pgprot_writecombine(PAGE_KERNEL));
264 if (!bo->vaddr) {
265 dev_err(drm->dev, "failed to vmap() framebuffer\n");
266 err = -ENOMEM;
267 goto destroy;
268 }
269 }
270
Arto Merilainende2ba662013-03-22 16:34:08 +0200271 drm->mode_config.fb_base = (resource_size_t)bo->paddr;
Thierry Reding9ab34152013-11-08 13:18:14 +0100272 info->screen_base = (void __iomem *)bo->vaddr + offset;
Arto Merilainende2ba662013-03-22 16:34:08 +0200273 info->screen_size = size;
274 info->fix.smem_start = (unsigned long)(bo->paddr + offset);
275 info->fix.smem_len = size;
276
277 return 0;
278
279destroy:
280 drm_framebuffer_unregister_private(fb);
281 tegra_fb_destroy(fb);
282release:
283 framebuffer_release(info);
284 return err;
285}
286
Thierry Reding3a493872014-06-27 17:19:23 +0200287static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
Arto Merilainende2ba662013-03-22 16:34:08 +0200288 .fb_probe = tegra_fbdev_probe,
289};
290
Thierry Redinge2215322014-06-27 17:19:25 +0200291static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)
Arto Merilainende2ba662013-03-22 16:34:08 +0200292{
Arto Merilainende2ba662013-03-22 16:34:08 +0200293 struct tegra_fbdev *fbdev;
Arto Merilainende2ba662013-03-22 16:34:08 +0200294
295 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
296 if (!fbdev) {
297 dev_err(drm->dev, "failed to allocate DRM fbdev\n");
298 return ERR_PTR(-ENOMEM);
299 }
300
Thierry Reding10a23102014-06-27 17:19:24 +0200301 drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
Arto Merilainende2ba662013-03-22 16:34:08 +0200302
Thierry Redinge2215322014-06-27 17:19:25 +0200303 return fbdev;
304}
305
Thierry Reding1d1e6fe2014-11-06 14:12:08 +0100306static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
307{
308 kfree(fbdev);
309}
310
Thierry Redinge2215322014-06-27 17:19:25 +0200311static int tegra_fbdev_init(struct tegra_fbdev *fbdev,
312 unsigned int preferred_bpp,
313 unsigned int num_crtc,
314 unsigned int max_connectors)
315{
316 struct drm_device *drm = fbdev->base.dev;
317 int err;
318
Arto Merilainende2ba662013-03-22 16:34:08 +0200319 err = drm_fb_helper_init(drm, &fbdev->base, num_crtc, max_connectors);
320 if (err < 0) {
321 dev_err(drm->dev, "failed to initialize DRM FB helper\n");
Thierry Redinge2215322014-06-27 17:19:25 +0200322 return err;
Arto Merilainende2ba662013-03-22 16:34:08 +0200323 }
324
325 err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
326 if (err < 0) {
327 dev_err(drm->dev, "failed to add connectors\n");
328 goto fini;
329 }
330
Arto Merilainende2ba662013-03-22 16:34:08 +0200331 err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
332 if (err < 0) {
333 dev_err(drm->dev, "failed to set initial configuration\n");
334 goto fini;
335 }
336
Thierry Redinge2215322014-06-27 17:19:25 +0200337 return 0;
Arto Merilainende2ba662013-03-22 16:34:08 +0200338
339fini:
340 drm_fb_helper_fini(&fbdev->base);
Thierry Redinge2215322014-06-27 17:19:25 +0200341 return err;
Arto Merilainende2ba662013-03-22 16:34:08 +0200342}
343
Thierry Reding1d1e6fe2014-11-06 14:12:08 +0100344static void tegra_fbdev_exit(struct tegra_fbdev *fbdev)
Arto Merilainende2ba662013-03-22 16:34:08 +0200345{
346 struct fb_info *info = fbdev->base.fbdev;
347
348 if (info) {
349 int err;
350
351 err = unregister_framebuffer(info);
352 if (err < 0)
353 DRM_DEBUG_KMS("failed to unregister framebuffer\n");
354
355 if (info->cmap.len)
356 fb_dealloc_cmap(&info->cmap);
357
358 framebuffer_release(info);
359 }
360
361 if (fbdev->fb) {
362 drm_framebuffer_unregister_private(&fbdev->fb->base);
Thierry Reding3feaf3e2014-10-21 13:56:59 +0200363 drm_framebuffer_remove(&fbdev->fb->base);
Arto Merilainende2ba662013-03-22 16:34:08 +0200364 }
365
366 drm_fb_helper_fini(&fbdev->base);
Thierry Reding1d1e6fe2014-11-06 14:12:08 +0100367 tegra_fbdev_free(fbdev);
Arto Merilainende2ba662013-03-22 16:34:08 +0200368}
369
Thierry Reding60c2f702013-10-31 13:28:50 +0100370void tegra_fbdev_restore_mode(struct tegra_fbdev *fbdev)
371{
Rob Clark5ea1f752014-05-30 12:29:48 -0400372 if (fbdev)
373 drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev->base);
Thierry Reding60c2f702013-10-31 13:28:50 +0100374}
375
Arto Merilainende2ba662013-03-22 16:34:08 +0200376static void tegra_fb_output_poll_changed(struct drm_device *drm)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000377{
Thierry Reding386a2a72013-09-24 13:22:17 +0200378 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000379
Thierry Reding386a2a72013-09-24 13:22:17 +0200380 if (tegra->fbdev)
381 drm_fb_helper_hotplug_event(&tegra->fbdev->base);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000382}
Thierry Reding60c2f702013-10-31 13:28:50 +0100383#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000384
385static const struct drm_mode_config_funcs tegra_drm_mode_funcs = {
Arto Merilainende2ba662013-03-22 16:34:08 +0200386 .fb_create = tegra_fb_create,
Thierry Reding60c2f702013-10-31 13:28:50 +0100387#ifdef CONFIG_DRM_TEGRA_FBDEV
Arto Merilainende2ba662013-03-22 16:34:08 +0200388 .output_poll_changed = tegra_fb_output_poll_changed,
Thierry Reding60c2f702013-10-31 13:28:50 +0100389#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000390};
391
Thierry Redinge2215322014-06-27 17:19:25 +0200392int tegra_drm_fb_prepare(struct drm_device *drm)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000393{
Thierry Reding60c2f702013-10-31 13:28:50 +0100394#ifdef CONFIG_DRM_TEGRA_FBDEV
Thierry Reding386a2a72013-09-24 13:22:17 +0200395 struct tegra_drm *tegra = drm->dev_private;
Thierry Reding60c2f702013-10-31 13:28:50 +0100396#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000397
398 drm->mode_config.min_width = 0;
399 drm->mode_config.min_height = 0;
400
401 drm->mode_config.max_width = 4096;
402 drm->mode_config.max_height = 4096;
403
404 drm->mode_config.funcs = &tegra_drm_mode_funcs;
405
Thierry Reding60c2f702013-10-31 13:28:50 +0100406#ifdef CONFIG_DRM_TEGRA_FBDEV
Thierry Redinge2215322014-06-27 17:19:25 +0200407 tegra->fbdev = tegra_fbdev_create(drm);
Thierry Reding60c2f702013-10-31 13:28:50 +0100408 if (IS_ERR(tegra->fbdev))
409 return PTR_ERR(tegra->fbdev);
410#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000411
412 return 0;
413}
414
Thierry Reding1d1e6fe2014-11-06 14:12:08 +0100415void tegra_drm_fb_free(struct drm_device *drm)
416{
417#ifdef CONFIG_DRM_TEGRA_FBDEV
418 struct tegra_drm *tegra = drm->dev_private;
419
420 tegra_fbdev_free(tegra->fbdev);
421#endif
422}
423
Thierry Redinge2215322014-06-27 17:19:25 +0200424int tegra_drm_fb_init(struct drm_device *drm)
425{
426#ifdef CONFIG_DRM_TEGRA_FBDEV
427 struct tegra_drm *tegra = drm->dev_private;
428 int err;
429
430 err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc,
431 drm->mode_config.num_connector);
432 if (err < 0)
433 return err;
434#endif
435
436 return 0;
437}
438
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000439void tegra_drm_fb_exit(struct drm_device *drm)
440{
Thierry Reding60c2f702013-10-31 13:28:50 +0100441#ifdef CONFIG_DRM_TEGRA_FBDEV
Thierry Reding386a2a72013-09-24 13:22:17 +0200442 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000443
Thierry Reding1d1e6fe2014-11-06 14:12:08 +0100444 tegra_fbdev_exit(tegra->fbdev);
Thierry Reding60c2f702013-10-31 13:28:50 +0100445#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000446}