xref: /linux/drivers/gpu/drm/exynos/exynos_drm_fb.c (revision 6c8c1406)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* exynos_drm_fb.c
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5  * Authors:
6  *	Inki Dae <inki.dae@samsung.com>
7  *	Joonyoung Shim <jy0922.shim@samsung.com>
8  *	Seung-Woo Kim <sw0312.kim@samsung.com>
9  */
10 
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_crtc.h>
14 #include <drm/drm_fb_helper.h>
15 #include <drm/drm_framebuffer.h>
16 #include <drm/drm_fourcc.h>
17 #include <drm/drm_gem_framebuffer_helper.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/exynos_drm.h>
20 
21 #include "exynos_drm_crtc.h"
22 #include "exynos_drm_drv.h"
23 #include "exynos_drm_fb.h"
24 #include "exynos_drm_fbdev.h"
25 
26 static int check_fb_gem_memory_type(struct drm_device *drm_dev,
27 				    struct exynos_drm_gem *exynos_gem)
28 {
29 	unsigned int flags;
30 
31 	/*
32 	 * if exynos drm driver supports iommu then framebuffer can use
33 	 * all the buffer types.
34 	 */
35 	if (is_drm_iommu_supported(drm_dev))
36 		return 0;
37 
38 	flags = exynos_gem->flags;
39 
40 	/*
41 	 * Physically non-contiguous memory type for framebuffer is not
42 	 * supported without IOMMU.
43 	 */
44 	if (IS_NONCONTIG_BUFFER(flags)) {
45 		DRM_DEV_ERROR(drm_dev->dev,
46 			      "Non-contiguous GEM memory is not supported.\n");
47 		return -EINVAL;
48 	}
49 
50 	return 0;
51 }
52 
53 static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
54 	.destroy	= drm_gem_fb_destroy,
55 	.create_handle	= drm_gem_fb_create_handle,
56 };
57 
58 struct drm_framebuffer *
59 exynos_drm_framebuffer_init(struct drm_device *dev,
60 			    const struct drm_mode_fb_cmd2 *mode_cmd,
61 			    struct exynos_drm_gem **exynos_gem,
62 			    int count)
63 {
64 	struct drm_framebuffer *fb;
65 	int i;
66 	int ret;
67 
68 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
69 	if (!fb)
70 		return ERR_PTR(-ENOMEM);
71 
72 	for (i = 0; i < count; i++) {
73 		ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
74 		if (ret < 0)
75 			goto err;
76 
77 		fb->obj[i] = &exynos_gem[i]->base;
78 	}
79 
80 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
81 
82 	ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
83 	if (ret < 0) {
84 		DRM_DEV_ERROR(dev->dev,
85 			      "failed to initialize framebuffer\n");
86 		goto err;
87 	}
88 
89 	return fb;
90 
91 err:
92 	kfree(fb);
93 	return ERR_PTR(ret);
94 }
95 
96 static struct drm_framebuffer *
97 exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
98 		      const struct drm_mode_fb_cmd2 *mode_cmd)
99 {
100 	const struct drm_format_info *info = drm_get_format_info(dev, mode_cmd);
101 	struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
102 	struct drm_framebuffer *fb;
103 	int i;
104 	int ret;
105 
106 	for (i = 0; i < info->num_planes; i++) {
107 		unsigned int height = (i == 0) ? mode_cmd->height :
108 				     DIV_ROUND_UP(mode_cmd->height, info->vsub);
109 		unsigned long size = height * mode_cmd->pitches[i] +
110 				     mode_cmd->offsets[i];
111 
112 		exynos_gem[i] = exynos_drm_gem_get(file_priv,
113 						   mode_cmd->handles[i]);
114 		if (!exynos_gem[i]) {
115 			DRM_DEV_ERROR(dev->dev,
116 				      "failed to lookup gem object\n");
117 			ret = -ENOENT;
118 			goto err;
119 		}
120 
121 		if (size > exynos_gem[i]->size) {
122 			i++;
123 			ret = -EINVAL;
124 			goto err;
125 		}
126 	}
127 
128 	fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
129 	if (IS_ERR(fb)) {
130 		ret = PTR_ERR(fb);
131 		goto err;
132 	}
133 
134 	return fb;
135 
136 err:
137 	while (i--)
138 		exynos_drm_gem_put(exynos_gem[i]);
139 
140 	return ERR_PTR(ret);
141 }
142 
143 dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
144 {
145 	struct exynos_drm_gem *exynos_gem;
146 
147 	if (WARN_ON_ONCE(index >= MAX_FB_BUFFER))
148 		return 0;
149 
150 	exynos_gem = to_exynos_gem(fb->obj[index]);
151 	return exynos_gem->dma_addr + fb->offsets[index];
152 }
153 
154 static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = {
155 	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
156 };
157 
158 static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
159 	.fb_create = exynos_user_fb_create,
160 	.output_poll_changed = drm_fb_helper_output_poll_changed,
161 	.atomic_check = drm_atomic_helper_check,
162 	.atomic_commit = drm_atomic_helper_commit,
163 };
164 
165 void exynos_drm_mode_config_init(struct drm_device *dev)
166 {
167 	dev->mode_config.min_width = 0;
168 	dev->mode_config.min_height = 0;
169 
170 	/*
171 	 * set max width and height as default value(4096x4096).
172 	 * this value would be used to check framebuffer size limitation
173 	 * at drm_mode_addfb().
174 	 */
175 	dev->mode_config.max_width = 4096;
176 	dev->mode_config.max_height = 4096;
177 
178 	dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
179 	dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
180 
181 	dev->mode_config.normalize_zpos = true;
182 }
183