xref: /openbmc/linux/drivers/gpu/drm/tegra/fb.c (revision 1fa0a7dc)
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 #include <drm/drm_gem_framebuffer_helper.h>
18 #include <drm/drm_modeset_helper.h>
19 
20 #ifdef CONFIG_DRM_FBDEV_EMULATION
21 static inline struct tegra_fbdev *to_tegra_fbdev(struct drm_fb_helper *helper)
22 {
23 	return container_of(helper, struct tegra_fbdev, base);
24 }
25 #endif
26 
27 struct tegra_bo *tegra_fb_get_plane(struct drm_framebuffer *framebuffer,
28 				    unsigned int index)
29 {
30 	return to_tegra_bo(drm_gem_fb_get_obj(framebuffer, index));
31 }
32 
33 bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer)
34 {
35 	struct tegra_bo *bo = tegra_fb_get_plane(framebuffer, 0);
36 
37 	if (bo->flags & TEGRA_BO_BOTTOM_UP)
38 		return true;
39 
40 	return false;
41 }
42 
43 int tegra_fb_get_tiling(struct drm_framebuffer *framebuffer,
44 			struct tegra_bo_tiling *tiling)
45 {
46 	uint64_t modifier = framebuffer->modifier;
47 
48 	switch (modifier) {
49 	case DRM_FORMAT_MOD_LINEAR:
50 		tiling->mode = TEGRA_BO_TILING_MODE_PITCH;
51 		tiling->value = 0;
52 		break;
53 
54 	case DRM_FORMAT_MOD_NVIDIA_TEGRA_TILED:
55 		tiling->mode = TEGRA_BO_TILING_MODE_TILED;
56 		tiling->value = 0;
57 		break;
58 
59 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(0):
60 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
61 		tiling->value = 0;
62 		break;
63 
64 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(1):
65 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
66 		tiling->value = 1;
67 		break;
68 
69 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(2):
70 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
71 		tiling->value = 2;
72 		break;
73 
74 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(3):
75 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
76 		tiling->value = 3;
77 		break;
78 
79 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(4):
80 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
81 		tiling->value = 4;
82 		break;
83 
84 	case DRM_FORMAT_MOD_NVIDIA_16BX2_BLOCK(5):
85 		tiling->mode = TEGRA_BO_TILING_MODE_BLOCK;
86 		tiling->value = 5;
87 		break;
88 
89 	default:
90 		return -EINVAL;
91 	}
92 
93 	return 0;
94 }
95 
96 static const struct drm_framebuffer_funcs tegra_fb_funcs = {
97 	.destroy = drm_gem_fb_destroy,
98 	.create_handle = drm_gem_fb_create_handle,
99 };
100 
101 static struct drm_framebuffer *tegra_fb_alloc(struct drm_device *drm,
102 					      const struct drm_mode_fb_cmd2 *mode_cmd,
103 					      struct tegra_bo **planes,
104 					      unsigned int num_planes)
105 {
106 	struct drm_framebuffer *fb;
107 	unsigned int i;
108 	int err;
109 
110 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
111 	if (!fb)
112 		return ERR_PTR(-ENOMEM);
113 
114 	drm_helper_mode_fill_fb_struct(drm, fb, mode_cmd);
115 
116 	for (i = 0; i < fb->format->num_planes; i++)
117 		fb->obj[i] = &planes[i]->gem;
118 
119 	err = drm_framebuffer_init(drm, fb, &tegra_fb_funcs);
120 	if (err < 0) {
121 		dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
122 			err);
123 		kfree(fb);
124 		return ERR_PTR(err);
125 	}
126 
127 	return fb;
128 }
129 
130 struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
131 					struct drm_file *file,
132 					const struct drm_mode_fb_cmd2 *cmd)
133 {
134 	unsigned int hsub, vsub, i;
135 	struct tegra_bo *planes[4];
136 	struct drm_gem_object *gem;
137 	struct drm_framebuffer *fb;
138 	int err;
139 
140 	hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
141 	vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
142 
143 	for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) {
144 		unsigned int width = cmd->width / (i ? hsub : 1);
145 		unsigned int height = cmd->height / (i ? vsub : 1);
146 		unsigned int size, bpp;
147 
148 		gem = drm_gem_object_lookup(file, cmd->handles[i]);
149 		if (!gem) {
150 			err = -ENXIO;
151 			goto unreference;
152 		}
153 
154 		bpp = drm_format_plane_cpp(cmd->pixel_format, i);
155 
156 		size = (height - 1) * cmd->pitches[i] +
157 		       width * bpp + cmd->offsets[i];
158 
159 		if (gem->size < size) {
160 			err = -EINVAL;
161 			goto unreference;
162 		}
163 
164 		planes[i] = to_tegra_bo(gem);
165 	}
166 
167 	fb = tegra_fb_alloc(drm, cmd, planes, i);
168 	if (IS_ERR(fb)) {
169 		err = PTR_ERR(fb);
170 		goto unreference;
171 	}
172 
173 	return fb;
174 
175 unreference:
176 	while (i--)
177 		drm_gem_object_put_unlocked(&planes[i]->gem);
178 
179 	return ERR_PTR(err);
180 }
181 
182 #ifdef CONFIG_DRM_FBDEV_EMULATION
183 static int tegra_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
184 {
185 	struct drm_fb_helper *helper = info->par;
186 	struct tegra_bo *bo;
187 	int err;
188 
189 	bo = tegra_fb_get_plane(helper->fb, 0);
190 
191 	err = drm_gem_mmap_obj(&bo->gem, bo->gem.size, vma);
192 	if (err < 0)
193 		return err;
194 
195 	return __tegra_gem_mmap(&bo->gem, vma);
196 }
197 
198 static struct fb_ops tegra_fb_ops = {
199 	.owner = THIS_MODULE,
200 	DRM_FB_HELPER_DEFAULT_OPS,
201 	.fb_fillrect = drm_fb_helper_sys_fillrect,
202 	.fb_copyarea = drm_fb_helper_sys_copyarea,
203 	.fb_imageblit = drm_fb_helper_sys_imageblit,
204 	.fb_mmap = tegra_fb_mmap,
205 };
206 
207 static int tegra_fbdev_probe(struct drm_fb_helper *helper,
208 			     struct drm_fb_helper_surface_size *sizes)
209 {
210 	struct tegra_fbdev *fbdev = to_tegra_fbdev(helper);
211 	struct tegra_drm *tegra = helper->dev->dev_private;
212 	struct drm_device *drm = helper->dev;
213 	struct drm_mode_fb_cmd2 cmd = { 0 };
214 	unsigned int bytes_per_pixel;
215 	struct drm_framebuffer *fb;
216 	unsigned long offset;
217 	struct fb_info *info;
218 	struct tegra_bo *bo;
219 	size_t size;
220 	int err;
221 
222 	bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
223 
224 	cmd.width = sizes->surface_width;
225 	cmd.height = sizes->surface_height;
226 	cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
227 				  tegra->pitch_align);
228 
229 	cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
230 						     sizes->surface_depth);
231 
232 	size = cmd.pitches[0] * cmd.height;
233 
234 	bo = tegra_bo_create(drm, size, 0);
235 	if (IS_ERR(bo))
236 		return PTR_ERR(bo);
237 
238 	info = drm_fb_helper_alloc_fbi(helper);
239 	if (IS_ERR(info)) {
240 		dev_err(drm->dev, "failed to allocate framebuffer info\n");
241 		drm_gem_object_put_unlocked(&bo->gem);
242 		return PTR_ERR(info);
243 	}
244 
245 	fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
246 	if (IS_ERR(fbdev->fb)) {
247 		err = PTR_ERR(fbdev->fb);
248 		dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n",
249 			err);
250 		drm_gem_object_put_unlocked(&bo->gem);
251 		return PTR_ERR(fbdev->fb);
252 	}
253 
254 	fb = fbdev->fb;
255 	helper->fb = fb;
256 	helper->fbdev = info;
257 
258 	info->fbops = &tegra_fb_ops;
259 
260 	drm_fb_helper_fill_info(info, helper, sizes);
261 
262 	offset = info->var.xoffset * bytes_per_pixel +
263 		 info->var.yoffset * fb->pitches[0];
264 
265 	if (bo->pages) {
266 		bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
267 				 pgprot_writecombine(PAGE_KERNEL));
268 		if (!bo->vaddr) {
269 			dev_err(drm->dev, "failed to vmap() framebuffer\n");
270 			err = -ENOMEM;
271 			goto destroy;
272 		}
273 	}
274 
275 	drm->mode_config.fb_base = (resource_size_t)bo->paddr;
276 	info->screen_base = (void __iomem *)bo->vaddr + offset;
277 	info->screen_size = size;
278 	info->fix.smem_start = (unsigned long)(bo->paddr + offset);
279 	info->fix.smem_len = size;
280 
281 	return 0;
282 
283 destroy:
284 	drm_framebuffer_remove(fb);
285 	return err;
286 }
287 
288 static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
289 	.fb_probe = tegra_fbdev_probe,
290 };
291 
292 static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)
293 {
294 	struct tegra_fbdev *fbdev;
295 
296 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
297 	if (!fbdev) {
298 		dev_err(drm->dev, "failed to allocate DRM fbdev\n");
299 		return ERR_PTR(-ENOMEM);
300 	}
301 
302 	drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
303 
304 	return fbdev;
305 }
306 
307 static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
308 {
309 	kfree(fbdev);
310 }
311 
312 static int tegra_fbdev_init(struct tegra_fbdev *fbdev,
313 			    unsigned int preferred_bpp,
314 			    unsigned int num_crtc,
315 			    unsigned int max_connectors)
316 {
317 	struct drm_device *drm = fbdev->base.dev;
318 	int err;
319 
320 	err = drm_fb_helper_init(drm, &fbdev->base, max_connectors);
321 	if (err < 0) {
322 		dev_err(drm->dev, "failed to initialize DRM FB helper: %d\n",
323 			err);
324 		return err;
325 	}
326 
327 	err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
328 	if (err < 0) {
329 		dev_err(drm->dev, "failed to add connectors: %d\n", err);
330 		goto fini;
331 	}
332 
333 	err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
334 	if (err < 0) {
335 		dev_err(drm->dev, "failed to set initial configuration: %d\n",
336 			err);
337 		goto fini;
338 	}
339 
340 	return 0;
341 
342 fini:
343 	drm_fb_helper_fini(&fbdev->base);
344 	return err;
345 }
346 
347 static void tegra_fbdev_exit(struct tegra_fbdev *fbdev)
348 {
349 	drm_fb_helper_unregister_fbi(&fbdev->base);
350 
351 	if (fbdev->fb) {
352 		struct tegra_bo *bo = tegra_fb_get_plane(fbdev->fb, 0);
353 
354 		/* Undo the special mapping we made in fbdev probe. */
355 		if (bo && bo->pages) {
356 			vunmap(bo->vaddr);
357 			bo->vaddr = NULL;
358 		}
359 
360 		drm_framebuffer_remove(fbdev->fb);
361 	}
362 
363 	drm_fb_helper_fini(&fbdev->base);
364 	tegra_fbdev_free(fbdev);
365 }
366 #endif
367 
368 int tegra_drm_fb_prepare(struct drm_device *drm)
369 {
370 #ifdef CONFIG_DRM_FBDEV_EMULATION
371 	struct tegra_drm *tegra = drm->dev_private;
372 
373 	tegra->fbdev = tegra_fbdev_create(drm);
374 	if (IS_ERR(tegra->fbdev))
375 		return PTR_ERR(tegra->fbdev);
376 #endif
377 
378 	return 0;
379 }
380 
381 void tegra_drm_fb_free(struct drm_device *drm)
382 {
383 #ifdef CONFIG_DRM_FBDEV_EMULATION
384 	struct tegra_drm *tegra = drm->dev_private;
385 
386 	tegra_fbdev_free(tegra->fbdev);
387 #endif
388 }
389 
390 int tegra_drm_fb_init(struct drm_device *drm)
391 {
392 #ifdef CONFIG_DRM_FBDEV_EMULATION
393 	struct tegra_drm *tegra = drm->dev_private;
394 	int err;
395 
396 	err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc,
397 			       drm->mode_config.num_connector);
398 	if (err < 0)
399 		return err;
400 #endif
401 
402 	return 0;
403 }
404 
405 void tegra_drm_fb_exit(struct drm_device *drm)
406 {
407 #ifdef CONFIG_DRM_FBDEV_EMULATION
408 	struct tegra_drm *tegra = drm->dev_private;
409 
410 	tegra_fbdev_exit(tegra->fbdev);
411 #endif
412 }
413