sde_encoder_phys_cmd.c 64 KB

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