sde_encoder_phys_vid.c 40 KB

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