1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics SA 2014
6 * for STMicroelectronics.
9 #include <linux/dma-mapping.h>
10 #include <linux/seq_file.h>
12 #include <drm/drm_atomic.h>
13 #include <drm/drm_device.h>
14 #include <drm/drm_fb_cma_helper.h>
15 #include <drm/drm_gem_cma_helper.h>
17 #include "sti_compositor.h"
18 #include "sti_cursor.h"
19 #include "sti_plane.h"
32 #define CUR_CTL_CLUT_UPDATE BIT(1)
34 #define STI_CURS_MIN_SIZE 1
35 #define STI_CURS_MAX_SIZE 128
38 * pixmap dma buffer structure
40 * @paddr: physical address
42 * @base: virtual address
51 * STI Cursor structure
53 * @sti_plane: sti_plane structure
55 * @regs: cursor registers
56 * @width: cursor width
57 * @height: cursor height
58 * @clut: color look up table
59 * @clut_paddr: color look up table physical address
60 * @pixmap: pixmap dma buffer (clut8-format cursor)
63 struct sti_plane plane;
69 dma_addr_t clut_paddr;
70 struct dma_pixmap pixmap;
73 static const uint32_t cursor_supported_formats[] = {
77 #define to_sti_cursor(x) container_of(x, struct sti_cursor, plane)
79 #define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
80 readl(cursor->regs + reg))
82 static void cursor_dbg_vpo(struct seq_file *s, u32 val)
84 seq_printf(s, "\txdo:%4d\tydo:%4d", val & 0x0FFF, (val >> 16) & 0x0FFF);
87 static void cursor_dbg_size(struct seq_file *s, u32 val)
89 seq_printf(s, "\t%d x %d", val & 0x07FF, (val >> 16) & 0x07FF);
92 static void cursor_dbg_pml(struct seq_file *s,
93 struct sti_cursor *cursor, u32 val)
95 if (cursor->pixmap.paddr == val)
96 seq_printf(s, "\tVirt @: %p", cursor->pixmap.base);
99 static void cursor_dbg_cml(struct seq_file *s,
100 struct sti_cursor *cursor, u32 val)
102 if (cursor->clut_paddr == val)
103 seq_printf(s, "\tVirt @: %p", cursor->clut);
106 static int cursor_dbg_show(struct seq_file *s, void *data)
108 struct drm_info_node *node = s->private;
109 struct sti_cursor *cursor = (struct sti_cursor *)node->info_ent->data;
111 seq_printf(s, "%s: (vaddr = 0x%p)",
112 sti_plane_to_str(&cursor->plane), cursor->regs);
116 cursor_dbg_vpo(s, readl(cursor->regs + CUR_VPO));
118 cursor_dbg_pml(s, cursor, readl(cursor->regs + CUR_PML));
120 DBGFS_DUMP(CUR_SIZE);
121 cursor_dbg_size(s, readl(cursor->regs + CUR_SIZE));
123 cursor_dbg_cml(s, cursor, readl(cursor->regs + CUR_CML));
130 static struct drm_info_list cursor_debugfs_files[] = {
131 { "cursor", cursor_dbg_show, 0, NULL },
134 static int cursor_debugfs_init(struct sti_cursor *cursor,
135 struct drm_minor *minor)
139 for (i = 0; i < ARRAY_SIZE(cursor_debugfs_files); i++)
140 cursor_debugfs_files[i].data = cursor;
142 return drm_debugfs_create_files(cursor_debugfs_files,
143 ARRAY_SIZE(cursor_debugfs_files),
144 minor->debugfs_root, minor);
147 static void sti_cursor_argb8888_to_clut8(struct sti_cursor *cursor, u32 *src)
149 u8 *dst = cursor->pixmap.base;
153 for (i = 0; i < cursor->height; i++) {
154 for (j = 0; j < cursor->width; j++) {
155 /* Pick the 2 higher bits of each component */
156 a = (*src >> 30) & 3;
157 r = (*src >> 22) & 3;
158 g = (*src >> 14) & 3;
160 *dst = a << 6 | r << 4 | g << 2 | b;
167 static void sti_cursor_init(struct sti_cursor *cursor)
169 unsigned short *base = cursor->clut;
170 unsigned int a, r, g, b;
172 /* Assign CLUT values, ARGB444 format */
173 for (a = 0; a < 4; a++)
174 for (r = 0; r < 4; r++)
175 for (g = 0; g < 4; g++)
176 for (b = 0; b < 4; b++)
177 *base++ = (a * 5) << 12 |
183 static int sti_cursor_atomic_check(struct drm_plane *drm_plane,
184 struct drm_plane_state *state)
186 struct sti_plane *plane = to_sti_plane(drm_plane);
187 struct sti_cursor *cursor = to_sti_cursor(plane);
188 struct drm_crtc *crtc = state->crtc;
189 struct drm_framebuffer *fb = state->fb;
190 struct drm_crtc_state *crtc_state;
191 struct drm_display_mode *mode;
192 int dst_x, dst_y, dst_w, dst_h;
195 /* no need for further checks if the plane is being disabled */
199 crtc_state = drm_atomic_get_crtc_state(state->state, crtc);
200 mode = &crtc_state->mode;
201 dst_x = state->crtc_x;
202 dst_y = state->crtc_y;
203 dst_w = clamp_val(state->crtc_w, 0, mode->crtc_hdisplay - dst_x);
204 dst_h = clamp_val(state->crtc_h, 0, mode->crtc_vdisplay - dst_y);
205 /* src_x are in 16.16 format */
206 src_w = state->src_w >> 16;
207 src_h = state->src_h >> 16;
209 if (src_w < STI_CURS_MIN_SIZE ||
210 src_h < STI_CURS_MIN_SIZE ||
211 src_w > STI_CURS_MAX_SIZE ||
212 src_h > STI_CURS_MAX_SIZE) {
213 DRM_ERROR("Invalid cursor size (%dx%d)\n",
218 /* If the cursor size has changed, re-allocated the pixmap */
219 if (!cursor->pixmap.base ||
220 (cursor->width != src_w) ||
221 (cursor->height != src_h)) {
222 cursor->width = src_w;
223 cursor->height = src_h;
225 if (cursor->pixmap.base)
226 dma_free_wc(cursor->dev, cursor->pixmap.size,
227 cursor->pixmap.base, cursor->pixmap.paddr);
229 cursor->pixmap.size = cursor->width * cursor->height;
231 cursor->pixmap.base = dma_alloc_wc(cursor->dev,
233 &cursor->pixmap.paddr,
234 GFP_KERNEL | GFP_DMA);
235 if (!cursor->pixmap.base) {
236 DRM_ERROR("Failed to allocate memory for pixmap\n");
241 if (!drm_fb_cma_get_gem_obj(fb, 0)) {
242 DRM_ERROR("Can't get CMA GEM object for fb\n");
246 DRM_DEBUG_KMS("CRTC:%d (%s) drm plane:%d (%s)\n",
247 crtc->base.id, sti_mixer_to_str(to_sti_mixer(crtc)),
248 drm_plane->base.id, sti_plane_to_str(plane));
249 DRM_DEBUG_KMS("(%dx%d)@(%d,%d)\n", dst_w, dst_h, dst_x, dst_y);
254 static void sti_cursor_atomic_update(struct drm_plane *drm_plane,
255 struct drm_plane_state *oldstate)
257 struct drm_plane_state *state = drm_plane->state;
258 struct sti_plane *plane = to_sti_plane(drm_plane);
259 struct sti_cursor *cursor = to_sti_cursor(plane);
260 struct drm_crtc *crtc = state->crtc;
261 struct drm_framebuffer *fb = state->fb;
262 struct drm_display_mode *mode;
264 struct drm_gem_cma_object *cma_obj;
272 dst_x = state->crtc_x;
273 dst_y = state->crtc_y;
275 cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
277 /* Convert ARGB8888 to CLUT8 */
278 sti_cursor_argb8888_to_clut8(cursor, (u32 *)cma_obj->vaddr);
280 /* AWS and AWE depend on the mode */
281 y = sti_vtg_get_line_number(*mode, 0);
282 x = sti_vtg_get_pixel_number(*mode, 0);
284 writel(val, cursor->regs + CUR_AWS);
285 y = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
286 x = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
288 writel(val, cursor->regs + CUR_AWE);
290 /* Set memory location, size, and position */
291 writel(cursor->pixmap.paddr, cursor->regs + CUR_PML);
292 writel(cursor->width, cursor->regs + CUR_PMP);
293 writel(cursor->height << 16 | cursor->width, cursor->regs + CUR_SIZE);
295 y = sti_vtg_get_line_number(*mode, dst_y);
296 x = sti_vtg_get_pixel_number(*mode, dst_x);
297 writel((y << 16) | x, cursor->regs + CUR_VPO);
299 /* Set and fetch CLUT */
300 writel(cursor->clut_paddr, cursor->regs + CUR_CML);
301 writel(CUR_CTL_CLUT_UPDATE, cursor->regs + CUR_CTL);
303 sti_plane_update_fps(plane, true, false);
305 plane->status = STI_PLANE_UPDATED;
308 static void sti_cursor_atomic_disable(struct drm_plane *drm_plane,
309 struct drm_plane_state *oldstate)
311 struct sti_plane *plane = to_sti_plane(drm_plane);
313 if (!oldstate->crtc) {
314 DRM_DEBUG_DRIVER("drm plane:%d not enabled\n",
319 DRM_DEBUG_DRIVER("CRTC:%d (%s) drm plane:%d (%s)\n",
320 oldstate->crtc->base.id,
321 sti_mixer_to_str(to_sti_mixer(oldstate->crtc)),
322 drm_plane->base.id, sti_plane_to_str(plane));
324 plane->status = STI_PLANE_DISABLING;
327 static const struct drm_plane_helper_funcs sti_cursor_helpers_funcs = {
328 .atomic_check = sti_cursor_atomic_check,
329 .atomic_update = sti_cursor_atomic_update,
330 .atomic_disable = sti_cursor_atomic_disable,
333 static void sti_cursor_destroy(struct drm_plane *drm_plane)
335 DRM_DEBUG_DRIVER("\n");
337 drm_plane_cleanup(drm_plane);
340 static int sti_cursor_late_register(struct drm_plane *drm_plane)
342 struct sti_plane *plane = to_sti_plane(drm_plane);
343 struct sti_cursor *cursor = to_sti_cursor(plane);
345 return cursor_debugfs_init(cursor, drm_plane->dev->primary);
348 static const struct drm_plane_funcs sti_cursor_plane_helpers_funcs = {
349 .update_plane = drm_atomic_helper_update_plane,
350 .disable_plane = drm_atomic_helper_disable_plane,
351 .destroy = sti_cursor_destroy,
352 .reset = sti_plane_reset,
353 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
354 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
355 .late_register = sti_cursor_late_register,
358 struct drm_plane *sti_cursor_create(struct drm_device *drm_dev,
359 struct device *dev, int desc,
360 void __iomem *baseaddr,
361 unsigned int possible_crtcs)
363 struct sti_cursor *cursor;
367 cursor = devm_kzalloc(dev, sizeof(*cursor), GFP_KERNEL);
369 DRM_ERROR("Failed to allocate memory for cursor\n");
373 /* Allocate clut buffer */
374 size = 0x100 * sizeof(unsigned short);
375 cursor->clut = dma_alloc_wc(dev, size, &cursor->clut_paddr,
376 GFP_KERNEL | GFP_DMA);
379 DRM_ERROR("Failed to allocate memory for cursor clut\n");
384 cursor->regs = baseaddr;
385 cursor->plane.desc = desc;
386 cursor->plane.status = STI_PLANE_DISABLED;
388 sti_cursor_init(cursor);
390 res = drm_universal_plane_init(drm_dev, &cursor->plane.drm_plane,
392 &sti_cursor_plane_helpers_funcs,
393 cursor_supported_formats,
394 ARRAY_SIZE(cursor_supported_formats),
395 NULL, DRM_PLANE_TYPE_CURSOR, NULL);
397 DRM_ERROR("Failed to initialize universal plane\n");
401 drm_plane_helper_add(&cursor->plane.drm_plane,
402 &sti_cursor_helpers_funcs);
404 sti_plane_init_property(&cursor->plane, DRM_PLANE_TYPE_CURSOR);
406 return &cursor->plane.drm_plane;
409 dma_free_wc(dev, size, cursor->clut, cursor->clut_paddr);
411 devm_kfree(dev, cursor);