1 /* 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 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 stucture 37 * 38 * @paddr: physical address 39 * @size: buffer size 40 * @base: virtual address 41 */ 42 struct 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 */ 60 struct 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 71 static 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 80 static 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 85 static 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 90 static 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 97 static 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 104 static 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_puts(s, "\n"); 125 126 return 0; 127 } 128 129 static struct drm_info_list cursor_debugfs_files[] = { 130 { "cursor", cursor_dbg_show, 0, NULL }, 131 }; 132 133 static int cursor_debugfs_init(struct sti_cursor *cursor, 134 struct drm_minor *minor) 135 { 136 unsigned int i; 137 138 for (i = 0; i < ARRAY_SIZE(cursor_debugfs_files); i++) 139 cursor_debugfs_files[i].data = cursor; 140 141 return drm_debugfs_create_files(cursor_debugfs_files, 142 ARRAY_SIZE(cursor_debugfs_files), 143 minor->debugfs_root, minor); 144 } 145 146 static void sti_cursor_argb8888_to_clut8(struct sti_cursor *cursor, u32 *src) 147 { 148 u8 *dst = cursor->pixmap.base; 149 unsigned int i, j; 150 u32 a, r, g, b; 151 152 for (i = 0; i < cursor->height; i++) { 153 for (j = 0; j < cursor->width; j++) { 154 /* Pick the 2 higher bits of each component */ 155 a = (*src >> 30) & 3; 156 r = (*src >> 22) & 3; 157 g = (*src >> 14) & 3; 158 b = (*src >> 6) & 3; 159 *dst = a << 6 | r << 4 | g << 2 | b; 160 src++; 161 dst++; 162 } 163 } 164 } 165 166 static void sti_cursor_init(struct sti_cursor *cursor) 167 { 168 unsigned short *base = cursor->clut; 169 unsigned int a, r, g, b; 170 171 /* Assign CLUT values, ARGB444 format */ 172 for (a = 0; a < 4; a++) 173 for (r = 0; r < 4; r++) 174 for (g = 0; g < 4; g++) 175 for (b = 0; b < 4; b++) 176 *base++ = (a * 5) << 12 | 177 (r * 5) << 8 | 178 (g * 5) << 4 | 179 (b * 5); 180 } 181 182 static int sti_cursor_atomic_check(struct drm_plane *drm_plane, 183 struct drm_plane_state *state) 184 { 185 struct sti_plane *plane = to_sti_plane(drm_plane); 186 struct sti_cursor *cursor = to_sti_cursor(plane); 187 struct drm_crtc *crtc = state->crtc; 188 struct drm_framebuffer *fb = state->fb; 189 struct drm_crtc_state *crtc_state; 190 struct drm_display_mode *mode; 191 int dst_x, dst_y, dst_w, dst_h; 192 int src_w, src_h; 193 194 /* no need for further checks if the plane is being disabled */ 195 if (!crtc || !fb) 196 return 0; 197 198 crtc_state = drm_atomic_get_crtc_state(state->state, crtc); 199 mode = &crtc_state->mode; 200 dst_x = state->crtc_x; 201 dst_y = state->crtc_y; 202 dst_w = clamp_val(state->crtc_w, 0, mode->crtc_hdisplay - dst_x); 203 dst_h = clamp_val(state->crtc_h, 0, mode->crtc_vdisplay - dst_y); 204 /* src_x are in 16.16 format */ 205 src_w = state->src_w >> 16; 206 src_h = state->src_h >> 16; 207 208 if (src_w < STI_CURS_MIN_SIZE || 209 src_h < STI_CURS_MIN_SIZE || 210 src_w > STI_CURS_MAX_SIZE || 211 src_h > STI_CURS_MAX_SIZE) { 212 DRM_ERROR("Invalid cursor size (%dx%d)\n", 213 src_w, src_h); 214 return -EINVAL; 215 } 216 217 /* If the cursor size has changed, re-allocated the pixmap */ 218 if (!cursor->pixmap.base || 219 (cursor->width != src_w) || 220 (cursor->height != src_h)) { 221 cursor->width = src_w; 222 cursor->height = src_h; 223 224 if (cursor->pixmap.base) 225 dma_free_wc(cursor->dev, cursor->pixmap.size, 226 cursor->pixmap.base, cursor->pixmap.paddr); 227 228 cursor->pixmap.size = cursor->width * cursor->height; 229 230 cursor->pixmap.base = dma_alloc_wc(cursor->dev, 231 cursor->pixmap.size, 232 &cursor->pixmap.paddr, 233 GFP_KERNEL | GFP_DMA); 234 if (!cursor->pixmap.base) { 235 DRM_ERROR("Failed to allocate memory for pixmap\n"); 236 return -EINVAL; 237 } 238 } 239 240 if (!drm_fb_cma_get_gem_obj(fb, 0)) { 241 DRM_ERROR("Can't get CMA GEM object for fb\n"); 242 return -EINVAL; 243 } 244 245 DRM_DEBUG_KMS("CRTC:%d (%s) drm plane:%d (%s)\n", 246 crtc->base.id, sti_mixer_to_str(to_sti_mixer(crtc)), 247 drm_plane->base.id, sti_plane_to_str(plane)); 248 DRM_DEBUG_KMS("(%dx%d)@(%d,%d)\n", dst_w, dst_h, dst_x, dst_y); 249 250 return 0; 251 } 252 253 static void sti_cursor_atomic_update(struct drm_plane *drm_plane, 254 struct drm_plane_state *oldstate) 255 { 256 struct drm_plane_state *state = drm_plane->state; 257 struct sti_plane *plane = to_sti_plane(drm_plane); 258 struct sti_cursor *cursor = to_sti_cursor(plane); 259 struct drm_crtc *crtc = state->crtc; 260 struct drm_framebuffer *fb = state->fb; 261 struct drm_display_mode *mode; 262 int dst_x, dst_y; 263 struct drm_gem_cma_object *cma_obj; 264 u32 y, x; 265 u32 val; 266 267 if (!crtc || !fb) 268 return; 269 270 mode = &crtc->mode; 271 dst_x = state->crtc_x; 272 dst_y = state->crtc_y; 273 274 cma_obj = drm_fb_cma_get_gem_obj(fb, 0); 275 276 /* Convert ARGB8888 to CLUT8 */ 277 sti_cursor_argb8888_to_clut8(cursor, (u32 *)cma_obj->vaddr); 278 279 /* AWS and AWE depend on the mode */ 280 y = sti_vtg_get_line_number(*mode, 0); 281 x = sti_vtg_get_pixel_number(*mode, 0); 282 val = y << 16 | x; 283 writel(val, cursor->regs + CUR_AWS); 284 y = sti_vtg_get_line_number(*mode, mode->vdisplay - 1); 285 x = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1); 286 val = y << 16 | x; 287 writel(val, cursor->regs + CUR_AWE); 288 289 /* Set memory location, size, and position */ 290 writel(cursor->pixmap.paddr, cursor->regs + CUR_PML); 291 writel(cursor->width, cursor->regs + CUR_PMP); 292 writel(cursor->height << 16 | cursor->width, cursor->regs + CUR_SIZE); 293 294 y = sti_vtg_get_line_number(*mode, dst_y); 295 x = sti_vtg_get_pixel_number(*mode, dst_x); 296 writel((y << 16) | x, cursor->regs + CUR_VPO); 297 298 /* Set and fetch CLUT */ 299 writel(cursor->clut_paddr, cursor->regs + CUR_CML); 300 writel(CUR_CTL_CLUT_UPDATE, cursor->regs + CUR_CTL); 301 302 sti_plane_update_fps(plane, true, false); 303 304 plane->status = STI_PLANE_UPDATED; 305 } 306 307 static void sti_cursor_atomic_disable(struct drm_plane *drm_plane, 308 struct drm_plane_state *oldstate) 309 { 310 struct sti_plane *plane = to_sti_plane(drm_plane); 311 312 if (!drm_plane->crtc) { 313 DRM_DEBUG_DRIVER("drm plane:%d not enabled\n", 314 drm_plane->base.id); 315 return; 316 } 317 318 DRM_DEBUG_DRIVER("CRTC:%d (%s) drm plane:%d (%s)\n", 319 drm_plane->crtc->base.id, 320 sti_mixer_to_str(to_sti_mixer(drm_plane->crtc)), 321 drm_plane->base.id, sti_plane_to_str(plane)); 322 323 plane->status = STI_PLANE_DISABLING; 324 } 325 326 static const struct drm_plane_helper_funcs sti_cursor_helpers_funcs = { 327 .atomic_check = sti_cursor_atomic_check, 328 .atomic_update = sti_cursor_atomic_update, 329 .atomic_disable = sti_cursor_atomic_disable, 330 }; 331 332 static void sti_cursor_destroy(struct drm_plane *drm_plane) 333 { 334 DRM_DEBUG_DRIVER("\n"); 335 336 drm_plane_helper_disable(drm_plane); 337 drm_plane_cleanup(drm_plane); 338 } 339 340 static 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 return cursor_debugfs_init(cursor, drm_plane->dev->primary); 346 } 347 348 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 .set_property = drm_atomic_helper_plane_set_property, 353 .reset = sti_plane_reset, 354 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 355 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 356 .late_register = sti_cursor_late_register, 357 }; 358 359 struct drm_plane *sti_cursor_create(struct drm_device *drm_dev, 360 struct device *dev, int desc, 361 void __iomem *baseaddr, 362 unsigned int possible_crtcs) 363 { 364 struct sti_cursor *cursor; 365 size_t size; 366 int res; 367 368 cursor = devm_kzalloc(dev, sizeof(*cursor), GFP_KERNEL); 369 if (!cursor) { 370 DRM_ERROR("Failed to allocate memory for cursor\n"); 371 return NULL; 372 } 373 374 /* Allocate clut buffer */ 375 size = 0x100 * sizeof(unsigned short); 376 cursor->clut = dma_alloc_wc(dev, size, &cursor->clut_paddr, 377 GFP_KERNEL | GFP_DMA); 378 379 if (!cursor->clut) { 380 DRM_ERROR("Failed to allocate memory for cursor clut\n"); 381 goto err_clut; 382 } 383 384 cursor->dev = dev; 385 cursor->regs = baseaddr; 386 cursor->plane.desc = desc; 387 cursor->plane.status = STI_PLANE_DISABLED; 388 389 sti_cursor_init(cursor); 390 391 res = drm_universal_plane_init(drm_dev, &cursor->plane.drm_plane, 392 possible_crtcs, 393 &sti_cursor_plane_helpers_funcs, 394 cursor_supported_formats, 395 ARRAY_SIZE(cursor_supported_formats), 396 DRM_PLANE_TYPE_CURSOR, NULL); 397 if (res) { 398 DRM_ERROR("Failed to initialize universal plane\n"); 399 goto err_plane; 400 } 401 402 drm_plane_helper_add(&cursor->plane.drm_plane, 403 &sti_cursor_helpers_funcs); 404 405 sti_plane_init_property(&cursor->plane, DRM_PLANE_TYPE_CURSOR); 406 407 return &cursor->plane.drm_plane; 408 409 err_plane: 410 dma_free_wc(dev, size, cursor->clut, cursor->clut_paddr); 411 err_clut: 412 devm_kfree(dev, cursor); 413 return NULL; 414 } 415