File Index
Files and directories
Sign in
FILE

omap_fb.c

File preview with syntax highlighting.

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