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