sde_encoder_phys_cmd.c 62 KB

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