sde_encoder_phys_cmd.c 84 KB

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