xref: /openbmc/linux/drivers/gpu/drm/tegra/fb.c (revision f7c35abe)
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 
56 	/* TODO: handle YUV formats? */
57 	*tiling = fb->planes[0]->tiling;
58 
59 	return 0;
60 }
61 
62 static void tegra_fb_destroy(struct drm_framebuffer *framebuffer)
63 {
64 	struct tegra_fb *fb = to_tegra_fb(framebuffer);
65 	unsigned int i;
66 
67 	for (i = 0; i < fb->num_planes; i++) {
68 		struct tegra_bo *bo = fb->planes[i];
69 
70 		if (bo) {
71 			if (bo->pages)
72 				vunmap(bo->vaddr);
73 
74 			drm_gem_object_unreference_unlocked(&bo->gem);
75 		}
76 	}
77 
78 	drm_framebuffer_cleanup(framebuffer);
79 	kfree(fb->planes);
80 	kfree(fb);
81 }
82 
83 static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer,
84 				  struct drm_file *file, unsigned int *handle)
85 {
86 	struct tegra_fb *fb = to_tegra_fb(framebuffer);
87 
88 	return drm_gem_handle_create(file, &fb->planes[0]->gem, handle);
89 }
90 
91 static const struct drm_framebuffer_funcs tegra_fb_funcs = {
92 	.destroy = tegra_fb_destroy,
93 	.create_handle = tegra_fb_create_handle,
94 };
95 
96 static struct tegra_fb *tegra_fb_alloc(struct drm_device *drm,
97 				       const struct drm_mode_fb_cmd2 *mode_cmd,
98 				       struct tegra_bo **planes,
99 				       unsigned int num_planes)
100 {
101 	struct tegra_fb *fb;
102 	unsigned int i;
103 	int err;
104 
105 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
106 	if (!fb)
107 		return ERR_PTR(-ENOMEM);
108 
109 	fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL);
110 	if (!fb->planes) {
111 		kfree(fb);
112 		return ERR_PTR(-ENOMEM);
113 	}
114 
115 	fb->num_planes = num_planes;
116 
117 	drm_helper_mode_fill_fb_struct(drm, &fb->base, mode_cmd);
118 
119 	for (i = 0; i < fb->num_planes; i++)
120 		fb->planes[i] = planes[i];
121 
122 	err = drm_framebuffer_init(drm, &fb->base, &tegra_fb_funcs);
123 	if (err < 0) {
124 		dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
125 			err);
126 		kfree(fb->planes);
127 		kfree(fb);
128 		return ERR_PTR(err);
129 	}
130 
131 	return fb;
132 }
133 
134 struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
135 					struct drm_file *file,
136 					const struct drm_mode_fb_cmd2 *cmd)
137 {
138 	unsigned int hsub, vsub, i;
139 	struct tegra_bo *planes[4];
140 	struct drm_gem_object *gem;
141 	struct tegra_fb *fb;
142 	int err;
143 
144 	hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
145 	vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
146 
147 	for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) {
148 		unsigned int width = cmd->width / (i ? hsub : 1);
149 		unsigned int height = cmd->height / (i ? vsub : 1);
150 		unsigned int size, bpp;
151 
152 		gem = drm_gem_object_lookup(file, cmd->handles[i]);
153 		if (!gem) {
154 			err = -ENXIO;
155 			goto unreference;
156 		}
157 
158 		bpp = drm_format_plane_cpp(cmd->pixel_format, i);
159 
160 		size = (height - 1) * cmd->pitches[i] +
161 		       width * bpp + cmd->offsets[i];
162 
163 		if (gem->size < size) {
164 			err = -EINVAL;
165 			goto unreference;
166 		}
167 
168 		planes[i] = to_tegra_bo(gem);
169 	}
170 
171 	fb = tegra_fb_alloc(drm, cmd, planes, i);
172 	if (IS_ERR(fb)) {
173 		err = PTR_ERR(fb);
174 		goto unreference;
175 	}
176 
177 	return &fb->base;
178 
179 unreference:
180 	while (i--)
181 		drm_gem_object_unreference_unlocked(&planes[i]->gem);
182 
183 	return ERR_PTR(err);
184 }
185 
186 #ifdef CONFIG_DRM_FBDEV_EMULATION
187 static struct fb_ops tegra_fb_ops = {
188 	.owner = THIS_MODULE,
189 	DRM_FB_HELPER_DEFAULT_OPS,
190 	.fb_fillrect = drm_fb_helper_sys_fillrect,
191 	.fb_copyarea = drm_fb_helper_sys_copyarea,
192 	.fb_imageblit = drm_fb_helper_sys_imageblit,
193 };
194 
195 static int tegra_fbdev_probe(struct drm_fb_helper *helper,
196 			     struct drm_fb_helper_surface_size *sizes)
197 {
198 	struct tegra_fbdev *fbdev = to_tegra_fbdev(helper);
199 	struct tegra_drm *tegra = helper->dev->dev_private;
200 	struct drm_device *drm = helper->dev;
201 	struct drm_mode_fb_cmd2 cmd = { 0 };
202 	unsigned int bytes_per_pixel;
203 	struct drm_framebuffer *fb;
204 	unsigned long offset;
205 	struct fb_info *info;
206 	struct tegra_bo *bo;
207 	size_t size;
208 	int err;
209 
210 	bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
211 
212 	cmd.width = sizes->surface_width;
213 	cmd.height = sizes->surface_height;
214 	cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
215 				  tegra->pitch_align);
216 	cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
217 						     sizes->surface_depth);
218 
219 	size = cmd.pitches[0] * cmd.height;
220 
221 	bo = tegra_bo_create(drm, size, 0);
222 	if (IS_ERR(bo))
223 		return PTR_ERR(bo);
224 
225 	info = drm_fb_helper_alloc_fbi(helper);
226 	if (IS_ERR(info)) {
227 		dev_err(drm->dev, "failed to allocate framebuffer info\n");
228 		drm_gem_object_unreference_unlocked(&bo->gem);
229 		return PTR_ERR(info);
230 	}
231 
232 	fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
233 	if (IS_ERR(fbdev->fb)) {
234 		err = PTR_ERR(fbdev->fb);
235 		dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n",
236 			err);
237 		drm_gem_object_unreference_unlocked(&bo->gem);
238 		return PTR_ERR(fbdev->fb);
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 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->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 	if (bo->pages) {
256 		bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
257 				 pgprot_writecombine(PAGE_KERNEL));
258 		if (!bo->vaddr) {
259 			dev_err(drm->dev, "failed to vmap() framebuffer\n");
260 			err = -ENOMEM;
261 			goto destroy;
262 		}
263 	}
264 
265 	drm->mode_config.fb_base = (resource_size_t)bo->paddr;
266 	info->screen_base = (void __iomem *)bo->vaddr + offset;
267 	info->screen_size = size;
268 	info->fix.smem_start = (unsigned long)(bo->paddr + offset);
269 	info->fix.smem_len = size;
270 
271 	return 0;
272 
273 destroy:
274 	drm_framebuffer_remove(fb);
275 	return err;
276 }
277 
278 static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
279 	.fb_probe = tegra_fbdev_probe,
280 };
281 
282 static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)
283 {
284 	struct tegra_fbdev *fbdev;
285 
286 	fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
287 	if (!fbdev) {
288 		dev_err(drm->dev, "failed to allocate DRM fbdev\n");
289 		return ERR_PTR(-ENOMEM);
290 	}
291 
292 	drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
293 
294 	return fbdev;
295 }
296 
297 static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
298 {
299 	kfree(fbdev);
300 }
301 
302 static int tegra_fbdev_init(struct tegra_fbdev *fbdev,
303 			    unsigned int preferred_bpp,
304 			    unsigned int num_crtc,
305 			    unsigned int max_connectors)
306 {
307 	struct drm_device *drm = fbdev->base.dev;
308 	int err;
309 
310 	err = drm_fb_helper_init(drm, &fbdev->base, max_connectors);
311 	if (err < 0) {
312 		dev_err(drm->dev, "failed to initialize DRM FB helper: %d\n",
313 			err);
314 		return err;
315 	}
316 
317 	err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
318 	if (err < 0) {
319 		dev_err(drm->dev, "failed to add connectors: %d\n", err);
320 		goto fini;
321 	}
322 
323 	err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
324 	if (err < 0) {
325 		dev_err(drm->dev, "failed to set initial configuration: %d\n",
326 			err);
327 		goto fini;
328 	}
329 
330 	return 0;
331 
332 fini:
333 	drm_fb_helper_fini(&fbdev->base);
334 	return err;
335 }
336 
337 static void tegra_fbdev_exit(struct tegra_fbdev *fbdev)
338 {
339 	drm_fb_helper_unregister_fbi(&fbdev->base);
340 
341 	if (fbdev->fb)
342 		drm_framebuffer_remove(&fbdev->fb->base);
343 
344 	drm_fb_helper_fini(&fbdev->base);
345 	tegra_fbdev_free(fbdev);
346 }
347 
348 void tegra_fbdev_restore_mode(struct tegra_fbdev *fbdev)
349 {
350 	if (fbdev)
351 		drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev->base);
352 }
353 
354 void tegra_fb_output_poll_changed(struct drm_device *drm)
355 {
356 	struct tegra_drm *tegra = drm->dev_private;
357 
358 	if (tegra->fbdev)
359 		drm_fb_helper_hotplug_event(&tegra->fbdev->base);
360 }
361 #endif
362 
363 int tegra_drm_fb_prepare(struct drm_device *drm)
364 {
365 #ifdef CONFIG_DRM_FBDEV_EMULATION
366 	struct tegra_drm *tegra = drm->dev_private;
367 
368 	tegra->fbdev = tegra_fbdev_create(drm);
369 	if (IS_ERR(tegra->fbdev))
370 		return PTR_ERR(tegra->fbdev);
371 #endif
372 
373 	return 0;
374 }
375 
376 void tegra_drm_fb_free(struct drm_device *drm)
377 {
378 #ifdef CONFIG_DRM_FBDEV_EMULATION
379 	struct tegra_drm *tegra = drm->dev_private;
380 
381 	tegra_fbdev_free(tegra->fbdev);
382 #endif
383 }
384 
385 int tegra_drm_fb_init(struct drm_device *drm)
386 {
387 #ifdef CONFIG_DRM_FBDEV_EMULATION
388 	struct tegra_drm *tegra = drm->dev_private;
389 	int err;
390 
391 	err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc,
392 			       drm->mode_config.num_connector);
393 	if (err < 0)
394 		return err;
395 #endif
396 
397 	return 0;
398 }
399 
400 void tegra_drm_fb_exit(struct drm_device *drm)
401 {
402 #ifdef CONFIG_DRM_FBDEV_EMULATION
403 	struct tegra_drm *tegra = drm->dev_private;
404 
405 	tegra_fbdev_exit(tegra->fbdev);
406 #endif
407 }
408 
409 void tegra_drm_fb_suspend(struct drm_device *drm)
410 {
411 #ifdef CONFIG_DRM_FBDEV_EMULATION
412 	struct tegra_drm *tegra = drm->dev_private;
413 
414 	console_lock();
415 	drm_fb_helper_set_suspend(&tegra->fbdev->base, 1);
416 	console_unlock();
417 #endif
418 }
419 
420 void tegra_drm_fb_resume(struct drm_device *drm)
421 {
422 #ifdef CONFIG_DRM_FBDEV_EMULATION
423 	struct tegra_drm *tegra = drm->dev_private;
424 
425 	console_lock();
426 	drm_fb_helper_set_suspend(&tegra->fbdev->base, 0);
427 	console_unlock();
428 #endif
429 }
430