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