sde_encoder_phys_vid.c 45 KB

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