sde_encoder_dce.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2020 The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kthread.h>
  6. #include <linux/debugfs.h>
  7. #include <linux/seq_file.h>
  8. #include <linux/sde_rsc.h>
  9. #include "msm_drv.h"
  10. #include "sde_kms.h"
  11. #include <drm/drm_crtc.h>
  12. #include <drm/drm_crtc_helper.h>
  13. #include "sde_hwio.h"
  14. #include "sde_hw_catalog.h"
  15. #include "sde_hw_intf.h"
  16. #include "sde_hw_ctl.h"
  17. #include "sde_formats.h"
  18. #include "sde_encoder_phys.h"
  19. #include "sde_power_handle.h"
  20. #include "sde_hw_dsc.h"
  21. #include "sde_crtc.h"
  22. #include "sde_trace.h"
  23. #include "sde_core_irq.h"
  24. #define SDE_DEBUG_DCE(e, fmt, ...) SDE_DEBUG("enc%d " fmt,\
  25. (e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
  26. #define SDE_ERROR_DCE(e, fmt, ...) SDE_ERROR("enc%d " fmt,\
  27. (e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
  28. bool sde_encoder_is_dsc_merge(struct drm_encoder *drm_enc)
  29. {
  30. enum sde_rm_topology_name topology;
  31. struct sde_encoder_virt *sde_enc;
  32. struct drm_connector *drm_conn;
  33. if (!drm_enc)
  34. return false;
  35. sde_enc = to_sde_encoder_virt(drm_enc);
  36. if (!sde_enc->cur_master)
  37. return false;
  38. drm_conn = sde_enc->cur_master->connector;
  39. if (!drm_conn)
  40. return false;
  41. topology = sde_connector_get_topology_name(drm_conn);
  42. if (topology == SDE_RM_TOPOLOGY_DUALPIPE_DSCMERGE)
  43. return true;
  44. return false;
  45. }
  46. static int _dce_dsc_update_pic_dim(struct msm_display_dsc_info *dsc,
  47. int pic_width, int pic_height)
  48. {
  49. if (!dsc || !pic_width || !pic_height) {
  50. SDE_ERROR("invalid input: pic_width=%d pic_height=%d\n",
  51. pic_width, pic_height);
  52. return -EINVAL;
  53. }
  54. if ((pic_width % dsc->slice_width) ||
  55. (pic_height % dsc->slice_height)) {
  56. SDE_ERROR("pic_dim=%dx%d has to be multiple of slice=%dx%d\n",
  57. pic_width, pic_height,
  58. dsc->slice_width, dsc->slice_height);
  59. return -EINVAL;
  60. }
  61. dsc->pic_width = pic_width;
  62. dsc->pic_height = pic_height;
  63. return 0;
  64. }
  65. static void _dce_dsc_pclk_param_calc(struct msm_display_dsc_info *dsc,
  66. int intf_width)
  67. {
  68. int slice_per_pkt, slice_per_intf;
  69. int bytes_in_slice, total_bytes_per_intf;
  70. if (!dsc || !dsc->slice_width || !dsc->slice_per_pkt ||
  71. (intf_width < dsc->slice_width)) {
  72. SDE_ERROR("invalid input: intf_width=%d slice_width=%d\n",
  73. intf_width, dsc ? dsc->slice_width : -1);
  74. return;
  75. }
  76. slice_per_pkt = dsc->slice_per_pkt;
  77. slice_per_intf = DIV_ROUND_UP(intf_width, dsc->slice_width);
  78. /*
  79. * If slice_per_pkt is greater than slice_per_intf then default to 1.
  80. * This can happen during partial update.
  81. */
  82. if (slice_per_pkt > slice_per_intf)
  83. slice_per_pkt = 1;
  84. bytes_in_slice = DIV_ROUND_UP(dsc->slice_width * dsc->bpp, 8);
  85. total_bytes_per_intf = bytes_in_slice * slice_per_intf;
  86. dsc->eol_byte_num = total_bytes_per_intf % 3;
  87. dsc->pclk_per_line = DIV_ROUND_UP(total_bytes_per_intf, 3);
  88. dsc->bytes_in_slice = bytes_in_slice;
  89. dsc->bytes_per_pkt = bytes_in_slice * slice_per_pkt;
  90. dsc->pkt_per_line = slice_per_intf / slice_per_pkt;
  91. }
  92. static int _dce_dsc_initial_line_calc(struct msm_display_dsc_info *dsc,
  93. int enc_ip_width,
  94. int dsc_cmn_mode)
  95. {
  96. int max_ssm_delay, max_se_size, max_muxword_size;
  97. int compress_bpp_group, obuf_latency, input_ssm_out_latency;
  98. int base_hs_latency, chunk_bits, ob_data_width;
  99. int output_rate_extra_budget_bits, multi_hs_extra_budget_bits;
  100. int multi_hs_extra_latency, mux_word_size;
  101. int ob_data_width_4comps, ob_data_width_3comps;
  102. int output_rate_ratio_complement, container_slice_width;
  103. int rtl_num_components, multi_hs_c, multi_hs_d;
  104. /* Hardent core config */
  105. int multiplex_mode_enable = 0, split_panel_enable = 0;
  106. int rtl_max_bpc = 10, rtl_output_data_width = 64;
  107. int pipeline_latency = 28;
  108. int bpc = dsc->bpc, bpp = dsc->bpp;
  109. int num_of_active_ss = dsc->full_frame_slices;
  110. bool native_422 = false, native_420 = false;
  111. if (dsc_cmn_mode & DSC_MODE_MULTIPLEX)
  112. multiplex_mode_enable = 1;
  113. if (dsc_cmn_mode & DSC_MODE_SPLIT_PANEL)
  114. split_panel_enable = 0;
  115. container_slice_width = (native_422 ?
  116. dsc->slice_width / 2 : dsc->slice_width);
  117. max_muxword_size = ((rtl_max_bpc >= 12) ? 64 : 48);
  118. max_se_size = 4 * (rtl_max_bpc + 1);
  119. max_ssm_delay = max_se_size + max_muxword_size - 1;
  120. mux_word_size = (bpc >= 12 ? 64 : 48);
  121. compress_bpp_group = (native_422 ? 2 * bpp : bpp);
  122. input_ssm_out_latency = pipeline_latency + (3 * (max_ssm_delay + 2)
  123. * num_of_active_ss);
  124. rtl_num_components = (native_420 | native_422 ? 4 : 3);
  125. ob_data_width_4comps = ((rtl_output_data_width >= (2 *
  126. max_muxword_size)) ?
  127. rtl_output_data_width :
  128. (2 * rtl_output_data_width));
  129. ob_data_width_3comps = (rtl_output_data_width >= max_muxword_size ?
  130. rtl_output_data_width : 2 * rtl_output_data_width);
  131. ob_data_width = (rtl_num_components == 4 ?
  132. ob_data_width_4comps : ob_data_width_3comps);
  133. obuf_latency = DIV_ROUND_UP((9 * ob_data_width + mux_word_size),
  134. compress_bpp_group) + 1;
  135. base_hs_latency = dsc->initial_xmit_delay + input_ssm_out_latency
  136. + obuf_latency;
  137. chunk_bits = 8 * dsc->chunk_size;
  138. output_rate_ratio_complement = ob_data_width - compress_bpp_group;
  139. output_rate_extra_budget_bits =
  140. (output_rate_ratio_complement * chunk_bits) >>
  141. (ob_data_width == 128 ? 7 : 6);
  142. multi_hs_c = split_panel_enable * multiplex_mode_enable;
  143. multi_hs_d = (num_of_active_ss > 1) * (ob_data_width >
  144. compress_bpp_group);
  145. multi_hs_extra_budget_bits = (multi_hs_c ?
  146. chunk_bits : (multi_hs_d ? chunk_bits :
  147. output_rate_extra_budget_bits));
  148. multi_hs_extra_latency = DIV_ROUND_UP(multi_hs_extra_budget_bits,
  149. compress_bpp_group);
  150. dsc->initial_lines = DIV_ROUND_UP((base_hs_latency +
  151. multi_hs_extra_latency),
  152. container_slice_width);
  153. return 0;
  154. }
  155. static bool _dce_dsc_ich_reset_override_needed(bool pu_en,
  156. struct msm_display_dsc_info *dsc)
  157. {
  158. /*
  159. * As per the DSC spec, ICH_RESET can be either end of the slice line
  160. * or at the end of the slice. HW internally generates ich_reset at
  161. * end of the slice line if DSC_MERGE is used or encoder has two
  162. * soft slices. However, if encoder has only 1 soft slice and DSC_MERGE
  163. * is not used then it will generate ich_reset at the end of slice.
  164. *
  165. * Now as per the spec, during one PPS session, position where
  166. * ich_reset is generated should not change. Now if full-screen frame
  167. * has more than 1 soft slice then HW will automatically generate
  168. * ich_reset at the end of slice_line. But for the same panel, if
  169. * partial frame is enabled and only 1 encoder is used with 1 slice,
  170. * then HW will generate ich_reset at end of the slice. This is a
  171. * mismatch. Prevent this by overriding HW's decision.
  172. */
  173. return pu_en && dsc && (dsc->full_frame_slices > 1) &&
  174. (dsc->slice_width == dsc->pic_width);
  175. }
  176. static void _dce_dsc_pipe_cfg(struct sde_hw_dsc *hw_dsc,
  177. struct sde_hw_pingpong *hw_pp, struct msm_display_dsc_info *dsc,
  178. u32 common_mode, bool ich_reset, bool enable,
  179. struct sde_hw_pingpong *hw_dsc_pp)
  180. {
  181. if (!enable) {
  182. if (hw_dsc_pp && hw_dsc_pp->ops.disable_dsc)
  183. hw_dsc_pp->ops.disable_dsc(hw_dsc_pp);
  184. if (hw_dsc && hw_dsc->ops.dsc_disable)
  185. hw_dsc->ops.dsc_disable(hw_dsc);
  186. if (hw_dsc && hw_dsc->ops.bind_pingpong_blk)
  187. hw_dsc->ops.bind_pingpong_blk(hw_dsc, false,
  188. PINGPONG_MAX);
  189. return;
  190. }
  191. if (!dsc || !hw_dsc || !hw_pp || !hw_dsc_pp) {
  192. SDE_ERROR("invalid params %d %d %d %d\n", !dsc, !hw_dsc,
  193. !hw_pp, !hw_dsc_pp);
  194. return;
  195. }
  196. if (hw_dsc->ops.dsc_config)
  197. hw_dsc->ops.dsc_config(hw_dsc, dsc, common_mode, ich_reset);
  198. if (hw_dsc->ops.dsc_config_thresh)
  199. hw_dsc->ops.dsc_config_thresh(hw_dsc, dsc);
  200. if (hw_dsc_pp->ops.setup_dsc)
  201. hw_dsc_pp->ops.setup_dsc(hw_dsc_pp);
  202. if (hw_dsc->ops.bind_pingpong_blk)
  203. hw_dsc->ops.bind_pingpong_blk(hw_dsc, true, hw_pp->idx);
  204. if (hw_dsc_pp->ops.enable_dsc)
  205. hw_dsc_pp->ops.enable_dsc(hw_dsc_pp);
  206. }
  207. static int _dce_dsc_setup(struct sde_encoder_virt *sde_enc,
  208. struct sde_encoder_kickoff_params *params)
  209. {
  210. struct sde_kms *sde_kms;
  211. struct msm_drm_private *priv;
  212. struct drm_encoder *drm_enc;
  213. struct drm_connector *drm_conn;
  214. struct sde_encoder_phys *enc_master;
  215. struct sde_hw_dsc *hw_dsc[MAX_CHANNELS_PER_ENC];
  216. struct sde_hw_pingpong *hw_pp[MAX_CHANNELS_PER_ENC];
  217. struct sde_hw_pingpong *hw_dsc_pp[MAX_CHANNELS_PER_ENC];
  218. struct msm_display_dsc_info *dsc = NULL;
  219. enum sde_rm_topology_name topology;
  220. const struct sde_rm_topology_def *def;
  221. const struct sde_rect *roi;
  222. struct sde_hw_ctl *hw_ctl;
  223. struct sde_ctl_dsc_cfg cfg;
  224. bool half_panel_partial_update, dsc_merge;
  225. int this_frame_slices;
  226. int intf_ip_w, enc_ip_w;
  227. int num_intf, num_dsc;
  228. int ich_res;
  229. int dsc_common_mode = 0;
  230. int i;
  231. if (!sde_enc || !params || !sde_enc->phys_encs[0] ||
  232. !sde_enc->phys_encs[0]->connector)
  233. return -EINVAL;
  234. drm_conn = sde_enc->phys_encs[0]->connector;
  235. drm_enc = &sde_enc->base;
  236. priv = drm_enc->dev->dev_private;
  237. sde_kms = to_sde_kms(priv->kms);
  238. topology = sde_connector_get_topology_name(drm_conn);
  239. if (topology == SDE_RM_TOPOLOGY_NONE) {
  240. SDE_ERROR_DCE(sde_enc, "topology not set yet\n");
  241. return -EINVAL;
  242. }
  243. SDE_DEBUG_DCE(sde_enc, "topology:%d\n", topology);
  244. if (sde_kms_rect_is_equal(&sde_enc->cur_conn_roi,
  245. &sde_enc->prv_conn_roi))
  246. return 0;
  247. SDE_EVT32(DRMID(&sde_enc->base), topology,
  248. sde_enc->cur_conn_roi.x,
  249. sde_enc->cur_conn_roi.y,
  250. sde_enc->cur_conn_roi.w,
  251. sde_enc->cur_conn_roi.h,
  252. sde_enc->prv_conn_roi.x,
  253. sde_enc->prv_conn_roi.y,
  254. sde_enc->prv_conn_roi.w,
  255. sde_enc->prv_conn_roi.h,
  256. sde_enc->cur_master->cached_mode.hdisplay,
  257. sde_enc->cur_master->cached_mode.vdisplay);
  258. memset(&cfg, 0, sizeof(cfg));
  259. enc_master = sde_enc->cur_master;
  260. roi = &sde_enc->cur_conn_roi;
  261. hw_ctl = enc_master->hw_ctl;
  262. dsc = &sde_enc->mode_info.comp_info.dsc_info;
  263. def = sde_rm_topology_get_topology_def(&sde_kms->rm, topology);
  264. if (IS_ERR_OR_NULL(def))
  265. return -EINVAL;
  266. num_dsc = def->num_comp_enc;
  267. num_intf = def->num_intf;
  268. /*
  269. * If this encoder is driving more than one DSC encoder, they
  270. * operate in tandem, same pic dimension needs to be used by
  271. * each of them.(pp-split is assumed to be not supported)
  272. */
  273. _dce_dsc_update_pic_dim(dsc, roi->w, roi->h);
  274. half_panel_partial_update = (num_dsc > 1) ?
  275. (hweight_long(params->affected_displays) != num_dsc) :
  276. false;
  277. dsc_merge = (num_dsc > num_intf) ? true : false;
  278. if (!half_panel_partial_update)
  279. dsc_common_mode |= DSC_MODE_SPLIT_PANEL;
  280. if (dsc_merge)
  281. dsc_common_mode |= DSC_MODE_MULTIPLEX;
  282. if (enc_master->intf_mode == INTF_MODE_VIDEO)
  283. dsc_common_mode |= DSC_MODE_VIDEO;
  284. this_frame_slices = roi->w / dsc->slice_width;
  285. intf_ip_w = this_frame_slices * dsc->slice_width;
  286. if ((!half_panel_partial_update) && (num_intf > 1))
  287. intf_ip_w /= 2;
  288. _dce_dsc_pclk_param_calc(dsc, intf_ip_w);
  289. /*
  290. * in dsc merge case: when using 2 encoders for the same stream,
  291. * no. of slices need to be same on both the encoders.
  292. */
  293. enc_ip_w = intf_ip_w;
  294. if (dsc_merge)
  295. enc_ip_w = intf_ip_w / 2;
  296. _dce_dsc_initial_line_calc(dsc, enc_ip_w, dsc_common_mode);
  297. /*
  298. * __is_ich_reset_override_needed should be called only after
  299. * updating pic dimension, mdss_panel_dsc_update_pic_dim.
  300. */
  301. ich_res = _dce_dsc_ich_reset_override_needed(
  302. half_panel_partial_update, dsc);
  303. SDE_DEBUG_DCE(sde_enc, "pic_w: %d pic_h: %d mode:%d\n",
  304. roi->w, roi->h, dsc_common_mode);
  305. for (i = 0; i < num_dsc; i++) {
  306. bool active = !!((1 << i) & params->affected_displays);
  307. hw_pp[i] = sde_enc->hw_pp[i];
  308. hw_dsc[i] = sde_enc->hw_dsc[i];
  309. hw_dsc_pp[i] = sde_enc->hw_dsc_pp[i];
  310. if (!hw_pp[i] || !hw_dsc[i] || !hw_dsc_pp[i]) {
  311. SDE_ERROR_DCE(sde_enc, "invalid params for DSC\n");
  312. SDE_EVT32(DRMID(&sde_enc->base), roi->w, roi->h,
  313. dsc_common_mode, i, active);
  314. return -EINVAL;
  315. }
  316. _dce_dsc_pipe_cfg(hw_dsc[i], hw_pp[i], dsc,
  317. dsc_common_mode, ich_res, active, hw_dsc_pp[i]);
  318. if (active) {
  319. if (cfg.dsc_count >= MAX_DSC_PER_CTL_V1) {
  320. pr_err("Invalid dsc count:%d\n",
  321. cfg.dsc_count);
  322. return -EINVAL;
  323. }
  324. cfg.dsc[cfg.dsc_count++] = hw_dsc[i]->idx;
  325. if (hw_ctl->ops.update_bitmask_dsc)
  326. hw_ctl->ops.update_bitmask_dsc(hw_ctl,
  327. hw_dsc[i]->idx, 1);
  328. }
  329. }
  330. /* setup dsc active configuration in the control path */
  331. if (hw_ctl->ops.setup_dsc_cfg) {
  332. hw_ctl->ops.setup_dsc_cfg(hw_ctl, &cfg);
  333. SDE_DEBUG_DCE(sde_enc,
  334. "setup dsc_cfg hw_ctl[%d], count:%d,dsc[0]:%d, dsc[1]:%d\n",
  335. hw_ctl->idx,
  336. cfg.dsc_count,
  337. cfg.dsc[0],
  338. cfg.dsc[1]);
  339. }
  340. return 0;
  341. }
  342. static void _dce_dsc_disable(struct sde_encoder_virt *sde_enc)
  343. {
  344. int i;
  345. struct sde_hw_pingpong *hw_pp = NULL;
  346. struct sde_hw_pingpong *hw_dsc_pp = NULL;
  347. struct sde_hw_dsc *hw_dsc = NULL;
  348. struct sde_hw_ctl *hw_ctl = NULL;
  349. struct sde_ctl_dsc_cfg cfg;
  350. if (!sde_enc || !sde_enc->phys_encs[0] ||
  351. !sde_enc->phys_encs[0]->connector) {
  352. SDE_ERROR("invalid params %d %d\n",
  353. !sde_enc, sde_enc ? !sde_enc->phys_encs[0] : -1);
  354. return;
  355. }
  356. if (sde_enc->cur_master)
  357. hw_ctl = sde_enc->cur_master->hw_ctl;
  358. /* Disable DSC for all the pp's present in this topology */
  359. for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
  360. hw_pp = sde_enc->hw_pp[i];
  361. hw_dsc = sde_enc->hw_dsc[i];
  362. hw_dsc_pp = sde_enc->hw_dsc_pp[i];
  363. _dce_dsc_pipe_cfg(hw_dsc, hw_pp, NULL,
  364. 0, 0, 0, hw_dsc_pp);
  365. if (hw_dsc)
  366. sde_enc->dirty_dsc_ids[i] = hw_dsc->idx;
  367. }
  368. /* Clear the DSC ACTIVE config for this CTL */
  369. if (hw_ctl && hw_ctl->ops.setup_dsc_cfg) {
  370. memset(&cfg, 0, sizeof(cfg));
  371. hw_ctl->ops.setup_dsc_cfg(hw_ctl, &cfg);
  372. }
  373. /**
  374. * Since pending flushes from previous commit get cleared
  375. * sometime after this point, setting DSC flush bits now
  376. * will have no effect. Therefore dirty_dsc_ids track which
  377. * DSC blocks must be flushed for the next trigger.
  378. */
  379. }
  380. static bool _dce_dsc_is_dirty(struct sde_encoder_virt *sde_enc)
  381. {
  382. int i;
  383. for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
  384. /**
  385. * This dirty_dsc_hw field is set during DSC disable to
  386. * indicate which DSC blocks need to be flushed
  387. */
  388. if (sde_enc->dirty_dsc_ids[i])
  389. return true;
  390. }
  391. return false;
  392. }
  393. static void _dce_helper_flush_dsc(struct sde_encoder_virt *sde_enc)
  394. {
  395. int i;
  396. struct sde_hw_ctl *hw_ctl = NULL;
  397. enum sde_dsc dsc_idx;
  398. if (sde_enc->cur_master)
  399. hw_ctl = sde_enc->cur_master->hw_ctl;
  400. for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
  401. dsc_idx = sde_enc->dirty_dsc_ids[i];
  402. if (dsc_idx && hw_ctl && hw_ctl->ops.update_bitmask_dsc)
  403. hw_ctl->ops.update_bitmask_dsc(hw_ctl, dsc_idx, 1);
  404. sde_enc->dirty_dsc_ids[i] = DSC_NONE;
  405. }
  406. }
  407. void sde_encoder_dce_disable(struct sde_encoder_virt *sde_enc)
  408. {
  409. enum msm_display_compression_type comp_type;
  410. if (!sde_enc)
  411. return;
  412. comp_type = sde_enc->mode_info.comp_info.comp_type;
  413. if (comp_type == MSM_DISPLAY_COMPRESSION_DSC)
  414. _dce_dsc_disable(sde_enc);
  415. }
  416. int sde_encoder_dce_flush(struct sde_encoder_virt *sde_enc)
  417. {
  418. int rc = 0;
  419. if (!sde_enc)
  420. return -EINVAL;
  421. if (_dce_dsc_is_dirty(sde_enc))
  422. _dce_helper_flush_dsc(sde_enc);
  423. return rc;
  424. }
  425. int sde_encoder_dce_setup(struct sde_encoder_virt *sde_enc,
  426. struct sde_encoder_kickoff_params *params)
  427. {
  428. enum msm_display_compression_type comp_type;
  429. int rc = 0;
  430. if (!sde_enc)
  431. return -EINVAL;
  432. comp_type = sde_enc->mode_info.comp_info.comp_type;
  433. if (comp_type == MSM_DISPLAY_COMPRESSION_DSC)
  434. rc = _dce_dsc_setup(sde_enc, params);
  435. return rc;
  436. }