sde_encoder_phys_vid.c 40 KB

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