sde_encoder_phys_vid.c 43 KB

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