xref: /openbmc/linux/drivers/gpu/drm/tegra/fb.c (revision ddc141e5)
1 /*
2  * Copyright (C) 2012-2013 Avionic Design GmbH
3  * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
4  *
5  * Based on the KMS/FB CMA helpers
6  *   Copyright (C) 2012 Analog Device Inc.
7  *
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.
11  */
12 
13 #include <linux/console.h>
14 
15 #include "drm.h"
16 #include "gem.h"
17 
18 static inline struct tegra_fb *to_tegra_fb(struct drm_framebuffer *fb)
19 {
20 	return container_of(fb, struct tegra_fb, base);
21 }
22 
23 #ifdef CONFIG_DRM_FBDEV_EMULATION
24 static inline struct tegra_fbdev *to_tegra_fbdev(struct drm_fb_helper *helper)
25 {
26 	return container_of(helper, struct tegra_fbdev, base);
27 }
28 #endif
29 
30 struct tegra_bo *tegra_fb_get_plane(struct drm_framebuffer *framebuffer,
31 				    unsigned int index)
32 {
33 	struct tegra_fb *fb = to_tegra_fb(framebuffer);
34 
35 	if (index >= framebuffer->format->num_planes)
36 		return NULL;
37 
38 	return fb->planes[index];
39 }
40 
41 bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer)
42 {
43 	struct tegra_fb *fb = to_tegra_fb(framebuffer);
44 
45 	if (fb->planes[0]->flags & TEGRA_BO_BOTTOM_UP)
46 		return true;
47 
48 	return false;
49 }
50 
51 int tegra_fb_get_tiling(struct drm_framebuffer *framebuffer,
52 			struct tegra_bo_tiling *tiling)
53 {
54 	struct tegra_fb *fb = to_tegra_fb(framebuffer);
55 	uint64_t modifier = fb->base.modifier;
56 
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;
62 
63 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
64 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
65 		tiling->value = 0;
66 		break;
67 
68 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
69 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
70 		tiling->value = 1;
71 		break;
72 
73 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
74 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
75 		tiling->value = 2;
76 		break;
77 
78 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
79 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
80 		tiling->value = 3;
81 		break;
82 
83 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
84 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
85 		tiling->value = 4;
86 		break;
87 
88 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
89 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
90 		tiling->value = 5;
91 		break;
92 
93 	default:
94 		/* TODO: handle YUV formats? */
95 		*tiling = fb->planes[0]->tiling;
96 		break;
97 	}
98 
99 	return 0;
100 }
101 
102 static void tegra_fb_destroy(struct drm_framebuffer *framebuffer)
103 {
104 	struct tegra_fb *fb = to_tegra_fb(framebuffer);
105 	unsigned int i;
106 
107 	for (i = 0; i < fb->num_planes; i++) {
108 		struct tegra_bo *bo = fb->planes[i];
109 
110 		if (bo) {
111 			if (bo->pages)
112 				vunmap(bo->vaddr);
113 
114 			drm_gem_object_put_unlocked(&bo->gem);
115 		}
116 	}
117 
118 	drm_framebuffer_cleanup(framebuffer);
119 	kfree(fb->planes);
120 	kfree(fb);
121 }
122 
123 static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer,
124 				  struct drm_file *file, unsigned int *handle)
125 {
126 	struct tegra_fb *fb = to_tegra_fb(framebuffer);
127 
128 	return drm_gem_handle_create(file, &fb->planes[0]->gem, handle);
129 }
130 
131 static const struct drm_framebuffer_funcs tegra_fb_funcs = {
132 	.destroy = tegra_fb_destroy,
133 	.create_handle = tegra_fb_create_handle,
134 };
135 
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)
140 {
141 	struct tegra_fb *fb;
142 	unsigned int i;
143 	int err;
144 
145 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
146 	if (!fb)
147 		return ERR_PTR(-ENOMEM);
148 
149 	fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL);
150 	if (!fb->planes) {
151 		kfree(fb);
152 		return ERR_PTR(-ENOMEM);
153 	}
154 
155 	fb->num_planes = num_planes;
156 
157 	drm_helper_mode_fill_fb_struct(drm, &fb->base, mode_cmd);
158 
159 	for (i = 0; i < fb->num_planes; i++)
160 		fb->planes[i] = planes[i];
161 
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);
169 	}
170 
171 	return fb;
172 }
173 
174 struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
175 					struct drm_file *file,
176 					const struct drm_mode_fb_cmd2 *cmd)
177 {
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;
183 
184 	hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
185 	vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
186 
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;
191 
192 		gem = drm_gem_object_lookup(file, cmd->handles[i]);
193 		if (!gem) {
194 			err = -ENXIO;
195 			goto unreference;
196 		}
197 
198 		bpp = drm_format_plane_cpp(cmd->pixel_format, i);
199 
200 		size = (height - 1) * cmd->pitches[i] +
201 		       width * bpp + cmd->offsets[i];
202 
203 		if (gem->size < size) {
204 			err = -EINVAL;
205 			goto unreference;
206 		}
207 
208 		planes[i] = to_tegra_bo(gem);
209 	}
210 
211 	fb = tegra_fb_alloc(drm, cmd, planes, i);
212 	if (IS_ERR(fb)) {
213 		err = PTR_ERR(fb);
214 		goto unreference;
215 	}
216 
217 	return &fb->base;
218 
219 unreference:
220 	while (i--)
221 		drm_gem_object_put_unlocked(&planes[i]->gem);
222 
223 	return ERR_PTR(err);
224 }
225 
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,
233 };
234 
235 static int tegra_fbdev_probe(struct drm_fb_helper *helper,
236 			     struct drm_fb_helper_surface_size *sizes)
237 {
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;
249 
250 	bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
251 
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);
256 
257 	cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
258 						     sizes->surface_depth);
259 
260 	size = cmd.pitches[0] * cmd.height;
261 
262 	bo = tegra_bo_create(drm, size, 0);
263 	if (IS_ERR(bo))
264 		return PTR_ERR(bo);
265 
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);
271 	}
272 
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);
280 	}
281 
282 	fb = &fbdev->fb->base;
283 	helper->fb = fb;
284 	helper->fbdev = info;
285 
286 	info->par = helper;
287 	info->flags = FBINFO_FLAG_DEFAULT;
288 	info->fbops = &tegra_fb_ops;
289 
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);
292 
293 	offset = info->var.xoffset * bytes_per_pixel +
294 		 info->var.yoffset * fb->pitches[0];
295 
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;
303 		}
304 	}
305 
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;
311 
312 	return 0;
313 
314 destroy:
315 	drm_framebuffer_remove(fb);
316 	return err;
317 }
318 
319 static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
320 	.fb_probe = tegra_fbdev_probe,
321 };
322 
323 static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)
324 {
325 	struct tegra_fbdev *fbdev;
326 
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);
331 	}
332 
333 	drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
334 
335 	return fbdev;
336 }
337 
338 static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
339 {
340 	kfree(fbdev);
341 }
342 
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)
347 {
348 	struct drm_device *drm = fbdev->base.dev;
349 	int err;
350 
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;
356 	}
357 
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;
362 	}
363 
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;
369 	}
370 
371 	return 0;
372 
373 fini:
374 	drm_fb_helper_fini(&fbdev->base);
375 	return err;
376 }
377 
378 static void tegra_fbdev_exit(struct tegra_fbdev *fbdev)
379 {
380 	drm_fb_helper_unregister_fbi(&fbdev->base);
381 
382 	if (fbdev->fb)
383 		drm_framebuffer_remove(&fbdev->fb->base);
384 
385 	drm_fb_helper_fini(&fbdev->base);
386 	tegra_fbdev_free(fbdev);
387 }
388 #endif
389 
390 int tegra_drm_fb_prepare(struct drm_device *drm)
391 {
392 #ifdef CONFIG_DRM_FBDEV_EMULATION
393 	struct tegra_drm *tegra = drm->dev_private;
394 
395 	tegra->fbdev = tegra_fbdev_create(drm);
396 	if (IS_ERR(tegra->fbdev))
397 		return PTR_ERR(tegra->fbdev);
398 #endif
399 
400 	return 0;
401 }
402 
403 void tegra_drm_fb_free(struct drm_device *drm)
404 {
405 #ifdef CONFIG_DRM_FBDEV_EMULATION
406 	struct tegra_drm *tegra = drm->dev_private;
407 
408 	tegra_fbdev_free(tegra->fbdev);
409 #endif
410 }
411 
412 int tegra_drm_fb_init(struct drm_device *drm)
413 {
414 #ifdef CONFIG_DRM_FBDEV_EMULATION
415 	struct tegra_drm *tegra = drm->dev_private;
416 	int err;
417 
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
423 
424 	return 0;
425 }
426 
427 void tegra_drm_fb_exit(struct drm_device *drm)
428 {
429 #ifdef CONFIG_DRM_FBDEV_EMULATION
430 	struct tegra_drm *tegra = drm->dev_private;
431 
432 	tegra_fbdev_exit(tegra->fbdev);
433 #endif
434 }
435 
436 void tegra_drm_fb_suspend(struct drm_device *drm)
437 {
438 #ifdef CONFIG_DRM_FBDEV_EMULATION
439 	struct tegra_drm *tegra = drm->dev_private;
440 
441 	console_lock();
442 	drm_fb_helper_set_suspend(&tegra->fbdev->base, 1);
443 	console_unlock();
444 #endif
445 }
446 
447 void tegra_drm_fb_resume(struct drm_device *drm)
448 {
449 #ifdef CONFIG_DRM_FBDEV_EMULATION
450 	struct tegra_drm *tegra = drm->dev_private;
451 
452 	console_lock();
453 	drm_fb_helper_set_suspend(&tegra->fbdev->base, 0);
454 	console_unlock();
455 #endif
456 }
457