sde_encoder_phys_cmd.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  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 "sde_trace.h"
  11. #define SDE_DEBUG_CMDENC(e, fmt, ...) SDE_DEBUG("enc%d intf%d " fmt, \
  12. (e) && (e)->base.parent ? \
  13. (e)->base.parent->base.id : -1, \
  14. (e) ? (e)->base.intf_idx - INTF_0 : -1, ##__VA_ARGS__)
  15. #define SDE_ERROR_CMDENC(e, fmt, ...) SDE_ERROR("enc%d intf%d " fmt, \
  16. (e) && (e)->base.parent ? \
  17. (e)->base.parent->base.id : -1, \
  18. (e) ? (e)->base.intf_idx - INTF_0 : -1, ##__VA_ARGS__)
  19. #define to_sde_encoder_phys_cmd(x) \
  20. container_of(x, struct sde_encoder_phys_cmd, base)
  21. #define PP_TIMEOUT_MAX_TRIALS 4
  22. /*
  23. * Tearcheck sync start and continue thresholds are empirically found
  24. * based on common panels In the future, may want to allow panels to override
  25. * these default values
  26. */
  27. #define DEFAULT_TEARCHECK_SYNC_THRESH_START 4
  28. #define DEFAULT_TEARCHECK_SYNC_THRESH_CONTINUE 4
  29. #define SDE_ENC_WR_PTR_START_TIMEOUT_US 20000
  30. /*
  31. * Threshold for signalling retire fences in cases where
  32. * CTL_START_IRQ is received just after RD_PTR_IRQ
  33. */
  34. #define SDE_ENC_CTL_START_THRESHOLD_US 500
  35. #define SDE_ENC_MAX_POLL_TIMEOUT_US 2000
  36. static inline int _sde_encoder_phys_cmd_get_idle_timeout(
  37. struct sde_encoder_phys_cmd *cmd_enc)
  38. {
  39. return cmd_enc->autorefresh.cfg.frame_count ?
  40. cmd_enc->autorefresh.cfg.frame_count *
  41. KICKOFF_TIMEOUT_MS : KICKOFF_TIMEOUT_MS;
  42. }
  43. static inline bool sde_encoder_phys_cmd_is_master(
  44. struct sde_encoder_phys *phys_enc)
  45. {
  46. return (phys_enc->split_role != ENC_ROLE_SLAVE) ? true : false;
  47. }
  48. static bool sde_encoder_phys_cmd_mode_fixup(
  49. struct sde_encoder_phys *phys_enc,
  50. const struct drm_display_mode *mode,
  51. struct drm_display_mode *adj_mode)
  52. {
  53. if (phys_enc)
  54. SDE_DEBUG_CMDENC(to_sde_encoder_phys_cmd(phys_enc), "\n");
  55. return true;
  56. }
  57. static uint64_t _sde_encoder_phys_cmd_get_autorefresh_property(
  58. struct sde_encoder_phys *phys_enc)
  59. {
  60. struct drm_connector *conn = phys_enc->connector;
  61. if (!conn || !conn->state)
  62. return 0;
  63. return sde_connector_get_property(conn->state,
  64. CONNECTOR_PROP_AUTOREFRESH);
  65. }
  66. static void _sde_encoder_phys_cmd_config_autorefresh(
  67. struct sde_encoder_phys *phys_enc,
  68. u32 new_frame_count)
  69. {
  70. struct sde_encoder_phys_cmd *cmd_enc =
  71. to_sde_encoder_phys_cmd(phys_enc);
  72. struct sde_hw_pingpong *hw_pp = phys_enc->hw_pp;
  73. struct sde_hw_intf *hw_intf = phys_enc->hw_intf;
  74. struct drm_connector *conn = phys_enc->connector;
  75. struct sde_hw_autorefresh *cfg_cur, cfg_nxt;
  76. if (!conn || !conn->state || !hw_pp || !hw_intf)
  77. return;
  78. cfg_cur = &cmd_enc->autorefresh.cfg;
  79. /* autorefresh property value should be validated already */
  80. memset(&cfg_nxt, 0, sizeof(cfg_nxt));
  81. cfg_nxt.frame_count = new_frame_count;
  82. cfg_nxt.enable = (cfg_nxt.frame_count != 0);
  83. SDE_DEBUG_CMDENC(cmd_enc, "autorefresh state %d->%d framecount %d\n",
  84. cfg_cur->enable, cfg_nxt.enable, cfg_nxt.frame_count);
  85. SDE_EVT32(DRMID(phys_enc->parent), hw_pp->idx, hw_intf->idx,
  86. cfg_cur->enable, cfg_nxt.enable, cfg_nxt.frame_count);
  87. /* only proceed on state changes */
  88. if (cfg_nxt.enable == cfg_cur->enable)
  89. return;
  90. memcpy(cfg_cur, &cfg_nxt, sizeof(*cfg_cur));
  91. if (phys_enc->has_intf_te && hw_intf->ops.setup_autorefresh)
  92. hw_intf->ops.setup_autorefresh(hw_intf, cfg_cur);
  93. else if (hw_pp->ops.setup_autorefresh)
  94. hw_pp->ops.setup_autorefresh(hw_pp, cfg_cur);
  95. }
  96. static void _sde_encoder_phys_cmd_update_flush_mask(
  97. struct sde_encoder_phys *phys_enc)
  98. {
  99. struct sde_encoder_phys_cmd *cmd_enc;
  100. struct sde_hw_ctl *ctl;
  101. if (!phys_enc || !phys_enc->hw_intf || !phys_enc->hw_pp)
  102. return;
  103. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  104. ctl = phys_enc->hw_ctl;
  105. if (!ctl)
  106. return;
  107. if (!ctl->ops.update_bitmask_intf ||
  108. (test_bit(SDE_CTL_ACTIVE_CFG, &ctl->caps->features) &&
  109. !ctl->ops.update_bitmask_merge3d)) {
  110. SDE_ERROR("invalid hw_ctl ops %d\n", ctl->idx);
  111. return;
  112. }
  113. ctl->ops.update_bitmask_intf(ctl, phys_enc->intf_idx, 1);
  114. if (ctl->ops.update_bitmask_merge3d && phys_enc->hw_pp->merge_3d)
  115. ctl->ops.update_bitmask_merge3d(ctl,
  116. phys_enc->hw_pp->merge_3d->idx, 1);
  117. SDE_DEBUG_CMDENC(cmd_enc, "update pending flush ctl %d intf_idx %x\n",
  118. ctl->idx - CTL_0, phys_enc->intf_idx);
  119. }
  120. static void _sde_encoder_phys_cmd_update_intf_cfg(
  121. struct sde_encoder_phys *phys_enc)
  122. {
  123. struct sde_encoder_phys_cmd *cmd_enc =
  124. to_sde_encoder_phys_cmd(phys_enc);
  125. struct sde_hw_ctl *ctl;
  126. if (!phys_enc)
  127. return;
  128. ctl = phys_enc->hw_ctl;
  129. if (!ctl)
  130. return;
  131. if (ctl->ops.setup_intf_cfg) {
  132. struct sde_hw_intf_cfg intf_cfg = { 0 };
  133. intf_cfg.intf = phys_enc->intf_idx;
  134. intf_cfg.intf_mode_sel = SDE_CTL_MODE_SEL_CMD;
  135. intf_cfg.stream_sel = cmd_enc->stream_sel;
  136. intf_cfg.mode_3d =
  137. sde_encoder_helper_get_3d_blend_mode(phys_enc);
  138. ctl->ops.setup_intf_cfg(ctl, &intf_cfg);
  139. } else if (test_bit(SDE_CTL_ACTIVE_CFG, &ctl->caps->features)) {
  140. sde_encoder_helper_update_intf_cfg(phys_enc);
  141. }
  142. }
  143. static void sde_encoder_phys_cmd_pp_tx_done_irq(void *arg, int irq_idx)
  144. {
  145. struct sde_encoder_phys *phys_enc = arg;
  146. unsigned long lock_flags;
  147. int new_cnt;
  148. u32 event = SDE_ENCODER_FRAME_EVENT_DONE |
  149. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  150. if (!phys_enc || !phys_enc->hw_pp)
  151. return;
  152. SDE_ATRACE_BEGIN("pp_done_irq");
  153. /* notify all synchronous clients first, then asynchronous clients */
  154. if (phys_enc->parent_ops.handle_frame_done &&
  155. atomic_read(&phys_enc->pending_kickoff_cnt))
  156. phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
  157. phys_enc, event);
  158. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  159. new_cnt = atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
  160. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  161. SDE_EVT32_IRQ(DRMID(phys_enc->parent),
  162. phys_enc->hw_pp->idx - PINGPONG_0, new_cnt, event);
  163. /*
  164. * Reduce the refcount for the retire fence as well as for the ctl_start
  165. * if the counters are greater than zero. Signal retire fence if there
  166. * was a retire fence count pending and kickoff count is zero.
  167. */
  168. if (sde_encoder_phys_cmd_is_master(phys_enc) && (new_cnt == 0)) {
  169. while (atomic_add_unless(&phys_enc->pending_retire_fence_cnt,
  170. -1, 0)) {
  171. if (phys_enc->parent_ops.handle_frame_done)
  172. phys_enc->parent_ops.handle_frame_done(
  173. phys_enc->parent, phys_enc,
  174. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  175. atomic_add_unless(&phys_enc->pending_ctlstart_cnt,
  176. -1, 0);
  177. atomic_set(&phys_enc->ctlstart_timeout, 0);
  178. }
  179. }
  180. /* Signal any waiting atomic commit thread */
  181. wake_up_all(&phys_enc->pending_kickoff_wq);
  182. SDE_ATRACE_END("pp_done_irq");
  183. }
  184. static void sde_encoder_phys_cmd_autorefresh_done_irq(void *arg, int irq_idx)
  185. {
  186. struct sde_encoder_phys *phys_enc = arg;
  187. struct sde_encoder_phys_cmd *cmd_enc =
  188. to_sde_encoder_phys_cmd(phys_enc);
  189. unsigned long lock_flags;
  190. int new_cnt;
  191. if (!cmd_enc)
  192. return;
  193. phys_enc = &cmd_enc->base;
  194. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  195. new_cnt = atomic_add_unless(&cmd_enc->autorefresh.kickoff_cnt, -1, 0);
  196. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  197. SDE_EVT32_IRQ(DRMID(phys_enc->parent),
  198. phys_enc->hw_pp->idx - PINGPONG_0,
  199. phys_enc->hw_intf->idx - INTF_0,
  200. new_cnt);
  201. /* Signal any waiting atomic commit thread */
  202. wake_up_all(&cmd_enc->autorefresh.kickoff_wq);
  203. }
  204. static void sde_encoder_phys_cmd_te_rd_ptr_irq(void *arg, int irq_idx)
  205. {
  206. struct sde_encoder_phys *phys_enc = arg;
  207. struct sde_encoder_phys_cmd *cmd_enc;
  208. u32 event = 0, scheduler_status = INVALID_CTL_STATUS;
  209. struct sde_hw_ctl *ctl;
  210. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  211. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  212. return;
  213. SDE_ATRACE_BEGIN("rd_ptr_irq");
  214. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  215. ctl = phys_enc->hw_ctl;
  216. /**
  217. * signal only for master, when the ctl_start irq is
  218. * done and incremented the pending_rd_ptr_cnt.
  219. */
  220. if (sde_encoder_phys_cmd_is_master(phys_enc)
  221. && atomic_add_unless(&cmd_enc->pending_rd_ptr_cnt, -1, 0)
  222. && atomic_add_unless(
  223. &phys_enc->pending_retire_fence_cnt, -1, 0)) {
  224. event = SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  225. if (phys_enc->parent_ops.handle_frame_done)
  226. phys_enc->parent_ops.handle_frame_done(
  227. phys_enc->parent, phys_enc, event);
  228. }
  229. if (ctl && ctl->ops.get_scheduler_status)
  230. scheduler_status = ctl->ops.get_scheduler_status(ctl);
  231. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  232. SDE_EVT32_IRQ(DRMID(phys_enc->parent),
  233. info[0].pp_idx, info[0].intf_idx, info[0].wr_ptr_line_count,
  234. event, scheduler_status,
  235. info[1].pp_idx, info[1].intf_idx, info[1].wr_ptr_line_count);
  236. if (phys_enc->parent_ops.handle_vblank_virt)
  237. phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
  238. phys_enc);
  239. cmd_enc->rd_ptr_timestamp = ktime_get();
  240. atomic_add_unless(&cmd_enc->pending_vblank_cnt, -1, 0);
  241. wake_up_all(&cmd_enc->pending_vblank_wq);
  242. SDE_ATRACE_END("rd_ptr_irq");
  243. }
  244. static void sde_encoder_phys_cmd_ctl_start_irq(void *arg, int irq_idx)
  245. {
  246. struct sde_encoder_phys *phys_enc = arg;
  247. struct sde_encoder_phys_cmd *cmd_enc;
  248. struct sde_hw_ctl *ctl;
  249. u32 event = 0;
  250. s64 time_diff_us;
  251. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  252. if (!phys_enc || !phys_enc->hw_ctl)
  253. return;
  254. SDE_ATRACE_BEGIN("ctl_start_irq");
  255. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  256. ctl = phys_enc->hw_ctl;
  257. atomic_add_unless(&phys_enc->pending_ctlstart_cnt, -1, 0);
  258. atomic_set(&phys_enc->ctlstart_timeout, 0);
  259. time_diff_us = ktime_us_delta(ktime_get(), cmd_enc->rd_ptr_timestamp);
  260. /* handle retire fence based on only master */
  261. if (sde_encoder_phys_cmd_is_master(phys_enc)
  262. && atomic_read(&phys_enc->pending_retire_fence_cnt)) {
  263. /**
  264. * Handle rare cases where the ctl_start_irq is received
  265. * after rd_ptr_irq. If it falls within a threshold, it is
  266. * guaranteed the frame would be picked up in the current TE.
  267. * Signal retire fence immediately in such case. The threshold
  268. * timer adds extra line time duration based on lowest panel
  269. * fps for qsync enabled case.
  270. */
  271. if ((time_diff_us <= cmd_enc->ctl_start_threshold)
  272. && atomic_add_unless(
  273. &phys_enc->pending_retire_fence_cnt, -1, 0)) {
  274. event = SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  275. if (phys_enc->parent_ops.handle_frame_done)
  276. phys_enc->parent_ops.handle_frame_done(
  277. phys_enc->parent, phys_enc, event);
  278. /**
  279. * In ideal cases, ctl_start_irq is received before the
  280. * rd_ptr_irq, so set the atomic flag to indicate the event
  281. * and rd_ptr_irq will handle signalling the retire fence
  282. */
  283. } else {
  284. atomic_inc(&cmd_enc->pending_rd_ptr_cnt);
  285. }
  286. }
  287. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  288. SDE_EVT32_IRQ(DRMID(phys_enc->parent),
  289. ctl->idx - CTL_0, time_diff_us, event,
  290. info[0].pp_idx, info[0].intf_idx, info[0].wr_ptr_line_count,
  291. info[1].pp_idx, info[1].intf_idx, info[1].wr_ptr_line_count);
  292. /* Signal any waiting ctl start interrupt */
  293. wake_up_all(&phys_enc->pending_kickoff_wq);
  294. SDE_ATRACE_END("ctl_start_irq");
  295. }
  296. static void sde_encoder_phys_cmd_underrun_irq(void *arg, int irq_idx)
  297. {
  298. struct sde_encoder_phys *phys_enc = arg;
  299. if (!phys_enc)
  300. return;
  301. if (phys_enc->parent_ops.handle_underrun_virt)
  302. phys_enc->parent_ops.handle_underrun_virt(phys_enc->parent,
  303. phys_enc);
  304. }
  305. static void _sde_encoder_phys_cmd_setup_irq_hw_idx(
  306. struct sde_encoder_phys *phys_enc)
  307. {
  308. struct sde_encoder_irq *irq;
  309. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_ctl) {
  310. SDE_ERROR("invalid args %d %d\n", !phys_enc,
  311. phys_enc ? !phys_enc->hw_pp : 0);
  312. return;
  313. }
  314. if (phys_enc->has_intf_te && !phys_enc->hw_intf) {
  315. SDE_ERROR("invalid intf configuration\n");
  316. return;
  317. }
  318. irq = &phys_enc->irq[INTR_IDX_CTL_START];
  319. irq->hw_idx = phys_enc->hw_ctl->idx;
  320. irq->irq_idx = -EINVAL;
  321. irq = &phys_enc->irq[INTR_IDX_PINGPONG];
  322. irq->hw_idx = phys_enc->hw_pp->idx;
  323. irq->irq_idx = -EINVAL;
  324. irq = &phys_enc->irq[INTR_IDX_RDPTR];
  325. irq->irq_idx = -EINVAL;
  326. if (phys_enc->has_intf_te)
  327. irq->hw_idx = phys_enc->hw_intf->idx;
  328. else
  329. irq->hw_idx = phys_enc->hw_pp->idx;
  330. irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
  331. irq->hw_idx = phys_enc->intf_idx;
  332. irq->irq_idx = -EINVAL;
  333. irq = &phys_enc->irq[INTR_IDX_AUTOREFRESH_DONE];
  334. irq->irq_idx = -EINVAL;
  335. if (phys_enc->has_intf_te)
  336. irq->hw_idx = phys_enc->hw_intf->idx;
  337. else
  338. irq->hw_idx = phys_enc->hw_pp->idx;
  339. }
  340. static void sde_encoder_phys_cmd_cont_splash_mode_set(
  341. struct sde_encoder_phys *phys_enc,
  342. struct drm_display_mode *adj_mode)
  343. {
  344. struct sde_hw_intf *hw_intf;
  345. struct sde_hw_pingpong *hw_pp;
  346. struct sde_encoder_phys_cmd *cmd_enc;
  347. if (!phys_enc || !adj_mode) {
  348. SDE_ERROR("invalid args\n");
  349. return;
  350. }
  351. phys_enc->cached_mode = *adj_mode;
  352. phys_enc->enable_state = SDE_ENC_ENABLED;
  353. if (!phys_enc->hw_ctl || !phys_enc->hw_pp) {
  354. SDE_DEBUG("invalid ctl:%d pp:%d\n",
  355. (phys_enc->hw_ctl == NULL),
  356. (phys_enc->hw_pp == NULL));
  357. return;
  358. }
  359. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  360. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  361. hw_pp = phys_enc->hw_pp;
  362. hw_intf = phys_enc->hw_intf;
  363. if (phys_enc->has_intf_te && hw_intf &&
  364. hw_intf->ops.get_autorefresh) {
  365. hw_intf->ops.get_autorefresh(hw_intf,
  366. &cmd_enc->autorefresh.cfg);
  367. } else if (hw_pp && hw_pp->ops.get_autorefresh) {
  368. hw_pp->ops.get_autorefresh(hw_pp,
  369. &cmd_enc->autorefresh.cfg);
  370. }
  371. }
  372. _sde_encoder_phys_cmd_setup_irq_hw_idx(phys_enc);
  373. }
  374. static void sde_encoder_phys_cmd_mode_set(
  375. struct sde_encoder_phys *phys_enc,
  376. struct drm_display_mode *mode,
  377. struct drm_display_mode *adj_mode)
  378. {
  379. struct sde_encoder_phys_cmd *cmd_enc =
  380. to_sde_encoder_phys_cmd(phys_enc);
  381. struct sde_rm *rm = &phys_enc->sde_kms->rm;
  382. struct sde_rm_hw_iter iter;
  383. int i, instance;
  384. if (!phys_enc || !mode || !adj_mode) {
  385. SDE_ERROR("invalid args\n");
  386. return;
  387. }
  388. phys_enc->cached_mode = *adj_mode;
  389. SDE_DEBUG_CMDENC(cmd_enc, "caching mode:\n");
  390. drm_mode_debug_printmodeline(adj_mode);
  391. instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
  392. /* Retrieve previously allocated HW Resources. Shouldn't fail */
  393. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
  394. for (i = 0; i <= instance; i++) {
  395. if (sde_rm_get_hw(rm, &iter))
  396. phys_enc->hw_ctl = (struct sde_hw_ctl *)iter.hw;
  397. }
  398. if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
  399. SDE_ERROR_CMDENC(cmd_enc, "failed to init ctl: %ld\n",
  400. PTR_ERR(phys_enc->hw_ctl));
  401. phys_enc->hw_ctl = NULL;
  402. return;
  403. }
  404. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_INTF);
  405. for (i = 0; i <= instance; i++) {
  406. if (sde_rm_get_hw(rm, &iter))
  407. phys_enc->hw_intf = (struct sde_hw_intf *)iter.hw;
  408. }
  409. if (IS_ERR_OR_NULL(phys_enc->hw_intf)) {
  410. SDE_ERROR_CMDENC(cmd_enc, "failed to init intf: %ld\n",
  411. PTR_ERR(phys_enc->hw_intf));
  412. phys_enc->hw_intf = NULL;
  413. return;
  414. }
  415. _sde_encoder_phys_cmd_setup_irq_hw_idx(phys_enc);
  416. }
  417. static int _sde_encoder_phys_cmd_handle_ppdone_timeout(
  418. struct sde_encoder_phys *phys_enc,
  419. bool recovery_events)
  420. {
  421. struct sde_encoder_phys_cmd *cmd_enc =
  422. to_sde_encoder_phys_cmd(phys_enc);
  423. u32 frame_event = SDE_ENCODER_FRAME_EVENT_ERROR
  424. | SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  425. struct drm_connector *conn;
  426. int event;
  427. u32 pending_kickoff_cnt;
  428. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_ctl)
  429. return -EINVAL;
  430. conn = phys_enc->connector;
  431. if (atomic_read(&phys_enc->pending_kickoff_cnt) == 0)
  432. return 0;
  433. cmd_enc->pp_timeout_report_cnt++;
  434. pending_kickoff_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  435. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  436. /* trigger the retire fence if it was missed */
  437. if (atomic_add_unless(&phys_enc->pending_retire_fence_cnt,
  438. -1, 0))
  439. phys_enc->parent_ops.handle_frame_done(
  440. phys_enc->parent,
  441. phys_enc,
  442. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  443. atomic_add_unless(&phys_enc->pending_ctlstart_cnt, -1, 0);
  444. }
  445. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  446. cmd_enc->pp_timeout_report_cnt,
  447. pending_kickoff_cnt,
  448. frame_event);
  449. /* decrement the kickoff_cnt before checking for ESD status */
  450. atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
  451. /* check if panel is still sending TE signal or not */
  452. if (sde_connector_esd_status(phys_enc->connector))
  453. goto exit;
  454. /* to avoid flooding, only log first time, and "dead" time */
  455. if (cmd_enc->pp_timeout_report_cnt == 1) {
  456. SDE_ERROR_CMDENC(cmd_enc,
  457. "pp:%d kickoff timed out ctl %d koff_cnt %d\n",
  458. phys_enc->hw_pp->idx - PINGPONG_0,
  459. phys_enc->hw_ctl->idx - CTL_0,
  460. pending_kickoff_cnt);
  461. SDE_EVT32(DRMID(phys_enc->parent), SDE_EVTLOG_FATAL);
  462. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_RDPTR);
  463. if (sde_kms_is_secure_session_inprogress(phys_enc->sde_kms))
  464. SDE_DBG_DUMP("secure", "all", "dbg_bus");
  465. else
  466. SDE_DBG_DUMP("all", "dbg_bus", "vbif_dbg_bus");
  467. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_RDPTR);
  468. }
  469. /*
  470. * if the recovery event is registered by user, don't panic
  471. * trigger panic on first timeout if no listener registered
  472. */
  473. if (recovery_events) {
  474. event = cmd_enc->pp_timeout_report_cnt > PP_TIMEOUT_MAX_TRIALS ?
  475. SDE_RECOVERY_HARD_RESET : SDE_RECOVERY_CAPTURE;
  476. sde_connector_event_notify(conn, DRM_EVENT_SDE_HW_RECOVERY,
  477. sizeof(uint8_t), event);
  478. } else if (cmd_enc->pp_timeout_report_cnt) {
  479. SDE_DBG_DUMP("panic");
  480. }
  481. /* request a ctl reset before the next kickoff */
  482. phys_enc->enable_state = SDE_ENC_ERR_NEEDS_HW_RESET;
  483. exit:
  484. if (phys_enc->parent_ops.handle_frame_done)
  485. phys_enc->parent_ops.handle_frame_done(
  486. phys_enc->parent, phys_enc, frame_event);
  487. return -ETIMEDOUT;
  488. }
  489. static bool _sde_encoder_phys_is_ppsplit_slave(
  490. struct sde_encoder_phys *phys_enc)
  491. {
  492. if (!phys_enc)
  493. return false;
  494. return _sde_encoder_phys_is_ppsplit(phys_enc) &&
  495. phys_enc->split_role == ENC_ROLE_SLAVE;
  496. }
  497. static bool _sde_encoder_phys_is_disabling_ppsplit_slave(
  498. struct sde_encoder_phys *phys_enc)
  499. {
  500. enum sde_rm_topology_name old_top;
  501. if (!phys_enc || !phys_enc->connector ||
  502. phys_enc->split_role != ENC_ROLE_SLAVE)
  503. return false;
  504. old_top = sde_connector_get_old_topology_name(
  505. phys_enc->connector->state);
  506. return old_top == SDE_RM_TOPOLOGY_PPSPLIT;
  507. }
  508. static int _sde_encoder_phys_cmd_poll_write_pointer_started(
  509. struct sde_encoder_phys *phys_enc)
  510. {
  511. struct sde_encoder_phys_cmd *cmd_enc =
  512. to_sde_encoder_phys_cmd(phys_enc);
  513. struct sde_hw_pingpong *hw_pp = phys_enc->hw_pp;
  514. struct sde_hw_intf *hw_intf = phys_enc->hw_intf;
  515. struct sde_hw_pp_vsync_info info;
  516. u32 timeout_us = SDE_ENC_WR_PTR_START_TIMEOUT_US;
  517. int ret = 0;
  518. if (!hw_pp || !hw_intf)
  519. return 0;
  520. if (phys_enc->has_intf_te) {
  521. if (!hw_intf->ops.get_vsync_info ||
  522. !hw_intf->ops.poll_timeout_wr_ptr)
  523. goto end;
  524. } else {
  525. if (!hw_pp->ops.get_vsync_info ||
  526. !hw_pp->ops.poll_timeout_wr_ptr)
  527. goto end;
  528. }
  529. if (phys_enc->has_intf_te)
  530. ret = hw_intf->ops.get_vsync_info(hw_intf, &info);
  531. else
  532. ret = hw_pp->ops.get_vsync_info(hw_pp, &info);
  533. if (ret)
  534. return ret;
  535. SDE_DEBUG_CMDENC(cmd_enc,
  536. "pp:%d intf:%d rd_ptr %d wr_ptr %d\n",
  537. phys_enc->hw_pp->idx - PINGPONG_0,
  538. phys_enc->hw_intf->idx - INTF_0,
  539. info.rd_ptr_line_count,
  540. info.wr_ptr_line_count);
  541. SDE_EVT32_VERBOSE(DRMID(phys_enc->parent),
  542. phys_enc->hw_pp->idx - PINGPONG_0,
  543. phys_enc->hw_intf->idx - INTF_0,
  544. info.wr_ptr_line_count);
  545. if (phys_enc->has_intf_te)
  546. ret = hw_intf->ops.poll_timeout_wr_ptr(hw_intf, timeout_us);
  547. else
  548. ret = hw_pp->ops.poll_timeout_wr_ptr(hw_pp, timeout_us);
  549. if (ret) {
  550. SDE_EVT32(DRMID(phys_enc->parent),
  551. phys_enc->hw_pp->idx - PINGPONG_0,
  552. phys_enc->hw_intf->idx - INTF_0,
  553. timeout_us,
  554. ret);
  555. SDE_DBG_DUMP("all", "dbg_bus", "vbif_dbg_bus", "panic");
  556. }
  557. end:
  558. return ret;
  559. }
  560. static bool _sde_encoder_phys_cmd_is_ongoing_pptx(
  561. struct sde_encoder_phys *phys_enc)
  562. {
  563. struct sde_hw_pingpong *hw_pp;
  564. struct sde_hw_pp_vsync_info info;
  565. struct sde_hw_intf *hw_intf;
  566. if (!phys_enc)
  567. return false;
  568. if (phys_enc->has_intf_te) {
  569. hw_intf = phys_enc->hw_intf;
  570. if (!hw_intf || !hw_intf->ops.get_vsync_info)
  571. return false;
  572. hw_intf->ops.get_vsync_info(hw_intf, &info);
  573. } else {
  574. hw_pp = phys_enc->hw_pp;
  575. if (!hw_pp || !hw_pp->ops.get_vsync_info)
  576. return false;
  577. hw_pp->ops.get_vsync_info(hw_pp, &info);
  578. }
  579. SDE_EVT32(DRMID(phys_enc->parent),
  580. phys_enc->hw_pp->idx - PINGPONG_0,
  581. phys_enc->hw_intf->idx - INTF_0,
  582. atomic_read(&phys_enc->pending_kickoff_cnt),
  583. info.wr_ptr_line_count,
  584. phys_enc->cached_mode.vdisplay);
  585. if (info.wr_ptr_line_count > 0 && info.wr_ptr_line_count <
  586. phys_enc->cached_mode.vdisplay)
  587. return true;
  588. return false;
  589. }
  590. static int _sde_encoder_phys_cmd_wait_for_idle(
  591. struct sde_encoder_phys *phys_enc)
  592. {
  593. struct sde_encoder_phys_cmd *cmd_enc =
  594. to_sde_encoder_phys_cmd(phys_enc);
  595. struct sde_encoder_wait_info wait_info;
  596. bool recovery_events;
  597. int ret, i, pending_cnt;
  598. if (!phys_enc) {
  599. SDE_ERROR("invalid encoder\n");
  600. return -EINVAL;
  601. }
  602. wait_info.wq = &phys_enc->pending_kickoff_wq;
  603. wait_info.atomic_cnt = &phys_enc->pending_kickoff_cnt;
  604. wait_info.timeout_ms = KICKOFF_TIMEOUT_MS;
  605. recovery_events = sde_encoder_recovery_events_enabled(
  606. phys_enc->parent);
  607. /* slave encoder doesn't enable for ppsplit */
  608. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  609. return 0;
  610. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_PINGPONG,
  611. &wait_info);
  612. if (ret == -ETIMEDOUT) {
  613. pending_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  614. for (i = 0; i < pending_cnt; i++)
  615. _sde_encoder_phys_cmd_handle_ppdone_timeout(phys_enc,
  616. recovery_events);
  617. } else if (!ret) {
  618. if (cmd_enc->pp_timeout_report_cnt && recovery_events) {
  619. struct drm_connector *conn = phys_enc->connector;
  620. sde_connector_event_notify(conn,
  621. DRM_EVENT_SDE_HW_RECOVERY,
  622. sizeof(uint8_t),
  623. SDE_RECOVERY_SUCCESS);
  624. }
  625. cmd_enc->pp_timeout_report_cnt = 0;
  626. }
  627. return ret;
  628. }
  629. static int _sde_encoder_phys_cmd_wait_for_autorefresh_done(
  630. struct sde_encoder_phys *phys_enc)
  631. {
  632. struct sde_encoder_phys_cmd *cmd_enc =
  633. to_sde_encoder_phys_cmd(phys_enc);
  634. struct sde_encoder_wait_info wait_info;
  635. int ret = 0;
  636. if (!phys_enc) {
  637. SDE_ERROR("invalid encoder\n");
  638. return -EINVAL;
  639. }
  640. /* only master deals with autorefresh */
  641. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  642. return 0;
  643. wait_info.wq = &cmd_enc->autorefresh.kickoff_wq;
  644. wait_info.atomic_cnt = &cmd_enc->autorefresh.kickoff_cnt;
  645. wait_info.timeout_ms = _sde_encoder_phys_cmd_get_idle_timeout(cmd_enc);
  646. /* wait for autorefresh kickoff to start */
  647. ret = sde_encoder_helper_wait_for_irq(phys_enc,
  648. INTR_IDX_AUTOREFRESH_DONE, &wait_info);
  649. /* double check that kickoff has started by reading write ptr reg */
  650. if (!ret)
  651. ret = _sde_encoder_phys_cmd_poll_write_pointer_started(
  652. phys_enc);
  653. else
  654. sde_encoder_helper_report_irq_timeout(phys_enc,
  655. INTR_IDX_AUTOREFRESH_DONE);
  656. return ret;
  657. }
  658. static int sde_encoder_phys_cmd_control_vblank_irq(
  659. struct sde_encoder_phys *phys_enc,
  660. bool enable)
  661. {
  662. struct sde_encoder_phys_cmd *cmd_enc =
  663. to_sde_encoder_phys_cmd(phys_enc);
  664. int ret = 0;
  665. int refcount;
  666. if (!phys_enc || !phys_enc->hw_pp) {
  667. SDE_ERROR("invalid encoder\n");
  668. return -EINVAL;
  669. }
  670. mutex_lock(phys_enc->vblank_ctl_lock);
  671. refcount = atomic_read(&phys_enc->vblank_refcount);
  672. /* Slave encoders don't report vblank */
  673. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  674. goto end;
  675. /* protect against negative */
  676. if (!enable && refcount == 0) {
  677. ret = -EINVAL;
  678. goto end;
  679. }
  680. SDE_DEBUG_CMDENC(cmd_enc, "[%pS] enable=%d/%d\n",
  681. __builtin_return_address(0), enable, refcount);
  682. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  683. enable, refcount);
  684. if (enable && atomic_inc_return(&phys_enc->vblank_refcount) == 1)
  685. ret = sde_encoder_helper_register_irq(phys_enc, INTR_IDX_RDPTR);
  686. else if (!enable && atomic_dec_return(&phys_enc->vblank_refcount) == 0)
  687. ret = sde_encoder_helper_unregister_irq(phys_enc,
  688. INTR_IDX_RDPTR);
  689. end:
  690. if (ret) {
  691. SDE_ERROR_CMDENC(cmd_enc,
  692. "control vblank irq error %d, enable %d, refcount %d\n",
  693. ret, enable, refcount);
  694. SDE_EVT32(DRMID(phys_enc->parent),
  695. phys_enc->hw_pp->idx - PINGPONG_0,
  696. enable, refcount, SDE_EVTLOG_ERROR);
  697. }
  698. mutex_unlock(phys_enc->vblank_ctl_lock);
  699. return ret;
  700. }
  701. void sde_encoder_phys_cmd_irq_control(struct sde_encoder_phys *phys_enc,
  702. bool enable)
  703. {
  704. struct sde_encoder_phys_cmd *cmd_enc;
  705. if (!phys_enc)
  706. return;
  707. /**
  708. * pingpong split slaves do not register for IRQs
  709. * check old and new topologies
  710. */
  711. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc) ||
  712. _sde_encoder_phys_is_disabling_ppsplit_slave(phys_enc))
  713. return;
  714. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  715. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  716. enable, atomic_read(&phys_enc->vblank_refcount));
  717. if (enable) {
  718. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_PINGPONG);
  719. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_UNDERRUN);
  720. sde_encoder_phys_cmd_control_vblank_irq(phys_enc, true);
  721. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  722. sde_encoder_helper_register_irq(phys_enc,
  723. INTR_IDX_CTL_START);
  724. sde_encoder_helper_register_irq(phys_enc,
  725. INTR_IDX_AUTOREFRESH_DONE);
  726. }
  727. } else {
  728. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  729. sde_encoder_helper_unregister_irq(phys_enc,
  730. INTR_IDX_CTL_START);
  731. sde_encoder_helper_unregister_irq(phys_enc,
  732. INTR_IDX_AUTOREFRESH_DONE);
  733. }
  734. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_UNDERRUN);
  735. sde_encoder_phys_cmd_control_vblank_irq(phys_enc, false);
  736. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_PINGPONG);
  737. }
  738. }
  739. static int _get_tearcheck_threshold(struct sde_encoder_phys *phys_enc,
  740. u32 *extra_frame_trigger_time)
  741. {
  742. struct drm_connector *conn = phys_enc->connector;
  743. u32 qsync_mode;
  744. struct drm_display_mode *mode;
  745. u32 threshold_lines = 0;
  746. struct sde_encoder_phys_cmd *cmd_enc =
  747. to_sde_encoder_phys_cmd(phys_enc);
  748. *extra_frame_trigger_time = 0;
  749. if (!conn || !conn->state)
  750. return 0;
  751. mode = &phys_enc->cached_mode;
  752. qsync_mode = sde_connector_get_qsync_mode(conn);
  753. if (mode && (qsync_mode == SDE_RM_QSYNC_CONTINUOUS_MODE)) {
  754. u32 qsync_min_fps = 0;
  755. u32 default_fps = mode->vrefresh;
  756. u32 yres = mode->vtotal;
  757. u32 slow_time_ns;
  758. u32 default_time_ns;
  759. u32 extra_time_ns;
  760. u32 total_extra_lines;
  761. u32 default_line_time_ns;
  762. if (phys_enc->parent_ops.get_qsync_fps)
  763. phys_enc->parent_ops.get_qsync_fps(
  764. phys_enc->parent, &qsync_min_fps);
  765. if (!qsync_min_fps || !default_fps || !yres) {
  766. SDE_ERROR_CMDENC(cmd_enc,
  767. "wrong qsync params %d %d %d\n",
  768. qsync_min_fps, default_fps, yres);
  769. goto exit;
  770. }
  771. if (qsync_min_fps >= default_fps) {
  772. SDE_ERROR_CMDENC(cmd_enc,
  773. "qsync fps:%d must be less than default:%d\n",
  774. qsync_min_fps, default_fps);
  775. goto exit;
  776. }
  777. /* Calculate the number of extra lines*/
  778. slow_time_ns = (1 * 1000000000) / qsync_min_fps;
  779. default_time_ns = (1 * 1000000000) / default_fps;
  780. extra_time_ns = slow_time_ns - default_time_ns;
  781. default_line_time_ns = (1 * 1000000000) / (default_fps * yres);
  782. total_extra_lines = extra_time_ns / default_line_time_ns;
  783. threshold_lines += total_extra_lines;
  784. SDE_DEBUG_CMDENC(cmd_enc, "slow:%d default:%d extra:%d(ns)\n",
  785. slow_time_ns, default_time_ns, extra_time_ns);
  786. SDE_DEBUG_CMDENC(cmd_enc, "extra_lines:%d threshold:%d\n",
  787. total_extra_lines, threshold_lines);
  788. SDE_DEBUG_CMDENC(cmd_enc, "min_fps:%d fps:%d yres:%d\n",
  789. qsync_min_fps, default_fps, yres);
  790. SDE_EVT32(qsync_mode, qsync_min_fps, extra_time_ns, default_fps,
  791. yres, threshold_lines);
  792. *extra_frame_trigger_time = extra_time_ns;
  793. }
  794. exit:
  795. threshold_lines += DEFAULT_TEARCHECK_SYNC_THRESH_START;
  796. return threshold_lines;
  797. }
  798. static void sde_encoder_phys_cmd_tearcheck_config(
  799. struct sde_encoder_phys *phys_enc)
  800. {
  801. struct sde_encoder_phys_cmd *cmd_enc =
  802. to_sde_encoder_phys_cmd(phys_enc);
  803. struct sde_hw_tear_check tc_cfg = { 0 };
  804. struct drm_display_mode *mode;
  805. bool tc_enable = true;
  806. u32 vsync_hz, extra_frame_trigger_time;
  807. struct msm_drm_private *priv;
  808. struct sde_kms *sde_kms;
  809. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf) {
  810. SDE_ERROR("invalid encoder\n");
  811. return;
  812. }
  813. mode = &phys_enc->cached_mode;
  814. SDE_DEBUG_CMDENC(cmd_enc, "pp %d, intf %d\n",
  815. phys_enc->hw_pp->idx - PINGPONG_0,
  816. phys_enc->hw_intf->idx - INTF_0);
  817. if (phys_enc->has_intf_te) {
  818. if (!phys_enc->hw_intf->ops.setup_tearcheck ||
  819. !phys_enc->hw_intf->ops.enable_tearcheck) {
  820. SDE_DEBUG_CMDENC(cmd_enc, "tearcheck not supported\n");
  821. return;
  822. }
  823. } else {
  824. if (!phys_enc->hw_pp->ops.setup_tearcheck ||
  825. !phys_enc->hw_pp->ops.enable_tearcheck) {
  826. SDE_DEBUG_CMDENC(cmd_enc, "tearcheck not supported\n");
  827. return;
  828. }
  829. }
  830. sde_kms = phys_enc->sde_kms;
  831. if (!sde_kms || !sde_kms->dev || !sde_kms->dev->dev_private) {
  832. SDE_ERROR("invalid device\n");
  833. return;
  834. }
  835. priv = sde_kms->dev->dev_private;
  836. /*
  837. * TE default: dsi byte clock calculated base on 70 fps;
  838. * around 14 ms to complete a kickoff cycle if te disabled;
  839. * vclk_line base on 60 fps; write is faster than read;
  840. * init == start == rdptr;
  841. *
  842. * vsync_count is ratio of MDP VSYNC clock frequency to LCD panel
  843. * frequency divided by the no. of rows (lines) in the LCDpanel.
  844. */
  845. vsync_hz = sde_power_clk_get_rate(&priv->phandle, "vsync_clk");
  846. if (!vsync_hz || !mode->vtotal || !mode->vrefresh) {
  847. SDE_DEBUG_CMDENC(cmd_enc,
  848. "invalid params - vsync_hz %u vtot %u vrefresh %u\n",
  849. vsync_hz, mode->vtotal, mode->vrefresh);
  850. return;
  851. }
  852. tc_cfg.vsync_count = vsync_hz / (mode->vtotal * mode->vrefresh);
  853. /* enable external TE after kickoff to avoid premature autorefresh */
  854. tc_cfg.hw_vsync_mode = 0;
  855. /*
  856. * By setting sync_cfg_height to near max register value, we essentially
  857. * disable sde hw generated TE signal, since hw TE will arrive first.
  858. * Only caveat is if due to error, we hit wrap-around.
  859. */
  860. tc_cfg.sync_cfg_height = 0xFFF0;
  861. tc_cfg.vsync_init_val = mode->vdisplay;
  862. tc_cfg.sync_threshold_start = _get_tearcheck_threshold(phys_enc,
  863. &extra_frame_trigger_time);
  864. tc_cfg.sync_threshold_continue = DEFAULT_TEARCHECK_SYNC_THRESH_CONTINUE;
  865. tc_cfg.start_pos = mode->vdisplay;
  866. tc_cfg.rd_ptr_irq = mode->vdisplay + 1;
  867. cmd_enc->ctl_start_threshold = (extra_frame_trigger_time / 1000) +
  868. SDE_ENC_CTL_START_THRESHOLD_US;
  869. SDE_DEBUG_CMDENC(cmd_enc,
  870. "tc %d intf %d vsync_clk_speed_hz %u vtotal %u vrefresh %u\n",
  871. phys_enc->hw_pp->idx - PINGPONG_0,
  872. phys_enc->hw_intf->idx - INTF_0,
  873. vsync_hz, mode->vtotal, mode->vrefresh);
  874. SDE_DEBUG_CMDENC(cmd_enc,
  875. "tc %d intf %d enable %u start_pos %u rd_ptr_irq %u\n",
  876. phys_enc->hw_pp->idx - PINGPONG_0,
  877. phys_enc->hw_intf->idx - INTF_0,
  878. tc_enable, tc_cfg.start_pos, tc_cfg.rd_ptr_irq);
  879. SDE_DEBUG_CMDENC(cmd_enc,
  880. "tc %d intf %d hw_vsync_mode %u vsync_count %u vsync_init_val %u\n",
  881. phys_enc->hw_pp->idx - PINGPONG_0,
  882. phys_enc->hw_intf->idx - INTF_0,
  883. tc_cfg.hw_vsync_mode, tc_cfg.vsync_count,
  884. tc_cfg.vsync_init_val);
  885. SDE_DEBUG_CMDENC(cmd_enc,
  886. "tc %d intf %d cfgheight %u thresh_start %u thresh_cont %u ctl_start_threshold:%d\n",
  887. phys_enc->hw_pp->idx - PINGPONG_0,
  888. phys_enc->hw_intf->idx - INTF_0,
  889. tc_cfg.sync_cfg_height,
  890. tc_cfg.sync_threshold_start, tc_cfg.sync_threshold_continue,
  891. cmd_enc->ctl_start_threshold);
  892. if (phys_enc->has_intf_te) {
  893. phys_enc->hw_intf->ops.setup_tearcheck(phys_enc->hw_intf,
  894. &tc_cfg);
  895. phys_enc->hw_intf->ops.enable_tearcheck(phys_enc->hw_intf,
  896. tc_enable);
  897. } else {
  898. phys_enc->hw_pp->ops.setup_tearcheck(phys_enc->hw_pp, &tc_cfg);
  899. phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp,
  900. tc_enable);
  901. }
  902. }
  903. static void _sde_encoder_phys_cmd_pingpong_config(
  904. struct sde_encoder_phys *phys_enc)
  905. {
  906. struct sde_encoder_phys_cmd *cmd_enc =
  907. to_sde_encoder_phys_cmd(phys_enc);
  908. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
  909. SDE_ERROR("invalid arg(s), enc %d\n", !phys_enc);
  910. return;
  911. }
  912. SDE_DEBUG_CMDENC(cmd_enc, "pp %d, enabling mode:\n",
  913. phys_enc->hw_pp->idx - PINGPONG_0);
  914. drm_mode_debug_printmodeline(&phys_enc->cached_mode);
  915. if (!_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  916. _sde_encoder_phys_cmd_update_intf_cfg(phys_enc);
  917. sde_encoder_phys_cmd_tearcheck_config(phys_enc);
  918. }
  919. static void sde_encoder_phys_cmd_enable_helper(
  920. struct sde_encoder_phys *phys_enc)
  921. {
  922. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
  923. SDE_ERROR("invalid arg(s), encoder %d\n", !phys_enc);
  924. return;
  925. }
  926. sde_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
  927. _sde_encoder_phys_cmd_pingpong_config(phys_enc);
  928. /*
  929. * For pp-split, skip setting the flush bit for the slave intf, since
  930. * both intfs use same ctl and HW will only flush the master.
  931. */
  932. if (_sde_encoder_phys_is_ppsplit(phys_enc) &&
  933. !sde_encoder_phys_cmd_is_master(phys_enc))
  934. goto skip_flush;
  935. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  936. skip_flush:
  937. return;
  938. }
  939. static void sde_encoder_phys_cmd_enable(struct sde_encoder_phys *phys_enc)
  940. {
  941. struct sde_encoder_phys_cmd *cmd_enc =
  942. to_sde_encoder_phys_cmd(phys_enc);
  943. if (!phys_enc || !phys_enc->hw_pp) {
  944. SDE_ERROR("invalid phys encoder\n");
  945. return;
  946. }
  947. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  948. if (phys_enc->enable_state == SDE_ENC_ENABLED) {
  949. if (!phys_enc->cont_splash_enabled)
  950. SDE_ERROR("already enabled\n");
  951. return;
  952. }
  953. sde_encoder_phys_cmd_enable_helper(phys_enc);
  954. phys_enc->enable_state = SDE_ENC_ENABLED;
  955. }
  956. static bool sde_encoder_phys_cmd_is_autorefresh_enabled(
  957. struct sde_encoder_phys *phys_enc)
  958. {
  959. struct sde_hw_pingpong *hw_pp;
  960. struct sde_hw_intf *hw_intf;
  961. struct sde_hw_autorefresh cfg;
  962. int ret;
  963. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  964. return false;
  965. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  966. return false;
  967. if (phys_enc->has_intf_te) {
  968. hw_intf = phys_enc->hw_intf;
  969. if (!hw_intf->ops.get_autorefresh)
  970. return false;
  971. ret = hw_intf->ops.get_autorefresh(hw_intf, &cfg);
  972. } else {
  973. hw_pp = phys_enc->hw_pp;
  974. if (!hw_pp->ops.get_autorefresh)
  975. return false;
  976. ret = hw_pp->ops.get_autorefresh(hw_pp, &cfg);
  977. }
  978. if (ret)
  979. return false;
  980. return cfg.enable;
  981. }
  982. static void sde_encoder_phys_cmd_connect_te(
  983. struct sde_encoder_phys *phys_enc, bool enable)
  984. {
  985. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  986. return;
  987. if (phys_enc->has_intf_te &&
  988. phys_enc->hw_intf->ops.connect_external_te)
  989. phys_enc->hw_intf->ops.connect_external_te(phys_enc->hw_intf,
  990. enable);
  991. else if (phys_enc->hw_pp->ops.connect_external_te)
  992. phys_enc->hw_pp->ops.connect_external_te(phys_enc->hw_pp,
  993. enable);
  994. else
  995. return;
  996. SDE_EVT32(DRMID(phys_enc->parent), enable);
  997. }
  998. static int sde_encoder_phys_cmd_te_get_line_count(
  999. struct sde_encoder_phys *phys_enc)
  1000. {
  1001. struct sde_hw_pingpong *hw_pp;
  1002. struct sde_hw_intf *hw_intf;
  1003. u32 line_count;
  1004. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1005. return -EINVAL;
  1006. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1007. return -EINVAL;
  1008. if (phys_enc->has_intf_te) {
  1009. hw_intf = phys_enc->hw_intf;
  1010. if (!hw_intf->ops.get_line_count)
  1011. return -EINVAL;
  1012. line_count = hw_intf->ops.get_line_count(hw_intf);
  1013. } else {
  1014. hw_pp = phys_enc->hw_pp;
  1015. if (!hw_pp->ops.get_line_count)
  1016. return -EINVAL;
  1017. line_count = hw_pp->ops.get_line_count(hw_pp);
  1018. }
  1019. return line_count;
  1020. }
  1021. static int sde_encoder_phys_cmd_get_write_line_count(
  1022. struct sde_encoder_phys *phys_enc)
  1023. {
  1024. struct sde_hw_pingpong *hw_pp;
  1025. struct sde_hw_intf *hw_intf;
  1026. struct sde_hw_pp_vsync_info info;
  1027. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1028. return -EINVAL;
  1029. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1030. return -EINVAL;
  1031. if (phys_enc->has_intf_te) {
  1032. hw_intf = phys_enc->hw_intf;
  1033. if (!hw_intf->ops.get_vsync_info)
  1034. return -EINVAL;
  1035. if (hw_intf->ops.get_vsync_info(hw_intf, &info))
  1036. return -EINVAL;
  1037. } else {
  1038. hw_pp = phys_enc->hw_pp;
  1039. if (!hw_pp->ops.get_vsync_info)
  1040. return -EINVAL;
  1041. if (hw_pp->ops.get_vsync_info(hw_pp, &info))
  1042. return -EINVAL;
  1043. }
  1044. return (int)info.wr_ptr_line_count;
  1045. }
  1046. static void sde_encoder_phys_cmd_disable(struct sde_encoder_phys *phys_enc)
  1047. {
  1048. struct sde_encoder_phys_cmd *cmd_enc =
  1049. to_sde_encoder_phys_cmd(phys_enc);
  1050. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf) {
  1051. SDE_ERROR("invalid encoder\n");
  1052. return;
  1053. }
  1054. atomic_set(&phys_enc->ctlstart_timeout, 0);
  1055. SDE_DEBUG_CMDENC(cmd_enc, "pp %d intf %d state %d\n",
  1056. phys_enc->hw_pp->idx - PINGPONG_0,
  1057. phys_enc->hw_intf->idx - INTF_0,
  1058. phys_enc->enable_state);
  1059. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1060. phys_enc->hw_intf->idx - INTF_0,
  1061. phys_enc->enable_state);
  1062. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1063. SDE_ERROR_CMDENC(cmd_enc, "already disabled\n");
  1064. return;
  1065. }
  1066. if (phys_enc->has_intf_te && phys_enc->hw_intf->ops.enable_tearcheck)
  1067. phys_enc->hw_intf->ops.enable_tearcheck(
  1068. phys_enc->hw_intf,
  1069. false);
  1070. else if (phys_enc->hw_pp->ops.enable_tearcheck)
  1071. phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp,
  1072. false);
  1073. phys_enc->enable_state = SDE_ENC_DISABLED;
  1074. }
  1075. static void sde_encoder_phys_cmd_destroy(struct sde_encoder_phys *phys_enc)
  1076. {
  1077. struct sde_encoder_phys_cmd *cmd_enc =
  1078. to_sde_encoder_phys_cmd(phys_enc);
  1079. if (!phys_enc) {
  1080. SDE_ERROR("invalid encoder\n");
  1081. return;
  1082. }
  1083. kfree(cmd_enc);
  1084. }
  1085. static void sde_encoder_phys_cmd_get_hw_resources(
  1086. struct sde_encoder_phys *phys_enc,
  1087. struct sde_encoder_hw_resources *hw_res,
  1088. struct drm_connector_state *conn_state)
  1089. {
  1090. struct sde_encoder_phys_cmd *cmd_enc =
  1091. to_sde_encoder_phys_cmd(phys_enc);
  1092. if (!phys_enc) {
  1093. SDE_ERROR("invalid encoder\n");
  1094. return;
  1095. }
  1096. if ((phys_enc->intf_idx - INTF_0) >= INTF_MAX) {
  1097. SDE_ERROR("invalid intf idx:%d\n", phys_enc->intf_idx);
  1098. return;
  1099. }
  1100. SDE_DEBUG_CMDENC(cmd_enc, "\n");
  1101. hw_res->intfs[phys_enc->intf_idx - INTF_0] = INTF_MODE_CMD;
  1102. }
  1103. static int sde_encoder_phys_cmd_prepare_for_kickoff(
  1104. struct sde_encoder_phys *phys_enc,
  1105. struct sde_encoder_kickoff_params *params)
  1106. {
  1107. struct sde_hw_tear_check tc_cfg = {0};
  1108. struct sde_encoder_phys_cmd *cmd_enc =
  1109. to_sde_encoder_phys_cmd(phys_enc);
  1110. int ret = 0;
  1111. u32 extra_frame_trigger_time;
  1112. if (!phys_enc || !phys_enc->hw_pp) {
  1113. SDE_ERROR("invalid encoder\n");
  1114. return -EINVAL;
  1115. }
  1116. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  1117. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1118. atomic_read(&phys_enc->pending_kickoff_cnt),
  1119. atomic_read(&cmd_enc->autorefresh.kickoff_cnt));
  1120. if (phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_DEFAULT) {
  1121. /*
  1122. * Mark kickoff request as outstanding. If there are more
  1123. * than one outstanding frame, then we have to wait for the
  1124. * previous frame to complete
  1125. */
  1126. ret = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1127. if (ret) {
  1128. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1129. SDE_EVT32(DRMID(phys_enc->parent),
  1130. phys_enc->hw_pp->idx - PINGPONG_0);
  1131. SDE_ERROR("failed wait_for_idle: %d\n", ret);
  1132. }
  1133. }
  1134. if (sde_connector_is_qsync_updated(phys_enc->connector)) {
  1135. tc_cfg.sync_threshold_start =
  1136. _get_tearcheck_threshold(phys_enc,
  1137. &extra_frame_trigger_time);
  1138. if (phys_enc->has_intf_te &&
  1139. phys_enc->hw_intf->ops.update_tearcheck)
  1140. phys_enc->hw_intf->ops.update_tearcheck(
  1141. phys_enc->hw_intf, &tc_cfg);
  1142. else if (phys_enc->hw_pp->ops.update_tearcheck)
  1143. phys_enc->hw_pp->ops.update_tearcheck(
  1144. phys_enc->hw_pp, &tc_cfg);
  1145. cmd_enc->ctl_start_threshold =
  1146. (extra_frame_trigger_time / 1000) +
  1147. SDE_ENC_CTL_START_THRESHOLD_US;
  1148. SDE_EVT32(DRMID(phys_enc->parent),
  1149. tc_cfg.sync_threshold_start, cmd_enc->ctl_start_threshold);
  1150. }
  1151. SDE_DEBUG_CMDENC(cmd_enc, "pp:%d pending_cnt %d\n",
  1152. phys_enc->hw_pp->idx - PINGPONG_0,
  1153. atomic_read(&phys_enc->pending_kickoff_cnt));
  1154. return ret;
  1155. }
  1156. static int _sde_encoder_phys_cmd_wait_for_ctl_start(
  1157. struct sde_encoder_phys *phys_enc)
  1158. {
  1159. struct sde_encoder_phys_cmd *cmd_enc =
  1160. to_sde_encoder_phys_cmd(phys_enc);
  1161. struct sde_encoder_wait_info wait_info;
  1162. int ret;
  1163. bool frame_pending = true;
  1164. struct sde_hw_ctl *ctl;
  1165. if (!phys_enc || !phys_enc->hw_ctl) {
  1166. SDE_ERROR("invalid argument(s)\n");
  1167. return -EINVAL;
  1168. }
  1169. ctl = phys_enc->hw_ctl;
  1170. wait_info.wq = &phys_enc->pending_kickoff_wq;
  1171. wait_info.atomic_cnt = &phys_enc->pending_ctlstart_cnt;
  1172. wait_info.timeout_ms = KICKOFF_TIMEOUT_MS;
  1173. /* slave encoder doesn't enable for ppsplit */
  1174. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  1175. return 0;
  1176. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_CTL_START,
  1177. &wait_info);
  1178. if (ret == -ETIMEDOUT) {
  1179. struct sde_hw_ctl *ctl = phys_enc->hw_ctl;
  1180. if (ctl && ctl->ops.get_start_state)
  1181. frame_pending = ctl->ops.get_start_state(ctl);
  1182. if (frame_pending)
  1183. SDE_ERROR_CMDENC(cmd_enc,
  1184. "ctl start interrupt wait failed\n");
  1185. else
  1186. ret = 0;
  1187. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  1188. /*
  1189. * Signaling the retire fence at ctl start timeout
  1190. * to allow the next commit and avoid device freeze.
  1191. * As ctl start timeout can occurs due to no read ptr,
  1192. * updating pending_rd_ptr_cnt here may not cover all
  1193. * cases. Hence signaling the retire fence.
  1194. */
  1195. if (atomic_add_unless(
  1196. &phys_enc->pending_retire_fence_cnt, -1, 0))
  1197. phys_enc->parent_ops.handle_frame_done(
  1198. phys_enc->parent,
  1199. phys_enc,
  1200. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  1201. atomic_add_unless(
  1202. &phys_enc->pending_ctlstart_cnt, -1, 0);
  1203. atomic_inc_return(&phys_enc->ctlstart_timeout);
  1204. }
  1205. } else if ((ret == 0) &&
  1206. (phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_POSTED_START) &&
  1207. atomic_read(&phys_enc->pending_kickoff_cnt) &&
  1208. ctl->ops.get_scheduler_status &&
  1209. (ctl->ops.get_scheduler_status(ctl) & BIT(0)) &&
  1210. phys_enc->parent_ops.handle_frame_done) {
  1211. atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
  1212. phys_enc->parent_ops.handle_frame_done(
  1213. phys_enc->parent, phys_enc,
  1214. SDE_ENCODER_FRAME_EVENT_DONE |
  1215. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE);
  1216. }
  1217. return ret;
  1218. }
  1219. static int sde_encoder_phys_cmd_wait_for_tx_complete(
  1220. struct sde_encoder_phys *phys_enc)
  1221. {
  1222. int rc;
  1223. struct sde_encoder_phys_cmd *cmd_enc;
  1224. if (!phys_enc)
  1225. return -EINVAL;
  1226. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1227. rc = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1228. if (rc) {
  1229. SDE_EVT32(DRMID(phys_enc->parent),
  1230. phys_enc->intf_idx - INTF_0);
  1231. SDE_ERROR("failed wait_for_idle: %d\n", rc);
  1232. }
  1233. return rc;
  1234. }
  1235. static int sde_encoder_phys_cmd_wait_for_commit_done(
  1236. struct sde_encoder_phys *phys_enc)
  1237. {
  1238. int rc = 0;
  1239. struct sde_encoder_phys_cmd *cmd_enc;
  1240. if (!phys_enc)
  1241. return -EINVAL;
  1242. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1243. /* only required for master controller */
  1244. if (sde_encoder_phys_cmd_is_master(phys_enc))
  1245. rc = _sde_encoder_phys_cmd_wait_for_ctl_start(phys_enc);
  1246. if (!rc && sde_encoder_phys_cmd_is_master(phys_enc) &&
  1247. cmd_enc->autorefresh.cfg.enable)
  1248. rc = _sde_encoder_phys_cmd_wait_for_autorefresh_done(phys_enc);
  1249. /* wait for posted start or serialize trigger */
  1250. if ((atomic_read(&phys_enc->pending_kickoff_cnt) > 1) ||
  1251. (!rc && phys_enc->frame_trigger_mode ==
  1252. FRAME_DONE_WAIT_SERIALIZE)) {
  1253. rc = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1254. if (rc) {
  1255. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1256. SDE_EVT32(DRMID(phys_enc->parent),
  1257. phys_enc->hw_pp->idx - PINGPONG_0);
  1258. SDE_ERROR("failed wait_for_idle: %d\n", rc);
  1259. }
  1260. }
  1261. return rc;
  1262. }
  1263. static int sde_encoder_phys_cmd_wait_for_vblank(
  1264. struct sde_encoder_phys *phys_enc)
  1265. {
  1266. int rc = 0;
  1267. struct sde_encoder_phys_cmd *cmd_enc;
  1268. struct sde_encoder_wait_info wait_info;
  1269. if (!phys_enc)
  1270. return -EINVAL;
  1271. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1272. /* only required for master controller */
  1273. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1274. return rc;
  1275. wait_info.wq = &cmd_enc->pending_vblank_wq;
  1276. wait_info.atomic_cnt = &cmd_enc->pending_vblank_cnt;
  1277. wait_info.timeout_ms = _sde_encoder_phys_cmd_get_idle_timeout(cmd_enc);
  1278. atomic_inc(&cmd_enc->pending_vblank_cnt);
  1279. rc = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_RDPTR,
  1280. &wait_info);
  1281. return rc;
  1282. }
  1283. static void sde_encoder_phys_cmd_update_split_role(
  1284. struct sde_encoder_phys *phys_enc,
  1285. enum sde_enc_split_role role)
  1286. {
  1287. struct sde_encoder_phys_cmd *cmd_enc;
  1288. enum sde_enc_split_role old_role;
  1289. bool is_ppsplit;
  1290. if (!phys_enc)
  1291. return;
  1292. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1293. old_role = phys_enc->split_role;
  1294. is_ppsplit = _sde_encoder_phys_is_ppsplit(phys_enc);
  1295. phys_enc->split_role = role;
  1296. SDE_DEBUG_CMDENC(cmd_enc, "old role %d new role %d\n",
  1297. old_role, role);
  1298. /*
  1299. * ppsplit solo needs to reprogram because intf may have swapped without
  1300. * role changing on left-only, right-only back-to-back commits
  1301. */
  1302. if (!(is_ppsplit && role == ENC_ROLE_SOLO) &&
  1303. (role == old_role || role == ENC_ROLE_SKIP))
  1304. return;
  1305. sde_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
  1306. _sde_encoder_phys_cmd_pingpong_config(phys_enc);
  1307. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  1308. }
  1309. static void sde_encoder_phys_cmd_prepare_commit(
  1310. struct sde_encoder_phys *phys_enc)
  1311. {
  1312. struct sde_encoder_phys_cmd *cmd_enc =
  1313. to_sde_encoder_phys_cmd(phys_enc);
  1314. int trial = 0;
  1315. if (!phys_enc)
  1316. return;
  1317. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1318. return;
  1319. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1320. cmd_enc->autorefresh.cfg.enable);
  1321. if (!sde_encoder_phys_cmd_is_autorefresh_enabled(phys_enc))
  1322. return;
  1323. /*
  1324. * If autorefresh is enabled, disable it and make sure it is safe to
  1325. * proceed with current frame commit/push. Sequence fallowed is,
  1326. * 1. Disable TE
  1327. * 2. Disable autorefresh config
  1328. * 4. Poll for frame transfer ongoing to be false
  1329. * 5. Enable TE back
  1330. */
  1331. sde_encoder_phys_cmd_connect_te(phys_enc, false);
  1332. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, 0);
  1333. do {
  1334. udelay(SDE_ENC_MAX_POLL_TIMEOUT_US);
  1335. if ((trial * SDE_ENC_MAX_POLL_TIMEOUT_US)
  1336. > (KICKOFF_TIMEOUT_MS * USEC_PER_MSEC)) {
  1337. SDE_ERROR_CMDENC(cmd_enc,
  1338. "disable autorefresh failed\n");
  1339. break;
  1340. }
  1341. trial++;
  1342. } while (_sde_encoder_phys_cmd_is_ongoing_pptx(phys_enc));
  1343. sde_encoder_phys_cmd_connect_te(phys_enc, true);
  1344. SDE_DEBUG_CMDENC(cmd_enc, "disabled autorefresh\n");
  1345. }
  1346. static void sde_encoder_phys_cmd_trigger_start(
  1347. struct sde_encoder_phys *phys_enc)
  1348. {
  1349. struct sde_encoder_phys_cmd *cmd_enc =
  1350. to_sde_encoder_phys_cmd(phys_enc);
  1351. u32 frame_cnt;
  1352. if (!phys_enc)
  1353. return;
  1354. /* we don't issue CTL_START when using autorefresh */
  1355. frame_cnt = _sde_encoder_phys_cmd_get_autorefresh_property(phys_enc);
  1356. if (frame_cnt) {
  1357. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, frame_cnt);
  1358. atomic_inc(&cmd_enc->autorefresh.kickoff_cnt);
  1359. } else {
  1360. sde_encoder_helper_trigger_start(phys_enc);
  1361. }
  1362. }
  1363. static void sde_encoder_phys_cmd_setup_vsync_source(
  1364. struct sde_encoder_phys *phys_enc,
  1365. u32 vsync_source, bool is_dummy)
  1366. {
  1367. if (!phys_enc || !phys_enc->hw_intf)
  1368. return;
  1369. sde_encoder_helper_vsync_config(phys_enc, vsync_source, is_dummy);
  1370. if (phys_enc->has_intf_te && phys_enc->hw_intf->ops.vsync_sel)
  1371. phys_enc->hw_intf->ops.vsync_sel(phys_enc->hw_intf,
  1372. vsync_source);
  1373. }
  1374. static void sde_encoder_phys_cmd_init_ops(struct sde_encoder_phys_ops *ops)
  1375. {
  1376. ops->prepare_commit = sde_encoder_phys_cmd_prepare_commit;
  1377. ops->is_master = sde_encoder_phys_cmd_is_master;
  1378. ops->mode_set = sde_encoder_phys_cmd_mode_set;
  1379. ops->cont_splash_mode_set = sde_encoder_phys_cmd_cont_splash_mode_set;
  1380. ops->mode_fixup = sde_encoder_phys_cmd_mode_fixup;
  1381. ops->enable = sde_encoder_phys_cmd_enable;
  1382. ops->disable = sde_encoder_phys_cmd_disable;
  1383. ops->destroy = sde_encoder_phys_cmd_destroy;
  1384. ops->get_hw_resources = sde_encoder_phys_cmd_get_hw_resources;
  1385. ops->control_vblank_irq = sde_encoder_phys_cmd_control_vblank_irq;
  1386. ops->wait_for_commit_done = sde_encoder_phys_cmd_wait_for_commit_done;
  1387. ops->prepare_for_kickoff = sde_encoder_phys_cmd_prepare_for_kickoff;
  1388. ops->wait_for_tx_complete = sde_encoder_phys_cmd_wait_for_tx_complete;
  1389. ops->wait_for_vblank = sde_encoder_phys_cmd_wait_for_vblank;
  1390. ops->trigger_flush = sde_encoder_helper_trigger_flush;
  1391. ops->trigger_start = sde_encoder_phys_cmd_trigger_start;
  1392. ops->needs_single_flush = sde_encoder_phys_needs_single_flush;
  1393. ops->hw_reset = sde_encoder_helper_hw_reset;
  1394. ops->irq_control = sde_encoder_phys_cmd_irq_control;
  1395. ops->update_split_role = sde_encoder_phys_cmd_update_split_role;
  1396. ops->restore = sde_encoder_phys_cmd_enable_helper;
  1397. ops->control_te = sde_encoder_phys_cmd_connect_te;
  1398. ops->is_autorefresh_enabled =
  1399. sde_encoder_phys_cmd_is_autorefresh_enabled;
  1400. ops->get_line_count = sde_encoder_phys_cmd_te_get_line_count;
  1401. ops->get_wr_line_count = sde_encoder_phys_cmd_get_write_line_count;
  1402. ops->wait_for_active = NULL;
  1403. ops->setup_vsync_source = sde_encoder_phys_cmd_setup_vsync_source;
  1404. ops->setup_misr = sde_encoder_helper_setup_misr;
  1405. ops->collect_misr = sde_encoder_helper_collect_misr;
  1406. }
  1407. struct sde_encoder_phys *sde_encoder_phys_cmd_init(
  1408. struct sde_enc_phys_init_params *p)
  1409. {
  1410. struct sde_encoder_phys *phys_enc = NULL;
  1411. struct sde_encoder_phys_cmd *cmd_enc = NULL;
  1412. struct sde_hw_mdp *hw_mdp;
  1413. struct sde_encoder_irq *irq;
  1414. int i, ret = 0;
  1415. SDE_DEBUG("intf %d\n", p->intf_idx - INTF_0);
  1416. cmd_enc = kzalloc(sizeof(*cmd_enc), GFP_KERNEL);
  1417. if (!cmd_enc) {
  1418. ret = -ENOMEM;
  1419. SDE_ERROR("failed to allocate\n");
  1420. goto fail;
  1421. }
  1422. phys_enc = &cmd_enc->base;
  1423. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  1424. if (IS_ERR_OR_NULL(hw_mdp)) {
  1425. ret = PTR_ERR(hw_mdp);
  1426. SDE_ERROR("failed to get mdptop\n");
  1427. goto fail_mdp_init;
  1428. }
  1429. phys_enc->hw_mdptop = hw_mdp;
  1430. phys_enc->intf_idx = p->intf_idx;
  1431. phys_enc->parent = p->parent;
  1432. phys_enc->parent_ops = p->parent_ops;
  1433. phys_enc->sde_kms = p->sde_kms;
  1434. phys_enc->split_role = p->split_role;
  1435. phys_enc->intf_mode = INTF_MODE_CMD;
  1436. phys_enc->enc_spinlock = p->enc_spinlock;
  1437. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  1438. cmd_enc->stream_sel = 0;
  1439. cmd_enc->ctl_start_threshold = SDE_ENC_CTL_START_THRESHOLD_US;
  1440. phys_enc->enable_state = SDE_ENC_DISABLED;
  1441. sde_encoder_phys_cmd_init_ops(&phys_enc->ops);
  1442. phys_enc->comp_type = p->comp_type;
  1443. if (sde_hw_intf_te_supported(phys_enc->sde_kms->catalog))
  1444. phys_enc->has_intf_te = true;
  1445. else
  1446. phys_enc->has_intf_te = false;
  1447. for (i = 0; i < INTR_IDX_MAX; i++) {
  1448. irq = &phys_enc->irq[i];
  1449. INIT_LIST_HEAD(&irq->cb.list);
  1450. irq->irq_idx = -EINVAL;
  1451. irq->hw_idx = -EINVAL;
  1452. irq->cb.arg = phys_enc;
  1453. }
  1454. irq = &phys_enc->irq[INTR_IDX_CTL_START];
  1455. irq->name = "ctl_start";
  1456. irq->intr_type = SDE_IRQ_TYPE_CTL_START;
  1457. irq->intr_idx = INTR_IDX_CTL_START;
  1458. irq->cb.func = sde_encoder_phys_cmd_ctl_start_irq;
  1459. irq = &phys_enc->irq[INTR_IDX_PINGPONG];
  1460. irq->name = "pp_done";
  1461. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_COMP;
  1462. irq->intr_idx = INTR_IDX_PINGPONG;
  1463. irq->cb.func = sde_encoder_phys_cmd_pp_tx_done_irq;
  1464. irq = &phys_enc->irq[INTR_IDX_RDPTR];
  1465. irq->intr_idx = INTR_IDX_RDPTR;
  1466. irq->name = "te_rd_ptr";
  1467. if (phys_enc->has_intf_te)
  1468. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_RD_PTR;
  1469. else
  1470. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_RD_PTR;
  1471. irq->cb.func = sde_encoder_phys_cmd_te_rd_ptr_irq;
  1472. irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
  1473. irq->name = "underrun";
  1474. irq->intr_type = SDE_IRQ_TYPE_INTF_UNDER_RUN;
  1475. irq->intr_idx = INTR_IDX_UNDERRUN;
  1476. irq->cb.func = sde_encoder_phys_cmd_underrun_irq;
  1477. irq = &phys_enc->irq[INTR_IDX_AUTOREFRESH_DONE];
  1478. irq->name = "autorefresh_done";
  1479. if (phys_enc->has_intf_te)
  1480. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_AUTO_REF;
  1481. else
  1482. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_AUTO_REF;
  1483. irq->intr_idx = INTR_IDX_AUTOREFRESH_DONE;
  1484. irq->cb.func = sde_encoder_phys_cmd_autorefresh_done_irq;
  1485. atomic_set(&phys_enc->vblank_refcount, 0);
  1486. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1487. atomic_set(&phys_enc->pending_ctlstart_cnt, 0);
  1488. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  1489. atomic_set(&cmd_enc->pending_rd_ptr_cnt, 0);
  1490. atomic_set(&cmd_enc->pending_vblank_cnt, 0);
  1491. atomic_set(&phys_enc->ctlstart_timeout, 0);
  1492. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  1493. init_waitqueue_head(&cmd_enc->pending_vblank_wq);
  1494. atomic_set(&cmd_enc->autorefresh.kickoff_cnt, 0);
  1495. init_waitqueue_head(&cmd_enc->autorefresh.kickoff_wq);
  1496. SDE_DEBUG_CMDENC(cmd_enc, "created\n");
  1497. return phys_enc;
  1498. fail_mdp_init:
  1499. kfree(cmd_enc);
  1500. fail:
  1501. return ERR_PTR(ret);
  1502. }