sde_encoder_phys_cmd.c 84 KB

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