sde_hw_pingpong.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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_START_POS, te->start_pos);
  148. SDE_REG_WRITE(c, PP_SYNC_THRESH,
  149. ((te->sync_threshold_continue << 16) |
  150. te->sync_threshold_start));
  151. SDE_REG_WRITE(c, PP_SYNC_WRCOUNT,
  152. (te->start_pos + te->sync_threshold_start + 1));
  153. return 0;
  154. }
  155. static void sde_hw_pp_update_te(struct sde_hw_pingpong *pp,
  156. struct sde_hw_tear_check *te)
  157. {
  158. struct sde_hw_blk_reg_map *c;
  159. int cfg;
  160. if (!pp || !te)
  161. return;
  162. c = &pp->hw;
  163. cfg = SDE_REG_READ(c, PP_SYNC_THRESH);
  164. cfg &= ~0xFFFF;
  165. cfg |= te->sync_threshold_start;
  166. SDE_REG_WRITE(c, PP_SYNC_THRESH, cfg);
  167. }
  168. static int sde_hw_pp_setup_autorefresh_config(struct sde_hw_pingpong *pp,
  169. struct sde_hw_autorefresh *cfg)
  170. {
  171. struct sde_hw_blk_reg_map *c;
  172. u32 refresh_cfg;
  173. if (!pp || !cfg)
  174. return -EINVAL;
  175. c = &pp->hw;
  176. if (cfg->enable)
  177. refresh_cfg = BIT(31) | cfg->frame_count;
  178. else
  179. refresh_cfg = 0;
  180. SDE_REG_WRITE(c, PP_AUTOREFRESH_CONFIG, refresh_cfg);
  181. SDE_EVT32(pp->idx - PINGPONG_0, refresh_cfg);
  182. return 0;
  183. }
  184. static int sde_hw_pp_get_autorefresh_config(struct sde_hw_pingpong *pp,
  185. struct sde_hw_autorefresh *cfg)
  186. {
  187. struct sde_hw_blk_reg_map *c;
  188. u32 val;
  189. if (!pp || !cfg)
  190. return -EINVAL;
  191. c = &pp->hw;
  192. val = SDE_REG_READ(c, PP_AUTOREFRESH_CONFIG);
  193. cfg->enable = (val & BIT(31)) >> 31;
  194. cfg->frame_count = val & 0xffff;
  195. return 0;
  196. }
  197. static int sde_hw_pp_poll_timeout_wr_ptr(struct sde_hw_pingpong *pp,
  198. u32 timeout_us)
  199. {
  200. struct sde_hw_blk_reg_map *c;
  201. u32 val;
  202. int rc;
  203. if (!pp)
  204. return -EINVAL;
  205. c = &pp->hw;
  206. rc = readl_poll_timeout(c->base_off + c->blk_off + PP_LINE_COUNT,
  207. val, (val & 0xffff) >= 1, 10, timeout_us);
  208. return rc;
  209. }
  210. static void sde_hw_pp_dsc_enable(struct sde_hw_pingpong *pp)
  211. {
  212. struct sde_hw_blk_reg_map *c;
  213. if (!pp)
  214. return;
  215. c = &pp->hw;
  216. SDE_REG_WRITE(c, PP_DSC_MODE, 1);
  217. }
  218. static u32 sde_hw_pp_get_dsc_status(struct sde_hw_pingpong *pp)
  219. {
  220. struct sde_hw_blk_reg_map *c;
  221. if (!pp)
  222. return 0;
  223. c = &pp->hw;
  224. return SDE_REG_READ(c, PP_DSC_MODE);
  225. }
  226. static void sde_hw_pp_dsc_disable(struct sde_hw_pingpong *pp)
  227. {
  228. struct sde_hw_blk_reg_map *c;
  229. u32 data;
  230. if (!pp)
  231. return;
  232. c = &pp->hw;
  233. data = SDE_REG_READ(c, PP_DCE_DATA_OUT_SWAP);
  234. data &= ~BIT(18); /* disable endian flip */
  235. SDE_REG_WRITE(c, PP_DCE_DATA_OUT_SWAP, data);
  236. SDE_REG_WRITE(c, PP_DSC_MODE, 0);
  237. }
  238. static int sde_hw_pp_setup_dsc(struct sde_hw_pingpong *pp)
  239. {
  240. struct sde_hw_blk_reg_map *c;
  241. int data;
  242. if (!pp)
  243. return -EINVAL;
  244. c = &pp->hw;
  245. data = SDE_REG_READ(c, PP_DCE_DATA_OUT_SWAP);
  246. data |= BIT(18); /* endian flip */
  247. SDE_REG_WRITE(c, PP_DCE_DATA_OUT_SWAP, data);
  248. return 0;
  249. }
  250. static int sde_hw_pp_setup_dither_v1(struct sde_hw_pingpong *pp,
  251. void *cfg, size_t len)
  252. {
  253. struct sde_hw_blk_reg_map *c;
  254. struct drm_msm_dither *dither = (struct drm_msm_dither *)cfg;
  255. u32 base = 0, offset = 0, data = 0, i = 0;
  256. if (!pp)
  257. return -EINVAL;
  258. c = &pp->hw;
  259. base = pp->caps->sblk->dither.base;
  260. if (!dither) {
  261. /* dither property disable case */
  262. SDE_REG_WRITE(c, base, 0);
  263. return 0;
  264. }
  265. if (len != sizeof(struct drm_msm_dither)) {
  266. DRM_ERROR("input len %zu, expected len %zu\n", len,
  267. sizeof(struct drm_msm_dither));
  268. return -EINVAL;
  269. }
  270. if (dither->c0_bitdepth >= DITHER_DEPTH_MAP_INDEX ||
  271. dither->c1_bitdepth >= DITHER_DEPTH_MAP_INDEX ||
  272. dither->c2_bitdepth >= DITHER_DEPTH_MAP_INDEX ||
  273. dither->c3_bitdepth >= DITHER_DEPTH_MAP_INDEX)
  274. return -EINVAL;
  275. offset += 4;
  276. data = dither_depth_map[dither->c0_bitdepth] & REG_MASK(2);
  277. data |= (dither_depth_map[dither->c1_bitdepth] & REG_MASK(2)) << 2;
  278. data |= (dither_depth_map[dither->c2_bitdepth] & REG_MASK(2)) << 4;
  279. data |= (dither_depth_map[dither->c3_bitdepth] & REG_MASK(2)) << 6;
  280. data |= (dither->temporal_en) ? (1 << 8) : 0;
  281. SDE_REG_WRITE(c, base + offset, data);
  282. for (i = 0; i < DITHER_MATRIX_SZ - 3; i += 4) {
  283. offset += 4;
  284. data = (dither->matrix[i] & REG_MASK(4)) |
  285. ((dither->matrix[i + 1] & REG_MASK(4)) << 4) |
  286. ((dither->matrix[i + 2] & REG_MASK(4)) << 8) |
  287. ((dither->matrix[i + 3] & REG_MASK(4)) << 12);
  288. SDE_REG_WRITE(c, base + offset, data);
  289. }
  290. SDE_REG_WRITE(c, base, 1);
  291. return 0;
  292. }
  293. static int sde_hw_pp_enable_te(struct sde_hw_pingpong *pp, bool enable)
  294. {
  295. struct sde_hw_blk_reg_map *c;
  296. if (!pp)
  297. return -EINVAL;
  298. c = &pp->hw;
  299. SDE_REG_WRITE(c, PP_TEAR_CHECK_EN, enable);
  300. return 0;
  301. }
  302. static int sde_hw_pp_connect_external_te(struct sde_hw_pingpong *pp,
  303. bool enable_external_te)
  304. {
  305. struct sde_hw_blk_reg_map *c = &pp->hw;
  306. u32 cfg;
  307. int orig;
  308. if (!pp)
  309. return -EINVAL;
  310. c = &pp->hw;
  311. cfg = SDE_REG_READ(c, PP_SYNC_CONFIG_VSYNC);
  312. orig = (bool)(cfg & BIT(20));
  313. if (enable_external_te)
  314. cfg |= BIT(20);
  315. else
  316. cfg &= ~BIT(20);
  317. SDE_REG_WRITE(c, PP_SYNC_CONFIG_VSYNC, cfg);
  318. SDE_EVT32(pp->idx - PINGPONG_0, cfg);
  319. return orig;
  320. }
  321. static int sde_hw_pp_get_vsync_info(struct sde_hw_pingpong *pp,
  322. struct sde_hw_pp_vsync_info *info)
  323. {
  324. struct sde_hw_blk_reg_map *c;
  325. u32 val;
  326. if (!pp || !info)
  327. return -EINVAL;
  328. c = &pp->hw;
  329. val = SDE_REG_READ(c, PP_VSYNC_INIT_VAL);
  330. info->rd_ptr_init_val = val & 0xffff;
  331. val = SDE_REG_READ(c, PP_INT_COUNT_VAL);
  332. info->rd_ptr_frame_count = (val & 0xffff0000) >> 16;
  333. info->rd_ptr_line_count = val & 0xffff;
  334. val = SDE_REG_READ(c, PP_LINE_COUNT);
  335. info->wr_ptr_line_count = val & 0xffff;
  336. return 0;
  337. }
  338. static u32 sde_hw_pp_get_line_count(struct sde_hw_pingpong *pp)
  339. {
  340. struct sde_hw_blk_reg_map *c = &pp->hw;
  341. u32 height, init;
  342. u32 line = 0xFFFF;
  343. if (!pp)
  344. return 0;
  345. c = &pp->hw;
  346. init = SDE_REG_READ(c, PP_VSYNC_INIT_VAL) & 0xFFFF;
  347. height = SDE_REG_READ(c, PP_SYNC_CONFIG_HEIGHT) & 0xFFFF;
  348. if (height < init)
  349. goto line_count_exit;
  350. line = SDE_REG_READ(c, PP_INT_COUNT_VAL) & 0xFFFF;
  351. if (line < init)
  352. line += (0xFFFF - init);
  353. else
  354. line -= init;
  355. line_count_exit:
  356. return line;
  357. }
  358. static void sde_hw_pp_setup_3d_merge_mode(struct sde_hw_pingpong *pp,
  359. enum sde_3d_blend_mode cfg)
  360. {
  361. if (pp->merge_3d && pp->merge_3d->ops.setup_blend_mode)
  362. pp->merge_3d->ops.setup_blend_mode(pp->merge_3d, cfg);
  363. }
  364. static void sde_hw_pp_reset_3d_merge_mode(struct sde_hw_pingpong *pp)
  365. {
  366. if (pp->merge_3d && pp->merge_3d->ops.reset_blend_mode)
  367. pp->merge_3d->ops.reset_blend_mode(pp->merge_3d);
  368. }
  369. static void _setup_pingpong_ops(struct sde_hw_pingpong_ops *ops,
  370. const struct sde_pingpong_cfg *hw_cap)
  371. {
  372. u32 version = 0;
  373. if (hw_cap->features & BIT(SDE_PINGPONG_TE)) {
  374. ops->setup_tearcheck = sde_hw_pp_setup_te_config;
  375. ops->enable_tearcheck = sde_hw_pp_enable_te;
  376. ops->update_tearcheck = sde_hw_pp_update_te;
  377. ops->connect_external_te = sde_hw_pp_connect_external_te;
  378. ops->get_vsync_info = sde_hw_pp_get_vsync_info;
  379. ops->setup_autorefresh = sde_hw_pp_setup_autorefresh_config;
  380. ops->get_autorefresh = sde_hw_pp_get_autorefresh_config;
  381. ops->poll_timeout_wr_ptr = sde_hw_pp_poll_timeout_wr_ptr;
  382. ops->get_line_count = sde_hw_pp_get_line_count;
  383. }
  384. ops->setup_dsc = sde_hw_pp_setup_dsc;
  385. ops->enable_dsc = sde_hw_pp_dsc_enable;
  386. ops->disable_dsc = sde_hw_pp_dsc_disable;
  387. ops->get_dsc_status = sde_hw_pp_get_dsc_status;
  388. version = SDE_COLOR_PROCESS_MAJOR(hw_cap->sblk->dither.version);
  389. switch (version) {
  390. case 1:
  391. ops->setup_dither = sde_hw_pp_setup_dither_v1;
  392. break;
  393. default:
  394. ops->setup_dither = NULL;
  395. break;
  396. }
  397. if (test_bit(SDE_PINGPONG_MERGE_3D, &hw_cap->features)) {
  398. ops->setup_3d_mode = sde_hw_pp_setup_3d_merge_mode;
  399. ops->reset_3d_mode = sde_hw_pp_reset_3d_merge_mode;
  400. }
  401. };
  402. static struct sde_hw_blk_ops sde_hw_ops = {
  403. .start = NULL,
  404. .stop = NULL,
  405. };
  406. struct sde_hw_pingpong *sde_hw_pingpong_init(enum sde_pingpong idx,
  407. void __iomem *addr,
  408. struct sde_mdss_cfg *m)
  409. {
  410. struct sde_hw_pingpong *c;
  411. struct sde_pingpong_cfg *cfg;
  412. int rc;
  413. c = kzalloc(sizeof(*c), GFP_KERNEL);
  414. if (!c)
  415. return ERR_PTR(-ENOMEM);
  416. cfg = _pingpong_offset(idx, m, addr, &c->hw);
  417. if (IS_ERR_OR_NULL(cfg)) {
  418. kfree(c);
  419. return ERR_PTR(-EINVAL);
  420. }
  421. c->idx = idx;
  422. c->caps = cfg;
  423. if (test_bit(SDE_PINGPONG_MERGE_3D, &cfg->features)) {
  424. c->merge_3d = _sde_pp_merge_3d_init(cfg->merge_3d_id, addr, m);
  425. if (IS_ERR(c->merge_3d)) {
  426. SDE_ERROR("invalid merge_3d block %d\n", idx);
  427. return ERR_PTR(-ENOMEM);
  428. }
  429. }
  430. _setup_pingpong_ops(&c->ops, c->caps);
  431. rc = sde_hw_blk_init(&c->base, SDE_HW_BLK_PINGPONG, idx, &sde_hw_ops);
  432. if (rc) {
  433. SDE_ERROR("failed to init hw blk %d\n", rc);
  434. goto blk_init_error;
  435. }
  436. sde_dbg_reg_register_dump_range(SDE_DBG_NAME, cfg->name, c->hw.blk_off,
  437. c->hw.blk_off + c->hw.length, c->hw.xin_id);
  438. if (cfg->sblk->dither.base && cfg->sblk->dither.len) {
  439. sde_dbg_reg_register_dump_range(SDE_DBG_NAME,
  440. cfg->sblk->dither.name,
  441. c->hw.blk_off + cfg->sblk->dither.base,
  442. c->hw.blk_off + cfg->sblk->dither.base +
  443. cfg->sblk->dither.len,
  444. c->hw.xin_id);
  445. }
  446. return c;
  447. blk_init_error:
  448. kzfree(c);
  449. return ERR_PTR(rc);
  450. }
  451. void sde_hw_pingpong_destroy(struct sde_hw_pingpong *pp)
  452. {
  453. if (pp) {
  454. sde_hw_blk_destroy(&pp->base);
  455. kfree(pp->merge_3d);
  456. kfree(pp);
  457. }
  458. }