xref: /openbmc/linux/drivers/gpu/drm/msm/msm_fb.c (revision c6fbbf1e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #include <drm/drm_crtc.h>
8 #include <drm/drm_damage_helper.h>
9 #include <drm/drm_file.h>
10 #include <drm/drm_fourcc.h>
11 #include <drm/drm_gem_framebuffer_helper.h>
12 #include <drm/drm_probe_helper.h>
13 
14 #include "msm_drv.h"
15 #include "msm_kms.h"
16 #include "msm_gem.h"
17 
18 struct msm_framebuffer {
19 	struct drm_framebuffer base;
20 	const struct msm_format *format;
21 
22 	/* Count of # of attached planes which need dirtyfb: */
23 	refcount_t dirtyfb;
24 };
25 #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
26 
27 static struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
28 		const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos);
29 
30 static int msm_framebuffer_dirtyfb(struct drm_framebuffer *fb,
31 				   struct drm_file *file_priv, unsigned int flags,
32 				   unsigned int color, struct drm_clip_rect *clips,
33 				   unsigned int num_clips)
34 {
35 	struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
36 
37 	/* If this fb is not used on any display requiring pixel data to be
38 	 * flushed, then skip dirtyfb
39 	 */
40 	if (refcount_read(&msm_fb->dirtyfb) == 1)
41 		return 0;
42 
43 	return drm_atomic_helper_dirtyfb(fb, file_priv, flags, color,
44 					 clips, num_clips);
45 }
46 
47 static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
48 	.create_handle = drm_gem_fb_create_handle,
49 	.destroy = drm_gem_fb_destroy,
50 	.dirty = msm_framebuffer_dirtyfb,
51 };
52 
53 #ifdef CONFIG_DEBUG_FS
54 void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
55 {
56 	struct msm_gem_stats stats = {};
57 	int i, n = fb->format->num_planes;
58 
59 	seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n",
60 			fb->width, fb->height, (char *)&fb->format->format,
61 			drm_framebuffer_read_refcount(fb), fb->base.id);
62 
63 	for (i = 0; i < n; i++) {
64 		seq_printf(m, "   %d: offset=%d pitch=%d, obj: ",
65 				i, fb->offsets[i], fb->pitches[i]);
66 		msm_gem_describe(fb->obj[i], m, &stats);
67 	}
68 }
69 #endif
70 
71 /* prepare/pin all the fb's bo's for scanout.
72  */
73 int msm_framebuffer_prepare(struct drm_framebuffer *fb,
74 		struct msm_gem_address_space *aspace,
75 		bool needs_dirtyfb)
76 {
77 	struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
78 	int ret, i, n = fb->format->num_planes;
79 	uint64_t iova;
80 
81 	if (needs_dirtyfb)
82 		refcount_inc(&msm_fb->dirtyfb);
83 
84 	for (i = 0; i < n; i++) {
85 		ret = msm_gem_get_and_pin_iova(fb->obj[i], aspace, &iova);
86 		drm_dbg_state(fb->dev, "FB[%u]: iova[%d]: %08llx (%d)", fb->base.id, i, iova, ret);
87 		if (ret)
88 			return ret;
89 	}
90 
91 	return 0;
92 }
93 
94 void msm_framebuffer_cleanup(struct drm_framebuffer *fb,
95 		struct msm_gem_address_space *aspace,
96 		bool needed_dirtyfb)
97 {
98 	struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
99 	int i, n = fb->format->num_planes;
100 
101 	if (needed_dirtyfb)
102 		refcount_dec(&msm_fb->dirtyfb);
103 
104 	for (i = 0; i < n; i++)
105 		msm_gem_unpin_iova(fb->obj[i], aspace);
106 }
107 
108 uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb,
109 		struct msm_gem_address_space *aspace, int plane)
110 {
111 	if (!fb->obj[plane])
112 		return 0;
113 	return msm_gem_iova(fb->obj[plane], aspace) + fb->offsets[plane];
114 }
115 
116 struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
117 {
118 	return drm_gem_fb_get_obj(fb, plane);
119 }
120 
121 const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
122 {
123 	struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
124 	return msm_fb->format;
125 }
126 
127 struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
128 		struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
129 {
130 	const struct drm_format_info *info = drm_get_format_info(dev,
131 								 mode_cmd);
132 	struct drm_gem_object *bos[4] = {0};
133 	struct drm_framebuffer *fb;
134 	int ret, i, n = info->num_planes;
135 
136 	for (i = 0; i < n; i++) {
137 		bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
138 		if (!bos[i]) {
139 			ret = -ENXIO;
140 			goto out_unref;
141 		}
142 	}
143 
144 	fb = msm_framebuffer_init(dev, mode_cmd, bos);
145 	if (IS_ERR(fb)) {
146 		ret = PTR_ERR(fb);
147 		goto out_unref;
148 	}
149 
150 	return fb;
151 
152 out_unref:
153 	for (i = 0; i < n; i++)
154 		drm_gem_object_put(bos[i]);
155 	return ERR_PTR(ret);
156 }
157 
158 static struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
159 		const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
160 {
161 	const struct drm_format_info *info = drm_get_format_info(dev,
162 								 mode_cmd);
163 	struct msm_drm_private *priv = dev->dev_private;
164 	struct msm_kms *kms = priv->kms;
165 	struct msm_framebuffer *msm_fb = NULL;
166 	struct drm_framebuffer *fb;
167 	const struct msm_format *format;
168 	int ret, i, n;
169 
170 	drm_dbg_state(dev, "create framebuffer: mode_cmd=%p (%dx%d@%4.4s)",
171 			mode_cmd, mode_cmd->width, mode_cmd->height,
172 			(char *)&mode_cmd->pixel_format);
173 
174 	n = info->num_planes;
175 	format = kms->funcs->get_format(kms, mode_cmd->pixel_format,
176 			mode_cmd->modifier[0]);
177 	if (!format) {
178 		DRM_DEV_ERROR(dev->dev, "unsupported pixel format: %4.4s\n",
179 				(char *)&mode_cmd->pixel_format);
180 		ret = -EINVAL;
181 		goto fail;
182 	}
183 
184 	msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
185 	if (!msm_fb) {
186 		ret = -ENOMEM;
187 		goto fail;
188 	}
189 
190 	fb = &msm_fb->base;
191 
192 	msm_fb->format = format;
193 
194 	if (n > ARRAY_SIZE(fb->obj)) {
195 		ret = -EINVAL;
196 		goto fail;
197 	}
198 
199 	for (i = 0; i < n; i++) {
200 		unsigned int width = mode_cmd->width / (i ? info->hsub : 1);
201 		unsigned int height = mode_cmd->height / (i ? info->vsub : 1);
202 		unsigned int min_size;
203 
204 		min_size = (height - 1) * mode_cmd->pitches[i]
205 			 + width * info->cpp[i]
206 			 + mode_cmd->offsets[i];
207 
208 		if (bos[i]->size < min_size) {
209 			ret = -EINVAL;
210 			goto fail;
211 		}
212 
213 		msm_fb->base.obj[i] = bos[i];
214 	}
215 
216 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
217 
218 	ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
219 	if (ret) {
220 		DRM_DEV_ERROR(dev->dev, "framebuffer init failed: %d\n", ret);
221 		goto fail;
222 	}
223 
224 	refcount_set(&msm_fb->dirtyfb, 1);
225 
226 	drm_dbg_state(dev, "create: FB ID: %d (%p)", fb->base.id, fb);
227 
228 	return fb;
229 
230 fail:
231 	kfree(msm_fb);
232 
233 	return ERR_PTR(ret);
234 }
235 
236 struct drm_framebuffer *
237 msm_alloc_stolen_fb(struct drm_device *dev, int w, int h, int p, uint32_t format)
238 {
239 	struct drm_mode_fb_cmd2 mode_cmd = {
240 		.pixel_format = format,
241 		.width = w,
242 		.height = h,
243 		.pitches = { p },
244 	};
245 	struct drm_gem_object *bo;
246 	struct drm_framebuffer *fb;
247 	int size;
248 
249 	/* allocate backing bo */
250 	size = mode_cmd.pitches[0] * mode_cmd.height;
251 	DBG("allocating %d bytes for fb %d", size, dev->primary->index);
252 	bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC | MSM_BO_STOLEN);
253 	if (IS_ERR(bo)) {
254 		dev_warn(dev->dev, "could not allocate stolen bo\n");
255 		/* try regular bo: */
256 		bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
257 	}
258 	if (IS_ERR(bo)) {
259 		DRM_DEV_ERROR(dev->dev, "failed to allocate buffer object\n");
260 		return ERR_CAST(bo);
261 	}
262 
263 	msm_gem_object_set_name(bo, "stolenfb");
264 
265 	fb = msm_framebuffer_init(dev, &mode_cmd, &bo);
266 	if (IS_ERR(fb)) {
267 		DRM_DEV_ERROR(dev->dev, "failed to allocate fb\n");
268 		/* note: if fb creation failed, we can't rely on fb destroy
269 		 * to unref the bo:
270 		 */
271 		drm_gem_object_put(bo);
272 		return ERR_CAST(fb);
273 	}
274 
275 	return fb;
276 }
277