File Index
Files and directories
Sign in
FILE

sti_cursor.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/sti/sti_cursor.c C
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) STMicroelectronics SA 2014
  4 * Authors: Vincent Abriou <vincent.abriou@st.com>
  5 *          Fabien Dessenne <fabien.dessenne@st.com>
  6 *          for STMicroelectronics.
  7 */
  8
  9#include <linux/dma-mapping.h>
 10#include <linux/seq_file.h>
 11
 12#include <drm/drm_atomic.h>
 13#include <drm/drm_device.h>
 14#include <drm/drm_fb_dma_helper.h>
 15#include <drm/drm_framebuffer.h>
 16#include <drm/drm_gem_dma_helper.h>
 17#include <drm/drm_print.h>
 18
 19#include "sti_compositor.h"
 20#include "sti_cursor.h"
 21#include "sti_plane.h"
 22#include "sti_vtg.h"
 23
 24/* Registers */
 25#define CUR_CTL             0x00
 26#define CUR_VPO             0x0C
 27#define CUR_PML             0x14
 28#define CUR_PMP             0x18
 29#define CUR_SIZE            0x1C
 30#define CUR_CML             0x20
 31#define CUR_AWS             0x28
 32#define CUR_AWE             0x2C
 33
 34#define CUR_CTL_CLUT_UPDATE BIT(1)
 35
 36#define STI_CURS_MIN_SIZE   1
 37#define STI_CURS_MAX_SIZE   128
 38
 39/*
 40 * pixmap dma buffer structure
 41 *
 42 * @paddr:  physical address
 43 * @size:   buffer size
 44 * @base:   virtual address
 45 */
 46struct dma_pixmap {
 47	dma_addr_t paddr;
 48	size_t size;
 49	void *base;
 50};
 51
 52/*
 53 * STI Cursor structure
 54 *
 55 * @sti_plane:    sti_plane structure
 56 * @dev:          driver device
 57 * @regs:         cursor registers
 58 * @width:        cursor width
 59 * @height:       cursor height
 60 * @clut:         color look up table
 61 * @clut_paddr:   color look up table physical address
 62 * @pixmap:       pixmap dma buffer (clut8-format cursor)
 63 */
 64struct sti_cursor {
 65	struct sti_plane plane;
 66	struct device *dev;
 67	void __iomem *regs;
 68	unsigned int width;
 69	unsigned int height;
 70	unsigned short *clut;
 71	dma_addr_t clut_paddr;
 72	struct dma_pixmap pixmap;
 73};
 74
 75static const uint32_t cursor_supported_formats[] = {
 76	DRM_FORMAT_ARGB8888,
 77};
 78
 79#define to_sti_cursor(x) container_of(x, struct sti_cursor, plane)
 80
 81#define DBGFS_DUMP(reg) seq_printf(s, "\n  %-25s 0x%08X", #reg, \
 82				   readl(cursor->regs + reg))
 83
 84static void cursor_dbg_vpo(struct seq_file *s, u32 val)
 85{
 86	seq_printf(s, "\txdo:%4d\tydo:%4d", val & 0x0FFF, (val >> 16) & 0x0FFF);
 87}
 88
 89static void cursor_dbg_size(struct seq_file *s, u32 val)
 90{
 91	seq_printf(s, "\t%d x %d", val & 0x07FF, (val >> 16) & 0x07FF);
 92}
 93
 94static void cursor_dbg_pml(struct seq_file *s,
 95			   struct sti_cursor *cursor, u32 val)
 96{
 97	if (cursor->pixmap.paddr == val)
 98		seq_printf(s, "\tVirt @: %p", cursor->pixmap.base);
 99}
100
101static void cursor_dbg_cml(struct seq_file *s,
102			   struct sti_cursor *cursor, u32 val)
103{
104	if (cursor->clut_paddr == val)
105		seq_printf(s, "\tVirt @: %p", cursor->clut);
106}
107
108static int cursor_dbg_show(struct seq_file *s, void *data)
109{
110	struct drm_info_node *node = s->private;
111	struct sti_cursor *cursor = (struct sti_cursor *)node->info_ent->data;
112
113	seq_printf(s, "%s: (vaddr = 0x%p)",
114		   sti_plane_to_str(&cursor->plane), cursor->regs);
115
116	DBGFS_DUMP(CUR_CTL);
117	DBGFS_DUMP(CUR_VPO);
118	cursor_dbg_vpo(s, readl(cursor->regs + CUR_VPO));
119	DBGFS_DUMP(CUR_PML);
120	cursor_dbg_pml(s, cursor, readl(cursor->regs + CUR_PML));
121	DBGFS_DUMP(CUR_PMP);
122	DBGFS_DUMP(CUR_SIZE);
123	cursor_dbg_size(s, readl(cursor->regs + CUR_SIZE));
124	DBGFS_DUMP(CUR_CML);
125	cursor_dbg_cml(s, cursor, readl(cursor->regs + CUR_CML));
126	DBGFS_DUMP(CUR_AWS);
127	DBGFS_DUMP(CUR_AWE);
128	seq_putc(s, '\n');
129	return 0;
130}
131
132static struct drm_info_list cursor_debugfs_files[] = {
133	{ "cursor", cursor_dbg_show, 0, NULL },
134};
135
136static void cursor_debugfs_init(struct sti_cursor *cursor,
137				struct drm_minor *minor)
138{
139	unsigned int i;
140
141	for (i = 0; i < ARRAY_SIZE(cursor_debugfs_files); i++)
142		cursor_debugfs_files[i].data = cursor;
143
144	drm_debugfs_create_files(cursor_debugfs_files,
145				 ARRAY_SIZE(cursor_debugfs_files),
146				 minor->debugfs_root, minor);
147}
148
149static void sti_cursor_argb8888_to_clut8(struct sti_cursor *cursor, u32 *src)
150{
151	u8  *dst = cursor->pixmap.base;
152	unsigned int i, j;
153	u32 a, r, g, b;
154
155	for (i = 0; i < cursor->height; i++) {
156		for (j = 0; j < cursor->width; j++) {
157			/* Pick the 2 higher bits of each component */
158			a = (*src >> 30) & 3;
159			r = (*src >> 22) & 3;
160			g = (*src >> 14) & 3;
161			b = (*src >> 6) & 3;
162			*dst = a << 6 | r << 4 | g << 2 | b;
163			src++;
164			dst++;
165		}
166	}
167}
168
169static void sti_cursor_init(struct sti_cursor *cursor)
170{
171	unsigned short *base = cursor->clut;
172	unsigned int a, r, g, b;
173
174	/* Assign CLUT values, ARGB444 format */
175	for (a = 0; a < 4; a++)
176		for (r = 0; r < 4; r++)
177			for (g = 0; g < 4; g++)
178				for (b = 0; b < 4; b++)
179					*base++ = (a * 5) << 12 |
180						  (r * 5) << 8 |
181						  (g * 5) << 4 |
182						  (b * 5);
183}
184
185static int sti_cursor_atomic_check(struct drm_plane *drm_plane,
186				   struct drm_atomic_state *state)
187{
188	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
189										 drm_plane);
190	struct sti_plane *plane = to_sti_plane(drm_plane);
191	struct sti_cursor *cursor = to_sti_cursor(plane);
192	struct drm_crtc *crtc = new_plane_state->crtc;
193	struct drm_framebuffer *fb = new_plane_state->fb;
194	struct drm_crtc_state *crtc_state;
195	struct drm_display_mode *mode;
196	int dst_x, dst_y, dst_w, dst_h;
197	int src_w, src_h;
198
199	/* no need for further checks if the plane is being disabled */
200	if (!crtc || !fb)
201		return 0;
202
203	crtc_state = drm_atomic_get_crtc_state(state, crtc);
204	if (IS_ERR(crtc_state))
205		return PTR_ERR(crtc_state);
206
207	mode = &crtc_state->mode;
208	dst_x = new_plane_state->crtc_x;
209	dst_y = new_plane_state->crtc_y;
210	dst_w = clamp_val(new_plane_state->crtc_w, 0,
211			  mode->crtc_hdisplay - dst_x);
212	dst_h = clamp_val(new_plane_state->crtc_h, 0,
213			  mode->crtc_vdisplay - dst_y);
214	/* src_x are in 16.16 format */
215	src_w = new_plane_state->src_w >> 16;
216	src_h = new_plane_state->src_h >> 16;
217
218	if (src_w < STI_CURS_MIN_SIZE ||
219	    src_h < STI_CURS_MIN_SIZE ||
220	    src_w > STI_CURS_MAX_SIZE ||
221	    src_h > STI_CURS_MAX_SIZE) {
222		DRM_ERROR("Invalid cursor size (%dx%d)\n",
223				src_w, src_h);
224		return -EINVAL;
225	}
226
227	/* If the cursor size has changed, re-allocated the pixmap */
228	if (!cursor->pixmap.base ||
229	    (cursor->width != src_w) ||
230	    (cursor->height != src_h)) {
231		cursor->width = src_w;
232		cursor->height = src_h;
233
234		if (cursor->pixmap.base)
235			dma_free_wc(cursor->dev, cursor->pixmap.size,
236				    cursor->pixmap.base, cursor->pixmap.paddr);
237
238		cursor->pixmap.size = cursor->width * cursor->height;
239
240		cursor->pixmap.base = dma_alloc_wc(cursor->dev,
241						   cursor->pixmap.size,
242						   &cursor->pixmap.paddr,
243						   GFP_KERNEL | GFP_DMA);
244		if (!cursor->pixmap.base) {
245			DRM_ERROR("Failed to allocate memory for pixmap\n");
246			return -EINVAL;
247		}
248	}
249
250	if (!drm_fb_dma_get_gem_obj(fb, 0)) {
251		DRM_ERROR("Can't get DMA GEM object for fb\n");
252		return -EINVAL;
253	}
254
255	DRM_DEBUG_KMS("CRTC:%d (%s) drm plane:%d (%s)\n",
256		      crtc->base.id, sti_mixer_to_str(to_sti_mixer(crtc)),
257		      drm_plane->base.id, sti_plane_to_str(plane));
258	DRM_DEBUG_KMS("(%dx%d)@(%d,%d)\n", dst_w, dst_h, dst_x, dst_y);
259
260	return 0;
261}
262
263static void sti_cursor_atomic_update(struct drm_plane *drm_plane,
264				     struct drm_atomic_state *state)
265{
266	struct drm_plane_state *newstate = drm_atomic_get_new_plane_state(state,
267									  drm_plane);
268	struct sti_plane *plane = to_sti_plane(drm_plane);
269	struct sti_cursor *cursor = to_sti_cursor(plane);
270	struct drm_crtc *crtc = newstate->crtc;
271	struct drm_framebuffer *fb = newstate->fb;
272	struct drm_display_mode *mode;
273	int dst_x, dst_y;
274	struct drm_gem_dma_object *dma_obj;
275	u32 y, x;
276	u32 val;
277
278	if (!crtc || !fb)
279		return;
280
281	mode = &crtc->mode;
282	dst_x = newstate->crtc_x;
283	dst_y = newstate->crtc_y;
284
285	dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
286
287	/* Convert ARGB8888 to CLUT8 */
288	sti_cursor_argb8888_to_clut8(cursor, (u32 *)dma_obj->vaddr);
289
290	/* AWS and AWE depend on the mode */
291	y = sti_vtg_get_line_number(*mode, 0);
292	x = sti_vtg_get_pixel_number(*mode, 0);
293	val = y << 16 | x;
294	writel(val, cursor->regs + CUR_AWS);
295	y = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
296	x = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
297	val = y << 16 | x;
298	writel(val, cursor->regs + CUR_AWE);
299
300	/* Set memory location, size, and position */
301	writel(cursor->pixmap.paddr, cursor->regs + CUR_PML);
302	writel(cursor->width, cursor->regs + CUR_PMP);
303	writel(cursor->height << 16 | cursor->width, cursor->regs + CUR_SIZE);
304
305	y = sti_vtg_get_line_number(*mode, dst_y);
306	x = sti_vtg_get_pixel_number(*mode, dst_x);
307	writel((y << 16) | x, cursor->regs + CUR_VPO);
308
309	/* Set and fetch CLUT */
310	writel(cursor->clut_paddr, cursor->regs + CUR_CML);
311	writel(CUR_CTL_CLUT_UPDATE, cursor->regs + CUR_CTL);
312
313	sti_plane_update_fps(plane, true, false);
314
315	plane->status = STI_PLANE_UPDATED;
316}
317
318static void sti_cursor_atomic_disable(struct drm_plane *drm_plane,
319				      struct drm_atomic_state *state)
320{
321	struct drm_plane_state *oldstate = drm_atomic_get_old_plane_state(state,
322									  drm_plane);
323	struct sti_plane *plane = to_sti_plane(drm_plane);
324
325	if (!oldstate->crtc) {
326		DRM_DEBUG_DRIVER("drm plane:%d not enabled\n",
327				 drm_plane->base.id);
328		return;
329	}
330
331	DRM_DEBUG_DRIVER("CRTC:%d (%s) drm plane:%d (%s)\n",
332			 oldstate->crtc->base.id,
333			 sti_mixer_to_str(to_sti_mixer(oldstate->crtc)),
334			 drm_plane->base.id, sti_plane_to_str(plane));
335
336	plane->status = STI_PLANE_DISABLING;
337}
338
339static const struct drm_plane_helper_funcs sti_cursor_helpers_funcs = {
340	.atomic_check = sti_cursor_atomic_check,
341	.atomic_update = sti_cursor_atomic_update,
342	.atomic_disable = sti_cursor_atomic_disable,
343};
344
345static int sti_cursor_late_register(struct drm_plane *drm_plane)
346{
347	struct sti_plane *plane = to_sti_plane(drm_plane);
348	struct sti_cursor *cursor = to_sti_cursor(plane);
349
350	cursor_debugfs_init(cursor, drm_plane->dev->primary);
351
352	return 0;
353}
354
355static const struct drm_plane_funcs sti_cursor_plane_helpers_funcs = {
356	.update_plane = drm_atomic_helper_update_plane,
357	.disable_plane = drm_atomic_helper_disable_plane,
358	.destroy = drm_plane_cleanup,
359	.reset = drm_atomic_helper_plane_reset,
360	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
361	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
362	.late_register = sti_cursor_late_register,
363};
364
365struct drm_plane *sti_cursor_create(struct drm_device *drm_dev,
366				    struct device *dev, int desc,
367				    void __iomem *baseaddr,
368				    unsigned int possible_crtcs)
369{
370	struct sti_cursor *cursor;
371	size_t size;
372	int res;
373
374	cursor = devm_kzalloc(dev, sizeof(*cursor), GFP_KERNEL);
375	if (!cursor) {
376		DRM_ERROR("Failed to allocate memory for cursor\n");
377		return NULL;
378	}
379
380	/* Allocate clut buffer */
381	size = 0x100 * sizeof(unsigned short);
382	cursor->clut = dma_alloc_wc(dev, size, &cursor->clut_paddr,
383				    GFP_KERNEL | GFP_DMA);
384
385	if (!cursor->clut) {
386		DRM_ERROR("Failed to allocate memory for cursor clut\n");
387		goto err_clut;
388	}
389
390	cursor->dev = dev;
391	cursor->regs = baseaddr;
392	cursor->plane.desc = desc;
393	cursor->plane.status = STI_PLANE_DISABLED;
394
395	sti_cursor_init(cursor);
396
397	res = drm_universal_plane_init(drm_dev, &cursor->plane.drm_plane,
398				       possible_crtcs,
399				       &sti_cursor_plane_helpers_funcs,
400				       cursor_supported_formats,
401				       ARRAY_SIZE(cursor_supported_formats),
402				       NULL, DRM_PLANE_TYPE_CURSOR, NULL);
403	if (res) {
404		DRM_ERROR("Failed to initialize universal plane\n");
405		goto err_plane;
406	}
407
408	drm_plane_helper_add(&cursor->plane.drm_plane,
409			     &sti_cursor_helpers_funcs);
410
411	sti_plane_init_property(&cursor->plane, DRM_PLANE_TYPE_CURSOR);
412
413	return &cursor->plane.drm_plane;
414
415err_plane:
416	dma_free_wc(dev, size, cursor->clut, cursor->clut_paddr);
417err_clut:
418	devm_kfree(dev, cursor);
419	return NULL;
420}
~/ Read-only file preview.