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