sde_hw_lm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. #define LM_NOISE_LAYER 0x320
  29. static struct sde_lm_cfg *_lm_offset(enum sde_lm mixer,
  30. struct sde_mdss_cfg *m,
  31. void __iomem *addr,
  32. struct sde_hw_blk_reg_map *b)
  33. {
  34. int i;
  35. for (i = 0; i < m->mixer_count; i++) {
  36. if (mixer == m->mixer[i].id) {
  37. b->base_off = addr;
  38. b->blk_off = m->mixer[i].base;
  39. b->length = m->mixer[i].len;
  40. b->hwversion = m->hwversion;
  41. b->log_mask = SDE_DBG_MASK_LM;
  42. return &m->mixer[i];
  43. }
  44. }
  45. return ERR_PTR(-ENOMEM);
  46. }
  47. /**
  48. * _stage_offset(): returns the relative offset of the blend registers
  49. * for the stage to be setup
  50. * @c: mixer ctx contains the mixer to be programmed
  51. * @stage: stage index to setup
  52. */
  53. static inline int _stage_offset(struct sde_hw_mixer *ctx, enum sde_stage stage)
  54. {
  55. const struct sde_lm_sub_blks *sblk = ctx->cap->sblk;
  56. int rc;
  57. if (stage == SDE_STAGE_BASE)
  58. rc = -EINVAL;
  59. else if (stage <= sblk->maxblendstages)
  60. rc = sblk->blendstage_base[stage - SDE_STAGE_0];
  61. else
  62. rc = -EINVAL;
  63. return rc;
  64. }
  65. static void sde_hw_lm_setup_out(struct sde_hw_mixer *ctx,
  66. struct sde_hw_mixer_cfg *mixer)
  67. {
  68. struct sde_hw_blk_reg_map *c = &ctx->hw;
  69. u32 outsize;
  70. u32 op_mode;
  71. op_mode = SDE_REG_READ(c, LM_OP_MODE);
  72. outsize = mixer->out_height << 16 | mixer->out_width;
  73. SDE_REG_WRITE(c, LM_OUT_SIZE, outsize);
  74. /* SPLIT_LEFT_RIGHT */
  75. if (mixer->right_mixer)
  76. op_mode |= BIT(31);
  77. else
  78. op_mode &= ~BIT(31);
  79. SDE_REG_WRITE(c, LM_OP_MODE, op_mode);
  80. }
  81. static void sde_hw_lm_setup_border_color(struct sde_hw_mixer *ctx,
  82. struct sde_mdss_color *color,
  83. u8 border_en)
  84. {
  85. struct sde_hw_blk_reg_map *c = &ctx->hw;
  86. if (border_en) {
  87. SDE_REG_WRITE(c, LM_BORDER_COLOR_0,
  88. (color->color_0 & 0xFFF) |
  89. ((color->color_1 & 0xFFF) << 0x10));
  90. SDE_REG_WRITE(c, LM_BORDER_COLOR_1,
  91. (color->color_2 & 0xFFF) |
  92. ((color->color_3 & 0xFFF) << 0x10));
  93. }
  94. }
  95. static void sde_hw_lm_setup_blend_config_combined_alpha(
  96. struct sde_hw_mixer *ctx, u32 stage,
  97. u32 fg_alpha, u32 bg_alpha, u32 blend_op)
  98. {
  99. struct sde_hw_blk_reg_map *c = &ctx->hw;
  100. int stage_off;
  101. u32 const_alpha;
  102. if (stage == SDE_STAGE_BASE)
  103. return;
  104. stage_off = _stage_offset(ctx, stage);
  105. if (WARN_ON(stage_off < 0))
  106. return;
  107. const_alpha = (bg_alpha & 0xFF) | ((fg_alpha & 0xFF) << 16);
  108. SDE_REG_WRITE(c, LM_BLEND0_CONST_ALPHA + stage_off, const_alpha);
  109. SDE_REG_WRITE(c, LM_BLEND0_OP + stage_off, blend_op);
  110. }
  111. static void sde_hw_lm_setup_blend_config(struct sde_hw_mixer *ctx,
  112. u32 stage, u32 fg_alpha, u32 bg_alpha, u32 blend_op)
  113. {
  114. struct sde_hw_blk_reg_map *c = &ctx->hw;
  115. int stage_off;
  116. if (stage == SDE_STAGE_BASE)
  117. return;
  118. stage_off = _stage_offset(ctx, stage);
  119. if (WARN_ON(stage_off < 0))
  120. return;
  121. SDE_REG_WRITE(c, LM_BLEND0_FG_ALPHA + stage_off, fg_alpha);
  122. SDE_REG_WRITE(c, LM_BLEND0_BG_ALPHA + stage_off, bg_alpha);
  123. SDE_REG_WRITE(c, LM_BLEND0_OP + stage_off, blend_op);
  124. }
  125. static void sde_hw_lm_setup_color3(struct sde_hw_mixer *ctx,
  126. uint32_t mixer_op_mode)
  127. {
  128. struct sde_hw_blk_reg_map *c = &ctx->hw;
  129. int op_mode;
  130. /* read the existing op_mode configuration */
  131. op_mode = SDE_REG_READ(c, LM_OP_MODE);
  132. op_mode = (op_mode & (BIT(31) | BIT(30))) | mixer_op_mode;
  133. SDE_REG_WRITE(c, LM_OP_MODE, op_mode);
  134. }
  135. static void sde_hw_lm_gc(struct sde_hw_mixer *mixer,
  136. void *cfg)
  137. {
  138. }
  139. static void sde_hw_lm_clear_dim_layer(struct sde_hw_mixer *ctx)
  140. {
  141. struct sde_hw_blk_reg_map *c = &ctx->hw;
  142. const struct sde_lm_sub_blks *sblk = ctx->cap->sblk;
  143. int stage_off, i;
  144. u32 reset = BIT(16), val;
  145. reset = ~reset;
  146. for (i = SDE_STAGE_0; i <= sblk->maxblendstages; i++) {
  147. stage_off = _stage_offset(ctx, i);
  148. if (WARN_ON(stage_off < 0))
  149. return;
  150. /*
  151. * read the existing blendn_op register and clear only DIM layer
  152. * bit (color_fill bit)
  153. */
  154. val = SDE_REG_READ(c, LM_BLEND0_OP + stage_off);
  155. val &= reset;
  156. SDE_REG_WRITE(c, LM_BLEND0_OP + stage_off, val);
  157. }
  158. }
  159. static void sde_hw_lm_setup_dim_layer(struct sde_hw_mixer *ctx,
  160. struct sde_hw_dim_layer *dim_layer)
  161. {
  162. struct sde_hw_blk_reg_map *c = &ctx->hw;
  163. int stage_off;
  164. u32 val = 0, alpha = 0;
  165. if (dim_layer->stage == SDE_STAGE_BASE)
  166. return;
  167. stage_off = _stage_offset(ctx, dim_layer->stage);
  168. if (stage_off < 0) {
  169. SDE_ERROR("invalid stage_off:%d for dim layer\n", stage_off);
  170. return;
  171. }
  172. alpha = dim_layer->color_fill.color_3 & 0xFF;
  173. val = ((dim_layer->color_fill.color_1 << 2) & 0xFFF) << 16 |
  174. ((dim_layer->color_fill.color_0 << 2) & 0xFFF);
  175. SDE_REG_WRITE(c, LM_FG_COLOR_FILL_COLOR_0 + stage_off, val);
  176. val = (alpha << 4) << 16 |
  177. ((dim_layer->color_fill.color_2 << 2) & 0xFFF);
  178. SDE_REG_WRITE(c, LM_FG_COLOR_FILL_COLOR_1 + stage_off, val);
  179. val = dim_layer->rect.h << 16 | dim_layer->rect.w;
  180. SDE_REG_WRITE(c, LM_FG_COLOR_FILL_SIZE + stage_off, val);
  181. val = dim_layer->rect.y << 16 | dim_layer->rect.x;
  182. SDE_REG_WRITE(c, LM_FG_COLOR_FILL_XY + stage_off, val);
  183. val = BIT(16); /* enable dim layer */
  184. val |= SDE_BLEND_FG_ALPHA_FG_CONST | SDE_BLEND_BG_ALPHA_BG_CONST;
  185. if (dim_layer->flags & SDE_DRM_DIM_LAYER_EXCLUSIVE)
  186. val |= BIT(17);
  187. else
  188. val &= ~BIT(17);
  189. SDE_REG_WRITE(c, LM_BLEND0_OP + stage_off, val);
  190. val = (alpha << 16) | (0xff - alpha);
  191. SDE_REG_WRITE(c, LM_BLEND0_CONST_ALPHA + stage_off, val);
  192. }
  193. static void sde_hw_lm_setup_misr(struct sde_hw_mixer *ctx,
  194. bool enable, u32 frame_count)
  195. {
  196. struct sde_hw_blk_reg_map *c = &ctx->hw;
  197. u32 config = 0;
  198. SDE_REG_WRITE(c, LM_MISR_CTRL, MISR_CTRL_STATUS_CLEAR);
  199. /* clear misr data */
  200. wmb();
  201. if (enable)
  202. config = (frame_count & MISR_FRAME_COUNT_MASK) |
  203. MISR_CTRL_ENABLE | INTF_MISR_CTRL_FREE_RUN_MASK;
  204. SDE_REG_WRITE(c, LM_MISR_CTRL, config);
  205. }
  206. static int sde_hw_lm_collect_misr(struct sde_hw_mixer *ctx, bool nonblock,
  207. u32 *misr_value)
  208. {
  209. struct sde_hw_blk_reg_map *c = &ctx->hw;
  210. u32 ctrl = 0;
  211. if (!misr_value)
  212. return -EINVAL;
  213. ctrl = SDE_REG_READ(c, LM_MISR_CTRL);
  214. if (!nonblock) {
  215. if (ctrl & MISR_CTRL_ENABLE) {
  216. int rc;
  217. rc = readl_poll_timeout(c->base_off + c->blk_off +
  218. LM_MISR_CTRL, ctrl,
  219. (ctrl & MISR_CTRL_STATUS) > 0, 500,
  220. 84000);
  221. if (rc)
  222. return rc;
  223. } else {
  224. return -EINVAL;
  225. }
  226. }
  227. *misr_value = SDE_REG_READ(c, LM_MISR_SIGNATURE);
  228. return 0;
  229. }
  230. static void sde_hw_clear_noise_layer(struct sde_hw_mixer *ctx)
  231. {
  232. struct sde_hw_blk_reg_map *c = &ctx->hw;
  233. const struct sde_lm_sub_blks *sblk = ctx->cap->sblk;
  234. int stage_off, i;
  235. u32 reset = BIT(18) | BIT(31), val;
  236. reset = ~reset;
  237. for (i = SDE_STAGE_0; i <= sblk->maxblendstages; i++) {
  238. stage_off = _stage_offset(ctx, i);
  239. if (WARN_ON(stage_off < 0))
  240. return;
  241. /**
  242. * read the blendn_op register and clear only noise layer
  243. */
  244. val = SDE_REG_READ(c, LM_BLEND0_OP + stage_off);
  245. val &= reset;
  246. SDE_REG_WRITE(c, LM_BLEND0_OP + stage_off, val);
  247. }
  248. SDE_REG_WRITE(c, LM_NOISE_LAYER, 0);
  249. }
  250. static int sde_hw_lm_setup_noise_layer(struct sde_hw_mixer *ctx,
  251. struct sde_hw_noise_layer_cfg *cfg)
  252. {
  253. struct sde_hw_blk_reg_map *c = &ctx->hw;
  254. int stage_off;
  255. u32 val = 0, alpha = 0;
  256. const struct sde_lm_sub_blks *sblk = ctx->cap->sblk;
  257. struct sde_hw_mixer_cfg *mixer = &ctx->cfg;
  258. sde_hw_clear_noise_layer(ctx);
  259. if (!cfg)
  260. return 0;
  261. if (cfg->zposn == SDE_STAGE_BASE || cfg->zposn + 1 != cfg->zposattn ||
  262. cfg->zposattn >= sblk->maxblendstages) {
  263. SDE_ERROR("invalid zposn %d zposattn %d max stage %d\n",
  264. cfg->zposn, cfg->zposattn, sblk->maxblendstages);
  265. return -EINVAL;
  266. }
  267. stage_off = _stage_offset(ctx, cfg->zposn);
  268. if (stage_off < 0) {
  269. SDE_ERROR("invalid stage_off:%d for noise layer stage_off %d\n",
  270. cfg->zposn, stage_off);
  271. return -EINVAL;
  272. }
  273. val = BIT(18) | BIT(31);
  274. val |= (1 << 8);
  275. alpha = 255 | (cfg->alpha_noise << 16);
  276. SDE_REG_WRITE(c, LM_BLEND0_OP + stage_off, val);
  277. SDE_REG_WRITE(c, LM_BLEND0_CONST_ALPHA + stage_off, alpha);
  278. val = ctx->cfg.out_width | (ctx->cfg.out_height << 16);
  279. SDE_REG_WRITE(c, LM_FG_COLOR_FILL_SIZE + stage_off, val);
  280. val = SDE_REG_READ(c, LM_OP_MODE);
  281. val = (1 << cfg->zposn) | val;
  282. SDE_REG_WRITE(c, LM_OP_MODE, val);
  283. stage_off = _stage_offset(ctx, cfg->zposattn);
  284. if (stage_off < 0) {
  285. SDE_ERROR("invalid stage_off:%d for noise layer\n",
  286. cfg->zposattn);
  287. sde_hw_clear_noise_layer(ctx);
  288. return -EINVAL;
  289. }
  290. val = 1 | BIT(31) | BIT(16);
  291. val |= BIT(2);
  292. val |= (1 << 8);
  293. alpha = cfg->attn_factor;
  294. SDE_REG_WRITE(c, LM_BLEND0_OP + stage_off, val);
  295. SDE_REG_WRITE(c, LM_BLEND0_CONST_ALPHA + stage_off, alpha);
  296. val = SDE_REG_READ(c, LM_OP_MODE);
  297. val = (1 << cfg->zposattn) | val;
  298. SDE_REG_WRITE(c, LM_OP_MODE, val);
  299. val = ctx->cfg.out_width | (ctx->cfg.out_height << 16);
  300. SDE_REG_WRITE(c, LM_FG_COLOR_FILL_SIZE + stage_off, val);
  301. val = 1;
  302. if (mixer->right_mixer)
  303. val |= (((mixer->out_width % 4) & 0x3) << 4);
  304. if (cfg->flags & DRM_NOISE_TEMPORAL_FLAG)
  305. val |= BIT(1);
  306. val |= ((cfg->strength & 0x7) << 8);
  307. SDE_REG_WRITE(c, LM_NOISE_LAYER, val);
  308. return 0;
  309. }
  310. static void _setup_mixer_ops(struct sde_mdss_cfg *m,
  311. struct sde_hw_lm_ops *ops,
  312. unsigned long features)
  313. {
  314. ops->setup_mixer_out = sde_hw_lm_setup_out;
  315. if (test_bit(SDE_MIXER_COMBINED_ALPHA, &features))
  316. ops->setup_blend_config =
  317. sde_hw_lm_setup_blend_config_combined_alpha;
  318. else
  319. ops->setup_blend_config = sde_hw_lm_setup_blend_config;
  320. ops->setup_alpha_out = sde_hw_lm_setup_color3;
  321. ops->setup_border_color = sde_hw_lm_setup_border_color;
  322. ops->setup_gc = sde_hw_lm_gc;
  323. ops->setup_misr = sde_hw_lm_setup_misr;
  324. ops->collect_misr = sde_hw_lm_collect_misr;
  325. if (test_bit(SDE_DIM_LAYER, &features)) {
  326. ops->setup_dim_layer = sde_hw_lm_setup_dim_layer;
  327. ops->clear_dim_layer = sde_hw_lm_clear_dim_layer;
  328. }
  329. if (test_bit(SDE_MIXER_NOISE_LAYER, &features))
  330. ops->setup_noise_layer = sde_hw_lm_setup_noise_layer;
  331. };
  332. static struct sde_hw_blk_ops sde_hw_ops = {
  333. .start = NULL,
  334. .stop = NULL,
  335. };
  336. struct sde_hw_mixer *sde_hw_lm_init(enum sde_lm idx,
  337. void __iomem *addr,
  338. struct sde_mdss_cfg *m)
  339. {
  340. struct sde_hw_mixer *c;
  341. struct sde_lm_cfg *cfg;
  342. int rc;
  343. c = kzalloc(sizeof(*c), GFP_KERNEL);
  344. if (!c)
  345. return ERR_PTR(-ENOMEM);
  346. cfg = _lm_offset(idx, m, addr, &c->hw);
  347. if (IS_ERR_OR_NULL(cfg)) {
  348. kfree(c);
  349. return ERR_PTR(-EINVAL);
  350. }
  351. /* Assign ops */
  352. c->idx = idx;
  353. c->cap = cfg;
  354. _setup_mixer_ops(m, &c->ops, c->cap->features);
  355. rc = sde_hw_blk_init(&c->base, SDE_HW_BLK_LM, idx, &sde_hw_ops);
  356. if (rc) {
  357. SDE_ERROR("failed to init hw blk %d\n", rc);
  358. goto blk_init_error;
  359. }
  360. sde_dbg_reg_register_dump_range(SDE_DBG_NAME, cfg->name, c->hw.blk_off,
  361. c->hw.blk_off + c->hw.length, c->hw.xin_id);
  362. return c;
  363. blk_init_error:
  364. kfree(c);
  365. return ERR_PTR(rc);
  366. }
  367. void sde_hw_lm_destroy(struct sde_hw_mixer *lm)
  368. {
  369. if (lm)
  370. sde_hw_blk_destroy(&lm->base);
  371. kfree(lm);
  372. }