sde_encoder_phys_cmd.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
  7. #include "sde_encoder_phys.h"
  8. #include "sde_hw_interrupts.h"
  9. #include "sde_core_irq.h"
  10. #include "sde_formats.h"
  11. #include "sde_trace.h"
  12. #define SDE_DEBUG_CMDENC(e, fmt, ...) SDE_DEBUG("enc%d intf%d " fmt, \
  13. (e) && (e)->base.parent ? \
  14. (e)->base.parent->base.id : -1, \
  15. (e) ? (e)->base.intf_idx - INTF_0 : -1, ##__VA_ARGS__)
  16. #define SDE_ERROR_CMDENC(e, fmt, ...) SDE_ERROR("enc%d intf%d " fmt, \
  17. (e) && (e)->base.parent ? \
  18. (e)->base.parent->base.id : -1, \
  19. (e) ? (e)->base.intf_idx - INTF_0 : -1, ##__VA_ARGS__)
  20. #define to_sde_encoder_phys_cmd(x) \
  21. container_of(x, struct sde_encoder_phys_cmd, base)
  22. /*
  23. * Tearcheck sync start and continue thresholds are empirically found
  24. * based on common panels In the future, may want to allow panels to override
  25. * these default values
  26. */
  27. #define DEFAULT_TEARCHECK_SYNC_THRESH_START 4
  28. #define DEFAULT_TEARCHECK_SYNC_THRESH_CONTINUE 4
  29. #define SDE_ENC_WR_PTR_START_TIMEOUT_US 20000
  30. #define AUTOREFRESH_SEQ1_POLL_TIME 2000
  31. #define AUTOREFRESH_SEQ2_POLL_TIME 25000
  32. #define AUTOREFRESH_SEQ2_POLL_TIMEOUT 1000000
  33. static inline int _sde_encoder_phys_cmd_get_idle_timeout(
  34. struct sde_encoder_phys *phys_enc)
  35. {
  36. u32 timeout = phys_enc->kickoff_timeout_ms;
  37. struct sde_encoder_phys_cmd *cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  38. return cmd_enc->autorefresh.cfg.frame_count ?
  39. cmd_enc->autorefresh.cfg.frame_count * timeout : timeout;
  40. }
  41. static inline bool sde_encoder_phys_cmd_is_master(
  42. struct sde_encoder_phys *phys_enc)
  43. {
  44. return (phys_enc->split_role != ENC_ROLE_SLAVE) ? true : false;
  45. }
  46. static bool sde_encoder_phys_cmd_mode_fixup(
  47. struct sde_encoder_phys *phys_enc,
  48. const struct drm_display_mode *mode,
  49. struct drm_display_mode *adj_mode)
  50. {
  51. if (phys_enc)
  52. SDE_DEBUG_CMDENC(to_sde_encoder_phys_cmd(phys_enc), "\n");
  53. return true;
  54. }
  55. static uint64_t _sde_encoder_phys_cmd_get_autorefresh_property(
  56. struct sde_encoder_phys *phys_enc)
  57. {
  58. struct drm_connector *conn = phys_enc->connector;
  59. if (!conn || !conn->state)
  60. return 0;
  61. return sde_connector_get_property(conn->state,
  62. CONNECTOR_PROP_AUTOREFRESH);
  63. }
  64. static void _sde_encoder_phys_cmd_config_autorefresh(
  65. struct sde_encoder_phys *phys_enc,
  66. u32 new_frame_count)
  67. {
  68. struct sde_encoder_phys_cmd *cmd_enc =
  69. to_sde_encoder_phys_cmd(phys_enc);
  70. struct sde_hw_pingpong *hw_pp = phys_enc->hw_pp;
  71. struct sde_hw_intf *hw_intf = phys_enc->hw_intf;
  72. struct drm_connector *conn = phys_enc->connector;
  73. struct sde_hw_autorefresh *cfg_cur, cfg_nxt;
  74. if (!conn || !conn->state || !hw_pp || !hw_intf)
  75. return;
  76. cfg_cur = &cmd_enc->autorefresh.cfg;
  77. /* autorefresh property value should be validated already */
  78. memset(&cfg_nxt, 0, sizeof(cfg_nxt));
  79. cfg_nxt.frame_count = new_frame_count;
  80. cfg_nxt.enable = (cfg_nxt.frame_count != 0);
  81. SDE_DEBUG_CMDENC(cmd_enc, "autorefresh state %d->%d framecount %d\n",
  82. cfg_cur->enable, cfg_nxt.enable, cfg_nxt.frame_count);
  83. SDE_EVT32(DRMID(phys_enc->parent), hw_pp->idx, hw_intf->idx,
  84. cfg_cur->enable, cfg_nxt.enable, cfg_nxt.frame_count);
  85. /* only proceed on state changes */
  86. if (cfg_nxt.enable == cfg_cur->enable)
  87. return;
  88. memcpy(cfg_cur, &cfg_nxt, sizeof(*cfg_cur));
  89. if (phys_enc->has_intf_te && hw_intf->ops.setup_autorefresh)
  90. hw_intf->ops.setup_autorefresh(hw_intf, cfg_cur);
  91. else if (hw_pp->ops.setup_autorefresh)
  92. hw_pp->ops.setup_autorefresh(hw_pp, cfg_cur);
  93. }
  94. static void _sde_encoder_phys_cmd_update_flush_mask(
  95. struct sde_encoder_phys *phys_enc)
  96. {
  97. struct sde_encoder_phys_cmd *cmd_enc;
  98. struct sde_hw_ctl *ctl;
  99. if (!phys_enc || !phys_enc->hw_intf || !phys_enc->hw_pp)
  100. return;
  101. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  102. ctl = phys_enc->hw_ctl;
  103. if (!ctl)
  104. return;
  105. if (!ctl->ops.update_bitmask) {
  106. SDE_ERROR("invalid hw_ctl ops %d\n", ctl->idx);
  107. return;
  108. }
  109. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_INTF, phys_enc->intf_idx, 1);
  110. if (phys_enc->hw_pp->merge_3d)
  111. ctl->ops.update_bitmask(ctl, SDE_HW_FLUSH_MERGE_3D,
  112. phys_enc->hw_pp->merge_3d->idx, 1);
  113. SDE_DEBUG_CMDENC(cmd_enc, "update pending flush ctl %d intf_idx %x\n",
  114. ctl->idx - CTL_0, phys_enc->intf_idx);
  115. }
  116. static void _sde_encoder_phys_cmd_update_intf_cfg(
  117. struct sde_encoder_phys *phys_enc)
  118. {
  119. struct sde_encoder_phys_cmd *cmd_enc =
  120. to_sde_encoder_phys_cmd(phys_enc);
  121. struct sde_hw_ctl *ctl;
  122. if (!phys_enc)
  123. return;
  124. ctl = phys_enc->hw_ctl;
  125. if (!ctl)
  126. return;
  127. if (ctl->ops.setup_intf_cfg) {
  128. struct sde_hw_intf_cfg intf_cfg = { 0 };
  129. intf_cfg.intf = phys_enc->intf_idx;
  130. intf_cfg.intf_mode_sel = SDE_CTL_MODE_SEL_CMD;
  131. intf_cfg.stream_sel = cmd_enc->stream_sel;
  132. intf_cfg.mode_3d =
  133. sde_encoder_helper_get_3d_blend_mode(phys_enc);
  134. ctl->ops.setup_intf_cfg(ctl, &intf_cfg);
  135. } else if (test_bit(SDE_CTL_ACTIVE_CFG, &ctl->caps->features)) {
  136. sde_encoder_helper_update_intf_cfg(phys_enc);
  137. }
  138. }
  139. static void sde_encoder_override_tearcheck_rd_ptr(struct sde_encoder_phys *phys_enc)
  140. {
  141. struct sde_hw_intf *hw_intf;
  142. struct drm_display_mode *mode;
  143. struct sde_encoder_phys_cmd *cmd_enc;
  144. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  145. u32 adjusted_tear_rd_ptr_line_cnt;
  146. if (!phys_enc || !phys_enc->hw_intf)
  147. return;
  148. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  149. hw_intf = phys_enc->hw_intf;
  150. mode = &phys_enc->cached_mode;
  151. /* Configure TE rd_ptr_val to the end of qsync Start Window.
  152. * This ensures next frame trigger_start does not get latched in the current
  153. * vsync window.
  154. */
  155. adjusted_tear_rd_ptr_line_cnt = mode->vdisplay + cmd_enc->qsync_threshold_lines + 1;
  156. if (hw_intf && hw_intf->ops.override_tear_rd_ptr_val)
  157. hw_intf->ops.override_tear_rd_ptr_val(hw_intf, adjusted_tear_rd_ptr_line_cnt);
  158. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  159. SDE_EVT32_VERBOSE(phys_enc->hw_intf->idx - INTF_0, mode->vdisplay,
  160. cmd_enc->qsync_threshold_lines, info[0].rd_ptr_line_count,
  161. info[0].rd_ptr_frame_count, info[0].wr_ptr_line_count,
  162. info[1].rd_ptr_line_count, info[1].rd_ptr_frame_count, info[1].wr_ptr_line_count);
  163. }
  164. static void _sde_encoder_phys_signal_frame_done(struct sde_encoder_phys *phys_enc)
  165. {
  166. struct sde_encoder_phys_cmd *cmd_enc;
  167. struct sde_hw_ctl *ctl;
  168. u32 scheduler_status = INVALID_CTL_STATUS, event = 0;
  169. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  170. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  171. ctl = phys_enc->hw_ctl;
  172. if (!ctl)
  173. return;
  174. /* notify all synchronous clients first, then asynchronous clients */
  175. if (phys_enc->parent_ops.handle_frame_done &&
  176. atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0)) {
  177. event = SDE_ENCODER_FRAME_EVENT_DONE |
  178. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  179. spin_lock(phys_enc->enc_spinlock);
  180. phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
  181. phys_enc, event);
  182. if (cmd_enc->frame_tx_timeout_report_cnt)
  183. phys_enc->recovered = true;
  184. spin_unlock(phys_enc->enc_spinlock);
  185. }
  186. if (ctl->ops.get_scheduler_status)
  187. scheduler_status = ctl->ops.get_scheduler_status(ctl);
  188. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  189. SDE_EVT32_IRQ(DRMID(phys_enc->parent), ctl->idx - CTL_0, phys_enc->hw_pp->idx - PINGPONG_0,
  190. event, scheduler_status, phys_enc->autorefresh_disable_trans, info[0].pp_idx,
  191. info[0].intf_idx, info[0].intf_frame_count, info[0].wr_ptr_line_count,
  192. info[0].rd_ptr_line_count, info[1].pp_idx, info[1].intf_idx,
  193. info[1].intf_frame_count, info[1].wr_ptr_line_count, info[1].rd_ptr_line_count);
  194. /*
  195. * For hw-fences, in the last frame during the autorefresh disable transition
  196. * hw won't trigger the output-fence signal once the frame is done, therefore
  197. * sw must trigger the override to force the signal here
  198. */
  199. if (phys_enc->autorefresh_disable_trans) {
  200. if (ctl->ops.trigger_output_fence_override)
  201. ctl->ops.trigger_output_fence_override(ctl);
  202. phys_enc->autorefresh_disable_trans = false;
  203. }
  204. /* Signal any waiting atomic commit thread */
  205. wake_up_all(&phys_enc->pending_kickoff_wq);
  206. }
  207. static void sde_encoder_phys_cmd_ctl_done_irq(void *arg, int irq_idx)
  208. {
  209. struct sde_encoder_phys *phys_enc = arg;
  210. if (!phys_enc)
  211. return;
  212. SDE_ATRACE_BEGIN("ctl_done_irq");
  213. _sde_encoder_phys_signal_frame_done(phys_enc);
  214. SDE_ATRACE_END("ctl_done_irq");
  215. }
  216. static void sde_encoder_phys_cmd_pp_tx_done_irq(void *arg, int irq_idx)
  217. {
  218. struct sde_encoder_phys *phys_enc = arg;
  219. if (!phys_enc || !phys_enc->hw_pp)
  220. return;
  221. SDE_ATRACE_BEGIN("pp_done_irq");
  222. _sde_encoder_phys_signal_frame_done(phys_enc);
  223. SDE_ATRACE_END("pp_done_irq");
  224. }
  225. static void sde_encoder_phys_cmd_autorefresh_done_irq(void *arg, int irq_idx)
  226. {
  227. struct sde_encoder_phys *phys_enc = arg;
  228. struct sde_encoder_phys_cmd *cmd_enc =
  229. to_sde_encoder_phys_cmd(phys_enc);
  230. unsigned long lock_flags;
  231. int new_cnt;
  232. if (!cmd_enc)
  233. return;
  234. phys_enc = &cmd_enc->base;
  235. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  236. new_cnt = atomic_add_unless(&cmd_enc->autorefresh.kickoff_cnt, -1, 0);
  237. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  238. SDE_EVT32_IRQ(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  239. phys_enc->hw_intf->idx - INTF_0, new_cnt);
  240. if (new_cnt)
  241. _sde_encoder_phys_signal_frame_done(phys_enc);
  242. /* Signal any waiting atomic commit thread */
  243. wake_up_all(&cmd_enc->autorefresh.kickoff_wq);
  244. }
  245. static void sde_encoder_phys_cmd_te_rd_ptr_irq(void *arg, int irq_idx)
  246. {
  247. struct sde_encoder_phys *phys_enc = arg;
  248. struct sde_encoder_phys_cmd *cmd_enc;
  249. u32 scheduler_status = INVALID_CTL_STATUS;
  250. struct sde_hw_ctl *ctl;
  251. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  252. struct sde_encoder_phys_cmd_te_timestamp *te_timestamp;
  253. unsigned long lock_flags;
  254. u32 fence_ready = 0;
  255. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf || !phys_enc->hw_ctl)
  256. return;
  257. SDE_ATRACE_BEGIN("rd_ptr_irq");
  258. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  259. ctl = phys_enc->hw_ctl;
  260. if (ctl->ops.get_scheduler_status)
  261. scheduler_status = ctl->ops.get_scheduler_status(ctl);
  262. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  263. te_timestamp = list_first_entry_or_null(&cmd_enc->te_timestamp_list,
  264. struct sde_encoder_phys_cmd_te_timestamp, list);
  265. if (te_timestamp) {
  266. list_del_init(&te_timestamp->list);
  267. te_timestamp->timestamp = ktime_get();
  268. list_add_tail(&te_timestamp->list, &cmd_enc->te_timestamp_list);
  269. }
  270. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  271. if ((scheduler_status != 0x1) && ctl->ops.get_hw_fence_status)
  272. fence_ready = ctl->ops.get_hw_fence_status(ctl);
  273. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  274. SDE_EVT32_IRQ(DRMID(phys_enc->parent), scheduler_status, fence_ready, info[0].pp_idx,
  275. info[0].intf_idx, info[0].intf_frame_count, info[0].wr_ptr_line_count,
  276. info[0].rd_ptr_line_count, info[1].pp_idx, info[1].intf_idx,
  277. info[1].intf_frame_count, info[1].wr_ptr_line_count, info[1].rd_ptr_line_count);
  278. if (phys_enc->parent_ops.handle_vblank_virt)
  279. phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
  280. phys_enc);
  281. atomic_add_unless(&cmd_enc->pending_vblank_cnt, -1, 0);
  282. wake_up_all(&cmd_enc->pending_vblank_wq);
  283. SDE_ATRACE_END("rd_ptr_irq");
  284. }
  285. static void sde_encoder_phys_cmd_wr_ptr_irq(void *arg, int irq_idx)
  286. {
  287. struct sde_encoder_phys *phys_enc = arg;
  288. struct sde_hw_ctl *ctl;
  289. u32 event = 0, qsync_mode = 0;
  290. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  291. if (!phys_enc || !phys_enc->hw_ctl)
  292. return;
  293. SDE_ATRACE_BEGIN("wr_ptr_irq");
  294. ctl = phys_enc->hw_ctl;
  295. qsync_mode = sde_connector_get_qsync_mode(phys_enc->connector);
  296. if (atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0)) {
  297. event = SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  298. if (phys_enc->parent_ops.handle_frame_done) {
  299. spin_lock(phys_enc->enc_spinlock);
  300. phys_enc->parent_ops.handle_frame_done(
  301. phys_enc->parent, phys_enc, event);
  302. spin_unlock(phys_enc->enc_spinlock);
  303. }
  304. }
  305. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  306. SDE_EVT32_IRQ(DRMID(phys_enc->parent), ctl->idx - CTL_0, event, qsync_mode,
  307. info[0].pp_idx, info[0].intf_idx, info[0].intf_frame_count,
  308. info[0].wr_ptr_line_count, info[0].rd_ptr_line_count, info[1].pp_idx,
  309. info[1].intf_idx, info[1].intf_frame_count, info[1].wr_ptr_line_count,
  310. info[1].rd_ptr_line_count);
  311. if (qsync_mode &&
  312. !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 = DEFAULT_TEARCHECK_SYNC_THRESH_START;
  802. struct sde_encoder_phys_cmd *cmd_enc =
  803. to_sde_encoder_phys_cmd(phys_enc);
  804. if (!conn || !conn->state)
  805. return 0;
  806. mode = &phys_enc->cached_mode;
  807. qsync_mode = sde_connector_get_qsync_mode(conn);
  808. if (mode && (qsync_mode == SDE_RM_QSYNC_CONTINUOUS_MODE)) {
  809. u32 qsync_min_fps = 0;
  810. u32 default_fps = drm_mode_vrefresh(mode);
  811. u32 yres = mode->vtotal;
  812. u32 slow_time_ns;
  813. u32 default_time_ns;
  814. u32 extra_time_ns;
  815. u32 default_line_time_ns;
  816. if (phys_enc->parent_ops.get_qsync_fps)
  817. phys_enc->parent_ops.get_qsync_fps(
  818. phys_enc->parent, &qsync_min_fps, conn->state);
  819. if (!qsync_min_fps || !default_fps || !yres) {
  820. SDE_ERROR_CMDENC(cmd_enc,
  821. "wrong qsync params %d %d %d\n",
  822. qsync_min_fps, default_fps, yres);
  823. goto exit;
  824. }
  825. if (qsync_min_fps >= default_fps) {
  826. SDE_ERROR_CMDENC(cmd_enc,
  827. "qsync fps:%d must be less than default:%d\n",
  828. qsync_min_fps, default_fps);
  829. goto exit;
  830. }
  831. /* Calculate the number of extra lines*/
  832. slow_time_ns = DIV_ROUND_UP(1000000000, qsync_min_fps);
  833. default_time_ns = DIV_ROUND_UP(1000000000, default_fps);
  834. extra_time_ns = slow_time_ns - default_time_ns;
  835. default_line_time_ns = DIV_ROUND_UP(default_time_ns, yres);
  836. threshold_lines = extra_time_ns / default_line_time_ns;
  837. /* some DDICs express the timeout value in lines/4, round down to compensate */
  838. threshold_lines = round_down(threshold_lines, 4);
  839. /* remove 2 lines to cover for latency */
  840. if (threshold_lines - 2 > DEFAULT_TEARCHECK_SYNC_THRESH_START)
  841. threshold_lines -= 2;
  842. SDE_DEBUG_CMDENC(cmd_enc, "slow:%d default:%d extra:%d(ns)\n",
  843. slow_time_ns, default_time_ns, extra_time_ns);
  844. SDE_DEBUG_CMDENC(cmd_enc, "min_fps:%d fps:%d yres:%d lines:%d\n",
  845. qsync_min_fps, default_fps, yres, threshold_lines);
  846. SDE_EVT32(qsync_mode, qsync_min_fps, extra_time_ns, default_fps,
  847. yres, threshold_lines);
  848. }
  849. exit:
  850. return threshold_lines;
  851. }
  852. static void sde_encoder_phys_cmd_tearcheck_config(
  853. struct sde_encoder_phys *phys_enc)
  854. {
  855. struct sde_encoder_phys_cmd *cmd_enc =
  856. to_sde_encoder_phys_cmd(phys_enc);
  857. struct sde_hw_tear_check tc_cfg = { 0 };
  858. struct drm_display_mode *mode;
  859. bool tc_enable = true;
  860. u32 vsync_hz;
  861. int vrefresh;
  862. struct msm_drm_private *priv;
  863. struct sde_kms *sde_kms;
  864. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf) {
  865. SDE_ERROR("invalid encoder\n");
  866. return;
  867. }
  868. mode = &phys_enc->cached_mode;
  869. SDE_DEBUG_CMDENC(cmd_enc, "pp %d, intf %d\n",
  870. phys_enc->hw_pp->idx - PINGPONG_0,
  871. phys_enc->hw_intf->idx - INTF_0);
  872. if (phys_enc->has_intf_te) {
  873. if (!phys_enc->hw_intf->ops.setup_tearcheck ||
  874. !phys_enc->hw_intf->ops.enable_tearcheck) {
  875. SDE_DEBUG_CMDENC(cmd_enc, "tearcheck not supported\n");
  876. return;
  877. }
  878. } else {
  879. if (!phys_enc->hw_pp->ops.setup_tearcheck ||
  880. !phys_enc->hw_pp->ops.enable_tearcheck) {
  881. SDE_DEBUG_CMDENC(cmd_enc, "tearcheck not supported\n");
  882. return;
  883. }
  884. }
  885. sde_kms = phys_enc->sde_kms;
  886. if (!sde_kms || !sde_kms->dev || !sde_kms->dev->dev_private) {
  887. SDE_ERROR("invalid device\n");
  888. return;
  889. }
  890. priv = sde_kms->dev->dev_private;
  891. vrefresh = drm_mode_vrefresh(mode);
  892. /*
  893. * TE default: dsi byte clock calculated base on 70 fps;
  894. * around 14 ms to complete a kickoff cycle if te disabled;
  895. * vclk_line base on 60 fps; write is faster than read;
  896. * init == start == rdptr;
  897. *
  898. * vsync_count is ratio of MDP VSYNC clock frequency to LCD panel
  899. * frequency divided by the no. of rows (lines) in the LCDpanel.
  900. */
  901. vsync_hz = sde_power_clk_get_rate(&priv->phandle, "vsync_clk");
  902. if (!vsync_hz || !mode->vtotal || !vrefresh) {
  903. SDE_DEBUG_CMDENC(cmd_enc,
  904. "invalid params - vsync_hz %u vtot %u vrefresh %u\n",
  905. vsync_hz, mode->vtotal, vrefresh);
  906. return;
  907. }
  908. tc_cfg.vsync_count = vsync_hz / (mode->vtotal * vrefresh);
  909. /* enable external TE after kickoff to avoid premature autorefresh */
  910. tc_cfg.hw_vsync_mode = 0;
  911. /*
  912. * By setting sync_cfg_height to near max register value, we essentially
  913. * disable sde hw generated TE signal, since hw TE will arrive first.
  914. * Only caveat is if due to error, we hit wrap-around.
  915. */
  916. tc_cfg.sync_cfg_height = 0xFFF0;
  917. tc_cfg.vsync_init_val = mode->vdisplay;
  918. tc_cfg.sync_threshold_start = _get_tearcheck_threshold(phys_enc);
  919. tc_cfg.sync_threshold_continue = DEFAULT_TEARCHECK_SYNC_THRESH_CONTINUE;
  920. tc_cfg.start_pos = mode->vdisplay;
  921. tc_cfg.rd_ptr_irq = mode->vdisplay + 1;
  922. tc_cfg.wr_ptr_irq = 1;
  923. cmd_enc->qsync_threshold_lines = tc_cfg.sync_threshold_start;
  924. SDE_DEBUG_CMDENC(cmd_enc,
  925. "tc %d intf %d vsync_clk_speed_hz %u vtotal %u vrefresh %u\n",
  926. phys_enc->hw_pp->idx - PINGPONG_0,
  927. phys_enc->hw_intf->idx - INTF_0,
  928. vsync_hz, mode->vtotal, vrefresh);
  929. SDE_DEBUG_CMDENC(cmd_enc,
  930. "tc %d intf %d enable %u start_pos %u rd_ptr_irq %u wr_ptr_irq %u\n",
  931. phys_enc->hw_pp->idx - PINGPONG_0,
  932. phys_enc->hw_intf->idx - INTF_0,
  933. tc_enable, tc_cfg.start_pos, tc_cfg.rd_ptr_irq,
  934. tc_cfg.wr_ptr_irq);
  935. SDE_DEBUG_CMDENC(cmd_enc,
  936. "tc %d intf %d hw_vsync_mode %u vsync_count %u vsync_init_val %u\n",
  937. phys_enc->hw_pp->idx - PINGPONG_0,
  938. phys_enc->hw_intf->idx - INTF_0,
  939. tc_cfg.hw_vsync_mode, tc_cfg.vsync_count,
  940. tc_cfg.vsync_init_val);
  941. SDE_DEBUG_CMDENC(cmd_enc,
  942. "tc %d intf %d cfgheight %u thresh_start %u thresh_cont %u\n",
  943. phys_enc->hw_pp->idx - PINGPONG_0,
  944. phys_enc->hw_intf->idx - INTF_0,
  945. tc_cfg.sync_cfg_height,
  946. tc_cfg.sync_threshold_start, tc_cfg.sync_threshold_continue);
  947. SDE_EVT32(phys_enc->hw_pp->idx - PINGPONG_0, phys_enc->hw_intf->idx - INTF_0,
  948. vsync_hz, mode->vtotal, vrefresh);
  949. SDE_EVT32(tc_enable, tc_cfg.start_pos, tc_cfg.rd_ptr_irq, tc_cfg.wr_ptr_irq,
  950. tc_cfg.hw_vsync_mode, tc_cfg.vsync_count, tc_cfg.vsync_init_val,
  951. tc_cfg.sync_cfg_height, tc_cfg.sync_threshold_start,
  952. tc_cfg.sync_threshold_continue);
  953. if (phys_enc->has_intf_te) {
  954. phys_enc->hw_intf->ops.setup_tearcheck(phys_enc->hw_intf,
  955. &tc_cfg);
  956. phys_enc->hw_intf->ops.enable_tearcheck(phys_enc->hw_intf,
  957. tc_enable);
  958. } else {
  959. phys_enc->hw_pp->ops.setup_tearcheck(phys_enc->hw_pp, &tc_cfg);
  960. phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp,
  961. tc_enable);
  962. }
  963. }
  964. static void _sde_encoder_phys_cmd_pingpong_config(
  965. struct sde_encoder_phys *phys_enc)
  966. {
  967. struct sde_encoder_phys_cmd *cmd_enc =
  968. to_sde_encoder_phys_cmd(phys_enc);
  969. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
  970. SDE_ERROR("invalid arg(s), enc %d\n", !phys_enc);
  971. return;
  972. }
  973. SDE_DEBUG_CMDENC(cmd_enc, "pp %d, enabling mode:\n",
  974. phys_enc->hw_pp->idx - PINGPONG_0);
  975. drm_mode_debug_printmodeline(&phys_enc->cached_mode);
  976. if (!_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  977. _sde_encoder_phys_cmd_update_intf_cfg(phys_enc);
  978. sde_encoder_phys_cmd_tearcheck_config(phys_enc);
  979. }
  980. static void sde_encoder_phys_cmd_enable_helper(
  981. struct sde_encoder_phys *phys_enc)
  982. {
  983. struct sde_hw_intf *hw_intf;
  984. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp ||
  985. !phys_enc->hw_intf) {
  986. SDE_ERROR("invalid arg(s), encoder %d\n", !phys_enc);
  987. return;
  988. }
  989. sde_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
  990. _sde_encoder_phys_cmd_pingpong_config(phys_enc);
  991. hw_intf = phys_enc->hw_intf;
  992. if (hw_intf->ops.enable_compressed_input)
  993. hw_intf->ops.enable_compressed_input(phys_enc->hw_intf,
  994. (phys_enc->comp_type !=
  995. MSM_DISPLAY_COMPRESSION_NONE), false);
  996. if (hw_intf->ops.enable_wide_bus)
  997. hw_intf->ops.enable_wide_bus(hw_intf,
  998. sde_encoder_is_widebus_enabled(phys_enc->parent));
  999. /*
  1000. * For pp-split, skip setting the flush bit for the slave intf, since
  1001. * both intfs use same ctl and HW will only flush the master.
  1002. */
  1003. if (_sde_encoder_phys_is_ppsplit(phys_enc) &&
  1004. !sde_encoder_phys_cmd_is_master(phys_enc))
  1005. goto skip_flush;
  1006. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  1007. skip_flush:
  1008. return;
  1009. }
  1010. static void sde_encoder_phys_cmd_enable(struct sde_encoder_phys *phys_enc)
  1011. {
  1012. struct sde_encoder_phys_cmd *cmd_enc =
  1013. to_sde_encoder_phys_cmd(phys_enc);
  1014. if (!phys_enc || !phys_enc->hw_pp) {
  1015. SDE_ERROR("invalid phys encoder\n");
  1016. return;
  1017. }
  1018. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  1019. if (phys_enc->enable_state == SDE_ENC_ENABLED) {
  1020. if (!phys_enc->cont_splash_enabled)
  1021. SDE_ERROR("already enabled\n");
  1022. return;
  1023. }
  1024. sde_encoder_phys_cmd_enable_helper(phys_enc);
  1025. phys_enc->enable_state = SDE_ENC_ENABLED;
  1026. }
  1027. static bool sde_encoder_phys_cmd_is_autorefresh_enabled(
  1028. struct sde_encoder_phys *phys_enc)
  1029. {
  1030. struct sde_hw_pingpong *hw_pp;
  1031. struct sde_hw_intf *hw_intf;
  1032. struct sde_hw_autorefresh cfg;
  1033. int ret;
  1034. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1035. return false;
  1036. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1037. return false;
  1038. if (phys_enc->has_intf_te) {
  1039. hw_intf = phys_enc->hw_intf;
  1040. if (!hw_intf->ops.get_autorefresh)
  1041. return false;
  1042. ret = hw_intf->ops.get_autorefresh(hw_intf, &cfg);
  1043. } else {
  1044. hw_pp = phys_enc->hw_pp;
  1045. if (!hw_pp->ops.get_autorefresh)
  1046. return false;
  1047. ret = hw_pp->ops.get_autorefresh(hw_pp, &cfg);
  1048. }
  1049. return ret ? false : cfg.enable;
  1050. }
  1051. static void sde_encoder_phys_cmd_connect_te(
  1052. struct sde_encoder_phys *phys_enc, bool enable)
  1053. {
  1054. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1055. return;
  1056. if (phys_enc->has_intf_te &&
  1057. phys_enc->hw_intf->ops.connect_external_te)
  1058. phys_enc->hw_intf->ops.connect_external_te(phys_enc->hw_intf,
  1059. enable);
  1060. else if (phys_enc->hw_pp->ops.connect_external_te)
  1061. phys_enc->hw_pp->ops.connect_external_te(phys_enc->hw_pp,
  1062. enable);
  1063. else
  1064. return;
  1065. SDE_EVT32(DRMID(phys_enc->parent), enable);
  1066. }
  1067. static int sde_encoder_phys_cmd_te_get_line_count(
  1068. struct sde_encoder_phys *phys_enc)
  1069. {
  1070. struct sde_hw_pingpong *hw_pp;
  1071. struct sde_hw_intf *hw_intf;
  1072. u32 line_count;
  1073. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1074. return -EINVAL;
  1075. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1076. return -EINVAL;
  1077. if (phys_enc->has_intf_te) {
  1078. hw_intf = phys_enc->hw_intf;
  1079. if (!hw_intf->ops.get_line_count)
  1080. return -EINVAL;
  1081. line_count = hw_intf->ops.get_line_count(hw_intf);
  1082. } else {
  1083. hw_pp = phys_enc->hw_pp;
  1084. if (!hw_pp->ops.get_line_count)
  1085. return -EINVAL;
  1086. line_count = hw_pp->ops.get_line_count(hw_pp);
  1087. }
  1088. return line_count;
  1089. }
  1090. static void sde_encoder_phys_cmd_disable(struct sde_encoder_phys *phys_enc)
  1091. {
  1092. struct sde_encoder_phys_cmd *cmd_enc =
  1093. to_sde_encoder_phys_cmd(phys_enc);
  1094. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf) {
  1095. SDE_ERROR("invalid encoder\n");
  1096. return;
  1097. }
  1098. SDE_DEBUG_CMDENC(cmd_enc, "pp %d intf %d state %d\n",
  1099. phys_enc->hw_pp->idx - PINGPONG_0,
  1100. phys_enc->hw_intf->idx - INTF_0,
  1101. phys_enc->enable_state);
  1102. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1103. phys_enc->hw_intf->idx - INTF_0,
  1104. phys_enc->enable_state);
  1105. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1106. SDE_ERROR_CMDENC(cmd_enc, "already disabled\n");
  1107. return;
  1108. }
  1109. if (!sde_in_trusted_vm(phys_enc->sde_kms)) {
  1110. if (phys_enc->has_intf_te &&
  1111. phys_enc->hw_intf->ops.enable_tearcheck)
  1112. phys_enc->hw_intf->ops.enable_tearcheck(
  1113. phys_enc->hw_intf,
  1114. false);
  1115. else if (phys_enc->hw_pp->ops.enable_tearcheck)
  1116. phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp,
  1117. false);
  1118. if (sde_encoder_phys_cmd_is_master(phys_enc))
  1119. sde_encoder_helper_phys_disable(phys_enc, NULL);
  1120. if (phys_enc->hw_intf->ops.reset_counter)
  1121. phys_enc->hw_intf->ops.reset_counter(phys_enc->hw_intf);
  1122. }
  1123. memset(&cmd_enc->autorefresh.cfg, 0, sizeof(struct sde_hw_autorefresh));
  1124. phys_enc->enable_state = SDE_ENC_DISABLED;
  1125. }
  1126. static void sde_encoder_phys_cmd_destroy(struct sde_encoder_phys *phys_enc)
  1127. {
  1128. struct sde_encoder_phys_cmd *cmd_enc =
  1129. to_sde_encoder_phys_cmd(phys_enc);
  1130. if (!phys_enc) {
  1131. SDE_ERROR("invalid encoder\n");
  1132. return;
  1133. }
  1134. kfree(cmd_enc);
  1135. }
  1136. static void sde_encoder_phys_cmd_get_hw_resources(
  1137. struct sde_encoder_phys *phys_enc,
  1138. struct sde_encoder_hw_resources *hw_res,
  1139. struct drm_connector_state *conn_state)
  1140. {
  1141. struct sde_encoder_phys_cmd *cmd_enc =
  1142. to_sde_encoder_phys_cmd(phys_enc);
  1143. if (!phys_enc) {
  1144. SDE_ERROR("invalid encoder\n");
  1145. return;
  1146. }
  1147. if ((phys_enc->intf_idx - INTF_0) >= INTF_MAX) {
  1148. SDE_ERROR("invalid intf idx:%d\n", phys_enc->intf_idx);
  1149. return;
  1150. }
  1151. SDE_DEBUG_CMDENC(cmd_enc, "\n");
  1152. hw_res->intfs[phys_enc->intf_idx - INTF_0] = INTF_MODE_CMD;
  1153. }
  1154. static int sde_encoder_phys_cmd_prepare_for_kickoff(
  1155. struct sde_encoder_phys *phys_enc,
  1156. struct sde_encoder_kickoff_params *params)
  1157. {
  1158. struct sde_hw_tear_check tc_cfg = {0};
  1159. struct sde_encoder_phys_cmd *cmd_enc =
  1160. to_sde_encoder_phys_cmd(phys_enc);
  1161. int ret = 0;
  1162. bool recovery_events;
  1163. if (!phys_enc || !phys_enc->hw_pp) {
  1164. SDE_ERROR("invalid encoder\n");
  1165. return -EINVAL;
  1166. }
  1167. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  1168. phys_enc->frame_trigger_mode = params->frame_trigger_mode;
  1169. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1170. atomic_read(&phys_enc->pending_kickoff_cnt),
  1171. atomic_read(&cmd_enc->autorefresh.kickoff_cnt),
  1172. phys_enc->frame_trigger_mode);
  1173. if (phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_DEFAULT) {
  1174. /*
  1175. * Mark kickoff request as outstanding. If there are more
  1176. * than one outstanding frame, then we have to wait for the
  1177. * previous frame to complete
  1178. */
  1179. ret = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1180. if (ret) {
  1181. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1182. SDE_EVT32(DRMID(phys_enc->parent),
  1183. phys_enc->hw_pp->idx - PINGPONG_0);
  1184. SDE_ERROR("failed wait_for_idle: %d\n", ret);
  1185. }
  1186. }
  1187. if (phys_enc->recovered) {
  1188. recovery_events = sde_encoder_recovery_events_enabled(
  1189. phys_enc->parent);
  1190. if (cmd_enc->frame_tx_timeout_report_cnt && recovery_events)
  1191. sde_connector_event_notify(phys_enc->connector,
  1192. DRM_EVENT_SDE_HW_RECOVERY,
  1193. sizeof(uint8_t),
  1194. SDE_RECOVERY_SUCCESS);
  1195. cmd_enc->frame_tx_timeout_report_cnt = 0;
  1196. phys_enc->recovered = false;
  1197. }
  1198. if (sde_connector_is_qsync_updated(phys_enc->connector)) {
  1199. tc_cfg.sync_threshold_start = _get_tearcheck_threshold(
  1200. phys_enc);
  1201. cmd_enc->qsync_threshold_lines = tc_cfg.sync_threshold_start;
  1202. if (phys_enc->has_intf_te &&
  1203. phys_enc->hw_intf->ops.update_tearcheck)
  1204. phys_enc->hw_intf->ops.update_tearcheck(
  1205. phys_enc->hw_intf, &tc_cfg);
  1206. else if (phys_enc->hw_pp->ops.update_tearcheck)
  1207. phys_enc->hw_pp->ops.update_tearcheck(
  1208. phys_enc->hw_pp, &tc_cfg);
  1209. SDE_EVT32(DRMID(phys_enc->parent), tc_cfg.sync_threshold_start);
  1210. }
  1211. SDE_DEBUG_CMDENC(cmd_enc, "pp:%d pending_cnt %d\n",
  1212. phys_enc->hw_pp->idx - PINGPONG_0,
  1213. atomic_read(&phys_enc->pending_kickoff_cnt));
  1214. return ret;
  1215. }
  1216. static bool _sde_encoder_phys_cmd_needs_vsync_change(
  1217. struct sde_encoder_phys *phys_enc, ktime_t profile_timestamp)
  1218. {
  1219. struct sde_encoder_phys_cmd *cmd_enc;
  1220. struct sde_encoder_phys_cmd_te_timestamp *cur;
  1221. struct sde_encoder_phys_cmd_te_timestamp *prev = NULL;
  1222. ktime_t time_diff;
  1223. u64 l_bound = 0, u_bound = 0;
  1224. bool ret = false;
  1225. unsigned long lock_flags;
  1226. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1227. sde_encoder_helper_get_jitter_bounds_ns(phys_enc->parent,
  1228. &l_bound, &u_bound);
  1229. if (!l_bound || !u_bound) {
  1230. SDE_ERROR_CMDENC(cmd_enc, "invalid vsync jitter bounds\n");
  1231. return false;
  1232. }
  1233. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  1234. list_for_each_entry_reverse(cur, &cmd_enc->te_timestamp_list, list) {
  1235. if (prev && ktime_after(cur->timestamp, profile_timestamp)) {
  1236. time_diff = ktime_sub(prev->timestamp, cur->timestamp);
  1237. if ((time_diff < l_bound) || (time_diff > u_bound)) {
  1238. ret = true;
  1239. break;
  1240. }
  1241. }
  1242. prev = cur;
  1243. }
  1244. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  1245. if (ret) {
  1246. SDE_DEBUG_CMDENC(cmd_enc,
  1247. "time_diff:%llu, prev:%llu, cur:%llu, jitter:%llu/%llu\n",
  1248. time_diff, prev->timestamp, cur->timestamp,
  1249. l_bound, u_bound);
  1250. time_diff = div_s64(time_diff, 1000);
  1251. SDE_EVT32(DRMID(phys_enc->parent),
  1252. (u32) (do_div(l_bound, 1000)),
  1253. (u32) (do_div(u_bound, 1000)),
  1254. (u32) (time_diff), SDE_EVTLOG_ERROR);
  1255. }
  1256. return ret;
  1257. }
  1258. static int _sde_encoder_phys_cmd_wait_for_wr_ptr(
  1259. struct sde_encoder_phys *phys_enc)
  1260. {
  1261. struct sde_encoder_phys_cmd *cmd_enc =
  1262. to_sde_encoder_phys_cmd(phys_enc);
  1263. struct sde_encoder_wait_info wait_info = {0};
  1264. struct sde_connector *c_conn;
  1265. bool frame_pending = true;
  1266. struct sde_hw_ctl *ctl;
  1267. unsigned long lock_flags;
  1268. int ret, timeout_ms;
  1269. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->connector) {
  1270. SDE_ERROR("invalid argument(s)\n");
  1271. return -EINVAL;
  1272. }
  1273. ctl = phys_enc->hw_ctl;
  1274. c_conn = to_sde_connector(phys_enc->connector);
  1275. timeout_ms = phys_enc->kickoff_timeout_ms;
  1276. if (c_conn->lp_mode == SDE_MODE_DPMS_LP1 ||
  1277. c_conn->lp_mode == SDE_MODE_DPMS_LP2)
  1278. timeout_ms = timeout_ms * 2;
  1279. wait_info.wq = &phys_enc->pending_kickoff_wq;
  1280. wait_info.atomic_cnt = &phys_enc->pending_retire_fence_cnt;
  1281. wait_info.timeout_ms = timeout_ms;
  1282. /* slave encoder doesn't enable for ppsplit */
  1283. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  1284. return 0;
  1285. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_WRPTR,
  1286. &wait_info);
  1287. if (ret == -ETIMEDOUT) {
  1288. struct sde_hw_ctl *ctl = phys_enc->hw_ctl;
  1289. if (ctl && ctl->ops.get_start_state)
  1290. frame_pending = ctl->ops.get_start_state(ctl);
  1291. ret = (frame_pending || sde_connector_esd_status(phys_enc->connector)) ? ret : 0;
  1292. /*
  1293. * There can be few cases of ESD where CTL_START is cleared but
  1294. * wr_ptr irq doesn't come. Signaling retire fence in these
  1295. * cases to avoid freeze and dangling pending_retire_fence_cnt
  1296. */
  1297. if (!ret) {
  1298. SDE_EVT32(DRMID(phys_enc->parent),
  1299. SDE_EVTLOG_FUNC_CASE1);
  1300. if (sde_encoder_phys_cmd_is_master(phys_enc) &&
  1301. atomic_add_unless(
  1302. &phys_enc->pending_retire_fence_cnt, -1, 0)) {
  1303. spin_lock_irqsave(phys_enc->enc_spinlock,
  1304. lock_flags);
  1305. phys_enc->parent_ops.handle_frame_done(
  1306. phys_enc->parent, phys_enc,
  1307. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  1308. spin_unlock_irqrestore(phys_enc->enc_spinlock,
  1309. lock_flags);
  1310. }
  1311. }
  1312. }
  1313. cmd_enc->wr_ptr_wait_success = (ret == 0) ? true : false;
  1314. return ret;
  1315. }
  1316. static int sde_encoder_phys_cmd_wait_for_tx_complete(
  1317. struct sde_encoder_phys *phys_enc)
  1318. {
  1319. int rc;
  1320. struct sde_encoder_phys_cmd *cmd_enc;
  1321. if (!phys_enc)
  1322. return -EINVAL;
  1323. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1324. if (sde_encoder_check_ctl_done_support(phys_enc->parent)
  1325. && !sde_encoder_phys_cmd_is_master(phys_enc))
  1326. return 0;
  1327. if (!atomic_read(&phys_enc->pending_kickoff_cnt)) {
  1328. SDE_EVT32(DRMID(phys_enc->parent),
  1329. phys_enc->intf_idx - INTF_0,
  1330. phys_enc->enable_state);
  1331. return 0;
  1332. }
  1333. rc = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1334. if (rc) {
  1335. SDE_EVT32(DRMID(phys_enc->parent),
  1336. phys_enc->intf_idx - INTF_0);
  1337. SDE_ERROR("failed wait_for_idle: %d\n", rc);
  1338. }
  1339. return rc;
  1340. }
  1341. static int _sde_encoder_phys_cmd_handle_wr_ptr_timeout(
  1342. struct sde_encoder_phys *phys_enc,
  1343. ktime_t profile_timestamp)
  1344. {
  1345. struct sde_encoder_phys_cmd *cmd_enc =
  1346. to_sde_encoder_phys_cmd(phys_enc);
  1347. bool switch_te;
  1348. int ret = -ETIMEDOUT;
  1349. unsigned long lock_flags;
  1350. switch_te = _sde_encoder_phys_cmd_needs_vsync_change(
  1351. phys_enc, profile_timestamp);
  1352. SDE_EVT32(DRMID(phys_enc->parent), switch_te, SDE_EVTLOG_FUNC_ENTRY);
  1353. if (sde_connector_panel_dead(phys_enc->connector)) {
  1354. ret = _sde_encoder_phys_cmd_wait_for_wr_ptr(phys_enc);
  1355. } else if (switch_te) {
  1356. SDE_DEBUG_CMDENC(cmd_enc,
  1357. "wr_ptr_irq wait failed, retry with WD TE\n");
  1358. /* switch to watchdog TE and wait again */
  1359. sde_encoder_helper_switch_vsync(phys_enc->parent, true);
  1360. ret = _sde_encoder_phys_cmd_wait_for_wr_ptr(phys_enc);
  1361. /* switch back to default TE */
  1362. sde_encoder_helper_switch_vsync(phys_enc->parent, false);
  1363. }
  1364. /*
  1365. * Signaling the retire fence at wr_ptr timeout
  1366. * to allow the next commit and avoid device freeze.
  1367. */
  1368. if (ret == -ETIMEDOUT) {
  1369. SDE_ERROR_CMDENC(cmd_enc,
  1370. "wr_ptr_irq wait failed, switch_te:%d\n", switch_te);
  1371. SDE_EVT32(DRMID(phys_enc->parent), switch_te, SDE_EVTLOG_ERROR);
  1372. if (sde_encoder_phys_cmd_is_master(phys_enc) &&
  1373. atomic_add_unless(
  1374. &phys_enc->pending_retire_fence_cnt, -1, 0)) {
  1375. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  1376. phys_enc->parent_ops.handle_frame_done(
  1377. phys_enc->parent, phys_enc,
  1378. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  1379. spin_unlock_irqrestore(phys_enc->enc_spinlock,
  1380. lock_flags);
  1381. }
  1382. }
  1383. cmd_enc->wr_ptr_wait_success = (ret == 0) ? true : false;
  1384. return ret;
  1385. }
  1386. static int sde_encoder_phys_cmd_wait_for_commit_done(
  1387. struct sde_encoder_phys *phys_enc)
  1388. {
  1389. int rc = 0, i, pending_cnt;
  1390. struct sde_encoder_phys_cmd *cmd_enc;
  1391. ktime_t profile_timestamp = ktime_get();
  1392. u32 scheduler_status = INVALID_CTL_STATUS;
  1393. struct sde_hw_ctl *ctl;
  1394. if (!phys_enc)
  1395. return -EINVAL;
  1396. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1397. if (sde_encoder_check_ctl_done_support(phys_enc->parent)
  1398. && !sde_encoder_phys_cmd_is_master(phys_enc))
  1399. return 0;
  1400. /* only required for master controller */
  1401. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  1402. rc = _sde_encoder_phys_cmd_wait_for_wr_ptr(phys_enc);
  1403. if (rc == -ETIMEDOUT) {
  1404. /*
  1405. * Profile all the TE received after profile_timestamp
  1406. * and if the jitter is more, switch to watchdog TE
  1407. * and wait for wr_ptr again. Finally move back to
  1408. * default TE.
  1409. */
  1410. rc = _sde_encoder_phys_cmd_handle_wr_ptr_timeout(
  1411. phys_enc, profile_timestamp);
  1412. if (rc == -ETIMEDOUT)
  1413. goto wait_for_idle;
  1414. }
  1415. if (cmd_enc->autorefresh.cfg.enable)
  1416. rc = _sde_encoder_phys_cmd_wait_for_autorefresh_done(
  1417. phys_enc);
  1418. ctl = phys_enc->hw_ctl;
  1419. if (ctl && ctl->ops.get_scheduler_status)
  1420. scheduler_status = ctl->ops.get_scheduler_status(ctl);
  1421. }
  1422. /* wait for posted start or serialize trigger */
  1423. pending_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  1424. if ((pending_cnt > 1) ||
  1425. (pending_cnt && (scheduler_status & BIT(0))) ||
  1426. (!rc && phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_SERIALIZE))
  1427. goto wait_for_idle;
  1428. return rc;
  1429. wait_for_idle:
  1430. pending_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  1431. for (i = 0; i < pending_cnt; i++)
  1432. rc |= sde_encoder_wait_for_event(phys_enc->parent,
  1433. MSM_ENC_TX_COMPLETE);
  1434. if (rc) {
  1435. SDE_EVT32(DRMID(phys_enc->parent),
  1436. phys_enc->hw_pp->idx - PINGPONG_0,
  1437. phys_enc->frame_trigger_mode,
  1438. atomic_read(&phys_enc->pending_kickoff_cnt),
  1439. phys_enc->enable_state,
  1440. cmd_enc->wr_ptr_wait_success, scheduler_status, rc);
  1441. SDE_ERROR("pp:%d failed wait_for_idle: %d\n",
  1442. phys_enc->hw_pp->idx - PINGPONG_0, rc);
  1443. if (phys_enc->enable_state == SDE_ENC_ERR_NEEDS_HW_RESET)
  1444. sde_encoder_needs_hw_reset(phys_enc->parent);
  1445. }
  1446. return rc;
  1447. }
  1448. static int sde_encoder_phys_cmd_wait_for_vblank(
  1449. struct sde_encoder_phys *phys_enc)
  1450. {
  1451. int rc = 0;
  1452. struct sde_encoder_phys_cmd *cmd_enc;
  1453. struct sde_encoder_wait_info wait_info = {0};
  1454. if (!phys_enc)
  1455. return -EINVAL;
  1456. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1457. /* only required for master controller */
  1458. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1459. return rc;
  1460. wait_info.wq = &cmd_enc->pending_vblank_wq;
  1461. wait_info.atomic_cnt = &cmd_enc->pending_vblank_cnt;
  1462. wait_info.timeout_ms = _sde_encoder_phys_cmd_get_idle_timeout(phys_enc);
  1463. atomic_inc(&cmd_enc->pending_vblank_cnt);
  1464. rc = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_RDPTR,
  1465. &wait_info);
  1466. return rc;
  1467. }
  1468. static void sde_encoder_phys_cmd_update_split_role(
  1469. struct sde_encoder_phys *phys_enc,
  1470. enum sde_enc_split_role role)
  1471. {
  1472. struct sde_encoder_phys_cmd *cmd_enc;
  1473. enum sde_enc_split_role old_role;
  1474. bool is_ppsplit;
  1475. if (!phys_enc)
  1476. return;
  1477. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1478. old_role = phys_enc->split_role;
  1479. is_ppsplit = _sde_encoder_phys_is_ppsplit(phys_enc);
  1480. phys_enc->split_role = role;
  1481. SDE_DEBUG_CMDENC(cmd_enc, "old role %d new role %d\n",
  1482. old_role, role);
  1483. /*
  1484. * ppsplit solo needs to reprogram because intf may have swapped without
  1485. * role changing on left-only, right-only back-to-back commits
  1486. */
  1487. if (!(is_ppsplit && role == ENC_ROLE_SOLO) &&
  1488. (role == old_role || role == ENC_ROLE_SKIP))
  1489. return;
  1490. sde_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
  1491. _sde_encoder_phys_cmd_pingpong_config(phys_enc);
  1492. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  1493. }
  1494. static void _sde_encoder_autorefresh_disable_seq1(
  1495. struct sde_encoder_phys *phys_enc)
  1496. {
  1497. int trial = 0;
  1498. u32 timeout_ms = phys_enc->kickoff_timeout_ms;
  1499. struct sde_encoder_phys_cmd *cmd_enc =
  1500. to_sde_encoder_phys_cmd(phys_enc);
  1501. /*
  1502. * If autorefresh is enabled, disable it and make sure it is safe to
  1503. * proceed with current frame commit/push. Sequence fallowed is,
  1504. * 1. Disable TE & autorefresh - caller will take care of it
  1505. * 2. Poll for frame transfer ongoing to be false
  1506. * 3. Enable TE back - caller will take care of it
  1507. */
  1508. do {
  1509. udelay(AUTOREFRESH_SEQ1_POLL_TIME);
  1510. if ((trial * AUTOREFRESH_SEQ1_POLL_TIME)
  1511. > (timeout_ms * USEC_PER_MSEC)) {
  1512. SDE_ERROR_CMDENC(cmd_enc,
  1513. "disable autorefresh failed\n");
  1514. phys_enc->enable_state = SDE_ENC_ERR_NEEDS_HW_RESET;
  1515. break;
  1516. }
  1517. trial++;
  1518. } while (_sde_encoder_phys_cmd_is_ongoing_pptx(phys_enc));
  1519. }
  1520. static void _sde_encoder_autorefresh_disable_seq2(
  1521. struct sde_encoder_phys *phys_enc)
  1522. {
  1523. int trial = 0;
  1524. struct sde_hw_mdp *hw_mdp = phys_enc->hw_mdptop;
  1525. u32 autorefresh_status = 0;
  1526. struct sde_encoder_phys_cmd *cmd_enc =
  1527. to_sde_encoder_phys_cmd(phys_enc);
  1528. struct intf_tear_status tear_status;
  1529. struct sde_hw_intf *hw_intf = phys_enc->hw_intf;
  1530. if (!hw_mdp->ops.get_autorefresh_status ||
  1531. !hw_intf->ops.check_and_reset_tearcheck) {
  1532. SDE_DEBUG_CMDENC(cmd_enc,
  1533. "autofresh disable seq2 not supported\n");
  1534. return;
  1535. }
  1536. /*
  1537. * If autorefresh is still enabled after sequence-1, proceed with
  1538. * below sequence-2.
  1539. * 1. Disable autorefresh config
  1540. * 2. Run in loop:
  1541. * 2.1 Poll for autorefresh to be disabled
  1542. * 2.2 Log read and write count status
  1543. * 2.3 Replace te write count with start_pos to meet trigger window
  1544. */
  1545. autorefresh_status = hw_mdp->ops.get_autorefresh_status(hw_mdp,
  1546. phys_enc->intf_idx);
  1547. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1548. autorefresh_status, SDE_EVTLOG_FUNC_CASE1);
  1549. if (!(autorefresh_status & BIT(7))) {
  1550. usleep_range(AUTOREFRESH_SEQ2_POLL_TIME,
  1551. AUTOREFRESH_SEQ2_POLL_TIME + 1);
  1552. autorefresh_status = hw_mdp->ops.get_autorefresh_status(hw_mdp,
  1553. phys_enc->intf_idx);
  1554. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1555. autorefresh_status, SDE_EVTLOG_FUNC_CASE2);
  1556. }
  1557. while (autorefresh_status & BIT(7)) {
  1558. if (!trial) {
  1559. pr_err("enc:%d autofresh status:0x%x intf:%d\n", DRMID(phys_enc->parent),
  1560. autorefresh_status, phys_enc->intf_idx - INTF_0);
  1561. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, 0);
  1562. }
  1563. usleep_range(AUTOREFRESH_SEQ2_POLL_TIME,
  1564. AUTOREFRESH_SEQ2_POLL_TIME + 1);
  1565. if ((trial * AUTOREFRESH_SEQ2_POLL_TIME)
  1566. > AUTOREFRESH_SEQ2_POLL_TIMEOUT) {
  1567. SDE_ERROR_CMDENC(cmd_enc,
  1568. "disable autorefresh failed\n");
  1569. SDE_DBG_DUMP(SDE_DBG_BUILT_IN_ALL, "panic");
  1570. break;
  1571. }
  1572. trial++;
  1573. autorefresh_status = hw_mdp->ops.get_autorefresh_status(hw_mdp,
  1574. phys_enc->intf_idx);
  1575. hw_intf->ops.check_and_reset_tearcheck(hw_intf, &tear_status);
  1576. pr_err("enc:%d autofresh status:0x%x intf:%d\n",
  1577. DRMID(phys_enc->parent), autorefresh_status,
  1578. phys_enc->intf_idx - INTF_0);
  1579. pr_err("tear_read_frame_count:%d tear_read_line_count:%d\n",
  1580. tear_status.read_frame_count, tear_status.read_line_count);
  1581. pr_err("tear_write_frame_count:%d tear_write_line_count:%d\n",
  1582. tear_status.write_frame_count, tear_status.write_line_count);
  1583. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0, autorefresh_status,
  1584. tear_status.read_frame_count, tear_status.read_line_count,
  1585. tear_status.write_frame_count, tear_status.write_line_count);
  1586. }
  1587. }
  1588. static void _sde_encoder_phys_disable_autorefresh(struct sde_encoder_phys *phys_enc)
  1589. {
  1590. struct sde_encoder_phys_cmd *cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1591. struct sde_kms *sde_kms;
  1592. if (!phys_enc || !sde_encoder_phys_cmd_is_master(phys_enc))
  1593. return;
  1594. if (!sde_encoder_phys_cmd_is_autorefresh_enabled(phys_enc))
  1595. return;
  1596. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1597. cmd_enc->autorefresh.cfg.enable);
  1598. sde_kms = phys_enc->sde_kms;
  1599. sde_encoder_phys_cmd_connect_te(phys_enc, false);
  1600. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, 0);
  1601. phys_enc->autorefresh_disable_trans = true;
  1602. if (sde_kms && sde_kms->catalog &&
  1603. (sde_kms->catalog->autorefresh_disable_seq == AUTOREFRESH_DISABLE_SEQ1)) {
  1604. _sde_encoder_autorefresh_disable_seq1(phys_enc);
  1605. _sde_encoder_autorefresh_disable_seq2(phys_enc);
  1606. }
  1607. sde_encoder_phys_cmd_connect_te(phys_enc, true);
  1608. SDE_DEBUG_CMDENC(cmd_enc, "autorefresh disabled successfully\n");
  1609. }
  1610. static void sde_encoder_phys_cmd_prepare_commit(struct sde_encoder_phys *phys_enc)
  1611. {
  1612. return _sde_encoder_phys_disable_autorefresh(phys_enc);
  1613. }
  1614. static void sde_encoder_phys_cmd_trigger_start(
  1615. struct sde_encoder_phys *phys_enc)
  1616. {
  1617. struct sde_encoder_phys_cmd *cmd_enc =
  1618. to_sde_encoder_phys_cmd(phys_enc);
  1619. u32 frame_cnt;
  1620. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  1621. if (!phys_enc)
  1622. return;
  1623. /* we don't issue CTL_START when using autorefresh */
  1624. frame_cnt = _sde_encoder_phys_cmd_get_autorefresh_property(phys_enc);
  1625. if (frame_cnt) {
  1626. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, frame_cnt);
  1627. atomic_inc(&cmd_enc->autorefresh.kickoff_cnt);
  1628. } else {
  1629. sde_encoder_helper_trigger_start(phys_enc);
  1630. }
  1631. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  1632. SDE_EVT32(DRMID(phys_enc->parent), frame_cnt, info[0].pp_idx, info[0].intf_idx,
  1633. info[0].intf_frame_count, info[0].wr_ptr_line_count, info[0].rd_ptr_line_count,
  1634. info[1].pp_idx, info[1].intf_idx, info[1].intf_frame_count,
  1635. info[1].wr_ptr_line_count, info[1].rd_ptr_line_count);
  1636. /* wr_ptr_wait_success is set true when wr_ptr arrives */
  1637. cmd_enc->wr_ptr_wait_success = false;
  1638. }
  1639. static void _sde_encoder_phys_cmd_calculate_wd_params(struct sde_encoder_phys *phys_enc,
  1640. struct intf_wd_jitter_params *wd_jitter)
  1641. {
  1642. u32 nominal_te_value;
  1643. struct sde_encoder_virt *sde_enc;
  1644. struct msm_mode_info *mode_info;
  1645. const u32 multiplier = 1 << 10;
  1646. sde_enc = to_sde_encoder_virt(phys_enc->parent);
  1647. mode_info = &sde_enc->mode_info;
  1648. if (mode_info->wd_jitter.jitter_type & MSM_DISPLAY_WD_INSTANTANEOUS_JITTER)
  1649. wd_jitter->jitter = mult_frac(multiplier, mode_info->wd_jitter.inst_jitter_numer,
  1650. (mode_info->wd_jitter.inst_jitter_denom * 100));
  1651. if (mode_info->wd_jitter.jitter_type & MSM_DISPLAY_WD_LTJ_JITTER) {
  1652. nominal_te_value = CALCULATE_WD_LOAD_VALUE(mode_info->frame_rate) * MDP_TICK_COUNT;
  1653. wd_jitter->ltj_max = mult_frac(nominal_te_value, mode_info->wd_jitter.ltj_max_numer,
  1654. (mode_info->wd_jitter.ltj_max_denom) * 100);
  1655. wd_jitter->ltj_slope = mult_frac((1 << 16), wd_jitter->ltj_max,
  1656. (mode_info->wd_jitter.ltj_time_sec * mode_info->frame_rate));
  1657. }
  1658. phys_enc->hw_intf->ops.configure_wd_jitter(phys_enc->hw_intf, wd_jitter);
  1659. }
  1660. static void sde_encoder_phys_cmd_setup_vsync_source(struct sde_encoder_phys *phys_enc,
  1661. u32 vsync_source, struct msm_display_info *disp_info)
  1662. {
  1663. struct sde_encoder_virt *sde_enc;
  1664. struct sde_connector *sde_conn;
  1665. struct intf_wd_jitter_params wd_jitter = {0, 0};
  1666. if (!phys_enc || !phys_enc->hw_intf)
  1667. return;
  1668. sde_enc = to_sde_encoder_virt(phys_enc->parent);
  1669. if (!sde_enc)
  1670. return;
  1671. sde_conn = to_sde_connector(phys_enc->connector);
  1672. if ((disp_info->is_te_using_watchdog_timer || sde_conn->panel_dead) &&
  1673. phys_enc->hw_intf->ops.setup_vsync_source) {
  1674. vsync_source = SDE_VSYNC_SOURCE_WD_TIMER_0;
  1675. if (phys_enc->hw_intf->ops.configure_wd_jitter)
  1676. _sde_encoder_phys_cmd_calculate_wd_params(phys_enc, &wd_jitter);
  1677. phys_enc->hw_intf->ops.setup_vsync_source(phys_enc->hw_intf,
  1678. sde_enc->mode_info.frame_rate);
  1679. } else {
  1680. sde_encoder_helper_vsync_config(phys_enc, vsync_source);
  1681. }
  1682. if (phys_enc->has_intf_te && phys_enc->hw_intf->ops.vsync_sel)
  1683. phys_enc->hw_intf->ops.vsync_sel(phys_enc->hw_intf,
  1684. vsync_source);
  1685. }
  1686. void sde_encoder_phys_cmd_add_enc_to_minidump(struct sde_encoder_phys *phys_enc)
  1687. {
  1688. struct sde_encoder_phys_cmd *cmd_enc;
  1689. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1690. sde_mini_dump_add_va_region("sde_enc_phys_cmd", sizeof(*cmd_enc), cmd_enc);
  1691. }
  1692. static void sde_encoder_phys_cmd_init_ops(struct sde_encoder_phys_ops *ops)
  1693. {
  1694. ops->prepare_commit = sde_encoder_phys_cmd_prepare_commit;
  1695. ops->is_master = sde_encoder_phys_cmd_is_master;
  1696. ops->mode_set = sde_encoder_phys_cmd_mode_set;
  1697. ops->cont_splash_mode_set = sde_encoder_phys_cmd_cont_splash_mode_set;
  1698. ops->mode_fixup = sde_encoder_phys_cmd_mode_fixup;
  1699. ops->enable = sde_encoder_phys_cmd_enable;
  1700. ops->disable = sde_encoder_phys_cmd_disable;
  1701. ops->destroy = sde_encoder_phys_cmd_destroy;
  1702. ops->get_hw_resources = sde_encoder_phys_cmd_get_hw_resources;
  1703. ops->control_vblank_irq = sde_encoder_phys_cmd_control_vblank_irq;
  1704. ops->wait_for_commit_done = sde_encoder_phys_cmd_wait_for_commit_done;
  1705. ops->prepare_for_kickoff = sde_encoder_phys_cmd_prepare_for_kickoff;
  1706. ops->wait_for_tx_complete = sde_encoder_phys_cmd_wait_for_tx_complete;
  1707. ops->wait_for_vblank = sde_encoder_phys_cmd_wait_for_vblank;
  1708. ops->trigger_flush = sde_encoder_helper_trigger_flush;
  1709. ops->trigger_start = sde_encoder_phys_cmd_trigger_start;
  1710. ops->needs_single_flush = sde_encoder_phys_needs_single_flush;
  1711. ops->hw_reset = sde_encoder_helper_hw_reset;
  1712. ops->irq_control = sde_encoder_phys_cmd_irq_control;
  1713. ops->update_split_role = sde_encoder_phys_cmd_update_split_role;
  1714. ops->restore = sde_encoder_phys_cmd_enable_helper;
  1715. ops->control_te = sde_encoder_phys_cmd_connect_te;
  1716. ops->is_autorefresh_enabled =
  1717. sde_encoder_phys_cmd_is_autorefresh_enabled;
  1718. ops->get_line_count = sde_encoder_phys_cmd_te_get_line_count;
  1719. ops->wait_for_active = NULL;
  1720. ops->setup_vsync_source = sde_encoder_phys_cmd_setup_vsync_source;
  1721. ops->setup_misr = sde_encoder_helper_setup_misr;
  1722. ops->collect_misr = sde_encoder_helper_collect_misr;
  1723. ops->add_to_minidump = sde_encoder_phys_cmd_add_enc_to_minidump;
  1724. ops->disable_autorefresh = _sde_encoder_phys_disable_autorefresh;
  1725. }
  1726. static inline bool sde_encoder_phys_cmd_intf_te_supported(
  1727. const struct sde_mdss_cfg *sde_cfg, enum sde_intf idx)
  1728. {
  1729. if (sde_cfg && ((idx - INTF_0) < sde_cfg->intf_count))
  1730. return test_bit(SDE_INTF_TE,
  1731. &(sde_cfg->intf[idx - INTF_0].features));
  1732. return false;
  1733. }
  1734. struct sde_encoder_phys *sde_encoder_phys_cmd_init(
  1735. struct sde_enc_phys_init_params *p)
  1736. {
  1737. struct sde_encoder_phys *phys_enc = NULL;
  1738. struct sde_encoder_phys_cmd *cmd_enc = NULL;
  1739. struct sde_hw_mdp *hw_mdp;
  1740. struct sde_encoder_irq *irq;
  1741. int i, ret = 0;
  1742. SDE_DEBUG("intf %d\n", p->intf_idx - INTF_0);
  1743. cmd_enc = kzalloc(sizeof(*cmd_enc), GFP_KERNEL);
  1744. if (!cmd_enc) {
  1745. ret = -ENOMEM;
  1746. SDE_ERROR("failed to allocate\n");
  1747. goto fail;
  1748. }
  1749. phys_enc = &cmd_enc->base;
  1750. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  1751. if (IS_ERR_OR_NULL(hw_mdp)) {
  1752. ret = PTR_ERR(hw_mdp);
  1753. SDE_ERROR("failed to get mdptop\n");
  1754. goto fail_mdp_init;
  1755. }
  1756. phys_enc->hw_mdptop = hw_mdp;
  1757. phys_enc->intf_idx = p->intf_idx;
  1758. phys_enc->parent = p->parent;
  1759. phys_enc->parent_ops = p->parent_ops;
  1760. phys_enc->sde_kms = p->sde_kms;
  1761. phys_enc->split_role = p->split_role;
  1762. phys_enc->intf_mode = INTF_MODE_CMD;
  1763. phys_enc->enc_spinlock = p->enc_spinlock;
  1764. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  1765. cmd_enc->stream_sel = 0;
  1766. phys_enc->enable_state = SDE_ENC_DISABLED;
  1767. phys_enc->kickoff_timeout_ms = DEFAULT_KICKOFF_TIMEOUT_MS;
  1768. sde_encoder_phys_cmd_init_ops(&phys_enc->ops);
  1769. phys_enc->comp_type = p->comp_type;
  1770. phys_enc->has_intf_te = sde_encoder_phys_cmd_intf_te_supported(
  1771. phys_enc->sde_kms->catalog, phys_enc->intf_idx);
  1772. for (i = 0; i < INTR_IDX_MAX; i++) {
  1773. irq = &phys_enc->irq[i];
  1774. INIT_LIST_HEAD(&irq->cb.list);
  1775. irq->irq_idx = -EINVAL;
  1776. irq->hw_idx = -EINVAL;
  1777. irq->cb.arg = phys_enc;
  1778. }
  1779. irq = &phys_enc->irq[INTR_IDX_CTL_START];
  1780. irq->name = "ctl_start";
  1781. irq->intr_type = SDE_IRQ_TYPE_CTL_START;
  1782. irq->intr_idx = INTR_IDX_CTL_START;
  1783. irq->cb.func = NULL;
  1784. irq = &phys_enc->irq[INTR_IDX_CTL_DONE];
  1785. irq->name = "ctl_done";
  1786. irq->intr_type = SDE_IRQ_TYPE_CTL_DONE;
  1787. irq->intr_idx = INTR_IDX_CTL_DONE;
  1788. irq->cb.func = sde_encoder_phys_cmd_ctl_done_irq;
  1789. irq = &phys_enc->irq[INTR_IDX_PINGPONG];
  1790. irq->name = "pp_done";
  1791. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_COMP;
  1792. irq->intr_idx = INTR_IDX_PINGPONG;
  1793. irq->cb.func = sde_encoder_phys_cmd_pp_tx_done_irq;
  1794. irq = &phys_enc->irq[INTR_IDX_RDPTR];
  1795. irq->intr_idx = INTR_IDX_RDPTR;
  1796. irq->name = "te_rd_ptr";
  1797. if (phys_enc->has_intf_te)
  1798. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_RD_PTR;
  1799. else
  1800. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_RD_PTR;
  1801. irq->cb.func = sde_encoder_phys_cmd_te_rd_ptr_irq;
  1802. irq = &phys_enc->irq[INTR_IDX_AUTOREFRESH_DONE];
  1803. irq->name = "autorefresh_done";
  1804. if (phys_enc->has_intf_te)
  1805. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_AUTO_REF;
  1806. else
  1807. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_AUTO_REF;
  1808. irq->intr_idx = INTR_IDX_AUTOREFRESH_DONE;
  1809. irq->cb.func = sde_encoder_phys_cmd_autorefresh_done_irq;
  1810. irq = &phys_enc->irq[INTR_IDX_WRPTR];
  1811. irq->intr_idx = INTR_IDX_WRPTR;
  1812. irq->name = "wr_ptr";
  1813. if (phys_enc->has_intf_te)
  1814. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_WR_PTR;
  1815. else
  1816. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_WR_PTR;
  1817. irq->cb.func = sde_encoder_phys_cmd_wr_ptr_irq;
  1818. atomic_set(&phys_enc->vblank_refcount, 0);
  1819. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1820. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  1821. atomic_set(&cmd_enc->pending_vblank_cnt, 0);
  1822. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  1823. init_waitqueue_head(&cmd_enc->pending_vblank_wq);
  1824. atomic_set(&cmd_enc->autorefresh.kickoff_cnt, 0);
  1825. init_waitqueue_head(&cmd_enc->autorefresh.kickoff_wq);
  1826. INIT_LIST_HEAD(&cmd_enc->te_timestamp_list);
  1827. for (i = 0; i < MAX_TE_PROFILE_COUNT; i++)
  1828. list_add(&cmd_enc->te_timestamp[i].list,
  1829. &cmd_enc->te_timestamp_list);
  1830. SDE_DEBUG_CMDENC(cmd_enc, "created\n");
  1831. return phys_enc;
  1832. fail_mdp_init:
  1833. kfree(cmd_enc);
  1834. fail:
  1835. return ERR_PTR(ret);
  1836. }