sde_hw_pingpong.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/iopoll.h>
  6. #include "sde_hw_mdss.h"
  7. #include "sde_hwio.h"
  8. #include "sde_hw_catalog.h"
  9. #include "sde_hw_pingpong.h"
  10. #include "sde_dbg.h"
  11. #include "sde_kms.h"
  12. #define PP_TEAR_CHECK_EN 0x000
  13. #define PP_SYNC_CONFIG_VSYNC 0x004
  14. #define PP_SYNC_CONFIG_HEIGHT 0x008
  15. #define PP_SYNC_WRCOUNT 0x00C
  16. #define PP_VSYNC_INIT_VAL 0x010
  17. #define PP_INT_COUNT_VAL 0x014
  18. #define PP_SYNC_THRESH 0x018
  19. #define PP_START_POS 0x01C
  20. #define PP_RD_PTR_IRQ 0x020
  21. #define PP_WR_PTR_IRQ 0x024
  22. #define PP_OUT_LINE_COUNT 0x028
  23. #define PP_LINE_COUNT 0x02C
  24. #define PP_AUTOREFRESH_CONFIG 0x030
  25. #define PP_FBC_MODE 0x034
  26. #define PP_FBC_BUDGET_CTL 0x038
  27. #define PP_FBC_LOSSY_MODE 0x03C
  28. #define PP_DSC_MODE 0x0a0
  29. #define PP_DCE_DATA_IN_SWAP 0x0ac
  30. #define PP_DCE_DATA_OUT_SWAP 0x0c8
  31. #define DITHER_DEPTH_MAP_INDEX 9
  32. static u32 dither_depth_map[DITHER_DEPTH_MAP_INDEX] = {
  33. 0, 0, 0, 0, 0, 1, 2, 3, 3
  34. };
  35. #define MERGE_3D_MODE 0x004
  36. #define MERGE_3D_MUX 0x000
  37. static struct sde_merge_3d_cfg *_merge_3d_offset(enum sde_merge_3d idx,
  38. struct sde_mdss_cfg *m,
  39. void __iomem *addr,
  40. struct sde_hw_blk_reg_map *b)
  41. {
  42. int i;
  43. for (i = 0; i < m->merge_3d_count; i++) {
  44. if (idx == m->merge_3d[i].id) {
  45. b->base_off = addr;
  46. b->blk_off = m->merge_3d[i].base;
  47. b->length = m->merge_3d[i].len;
  48. b->hwversion = m->hwversion;
  49. b->log_mask = SDE_DBG_MASK_PINGPONG;
  50. return &m->merge_3d[i];
  51. }
  52. }
  53. return ERR_PTR(-EINVAL);
  54. }
  55. static void _sde_hw_merge_3d_setup_blend_mode(struct sde_hw_merge_3d *ctx,
  56. enum sde_3d_blend_mode cfg)
  57. {
  58. struct sde_hw_blk_reg_map *c;
  59. u32 mode = 0;
  60. if (!ctx)
  61. return;
  62. c = &ctx->hw;
  63. if (cfg) {
  64. mode = BIT(0);
  65. mode |= (cfg - 0x1) << 1;
  66. }
  67. SDE_REG_WRITE(c, MERGE_3D_MODE, mode);
  68. }
  69. static void sde_hw_merge_3d_reset_blend_mode(struct sde_hw_merge_3d *ctx)
  70. {
  71. struct sde_hw_blk_reg_map *c;
  72. if (!ctx)
  73. return;
  74. c = &ctx->hw;
  75. SDE_REG_WRITE(c, MERGE_3D_MODE, 0x0);
  76. SDE_REG_WRITE(c, MERGE_3D_MUX, 0x0);
  77. }
  78. static void _setup_merge_3d_ops(struct sde_hw_merge_3d_ops *ops,
  79. const struct sde_merge_3d_cfg *hw_cap)
  80. {
  81. ops->setup_blend_mode = _sde_hw_merge_3d_setup_blend_mode;
  82. ops->reset_blend_mode = sde_hw_merge_3d_reset_blend_mode;
  83. }
  84. static struct sde_hw_merge_3d *_sde_pp_merge_3d_init(enum sde_merge_3d idx,
  85. void __iomem *addr,
  86. struct sde_mdss_cfg *m)
  87. {
  88. struct sde_hw_merge_3d *c;
  89. struct sde_merge_3d_cfg *cfg;
  90. static u32 merge3d_init_mask;
  91. if (idx < MERGE_3D_0)
  92. return NULL;
  93. c = kzalloc(sizeof(*c), GFP_KERNEL);
  94. if (!c)
  95. return ERR_PTR(-ENOMEM);
  96. cfg = _merge_3d_offset(idx, m, addr, &c->hw);
  97. if (IS_ERR_OR_NULL(cfg)) {
  98. pr_err("invalid merge_3d cfg%d\n", idx);
  99. kfree(c);
  100. return ERR_PTR(-EINVAL);
  101. }
  102. c->idx = idx;
  103. c->caps = cfg;
  104. _setup_merge_3d_ops(&c->ops, c->caps);
  105. if (!(merge3d_init_mask & BIT(idx))) {
  106. sde_dbg_reg_register_dump_range(SDE_DBG_NAME, cfg->name,
  107. c->hw.blk_off, c->hw.blk_off + c->hw.length,
  108. c->hw.xin_id);
  109. merge3d_init_mask |= BIT(idx);
  110. }
  111. return c;
  112. }
  113. static struct sde_pingpong_cfg *_pingpong_offset(enum sde_pingpong pp,
  114. struct sde_mdss_cfg *m,
  115. void __iomem *addr,
  116. struct sde_hw_blk_reg_map *b)
  117. {
  118. int i;
  119. for (i = 0; i < m->pingpong_count; i++) {
  120. if (pp == m->pingpong[i].id) {
  121. b->base_off = addr;
  122. b->blk_off = m->pingpong[i].base;
  123. b->length = m->pingpong[i].len;
  124. b->hwversion = m->hwversion;
  125. b->log_mask = SDE_DBG_MASK_PINGPONG;
  126. return &m->pingpong[i];
  127. }
  128. }
  129. return ERR_PTR(-EINVAL);
  130. }
  131. static int sde_hw_pp_setup_te_config(struct sde_hw_pingpong *pp,
  132. struct sde_hw_tear_check *te)
  133. {
  134. struct sde_hw_blk_reg_map *c;
  135. int cfg;
  136. if (!pp || !te)
  137. return -EINVAL;
  138. c = &pp->hw;
  139. cfg = BIT(19); /*VSYNC_COUNTER_EN */
  140. if (te->hw_vsync_mode)
  141. cfg |= BIT(20);
  142. cfg |= te->vsync_count;
  143. SDE_REG_WRITE(c, PP_SYNC_CONFIG_VSYNC, cfg);
  144. SDE_REG_WRITE(c, PP_SYNC_CONFIG_HEIGHT, te->sync_cfg_height);
  145. SDE_REG_WRITE(c, PP_VSYNC_INIT_VAL, te->vsync_init_val);
  146. SDE_REG_WRITE(c, PP_RD_PTR_IRQ, te->rd_ptr_irq);
  147. SDE_REG_WRITE(c, PP_WR_PTR_IRQ, te->wr_ptr_irq);
  148. SDE_REG_WRITE(c, PP_START_POS, te->start_pos);
  149. SDE_REG_WRITE(c, PP_SYNC_THRESH,
  150. ((te->sync_threshold_continue << 16) |
  151. te->sync_threshold_start));
  152. SDE_REG_WRITE(c, PP_SYNC_WRCOUNT,
  153. (te->start_pos + te->sync_threshold_start + 1));
  154. return 0;
  155. }
  156. static void sde_hw_pp_update_te(struct sde_hw_pingpong *pp,
  157. struct sde_hw_tear_check *te)
  158. {
  159. struct sde_hw_blk_reg_map *c;
  160. int cfg;
  161. if (!pp || !te)
  162. return;
  163. c = &pp->hw;
  164. cfg = SDE_REG_READ(c, PP_SYNC_THRESH);
  165. cfg &= ~0xFFFF;
  166. cfg |= te->sync_threshold_start;
  167. SDE_REG_WRITE(c, PP_SYNC_THRESH, cfg);
  168. }
  169. static int sde_hw_pp_setup_autorefresh_config(struct sde_hw_pingpong *pp,
  170. struct sde_hw_autorefresh *cfg)
  171. {
  172. struct sde_hw_blk_reg_map *c;
  173. u32 refresh_cfg;
  174. if (!pp || !cfg)
  175. return -EINVAL;
  176. c = &pp->hw;
  177. if (cfg->enable)
  178. refresh_cfg = BIT(31) | cfg->frame_count;
  179. else
  180. refresh_cfg = 0;
  181. SDE_REG_WRITE(c, PP_AUTOREFRESH_CONFIG, refresh_cfg);
  182. SDE_EVT32(pp->idx - PINGPONG_0, refresh_cfg);
  183. return 0;
  184. }
  185. static int sde_hw_pp_get_autorefresh_config(struct sde_hw_pingpong *pp,
  186. struct sde_hw_autorefresh *cfg)
  187. {
  188. struct sde_hw_blk_reg_map *c;
  189. u32 val;
  190. if (!pp || !cfg)
  191. return -EINVAL;
  192. c = &pp->hw;
  193. val = SDE_REG_READ(c, PP_AUTOREFRESH_CONFIG);
  194. cfg->enable = (val & BIT(31)) >> 31;
  195. cfg->frame_count = val & 0xffff;
  196. return 0;
  197. }
  198. static int sde_hw_pp_poll_timeout_wr_ptr(struct sde_hw_pingpong *pp,
  199. u32 timeout_us)
  200. {
  201. struct sde_hw_blk_reg_map *c;
  202. u32 val;
  203. int rc;
  204. if (!pp)
  205. return -EINVAL;
  206. c = &pp->hw;
  207. rc = readl_poll_timeout(c->base_off + c->blk_off + PP_LINE_COUNT,
  208. val, (val & 0xffff) >= 1, 10, timeout_us);
  209. return rc;
  210. }
  211. static void sde_hw_pp_dsc_enable(struct sde_hw_pingpong *pp)
  212. {
  213. struct sde_hw_blk_reg_map *c;
  214. if (!pp)
  215. return;
  216. c = &pp->hw;
  217. SDE_REG_WRITE(c, PP_DSC_MODE, 1);
  218. }
  219. static u32 sde_hw_pp_get_dsc_status(struct sde_hw_pingpong *pp)
  220. {
  221. struct sde_hw_blk_reg_map *c;
  222. if (!pp)
  223. return 0;
  224. c = &pp->hw;
  225. return SDE_REG_READ(c, PP_DSC_MODE);
  226. }
  227. static void sde_hw_pp_dsc_disable(struct sde_hw_pingpong *pp)
  228. {
  229. struct sde_hw_blk_reg_map *c;
  230. u32 data;
  231. if (!pp)
  232. return;
  233. c = &pp->hw;
  234. data = SDE_REG_READ(c, PP_DCE_DATA_OUT_SWAP);
  235. data &= ~BIT(18); /* disable endian flip */
  236. SDE_REG_WRITE(c, PP_DCE_DATA_OUT_SWAP, data);
  237. SDE_REG_WRITE(c, PP_DSC_MODE, 0);
  238. }
  239. static int sde_hw_pp_setup_dsc(struct sde_hw_pingpong *pp)
  240. {
  241. struct sde_hw_blk_reg_map *c;
  242. int data;
  243. if (!pp)
  244. return -EINVAL;
  245. c = &pp->hw;
  246. data = SDE_REG_READ(c, PP_DCE_DATA_OUT_SWAP);
  247. data |= BIT(18); /* endian flip */
  248. SDE_REG_WRITE(c, PP_DCE_DATA_OUT_SWAP, data);
  249. return 0;
  250. }
  251. static int sde_hw_pp_setup_dither_v1(struct sde_hw_pingpong *pp,
  252. void *cfg, size_t len)
  253. {
  254. struct sde_hw_blk_reg_map *c;
  255. struct drm_msm_dither *dither = (struct drm_msm_dither *)cfg;
  256. u32 base = 0, offset = 0, data = 0, i = 0;
  257. if (!pp)
  258. return -EINVAL;
  259. c = &pp->hw;
  260. base = pp->caps->sblk->dither.base;
  261. if (!dither) {
  262. /* dither property disable case */
  263. SDE_REG_WRITE(c, base, 0);
  264. return 0;
  265. }
  266. if (len != sizeof(struct drm_msm_dither)) {
  267. DRM_ERROR("input len %zu, expected len %zu\n", len,
  268. sizeof(struct drm_msm_dither));
  269. return -EINVAL;
  270. }
  271. if (dither->c0_bitdepth >= DITHER_DEPTH_MAP_INDEX ||
  272. dither->c1_bitdepth >= DITHER_DEPTH_MAP_INDEX ||
  273. dither->c2_bitdepth >= DITHER_DEPTH_MAP_INDEX ||
  274. dither->c3_bitdepth >= DITHER_DEPTH_MAP_INDEX)
  275. return -EINVAL;
  276. offset += 4;
  277. data = dither_depth_map[dither->c0_bitdepth] & REG_MASK(2);
  278. data |= (dither_depth_map[dither->c1_bitdepth] & REG_MASK(2)) << 2;
  279. data |= (dither_depth_map[dither->c2_bitdepth] & REG_MASK(2)) << 4;
  280. data |= (dither_depth_map[dither->c3_bitdepth] & REG_MASK(2)) << 6;
  281. data |= (dither->temporal_en) ? (1 << 8) : 0;
  282. SDE_REG_WRITE(c, base + offset, data);
  283. for (i = 0; i < DITHER_MATRIX_SZ - 3; i += 4) {
  284. offset += 4;
  285. data = (dither->matrix[i] & REG_MASK(4)) |
  286. ((dither->matrix[i + 1] & REG_MASK(4)) << 4) |
  287. ((dither->matrix[i + 2] & REG_MASK(4)) << 8) |
  288. ((dither->matrix[i + 3] & REG_MASK(4)) << 12);
  289. SDE_REG_WRITE(c, base + offset, data);
  290. }
  291. SDE_REG_WRITE(c, base, 1);
  292. return 0;
  293. }
  294. static int sde_hw_pp_enable_te(struct sde_hw_pingpong *pp, bool enable)
  295. {
  296. struct sde_hw_blk_reg_map *c;
  297. if (!pp)
  298. return -EINVAL;
  299. c = &pp->hw;
  300. SDE_REG_WRITE(c, PP_TEAR_CHECK_EN, enable);
  301. return 0;
  302. }
  303. static int sde_hw_pp_connect_external_te(struct sde_hw_pingpong *pp,
  304. bool enable_external_te)
  305. {
  306. struct sde_hw_blk_reg_map *c = &pp->hw;
  307. u32 cfg;
  308. int orig;
  309. if (!pp)
  310. return -EINVAL;
  311. c = &pp->hw;
  312. cfg = SDE_REG_READ(c, PP_SYNC_CONFIG_VSYNC);
  313. orig = (bool)(cfg & BIT(20));
  314. if (enable_external_te)
  315. cfg |= BIT(20);
  316. else
  317. cfg &= ~BIT(20);
  318. SDE_REG_WRITE(c, PP_SYNC_CONFIG_VSYNC, cfg);
  319. SDE_EVT32(pp->idx - PINGPONG_0, cfg);
  320. return orig;
  321. }
  322. static int sde_hw_pp_get_vsync_info(struct sde_hw_pingpong *pp,
  323. struct sde_hw_pp_vsync_info *info)
  324. {
  325. struct sde_hw_blk_reg_map *c;
  326. u32 val;
  327. if (!pp || !info)
  328. return -EINVAL;
  329. c = &pp->hw;
  330. val = SDE_REG_READ(c, PP_VSYNC_INIT_VAL);
  331. info->rd_ptr_init_val = val & 0xffff;
  332. val = SDE_REG_READ(c, PP_INT_COUNT_VAL);
  333. info->rd_ptr_frame_count = (val & 0xffff0000) >> 16;
  334. info->rd_ptr_line_count = val & 0xffff;
  335. val = SDE_REG_READ(c, PP_LINE_COUNT);
  336. info->wr_ptr_line_count = val & 0xffff;
  337. return 0;
  338. }
  339. static u32 sde_hw_pp_get_line_count(struct sde_hw_pingpong *pp)
  340. {
  341. struct sde_hw_blk_reg_map *c = &pp->hw;
  342. u32 height, init;
  343. u32 line = 0xFFFF;
  344. if (!pp)
  345. return 0;
  346. c = &pp->hw;
  347. init = SDE_REG_READ(c, PP_VSYNC_INIT_VAL) & 0xFFFF;
  348. height = SDE_REG_READ(c, PP_SYNC_CONFIG_HEIGHT) & 0xFFFF;
  349. if (height < init)
  350. goto line_count_exit;
  351. line = SDE_REG_READ(c, PP_INT_COUNT_VAL) & 0xFFFF;
  352. if (line < init)
  353. line += (0xFFFF - init);
  354. else
  355. line -= init;
  356. line_count_exit:
  357. return line;
  358. }
  359. static void sde_hw_pp_setup_3d_merge_mode(struct sde_hw_pingpong *pp,
  360. enum sde_3d_blend_mode cfg)
  361. {
  362. if (pp->merge_3d && pp->merge_3d->ops.setup_blend_mode)
  363. pp->merge_3d->ops.setup_blend_mode(pp->merge_3d, cfg);
  364. }
  365. static void sde_hw_pp_reset_3d_merge_mode(struct sde_hw_pingpong *pp)
  366. {
  367. if (pp->merge_3d && pp->merge_3d->ops.reset_blend_mode)
  368. pp->merge_3d->ops.reset_blend_mode(pp->merge_3d);
  369. }
  370. static void _setup_pingpong_ops(struct sde_hw_pingpong_ops *ops,
  371. const struct sde_pingpong_cfg *hw_cap)
  372. {
  373. u32 version = 0;
  374. if (hw_cap->features & BIT(SDE_PINGPONG_TE)) {
  375. ops->setup_tearcheck = sde_hw_pp_setup_te_config;
  376. ops->enable_tearcheck = sde_hw_pp_enable_te;
  377. ops->update_tearcheck = sde_hw_pp_update_te;
  378. ops->connect_external_te = sde_hw_pp_connect_external_te;
  379. ops->get_vsync_info = sde_hw_pp_get_vsync_info;
  380. ops->setup_autorefresh = sde_hw_pp_setup_autorefresh_config;
  381. ops->get_autorefresh = sde_hw_pp_get_autorefresh_config;
  382. ops->poll_timeout_wr_ptr = sde_hw_pp_poll_timeout_wr_ptr;
  383. ops->get_line_count = sde_hw_pp_get_line_count;
  384. }
  385. ops->setup_dsc = sde_hw_pp_setup_dsc;
  386. ops->enable_dsc = sde_hw_pp_dsc_enable;
  387. ops->disable_dsc = sde_hw_pp_dsc_disable;
  388. ops->get_dsc_status = sde_hw_pp_get_dsc_status;
  389. version = SDE_COLOR_PROCESS_MAJOR(hw_cap->sblk->dither.version);
  390. switch (version) {
  391. case 1:
  392. ops->setup_dither = sde_hw_pp_setup_dither_v1;
  393. break;
  394. default:
  395. ops->setup_dither = NULL;
  396. break;
  397. }
  398. if (test_bit(SDE_PINGPONG_MERGE_3D, &hw_cap->features)) {
  399. ops->setup_3d_mode = sde_hw_pp_setup_3d_merge_mode;
  400. ops->reset_3d_mode = sde_hw_pp_reset_3d_merge_mode;
  401. }
  402. };
  403. static struct sde_hw_blk_ops sde_hw_ops = {
  404. .start = NULL,
  405. .stop = NULL,
  406. };
  407. struct sde_hw_pingpong *sde_hw_pingpong_init(enum sde_pingpong idx,
  408. void __iomem *addr,
  409. struct sde_mdss_cfg *m)
  410. {
  411. struct sde_hw_pingpong *c;
  412. struct sde_pingpong_cfg *cfg;
  413. int rc;
  414. c = kzalloc(sizeof(*c), GFP_KERNEL);
  415. if (!c)
  416. return ERR_PTR(-ENOMEM);
  417. cfg = _pingpong_offset(idx, m, addr, &c->hw);
  418. if (IS_ERR_OR_NULL(cfg)) {
  419. kfree(c);
  420. return ERR_PTR(-EINVAL);
  421. }
  422. c->idx = idx;
  423. c->caps = cfg;
  424. if (test_bit(SDE_PINGPONG_MERGE_3D, &cfg->features)) {
  425. c->merge_3d = _sde_pp_merge_3d_init(cfg->merge_3d_id, addr, m);
  426. if (IS_ERR(c->merge_3d)) {
  427. SDE_ERROR("invalid merge_3d block %d\n", idx);
  428. return ERR_PTR(-ENOMEM);
  429. }
  430. }
  431. _setup_pingpong_ops(&c->ops, c->caps);
  432. rc = sde_hw_blk_init(&c->base, SDE_HW_BLK_PINGPONG, idx, &sde_hw_ops);
  433. if (rc) {
  434. SDE_ERROR("failed to init hw blk %d\n", rc);
  435. goto blk_init_error;
  436. }
  437. sde_dbg_reg_register_dump_range(SDE_DBG_NAME, cfg->name, c->hw.blk_off,
  438. c->hw.blk_off + c->hw.length, c->hw.xin_id);
  439. if (cfg->sblk->dither.base && cfg->sblk->dither.len) {
  440. sde_dbg_reg_register_dump_range(SDE_DBG_NAME,
  441. cfg->sblk->dither.name,
  442. c->hw.blk_off + cfg->sblk->dither.base,
  443. c->hw.blk_off + cfg->sblk->dither.base +
  444. cfg->sblk->dither.len,
  445. c->hw.xin_id);
  446. }
  447. return c;
  448. blk_init_error:
  449. kzfree(c);
  450. return ERR_PTR(rc);
  451. }
  452. void sde_hw_pingpong_destroy(struct sde_hw_pingpong *pp)
  453. {
  454. if (pp) {
  455. sde_hw_blk_destroy(&pp->base);
  456. kfree(pp->merge_3d);
  457. kfree(pp);
  458. }
  459. }