sde_encoder_phys_vid.c 41 KB

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