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