sde_encoder_phys_vid.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
  7. #include "sde_encoder_phys.h"
  8. #include "sde_hw_interrupts.h"
  9. #include "sde_core_irq.h"
  10. #include "sde_formats.h"
  11. #include "dsi_display.h"
  12. #include "sde_trace.h"
  13. #define SDE_DEBUG_VIDENC(e, fmt, ...) SDE_DEBUG("enc%d intf%d " fmt, \
  14. (e) && (e)->base.parent ? \
  15. (e)->base.parent->base.id : -1, \
  16. (e) && (e)->base.hw_intf ? \
  17. (e)->base.hw_intf->idx - INTF_0 : -1, ##__VA_ARGS__)
  18. #define SDE_ERROR_VIDENC(e, fmt, ...) SDE_ERROR("enc%d intf%d " fmt, \
  19. (e) && (e)->base.parent ? \
  20. (e)->base.parent->base.id : -1, \
  21. (e) && (e)->base.hw_intf ? \
  22. (e)->base.hw_intf->idx - INTF_0 : -1, ##__VA_ARGS__)
  23. #define to_sde_encoder_phys_vid(x) \
  24. container_of(x, struct sde_encoder_phys_vid, base)
  25. /* Poll time to do recovery during active region */
  26. #define POLL_TIME_USEC_FOR_LN_CNT 500
  27. #define MAX_POLL_CNT 10
  28. static bool sde_encoder_phys_vid_is_master(
  29. struct sde_encoder_phys *phys_enc)
  30. {
  31. bool ret = false;
  32. if (phys_enc->split_role != ENC_ROLE_SLAVE)
  33. ret = true;
  34. return ret;
  35. }
  36. static void drm_mode_to_intf_timing_params(
  37. const struct sde_encoder_phys_vid *vid_enc,
  38. const struct drm_display_mode *mode,
  39. struct intf_timing_params *timing)
  40. {
  41. const struct sde_encoder_phys *phys_enc = &vid_enc->base;
  42. memset(timing, 0, sizeof(*timing));
  43. if ((mode->htotal < mode->hsync_end)
  44. || (mode->hsync_start < mode->hdisplay)
  45. || (mode->vtotal < mode->vsync_end)
  46. || (mode->vsync_start < mode->vdisplay)
  47. || (mode->hsync_end < mode->hsync_start)
  48. || (mode->vsync_end < mode->vsync_start)) {
  49. SDE_ERROR(
  50. "invalid params - hstart:%d,hend:%d,htot:%d,hdisplay:%d\n",
  51. mode->hsync_start, mode->hsync_end,
  52. mode->htotal, mode->hdisplay);
  53. SDE_ERROR("vstart:%d,vend:%d,vtot:%d,vdisplay:%d\n",
  54. mode->vsync_start, mode->vsync_end,
  55. mode->vtotal, mode->vdisplay);
  56. return;
  57. }
  58. /*
  59. * https://www.kernel.org/doc/htmldocs/drm/ch02s05.html
  60. * Active Region Front Porch Sync Back Porch
  61. * <-----------------><------------><-----><----------->
  62. * <- [hv]display --->
  63. * <--------- [hv]sync_start ------>
  64. * <----------------- [hv]sync_end ------->
  65. * <---------------------------- [hv]total ------------->
  66. */
  67. timing->poms_align_vsync = phys_enc->poms_align_vsync;
  68. timing->width = mode->hdisplay; /* active width */
  69. timing->height = mode->vdisplay; /* active height */
  70. timing->xres = timing->width;
  71. timing->yres = timing->height;
  72. timing->h_back_porch = mode->htotal - mode->hsync_end;
  73. timing->h_front_porch = mode->hsync_start - mode->hdisplay;
  74. timing->v_back_porch = mode->vtotal - mode->vsync_end;
  75. timing->v_front_porch = mode->vsync_start - mode->vdisplay;
  76. timing->hsync_pulse_width = mode->hsync_end - mode->hsync_start;
  77. timing->vsync_pulse_width = mode->vsync_end - mode->vsync_start;
  78. timing->hsync_polarity = (mode->flags & DRM_MODE_FLAG_NHSYNC) ? 1 : 0;
  79. timing->vsync_polarity = (mode->flags & DRM_MODE_FLAG_NVSYNC) ? 1 : 0;
  80. timing->border_clr = 0;
  81. timing->underflow_clr = 0xff;
  82. timing->hsync_skew = mode->hskew;
  83. timing->v_front_porch_fixed = vid_enc->base.vfp_cached;
  84. timing->vrefresh = drm_mode_vrefresh(mode);
  85. if (vid_enc->base.comp_type != MSM_DISPLAY_COMPRESSION_NONE) {
  86. timing->compression_en = true;
  87. timing->dce_bytes_per_line = vid_enc->base.dce_bytes_per_line;
  88. }
  89. /* DSI controller cannot handle active-low sync signals. */
  90. if (phys_enc->hw_intf->cap->type == INTF_DSI) {
  91. timing->hsync_polarity = 0;
  92. timing->vsync_polarity = 0;
  93. }
  94. /* for DP/EDP, Shift timings to align it to bottom right */
  95. if ((phys_enc->hw_intf->cap->type == INTF_DP) ||
  96. (phys_enc->hw_intf->cap->type == INTF_EDP)) {
  97. timing->h_back_porch += timing->h_front_porch;
  98. timing->h_front_porch = 0;
  99. timing->v_back_porch += timing->v_front_porch;
  100. timing->v_front_porch = 0;
  101. }
  102. timing->wide_bus_en = sde_encoder_is_widebus_enabled(phys_enc->parent);
  103. /*
  104. * for DP, divide the horizonal parameters by 2 when
  105. * widebus or compression is enabled, irrespective of
  106. * compression ratio
  107. */
  108. if (phys_enc->hw_intf->cap->type == INTF_DP &&
  109. (timing->wide_bus_en ||
  110. (vid_enc->base.comp_ratio > 1))) {
  111. timing->width = timing->width >> 1;
  112. timing->xres = timing->xres >> 1;
  113. timing->h_back_porch = timing->h_back_porch >> 1;
  114. timing->h_front_porch = timing->h_front_porch >> 1;
  115. timing->hsync_pulse_width = timing->hsync_pulse_width >> 1;
  116. if (vid_enc->base.comp_type == MSM_DISPLAY_COMPRESSION_DSC &&
  117. (vid_enc->base.comp_ratio > 1)) {
  118. timing->extra_dto_cycles =
  119. vid_enc->base.dsc_extra_pclk_cycle_cnt;
  120. timing->width += vid_enc->base.dsc_extra_disp_width;
  121. timing->h_back_porch +=
  122. vid_enc->base.dsc_extra_disp_width;
  123. }
  124. }
  125. /*
  126. * for DSI, if compression is enabled, then divide the horizonal active
  127. * timing parameters by compression ratio.
  128. */
  129. if ((phys_enc->hw_intf->cap->type != INTF_DP) &&
  130. ((vid_enc->base.comp_type ==
  131. MSM_DISPLAY_COMPRESSION_DSC) ||
  132. (vid_enc->base.comp_type ==
  133. MSM_DISPLAY_COMPRESSION_VDC))) {
  134. // adjust active dimensions
  135. timing->width = DIV_ROUND_UP(timing->width,
  136. vid_enc->base.comp_ratio);
  137. timing->xres = DIV_ROUND_UP(timing->xres,
  138. vid_enc->base.comp_ratio);
  139. }
  140. /*
  141. * For edp only:
  142. * DISPLAY_V_START = (VBP * HCYCLE) + HBP
  143. * DISPLAY_V_END = (VBP + VACTIVE) * HCYCLE - 1 - HFP
  144. */
  145. /*
  146. * if (vid_enc->hw->cap->type == INTF_EDP) {
  147. * display_v_start += mode->htotal - mode->hsync_start;
  148. * display_v_end -= mode->hsync_start - mode->hdisplay;
  149. * }
  150. */
  151. }
  152. static inline u32 get_horizontal_total(const struct intf_timing_params *timing)
  153. {
  154. u32 active = timing->xres;
  155. u32 inactive =
  156. timing->h_back_porch + timing->h_front_porch +
  157. timing->hsync_pulse_width;
  158. return active + inactive;
  159. }
  160. static inline u32 get_vertical_total(const struct intf_timing_params *timing)
  161. {
  162. u32 active = timing->yres;
  163. u32 inactive = timing->v_back_porch + timing->v_front_porch +
  164. timing->vsync_pulse_width;
  165. return active + inactive;
  166. }
  167. /*
  168. * programmable_fetch_get_num_lines:
  169. * Number of fetch lines in vertical front porch
  170. * @timing: Pointer to the intf timing information for the requested mode
  171. *
  172. * Returns the number of fetch lines in vertical front porch at which mdp
  173. * can start fetching the next frame.
  174. *
  175. * Number of needed prefetch lines is anything that cannot be absorbed in the
  176. * start of frame time (back porch + vsync pulse width).
  177. *
  178. * Some panels have very large VFP, however we only need a total number of
  179. * lines based on the chip worst case latencies.
  180. */
  181. static u32 programmable_fetch_get_num_lines(
  182. struct sde_encoder_phys_vid *vid_enc,
  183. const struct intf_timing_params *timing)
  184. {
  185. struct sde_encoder_phys *phys_enc = &vid_enc->base;
  186. struct sde_mdss_cfg *m;
  187. u32 needed_prefill_lines, needed_vfp_lines, actual_vfp_lines;
  188. const u32 fixed_prefill_fps = DEFAULT_FPS;
  189. u32 default_prefill_lines =
  190. phys_enc->hw_intf->cap->prog_fetch_lines_worst_case;
  191. u32 start_of_frame_lines =
  192. timing->v_back_porch + timing->vsync_pulse_width;
  193. u32 v_front_porch = timing->v_front_porch;
  194. u32 vrefresh, max_fps;
  195. m = phys_enc->sde_kms->catalog;
  196. max_fps = sde_encoder_get_dfps_maxfps(phys_enc->parent);
  197. vrefresh = (max_fps > timing->vrefresh) ? max_fps : timing->vrefresh;
  198. /* minimum prefill lines are defined based on 60fps */
  199. needed_prefill_lines = (vrefresh > fixed_prefill_fps) ?
  200. ((default_prefill_lines * vrefresh) /
  201. fixed_prefill_fps) : default_prefill_lines;
  202. needed_vfp_lines = needed_prefill_lines - start_of_frame_lines;
  203. /* Fetch must be outside active lines, otherwise undefined. */
  204. if (start_of_frame_lines >= needed_prefill_lines) {
  205. SDE_DEBUG_VIDENC(vid_enc,
  206. "prog fetch always enabled case\n");
  207. actual_vfp_lines = (test_bit(SDE_FEATURE_DELAY_PRG_FETCH, m->features)) ? 2 : 1;
  208. } else if (v_front_porch < needed_vfp_lines) {
  209. /* Warn fetch needed, but not enough porch in panel config */
  210. pr_warn_once
  211. ("low vbp+vfp may lead to perf issues in some cases\n");
  212. SDE_DEBUG_VIDENC(vid_enc,
  213. "less vfp than fetch req, using entire vfp\n");
  214. actual_vfp_lines = v_front_porch;
  215. } else {
  216. SDE_DEBUG_VIDENC(vid_enc, "room in vfp for needed prefetch\n");
  217. actual_vfp_lines = needed_vfp_lines;
  218. }
  219. SDE_DEBUG_VIDENC(vid_enc,
  220. "vrefresh:%u v_front_porch:%u v_back_porch:%u vsync_pulse_width:%u\n",
  221. vrefresh, v_front_porch, timing->v_back_porch,
  222. timing->vsync_pulse_width);
  223. SDE_DEBUG_VIDENC(vid_enc,
  224. "prefill_lines:%u needed_vfp_lines:%u actual_vfp_lines:%u\n",
  225. needed_prefill_lines, needed_vfp_lines, actual_vfp_lines);
  226. return actual_vfp_lines;
  227. }
  228. /*
  229. * programmable_fetch_config: Programs HW to prefetch lines by offsetting
  230. * the start of fetch into the vertical front porch for cases where the
  231. * vsync pulse width and vertical back porch time is insufficient
  232. *
  233. * Gets # of lines to pre-fetch, then calculate VSYNC counter value.
  234. * HW layer requires VSYNC counter of first pixel of tgt VFP line.
  235. *
  236. * @timing: Pointer to the intf timing information for the requested mode
  237. */
  238. static void programmable_fetch_config(struct sde_encoder_phys *phys_enc,
  239. const struct intf_timing_params *timing)
  240. {
  241. struct sde_encoder_phys_vid *vid_enc =
  242. to_sde_encoder_phys_vid(phys_enc);
  243. struct intf_prog_fetch f = { 0 };
  244. u32 vfp_fetch_lines = 0;
  245. u32 horiz_total = 0;
  246. u32 vert_total = 0;
  247. u32 vfp_fetch_start_vsync_counter = 0;
  248. unsigned long lock_flags;
  249. struct sde_mdss_cfg *m;
  250. if (WARN_ON_ONCE(!phys_enc->hw_intf->ops.setup_prg_fetch))
  251. return;
  252. m = phys_enc->sde_kms->catalog;
  253. vfp_fetch_lines = programmable_fetch_get_num_lines(vid_enc, timing);
  254. if (vfp_fetch_lines) {
  255. vert_total = get_vertical_total(timing);
  256. horiz_total = get_horizontal_total(timing);
  257. vfp_fetch_start_vsync_counter =
  258. (vert_total - vfp_fetch_lines) * horiz_total + 1;
  259. /**
  260. * Check if we need to throttle the fetch to start
  261. * from second line after the active region.
  262. */
  263. if (test_bit(SDE_FEATURE_DELAY_PRG_FETCH, m->features))
  264. vfp_fetch_start_vsync_counter += horiz_total;
  265. f.enable = 1;
  266. f.fetch_start = vfp_fetch_start_vsync_counter;
  267. }
  268. SDE_DEBUG_VIDENC(vid_enc,
  269. "vfp_fetch_lines %u vfp_fetch_start_vsync_counter %u\n",
  270. vfp_fetch_lines, vfp_fetch_start_vsync_counter);
  271. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  272. phys_enc->hw_intf->ops.setup_prg_fetch(phys_enc->hw_intf, &f);
  273. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  274. }
  275. static bool sde_encoder_phys_vid_mode_fixup(
  276. struct sde_encoder_phys *phys_enc,
  277. const struct drm_display_mode *mode,
  278. struct drm_display_mode *adj_mode)
  279. {
  280. if (phys_enc)
  281. SDE_DEBUG_VIDENC(to_sde_encoder_phys_vid(phys_enc), "\n");
  282. /*
  283. * Modifying mode has consequences when the mode comes back to us
  284. */
  285. return true;
  286. }
  287. /* vid_enc timing_params must be configured before calling this function */
  288. static void _sde_encoder_phys_vid_setup_avr(
  289. struct sde_encoder_phys *phys_enc, u32 qsync_min_fps)
  290. {
  291. struct sde_encoder_phys_vid *vid_enc;
  292. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  293. if (vid_enc->base.hw_intf->ops.avr_setup) {
  294. struct intf_avr_params avr_params = {0};
  295. u32 default_fps = drm_mode_vrefresh(&phys_enc->cached_mode);
  296. int ret;
  297. if (!default_fps) {
  298. SDE_ERROR_VIDENC(vid_enc,
  299. "invalid default fps %d\n",
  300. default_fps);
  301. return;
  302. }
  303. if (qsync_min_fps > default_fps) {
  304. SDE_ERROR_VIDENC(vid_enc,
  305. "qsync fps %d must be less than default %d\n",
  306. qsync_min_fps, default_fps);
  307. return;
  308. }
  309. avr_params.default_fps = default_fps;
  310. avr_params.min_fps = qsync_min_fps;
  311. ret = vid_enc->base.hw_intf->ops.avr_setup(
  312. vid_enc->base.hw_intf,
  313. &vid_enc->timing_params, &avr_params);
  314. if (ret)
  315. SDE_ERROR_VIDENC(vid_enc,
  316. "bad settings, can't configure AVR\n");
  317. SDE_EVT32(DRMID(phys_enc->parent), default_fps,
  318. qsync_min_fps, ret);
  319. }
  320. }
  321. static void _sde_encoder_phys_vid_avr_ctrl(struct sde_encoder_phys *phys_enc)
  322. {
  323. struct intf_avr_params avr_params;
  324. struct sde_encoder_phys_vid *vid_enc = to_sde_encoder_phys_vid(phys_enc);
  325. u32 avr_step_fps = sde_connector_get_avr_step(phys_enc->connector);
  326. memset(&avr_params, 0, sizeof(avr_params));
  327. avr_params.avr_mode = sde_connector_get_qsync_mode(phys_enc->connector);
  328. if (avr_step_fps)
  329. avr_params.avr_step_lines = mult_frac(phys_enc->cached_mode.vtotal,
  330. vid_enc->timing_params.vrefresh, avr_step_fps);
  331. if (vid_enc->base.hw_intf->ops.avr_ctrl)
  332. vid_enc->base.hw_intf->ops.avr_ctrl(vid_enc->base.hw_intf, &avr_params);
  333. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_intf->idx - INTF_0,
  334. avr_params.avr_mode, avr_params.avr_step_lines, avr_step_fps);
  335. }
  336. static void sde_encoder_phys_vid_setup_timing_engine(
  337. struct sde_encoder_phys *phys_enc)
  338. {
  339. struct sde_encoder_phys_vid *vid_enc;
  340. struct drm_display_mode mode;
  341. struct intf_timing_params timing_params = { 0 };
  342. const struct sde_format *fmt = NULL;
  343. u32 fmt_fourcc = DRM_FORMAT_RGB888;
  344. u32 qsync_min_fps = 0;
  345. unsigned long lock_flags;
  346. struct sde_hw_intf_cfg intf_cfg = { 0 };
  347. bool is_split_link = false;
  348. if (!phys_enc || !phys_enc->sde_kms || !phys_enc->hw_ctl ||
  349. !phys_enc->hw_intf || !phys_enc->connector) {
  350. SDE_ERROR("invalid encoder %d\n", !phys_enc);
  351. return;
  352. }
  353. mode = phys_enc->cached_mode;
  354. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  355. if (!phys_enc->hw_intf->ops.setup_timing_gen) {
  356. SDE_ERROR("timing engine setup is not supported\n");
  357. return;
  358. }
  359. SDE_DEBUG_VIDENC(vid_enc, "enabling mode:\n");
  360. drm_mode_debug_printmodeline(&mode);
  361. is_split_link = phys_enc->hw_intf->cfg.split_link_en;
  362. if (phys_enc->split_role != ENC_ROLE_SOLO || is_split_link) {
  363. mode.hdisplay >>= 1;
  364. mode.htotal >>= 1;
  365. mode.hsync_start >>= 1;
  366. mode.hsync_end >>= 1;
  367. SDE_DEBUG_VIDENC(vid_enc,
  368. "split_role %d, halve horizontal %d %d %d %d\n",
  369. phys_enc->split_role,
  370. mode.hdisplay, mode.htotal,
  371. mode.hsync_start, mode.hsync_end);
  372. }
  373. if (!phys_enc->vfp_cached) {
  374. phys_enc->vfp_cached =
  375. sde_connector_get_panel_vfp(phys_enc->connector, &mode);
  376. if (phys_enc->vfp_cached <= 0)
  377. phys_enc->vfp_cached = mode.vsync_start - mode.vdisplay;
  378. }
  379. drm_mode_to_intf_timing_params(vid_enc, &mode, &timing_params);
  380. vid_enc->timing_params = timing_params;
  381. if (phys_enc->cont_splash_enabled) {
  382. SDE_DEBUG_VIDENC(vid_enc,
  383. "skipping intf programming since cont splash is enabled\n");
  384. goto exit;
  385. }
  386. fmt = sde_get_sde_format(fmt_fourcc);
  387. SDE_DEBUG_VIDENC(vid_enc, "fmt_fourcc 0x%X\n", fmt_fourcc);
  388. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  389. phys_enc->hw_intf->ops.setup_timing_gen(phys_enc->hw_intf,
  390. &timing_params, fmt);
  391. if (test_bit(SDE_CTL_ACTIVE_CFG,
  392. &phys_enc->hw_ctl->caps->features)) {
  393. sde_encoder_helper_update_intf_cfg(phys_enc);
  394. } else if (phys_enc->hw_ctl->ops.setup_intf_cfg) {
  395. intf_cfg.intf = phys_enc->hw_intf->idx;
  396. intf_cfg.intf_mode_sel = SDE_CTL_MODE_SEL_VID;
  397. intf_cfg.stream_sel = 0; /* Don't care value for video mode */
  398. intf_cfg.mode_3d =
  399. sde_encoder_helper_get_3d_blend_mode(phys_enc);
  400. phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl,
  401. &intf_cfg);
  402. }
  403. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  404. if (phys_enc->hw_intf->cap->type == INTF_DSI)
  405. programmable_fetch_config(phys_enc, &timing_params);
  406. exit:
  407. if (phys_enc->parent_ops.get_qsync_fps)
  408. phys_enc->parent_ops.get_qsync_fps(
  409. phys_enc->parent, &qsync_min_fps, phys_enc->connector->state);
  410. /* only panels which support qsync will have a non-zero min fps */
  411. if (qsync_min_fps) {
  412. _sde_encoder_phys_vid_setup_avr(phys_enc, qsync_min_fps);
  413. _sde_encoder_phys_vid_avr_ctrl(phys_enc);
  414. }
  415. }
  416. static void sde_encoder_phys_vid_vblank_irq(void *arg, int irq_idx)
  417. {
  418. struct sde_encoder_phys *phys_enc = arg;
  419. struct sde_hw_ctl *hw_ctl;
  420. struct intf_status intf_status = {0};
  421. unsigned long lock_flags;
  422. u32 flush_register = ~0;
  423. u32 reset_status = 0;
  424. int new_cnt = -1, old_cnt = -1;
  425. u32 event = 0;
  426. int pend_ret_fence_cnt = 0;
  427. if (!phys_enc)
  428. return;
  429. hw_ctl = phys_enc->hw_ctl;
  430. if (!hw_ctl)
  431. return;
  432. SDE_ATRACE_BEGIN("vblank_irq");
  433. /*
  434. * only decrement the pending flush count if we've actually flushed
  435. * hardware. due to sw irq latency, vblank may have already happened
  436. * so we need to double-check with hw that it accepted the flush bits
  437. */
  438. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  439. old_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  440. if (hw_ctl && hw_ctl->ops.get_flush_register)
  441. flush_register = hw_ctl->ops.get_flush_register(hw_ctl);
  442. if (flush_register)
  443. goto not_flushed;
  444. new_cnt = atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
  445. pend_ret_fence_cnt = atomic_read(&phys_enc->pending_retire_fence_cnt);
  446. /* signal only for master, where there is a pending kickoff */
  447. if (sde_encoder_phys_vid_is_master(phys_enc) &&
  448. atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0)) {
  449. event = SDE_ENCODER_FRAME_EVENT_DONE |
  450. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE |
  451. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  452. }
  453. not_flushed:
  454. if (hw_ctl && hw_ctl->ops.get_reset)
  455. reset_status = hw_ctl->ops.get_reset(hw_ctl);
  456. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  457. if (event && phys_enc->parent_ops.handle_frame_done)
  458. phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
  459. phys_enc, event);
  460. if (phys_enc->parent_ops.handle_vblank_virt)
  461. phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
  462. phys_enc);
  463. if (phys_enc->hw_intf->ops.get_status)
  464. phys_enc->hw_intf->ops.get_status(phys_enc->hw_intf,
  465. &intf_status);
  466. SDE_EVT32_IRQ(DRMID(phys_enc->parent), phys_enc->hw_intf->idx - INTF_0,
  467. old_cnt, atomic_read(&phys_enc->pending_kickoff_cnt),
  468. reset_status ? SDE_EVTLOG_ERROR : 0,
  469. flush_register, event,
  470. atomic_read(&phys_enc->pending_retire_fence_cnt),
  471. intf_status.frame_count, intf_status.line_count);
  472. /* Signal any waiting atomic commit thread */
  473. wake_up_all(&phys_enc->pending_kickoff_wq);
  474. SDE_ATRACE_END("vblank_irq");
  475. }
  476. static void sde_encoder_phys_vid_underrun_irq(void *arg, int irq_idx)
  477. {
  478. struct sde_encoder_phys *phys_enc = arg;
  479. if (!phys_enc)
  480. return;
  481. if (phys_enc->parent_ops.handle_underrun_virt)
  482. phys_enc->parent_ops.handle_underrun_virt(phys_enc->parent,
  483. phys_enc);
  484. }
  485. static void _sde_encoder_phys_vid_setup_irq_hw_idx(
  486. struct sde_encoder_phys *phys_enc)
  487. {
  488. struct sde_encoder_irq *irq;
  489. /*
  490. * Initialize irq->hw_idx only when irq is not registered.
  491. * Prevent invalidating irq->irq_idx as modeset may be
  492. * called many times during dfps.
  493. */
  494. irq = &phys_enc->irq[INTR_IDX_VSYNC];
  495. if (irq->irq_idx < 0)
  496. irq->hw_idx = phys_enc->intf_idx;
  497. irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
  498. if (irq->irq_idx < 0)
  499. irq->hw_idx = phys_enc->intf_idx;
  500. }
  501. static void sde_encoder_phys_vid_cont_splash_mode_set(
  502. struct sde_encoder_phys *phys_enc,
  503. struct drm_display_mode *adj_mode)
  504. {
  505. if (!phys_enc || !adj_mode) {
  506. SDE_ERROR("invalid args\n");
  507. return;
  508. }
  509. phys_enc->cached_mode = *adj_mode;
  510. phys_enc->enable_state = SDE_ENC_ENABLED;
  511. _sde_encoder_phys_vid_setup_irq_hw_idx(phys_enc);
  512. }
  513. static void sde_encoder_phys_vid_mode_set(
  514. struct sde_encoder_phys *phys_enc,
  515. struct drm_display_mode *mode,
  516. struct drm_display_mode *adj_mode, bool *reinit_mixers)
  517. {
  518. struct sde_rm *rm;
  519. struct sde_rm_hw_iter iter;
  520. int i, instance;
  521. struct sde_encoder_phys_vid *vid_enc;
  522. if (!phys_enc || !phys_enc->sde_kms) {
  523. SDE_ERROR("invalid encoder/kms\n");
  524. return;
  525. }
  526. rm = &phys_enc->sde_kms->rm;
  527. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  528. if (adj_mode) {
  529. phys_enc->cached_mode = *adj_mode;
  530. drm_mode_debug_printmodeline(adj_mode);
  531. SDE_DEBUG_VIDENC(vid_enc, "caching mode:\n");
  532. }
  533. instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
  534. /* Retrieve previously allocated HW Resources. Shouldn't fail */
  535. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
  536. for (i = 0; i <= instance; i++) {
  537. if (sde_rm_get_hw(rm, &iter)) {
  538. if (phys_enc->hw_ctl && phys_enc->hw_ctl != to_sde_hw_ctl(iter.hw)) {
  539. *reinit_mixers = true;
  540. SDE_EVT32(phys_enc->hw_ctl->idx,
  541. to_sde_hw_ctl(iter.hw)->idx);
  542. }
  543. phys_enc->hw_ctl = to_sde_hw_ctl(iter.hw);
  544. }
  545. }
  546. if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
  547. SDE_ERROR_VIDENC(vid_enc, "failed to init ctl, %ld\n",
  548. PTR_ERR(phys_enc->hw_ctl));
  549. phys_enc->hw_ctl = NULL;
  550. return;
  551. }
  552. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_INTF);
  553. for (i = 0; i <= instance; i++) {
  554. if (sde_rm_get_hw(rm, &iter))
  555. phys_enc->hw_intf = to_sde_hw_intf(iter.hw);
  556. }
  557. if (IS_ERR_OR_NULL(phys_enc->hw_intf)) {
  558. SDE_ERROR_VIDENC(vid_enc, "failed to init intf: %ld\n",
  559. PTR_ERR(phys_enc->hw_intf));
  560. phys_enc->hw_intf = NULL;
  561. return;
  562. }
  563. _sde_encoder_phys_vid_setup_irq_hw_idx(phys_enc);
  564. phys_enc->kickoff_timeout_ms =
  565. sde_encoder_helper_get_kickoff_timeout_ms(phys_enc->parent);
  566. }
  567. static int sde_encoder_phys_vid_control_vblank_irq(
  568. struct sde_encoder_phys *phys_enc,
  569. bool enable)
  570. {
  571. int ret = 0;
  572. struct sde_encoder_phys_vid *vid_enc;
  573. int refcount;
  574. if (!phys_enc) {
  575. SDE_ERROR("invalid encoder\n");
  576. return -EINVAL;
  577. }
  578. mutex_lock(phys_enc->vblank_ctl_lock);
  579. refcount = atomic_read(&phys_enc->vblank_refcount);
  580. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  581. /* Slave encoders don't report vblank */
  582. if (!sde_encoder_phys_vid_is_master(phys_enc))
  583. goto end;
  584. /* protect against negative */
  585. if (!enable && refcount == 0) {
  586. ret = -EINVAL;
  587. goto end;
  588. }
  589. SDE_DEBUG_VIDENC(vid_enc, "[%pS] enable=%d/%d\n",
  590. __builtin_return_address(0),
  591. enable, atomic_read(&phys_enc->vblank_refcount));
  592. SDE_EVT32(DRMID(phys_enc->parent), enable,
  593. atomic_read(&phys_enc->vblank_refcount));
  594. if (enable && atomic_inc_return(&phys_enc->vblank_refcount) == 1) {
  595. ret = sde_encoder_helper_register_irq(phys_enc, INTR_IDX_VSYNC);
  596. if (ret)
  597. atomic_dec_return(&phys_enc->vblank_refcount);
  598. } else if (!enable &&
  599. atomic_dec_return(&phys_enc->vblank_refcount) == 0) {
  600. ret = sde_encoder_helper_unregister_irq(phys_enc,
  601. INTR_IDX_VSYNC);
  602. if (ret)
  603. atomic_inc_return(&phys_enc->vblank_refcount);
  604. }
  605. end:
  606. if (ret) {
  607. SDE_ERROR_VIDENC(vid_enc,
  608. "control vblank irq error %d, enable %d\n",
  609. ret, enable);
  610. SDE_EVT32(DRMID(phys_enc->parent),
  611. phys_enc->hw_intf->idx - INTF_0,
  612. enable, refcount, SDE_EVTLOG_ERROR);
  613. }
  614. mutex_unlock(phys_enc->vblank_ctl_lock);
  615. return ret;
  616. }
  617. static bool sde_encoder_phys_vid_wait_dma_trigger(
  618. struct sde_encoder_phys *phys_enc)
  619. {
  620. struct sde_encoder_phys_vid *vid_enc;
  621. struct sde_hw_intf *intf;
  622. struct sde_hw_ctl *ctl;
  623. struct intf_status status;
  624. if (!phys_enc) {
  625. SDE_ERROR("invalid encoder\n");
  626. return false;
  627. }
  628. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  629. intf = phys_enc->hw_intf;
  630. ctl = phys_enc->hw_ctl;
  631. if (!phys_enc->hw_intf || !phys_enc->hw_ctl) {
  632. SDE_ERROR("invalid hw_intf %d hw_ctl %d\n",
  633. phys_enc->hw_intf != NULL, phys_enc->hw_ctl != NULL);
  634. return false;
  635. }
  636. if (!intf->ops.get_status)
  637. return false;
  638. intf->ops.get_status(intf, &status);
  639. /* if interface is not enabled, return true to wait for dma trigger */
  640. return status.is_en ? false : true;
  641. }
  642. static void sde_encoder_phys_vid_enable(struct sde_encoder_phys *phys_enc)
  643. {
  644. struct msm_drm_private *priv;
  645. struct sde_encoder_phys_vid *vid_enc;
  646. struct sde_hw_intf *intf;
  647. struct sde_hw_ctl *ctl;
  648. if (!phys_enc || !phys_enc->parent || !phys_enc->parent->dev ||
  649. !phys_enc->parent->dev->dev_private ||
  650. !phys_enc->sde_kms) {
  651. SDE_ERROR("invalid encoder/device\n");
  652. return;
  653. }
  654. priv = phys_enc->parent->dev->dev_private;
  655. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  656. intf = phys_enc->hw_intf;
  657. ctl = phys_enc->hw_ctl;
  658. if (!phys_enc->hw_intf || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
  659. SDE_ERROR("invalid hw_intf %d hw_ctl %d hw_pp %d\n",
  660. !phys_enc->hw_intf, !phys_enc->hw_ctl,
  661. !phys_enc->hw_pp);
  662. return;
  663. }
  664. if (!ctl->ops.update_bitmask) {
  665. SDE_ERROR("invalid hw_ctl ops %d\n", ctl->idx);
  666. return;
  667. }
  668. SDE_DEBUG_VIDENC(vid_enc, "\n");
  669. if (WARN_ON(!phys_enc->hw_intf->ops.enable_timing))
  670. return;
  671. if (!phys_enc->cont_splash_enabled)
  672. sde_encoder_helper_split_config(phys_enc,
  673. phys_enc->hw_intf->idx);
  674. sde_encoder_phys_vid_setup_timing_engine(phys_enc);
  675. /*
  676. * For cases where both the interfaces are connected to same ctl,
  677. * set the flush bit for both master and slave.
  678. * For single flush cases (dual-ctl or pp-split), skip setting the
  679. * flush bit for the slave intf, since both intfs use same ctl
  680. * and HW will only flush the master.
  681. */
  682. if (!test_bit(SDE_CTL_ACTIVE_CFG, &ctl->caps->features) &&
  683. sde_encoder_phys_needs_single_flush(phys_enc) &&
  684. !sde_encoder_phys_vid_is_master(phys_enc))
  685. goto skip_flush;
  686. /**
  687. * skip flushing intf during cont. splash handoff since bootloader
  688. * has already enabled the hardware and is single buffered.
  689. */
  690. if (phys_enc->cont_splash_enabled) {
  691. SDE_DEBUG_VIDENC(vid_enc,
  692. "skipping intf flush bit set as cont. splash is enabled\n");
  693. goto skip_flush;
  694. }
  695. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_INTF, intf->idx, 1);
  696. if (phys_enc->hw_pp->merge_3d)
  697. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_MERGE_3D,
  698. phys_enc->hw_pp->merge_3d->idx, 1);
  699. if (phys_enc->hw_intf->cap->type == INTF_DP &&
  700. phys_enc->comp_type == MSM_DISPLAY_COMPRESSION_DSC &&
  701. phys_enc->comp_ratio)
  702. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_PERIPH, intf->idx, 1);
  703. skip_flush:
  704. SDE_DEBUG_VIDENC(vid_enc, "update pending flush ctl %d intf %d\n",
  705. ctl->idx - CTL_0, intf->idx);
  706. SDE_EVT32(DRMID(phys_enc->parent),
  707. atomic_read(&phys_enc->pending_retire_fence_cnt));
  708. /* ctl_flush & timing engine enable will be triggered by framework */
  709. if (phys_enc->enable_state == SDE_ENC_DISABLED)
  710. phys_enc->enable_state = SDE_ENC_ENABLING;
  711. }
  712. static void sde_encoder_phys_vid_destroy(struct sde_encoder_phys *phys_enc)
  713. {
  714. struct sde_encoder_phys_vid *vid_enc;
  715. if (!phys_enc) {
  716. SDE_ERROR("invalid encoder\n");
  717. return;
  718. }
  719. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  720. SDE_DEBUG_VIDENC(vid_enc, "\n");
  721. kfree(vid_enc);
  722. }
  723. static void sde_encoder_phys_vid_get_hw_resources(
  724. struct sde_encoder_phys *phys_enc,
  725. struct sde_encoder_hw_resources *hw_res,
  726. struct drm_connector_state *conn_state)
  727. {
  728. struct sde_encoder_phys_vid *vid_enc;
  729. if (!phys_enc || !hw_res) {
  730. SDE_ERROR("invalid arg(s), enc %d hw_res %d conn_state %d\n",
  731. !phys_enc, !hw_res, !conn_state);
  732. return;
  733. }
  734. if ((phys_enc->intf_idx - INTF_0) >= INTF_MAX) {
  735. SDE_ERROR("invalid intf idx:%d\n", phys_enc->intf_idx);
  736. return;
  737. }
  738. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  739. SDE_DEBUG_VIDENC(vid_enc, "\n");
  740. hw_res->intfs[phys_enc->intf_idx - INTF_0] = INTF_MODE_VIDEO;
  741. }
  742. static int _sde_encoder_phys_vid_wait_for_vblank(
  743. struct sde_encoder_phys *phys_enc, bool notify)
  744. {
  745. struct sde_encoder_wait_info wait_info = {0};
  746. int ret = 0;
  747. u32 event = SDE_ENCODER_FRAME_EVENT_ERROR |
  748. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE |
  749. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  750. struct drm_connector *conn;
  751. if (!phys_enc) {
  752. pr_err("invalid encoder\n");
  753. return -EINVAL;
  754. }
  755. conn = phys_enc->connector;
  756. wait_info.wq = &phys_enc->pending_kickoff_wq;
  757. wait_info.atomic_cnt = &phys_enc->pending_kickoff_cnt;
  758. wait_info.timeout_ms = phys_enc->kickoff_timeout_ms;
  759. /* Wait for kickoff to complete */
  760. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_VSYNC,
  761. &wait_info);
  762. if (notify && (ret == -ETIMEDOUT) &&
  763. atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0) &&
  764. phys_enc->parent_ops.handle_frame_done) {
  765. phys_enc->parent_ops.handle_frame_done(
  766. phys_enc->parent, phys_enc, event);
  767. if (sde_encoder_recovery_events_enabled(phys_enc->parent))
  768. sde_connector_event_notify(conn,
  769. DRM_EVENT_SDE_HW_RECOVERY,
  770. sizeof(uint8_t), SDE_RECOVERY_HARD_RESET);
  771. }
  772. SDE_EVT32(DRMID(phys_enc->parent), event, notify, ret,
  773. ret ? SDE_EVTLOG_FATAL : 0);
  774. return ret;
  775. }
  776. static int sde_encoder_phys_vid_wait_for_vblank(
  777. struct sde_encoder_phys *phys_enc)
  778. {
  779. return _sde_encoder_phys_vid_wait_for_vblank(phys_enc, true);
  780. }
  781. static int sde_encoder_phys_vid_wait_for_commit_done(
  782. struct sde_encoder_phys *phys_enc)
  783. {
  784. int rc;
  785. rc = _sde_encoder_phys_vid_wait_for_vblank(phys_enc, true);
  786. if (rc)
  787. sde_encoder_helper_phys_reset(phys_enc);
  788. return rc;
  789. }
  790. static int sde_encoder_phys_vid_wait_for_vblank_no_notify(
  791. struct sde_encoder_phys *phys_enc)
  792. {
  793. return _sde_encoder_phys_vid_wait_for_vblank(phys_enc, false);
  794. }
  795. static int sde_encoder_phys_vid_prepare_for_kickoff(
  796. struct sde_encoder_phys *phys_enc,
  797. struct sde_encoder_kickoff_params *params)
  798. {
  799. struct sde_encoder_phys_vid *vid_enc;
  800. struct sde_hw_ctl *ctl;
  801. bool recovery_events;
  802. struct drm_connector *conn;
  803. int rc;
  804. int irq_enable;
  805. if (!phys_enc || !params || !phys_enc->hw_ctl) {
  806. SDE_ERROR("invalid encoder/parameters\n");
  807. return -EINVAL;
  808. }
  809. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  810. ctl = phys_enc->hw_ctl;
  811. if (!ctl->ops.wait_reset_status)
  812. return 0;
  813. conn = phys_enc->connector;
  814. recovery_events = sde_encoder_recovery_events_enabled(
  815. phys_enc->parent);
  816. /*
  817. * hw supports hardware initiated ctl reset, so before we kickoff a new
  818. * frame, need to check and wait for hw initiated ctl reset completion
  819. */
  820. rc = ctl->ops.wait_reset_status(ctl);
  821. if (rc) {
  822. SDE_ERROR_VIDENC(vid_enc, "ctl %d reset failure: %d\n",
  823. ctl->idx, rc);
  824. ++vid_enc->error_count;
  825. /* to avoid flooding, only log first time, and "dead" time */
  826. if (vid_enc->error_count == 1) {
  827. SDE_EVT32(DRMID(phys_enc->parent), SDE_EVTLOG_FATAL);
  828. mutex_lock(phys_enc->vblank_ctl_lock);
  829. irq_enable = atomic_read(&phys_enc->vblank_refcount);
  830. if (irq_enable)
  831. sde_encoder_helper_unregister_irq(
  832. phys_enc, INTR_IDX_VSYNC);
  833. SDE_DBG_DUMP(SDE_DBG_BUILT_IN_ALL);
  834. if (irq_enable)
  835. sde_encoder_helper_register_irq(
  836. phys_enc, INTR_IDX_VSYNC);
  837. mutex_unlock(phys_enc->vblank_ctl_lock);
  838. }
  839. /*
  840. * if the recovery event is registered by user, don't panic
  841. * trigger panic on first timeout if no listener registered
  842. */
  843. if (recovery_events)
  844. sde_connector_event_notify(conn, DRM_EVENT_SDE_HW_RECOVERY,
  845. sizeof(uint8_t), SDE_RECOVERY_CAPTURE);
  846. else
  847. SDE_DBG_DUMP(0x0, "panic");
  848. /* request a ctl reset before the next flush */
  849. phys_enc->enable_state = SDE_ENC_ERR_NEEDS_HW_RESET;
  850. } else {
  851. if (recovery_events && vid_enc->error_count)
  852. sde_connector_event_notify(conn,
  853. DRM_EVENT_SDE_HW_RECOVERY,
  854. sizeof(uint8_t),
  855. SDE_RECOVERY_SUCCESS);
  856. vid_enc->error_count = 0;
  857. }
  858. return rc;
  859. }
  860. static void sde_encoder_phys_vid_single_vblank_wait(
  861. struct sde_encoder_phys *phys_enc)
  862. {
  863. int ret;
  864. struct sde_encoder_phys_vid *vid_enc
  865. = to_sde_encoder_phys_vid(phys_enc);
  866. /*
  867. * Wait for a vsync so we know the ENABLE=0 latched before
  868. * the (connector) source of the vsync's gets disabled,
  869. * otherwise we end up in a funny state if we re-enable
  870. * before the disable latches, which results that some of
  871. * the settings changes for the new modeset (like new
  872. * scanout buffer) don't latch properly..
  873. */
  874. ret = sde_encoder_phys_vid_control_vblank_irq(phys_enc, true);
  875. if (ret) {
  876. SDE_ERROR_VIDENC(vid_enc,
  877. "failed to enable vblank irq: %d\n",
  878. ret);
  879. SDE_EVT32(DRMID(phys_enc->parent),
  880. phys_enc->hw_intf->idx - INTF_0, ret,
  881. SDE_EVTLOG_FUNC_CASE1,
  882. SDE_EVTLOG_ERROR);
  883. } else {
  884. ret = _sde_encoder_phys_vid_wait_for_vblank(phys_enc, false);
  885. if (ret) {
  886. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  887. SDE_ERROR_VIDENC(vid_enc,
  888. "failure waiting for disable: %d\n",
  889. ret);
  890. SDE_EVT32(DRMID(phys_enc->parent),
  891. phys_enc->hw_intf->idx - INTF_0, ret,
  892. SDE_EVTLOG_FUNC_CASE2,
  893. SDE_EVTLOG_ERROR);
  894. }
  895. sde_encoder_phys_vid_control_vblank_irq(phys_enc, false);
  896. }
  897. }
  898. static void sde_encoder_phys_vid_disable(struct sde_encoder_phys *phys_enc)
  899. {
  900. struct msm_drm_private *priv;
  901. struct sde_encoder_phys_vid *vid_enc;
  902. unsigned long lock_flags;
  903. struct intf_status intf_status = {0};
  904. if (!phys_enc || !phys_enc->parent || !phys_enc->parent->dev ||
  905. !phys_enc->parent->dev->dev_private) {
  906. SDE_ERROR("invalid encoder/device\n");
  907. return;
  908. }
  909. priv = phys_enc->parent->dev->dev_private;
  910. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  911. if (!phys_enc->hw_intf || !phys_enc->hw_ctl) {
  912. SDE_ERROR("invalid hw_intf %d hw_ctl %d\n",
  913. !phys_enc->hw_intf, !phys_enc->hw_ctl);
  914. return;
  915. }
  916. SDE_DEBUG_VIDENC(vid_enc, "\n");
  917. if (WARN_ON(!phys_enc->hw_intf->ops.enable_timing))
  918. return;
  919. else if (!sde_encoder_phys_vid_is_master(phys_enc))
  920. goto exit;
  921. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  922. SDE_ERROR("already disabled\n");
  923. return;
  924. }
  925. if (sde_in_trusted_vm(phys_enc->sde_kms))
  926. goto exit;
  927. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  928. phys_enc->hw_intf->ops.enable_timing(phys_enc->hw_intf, 0);
  929. sde_encoder_phys_inc_pending(phys_enc);
  930. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  931. if (phys_enc->hw_intf->ops.reset_counter)
  932. phys_enc->hw_intf->ops.reset_counter(phys_enc->hw_intf);
  933. sde_encoder_phys_vid_single_vblank_wait(phys_enc);
  934. if (phys_enc->hw_intf->ops.get_status)
  935. phys_enc->hw_intf->ops.get_status(phys_enc->hw_intf,
  936. &intf_status);
  937. if (intf_status.is_en) {
  938. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  939. sde_encoder_phys_inc_pending(phys_enc);
  940. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  941. sde_encoder_phys_vid_single_vblank_wait(phys_enc);
  942. }
  943. sde_encoder_helper_phys_disable(phys_enc, NULL);
  944. exit:
  945. SDE_EVT32(DRMID(phys_enc->parent),
  946. atomic_read(&phys_enc->pending_retire_fence_cnt));
  947. phys_enc->vfp_cached = 0;
  948. phys_enc->enable_state = SDE_ENC_DISABLED;
  949. }
  950. static int sde_encoder_phys_vid_poll_for_active_region(struct sde_encoder_phys *phys_enc)
  951. {
  952. struct sde_encoder_phys_vid *vid_enc;
  953. struct intf_timing_params *timing;
  954. u32 line_cnt, v_inactive, poll_time_us, trial = 0;
  955. if (!phys_enc || !phys_enc->hw_intf || !phys_enc->hw_intf->ops.get_line_count)
  956. return -EINVAL;
  957. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  958. timing = &vid_enc->timing_params;
  959. /* if programmable fetch is not enabled return early */
  960. if (!programmable_fetch_get_num_lines(vid_enc, timing))
  961. return 0;
  962. poll_time_us = DIV_ROUND_UP(1000000, timing->vrefresh) / MAX_POLL_CNT;
  963. v_inactive = timing->v_front_porch + timing->v_back_porch + timing->vsync_pulse_width;
  964. do {
  965. usleep_range(poll_time_us, poll_time_us + 5);
  966. line_cnt = phys_enc->hw_intf->ops.get_line_count(phys_enc->hw_intf);
  967. trial++;
  968. } while ((trial < MAX_POLL_CNT) || (line_cnt < v_inactive));
  969. return (trial >= MAX_POLL_CNT) ? -ETIMEDOUT : 0;
  970. }
  971. static void sde_encoder_phys_vid_handle_post_kickoff(
  972. struct sde_encoder_phys *phys_enc)
  973. {
  974. unsigned long lock_flags;
  975. struct sde_encoder_phys_vid *vid_enc;
  976. u32 avr_mode;
  977. u32 ret;
  978. if (!phys_enc) {
  979. SDE_ERROR("invalid encoder\n");
  980. return;
  981. }
  982. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  983. SDE_DEBUG_VIDENC(vid_enc, "enable_state %d\n", phys_enc->enable_state);
  984. /*
  985. * Video mode must flush CTL before enabling timing engine
  986. * Video encoders need to turn on their interfaces now
  987. */
  988. if (phys_enc->enable_state == SDE_ENC_ENABLING) {
  989. if (sde_encoder_phys_vid_is_master(phys_enc)) {
  990. SDE_EVT32(DRMID(phys_enc->parent),
  991. phys_enc->hw_intf->idx - INTF_0);
  992. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  993. phys_enc->hw_intf->ops.enable_timing(phys_enc->hw_intf,
  994. 1);
  995. spin_unlock_irqrestore(phys_enc->enc_spinlock,
  996. lock_flags);
  997. ret = sde_encoder_phys_vid_poll_for_active_region(phys_enc);
  998. if (ret)
  999. SDE_DEBUG_VIDENC(vid_enc, "poll for active failed ret:%d\n", ret);
  1000. }
  1001. phys_enc->enable_state = SDE_ENC_ENABLED;
  1002. }
  1003. avr_mode = sde_connector_get_qsync_mode(phys_enc->connector);
  1004. if (avr_mode && vid_enc->base.hw_intf->ops.avr_trigger) {
  1005. vid_enc->base.hw_intf->ops.avr_trigger(vid_enc->base.hw_intf);
  1006. SDE_EVT32(DRMID(phys_enc->parent),
  1007. phys_enc->hw_intf->idx - INTF_0,
  1008. SDE_EVTLOG_FUNC_CASE9);
  1009. }
  1010. }
  1011. static void sde_encoder_phys_vid_prepare_for_commit(
  1012. struct sde_encoder_phys *phys_enc)
  1013. {
  1014. struct sde_connector_state *c_state;
  1015. if (!phys_enc || !phys_enc->parent) {
  1016. SDE_ERROR("invalid encoder parameters\n");
  1017. return;
  1018. }
  1019. if (phys_enc->connector && phys_enc->connector->state) {
  1020. c_state = to_sde_connector_state(phys_enc->connector->state);
  1021. if (!c_state) {
  1022. SDE_ERROR("invalid connector state\n");
  1023. return;
  1024. }
  1025. if (!msm_is_mode_seamless_vrr(&c_state->msm_mode)
  1026. && sde_connector_is_qsync_updated(phys_enc->connector))
  1027. _sde_encoder_phys_vid_avr_ctrl(phys_enc);
  1028. }
  1029. }
  1030. static void sde_encoder_phys_vid_irq_control(struct sde_encoder_phys *phys_enc,
  1031. bool enable)
  1032. {
  1033. struct sde_encoder_phys_vid *vid_enc;
  1034. int ret;
  1035. if (!phys_enc)
  1036. return;
  1037. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  1038. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_intf->idx - INTF_0,
  1039. enable, atomic_read(&phys_enc->vblank_refcount));
  1040. if (enable) {
  1041. ret = sde_encoder_phys_vid_control_vblank_irq(phys_enc, true);
  1042. if (ret)
  1043. return;
  1044. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_UNDERRUN);
  1045. } else {
  1046. sde_encoder_phys_vid_control_vblank_irq(phys_enc, false);
  1047. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_UNDERRUN);
  1048. }
  1049. }
  1050. static int sde_encoder_phys_vid_get_line_count(
  1051. struct sde_encoder_phys *phys_enc)
  1052. {
  1053. if (!phys_enc)
  1054. return -EINVAL;
  1055. if (!sde_encoder_phys_vid_is_master(phys_enc))
  1056. return -EINVAL;
  1057. if (!phys_enc->hw_intf || !phys_enc->hw_intf->ops.get_line_count)
  1058. return -EINVAL;
  1059. return phys_enc->hw_intf->ops.get_line_count(phys_enc->hw_intf);
  1060. }
  1061. static u32 sde_encoder_phys_vid_get_underrun_line_count(
  1062. struct sde_encoder_phys *phys_enc)
  1063. {
  1064. u32 underrun_linecount = 0xebadebad;
  1065. u32 intf_intr_status = 0xebadebad;
  1066. struct intf_status intf_status = {0};
  1067. if (!phys_enc)
  1068. return -EINVAL;
  1069. if (!sde_encoder_phys_vid_is_master(phys_enc) || !phys_enc->hw_intf)
  1070. return -EINVAL;
  1071. if (phys_enc->hw_intf->ops.get_status)
  1072. phys_enc->hw_intf->ops.get_status(phys_enc->hw_intf,
  1073. &intf_status);
  1074. if (phys_enc->hw_intf->ops.get_underrun_line_count)
  1075. underrun_linecount =
  1076. phys_enc->hw_intf->ops.get_underrun_line_count(
  1077. phys_enc->hw_intf);
  1078. if (phys_enc->hw_intf->ops.get_intr_status)
  1079. intf_intr_status = phys_enc->hw_intf->ops.get_intr_status(
  1080. phys_enc->hw_intf);
  1081. SDE_EVT32(DRMID(phys_enc->parent), underrun_linecount,
  1082. intf_status.frame_count, intf_status.line_count,
  1083. intf_intr_status);
  1084. return underrun_linecount;
  1085. }
  1086. static int sde_encoder_phys_vid_wait_for_active(
  1087. struct sde_encoder_phys *phys_enc)
  1088. {
  1089. struct drm_display_mode mode;
  1090. struct sde_encoder_phys_vid *vid_enc;
  1091. u32 ln_cnt, min_ln_cnt, active_lns_cnt;
  1092. u32 retry = MAX_POLL_CNT;
  1093. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  1094. if (!phys_enc->hw_intf || !phys_enc->hw_intf->ops.get_line_count) {
  1095. SDE_ERROR_VIDENC(vid_enc, "invalid vid_enc params\n");
  1096. return -EINVAL;
  1097. }
  1098. mode = phys_enc->cached_mode;
  1099. min_ln_cnt = (mode.vtotal - mode.vsync_start) +
  1100. (mode.vsync_end - mode.vsync_start);
  1101. active_lns_cnt = mode.vdisplay;
  1102. while (retry) {
  1103. ln_cnt = phys_enc->hw_intf->ops.get_line_count(
  1104. phys_enc->hw_intf);
  1105. if ((ln_cnt >= min_ln_cnt) &&
  1106. (ln_cnt < (active_lns_cnt + min_ln_cnt))) {
  1107. SDE_DEBUG_VIDENC(vid_enc,
  1108. "Needed lines left line_cnt=%d\n",
  1109. ln_cnt);
  1110. return 0;
  1111. }
  1112. SDE_ERROR_VIDENC(vid_enc, "line count is less. line_cnt = %d\n", ln_cnt);
  1113. udelay(POLL_TIME_USEC_FOR_LN_CNT);
  1114. retry--;
  1115. }
  1116. return -EINVAL;
  1117. }
  1118. void sde_encoder_phys_vid_add_enc_to_minidump(struct sde_encoder_phys *phys_enc)
  1119. {
  1120. struct sde_encoder_phys_vid *vid_enc;
  1121. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  1122. sde_mini_dump_add_va_region("sde_enc_phys_vid", sizeof(*vid_enc), vid_enc);
  1123. }
  1124. static void sde_encoder_phys_vid_init_ops(struct sde_encoder_phys_ops *ops)
  1125. {
  1126. ops->is_master = sde_encoder_phys_vid_is_master;
  1127. ops->mode_set = sde_encoder_phys_vid_mode_set;
  1128. ops->cont_splash_mode_set = sde_encoder_phys_vid_cont_splash_mode_set;
  1129. ops->mode_fixup = sde_encoder_phys_vid_mode_fixup;
  1130. ops->enable = sde_encoder_phys_vid_enable;
  1131. ops->disable = sde_encoder_phys_vid_disable;
  1132. ops->destroy = sde_encoder_phys_vid_destroy;
  1133. ops->get_hw_resources = sde_encoder_phys_vid_get_hw_resources;
  1134. ops->control_vblank_irq = sde_encoder_phys_vid_control_vblank_irq;
  1135. ops->wait_for_commit_done = sde_encoder_phys_vid_wait_for_commit_done;
  1136. ops->wait_for_vblank = sde_encoder_phys_vid_wait_for_vblank_no_notify;
  1137. ops->wait_for_tx_complete = sde_encoder_phys_vid_wait_for_vblank;
  1138. ops->irq_control = sde_encoder_phys_vid_irq_control;
  1139. ops->prepare_for_kickoff = sde_encoder_phys_vid_prepare_for_kickoff;
  1140. ops->handle_post_kickoff = sde_encoder_phys_vid_handle_post_kickoff;
  1141. ops->needs_single_flush = sde_encoder_phys_needs_single_flush;
  1142. ops->setup_misr = sde_encoder_helper_setup_misr;
  1143. ops->collect_misr = sde_encoder_helper_collect_misr;
  1144. ops->trigger_flush = sde_encoder_helper_trigger_flush;
  1145. ops->hw_reset = sde_encoder_helper_hw_reset;
  1146. ops->get_line_count = sde_encoder_phys_vid_get_line_count;
  1147. ops->wait_dma_trigger = sde_encoder_phys_vid_wait_dma_trigger;
  1148. ops->wait_for_active = sde_encoder_phys_vid_wait_for_active;
  1149. ops->prepare_commit = sde_encoder_phys_vid_prepare_for_commit;
  1150. ops->get_underrun_line_count =
  1151. sde_encoder_phys_vid_get_underrun_line_count;
  1152. ops->add_to_minidump = sde_encoder_phys_vid_add_enc_to_minidump;
  1153. }
  1154. struct sde_encoder_phys *sde_encoder_phys_vid_init(
  1155. struct sde_enc_phys_init_params *p)
  1156. {
  1157. struct sde_encoder_phys *phys_enc = NULL;
  1158. struct sde_encoder_phys_vid *vid_enc = NULL;
  1159. struct sde_hw_mdp *hw_mdp;
  1160. struct sde_encoder_irq *irq;
  1161. int i, ret = 0;
  1162. if (!p) {
  1163. ret = -EINVAL;
  1164. goto fail;
  1165. }
  1166. vid_enc = kzalloc(sizeof(*vid_enc), GFP_KERNEL);
  1167. if (!vid_enc) {
  1168. ret = -ENOMEM;
  1169. goto fail;
  1170. }
  1171. phys_enc = &vid_enc->base;
  1172. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  1173. if (IS_ERR_OR_NULL(hw_mdp)) {
  1174. ret = PTR_ERR(hw_mdp);
  1175. SDE_ERROR("failed to get mdptop\n");
  1176. goto fail;
  1177. }
  1178. phys_enc->hw_mdptop = hw_mdp;
  1179. phys_enc->intf_idx = p->intf_idx;
  1180. SDE_DEBUG_VIDENC(vid_enc, "\n");
  1181. sde_encoder_phys_vid_init_ops(&phys_enc->ops);
  1182. phys_enc->parent = p->parent;
  1183. phys_enc->parent_ops = p->parent_ops;
  1184. phys_enc->sde_kms = p->sde_kms;
  1185. phys_enc->split_role = p->split_role;
  1186. phys_enc->intf_mode = INTF_MODE_VIDEO;
  1187. phys_enc->enc_spinlock = p->enc_spinlock;
  1188. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  1189. phys_enc->comp_type = p->comp_type;
  1190. phys_enc->kickoff_timeout_ms = DEFAULT_KICKOFF_TIMEOUT_MS;
  1191. for (i = 0; i < INTR_IDX_MAX; i++) {
  1192. irq = &phys_enc->irq[i];
  1193. INIT_LIST_HEAD(&irq->cb.list);
  1194. irq->irq_idx = -EINVAL;
  1195. irq->hw_idx = -EINVAL;
  1196. irq->cb.arg = phys_enc;
  1197. }
  1198. irq = &phys_enc->irq[INTR_IDX_VSYNC];
  1199. irq->name = "vsync_irq";
  1200. irq->intr_type = SDE_IRQ_TYPE_INTF_VSYNC;
  1201. irq->intr_idx = INTR_IDX_VSYNC;
  1202. irq->cb.func = sde_encoder_phys_vid_vblank_irq;
  1203. irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
  1204. irq->name = "underrun";
  1205. irq->intr_type = SDE_IRQ_TYPE_INTF_UNDER_RUN;
  1206. irq->intr_idx = INTR_IDX_UNDERRUN;
  1207. irq->cb.func = sde_encoder_phys_vid_underrun_irq;
  1208. atomic_set(&phys_enc->vblank_refcount, 0);
  1209. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1210. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  1211. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  1212. phys_enc->enable_state = SDE_ENC_DISABLED;
  1213. SDE_DEBUG_VIDENC(vid_enc, "created intf idx:%d\n", p->intf_idx);
  1214. return phys_enc;
  1215. fail:
  1216. SDE_ERROR("failed to create encoder\n");
  1217. if (vid_enc)
  1218. sde_encoder_phys_vid_destroy(phys_enc);
  1219. return ERR_PTR(ret);
  1220. }