sde_encoder_phys_cmd.c 52 KB

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