sde_encoder_phys_cmd.c 82 KB

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