2 * Copyright (C) 2013 Samsung Electronics Co.Ltd
4 * Inki Dae <inki.dae@samsung.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
23 #include <linux/stddef.h>
28 #include "exynos_drm.h"
29 #include "fimg2d_reg.h"
32 #define SET_BF(val, sc, si, scsa, scda, dc, di, dcsa, dcda) \
33 val.data.src_coeff = sc; \
34 val.data.inv_src_color_coeff = si; \
35 val.data.src_coeff_src_a = scsa; \
36 val.data.src_coeff_dst_a = scda; \
37 val.data.dst_coeff = dc; \
38 val.data.inv_dst_color_coeff = di; \
39 val.data.dst_coeff_src_a = dcsa; \
40 val.data.dst_coeff_dst_a = dcda;
42 #define MIN(a, b) ((a) < (b) ? (a) : (b))
44 static unsigned int g2d_get_blend_op(enum e_g2d_op op)
46 union g2d_blend_func_val val;
52 case G2D_OP_DISJOINT_CLEAR:
53 case G2D_OP_CONJOINT_CLEAR:
54 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ZERO,
58 case G2D_OP_DISJOINT_SRC:
59 case G2D_OP_CONJOINT_SRC:
60 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
64 case G2D_OP_DISJOINT_DST:
65 case G2D_OP_CONJOINT_DST:
66 SET_BF(val, G2D_COEFF_MODE_ZERO, 0, 0, 0, G2D_COEFF_MODE_ONE,
70 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0,
71 G2D_COEFF_MODE_SRC_ALPHA, 1, 0, 0);
74 fprintf(stderr, "Not support operation(%d).\n", op);
75 SET_BF(val, G2D_COEFF_MODE_ONE, 0, 0, 0, G2D_COEFF_MODE_ZERO,
84 * g2d_add_cmd - set given command and value to user side command buffer.
86 * @ctx: a pointer to g2d_context structure.
90 static int g2d_add_cmd(struct g2d_context *ctx, unsigned long cmd,
93 switch (cmd & ~(G2D_BUF_USERPTR)) {
94 case SRC_BASE_ADDR_REG:
95 case SRC_PLANE2_BASE_ADDR_REG:
96 case DST_BASE_ADDR_REG:
97 case DST_PLANE2_BASE_ADDR_REG:
98 case PAT_BASE_ADDR_REG:
99 case MASK_BASE_ADDR_REG:
100 if (ctx->cmd_buf_nr >= G2D_MAX_GEM_CMD_NR) {
101 fprintf(stderr, "Overflow cmd_gem size.\n");
105 ctx->cmd_buf[ctx->cmd_buf_nr].offset = cmd;
106 ctx->cmd_buf[ctx->cmd_buf_nr].data = value;
110 if (ctx->cmd_nr >= G2D_MAX_CMD_NR) {
111 fprintf(stderr, "Overflow cmd size.\n");
115 ctx->cmd[ctx->cmd_nr].offset = cmd;
116 ctx->cmd[ctx->cmd_nr].data = value;
125 * g2d_reset - reset fimg2d hardware.
127 * @ctx: a pointer to g2d_context structure.
130 static void g2d_reset(struct g2d_context *ctx)
135 g2d_add_cmd(ctx, SOFT_RESET_REG, 0x01);
139 * g2d_flush - summit all commands and values in user side command buffer
140 * to command queue aware of fimg2d dma.
142 * @ctx: a pointer to g2d_context structure.
144 * This function should be called after all commands and values to user
145 * side command buffer is set to summit that buffer to kernel side driver.
147 static int g2d_flush(struct g2d_context *ctx)
150 struct drm_exynos_g2d_set_cmdlist cmdlist;
152 if (ctx->cmd_nr == 0 && ctx->cmd_buf_nr == 0)
155 if (ctx->cmdlist_nr >= G2D_MAX_CMD_LIST_NR) {
156 fprintf(stderr, "Overflow cmdlist.\n");
160 memset(&cmdlist, 0, sizeof(struct drm_exynos_g2d_set_cmdlist));
162 cmdlist.cmd = (uint64_t)(uintptr_t)&ctx->cmd[0];
163 cmdlist.cmd_buf = (uint64_t)(uintptr_t)&ctx->cmd_buf[0];
164 cmdlist.cmd_nr = ctx->cmd_nr;
165 cmdlist.cmd_buf_nr = ctx->cmd_buf_nr;
166 cmdlist.event_type = G2D_EVENT_NOT;
167 cmdlist.user_data = 0;
172 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_SET_CMDLIST, &cmdlist);
174 fprintf(stderr, "failed to set cmdlist.\n");
184 * g2d_init - create a new g2d context and get hardware version.
186 * fd: a file descriptor to drm device driver opened.
188 drm_public struct g2d_context *g2d_init(int fd)
190 struct drm_exynos_g2d_get_ver ver;
191 struct g2d_context *ctx;
194 ctx = calloc(1, sizeof(*ctx));
196 fprintf(stderr, "failed to allocate context.\n");
202 ret = drmIoctl(fd, DRM_IOCTL_EXYNOS_G2D_GET_VER, &ver);
204 fprintf(stderr, "failed to get version.\n");
209 ctx->major = ver.major;
210 ctx->minor = ver.minor;
212 printf("g2d version(%d.%d).\n", ctx->major, ctx->minor);
216 drm_public void g2d_fini(struct g2d_context *ctx)
223 * g2d_exec - start the dma to process all commands summited by g2d_flush().
225 * @ctx: a pointer to g2d_context structure.
227 drm_public int g2d_exec(struct g2d_context *ctx)
229 struct drm_exynos_g2d_exec exec;
232 if (ctx->cmdlist_nr == 0)
237 ret = drmIoctl(ctx->fd, DRM_IOCTL_EXYNOS_G2D_EXEC, &exec);
239 fprintf(stderr, "failed to execute.\n");
249 * g2d_solid_fill - fill given buffer with given color data.
251 * @ctx: a pointer to g2d_context structure.
252 * @img: a pointer to g2d_image structure including image and buffer
254 * @x: x start position to buffer filled with given color data.
255 * @y: y start position to buffer filled with given color data.
256 * @w: width value to buffer filled with given color data.
257 * @h: height value to buffer filled with given color data.
260 g2d_solid_fill(struct g2d_context *ctx, struct g2d_image *img,
261 unsigned int x, unsigned int y, unsigned int w,
264 union g2d_bitblt_cmd_val bitblt;
265 union g2d_point_val pt;
267 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
268 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, img->color_mode);
270 if (img->buf_type == G2D_IMGBUF_USERPTR)
271 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
272 (unsigned long)&img->user_ptr[0]);
274 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, img->bo[0]);
276 g2d_add_cmd(ctx, DST_STRIDE_REG, img->stride);
278 if (x + w > img->width)
280 if (y + h > img->height)
286 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
292 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
294 g2d_add_cmd(ctx, SF_COLOR_REG, img->color);
297 bitblt.data.fast_solid_color_fill_en = 1;
298 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
306 * g2d_copy - copy contents in source buffer to destination buffer.
308 * @ctx: a pointer to g2d_context structure.
309 * @src: a pointer to g2d_image structure including image and buffer
310 * information to source.
311 * @dst: a pointer to g2d_image structure including image and buffer
312 * information to destination.
313 * @src_x: x start position to source buffer.
314 * @src_y: y start position to source buffer.
315 * @dst_x: x start position to destination buffer.
316 * @dst_y: y start position to destination buffer.
317 * @w: width value to source and destination buffers.
318 * @h: height value to source and destination buffers.
321 g2d_copy(struct g2d_context *ctx, struct g2d_image *src,
322 struct g2d_image *dst, unsigned int src_x, unsigned int src_y,
323 unsigned int dst_x, unsigned dst_y, unsigned int w,
326 union g2d_rop4_val rop4;
327 union g2d_point_val pt;
328 unsigned int src_w = 0, src_h = 0, dst_w = 0, dst_h = 0;
330 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
331 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
332 if (dst->buf_type == G2D_IMGBUF_USERPTR)
333 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
334 (unsigned long)&dst->user_ptr[0]);
336 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
338 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
340 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
341 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
342 if (src->buf_type == G2D_IMGBUF_USERPTR)
343 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
344 (unsigned long)&src->user_ptr[0]);
346 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
348 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
352 if (src_x + src->width > w)
353 src_w = src->width - src_x;
354 if (src_y + src->height > h)
355 src_h = src->height - src_y;
359 if (dst_x + dst->width > w)
360 dst_w = dst->width - dst_x;
361 if (dst_y + dst->height > h)
362 dst_h = dst->height - dst_y;
364 w = MIN(src_w, dst_w);
365 h = MIN(src_h, dst_h);
367 if (w <= 0 || h <= 0) {
368 fprintf(stderr, "invalid width or height.\n");
376 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
378 pt.data.x = src_x + w;
379 pt.data.y = src_y + h;
380 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
385 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
387 pt.data.x = dst_x + w;
388 pt.data.y = dst_y + h;
389 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
392 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
393 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
401 * g2d_copy_with_scale - copy contents in source buffer to destination buffer
402 * scaling up or down properly.
404 * @ctx: a pointer to g2d_context structure.
405 * @src: a pointer to g2d_image structure including image and buffer
406 * information to source.
407 * @dst: a pointer to g2d_image structure including image and buffer
408 * information to destination.
409 * @src_x: x start position to source buffer.
410 * @src_y: y start position to source buffer.
411 * @src_w: width value to source buffer.
412 * @src_h: height value to source buffer.
413 * @dst_x: x start position to destination buffer.
414 * @dst_y: y start position to destination buffer.
415 * @dst_w: width value to destination buffer.
416 * @dst_h: height value to destination buffer.
417 * @negative: indicate that it uses color negative to source and
418 * destination buffers.
421 g2d_copy_with_scale(struct g2d_context *ctx, struct g2d_image *src,
422 struct g2d_image *dst, unsigned int src_x,
423 unsigned int src_y, unsigned int src_w,
424 unsigned int src_h, unsigned int dst_x,
425 unsigned int dst_y, unsigned int dst_w,
426 unsigned int dst_h, unsigned int negative)
428 union g2d_rop4_val rop4;
429 union g2d_point_val pt;
431 double scale_x = 0.0f, scale_y = 0.0f;
433 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
434 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
435 if (dst->buf_type == G2D_IMGBUF_USERPTR)
436 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
437 (unsigned long)&dst->user_ptr[0]);
439 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
441 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
443 g2d_add_cmd(ctx, SRC_SELECT_REG, G2D_SELECT_MODE_NORMAL);
444 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
445 if (src->buf_type == G2D_IMGBUF_USERPTR)
446 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
447 (unsigned long)&src->user_ptr[0]);
449 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
451 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
453 if (src_w == dst_w && src_h == dst_h)
457 scale_x = (double)src_w / (double)dst_w;
458 scale_y = (double)src_h / (double)dst_h;
461 if (src_x + src_w > src->width)
462 src_w = src->width - src_x;
463 if (src_y + src_h > src->height)
464 src_h = src->height - src_y;
466 if (dst_x + dst_w > dst->width)
467 dst_w = dst->width - dst_x;
468 if (dst_y + dst_h > dst->height)
469 dst_h = dst->height - dst_y;
471 if (src_w <= 0 || src_h <= 0 || dst_w <= 0 || dst_h <= 0) {
472 fprintf(stderr, "invalid width or height.\n");
478 g2d_add_cmd(ctx, BG_COLOR_REG, 0x00FFFFFF);
480 rop4.data.unmasked_rop3 = G2D_ROP3_SRC^G2D_ROP3_DST;
481 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
484 rop4.data.unmasked_rop3 = G2D_ROP3_SRC;
485 g2d_add_cmd(ctx, ROP4_REG, rop4.val);
489 g2d_add_cmd(ctx, SRC_SCALE_CTRL_REG, G2D_SCALE_MODE_BILINEAR);
490 g2d_add_cmd(ctx, SRC_XSCALE_REG, G2D_DOUBLE_TO_FIXED(scale_x));
491 g2d_add_cmd(ctx, SRC_YSCALE_REG, G2D_DOUBLE_TO_FIXED(scale_y));
497 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
499 pt.data.x = src_x + src_w;
500 pt.data.y = src_y + src_h;
501 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
506 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
508 pt.data.x = dst_x + dst_w;
509 pt.data.y = dst_y + dst_h;
510 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);
518 * g2d_blend - blend image data in source and destion buffers
520 * @ctx: a pointer to g2d_context structure.
521 * @src: a pointer to g2d_image structure including image and buffer
522 * information to source.
523 * @dst: a pointer to g2d_image structure including image and buffer
524 * information to destination.
525 * @src_x: x start position to source buffer.
526 * @src_y: y start position to source buffer.
527 * @dst_x: x start position to destination buffer.
528 * @dst_y: y start position to destination buffer.
529 * @w: width value to source and destination buffer.
530 * @h: height value to source and destination buffer.
531 * @op: blend operation type.
534 g2d_blend(struct g2d_context *ctx, struct g2d_image *src,
535 struct g2d_image *dst, unsigned int src_x,
536 unsigned int src_y, unsigned int dst_x, unsigned int dst_y,
537 unsigned int w, unsigned int h, enum e_g2d_op op)
539 union g2d_point_val pt;
540 union g2d_bitblt_cmd_val bitblt;
541 union g2d_blend_func_val blend;
542 unsigned int src_w = 0, src_h = 0, dst_w = 0, dst_h = 0;
547 if (op == G2D_OP_SRC || op == G2D_OP_CLEAR)
548 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_BGCOLOR);
550 g2d_add_cmd(ctx, DST_SELECT_REG, G2D_SELECT_MODE_NORMAL);
552 g2d_add_cmd(ctx, DST_COLOR_MODE_REG, dst->color_mode);
553 if (dst->buf_type == G2D_IMGBUF_USERPTR)
554 g2d_add_cmd(ctx, DST_BASE_ADDR_REG | G2D_BUF_USERPTR,
555 (unsigned long)&dst->user_ptr[0]);
557 g2d_add_cmd(ctx, DST_BASE_ADDR_REG, dst->bo[0]);
559 g2d_add_cmd(ctx, DST_STRIDE_REG, dst->stride);
561 g2d_add_cmd(ctx, SRC_SELECT_REG, src->select_mode);
562 g2d_add_cmd(ctx, SRC_COLOR_MODE_REG, src->color_mode);
564 switch (src->select_mode) {
565 case G2D_SELECT_MODE_NORMAL:
566 if (src->buf_type == G2D_IMGBUF_USERPTR)
567 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG | G2D_BUF_USERPTR,
568 (unsigned long)&src->user_ptr[0]);
570 g2d_add_cmd(ctx, SRC_BASE_ADDR_REG, src->bo[0]);
572 g2d_add_cmd(ctx, SRC_STRIDE_REG, src->stride);
574 case G2D_SELECT_MODE_FGCOLOR:
575 g2d_add_cmd(ctx, FG_COLOR_REG, src->color);
577 case G2D_SELECT_MODE_BGCOLOR:
578 g2d_add_cmd(ctx, BG_COLOR_REG, src->color);
581 fprintf(stderr , "failed to set src.\n");
587 if (src_x + w > src->width)
588 src_w = src->width - src_x;
589 if (src_y + h > src->height)
590 src_h = src->height - src_y;
594 if (dst_x + w > dst->width)
595 dst_w = dst->width - dst_x;
596 if (dst_y + h > dst->height)
597 dst_h = dst->height - dst_y;
599 w = MIN(src_w, dst_w);
600 h = MIN(src_h, dst_h);
602 if (w <= 0 || h <= 0) {
603 fprintf(stderr, "invalid width or height.\n");
608 bitblt.data.alpha_blend_mode = G2D_ALPHA_BLEND_MODE_ENABLE;
609 blend.val = g2d_get_blend_op(op);
610 g2d_add_cmd(ctx, BITBLT_COMMAND_REG, bitblt.val);
611 g2d_add_cmd(ctx, BLEND_FUNCTION_REG, blend.val);
616 g2d_add_cmd(ctx, SRC_LEFT_TOP_REG, pt.val);
618 pt.data.x = src_x + w;
619 pt.data.y = src_y + h;
620 g2d_add_cmd(ctx, SRC_RIGHT_BOTTOM_REG, pt.val);
625 g2d_add_cmd(ctx, DST_LEFT_TOP_REG, pt.val);
627 pt.data.x = dst_x + w;
628 pt.data.y = dst_y + h;
629 g2d_add_cmd(ctx, DST_RIGHT_BOTTOM_REG, pt.val);