2 * Copyright (C) STMicroelectronics SA 2014
3 * Authors: Vincent Abriou <vincent.abriou@st.com>
4 * Fabien Dessenne <fabien.dessenne@st.com>
5 * for STMicroelectronics.
6 * License terms: GNU General Public License (GPL), version 2
9 #include <linux/seq_file.h>
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_fb_cma_helper.h>
13 #include <drm/drm_gem_cma_helper.h>
15 #include "sti_compositor.h"
16 #include "sti_cursor.h"
17 #include "sti_plane.h"
30 #define CUR_CTL_CLUT_UPDATE BIT(1)
32 #define STI_CURS_MIN_SIZE 1
33 #define STI_CURS_MAX_SIZE 128
36 * pixmap dma buffer stucture
38 * @paddr: physical address
40 * @base: virtual address
49 * STI Cursor structure
51 * @sti_plane: sti_plane structure
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)
61 struct sti_plane plane
;
67 dma_addr_t clut_paddr
;
68 struct dma_pixmap pixmap
;
71 static const uint32_t cursor_supported_formats
[] = {
75 #define to_sti_cursor(x) container_of(x, struct sti_cursor, plane)
77 #define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
78 readl(cursor->regs + reg))
80 static void cursor_dbg_vpo(struct seq_file
*s
, u32 val
)
82 seq_printf(s
, "\txdo:%4d\tydo:%4d", val
& 0x0FFF, (val
>> 16) & 0x0FFF);
85 static void cursor_dbg_size(struct seq_file
*s
, u32 val
)
87 seq_printf(s
, "\t%d x %d", val
& 0x07FF, (val
>> 16) & 0x07FF);
90 static void cursor_dbg_pml(struct seq_file
*s
,
91 struct sti_cursor
*cursor
, u32 val
)
93 if (cursor
->pixmap
.paddr
== val
)
94 seq_printf(s
, "\tVirt @: %p", cursor
->pixmap
.base
);
97 static void cursor_dbg_cml(struct seq_file
*s
,
98 struct sti_cursor
*cursor
, u32 val
)
100 if (cursor
->clut_paddr
== val
)
101 seq_printf(s
, "\tVirt @: %p", cursor
->clut
);
104 static int cursor_dbg_show(struct seq_file
*s
, void *data
)
106 struct drm_info_node
*node
= s
->private;
107 struct sti_cursor
*cursor
= (struct sti_cursor
*)node
->info_ent
->data
;
108 struct drm_device
*dev
= node
->minor
->dev
;
111 ret
= mutex_lock_interruptible(&dev
->struct_mutex
);
115 seq_printf(s
, "%s: (vaddr = 0x%p)",
116 sti_plane_to_str(&cursor
->plane
), cursor
->regs
);
120 cursor_dbg_vpo(s
, readl(cursor
->regs
+ CUR_VPO
));
122 cursor_dbg_pml(s
, cursor
, readl(cursor
->regs
+ CUR_PML
));
124 DBGFS_DUMP(CUR_SIZE
);
125 cursor_dbg_size(s
, readl(cursor
->regs
+ CUR_SIZE
));
127 cursor_dbg_cml(s
, cursor
, readl(cursor
->regs
+ CUR_CML
));
132 mutex_unlock(&dev
->struct_mutex
);
136 static struct drm_info_list cursor_debugfs_files
[] = {
137 { "cursor", cursor_dbg_show
, 0, NULL
},
140 static int cursor_debugfs_init(struct sti_cursor
*cursor
,
141 struct drm_minor
*minor
)
145 for (i
= 0; i
< ARRAY_SIZE(cursor_debugfs_files
); i
++)
146 cursor_debugfs_files
[i
].data
= cursor
;
148 return drm_debugfs_create_files(cursor_debugfs_files
,
149 ARRAY_SIZE(cursor_debugfs_files
),
150 minor
->debugfs_root
, minor
);
153 static void sti_cursor_argb8888_to_clut8(struct sti_cursor
*cursor
, u32
*src
)
155 u8
*dst
= cursor
->pixmap
.base
;
159 for (i
= 0; i
< cursor
->height
; i
++) {
160 for (j
= 0; j
< cursor
->width
; j
++) {
161 /* Pick the 2 higher bits of each component */
162 a
= (*src
>> 30) & 3;
163 r
= (*src
>> 22) & 3;
164 g
= (*src
>> 14) & 3;
166 *dst
= a
<< 6 | r
<< 4 | g
<< 2 | b
;
173 static void sti_cursor_init(struct sti_cursor
*cursor
)
175 unsigned short *base
= cursor
->clut
;
176 unsigned int a
, r
, g
, b
;
178 /* Assign CLUT values, ARGB444 format */
179 for (a
= 0; a
< 4; a
++)
180 for (r
= 0; r
< 4; r
++)
181 for (g
= 0; g
< 4; g
++)
182 for (b
= 0; b
< 4; b
++)
183 *base
++ = (a
* 5) << 12 |
189 static int sti_cursor_atomic_check(struct drm_plane
*drm_plane
,
190 struct drm_plane_state
*state
)
192 struct sti_plane
*plane
= to_sti_plane(drm_plane
);
193 struct sti_cursor
*cursor
= to_sti_cursor(plane
);
194 struct drm_crtc
*crtc
= state
->crtc
;
195 struct drm_framebuffer
*fb
= state
->fb
;
196 struct drm_crtc_state
*crtc_state
;
197 struct drm_display_mode
*mode
;
198 int dst_x
, dst_y
, dst_w
, dst_h
;
201 /* no need for further checks if the plane is being disabled */
205 crtc_state
= drm_atomic_get_crtc_state(state
->state
, crtc
);
206 mode
= &crtc_state
->mode
;
207 dst_x
= state
->crtc_x
;
208 dst_y
= state
->crtc_y
;
209 dst_w
= clamp_val(state
->crtc_w
, 0, mode
->crtc_hdisplay
- dst_x
);
210 dst_h
= clamp_val(state
->crtc_h
, 0, mode
->crtc_vdisplay
- dst_y
);
211 /* src_x are in 16.16 format */
212 src_w
= state
->src_w
>> 16;
213 src_h
= state
->src_h
>> 16;
215 if (src_w
< STI_CURS_MIN_SIZE
||
216 src_h
< STI_CURS_MIN_SIZE
||
217 src_w
> STI_CURS_MAX_SIZE
||
218 src_h
> STI_CURS_MAX_SIZE
) {
219 DRM_ERROR("Invalid cursor size (%dx%d)\n",
224 /* If the cursor size has changed, re-allocated the pixmap */
225 if (!cursor
->pixmap
.base
||
226 (cursor
->width
!= src_w
) ||
227 (cursor
->height
!= src_h
)) {
228 cursor
->width
= src_w
;
229 cursor
->height
= src_h
;
231 if (cursor
->pixmap
.base
)
232 dma_free_wc(cursor
->dev
, cursor
->pixmap
.size
,
233 cursor
->pixmap
.base
, cursor
->pixmap
.paddr
);
235 cursor
->pixmap
.size
= cursor
->width
* cursor
->height
;
237 cursor
->pixmap
.base
= dma_alloc_wc(cursor
->dev
,
239 &cursor
->pixmap
.paddr
,
240 GFP_KERNEL
| GFP_DMA
);
241 if (!cursor
->pixmap
.base
) {
242 DRM_ERROR("Failed to allocate memory for pixmap\n");
247 if (!drm_fb_cma_get_gem_obj(fb
, 0)) {
248 DRM_ERROR("Can't get CMA GEM object for fb\n");
252 DRM_DEBUG_KMS("CRTC:%d (%s) drm plane:%d (%s)\n",
253 crtc
->base
.id
, sti_mixer_to_str(to_sti_mixer(crtc
)),
254 drm_plane
->base
.id
, sti_plane_to_str(plane
));
255 DRM_DEBUG_KMS("(%dx%d)@(%d,%d)\n", dst_w
, dst_h
, dst_x
, dst_y
);
260 static void sti_cursor_atomic_update(struct drm_plane
*drm_plane
,
261 struct drm_plane_state
*oldstate
)
263 struct drm_plane_state
*state
= drm_plane
->state
;
264 struct sti_plane
*plane
= to_sti_plane(drm_plane
);
265 struct sti_cursor
*cursor
= to_sti_cursor(plane
);
266 struct drm_crtc
*crtc
= state
->crtc
;
267 struct drm_framebuffer
*fb
= state
->fb
;
268 struct drm_display_mode
*mode
;
270 struct drm_gem_cma_object
*cma_obj
;
278 dst_x
= state
->crtc_x
;
279 dst_y
= state
->crtc_y
;
281 cma_obj
= drm_fb_cma_get_gem_obj(fb
, 0);
283 /* Convert ARGB8888 to CLUT8 */
284 sti_cursor_argb8888_to_clut8(cursor
, (u32
*)cma_obj
->vaddr
);
286 /* AWS and AWE depend on the mode */
287 y
= sti_vtg_get_line_number(*mode
, 0);
288 x
= sti_vtg_get_pixel_number(*mode
, 0);
290 writel(val
, cursor
->regs
+ CUR_AWS
);
291 y
= sti_vtg_get_line_number(*mode
, mode
->vdisplay
- 1);
292 x
= sti_vtg_get_pixel_number(*mode
, mode
->hdisplay
- 1);
294 writel(val
, cursor
->regs
+ CUR_AWE
);
296 /* Set memory location, size, and position */
297 writel(cursor
->pixmap
.paddr
, cursor
->regs
+ CUR_PML
);
298 writel(cursor
->width
, cursor
->regs
+ CUR_PMP
);
299 writel(cursor
->height
<< 16 | cursor
->width
, cursor
->regs
+ CUR_SIZE
);
301 y
= sti_vtg_get_line_number(*mode
, dst_y
);
302 x
= sti_vtg_get_pixel_number(*mode
, dst_x
);
303 writel((y
<< 16) | x
, cursor
->regs
+ CUR_VPO
);
305 /* Set and fetch CLUT */
306 writel(cursor
->clut_paddr
, cursor
->regs
+ CUR_CML
);
307 writel(CUR_CTL_CLUT_UPDATE
, cursor
->regs
+ CUR_CTL
);
309 sti_plane_update_fps(plane
, true, false);
311 plane
->status
= STI_PLANE_UPDATED
;
314 static void sti_cursor_atomic_disable(struct drm_plane
*drm_plane
,
315 struct drm_plane_state
*oldstate
)
317 struct sti_plane
*plane
= to_sti_plane(drm_plane
);
319 if (!drm_plane
->crtc
) {
320 DRM_DEBUG_DRIVER("drm plane:%d not enabled\n",
325 DRM_DEBUG_DRIVER("CRTC:%d (%s) drm plane:%d (%s)\n",
326 drm_plane
->crtc
->base
.id
,
327 sti_mixer_to_str(to_sti_mixer(drm_plane
->crtc
)),
328 drm_plane
->base
.id
, sti_plane_to_str(plane
));
330 plane
->status
= STI_PLANE_DISABLING
;
333 static const struct drm_plane_helper_funcs sti_cursor_helpers_funcs
= {
334 .atomic_check
= sti_cursor_atomic_check
,
335 .atomic_update
= sti_cursor_atomic_update
,
336 .atomic_disable
= sti_cursor_atomic_disable
,
339 struct drm_plane
*sti_cursor_create(struct drm_device
*drm_dev
,
340 struct device
*dev
, int desc
,
341 void __iomem
*baseaddr
,
342 unsigned int possible_crtcs
)
344 struct sti_cursor
*cursor
;
348 cursor
= devm_kzalloc(dev
, sizeof(*cursor
), GFP_KERNEL
);
350 DRM_ERROR("Failed to allocate memory for cursor\n");
354 /* Allocate clut buffer */
355 size
= 0x100 * sizeof(unsigned short);
356 cursor
->clut
= dma_alloc_wc(dev
, size
, &cursor
->clut_paddr
,
357 GFP_KERNEL
| GFP_DMA
);
360 DRM_ERROR("Failed to allocate memory for cursor clut\n");
365 cursor
->regs
= baseaddr
;
366 cursor
->plane
.desc
= desc
;
367 cursor
->plane
.status
= STI_PLANE_DISABLED
;
369 sti_cursor_init(cursor
);
371 res
= drm_universal_plane_init(drm_dev
, &cursor
->plane
.drm_plane
,
373 &sti_plane_helpers_funcs
,
374 cursor_supported_formats
,
375 ARRAY_SIZE(cursor_supported_formats
),
376 DRM_PLANE_TYPE_CURSOR
, NULL
);
378 DRM_ERROR("Failed to initialize universal plane\n");
382 drm_plane_helper_add(&cursor
->plane
.drm_plane
,
383 &sti_cursor_helpers_funcs
);
385 sti_plane_init_property(&cursor
->plane
, DRM_PLANE_TYPE_CURSOR
);
387 if (cursor_debugfs_init(cursor
, drm_dev
->primary
))
388 DRM_ERROR("CURSOR debugfs setup failed\n");
390 return &cursor
->plane
.drm_plane
;
393 dma_free_wc(dev
, size
, cursor
->clut
, cursor
->clut_paddr
);
395 devm_kfree(dev
, cursor
);