sde_encoder_phys_cmd.c 51 KB

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