sde_encoder_phys_vid.c 44 KB

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