sde_encoder_phys_cmd.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
  6. #include "sde_encoder_phys.h"
  7. #include "sde_hw_interrupts.h"
  8. #include "sde_core_irq.h"
  9. #include "sde_formats.h"
  10. #include "sde_trace.h"
  11. #define SDE_DEBUG_CMDENC(e, fmt, ...) SDE_DEBUG("enc%d intf%d " fmt, \
  12. (e) && (e)->base.parent ? \
  13. (e)->base.parent->base.id : -1, \
  14. (e) ? (e)->base.intf_idx - INTF_0 : -1, ##__VA_ARGS__)
  15. #define SDE_ERROR_CMDENC(e, fmt, ...) SDE_ERROR("enc%d intf%d " fmt, \
  16. (e) && (e)->base.parent ? \
  17. (e)->base.parent->base.id : -1, \
  18. (e) ? (e)->base.intf_idx - INTF_0 : -1, ##__VA_ARGS__)
  19. #define to_sde_encoder_phys_cmd(x) \
  20. container_of(x, struct sde_encoder_phys_cmd, base)
  21. #define PP_TIMEOUT_MAX_TRIALS 4
  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. /*
  31. * Threshold for signalling retire fences in cases where
  32. * CTL_START_IRQ is received just after RD_PTR_IRQ
  33. */
  34. #define SDE_ENC_CTL_START_THRESHOLD_US 500
  35. #define SDE_ENC_MAX_POLL_TIMEOUT_US 2000
  36. static inline int _sde_encoder_phys_cmd_get_idle_timeout(
  37. struct sde_encoder_phys_cmd *cmd_enc)
  38. {
  39. return cmd_enc->autorefresh.cfg.frame_count ?
  40. cmd_enc->autorefresh.cfg.frame_count *
  41. KICKOFF_TIMEOUT_MS : KICKOFF_TIMEOUT_MS;
  42. }
  43. static inline bool sde_encoder_phys_cmd_is_master(
  44. struct sde_encoder_phys *phys_enc)
  45. {
  46. return (phys_enc->split_role != ENC_ROLE_SLAVE) ? true : false;
  47. }
  48. static bool sde_encoder_phys_cmd_mode_fixup(
  49. struct sde_encoder_phys *phys_enc,
  50. const struct drm_display_mode *mode,
  51. struct drm_display_mode *adj_mode)
  52. {
  53. if (phys_enc)
  54. SDE_DEBUG_CMDENC(to_sde_encoder_phys_cmd(phys_enc), "\n");
  55. return true;
  56. }
  57. static uint64_t _sde_encoder_phys_cmd_get_autorefresh_property(
  58. struct sde_encoder_phys *phys_enc)
  59. {
  60. struct drm_connector *conn = phys_enc->connector;
  61. if (!conn || !conn->state)
  62. return 0;
  63. return sde_connector_get_property(conn->state,
  64. CONNECTOR_PROP_AUTOREFRESH);
  65. }
  66. static void _sde_encoder_phys_cmd_config_autorefresh(
  67. struct sde_encoder_phys *phys_enc,
  68. u32 new_frame_count)
  69. {
  70. struct sde_encoder_phys_cmd *cmd_enc =
  71. to_sde_encoder_phys_cmd(phys_enc);
  72. struct sde_hw_pingpong *hw_pp = phys_enc->hw_pp;
  73. struct sde_hw_intf *hw_intf = phys_enc->hw_intf;
  74. struct drm_connector *conn = phys_enc->connector;
  75. struct sde_hw_autorefresh *cfg_cur, cfg_nxt;
  76. if (!conn || !conn->state || !hw_pp || !hw_intf)
  77. return;
  78. cfg_cur = &cmd_enc->autorefresh.cfg;
  79. /* autorefresh property value should be validated already */
  80. memset(&cfg_nxt, 0, sizeof(cfg_nxt));
  81. cfg_nxt.frame_count = new_frame_count;
  82. cfg_nxt.enable = (cfg_nxt.frame_count != 0);
  83. SDE_DEBUG_CMDENC(cmd_enc, "autorefresh state %d->%d framecount %d\n",
  84. cfg_cur->enable, cfg_nxt.enable, cfg_nxt.frame_count);
  85. SDE_EVT32(DRMID(phys_enc->parent), hw_pp->idx, hw_intf->idx,
  86. cfg_cur->enable, cfg_nxt.enable, cfg_nxt.frame_count);
  87. /* only proceed on state changes */
  88. if (cfg_nxt.enable == cfg_cur->enable)
  89. return;
  90. memcpy(cfg_cur, &cfg_nxt, sizeof(*cfg_cur));
  91. if (phys_enc->has_intf_te && hw_intf->ops.setup_autorefresh)
  92. hw_intf->ops.setup_autorefresh(hw_intf, cfg_cur);
  93. else if (hw_pp->ops.setup_autorefresh)
  94. hw_pp->ops.setup_autorefresh(hw_pp, cfg_cur);
  95. }
  96. static void _sde_encoder_phys_cmd_update_flush_mask(
  97. struct sde_encoder_phys *phys_enc)
  98. {
  99. struct sde_encoder_phys_cmd *cmd_enc;
  100. struct sde_hw_ctl *ctl;
  101. if (!phys_enc || !phys_enc->hw_intf || !phys_enc->hw_pp)
  102. return;
  103. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  104. ctl = phys_enc->hw_ctl;
  105. if (!ctl)
  106. return;
  107. if (!ctl->ops.update_bitmask_intf ||
  108. (test_bit(SDE_CTL_ACTIVE_CFG, &ctl->caps->features) &&
  109. !ctl->ops.update_bitmask_merge3d)) {
  110. SDE_ERROR("invalid hw_ctl ops %d\n", ctl->idx);
  111. return;
  112. }
  113. ctl->ops.update_bitmask_intf(ctl, phys_enc->intf_idx, 1);
  114. if (ctl->ops.update_bitmask_merge3d && phys_enc->hw_pp->merge_3d)
  115. ctl->ops.update_bitmask_merge3d(ctl,
  116. phys_enc->hw_pp->merge_3d->idx, 1);
  117. SDE_DEBUG_CMDENC(cmd_enc, "update pending flush ctl %d intf_idx %x\n",
  118. ctl->idx - CTL_0, phys_enc->intf_idx);
  119. }
  120. static void _sde_encoder_phys_cmd_update_intf_cfg(
  121. struct sde_encoder_phys *phys_enc)
  122. {
  123. struct sde_encoder_phys_cmd *cmd_enc =
  124. to_sde_encoder_phys_cmd(phys_enc);
  125. struct sde_hw_ctl *ctl;
  126. if (!phys_enc)
  127. return;
  128. ctl = phys_enc->hw_ctl;
  129. if (!ctl)
  130. return;
  131. if (ctl->ops.setup_intf_cfg) {
  132. struct sde_hw_intf_cfg intf_cfg = { 0 };
  133. intf_cfg.intf = phys_enc->intf_idx;
  134. intf_cfg.intf_mode_sel = SDE_CTL_MODE_SEL_CMD;
  135. intf_cfg.stream_sel = cmd_enc->stream_sel;
  136. intf_cfg.mode_3d =
  137. sde_encoder_helper_get_3d_blend_mode(phys_enc);
  138. ctl->ops.setup_intf_cfg(ctl, &intf_cfg);
  139. } else if (test_bit(SDE_CTL_ACTIVE_CFG, &ctl->caps->features)) {
  140. sde_encoder_helper_update_intf_cfg(phys_enc);
  141. }
  142. }
  143. static void sde_encoder_phys_cmd_pp_tx_done_irq(void *arg, int irq_idx)
  144. {
  145. struct sde_encoder_phys *phys_enc = arg;
  146. unsigned long lock_flags;
  147. int new_cnt;
  148. u32 event = SDE_ENCODER_FRAME_EVENT_DONE |
  149. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  150. if (!phys_enc || !phys_enc->hw_pp)
  151. return;
  152. SDE_ATRACE_BEGIN("pp_done_irq");
  153. /* notify all synchronous clients first, then asynchronous clients */
  154. if (phys_enc->parent_ops.handle_frame_done &&
  155. atomic_read(&phys_enc->pending_kickoff_cnt))
  156. phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
  157. phys_enc, event);
  158. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  159. new_cnt = atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
  160. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  161. SDE_EVT32_IRQ(DRMID(phys_enc->parent),
  162. phys_enc->hw_pp->idx - PINGPONG_0, new_cnt, event);
  163. /*
  164. * Reduce the refcount for the retire fence as well as for the ctl_start
  165. * if the counters are greater than zero. Signal retire fence if there
  166. * was a retire fence count pending and kickoff count is zero.
  167. */
  168. if (sde_encoder_phys_cmd_is_master(phys_enc) && (new_cnt == 0)) {
  169. while (atomic_add_unless(&phys_enc->pending_retire_fence_cnt,
  170. -1, 0)) {
  171. if (phys_enc->parent_ops.handle_frame_done)
  172. phys_enc->parent_ops.handle_frame_done(
  173. phys_enc->parent, phys_enc,
  174. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  175. atomic_add_unless(&phys_enc->pending_ctlstart_cnt,
  176. -1, 0);
  177. }
  178. }
  179. /* Signal any waiting atomic commit thread */
  180. wake_up_all(&phys_enc->pending_kickoff_wq);
  181. SDE_ATRACE_END("pp_done_irq");
  182. }
  183. static void sde_encoder_phys_cmd_autorefresh_done_irq(void *arg, int irq_idx)
  184. {
  185. struct sde_encoder_phys *phys_enc = arg;
  186. struct sde_encoder_phys_cmd *cmd_enc =
  187. to_sde_encoder_phys_cmd(phys_enc);
  188. unsigned long lock_flags;
  189. int new_cnt;
  190. if (!cmd_enc)
  191. return;
  192. phys_enc = &cmd_enc->base;
  193. spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
  194. new_cnt = atomic_add_unless(&cmd_enc->autorefresh.kickoff_cnt, -1, 0);
  195. spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
  196. SDE_EVT32_IRQ(DRMID(phys_enc->parent),
  197. phys_enc->hw_pp->idx - PINGPONG_0,
  198. phys_enc->hw_intf->idx - INTF_0,
  199. new_cnt);
  200. /* Signal any waiting atomic commit thread */
  201. wake_up_all(&cmd_enc->autorefresh.kickoff_wq);
  202. }
  203. static void sde_encoder_phys_cmd_te_rd_ptr_irq(void *arg, int irq_idx)
  204. {
  205. struct sde_encoder_phys *phys_enc = arg;
  206. struct sde_encoder_phys_cmd *cmd_enc;
  207. u32 event = 0, scheduler_status = INVALID_CTL_STATUS;
  208. struct sde_hw_ctl *ctl;
  209. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  210. return;
  211. SDE_ATRACE_BEGIN("rd_ptr_irq");
  212. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  213. ctl = phys_enc->hw_ctl;
  214. /**
  215. * signal only for master, when the ctl_start irq is
  216. * done and incremented the pending_rd_ptr_cnt.
  217. */
  218. if (sde_encoder_phys_cmd_is_master(phys_enc)
  219. && atomic_add_unless(&cmd_enc->pending_rd_ptr_cnt, -1, 0)
  220. && atomic_add_unless(
  221. &phys_enc->pending_retire_fence_cnt, -1, 0)) {
  222. event = SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  223. if (phys_enc->parent_ops.handle_frame_done)
  224. phys_enc->parent_ops.handle_frame_done(
  225. phys_enc->parent, phys_enc, event);
  226. }
  227. if (ctl && ctl->ops.get_scheduler_status)
  228. scheduler_status = ctl->ops.get_scheduler_status(ctl);
  229. SDE_EVT32_IRQ(DRMID(phys_enc->parent),
  230. phys_enc->hw_pp->idx - PINGPONG_0,
  231. phys_enc->hw_intf->idx - INTF_0,
  232. event, scheduler_status, 0xfff);
  233. if (phys_enc->parent_ops.handle_vblank_virt)
  234. phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
  235. phys_enc);
  236. cmd_enc->rd_ptr_timestamp = ktime_get();
  237. atomic_add_unless(&cmd_enc->pending_vblank_cnt, -1, 0);
  238. wake_up_all(&cmd_enc->pending_vblank_wq);
  239. SDE_ATRACE_END("rd_ptr_irq");
  240. }
  241. static void sde_encoder_phys_cmd_ctl_start_irq(void *arg, int irq_idx)
  242. {
  243. struct sde_encoder_phys *phys_enc = arg;
  244. struct sde_encoder_phys_cmd *cmd_enc;
  245. struct sde_hw_ctl *ctl;
  246. u32 event = 0;
  247. s64 time_diff_us;
  248. if (!phys_enc || !phys_enc->hw_ctl)
  249. return;
  250. SDE_ATRACE_BEGIN("ctl_start_irq");
  251. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  252. ctl = phys_enc->hw_ctl;
  253. atomic_add_unless(&phys_enc->pending_ctlstart_cnt, -1, 0);
  254. time_diff_us = ktime_us_delta(ktime_get(), cmd_enc->rd_ptr_timestamp);
  255. /* handle retire fence based on only master */
  256. if (sde_encoder_phys_cmd_is_master(phys_enc)
  257. && atomic_read(&phys_enc->pending_retire_fence_cnt)) {
  258. /**
  259. * Handle rare cases where the ctl_start_irq is received
  260. * after rd_ptr_irq. If it falls within a threshold, it is
  261. * guaranteed the frame would be picked up in the current TE.
  262. * Signal retire fence immediately in such case. The threshold
  263. * timer adds extra line time duration based on lowest panel
  264. * fps for qsync enabled case.
  265. */
  266. if ((time_diff_us <= cmd_enc->ctl_start_threshold)
  267. && atomic_add_unless(
  268. &phys_enc->pending_retire_fence_cnt, -1, 0)) {
  269. event = SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  270. if (phys_enc->parent_ops.handle_frame_done)
  271. phys_enc->parent_ops.handle_frame_done(
  272. phys_enc->parent, phys_enc, event);
  273. /**
  274. * In ideal cases, ctl_start_irq is received before the
  275. * rd_ptr_irq, so set the atomic flag to indicate the event
  276. * and rd_ptr_irq will handle signalling the retire fence
  277. */
  278. } else {
  279. atomic_inc(&cmd_enc->pending_rd_ptr_cnt);
  280. }
  281. }
  282. SDE_EVT32_IRQ(DRMID(phys_enc->parent), ctl->idx - CTL_0,
  283. time_diff_us, event, 0xfff);
  284. /* Signal any waiting ctl start interrupt */
  285. wake_up_all(&phys_enc->pending_kickoff_wq);
  286. SDE_ATRACE_END("ctl_start_irq");
  287. }
  288. static void sde_encoder_phys_cmd_underrun_irq(void *arg, int irq_idx)
  289. {
  290. struct sde_encoder_phys *phys_enc = arg;
  291. if (!phys_enc)
  292. return;
  293. if (phys_enc->parent_ops.handle_underrun_virt)
  294. phys_enc->parent_ops.handle_underrun_virt(phys_enc->parent,
  295. phys_enc);
  296. }
  297. static void _sde_encoder_phys_cmd_setup_irq_hw_idx(
  298. struct sde_encoder_phys *phys_enc)
  299. {
  300. struct sde_encoder_irq *irq;
  301. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_ctl) {
  302. SDE_ERROR("invalid args %d %d\n", !phys_enc,
  303. phys_enc ? !phys_enc->hw_pp : 0);
  304. return;
  305. }
  306. if (phys_enc->has_intf_te && !phys_enc->hw_intf) {
  307. SDE_ERROR("invalid intf configuration\n");
  308. return;
  309. }
  310. irq = &phys_enc->irq[INTR_IDX_CTL_START];
  311. irq->hw_idx = phys_enc->hw_ctl->idx;
  312. irq->irq_idx = -EINVAL;
  313. irq = &phys_enc->irq[INTR_IDX_PINGPONG];
  314. irq->hw_idx = phys_enc->hw_pp->idx;
  315. irq->irq_idx = -EINVAL;
  316. irq = &phys_enc->irq[INTR_IDX_RDPTR];
  317. irq->irq_idx = -EINVAL;
  318. if (phys_enc->has_intf_te)
  319. irq->hw_idx = phys_enc->hw_intf->idx;
  320. else
  321. irq->hw_idx = phys_enc->hw_pp->idx;
  322. irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
  323. irq->hw_idx = phys_enc->intf_idx;
  324. irq->irq_idx = -EINVAL;
  325. irq = &phys_enc->irq[INTR_IDX_AUTOREFRESH_DONE];
  326. irq->irq_idx = -EINVAL;
  327. if (phys_enc->has_intf_te)
  328. irq->hw_idx = phys_enc->hw_intf->idx;
  329. else
  330. irq->hw_idx = phys_enc->hw_pp->idx;
  331. }
  332. static void sde_encoder_phys_cmd_cont_splash_mode_set(
  333. struct sde_encoder_phys *phys_enc,
  334. struct drm_display_mode *adj_mode)
  335. {
  336. struct sde_hw_intf *hw_intf;
  337. struct sde_hw_pingpong *hw_pp;
  338. struct sde_encoder_phys_cmd *cmd_enc;
  339. if (!phys_enc || !adj_mode) {
  340. SDE_ERROR("invalid args\n");
  341. return;
  342. }
  343. phys_enc->cached_mode = *adj_mode;
  344. phys_enc->enable_state = SDE_ENC_ENABLED;
  345. if (!phys_enc->hw_ctl || !phys_enc->hw_pp) {
  346. SDE_DEBUG("invalid ctl:%d pp:%d\n",
  347. (phys_enc->hw_ctl == NULL),
  348. (phys_enc->hw_pp == NULL));
  349. return;
  350. }
  351. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  352. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  353. hw_pp = phys_enc->hw_pp;
  354. hw_intf = phys_enc->hw_intf;
  355. if (phys_enc->has_intf_te && hw_intf &&
  356. hw_intf->ops.get_autorefresh) {
  357. hw_intf->ops.get_autorefresh(hw_intf,
  358. &cmd_enc->autorefresh.cfg);
  359. } else if (hw_pp && hw_pp->ops.get_autorefresh) {
  360. hw_pp->ops.get_autorefresh(hw_pp,
  361. &cmd_enc->autorefresh.cfg);
  362. }
  363. }
  364. _sde_encoder_phys_cmd_setup_irq_hw_idx(phys_enc);
  365. }
  366. static void sde_encoder_phys_cmd_mode_set(
  367. struct sde_encoder_phys *phys_enc,
  368. struct drm_display_mode *mode,
  369. struct drm_display_mode *adj_mode)
  370. {
  371. struct sde_encoder_phys_cmd *cmd_enc =
  372. to_sde_encoder_phys_cmd(phys_enc);
  373. struct sde_rm *rm = &phys_enc->sde_kms->rm;
  374. struct sde_rm_hw_iter iter;
  375. int i, instance;
  376. if (!phys_enc || !mode || !adj_mode) {
  377. SDE_ERROR("invalid args\n");
  378. return;
  379. }
  380. phys_enc->cached_mode = *adj_mode;
  381. SDE_DEBUG_CMDENC(cmd_enc, "caching mode:\n");
  382. drm_mode_debug_printmodeline(adj_mode);
  383. instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
  384. /* Retrieve previously allocated HW Resources. Shouldn't fail */
  385. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
  386. for (i = 0; i <= instance; i++) {
  387. if (sde_rm_get_hw(rm, &iter))
  388. phys_enc->hw_ctl = (struct sde_hw_ctl *)iter.hw;
  389. }
  390. if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
  391. SDE_ERROR_CMDENC(cmd_enc, "failed to init ctl: %ld\n",
  392. PTR_ERR(phys_enc->hw_ctl));
  393. phys_enc->hw_ctl = NULL;
  394. return;
  395. }
  396. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_INTF);
  397. for (i = 0; i <= instance; i++) {
  398. if (sde_rm_get_hw(rm, &iter))
  399. phys_enc->hw_intf = (struct sde_hw_intf *)iter.hw;
  400. }
  401. if (IS_ERR_OR_NULL(phys_enc->hw_intf)) {
  402. SDE_ERROR_CMDENC(cmd_enc, "failed to init intf: %ld\n",
  403. PTR_ERR(phys_enc->hw_intf));
  404. phys_enc->hw_intf = NULL;
  405. return;
  406. }
  407. _sde_encoder_phys_cmd_setup_irq_hw_idx(phys_enc);
  408. }
  409. static int _sde_encoder_phys_cmd_handle_ppdone_timeout(
  410. struct sde_encoder_phys *phys_enc,
  411. bool recovery_events)
  412. {
  413. struct sde_encoder_phys_cmd *cmd_enc =
  414. to_sde_encoder_phys_cmd(phys_enc);
  415. u32 frame_event = SDE_ENCODER_FRAME_EVENT_ERROR
  416. | SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  417. struct drm_connector *conn;
  418. int event;
  419. u32 pending_kickoff_cnt;
  420. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_ctl)
  421. return -EINVAL;
  422. conn = phys_enc->connector;
  423. if (atomic_read(&phys_enc->pending_kickoff_cnt) == 0)
  424. return 0;
  425. cmd_enc->pp_timeout_report_cnt++;
  426. pending_kickoff_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  427. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  428. /* trigger the retire fence if it was missed */
  429. if (atomic_add_unless(&phys_enc->pending_retire_fence_cnt,
  430. -1, 0))
  431. phys_enc->parent_ops.handle_frame_done(
  432. phys_enc->parent,
  433. phys_enc,
  434. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  435. atomic_add_unless(&phys_enc->pending_ctlstart_cnt, -1, 0);
  436. }
  437. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  438. cmd_enc->pp_timeout_report_cnt,
  439. pending_kickoff_cnt,
  440. frame_event);
  441. /* decrement the kickoff_cnt before checking for ESD status */
  442. atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
  443. /* check if panel is still sending TE signal or not */
  444. if (sde_connector_esd_status(phys_enc->connector))
  445. goto exit;
  446. /* to avoid flooding, only log first time, and "dead" time */
  447. if (cmd_enc->pp_timeout_report_cnt == 1) {
  448. SDE_ERROR_CMDENC(cmd_enc,
  449. "pp:%d kickoff timed out ctl %d koff_cnt %d\n",
  450. phys_enc->hw_pp->idx - PINGPONG_0,
  451. phys_enc->hw_ctl->idx - CTL_0,
  452. pending_kickoff_cnt);
  453. SDE_EVT32(DRMID(phys_enc->parent), SDE_EVTLOG_FATAL);
  454. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_RDPTR);
  455. SDE_DBG_DUMP("all", "dbg_bus", "vbif_dbg_bus");
  456. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_RDPTR);
  457. }
  458. /*
  459. * if the recovery event is registered by user, don't panic
  460. * trigger panic on first timeout if no listener registered
  461. */
  462. if (recovery_events) {
  463. event = cmd_enc->pp_timeout_report_cnt > PP_TIMEOUT_MAX_TRIALS ?
  464. SDE_RECOVERY_HARD_RESET : SDE_RECOVERY_CAPTURE;
  465. sde_connector_event_notify(conn, DRM_EVENT_SDE_HW_RECOVERY,
  466. sizeof(uint8_t), event);
  467. } else if (cmd_enc->pp_timeout_report_cnt) {
  468. SDE_DBG_DUMP("panic");
  469. }
  470. /* request a ctl reset before the next kickoff */
  471. phys_enc->enable_state = SDE_ENC_ERR_NEEDS_HW_RESET;
  472. exit:
  473. if (phys_enc->parent_ops.handle_frame_done)
  474. phys_enc->parent_ops.handle_frame_done(
  475. phys_enc->parent, phys_enc, frame_event);
  476. return -ETIMEDOUT;
  477. }
  478. static bool _sde_encoder_phys_is_ppsplit_slave(
  479. struct sde_encoder_phys *phys_enc)
  480. {
  481. if (!phys_enc)
  482. return false;
  483. return _sde_encoder_phys_is_ppsplit(phys_enc) &&
  484. phys_enc->split_role == ENC_ROLE_SLAVE;
  485. }
  486. static bool _sde_encoder_phys_is_disabling_ppsplit_slave(
  487. struct sde_encoder_phys *phys_enc)
  488. {
  489. enum sde_rm_topology_name old_top;
  490. if (!phys_enc || !phys_enc->connector ||
  491. phys_enc->split_role != ENC_ROLE_SLAVE)
  492. return false;
  493. old_top = sde_connector_get_old_topology_name(
  494. phys_enc->connector->state);
  495. return old_top == SDE_RM_TOPOLOGY_PPSPLIT;
  496. }
  497. static int _sde_encoder_phys_cmd_poll_write_pointer_started(
  498. struct sde_encoder_phys *phys_enc)
  499. {
  500. struct sde_encoder_phys_cmd *cmd_enc =
  501. to_sde_encoder_phys_cmd(phys_enc);
  502. struct sde_hw_pingpong *hw_pp = phys_enc->hw_pp;
  503. struct sde_hw_intf *hw_intf = phys_enc->hw_intf;
  504. struct sde_hw_pp_vsync_info info;
  505. u32 timeout_us = SDE_ENC_WR_PTR_START_TIMEOUT_US;
  506. int ret = 0;
  507. if (!hw_pp || !hw_intf)
  508. return 0;
  509. if (phys_enc->has_intf_te) {
  510. if (!hw_intf->ops.get_vsync_info ||
  511. !hw_intf->ops.poll_timeout_wr_ptr)
  512. goto end;
  513. } else {
  514. if (!hw_pp->ops.get_vsync_info ||
  515. !hw_pp->ops.poll_timeout_wr_ptr)
  516. goto end;
  517. }
  518. if (phys_enc->has_intf_te)
  519. ret = hw_intf->ops.get_vsync_info(hw_intf, &info);
  520. else
  521. ret = hw_pp->ops.get_vsync_info(hw_pp, &info);
  522. if (ret)
  523. return ret;
  524. SDE_DEBUG_CMDENC(cmd_enc,
  525. "pp:%d intf:%d rd_ptr %d wr_ptr %d\n",
  526. phys_enc->hw_pp->idx - PINGPONG_0,
  527. phys_enc->hw_intf->idx - INTF_0,
  528. info.rd_ptr_line_count,
  529. info.wr_ptr_line_count);
  530. SDE_EVT32_VERBOSE(DRMID(phys_enc->parent),
  531. phys_enc->hw_pp->idx - PINGPONG_0,
  532. phys_enc->hw_intf->idx - INTF_0,
  533. info.wr_ptr_line_count);
  534. if (phys_enc->has_intf_te)
  535. ret = hw_intf->ops.poll_timeout_wr_ptr(hw_intf, timeout_us);
  536. else
  537. ret = hw_pp->ops.poll_timeout_wr_ptr(hw_pp, timeout_us);
  538. if (ret) {
  539. SDE_EVT32(DRMID(phys_enc->parent),
  540. phys_enc->hw_pp->idx - PINGPONG_0,
  541. phys_enc->hw_intf->idx - INTF_0,
  542. timeout_us,
  543. ret);
  544. SDE_DBG_DUMP("all", "dbg_bus", "vbif_dbg_bus", "panic");
  545. }
  546. end:
  547. return ret;
  548. }
  549. static bool _sde_encoder_phys_cmd_is_ongoing_pptx(
  550. struct sde_encoder_phys *phys_enc)
  551. {
  552. struct sde_hw_pingpong *hw_pp;
  553. struct sde_hw_pp_vsync_info info;
  554. struct sde_hw_intf *hw_intf;
  555. if (!phys_enc)
  556. return false;
  557. if (phys_enc->has_intf_te) {
  558. hw_intf = phys_enc->hw_intf;
  559. if (!hw_intf || !hw_intf->ops.get_vsync_info)
  560. return false;
  561. hw_intf->ops.get_vsync_info(hw_intf, &info);
  562. } else {
  563. hw_pp = phys_enc->hw_pp;
  564. if (!hw_pp || !hw_pp->ops.get_vsync_info)
  565. return false;
  566. hw_pp->ops.get_vsync_info(hw_pp, &info);
  567. }
  568. SDE_EVT32(DRMID(phys_enc->parent),
  569. phys_enc->hw_pp->idx - PINGPONG_0,
  570. phys_enc->hw_intf->idx - INTF_0,
  571. atomic_read(&phys_enc->pending_kickoff_cnt),
  572. info.wr_ptr_line_count,
  573. phys_enc->cached_mode.vdisplay);
  574. if (info.wr_ptr_line_count > 0 && info.wr_ptr_line_count <
  575. phys_enc->cached_mode.vdisplay)
  576. return true;
  577. return false;
  578. }
  579. static int _sde_encoder_phys_cmd_wait_for_idle(
  580. struct sde_encoder_phys *phys_enc)
  581. {
  582. struct sde_encoder_phys_cmd *cmd_enc =
  583. to_sde_encoder_phys_cmd(phys_enc);
  584. struct sde_encoder_wait_info wait_info;
  585. bool recovery_events;
  586. int ret, i, pending_cnt;
  587. if (!phys_enc) {
  588. SDE_ERROR("invalid encoder\n");
  589. return -EINVAL;
  590. }
  591. wait_info.wq = &phys_enc->pending_kickoff_wq;
  592. wait_info.atomic_cnt = &phys_enc->pending_kickoff_cnt;
  593. wait_info.timeout_ms = KICKOFF_TIMEOUT_MS;
  594. recovery_events = sde_encoder_recovery_events_enabled(
  595. phys_enc->parent);
  596. /* slave encoder doesn't enable for ppsplit */
  597. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  598. return 0;
  599. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_PINGPONG,
  600. &wait_info);
  601. if (ret == -ETIMEDOUT) {
  602. pending_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  603. for (i = 0; i < pending_cnt; i++)
  604. _sde_encoder_phys_cmd_handle_ppdone_timeout(phys_enc,
  605. recovery_events);
  606. } else if (!ret) {
  607. if (cmd_enc->pp_timeout_report_cnt && recovery_events) {
  608. struct drm_connector *conn = phys_enc->connector;
  609. sde_connector_event_notify(conn,
  610. DRM_EVENT_SDE_HW_RECOVERY,
  611. sizeof(uint8_t),
  612. SDE_RECOVERY_SUCCESS);
  613. }
  614. cmd_enc->pp_timeout_report_cnt = 0;
  615. }
  616. return ret;
  617. }
  618. static int _sde_encoder_phys_cmd_wait_for_autorefresh_done(
  619. struct sde_encoder_phys *phys_enc)
  620. {
  621. struct sde_encoder_phys_cmd *cmd_enc =
  622. to_sde_encoder_phys_cmd(phys_enc);
  623. struct sde_encoder_wait_info wait_info;
  624. int ret = 0;
  625. if (!phys_enc) {
  626. SDE_ERROR("invalid encoder\n");
  627. return -EINVAL;
  628. }
  629. /* only master deals with autorefresh */
  630. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  631. return 0;
  632. wait_info.wq = &cmd_enc->autorefresh.kickoff_wq;
  633. wait_info.atomic_cnt = &cmd_enc->autorefresh.kickoff_cnt;
  634. wait_info.timeout_ms = _sde_encoder_phys_cmd_get_idle_timeout(cmd_enc);
  635. /* wait for autorefresh kickoff to start */
  636. ret = sde_encoder_helper_wait_for_irq(phys_enc,
  637. INTR_IDX_AUTOREFRESH_DONE, &wait_info);
  638. /* double check that kickoff has started by reading write ptr reg */
  639. if (!ret)
  640. ret = _sde_encoder_phys_cmd_poll_write_pointer_started(
  641. phys_enc);
  642. else
  643. sde_encoder_helper_report_irq_timeout(phys_enc,
  644. INTR_IDX_AUTOREFRESH_DONE);
  645. return ret;
  646. }
  647. static int sde_encoder_phys_cmd_control_vblank_irq(
  648. struct sde_encoder_phys *phys_enc,
  649. bool enable)
  650. {
  651. struct sde_encoder_phys_cmd *cmd_enc =
  652. to_sde_encoder_phys_cmd(phys_enc);
  653. int ret = 0;
  654. int refcount;
  655. if (!phys_enc || !phys_enc->hw_pp) {
  656. SDE_ERROR("invalid encoder\n");
  657. return -EINVAL;
  658. }
  659. mutex_lock(phys_enc->vblank_ctl_lock);
  660. refcount = atomic_read(&phys_enc->vblank_refcount);
  661. /* Slave encoders don't report vblank */
  662. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  663. goto end;
  664. /* protect against negative */
  665. if (!enable && refcount == 0) {
  666. ret = -EINVAL;
  667. goto end;
  668. }
  669. SDE_DEBUG_CMDENC(cmd_enc, "[%pS] enable=%d/%d\n",
  670. __builtin_return_address(0), enable, refcount);
  671. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  672. enable, refcount);
  673. if (enable && atomic_inc_return(&phys_enc->vblank_refcount) == 1)
  674. ret = sde_encoder_helper_register_irq(phys_enc, INTR_IDX_RDPTR);
  675. else if (!enable && atomic_dec_return(&phys_enc->vblank_refcount) == 0)
  676. ret = sde_encoder_helper_unregister_irq(phys_enc,
  677. INTR_IDX_RDPTR);
  678. end:
  679. if (ret) {
  680. SDE_ERROR_CMDENC(cmd_enc,
  681. "control vblank irq error %d, enable %d, refcount %d\n",
  682. ret, enable, refcount);
  683. SDE_EVT32(DRMID(phys_enc->parent),
  684. phys_enc->hw_pp->idx - PINGPONG_0,
  685. enable, refcount, SDE_EVTLOG_ERROR);
  686. }
  687. mutex_unlock(phys_enc->vblank_ctl_lock);
  688. return ret;
  689. }
  690. void sde_encoder_phys_cmd_irq_control(struct sde_encoder_phys *phys_enc,
  691. bool enable)
  692. {
  693. struct sde_encoder_phys_cmd *cmd_enc;
  694. if (!phys_enc)
  695. return;
  696. /**
  697. * pingpong split slaves do not register for IRQs
  698. * check old and new topologies
  699. */
  700. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc) ||
  701. _sde_encoder_phys_is_disabling_ppsplit_slave(phys_enc))
  702. return;
  703. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  704. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  705. enable, atomic_read(&phys_enc->vblank_refcount));
  706. if (enable) {
  707. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_PINGPONG);
  708. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_UNDERRUN);
  709. sde_encoder_phys_cmd_control_vblank_irq(phys_enc, true);
  710. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  711. sde_encoder_helper_register_irq(phys_enc,
  712. INTR_IDX_CTL_START);
  713. sde_encoder_helper_register_irq(phys_enc,
  714. INTR_IDX_AUTOREFRESH_DONE);
  715. }
  716. } else {
  717. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  718. sde_encoder_helper_unregister_irq(phys_enc,
  719. INTR_IDX_CTL_START);
  720. sde_encoder_helper_unregister_irq(phys_enc,
  721. INTR_IDX_AUTOREFRESH_DONE);
  722. }
  723. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_UNDERRUN);
  724. sde_encoder_phys_cmd_control_vblank_irq(phys_enc, false);
  725. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_PINGPONG);
  726. }
  727. }
  728. static int _get_tearcheck_threshold(struct sde_encoder_phys *phys_enc,
  729. u32 *extra_frame_trigger_time)
  730. {
  731. struct drm_connector *conn = phys_enc->connector;
  732. u32 qsync_mode;
  733. struct drm_display_mode *mode;
  734. u32 threshold_lines = 0;
  735. struct sde_encoder_phys_cmd *cmd_enc =
  736. to_sde_encoder_phys_cmd(phys_enc);
  737. *extra_frame_trigger_time = 0;
  738. if (!conn || !conn->state)
  739. return 0;
  740. mode = &phys_enc->cached_mode;
  741. qsync_mode = sde_connector_get_qsync_mode(conn);
  742. if (mode && (qsync_mode == SDE_RM_QSYNC_CONTINUOUS_MODE)) {
  743. u32 qsync_min_fps = 0;
  744. u32 default_fps = mode->vrefresh;
  745. u32 yres = mode->vtotal;
  746. u32 slow_time_ns;
  747. u32 default_time_ns;
  748. u32 extra_time_ns;
  749. u32 total_extra_lines;
  750. u32 default_line_time_ns;
  751. if (phys_enc->parent_ops.get_qsync_fps)
  752. phys_enc->parent_ops.get_qsync_fps(
  753. phys_enc->parent, &qsync_min_fps);
  754. if (!qsync_min_fps || !default_fps || !yres) {
  755. SDE_ERROR_CMDENC(cmd_enc,
  756. "wrong qsync params %d %d %d\n",
  757. qsync_min_fps, default_fps, yres);
  758. goto exit;
  759. }
  760. if (qsync_min_fps >= default_fps) {
  761. SDE_ERROR_CMDENC(cmd_enc,
  762. "qsync fps:%d must be less than default:%d\n",
  763. qsync_min_fps, default_fps);
  764. goto exit;
  765. }
  766. /* Calculate the number of extra lines*/
  767. slow_time_ns = (1 * 1000000000) / qsync_min_fps;
  768. default_time_ns = (1 * 1000000000) / default_fps;
  769. extra_time_ns = slow_time_ns - default_time_ns;
  770. default_line_time_ns = (1 * 1000000000) / (default_fps * yres);
  771. total_extra_lines = extra_time_ns / default_line_time_ns;
  772. threshold_lines += total_extra_lines;
  773. SDE_DEBUG_CMDENC(cmd_enc, "slow:%d default:%d extra:%d(ns)\n",
  774. slow_time_ns, default_time_ns, extra_time_ns);
  775. SDE_DEBUG_CMDENC(cmd_enc, "extra_lines:%d threshold:%d\n",
  776. total_extra_lines, threshold_lines);
  777. SDE_DEBUG_CMDENC(cmd_enc, "min_fps:%d fps:%d yres:%d\n",
  778. qsync_min_fps, default_fps, yres);
  779. SDE_EVT32(qsync_mode, qsync_min_fps, extra_time_ns, default_fps,
  780. yres, threshold_lines);
  781. *extra_frame_trigger_time = extra_time_ns;
  782. }
  783. exit:
  784. threshold_lines += DEFAULT_TEARCHECK_SYNC_THRESH_START;
  785. return threshold_lines;
  786. }
  787. static void sde_encoder_phys_cmd_tearcheck_config(
  788. struct sde_encoder_phys *phys_enc)
  789. {
  790. struct sde_encoder_phys_cmd *cmd_enc =
  791. to_sde_encoder_phys_cmd(phys_enc);
  792. struct sde_hw_tear_check tc_cfg = { 0 };
  793. struct drm_display_mode *mode;
  794. bool tc_enable = true;
  795. u32 vsync_hz, extra_frame_trigger_time;
  796. struct msm_drm_private *priv;
  797. struct sde_kms *sde_kms;
  798. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf) {
  799. SDE_ERROR("invalid encoder\n");
  800. return;
  801. }
  802. mode = &phys_enc->cached_mode;
  803. SDE_DEBUG_CMDENC(cmd_enc, "pp %d, intf %d\n",
  804. phys_enc->hw_pp->idx - PINGPONG_0,
  805. phys_enc->hw_intf->idx - INTF_0);
  806. if (phys_enc->has_intf_te) {
  807. if (!phys_enc->hw_intf->ops.setup_tearcheck ||
  808. !phys_enc->hw_intf->ops.enable_tearcheck) {
  809. SDE_DEBUG_CMDENC(cmd_enc, "tearcheck not supported\n");
  810. return;
  811. }
  812. } else {
  813. if (!phys_enc->hw_pp->ops.setup_tearcheck ||
  814. !phys_enc->hw_pp->ops.enable_tearcheck) {
  815. SDE_DEBUG_CMDENC(cmd_enc, "tearcheck not supported\n");
  816. return;
  817. }
  818. }
  819. sde_kms = phys_enc->sde_kms;
  820. if (!sde_kms || !sde_kms->dev || !sde_kms->dev->dev_private) {
  821. SDE_ERROR("invalid device\n");
  822. return;
  823. }
  824. priv = sde_kms->dev->dev_private;
  825. /*
  826. * TE default: dsi byte clock calculated base on 70 fps;
  827. * around 14 ms to complete a kickoff cycle if te disabled;
  828. * vclk_line base on 60 fps; write is faster than read;
  829. * init == start == rdptr;
  830. *
  831. * vsync_count is ratio of MDP VSYNC clock frequency to LCD panel
  832. * frequency divided by the no. of rows (lines) in the LCDpanel.
  833. */
  834. vsync_hz = sde_power_clk_get_rate(&priv->phandle, "vsync_clk");
  835. if (!vsync_hz || !mode->vtotal || !mode->vrefresh) {
  836. SDE_DEBUG_CMDENC(cmd_enc,
  837. "invalid params - vsync_hz %u vtot %u vrefresh %u\n",
  838. vsync_hz, mode->vtotal, mode->vrefresh);
  839. return;
  840. }
  841. tc_cfg.vsync_count = vsync_hz / (mode->vtotal * mode->vrefresh);
  842. /* enable external TE after kickoff to avoid premature autorefresh */
  843. tc_cfg.hw_vsync_mode = 0;
  844. /*
  845. * By setting sync_cfg_height to near max register value, we essentially
  846. * disable sde hw generated TE signal, since hw TE will arrive first.
  847. * Only caveat is if due to error, we hit wrap-around.
  848. */
  849. tc_cfg.sync_cfg_height = 0xFFF0;
  850. tc_cfg.vsync_init_val = mode->vdisplay;
  851. tc_cfg.sync_threshold_start = _get_tearcheck_threshold(phys_enc,
  852. &extra_frame_trigger_time);
  853. tc_cfg.sync_threshold_continue = DEFAULT_TEARCHECK_SYNC_THRESH_CONTINUE;
  854. tc_cfg.start_pos = mode->vdisplay;
  855. tc_cfg.rd_ptr_irq = mode->vdisplay + 1;
  856. cmd_enc->ctl_start_threshold = (extra_frame_trigger_time / 1000) +
  857. SDE_ENC_CTL_START_THRESHOLD_US;
  858. SDE_DEBUG_CMDENC(cmd_enc,
  859. "tc %d intf %d vsync_clk_speed_hz %u vtotal %u vrefresh %u\n",
  860. phys_enc->hw_pp->idx - PINGPONG_0,
  861. phys_enc->hw_intf->idx - INTF_0,
  862. vsync_hz, mode->vtotal, mode->vrefresh);
  863. SDE_DEBUG_CMDENC(cmd_enc,
  864. "tc %d intf %d enable %u start_pos %u rd_ptr_irq %u\n",
  865. phys_enc->hw_pp->idx - PINGPONG_0,
  866. phys_enc->hw_intf->idx - INTF_0,
  867. tc_enable, tc_cfg.start_pos, tc_cfg.rd_ptr_irq);
  868. SDE_DEBUG_CMDENC(cmd_enc,
  869. "tc %d intf %d hw_vsync_mode %u vsync_count %u vsync_init_val %u\n",
  870. phys_enc->hw_pp->idx - PINGPONG_0,
  871. phys_enc->hw_intf->idx - INTF_0,
  872. tc_cfg.hw_vsync_mode, tc_cfg.vsync_count,
  873. tc_cfg.vsync_init_val);
  874. SDE_DEBUG_CMDENC(cmd_enc,
  875. "tc %d intf %d cfgheight %u thresh_start %u thresh_cont %u ctl_start_threshold:%d\n",
  876. phys_enc->hw_pp->idx - PINGPONG_0,
  877. phys_enc->hw_intf->idx - INTF_0,
  878. tc_cfg.sync_cfg_height,
  879. tc_cfg.sync_threshold_start, tc_cfg.sync_threshold_continue,
  880. cmd_enc->ctl_start_threshold);
  881. if (phys_enc->has_intf_te) {
  882. phys_enc->hw_intf->ops.setup_tearcheck(phys_enc->hw_intf,
  883. &tc_cfg);
  884. phys_enc->hw_intf->ops.enable_tearcheck(phys_enc->hw_intf,
  885. tc_enable);
  886. } else {
  887. phys_enc->hw_pp->ops.setup_tearcheck(phys_enc->hw_pp, &tc_cfg);
  888. phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp,
  889. tc_enable);
  890. }
  891. }
  892. static void _sde_encoder_phys_cmd_pingpong_config(
  893. struct sde_encoder_phys *phys_enc)
  894. {
  895. struct sde_encoder_phys_cmd *cmd_enc =
  896. to_sde_encoder_phys_cmd(phys_enc);
  897. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
  898. SDE_ERROR("invalid arg(s), enc %d\n", !phys_enc);
  899. return;
  900. }
  901. SDE_DEBUG_CMDENC(cmd_enc, "pp %d, enabling mode:\n",
  902. phys_enc->hw_pp->idx - PINGPONG_0);
  903. drm_mode_debug_printmodeline(&phys_enc->cached_mode);
  904. if (!_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  905. _sde_encoder_phys_cmd_update_intf_cfg(phys_enc);
  906. sde_encoder_phys_cmd_tearcheck_config(phys_enc);
  907. }
  908. static void sde_encoder_phys_cmd_enable_helper(
  909. struct sde_encoder_phys *phys_enc)
  910. {
  911. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
  912. SDE_ERROR("invalid arg(s), encoder %d\n", !phys_enc);
  913. return;
  914. }
  915. sde_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
  916. _sde_encoder_phys_cmd_pingpong_config(phys_enc);
  917. /*
  918. * For pp-split, skip setting the flush bit for the slave intf, since
  919. * both intfs use same ctl and HW will only flush the master.
  920. */
  921. if (_sde_encoder_phys_is_ppsplit(phys_enc) &&
  922. !sde_encoder_phys_cmd_is_master(phys_enc))
  923. goto skip_flush;
  924. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  925. skip_flush:
  926. return;
  927. }
  928. static void sde_encoder_phys_cmd_enable(struct sde_encoder_phys *phys_enc)
  929. {
  930. struct sde_encoder_phys_cmd *cmd_enc =
  931. to_sde_encoder_phys_cmd(phys_enc);
  932. if (!phys_enc || !phys_enc->hw_pp) {
  933. SDE_ERROR("invalid phys encoder\n");
  934. return;
  935. }
  936. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  937. if (phys_enc->enable_state == SDE_ENC_ENABLED) {
  938. if (!phys_enc->cont_splash_enabled)
  939. SDE_ERROR("already enabled\n");
  940. return;
  941. }
  942. sde_encoder_phys_cmd_enable_helper(phys_enc);
  943. phys_enc->enable_state = SDE_ENC_ENABLED;
  944. }
  945. static bool sde_encoder_phys_cmd_is_autorefresh_enabled(
  946. struct sde_encoder_phys *phys_enc)
  947. {
  948. struct sde_hw_pingpong *hw_pp;
  949. struct sde_hw_intf *hw_intf;
  950. struct sde_hw_autorefresh cfg;
  951. int ret;
  952. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  953. return false;
  954. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  955. return false;
  956. if (phys_enc->has_intf_te) {
  957. hw_intf = phys_enc->hw_intf;
  958. if (!hw_intf->ops.get_autorefresh)
  959. return false;
  960. ret = hw_intf->ops.get_autorefresh(hw_intf, &cfg);
  961. } else {
  962. hw_pp = phys_enc->hw_pp;
  963. if (!hw_pp->ops.get_autorefresh)
  964. return false;
  965. ret = hw_pp->ops.get_autorefresh(hw_pp, &cfg);
  966. }
  967. if (ret)
  968. return false;
  969. return cfg.enable;
  970. }
  971. static void sde_encoder_phys_cmd_connect_te(
  972. struct sde_encoder_phys *phys_enc, bool enable)
  973. {
  974. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  975. return;
  976. if (phys_enc->has_intf_te &&
  977. phys_enc->hw_intf->ops.connect_external_te)
  978. phys_enc->hw_intf->ops.connect_external_te(phys_enc->hw_intf,
  979. enable);
  980. else if (phys_enc->hw_pp->ops.connect_external_te)
  981. phys_enc->hw_pp->ops.connect_external_te(phys_enc->hw_pp,
  982. enable);
  983. else
  984. return;
  985. SDE_EVT32(DRMID(phys_enc->parent), enable);
  986. }
  987. static int sde_encoder_phys_cmd_te_get_line_count(
  988. struct sde_encoder_phys *phys_enc)
  989. {
  990. struct sde_hw_pingpong *hw_pp;
  991. struct sde_hw_intf *hw_intf;
  992. u32 line_count;
  993. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  994. return -EINVAL;
  995. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  996. return -EINVAL;
  997. if (phys_enc->has_intf_te) {
  998. hw_intf = phys_enc->hw_intf;
  999. if (!hw_intf->ops.get_line_count)
  1000. return -EINVAL;
  1001. line_count = hw_intf->ops.get_line_count(hw_intf);
  1002. } else {
  1003. hw_pp = phys_enc->hw_pp;
  1004. if (!hw_pp->ops.get_line_count)
  1005. return -EINVAL;
  1006. line_count = hw_pp->ops.get_line_count(hw_pp);
  1007. }
  1008. return line_count;
  1009. }
  1010. static int sde_encoder_phys_cmd_get_write_line_count(
  1011. struct sde_encoder_phys *phys_enc)
  1012. {
  1013. struct sde_hw_pingpong *hw_pp;
  1014. struct sde_hw_intf *hw_intf;
  1015. struct sde_hw_pp_vsync_info info;
  1016. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1017. return -EINVAL;
  1018. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1019. return -EINVAL;
  1020. if (phys_enc->has_intf_te) {
  1021. hw_intf = phys_enc->hw_intf;
  1022. if (!hw_intf->ops.get_vsync_info)
  1023. return -EINVAL;
  1024. if (hw_intf->ops.get_vsync_info(hw_intf, &info))
  1025. return -EINVAL;
  1026. } else {
  1027. hw_pp = phys_enc->hw_pp;
  1028. if (!hw_pp->ops.get_vsync_info)
  1029. return -EINVAL;
  1030. if (hw_pp->ops.get_vsync_info(hw_pp, &info))
  1031. return -EINVAL;
  1032. }
  1033. return (int)info.wr_ptr_line_count;
  1034. }
  1035. static void sde_encoder_phys_cmd_disable(struct sde_encoder_phys *phys_enc)
  1036. {
  1037. struct sde_encoder_phys_cmd *cmd_enc =
  1038. to_sde_encoder_phys_cmd(phys_enc);
  1039. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf) {
  1040. SDE_ERROR("invalid encoder\n");
  1041. return;
  1042. }
  1043. SDE_DEBUG_CMDENC(cmd_enc, "pp %d intf %d state %d\n",
  1044. phys_enc->hw_pp->idx - PINGPONG_0,
  1045. phys_enc->hw_intf->idx - INTF_0,
  1046. phys_enc->enable_state);
  1047. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1048. phys_enc->hw_intf->idx - INTF_0,
  1049. phys_enc->enable_state);
  1050. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1051. SDE_ERROR_CMDENC(cmd_enc, "already disabled\n");
  1052. return;
  1053. }
  1054. if (phys_enc->has_intf_te && phys_enc->hw_intf->ops.enable_tearcheck)
  1055. phys_enc->hw_intf->ops.enable_tearcheck(
  1056. phys_enc->hw_intf,
  1057. false);
  1058. else if (phys_enc->hw_pp->ops.enable_tearcheck)
  1059. phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp,
  1060. false);
  1061. phys_enc->enable_state = SDE_ENC_DISABLED;
  1062. }
  1063. static void sde_encoder_phys_cmd_destroy(struct sde_encoder_phys *phys_enc)
  1064. {
  1065. struct sde_encoder_phys_cmd *cmd_enc =
  1066. to_sde_encoder_phys_cmd(phys_enc);
  1067. if (!phys_enc) {
  1068. SDE_ERROR("invalid encoder\n");
  1069. return;
  1070. }
  1071. kfree(cmd_enc);
  1072. }
  1073. static void sde_encoder_phys_cmd_get_hw_resources(
  1074. struct sde_encoder_phys *phys_enc,
  1075. struct sde_encoder_hw_resources *hw_res,
  1076. struct drm_connector_state *conn_state)
  1077. {
  1078. struct sde_encoder_phys_cmd *cmd_enc =
  1079. to_sde_encoder_phys_cmd(phys_enc);
  1080. if (!phys_enc) {
  1081. SDE_ERROR("invalid encoder\n");
  1082. return;
  1083. }
  1084. if ((phys_enc->intf_idx - INTF_0) >= INTF_MAX) {
  1085. SDE_ERROR("invalid intf idx:%d\n", phys_enc->intf_idx);
  1086. return;
  1087. }
  1088. SDE_DEBUG_CMDENC(cmd_enc, "\n");
  1089. hw_res->intfs[phys_enc->intf_idx - INTF_0] = INTF_MODE_CMD;
  1090. }
  1091. static int sde_encoder_phys_cmd_prepare_for_kickoff(
  1092. struct sde_encoder_phys *phys_enc,
  1093. struct sde_encoder_kickoff_params *params)
  1094. {
  1095. struct sde_hw_tear_check tc_cfg = {0};
  1096. struct sde_encoder_phys_cmd *cmd_enc =
  1097. to_sde_encoder_phys_cmd(phys_enc);
  1098. int ret = 0;
  1099. u32 extra_frame_trigger_time;
  1100. if (!phys_enc || !phys_enc->hw_pp) {
  1101. SDE_ERROR("invalid encoder\n");
  1102. return -EINVAL;
  1103. }
  1104. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  1105. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1106. atomic_read(&phys_enc->pending_kickoff_cnt),
  1107. atomic_read(&cmd_enc->autorefresh.kickoff_cnt));
  1108. if (phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_DEFAULT) {
  1109. /*
  1110. * Mark kickoff request as outstanding. If there are more
  1111. * than one outstanding frame, then we have to wait for the
  1112. * previous frame to complete
  1113. */
  1114. ret = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1115. if (ret) {
  1116. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1117. SDE_EVT32(DRMID(phys_enc->parent),
  1118. phys_enc->hw_pp->idx - PINGPONG_0);
  1119. SDE_ERROR("failed wait_for_idle: %d\n", ret);
  1120. }
  1121. }
  1122. if (sde_connector_is_qsync_updated(phys_enc->connector)) {
  1123. tc_cfg.sync_threshold_start =
  1124. _get_tearcheck_threshold(phys_enc,
  1125. &extra_frame_trigger_time);
  1126. if (phys_enc->has_intf_te &&
  1127. phys_enc->hw_intf->ops.update_tearcheck)
  1128. phys_enc->hw_intf->ops.update_tearcheck(
  1129. phys_enc->hw_intf, &tc_cfg);
  1130. else if (phys_enc->hw_pp->ops.update_tearcheck)
  1131. phys_enc->hw_pp->ops.update_tearcheck(
  1132. phys_enc->hw_pp, &tc_cfg);
  1133. cmd_enc->ctl_start_threshold =
  1134. (extra_frame_trigger_time / 1000) +
  1135. SDE_ENC_CTL_START_THRESHOLD_US;
  1136. SDE_EVT32(DRMID(phys_enc->parent),
  1137. tc_cfg.sync_threshold_start, cmd_enc->ctl_start_threshold);
  1138. }
  1139. SDE_DEBUG_CMDENC(cmd_enc, "pp:%d pending_cnt %d\n",
  1140. phys_enc->hw_pp->idx - PINGPONG_0,
  1141. atomic_read(&phys_enc->pending_kickoff_cnt));
  1142. return ret;
  1143. }
  1144. static int _sde_encoder_phys_cmd_wait_for_ctl_start(
  1145. struct sde_encoder_phys *phys_enc)
  1146. {
  1147. struct sde_encoder_phys_cmd *cmd_enc =
  1148. to_sde_encoder_phys_cmd(phys_enc);
  1149. struct sde_encoder_wait_info wait_info;
  1150. int ret;
  1151. bool frame_pending = true;
  1152. struct sde_hw_ctl *ctl;
  1153. if (!phys_enc || !phys_enc->hw_ctl) {
  1154. SDE_ERROR("invalid argument(s)\n");
  1155. return -EINVAL;
  1156. }
  1157. ctl = phys_enc->hw_ctl;
  1158. wait_info.wq = &phys_enc->pending_kickoff_wq;
  1159. wait_info.atomic_cnt = &phys_enc->pending_ctlstart_cnt;
  1160. wait_info.timeout_ms = KICKOFF_TIMEOUT_MS;
  1161. /* slave encoder doesn't enable for ppsplit */
  1162. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  1163. return 0;
  1164. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_CTL_START,
  1165. &wait_info);
  1166. if (ret == -ETIMEDOUT) {
  1167. struct sde_hw_ctl *ctl = phys_enc->hw_ctl;
  1168. if (ctl && ctl->ops.get_start_state)
  1169. frame_pending = ctl->ops.get_start_state(ctl);
  1170. if (frame_pending)
  1171. SDE_ERROR_CMDENC(cmd_enc,
  1172. "ctl start interrupt wait failed\n");
  1173. else
  1174. ret = 0;
  1175. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  1176. /*
  1177. * Signaling the retire fence at ctl start timeout
  1178. * to allow the next commit and avoid device freeze.
  1179. * As ctl start timeout can occurs due to no read ptr,
  1180. * updating pending_rd_ptr_cnt here may not cover all
  1181. * cases. Hence signaling the retire fence.
  1182. */
  1183. if (atomic_add_unless(
  1184. &phys_enc->pending_retire_fence_cnt, -1, 0))
  1185. phys_enc->parent_ops.handle_frame_done(
  1186. phys_enc->parent,
  1187. phys_enc,
  1188. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  1189. atomic_add_unless(
  1190. &phys_enc->pending_ctlstart_cnt, -1, 0);
  1191. }
  1192. } else if ((ret == 0) &&
  1193. (phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_POSTED_START) &&
  1194. atomic_read(&phys_enc->pending_kickoff_cnt) &&
  1195. ctl->ops.get_scheduler_status &&
  1196. (ctl->ops.get_scheduler_status(ctl) & BIT(0)) &&
  1197. phys_enc->parent_ops.handle_frame_done) {
  1198. atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
  1199. phys_enc->parent_ops.handle_frame_done(
  1200. phys_enc->parent, phys_enc,
  1201. SDE_ENCODER_FRAME_EVENT_DONE |
  1202. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE);
  1203. }
  1204. return ret;
  1205. }
  1206. static int sde_encoder_phys_cmd_wait_for_tx_complete(
  1207. struct sde_encoder_phys *phys_enc)
  1208. {
  1209. int rc;
  1210. struct sde_encoder_phys_cmd *cmd_enc;
  1211. if (!phys_enc)
  1212. return -EINVAL;
  1213. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1214. rc = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1215. if (rc) {
  1216. SDE_EVT32(DRMID(phys_enc->parent),
  1217. phys_enc->intf_idx - INTF_0);
  1218. SDE_ERROR("failed wait_for_idle: %d\n", rc);
  1219. }
  1220. return rc;
  1221. }
  1222. static int sde_encoder_phys_cmd_wait_for_commit_done(
  1223. struct sde_encoder_phys *phys_enc)
  1224. {
  1225. int rc = 0;
  1226. struct sde_encoder_phys_cmd *cmd_enc;
  1227. if (!phys_enc)
  1228. return -EINVAL;
  1229. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1230. /* only required for master controller */
  1231. if (sde_encoder_phys_cmd_is_master(phys_enc))
  1232. rc = _sde_encoder_phys_cmd_wait_for_ctl_start(phys_enc);
  1233. if (!rc && sde_encoder_phys_cmd_is_master(phys_enc) &&
  1234. cmd_enc->autorefresh.cfg.enable)
  1235. rc = _sde_encoder_phys_cmd_wait_for_autorefresh_done(phys_enc);
  1236. /* wait for posted start or serialize trigger */
  1237. if ((atomic_read(&phys_enc->pending_kickoff_cnt) > 1) ||
  1238. (!rc && phys_enc->frame_trigger_mode ==
  1239. FRAME_DONE_WAIT_SERIALIZE)) {
  1240. rc = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1241. if (rc) {
  1242. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1243. SDE_EVT32(DRMID(phys_enc->parent),
  1244. phys_enc->hw_pp->idx - PINGPONG_0);
  1245. SDE_ERROR("failed wait_for_idle: %d\n", rc);
  1246. }
  1247. }
  1248. return rc;
  1249. }
  1250. static int sde_encoder_phys_cmd_wait_for_vblank(
  1251. struct sde_encoder_phys *phys_enc)
  1252. {
  1253. int rc = 0;
  1254. struct sde_encoder_phys_cmd *cmd_enc;
  1255. struct sde_encoder_wait_info wait_info;
  1256. if (!phys_enc)
  1257. return -EINVAL;
  1258. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1259. /* only required for master controller */
  1260. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1261. return rc;
  1262. wait_info.wq = &cmd_enc->pending_vblank_wq;
  1263. wait_info.atomic_cnt = &cmd_enc->pending_vblank_cnt;
  1264. wait_info.timeout_ms = _sde_encoder_phys_cmd_get_idle_timeout(cmd_enc);
  1265. atomic_inc(&cmd_enc->pending_vblank_cnt);
  1266. rc = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_RDPTR,
  1267. &wait_info);
  1268. return rc;
  1269. }
  1270. static void sde_encoder_phys_cmd_update_split_role(
  1271. struct sde_encoder_phys *phys_enc,
  1272. enum sde_enc_split_role role)
  1273. {
  1274. struct sde_encoder_phys_cmd *cmd_enc;
  1275. enum sde_enc_split_role old_role;
  1276. bool is_ppsplit;
  1277. if (!phys_enc)
  1278. return;
  1279. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1280. old_role = phys_enc->split_role;
  1281. is_ppsplit = _sde_encoder_phys_is_ppsplit(phys_enc);
  1282. phys_enc->split_role = role;
  1283. SDE_DEBUG_CMDENC(cmd_enc, "old role %d new role %d\n",
  1284. old_role, role);
  1285. /*
  1286. * ppsplit solo needs to reprogram because intf may have swapped without
  1287. * role changing on left-only, right-only back-to-back commits
  1288. */
  1289. if (!(is_ppsplit && role == ENC_ROLE_SOLO) &&
  1290. (role == old_role || role == ENC_ROLE_SKIP))
  1291. return;
  1292. sde_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
  1293. _sde_encoder_phys_cmd_pingpong_config(phys_enc);
  1294. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  1295. }
  1296. static void sde_encoder_phys_cmd_prepare_commit(
  1297. struct sde_encoder_phys *phys_enc)
  1298. {
  1299. struct sde_encoder_phys_cmd *cmd_enc =
  1300. to_sde_encoder_phys_cmd(phys_enc);
  1301. int trial = 0;
  1302. if (!phys_enc)
  1303. return;
  1304. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1305. return;
  1306. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1307. cmd_enc->autorefresh.cfg.enable);
  1308. if (!sde_encoder_phys_cmd_is_autorefresh_enabled(phys_enc))
  1309. return;
  1310. /*
  1311. * If autorefresh is enabled, disable it and make sure it is safe to
  1312. * proceed with current frame commit/push. Sequence fallowed is,
  1313. * 1. Disable TE
  1314. * 2. Disable autorefresh config
  1315. * 4. Poll for frame transfer ongoing to be false
  1316. * 5. Enable TE back
  1317. */
  1318. sde_encoder_phys_cmd_connect_te(phys_enc, false);
  1319. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, 0);
  1320. do {
  1321. udelay(SDE_ENC_MAX_POLL_TIMEOUT_US);
  1322. if ((trial * SDE_ENC_MAX_POLL_TIMEOUT_US)
  1323. > (KICKOFF_TIMEOUT_MS * USEC_PER_MSEC)) {
  1324. SDE_ERROR_CMDENC(cmd_enc,
  1325. "disable autorefresh failed\n");
  1326. break;
  1327. }
  1328. trial++;
  1329. } while (_sde_encoder_phys_cmd_is_ongoing_pptx(phys_enc));
  1330. sde_encoder_phys_cmd_connect_te(phys_enc, true);
  1331. SDE_DEBUG_CMDENC(cmd_enc, "disabled autorefresh\n");
  1332. }
  1333. static void sde_encoder_phys_cmd_trigger_start(
  1334. struct sde_encoder_phys *phys_enc)
  1335. {
  1336. struct sde_encoder_phys_cmd *cmd_enc =
  1337. to_sde_encoder_phys_cmd(phys_enc);
  1338. u32 frame_cnt;
  1339. if (!phys_enc)
  1340. return;
  1341. /* we don't issue CTL_START when using autorefresh */
  1342. frame_cnt = _sde_encoder_phys_cmd_get_autorefresh_property(phys_enc);
  1343. if (frame_cnt) {
  1344. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, frame_cnt);
  1345. atomic_inc(&cmd_enc->autorefresh.kickoff_cnt);
  1346. } else {
  1347. sde_encoder_helper_trigger_start(phys_enc);
  1348. }
  1349. }
  1350. static void sde_encoder_phys_cmd_setup_vsync_source(
  1351. struct sde_encoder_phys *phys_enc,
  1352. u32 vsync_source, bool is_dummy)
  1353. {
  1354. if (!phys_enc || !phys_enc->hw_intf)
  1355. return;
  1356. sde_encoder_helper_vsync_config(phys_enc, vsync_source, is_dummy);
  1357. if (phys_enc->has_intf_te && phys_enc->hw_intf->ops.vsync_sel)
  1358. phys_enc->hw_intf->ops.vsync_sel(phys_enc->hw_intf,
  1359. vsync_source);
  1360. }
  1361. static void sde_encoder_phys_cmd_init_ops(struct sde_encoder_phys_ops *ops)
  1362. {
  1363. ops->prepare_commit = sde_encoder_phys_cmd_prepare_commit;
  1364. ops->is_master = sde_encoder_phys_cmd_is_master;
  1365. ops->mode_set = sde_encoder_phys_cmd_mode_set;
  1366. ops->cont_splash_mode_set = sde_encoder_phys_cmd_cont_splash_mode_set;
  1367. ops->mode_fixup = sde_encoder_phys_cmd_mode_fixup;
  1368. ops->enable = sde_encoder_phys_cmd_enable;
  1369. ops->disable = sde_encoder_phys_cmd_disable;
  1370. ops->destroy = sde_encoder_phys_cmd_destroy;
  1371. ops->get_hw_resources = sde_encoder_phys_cmd_get_hw_resources;
  1372. ops->control_vblank_irq = sde_encoder_phys_cmd_control_vblank_irq;
  1373. ops->wait_for_commit_done = sde_encoder_phys_cmd_wait_for_commit_done;
  1374. ops->prepare_for_kickoff = sde_encoder_phys_cmd_prepare_for_kickoff;
  1375. ops->wait_for_tx_complete = sde_encoder_phys_cmd_wait_for_tx_complete;
  1376. ops->wait_for_vblank = sde_encoder_phys_cmd_wait_for_vblank;
  1377. ops->trigger_flush = sde_encoder_helper_trigger_flush;
  1378. ops->trigger_start = sde_encoder_phys_cmd_trigger_start;
  1379. ops->needs_single_flush = sde_encoder_phys_needs_single_flush;
  1380. ops->hw_reset = sde_encoder_helper_hw_reset;
  1381. ops->irq_control = sde_encoder_phys_cmd_irq_control;
  1382. ops->update_split_role = sde_encoder_phys_cmd_update_split_role;
  1383. ops->restore = sde_encoder_phys_cmd_enable_helper;
  1384. ops->control_te = sde_encoder_phys_cmd_connect_te;
  1385. ops->is_autorefresh_enabled =
  1386. sde_encoder_phys_cmd_is_autorefresh_enabled;
  1387. ops->get_line_count = sde_encoder_phys_cmd_te_get_line_count;
  1388. ops->get_wr_line_count = sde_encoder_phys_cmd_get_write_line_count;
  1389. ops->wait_for_active = NULL;
  1390. ops->setup_vsync_source = sde_encoder_phys_cmd_setup_vsync_source;
  1391. ops->setup_misr = sde_encoder_helper_setup_misr;
  1392. ops->collect_misr = sde_encoder_helper_collect_misr;
  1393. }
  1394. struct sde_encoder_phys *sde_encoder_phys_cmd_init(
  1395. struct sde_enc_phys_init_params *p)
  1396. {
  1397. struct sde_encoder_phys *phys_enc = NULL;
  1398. struct sde_encoder_phys_cmd *cmd_enc = NULL;
  1399. struct sde_hw_mdp *hw_mdp;
  1400. struct sde_encoder_irq *irq;
  1401. int i, ret = 0;
  1402. SDE_DEBUG("intf %d\n", p->intf_idx - INTF_0);
  1403. cmd_enc = kzalloc(sizeof(*cmd_enc), GFP_KERNEL);
  1404. if (!cmd_enc) {
  1405. ret = -ENOMEM;
  1406. SDE_ERROR("failed to allocate\n");
  1407. goto fail;
  1408. }
  1409. phys_enc = &cmd_enc->base;
  1410. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  1411. if (IS_ERR_OR_NULL(hw_mdp)) {
  1412. ret = PTR_ERR(hw_mdp);
  1413. SDE_ERROR("failed to get mdptop\n");
  1414. goto fail_mdp_init;
  1415. }
  1416. phys_enc->hw_mdptop = hw_mdp;
  1417. phys_enc->intf_idx = p->intf_idx;
  1418. phys_enc->parent = p->parent;
  1419. phys_enc->parent_ops = p->parent_ops;
  1420. phys_enc->sde_kms = p->sde_kms;
  1421. phys_enc->split_role = p->split_role;
  1422. phys_enc->intf_mode = INTF_MODE_CMD;
  1423. phys_enc->enc_spinlock = p->enc_spinlock;
  1424. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  1425. cmd_enc->stream_sel = 0;
  1426. cmd_enc->ctl_start_threshold = SDE_ENC_CTL_START_THRESHOLD_US;
  1427. phys_enc->enable_state = SDE_ENC_DISABLED;
  1428. sde_encoder_phys_cmd_init_ops(&phys_enc->ops);
  1429. phys_enc->comp_type = p->comp_type;
  1430. if (sde_hw_intf_te_supported(phys_enc->sde_kms->catalog))
  1431. phys_enc->has_intf_te = true;
  1432. else
  1433. phys_enc->has_intf_te = false;
  1434. for (i = 0; i < INTR_IDX_MAX; i++) {
  1435. irq = &phys_enc->irq[i];
  1436. INIT_LIST_HEAD(&irq->cb.list);
  1437. irq->irq_idx = -EINVAL;
  1438. irq->hw_idx = -EINVAL;
  1439. irq->cb.arg = phys_enc;
  1440. }
  1441. irq = &phys_enc->irq[INTR_IDX_CTL_START];
  1442. irq->name = "ctl_start";
  1443. irq->intr_type = SDE_IRQ_TYPE_CTL_START;
  1444. irq->intr_idx = INTR_IDX_CTL_START;
  1445. irq->cb.func = sde_encoder_phys_cmd_ctl_start_irq;
  1446. irq = &phys_enc->irq[INTR_IDX_PINGPONG];
  1447. irq->name = "pp_done";
  1448. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_COMP;
  1449. irq->intr_idx = INTR_IDX_PINGPONG;
  1450. irq->cb.func = sde_encoder_phys_cmd_pp_tx_done_irq;
  1451. irq = &phys_enc->irq[INTR_IDX_RDPTR];
  1452. irq->intr_idx = INTR_IDX_RDPTR;
  1453. irq->name = "te_rd_ptr";
  1454. if (phys_enc->has_intf_te)
  1455. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_RD_PTR;
  1456. else
  1457. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_RD_PTR;
  1458. irq->cb.func = sde_encoder_phys_cmd_te_rd_ptr_irq;
  1459. irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
  1460. irq->name = "underrun";
  1461. irq->intr_type = SDE_IRQ_TYPE_INTF_UNDER_RUN;
  1462. irq->intr_idx = INTR_IDX_UNDERRUN;
  1463. irq->cb.func = sde_encoder_phys_cmd_underrun_irq;
  1464. irq = &phys_enc->irq[INTR_IDX_AUTOREFRESH_DONE];
  1465. irq->name = "autorefresh_done";
  1466. if (phys_enc->has_intf_te)
  1467. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_AUTO_REF;
  1468. else
  1469. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_AUTO_REF;
  1470. irq->intr_idx = INTR_IDX_AUTOREFRESH_DONE;
  1471. irq->cb.func = sde_encoder_phys_cmd_autorefresh_done_irq;
  1472. atomic_set(&phys_enc->vblank_refcount, 0);
  1473. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1474. atomic_set(&phys_enc->pending_ctlstart_cnt, 0);
  1475. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  1476. atomic_set(&cmd_enc->pending_rd_ptr_cnt, 0);
  1477. atomic_set(&cmd_enc->pending_vblank_cnt, 0);
  1478. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  1479. init_waitqueue_head(&cmd_enc->pending_vblank_wq);
  1480. atomic_set(&cmd_enc->autorefresh.kickoff_cnt, 0);
  1481. init_waitqueue_head(&cmd_enc->autorefresh.kickoff_wq);
  1482. SDE_DEBUG_CMDENC(cmd_enc, "created\n");
  1483. return phys_enc;
  1484. fail_mdp_init:
  1485. kfree(cmd_enc);
  1486. fail:
  1487. return ERR_PTR(ret);
  1488. }