sde_encoder_phys_cmd.c 67 KB

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