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