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