sde_encoder_phys_cmd.c 62 KB

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