sde_encoder_phys_vid.c 39 KB

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