rga-hw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  3. * Author: Jacob Chen <jacob-chen@iotwrt.com>
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/pm_runtime.h>
  15. #include "rga-hw.h"
  16. #include "rga.h"
  17. enum e_rga_start_pos {
  18. LT = 0,
  19. LB = 1,
  20. RT = 2,
  21. RB = 3,
  22. };
  23. struct rga_addr_offset {
  24. unsigned int y_off;
  25. unsigned int u_off;
  26. unsigned int v_off;
  27. };
  28. struct rga_corners_addr_offset {
  29. struct rga_addr_offset left_top;
  30. struct rga_addr_offset right_top;
  31. struct rga_addr_offset left_bottom;
  32. struct rga_addr_offset right_bottom;
  33. };
  34. static unsigned int rga_get_scaling(unsigned int src, unsigned int dst)
  35. {
  36. /*
  37. * The rga hw scaling factor is a normalized inverse of the
  38. * scaling factor.
  39. * For example: When source width is 100 and destination width is 200
  40. * (scaling of 2x), then the hw factor is NC * 100 / 200.
  41. * The normalization factor (NC) is 2^16 = 0x10000.
  42. */
  43. return (src > dst) ? ((dst << 16) / src) : ((src << 16) / dst);
  44. }
  45. static struct rga_corners_addr_offset
  46. rga_get_addr_offset(struct rga_frame *frm, unsigned int x, unsigned int y,
  47. unsigned int w, unsigned int h)
  48. {
  49. struct rga_corners_addr_offset offsets;
  50. struct rga_addr_offset *lt, *lb, *rt, *rb;
  51. unsigned int x_div = 0,
  52. y_div = 0, uv_stride = 0, pixel_width = 0, uv_factor = 0;
  53. lt = &offsets.left_top;
  54. lb = &offsets.left_bottom;
  55. rt = &offsets.right_top;
  56. rb = &offsets.right_bottom;
  57. x_div = frm->fmt->x_div;
  58. y_div = frm->fmt->y_div;
  59. uv_factor = frm->fmt->uv_factor;
  60. uv_stride = frm->stride / x_div;
  61. pixel_width = frm->stride / frm->width;
  62. lt->y_off = y * frm->stride + x * pixel_width;
  63. lt->u_off =
  64. frm->width * frm->height + (y / y_div) * uv_stride + x / x_div;
  65. lt->v_off = lt->u_off + frm->width * frm->height / uv_factor;
  66. lb->y_off = lt->y_off + (h - 1) * frm->stride;
  67. lb->u_off = lt->u_off + (h / y_div - 1) * uv_stride;
  68. lb->v_off = lt->v_off + (h / y_div - 1) * uv_stride;
  69. rt->y_off = lt->y_off + (w - 1) * pixel_width;
  70. rt->u_off = lt->u_off + w / x_div - 1;
  71. rt->v_off = lt->v_off + w / x_div - 1;
  72. rb->y_off = lb->y_off + (w - 1) * pixel_width;
  73. rb->u_off = lb->u_off + w / x_div - 1;
  74. rb->v_off = lb->v_off + w / x_div - 1;
  75. return offsets;
  76. }
  77. static struct rga_addr_offset *rga_lookup_draw_pos(struct
  78. rga_corners_addr_offset
  79. * offsets, u32 rotate_mode,
  80. u32 mirr_mode)
  81. {
  82. static enum e_rga_start_pos rot_mir_point_matrix[4][4] = {
  83. {
  84. LT, RT, LB, RB,
  85. },
  86. {
  87. RT, LT, RB, LB,
  88. },
  89. {
  90. RB, LB, RT, LT,
  91. },
  92. {
  93. LB, RB, LT, RT,
  94. },
  95. };
  96. if (!offsets)
  97. return NULL;
  98. switch (rot_mir_point_matrix[rotate_mode][mirr_mode]) {
  99. case LT:
  100. return &offsets->left_top;
  101. case LB:
  102. return &offsets->left_bottom;
  103. case RT:
  104. return &offsets->right_top;
  105. case RB:
  106. return &offsets->right_bottom;
  107. }
  108. return NULL;
  109. }
  110. static void rga_cmd_set_src_addr(struct rga_ctx *ctx, void *mmu_pages)
  111. {
  112. struct rockchip_rga *rga = ctx->rga;
  113. u32 *dest = rga->cmdbuf_virt;
  114. unsigned int reg;
  115. reg = RGA_MMU_SRC_BASE - RGA_MODE_BASE_REG;
  116. dest[reg >> 2] = virt_to_phys(mmu_pages) >> 4;
  117. reg = RGA_MMU_CTRL1 - RGA_MODE_BASE_REG;
  118. dest[reg >> 2] |= 0x7;
  119. }
  120. static void rga_cmd_set_src1_addr(struct rga_ctx *ctx, void *mmu_pages)
  121. {
  122. struct rockchip_rga *rga = ctx->rga;
  123. u32 *dest = rga->cmdbuf_virt;
  124. unsigned int reg;
  125. reg = RGA_MMU_SRC1_BASE - RGA_MODE_BASE_REG;
  126. dest[reg >> 2] = virt_to_phys(mmu_pages) >> 4;
  127. reg = RGA_MMU_CTRL1 - RGA_MODE_BASE_REG;
  128. dest[reg >> 2] |= 0x7 << 4;
  129. }
  130. static void rga_cmd_set_dst_addr(struct rga_ctx *ctx, void *mmu_pages)
  131. {
  132. struct rockchip_rga *rga = ctx->rga;
  133. u32 *dest = rga->cmdbuf_virt;
  134. unsigned int reg;
  135. reg = RGA_MMU_DST_BASE - RGA_MODE_BASE_REG;
  136. dest[reg >> 2] = virt_to_phys(mmu_pages) >> 4;
  137. reg = RGA_MMU_CTRL1 - RGA_MODE_BASE_REG;
  138. dest[reg >> 2] |= 0x7 << 8;
  139. }
  140. static void rga_cmd_set_trans_info(struct rga_ctx *ctx)
  141. {
  142. struct rockchip_rga *rga = ctx->rga;
  143. u32 *dest = rga->cmdbuf_virt;
  144. unsigned int scale_dst_w, scale_dst_h;
  145. unsigned int src_h, src_w, src_x, src_y, dst_h, dst_w, dst_x, dst_y;
  146. union rga_src_info src_info;
  147. union rga_dst_info dst_info;
  148. union rga_src_x_factor x_factor;
  149. union rga_src_y_factor y_factor;
  150. union rga_src_vir_info src_vir_info;
  151. union rga_src_act_info src_act_info;
  152. union rga_dst_vir_info dst_vir_info;
  153. union rga_dst_act_info dst_act_info;
  154. struct rga_addr_offset *dst_offset;
  155. struct rga_corners_addr_offset offsets;
  156. struct rga_corners_addr_offset src_offsets;
  157. src_h = ctx->in.crop.height;
  158. src_w = ctx->in.crop.width;
  159. src_x = ctx->in.crop.left;
  160. src_y = ctx->in.crop.top;
  161. dst_h = ctx->out.crop.height;
  162. dst_w = ctx->out.crop.width;
  163. dst_x = ctx->out.crop.left;
  164. dst_y = ctx->out.crop.top;
  165. src_info.val = dest[(RGA_SRC_INFO - RGA_MODE_BASE_REG) >> 2];
  166. dst_info.val = dest[(RGA_DST_INFO - RGA_MODE_BASE_REG) >> 2];
  167. x_factor.val = dest[(RGA_SRC_X_FACTOR - RGA_MODE_BASE_REG) >> 2];
  168. y_factor.val = dest[(RGA_SRC_Y_FACTOR - RGA_MODE_BASE_REG) >> 2];
  169. src_vir_info.val = dest[(RGA_SRC_VIR_INFO - RGA_MODE_BASE_REG) >> 2];
  170. src_act_info.val = dest[(RGA_SRC_ACT_INFO - RGA_MODE_BASE_REG) >> 2];
  171. dst_vir_info.val = dest[(RGA_DST_VIR_INFO - RGA_MODE_BASE_REG) >> 2];
  172. dst_act_info.val = dest[(RGA_DST_ACT_INFO - RGA_MODE_BASE_REG) >> 2];
  173. src_info.data.format = ctx->in.fmt->hw_format;
  174. src_info.data.swap = ctx->in.fmt->color_swap;
  175. dst_info.data.format = ctx->out.fmt->hw_format;
  176. dst_info.data.swap = ctx->out.fmt->color_swap;
  177. if (ctx->in.fmt->hw_format >= RGA_COLOR_FMT_YUV422SP) {
  178. if (ctx->out.fmt->hw_format < RGA_COLOR_FMT_YUV422SP) {
  179. switch (ctx->in.colorspace) {
  180. case V4L2_COLORSPACE_REC709:
  181. src_info.data.csc_mode =
  182. RGA_SRC_CSC_MODE_BT709_R0;
  183. break;
  184. default:
  185. src_info.data.csc_mode =
  186. RGA_SRC_CSC_MODE_BT601_R0;
  187. break;
  188. }
  189. }
  190. }
  191. if (ctx->out.fmt->hw_format >= RGA_COLOR_FMT_YUV422SP) {
  192. switch (ctx->out.colorspace) {
  193. case V4L2_COLORSPACE_REC709:
  194. dst_info.data.csc_mode = RGA_SRC_CSC_MODE_BT709_R0;
  195. break;
  196. default:
  197. dst_info.data.csc_mode = RGA_DST_CSC_MODE_BT601_R0;
  198. break;
  199. }
  200. }
  201. if (ctx->vflip)
  202. src_info.data.mir_mode |= RGA_SRC_MIRR_MODE_X;
  203. if (ctx->hflip)
  204. src_info.data.mir_mode |= RGA_SRC_MIRR_MODE_Y;
  205. switch (ctx->rotate) {
  206. case 90:
  207. src_info.data.rot_mode = RGA_SRC_ROT_MODE_90_DEGREE;
  208. break;
  209. case 180:
  210. src_info.data.rot_mode = RGA_SRC_ROT_MODE_180_DEGREE;
  211. break;
  212. case 270:
  213. src_info.data.rot_mode = RGA_SRC_ROT_MODE_270_DEGREE;
  214. break;
  215. default:
  216. src_info.data.rot_mode = RGA_SRC_ROT_MODE_0_DEGREE;
  217. break;
  218. }
  219. /*
  220. * Cacluate the up/down scaling mode/factor.
  221. *
  222. * RGA used to scale the picture first, and then rotate second,
  223. * so we need to swap the w/h when rotate degree is 90/270.
  224. */
  225. if (src_info.data.rot_mode == RGA_SRC_ROT_MODE_90_DEGREE ||
  226. src_info.data.rot_mode == RGA_SRC_ROT_MODE_270_DEGREE) {
  227. if (rga->version.major == 0 || rga->version.minor == 0) {
  228. if (dst_w == src_h)
  229. src_h -= 8;
  230. if (abs(src_w - dst_h) < 16)
  231. src_w -= 16;
  232. }
  233. scale_dst_h = dst_w;
  234. scale_dst_w = dst_h;
  235. } else {
  236. scale_dst_w = dst_w;
  237. scale_dst_h = dst_h;
  238. }
  239. if (src_w == scale_dst_w) {
  240. src_info.data.hscl_mode = RGA_SRC_HSCL_MODE_NO;
  241. x_factor.val = 0;
  242. } else if (src_w > scale_dst_w) {
  243. src_info.data.hscl_mode = RGA_SRC_HSCL_MODE_DOWN;
  244. x_factor.data.down_scale_factor =
  245. rga_get_scaling(src_w, scale_dst_w) + 1;
  246. } else {
  247. src_info.data.hscl_mode = RGA_SRC_HSCL_MODE_UP;
  248. x_factor.data.up_scale_factor =
  249. rga_get_scaling(src_w - 1, scale_dst_w - 1);
  250. }
  251. if (src_h == scale_dst_h) {
  252. src_info.data.vscl_mode = RGA_SRC_VSCL_MODE_NO;
  253. y_factor.val = 0;
  254. } else if (src_h > scale_dst_h) {
  255. src_info.data.vscl_mode = RGA_SRC_VSCL_MODE_DOWN;
  256. y_factor.data.down_scale_factor =
  257. rga_get_scaling(src_h, scale_dst_h) + 1;
  258. } else {
  259. src_info.data.vscl_mode = RGA_SRC_VSCL_MODE_UP;
  260. y_factor.data.up_scale_factor =
  261. rga_get_scaling(src_h - 1, scale_dst_h - 1);
  262. }
  263. /*
  264. * Cacluate the framebuffer virtual strides and active size,
  265. * note that the step of vir_stride / vir_width is 4 byte words
  266. */
  267. src_vir_info.data.vir_stride = ctx->in.stride >> 2;
  268. src_vir_info.data.vir_width = ctx->in.stride >> 2;
  269. src_act_info.data.act_height = src_h - 1;
  270. src_act_info.data.act_width = src_w - 1;
  271. dst_vir_info.data.vir_stride = ctx->out.stride >> 2;
  272. dst_act_info.data.act_height = dst_h - 1;
  273. dst_act_info.data.act_width = dst_w - 1;
  274. /*
  275. * Cacluate the source framebuffer base address with offset pixel.
  276. */
  277. src_offsets = rga_get_addr_offset(&ctx->in, src_x, src_y,
  278. src_w, src_h);
  279. /*
  280. * Configure the dest framebuffer base address with pixel offset.
  281. */
  282. offsets = rga_get_addr_offset(&ctx->out, dst_x, dst_y, dst_w, dst_h);
  283. dst_offset = rga_lookup_draw_pos(&offsets, src_info.data.rot_mode,
  284. src_info.data.mir_mode);
  285. dest[(RGA_SRC_Y_RGB_BASE_ADDR - RGA_MODE_BASE_REG) >> 2] =
  286. src_offsets.left_top.y_off;
  287. dest[(RGA_SRC_CB_BASE_ADDR - RGA_MODE_BASE_REG) >> 2] =
  288. src_offsets.left_top.u_off;
  289. dest[(RGA_SRC_CR_BASE_ADDR - RGA_MODE_BASE_REG) >> 2] =
  290. src_offsets.left_top.v_off;
  291. dest[(RGA_SRC_X_FACTOR - RGA_MODE_BASE_REG) >> 2] = x_factor.val;
  292. dest[(RGA_SRC_Y_FACTOR - RGA_MODE_BASE_REG) >> 2] = y_factor.val;
  293. dest[(RGA_SRC_VIR_INFO - RGA_MODE_BASE_REG) >> 2] = src_vir_info.val;
  294. dest[(RGA_SRC_ACT_INFO - RGA_MODE_BASE_REG) >> 2] = src_act_info.val;
  295. dest[(RGA_SRC_INFO - RGA_MODE_BASE_REG) >> 2] = src_info.val;
  296. dest[(RGA_DST_Y_RGB_BASE_ADDR - RGA_MODE_BASE_REG) >> 2] =
  297. dst_offset->y_off;
  298. dest[(RGA_DST_CB_BASE_ADDR - RGA_MODE_BASE_REG) >> 2] =
  299. dst_offset->u_off;
  300. dest[(RGA_DST_CR_BASE_ADDR - RGA_MODE_BASE_REG) >> 2] =
  301. dst_offset->v_off;
  302. dest[(RGA_DST_VIR_INFO - RGA_MODE_BASE_REG) >> 2] = dst_vir_info.val;
  303. dest[(RGA_DST_ACT_INFO - RGA_MODE_BASE_REG) >> 2] = dst_act_info.val;
  304. dest[(RGA_DST_INFO - RGA_MODE_BASE_REG) >> 2] = dst_info.val;
  305. }
  306. static void rga_cmd_set_mode(struct rga_ctx *ctx)
  307. {
  308. struct rockchip_rga *rga = ctx->rga;
  309. u32 *dest = rga->cmdbuf_virt;
  310. union rga_mode_ctrl mode;
  311. union rga_alpha_ctrl0 alpha_ctrl0;
  312. union rga_alpha_ctrl1 alpha_ctrl1;
  313. mode.val = 0;
  314. alpha_ctrl0.val = 0;
  315. alpha_ctrl1.val = 0;
  316. mode.data.gradient_sat = 1;
  317. mode.data.render = RGA_MODE_RENDER_BITBLT;
  318. mode.data.bitblt = RGA_MODE_BITBLT_MODE_SRC_TO_DST;
  319. /* disable alpha blending */
  320. dest[(RGA_ALPHA_CTRL0 - RGA_MODE_BASE_REG) >> 2] = alpha_ctrl0.val;
  321. dest[(RGA_ALPHA_CTRL1 - RGA_MODE_BASE_REG) >> 2] = alpha_ctrl1.val;
  322. dest[(RGA_MODE_CTRL - RGA_MODE_BASE_REG) >> 2] = mode.val;
  323. }
  324. static void rga_cmd_set(struct rga_ctx *ctx)
  325. {
  326. struct rockchip_rga *rga = ctx->rga;
  327. memset(rga->cmdbuf_virt, 0, RGA_CMDBUF_SIZE * 4);
  328. rga_cmd_set_src_addr(ctx, rga->src_mmu_pages);
  329. /*
  330. * Due to hardware bug,
  331. * src1 mmu also should be configured when using alpha blending.
  332. */
  333. rga_cmd_set_src1_addr(ctx, rga->dst_mmu_pages);
  334. rga_cmd_set_dst_addr(ctx, rga->dst_mmu_pages);
  335. rga_cmd_set_mode(ctx);
  336. rga_cmd_set_trans_info(ctx);
  337. rga_write(rga, RGA_CMD_BASE, rga->cmdbuf_phy);
  338. /* sync CMD buf for RGA */
  339. dma_sync_single_for_device(rga->dev, rga->cmdbuf_phy,
  340. PAGE_SIZE, DMA_BIDIRECTIONAL);
  341. }
  342. void rga_hw_start(struct rockchip_rga *rga)
  343. {
  344. struct rga_ctx *ctx = rga->curr;
  345. rga_cmd_set(ctx);
  346. rga_write(rga, RGA_SYS_CTRL, 0x00);
  347. rga_write(rga, RGA_SYS_CTRL, 0x22);
  348. rga_write(rga, RGA_INT, 0x600);
  349. rga_write(rga, RGA_CMD_CTRL, 0x1);
  350. }