sde_encoder_phys_vid.c 38 KB

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