sde_encoder_phys_cmd.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
  7. #include "sde_encoder_phys.h"
  8. #include "sde_hw_interrupts.h"
  9. #include "sde_core_irq.h"
  10. #include "sde_formats.h"
  11. #include "sde_trace.h"
  12. #define SDE_DEBUG_CMDENC(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.intf_idx - INTF_0 : -1, ##__VA_ARGS__)
  16. #define SDE_ERROR_CMDENC(e, fmt, ...) SDE_ERROR("enc%d intf%d " fmt, \
  17. (e) && (e)->base.parent ? \
  18. (e)->base.parent->base.id : -1, \
  19. (e) ? (e)->base.intf_idx - INTF_0 : -1, ##__VA_ARGS__)
  20. #define to_sde_encoder_phys_cmd(x) \
  21. container_of(x, struct sde_encoder_phys_cmd, base)
  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. #define AUTOREFRESH_SEQ1_POLL_TIME 2000
  31. #define AUTOREFRESH_SEQ2_POLL_TIME 25000
  32. #define AUTOREFRESH_SEQ2_POLL_TIMEOUT 1000000
  33. static inline int _sde_encoder_phys_cmd_get_idle_timeout(
  34. struct sde_encoder_phys *phys_enc)
  35. {
  36. u32 timeout = phys_enc->kickoff_timeout_ms;
  37. struct sde_encoder_phys_cmd *cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  38. return cmd_enc->autorefresh.cfg.frame_count ?
  39. cmd_enc->autorefresh.cfg.frame_count * timeout : timeout;
  40. }
  41. static inline bool sde_encoder_phys_cmd_is_master(
  42. struct sde_encoder_phys *phys_enc)
  43. {
  44. return (phys_enc->split_role != ENC_ROLE_SLAVE) ? true : false;
  45. }
  46. static bool sde_encoder_phys_cmd_mode_fixup(
  47. struct sde_encoder_phys *phys_enc,
  48. const struct drm_display_mode *mode,
  49. struct drm_display_mode *adj_mode)
  50. {
  51. if (phys_enc)
  52. SDE_DEBUG_CMDENC(to_sde_encoder_phys_cmd(phys_enc), "\n");
  53. return true;
  54. }
  55. static uint64_t _sde_encoder_phys_cmd_get_autorefresh_property(
  56. struct sde_encoder_phys *phys_enc)
  57. {
  58. struct drm_connector *conn = phys_enc->connector;
  59. if (!conn || !conn->state)
  60. return 0;
  61. return sde_connector_get_property(conn->state,
  62. CONNECTOR_PROP_AUTOREFRESH);
  63. }
  64. static void _sde_encoder_phys_cmd_config_autorefresh(
  65. struct sde_encoder_phys *phys_enc,
  66. u32 new_frame_count)
  67. {
  68. struct sde_encoder_phys_cmd *cmd_enc =
  69. to_sde_encoder_phys_cmd(phys_enc);
  70. struct sde_hw_pingpong *hw_pp = phys_enc->hw_pp;
  71. struct sde_hw_intf *hw_intf = phys_enc->hw_intf;
  72. struct drm_connector *conn = phys_enc->connector;
  73. struct sde_hw_autorefresh *cfg_cur, cfg_nxt;
  74. if (!conn || !conn->state || !hw_pp || !hw_intf)
  75. return;
  76. cfg_cur = &cmd_enc->autorefresh.cfg;
  77. /* autorefresh property value should be validated already */
  78. memset(&cfg_nxt, 0, sizeof(cfg_nxt));
  79. cfg_nxt.frame_count = new_frame_count;
  80. cfg_nxt.enable = (cfg_nxt.frame_count != 0);
  81. SDE_DEBUG_CMDENC(cmd_enc, "autorefresh state %d->%d framecount %d\n",
  82. cfg_cur->enable, cfg_nxt.enable, cfg_nxt.frame_count);
  83. SDE_EVT32(DRMID(phys_enc->parent), hw_pp->idx, hw_intf->idx,
  84. cfg_cur->enable, cfg_nxt.enable, cfg_nxt.frame_count);
  85. /* only proceed on state changes */
  86. if (cfg_nxt.enable == cfg_cur->enable)
  87. return;
  88. memcpy(cfg_cur, &cfg_nxt, sizeof(*cfg_cur));
  89. if (phys_enc->has_intf_te && hw_intf->ops.setup_autorefresh)
  90. hw_intf->ops.setup_autorefresh(hw_intf, cfg_cur);
  91. else if (hw_pp->ops.setup_autorefresh)
  92. hw_pp->ops.setup_autorefresh(hw_pp, cfg_cur);
  93. }
  94. static void _sde_encoder_phys_cmd_update_flush_mask(
  95. struct sde_encoder_phys *phys_enc)
  96. {
  97. struct sde_encoder_phys_cmd *cmd_enc;
  98. struct sde_hw_ctl *ctl;
  99. if (!phys_enc || !phys_enc->hw_intf || !phys_enc->hw_pp)
  100. return;
  101. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  102. ctl = phys_enc->hw_ctl;
  103. if (!ctl)
  104. return;
  105. if (!ctl->ops.update_bitmask) {
  106. SDE_ERROR("invalid hw_ctl ops %d\n", ctl->idx);
  107. return;
  108. }
  109. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_INTF, phys_enc->intf_idx, 1);
  110. if (phys_enc->hw_pp->merge_3d)
  111. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_MERGE_3D,
  112. phys_enc->hw_pp->merge_3d->idx, 1);
  113. SDE_DEBUG_CMDENC(cmd_enc, "update pending flush ctl %d intf_idx %x\n",
  114. ctl->idx - CTL_0, phys_enc->intf_idx);
  115. }
  116. static void _sde_encoder_phys_cmd_update_intf_cfg(
  117. struct sde_encoder_phys *phys_enc)
  118. {
  119. struct sde_encoder_phys_cmd *cmd_enc =
  120. to_sde_encoder_phys_cmd(phys_enc);
  121. struct sde_hw_ctl *ctl;
  122. if (!phys_enc)
  123. return;
  124. ctl = phys_enc->hw_ctl;
  125. if (!ctl)
  126. return;
  127. if (ctl->ops.setup_intf_cfg) {
  128. struct sde_hw_intf_cfg intf_cfg = { 0 };
  129. intf_cfg.intf = phys_enc->intf_idx;
  130. intf_cfg.intf_mode_sel = SDE_CTL_MODE_SEL_CMD;
  131. intf_cfg.stream_sel = cmd_enc->stream_sel;
  132. intf_cfg.mode_3d =
  133. sde_encoder_helper_get_3d_blend_mode(phys_enc);
  134. ctl->ops.setup_intf_cfg(ctl, &intf_cfg);
  135. } else if (test_bit(SDE_CTL_ACTIVE_CFG, &ctl->caps->features)) {
  136. sde_encoder_helper_update_intf_cfg(phys_enc);
  137. }
  138. }
  139. static void sde_encoder_override_tearcheck_rd_ptr(struct sde_encoder_phys *phys_enc)
  140. {
  141. struct sde_hw_intf *hw_intf;
  142. struct drm_display_mode *mode;
  143. struct sde_encoder_phys_cmd *cmd_enc;
  144. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  145. u32 adjusted_tear_rd_ptr_line_cnt;
  146. if (!phys_enc || !phys_enc->hw_intf)
  147. return;
  148. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  149. hw_intf = phys_enc->hw_intf;
  150. mode = &phys_enc->cached_mode;
  151. /* Configure TE rd_ptr_val to the end of qsync Start Window.
  152. * This ensures next frame trigger_start does not get latched in the current
  153. * vsync window.
  154. */
  155. adjusted_tear_rd_ptr_line_cnt = mode->vdisplay + cmd_enc->qsync_threshold_lines + 1;
  156. if (hw_intf && hw_intf->ops.override_tear_rd_ptr_val)
  157. hw_intf->ops.override_tear_rd_ptr_val(hw_intf, adjusted_tear_rd_ptr_line_cnt);
  158. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  159. SDE_EVT32_VERBOSE(phys_enc->hw_intf->idx - INTF_0, mode->vdisplay,
  160. cmd_enc->qsync_threshold_lines, info[0].rd_ptr_line_count,
  161. info[0].rd_ptr_frame_count, info[0].wr_ptr_line_count,
  162. info[1].rd_ptr_line_count, info[1].rd_ptr_frame_count, info[1].wr_ptr_line_count);
  163. }
  164. static void _sde_encoder_phys_signal_frame_done(struct sde_encoder_phys *phys_enc)
  165. {
  166. struct sde_encoder_phys_cmd *cmd_enc;
  167. struct sde_hw_ctl *ctl;
  168. u32 scheduler_status = INVALID_CTL_STATUS, event = 0;
  169. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  170. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  171. ctl = phys_enc->hw_ctl;
  172. if (!ctl)
  173. return;
  174. /* notify all synchronous clients first, then asynchronous clients */
  175. if (phys_enc->parent_ops.handle_frame_done &&
  176. atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0)) {
  177. event = SDE_ENCODER_FRAME_EVENT_DONE |
  178. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  179. spin_lock(phys_enc->enc_spinlock);
  180. phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
  181. phys_enc, event);
  182. if (cmd_enc->frame_tx_timeout_report_cnt)
  183. phys_enc->recovered = true;
  184. spin_unlock(phys_enc->enc_spinlock);
  185. }
  186. if (ctl->ops.get_scheduler_status)
  187. scheduler_status = ctl->ops.get_scheduler_status(ctl);
  188. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  189. SDE_EVT32_IRQ(DRMID(phys_enc->parent), ctl->idx - CTL_0, phys_enc->hw_pp->idx - PINGPONG_0,
  190. event, scheduler_status, phys_enc->autorefresh_disable_trans, info[0].pp_idx,
  191. info[0].intf_idx, info[0].intf_frame_count, info[0].wr_ptr_line_count,
  192. info[0].rd_ptr_line_count, info[1].pp_idx, info[1].intf_idx,
  193. info[1].intf_frame_count, info[1].wr_ptr_line_count, info[1].rd_ptr_line_count);
  194. /*
  195. * For hw-fences, in the last frame during the autorefresh disable transition
  196. * hw won't trigger the output-fence signal once the frame is done, therefore
  197. * sw must trigger the override to force the signal here
  198. */
  199. if (phys_enc->autorefresh_disable_trans) {
  200. if (ctl->ops.trigger_output_fence_override)
  201. ctl->ops.trigger_output_fence_override(ctl);
  202. phys_enc->autorefresh_disable_trans = false;
  203. }
  204. /* Signal any waiting atomic commit thread */
  205. wake_up_all(&phys_enc->pending_kickoff_wq);
  206. }
  207. static void sde_encoder_phys_cmd_ctl_done_irq(void *arg, int irq_idx)
  208. {
  209. struct sde_encoder_phys *phys_enc = arg;
  210. if (!phys_enc)
  211. return;
  212. SDE_ATRACE_BEGIN("ctl_done_irq");
  213. _sde_encoder_phys_signal_frame_done(phys_enc);
  214. SDE_ATRACE_END("ctl_done_irq");
  215. }
  216. static void sde_encoder_phys_cmd_pp_tx_done_irq(void *arg, int irq_idx)
  217. {
  218. struct sde_encoder_phys *phys_enc = arg;
  219. if (!phys_enc || !phys_enc->hw_pp)
  220. return;
  221. SDE_ATRACE_BEGIN("pp_done_irq");
  222. _sde_encoder_phys_signal_frame_done(phys_enc);
  223. SDE_ATRACE_END("pp_done_irq");
  224. }
  225. static void sde_encoder_phys_cmd_autorefresh_done_irq(void *arg, int irq_idx)
  226. {
  227. struct sde_encoder_phys *phys_enc = arg;
  228. struct sde_encoder_phys_cmd *cmd_enc =
  229. to_sde_encoder_phys_cmd(phys_enc);
  230. unsigned long lock_flags;
  231. int new_cnt;
  232. if (!cmd_enc)
  233. return;
  234. phys_enc = &cmd_enc->base;
  235. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  236. new_cnt = atomic_add_unless(&cmd_enc->autorefresh.kickoff_cnt, -1, 0);
  237. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  238. SDE_EVT32_IRQ(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  239. phys_enc->hw_intf->idx - INTF_0, new_cnt);
  240. if (new_cnt)
  241. _sde_encoder_phys_signal_frame_done(phys_enc);
  242. /* Signal any waiting atomic commit thread */
  243. wake_up_all(&cmd_enc->autorefresh.kickoff_wq);
  244. }
  245. static void sde_encoder_phys_cmd_te_rd_ptr_irq(void *arg, int irq_idx)
  246. {
  247. struct sde_encoder_phys *phys_enc = arg;
  248. struct sde_encoder_phys_cmd *cmd_enc;
  249. u32 scheduler_status = INVALID_CTL_STATUS;
  250. struct sde_hw_ctl *ctl;
  251. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  252. struct sde_encoder_phys_cmd_te_timestamp *te_timestamp;
  253. unsigned long lock_flags;
  254. u32 fence_ready = 0;
  255. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf || !phys_enc->hw_ctl)
  256. return;
  257. SDE_ATRACE_BEGIN("rd_ptr_irq");
  258. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  259. ctl = phys_enc->hw_ctl;
  260. if (ctl->ops.get_scheduler_status)
  261. scheduler_status = ctl->ops.get_scheduler_status(ctl);
  262. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  263. te_timestamp = list_first_entry_or_null(&cmd_enc->te_timestamp_list,
  264. struct sde_encoder_phys_cmd_te_timestamp, list);
  265. if (te_timestamp) {
  266. list_del_init(&te_timestamp->list);
  267. te_timestamp->timestamp = ktime_get();
  268. list_add_tail(&te_timestamp->list, &cmd_enc->te_timestamp_list);
  269. }
  270. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  271. if ((scheduler_status != 0x1) && ctl->ops.get_hw_fence_status)
  272. fence_ready = ctl->ops.get_hw_fence_status(ctl);
  273. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  274. SDE_EVT32_IRQ(DRMID(phys_enc->parent), scheduler_status, fence_ready, info[0].pp_idx,
  275. info[0].intf_idx, info[0].intf_frame_count, info[0].wr_ptr_line_count,
  276. info[0].rd_ptr_line_count, info[1].pp_idx, info[1].intf_idx,
  277. info[1].intf_frame_count, info[1].wr_ptr_line_count, info[1].rd_ptr_line_count);
  278. if (phys_enc->parent_ops.handle_vblank_virt)
  279. phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
  280. phys_enc);
  281. atomic_add_unless(&cmd_enc->pending_vblank_cnt, -1, 0);
  282. wake_up_all(&cmd_enc->pending_vblank_wq);
  283. SDE_ATRACE_END("rd_ptr_irq");
  284. }
  285. static void sde_encoder_phys_cmd_wr_ptr_irq(void *arg, int irq_idx)
  286. {
  287. struct sde_encoder_phys *phys_enc = arg;
  288. struct sde_hw_ctl *ctl;
  289. u32 event = 0, qsync_mode = 0;
  290. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  291. if (!phys_enc || !phys_enc->hw_ctl)
  292. return;
  293. SDE_ATRACE_BEGIN("wr_ptr_irq");
  294. ctl = phys_enc->hw_ctl;
  295. qsync_mode = sde_connector_get_qsync_mode(phys_enc->connector);
  296. if (atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0)) {
  297. event = SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  298. if (phys_enc->parent_ops.handle_frame_done) {
  299. spin_lock(phys_enc->enc_spinlock);
  300. phys_enc->parent_ops.handle_frame_done(
  301. phys_enc->parent, phys_enc, event);
  302. spin_unlock(phys_enc->enc_spinlock);
  303. }
  304. }
  305. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  306. SDE_EVT32_IRQ(DRMID(phys_enc->parent), ctl->idx - CTL_0, event, qsync_mode,
  307. info[0].pp_idx, info[0].intf_idx, info[0].intf_frame_count,
  308. info[0].wr_ptr_line_count, info[0].rd_ptr_line_count, info[1].pp_idx,
  309. info[1].intf_idx, info[1].intf_frame_count, info[1].wr_ptr_line_count,
  310. info[1].rd_ptr_line_count);
  311. if (qsync_mode)
  312. sde_encoder_override_tearcheck_rd_ptr(phys_enc);
  313. /* Signal any waiting wr_ptr start interrupt */
  314. wake_up_all(&phys_enc->pending_kickoff_wq);
  315. SDE_ATRACE_END("wr_ptr_irq");
  316. }
  317. static void _sde_encoder_phys_cmd_setup_irq_hw_idx(
  318. struct sde_encoder_phys *phys_enc)
  319. {
  320. struct sde_encoder_irq *irq;
  321. struct sde_kms *sde_kms;
  322. if (!phys_enc->sde_kms || !phys_enc->hw_pp || !phys_enc->hw_ctl) {
  323. SDE_ERROR("invalid args %d %d %d\n", !phys_enc->sde_kms,
  324. !phys_enc->hw_pp, !phys_enc->hw_ctl);
  325. return;
  326. }
  327. if (phys_enc->has_intf_te && !phys_enc->hw_intf) {
  328. SDE_ERROR("invalid intf configuration\n");
  329. return;
  330. }
  331. sde_kms = phys_enc->sde_kms;
  332. irq = &phys_enc->irq[INTR_IDX_CTL_START];
  333. irq->hw_idx = phys_enc->hw_ctl->idx;
  334. irq = &phys_enc->irq[INTR_IDX_CTL_DONE];
  335. irq->hw_idx = phys_enc->hw_ctl->idx;
  336. irq = &phys_enc->irq[INTR_IDX_PINGPONG];
  337. irq->hw_idx = phys_enc->hw_pp->idx;
  338. irq = &phys_enc->irq[INTR_IDX_RDPTR];
  339. if (phys_enc->has_intf_te)
  340. irq->hw_idx = phys_enc->hw_intf->idx;
  341. else
  342. irq->hw_idx = phys_enc->hw_pp->idx;
  343. irq = &phys_enc->irq[INTR_IDX_AUTOREFRESH_DONE];
  344. if (phys_enc->has_intf_te)
  345. irq->hw_idx = phys_enc->hw_intf->idx;
  346. else
  347. irq->hw_idx = phys_enc->hw_pp->idx;
  348. irq = &phys_enc->irq[INTR_IDX_WRPTR];
  349. if (phys_enc->has_intf_te)
  350. irq->hw_idx = phys_enc->hw_intf->idx;
  351. else
  352. irq->hw_idx = phys_enc->hw_pp->idx;
  353. }
  354. static void sde_encoder_phys_cmd_cont_splash_mode_set(
  355. struct sde_encoder_phys *phys_enc,
  356. struct drm_display_mode *adj_mode)
  357. {
  358. struct sde_hw_intf *hw_intf;
  359. struct sde_hw_pingpong *hw_pp;
  360. struct sde_encoder_phys_cmd *cmd_enc;
  361. if (!phys_enc || !adj_mode) {
  362. SDE_ERROR("invalid args\n");
  363. return;
  364. }
  365. phys_enc->cached_mode = *adj_mode;
  366. phys_enc->enable_state = SDE_ENC_ENABLED;
  367. if (!phys_enc->hw_ctl || !phys_enc->hw_pp) {
  368. SDE_DEBUG("invalid ctl:%d pp:%d\n",
  369. (phys_enc->hw_ctl == NULL),
  370. (phys_enc->hw_pp == NULL));
  371. return;
  372. }
  373. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  374. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  375. hw_pp = phys_enc->hw_pp;
  376. hw_intf = phys_enc->hw_intf;
  377. if (phys_enc->has_intf_te && hw_intf &&
  378. hw_intf->ops.get_autorefresh) {
  379. hw_intf->ops.get_autorefresh(hw_intf,
  380. &cmd_enc->autorefresh.cfg);
  381. } else if (hw_pp && hw_pp->ops.get_autorefresh) {
  382. hw_pp->ops.get_autorefresh(hw_pp,
  383. &cmd_enc->autorefresh.cfg);
  384. }
  385. if (hw_intf && hw_intf->ops.reset_counter)
  386. hw_intf->ops.reset_counter(hw_intf);
  387. }
  388. _sde_encoder_phys_cmd_setup_irq_hw_idx(phys_enc);
  389. }
  390. static void sde_encoder_phys_cmd_mode_set(
  391. struct sde_encoder_phys *phys_enc,
  392. struct drm_display_mode *mode,
  393. struct drm_display_mode *adj_mode, bool *reinit_mixers)
  394. {
  395. struct sde_encoder_phys_cmd *cmd_enc =
  396. to_sde_encoder_phys_cmd(phys_enc);
  397. struct sde_rm *rm = &phys_enc->sde_kms->rm;
  398. struct sde_rm_hw_iter iter;
  399. int i, instance;
  400. if (!phys_enc || !mode || !adj_mode) {
  401. SDE_ERROR("invalid args\n");
  402. return;
  403. }
  404. phys_enc->cached_mode = *adj_mode;
  405. SDE_DEBUG_CMDENC(cmd_enc, "caching mode:\n");
  406. drm_mode_debug_printmodeline(adj_mode);
  407. instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
  408. /* Retrieve previously allocated HW Resources. Shouldn't fail */
  409. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
  410. for (i = 0; i <= instance; i++) {
  411. if (sde_rm_get_hw(rm, &iter)) {
  412. if (phys_enc->hw_ctl && phys_enc->hw_ctl != to_sde_hw_ctl(iter.hw)) {
  413. *reinit_mixers = true;
  414. SDE_EVT32(phys_enc->hw_ctl->idx,
  415. to_sde_hw_ctl(iter.hw)->idx);
  416. }
  417. phys_enc->hw_ctl = to_sde_hw_ctl(iter.hw);
  418. }
  419. }
  420. if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
  421. SDE_ERROR_CMDENC(cmd_enc, "failed to init ctl: %ld\n",
  422. PTR_ERR(phys_enc->hw_ctl));
  423. phys_enc->hw_ctl = NULL;
  424. return;
  425. }
  426. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_INTF);
  427. for (i = 0; i <= instance; i++) {
  428. if (sde_rm_get_hw(rm, &iter))
  429. phys_enc->hw_intf = to_sde_hw_intf(iter.hw);
  430. }
  431. if (IS_ERR_OR_NULL(phys_enc->hw_intf)) {
  432. SDE_ERROR_CMDENC(cmd_enc, "failed to init intf: %ld\n",
  433. PTR_ERR(phys_enc->hw_intf));
  434. phys_enc->hw_intf = NULL;
  435. return;
  436. }
  437. _sde_encoder_phys_cmd_setup_irq_hw_idx(phys_enc);
  438. phys_enc->kickoff_timeout_ms =
  439. sde_encoder_helper_get_kickoff_timeout_ms(phys_enc->parent);
  440. }
  441. static int _sde_encoder_phys_cmd_handle_framedone_timeout(
  442. struct sde_encoder_phys *phys_enc)
  443. {
  444. struct sde_encoder_phys_cmd *cmd_enc =
  445. to_sde_encoder_phys_cmd(phys_enc);
  446. bool recovery_events = sde_encoder_recovery_events_enabled(
  447. phys_enc->parent);
  448. u32 frame_event = SDE_ENCODER_FRAME_EVENT_ERROR
  449. | SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  450. struct drm_connector *conn;
  451. u32 pending_kickoff_cnt;
  452. unsigned long lock_flags;
  453. if (!phys_enc->hw_pp || !phys_enc->hw_ctl)
  454. return -EINVAL;
  455. conn = phys_enc->connector;
  456. /* decrement the kickoff_cnt before checking for ESD status */
  457. if (!atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0))
  458. return 0;
  459. cmd_enc->frame_tx_timeout_report_cnt++;
  460. pending_kickoff_cnt = atomic_read(&phys_enc->pending_kickoff_cnt) + 1;
  461. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  462. cmd_enc->frame_tx_timeout_report_cnt,
  463. pending_kickoff_cnt,
  464. frame_event);
  465. /* check if panel is still sending TE signal or not */
  466. if (sde_connector_esd_status(phys_enc->connector))
  467. goto exit;
  468. /* to avoid flooding, only log first time, and "dead" time */
  469. if (cmd_enc->frame_tx_timeout_report_cnt == 1) {
  470. SDE_ERROR_CMDENC(cmd_enc,
  471. "pp:%d kickoff timed out ctl %d koff_cnt %d\n",
  472. phys_enc->hw_pp->idx - PINGPONG_0,
  473. phys_enc->hw_ctl->idx - CTL_0,
  474. pending_kickoff_cnt);
  475. SDE_EVT32(DRMID(phys_enc->parent), SDE_EVTLOG_FATAL);
  476. mutex_lock(phys_enc->vblank_ctl_lock);
  477. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_RDPTR);
  478. if (sde_kms_is_secure_session_inprogress(phys_enc->sde_kms))
  479. SDE_DBG_DUMP(SDE_DBG_BUILT_IN_ALL, "secure");
  480. else
  481. SDE_DBG_DUMP(SDE_DBG_BUILT_IN_ALL);
  482. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_RDPTR);
  483. mutex_unlock(phys_enc->vblank_ctl_lock);
  484. }
  485. /*
  486. * if the recovery event is registered by user, don't panic
  487. * trigger panic on first timeout if no listener registered
  488. */
  489. if (recovery_events)
  490. sde_connector_event_notify(conn, DRM_EVENT_SDE_HW_RECOVERY,
  491. sizeof(uint8_t), SDE_RECOVERY_CAPTURE);
  492. else if (cmd_enc->frame_tx_timeout_report_cnt)
  493. SDE_DBG_DUMP(0x0, "panic");
  494. /* request a ctl reset before the next kickoff */
  495. phys_enc->enable_state = SDE_ENC_ERR_NEEDS_HW_RESET;
  496. exit:
  497. if (phys_enc->parent_ops.handle_frame_done) {
  498. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  499. phys_enc->parent_ops.handle_frame_done(
  500. phys_enc->parent, phys_enc, frame_event);
  501. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  502. }
  503. return -ETIMEDOUT;
  504. }
  505. static bool _sde_encoder_phys_is_ppsplit_slave(
  506. struct sde_encoder_phys *phys_enc)
  507. {
  508. if (!phys_enc)
  509. return false;
  510. return _sde_encoder_phys_is_ppsplit(phys_enc) &&
  511. phys_enc->split_role == ENC_ROLE_SLAVE;
  512. }
  513. static bool _sde_encoder_phys_is_disabling_ppsplit_slave(
  514. struct sde_encoder_phys *phys_enc)
  515. {
  516. enum sde_rm_topology_name old_top;
  517. if (!phys_enc || !phys_enc->connector ||
  518. phys_enc->split_role != ENC_ROLE_SLAVE)
  519. return false;
  520. old_top = sde_connector_get_old_topology_name(
  521. phys_enc->connector->state);
  522. return old_top == SDE_RM_TOPOLOGY_PPSPLIT;
  523. }
  524. static int _sde_encoder_phys_cmd_poll_write_pointer_started(
  525. struct sde_encoder_phys *phys_enc)
  526. {
  527. struct sde_encoder_phys_cmd *cmd_enc =
  528. to_sde_encoder_phys_cmd(phys_enc);
  529. struct sde_hw_pingpong *hw_pp = phys_enc->hw_pp;
  530. struct sde_hw_intf *hw_intf = phys_enc->hw_intf;
  531. struct sde_hw_pp_vsync_info info;
  532. u32 timeout_us = SDE_ENC_WR_PTR_START_TIMEOUT_US;
  533. int ret = 0;
  534. if (!hw_pp || !hw_intf)
  535. return 0;
  536. if (phys_enc->has_intf_te) {
  537. if (!hw_intf->ops.get_vsync_info ||
  538. !hw_intf->ops.poll_timeout_wr_ptr)
  539. goto end;
  540. } else {
  541. if (!hw_pp->ops.get_vsync_info ||
  542. !hw_pp->ops.poll_timeout_wr_ptr)
  543. goto end;
  544. }
  545. if (phys_enc->has_intf_te)
  546. ret = hw_intf->ops.get_vsync_info(hw_intf, &info);
  547. else
  548. ret = hw_pp->ops.get_vsync_info(hw_pp, &info);
  549. if (ret)
  550. return ret;
  551. SDE_DEBUG_CMDENC(cmd_enc,
  552. "pp:%d intf:%d rd_ptr %d wr_ptr %d\n",
  553. phys_enc->hw_pp->idx - PINGPONG_0,
  554. phys_enc->hw_intf->idx - INTF_0,
  555. info.rd_ptr_line_count,
  556. info.wr_ptr_line_count);
  557. SDE_EVT32_VERBOSE(DRMID(phys_enc->parent),
  558. phys_enc->hw_pp->idx - PINGPONG_0,
  559. phys_enc->hw_intf->idx - INTF_0,
  560. info.wr_ptr_line_count);
  561. if (phys_enc->has_intf_te)
  562. ret = hw_intf->ops.poll_timeout_wr_ptr(hw_intf, timeout_us);
  563. else
  564. ret = hw_pp->ops.poll_timeout_wr_ptr(hw_pp, timeout_us);
  565. if (ret) {
  566. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  567. phys_enc->hw_intf->idx - INTF_0, timeout_us, ret);
  568. SDE_DBG_DUMP(SDE_DBG_BUILT_IN_ALL, "panic");
  569. }
  570. end:
  571. return ret;
  572. }
  573. static bool _sde_encoder_phys_cmd_is_ongoing_pptx(
  574. struct sde_encoder_phys *phys_enc)
  575. {
  576. struct sde_hw_pingpong *hw_pp;
  577. struct sde_hw_pp_vsync_info info;
  578. struct sde_hw_intf *hw_intf;
  579. if (!phys_enc)
  580. return false;
  581. if (phys_enc->has_intf_te) {
  582. hw_intf = phys_enc->hw_intf;
  583. if (!hw_intf || !hw_intf->ops.get_vsync_info)
  584. return false;
  585. hw_intf->ops.get_vsync_info(hw_intf, &info);
  586. } else {
  587. hw_pp = phys_enc->hw_pp;
  588. if (!hw_pp || !hw_pp->ops.get_vsync_info)
  589. return false;
  590. hw_pp->ops.get_vsync_info(hw_pp, &info);
  591. }
  592. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  593. phys_enc->hw_intf->idx - INTF_0, atomic_read(&phys_enc->pending_kickoff_cnt),
  594. info.wr_ptr_line_count, info.intf_frame_count, phys_enc->cached_mode.vdisplay);
  595. if (info.wr_ptr_line_count > 0 && info.wr_ptr_line_count <
  596. phys_enc->cached_mode.vdisplay)
  597. return true;
  598. return false;
  599. }
  600. static bool _sde_encoder_phys_cmd_is_scheduler_idle(
  601. struct sde_encoder_phys *phys_enc)
  602. {
  603. bool wr_ptr_wait_success = true;
  604. unsigned long lock_flags;
  605. bool ret = false;
  606. struct sde_encoder_phys_cmd *cmd_enc =
  607. to_sde_encoder_phys_cmd(phys_enc);
  608. struct sde_hw_ctl *ctl = phys_enc->hw_ctl;
  609. enum frame_trigger_mode_type frame_trigger_mode =
  610. phys_enc->frame_trigger_mode;
  611. if (sde_encoder_phys_cmd_is_master(phys_enc))
  612. wr_ptr_wait_success = cmd_enc->wr_ptr_wait_success;
  613. /*
  614. * Handle cases where a pp-done interrupt is missed
  615. * due to irq latency with POSTED start
  616. */
  617. if (wr_ptr_wait_success &&
  618. (frame_trigger_mode == FRAME_DONE_WAIT_POSTED_START) &&
  619. ctl->ops.get_scheduler_status &&
  620. phys_enc->parent_ops.handle_frame_done &&
  621. atomic_read(&phys_enc->pending_kickoff_cnt) > 0 &&
  622. (ctl->ops.get_scheduler_status(ctl) & BIT(0)) &&
  623. atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0)) {
  624. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  625. phys_enc->parent_ops.handle_frame_done(
  626. phys_enc->parent, phys_enc,
  627. SDE_ENCODER_FRAME_EVENT_DONE |
  628. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE);
  629. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  630. SDE_EVT32(DRMID(phys_enc->parent),
  631. phys_enc->hw_pp->idx - PINGPONG_0,
  632. phys_enc->hw_intf->idx - INTF_0,
  633. atomic_read(&phys_enc->pending_kickoff_cnt));
  634. ret = true;
  635. }
  636. return ret;
  637. }
  638. static int _sde_encoder_phys_cmd_wait_for_idle(
  639. struct sde_encoder_phys *phys_enc)
  640. {
  641. struct sde_encoder_wait_info wait_info = {0};
  642. enum sde_intr_idx intr_idx;
  643. int ret;
  644. if (!phys_enc) {
  645. SDE_ERROR("invalid encoder\n");
  646. return -EINVAL;
  647. }
  648. if (sde_encoder_check_ctl_done_support(phys_enc->parent)
  649. && !sde_encoder_phys_cmd_is_master(phys_enc))
  650. return 0;
  651. if (atomic_read(&phys_enc->pending_kickoff_cnt) > 1)
  652. wait_info.count_check = 1;
  653. wait_info.wq = &phys_enc->pending_kickoff_wq;
  654. wait_info.atomic_cnt = &phys_enc->pending_kickoff_cnt;
  655. wait_info.timeout_ms = phys_enc->kickoff_timeout_ms;
  656. /* slave encoder doesn't enable for ppsplit */
  657. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  658. return 0;
  659. if (_sde_encoder_phys_cmd_is_scheduler_idle(phys_enc))
  660. return 0;
  661. intr_idx = sde_encoder_check_ctl_done_support(phys_enc->parent) ?
  662. INTR_IDX_CTL_DONE : INTR_IDX_PINGPONG;
  663. ret = sde_encoder_helper_wait_for_irq(phys_enc, intr_idx, &wait_info);
  664. if (ret == -ETIMEDOUT) {
  665. if (_sde_encoder_phys_cmd_is_scheduler_idle(phys_enc))
  666. return 0;
  667. _sde_encoder_phys_cmd_handle_framedone_timeout(phys_enc);
  668. }
  669. return ret;
  670. }
  671. static int _sde_encoder_phys_cmd_wait_for_autorefresh_done(
  672. struct sde_encoder_phys *phys_enc)
  673. {
  674. struct sde_encoder_phys_cmd *cmd_enc =
  675. to_sde_encoder_phys_cmd(phys_enc);
  676. struct sde_encoder_wait_info wait_info = {0};
  677. int ret = 0;
  678. if (!phys_enc) {
  679. SDE_ERROR("invalid encoder\n");
  680. return -EINVAL;
  681. }
  682. /* only master deals with autorefresh */
  683. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  684. return 0;
  685. wait_info.wq = &cmd_enc->autorefresh.kickoff_wq;
  686. wait_info.atomic_cnt = &cmd_enc->autorefresh.kickoff_cnt;
  687. wait_info.timeout_ms = _sde_encoder_phys_cmd_get_idle_timeout(phys_enc);
  688. /* wait for autorefresh kickoff to start */
  689. ret = sde_encoder_helper_wait_for_irq(phys_enc,
  690. INTR_IDX_AUTOREFRESH_DONE, &wait_info);
  691. /* double check that kickoff has started by reading write ptr reg */
  692. if (!ret)
  693. ret = _sde_encoder_phys_cmd_poll_write_pointer_started(
  694. phys_enc);
  695. else
  696. sde_encoder_helper_report_irq_timeout(phys_enc,
  697. INTR_IDX_AUTOREFRESH_DONE);
  698. return ret;
  699. }
  700. static int sde_encoder_phys_cmd_control_vblank_irq(
  701. struct sde_encoder_phys *phys_enc,
  702. bool enable)
  703. {
  704. struct sde_encoder_phys_cmd *cmd_enc =
  705. to_sde_encoder_phys_cmd(phys_enc);
  706. int ret = 0;
  707. u32 refcount;
  708. struct sde_kms *sde_kms;
  709. if (!phys_enc || !phys_enc->hw_pp) {
  710. SDE_ERROR("invalid encoder\n");
  711. return -EINVAL;
  712. }
  713. sde_kms = phys_enc->sde_kms;
  714. mutex_lock(phys_enc->vblank_ctl_lock);
  715. /* Slave encoders don't report vblank */
  716. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  717. goto end;
  718. refcount = atomic_read(&phys_enc->vblank_refcount);
  719. /* protect against negative */
  720. if (!enable && refcount == 0) {
  721. ret = -EINVAL;
  722. goto end;
  723. }
  724. SDE_DEBUG_CMDENC(cmd_enc, "[%pS] enable=%d/%d\n",
  725. __builtin_return_address(0), enable, refcount);
  726. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  727. enable, refcount);
  728. if (enable && atomic_inc_return(&phys_enc->vblank_refcount) == 1) {
  729. ret = sde_encoder_helper_register_irq(phys_enc, INTR_IDX_RDPTR);
  730. if (ret)
  731. atomic_dec_return(&phys_enc->vblank_refcount);
  732. } else if (!enable &&
  733. atomic_dec_return(&phys_enc->vblank_refcount) == 0) {
  734. ret = sde_encoder_helper_unregister_irq(phys_enc,
  735. INTR_IDX_RDPTR);
  736. if (ret)
  737. atomic_inc_return(&phys_enc->vblank_refcount);
  738. }
  739. end:
  740. mutex_unlock(phys_enc->vblank_ctl_lock);
  741. if (ret) {
  742. SDE_ERROR_CMDENC(cmd_enc,
  743. "control vblank irq error %d, enable %d, refcount %d\n",
  744. ret, enable, refcount);
  745. SDE_EVT32(DRMID(phys_enc->parent),
  746. phys_enc->hw_pp->idx - PINGPONG_0,
  747. enable, refcount, SDE_EVTLOG_ERROR);
  748. }
  749. return ret;
  750. }
  751. void sde_encoder_phys_cmd_irq_control(struct sde_encoder_phys *phys_enc,
  752. bool enable)
  753. {
  754. struct sde_encoder_phys_cmd *cmd_enc;
  755. bool ctl_done_supported = false;
  756. if (!phys_enc)
  757. return;
  758. /**
  759. * pingpong split slaves do not register for IRQs
  760. * check old and new topologies
  761. */
  762. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc) ||
  763. _sde_encoder_phys_is_disabling_ppsplit_slave(phys_enc))
  764. return;
  765. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  766. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  767. enable, atomic_read(&phys_enc->vblank_refcount));
  768. ctl_done_supported = sde_encoder_check_ctl_done_support(phys_enc->parent);
  769. if (enable) {
  770. if (!ctl_done_supported)
  771. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_PINGPONG);
  772. sde_encoder_phys_cmd_control_vblank_irq(phys_enc, true);
  773. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  774. sde_encoder_helper_register_irq(phys_enc,
  775. INTR_IDX_WRPTR);
  776. sde_encoder_helper_register_irq(phys_enc,
  777. INTR_IDX_AUTOREFRESH_DONE);
  778. if (ctl_done_supported)
  779. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_CTL_DONE);
  780. }
  781. } else {
  782. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  783. sde_encoder_helper_unregister_irq(phys_enc,
  784. INTR_IDX_WRPTR);
  785. sde_encoder_helper_unregister_irq(phys_enc,
  786. INTR_IDX_AUTOREFRESH_DONE);
  787. if (ctl_done_supported)
  788. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_CTL_DONE);
  789. }
  790. sde_encoder_phys_cmd_control_vblank_irq(phys_enc, false);
  791. if (!ctl_done_supported)
  792. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_PINGPONG);
  793. }
  794. }
  795. static int _get_tearcheck_threshold(struct sde_encoder_phys *phys_enc)
  796. {
  797. struct drm_connector *conn = phys_enc->connector;
  798. u32 qsync_mode;
  799. struct drm_display_mode *mode;
  800. u32 threshold_lines = DEFAULT_TEARCHECK_SYNC_THRESH_START;
  801. struct sde_encoder_phys_cmd *cmd_enc =
  802. to_sde_encoder_phys_cmd(phys_enc);
  803. if (!conn || !conn->state)
  804. return 0;
  805. mode = &phys_enc->cached_mode;
  806. qsync_mode = sde_connector_get_qsync_mode(conn);
  807. if (mode && (qsync_mode == SDE_RM_QSYNC_CONTINUOUS_MODE)) {
  808. u32 qsync_min_fps = 0;
  809. u32 default_fps = drm_mode_vrefresh(mode);
  810. u32 yres = mode->vtotal;
  811. u32 slow_time_ns;
  812. u32 default_time_ns;
  813. u32 extra_time_ns;
  814. u32 default_line_time_ns;
  815. if (phys_enc->parent_ops.get_qsync_fps)
  816. phys_enc->parent_ops.get_qsync_fps(
  817. phys_enc->parent, &qsync_min_fps, conn->state);
  818. if (!qsync_min_fps || !default_fps || !yres) {
  819. SDE_ERROR_CMDENC(cmd_enc,
  820. "wrong qsync params %d %d %d\n",
  821. qsync_min_fps, default_fps, yres);
  822. goto exit;
  823. }
  824. if (qsync_min_fps >= default_fps) {
  825. SDE_ERROR_CMDENC(cmd_enc,
  826. "qsync fps:%d must be less than default:%d\n",
  827. qsync_min_fps, default_fps);
  828. goto exit;
  829. }
  830. /* Calculate the number of extra lines*/
  831. slow_time_ns = DIV_ROUND_UP(1000000000, qsync_min_fps);
  832. default_time_ns = DIV_ROUND_UP(1000000000, default_fps);
  833. extra_time_ns = slow_time_ns - default_time_ns;
  834. default_line_time_ns = DIV_ROUND_UP(default_time_ns, yres);
  835. threshold_lines = extra_time_ns / default_line_time_ns;
  836. /* some DDICs express the timeout value in lines/4, round down to compensate */
  837. threshold_lines = round_down(threshold_lines, 4);
  838. /* remove 2 lines to cover for latency */
  839. if (threshold_lines - 2 > DEFAULT_TEARCHECK_SYNC_THRESH_START)
  840. threshold_lines -= 2;
  841. SDE_DEBUG_CMDENC(cmd_enc, "slow:%d default:%d extra:%d(ns)\n",
  842. slow_time_ns, default_time_ns, extra_time_ns);
  843. SDE_DEBUG_CMDENC(cmd_enc, "min_fps:%d fps:%d yres:%d lines:%d\n",
  844. qsync_min_fps, default_fps, yres, threshold_lines);
  845. SDE_EVT32(qsync_mode, qsync_min_fps, extra_time_ns, default_fps,
  846. yres, threshold_lines);
  847. }
  848. exit:
  849. return threshold_lines;
  850. }
  851. static void sde_encoder_phys_cmd_tearcheck_config(
  852. struct sde_encoder_phys *phys_enc)
  853. {
  854. struct sde_encoder_phys_cmd *cmd_enc =
  855. to_sde_encoder_phys_cmd(phys_enc);
  856. struct sde_hw_tear_check tc_cfg = { 0 };
  857. struct drm_display_mode *mode;
  858. bool tc_enable = true;
  859. u32 vsync_hz;
  860. int vrefresh;
  861. struct msm_drm_private *priv;
  862. struct sde_kms *sde_kms;
  863. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf) {
  864. SDE_ERROR("invalid encoder\n");
  865. return;
  866. }
  867. mode = &phys_enc->cached_mode;
  868. SDE_DEBUG_CMDENC(cmd_enc, "pp %d, intf %d\n",
  869. phys_enc->hw_pp->idx - PINGPONG_0,
  870. phys_enc->hw_intf->idx - INTF_0);
  871. if (phys_enc->has_intf_te) {
  872. if (!phys_enc->hw_intf->ops.setup_tearcheck ||
  873. !phys_enc->hw_intf->ops.enable_tearcheck) {
  874. SDE_DEBUG_CMDENC(cmd_enc, "tearcheck not supported\n");
  875. return;
  876. }
  877. } else {
  878. if (!phys_enc->hw_pp->ops.setup_tearcheck ||
  879. !phys_enc->hw_pp->ops.enable_tearcheck) {
  880. SDE_DEBUG_CMDENC(cmd_enc, "tearcheck not supported\n");
  881. return;
  882. }
  883. }
  884. sde_kms = phys_enc->sde_kms;
  885. if (!sde_kms || !sde_kms->dev || !sde_kms->dev->dev_private) {
  886. SDE_ERROR("invalid device\n");
  887. return;
  888. }
  889. priv = sde_kms->dev->dev_private;
  890. vrefresh = drm_mode_vrefresh(mode);
  891. /*
  892. * TE default: dsi byte clock calculated base on 70 fps;
  893. * around 14 ms to complete a kickoff cycle if te disabled;
  894. * vclk_line base on 60 fps; write is faster than read;
  895. * init == start == rdptr;
  896. *
  897. * vsync_count is ratio of MDP VSYNC clock frequency to LCD panel
  898. * frequency divided by the no. of rows (lines) in the LCDpanel.
  899. */
  900. vsync_hz = sde_power_clk_get_rate(&priv->phandle, "vsync_clk");
  901. if (!vsync_hz || !mode->vtotal || !vrefresh) {
  902. SDE_DEBUG_CMDENC(cmd_enc,
  903. "invalid params - vsync_hz %u vtot %u vrefresh %u\n",
  904. vsync_hz, mode->vtotal, vrefresh);
  905. return;
  906. }
  907. tc_cfg.vsync_count = vsync_hz / (mode->vtotal * vrefresh);
  908. /* enable external TE after kickoff to avoid premature autorefresh */
  909. tc_cfg.hw_vsync_mode = 0;
  910. /*
  911. * By setting sync_cfg_height to near max register value, we essentially
  912. * disable sde hw generated TE signal, since hw TE will arrive first.
  913. * Only caveat is if due to error, we hit wrap-around.
  914. */
  915. tc_cfg.sync_cfg_height = 0xFFF0;
  916. tc_cfg.vsync_init_val = mode->vdisplay;
  917. tc_cfg.sync_threshold_start = _get_tearcheck_threshold(phys_enc);
  918. tc_cfg.sync_threshold_continue = DEFAULT_TEARCHECK_SYNC_THRESH_CONTINUE;
  919. tc_cfg.start_pos = mode->vdisplay;
  920. tc_cfg.rd_ptr_irq = mode->vdisplay + 1;
  921. tc_cfg.wr_ptr_irq = 1;
  922. cmd_enc->qsync_threshold_lines = tc_cfg.sync_threshold_start;
  923. SDE_DEBUG_CMDENC(cmd_enc,
  924. "tc %d intf %d vsync_clk_speed_hz %u vtotal %u vrefresh %u\n",
  925. phys_enc->hw_pp->idx - PINGPONG_0,
  926. phys_enc->hw_intf->idx - INTF_0,
  927. vsync_hz, mode->vtotal, vrefresh);
  928. SDE_DEBUG_CMDENC(cmd_enc,
  929. "tc %d intf %d enable %u start_pos %u rd_ptr_irq %u wr_ptr_irq %u\n",
  930. phys_enc->hw_pp->idx - PINGPONG_0,
  931. phys_enc->hw_intf->idx - INTF_0,
  932. tc_enable, tc_cfg.start_pos, tc_cfg.rd_ptr_irq,
  933. tc_cfg.wr_ptr_irq);
  934. SDE_DEBUG_CMDENC(cmd_enc,
  935. "tc %d intf %d hw_vsync_mode %u vsync_count %u vsync_init_val %u\n",
  936. phys_enc->hw_pp->idx - PINGPONG_0,
  937. phys_enc->hw_intf->idx - INTF_0,
  938. tc_cfg.hw_vsync_mode, tc_cfg.vsync_count,
  939. tc_cfg.vsync_init_val);
  940. SDE_DEBUG_CMDENC(cmd_enc,
  941. "tc %d intf %d cfgheight %u thresh_start %u thresh_cont %u\n",
  942. phys_enc->hw_pp->idx - PINGPONG_0,
  943. phys_enc->hw_intf->idx - INTF_0,
  944. tc_cfg.sync_cfg_height,
  945. tc_cfg.sync_threshold_start, tc_cfg.sync_threshold_continue);
  946. SDE_EVT32(phys_enc->hw_pp->idx - PINGPONG_0, phys_enc->hw_intf->idx - INTF_0,
  947. vsync_hz, mode->vtotal, vrefresh);
  948. SDE_EVT32(tc_enable, tc_cfg.start_pos, tc_cfg.rd_ptr_irq, tc_cfg.wr_ptr_irq,
  949. tc_cfg.hw_vsync_mode, tc_cfg.vsync_count, tc_cfg.vsync_init_val,
  950. tc_cfg.sync_cfg_height, tc_cfg.sync_threshold_start,
  951. tc_cfg.sync_threshold_continue);
  952. if (phys_enc->has_intf_te) {
  953. phys_enc->hw_intf->ops.setup_tearcheck(phys_enc->hw_intf,
  954. &tc_cfg);
  955. phys_enc->hw_intf->ops.enable_tearcheck(phys_enc->hw_intf,
  956. tc_enable);
  957. } else {
  958. phys_enc->hw_pp->ops.setup_tearcheck(phys_enc->hw_pp, &tc_cfg);
  959. phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp,
  960. tc_enable);
  961. }
  962. }
  963. static void _sde_encoder_phys_cmd_pingpong_config(
  964. struct sde_encoder_phys *phys_enc)
  965. {
  966. struct sde_encoder_phys_cmd *cmd_enc =
  967. to_sde_encoder_phys_cmd(phys_enc);
  968. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
  969. SDE_ERROR("invalid arg(s), enc %d\n", !phys_enc);
  970. return;
  971. }
  972. SDE_DEBUG_CMDENC(cmd_enc, "pp %d, enabling mode:\n",
  973. phys_enc->hw_pp->idx - PINGPONG_0);
  974. drm_mode_debug_printmodeline(&phys_enc->cached_mode);
  975. if (!_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  976. _sde_encoder_phys_cmd_update_intf_cfg(phys_enc);
  977. sde_encoder_phys_cmd_tearcheck_config(phys_enc);
  978. }
  979. static void sde_encoder_phys_cmd_enable_helper(
  980. struct sde_encoder_phys *phys_enc)
  981. {
  982. struct sde_hw_intf *hw_intf;
  983. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp ||
  984. !phys_enc->hw_intf) {
  985. SDE_ERROR("invalid arg(s), encoder %d\n", !phys_enc);
  986. return;
  987. }
  988. sde_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
  989. _sde_encoder_phys_cmd_pingpong_config(phys_enc);
  990. hw_intf = phys_enc->hw_intf;
  991. if (hw_intf->ops.enable_compressed_input)
  992. hw_intf->ops.enable_compressed_input(phys_enc->hw_intf,
  993. (phys_enc->comp_type !=
  994. MSM_DISPLAY_COMPRESSION_NONE), false);
  995. if (hw_intf->ops.enable_wide_bus)
  996. hw_intf->ops.enable_wide_bus(hw_intf,
  997. sde_encoder_is_widebus_enabled(phys_enc->parent));
  998. /*
  999. * For pp-split, skip setting the flush bit for the slave intf, since
  1000. * both intfs use same ctl and HW will only flush the master.
  1001. */
  1002. if (_sde_encoder_phys_is_ppsplit(phys_enc) &&
  1003. !sde_encoder_phys_cmd_is_master(phys_enc))
  1004. goto skip_flush;
  1005. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  1006. skip_flush:
  1007. return;
  1008. }
  1009. static void sde_encoder_phys_cmd_enable(struct sde_encoder_phys *phys_enc)
  1010. {
  1011. struct sde_encoder_phys_cmd *cmd_enc =
  1012. to_sde_encoder_phys_cmd(phys_enc);
  1013. if (!phys_enc || !phys_enc->hw_pp) {
  1014. SDE_ERROR("invalid phys encoder\n");
  1015. return;
  1016. }
  1017. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  1018. if (phys_enc->enable_state == SDE_ENC_ENABLED) {
  1019. if (!phys_enc->cont_splash_enabled)
  1020. SDE_ERROR("already enabled\n");
  1021. return;
  1022. }
  1023. sde_encoder_phys_cmd_enable_helper(phys_enc);
  1024. phys_enc->enable_state = SDE_ENC_ENABLED;
  1025. }
  1026. static bool sde_encoder_phys_cmd_is_autorefresh_enabled(
  1027. struct sde_encoder_phys *phys_enc)
  1028. {
  1029. struct sde_hw_pingpong *hw_pp;
  1030. struct sde_hw_intf *hw_intf;
  1031. struct sde_hw_autorefresh cfg;
  1032. int ret;
  1033. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1034. return false;
  1035. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1036. return false;
  1037. if (phys_enc->has_intf_te) {
  1038. hw_intf = phys_enc->hw_intf;
  1039. if (!hw_intf->ops.get_autorefresh)
  1040. return false;
  1041. ret = hw_intf->ops.get_autorefresh(hw_intf, &cfg);
  1042. } else {
  1043. hw_pp = phys_enc->hw_pp;
  1044. if (!hw_pp->ops.get_autorefresh)
  1045. return false;
  1046. ret = hw_pp->ops.get_autorefresh(hw_pp, &cfg);
  1047. }
  1048. return ret ? false : cfg.enable;
  1049. }
  1050. static void sde_encoder_phys_cmd_connect_te(
  1051. struct sde_encoder_phys *phys_enc, bool enable)
  1052. {
  1053. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1054. return;
  1055. if (phys_enc->has_intf_te &&
  1056. phys_enc->hw_intf->ops.connect_external_te)
  1057. phys_enc->hw_intf->ops.connect_external_te(phys_enc->hw_intf,
  1058. enable);
  1059. else if (phys_enc->hw_pp->ops.connect_external_te)
  1060. phys_enc->hw_pp->ops.connect_external_te(phys_enc->hw_pp,
  1061. enable);
  1062. else
  1063. return;
  1064. SDE_EVT32(DRMID(phys_enc->parent), enable);
  1065. }
  1066. static int sde_encoder_phys_cmd_te_get_line_count(
  1067. struct sde_encoder_phys *phys_enc)
  1068. {
  1069. struct sde_hw_pingpong *hw_pp;
  1070. struct sde_hw_intf *hw_intf;
  1071. u32 line_count;
  1072. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1073. return -EINVAL;
  1074. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1075. return -EINVAL;
  1076. if (phys_enc->has_intf_te) {
  1077. hw_intf = phys_enc->hw_intf;
  1078. if (!hw_intf->ops.get_line_count)
  1079. return -EINVAL;
  1080. line_count = hw_intf->ops.get_line_count(hw_intf);
  1081. } else {
  1082. hw_pp = phys_enc->hw_pp;
  1083. if (!hw_pp->ops.get_line_count)
  1084. return -EINVAL;
  1085. line_count = hw_pp->ops.get_line_count(hw_pp);
  1086. }
  1087. return line_count;
  1088. }
  1089. static void sde_encoder_phys_cmd_disable(struct sde_encoder_phys *phys_enc)
  1090. {
  1091. struct sde_encoder_phys_cmd *cmd_enc =
  1092. to_sde_encoder_phys_cmd(phys_enc);
  1093. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf) {
  1094. SDE_ERROR("invalid encoder\n");
  1095. return;
  1096. }
  1097. SDE_DEBUG_CMDENC(cmd_enc, "pp %d intf %d state %d\n",
  1098. phys_enc->hw_pp->idx - PINGPONG_0,
  1099. phys_enc->hw_intf->idx - INTF_0,
  1100. phys_enc->enable_state);
  1101. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1102. phys_enc->hw_intf->idx - INTF_0,
  1103. phys_enc->enable_state);
  1104. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1105. SDE_ERROR_CMDENC(cmd_enc, "already disabled\n");
  1106. return;
  1107. }
  1108. if (!sde_in_trusted_vm(phys_enc->sde_kms)) {
  1109. if (phys_enc->has_intf_te &&
  1110. phys_enc->hw_intf->ops.enable_tearcheck)
  1111. phys_enc->hw_intf->ops.enable_tearcheck(
  1112. phys_enc->hw_intf,
  1113. false);
  1114. else if (phys_enc->hw_pp->ops.enable_tearcheck)
  1115. phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp,
  1116. false);
  1117. if (sde_encoder_phys_cmd_is_master(phys_enc))
  1118. sde_encoder_helper_phys_disable(phys_enc, NULL);
  1119. if (phys_enc->hw_intf->ops.reset_counter)
  1120. phys_enc->hw_intf->ops.reset_counter(phys_enc->hw_intf);
  1121. }
  1122. memset(&cmd_enc->autorefresh.cfg, 0, sizeof(struct sde_hw_autorefresh));
  1123. phys_enc->enable_state = SDE_ENC_DISABLED;
  1124. }
  1125. static void sde_encoder_phys_cmd_destroy(struct sde_encoder_phys *phys_enc)
  1126. {
  1127. struct sde_encoder_phys_cmd *cmd_enc =
  1128. to_sde_encoder_phys_cmd(phys_enc);
  1129. if (!phys_enc) {
  1130. SDE_ERROR("invalid encoder\n");
  1131. return;
  1132. }
  1133. kfree(cmd_enc);
  1134. }
  1135. static void sde_encoder_phys_cmd_get_hw_resources(
  1136. struct sde_encoder_phys *phys_enc,
  1137. struct sde_encoder_hw_resources *hw_res,
  1138. struct drm_connector_state *conn_state)
  1139. {
  1140. struct sde_encoder_phys_cmd *cmd_enc =
  1141. to_sde_encoder_phys_cmd(phys_enc);
  1142. if (!phys_enc) {
  1143. SDE_ERROR("invalid encoder\n");
  1144. return;
  1145. }
  1146. if ((phys_enc->intf_idx - INTF_0) >= INTF_MAX) {
  1147. SDE_ERROR("invalid intf idx:%d\n", phys_enc->intf_idx);
  1148. return;
  1149. }
  1150. SDE_DEBUG_CMDENC(cmd_enc, "\n");
  1151. hw_res->intfs[phys_enc->intf_idx - INTF_0] = INTF_MODE_CMD;
  1152. }
  1153. static int sde_encoder_phys_cmd_prepare_for_kickoff(
  1154. struct sde_encoder_phys *phys_enc,
  1155. struct sde_encoder_kickoff_params *params)
  1156. {
  1157. struct sde_hw_tear_check tc_cfg = {0};
  1158. struct sde_encoder_phys_cmd *cmd_enc =
  1159. to_sde_encoder_phys_cmd(phys_enc);
  1160. int ret = 0;
  1161. bool recovery_events;
  1162. if (!phys_enc || !phys_enc->hw_pp) {
  1163. SDE_ERROR("invalid encoder\n");
  1164. return -EINVAL;
  1165. }
  1166. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  1167. phys_enc->frame_trigger_mode = params->frame_trigger_mode;
  1168. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1169. atomic_read(&phys_enc->pending_kickoff_cnt),
  1170. atomic_read(&cmd_enc->autorefresh.kickoff_cnt),
  1171. phys_enc->frame_trigger_mode);
  1172. if (phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_DEFAULT) {
  1173. /*
  1174. * Mark kickoff request as outstanding. If there are more
  1175. * than one outstanding frame, then we have to wait for the
  1176. * previous frame to complete
  1177. */
  1178. ret = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1179. if (ret) {
  1180. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1181. SDE_EVT32(DRMID(phys_enc->parent),
  1182. phys_enc->hw_pp->idx - PINGPONG_0);
  1183. SDE_ERROR("failed wait_for_idle: %d\n", ret);
  1184. }
  1185. }
  1186. if (phys_enc->recovered) {
  1187. recovery_events = sde_encoder_recovery_events_enabled(
  1188. phys_enc->parent);
  1189. if (cmd_enc->frame_tx_timeout_report_cnt && recovery_events)
  1190. sde_connector_event_notify(phys_enc->connector,
  1191. DRM_EVENT_SDE_HW_RECOVERY,
  1192. sizeof(uint8_t),
  1193. SDE_RECOVERY_SUCCESS);
  1194. cmd_enc->frame_tx_timeout_report_cnt = 0;
  1195. phys_enc->recovered = false;
  1196. }
  1197. if (sde_connector_is_qsync_updated(phys_enc->connector)) {
  1198. tc_cfg.sync_threshold_start = _get_tearcheck_threshold(
  1199. phys_enc);
  1200. cmd_enc->qsync_threshold_lines = tc_cfg.sync_threshold_start;
  1201. if (phys_enc->has_intf_te &&
  1202. phys_enc->hw_intf->ops.update_tearcheck)
  1203. phys_enc->hw_intf->ops.update_tearcheck(
  1204. phys_enc->hw_intf, &tc_cfg);
  1205. else if (phys_enc->hw_pp->ops.update_tearcheck)
  1206. phys_enc->hw_pp->ops.update_tearcheck(
  1207. phys_enc->hw_pp, &tc_cfg);
  1208. SDE_EVT32(DRMID(phys_enc->parent), tc_cfg.sync_threshold_start);
  1209. }
  1210. SDE_DEBUG_CMDENC(cmd_enc, "pp:%d pending_cnt %d\n",
  1211. phys_enc->hw_pp->idx - PINGPONG_0,
  1212. atomic_read(&phys_enc->pending_kickoff_cnt));
  1213. return ret;
  1214. }
  1215. static bool _sde_encoder_phys_cmd_needs_vsync_change(
  1216. struct sde_encoder_phys *phys_enc, ktime_t profile_timestamp)
  1217. {
  1218. struct sde_encoder_phys_cmd *cmd_enc;
  1219. struct sde_encoder_phys_cmd_te_timestamp *cur;
  1220. struct sde_encoder_phys_cmd_te_timestamp *prev = NULL;
  1221. ktime_t time_diff;
  1222. u64 l_bound = 0, u_bound = 0;
  1223. bool ret = false;
  1224. unsigned long lock_flags;
  1225. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1226. sde_encoder_helper_get_jitter_bounds_ns(phys_enc->parent,
  1227. &l_bound, &u_bound);
  1228. if (!l_bound || !u_bound) {
  1229. SDE_ERROR_CMDENC(cmd_enc, "invalid vsync jitter bounds\n");
  1230. return false;
  1231. }
  1232. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  1233. list_for_each_entry_reverse(cur, &cmd_enc->te_timestamp_list, list) {
  1234. if (prev && ktime_after(cur->timestamp, profile_timestamp)) {
  1235. time_diff = ktime_sub(prev->timestamp, cur->timestamp);
  1236. if ((time_diff < l_bound) || (time_diff > u_bound)) {
  1237. ret = true;
  1238. break;
  1239. }
  1240. }
  1241. prev = cur;
  1242. }
  1243. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  1244. if (ret) {
  1245. SDE_DEBUG_CMDENC(cmd_enc,
  1246. "time_diff:%llu, prev:%llu, cur:%llu, jitter:%llu/%llu\n",
  1247. time_diff, prev->timestamp, cur->timestamp,
  1248. l_bound, u_bound);
  1249. time_diff = div_s64(time_diff, 1000);
  1250. SDE_EVT32(DRMID(phys_enc->parent),
  1251. (u32) (do_div(l_bound, 1000)),
  1252. (u32) (do_div(u_bound, 1000)),
  1253. (u32) (time_diff), SDE_EVTLOG_ERROR);
  1254. }
  1255. return ret;
  1256. }
  1257. static int _sde_encoder_phys_cmd_wait_for_wr_ptr(
  1258. struct sde_encoder_phys *phys_enc)
  1259. {
  1260. struct sde_encoder_phys_cmd *cmd_enc =
  1261. to_sde_encoder_phys_cmd(phys_enc);
  1262. struct sde_encoder_wait_info wait_info = {0};
  1263. struct sde_connector *c_conn;
  1264. bool frame_pending = true;
  1265. struct sde_hw_ctl *ctl;
  1266. unsigned long lock_flags;
  1267. int ret, timeout_ms;
  1268. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->connector) {
  1269. SDE_ERROR("invalid argument(s)\n");
  1270. return -EINVAL;
  1271. }
  1272. ctl = phys_enc->hw_ctl;
  1273. c_conn = to_sde_connector(phys_enc->connector);
  1274. timeout_ms = phys_enc->kickoff_timeout_ms;
  1275. if (c_conn->lp_mode == SDE_MODE_DPMS_LP1 ||
  1276. c_conn->lp_mode == SDE_MODE_DPMS_LP2)
  1277. timeout_ms = timeout_ms * 2;
  1278. wait_info.wq = &phys_enc->pending_kickoff_wq;
  1279. wait_info.atomic_cnt = &phys_enc->pending_retire_fence_cnt;
  1280. wait_info.timeout_ms = timeout_ms;
  1281. /* slave encoder doesn't enable for ppsplit */
  1282. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  1283. return 0;
  1284. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_WRPTR,
  1285. &wait_info);
  1286. if (ret == -ETIMEDOUT) {
  1287. struct sde_hw_ctl *ctl = phys_enc->hw_ctl;
  1288. if (ctl && ctl->ops.get_start_state)
  1289. frame_pending = ctl->ops.get_start_state(ctl);
  1290. ret = (frame_pending || sde_connector_esd_status(phys_enc->connector)) ? ret : 0;
  1291. /*
  1292. * There can be few cases of ESD where CTL_START is cleared but
  1293. * wr_ptr irq doesn't come. Signaling retire fence in these
  1294. * cases to avoid freeze and dangling pending_retire_fence_cnt
  1295. */
  1296. if (!ret) {
  1297. SDE_EVT32(DRMID(phys_enc->parent),
  1298. SDE_EVTLOG_FUNC_CASE1);
  1299. if (sde_encoder_phys_cmd_is_master(phys_enc) &&
  1300. atomic_add_unless(
  1301. &phys_enc->pending_retire_fence_cnt, -1, 0)) {
  1302. spin_lock_irqsave(phys_enc->enc_spinlock,
  1303. lock_flags);
  1304. phys_enc->parent_ops.handle_frame_done(
  1305. phys_enc->parent, phys_enc,
  1306. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  1307. spin_unlock_irqrestore(phys_enc->enc_spinlock,
  1308. lock_flags);
  1309. }
  1310. }
  1311. }
  1312. cmd_enc->wr_ptr_wait_success = (ret == 0) ? true : false;
  1313. return ret;
  1314. }
  1315. static int sde_encoder_phys_cmd_wait_for_tx_complete(
  1316. struct sde_encoder_phys *phys_enc)
  1317. {
  1318. int rc;
  1319. struct sde_encoder_phys_cmd *cmd_enc;
  1320. if (!phys_enc)
  1321. return -EINVAL;
  1322. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1323. if (sde_encoder_check_ctl_done_support(phys_enc->parent)
  1324. && !sde_encoder_phys_cmd_is_master(phys_enc))
  1325. return 0;
  1326. if (!atomic_read(&phys_enc->pending_kickoff_cnt)) {
  1327. SDE_EVT32(DRMID(phys_enc->parent),
  1328. phys_enc->intf_idx - INTF_0,
  1329. phys_enc->enable_state);
  1330. return 0;
  1331. }
  1332. rc = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1333. if (rc) {
  1334. SDE_EVT32(DRMID(phys_enc->parent),
  1335. phys_enc->intf_idx - INTF_0);
  1336. SDE_ERROR("failed wait_for_idle: %d\n", rc);
  1337. }
  1338. return rc;
  1339. }
  1340. static int _sde_encoder_phys_cmd_handle_wr_ptr_timeout(
  1341. struct sde_encoder_phys *phys_enc,
  1342. ktime_t profile_timestamp)
  1343. {
  1344. struct sde_encoder_phys_cmd *cmd_enc =
  1345. to_sde_encoder_phys_cmd(phys_enc);
  1346. bool switch_te;
  1347. int ret = -ETIMEDOUT;
  1348. unsigned long lock_flags;
  1349. switch_te = _sde_encoder_phys_cmd_needs_vsync_change(
  1350. phys_enc, profile_timestamp);
  1351. SDE_EVT32(DRMID(phys_enc->parent), switch_te, SDE_EVTLOG_FUNC_ENTRY);
  1352. if (sde_connector_panel_dead(phys_enc->connector)) {
  1353. ret = _sde_encoder_phys_cmd_wait_for_wr_ptr(phys_enc);
  1354. } else if (switch_te) {
  1355. SDE_DEBUG_CMDENC(cmd_enc,
  1356. "wr_ptr_irq wait failed, retry with WD TE\n");
  1357. /* switch to watchdog TE and wait again */
  1358. sde_encoder_helper_switch_vsync(phys_enc->parent, true);
  1359. ret = _sde_encoder_phys_cmd_wait_for_wr_ptr(phys_enc);
  1360. /* switch back to default TE */
  1361. sde_encoder_helper_switch_vsync(phys_enc->parent, false);
  1362. }
  1363. /*
  1364. * Signaling the retire fence at wr_ptr timeout
  1365. * to allow the next commit and avoid device freeze.
  1366. */
  1367. if (ret == -ETIMEDOUT) {
  1368. SDE_ERROR_CMDENC(cmd_enc,
  1369. "wr_ptr_irq wait failed, switch_te:%d\n", switch_te);
  1370. SDE_EVT32(DRMID(phys_enc->parent), switch_te, SDE_EVTLOG_ERROR);
  1371. if (sde_encoder_phys_cmd_is_master(phys_enc) &&
  1372. atomic_add_unless(
  1373. &phys_enc->pending_retire_fence_cnt, -1, 0)) {
  1374. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  1375. phys_enc->parent_ops.handle_frame_done(
  1376. phys_enc->parent, phys_enc,
  1377. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  1378. spin_unlock_irqrestore(phys_enc->enc_spinlock,
  1379. lock_flags);
  1380. }
  1381. }
  1382. cmd_enc->wr_ptr_wait_success = (ret == 0) ? true : false;
  1383. return ret;
  1384. }
  1385. static int sde_encoder_phys_cmd_wait_for_commit_done(
  1386. struct sde_encoder_phys *phys_enc)
  1387. {
  1388. int rc = 0, i, pending_cnt;
  1389. struct sde_encoder_phys_cmd *cmd_enc;
  1390. ktime_t profile_timestamp = ktime_get();
  1391. u32 scheduler_status = INVALID_CTL_STATUS;
  1392. struct sde_hw_ctl *ctl;
  1393. if (!phys_enc)
  1394. return -EINVAL;
  1395. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1396. if (sde_encoder_check_ctl_done_support(phys_enc->parent)
  1397. && !sde_encoder_phys_cmd_is_master(phys_enc))
  1398. return 0;
  1399. /* only required for master controller */
  1400. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  1401. rc = _sde_encoder_phys_cmd_wait_for_wr_ptr(phys_enc);
  1402. if (rc == -ETIMEDOUT) {
  1403. /*
  1404. * Profile all the TE received after profile_timestamp
  1405. * and if the jitter is more, switch to watchdog TE
  1406. * and wait for wr_ptr again. Finally move back to
  1407. * default TE.
  1408. */
  1409. rc = _sde_encoder_phys_cmd_handle_wr_ptr_timeout(
  1410. phys_enc, profile_timestamp);
  1411. if (rc == -ETIMEDOUT)
  1412. goto wait_for_idle;
  1413. }
  1414. if (cmd_enc->autorefresh.cfg.enable)
  1415. rc = _sde_encoder_phys_cmd_wait_for_autorefresh_done(
  1416. phys_enc);
  1417. ctl = phys_enc->hw_ctl;
  1418. if (ctl && ctl->ops.get_scheduler_status)
  1419. scheduler_status = ctl->ops.get_scheduler_status(ctl);
  1420. }
  1421. /* wait for posted start or serialize trigger */
  1422. pending_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  1423. if ((pending_cnt > 1) ||
  1424. (pending_cnt && (scheduler_status & BIT(0))) ||
  1425. (!rc && phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_SERIALIZE))
  1426. goto wait_for_idle;
  1427. return rc;
  1428. wait_for_idle:
  1429. pending_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  1430. for (i = 0; i < pending_cnt; i++)
  1431. rc |= sde_encoder_wait_for_event(phys_enc->parent,
  1432. MSM_ENC_TX_COMPLETE);
  1433. if (rc) {
  1434. SDE_EVT32(DRMID(phys_enc->parent),
  1435. phys_enc->hw_pp->idx - PINGPONG_0,
  1436. phys_enc->frame_trigger_mode,
  1437. atomic_read(&phys_enc->pending_kickoff_cnt),
  1438. phys_enc->enable_state,
  1439. cmd_enc->wr_ptr_wait_success, scheduler_status, rc);
  1440. SDE_ERROR("pp:%d failed wait_for_idle: %d\n",
  1441. phys_enc->hw_pp->idx - PINGPONG_0, rc);
  1442. if (phys_enc->enable_state == SDE_ENC_ERR_NEEDS_HW_RESET)
  1443. sde_encoder_needs_hw_reset(phys_enc->parent);
  1444. }
  1445. return rc;
  1446. }
  1447. static int sde_encoder_phys_cmd_wait_for_vblank(
  1448. struct sde_encoder_phys *phys_enc)
  1449. {
  1450. int rc = 0;
  1451. struct sde_encoder_phys_cmd *cmd_enc;
  1452. struct sde_encoder_wait_info wait_info = {0};
  1453. if (!phys_enc)
  1454. return -EINVAL;
  1455. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1456. /* only required for master controller */
  1457. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1458. return rc;
  1459. wait_info.wq = &cmd_enc->pending_vblank_wq;
  1460. wait_info.atomic_cnt = &cmd_enc->pending_vblank_cnt;
  1461. wait_info.timeout_ms = _sde_encoder_phys_cmd_get_idle_timeout(phys_enc);
  1462. atomic_inc(&cmd_enc->pending_vblank_cnt);
  1463. rc = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_RDPTR,
  1464. &wait_info);
  1465. return rc;
  1466. }
  1467. static void sde_encoder_phys_cmd_update_split_role(
  1468. struct sde_encoder_phys *phys_enc,
  1469. enum sde_enc_split_role role)
  1470. {
  1471. struct sde_encoder_phys_cmd *cmd_enc;
  1472. enum sde_enc_split_role old_role;
  1473. bool is_ppsplit;
  1474. if (!phys_enc)
  1475. return;
  1476. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1477. old_role = phys_enc->split_role;
  1478. is_ppsplit = _sde_encoder_phys_is_ppsplit(phys_enc);
  1479. phys_enc->split_role = role;
  1480. SDE_DEBUG_CMDENC(cmd_enc, "old role %d new role %d\n",
  1481. old_role, role);
  1482. /*
  1483. * ppsplit solo needs to reprogram because intf may have swapped without
  1484. * role changing on left-only, right-only back-to-back commits
  1485. */
  1486. if (!(is_ppsplit && role == ENC_ROLE_SOLO) &&
  1487. (role == old_role || role == ENC_ROLE_SKIP))
  1488. return;
  1489. sde_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
  1490. _sde_encoder_phys_cmd_pingpong_config(phys_enc);
  1491. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  1492. }
  1493. static void _sde_encoder_autorefresh_disable_seq1(
  1494. struct sde_encoder_phys *phys_enc)
  1495. {
  1496. int trial = 0;
  1497. u32 timeout_ms = phys_enc->kickoff_timeout_ms;
  1498. struct sde_encoder_phys_cmd *cmd_enc =
  1499. to_sde_encoder_phys_cmd(phys_enc);
  1500. /*
  1501. * If autorefresh is enabled, disable it and make sure it is safe to
  1502. * proceed with current frame commit/push. Sequence fallowed is,
  1503. * 1. Disable TE & autorefresh - caller will take care of it
  1504. * 2. Poll for frame transfer ongoing to be false
  1505. * 3. Enable TE back - caller will take care of it
  1506. */
  1507. do {
  1508. udelay(AUTOREFRESH_SEQ1_POLL_TIME);
  1509. if ((trial * AUTOREFRESH_SEQ1_POLL_TIME)
  1510. > (timeout_ms * USEC_PER_MSEC)) {
  1511. SDE_ERROR_CMDENC(cmd_enc,
  1512. "disable autorefresh failed\n");
  1513. phys_enc->enable_state = SDE_ENC_ERR_NEEDS_HW_RESET;
  1514. break;
  1515. }
  1516. trial++;
  1517. } while (_sde_encoder_phys_cmd_is_ongoing_pptx(phys_enc));
  1518. }
  1519. static void _sde_encoder_autorefresh_disable_seq2(
  1520. struct sde_encoder_phys *phys_enc)
  1521. {
  1522. int trial = 0;
  1523. struct sde_hw_mdp *hw_mdp = phys_enc->hw_mdptop;
  1524. u32 autorefresh_status = 0;
  1525. struct sde_encoder_phys_cmd *cmd_enc =
  1526. to_sde_encoder_phys_cmd(phys_enc);
  1527. struct intf_tear_status tear_status;
  1528. struct sde_hw_intf *hw_intf = phys_enc->hw_intf;
  1529. if (!hw_mdp->ops.get_autorefresh_status ||
  1530. !hw_intf->ops.check_and_reset_tearcheck) {
  1531. SDE_DEBUG_CMDENC(cmd_enc,
  1532. "autofresh disable seq2 not supported\n");
  1533. return;
  1534. }
  1535. /*
  1536. * If autorefresh is still enabled after sequence-1, proceed with
  1537. * below sequence-2.
  1538. * 1. Disable autorefresh config
  1539. * 2. Run in loop:
  1540. * 2.1 Poll for autorefresh to be disabled
  1541. * 2.2 Log read and write count status
  1542. * 2.3 Replace te write count with start_pos to meet trigger window
  1543. */
  1544. autorefresh_status = hw_mdp->ops.get_autorefresh_status(hw_mdp,
  1545. phys_enc->intf_idx);
  1546. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1547. autorefresh_status, SDE_EVTLOG_FUNC_CASE1);
  1548. if (!(autorefresh_status & BIT(7))) {
  1549. usleep_range(AUTOREFRESH_SEQ2_POLL_TIME,
  1550. AUTOREFRESH_SEQ2_POLL_TIME + 1);
  1551. autorefresh_status = hw_mdp->ops.get_autorefresh_status(hw_mdp,
  1552. phys_enc->intf_idx);
  1553. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1554. autorefresh_status, SDE_EVTLOG_FUNC_CASE2);
  1555. }
  1556. while (autorefresh_status & BIT(7)) {
  1557. if (!trial) {
  1558. pr_err("enc:%d autofresh status:0x%x intf:%d\n", DRMID(phys_enc->parent),
  1559. autorefresh_status, phys_enc->intf_idx - INTF_0);
  1560. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, 0);
  1561. }
  1562. usleep_range(AUTOREFRESH_SEQ2_POLL_TIME,
  1563. AUTOREFRESH_SEQ2_POLL_TIME + 1);
  1564. if ((trial * AUTOREFRESH_SEQ2_POLL_TIME)
  1565. > AUTOREFRESH_SEQ2_POLL_TIMEOUT) {
  1566. SDE_ERROR_CMDENC(cmd_enc,
  1567. "disable autorefresh failed\n");
  1568. SDE_DBG_DUMP(SDE_DBG_BUILT_IN_ALL, "panic");
  1569. break;
  1570. }
  1571. trial++;
  1572. autorefresh_status = hw_mdp->ops.get_autorefresh_status(hw_mdp,
  1573. phys_enc->intf_idx);
  1574. hw_intf->ops.check_and_reset_tearcheck(hw_intf, &tear_status);
  1575. pr_err("enc:%d autofresh status:0x%x intf:%d tear_read:0x%x tear_write:0x%x\n",
  1576. DRMID(phys_enc->parent), autorefresh_status, phys_enc->intf_idx - INTF_0,
  1577. tear_status.read_count, tear_status.write_count);
  1578. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1579. autorefresh_status, tear_status.read_count,
  1580. tear_status.write_count);
  1581. }
  1582. }
  1583. static void _sde_encoder_phys_disable_autorefresh(struct sde_encoder_phys *phys_enc)
  1584. {
  1585. struct sde_encoder_phys_cmd *cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1586. struct sde_kms *sde_kms;
  1587. if (!phys_enc || !sde_encoder_phys_cmd_is_master(phys_enc))
  1588. return;
  1589. if (!sde_encoder_phys_cmd_is_autorefresh_enabled(phys_enc))
  1590. return;
  1591. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1592. cmd_enc->autorefresh.cfg.enable);
  1593. sde_kms = phys_enc->sde_kms;
  1594. sde_encoder_phys_cmd_connect_te(phys_enc, false);
  1595. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, 0);
  1596. phys_enc->autorefresh_disable_trans = true;
  1597. if (sde_kms && sde_kms->catalog &&
  1598. (sde_kms->catalog->autorefresh_disable_seq == AUTOREFRESH_DISABLE_SEQ1)) {
  1599. _sde_encoder_autorefresh_disable_seq1(phys_enc);
  1600. _sde_encoder_autorefresh_disable_seq2(phys_enc);
  1601. }
  1602. sde_encoder_phys_cmd_connect_te(phys_enc, true);
  1603. SDE_DEBUG_CMDENC(cmd_enc, "autorefresh disabled successfully\n");
  1604. }
  1605. static void sde_encoder_phys_cmd_prepare_commit(struct sde_encoder_phys *phys_enc)
  1606. {
  1607. return _sde_encoder_phys_disable_autorefresh(phys_enc);
  1608. }
  1609. static void sde_encoder_phys_cmd_trigger_start(
  1610. struct sde_encoder_phys *phys_enc)
  1611. {
  1612. struct sde_encoder_phys_cmd *cmd_enc =
  1613. to_sde_encoder_phys_cmd(phys_enc);
  1614. u32 frame_cnt;
  1615. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  1616. if (!phys_enc)
  1617. return;
  1618. /* we don't issue CTL_START when using autorefresh */
  1619. frame_cnt = _sde_encoder_phys_cmd_get_autorefresh_property(phys_enc);
  1620. if (frame_cnt) {
  1621. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, frame_cnt);
  1622. atomic_inc(&cmd_enc->autorefresh.kickoff_cnt);
  1623. } else {
  1624. sde_encoder_helper_trigger_start(phys_enc);
  1625. }
  1626. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  1627. SDE_EVT32(DRMID(phys_enc->parent), frame_cnt, info[0].pp_idx, info[0].intf_idx,
  1628. info[0].intf_frame_count, info[0].wr_ptr_line_count, info[0].rd_ptr_line_count,
  1629. info[1].pp_idx, info[1].intf_idx, info[1].intf_frame_count,
  1630. info[1].wr_ptr_line_count, info[1].rd_ptr_line_count);
  1631. /* wr_ptr_wait_success is set true when wr_ptr arrives */
  1632. cmd_enc->wr_ptr_wait_success = false;
  1633. }
  1634. static void _sde_encoder_phys_cmd_calculate_wd_params(struct sde_encoder_phys *phys_enc,
  1635. struct intf_wd_jitter_params *wd_jitter)
  1636. {
  1637. u32 nominal_te_value;
  1638. struct sde_encoder_virt *sde_enc;
  1639. struct msm_mode_info *mode_info;
  1640. const u32 multiplier = 1 << 10;
  1641. sde_enc = to_sde_encoder_virt(phys_enc->parent);
  1642. mode_info = &sde_enc->mode_info;
  1643. if (mode_info->wd_jitter.jitter_type & MSM_DISPLAY_WD_INSTANTANEOUS_JITTER)
  1644. wd_jitter->jitter = mult_frac(multiplier, mode_info->wd_jitter.inst_jitter_numer,
  1645. (mode_info->wd_jitter.inst_jitter_denom * 100));
  1646. if (mode_info->wd_jitter.jitter_type & MSM_DISPLAY_WD_LTJ_JITTER) {
  1647. nominal_te_value = CALCULATE_WD_LOAD_VALUE(mode_info->frame_rate) * MDP_TICK_COUNT;
  1648. wd_jitter->ltj_max = mult_frac(nominal_te_value, mode_info->wd_jitter.ltj_max_numer,
  1649. (mode_info->wd_jitter.ltj_max_denom) * 100);
  1650. wd_jitter->ltj_slope = mult_frac((1 << 16), wd_jitter->ltj_max,
  1651. (mode_info->wd_jitter.ltj_time_sec * mode_info->frame_rate));
  1652. }
  1653. phys_enc->hw_intf->ops.configure_wd_jitter(phys_enc->hw_intf, wd_jitter);
  1654. }
  1655. static void sde_encoder_phys_cmd_setup_vsync_source(struct sde_encoder_phys *phys_enc,
  1656. u32 vsync_source, struct msm_display_info *disp_info)
  1657. {
  1658. struct sde_encoder_virt *sde_enc;
  1659. struct sde_connector *sde_conn;
  1660. struct intf_wd_jitter_params wd_jitter = {0, 0};
  1661. if (!phys_enc || !phys_enc->hw_intf)
  1662. return;
  1663. sde_enc = to_sde_encoder_virt(phys_enc->parent);
  1664. if (!sde_enc)
  1665. return;
  1666. sde_conn = to_sde_connector(phys_enc->connector);
  1667. if ((disp_info->is_te_using_watchdog_timer || sde_conn->panel_dead) &&
  1668. phys_enc->hw_intf->ops.setup_vsync_source) {
  1669. vsync_source = SDE_VSYNC_SOURCE_WD_TIMER_0;
  1670. if (phys_enc->hw_intf->ops.configure_wd_jitter)
  1671. _sde_encoder_phys_cmd_calculate_wd_params(phys_enc, &wd_jitter);
  1672. phys_enc->hw_intf->ops.setup_vsync_source(phys_enc->hw_intf,
  1673. sde_enc->mode_info.frame_rate);
  1674. } else {
  1675. sde_encoder_helper_vsync_config(phys_enc, vsync_source);
  1676. }
  1677. if (phys_enc->has_intf_te && phys_enc->hw_intf->ops.vsync_sel)
  1678. phys_enc->hw_intf->ops.vsync_sel(phys_enc->hw_intf,
  1679. vsync_source);
  1680. }
  1681. void sde_encoder_phys_cmd_add_enc_to_minidump(struct sde_encoder_phys *phys_enc)
  1682. {
  1683. struct sde_encoder_phys_cmd *cmd_enc;
  1684. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1685. sde_mini_dump_add_va_region("sde_enc_phys_cmd", sizeof(*cmd_enc), cmd_enc);
  1686. }
  1687. static void sde_encoder_phys_cmd_init_ops(struct sde_encoder_phys_ops *ops)
  1688. {
  1689. ops->prepare_commit = sde_encoder_phys_cmd_prepare_commit;
  1690. ops->is_master = sde_encoder_phys_cmd_is_master;
  1691. ops->mode_set = sde_encoder_phys_cmd_mode_set;
  1692. ops->cont_splash_mode_set = sde_encoder_phys_cmd_cont_splash_mode_set;
  1693. ops->mode_fixup = sde_encoder_phys_cmd_mode_fixup;
  1694. ops->enable = sde_encoder_phys_cmd_enable;
  1695. ops->disable = sde_encoder_phys_cmd_disable;
  1696. ops->destroy = sde_encoder_phys_cmd_destroy;
  1697. ops->get_hw_resources = sde_encoder_phys_cmd_get_hw_resources;
  1698. ops->control_vblank_irq = sde_encoder_phys_cmd_control_vblank_irq;
  1699. ops->wait_for_commit_done = sde_encoder_phys_cmd_wait_for_commit_done;
  1700. ops->prepare_for_kickoff = sde_encoder_phys_cmd_prepare_for_kickoff;
  1701. ops->wait_for_tx_complete = sde_encoder_phys_cmd_wait_for_tx_complete;
  1702. ops->wait_for_vblank = sde_encoder_phys_cmd_wait_for_vblank;
  1703. ops->trigger_flush = sde_encoder_helper_trigger_flush;
  1704. ops->trigger_start = sde_encoder_phys_cmd_trigger_start;
  1705. ops->needs_single_flush = sde_encoder_phys_needs_single_flush;
  1706. ops->hw_reset = sde_encoder_helper_hw_reset;
  1707. ops->irq_control = sde_encoder_phys_cmd_irq_control;
  1708. ops->update_split_role = sde_encoder_phys_cmd_update_split_role;
  1709. ops->restore = sde_encoder_phys_cmd_enable_helper;
  1710. ops->control_te = sde_encoder_phys_cmd_connect_te;
  1711. ops->is_autorefresh_enabled =
  1712. sde_encoder_phys_cmd_is_autorefresh_enabled;
  1713. ops->get_line_count = sde_encoder_phys_cmd_te_get_line_count;
  1714. ops->wait_for_active = NULL;
  1715. ops->setup_vsync_source = sde_encoder_phys_cmd_setup_vsync_source;
  1716. ops->setup_misr = sde_encoder_helper_setup_misr;
  1717. ops->collect_misr = sde_encoder_helper_collect_misr;
  1718. ops->add_to_minidump = sde_encoder_phys_cmd_add_enc_to_minidump;
  1719. ops->disable_autorefresh = _sde_encoder_phys_disable_autorefresh;
  1720. }
  1721. static inline bool sde_encoder_phys_cmd_intf_te_supported(
  1722. const struct sde_mdss_cfg *sde_cfg, enum sde_intf idx)
  1723. {
  1724. if (sde_cfg && ((idx - INTF_0) < sde_cfg->intf_count))
  1725. return test_bit(SDE_INTF_TE,
  1726. &(sde_cfg->intf[idx - INTF_0].features));
  1727. return false;
  1728. }
  1729. struct sde_encoder_phys *sde_encoder_phys_cmd_init(
  1730. struct sde_enc_phys_init_params *p)
  1731. {
  1732. struct sde_encoder_phys *phys_enc = NULL;
  1733. struct sde_encoder_phys_cmd *cmd_enc = NULL;
  1734. struct sde_hw_mdp *hw_mdp;
  1735. struct sde_encoder_irq *irq;
  1736. int i, ret = 0;
  1737. SDE_DEBUG("intf %d\n", p->intf_idx - INTF_0);
  1738. cmd_enc = kzalloc(sizeof(*cmd_enc), GFP_KERNEL);
  1739. if (!cmd_enc) {
  1740. ret = -ENOMEM;
  1741. SDE_ERROR("failed to allocate\n");
  1742. goto fail;
  1743. }
  1744. phys_enc = &cmd_enc->base;
  1745. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  1746. if (IS_ERR_OR_NULL(hw_mdp)) {
  1747. ret = PTR_ERR(hw_mdp);
  1748. SDE_ERROR("failed to get mdptop\n");
  1749. goto fail_mdp_init;
  1750. }
  1751. phys_enc->hw_mdptop = hw_mdp;
  1752. phys_enc->intf_idx = p->intf_idx;
  1753. phys_enc->parent = p->parent;
  1754. phys_enc->parent_ops = p->parent_ops;
  1755. phys_enc->sde_kms = p->sde_kms;
  1756. phys_enc->split_role = p->split_role;
  1757. phys_enc->intf_mode = INTF_MODE_CMD;
  1758. phys_enc->enc_spinlock = p->enc_spinlock;
  1759. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  1760. cmd_enc->stream_sel = 0;
  1761. phys_enc->enable_state = SDE_ENC_DISABLED;
  1762. phys_enc->kickoff_timeout_ms = DEFAULT_KICKOFF_TIMEOUT_MS;
  1763. sde_encoder_phys_cmd_init_ops(&phys_enc->ops);
  1764. phys_enc->comp_type = p->comp_type;
  1765. phys_enc->has_intf_te = sde_encoder_phys_cmd_intf_te_supported(
  1766. phys_enc->sde_kms->catalog, phys_enc->intf_idx);
  1767. for (i = 0; i < INTR_IDX_MAX; i++) {
  1768. irq = &phys_enc->irq[i];
  1769. INIT_LIST_HEAD(&irq->cb.list);
  1770. irq->irq_idx = -EINVAL;
  1771. irq->hw_idx = -EINVAL;
  1772. irq->cb.arg = phys_enc;
  1773. }
  1774. irq = &phys_enc->irq[INTR_IDX_CTL_START];
  1775. irq->name = "ctl_start";
  1776. irq->intr_type = SDE_IRQ_TYPE_CTL_START;
  1777. irq->intr_idx = INTR_IDX_CTL_START;
  1778. irq->cb.func = NULL;
  1779. irq = &phys_enc->irq[INTR_IDX_CTL_DONE];
  1780. irq->name = "ctl_done";
  1781. irq->intr_type = SDE_IRQ_TYPE_CTL_DONE;
  1782. irq->intr_idx = INTR_IDX_CTL_DONE;
  1783. irq->cb.func = sde_encoder_phys_cmd_ctl_done_irq;
  1784. irq = &phys_enc->irq[INTR_IDX_PINGPONG];
  1785. irq->name = "pp_done";
  1786. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_COMP;
  1787. irq->intr_idx = INTR_IDX_PINGPONG;
  1788. irq->cb.func = sde_encoder_phys_cmd_pp_tx_done_irq;
  1789. irq = &phys_enc->irq[INTR_IDX_RDPTR];
  1790. irq->intr_idx = INTR_IDX_RDPTR;
  1791. irq->name = "te_rd_ptr";
  1792. if (phys_enc->has_intf_te)
  1793. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_RD_PTR;
  1794. else
  1795. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_RD_PTR;
  1796. irq->cb.func = sde_encoder_phys_cmd_te_rd_ptr_irq;
  1797. irq = &phys_enc->irq[INTR_IDX_AUTOREFRESH_DONE];
  1798. irq->name = "autorefresh_done";
  1799. if (phys_enc->has_intf_te)
  1800. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_AUTO_REF;
  1801. else
  1802. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_AUTO_REF;
  1803. irq->intr_idx = INTR_IDX_AUTOREFRESH_DONE;
  1804. irq->cb.func = sde_encoder_phys_cmd_autorefresh_done_irq;
  1805. irq = &phys_enc->irq[INTR_IDX_WRPTR];
  1806. irq->intr_idx = INTR_IDX_WRPTR;
  1807. irq->name = "wr_ptr";
  1808. if (phys_enc->has_intf_te)
  1809. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_WR_PTR;
  1810. else
  1811. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_WR_PTR;
  1812. irq->cb.func = sde_encoder_phys_cmd_wr_ptr_irq;
  1813. atomic_set(&phys_enc->vblank_refcount, 0);
  1814. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1815. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  1816. atomic_set(&cmd_enc->pending_vblank_cnt, 0);
  1817. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  1818. init_waitqueue_head(&cmd_enc->pending_vblank_wq);
  1819. atomic_set(&cmd_enc->autorefresh.kickoff_cnt, 0);
  1820. init_waitqueue_head(&cmd_enc->autorefresh.kickoff_wq);
  1821. INIT_LIST_HEAD(&cmd_enc->te_timestamp_list);
  1822. for (i = 0; i < MAX_TE_PROFILE_COUNT; i++)
  1823. list_add(&cmd_enc->te_timestamp[i].list,
  1824. &cmd_enc->te_timestamp_list);
  1825. SDE_DEBUG_CMDENC(cmd_enc, "created\n");
  1826. return phys_enc;
  1827. fail_mdp_init:
  1828. kfree(cmd_enc);
  1829. fail:
  1830. return ERR_PTR(ret);
  1831. }