sde_encoder_phys_cmd.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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. if (sde_kms_is_secure_session_inprogress(phys_enc->sde_kms))
  456. SDE_DBG_DUMP("secure", "all", "dbg_bus");
  457. else
  458. SDE_DBG_DUMP("all", "dbg_bus", "vbif_dbg_bus");
  459. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_RDPTR);
  460. }
  461. /*
  462. * if the recovery event is registered by user, don't panic
  463. * trigger panic on first timeout if no listener registered
  464. */
  465. if (recovery_events) {
  466. event = cmd_enc->pp_timeout_report_cnt > PP_TIMEOUT_MAX_TRIALS ?
  467. SDE_RECOVERY_HARD_RESET : SDE_RECOVERY_CAPTURE;
  468. sde_connector_event_notify(conn, DRM_EVENT_SDE_HW_RECOVERY,
  469. sizeof(uint8_t), event);
  470. } else if (cmd_enc->pp_timeout_report_cnt) {
  471. SDE_DBG_DUMP("panic");
  472. }
  473. /* request a ctl reset before the next kickoff */
  474. phys_enc->enable_state = SDE_ENC_ERR_NEEDS_HW_RESET;
  475. exit:
  476. if (phys_enc->parent_ops.handle_frame_done)
  477. phys_enc->parent_ops.handle_frame_done(
  478. phys_enc->parent, phys_enc, frame_event);
  479. return -ETIMEDOUT;
  480. }
  481. static bool _sde_encoder_phys_is_ppsplit_slave(
  482. struct sde_encoder_phys *phys_enc)
  483. {
  484. if (!phys_enc)
  485. return false;
  486. return _sde_encoder_phys_is_ppsplit(phys_enc) &&
  487. phys_enc->split_role == ENC_ROLE_SLAVE;
  488. }
  489. static bool _sde_encoder_phys_is_disabling_ppsplit_slave(
  490. struct sde_encoder_phys *phys_enc)
  491. {
  492. enum sde_rm_topology_name old_top;
  493. if (!phys_enc || !phys_enc->connector ||
  494. phys_enc->split_role != ENC_ROLE_SLAVE)
  495. return false;
  496. old_top = sde_connector_get_old_topology_name(
  497. phys_enc->connector->state);
  498. return old_top == SDE_RM_TOPOLOGY_PPSPLIT;
  499. }
  500. static int _sde_encoder_phys_cmd_poll_write_pointer_started(
  501. struct sde_encoder_phys *phys_enc)
  502. {
  503. struct sde_encoder_phys_cmd *cmd_enc =
  504. to_sde_encoder_phys_cmd(phys_enc);
  505. struct sde_hw_pingpong *hw_pp = phys_enc->hw_pp;
  506. struct sde_hw_intf *hw_intf = phys_enc->hw_intf;
  507. struct sde_hw_pp_vsync_info info;
  508. u32 timeout_us = SDE_ENC_WR_PTR_START_TIMEOUT_US;
  509. int ret = 0;
  510. if (!hw_pp || !hw_intf)
  511. return 0;
  512. if (phys_enc->has_intf_te) {
  513. if (!hw_intf->ops.get_vsync_info ||
  514. !hw_intf->ops.poll_timeout_wr_ptr)
  515. goto end;
  516. } else {
  517. if (!hw_pp->ops.get_vsync_info ||
  518. !hw_pp->ops.poll_timeout_wr_ptr)
  519. goto end;
  520. }
  521. if (phys_enc->has_intf_te)
  522. ret = hw_intf->ops.get_vsync_info(hw_intf, &info);
  523. else
  524. ret = hw_pp->ops.get_vsync_info(hw_pp, &info);
  525. if (ret)
  526. return ret;
  527. SDE_DEBUG_CMDENC(cmd_enc,
  528. "pp:%d intf:%d rd_ptr %d wr_ptr %d\n",
  529. phys_enc->hw_pp->idx - PINGPONG_0,
  530. phys_enc->hw_intf->idx - INTF_0,
  531. info.rd_ptr_line_count,
  532. info.wr_ptr_line_count);
  533. SDE_EVT32_VERBOSE(DRMID(phys_enc->parent),
  534. phys_enc->hw_pp->idx - PINGPONG_0,
  535. phys_enc->hw_intf->idx - INTF_0,
  536. info.wr_ptr_line_count);
  537. if (phys_enc->has_intf_te)
  538. ret = hw_intf->ops.poll_timeout_wr_ptr(hw_intf, timeout_us);
  539. else
  540. ret = hw_pp->ops.poll_timeout_wr_ptr(hw_pp, timeout_us);
  541. if (ret) {
  542. SDE_EVT32(DRMID(phys_enc->parent),
  543. phys_enc->hw_pp->idx - PINGPONG_0,
  544. phys_enc->hw_intf->idx - INTF_0,
  545. timeout_us,
  546. ret);
  547. SDE_DBG_DUMP("all", "dbg_bus", "vbif_dbg_bus", "panic");
  548. }
  549. end:
  550. return ret;
  551. }
  552. static bool _sde_encoder_phys_cmd_is_ongoing_pptx(
  553. struct sde_encoder_phys *phys_enc)
  554. {
  555. struct sde_hw_pingpong *hw_pp;
  556. struct sde_hw_pp_vsync_info info;
  557. struct sde_hw_intf *hw_intf;
  558. if (!phys_enc)
  559. return false;
  560. if (phys_enc->has_intf_te) {
  561. hw_intf = phys_enc->hw_intf;
  562. if (!hw_intf || !hw_intf->ops.get_vsync_info)
  563. return false;
  564. hw_intf->ops.get_vsync_info(hw_intf, &info);
  565. } else {
  566. hw_pp = phys_enc->hw_pp;
  567. if (!hw_pp || !hw_pp->ops.get_vsync_info)
  568. return false;
  569. hw_pp->ops.get_vsync_info(hw_pp, &info);
  570. }
  571. SDE_EVT32(DRMID(phys_enc->parent),
  572. phys_enc->hw_pp->idx - PINGPONG_0,
  573. phys_enc->hw_intf->idx - INTF_0,
  574. atomic_read(&phys_enc->pending_kickoff_cnt),
  575. info.wr_ptr_line_count,
  576. phys_enc->cached_mode.vdisplay);
  577. if (info.wr_ptr_line_count > 0 && info.wr_ptr_line_count <
  578. phys_enc->cached_mode.vdisplay)
  579. return true;
  580. return false;
  581. }
  582. static int _sde_encoder_phys_cmd_wait_for_idle(
  583. struct sde_encoder_phys *phys_enc)
  584. {
  585. struct sde_encoder_phys_cmd *cmd_enc =
  586. to_sde_encoder_phys_cmd(phys_enc);
  587. struct sde_encoder_wait_info wait_info;
  588. bool recovery_events;
  589. int ret, i, pending_cnt;
  590. if (!phys_enc) {
  591. SDE_ERROR("invalid encoder\n");
  592. return -EINVAL;
  593. }
  594. wait_info.wq = &phys_enc->pending_kickoff_wq;
  595. wait_info.atomic_cnt = &phys_enc->pending_kickoff_cnt;
  596. wait_info.timeout_ms = KICKOFF_TIMEOUT_MS;
  597. recovery_events = sde_encoder_recovery_events_enabled(
  598. phys_enc->parent);
  599. /* slave encoder doesn't enable for ppsplit */
  600. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  601. return 0;
  602. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_PINGPONG,
  603. &wait_info);
  604. if (ret == -ETIMEDOUT) {
  605. pending_cnt = atomic_read(&phys_enc->pending_kickoff_cnt);
  606. for (i = 0; i < pending_cnt; i++)
  607. _sde_encoder_phys_cmd_handle_ppdone_timeout(phys_enc,
  608. recovery_events);
  609. } else if (!ret) {
  610. if (cmd_enc->pp_timeout_report_cnt && recovery_events) {
  611. struct drm_connector *conn = phys_enc->connector;
  612. sde_connector_event_notify(conn,
  613. DRM_EVENT_SDE_HW_RECOVERY,
  614. sizeof(uint8_t),
  615. SDE_RECOVERY_SUCCESS);
  616. }
  617. cmd_enc->pp_timeout_report_cnt = 0;
  618. }
  619. return ret;
  620. }
  621. static int _sde_encoder_phys_cmd_wait_for_autorefresh_done(
  622. struct sde_encoder_phys *phys_enc)
  623. {
  624. struct sde_encoder_phys_cmd *cmd_enc =
  625. to_sde_encoder_phys_cmd(phys_enc);
  626. struct sde_encoder_wait_info wait_info;
  627. int ret = 0;
  628. if (!phys_enc) {
  629. SDE_ERROR("invalid encoder\n");
  630. return -EINVAL;
  631. }
  632. /* only master deals with autorefresh */
  633. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  634. return 0;
  635. wait_info.wq = &cmd_enc->autorefresh.kickoff_wq;
  636. wait_info.atomic_cnt = &cmd_enc->autorefresh.kickoff_cnt;
  637. wait_info.timeout_ms = _sde_encoder_phys_cmd_get_idle_timeout(cmd_enc);
  638. /* wait for autorefresh kickoff to start */
  639. ret = sde_encoder_helper_wait_for_irq(phys_enc,
  640. INTR_IDX_AUTOREFRESH_DONE, &wait_info);
  641. /* double check that kickoff has started by reading write ptr reg */
  642. if (!ret)
  643. ret = _sde_encoder_phys_cmd_poll_write_pointer_started(
  644. phys_enc);
  645. else
  646. sde_encoder_helper_report_irq_timeout(phys_enc,
  647. INTR_IDX_AUTOREFRESH_DONE);
  648. return ret;
  649. }
  650. static int sde_encoder_phys_cmd_control_vblank_irq(
  651. struct sde_encoder_phys *phys_enc,
  652. bool enable)
  653. {
  654. struct sde_encoder_phys_cmd *cmd_enc =
  655. to_sde_encoder_phys_cmd(phys_enc);
  656. int ret = 0;
  657. int refcount;
  658. if (!phys_enc || !phys_enc->hw_pp) {
  659. SDE_ERROR("invalid encoder\n");
  660. return -EINVAL;
  661. }
  662. mutex_lock(phys_enc->vblank_ctl_lock);
  663. refcount = atomic_read(&phys_enc->vblank_refcount);
  664. /* Slave encoders don't report vblank */
  665. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  666. goto end;
  667. /* protect against negative */
  668. if (!enable && refcount == 0) {
  669. ret = -EINVAL;
  670. goto end;
  671. }
  672. SDE_DEBUG_CMDENC(cmd_enc, "[%pS] enable=%d/%d\n",
  673. __builtin_return_address(0), enable, refcount);
  674. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  675. enable, refcount);
  676. if (enable && atomic_inc_return(&phys_enc->vblank_refcount) == 1)
  677. ret = sde_encoder_helper_register_irq(phys_enc, INTR_IDX_RDPTR);
  678. else if (!enable && atomic_dec_return(&phys_enc->vblank_refcount) == 0)
  679. ret = sde_encoder_helper_unregister_irq(phys_enc,
  680. INTR_IDX_RDPTR);
  681. end:
  682. if (ret) {
  683. SDE_ERROR_CMDENC(cmd_enc,
  684. "control vblank irq error %d, enable %d, refcount %d\n",
  685. ret, enable, refcount);
  686. SDE_EVT32(DRMID(phys_enc->parent),
  687. phys_enc->hw_pp->idx - PINGPONG_0,
  688. enable, refcount, SDE_EVTLOG_ERROR);
  689. }
  690. mutex_unlock(phys_enc->vblank_ctl_lock);
  691. return ret;
  692. }
  693. void sde_encoder_phys_cmd_irq_control(struct sde_encoder_phys *phys_enc,
  694. bool enable)
  695. {
  696. struct sde_encoder_phys_cmd *cmd_enc;
  697. if (!phys_enc)
  698. return;
  699. /**
  700. * pingpong split slaves do not register for IRQs
  701. * check old and new topologies
  702. */
  703. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc) ||
  704. _sde_encoder_phys_is_disabling_ppsplit_slave(phys_enc))
  705. return;
  706. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  707. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  708. enable, atomic_read(&phys_enc->vblank_refcount));
  709. if (enable) {
  710. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_PINGPONG);
  711. sde_encoder_helper_register_irq(phys_enc, INTR_IDX_UNDERRUN);
  712. sde_encoder_phys_cmd_control_vblank_irq(phys_enc, true);
  713. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  714. sde_encoder_helper_register_irq(phys_enc,
  715. INTR_IDX_CTL_START);
  716. sde_encoder_helper_register_irq(phys_enc,
  717. INTR_IDX_AUTOREFRESH_DONE);
  718. }
  719. } else {
  720. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  721. sde_encoder_helper_unregister_irq(phys_enc,
  722. INTR_IDX_CTL_START);
  723. sde_encoder_helper_unregister_irq(phys_enc,
  724. INTR_IDX_AUTOREFRESH_DONE);
  725. }
  726. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_UNDERRUN);
  727. sde_encoder_phys_cmd_control_vblank_irq(phys_enc, false);
  728. sde_encoder_helper_unregister_irq(phys_enc, INTR_IDX_PINGPONG);
  729. }
  730. }
  731. static int _get_tearcheck_threshold(struct sde_encoder_phys *phys_enc,
  732. u32 *extra_frame_trigger_time)
  733. {
  734. struct drm_connector *conn = phys_enc->connector;
  735. u32 qsync_mode;
  736. struct drm_display_mode *mode;
  737. u32 threshold_lines = 0;
  738. struct sde_encoder_phys_cmd *cmd_enc =
  739. to_sde_encoder_phys_cmd(phys_enc);
  740. *extra_frame_trigger_time = 0;
  741. if (!conn || !conn->state)
  742. return 0;
  743. mode = &phys_enc->cached_mode;
  744. qsync_mode = sde_connector_get_qsync_mode(conn);
  745. if (mode && (qsync_mode == SDE_RM_QSYNC_CONTINUOUS_MODE)) {
  746. u32 qsync_min_fps = 0;
  747. u32 default_fps = mode->vrefresh;
  748. u32 yres = mode->vtotal;
  749. u32 slow_time_ns;
  750. u32 default_time_ns;
  751. u32 extra_time_ns;
  752. u32 total_extra_lines;
  753. u32 default_line_time_ns;
  754. if (phys_enc->parent_ops.get_qsync_fps)
  755. phys_enc->parent_ops.get_qsync_fps(
  756. phys_enc->parent, &qsync_min_fps);
  757. if (!qsync_min_fps || !default_fps || !yres) {
  758. SDE_ERROR_CMDENC(cmd_enc,
  759. "wrong qsync params %d %d %d\n",
  760. qsync_min_fps, default_fps, yres);
  761. goto exit;
  762. }
  763. if (qsync_min_fps >= default_fps) {
  764. SDE_ERROR_CMDENC(cmd_enc,
  765. "qsync fps:%d must be less than default:%d\n",
  766. qsync_min_fps, default_fps);
  767. goto exit;
  768. }
  769. /* Calculate the number of extra lines*/
  770. slow_time_ns = (1 * 1000000000) / qsync_min_fps;
  771. default_time_ns = (1 * 1000000000) / default_fps;
  772. extra_time_ns = slow_time_ns - default_time_ns;
  773. default_line_time_ns = (1 * 1000000000) / (default_fps * yres);
  774. total_extra_lines = extra_time_ns / default_line_time_ns;
  775. threshold_lines += total_extra_lines;
  776. SDE_DEBUG_CMDENC(cmd_enc, "slow:%d default:%d extra:%d(ns)\n",
  777. slow_time_ns, default_time_ns, extra_time_ns);
  778. SDE_DEBUG_CMDENC(cmd_enc, "extra_lines:%d threshold:%d\n",
  779. total_extra_lines, threshold_lines);
  780. SDE_DEBUG_CMDENC(cmd_enc, "min_fps:%d fps:%d yres:%d\n",
  781. qsync_min_fps, default_fps, yres);
  782. SDE_EVT32(qsync_mode, qsync_min_fps, extra_time_ns, default_fps,
  783. yres, threshold_lines);
  784. *extra_frame_trigger_time = extra_time_ns;
  785. }
  786. exit:
  787. threshold_lines += DEFAULT_TEARCHECK_SYNC_THRESH_START;
  788. return threshold_lines;
  789. }
  790. static void sde_encoder_phys_cmd_tearcheck_config(
  791. struct sde_encoder_phys *phys_enc)
  792. {
  793. struct sde_encoder_phys_cmd *cmd_enc =
  794. to_sde_encoder_phys_cmd(phys_enc);
  795. struct sde_hw_tear_check tc_cfg = { 0 };
  796. struct drm_display_mode *mode;
  797. bool tc_enable = true;
  798. u32 vsync_hz, extra_frame_trigger_time;
  799. struct msm_drm_private *priv;
  800. struct sde_kms *sde_kms;
  801. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf) {
  802. SDE_ERROR("invalid encoder\n");
  803. return;
  804. }
  805. mode = &phys_enc->cached_mode;
  806. SDE_DEBUG_CMDENC(cmd_enc, "pp %d, intf %d\n",
  807. phys_enc->hw_pp->idx - PINGPONG_0,
  808. phys_enc->hw_intf->idx - INTF_0);
  809. if (phys_enc->has_intf_te) {
  810. if (!phys_enc->hw_intf->ops.setup_tearcheck ||
  811. !phys_enc->hw_intf->ops.enable_tearcheck) {
  812. SDE_DEBUG_CMDENC(cmd_enc, "tearcheck not supported\n");
  813. return;
  814. }
  815. } else {
  816. if (!phys_enc->hw_pp->ops.setup_tearcheck ||
  817. !phys_enc->hw_pp->ops.enable_tearcheck) {
  818. SDE_DEBUG_CMDENC(cmd_enc, "tearcheck not supported\n");
  819. return;
  820. }
  821. }
  822. sde_kms = phys_enc->sde_kms;
  823. if (!sde_kms || !sde_kms->dev || !sde_kms->dev->dev_private) {
  824. SDE_ERROR("invalid device\n");
  825. return;
  826. }
  827. priv = sde_kms->dev->dev_private;
  828. /*
  829. * TE default: dsi byte clock calculated base on 70 fps;
  830. * around 14 ms to complete a kickoff cycle if te disabled;
  831. * vclk_line base on 60 fps; write is faster than read;
  832. * init == start == rdptr;
  833. *
  834. * vsync_count is ratio of MDP VSYNC clock frequency to LCD panel
  835. * frequency divided by the no. of rows (lines) in the LCDpanel.
  836. */
  837. vsync_hz = sde_power_clk_get_rate(&priv->phandle, "vsync_clk");
  838. if (!vsync_hz || !mode->vtotal || !mode->vrefresh) {
  839. SDE_DEBUG_CMDENC(cmd_enc,
  840. "invalid params - vsync_hz %u vtot %u vrefresh %u\n",
  841. vsync_hz, mode->vtotal, mode->vrefresh);
  842. return;
  843. }
  844. tc_cfg.vsync_count = vsync_hz / (mode->vtotal * mode->vrefresh);
  845. /* enable external TE after kickoff to avoid premature autorefresh */
  846. tc_cfg.hw_vsync_mode = 0;
  847. /*
  848. * By setting sync_cfg_height to near max register value, we essentially
  849. * disable sde hw generated TE signal, since hw TE will arrive first.
  850. * Only caveat is if due to error, we hit wrap-around.
  851. */
  852. tc_cfg.sync_cfg_height = 0xFFF0;
  853. tc_cfg.vsync_init_val = mode->vdisplay;
  854. tc_cfg.sync_threshold_start = _get_tearcheck_threshold(phys_enc,
  855. &extra_frame_trigger_time);
  856. tc_cfg.sync_threshold_continue = DEFAULT_TEARCHECK_SYNC_THRESH_CONTINUE;
  857. tc_cfg.start_pos = mode->vdisplay;
  858. tc_cfg.rd_ptr_irq = mode->vdisplay + 1;
  859. cmd_enc->ctl_start_threshold = (extra_frame_trigger_time / 1000) +
  860. SDE_ENC_CTL_START_THRESHOLD_US;
  861. SDE_DEBUG_CMDENC(cmd_enc,
  862. "tc %d intf %d vsync_clk_speed_hz %u vtotal %u vrefresh %u\n",
  863. phys_enc->hw_pp->idx - PINGPONG_0,
  864. phys_enc->hw_intf->idx - INTF_0,
  865. vsync_hz, mode->vtotal, mode->vrefresh);
  866. SDE_DEBUG_CMDENC(cmd_enc,
  867. "tc %d intf %d enable %u start_pos %u rd_ptr_irq %u\n",
  868. phys_enc->hw_pp->idx - PINGPONG_0,
  869. phys_enc->hw_intf->idx - INTF_0,
  870. tc_enable, tc_cfg.start_pos, tc_cfg.rd_ptr_irq);
  871. SDE_DEBUG_CMDENC(cmd_enc,
  872. "tc %d intf %d hw_vsync_mode %u vsync_count %u vsync_init_val %u\n",
  873. phys_enc->hw_pp->idx - PINGPONG_0,
  874. phys_enc->hw_intf->idx - INTF_0,
  875. tc_cfg.hw_vsync_mode, tc_cfg.vsync_count,
  876. tc_cfg.vsync_init_val);
  877. SDE_DEBUG_CMDENC(cmd_enc,
  878. "tc %d intf %d cfgheight %u thresh_start %u thresh_cont %u ctl_start_threshold:%d\n",
  879. phys_enc->hw_pp->idx - PINGPONG_0,
  880. phys_enc->hw_intf->idx - INTF_0,
  881. tc_cfg.sync_cfg_height,
  882. tc_cfg.sync_threshold_start, tc_cfg.sync_threshold_continue,
  883. cmd_enc->ctl_start_threshold);
  884. if (phys_enc->has_intf_te) {
  885. phys_enc->hw_intf->ops.setup_tearcheck(phys_enc->hw_intf,
  886. &tc_cfg);
  887. phys_enc->hw_intf->ops.enable_tearcheck(phys_enc->hw_intf,
  888. tc_enable);
  889. } else {
  890. phys_enc->hw_pp->ops.setup_tearcheck(phys_enc->hw_pp, &tc_cfg);
  891. phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp,
  892. tc_enable);
  893. }
  894. }
  895. static void _sde_encoder_phys_cmd_pingpong_config(
  896. struct sde_encoder_phys *phys_enc)
  897. {
  898. struct sde_encoder_phys_cmd *cmd_enc =
  899. to_sde_encoder_phys_cmd(phys_enc);
  900. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
  901. SDE_ERROR("invalid arg(s), enc %d\n", !phys_enc);
  902. return;
  903. }
  904. SDE_DEBUG_CMDENC(cmd_enc, "pp %d, enabling mode:\n",
  905. phys_enc->hw_pp->idx - PINGPONG_0);
  906. drm_mode_debug_printmodeline(&phys_enc->cached_mode);
  907. if (!_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  908. _sde_encoder_phys_cmd_update_intf_cfg(phys_enc);
  909. sde_encoder_phys_cmd_tearcheck_config(phys_enc);
  910. }
  911. static void sde_encoder_phys_cmd_enable_helper(
  912. struct sde_encoder_phys *phys_enc)
  913. {
  914. if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
  915. SDE_ERROR("invalid arg(s), encoder %d\n", !phys_enc);
  916. return;
  917. }
  918. sde_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
  919. _sde_encoder_phys_cmd_pingpong_config(phys_enc);
  920. /*
  921. * For pp-split, skip setting the flush bit for the slave intf, since
  922. * both intfs use same ctl and HW will only flush the master.
  923. */
  924. if (_sde_encoder_phys_is_ppsplit(phys_enc) &&
  925. !sde_encoder_phys_cmd_is_master(phys_enc))
  926. goto skip_flush;
  927. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  928. skip_flush:
  929. return;
  930. }
  931. static void sde_encoder_phys_cmd_enable(struct sde_encoder_phys *phys_enc)
  932. {
  933. struct sde_encoder_phys_cmd *cmd_enc =
  934. to_sde_encoder_phys_cmd(phys_enc);
  935. if (!phys_enc || !phys_enc->hw_pp) {
  936. SDE_ERROR("invalid phys encoder\n");
  937. return;
  938. }
  939. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  940. if (phys_enc->enable_state == SDE_ENC_ENABLED) {
  941. if (!phys_enc->cont_splash_enabled)
  942. SDE_ERROR("already enabled\n");
  943. return;
  944. }
  945. sde_encoder_phys_cmd_enable_helper(phys_enc);
  946. phys_enc->enable_state = SDE_ENC_ENABLED;
  947. }
  948. static bool sde_encoder_phys_cmd_is_autorefresh_enabled(
  949. struct sde_encoder_phys *phys_enc)
  950. {
  951. struct sde_hw_pingpong *hw_pp;
  952. struct sde_hw_intf *hw_intf;
  953. struct sde_hw_autorefresh cfg;
  954. int ret;
  955. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  956. return false;
  957. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  958. return false;
  959. if (phys_enc->has_intf_te) {
  960. hw_intf = phys_enc->hw_intf;
  961. if (!hw_intf->ops.get_autorefresh)
  962. return false;
  963. ret = hw_intf->ops.get_autorefresh(hw_intf, &cfg);
  964. } else {
  965. hw_pp = phys_enc->hw_pp;
  966. if (!hw_pp->ops.get_autorefresh)
  967. return false;
  968. ret = hw_pp->ops.get_autorefresh(hw_pp, &cfg);
  969. }
  970. if (ret)
  971. return false;
  972. return cfg.enable;
  973. }
  974. static void sde_encoder_phys_cmd_connect_te(
  975. struct sde_encoder_phys *phys_enc, bool enable)
  976. {
  977. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  978. return;
  979. if (phys_enc->has_intf_te &&
  980. phys_enc->hw_intf->ops.connect_external_te)
  981. phys_enc->hw_intf->ops.connect_external_te(phys_enc->hw_intf,
  982. enable);
  983. else if (phys_enc->hw_pp->ops.connect_external_te)
  984. phys_enc->hw_pp->ops.connect_external_te(phys_enc->hw_pp,
  985. enable);
  986. else
  987. return;
  988. SDE_EVT32(DRMID(phys_enc->parent), enable);
  989. }
  990. static int sde_encoder_phys_cmd_te_get_line_count(
  991. struct sde_encoder_phys *phys_enc)
  992. {
  993. struct sde_hw_pingpong *hw_pp;
  994. struct sde_hw_intf *hw_intf;
  995. u32 line_count;
  996. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  997. return -EINVAL;
  998. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  999. return -EINVAL;
  1000. if (phys_enc->has_intf_te) {
  1001. hw_intf = phys_enc->hw_intf;
  1002. if (!hw_intf->ops.get_line_count)
  1003. return -EINVAL;
  1004. line_count = hw_intf->ops.get_line_count(hw_intf);
  1005. } else {
  1006. hw_pp = phys_enc->hw_pp;
  1007. if (!hw_pp->ops.get_line_count)
  1008. return -EINVAL;
  1009. line_count = hw_pp->ops.get_line_count(hw_pp);
  1010. }
  1011. return line_count;
  1012. }
  1013. static int sde_encoder_phys_cmd_get_write_line_count(
  1014. struct sde_encoder_phys *phys_enc)
  1015. {
  1016. struct sde_hw_pingpong *hw_pp;
  1017. struct sde_hw_intf *hw_intf;
  1018. struct sde_hw_pp_vsync_info info;
  1019. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf)
  1020. return -EINVAL;
  1021. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1022. return -EINVAL;
  1023. if (phys_enc->has_intf_te) {
  1024. hw_intf = phys_enc->hw_intf;
  1025. if (!hw_intf->ops.get_vsync_info)
  1026. return -EINVAL;
  1027. if (hw_intf->ops.get_vsync_info(hw_intf, &info))
  1028. return -EINVAL;
  1029. } else {
  1030. hw_pp = phys_enc->hw_pp;
  1031. if (!hw_pp->ops.get_vsync_info)
  1032. return -EINVAL;
  1033. if (hw_pp->ops.get_vsync_info(hw_pp, &info))
  1034. return -EINVAL;
  1035. }
  1036. return (int)info.wr_ptr_line_count;
  1037. }
  1038. static void sde_encoder_phys_cmd_disable(struct sde_encoder_phys *phys_enc)
  1039. {
  1040. struct sde_encoder_phys_cmd *cmd_enc =
  1041. to_sde_encoder_phys_cmd(phys_enc);
  1042. if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_intf) {
  1043. SDE_ERROR("invalid encoder\n");
  1044. return;
  1045. }
  1046. SDE_DEBUG_CMDENC(cmd_enc, "pp %d intf %d state %d\n",
  1047. phys_enc->hw_pp->idx - PINGPONG_0,
  1048. phys_enc->hw_intf->idx - INTF_0,
  1049. phys_enc->enable_state);
  1050. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1051. phys_enc->hw_intf->idx - INTF_0,
  1052. phys_enc->enable_state);
  1053. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1054. SDE_ERROR_CMDENC(cmd_enc, "already disabled\n");
  1055. return;
  1056. }
  1057. if (phys_enc->has_intf_te && phys_enc->hw_intf->ops.enable_tearcheck)
  1058. phys_enc->hw_intf->ops.enable_tearcheck(
  1059. phys_enc->hw_intf,
  1060. false);
  1061. else if (phys_enc->hw_pp->ops.enable_tearcheck)
  1062. phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp,
  1063. false);
  1064. phys_enc->enable_state = SDE_ENC_DISABLED;
  1065. }
  1066. static void sde_encoder_phys_cmd_destroy(struct sde_encoder_phys *phys_enc)
  1067. {
  1068. struct sde_encoder_phys_cmd *cmd_enc =
  1069. to_sde_encoder_phys_cmd(phys_enc);
  1070. if (!phys_enc) {
  1071. SDE_ERROR("invalid encoder\n");
  1072. return;
  1073. }
  1074. kfree(cmd_enc);
  1075. }
  1076. static void sde_encoder_phys_cmd_get_hw_resources(
  1077. struct sde_encoder_phys *phys_enc,
  1078. struct sde_encoder_hw_resources *hw_res,
  1079. struct drm_connector_state *conn_state)
  1080. {
  1081. struct sde_encoder_phys_cmd *cmd_enc =
  1082. to_sde_encoder_phys_cmd(phys_enc);
  1083. if (!phys_enc) {
  1084. SDE_ERROR("invalid encoder\n");
  1085. return;
  1086. }
  1087. if ((phys_enc->intf_idx - INTF_0) >= INTF_MAX) {
  1088. SDE_ERROR("invalid intf idx:%d\n", phys_enc->intf_idx);
  1089. return;
  1090. }
  1091. SDE_DEBUG_CMDENC(cmd_enc, "\n");
  1092. hw_res->intfs[phys_enc->intf_idx - INTF_0] = INTF_MODE_CMD;
  1093. }
  1094. static int sde_encoder_phys_cmd_prepare_for_kickoff(
  1095. struct sde_encoder_phys *phys_enc,
  1096. struct sde_encoder_kickoff_params *params)
  1097. {
  1098. struct sde_hw_tear_check tc_cfg = {0};
  1099. struct sde_encoder_phys_cmd *cmd_enc =
  1100. to_sde_encoder_phys_cmd(phys_enc);
  1101. int ret = 0;
  1102. u32 extra_frame_trigger_time;
  1103. if (!phys_enc || !phys_enc->hw_pp) {
  1104. SDE_ERROR("invalid encoder\n");
  1105. return -EINVAL;
  1106. }
  1107. SDE_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
  1108. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->hw_pp->idx - PINGPONG_0,
  1109. atomic_read(&phys_enc->pending_kickoff_cnt),
  1110. atomic_read(&cmd_enc->autorefresh.kickoff_cnt));
  1111. if (phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_DEFAULT) {
  1112. /*
  1113. * Mark kickoff request as outstanding. If there are more
  1114. * than one outstanding frame, then we have to wait for the
  1115. * previous frame to complete
  1116. */
  1117. ret = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1118. if (ret) {
  1119. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1120. SDE_EVT32(DRMID(phys_enc->parent),
  1121. phys_enc->hw_pp->idx - PINGPONG_0);
  1122. SDE_ERROR("failed wait_for_idle: %d\n", ret);
  1123. }
  1124. }
  1125. if (sde_connector_is_qsync_updated(phys_enc->connector)) {
  1126. tc_cfg.sync_threshold_start =
  1127. _get_tearcheck_threshold(phys_enc,
  1128. &extra_frame_trigger_time);
  1129. if (phys_enc->has_intf_te &&
  1130. phys_enc->hw_intf->ops.update_tearcheck)
  1131. phys_enc->hw_intf->ops.update_tearcheck(
  1132. phys_enc->hw_intf, &tc_cfg);
  1133. else if (phys_enc->hw_pp->ops.update_tearcheck)
  1134. phys_enc->hw_pp->ops.update_tearcheck(
  1135. phys_enc->hw_pp, &tc_cfg);
  1136. cmd_enc->ctl_start_threshold =
  1137. (extra_frame_trigger_time / 1000) +
  1138. SDE_ENC_CTL_START_THRESHOLD_US;
  1139. SDE_EVT32(DRMID(phys_enc->parent),
  1140. tc_cfg.sync_threshold_start, cmd_enc->ctl_start_threshold);
  1141. }
  1142. SDE_DEBUG_CMDENC(cmd_enc, "pp:%d pending_cnt %d\n",
  1143. phys_enc->hw_pp->idx - PINGPONG_0,
  1144. atomic_read(&phys_enc->pending_kickoff_cnt));
  1145. return ret;
  1146. }
  1147. static int _sde_encoder_phys_cmd_wait_for_ctl_start(
  1148. struct sde_encoder_phys *phys_enc)
  1149. {
  1150. struct sde_encoder_phys_cmd *cmd_enc =
  1151. to_sde_encoder_phys_cmd(phys_enc);
  1152. struct sde_encoder_wait_info wait_info;
  1153. int ret;
  1154. bool frame_pending = true;
  1155. struct sde_hw_ctl *ctl;
  1156. if (!phys_enc || !phys_enc->hw_ctl) {
  1157. SDE_ERROR("invalid argument(s)\n");
  1158. return -EINVAL;
  1159. }
  1160. ctl = phys_enc->hw_ctl;
  1161. wait_info.wq = &phys_enc->pending_kickoff_wq;
  1162. wait_info.atomic_cnt = &phys_enc->pending_ctlstart_cnt;
  1163. wait_info.timeout_ms = KICKOFF_TIMEOUT_MS;
  1164. /* slave encoder doesn't enable for ppsplit */
  1165. if (_sde_encoder_phys_is_ppsplit_slave(phys_enc))
  1166. return 0;
  1167. ret = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_CTL_START,
  1168. &wait_info);
  1169. if (ret == -ETIMEDOUT) {
  1170. struct sde_hw_ctl *ctl = phys_enc->hw_ctl;
  1171. if (ctl && ctl->ops.get_start_state)
  1172. frame_pending = ctl->ops.get_start_state(ctl);
  1173. if (frame_pending)
  1174. SDE_ERROR_CMDENC(cmd_enc,
  1175. "ctl start interrupt wait failed\n");
  1176. else
  1177. ret = 0;
  1178. if (sde_encoder_phys_cmd_is_master(phys_enc)) {
  1179. /*
  1180. * Signaling the retire fence at ctl start timeout
  1181. * to allow the next commit and avoid device freeze.
  1182. * As ctl start timeout can occurs due to no read ptr,
  1183. * updating pending_rd_ptr_cnt here may not cover all
  1184. * cases. Hence signaling the retire fence.
  1185. */
  1186. if (atomic_add_unless(
  1187. &phys_enc->pending_retire_fence_cnt, -1, 0))
  1188. phys_enc->parent_ops.handle_frame_done(
  1189. phys_enc->parent,
  1190. phys_enc,
  1191. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE);
  1192. atomic_add_unless(
  1193. &phys_enc->pending_ctlstart_cnt, -1, 0);
  1194. }
  1195. } else if ((ret == 0) &&
  1196. (phys_enc->frame_trigger_mode == FRAME_DONE_WAIT_POSTED_START) &&
  1197. atomic_read(&phys_enc->pending_kickoff_cnt) &&
  1198. ctl->ops.get_scheduler_status &&
  1199. (ctl->ops.get_scheduler_status(ctl) & BIT(0)) &&
  1200. phys_enc->parent_ops.handle_frame_done) {
  1201. atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
  1202. phys_enc->parent_ops.handle_frame_done(
  1203. phys_enc->parent, phys_enc,
  1204. SDE_ENCODER_FRAME_EVENT_DONE |
  1205. SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE);
  1206. }
  1207. return ret;
  1208. }
  1209. static int sde_encoder_phys_cmd_wait_for_tx_complete(
  1210. struct sde_encoder_phys *phys_enc)
  1211. {
  1212. int rc;
  1213. struct sde_encoder_phys_cmd *cmd_enc;
  1214. if (!phys_enc)
  1215. return -EINVAL;
  1216. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1217. rc = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1218. if (rc) {
  1219. SDE_EVT32(DRMID(phys_enc->parent),
  1220. phys_enc->intf_idx - INTF_0);
  1221. SDE_ERROR("failed wait_for_idle: %d\n", rc);
  1222. }
  1223. return rc;
  1224. }
  1225. static int sde_encoder_phys_cmd_wait_for_commit_done(
  1226. struct sde_encoder_phys *phys_enc)
  1227. {
  1228. int rc = 0;
  1229. struct sde_encoder_phys_cmd *cmd_enc;
  1230. if (!phys_enc)
  1231. return -EINVAL;
  1232. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1233. /* only required for master controller */
  1234. if (sde_encoder_phys_cmd_is_master(phys_enc))
  1235. rc = _sde_encoder_phys_cmd_wait_for_ctl_start(phys_enc);
  1236. if (!rc && sde_encoder_phys_cmd_is_master(phys_enc) &&
  1237. cmd_enc->autorefresh.cfg.enable)
  1238. rc = _sde_encoder_phys_cmd_wait_for_autorefresh_done(phys_enc);
  1239. /* wait for posted start or serialize trigger */
  1240. if ((atomic_read(&phys_enc->pending_kickoff_cnt) > 1) ||
  1241. (!rc && phys_enc->frame_trigger_mode ==
  1242. FRAME_DONE_WAIT_SERIALIZE)) {
  1243. rc = _sde_encoder_phys_cmd_wait_for_idle(phys_enc);
  1244. if (rc) {
  1245. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1246. SDE_EVT32(DRMID(phys_enc->parent),
  1247. phys_enc->hw_pp->idx - PINGPONG_0);
  1248. SDE_ERROR("failed wait_for_idle: %d\n", rc);
  1249. }
  1250. }
  1251. return rc;
  1252. }
  1253. static int sde_encoder_phys_cmd_wait_for_vblank(
  1254. struct sde_encoder_phys *phys_enc)
  1255. {
  1256. int rc = 0;
  1257. struct sde_encoder_phys_cmd *cmd_enc;
  1258. struct sde_encoder_wait_info wait_info;
  1259. if (!phys_enc)
  1260. return -EINVAL;
  1261. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1262. /* only required for master controller */
  1263. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1264. return rc;
  1265. wait_info.wq = &cmd_enc->pending_vblank_wq;
  1266. wait_info.atomic_cnt = &cmd_enc->pending_vblank_cnt;
  1267. wait_info.timeout_ms = _sde_encoder_phys_cmd_get_idle_timeout(cmd_enc);
  1268. atomic_inc(&cmd_enc->pending_vblank_cnt);
  1269. rc = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_RDPTR,
  1270. &wait_info);
  1271. return rc;
  1272. }
  1273. static void sde_encoder_phys_cmd_update_split_role(
  1274. struct sde_encoder_phys *phys_enc,
  1275. enum sde_enc_split_role role)
  1276. {
  1277. struct sde_encoder_phys_cmd *cmd_enc;
  1278. enum sde_enc_split_role old_role;
  1279. bool is_ppsplit;
  1280. if (!phys_enc)
  1281. return;
  1282. cmd_enc = to_sde_encoder_phys_cmd(phys_enc);
  1283. old_role = phys_enc->split_role;
  1284. is_ppsplit = _sde_encoder_phys_is_ppsplit(phys_enc);
  1285. phys_enc->split_role = role;
  1286. SDE_DEBUG_CMDENC(cmd_enc, "old role %d new role %d\n",
  1287. old_role, role);
  1288. /*
  1289. * ppsplit solo needs to reprogram because intf may have swapped without
  1290. * role changing on left-only, right-only back-to-back commits
  1291. */
  1292. if (!(is_ppsplit && role == ENC_ROLE_SOLO) &&
  1293. (role == old_role || role == ENC_ROLE_SKIP))
  1294. return;
  1295. sde_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
  1296. _sde_encoder_phys_cmd_pingpong_config(phys_enc);
  1297. _sde_encoder_phys_cmd_update_flush_mask(phys_enc);
  1298. }
  1299. static void sde_encoder_phys_cmd_prepare_commit(
  1300. struct sde_encoder_phys *phys_enc)
  1301. {
  1302. struct sde_encoder_phys_cmd *cmd_enc =
  1303. to_sde_encoder_phys_cmd(phys_enc);
  1304. int trial = 0;
  1305. if (!phys_enc)
  1306. return;
  1307. if (!sde_encoder_phys_cmd_is_master(phys_enc))
  1308. return;
  1309. SDE_EVT32(DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
  1310. cmd_enc->autorefresh.cfg.enable);
  1311. if (!sde_encoder_phys_cmd_is_autorefresh_enabled(phys_enc))
  1312. return;
  1313. /*
  1314. * If autorefresh is enabled, disable it and make sure it is safe to
  1315. * proceed with current frame commit/push. Sequence fallowed is,
  1316. * 1. Disable TE
  1317. * 2. Disable autorefresh config
  1318. * 4. Poll for frame transfer ongoing to be false
  1319. * 5. Enable TE back
  1320. */
  1321. sde_encoder_phys_cmd_connect_te(phys_enc, false);
  1322. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, 0);
  1323. do {
  1324. udelay(SDE_ENC_MAX_POLL_TIMEOUT_US);
  1325. if ((trial * SDE_ENC_MAX_POLL_TIMEOUT_US)
  1326. > (KICKOFF_TIMEOUT_MS * USEC_PER_MSEC)) {
  1327. SDE_ERROR_CMDENC(cmd_enc,
  1328. "disable autorefresh failed\n");
  1329. break;
  1330. }
  1331. trial++;
  1332. } while (_sde_encoder_phys_cmd_is_ongoing_pptx(phys_enc));
  1333. sde_encoder_phys_cmd_connect_te(phys_enc, true);
  1334. SDE_DEBUG_CMDENC(cmd_enc, "disabled autorefresh\n");
  1335. }
  1336. static void sde_encoder_phys_cmd_trigger_start(
  1337. struct sde_encoder_phys *phys_enc)
  1338. {
  1339. struct sde_encoder_phys_cmd *cmd_enc =
  1340. to_sde_encoder_phys_cmd(phys_enc);
  1341. u32 frame_cnt;
  1342. if (!phys_enc)
  1343. return;
  1344. /* we don't issue CTL_START when using autorefresh */
  1345. frame_cnt = _sde_encoder_phys_cmd_get_autorefresh_property(phys_enc);
  1346. if (frame_cnt) {
  1347. _sde_encoder_phys_cmd_config_autorefresh(phys_enc, frame_cnt);
  1348. atomic_inc(&cmd_enc->autorefresh.kickoff_cnt);
  1349. } else {
  1350. sde_encoder_helper_trigger_start(phys_enc);
  1351. }
  1352. }
  1353. static void sde_encoder_phys_cmd_setup_vsync_source(
  1354. struct sde_encoder_phys *phys_enc,
  1355. u32 vsync_source, bool is_dummy)
  1356. {
  1357. if (!phys_enc || !phys_enc->hw_intf)
  1358. return;
  1359. sde_encoder_helper_vsync_config(phys_enc, vsync_source, is_dummy);
  1360. if (phys_enc->has_intf_te && phys_enc->hw_intf->ops.vsync_sel)
  1361. phys_enc->hw_intf->ops.vsync_sel(phys_enc->hw_intf,
  1362. vsync_source);
  1363. }
  1364. static void sde_encoder_phys_cmd_init_ops(struct sde_encoder_phys_ops *ops)
  1365. {
  1366. ops->prepare_commit = sde_encoder_phys_cmd_prepare_commit;
  1367. ops->is_master = sde_encoder_phys_cmd_is_master;
  1368. ops->mode_set = sde_encoder_phys_cmd_mode_set;
  1369. ops->cont_splash_mode_set = sde_encoder_phys_cmd_cont_splash_mode_set;
  1370. ops->mode_fixup = sde_encoder_phys_cmd_mode_fixup;
  1371. ops->enable = sde_encoder_phys_cmd_enable;
  1372. ops->disable = sde_encoder_phys_cmd_disable;
  1373. ops->destroy = sde_encoder_phys_cmd_destroy;
  1374. ops->get_hw_resources = sde_encoder_phys_cmd_get_hw_resources;
  1375. ops->control_vblank_irq = sde_encoder_phys_cmd_control_vblank_irq;
  1376. ops->wait_for_commit_done = sde_encoder_phys_cmd_wait_for_commit_done;
  1377. ops->prepare_for_kickoff = sde_encoder_phys_cmd_prepare_for_kickoff;
  1378. ops->wait_for_tx_complete = sde_encoder_phys_cmd_wait_for_tx_complete;
  1379. ops->wait_for_vblank = sde_encoder_phys_cmd_wait_for_vblank;
  1380. ops->trigger_flush = sde_encoder_helper_trigger_flush;
  1381. ops->trigger_start = sde_encoder_phys_cmd_trigger_start;
  1382. ops->needs_single_flush = sde_encoder_phys_needs_single_flush;
  1383. ops->hw_reset = sde_encoder_helper_hw_reset;
  1384. ops->irq_control = sde_encoder_phys_cmd_irq_control;
  1385. ops->update_split_role = sde_encoder_phys_cmd_update_split_role;
  1386. ops->restore = sde_encoder_phys_cmd_enable_helper;
  1387. ops->control_te = sde_encoder_phys_cmd_connect_te;
  1388. ops->is_autorefresh_enabled =
  1389. sde_encoder_phys_cmd_is_autorefresh_enabled;
  1390. ops->get_line_count = sde_encoder_phys_cmd_te_get_line_count;
  1391. ops->get_wr_line_count = sde_encoder_phys_cmd_get_write_line_count;
  1392. ops->wait_for_active = NULL;
  1393. ops->setup_vsync_source = sde_encoder_phys_cmd_setup_vsync_source;
  1394. ops->setup_misr = sde_encoder_helper_setup_misr;
  1395. ops->collect_misr = sde_encoder_helper_collect_misr;
  1396. }
  1397. struct sde_encoder_phys *sde_encoder_phys_cmd_init(
  1398. struct sde_enc_phys_init_params *p)
  1399. {
  1400. struct sde_encoder_phys *phys_enc = NULL;
  1401. struct sde_encoder_phys_cmd *cmd_enc = NULL;
  1402. struct sde_hw_mdp *hw_mdp;
  1403. struct sde_encoder_irq *irq;
  1404. int i, ret = 0;
  1405. SDE_DEBUG("intf %d\n", p->intf_idx - INTF_0);
  1406. cmd_enc = kzalloc(sizeof(*cmd_enc), GFP_KERNEL);
  1407. if (!cmd_enc) {
  1408. ret = -ENOMEM;
  1409. SDE_ERROR("failed to allocate\n");
  1410. goto fail;
  1411. }
  1412. phys_enc = &cmd_enc->base;
  1413. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  1414. if (IS_ERR_OR_NULL(hw_mdp)) {
  1415. ret = PTR_ERR(hw_mdp);
  1416. SDE_ERROR("failed to get mdptop\n");
  1417. goto fail_mdp_init;
  1418. }
  1419. phys_enc->hw_mdptop = hw_mdp;
  1420. phys_enc->intf_idx = p->intf_idx;
  1421. phys_enc->parent = p->parent;
  1422. phys_enc->parent_ops = p->parent_ops;
  1423. phys_enc->sde_kms = p->sde_kms;
  1424. phys_enc->split_role = p->split_role;
  1425. phys_enc->intf_mode = INTF_MODE_CMD;
  1426. phys_enc->enc_spinlock = p->enc_spinlock;
  1427. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  1428. cmd_enc->stream_sel = 0;
  1429. cmd_enc->ctl_start_threshold = SDE_ENC_CTL_START_THRESHOLD_US;
  1430. phys_enc->enable_state = SDE_ENC_DISABLED;
  1431. sde_encoder_phys_cmd_init_ops(&phys_enc->ops);
  1432. phys_enc->comp_type = p->comp_type;
  1433. if (sde_hw_intf_te_supported(phys_enc->sde_kms->catalog))
  1434. phys_enc->has_intf_te = true;
  1435. else
  1436. phys_enc->has_intf_te = false;
  1437. for (i = 0; i < INTR_IDX_MAX; i++) {
  1438. irq = &phys_enc->irq[i];
  1439. INIT_LIST_HEAD(&irq->cb.list);
  1440. irq->irq_idx = -EINVAL;
  1441. irq->hw_idx = -EINVAL;
  1442. irq->cb.arg = phys_enc;
  1443. }
  1444. irq = &phys_enc->irq[INTR_IDX_CTL_START];
  1445. irq->name = "ctl_start";
  1446. irq->intr_type = SDE_IRQ_TYPE_CTL_START;
  1447. irq->intr_idx = INTR_IDX_CTL_START;
  1448. irq->cb.func = sde_encoder_phys_cmd_ctl_start_irq;
  1449. irq = &phys_enc->irq[INTR_IDX_PINGPONG];
  1450. irq->name = "pp_done";
  1451. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_COMP;
  1452. irq->intr_idx = INTR_IDX_PINGPONG;
  1453. irq->cb.func = sde_encoder_phys_cmd_pp_tx_done_irq;
  1454. irq = &phys_enc->irq[INTR_IDX_RDPTR];
  1455. irq->intr_idx = INTR_IDX_RDPTR;
  1456. irq->name = "te_rd_ptr";
  1457. if (phys_enc->has_intf_te)
  1458. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_RD_PTR;
  1459. else
  1460. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_RD_PTR;
  1461. irq->cb.func = sde_encoder_phys_cmd_te_rd_ptr_irq;
  1462. irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
  1463. irq->name = "underrun";
  1464. irq->intr_type = SDE_IRQ_TYPE_INTF_UNDER_RUN;
  1465. irq->intr_idx = INTR_IDX_UNDERRUN;
  1466. irq->cb.func = sde_encoder_phys_cmd_underrun_irq;
  1467. irq = &phys_enc->irq[INTR_IDX_AUTOREFRESH_DONE];
  1468. irq->name = "autorefresh_done";
  1469. if (phys_enc->has_intf_te)
  1470. irq->intr_type = SDE_IRQ_TYPE_INTF_TEAR_AUTO_REF;
  1471. else
  1472. irq->intr_type = SDE_IRQ_TYPE_PING_PONG_AUTO_REF;
  1473. irq->intr_idx = INTR_IDX_AUTOREFRESH_DONE;
  1474. irq->cb.func = sde_encoder_phys_cmd_autorefresh_done_irq;
  1475. atomic_set(&phys_enc->vblank_refcount, 0);
  1476. atomic_set(&phys_enc->pending_kickoff_cnt, 0);
  1477. atomic_set(&phys_enc->pending_ctlstart_cnt, 0);
  1478. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  1479. atomic_set(&cmd_enc->pending_rd_ptr_cnt, 0);
  1480. atomic_set(&cmd_enc->pending_vblank_cnt, 0);
  1481. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  1482. init_waitqueue_head(&cmd_enc->pending_vblank_wq);
  1483. atomic_set(&cmd_enc->autorefresh.kickoff_cnt, 0);
  1484. init_waitqueue_head(&cmd_enc->autorefresh.kickoff_wq);
  1485. SDE_DEBUG_CMDENC(cmd_enc, "created\n");
  1486. return phys_enc;
  1487. fail_mdp_init:
  1488. kfree(cmd_enc);
  1489. fail:
  1490. return ERR_PTR(ret);
  1491. }