sde_hw_pingpong.c 13 KB

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