sde_encoder_phys_vid.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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. struct drm_connector *conn = phys_enc->connector;
  329. struct sde_encoder_virt *sde_enc = to_sde_encoder_virt(phys_enc->parent);
  330. struct msm_mode_info *info = &sde_enc->mode_info;
  331. u32 avr_step_state;
  332. if (!conn || !conn->state)
  333. return;
  334. avr_step_state = sde_connector_get_property(conn->state, CONNECTOR_PROP_AVR_STEP_STATE);
  335. memset(&avr_params, 0, sizeof(avr_params));
  336. avr_params.avr_mode = sde_connector_get_qsync_mode(phys_enc->connector);
  337. if (info->avr_step_fps && (avr_step_state == AVR_STEP_ENABLE))
  338. avr_params.avr_step_lines = mult_frac(phys_enc->cached_mode.vtotal,
  339. vid_enc->timing_params.vrefresh, info->avr_step_fps);
  340. if (vid_enc->base.hw_intf->ops.avr_ctrl)
  341. vid_enc->base.hw_intf->ops.avr_ctrl(vid_enc->base.hw_intf, &avr_params);
  342. if (vid_enc->base.hw_intf->ops.enable_te_level_trigger &&
  343. !sde_enc->disp_info.is_te_using_watchdog_timer)
  344. vid_enc->base.hw_intf->ops.enable_te_level_trigger(vid_enc->base.hw_intf,
  345. (avr_step_state == AVR_STEP_ENABLE));
  346. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_intf->idx - INTF_0, avr_params.avr_mode,
  347. avr_params.avr_step_lines, info->avr_step_fps, avr_step_state,
  348. sde_enc->disp_info.is_te_using_watchdog_timer);
  349. }
  350. static void sde_encoder_phys_vid_setup_timing_engine(
  351. struct sde_encoder_phys *phys_enc)
  352. {
  353. struct sde_encoder_phys_vid *vid_enc;
  354. struct drm_display_mode mode;
  355. struct intf_timing_params timing_params = { 0 };
  356. const struct sde_format *fmt = NULL;
  357. u32 fmt_fourcc = DRM_FORMAT_RGB888;
  358. u32 qsync_min_fps = 0;
  359. unsigned long lock_flags;
  360. struct sde_hw_intf_cfg intf_cfg = { 0 };
  361. bool is_split_link = false;
  362. if (!phys_enc || !phys_enc->sde_kms || !phys_enc->hw_ctl ||
  363. !phys_enc->hw_intf || !phys_enc->connector) {
  364. SDE_ERROR("invalid encoder %d\n", !phys_enc);
  365. return;
  366. }
  367. mode = phys_enc->cached_mode;
  368. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  369. if (!phys_enc->hw_intf->ops.setup_timing_gen) {
  370. SDE_ERROR("timing engine setup is not supported\n");
  371. return;
  372. }
  373. SDE_DEBUG_VIDENC(vid_enc, "enabling mode:\n");
  374. drm_mode_debug_printmodeline(&mode);
  375. is_split_link = phys_enc->hw_intf->cfg.split_link_en;
  376. if (phys_enc->split_role != ENC_ROLE_SOLO || is_split_link) {
  377. mode.hdisplay >>= 1;
  378. mode.htotal >>= 1;
  379. mode.hsync_start >>= 1;
  380. mode.hsync_end >>= 1;
  381. SDE_DEBUG_VIDENC(vid_enc,
  382. "split_role %d, halve horizontal %d %d %d %d\n",
  383. phys_enc->split_role,
  384. mode.hdisplay, mode.htotal,
  385. mode.hsync_start, mode.hsync_end);
  386. }
  387. if (!phys_enc->vfp_cached) {
  388. phys_enc->vfp_cached =
  389. sde_connector_get_panel_vfp(phys_enc->connector, &mode);
  390. if (phys_enc->vfp_cached <= 0)
  391. phys_enc->vfp_cached = mode.vsync_start - mode.vdisplay;
  392. }
  393. drm_mode_to_intf_timing_params(vid_enc, &mode, &timing_params);
  394. vid_enc->timing_params = timing_params;
  395. if (phys_enc->cont_splash_enabled) {
  396. SDE_DEBUG_VIDENC(vid_enc,
  397. "skipping intf programming since cont splash is enabled\n");
  398. goto exit;
  399. }
  400. fmt = sde_get_sde_format(fmt_fourcc);
  401. SDE_DEBUG_VIDENC(vid_enc, "fmt_fourcc 0x%X\n", fmt_fourcc);
  402. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  403. phys_enc->hw_intf->ops.setup_timing_gen(phys_enc->hw_intf,
  404. &timing_params, fmt);
  405. if (test_bit(SDE_CTL_ACTIVE_CFG,
  406. &phys_enc->hw_ctl->caps->features)) {
  407. sde_encoder_helper_update_intf_cfg(phys_enc);
  408. } else if (phys_enc->hw_ctl->ops.setup_intf_cfg) {
  409. intf_cfg.intf = phys_enc->hw_intf->idx;
  410. intf_cfg.intf_mode_sel = SDE_CTL_MODE_SEL_VID;
  411. intf_cfg.stream_sel = 0; /* Don't care value for video mode */
  412. intf_cfg.mode_3d =
  413. sde_encoder_helper_get_3d_blend_mode(phys_enc);
  414. phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl,
  415. &intf_cfg);
  416. }
  417. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  418. if (phys_enc->hw_intf->cap->type == INTF_DSI)
  419. programmable_fetch_config(phys_enc, &timing_params);
  420. exit:
  421. if (phys_enc->parent_ops.get_qsync_fps)
  422. phys_enc->parent_ops.get_qsync_fps(
  423. phys_enc->parent, &qsync_min_fps, phys_enc->connector->state);
  424. /* only panels which support qsync will have a non-zero min fps */
  425. if (qsync_min_fps) {
  426. _sde_encoder_phys_vid_setup_avr(phys_enc, qsync_min_fps);
  427. _sde_encoder_phys_vid_avr_ctrl(phys_enc);
  428. }
  429. }
  430. static void sde_encoder_phys_vid_vblank_irq(void *arg, int irq_idx)
  431. {
  432. struct sde_encoder_phys *phys_enc = arg;
  433. struct sde_hw_ctl *hw_ctl;
  434. struct intf_status intf_status = {0};
  435. unsigned long lock_flags;
  436. u32 flush_register = ~0;
  437. u32 reset_status = 0;
  438. int new_cnt = -1, old_cnt = -1;
  439. u32 event = 0;
  440. int pend_ret_fence_cnt = 0;
  441. u32 fence_ready = -1;
  442. if (!phys_enc)
  443. return;
  444. hw_ctl = phys_enc->hw_ctl;
  445. if (!hw_ctl)
  446. return;
  447. SDE_ATRACE_BEGIN("vblank_irq");
  448. /*
  449. * only decrement the pending flush count if we've actually flushed
  450. * hardware. due to sw irq latency, vblank may have already happened
  451. * so we need to double-check with hw that it accepted the flush bits
  452. */
  453. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  454. old_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  455. if (hw_ctl->ops.get_flush_register)
  456. flush_register = hw_ctl->ops.get_flush_register(hw_ctl);
  457. if (flush_register)
  458. goto not_flushed;
  459. new_cnt = atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
  460. pend_ret_fence_cnt = atomic_read(&phys_enc->pending_retire_fence_cnt);
  461. /* signal only for master, where there is a pending kickoff */
  462. if (sde_encoder_phys_vid_is_master(phys_enc) &&
  463. atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0)) {
  464. event = SDE_ENCODER_FRAME_EVENT_DONE |
  465. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE |
  466. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  467. }
  468. not_flushed:
  469. if (hw_ctl->ops.get_reset)
  470. reset_status = hw_ctl->ops.get_reset(hw_ctl);
  471. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  472. if (event && phys_enc->parent_ops.handle_frame_done)
  473. phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
  474. phys_enc, event);
  475. if (phys_enc->parent_ops.handle_vblank_virt)
  476. phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
  477. phys_enc);
  478. if (phys_enc->hw_intf->ops.get_status)
  479. phys_enc->hw_intf->ops.get_status(phys_enc->hw_intf,
  480. &intf_status);
  481. if (flush_register && hw_ctl->ops.get_hw_fence_status)
  482. fence_ready = hw_ctl->ops.get_hw_fence_status(hw_ctl);
  483. SDE_EVT32_IRQ(DRMID(phys_enc->parent), phys_enc->hw_intf->idx - INTF_0,
  484. old_cnt, atomic_read(&phys_enc->pending_kickoff_cnt),
  485. reset_status ? SDE_EVTLOG_ERROR : 0,
  486. flush_register, event,
  487. atomic_read(&phys_enc->pending_retire_fence_cnt),
  488. intf_status.frame_count, intf_status.line_count,
  489. fence_ready);
  490. /* Signal any waiting atomic commit thread */
  491. wake_up_all(&phys_enc->pending_kickoff_wq);
  492. SDE_ATRACE_END("vblank_irq");
  493. }
  494. static void sde_encoder_phys_vid_underrun_irq(void *arg, int irq_idx)
  495. {
  496. struct sde_encoder_phys *phys_enc = arg;
  497. if (!phys_enc)
  498. return;
  499. if (phys_enc->parent_ops.handle_underrun_virt)
  500. phys_enc->parent_ops.handle_underrun_virt(phys_enc->parent,
  501. phys_enc);
  502. }
  503. static void _sde_encoder_phys_vid_setup_irq_hw_idx(
  504. struct sde_encoder_phys *phys_enc)
  505. {
  506. struct sde_encoder_irq *irq;
  507. /*
  508. * Initialize irq->hw_idx only when irq is not registered.
  509. * Prevent invalidating irq->irq_idx as modeset may be
  510. * called many times during dfps.
  511. */
  512. irq = &phys_enc->irq[INTR_IDX_VSYNC];
  513. if (irq->irq_idx < 0)
  514. irq->hw_idx = phys_enc->intf_idx;
  515. irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
  516. if (irq->irq_idx < 0)
  517. irq->hw_idx = phys_enc->intf_idx;
  518. }
  519. static void sde_encoder_phys_vid_cont_splash_mode_set(
  520. struct sde_encoder_phys *phys_enc,
  521. struct drm_display_mode *adj_mode)
  522. {
  523. if (!phys_enc || !adj_mode) {
  524. SDE_ERROR("invalid args\n");
  525. return;
  526. }
  527. phys_enc->cached_mode = *adj_mode;
  528. phys_enc->enable_state = SDE_ENC_ENABLED;
  529. _sde_encoder_phys_vid_setup_irq_hw_idx(phys_enc);
  530. }
  531. static void sde_encoder_phys_vid_mode_set(
  532. struct sde_encoder_phys *phys_enc,
  533. struct drm_display_mode *mode,
  534. struct drm_display_mode *adj_mode, bool *reinit_mixers)
  535. {
  536. struct sde_rm *rm;
  537. struct sde_rm_hw_iter iter;
  538. int i, instance;
  539. struct sde_encoder_phys_vid *vid_enc;
  540. if (!phys_enc || !phys_enc->sde_kms) {
  541. SDE_ERROR("invalid encoder/kms\n");
  542. return;
  543. }
  544. rm = &phys_enc->sde_kms->rm;
  545. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  546. if (adj_mode) {
  547. phys_enc->cached_mode = *adj_mode;
  548. drm_mode_debug_printmodeline(adj_mode);
  549. SDE_DEBUG_VIDENC(vid_enc, "caching mode:\n");
  550. }
  551. instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
  552. /* Retrieve previously allocated HW Resources. Shouldn't fail */
  553. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
  554. for (i = 0; i <= instance; i++) {
  555. if (sde_rm_get_hw(rm, &iter)) {
  556. if (phys_enc->hw_ctl && phys_enc->hw_ctl != to_sde_hw_ctl(iter.hw)) {
  557. *reinit_mixers = true;
  558. SDE_EVT32(phys_enc->hw_ctl->idx,
  559. to_sde_hw_ctl(iter.hw)->idx);
  560. }
  561. phys_enc->hw_ctl = to_sde_hw_ctl(iter.hw);
  562. }
  563. }
  564. if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
  565. SDE_ERROR_VIDENC(vid_enc, "failed to init ctl, %ld\n",
  566. PTR_ERR(phys_enc->hw_ctl));
  567. phys_enc->hw_ctl = NULL;
  568. return;
  569. }
  570. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_INTF);
  571. for (i = 0; i <= instance; i++) {
  572. if (sde_rm_get_hw(rm, &iter))
  573. phys_enc->hw_intf = to_sde_hw_intf(iter.hw);
  574. }
  575. if (IS_ERR_OR_NULL(phys_enc->hw_intf)) {
  576. SDE_ERROR_VIDENC(vid_enc, "failed to init intf: %ld\n",
  577. PTR_ERR(phys_enc->hw_intf));
  578. phys_enc->hw_intf = NULL;
  579. return;
  580. }
  581. _sde_encoder_phys_vid_setup_irq_hw_idx(phys_enc);
  582. phys_enc->kickoff_timeout_ms =
  583. sde_encoder_helper_get_kickoff_timeout_ms(phys_enc->parent);
  584. }
  585. static int sde_encoder_phys_vid_control_vblank_irq(
  586. struct sde_encoder_phys *phys_enc,
  587. bool enable)
  588. {
  589. int ret = 0;
  590. struct sde_encoder_phys_vid *vid_enc;
  591. int refcount;
  592. if (!phys_enc) {
  593. SDE_ERROR("invalid encoder\n");
  594. return -EINVAL;
  595. }
  596. mutex_lock(phys_enc->vblank_ctl_lock);
  597. refcount = atomic_read(&phys_enc->vblank_refcount);
  598. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  599. /* Slave encoders don't report vblank */
  600. if (!sde_encoder_phys_vid_is_master(phys_enc))
  601. goto end;
  602. /* protect against negative */
  603. if (!enable && refcount == 0) {
  604. ret = -EINVAL;
  605. goto end;
  606. }
  607. SDE_DEBUG_VIDENC(vid_enc, "[%pS] enable=%d/%d\n",
  608. __builtin_return_address(0),
  609. enable, atomic_read(&phys_enc->vblank_refcount));
  610. SDE_EVT32(DRMID(phys_enc->parent), enable,
  611. atomic_read(&phys_enc->vblank_refcount));
  612. if (enable && atomic_inc_return(&phys_enc->vblank_refcount) == 1) {
  613. ret = sde_encoder_helper_register_irq(phys_enc, INTR_IDX_VSYNC);
  614. if (ret)
  615. atomic_dec_return(&phys_enc->vblank_refcount);
  616. } else if (!enable &&
  617. atomic_dec_return(&phys_enc->vblank_refcount) == 0) {
  618. ret = sde_encoder_helper_unregister_irq(phys_enc,
  619. INTR_IDX_VSYNC);
  620. if (ret)
  621. atomic_inc_return(&phys_enc->vblank_refcount);
  622. }
  623. end:
  624. if (ret) {
  625. SDE_ERROR_VIDENC(vid_enc,
  626. "control vblank irq error %d, enable %d\n",
  627. ret, enable);
  628. SDE_EVT32(DRMID(phys_enc->parent),
  629. phys_enc->hw_intf->idx - INTF_0,
  630. enable, refcount, SDE_EVTLOG_ERROR);
  631. }
  632. mutex_unlock(phys_enc->vblank_ctl_lock);
  633. return ret;
  634. }
  635. static bool sde_encoder_phys_vid_wait_dma_trigger(
  636. struct sde_encoder_phys *phys_enc)
  637. {
  638. struct sde_encoder_phys_vid *vid_enc;
  639. struct sde_hw_intf *intf;
  640. struct sde_hw_ctl *ctl;
  641. struct intf_status status;
  642. if (!phys_enc) {
  643. SDE_ERROR("invalid encoder\n");
  644. return false;
  645. }
  646. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  647. intf = phys_enc->hw_intf;
  648. ctl = phys_enc->hw_ctl;
  649. if (!phys_enc->hw_intf || !phys_enc->hw_ctl) {
  650. SDE_ERROR("invalid hw_intf %d hw_ctl %d\n",
  651. phys_enc->hw_intf != NULL, phys_enc->hw_ctl != NULL);
  652. return false;
  653. }
  654. if (!intf->ops.get_status)
  655. return false;
  656. intf->ops.get_status(intf, &status);
  657. /* if interface is not enabled, return true to wait for dma trigger */
  658. return status.is_en ? false : true;
  659. }
  660. static void sde_encoder_phys_vid_enable(struct sde_encoder_phys *phys_enc)
  661. {
  662. struct msm_drm_private *priv;
  663. struct sde_encoder_phys_vid *vid_enc;
  664. struct sde_hw_intf *intf;
  665. struct sde_hw_ctl *ctl;
  666. if (!phys_enc || !phys_enc->parent || !phys_enc->parent->dev ||
  667. !phys_enc->parent->dev->dev_private ||
  668. !phys_enc->sde_kms) {
  669. SDE_ERROR("invalid encoder/device\n");
  670. return;
  671. }
  672. priv = phys_enc->parent->dev->dev_private;
  673. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  674. intf = phys_enc->hw_intf;
  675. ctl = phys_enc->hw_ctl;
  676. if (!phys_enc->hw_intf || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
  677. SDE_ERROR("invalid hw_intf %d hw_ctl %d hw_pp %d\n",
  678. !phys_enc->hw_intf, !phys_enc->hw_ctl,
  679. !phys_enc->hw_pp);
  680. return;
  681. }
  682. if (!ctl->ops.update_bitmask) {
  683. SDE_ERROR("invalid hw_ctl ops %d\n", ctl->idx);
  684. return;
  685. }
  686. SDE_DEBUG_VIDENC(vid_enc, "\n");
  687. if (WARN_ON(!phys_enc->hw_intf->ops.enable_timing))
  688. return;
  689. if (!phys_enc->cont_splash_enabled)
  690. sde_encoder_helper_split_config(phys_enc,
  691. phys_enc->hw_intf->idx);
  692. sde_encoder_phys_vid_setup_timing_engine(phys_enc);
  693. /*
  694. * For cases where both the interfaces are connected to same ctl,
  695. * set the flush bit for both master and slave.
  696. * For single flush cases (dual-ctl or pp-split), skip setting the
  697. * flush bit for the slave intf, since both intfs use same ctl
  698. * and HW will only flush the master.
  699. */
  700. if (!test_bit(SDE_CTL_ACTIVE_CFG, &ctl->caps->features) &&
  701. sde_encoder_phys_needs_single_flush(phys_enc) &&
  702. !sde_encoder_phys_vid_is_master(phys_enc))
  703. goto skip_flush;
  704. /**
  705. * skip flushing intf during cont. splash handoff since bootloader
  706. * has already enabled the hardware and is single buffered.
  707. */
  708. if (phys_enc->cont_splash_enabled) {
  709. SDE_DEBUG_VIDENC(vid_enc,
  710. "skipping intf flush bit set as cont. splash is enabled\n");
  711. goto skip_flush;
  712. }
  713. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_INTF, intf->idx, 1);
  714. if (phys_enc->hw_pp->merge_3d)
  715. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_MERGE_3D,
  716. phys_enc->hw_pp->merge_3d->idx, 1);
  717. if (phys_enc->hw_intf->cap->type == INTF_DP &&
  718. phys_enc->comp_type == MSM_DISPLAY_COMPRESSION_DSC &&
  719. phys_enc->comp_ratio)
  720. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_PERIPH, intf->idx, 1);
  721. skip_flush:
  722. SDE_DEBUG_VIDENC(vid_enc, "update pending flush ctl %d intf %d\n",
  723. ctl->idx - CTL_0, intf->idx);
  724. SDE_EVT32(DRMID(phys_enc->parent),
  725. atomic_read(&phys_enc->pending_retire_fence_cnt));
  726. /* ctl_flush & timing engine enable will be triggered by framework */
  727. if (phys_enc->enable_state == SDE_ENC_DISABLED)
  728. phys_enc->enable_state = SDE_ENC_ENABLING;
  729. }
  730. static void sde_encoder_phys_vid_destroy(struct sde_encoder_phys *phys_enc)
  731. {
  732. struct sde_encoder_phys_vid *vid_enc;
  733. if (!phys_enc) {
  734. SDE_ERROR("invalid encoder\n");
  735. return;
  736. }
  737. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  738. SDE_DEBUG_VIDENC(vid_enc, "\n");
  739. kfree(vid_enc);
  740. }
  741. static void sde_encoder_phys_vid_get_hw_resources(
  742. struct sde_encoder_phys *phys_enc,
  743. struct sde_encoder_hw_resources *hw_res,
  744. struct drm_connector_state *conn_state)
  745. {
  746. struct sde_encoder_phys_vid *vid_enc;
  747. if (!phys_enc || !hw_res) {
  748. SDE_ERROR("invalid arg(s), enc %d hw_res %d conn_state %d\n",
  749. !phys_enc, !hw_res, !conn_state);
  750. return;
  751. }
  752. if ((phys_enc->intf_idx - INTF_0) >= INTF_MAX) {
  753. SDE_ERROR("invalid intf idx:%d\n", phys_enc->intf_idx);
  754. return;
  755. }
  756. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  757. SDE_DEBUG_VIDENC(vid_enc, "\n");
  758. hw_res->intfs[phys_enc->intf_idx - INTF_0] = INTF_MODE_VIDEO;
  759. }
  760. static int _sde_encoder_phys_vid_wait_for_vblank(
  761. struct sde_encoder_phys *phys_enc, bool notify)
  762. {
  763. struct sde_encoder_wait_info wait_info = {0};
  764. int ret = 0, new_cnt;
  765. u32 event = SDE_ENCODER_FRAME_EVENT_ERROR |
  766. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE |
  767. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  768. struct drm_connector *conn;
  769. struct sde_hw_ctl *hw_ctl;
  770. u32 flush_register = 0xebad;
  771. bool timeout = false;
  772. if (!phys_enc || !phys_enc->hw_ctl) {
  773. pr_err("invalid encoder\n");
  774. return -EINVAL;
  775. }
  776. hw_ctl = phys_enc->hw_ctl;
  777. conn = phys_enc->connector;
  778. wait_info.wq = &phys_enc->pending_kickoff_wq;
  779. wait_info.atomic_cnt = &phys_enc->pending_kickoff_cnt;
  780. wait_info.timeout_ms = phys_enc->kickoff_timeout_ms;
  781. /* Wait for kickoff to complete */
  782. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_VSYNC,
  783. &wait_info);
  784. /*
  785. * if hwfencing enabled, try again to wait for up to the extended timeout time in
  786. * increments as long as fence has not been signaled.
  787. */
  788. if (ret == -ETIMEDOUT && phys_enc->sde_kms->catalog->hw_fence_rev)
  789. ret = sde_encoder_helper_hw_fence_extended_wait(phys_enc, phys_enc->hw_ctl,
  790. &wait_info, INTR_IDX_VSYNC);
  791. if (ret == -ETIMEDOUT) {
  792. new_cnt = atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
  793. timeout = true;
  794. /*
  795. * Reset ret when flush register is consumed. This handles a race condition between
  796. * irq wait timeout handler reading the register status and the actual IRQ handler
  797. */
  798. if (hw_ctl->ops.get_flush_register)
  799. flush_register = hw_ctl->ops.get_flush_register(hw_ctl);
  800. if (!flush_register)
  801. ret = 0;
  802. /* if we timeout after the extended wait, reset mixers and do sw override */
  803. if (ret && phys_enc->sde_kms->catalog->hw_fence_rev)
  804. sde_encoder_helper_hw_fence_sw_override(phys_enc, hw_ctl);
  805. SDE_EVT32(DRMID(phys_enc->parent), new_cnt, flush_register, ret,
  806. SDE_EVTLOG_FUNC_CASE1);
  807. }
  808. if (notify && timeout && atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0)
  809. && phys_enc->parent_ops.handle_frame_done) {
  810. phys_enc->parent_ops.handle_frame_done(phys_enc->parent, phys_enc, event);
  811. /* notify only on actual timeout cases */
  812. if ((ret == -ETIMEDOUT) && sde_encoder_recovery_events_enabled(phys_enc->parent))
  813. sde_connector_event_notify(conn, DRM_EVENT_SDE_HW_RECOVERY,
  814. sizeof(uint8_t), SDE_RECOVERY_HARD_RESET);
  815. }
  816. SDE_EVT32(DRMID(phys_enc->parent), event, notify, timeout, ret,
  817. ret ? SDE_EVTLOG_FATAL : 0, SDE_EVTLOG_FUNC_EXIT);
  818. if (!ret)
  819. sde_encoder_clear_fence_error_in_progress(phys_enc);
  820. return ret;
  821. }
  822. static int sde_encoder_phys_vid_wait_for_vblank(
  823. struct sde_encoder_phys *phys_enc)
  824. {
  825. return _sde_encoder_phys_vid_wait_for_vblank(phys_enc, true);
  826. }
  827. static void sde_encoder_phys_vid_update_txq(struct sde_encoder_phys *phys_enc)
  828. {
  829. struct sde_encoder_virt *sde_enc;
  830. if (!phys_enc)
  831. return;
  832. sde_enc = to_sde_encoder_virt(phys_enc->parent);
  833. if (!sde_enc)
  834. return;
  835. sde_encoder_helper_update_out_fence_txq(sde_enc, true);
  836. }
  837. static int sde_encoder_phys_vid_wait_for_commit_done(
  838. struct sde_encoder_phys *phys_enc)
  839. {
  840. int rc;
  841. rc = _sde_encoder_phys_vid_wait_for_vblank(phys_enc, true);
  842. if (rc)
  843. sde_encoder_helper_phys_reset(phys_enc);
  844. /* Update TxQ for the incoming frame */
  845. sde_encoder_phys_vid_update_txq(phys_enc);
  846. return rc;
  847. }
  848. static int sde_encoder_phys_vid_wait_for_vblank_no_notify(
  849. struct sde_encoder_phys *phys_enc)
  850. {
  851. return _sde_encoder_phys_vid_wait_for_vblank(phys_enc, false);
  852. }
  853. static int sde_encoder_phys_vid_prepare_for_kickoff(
  854. struct sde_encoder_phys *phys_enc,
  855. struct sde_encoder_kickoff_params *params)
  856. {
  857. struct sde_encoder_phys_vid *vid_enc;
  858. struct sde_hw_ctl *ctl;
  859. bool recovery_events;
  860. struct drm_connector *conn;
  861. int rc;
  862. int irq_enable;
  863. if (!phys_enc || !params || !phys_enc->hw_ctl) {
  864. SDE_ERROR("invalid encoder/parameters\n");
  865. return -EINVAL;
  866. }
  867. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  868. ctl = phys_enc->hw_ctl;
  869. if (!ctl->ops.wait_reset_status)
  870. return 0;
  871. conn = phys_enc->connector;
  872. recovery_events = sde_encoder_recovery_events_enabled(
  873. phys_enc->parent);
  874. /*
  875. * hw supports hardware initiated ctl reset, so before we kickoff a new
  876. * frame, need to check and wait for hw initiated ctl reset completion
  877. */
  878. rc = ctl->ops.wait_reset_status(ctl);
  879. if (rc) {
  880. SDE_ERROR_VIDENC(vid_enc, "ctl %d reset failure: %d\n",
  881. ctl->idx, rc);
  882. ++vid_enc->error_count;
  883. /* to avoid flooding, only log first time, and "dead" time */
  884. if (vid_enc->error_count == 1) {
  885. SDE_EVT32(DRMID(phys_enc->parent), SDE_EVTLOG_FATAL);
  886. mutex_lock(phys_enc->vblank_ctl_lock);
  887. irq_enable = atomic_read(&phys_enc->vblank_refcount);
  888. if (irq_enable)
  889. sde_encoder_helper_unregister_irq(
  890. phys_enc, INTR_IDX_VSYNC);
  891. SDE_DBG_DUMP(SDE_DBG_BUILT_IN_ALL);
  892. if (irq_enable)
  893. sde_encoder_helper_register_irq(
  894. phys_enc, INTR_IDX_VSYNC);
  895. mutex_unlock(phys_enc->vblank_ctl_lock);
  896. }
  897. /*
  898. * if the recovery event is registered by user, don't panic
  899. * trigger panic on first timeout if no listener registered
  900. */
  901. if (recovery_events)
  902. sde_connector_event_notify(conn, DRM_EVENT_SDE_HW_RECOVERY,
  903. sizeof(uint8_t), SDE_RECOVERY_CAPTURE);
  904. else
  905. SDE_DBG_DUMP(0x0, "panic");
  906. /* request a ctl reset before the next flush */
  907. phys_enc->enable_state = SDE_ENC_ERR_NEEDS_HW_RESET;
  908. } else {
  909. if (recovery_events && vid_enc->error_count)
  910. sde_connector_event_notify(conn,
  911. DRM_EVENT_SDE_HW_RECOVERY,
  912. sizeof(uint8_t),
  913. SDE_RECOVERY_SUCCESS);
  914. vid_enc->error_count = 0;
  915. }
  916. return rc;
  917. }
  918. static void sde_encoder_phys_vid_single_vblank_wait(
  919. struct sde_encoder_phys *phys_enc)
  920. {
  921. int ret;
  922. struct sde_encoder_phys_vid *vid_enc
  923. = to_sde_encoder_phys_vid(phys_enc);
  924. /*
  925. * Wait for a vsync so we know the ENABLE=0 latched before
  926. * the (connector) source of the vsync's gets disabled,
  927. * otherwise we end up in a funny state if we re-enable
  928. * before the disable latches, which results that some of
  929. * the settings changes for the new modeset (like new
  930. * scanout buffer) don't latch properly..
  931. */
  932. ret = sde_encoder_phys_vid_control_vblank_irq(phys_enc, true);
  933. if (ret) {
  934. SDE_ERROR_VIDENC(vid_enc,
  935. "failed to enable vblank irq: %d\n",
  936. ret);
  937. SDE_EVT32(DRMID(phys_enc->parent),
  938. phys_enc->hw_intf->idx - INTF_0, ret,
  939. SDE_EVTLOG_FUNC_CASE1,
  940. SDE_EVTLOG_ERROR);
  941. } else {
  942. ret = _sde_encoder_phys_vid_wait_for_vblank(phys_enc, false);
  943. if (ret) {
  944. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  945. SDE_ERROR_VIDENC(vid_enc,
  946. "failure waiting for disable: %d\n",
  947. ret);
  948. SDE_EVT32(DRMID(phys_enc->parent),
  949. phys_enc->hw_intf->idx - INTF_0, ret,
  950. SDE_EVTLOG_FUNC_CASE2,
  951. SDE_EVTLOG_ERROR);
  952. }
  953. sde_encoder_phys_vid_control_vblank_irq(phys_enc, false);
  954. }
  955. }
  956. static void sde_encoder_phys_vid_disable(struct sde_encoder_phys *phys_enc)
  957. {
  958. struct msm_drm_private *priv;
  959. struct sde_encoder_phys_vid *vid_enc;
  960. unsigned long lock_flags;
  961. struct intf_status intf_status = {0};
  962. if (!phys_enc || !phys_enc->parent || !phys_enc->parent->dev ||
  963. !phys_enc->parent->dev->dev_private) {
  964. SDE_ERROR("invalid encoder/device\n");
  965. return;
  966. }
  967. priv = phys_enc->parent->dev->dev_private;
  968. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  969. if (!phys_enc->hw_intf || !phys_enc->hw_ctl) {
  970. SDE_ERROR("invalid hw_intf %d hw_ctl %d\n",
  971. !phys_enc->hw_intf, !phys_enc->hw_ctl);
  972. return;
  973. }
  974. SDE_DEBUG_VIDENC(vid_enc, "\n");
  975. if (WARN_ON(!phys_enc->hw_intf->ops.enable_timing))
  976. return;
  977. else if (!sde_encoder_phys_vid_is_master(phys_enc))
  978. goto exit;
  979. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  980. SDE_ERROR("already disabled\n");
  981. return;
  982. }
  983. if (sde_in_trusted_vm(phys_enc->sde_kms))
  984. goto exit;
  985. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  986. phys_enc->hw_intf->ops.enable_timing(phys_enc->hw_intf, 0);
  987. sde_encoder_phys_inc_pending(phys_enc);
  988. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  989. if (phys_enc->hw_intf->ops.reset_counter)
  990. phys_enc->hw_intf->ops.reset_counter(phys_enc->hw_intf);
  991. sde_encoder_phys_vid_single_vblank_wait(phys_enc);
  992. if (phys_enc->hw_intf->ops.get_status)
  993. phys_enc->hw_intf->ops.get_status(phys_enc->hw_intf,
  994. &intf_status);
  995. if (intf_status.is_en) {
  996. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  997. sde_encoder_phys_inc_pending(phys_enc);
  998. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  999. sde_encoder_phys_vid_single_vblank_wait(phys_enc);
  1000. }
  1001. sde_encoder_helper_phys_disable(phys_enc, NULL);
  1002. exit:
  1003. SDE_EVT32(DRMID(phys_enc->parent),
  1004. atomic_read(&phys_enc->pending_retire_fence_cnt));
  1005. phys_enc->vfp_cached = 0;
  1006. phys_enc->enable_state = SDE_ENC_DISABLED;
  1007. }
  1008. static int sde_encoder_phys_vid_poll_for_active_region(struct sde_encoder_phys *phys_enc)
  1009. {
  1010. struct sde_encoder_phys_vid *vid_enc;
  1011. struct intf_timing_params *timing;
  1012. u32 line_cnt, v_inactive, poll_time_us, trial = 0;
  1013. if (!phys_enc || !phys_enc->hw_intf || !phys_enc->hw_intf->ops.get_line_count)
  1014. return -EINVAL;
  1015. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  1016. timing = &vid_enc->timing_params;
  1017. /* if programmable fetch is not enabled return early or if it is not a DSI interface*/
  1018. if (!programmable_fetch_get_num_lines(vid_enc, timing) ||
  1019. phys_enc->hw_intf->cap->type != INTF_DSI)
  1020. return 0;
  1021. poll_time_us = DIV_ROUND_UP(1000000, timing->vrefresh) / MAX_POLL_CNT;
  1022. v_inactive = timing->v_front_porch + timing->v_back_porch + timing->vsync_pulse_width;
  1023. do {
  1024. usleep_range(poll_time_us, poll_time_us + 5);
  1025. line_cnt = phys_enc->hw_intf->ops.get_line_count(phys_enc->hw_intf);
  1026. trial++;
  1027. } while ((trial < MAX_POLL_CNT) || (line_cnt < v_inactive));
  1028. return (trial >= MAX_POLL_CNT) ? -ETIMEDOUT : 0;
  1029. }
  1030. static void sde_encoder_phys_vid_handle_post_kickoff(
  1031. struct sde_encoder_phys *phys_enc)
  1032. {
  1033. unsigned long lock_flags;
  1034. struct sde_encoder_phys_vid *vid_enc;
  1035. u32 avr_mode;
  1036. u32 ret;
  1037. if (!phys_enc) {
  1038. SDE_ERROR("invalid encoder\n");
  1039. return;
  1040. }
  1041. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  1042. SDE_DEBUG_VIDENC(vid_enc, "enable_state %d\n", phys_enc->enable_state);
  1043. /*
  1044. * Video mode must flush CTL before enabling timing engine
  1045. * Video encoders need to turn on their interfaces now
  1046. */
  1047. if (phys_enc->enable_state == SDE_ENC_ENABLING) {
  1048. if (sde_encoder_phys_vid_is_master(phys_enc)) {
  1049. SDE_EVT32(DRMID(phys_enc->parent),
  1050. phys_enc->hw_intf->idx - INTF_0);
  1051. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  1052. phys_enc->hw_intf->ops.enable_timing(phys_enc->hw_intf,
  1053. 1);
  1054. spin_unlock_irqrestore(phys_enc->enc_spinlock,
  1055. lock_flags);
  1056. ret = sde_encoder_phys_vid_poll_for_active_region(phys_enc);
  1057. if (ret)
  1058. SDE_DEBUG_VIDENC(vid_enc, "poll for active failed ret:%d\n", ret);
  1059. }
  1060. phys_enc->enable_state = SDE_ENC_ENABLED;
  1061. }
  1062. avr_mode = sde_connector_get_qsync_mode(phys_enc->connector);
  1063. if (avr_mode && vid_enc->base.hw_intf->ops.avr_trigger) {
  1064. vid_enc->base.hw_intf->ops.avr_trigger(vid_enc->base.hw_intf);
  1065. SDE_EVT32(DRMID(phys_enc->parent),
  1066. phys_enc->hw_intf->idx - INTF_0,
  1067. SDE_EVTLOG_FUNC_CASE9);
  1068. }
  1069. }
  1070. static void sde_encoder_phys_vid_prepare_for_commit(
  1071. struct sde_encoder_phys *phys_enc)
  1072. {
  1073. struct sde_connector_state *c_state;
  1074. if (!phys_enc || !phys_enc->parent) {
  1075. SDE_ERROR("invalid encoder parameters\n");
  1076. return;
  1077. }
  1078. if (phys_enc->connector && phys_enc->connector->state) {
  1079. c_state = to_sde_connector_state(phys_enc->connector->state);
  1080. if (!c_state) {
  1081. SDE_ERROR("invalid connector state\n");
  1082. return;
  1083. }
  1084. if (!msm_is_mode_seamless_vrr(&c_state->msm_mode)
  1085. && sde_connector_is_qsync_updated(phys_enc->connector))
  1086. _sde_encoder_phys_vid_avr_ctrl(phys_enc);
  1087. }
  1088. }
  1089. static void sde_encoder_phys_vid_irq_control(struct sde_encoder_phys *phys_enc,
  1090. bool enable)
  1091. {
  1092. struct sde_encoder_phys_vid *vid_enc;
  1093. int ret;
  1094. if (!phys_enc)
  1095. return;
  1096. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  1097. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_intf->idx - INTF_0,
  1098. enable, atomic_read(&phys_enc->vblank_refcount));
  1099. if (enable) {
  1100. ret = sde_encoder_phys_vid_control_vblank_irq(phys_enc, true);
  1101. if (ret)
  1102. return;
  1103. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_UNDERRUN);
  1104. } else {
  1105. sde_encoder_phys_vid_control_vblank_irq(phys_enc, false);
  1106. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_UNDERRUN);
  1107. }
  1108. }
  1109. static int sde_encoder_phys_vid_get_line_count(
  1110. struct sde_encoder_phys *phys_enc)
  1111. {
  1112. if (!phys_enc)
  1113. return -EINVAL;
  1114. if (!sde_encoder_phys_vid_is_master(phys_enc))
  1115. return -EINVAL;
  1116. if (!phys_enc->hw_intf || !phys_enc->hw_intf->ops.get_line_count)
  1117. return -EINVAL;
  1118. return phys_enc->hw_intf->ops.get_line_count(phys_enc->hw_intf);
  1119. }
  1120. static u32 sde_encoder_phys_vid_get_underrun_line_count(
  1121. struct sde_encoder_phys *phys_enc)
  1122. {
  1123. u32 underrun_linecount = 0xebadebad;
  1124. u32 intf_intr_status = 0xebadebad;
  1125. struct intf_status intf_status = {0};
  1126. if (!phys_enc)
  1127. return -EINVAL;
  1128. if (!sde_encoder_phys_vid_is_master(phys_enc) || !phys_enc->hw_intf)
  1129. return -EINVAL;
  1130. if (phys_enc->hw_intf->ops.get_status)
  1131. phys_enc->hw_intf->ops.get_status(phys_enc->hw_intf,
  1132. &intf_status);
  1133. if (phys_enc->hw_intf->ops.get_underrun_line_count)
  1134. underrun_linecount =
  1135. phys_enc->hw_intf->ops.get_underrun_line_count(
  1136. phys_enc->hw_intf);
  1137. if (phys_enc->hw_intf->ops.get_intr_status)
  1138. intf_intr_status = phys_enc->hw_intf->ops.get_intr_status(
  1139. phys_enc->hw_intf);
  1140. SDE_EVT32(DRMID(phys_enc->parent), underrun_linecount,
  1141. intf_status.frame_count, intf_status.line_count,
  1142. intf_intr_status);
  1143. return underrun_linecount;
  1144. }
  1145. static int sde_encoder_phys_vid_wait_for_active(
  1146. struct sde_encoder_phys *phys_enc)
  1147. {
  1148. struct drm_display_mode mode;
  1149. struct sde_encoder_phys_vid *vid_enc;
  1150. u32 ln_cnt, min_ln_cnt, active_lns_cnt;
  1151. u32 retry = MAX_POLL_CNT;
  1152. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  1153. if (!phys_enc->hw_intf || !phys_enc->hw_intf->ops.get_line_count) {
  1154. SDE_ERROR_VIDENC(vid_enc, "invalid vid_enc params\n");
  1155. return -EINVAL;
  1156. }
  1157. mode = phys_enc->cached_mode;
  1158. min_ln_cnt = (mode.vtotal - mode.vsync_start) +
  1159. (mode.vsync_end - mode.vsync_start);
  1160. active_lns_cnt = mode.vdisplay;
  1161. while (retry) {
  1162. ln_cnt = phys_enc->hw_intf->ops.get_line_count(
  1163. phys_enc->hw_intf);
  1164. if ((ln_cnt >= min_ln_cnt) &&
  1165. (ln_cnt < (active_lns_cnt + min_ln_cnt))) {
  1166. SDE_DEBUG_VIDENC(vid_enc,
  1167. "Needed lines left line_cnt=%d\n",
  1168. ln_cnt);
  1169. return 0;
  1170. }
  1171. SDE_ERROR_VIDENC(vid_enc, "line count is less. line_cnt = %d\n", ln_cnt);
  1172. udelay(POLL_TIME_USEC_FOR_LN_CNT);
  1173. retry--;
  1174. }
  1175. return -EINVAL;
  1176. }
  1177. void sde_encoder_phys_vid_add_enc_to_minidump(struct sde_encoder_phys *phys_enc)
  1178. {
  1179. struct sde_encoder_phys_vid *vid_enc;
  1180. vid_enc = to_sde_encoder_phys_vid(phys_enc);
  1181. sde_mini_dump_add_va_region("sde_enc_phys_vid", sizeof(*vid_enc), vid_enc);
  1182. }
  1183. static void sde_encoder_phys_vid_init_ops(struct sde_encoder_phys_ops *ops)
  1184. {
  1185. ops->is_master = sde_encoder_phys_vid_is_master;
  1186. ops->mode_set = sde_encoder_phys_vid_mode_set;
  1187. ops->cont_splash_mode_set = sde_encoder_phys_vid_cont_splash_mode_set;
  1188. ops->mode_fixup = sde_encoder_phys_vid_mode_fixup;
  1189. ops->enable = sde_encoder_phys_vid_enable;
  1190. ops->disable = sde_encoder_phys_vid_disable;
  1191. ops->destroy = sde_encoder_phys_vid_destroy;
  1192. ops->get_hw_resources = sde_encoder_phys_vid_get_hw_resources;
  1193. ops->control_vblank_irq = sde_encoder_phys_vid_control_vblank_irq;
  1194. ops->wait_for_commit_done = sde_encoder_phys_vid_wait_for_commit_done;
  1195. ops->wait_for_vblank = sde_encoder_phys_vid_wait_for_vblank_no_notify;
  1196. ops->wait_for_tx_complete = sde_encoder_phys_vid_wait_for_vblank;
  1197. ops->irq_control = sde_encoder_phys_vid_irq_control;
  1198. ops->prepare_for_kickoff = sde_encoder_phys_vid_prepare_for_kickoff;
  1199. ops->handle_post_kickoff = sde_encoder_phys_vid_handle_post_kickoff;
  1200. ops->needs_single_flush = sde_encoder_phys_needs_single_flush;
  1201. ops->setup_misr = sde_encoder_helper_setup_misr;
  1202. ops->collect_misr = sde_encoder_helper_collect_misr;
  1203. ops->trigger_flush = sde_encoder_helper_trigger_flush;
  1204. ops->hw_reset = sde_encoder_helper_hw_reset;
  1205. ops->get_line_count = sde_encoder_phys_vid_get_line_count;
  1206. ops->wait_dma_trigger = sde_encoder_phys_vid_wait_dma_trigger;
  1207. ops->wait_for_active = sde_encoder_phys_vid_wait_for_active;
  1208. ops->prepare_commit = sde_encoder_phys_vid_prepare_for_commit;
  1209. ops->get_underrun_line_count =
  1210. sde_encoder_phys_vid_get_underrun_line_count;
  1211. ops->add_to_minidump = sde_encoder_phys_vid_add_enc_to_minidump;
  1212. }
  1213. struct sde_encoder_phys *sde_encoder_phys_vid_init(
  1214. struct sde_enc_phys_init_params *p)
  1215. {
  1216. struct sde_encoder_phys *phys_enc = NULL;
  1217. struct sde_encoder_phys_vid *vid_enc = NULL;
  1218. struct sde_hw_mdp *hw_mdp;
  1219. struct sde_encoder_irq *irq;
  1220. int i, ret = 0;
  1221. if (!p) {
  1222. ret = -EINVAL;
  1223. goto fail;
  1224. }
  1225. vid_enc = kzalloc(sizeof(*vid_enc), GFP_KERNEL);
  1226. if (!vid_enc) {
  1227. ret = -ENOMEM;
  1228. goto fail;
  1229. }
  1230. phys_enc = &vid_enc->base;
  1231. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  1232. if (IS_ERR_OR_NULL(hw_mdp)) {
  1233. ret = PTR_ERR(hw_mdp);
  1234. SDE_ERROR("failed to get mdptop\n");
  1235. goto fail;
  1236. }
  1237. phys_enc->hw_mdptop = hw_mdp;
  1238. phys_enc->intf_idx = p->intf_idx;
  1239. SDE_DEBUG_VIDENC(vid_enc, "\n");
  1240. sde_encoder_phys_vid_init_ops(&phys_enc->ops);
  1241. phys_enc->parent = p->parent;
  1242. phys_enc->parent_ops = p->parent_ops;
  1243. phys_enc->sde_kms = p->sde_kms;
  1244. phys_enc->split_role = p->split_role;
  1245. phys_enc->intf_mode = INTF_MODE_VIDEO;
  1246. phys_enc->enc_spinlock = p->enc_spinlock;
  1247. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  1248. phys_enc->comp_type = p->comp_type;
  1249. phys_enc->kickoff_timeout_ms = DEFAULT_KICKOFF_TIMEOUT_MS;
  1250. for (i = 0; i < INTR_IDX_MAX; i++) {
  1251. irq = &phys_enc->irq[i];
  1252. INIT_LIST_HEAD(&irq->cb.list);
  1253. irq->irq_idx = -EINVAL;
  1254. irq->hw_idx = -EINVAL;
  1255. irq->cb.arg = phys_enc;
  1256. }
  1257. irq = &phys_enc->irq[INTR_IDX_VSYNC];
  1258. irq->name = "vsync_irq";
  1259. irq->intr_type = SDE_IRQ_TYPE_INTF_VSYNC;
  1260. irq->intr_idx = INTR_IDX_VSYNC;
  1261. irq->cb.func = sde_encoder_phys_vid_vblank_irq;
  1262. irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
  1263. irq->name = "underrun";
  1264. irq->intr_type = SDE_IRQ_TYPE_INTF_UNDER_RUN;
  1265. irq->intr_idx = INTR_IDX_UNDERRUN;
  1266. irq->cb.func = sde_encoder_phys_vid_underrun_irq;
  1267. atomic_set(&phys_enc->vblank_refcount, 0);
  1268. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1269. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  1270. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  1271. phys_enc->enable_state = SDE_ENC_DISABLED;
  1272. SDE_DEBUG_VIDENC(vid_enc, "created intf idx:%d\n", p->intf_idx);
  1273. return phys_enc;
  1274. fail:
  1275. SDE_ERROR("failed to create encoder\n");
  1276. if (vid_enc)
  1277. sde_encoder_phys_vid_destroy(phys_enc);
  1278. return ERR_PTR(ret);
  1279. }