sde_encoder_phys_vid.c 39 KB

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