blob: 7790d43ad082e2a9fed72f7183e2e386277e1602 [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
68 if (bo)
69 drm_gem_object_unreference_unlocked(&bo->gem);
70 }
71
72 drm_framebuffer_cleanup(framebuffer);
73 kfree(fb->planes);
74 kfree(fb);
75}
76
77static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer,
78 struct drm_file *file, unsigned int *handle)
79{
80 struct tegra_fb *fb = to_tegra_fb(framebuffer);
81
82 return drm_gem_handle_create(file, &fb->planes[0]->gem, handle);
83}
84
85static struct drm_framebuffer_funcs tegra_fb_funcs = {
86 .destroy = tegra_fb_destroy,
87 .create_handle = tegra_fb_create_handle,
88};
89
90static struct tegra_fb *tegra_fb_alloc(struct drm_device *drm,
91 struct drm_mode_fb_cmd2 *mode_cmd,
92 struct tegra_bo **planes,
93 unsigned int num_planes)
94{
95 struct tegra_fb *fb;
96 unsigned int i;
97 int err;
98
99 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
100 if (!fb)
101 return ERR_PTR(-ENOMEM);
102
103 fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL);
Dan Carpenter6b7c79d2013-11-12 13:25:26 +0300104 if (!fb->planes) {
105 kfree(fb);
Arto Merilainende2ba662013-03-22 16:34:08 +0200106 return ERR_PTR(-ENOMEM);
Dan Carpenter6b7c79d2013-11-12 13:25:26 +0300107 }
Arto Merilainende2ba662013-03-22 16:34:08 +0200108
109 fb->num_planes = num_planes;
110
111 drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
112
113 for (i = 0; i < fb->num_planes; i++)
114 fb->planes[i] = planes[i];
115
116 err = drm_framebuffer_init(drm, &fb->base, &tegra_fb_funcs);
117 if (err < 0) {
118 dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
119 err);
120 kfree(fb->planes);
121 kfree(fb);
122 return ERR_PTR(err);
123 }
124
125 return fb;
126}
127
128static struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
129 struct drm_file *file,
130 struct drm_mode_fb_cmd2 *cmd)
131{
132 unsigned int hsub, vsub, i;
133 struct tegra_bo *planes[4];
134 struct drm_gem_object *gem;
135 struct tegra_fb *fb;
136 int err;
137
138 hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
139 vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
140
141 for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) {
142 unsigned int width = cmd->width / (i ? hsub : 1);
143 unsigned int height = cmd->height / (i ? vsub : 1);
144 unsigned int size, bpp;
145
146 gem = drm_gem_object_lookup(drm, file, cmd->handles[i]);
147 if (!gem) {
148 err = -ENXIO;
149 goto unreference;
150 }
151
152 bpp = drm_format_plane_cpp(cmd->pixel_format, i);
153
154 size = (height - 1) * cmd->pitches[i] +
155 width * bpp + cmd->offsets[i];
156
157 if (gem->size < size) {
158 err = -EINVAL;
159 goto unreference;
160 }
161
162 planes[i] = to_tegra_bo(gem);
163 }
164
165 fb = tegra_fb_alloc(drm, cmd, planes, i);
166 if (IS_ERR(fb)) {
167 err = PTR_ERR(fb);
168 goto unreference;
169 }
170
171 return &fb->base;
172
173unreference:
174 while (i--)
175 drm_gem_object_unreference_unlocked(&planes[i]->gem);
176
177 return ERR_PTR(err);
178}
179
Thierry Reding60c2f702013-10-31 13:28:50 +0100180#ifdef CONFIG_DRM_TEGRA_FBDEV
Arto Merilainende2ba662013-03-22 16:34:08 +0200181static struct fb_ops tegra_fb_ops = {
182 .owner = THIS_MODULE,
183 .fb_fillrect = sys_fillrect,
184 .fb_copyarea = sys_copyarea,
185 .fb_imageblit = sys_imageblit,
186 .fb_check_var = drm_fb_helper_check_var,
187 .fb_set_par = drm_fb_helper_set_par,
188 .fb_blank = drm_fb_helper_blank,
189 .fb_pan_display = drm_fb_helper_pan_display,
190 .fb_setcmap = drm_fb_helper_setcmap,
191};
192
193static int tegra_fbdev_probe(struct drm_fb_helper *helper,
194 struct drm_fb_helper_surface_size *sizes)
195{
196 struct tegra_fbdev *fbdev = to_tegra_fbdev(helper);
197 struct drm_device *drm = helper->dev;
198 struct drm_mode_fb_cmd2 cmd = { 0 };
199 unsigned int bytes_per_pixel;
200 struct drm_framebuffer *fb;
201 unsigned long offset;
202 struct fb_info *info;
203 struct tegra_bo *bo;
204 size_t size;
205 int err;
206
207 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
208
209 cmd.width = sizes->surface_width;
210 cmd.height = sizes->surface_height;
211 cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
212 cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
213 sizes->surface_depth);
214
215 size = cmd.pitches[0] * cmd.height;
216
Thierry Reding773af772013-10-04 22:34:01 +0200217 bo = tegra_bo_create(drm, size, 0);
Arto Merilainende2ba662013-03-22 16:34:08 +0200218 if (IS_ERR(bo))
219 return PTR_ERR(bo);
220
221 info = framebuffer_alloc(0, drm->dev);
222 if (!info) {
223 dev_err(drm->dev, "failed to allocate framebuffer info\n");
224 tegra_bo_free_object(&bo->gem);
225 return -ENOMEM;
226 }
227
228 fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
229 if (IS_ERR(fbdev->fb)) {
230 dev_err(drm->dev, "failed to allocate DRM framebuffer\n");
231 err = PTR_ERR(fbdev->fb);
232 goto release;
233 }
234
235 fb = &fbdev->fb->base;
236 helper->fb = fb;
237 helper->fbdev = info;
238
239 info->par = helper;
240 info->flags = FBINFO_FLAG_DEFAULT;
241 info->fbops = &tegra_fb_ops;
242
243 err = fb_alloc_cmap(&info->cmap, 256, 0);
244 if (err < 0) {
245 dev_err(drm->dev, "failed to allocate color map: %d\n", err);
246 goto destroy;
247 }
248
249 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
250 drm_fb_helper_fill_var(info, helper, fb->width, fb->height);
251
252 offset = info->var.xoffset * bytes_per_pixel +
253 info->var.yoffset * fb->pitches[0];
254
255 drm->mode_config.fb_base = (resource_size_t)bo->paddr;
Thierry Reding9ab34152013-11-08 13:18:14 +0100256 info->screen_base = (void __iomem *)bo->vaddr + offset;
Arto Merilainende2ba662013-03-22 16:34:08 +0200257 info->screen_size = size;
258 info->fix.smem_start = (unsigned long)(bo->paddr + offset);
259 info->fix.smem_len = size;
260
261 return 0;
262
263destroy:
264 drm_framebuffer_unregister_private(fb);
265 tegra_fb_destroy(fb);
266release:
267 framebuffer_release(info);
268 return err;
269}
270
Thierry Reding3a493872014-06-27 17:19:23 +0200271static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
Arto Merilainende2ba662013-03-22 16:34:08 +0200272 .fb_probe = tegra_fbdev_probe,
273};
274
Thierry Redinge2215322014-06-27 17:19:25 +0200275static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)
Arto Merilainende2ba662013-03-22 16:34:08 +0200276{
Arto Merilainende2ba662013-03-22 16:34:08 +0200277 struct tegra_fbdev *fbdev;
Arto Merilainende2ba662013-03-22 16:34:08 +0200278
279 fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
280 if (!fbdev) {
281 dev_err(drm->dev, "failed to allocate DRM fbdev\n");
282 return ERR_PTR(-ENOMEM);
283 }
284
Thierry Reding10a23102014-06-27 17:19:24 +0200285 drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
Arto Merilainende2ba662013-03-22 16:34:08 +0200286
Thierry Redinge2215322014-06-27 17:19:25 +0200287 return fbdev;
288}
289
290static int tegra_fbdev_init(struct tegra_fbdev *fbdev,
291 unsigned int preferred_bpp,
292 unsigned int num_crtc,
293 unsigned int max_connectors)
294{
295 struct drm_device *drm = fbdev->base.dev;
296 int err;
297
Arto Merilainende2ba662013-03-22 16:34:08 +0200298 err = drm_fb_helper_init(drm, &fbdev->base, num_crtc, max_connectors);
299 if (err < 0) {
300 dev_err(drm->dev, "failed to initialize DRM FB helper\n");
Thierry Redinge2215322014-06-27 17:19:25 +0200301 return err;
Arto Merilainende2ba662013-03-22 16:34:08 +0200302 }
303
304 err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
305 if (err < 0) {
306 dev_err(drm->dev, "failed to add connectors\n");
307 goto fini;
308 }
309
Arto Merilainende2ba662013-03-22 16:34:08 +0200310 err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
311 if (err < 0) {
312 dev_err(drm->dev, "failed to set initial configuration\n");
313 goto fini;
314 }
315
Thierry Redinge2215322014-06-27 17:19:25 +0200316 return 0;
Arto Merilainende2ba662013-03-22 16:34:08 +0200317
318fini:
319 drm_fb_helper_fini(&fbdev->base);
Thierry Redinge2215322014-06-27 17:19:25 +0200320 return err;
Arto Merilainende2ba662013-03-22 16:34:08 +0200321}
322
323static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
324{
325 struct fb_info *info = fbdev->base.fbdev;
326
327 if (info) {
328 int err;
329
330 err = unregister_framebuffer(info);
331 if (err < 0)
332 DRM_DEBUG_KMS("failed to unregister framebuffer\n");
333
334 if (info->cmap.len)
335 fb_dealloc_cmap(&info->cmap);
336
337 framebuffer_release(info);
338 }
339
340 if (fbdev->fb) {
341 drm_framebuffer_unregister_private(&fbdev->fb->base);
342 tegra_fb_destroy(&fbdev->fb->base);
343 }
344
345 drm_fb_helper_fini(&fbdev->base);
346 kfree(fbdev);
347}
348
Thierry Reding60c2f702013-10-31 13:28:50 +0100349void tegra_fbdev_restore_mode(struct tegra_fbdev *fbdev)
350{
Rob Clark5ea1f752014-05-30 12:29:48 -0400351 if (fbdev)
352 drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev->base);
Thierry Reding60c2f702013-10-31 13:28:50 +0100353}
354
Arto Merilainende2ba662013-03-22 16:34:08 +0200355static void tegra_fb_output_poll_changed(struct drm_device *drm)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000356{
Thierry Reding386a2a72013-09-24 13:22:17 +0200357 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000358
Thierry Reding386a2a72013-09-24 13:22:17 +0200359 if (tegra->fbdev)
360 drm_fb_helper_hotplug_event(&tegra->fbdev->base);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000361}
Thierry Reding60c2f702013-10-31 13:28:50 +0100362#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000363
364static const struct drm_mode_config_funcs tegra_drm_mode_funcs = {
Arto Merilainende2ba662013-03-22 16:34:08 +0200365 .fb_create = tegra_fb_create,
Thierry Reding60c2f702013-10-31 13:28:50 +0100366#ifdef CONFIG_DRM_TEGRA_FBDEV
Arto Merilainende2ba662013-03-22 16:34:08 +0200367 .output_poll_changed = tegra_fb_output_poll_changed,
Thierry Reding60c2f702013-10-31 13:28:50 +0100368#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000369};
370
Thierry Redinge2215322014-06-27 17:19:25 +0200371int tegra_drm_fb_prepare(struct drm_device *drm)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000372{
Thierry Reding60c2f702013-10-31 13:28:50 +0100373#ifdef CONFIG_DRM_TEGRA_FBDEV
Thierry Reding386a2a72013-09-24 13:22:17 +0200374 struct tegra_drm *tegra = drm->dev_private;
Thierry Reding60c2f702013-10-31 13:28:50 +0100375#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000376
377 drm->mode_config.min_width = 0;
378 drm->mode_config.min_height = 0;
379
380 drm->mode_config.max_width = 4096;
381 drm->mode_config.max_height = 4096;
382
383 drm->mode_config.funcs = &tegra_drm_mode_funcs;
384
Thierry Reding60c2f702013-10-31 13:28:50 +0100385#ifdef CONFIG_DRM_TEGRA_FBDEV
Thierry Redinge2215322014-06-27 17:19:25 +0200386 tegra->fbdev = tegra_fbdev_create(drm);
Thierry Reding60c2f702013-10-31 13:28:50 +0100387 if (IS_ERR(tegra->fbdev))
388 return PTR_ERR(tegra->fbdev);
389#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000390
391 return 0;
392}
393
Thierry Redinge2215322014-06-27 17:19:25 +0200394int tegra_drm_fb_init(struct drm_device *drm)
395{
396#ifdef CONFIG_DRM_TEGRA_FBDEV
397 struct tegra_drm *tegra = drm->dev_private;
398 int err;
399
400 err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc,
401 drm->mode_config.num_connector);
402 if (err < 0)
403 return err;
404#endif
405
406 return 0;
407}
408
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000409void tegra_drm_fb_exit(struct drm_device *drm)
410{
Thierry Reding60c2f702013-10-31 13:28:50 +0100411#ifdef CONFIG_DRM_TEGRA_FBDEV
Thierry Reding386a2a72013-09-24 13:22:17 +0200412 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000413
Thierry Reding386a2a72013-09-24 13:22:17 +0200414 tegra_fbdev_free(tegra->fbdev);
Thierry Reding60c2f702013-10-31 13:28:50 +0100415#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000416}