xref: /openbmc/linux/drivers/gpu/drm/omapdrm/omap_fb.c (revision 22d55f02)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
4  * Author: Rob Clark <rob@ti.com>
5  */
6 
7 #include <linux/seq_file.h>
8 
9 #include <drm/drm_crtc.h>
10 #include <drm/drm_modeset_helper.h>
11 #include <drm/drm_gem_framebuffer_helper.h>
12 
13 #include "omap_dmm_tiler.h"
14 #include "omap_drv.h"
15 
16 /*
17  * framebuffer funcs
18  */
19 
20 static const u32 formats[] = {
21 	/* 16bpp [A]RGB: */
22 	DRM_FORMAT_RGB565, /* RGB16-565 */
23 	DRM_FORMAT_RGBX4444, /* RGB12x-4444 */
24 	DRM_FORMAT_XRGB4444, /* xRGB12-4444 */
25 	DRM_FORMAT_RGBA4444, /* RGBA12-4444 */
26 	DRM_FORMAT_ARGB4444, /* ARGB16-4444 */
27 	DRM_FORMAT_XRGB1555, /* xRGB15-1555 */
28 	DRM_FORMAT_ARGB1555, /* ARGB16-1555 */
29 	/* 24bpp RGB: */
30 	DRM_FORMAT_RGB888,   /* RGB24-888 */
31 	/* 32bpp [A]RGB: */
32 	DRM_FORMAT_RGBX8888, /* RGBx24-8888 */
33 	DRM_FORMAT_XRGB8888, /* xRGB24-8888 */
34 	DRM_FORMAT_RGBA8888, /* RGBA32-8888 */
35 	DRM_FORMAT_ARGB8888, /* ARGB32-8888 */
36 	/* YUV: */
37 	DRM_FORMAT_NV12,
38 	DRM_FORMAT_YUYV,
39 	DRM_FORMAT_UYVY,
40 };
41 
42 /* per-plane info for the fb: */
43 struct plane {
44 	dma_addr_t dma_addr;
45 };
46 
47 #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
48 
49 struct omap_framebuffer {
50 	struct drm_framebuffer base;
51 	int pin_count;
52 	const struct drm_format_info *format;
53 	struct plane planes[2];
54 	/* lock for pinning (pin_count and planes.dma_addr) */
55 	struct mutex lock;
56 };
57 
58 static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
59 	.create_handle = drm_gem_fb_create_handle,
60 	.destroy = drm_gem_fb_destroy,
61 };
62 
63 static u32 get_linear_addr(struct drm_framebuffer *fb,
64 		const struct drm_format_info *format, int n, int x, int y)
65 {
66 	struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
67 	struct plane *plane = &omap_fb->planes[n];
68 	u32 offset;
69 
70 	offset = fb->offsets[n]
71 	       + (x * format->cpp[n] / (n == 0 ? 1 : format->hsub))
72 	       + (y * fb->pitches[n] / (n == 0 ? 1 : format->vsub));
73 
74 	return plane->dma_addr + offset;
75 }
76 
77 bool omap_framebuffer_supports_rotation(struct drm_framebuffer *fb)
78 {
79 	return omap_gem_flags(fb->obj[0]) & OMAP_BO_TILED;
80 }
81 
82 /* Note: DRM rotates counter-clockwise, TILER & DSS rotates clockwise */
83 static u32 drm_rotation_to_tiler(unsigned int drm_rot)
84 {
85 	u32 orient;
86 
87 	switch (drm_rot & DRM_MODE_ROTATE_MASK) {
88 	default:
89 	case DRM_MODE_ROTATE_0:
90 		orient = 0;
91 		break;
92 	case DRM_MODE_ROTATE_90:
93 		orient = MASK_XY_FLIP | MASK_X_INVERT;
94 		break;
95 	case DRM_MODE_ROTATE_180:
96 		orient = MASK_X_INVERT | MASK_Y_INVERT;
97 		break;
98 	case DRM_MODE_ROTATE_270:
99 		orient = MASK_XY_FLIP | MASK_Y_INVERT;
100 		break;
101 	}
102 
103 	if (drm_rot & DRM_MODE_REFLECT_X)
104 		orient ^= MASK_X_INVERT;
105 
106 	if (drm_rot & DRM_MODE_REFLECT_Y)
107 		orient ^= MASK_Y_INVERT;
108 
109 	return orient;
110 }
111 
112 /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
113  */
114 void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
115 		struct drm_plane_state *state, struct omap_overlay_info *info)
116 {
117 	struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
118 	const struct drm_format_info *format = omap_fb->format;
119 	struct plane *plane = &omap_fb->planes[0];
120 	u32 x, y, orient = 0;
121 
122 	info->fourcc = fb->format->format;
123 
124 	info->pos_x      = state->crtc_x;
125 	info->pos_y      = state->crtc_y;
126 	info->out_width  = state->crtc_w;
127 	info->out_height = state->crtc_h;
128 	info->width      = state->src_w >> 16;
129 	info->height     = state->src_h >> 16;
130 
131 	/* DSS driver wants the w & h in rotated orientation */
132 	if (drm_rotation_90_or_270(state->rotation))
133 		swap(info->width, info->height);
134 
135 	x = state->src_x >> 16;
136 	y = state->src_y >> 16;
137 
138 	if (omap_gem_flags(fb->obj[0]) & OMAP_BO_TILED) {
139 		u32 w = state->src_w >> 16;
140 		u32 h = state->src_h >> 16;
141 
142 		orient = drm_rotation_to_tiler(state->rotation);
143 
144 		/*
145 		 * omap_gem_rotated_paddr() wants the x & y in tiler units.
146 		 * Usually tiler unit size is the same as the pixel size, except
147 		 * for YUV422 formats, for which the tiler unit size is 32 bits
148 		 * and pixel size is 16 bits.
149 		 */
150 		if (fb->format->format == DRM_FORMAT_UYVY ||
151 				fb->format->format == DRM_FORMAT_YUYV) {
152 			x /= 2;
153 			w /= 2;
154 		}
155 
156 		/* adjust x,y offset for invert: */
157 		if (orient & MASK_Y_INVERT)
158 			y += h - 1;
159 		if (orient & MASK_X_INVERT)
160 			x += w - 1;
161 
162 		/* Note: x and y are in TILER units, not pixels */
163 		omap_gem_rotated_dma_addr(fb->obj[0], orient, x, y,
164 					  &info->paddr);
165 		info->rotation_type = OMAP_DSS_ROT_TILER;
166 		info->rotation = state->rotation ?: DRM_MODE_ROTATE_0;
167 		/* Note: stride in TILER units, not pixels */
168 		info->screen_width  = omap_gem_tiled_stride(fb->obj[0], orient);
169 	} else {
170 		switch (state->rotation & DRM_MODE_ROTATE_MASK) {
171 		case 0:
172 		case DRM_MODE_ROTATE_0:
173 			/* OK */
174 			break;
175 
176 		default:
177 			dev_warn(fb->dev->dev,
178 				"rotation '%d' ignored for non-tiled fb\n",
179 				state->rotation);
180 			break;
181 		}
182 
183 		info->paddr         = get_linear_addr(fb, format, 0, x, y);
184 		info->rotation_type = OMAP_DSS_ROT_NONE;
185 		info->rotation      = DRM_MODE_ROTATE_0;
186 		info->screen_width  = fb->pitches[0];
187 	}
188 
189 	/* convert to pixels: */
190 	info->screen_width /= format->cpp[0];
191 
192 	if (fb->format->format == DRM_FORMAT_NV12) {
193 		plane = &omap_fb->planes[1];
194 
195 		if (info->rotation_type == OMAP_DSS_ROT_TILER) {
196 			WARN_ON(!(omap_gem_flags(fb->obj[1]) & OMAP_BO_TILED));
197 			omap_gem_rotated_dma_addr(fb->obj[1], orient, x/2, y/2,
198 						  &info->p_uv_addr);
199 		} else {
200 			info->p_uv_addr = get_linear_addr(fb, format, 1, x, y);
201 		}
202 	} else {
203 		info->p_uv_addr = 0;
204 	}
205 }
206 
207 /* pin, prepare for scanout: */
208 int omap_framebuffer_pin(struct drm_framebuffer *fb)
209 {
210 	struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
211 	int ret, i, n = fb->format->num_planes;
212 
213 	mutex_lock(&omap_fb->lock);
214 
215 	if (omap_fb->pin_count > 0) {
216 		omap_fb->pin_count++;
217 		mutex_unlock(&omap_fb->lock);
218 		return 0;
219 	}
220 
221 	for (i = 0; i < n; i++) {
222 		struct plane *plane = &omap_fb->planes[i];
223 		ret = omap_gem_pin(fb->obj[i], &plane->dma_addr);
224 		if (ret)
225 			goto fail;
226 		omap_gem_dma_sync_buffer(fb->obj[i], DMA_TO_DEVICE);
227 	}
228 
229 	omap_fb->pin_count++;
230 
231 	mutex_unlock(&omap_fb->lock);
232 
233 	return 0;
234 
235 fail:
236 	for (i--; i >= 0; i--) {
237 		struct plane *plane = &omap_fb->planes[i];
238 		omap_gem_unpin(fb->obj[i]);
239 		plane->dma_addr = 0;
240 	}
241 
242 	mutex_unlock(&omap_fb->lock);
243 
244 	return ret;
245 }
246 
247 /* unpin, no longer being scanned out: */
248 void omap_framebuffer_unpin(struct drm_framebuffer *fb)
249 {
250 	struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
251 	int i, n = fb->format->num_planes;
252 
253 	mutex_lock(&omap_fb->lock);
254 
255 	omap_fb->pin_count--;
256 
257 	if (omap_fb->pin_count > 0) {
258 		mutex_unlock(&omap_fb->lock);
259 		return;
260 	}
261 
262 	for (i = 0; i < n; i++) {
263 		struct plane *plane = &omap_fb->planes[i];
264 		omap_gem_unpin(fb->obj[i]);
265 		plane->dma_addr = 0;
266 	}
267 
268 	mutex_unlock(&omap_fb->lock);
269 }
270 
271 #ifdef CONFIG_DEBUG_FS
272 void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
273 {
274 	int i, n = fb->format->num_planes;
275 
276 	seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
277 			(char *)&fb->format->format);
278 
279 	for (i = 0; i < n; i++) {
280 		seq_printf(m, "   %d: offset=%d pitch=%d, obj: ",
281 				i, fb->offsets[n], fb->pitches[i]);
282 		omap_gem_describe(fb->obj[i], m);
283 	}
284 }
285 #endif
286 
287 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
288 		struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
289 {
290 	unsigned int num_planes = drm_format_num_planes(mode_cmd->pixel_format);
291 	struct drm_gem_object *bos[4];
292 	struct drm_framebuffer *fb;
293 	int i;
294 
295 	for (i = 0; i < num_planes; i++) {
296 		bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
297 		if (!bos[i]) {
298 			fb = ERR_PTR(-ENOENT);
299 			goto error;
300 		}
301 	}
302 
303 	fb = omap_framebuffer_init(dev, mode_cmd, bos);
304 	if (IS_ERR(fb))
305 		goto error;
306 
307 	return fb;
308 
309 error:
310 	while (--i >= 0)
311 		drm_gem_object_put_unlocked(bos[i]);
312 
313 	return fb;
314 }
315 
316 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
317 		const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
318 {
319 	const struct drm_format_info *format = NULL;
320 	struct omap_framebuffer *omap_fb = NULL;
321 	struct drm_framebuffer *fb = NULL;
322 	unsigned int pitch = mode_cmd->pitches[0];
323 	int ret, i;
324 
325 	DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
326 			dev, mode_cmd, mode_cmd->width, mode_cmd->height,
327 			(char *)&mode_cmd->pixel_format);
328 
329 	format = drm_format_info(mode_cmd->pixel_format);
330 
331 	for (i = 0; i < ARRAY_SIZE(formats); i++) {
332 		if (formats[i] == mode_cmd->pixel_format)
333 			break;
334 	}
335 
336 	if (!format || i == ARRAY_SIZE(formats)) {
337 		dev_dbg(dev->dev, "unsupported pixel format: %4.4s\n",
338 			(char *)&mode_cmd->pixel_format);
339 		ret = -EINVAL;
340 		goto fail;
341 	}
342 
343 	omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
344 	if (!omap_fb) {
345 		ret = -ENOMEM;
346 		goto fail;
347 	}
348 
349 	fb = &omap_fb->base;
350 	omap_fb->format = format;
351 	mutex_init(&omap_fb->lock);
352 
353 	/*
354 	 * The code below assumes that no format use more than two planes, and
355 	 * that the two planes of multiplane formats need the same number of
356 	 * bytes per pixel.
357 	 */
358 	if (format->num_planes == 2 && pitch != mode_cmd->pitches[1]) {
359 		dev_dbg(dev->dev, "pitches differ between planes 0 and 1\n");
360 		ret = -EINVAL;
361 		goto fail;
362 	}
363 
364 	if (pitch % format->cpp[0]) {
365 		dev_dbg(dev->dev,
366 			"buffer pitch (%u bytes) is not a multiple of pixel size (%u bytes)\n",
367 			pitch, format->cpp[0]);
368 		ret = -EINVAL;
369 		goto fail;
370 	}
371 
372 	for (i = 0; i < format->num_planes; i++) {
373 		struct plane *plane = &omap_fb->planes[i];
374 		unsigned int vsub = i == 0 ? 1 : format->vsub;
375 		unsigned int size;
376 
377 		size = pitch * mode_cmd->height / vsub;
378 
379 		if (size > omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i]) {
380 			dev_dbg(dev->dev,
381 				"provided buffer object is too small! %zu < %d\n",
382 				bos[i]->size - mode_cmd->offsets[i], size);
383 			ret = -EINVAL;
384 			goto fail;
385 		}
386 
387 		fb->obj[i]    = bos[i];
388 		plane->dma_addr  = 0;
389 	}
390 
391 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
392 
393 	ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
394 	if (ret) {
395 		dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
396 		goto fail;
397 	}
398 
399 	DBG("create: FB ID: %d (%p)", fb->base.id, fb);
400 
401 	return fb;
402 
403 fail:
404 	kfree(omap_fb);
405 
406 	return ERR_PTR(ret);
407 }
408