sde_encoder_phys_vid.c 40 KB

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