sde_hw_lm.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/iopoll.h>
  6. #include "sde_kms.h"
  7. #include "sde_hw_catalog.h"
  8. #include "sde_hwio.h"
  9. #include "sde_hw_lm.h"
  10. #include "sde_hw_mdss.h"
  11. #include "sde_dbg.h"
  12. #include "sde_kms.h"
  13. #define LM_OP_MODE 0x00
  14. #define LM_OUT_SIZE 0x04
  15. #define LM_BORDER_COLOR_0 0x08
  16. #define LM_BORDER_COLOR_1 0x010
  17. /* These register are offset to mixer base + stage base */
  18. #define LM_BLEND0_OP 0x00
  19. #define LM_BLEND0_CONST_ALPHA 0x04
  20. #define LM_FG_COLOR_FILL_COLOR_0 0x08
  21. #define LM_FG_COLOR_FILL_COLOR_1 0x0C
  22. #define LM_FG_COLOR_FILL_SIZE 0x10
  23. #define LM_FG_COLOR_FILL_XY 0x14
  24. #define LM_BLEND0_FG_ALPHA 0x04
  25. #define LM_BLEND0_BG_ALPHA 0x08
  26. #define LM_MISR_CTRL 0x310
  27. #define LM_MISR_SIGNATURE 0x314
  28. static struct sde_lm_cfg *_lm_offset(enum sde_lm mixer,
  29. struct sde_mdss_cfg *m,
  30. void __iomem *addr,
  31. struct sde_hw_blk_reg_map *b)
  32. {
  33. int i;
  34. for (i = 0; i < m->mixer_count; i++) {
  35. if (mixer == m->mixer[i].id) {
  36. b->base_off = addr;
  37. b->blk_off = m->mixer[i].base;
  38. b->length = m->mixer[i].len;
  39. b->hwversion = m->hwversion;
  40. b->log_mask = SDE_DBG_MASK_LM;
  41. return &m->mixer[i];
  42. }
  43. }
  44. return ERR_PTR(-ENOMEM);
  45. }
  46. /**
  47. * _stage_offset(): returns the relative offset of the blend registers
  48. * for the stage to be setup
  49. * @c: mixer ctx contains the mixer to be programmed
  50. * @stage: stage index to setup
  51. */
  52. static inline int _stage_offset(struct sde_hw_mixer *ctx, enum sde_stage stage)
  53. {
  54. const struct sde_lm_sub_blks *sblk = ctx->cap->sblk;
  55. int rc;
  56. if (stage == SDE_STAGE_BASE)
  57. rc = -EINVAL;
  58. else if (stage <= sblk->maxblendstages)
  59. rc = sblk->blendstage_base[stage - SDE_STAGE_0];
  60. else
  61. rc = -EINVAL;
  62. return rc;
  63. }
  64. static void sde_hw_lm_setup_out(struct sde_hw_mixer *ctx,
  65. struct sde_hw_mixer_cfg *mixer)
  66. {
  67. struct sde_hw_blk_reg_map *c = &ctx->hw;
  68. u32 outsize;
  69. u32 op_mode;
  70. op_mode = SDE_REG_READ(c, LM_OP_MODE);
  71. outsize = mixer->out_height << 16 | mixer->out_width;
  72. SDE_REG_WRITE(c, LM_OUT_SIZE, outsize);
  73. /* SPLIT_LEFT_RIGHT */
  74. if (mixer->right_mixer)
  75. op_mode |= BIT(31);
  76. else
  77. op_mode &= ~BIT(31);
  78. SDE_REG_WRITE(c, LM_OP_MODE, op_mode);
  79. }
  80. static void sde_hw_lm_setup_border_color(struct sde_hw_mixer *ctx,
  81. struct sde_mdss_color *color,
  82. u8 border_en)
  83. {
  84. struct sde_hw_blk_reg_map *c = &ctx->hw;
  85. if (border_en) {
  86. SDE_REG_WRITE(c, LM_BORDER_COLOR_0,
  87. (color->color_0 & 0xFFF) |
  88. ((color->color_1 & 0xFFF) << 0x10));
  89. SDE_REG_WRITE(c, LM_BORDER_COLOR_1,
  90. (color->color_2 & 0xFFF) |
  91. ((color->color_3 & 0xFFF) << 0x10));
  92. }
  93. }
  94. static void sde_hw_lm_setup_blend_config_combined_alpha(
  95. struct sde_hw_mixer *ctx, u32 stage,
  96. u32 fg_alpha, u32 bg_alpha, u32 blend_op)
  97. {
  98. struct sde_hw_blk_reg_map *c = &ctx->hw;
  99. int stage_off;
  100. u32 const_alpha;
  101. if (stage == SDE_STAGE_BASE)
  102. return;
  103. stage_off = _stage_offset(ctx, stage);
  104. if (WARN_ON(stage_off < 0))
  105. return;
  106. const_alpha = (bg_alpha & 0xFF) | ((fg_alpha & 0xFF) << 16);
  107. SDE_REG_WRITE(c, LM_BLEND0_CONST_ALPHA + stage_off, const_alpha);
  108. SDE_REG_WRITE(c, LM_BLEND0_OP + stage_off, blend_op);
  109. }
  110. static void sde_hw_lm_setup_blend_config(struct sde_hw_mixer *ctx,
  111. u32 stage, u32 fg_alpha, u32 bg_alpha, u32 blend_op)
  112. {
  113. struct sde_hw_blk_reg_map *c = &ctx->hw;
  114. int stage_off;
  115. if (stage == SDE_STAGE_BASE)
  116. return;
  117. stage_off = _stage_offset(ctx, stage);
  118. if (WARN_ON(stage_off < 0))
  119. return;
  120. SDE_REG_WRITE(c, LM_BLEND0_FG_ALPHA + stage_off, fg_alpha);
  121. SDE_REG_WRITE(c, LM_BLEND0_BG_ALPHA + stage_off, bg_alpha);
  122. SDE_REG_WRITE(c, LM_BLEND0_OP + stage_off, blend_op);
  123. }
  124. static void sde_hw_lm_setup_color3(struct sde_hw_mixer *ctx,
  125. uint32_t mixer_op_mode)
  126. {
  127. struct sde_hw_blk_reg_map *c = &ctx->hw;
  128. int op_mode;
  129. /* read the existing op_mode configuration */
  130. op_mode = SDE_REG_READ(c, LM_OP_MODE);
  131. op_mode = (op_mode & (BIT(31) | BIT(30))) | mixer_op_mode;
  132. SDE_REG_WRITE(c, LM_OP_MODE, op_mode);
  133. }
  134. static void sde_hw_lm_gc(struct sde_hw_mixer *mixer,
  135. void *cfg)
  136. {
  137. }
  138. static void sde_hw_lm_clear_dim_layer(struct sde_hw_mixer *ctx)
  139. {
  140. struct sde_hw_blk_reg_map *c = &ctx->hw;
  141. const struct sde_lm_sub_blks *sblk = ctx->cap->sblk;
  142. int stage_off, i;
  143. u32 reset = BIT(16), val;
  144. reset = ~reset;
  145. for (i = SDE_STAGE_0; i <= sblk->maxblendstages; i++) {
  146. stage_off = _stage_offset(ctx, i);
  147. if (WARN_ON(stage_off < 0))
  148. return;
  149. /*
  150. * read the existing blendn_op register and clear only DIM layer
  151. * bit (color_fill bit)
  152. */
  153. val = SDE_REG_READ(c, LM_BLEND0_OP + stage_off);
  154. val &= reset;
  155. SDE_REG_WRITE(c, LM_BLEND0_OP + stage_off, val);
  156. }
  157. }
  158. static void sde_hw_lm_setup_dim_layer(struct sde_hw_mixer *ctx,
  159. struct sde_hw_dim_layer *dim_layer)
  160. {
  161. struct sde_hw_blk_reg_map *c = &ctx->hw;
  162. int stage_off;
  163. u32 val = 0, alpha = 0;
  164. if (dim_layer->stage == SDE_STAGE_BASE)
  165. return;
  166. stage_off = _stage_offset(ctx, dim_layer->stage);
  167. if (stage_off < 0) {
  168. SDE_ERROR("invalid stage_off:%d for dim layer\n", stage_off);
  169. return;
  170. }
  171. alpha = dim_layer->color_fill.color_3 & 0xFF;
  172. val = ((dim_layer->color_fill.color_1 << 2) & 0xFFF) << 16 |
  173. ((dim_layer->color_fill.color_0 << 2) & 0xFFF);
  174. SDE_REG_WRITE(c, LM_FG_COLOR_FILL_COLOR_0 + stage_off, val);
  175. val = (alpha << 4) << 16 |
  176. ((dim_layer->color_fill.color_2 << 2) & 0xFFF);
  177. SDE_REG_WRITE(c, LM_FG_COLOR_FILL_COLOR_1 + stage_off, val);
  178. val = dim_layer->rect.h << 16 | dim_layer->rect.w;
  179. SDE_REG_WRITE(c, LM_FG_COLOR_FILL_SIZE + stage_off, val);
  180. val = dim_layer->rect.y << 16 | dim_layer->rect.x;
  181. SDE_REG_WRITE(c, LM_FG_COLOR_FILL_XY + stage_off, val);
  182. val = BIT(16); /* enable dim layer */
  183. val |= SDE_BLEND_FG_ALPHA_FG_CONST | SDE_BLEND_BG_ALPHA_BG_CONST;
  184. if (dim_layer->flags & SDE_DRM_DIM_LAYER_EXCLUSIVE)
  185. val |= BIT(17);
  186. else
  187. val &= ~BIT(17);
  188. SDE_REG_WRITE(c, LM_BLEND0_OP + stage_off, val);
  189. val = (alpha << 16) | (0xff - alpha);
  190. SDE_REG_WRITE(c, LM_BLEND0_CONST_ALPHA + stage_off, val);
  191. }
  192. static void sde_hw_lm_setup_misr(struct sde_hw_mixer *ctx,
  193. bool enable, u32 frame_count)
  194. {
  195. struct sde_hw_blk_reg_map *c = &ctx->hw;
  196. u32 config = 0;
  197. SDE_REG_WRITE(c, LM_MISR_CTRL, MISR_CTRL_STATUS_CLEAR);
  198. /* clear misr data */
  199. wmb();
  200. if (enable)
  201. config = (frame_count & MISR_FRAME_COUNT_MASK) |
  202. MISR_CTRL_ENABLE | INTF_MISR_CTRL_FREE_RUN_MASK;
  203. SDE_REG_WRITE(c, LM_MISR_CTRL, config);
  204. }
  205. static int sde_hw_lm_collect_misr(struct sde_hw_mixer *ctx, bool nonblock,
  206. u32 *misr_value)
  207. {
  208. struct sde_hw_blk_reg_map *c = &ctx->hw;
  209. u32 ctrl = 0;
  210. if (!misr_value)
  211. return -EINVAL;
  212. ctrl = SDE_REG_READ(c, LM_MISR_CTRL);
  213. if (!nonblock) {
  214. if (ctrl & MISR_CTRL_ENABLE) {
  215. int rc;
  216. rc = readl_poll_timeout(c->base_off + c->blk_off +
  217. LM_MISR_CTRL, ctrl,
  218. (ctrl & MISR_CTRL_STATUS) > 0, 500,
  219. 84000);
  220. if (rc)
  221. return rc;
  222. } else {
  223. return -EINVAL;
  224. }
  225. }
  226. *misr_value = SDE_REG_READ(c, LM_MISR_SIGNATURE);
  227. return 0;
  228. }
  229. static void _setup_mixer_ops(struct sde_mdss_cfg *m,
  230. struct sde_hw_lm_ops *ops,
  231. unsigned long features)
  232. {
  233. ops->setup_mixer_out = sde_hw_lm_setup_out;
  234. if (test_bit(SDE_MIXER_COMBINED_ALPHA, &features))
  235. ops->setup_blend_config =
  236. sde_hw_lm_setup_blend_config_combined_alpha;
  237. else
  238. ops->setup_blend_config = sde_hw_lm_setup_blend_config;
  239. ops->setup_alpha_out = sde_hw_lm_setup_color3;
  240. ops->setup_border_color = sde_hw_lm_setup_border_color;
  241. ops->setup_gc = sde_hw_lm_gc;
  242. ops->setup_misr = sde_hw_lm_setup_misr;
  243. ops->collect_misr = sde_hw_lm_collect_misr;
  244. if (test_bit(SDE_DIM_LAYER, &features)) {
  245. ops->setup_dim_layer = sde_hw_lm_setup_dim_layer;
  246. ops->clear_dim_layer = sde_hw_lm_clear_dim_layer;
  247. }
  248. };
  249. static struct sde_hw_blk_ops sde_hw_ops = {
  250. .start = NULL,
  251. .stop = NULL,
  252. };
  253. struct sde_hw_mixer *sde_hw_lm_init(enum sde_lm idx,
  254. void __iomem *addr,
  255. struct sde_mdss_cfg *m)
  256. {
  257. struct sde_hw_mixer *c;
  258. struct sde_lm_cfg *cfg;
  259. int rc;
  260. c = kzalloc(sizeof(*c), GFP_KERNEL);
  261. if (!c)
  262. return ERR_PTR(-ENOMEM);
  263. cfg = _lm_offset(idx, m, addr, &c->hw);
  264. if (IS_ERR_OR_NULL(cfg)) {
  265. kfree(c);
  266. return ERR_PTR(-EINVAL);
  267. }
  268. /* Assign ops */
  269. c->idx = idx;
  270. c->cap = cfg;
  271. _setup_mixer_ops(m, &c->ops, c->cap->features);
  272. rc = sde_hw_blk_init(&c->base, SDE_HW_BLK_LM, idx, &sde_hw_ops);
  273. if (rc) {
  274. SDE_ERROR("failed to init hw blk %d\n", rc);
  275. goto blk_init_error;
  276. }
  277. sde_dbg_reg_register_dump_range(SDE_DBG_NAME, cfg->name, c->hw.blk_off,
  278. c->hw.blk_off + c->hw.length, c->hw.xin_id);
  279. return c;
  280. blk_init_error:
  281. kzfree(c);
  282. return ERR_PTR(rc);
  283. }
  284. void sde_hw_lm_destroy(struct sde_hw_mixer *lm)
  285. {
  286. if (lm)
  287. sde_hw_blk_destroy(&lm->base);
  288. kfree(lm);
  289. }