sde_encoder_phys_vid.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
  6. #include "sde_encoder_phys.h"
  7. #include "sde_hw_interrupts.h"
  8. #include "sde_core_irq.h"
  9. #include "sde_formats.h"
  10. #include "dsi_display.h"
  11. #include "sde_trace.h"
  12. #define SDE_DEBUG_VIDENC(e, fmt, ...) SDE_DEBUG("enc%d intf%d " fmt, \
  13. (e) && (e)->base.parent ? \
  14. (e)->base.parent->base.id : -1, \
  15. (e) && (e)->base.hw_intf ? \
  16. (e)->base.hw_intf->idx - INTF_0 : -1, ##__VA_ARGS__)
  17. #define SDE_ERROR_VIDENC(e, fmt, ...) SDE_ERROR("enc%d intf%d " fmt, \
  18. (e) && (e)->base.parent ? \
  19. (e)->base.parent->base.id : -1, \
  20. (e) && (e)->base.hw_intf ? \
  21. (e)->base.hw_intf->idx - INTF_0 : -1, ##__VA_ARGS__)
  22. #define to_sde_encoder_phys_vid(x) \
  23. container_of(x, struct sde_encoder_phys_vid, base)
  24. /* maximum number of consecutive kickoff errors */
  25. #define KICKOFF_MAX_ERRORS 2
  26. /* Poll time to do recovery during active region */
  27. #define POLL_TIME_USEC_FOR_LN_CNT 500
  28. #define MAX_POLL_CNT 10
  29. static bool sde_encoder_phys_vid_is_master(
  30. struct sde_encoder_phys *phys_enc)
  31. {
  32. bool ret = false;
  33. if (phys_enc->split_role != ENC_ROLE_SLAVE)
  34. ret = true;
  35. return ret;
  36. }
  37. static void drm_mode_to_intf_timing_params(
  38. const struct sde_encoder_phys_vid *vid_enc,
  39. const struct drm_display_mode *mode,
  40. struct intf_timing_params *timing)
  41. {
  42. const struct sde_encoder_phys *phys_enc = &vid_enc->base;
  43. memset(timing, 0, sizeof(*timing));
  44. if ((mode->htotal < mode->hsync_end)
  45. || (mode->hsync_start < mode->hdisplay)
  46. || (mode->vtotal < mode->vsync_end)
  47. || (mode->vsync_start < mode->vdisplay)
  48. || (mode->hsync_end < mode->hsync_start)
  49. || (mode->vsync_end < mode->vsync_start)) {
  50. SDE_ERROR(
  51. "invalid params - hstart:%d,hend:%d,htot:%d,hdisplay:%d\n",
  52. mode->hsync_start, mode->hsync_end,
  53. mode->htotal, mode->hdisplay);
  54. SDE_ERROR("vstart:%d,vend:%d,vtot:%d,vdisplay:%d\n",
  55. mode->vsync_start, mode->vsync_end,
  56. mode->vtotal, mode->vdisplay);
  57. return;
  58. }
  59. /*
  60. * https://www.kernel.org/doc/htmldocs/drm/ch02s05.html
  61. * Active Region Front Porch Sync Back Porch
  62. * <-----------------><------------><-----><----------->
  63. * <- [hv]display --->
  64. * <--------- [hv]sync_start ------>
  65. * <----------------- [hv]sync_end ------->
  66. * <---------------------------- [hv]total ------------->
  67. */
  68. timing->poms_align_vsync = phys_enc->poms_align_vsync;
  69. timing->width = mode->hdisplay; /* active width */
  70. timing->height = mode->vdisplay; /* active height */
  71. timing->xres = timing->width;
  72. timing->yres = timing->height;
  73. timing->h_back_porch = mode->htotal - mode->hsync_end;
  74. timing->h_front_porch = mode->hsync_start - mode->hdisplay;
  75. timing->v_back_porch = mode->vtotal - mode->vsync_end;
  76. timing->v_front_porch = mode->vsync_start - mode->vdisplay;
  77. timing->hsync_pulse_width = mode->hsync_end - mode->hsync_start;
  78. timing->vsync_pulse_width = mode->vsync_end - mode->vsync_start;
  79. timing->hsync_polarity = (mode->flags & DRM_MODE_FLAG_NHSYNC) ? 1 : 0;
  80. timing->vsync_polarity = (mode->flags & DRM_MODE_FLAG_NVSYNC) ? 1 : 0;
  81. timing->border_clr = 0;
  82. timing->underflow_clr = 0xff;
  83. timing->hsync_skew = mode->hskew;
  84. timing->v_front_porch_fixed = vid_enc->base.vfp_cached;
  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. bool use_fixed_vfp)
  162. {
  163. u32 inactive;
  164. u32 active = timing->yres;
  165. u32 v_front_porch = use_fixed_vfp ?
  166. timing->v_front_porch_fixed : timing->v_front_porch;
  167. inactive = timing->v_back_porch + v_front_porch +
  168. timing->vsync_pulse_width;
  169. return active + inactive;
  170. }
  171. /*
  172. * programmable_fetch_get_num_lines:
  173. * Number of fetch lines in vertical front porch
  174. * @timing: Pointer to the intf timing information for the requested mode
  175. *
  176. * Returns the number of fetch lines in vertical front porch at which mdp
  177. * can start fetching the next frame.
  178. *
  179. * Number of needed prefetch lines is anything that cannot be absorbed in the
  180. * start of frame time (back porch + vsync pulse width).
  181. *
  182. * Some panels have very large VFP, however we only need a total number of
  183. * lines based on the chip worst case latencies.
  184. */
  185. static u32 programmable_fetch_get_num_lines(
  186. struct sde_encoder_phys_vid *vid_enc,
  187. const struct intf_timing_params *timing,
  188. bool use_fixed_vfp)
  189. {
  190. struct sde_encoder_phys *phys_enc = &vid_enc->base;
  191. u32 worst_case_needed_lines =
  192. phys_enc->hw_intf->cap->prog_fetch_lines_worst_case;
  193. u32 start_of_frame_lines =
  194. timing->v_back_porch + timing->vsync_pulse_width;
  195. u32 needed_vfp_lines = worst_case_needed_lines - start_of_frame_lines;
  196. u32 actual_vfp_lines = 0;
  197. u32 v_front_porch = use_fixed_vfp ?
  198. timing->v_front_porch_fixed : timing->v_front_porch;
  199. /* Fetch must be outside active lines, otherwise undefined. */
  200. if (start_of_frame_lines >= worst_case_needed_lines) {
  201. SDE_DEBUG_VIDENC(vid_enc,
  202. "prog fetch is not needed, large vbp+vsw\n");
  203. actual_vfp_lines = 0;
  204. } else if (v_front_porch < needed_vfp_lines) {
  205. /* Warn fetch needed, but not enough porch in panel config */
  206. pr_warn_once
  207. ("low vbp+vfp may lead to perf issues in some cases\n");
  208. SDE_DEBUG_VIDENC(vid_enc,
  209. "less vfp than fetch req, using entire vfp\n");
  210. actual_vfp_lines = v_front_porch;
  211. } else {
  212. SDE_DEBUG_VIDENC(vid_enc, "room in vfp for needed prefetch\n");
  213. actual_vfp_lines = needed_vfp_lines;
  214. }
  215. SDE_DEBUG_VIDENC(vid_enc,
  216. "v_front_porch %u v_back_porch %u vsync_pulse_width %u\n",
  217. v_front_porch, timing->v_back_porch,
  218. timing->vsync_pulse_width);
  219. SDE_DEBUG_VIDENC(vid_enc,
  220. "wc_lines %u needed_vfp_lines %u actual_vfp_lines %u\n",
  221. worst_case_needed_lines, needed_vfp_lines, actual_vfp_lines);
  222. return actual_vfp_lines;
  223. }
  224. /*
  225. * programmable_fetch_config: Programs HW to prefetch lines by offsetting
  226. * the start of fetch into the vertical front porch for cases where the
  227. * vsync pulse width and vertical back porch time is insufficient
  228. *
  229. * Gets # of lines to pre-fetch, then calculate VSYNC counter value.
  230. * HW layer requires VSYNC counter of first pixel of tgt VFP line.
  231. *
  232. * @timing: Pointer to the intf timing information for the requested mode
  233. */
  234. static void programmable_fetch_config(struct sde_encoder_phys *phys_enc,
  235. const struct intf_timing_params *timing)
  236. {
  237. struct sde_encoder_phys_vid *vid_enc =
  238. to_sde_encoder_phys_vid(phys_enc);
  239. struct intf_prog_fetch f = { 0 };
  240. u32 vfp_fetch_lines = 0;
  241. u32 horiz_total = 0;
  242. u32 vert_total = 0;
  243. u32 vfp_fetch_start_vsync_counter = 0;
  244. unsigned long lock_flags;
  245. struct sde_mdss_cfg *m;
  246. if (WARN_ON_ONCE(!phys_enc->hw_intf->ops.setup_prg_fetch))
  247. return;
  248. m = phys_enc->sde_kms->catalog;
  249. vfp_fetch_lines = programmable_fetch_get_num_lines(vid_enc,
  250. timing, true);
  251. if (vfp_fetch_lines) {
  252. vert_total = get_vertical_total(timing, true);
  253. horiz_total = get_horizontal_total(timing);
  254. vfp_fetch_start_vsync_counter =
  255. (vert_total - vfp_fetch_lines) * horiz_total + 1;
  256. /**
  257. * Check if we need to throttle the fetch to start
  258. * from second line after the active region.
  259. */
  260. if (m->delay_prg_fetch_start)
  261. vfp_fetch_start_vsync_counter += horiz_total;
  262. f.enable = 1;
  263. f.fetch_start = vfp_fetch_start_vsync_counter;
  264. }
  265. SDE_DEBUG_VIDENC(vid_enc,
  266. "vfp_fetch_lines %u vfp_fetch_start_vsync_counter %u\n",
  267. vfp_fetch_lines, vfp_fetch_start_vsync_counter);
  268. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  269. phys_enc->hw_intf->ops.setup_prg_fetch(phys_enc->hw_intf, &f);
  270. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  271. }
  272. static bool sde_encoder_phys_vid_mode_fixup(
  273. struct sde_encoder_phys *phys_enc,
  274. const struct drm_display_mode *mode,
  275. struct drm_display_mode *adj_mode)
  276. {
  277. if (phys_enc)
  278. SDE_DEBUG_VIDENC(to_sde_encoder_phys_vid(phys_enc), "\n");
  279. /*
  280. * Modifying mode has consequences when the mode comes back to us
  281. */
  282. return true;
  283. }
  284. /* vid_enc timing_params must be configured before calling this function */
  285. static void _sde_encoder_phys_vid_setup_avr(
  286. struct sde_encoder_phys *phys_enc, u32 qsync_min_fps)
  287. {
  288. struct sde_encoder_phys_vid *vid_enc;
  289. struct drm_display_mode mode;
  290. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  291. mode = phys_enc->cached_mode;
  292. if (vid_enc->base.hw_intf->ops.avr_setup) {
  293. struct intf_avr_params avr_params = {0};
  294. u32 default_fps = mode.vrefresh;
  295. int ret;
  296. if (!default_fps) {
  297. SDE_ERROR_VIDENC(vid_enc,
  298. "invalid default fps %d\n",
  299. default_fps);
  300. return;
  301. }
  302. if (qsync_min_fps > default_fps) {
  303. SDE_ERROR_VIDENC(vid_enc,
  304. "qsync fps %d must be less than default %d\n",
  305. qsync_min_fps, default_fps);
  306. return;
  307. }
  308. avr_params.default_fps = default_fps;
  309. avr_params.min_fps = qsync_min_fps;
  310. ret = vid_enc->base.hw_intf->ops.avr_setup(
  311. vid_enc->base.hw_intf,
  312. &vid_enc->timing_params, &avr_params);
  313. if (ret)
  314. SDE_ERROR_VIDENC(vid_enc,
  315. "bad settings, can't configure AVR\n");
  316. SDE_EVT32(DRMID(phys_enc->parent), default_fps,
  317. qsync_min_fps, ret);
  318. }
  319. }
  320. static void _sde_encoder_phys_vid_avr_ctrl(struct sde_encoder_phys *phys_enc)
  321. {
  322. struct intf_avr_params avr_params;
  323. struct sde_encoder_phys_vid *vid_enc =
  324. to_sde_encoder_phys_vid(phys_enc);
  325. avr_params.avr_mode = sde_connector_get_qsync_mode(
  326. phys_enc->connector);
  327. if (vid_enc->base.hw_intf->ops.avr_ctrl) {
  328. vid_enc->base.hw_intf->ops.avr_ctrl(
  329. vid_enc->base.hw_intf,
  330. &avr_params);
  331. }
  332. SDE_EVT32(DRMID(phys_enc->parent),
  333. phys_enc->hw_intf->idx - INTF_0,
  334. avr_params.avr_mode);
  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) {
  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);
  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);
  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)
  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. phys_enc->hw_ctl = (struct sde_hw_ctl *)iter.hw;
  539. }
  540. if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
  541. SDE_ERROR_VIDENC(vid_enc, "failed to init ctl, %ld\n",
  542. PTR_ERR(phys_enc->hw_ctl));
  543. phys_enc->hw_ctl = NULL;
  544. return;
  545. }
  546. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_INTF);
  547. for (i = 0; i <= instance; i++) {
  548. if (sde_rm_get_hw(rm, &iter))
  549. phys_enc->hw_intf = (struct sde_hw_intf *)iter.hw;
  550. }
  551. if (IS_ERR_OR_NULL(phys_enc->hw_intf)) {
  552. SDE_ERROR_VIDENC(vid_enc, "failed to init intf: %ld\n",
  553. PTR_ERR(phys_enc->hw_intf));
  554. phys_enc->hw_intf = NULL;
  555. return;
  556. }
  557. _sde_encoder_phys_vid_setup_irq_hw_idx(phys_enc);
  558. }
  559. static int sde_encoder_phys_vid_control_vblank_irq(
  560. struct sde_encoder_phys *phys_enc,
  561. bool enable)
  562. {
  563. int ret = 0;
  564. struct sde_encoder_phys_vid *vid_enc;
  565. int refcount;
  566. if (!phys_enc) {
  567. SDE_ERROR("invalid encoder\n");
  568. return -EINVAL;
  569. }
  570. mutex_lock(phys_enc->vblank_ctl_lock);
  571. refcount = atomic_read(&phys_enc->vblank_refcount);
  572. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  573. /* Slave encoders don't report vblank */
  574. if (!sde_encoder_phys_vid_is_master(phys_enc))
  575. goto end;
  576. /* protect against negative */
  577. if (!enable && refcount == 0) {
  578. ret = -EINVAL;
  579. goto end;
  580. }
  581. SDE_DEBUG_VIDENC(vid_enc, "[%pS] enable=%d/%d\n",
  582. __builtin_return_address(0),
  583. enable, atomic_read(&phys_enc->vblank_refcount));
  584. SDE_EVT32(DRMID(phys_enc->parent), enable,
  585. atomic_read(&phys_enc->vblank_refcount));
  586. if (enable && atomic_inc_return(&phys_enc->vblank_refcount) == 1) {
  587. ret = sde_encoder_helper_register_irq(phys_enc, INTR_IDX_VSYNC);
  588. if (ret)
  589. atomic_dec_return(&phys_enc->vblank_refcount);
  590. } else if (!enable &&
  591. atomic_dec_return(&phys_enc->vblank_refcount) == 0) {
  592. ret = sde_encoder_helper_unregister_irq(phys_enc,
  593. INTR_IDX_VSYNC);
  594. if (ret)
  595. atomic_inc_return(&phys_enc->vblank_refcount);
  596. }
  597. end:
  598. if (ret) {
  599. SDE_ERROR_VIDENC(vid_enc,
  600. "control vblank irq error %d, enable %d\n",
  601. ret, enable);
  602. SDE_EVT32(DRMID(phys_enc->parent),
  603. phys_enc->hw_intf->idx - INTF_0,
  604. enable, refcount, SDE_EVTLOG_ERROR);
  605. }
  606. mutex_unlock(phys_enc->vblank_ctl_lock);
  607. return ret;
  608. }
  609. static bool sde_encoder_phys_vid_wait_dma_trigger(
  610. struct sde_encoder_phys *phys_enc)
  611. {
  612. struct sde_encoder_phys_vid *vid_enc;
  613. struct sde_hw_intf *intf;
  614. struct sde_hw_ctl *ctl;
  615. struct intf_status status;
  616. if (!phys_enc) {
  617. SDE_ERROR("invalid encoder\n");
  618. return false;
  619. }
  620. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  621. intf = phys_enc->hw_intf;
  622. ctl = phys_enc->hw_ctl;
  623. if (!phys_enc->hw_intf || !phys_enc->hw_ctl) {
  624. SDE_ERROR("invalid hw_intf %d hw_ctl %d\n",
  625. phys_enc->hw_intf != NULL, phys_enc->hw_ctl != NULL);
  626. return false;
  627. }
  628. if (!intf->ops.get_status)
  629. return false;
  630. intf->ops.get_status(intf, &status);
  631. /* if interface is not enabled, return true to wait for dma trigger */
  632. return status.is_en ? false : true;
  633. }
  634. static void sde_encoder_phys_vid_enable(struct sde_encoder_phys *phys_enc)
  635. {
  636. struct msm_drm_private *priv;
  637. struct sde_encoder_phys_vid *vid_enc;
  638. struct sde_hw_intf *intf;
  639. struct sde_hw_ctl *ctl;
  640. if (!phys_enc || !phys_enc->parent || !phys_enc->parent->dev ||
  641. !phys_enc->parent->dev->dev_private ||
  642. !phys_enc->sde_kms) {
  643. SDE_ERROR("invalid encoder/device\n");
  644. return;
  645. }
  646. priv = phys_enc->parent->dev->dev_private;
  647. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  648. intf = phys_enc->hw_intf;
  649. ctl = phys_enc->hw_ctl;
  650. if (!phys_enc->hw_intf || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
  651. SDE_ERROR("invalid hw_intf %d hw_ctl %d hw_pp %d\n",
  652. !phys_enc->hw_intf, !phys_enc->hw_ctl,
  653. !phys_enc->hw_pp);
  654. return;
  655. }
  656. if (!ctl->ops.update_bitmask) {
  657. SDE_ERROR("invalid hw_ctl ops %d\n", ctl->idx);
  658. return;
  659. }
  660. SDE_DEBUG_VIDENC(vid_enc, "\n");
  661. if (WARN_ON(!phys_enc->hw_intf->ops.enable_timing))
  662. return;
  663. if (!phys_enc->cont_splash_enabled)
  664. sde_encoder_helper_split_config(phys_enc,
  665. phys_enc->hw_intf->idx);
  666. sde_encoder_phys_vid_setup_timing_engine(phys_enc);
  667. /*
  668. * For cases where both the interfaces are connected to same ctl,
  669. * set the flush bit for both master and slave.
  670. * For single flush cases (dual-ctl or pp-split), skip setting the
  671. * flush bit for the slave intf, since both intfs use same ctl
  672. * and HW will only flush the master.
  673. */
  674. if (!test_bit(SDE_CTL_ACTIVE_CFG, &ctl->caps->features) &&
  675. sde_encoder_phys_needs_single_flush(phys_enc) &&
  676. !sde_encoder_phys_vid_is_master(phys_enc))
  677. goto skip_flush;
  678. /**
  679. * skip flushing intf during cont. splash handoff since bootloader
  680. * has already enabled the hardware and is single buffered.
  681. */
  682. if (phys_enc->cont_splash_enabled) {
  683. SDE_DEBUG_VIDENC(vid_enc,
  684. "skipping intf flush bit set as cont. splash is enabled\n");
  685. goto skip_flush;
  686. }
  687. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_INTF, intf->idx, 1);
  688. if (phys_enc->hw_pp->merge_3d)
  689. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_MERGE_3D,
  690. phys_enc->hw_pp->merge_3d->idx, 1);
  691. if (phys_enc->hw_intf->cap->type == INTF_DP &&
  692. phys_enc->comp_type == MSM_DISPLAY_COMPRESSION_DSC &&
  693. phys_enc->comp_ratio)
  694. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_PERIPH, intf->idx, 1);
  695. skip_flush:
  696. SDE_DEBUG_VIDENC(vid_enc, "update pending flush ctl %d intf %d\n",
  697. ctl->idx - CTL_0, intf->idx);
  698. SDE_EVT32(DRMID(phys_enc->parent),
  699. atomic_read(&phys_enc->pending_retire_fence_cnt));
  700. /* ctl_flush & timing engine enable will be triggered by framework */
  701. if (phys_enc->enable_state == SDE_ENC_DISABLED)
  702. phys_enc->enable_state = SDE_ENC_ENABLING;
  703. }
  704. static void sde_encoder_phys_vid_destroy(struct sde_encoder_phys *phys_enc)
  705. {
  706. struct sde_encoder_phys_vid *vid_enc;
  707. if (!phys_enc) {
  708. SDE_ERROR("invalid encoder\n");
  709. return;
  710. }
  711. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  712. SDE_DEBUG_VIDENC(vid_enc, "\n");
  713. kfree(vid_enc);
  714. }
  715. static void sde_encoder_phys_vid_get_hw_resources(
  716. struct sde_encoder_phys *phys_enc,
  717. struct sde_encoder_hw_resources *hw_res,
  718. struct drm_connector_state *conn_state)
  719. {
  720. struct sde_encoder_phys_vid *vid_enc;
  721. if (!phys_enc || !hw_res) {
  722. SDE_ERROR("invalid arg(s), enc %d hw_res %d conn_state %d\n",
  723. !phys_enc, !hw_res, !conn_state);
  724. return;
  725. }
  726. if ((phys_enc->intf_idx - INTF_0) >= INTF_MAX) {
  727. SDE_ERROR("invalid intf idx:%d\n", phys_enc->intf_idx);
  728. return;
  729. }
  730. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  731. SDE_DEBUG_VIDENC(vid_enc, "\n");
  732. hw_res->intfs[phys_enc->intf_idx - INTF_0] = INTF_MODE_VIDEO;
  733. }
  734. static int _sde_encoder_phys_vid_wait_for_vblank(
  735. struct sde_encoder_phys *phys_enc, bool notify)
  736. {
  737. struct sde_encoder_wait_info wait_info = {0};
  738. int ret = 0;
  739. u32 event = SDE_ENCODER_FRAME_EVENT_ERROR |
  740. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE |
  741. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  742. if (!phys_enc) {
  743. pr_err("invalid encoder\n");
  744. return -EINVAL;
  745. }
  746. wait_info.wq = &phys_enc->pending_kickoff_wq;
  747. wait_info.atomic_cnt = &phys_enc->pending_kickoff_cnt;
  748. wait_info.timeout_ms = KICKOFF_TIMEOUT_MS;
  749. /* Wait for kickoff to complete */
  750. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_VSYNC,
  751. &wait_info);
  752. if (notify && (ret == -ETIMEDOUT) &&
  753. atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0) &&
  754. phys_enc->parent_ops.handle_frame_done)
  755. phys_enc->parent_ops.handle_frame_done(
  756. phys_enc->parent, phys_enc, event);
  757. SDE_EVT32(DRMID(phys_enc->parent), event, notify, ret,
  758. ret ? SDE_EVTLOG_FATAL : 0);
  759. return ret;
  760. }
  761. static int sde_encoder_phys_vid_wait_for_vblank(
  762. struct sde_encoder_phys *phys_enc)
  763. {
  764. return _sde_encoder_phys_vid_wait_for_vblank(phys_enc, true);
  765. }
  766. static int sde_encoder_phys_vid_wait_for_vblank_no_notify(
  767. struct sde_encoder_phys *phys_enc)
  768. {
  769. return _sde_encoder_phys_vid_wait_for_vblank(phys_enc, false);
  770. }
  771. static int sde_encoder_phys_vid_prepare_for_kickoff(
  772. struct sde_encoder_phys *phys_enc,
  773. struct sde_encoder_kickoff_params *params)
  774. {
  775. struct sde_encoder_phys_vid *vid_enc;
  776. struct sde_hw_ctl *ctl;
  777. bool recovery_events;
  778. struct drm_connector *conn;
  779. int event;
  780. int rc;
  781. if (!phys_enc || !params || !phys_enc->hw_ctl) {
  782. SDE_ERROR("invalid encoder/parameters\n");
  783. return -EINVAL;
  784. }
  785. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  786. ctl = phys_enc->hw_ctl;
  787. if (!ctl->ops.wait_reset_status)
  788. return 0;
  789. conn = phys_enc->connector;
  790. recovery_events = sde_encoder_recovery_events_enabled(
  791. phys_enc->parent);
  792. /*
  793. * hw supports hardware initiated ctl reset, so before we kickoff a new
  794. * frame, need to check and wait for hw initiated ctl reset completion
  795. */
  796. rc = ctl->ops.wait_reset_status(ctl);
  797. if (rc) {
  798. SDE_ERROR_VIDENC(vid_enc, "ctl %d reset failure: %d\n",
  799. ctl->idx, rc);
  800. ++vid_enc->error_count;
  801. /* to avoid flooding, only log first time, and "dead" time */
  802. if (vid_enc->error_count == 1) {
  803. SDE_EVT32(DRMID(phys_enc->parent), SDE_EVTLOG_FATAL);
  804. sde_encoder_helper_unregister_irq(
  805. phys_enc, INTR_IDX_VSYNC);
  806. SDE_DBG_DUMP("all", "dbg_bus", "vbif_dbg_bus");
  807. sde_encoder_helper_register_irq(
  808. phys_enc, INTR_IDX_VSYNC);
  809. }
  810. /*
  811. * if the recovery event is registered by user, don't panic
  812. * trigger panic on first timeout if no listener registered
  813. */
  814. if (recovery_events) {
  815. event = vid_enc->error_count > KICKOFF_MAX_ERRORS ?
  816. SDE_RECOVERY_HARD_RESET : SDE_RECOVERY_CAPTURE;
  817. sde_connector_event_notify(conn,
  818. DRM_EVENT_SDE_HW_RECOVERY,
  819. sizeof(uint8_t), event);
  820. } else {
  821. SDE_DBG_DUMP("panic");
  822. }
  823. /* request a ctl reset before the next flush */
  824. phys_enc->enable_state = SDE_ENC_ERR_NEEDS_HW_RESET;
  825. } else {
  826. if (recovery_events && vid_enc->error_count)
  827. sde_connector_event_notify(conn,
  828. DRM_EVENT_SDE_HW_RECOVERY,
  829. sizeof(uint8_t),
  830. SDE_RECOVERY_SUCCESS);
  831. vid_enc->error_count = 0;
  832. }
  833. return rc;
  834. }
  835. static void sde_encoder_phys_vid_single_vblank_wait(
  836. struct sde_encoder_phys *phys_enc)
  837. {
  838. int ret;
  839. struct sde_encoder_phys_vid *vid_enc
  840. = to_sde_encoder_phys_vid(phys_enc);
  841. /*
  842. * Wait for a vsync so we know the ENABLE=0 latched before
  843. * the (connector) source of the vsync's gets disabled,
  844. * otherwise we end up in a funny state if we re-enable
  845. * before the disable latches, which results that some of
  846. * the settings changes for the new modeset (like new
  847. * scanout buffer) don't latch properly..
  848. */
  849. ret = sde_encoder_phys_vid_control_vblank_irq(phys_enc, true);
  850. if (ret) {
  851. SDE_ERROR_VIDENC(vid_enc,
  852. "failed to enable vblank irq: %d\n",
  853. ret);
  854. SDE_EVT32(DRMID(phys_enc->parent),
  855. phys_enc->hw_intf->idx - INTF_0, ret,
  856. SDE_EVTLOG_FUNC_CASE1,
  857. SDE_EVTLOG_ERROR);
  858. } else {
  859. ret = _sde_encoder_phys_vid_wait_for_vblank(phys_enc, false);
  860. if (ret) {
  861. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  862. SDE_ERROR_VIDENC(vid_enc,
  863. "failure waiting for disable: %d\n",
  864. ret);
  865. SDE_EVT32(DRMID(phys_enc->parent),
  866. phys_enc->hw_intf->idx - INTF_0, ret,
  867. SDE_EVTLOG_FUNC_CASE2,
  868. SDE_EVTLOG_ERROR);
  869. }
  870. sde_encoder_phys_vid_control_vblank_irq(phys_enc, false);
  871. }
  872. }
  873. static void sde_encoder_phys_vid_disable(struct sde_encoder_phys *phys_enc)
  874. {
  875. struct msm_drm_private *priv;
  876. struct sde_encoder_phys_vid *vid_enc;
  877. unsigned long lock_flags;
  878. struct intf_status intf_status = {0};
  879. if (!phys_enc || !phys_enc->parent || !phys_enc->parent->dev ||
  880. !phys_enc->parent->dev->dev_private) {
  881. SDE_ERROR("invalid encoder/device\n");
  882. return;
  883. }
  884. priv = phys_enc->parent->dev->dev_private;
  885. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  886. if (!phys_enc->hw_intf || !phys_enc->hw_ctl) {
  887. SDE_ERROR("invalid hw_intf %d hw_ctl %d\n",
  888. !phys_enc->hw_intf, !phys_enc->hw_ctl);
  889. return;
  890. }
  891. SDE_DEBUG_VIDENC(vid_enc, "\n");
  892. if (WARN_ON(!phys_enc->hw_intf->ops.enable_timing))
  893. return;
  894. else if (!sde_encoder_phys_vid_is_master(phys_enc))
  895. goto exit;
  896. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  897. SDE_ERROR("already disabled\n");
  898. return;
  899. }
  900. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  901. phys_enc->hw_intf->ops.enable_timing(phys_enc->hw_intf, 0);
  902. sde_encoder_phys_inc_pending(phys_enc);
  903. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  904. sde_encoder_phys_vid_single_vblank_wait(phys_enc);
  905. if (phys_enc->hw_intf->ops.get_status)
  906. phys_enc->hw_intf->ops.get_status(phys_enc->hw_intf,
  907. &intf_status);
  908. if (intf_status.is_en) {
  909. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  910. sde_encoder_phys_inc_pending(phys_enc);
  911. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  912. sde_encoder_phys_vid_single_vblank_wait(phys_enc);
  913. }
  914. sde_encoder_helper_phys_disable(phys_enc, NULL);
  915. exit:
  916. SDE_EVT32(DRMID(phys_enc->parent),
  917. atomic_read(&phys_enc->pending_retire_fence_cnt));
  918. phys_enc->vfp_cached = 0;
  919. phys_enc->enable_state = SDE_ENC_DISABLED;
  920. }
  921. static void sde_encoder_phys_vid_handle_post_kickoff(
  922. struct sde_encoder_phys *phys_enc)
  923. {
  924. unsigned long lock_flags;
  925. struct sde_encoder_phys_vid *vid_enc;
  926. u32 avr_mode;
  927. if (!phys_enc) {
  928. SDE_ERROR("invalid encoder\n");
  929. return;
  930. }
  931. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  932. SDE_DEBUG_VIDENC(vid_enc, "enable_state %d\n", phys_enc->enable_state);
  933. /*
  934. * Video mode must flush CTL before enabling timing engine
  935. * Video encoders need to turn on their interfaces now
  936. */
  937. if (phys_enc->enable_state == SDE_ENC_ENABLING) {
  938. if (sde_encoder_phys_vid_is_master(phys_enc)) {
  939. SDE_EVT32(DRMID(phys_enc->parent),
  940. phys_enc->hw_intf->idx - INTF_0);
  941. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  942. phys_enc->hw_intf->ops.enable_timing(phys_enc->hw_intf,
  943. 1);
  944. spin_unlock_irqrestore(phys_enc->enc_spinlock,
  945. lock_flags);
  946. }
  947. phys_enc->enable_state = SDE_ENC_ENABLED;
  948. }
  949. avr_mode = sde_connector_get_qsync_mode(phys_enc->connector);
  950. if (avr_mode && vid_enc->base.hw_intf->ops.avr_trigger) {
  951. vid_enc->base.hw_intf->ops.avr_trigger(vid_enc->base.hw_intf);
  952. SDE_EVT32(DRMID(phys_enc->parent),
  953. phys_enc->hw_intf->idx - INTF_0,
  954. SDE_EVTLOG_FUNC_CASE9);
  955. }
  956. }
  957. static void sde_encoder_phys_vid_prepare_for_commit(
  958. struct sde_encoder_phys *phys_enc)
  959. {
  960. if (!phys_enc) {
  961. SDE_ERROR("invalid encoder parameters\n");
  962. return;
  963. }
  964. if (sde_connector_is_qsync_updated(phys_enc->connector))
  965. _sde_encoder_phys_vid_avr_ctrl(phys_enc);
  966. }
  967. static void sde_encoder_phys_vid_irq_control(struct sde_encoder_phys *phys_enc,
  968. bool enable)
  969. {
  970. struct sde_encoder_phys_vid *vid_enc;
  971. int ret;
  972. if (!phys_enc)
  973. return;
  974. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  975. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_intf->idx - INTF_0,
  976. enable, atomic_read(&phys_enc->vblank_refcount));
  977. if (enable) {
  978. ret = sde_encoder_phys_vid_control_vblank_irq(phys_enc, true);
  979. if (ret)
  980. return;
  981. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_UNDERRUN);
  982. } else {
  983. sde_encoder_phys_vid_control_vblank_irq(phys_enc, false);
  984. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_UNDERRUN);
  985. }
  986. }
  987. static int sde_encoder_phys_vid_get_line_count(
  988. struct sde_encoder_phys *phys_enc)
  989. {
  990. if (!phys_enc)
  991. return -EINVAL;
  992. if (!sde_encoder_phys_vid_is_master(phys_enc))
  993. return -EINVAL;
  994. if (!phys_enc->hw_intf || !phys_enc->hw_intf->ops.get_line_count)
  995. return -EINVAL;
  996. return phys_enc->hw_intf->ops.get_line_count(phys_enc->hw_intf);
  997. }
  998. static u32 sde_encoder_phys_vid_get_underrun_line_count(
  999. struct sde_encoder_phys *phys_enc)
  1000. {
  1001. u32 underrun_linecount = 0xebadebad;
  1002. struct intf_status intf_status = {0};
  1003. if (!phys_enc)
  1004. return -EINVAL;
  1005. if (!sde_encoder_phys_vid_is_master(phys_enc) || !phys_enc->hw_intf)
  1006. return -EINVAL;
  1007. if (phys_enc->hw_intf->ops.get_status)
  1008. phys_enc->hw_intf->ops.get_status(phys_enc->hw_intf,
  1009. &intf_status);
  1010. if (phys_enc->hw_intf->ops.get_underrun_line_count)
  1011. underrun_linecount =
  1012. phys_enc->hw_intf->ops.get_underrun_line_count(
  1013. phys_enc->hw_intf);
  1014. SDE_EVT32(DRMID(phys_enc->parent), underrun_linecount,
  1015. intf_status.frame_count, intf_status.line_count);
  1016. return underrun_linecount;
  1017. }
  1018. static int sde_encoder_phys_vid_wait_for_active(
  1019. struct sde_encoder_phys *phys_enc)
  1020. {
  1021. struct drm_display_mode mode;
  1022. struct sde_encoder_phys_vid *vid_enc;
  1023. u32 ln_cnt, min_ln_cnt, active_lns_cnt;
  1024. u32 clk_period, time_of_line;
  1025. u32 delay, retry = MAX_POLL_CNT;
  1026. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  1027. if (!phys_enc->hw_intf || !phys_enc->hw_intf->ops.get_line_count) {
  1028. SDE_ERROR_VIDENC(vid_enc, "invalid vid_enc params\n");
  1029. return -EINVAL;
  1030. }
  1031. mode = phys_enc->cached_mode;
  1032. /*
  1033. * calculate clk_period as pico second to maintain good
  1034. * accuracy with high pclk rate and this number is in 17 bit
  1035. * range.
  1036. */
  1037. clk_period = DIV_ROUND_UP_ULL(1000000000, mode.clock);
  1038. if (!clk_period) {
  1039. SDE_ERROR_VIDENC(vid_enc, "Unable to calculate clock period\n");
  1040. return -EINVAL;
  1041. }
  1042. min_ln_cnt = (mode.vtotal - mode.vsync_start) +
  1043. (mode.vsync_end - mode.vsync_start);
  1044. active_lns_cnt = mode.vdisplay;
  1045. time_of_line = mode.htotal * clk_period;
  1046. /* delay in micro seconds */
  1047. delay = (time_of_line * (min_ln_cnt +
  1048. (mode.vsync_start - mode.vdisplay))) / 1000000;
  1049. /*
  1050. * Wait for max delay before
  1051. * polling to check active region
  1052. */
  1053. if (delay > POLL_TIME_USEC_FOR_LN_CNT)
  1054. delay = POLL_TIME_USEC_FOR_LN_CNT;
  1055. while (retry) {
  1056. ln_cnt = phys_enc->hw_intf->ops.get_line_count(
  1057. phys_enc->hw_intf);
  1058. if ((ln_cnt >= min_ln_cnt) &&
  1059. (ln_cnt < (active_lns_cnt + min_ln_cnt))) {
  1060. SDE_DEBUG_VIDENC(vid_enc,
  1061. "Needed lines left line_cnt=%d\n",
  1062. ln_cnt);
  1063. return 0;
  1064. }
  1065. SDE_ERROR_VIDENC(vid_enc, "line count is less. line_cnt = %d\n",
  1066. ln_cnt);
  1067. /* Add delay so that line count is in active region */
  1068. udelay(delay);
  1069. retry--;
  1070. }
  1071. return -EINVAL;
  1072. }
  1073. static void sde_encoder_phys_vid_init_ops(struct sde_encoder_phys_ops *ops)
  1074. {
  1075. ops->is_master = sde_encoder_phys_vid_is_master;
  1076. ops->mode_set = sde_encoder_phys_vid_mode_set;
  1077. ops->cont_splash_mode_set = sde_encoder_phys_vid_cont_splash_mode_set;
  1078. ops->mode_fixup = sde_encoder_phys_vid_mode_fixup;
  1079. ops->enable = sde_encoder_phys_vid_enable;
  1080. ops->disable = sde_encoder_phys_vid_disable;
  1081. ops->destroy = sde_encoder_phys_vid_destroy;
  1082. ops->get_hw_resources = sde_encoder_phys_vid_get_hw_resources;
  1083. ops->control_vblank_irq = sde_encoder_phys_vid_control_vblank_irq;
  1084. ops->wait_for_commit_done = sde_encoder_phys_vid_wait_for_vblank;
  1085. ops->wait_for_vblank = sde_encoder_phys_vid_wait_for_vblank_no_notify;
  1086. ops->wait_for_tx_complete = sde_encoder_phys_vid_wait_for_vblank;
  1087. ops->irq_control = sde_encoder_phys_vid_irq_control;
  1088. ops->prepare_for_kickoff = sde_encoder_phys_vid_prepare_for_kickoff;
  1089. ops->handle_post_kickoff = sde_encoder_phys_vid_handle_post_kickoff;
  1090. ops->needs_single_flush = sde_encoder_phys_needs_single_flush;
  1091. ops->setup_misr = sde_encoder_helper_setup_misr;
  1092. ops->collect_misr = sde_encoder_helper_collect_misr;
  1093. ops->trigger_flush = sde_encoder_helper_trigger_flush;
  1094. ops->hw_reset = sde_encoder_helper_hw_reset;
  1095. ops->get_line_count = sde_encoder_phys_vid_get_line_count;
  1096. ops->get_wr_line_count = sde_encoder_phys_vid_get_line_count;
  1097. ops->wait_dma_trigger = sde_encoder_phys_vid_wait_dma_trigger;
  1098. ops->wait_for_active = sde_encoder_phys_vid_wait_for_active;
  1099. ops->prepare_commit = sde_encoder_phys_vid_prepare_for_commit;
  1100. ops->get_underrun_line_count =
  1101. sde_encoder_phys_vid_get_underrun_line_count;
  1102. }
  1103. struct sde_encoder_phys *sde_encoder_phys_vid_init(
  1104. struct sde_enc_phys_init_params *p)
  1105. {
  1106. struct sde_encoder_phys *phys_enc = NULL;
  1107. struct sde_encoder_phys_vid *vid_enc = NULL;
  1108. struct sde_hw_mdp *hw_mdp;
  1109. struct sde_encoder_irq *irq;
  1110. int i, ret = 0;
  1111. if (!p) {
  1112. ret = -EINVAL;
  1113. goto fail;
  1114. }
  1115. vid_enc = kzalloc(sizeof(*vid_enc), GFP_KERNEL);
  1116. if (!vid_enc) {
  1117. ret = -ENOMEM;
  1118. goto fail;
  1119. }
  1120. phys_enc = &vid_enc->base;
  1121. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  1122. if (IS_ERR_OR_NULL(hw_mdp)) {
  1123. ret = PTR_ERR(hw_mdp);
  1124. SDE_ERROR("failed to get mdptop\n");
  1125. goto fail;
  1126. }
  1127. phys_enc->hw_mdptop = hw_mdp;
  1128. phys_enc->intf_idx = p->intf_idx;
  1129. SDE_DEBUG_VIDENC(vid_enc, "\n");
  1130. sde_encoder_phys_vid_init_ops(&phys_enc->ops);
  1131. phys_enc->parent = p->parent;
  1132. phys_enc->parent_ops = p->parent_ops;
  1133. phys_enc->sde_kms = p->sde_kms;
  1134. phys_enc->split_role = p->split_role;
  1135. phys_enc->intf_mode = INTF_MODE_VIDEO;
  1136. phys_enc->enc_spinlock = p->enc_spinlock;
  1137. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  1138. phys_enc->comp_type = p->comp_type;
  1139. for (i = 0; i < INTR_IDX_MAX; i++) {
  1140. irq = &phys_enc->irq[i];
  1141. INIT_LIST_HEAD(&irq->cb.list);
  1142. irq->irq_idx = -EINVAL;
  1143. irq->hw_idx = -EINVAL;
  1144. irq->cb.arg = phys_enc;
  1145. }
  1146. irq = &phys_enc->irq[INTR_IDX_VSYNC];
  1147. irq->name = "vsync_irq";
  1148. irq->intr_type = SDE_IRQ_TYPE_INTF_VSYNC;
  1149. irq->intr_idx = INTR_IDX_VSYNC;
  1150. irq->cb.func = sde_encoder_phys_vid_vblank_irq;
  1151. irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
  1152. irq->name = "underrun";
  1153. irq->intr_type = SDE_IRQ_TYPE_INTF_UNDER_RUN;
  1154. irq->intr_idx = INTR_IDX_UNDERRUN;
  1155. irq->cb.func = sde_encoder_phys_vid_underrun_irq;
  1156. atomic_set(&phys_enc->vblank_refcount, 0);
  1157. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1158. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  1159. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  1160. phys_enc->enable_state = SDE_ENC_DISABLED;
  1161. SDE_DEBUG_VIDENC(vid_enc, "created intf idx:%d\n", p->intf_idx);
  1162. return phys_enc;
  1163. fail:
  1164. SDE_ERROR("failed to create encoder\n");
  1165. if (vid_enc)
  1166. sde_encoder_phys_vid_destroy(phys_enc);
  1167. return ERR_PTR(ret);
  1168. }