sde_encoder_phys_cmd.c 51 KB

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