sde_encoder_phys_cmd.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716
  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 && !test_bit(SDE_INTF_TE_LEVEL_TRIGGER,
  1311. &phys_enc->hw_intf->cap->features))
  1312. sde_enc->restore_te_rd_ptr = true;
  1313. }
  1314. /*
  1315. * For pp-split, skip setting the flush bit for the slave intf, since
  1316. * both intfs use same ctl and HW will only flush the master.
  1317. */
  1318. if (_sde_encoder_phys_is_ppsplit(phys_enc) &&
  1319. !sde_encoder_phys_cmd_is_master(phys_enc))
  1320. goto skip_flush;
  1321. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  1322. skip_flush:
  1323. return;
  1324. }
  1325. static void sde_encoder_phys_cmd_enable(struct sde_encoder_phys *phys_enc)
  1326. {
  1327. struct sde_encoder_phys_cmd *cmd_enc =
  1328. to_sde_encoder_phys_cmd(phys_enc);
  1329. if (!phys_enc || !phys_enc->hw_pp) {
  1330. SDE_ERROR("invalid phys encoder\n");
  1331. return;
  1332. }
  1333. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  1334. if (phys_enc->enable_state == SDE_ENC_ENABLED) {
  1335. if (!phys_enc->cont_splash_enabled)
  1336. SDE_ERROR("already enabled\n");
  1337. return;
  1338. }
  1339. sde_encoder_phys_cmd_enable_helper(phys_enc);
  1340. phys_enc->enable_state = SDE_ENC_ENABLED;
  1341. }
  1342. static bool sde_encoder_phys_cmd_is_autorefresh_enabled(
  1343. struct sde_encoder_phys *phys_enc)
  1344. {
  1345. struct sde_hw_pingpong *hw_pp;
  1346. struct sde_hw_intf *hw_intf;
  1347. struct sde_hw_autorefresh cfg;
  1348. int ret;
  1349. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1350. return false;
  1351. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1352. return false;
  1353. if (phys_enc->has_intf_te) {
  1354. hw_intf = phys_enc->hw_intf;
  1355. if (!hw_intf->ops.get_autorefresh)
  1356. return false;
  1357. ret = hw_intf->ops.get_autorefresh(hw_intf, &cfg);
  1358. } else {
  1359. hw_pp = phys_enc->hw_pp;
  1360. if (!hw_pp->ops.get_autorefresh)
  1361. return false;
  1362. ret = hw_pp->ops.get_autorefresh(hw_pp, &cfg);
  1363. }
  1364. return ret ? false : cfg.enable;
  1365. }
  1366. static void sde_encoder_phys_cmd_connect_te(
  1367. struct sde_encoder_phys *phys_enc, bool enable)
  1368. {
  1369. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1370. return;
  1371. if (phys_enc->has_intf_te &&
  1372. phys_enc->hw_intf->ops.connect_external_te)
  1373. phys_enc->hw_intf->ops.connect_external_te(phys_enc->hw_intf,
  1374. enable);
  1375. else if (phys_enc->hw_pp->ops.connect_external_te)
  1376. phys_enc->hw_pp->ops.connect_external_te(phys_enc->hw_pp,
  1377. enable);
  1378. else
  1379. return;
  1380. SDE_EVT32(DRMID(phys_enc->parent), enable);
  1381. }
  1382. static int sde_encoder_phys_cmd_te_get_line_count(
  1383. struct sde_encoder_phys *phys_enc)
  1384. {
  1385. struct sde_hw_pingpong *hw_pp;
  1386. struct sde_hw_intf *hw_intf;
  1387. u32 line_count;
  1388. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1389. return -EINVAL;
  1390. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1391. return -EINVAL;
  1392. if (phys_enc->has_intf_te) {
  1393. hw_intf = phys_enc->hw_intf;
  1394. if (!hw_intf->ops.get_line_count)
  1395. return -EINVAL;
  1396. line_count = hw_intf->ops.get_line_count(hw_intf);
  1397. } else {
  1398. hw_pp = phys_enc->hw_pp;
  1399. if (!hw_pp->ops.get_line_count)
  1400. return -EINVAL;
  1401. line_count = hw_pp->ops.get_line_count(hw_pp);
  1402. }
  1403. return line_count;
  1404. }
  1405. static void sde_encoder_phys_cmd_disable(struct sde_encoder_phys *phys_enc)
  1406. {
  1407. struct sde_encoder_phys_cmd *cmd_enc =
  1408. to_sde_encoder_phys_cmd(phys_enc);
  1409. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf) {
  1410. SDE_ERROR("invalid encoder\n");
  1411. return;
  1412. }
  1413. SDE_DEBUG_CMDENC(cmd_enc, "pp %d intf %d state %d\n",
  1414. phys_enc->hw_pp->idx - PINGPONG_0,
  1415. phys_enc->hw_intf->idx - INTF_0,
  1416. phys_enc->enable_state);
  1417. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1418. phys_enc->hw_intf->idx - INTF_0,
  1419. phys_enc->enable_state);
  1420. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1421. SDE_ERROR_CMDENC(cmd_enc, "already disabled\n");
  1422. return;
  1423. }
  1424. if (!sde_in_trusted_vm(phys_enc->sde_kms)) {
  1425. if (phys_enc->has_intf_te &&
  1426. phys_enc->hw_intf->ops.enable_tearcheck)
  1427. phys_enc->hw_intf->ops.enable_tearcheck(
  1428. phys_enc->hw_intf,
  1429. false);
  1430. else if (phys_enc->hw_pp->ops.enable_tearcheck)
  1431. phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp,
  1432. false);
  1433. if (sde_encoder_phys_cmd_is_master(phys_enc))
  1434. sde_encoder_helper_phys_disable(phys_enc, NULL);
  1435. if (phys_enc->hw_intf->ops.reset_counter)
  1436. phys_enc->hw_intf->ops.reset_counter(phys_enc->hw_intf);
  1437. }
  1438. memset(&cmd_enc->autorefresh.cfg, 0, sizeof(struct sde_hw_autorefresh));
  1439. phys_enc->enable_state = SDE_ENC_DISABLED;
  1440. }
  1441. static void sde_encoder_phys_cmd_destroy(struct sde_encoder_phys *phys_enc)
  1442. {
  1443. struct sde_encoder_phys_cmd *cmd_enc =
  1444. to_sde_encoder_phys_cmd(phys_enc);
  1445. if (!phys_enc) {
  1446. SDE_ERROR("invalid encoder\n");
  1447. return;
  1448. }
  1449. kfree(cmd_enc);
  1450. }
  1451. static void sde_encoder_phys_cmd_get_hw_resources(
  1452. struct sde_encoder_phys *phys_enc,
  1453. struct sde_encoder_hw_resources *hw_res,
  1454. struct drm_connector_state *conn_state)
  1455. {
  1456. struct sde_encoder_phys_cmd *cmd_enc =
  1457. to_sde_encoder_phys_cmd(phys_enc);
  1458. if (!phys_enc) {
  1459. SDE_ERROR("invalid encoder\n");
  1460. return;
  1461. }
  1462. if ((phys_enc->intf_idx - INTF_0) >= INTF_MAX) {
  1463. SDE_ERROR("invalid intf idx:%d\n", phys_enc->intf_idx);
  1464. return;
  1465. }
  1466. SDE_DEBUG_CMDENC(cmd_enc, "\n");
  1467. hw_res->intfs[phys_enc->intf_idx - INTF_0] = INTF_MODE_CMD;
  1468. }
  1469. static int sde_encoder_phys_cmd_prepare_for_kickoff(
  1470. struct sde_encoder_phys *phys_enc,
  1471. struct sde_encoder_kickoff_params *params)
  1472. {
  1473. struct sde_hw_tear_check tc_cfg = {0};
  1474. struct sde_encoder_phys_cmd *cmd_enc =
  1475. to_sde_encoder_phys_cmd(phys_enc);
  1476. struct sde_encoder_virt *sde_enc;
  1477. int ret = 0;
  1478. bool recovery_events;
  1479. u32 qsync_mode = 0;
  1480. bool panel_dead = false;
  1481. if (!phys_enc || !phys_enc->hw_pp) {
  1482. SDE_ERROR("invalid encoder\n");
  1483. return -EINVAL;
  1484. }
  1485. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  1486. sde_enc = to_sde_encoder_virt(phys_enc->parent);
  1487. phys_enc->frame_trigger_mode = params->frame_trigger_mode;
  1488. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1489. atomic_read(&phys_enc->pending_kickoff_cnt),
  1490. atomic_read(&cmd_enc->autorefresh.kickoff_cnt),
  1491. phys_enc->frame_trigger_mode, SDE_EVTLOG_FUNC_CASE1);
  1492. if (phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_DEFAULT) {
  1493. /*
  1494. * Mark kickoff request as outstanding. If there are more
  1495. * than one outstanding frame, then we have to wait for the
  1496. * previous frame to complete
  1497. */
  1498. ret = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1499. if (ret) {
  1500. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1501. SDE_EVT32(DRMID(phys_enc->parent),
  1502. phys_enc->hw_pp->idx - PINGPONG_0, SDE_EVTLOG_FUNC_CASE2);
  1503. SDE_ERROR("failed wait_for_idle: %d\n", ret);
  1504. }
  1505. }
  1506. if (phys_enc->recovered) {
  1507. recovery_events = sde_encoder_recovery_events_enabled(
  1508. phys_enc->parent);
  1509. if (cmd_enc->frame_tx_timeout_report_cnt && recovery_events)
  1510. sde_connector_event_notify(phys_enc->connector,
  1511. DRM_EVENT_SDE_HW_RECOVERY,
  1512. sizeof(uint8_t),
  1513. SDE_RECOVERY_SUCCESS);
  1514. cmd_enc->frame_tx_timeout_report_cnt = 0;
  1515. phys_enc->recovered = false;
  1516. }
  1517. if (sde_connector_is_qsync_updated(phys_enc->connector)) {
  1518. u32 threshold, cfg_height, start_pos;
  1519. _get_tearcheck_cfg(phys_enc, &threshold, &cfg_height, &start_pos);
  1520. tc_cfg.sync_threshold_start = threshold;
  1521. tc_cfg.start_pos = start_pos;
  1522. cmd_enc->qsync_threshold_lines = tc_cfg.sync_threshold_start;
  1523. if (phys_enc->has_intf_te &&
  1524. phys_enc->hw_intf->ops.update_tearcheck)
  1525. phys_enc->hw_intf->ops.update_tearcheck(
  1526. phys_enc->hw_intf, &tc_cfg);
  1527. else if (phys_enc->hw_pp->ops.update_tearcheck)
  1528. phys_enc->hw_pp->ops.update_tearcheck(
  1529. phys_enc->hw_pp, &tc_cfg);
  1530. qsync_mode = sde_connector_get_qsync_mode(phys_enc->connector);
  1531. panel_dead = sde_connector_panel_dead(phys_enc->connector);
  1532. if (cmd_enc->base.hw_intf->ops.enable_te_level_trigger &&
  1533. !sde_enc->disp_info.is_te_using_watchdog_timer)
  1534. cmd_enc->base.hw_intf->ops.enable_te_level_trigger(cmd_enc->base.hw_intf,
  1535. qsync_mode && !panel_dead);
  1536. SDE_EVT32(DRMID(phys_enc->parent), tc_cfg.sync_threshold_start, tc_cfg.start_pos,
  1537. qsync_mode, sde_enc->disp_info.is_te_using_watchdog_timer,
  1538. panel_dead, SDE_EVTLOG_FUNC_CASE3);
  1539. }
  1540. if (sde_enc->restore_te_rd_ptr) {
  1541. sde_encoder_restore_tearcheck_rd_ptr(phys_enc);
  1542. sde_enc->restore_te_rd_ptr = false;
  1543. }
  1544. SDE_DEBUG_CMDENC(cmd_enc, "pp:%d pending_cnt %d\n",
  1545. phys_enc->hw_pp->idx - PINGPONG_0,
  1546. atomic_read(&phys_enc->pending_kickoff_cnt));
  1547. return ret;
  1548. }
  1549. static bool _sde_encoder_phys_cmd_needs_vsync_change(
  1550. struct sde_encoder_phys *phys_enc, ktime_t profile_timestamp)
  1551. {
  1552. struct sde_encoder_virt *sde_enc;
  1553. struct sde_encoder_phys_cmd *cmd_enc;
  1554. struct sde_encoder_phys_cmd_te_timestamp *cur;
  1555. struct sde_encoder_phys_cmd_te_timestamp *prev = NULL;
  1556. ktime_t time_diff;
  1557. struct msm_mode_info *info;
  1558. ktime_t l_bound = 0, u_bound = 0;
  1559. bool ret = false;
  1560. unsigned long lock_flags;
  1561. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1562. sde_enc = to_sde_encoder_virt(phys_enc->parent);
  1563. info = &sde_enc->mode_info;
  1564. sde_encoder_helper_get_jitter_bounds_ns(info->frame_rate, info->jitter_numer,
  1565. info->jitter_denom, &l_bound, &u_bound);
  1566. if (!l_bound || !u_bound) {
  1567. SDE_ERROR_CMDENC(cmd_enc, "invalid vsync jitter bounds\n");
  1568. return false;
  1569. }
  1570. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  1571. list_for_each_entry_reverse(cur, &cmd_enc->te_timestamp_list, list) {
  1572. if (prev && ktime_after(cur->timestamp, profile_timestamp)) {
  1573. time_diff = ktime_sub(prev->timestamp, cur->timestamp);
  1574. if ((time_diff < l_bound) || (time_diff > u_bound)) {
  1575. ret = true;
  1576. break;
  1577. }
  1578. }
  1579. prev = cur;
  1580. }
  1581. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  1582. if (ret) {
  1583. SDE_DEBUG_CMDENC(cmd_enc,
  1584. "time_diff:%llu, prev:%llu, cur:%llu, jitter:%llu/%llu\n",
  1585. time_diff, prev->timestamp, cur->timestamp,
  1586. l_bound, u_bound);
  1587. time_diff = div_s64(time_diff, 1000);
  1588. SDE_EVT32(DRMID(phys_enc->parent),
  1589. (u32) (do_div(l_bound, 1000)),
  1590. (u32) (do_div(u_bound, 1000)),
  1591. (u32) (time_diff), SDE_EVTLOG_ERROR);
  1592. }
  1593. return ret;
  1594. }
  1595. static int _sde_encoder_phys_cmd_wait_for_wr_ptr(
  1596. struct sde_encoder_phys *phys_enc)
  1597. {
  1598. struct sde_encoder_phys_cmd *cmd_enc =
  1599. to_sde_encoder_phys_cmd(phys_enc);
  1600. struct sde_encoder_wait_info wait_info = {0};
  1601. struct sde_connector *c_conn;
  1602. bool frame_pending = true;
  1603. struct sde_hw_ctl *ctl;
  1604. unsigned long lock_flags;
  1605. int ret, timeout_ms;
  1606. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->connector) {
  1607. SDE_ERROR("invalid argument(s)\n");
  1608. return -EINVAL;
  1609. }
  1610. ctl = phys_enc->hw_ctl;
  1611. c_conn = to_sde_connector(phys_enc->connector);
  1612. timeout_ms = phys_enc->kickoff_timeout_ms;
  1613. if (c_conn->lp_mode == SDE_MODE_DPMS_LP1 ||
  1614. c_conn->lp_mode == SDE_MODE_DPMS_LP2)
  1615. timeout_ms = timeout_ms * 2;
  1616. wait_info.wq = &phys_enc->pending_kickoff_wq;
  1617. wait_info.atomic_cnt = &phys_enc->pending_retire_fence_cnt;
  1618. wait_info.timeout_ms = timeout_ms;
  1619. /* slave encoder doesn't enable for ppsplit */
  1620. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  1621. return 0;
  1622. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_WRPTR,
  1623. &wait_info);
  1624. /*
  1625. * if hwfencing enabled, try again to wait for up to the extended timeout time in
  1626. * increments as long as fence has not been signaled.
  1627. */
  1628. if (ret == -ETIMEDOUT && phys_enc->sde_kms->catalog->hw_fence_rev)
  1629. ret = sde_encoder_helper_hw_fence_extended_wait(phys_enc, ctl, &wait_info,
  1630. INTR_IDX_WRPTR);
  1631. if (ret == -ETIMEDOUT) {
  1632. struct sde_hw_ctl *ctl = phys_enc->hw_ctl;
  1633. if (ctl && ctl->ops.get_start_state)
  1634. frame_pending = ctl->ops.get_start_state(ctl);
  1635. ret = (frame_pending || sde_connector_esd_status(phys_enc->connector)) ? ret : 0;
  1636. /*
  1637. * There can be few cases of ESD where CTL_START is cleared but
  1638. * wr_ptr irq doesn't come. Signaling retire fence in these
  1639. * cases to avoid freeze and dangling pending_retire_fence_cnt
  1640. */
  1641. if (!ret) {
  1642. SDE_EVT32(DRMID(phys_enc->parent),
  1643. SDE_EVTLOG_FUNC_CASE1);
  1644. if (sde_encoder_phys_cmd_is_master(phys_enc) &&
  1645. atomic_add_unless(
  1646. &phys_enc->pending_retire_fence_cnt, -1, 0)) {
  1647. spin_lock_irqsave(phys_enc->enc_spinlock,
  1648. lock_flags);
  1649. phys_enc->parent_ops.handle_frame_done(
  1650. phys_enc->parent, phys_enc,
  1651. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  1652. spin_unlock_irqrestore(phys_enc->enc_spinlock,
  1653. lock_flags);
  1654. }
  1655. }
  1656. /* if we timeout after the extended wait, reset mixers and do sw override */
  1657. if (ret && phys_enc->sde_kms->catalog->hw_fence_rev)
  1658. sde_encoder_helper_hw_fence_sw_override(phys_enc, ctl);
  1659. }
  1660. cmd_enc->wr_ptr_wait_success = (ret == 0) ? true : false;
  1661. return ret;
  1662. }
  1663. static int sde_encoder_phys_cmd_wait_for_tx_complete(
  1664. struct sde_encoder_phys *phys_enc)
  1665. {
  1666. int rc;
  1667. struct sde_encoder_phys_cmd *cmd_enc;
  1668. if (!phys_enc)
  1669. return -EINVAL;
  1670. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1671. if (sde_encoder_check_ctl_done_support(phys_enc->parent)
  1672. && !sde_encoder_phys_cmd_is_master(phys_enc))
  1673. return 0;
  1674. if (!atomic_read(&phys_enc->pending_kickoff_cnt)) {
  1675. SDE_EVT32(DRMID(phys_enc->parent),
  1676. phys_enc->intf_idx - INTF_0,
  1677. phys_enc->enable_state);
  1678. return 0;
  1679. }
  1680. rc = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1681. if (rc) {
  1682. SDE_EVT32(DRMID(phys_enc->parent),
  1683. phys_enc->intf_idx - INTF_0);
  1684. SDE_ERROR("failed wait_for_idle: %d\n", rc);
  1685. }
  1686. return rc;
  1687. }
  1688. static int _sde_encoder_phys_cmd_handle_wr_ptr_timeout(
  1689. struct sde_encoder_phys *phys_enc,
  1690. ktime_t profile_timestamp)
  1691. {
  1692. struct sde_encoder_phys_cmd *cmd_enc =
  1693. to_sde_encoder_phys_cmd(phys_enc);
  1694. bool switch_te;
  1695. int ret = -ETIMEDOUT;
  1696. unsigned long lock_flags;
  1697. struct sde_encoder_virt *sde_enc;
  1698. switch_te = _sde_encoder_phys_cmd_needs_vsync_change(
  1699. phys_enc, profile_timestamp);
  1700. sde_enc = to_sde_encoder_virt(phys_enc->parent);
  1701. SDE_EVT32(DRMID(phys_enc->parent), switch_te, SDE_EVTLOG_FUNC_ENTRY);
  1702. if (sde_connector_panel_dead(phys_enc->connector)) {
  1703. if (cmd_enc->base.hw_intf->ops.enable_te_level_trigger &&
  1704. !sde_enc->disp_info.is_te_using_watchdog_timer)
  1705. cmd_enc->base.hw_intf->ops.enable_te_level_trigger(cmd_enc->base.hw_intf,
  1706. false);
  1707. ret = _sde_encoder_phys_cmd_wait_for_wr_ptr(phys_enc);
  1708. } else if (switch_te) {
  1709. SDE_DEBUG_CMDENC(cmd_enc,
  1710. "wr_ptr_irq wait failed, retry with WD TE\n");
  1711. /* switch to watchdog TE and wait again */
  1712. sde_encoder_helper_switch_vsync(phys_enc->parent, true);
  1713. ret = _sde_encoder_phys_cmd_wait_for_wr_ptr(phys_enc);
  1714. /* switch back to default TE */
  1715. sde_encoder_helper_switch_vsync(phys_enc->parent, false);
  1716. }
  1717. /*
  1718. * Signaling the retire fence at wr_ptr timeout
  1719. * to allow the next commit and avoid device freeze.
  1720. */
  1721. if (ret == -ETIMEDOUT) {
  1722. SDE_ERROR_CMDENC(cmd_enc,
  1723. "wr_ptr_irq wait failed, switch_te:%d\n", switch_te);
  1724. SDE_EVT32(DRMID(phys_enc->parent), switch_te, SDE_EVTLOG_ERROR);
  1725. if (sde_encoder_phys_cmd_is_master(phys_enc) &&
  1726. atomic_add_unless(
  1727. &phys_enc->pending_retire_fence_cnt, -1, 0)) {
  1728. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  1729. phys_enc->parent_ops.handle_frame_done(
  1730. phys_enc->parent, phys_enc,
  1731. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  1732. spin_unlock_irqrestore(phys_enc->enc_spinlock,
  1733. lock_flags);
  1734. }
  1735. }
  1736. cmd_enc->wr_ptr_wait_success = (ret == 0) ? true : false;
  1737. return ret;
  1738. }
  1739. static int sde_encoder_phys_cmd_wait_for_commit_done(
  1740. struct sde_encoder_phys *phys_enc)
  1741. {
  1742. int rc = 0, i, pending_cnt;
  1743. struct sde_encoder_phys_cmd *cmd_enc;
  1744. ktime_t profile_timestamp = ktime_get();
  1745. u32 scheduler_status = INVALID_CTL_STATUS;
  1746. struct sde_hw_ctl *ctl;
  1747. if (!phys_enc)
  1748. return -EINVAL;
  1749. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1750. if (sde_encoder_check_ctl_done_support(phys_enc->parent)
  1751. && !sde_encoder_phys_cmd_is_master(phys_enc))
  1752. return 0;
  1753. /* only required for master controller */
  1754. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  1755. rc = _sde_encoder_phys_cmd_wait_for_wr_ptr(phys_enc);
  1756. if (rc == -ETIMEDOUT) {
  1757. /*
  1758. * Profile all the TE received after profile_timestamp
  1759. * and if the jitter is more, switch to watchdog TE
  1760. * and wait for wr_ptr again. Finally move back to
  1761. * default TE.
  1762. */
  1763. rc = _sde_encoder_phys_cmd_handle_wr_ptr_timeout(
  1764. phys_enc, profile_timestamp);
  1765. if (rc == -ETIMEDOUT)
  1766. goto wait_for_idle;
  1767. }
  1768. if (cmd_enc->autorefresh.cfg.enable)
  1769. rc = _sde_encoder_phys_cmd_wait_for_autorefresh_done(
  1770. phys_enc);
  1771. ctl = phys_enc->hw_ctl;
  1772. if (ctl && ctl->ops.get_scheduler_status)
  1773. scheduler_status = ctl->ops.get_scheduler_status(ctl);
  1774. }
  1775. /* wait for posted start or serialize trigger */
  1776. pending_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  1777. if ((pending_cnt > 1) ||
  1778. (pending_cnt && (scheduler_status & BIT(0))) ||
  1779. (!rc && phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_SERIALIZE))
  1780. goto wait_for_idle;
  1781. return rc;
  1782. wait_for_idle:
  1783. pending_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  1784. for (i = 0; i < pending_cnt; i++)
  1785. rc |= sde_encoder_wait_for_event(phys_enc->parent,
  1786. MSM_ENC_TX_COMPLETE);
  1787. if (rc) {
  1788. SDE_EVT32(DRMID(phys_enc->parent),
  1789. phys_enc->hw_pp->idx - PINGPONG_0,
  1790. phys_enc->frame_trigger_mode,
  1791. atomic_read(&phys_enc->pending_kickoff_cnt),
  1792. phys_enc->enable_state,
  1793. cmd_enc->wr_ptr_wait_success, scheduler_status, rc);
  1794. SDE_ERROR("pp:%d failed wait_for_idle: %d\n",
  1795. phys_enc->hw_pp->idx - PINGPONG_0, rc);
  1796. if (phys_enc->enable_state == SDE_ENC_ERR_NEEDS_HW_RESET)
  1797. sde_encoder_needs_hw_reset(phys_enc->parent);
  1798. }
  1799. return rc;
  1800. }
  1801. static int sde_encoder_phys_cmd_wait_for_vblank(
  1802. struct sde_encoder_phys *phys_enc)
  1803. {
  1804. int rc = 0;
  1805. struct sde_encoder_phys_cmd *cmd_enc;
  1806. struct sde_encoder_wait_info wait_info = {0};
  1807. if (!phys_enc)
  1808. return -EINVAL;
  1809. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1810. /* only required for master controller */
  1811. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1812. return rc;
  1813. wait_info.wq = &cmd_enc->pending_vblank_wq;
  1814. wait_info.atomic_cnt = &cmd_enc->pending_vblank_cnt;
  1815. wait_info.timeout_ms = _sde_encoder_phys_cmd_get_idle_timeout(phys_enc);
  1816. atomic_inc(&cmd_enc->pending_vblank_cnt);
  1817. rc = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_RDPTR,
  1818. &wait_info);
  1819. return rc;
  1820. }
  1821. static void sde_encoder_phys_cmd_update_split_role(
  1822. struct sde_encoder_phys *phys_enc,
  1823. enum sde_enc_split_role role)
  1824. {
  1825. struct sde_encoder_phys_cmd *cmd_enc;
  1826. enum sde_enc_split_role old_role;
  1827. bool is_ppsplit;
  1828. if (!phys_enc)
  1829. return;
  1830. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1831. old_role = phys_enc->split_role;
  1832. is_ppsplit = _sde_encoder_phys_is_ppsplit(phys_enc);
  1833. phys_enc->split_role = role;
  1834. SDE_DEBUG_CMDENC(cmd_enc, "old role %d new role %d\n",
  1835. old_role, role);
  1836. /*
  1837. * ppsplit solo needs to reprogram because intf may have swapped without
  1838. * role changing on left-only, right-only back-to-back commits
  1839. */
  1840. if (!(is_ppsplit && role == ENC_ROLE_SOLO) &&
  1841. (role == old_role || role == ENC_ROLE_SKIP))
  1842. return;
  1843. sde_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
  1844. _sde_encoder_phys_cmd_pingpong_config(phys_enc);
  1845. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  1846. }
  1847. static void _sde_encoder_autorefresh_disable_seq1(
  1848. struct sde_encoder_phys *phys_enc)
  1849. {
  1850. int trial = 0;
  1851. u32 timeout_ms = phys_enc->kickoff_timeout_ms;
  1852. struct sde_encoder_phys_cmd *cmd_enc =
  1853. to_sde_encoder_phys_cmd(phys_enc);
  1854. /*
  1855. * If autorefresh is enabled, disable it and make sure it is safe to
  1856. * proceed with current frame commit/push. Sequence fallowed is,
  1857. * 1. Disable TE & autorefresh - caller will take care of it
  1858. * 2. Poll for frame transfer ongoing to be false
  1859. * 3. Enable TE back - caller will take care of it
  1860. */
  1861. do {
  1862. udelay(AUTOREFRESH_SEQ1_POLL_TIME);
  1863. if ((trial * AUTOREFRESH_SEQ1_POLL_TIME)
  1864. > (timeout_ms * USEC_PER_MSEC)) {
  1865. SDE_ERROR_CMDENC(cmd_enc,
  1866. "disable autorefresh failed\n");
  1867. phys_enc->enable_state = SDE_ENC_ERR_NEEDS_HW_RESET;
  1868. break;
  1869. }
  1870. trial++;
  1871. } while (_sde_encoder_phys_cmd_is_ongoing_pptx(phys_enc));
  1872. }
  1873. static void _sde_encoder_autorefresh_disable_seq2(
  1874. struct sde_encoder_phys *phys_enc)
  1875. {
  1876. int trial = 0;
  1877. struct sde_hw_mdp *hw_mdp = phys_enc->hw_mdptop;
  1878. u32 autorefresh_status = 0;
  1879. struct sde_encoder_phys_cmd *cmd_enc =
  1880. to_sde_encoder_phys_cmd(phys_enc);
  1881. struct intf_tear_status tear_status;
  1882. struct sde_hw_intf *hw_intf = phys_enc->hw_intf;
  1883. if (!hw_mdp->ops.get_autorefresh_status ||
  1884. !hw_intf->ops.check_and_reset_tearcheck) {
  1885. SDE_DEBUG_CMDENC(cmd_enc,
  1886. "autofresh disable seq2 not supported\n");
  1887. return;
  1888. }
  1889. /*
  1890. * If autorefresh is still enabled after sequence-1, proceed with
  1891. * below sequence-2.
  1892. * 1. Disable autorefresh config
  1893. * 2. Run in loop:
  1894. * 2.1 Poll for autorefresh to be disabled
  1895. * 2.2 Log read and write count status
  1896. * 2.3 Replace te write count with start_pos to meet trigger window
  1897. */
  1898. autorefresh_status = hw_mdp->ops.get_autorefresh_status(hw_mdp,
  1899. phys_enc->intf_idx);
  1900. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1901. autorefresh_status, SDE_EVTLOG_FUNC_CASE1);
  1902. if (!(autorefresh_status & BIT(7))) {
  1903. usleep_range(AUTOREFRESH_SEQ2_POLL_TIME,
  1904. AUTOREFRESH_SEQ2_POLL_TIME + 1);
  1905. autorefresh_status = hw_mdp->ops.get_autorefresh_status(hw_mdp,
  1906. phys_enc->intf_idx);
  1907. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1908. autorefresh_status, SDE_EVTLOG_FUNC_CASE2);
  1909. }
  1910. while (autorefresh_status & BIT(7)) {
  1911. if (!trial) {
  1912. pr_err("enc:%d autofresh status:0x%x intf:%d\n", DRMID(phys_enc->parent),
  1913. autorefresh_status, phys_enc->intf_idx - INTF_0);
  1914. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, 0);
  1915. }
  1916. usleep_range(AUTOREFRESH_SEQ2_POLL_TIME,
  1917. AUTOREFRESH_SEQ2_POLL_TIME + 1);
  1918. if ((trial * AUTOREFRESH_SEQ2_POLL_TIME)
  1919. > AUTOREFRESH_SEQ2_POLL_TIMEOUT) {
  1920. SDE_ERROR_CMDENC(cmd_enc,
  1921. "disable autorefresh failed\n");
  1922. SDE_DBG_DUMP(SDE_DBG_BUILT_IN_ALL, "panic");
  1923. break;
  1924. }
  1925. trial++;
  1926. autorefresh_status = hw_mdp->ops.get_autorefresh_status(hw_mdp,
  1927. phys_enc->intf_idx);
  1928. hw_intf->ops.check_and_reset_tearcheck(hw_intf, &tear_status);
  1929. pr_err("enc:%d autofresh status:0x%x intf:%d\n",
  1930. DRMID(phys_enc->parent), autorefresh_status,
  1931. phys_enc->intf_idx - INTF_0);
  1932. pr_err("tear_read_frame_count:%d tear_read_line_count:%d\n",
  1933. tear_status.read_frame_count, tear_status.read_line_count);
  1934. pr_err("tear_write_frame_count:%d tear_write_line_count:%d\n",
  1935. tear_status.write_frame_count, tear_status.write_line_count);
  1936. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0, autorefresh_status,
  1937. tear_status.read_frame_count, tear_status.read_line_count,
  1938. tear_status.write_frame_count, tear_status.write_line_count);
  1939. }
  1940. }
  1941. static void _sde_encoder_phys_disable_autorefresh(struct sde_encoder_phys *phys_enc)
  1942. {
  1943. struct sde_encoder_phys_cmd *cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1944. struct sde_kms *sde_kms;
  1945. if (!phys_enc || !sde_encoder_phys_cmd_is_master(phys_enc))
  1946. return;
  1947. if (!sde_encoder_phys_cmd_is_autorefresh_enabled(phys_enc))
  1948. return;
  1949. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1950. cmd_enc->autorefresh.cfg.enable);
  1951. sde_kms = phys_enc->sde_kms;
  1952. sde_encoder_phys_cmd_connect_te(phys_enc, false);
  1953. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, 0);
  1954. phys_enc->autorefresh_disable_trans = true;
  1955. if (sde_kms && sde_kms->catalog &&
  1956. (sde_kms->catalog->autorefresh_disable_seq == AUTOREFRESH_DISABLE_SEQ1)) {
  1957. _sde_encoder_autorefresh_disable_seq1(phys_enc);
  1958. _sde_encoder_autorefresh_disable_seq2(phys_enc);
  1959. }
  1960. sde_encoder_phys_cmd_connect_te(phys_enc, true);
  1961. SDE_DEBUG_CMDENC(cmd_enc, "autorefresh disabled successfully\n");
  1962. }
  1963. static void sde_encoder_phys_cmd_prepare_commit(struct sde_encoder_phys *phys_enc)
  1964. {
  1965. return _sde_encoder_phys_disable_autorefresh(phys_enc);
  1966. }
  1967. static void sde_encoder_phys_cmd_trigger_start(
  1968. struct sde_encoder_phys *phys_enc)
  1969. {
  1970. struct sde_encoder_phys_cmd *cmd_enc =
  1971. to_sde_encoder_phys_cmd(phys_enc);
  1972. u32 frame_cnt;
  1973. struct sde_hw_pp_vsync_info info[MAX_CHANNELS_PER_ENC] = {{0}};
  1974. if (!phys_enc)
  1975. return;
  1976. /* we don't issue CTL_START when using autorefresh */
  1977. frame_cnt = _sde_encoder_phys_cmd_get_autorefresh_property(phys_enc);
  1978. if (frame_cnt) {
  1979. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, frame_cnt);
  1980. atomic_inc(&cmd_enc->autorefresh.kickoff_cnt);
  1981. } else {
  1982. sde_encoder_helper_trigger_start(phys_enc);
  1983. }
  1984. sde_encoder_helper_get_pp_line_count(phys_enc->parent, info);
  1985. SDE_EVT32(DRMID(phys_enc->parent), frame_cnt, info[0].pp_idx, info[0].intf_idx,
  1986. info[0].intf_frame_count, info[0].wr_ptr_line_count, info[0].rd_ptr_line_count,
  1987. info[1].pp_idx, info[1].intf_idx, info[1].intf_frame_count,
  1988. info[1].wr_ptr_line_count, info[1].rd_ptr_line_count);
  1989. /* wr_ptr_wait_success is set true when wr_ptr arrives */
  1990. cmd_enc->wr_ptr_wait_success = false;
  1991. }
  1992. static void sde_encoder_phys_cmd_handle_post_kickoff(struct sde_encoder_phys *phys_enc)
  1993. {
  1994. if (!phys_enc) {
  1995. SDE_ERROR("invalid encoder\n");
  1996. return;
  1997. }
  1998. if (sde_encoder_phys_cmd_is_master(phys_enc))
  1999. _sde_encoder_phys_cmd_process_sim_qsync_event(phys_enc,
  2000. SDE_SIM_QSYNC_EVENT_FRAME_DETECTED);
  2001. }
  2002. static void _sde_encoder_phys_cmd_calculate_wd_params(struct sde_encoder_phys *phys_enc)
  2003. {
  2004. u32 nominal_te_value;
  2005. struct sde_encoder_virt *sde_enc;
  2006. struct msm_mode_info *mode_info;
  2007. const u32 multiplier = 1 << 10;
  2008. struct intf_wd_jitter_params wd_jtr;
  2009. sde_enc = to_sde_encoder_virt(phys_enc->parent);
  2010. mode_info = &sde_enc->mode_info;
  2011. if (mode_info->wd_jitter.jitter_type & MSM_DISPLAY_WD_INSTANTANEOUS_JITTER) {
  2012. wd_jtr.jitter = mult_frac(multiplier,
  2013. mode_info->wd_jitter.inst_jitter_numer,
  2014. (mode_info->wd_jitter.inst_jitter_denom * 100));
  2015. phys_enc->wd_jitter.jitter = wd_jtr.jitter;
  2016. }
  2017. if (mode_info->wd_jitter.jitter_type & MSM_DISPLAY_WD_LTJ_JITTER) {
  2018. nominal_te_value = CALCULATE_WD_LOAD_VALUE(mode_info->frame_rate) * MDP_TICK_COUNT;
  2019. wd_jtr.ltj_max = mult_frac(nominal_te_value,
  2020. mode_info->wd_jitter.ltj_max_numer,
  2021. (mode_info->wd_jitter.ltj_max_denom) * 100);
  2022. wd_jtr.ltj_slope = mult_frac((1 << 16), wd_jtr.ltj_max,
  2023. (mode_info->wd_jitter.ltj_time_sec * mode_info->frame_rate));
  2024. phys_enc->wd_jitter.ltj_max = wd_jtr.ltj_max;
  2025. phys_enc->wd_jitter.ltj_slope = wd_jtr.ltj_slope;
  2026. }
  2027. phys_enc->hw_intf->ops.configure_wd_jitter(phys_enc->hw_intf, &phys_enc->wd_jitter);
  2028. }
  2029. static void sde_encoder_phys_cmd_store_ltj_values(struct sde_encoder_phys *phys_enc)
  2030. {
  2031. if (phys_enc && phys_enc->hw_intf->ops.get_wd_ltj_status)
  2032. phys_enc->hw_intf->ops.get_wd_ltj_status(phys_enc->hw_intf, &phys_enc->wd_jitter);
  2033. }
  2034. static void sde_encoder_phys_cmd_setup_vsync_source(struct sde_encoder_phys *phys_enc,
  2035. u32 vsync_source, struct msm_display_info *disp_info)
  2036. {
  2037. struct sde_encoder_virt *sde_enc;
  2038. struct sde_connector *sde_conn;
  2039. if (!phys_enc || !phys_enc->hw_intf)
  2040. return;
  2041. sde_enc = to_sde_encoder_virt(phys_enc->parent);
  2042. if (!sde_enc)
  2043. return;
  2044. sde_conn = to_sde_connector(phys_enc->connector);
  2045. if ((disp_info->is_te_using_watchdog_timer || sde_conn->panel_dead) &&
  2046. phys_enc->hw_intf->ops.setup_vsync_source) {
  2047. vsync_source = SDE_VSYNC_SOURCE_WD_TIMER_0;
  2048. if (phys_enc->hw_intf->ops.configure_wd_jitter)
  2049. _sde_encoder_phys_cmd_calculate_wd_params(phys_enc);
  2050. phys_enc->hw_intf->ops.setup_vsync_source(phys_enc->hw_intf,
  2051. sde_enc->mode_info.frame_rate);
  2052. } else {
  2053. sde_encoder_helper_vsync_config(phys_enc, vsync_source);
  2054. }
  2055. if (phys_enc->has_intf_te && phys_enc->hw_intf->ops.vsync_sel)
  2056. phys_enc->hw_intf->ops.vsync_sel(phys_enc->hw_intf,
  2057. vsync_source);
  2058. }
  2059. void sde_encoder_phys_cmd_add_enc_to_minidump(struct sde_encoder_phys *phys_enc)
  2060. {
  2061. struct sde_encoder_phys_cmd *cmd_enc;
  2062. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  2063. sde_mini_dump_add_va_region("sde_enc_phys_cmd", sizeof(*cmd_enc), cmd_enc);
  2064. }
  2065. static void sde_encoder_phys_cmd_init_ops(struct sde_encoder_phys_ops *ops)
  2066. {
  2067. ops->prepare_commit = sde_encoder_phys_cmd_prepare_commit;
  2068. ops->is_master = sde_encoder_phys_cmd_is_master;
  2069. ops->mode_set = sde_encoder_phys_cmd_mode_set;
  2070. ops->cont_splash_mode_set = sde_encoder_phys_cmd_cont_splash_mode_set;
  2071. ops->mode_fixup = sde_encoder_phys_cmd_mode_fixup;
  2072. ops->enable = sde_encoder_phys_cmd_enable;
  2073. ops->disable = sde_encoder_phys_cmd_disable;
  2074. ops->destroy = sde_encoder_phys_cmd_destroy;
  2075. ops->get_hw_resources = sde_encoder_phys_cmd_get_hw_resources;
  2076. ops->control_vblank_irq = sde_encoder_phys_cmd_control_vblank_irq;
  2077. ops->wait_for_commit_done = sde_encoder_phys_cmd_wait_for_commit_done;
  2078. ops->prepare_for_kickoff = sde_encoder_phys_cmd_prepare_for_kickoff;
  2079. ops->wait_for_tx_complete = sde_encoder_phys_cmd_wait_for_tx_complete;
  2080. ops->wait_for_vblank = sde_encoder_phys_cmd_wait_for_vblank;
  2081. ops->trigger_flush = sde_encoder_helper_trigger_flush;
  2082. ops->trigger_start = sde_encoder_phys_cmd_trigger_start;
  2083. ops->needs_single_flush = sde_encoder_phys_needs_single_flush;
  2084. ops->hw_reset = sde_encoder_helper_hw_reset;
  2085. ops->irq_control = sde_encoder_phys_cmd_irq_control;
  2086. ops->dynamic_irq_control = sde_encoder_phys_cmd_dynamic_irq_control;
  2087. ops->update_split_role = sde_encoder_phys_cmd_update_split_role;
  2088. ops->restore = sde_encoder_phys_cmd_enable_helper;
  2089. ops->control_te = sde_encoder_phys_cmd_connect_te;
  2090. ops->is_autorefresh_enabled =
  2091. sde_encoder_phys_cmd_is_autorefresh_enabled;
  2092. ops->get_line_count = sde_encoder_phys_cmd_te_get_line_count;
  2093. ops->wait_for_active = NULL;
  2094. ops->setup_vsync_source = sde_encoder_phys_cmd_setup_vsync_source;
  2095. ops->setup_misr = sde_encoder_helper_setup_misr;
  2096. ops->collect_misr = sde_encoder_helper_collect_misr;
  2097. ops->add_to_minidump = sde_encoder_phys_cmd_add_enc_to_minidump;
  2098. ops->disable_autorefresh = _sde_encoder_phys_disable_autorefresh;
  2099. ops->idle_pc_cache_display_status = sde_encoder_phys_cmd_store_ltj_values;
  2100. ops->handle_post_kickoff = sde_encoder_phys_cmd_handle_post_kickoff;
  2101. }
  2102. static inline bool sde_encoder_phys_cmd_intf_te_supported(
  2103. const struct sde_mdss_cfg *sde_cfg, enum sde_intf idx)
  2104. {
  2105. if (sde_cfg && ((idx - INTF_0) < sde_cfg->intf_count))
  2106. return test_bit(SDE_INTF_TE,
  2107. &(sde_cfg->intf[idx - INTF_0].features));
  2108. return false;
  2109. }
  2110. static void _sde_encoder_phys_cmd_init_irqs(struct sde_encoder_phys *phys_enc)
  2111. {
  2112. struct sde_encoder_irq *irq;
  2113. int i;
  2114. for (i = 0; i < INTR_IDX_MAX; i++) {
  2115. irq = &phys_enc->irq[i];
  2116. INIT_LIST_HEAD(&irq->cb.list);
  2117. irq->irq_idx = -EINVAL;
  2118. irq->hw_idx = -EINVAL;
  2119. irq->cb.arg = phys_enc;
  2120. }
  2121. irq = &phys_enc->irq[INTR_IDX_CTL_START];
  2122. irq->name = "ctl_start";
  2123. irq->intr_type = SDE_IRQ_TYPE_CTL_START;
  2124. irq->intr_idx = INTR_IDX_CTL_START;
  2125. irq->cb.func = NULL;
  2126. irq = &phys_enc->irq[INTR_IDX_CTL_DONE];
  2127. irq->name = "ctl_done";
  2128. irq->intr_type = SDE_IRQ_TYPE_CTL_DONE;
  2129. irq->intr_idx = INTR_IDX_CTL_DONE;
  2130. irq->cb.func = sde_encoder_phys_cmd_ctl_done_irq;
  2131. irq = &phys_enc->irq[INTR_IDX_PINGPONG];
  2132. irq->name = "pp_done";
  2133. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_COMP;
  2134. irq->intr_idx = INTR_IDX_PINGPONG;
  2135. irq->cb.func = sde_encoder_phys_cmd_pp_tx_done_irq;
  2136. irq = &phys_enc->irq[INTR_IDX_RDPTR];
  2137. irq->intr_idx = INTR_IDX_RDPTR;
  2138. irq->name = "te_rd_ptr";
  2139. if (phys_enc->has_intf_te)
  2140. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_RD_PTR;
  2141. else
  2142. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_RD_PTR;
  2143. irq->cb.func = sde_encoder_phys_cmd_te_rd_ptr_irq;
  2144. irq = &phys_enc->irq[INTR_IDX_AUTOREFRESH_DONE];
  2145. irq->name = "autorefresh_done";
  2146. if (phys_enc->has_intf_te)
  2147. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_AUTO_REF;
  2148. else
  2149. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_AUTO_REF;
  2150. irq->intr_idx = INTR_IDX_AUTOREFRESH_DONE;
  2151. irq->cb.func = sde_encoder_phys_cmd_autorefresh_done_irq;
  2152. irq = &phys_enc->irq[INTR_IDX_WRPTR];
  2153. irq->intr_idx = INTR_IDX_WRPTR;
  2154. irq->name = "wr_ptr";
  2155. if (phys_enc->has_intf_te)
  2156. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_WR_PTR;
  2157. else
  2158. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_WR_PTR;
  2159. irq->cb.func = sde_encoder_phys_cmd_wr_ptr_irq;
  2160. irq = &phys_enc->irq[INTF_IDX_TEAR_DETECT];
  2161. irq->intr_idx = INTF_IDX_TEAR_DETECT;
  2162. irq->name = "te_tear_detect";
  2163. if (phys_enc->has_intf_te)
  2164. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_TEAR_DETECT;
  2165. else
  2166. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_TEAR_CHECK;
  2167. irq->cb.func = sde_encoder_phys_cmd_tear_detect_irq;
  2168. if (phys_enc->has_intf_te) {
  2169. irq = &phys_enc->irq[INTR_IDX_TE_ASSERT];
  2170. irq->intr_idx = INTR_IDX_TE_ASSERT;
  2171. irq->name = "te_assert";
  2172. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_TE_ASSERT;
  2173. irq->cb.func = sde_encoder_phys_cmd_te_assert_irq;
  2174. irq = &phys_enc->irq[INTR_IDX_TE_DEASSERT];
  2175. irq->intr_idx = INTR_IDX_TE_DEASSERT;
  2176. irq->name = "te_deassert";
  2177. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_TE_DEASSERT;
  2178. irq->cb.func = sde_encoder_phys_cmd_te_deassert_irq;
  2179. }
  2180. }
  2181. struct sde_encoder_phys *sde_encoder_phys_cmd_init(
  2182. struct sde_enc_phys_init_params *p)
  2183. {
  2184. struct sde_encoder_phys *phys_enc = NULL;
  2185. struct sde_encoder_phys_cmd *cmd_enc = NULL;
  2186. struct sde_hw_mdp *hw_mdp;
  2187. int i, ret = 0;
  2188. SDE_DEBUG("intf %d\n", p->intf_idx - INTF_0);
  2189. cmd_enc = kzalloc(sizeof(*cmd_enc), GFP_KERNEL);
  2190. if (!cmd_enc) {
  2191. ret = -ENOMEM;
  2192. SDE_ERROR("failed to allocate\n");
  2193. goto fail;
  2194. }
  2195. phys_enc = &cmd_enc->base;
  2196. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  2197. if (IS_ERR_OR_NULL(hw_mdp)) {
  2198. ret = PTR_ERR(hw_mdp);
  2199. SDE_ERROR("failed to get mdptop\n");
  2200. goto fail_mdp_init;
  2201. }
  2202. phys_enc->hw_mdptop = hw_mdp;
  2203. phys_enc->intf_idx = p->intf_idx;
  2204. phys_enc->parent = p->parent;
  2205. phys_enc->parent_ops = p->parent_ops;
  2206. phys_enc->sde_kms = p->sde_kms;
  2207. phys_enc->split_role = p->split_role;
  2208. phys_enc->intf_mode = INTF_MODE_CMD;
  2209. phys_enc->enc_spinlock = p->enc_spinlock;
  2210. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  2211. cmd_enc->stream_sel = 0;
  2212. phys_enc->enable_state = SDE_ENC_DISABLED;
  2213. phys_enc->kickoff_timeout_ms = DEFAULT_KICKOFF_TIMEOUT_MS;
  2214. sde_encoder_phys_cmd_init_ops(&phys_enc->ops);
  2215. phys_enc->comp_type = p->comp_type;
  2216. phys_enc->has_intf_te = sde_encoder_phys_cmd_intf_te_supported(
  2217. phys_enc->sde_kms->catalog, phys_enc->intf_idx);
  2218. _sde_encoder_phys_cmd_init_irqs(phys_enc);
  2219. atomic_set(&phys_enc->vblank_refcount, 0);
  2220. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  2221. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  2222. atomic_set(&cmd_enc->pending_vblank_cnt, 0);
  2223. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  2224. init_waitqueue_head(&cmd_enc->pending_vblank_wq);
  2225. atomic_set(&cmd_enc->autorefresh.kickoff_cnt, 0);
  2226. init_waitqueue_head(&cmd_enc->autorefresh.kickoff_wq);
  2227. INIT_LIST_HEAD(&cmd_enc->te_timestamp_list);
  2228. for (i = 0; i < MAX_TE_PROFILE_COUNT; i++)
  2229. list_add(&cmd_enc->te_timestamp[i].list,
  2230. &cmd_enc->te_timestamp_list);
  2231. SDE_DEBUG_CMDENC(cmd_enc, "created\n");
  2232. return phys_enc;
  2233. fail_mdp_init:
  2234. kfree(cmd_enc);
  2235. fail:
  2236. return ERR_PTR(ret);
  2237. }