sde_encoder_phys_cmd.c 80 KB

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