sde_encoder_phys_cmd.c 72 KB

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