sde_encoder_phys_vid.c 39 KB

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