sde_encoder_phys_wb.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
  6. #include <linux/debugfs.h>
  7. #include <drm/sde_drm.h>
  8. #include "sde_encoder_phys.h"
  9. #include "sde_formats.h"
  10. #include "sde_hw_top.h"
  11. #include "sde_hw_interrupts.h"
  12. #include "sde_core_irq.h"
  13. #include "sde_wb.h"
  14. #include "sde_vbif.h"
  15. #include "sde_crtc.h"
  16. #define to_sde_encoder_phys_wb(x) \
  17. container_of(x, struct sde_encoder_phys_wb, base)
  18. #define WBID(wb_enc) \
  19. ((wb_enc && wb_enc->wb_dev) ? wb_enc->wb_dev->wb_idx - WB_0 : -1)
  20. #define TO_S15D16(_x_) ((_x_) << 7)
  21. static const u32 cwb_irq_tbl[PINGPONG_MAX] = {SDE_NONE, INTR_IDX_PP1_OVFL,
  22. INTR_IDX_PP2_OVFL, INTR_IDX_PP3_OVFL, INTR_IDX_PP4_OVFL,
  23. INTR_IDX_PP5_OVFL, SDE_NONE, SDE_NONE};
  24. /**
  25. * sde_rgb2yuv_601l - rgb to yuv color space conversion matrix
  26. *
  27. */
  28. static struct sde_csc_cfg sde_encoder_phys_wb_rgb2yuv_601l = {
  29. {
  30. TO_S15D16(0x0083), TO_S15D16(0x0102), TO_S15D16(0x0032),
  31. TO_S15D16(0x1fb5), TO_S15D16(0x1f6c), TO_S15D16(0x00e1),
  32. TO_S15D16(0x00e1), TO_S15D16(0x1f45), TO_S15D16(0x1fdc)
  33. },
  34. { 0x00, 0x00, 0x00 },
  35. { 0x0040, 0x0200, 0x0200 },
  36. { 0x000, 0x3ff, 0x000, 0x3ff, 0x000, 0x3ff },
  37. { 0x040, 0x3ac, 0x040, 0x3c0, 0x040, 0x3c0 },
  38. };
  39. /**
  40. * sde_encoder_phys_wb_is_master - report wb always as master encoder
  41. */
  42. static bool sde_encoder_phys_wb_is_master(struct sde_encoder_phys *phys_enc)
  43. {
  44. return true;
  45. }
  46. /**
  47. * sde_encoder_phys_wb_get_intr_type - get interrupt type based on block mode
  48. * @hw_wb: Pointer to h/w writeback driver
  49. */
  50. static enum sde_intr_type sde_encoder_phys_wb_get_intr_type(
  51. struct sde_hw_wb *hw_wb)
  52. {
  53. return (hw_wb->caps->features & BIT(SDE_WB_BLOCK_MODE)) ?
  54. SDE_IRQ_TYPE_WB_ROT_COMP : SDE_IRQ_TYPE_WB_WFD_COMP;
  55. }
  56. /**
  57. * sde_encoder_phys_wb_set_ot_limit - set OT limit for writeback interface
  58. * @phys_enc: Pointer to physical encoder
  59. */
  60. static void sde_encoder_phys_wb_set_ot_limit(
  61. struct sde_encoder_phys *phys_enc)
  62. {
  63. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  64. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  65. struct sde_vbif_set_ot_params ot_params;
  66. memset(&ot_params, 0, sizeof(ot_params));
  67. ot_params.xin_id = hw_wb->caps->xin_id;
  68. ot_params.num = hw_wb->idx - WB_0;
  69. ot_params.width = wb_enc->wb_roi.w;
  70. ot_params.height = wb_enc->wb_roi.h;
  71. ot_params.is_wfd = true;
  72. ot_params.frame_rate = phys_enc->cached_mode.vrefresh;
  73. ot_params.vbif_idx = hw_wb->caps->vbif_idx;
  74. ot_params.clk_ctrl = hw_wb->caps->clk_ctrl;
  75. ot_params.rd = false;
  76. sde_vbif_set_ot_limit(phys_enc->sde_kms, &ot_params);
  77. }
  78. /**
  79. * sde_encoder_phys_wb_set_qos_remap - set QoS remapper for writeback
  80. * @phys_enc: Pointer to physical encoder
  81. */
  82. static void sde_encoder_phys_wb_set_qos_remap(
  83. struct sde_encoder_phys *phys_enc)
  84. {
  85. struct sde_encoder_phys_wb *wb_enc;
  86. struct sde_hw_wb *hw_wb;
  87. struct drm_crtc *crtc;
  88. struct sde_vbif_set_qos_params qos_params;
  89. if (!phys_enc || !phys_enc->parent || !phys_enc->parent->crtc) {
  90. SDE_ERROR("invalid arguments\n");
  91. return;
  92. }
  93. wb_enc = to_sde_encoder_phys_wb(phys_enc);
  94. if (!wb_enc->crtc) {
  95. SDE_ERROR("invalid crtc");
  96. return;
  97. }
  98. crtc = wb_enc->crtc;
  99. if (!wb_enc->hw_wb || !wb_enc->hw_wb->caps) {
  100. SDE_ERROR("invalid writeback hardware\n");
  101. return;
  102. }
  103. hw_wb = wb_enc->hw_wb;
  104. memset(&qos_params, 0, sizeof(qos_params));
  105. qos_params.vbif_idx = hw_wb->caps->vbif_idx;
  106. qos_params.xin_id = hw_wb->caps->xin_id;
  107. qos_params.clk_ctrl = hw_wb->caps->clk_ctrl;
  108. qos_params.num = hw_wb->idx - WB_0;
  109. qos_params.client_type = phys_enc->in_clone_mode ?
  110. VBIF_CWB_CLIENT : VBIF_NRT_CLIENT;
  111. SDE_DEBUG("[qos_remap] wb:%d vbif:%d xin:%d clone:%d\n",
  112. qos_params.num,
  113. qos_params.vbif_idx,
  114. qos_params.xin_id, qos_params.client_type);
  115. sde_vbif_set_qos_remap(phys_enc->sde_kms, &qos_params);
  116. }
  117. static u64 _sde_encoder_phys_wb_get_qos_lut(const struct sde_qos_lut_tbl *tbl,
  118. u32 total_fl)
  119. {
  120. int i;
  121. if (!tbl || !tbl->nentry || !tbl->entries)
  122. return 0;
  123. for (i = 0; i < tbl->nentry; i++)
  124. if (total_fl <= tbl->entries[i].fl)
  125. return tbl->entries[i].lut;
  126. /* if last fl is zero, use as default */
  127. if (!tbl->entries[i-1].fl)
  128. return tbl->entries[i-1].lut;
  129. return 0;
  130. }
  131. /**
  132. * sde_encoder_phys_wb_set_qos - set QoS/danger/safe LUTs for writeback
  133. * @phys_enc: Pointer to physical encoder
  134. */
  135. static void sde_encoder_phys_wb_set_qos(struct sde_encoder_phys *phys_enc)
  136. {
  137. struct sde_encoder_phys_wb *wb_enc;
  138. struct sde_hw_wb *hw_wb;
  139. struct sde_hw_wb_qos_cfg qos_cfg;
  140. struct sde_mdss_cfg *catalog;
  141. if (!phys_enc || !phys_enc->sde_kms || !phys_enc->sde_kms->catalog) {
  142. SDE_ERROR("invalid parameter(s)\n");
  143. return;
  144. }
  145. catalog = phys_enc->sde_kms->catalog;
  146. wb_enc = to_sde_encoder_phys_wb(phys_enc);
  147. if (!wb_enc->hw_wb) {
  148. SDE_ERROR("invalid writeback hardware\n");
  149. return;
  150. }
  151. hw_wb = wb_enc->hw_wb;
  152. memset(&qos_cfg, 0, sizeof(struct sde_hw_wb_qos_cfg));
  153. qos_cfg.danger_safe_en = true;
  154. qos_cfg.danger_lut =
  155. catalog->perf.danger_lut_tbl[SDE_QOS_LUT_USAGE_NRT];
  156. if (phys_enc->in_clone_mode)
  157. qos_cfg.safe_lut = (u32) _sde_encoder_phys_wb_get_qos_lut(
  158. &catalog->perf.sfe_lut_tbl[SDE_QOS_LUT_USAGE_CWB], 0);
  159. else
  160. qos_cfg.safe_lut = (u32) _sde_encoder_phys_wb_get_qos_lut(
  161. &catalog->perf.sfe_lut_tbl[SDE_QOS_LUT_USAGE_NRT], 0);
  162. if (phys_enc->in_clone_mode)
  163. qos_cfg.creq_lut = _sde_encoder_phys_wb_get_qos_lut(
  164. &catalog->perf.qos_lut_tbl[SDE_QOS_LUT_USAGE_CWB], 0);
  165. else
  166. qos_cfg.creq_lut = _sde_encoder_phys_wb_get_qos_lut(
  167. &catalog->perf.qos_lut_tbl[SDE_QOS_LUT_USAGE_NRT], 0);
  168. if (hw_wb->ops.setup_danger_safe_lut)
  169. hw_wb->ops.setup_danger_safe_lut(hw_wb, &qos_cfg);
  170. if (hw_wb->ops.setup_creq_lut)
  171. hw_wb->ops.setup_creq_lut(hw_wb, &qos_cfg);
  172. if (hw_wb->ops.setup_qos_ctrl)
  173. hw_wb->ops.setup_qos_ctrl(hw_wb, &qos_cfg);
  174. }
  175. /**
  176. * sde_encoder_phys_setup_cdm - setup chroma down block
  177. * @phys_enc: Pointer to physical encoder
  178. * @fb: Pointer to output framebuffer
  179. * @format: Output format
  180. */
  181. void sde_encoder_phys_setup_cdm(struct sde_encoder_phys *phys_enc,
  182. struct drm_framebuffer *fb, const struct sde_format *format,
  183. struct sde_rect *wb_roi)
  184. {
  185. struct sde_hw_cdm *hw_cdm;
  186. struct sde_hw_cdm_cfg *cdm_cfg;
  187. struct sde_hw_pingpong *hw_pp;
  188. int ret;
  189. if (!phys_enc || !format)
  190. return;
  191. cdm_cfg = &phys_enc->cdm_cfg;
  192. hw_pp = phys_enc->hw_pp;
  193. hw_cdm = phys_enc->hw_cdm;
  194. if (!hw_cdm)
  195. return;
  196. if (!SDE_FORMAT_IS_YUV(format)) {
  197. SDE_DEBUG("[cdm_disable fmt:%x]\n",
  198. format->base.pixel_format);
  199. if (hw_cdm && hw_cdm->ops.disable)
  200. hw_cdm->ops.disable(hw_cdm);
  201. return;
  202. }
  203. memset(cdm_cfg, 0, sizeof(struct sde_hw_cdm_cfg));
  204. if (!wb_roi)
  205. return;
  206. cdm_cfg->output_width = wb_roi->w;
  207. cdm_cfg->output_height = wb_roi->h;
  208. cdm_cfg->output_fmt = format;
  209. cdm_cfg->output_type = CDM_CDWN_OUTPUT_WB;
  210. cdm_cfg->output_bit_depth = SDE_FORMAT_IS_DX(format) ?
  211. CDM_CDWN_OUTPUT_10BIT : CDM_CDWN_OUTPUT_8BIT;
  212. /* enable 10 bit logic */
  213. switch (cdm_cfg->output_fmt->chroma_sample) {
  214. case SDE_CHROMA_RGB:
  215. cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
  216. cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
  217. break;
  218. case SDE_CHROMA_H2V1:
  219. cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
  220. cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
  221. break;
  222. case SDE_CHROMA_420:
  223. cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
  224. cdm_cfg->v_cdwn_type = CDM_CDWN_OFFSITE;
  225. break;
  226. case SDE_CHROMA_H1V2:
  227. default:
  228. SDE_ERROR("unsupported chroma sampling type\n");
  229. cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
  230. cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
  231. break;
  232. }
  233. SDE_DEBUG("[cdm_enable:%d,%d,%X,%d,%d,%d,%d]\n",
  234. cdm_cfg->output_width,
  235. cdm_cfg->output_height,
  236. cdm_cfg->output_fmt->base.pixel_format,
  237. cdm_cfg->output_type,
  238. cdm_cfg->output_bit_depth,
  239. cdm_cfg->h_cdwn_type,
  240. cdm_cfg->v_cdwn_type);
  241. if (hw_cdm && hw_cdm->ops.setup_csc_data) {
  242. ret = hw_cdm->ops.setup_csc_data(hw_cdm,
  243. &sde_encoder_phys_wb_rgb2yuv_601l);
  244. if (ret < 0) {
  245. SDE_ERROR("failed to setup CSC %d\n", ret);
  246. return;
  247. }
  248. }
  249. if (hw_cdm && hw_cdm->ops.setup_cdwn) {
  250. ret = hw_cdm->ops.setup_cdwn(hw_cdm, cdm_cfg);
  251. if (ret < 0) {
  252. SDE_ERROR("failed to setup CDM %d\n", ret);
  253. return;
  254. }
  255. }
  256. if (hw_cdm && hw_pp && hw_cdm->ops.enable) {
  257. cdm_cfg->pp_id = hw_pp->idx;
  258. ret = hw_cdm->ops.enable(hw_cdm, cdm_cfg);
  259. if (ret < 0) {
  260. SDE_ERROR("failed to enable CDM %d\n", ret);
  261. return;
  262. }
  263. }
  264. }
  265. /**
  266. * sde_encoder_phys_wb_setup_fb - setup output framebuffer
  267. * @phys_enc: Pointer to physical encoder
  268. * @fb: Pointer to output framebuffer
  269. * @wb_roi: Pointer to output region of interest
  270. */
  271. static void sde_encoder_phys_wb_setup_fb(struct sde_encoder_phys *phys_enc,
  272. struct drm_framebuffer *fb, struct sde_rect *wb_roi)
  273. {
  274. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  275. struct sde_hw_wb *hw_wb;
  276. struct sde_hw_wb_cfg *wb_cfg;
  277. struct sde_hw_wb_cdp_cfg *cdp_cfg;
  278. const struct msm_format *format;
  279. int ret;
  280. struct msm_gem_address_space *aspace;
  281. u32 fb_mode;
  282. if (!phys_enc || !phys_enc->sde_kms || !phys_enc->sde_kms->catalog ||
  283. !phys_enc->connector) {
  284. SDE_ERROR("invalid encoder\n");
  285. return;
  286. }
  287. hw_wb = wb_enc->hw_wb;
  288. wb_cfg = &wb_enc->wb_cfg;
  289. cdp_cfg = &wb_enc->cdp_cfg;
  290. memset(wb_cfg, 0, sizeof(struct sde_hw_wb_cfg));
  291. wb_cfg->intf_mode = phys_enc->intf_mode;
  292. fb_mode = sde_connector_get_property(phys_enc->connector->state,
  293. CONNECTOR_PROP_FB_TRANSLATION_MODE);
  294. if (phys_enc->enable_state == SDE_ENC_DISABLING)
  295. wb_cfg->is_secure = false;
  296. else if (fb_mode == SDE_DRM_FB_SEC)
  297. wb_cfg->is_secure = true;
  298. else
  299. wb_cfg->is_secure = false;
  300. aspace = (wb_cfg->is_secure) ?
  301. wb_enc->aspace[SDE_IOMMU_DOMAIN_SECURE] :
  302. wb_enc->aspace[SDE_IOMMU_DOMAIN_UNSECURE];
  303. SDE_DEBUG("[fb_secure:%d]\n", wb_cfg->is_secure);
  304. ret = msm_framebuffer_prepare(fb, aspace);
  305. if (ret) {
  306. SDE_ERROR("prep fb failed, %d\n", ret);
  307. return;
  308. }
  309. /* cache framebuffer for cleanup in writeback done */
  310. wb_enc->wb_fb = fb;
  311. wb_enc->wb_aspace = aspace;
  312. drm_framebuffer_get(fb);
  313. format = msm_framebuffer_format(fb);
  314. if (!format) {
  315. SDE_DEBUG("invalid format for fb\n");
  316. return;
  317. }
  318. wb_cfg->dest.format = sde_get_sde_format_ext(
  319. format->pixel_format,
  320. fb->modifier);
  321. if (!wb_cfg->dest.format) {
  322. /* this error should be detected during atomic_check */
  323. SDE_ERROR("failed to get format %x\n", format->pixel_format);
  324. return;
  325. }
  326. wb_cfg->roi = *wb_roi;
  327. if (hw_wb->caps->features & BIT(SDE_WB_XY_ROI_OFFSET)) {
  328. ret = sde_format_populate_layout(aspace, fb, &wb_cfg->dest);
  329. if (ret) {
  330. SDE_DEBUG("failed to populate layout %d\n", ret);
  331. return;
  332. }
  333. wb_cfg->dest.width = fb->width;
  334. wb_cfg->dest.height = fb->height;
  335. wb_cfg->dest.num_planes = wb_cfg->dest.format->num_planes;
  336. } else {
  337. ret = sde_format_populate_layout_with_roi(aspace, fb, wb_roi,
  338. &wb_cfg->dest);
  339. if (ret) {
  340. /* this error should be detected during atomic_check */
  341. SDE_DEBUG("failed to populate layout %d\n", ret);
  342. return;
  343. }
  344. }
  345. if ((wb_cfg->dest.format->fetch_planes == SDE_PLANE_PLANAR) &&
  346. (wb_cfg->dest.format->element[0] == C1_B_Cb))
  347. swap(wb_cfg->dest.plane_addr[1], wb_cfg->dest.plane_addr[2]);
  348. SDE_DEBUG("[fb_offset:%8.8x,%8.8x,%8.8x,%8.8x]\n",
  349. wb_cfg->dest.plane_addr[0],
  350. wb_cfg->dest.plane_addr[1],
  351. wb_cfg->dest.plane_addr[2],
  352. wb_cfg->dest.plane_addr[3]);
  353. SDE_DEBUG("[fb_stride:%8.8x,%8.8x,%8.8x,%8.8x]\n",
  354. wb_cfg->dest.plane_pitch[0],
  355. wb_cfg->dest.plane_pitch[1],
  356. wb_cfg->dest.plane_pitch[2],
  357. wb_cfg->dest.plane_pitch[3]);
  358. if (hw_wb->ops.setup_roi)
  359. hw_wb->ops.setup_roi(hw_wb, wb_cfg);
  360. if (hw_wb->ops.setup_outformat)
  361. hw_wb->ops.setup_outformat(hw_wb, wb_cfg);
  362. if (hw_wb->ops.setup_cdp) {
  363. memset(cdp_cfg, 0, sizeof(struct sde_hw_wb_cdp_cfg));
  364. cdp_cfg->enable = phys_enc->sde_kms->catalog->perf.cdp_cfg
  365. [SDE_PERF_CDP_USAGE_NRT].wr_enable;
  366. cdp_cfg->ubwc_meta_enable =
  367. SDE_FORMAT_IS_UBWC(wb_cfg->dest.format);
  368. cdp_cfg->tile_amortize_enable =
  369. SDE_FORMAT_IS_UBWC(wb_cfg->dest.format) ||
  370. SDE_FORMAT_IS_TILE(wb_cfg->dest.format);
  371. cdp_cfg->preload_ahead = SDE_WB_CDP_PRELOAD_AHEAD_64;
  372. hw_wb->ops.setup_cdp(hw_wb, cdp_cfg);
  373. }
  374. if (hw_wb->ops.setup_outaddress) {
  375. SDE_EVT32(hw_wb->idx,
  376. wb_cfg->dest.width,
  377. wb_cfg->dest.height,
  378. wb_cfg->dest.plane_addr[0],
  379. wb_cfg->dest.plane_size[0],
  380. wb_cfg->dest.plane_addr[1],
  381. wb_cfg->dest.plane_size[1],
  382. wb_cfg->dest.plane_addr[2],
  383. wb_cfg->dest.plane_size[2],
  384. wb_cfg->dest.plane_addr[3],
  385. wb_cfg->dest.plane_size[3]);
  386. hw_wb->ops.setup_outaddress(hw_wb, wb_cfg);
  387. }
  388. }
  389. static void _sde_encoder_phys_wb_setup_cwb(struct sde_encoder_phys *phys_enc,
  390. bool enable)
  391. {
  392. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  393. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  394. struct sde_hw_ctl *hw_ctl = phys_enc->hw_ctl;
  395. struct sde_crtc *crtc = to_sde_crtc(wb_enc->crtc);
  396. struct sde_hw_pingpong *hw_pp = phys_enc->hw_pp;
  397. bool need_merge = (crtc->num_mixers > 1);
  398. int i = 0;
  399. if (!phys_enc->in_clone_mode) {
  400. SDE_DEBUG("not in CWB mode. early return\n");
  401. return;
  402. }
  403. if (!hw_pp || !hw_ctl || !hw_wb || hw_pp->idx >= PINGPONG_MAX) {
  404. SDE_ERROR("invalid hw resources - return\n");
  405. return;
  406. }
  407. hw_ctl = crtc->mixers[0].hw_ctl;
  408. if (hw_ctl && hw_ctl->ops.setup_intf_cfg_v1 &&
  409. test_bit(SDE_WB_CWB_CTRL, &hw_wb->caps->features)) {
  410. struct sde_hw_intf_cfg_v1 intf_cfg = { 0, };
  411. for (i = 0; i < crtc->num_mixers; i++)
  412. intf_cfg.cwb[intf_cfg.cwb_count++] =
  413. (enum sde_cwb)(hw_pp->idx + i);
  414. if (hw_pp->merge_3d && (intf_cfg.merge_3d_count <
  415. MAX_MERGE_3D_PER_CTL_V1) && need_merge)
  416. intf_cfg.merge_3d[intf_cfg.merge_3d_count++] =
  417. hw_pp->merge_3d->idx;
  418. if (hw_pp->ops.setup_3d_mode)
  419. hw_pp->ops.setup_3d_mode(hw_pp, (enable && need_merge) ?
  420. BLEND_3D_H_ROW_INT : 0);
  421. if (hw_wb->ops.bind_pingpong_blk)
  422. hw_wb->ops.bind_pingpong_blk(hw_wb, enable, hw_pp->idx);
  423. if (hw_ctl->ops.update_intf_cfg) {
  424. hw_ctl->ops.update_intf_cfg(hw_ctl, &intf_cfg, enable);
  425. SDE_DEBUG("in CWB mode on CTL_%d PP-%d merge3d:%d\n",
  426. hw_ctl->idx - CTL_0,
  427. hw_pp->idx - PINGPONG_0,
  428. hw_pp->merge_3d ?
  429. hw_pp->merge_3d->idx - MERGE_3D_0 : -1);
  430. }
  431. } else {
  432. struct sde_hw_intf_cfg *intf_cfg = &phys_enc->intf_cfg;
  433. memset(intf_cfg, 0, sizeof(struct sde_hw_intf_cfg));
  434. intf_cfg->intf = SDE_NONE;
  435. intf_cfg->wb = hw_wb->idx;
  436. if (hw_ctl && hw_ctl->ops.update_wb_cfg) {
  437. hw_ctl->ops.update_wb_cfg(hw_ctl, intf_cfg, enable);
  438. SDE_DEBUG("in CWB mode adding WB for CTL_%d\n",
  439. hw_ctl->idx - CTL_0);
  440. }
  441. }
  442. }
  443. /**
  444. * sde_encoder_phys_wb_setup_cdp - setup chroma down prefetch block
  445. * @phys_enc: Pointer to physical encoder
  446. */
  447. static void sde_encoder_phys_wb_setup_cdp(struct sde_encoder_phys *phys_enc,
  448. const struct sde_format *format)
  449. {
  450. struct sde_encoder_phys_wb *wb_enc;
  451. struct sde_hw_wb *hw_wb;
  452. struct sde_hw_cdm *hw_cdm;
  453. struct sde_hw_ctl *ctl;
  454. const int num_wb = 1;
  455. if (!phys_enc) {
  456. SDE_ERROR("invalid encoder\n");
  457. return;
  458. }
  459. if (phys_enc->in_clone_mode) {
  460. SDE_DEBUG("in CWB mode. early return\n");
  461. return;
  462. }
  463. wb_enc = to_sde_encoder_phys_wb(phys_enc);
  464. hw_wb = wb_enc->hw_wb;
  465. hw_cdm = phys_enc->hw_cdm;
  466. ctl = phys_enc->hw_ctl;
  467. if (test_bit(SDE_CTL_ACTIVE_CFG, &ctl->caps->features) &&
  468. (phys_enc->hw_ctl &&
  469. phys_enc->hw_ctl->ops.setup_intf_cfg_v1)) {
  470. struct sde_hw_intf_cfg_v1 *intf_cfg_v1 = &phys_enc->intf_cfg_v1;
  471. struct sde_hw_pingpong *hw_pp = phys_enc->hw_pp;
  472. enum sde_3d_blend_mode mode_3d;
  473. memset(intf_cfg_v1, 0, sizeof(struct sde_hw_intf_cfg_v1));
  474. mode_3d = sde_encoder_helper_get_3d_blend_mode(phys_enc);
  475. intf_cfg_v1->intf_count = SDE_NONE;
  476. intf_cfg_v1->wb_count = num_wb;
  477. intf_cfg_v1->wb[0] = hw_wb->idx;
  478. if (SDE_FORMAT_IS_YUV(format)) {
  479. intf_cfg_v1->cdm_count = num_wb;
  480. intf_cfg_v1->cdm[0] = hw_cdm->idx;
  481. }
  482. if (mode_3d && hw_pp && hw_pp->merge_3d &&
  483. intf_cfg_v1->merge_3d_count < MAX_MERGE_3D_PER_CTL_V1)
  484. intf_cfg_v1->merge_3d[intf_cfg_v1->merge_3d_count++] =
  485. hw_pp->merge_3d->idx;
  486. if (hw_pp && hw_pp->ops.setup_3d_mode)
  487. hw_pp->ops.setup_3d_mode(hw_pp, mode_3d);
  488. /* setup which pp blk will connect to this wb */
  489. if (hw_pp && hw_wb->ops.bind_pingpong_blk)
  490. hw_wb->ops.bind_pingpong_blk(hw_wb, true,
  491. hw_pp->idx);
  492. phys_enc->hw_ctl->ops.setup_intf_cfg_v1(phys_enc->hw_ctl,
  493. intf_cfg_v1);
  494. } else if (phys_enc->hw_ctl && phys_enc->hw_ctl->ops.setup_intf_cfg) {
  495. struct sde_hw_intf_cfg *intf_cfg = &phys_enc->intf_cfg;
  496. memset(intf_cfg, 0, sizeof(struct sde_hw_intf_cfg));
  497. intf_cfg->intf = SDE_NONE;
  498. intf_cfg->wb = hw_wb->idx;
  499. intf_cfg->mode_3d =
  500. sde_encoder_helper_get_3d_blend_mode(phys_enc);
  501. phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl,
  502. intf_cfg);
  503. }
  504. }
  505. static void _sde_enc_phys_wb_detect_cwb(struct sde_encoder_phys *phys_enc,
  506. struct drm_crtc_state *crtc_state)
  507. {
  508. struct drm_encoder *encoder;
  509. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  510. const struct sde_wb_cfg *wb_cfg = wb_enc->hw_wb->caps;
  511. phys_enc->in_clone_mode = false;
  512. /* Check if WB has CWB support */
  513. if (!(wb_cfg->features & BIT(SDE_WB_HAS_CWB)))
  514. return;
  515. /* if any other encoder is connected to same crtc enable clone mode*/
  516. drm_for_each_encoder(encoder, crtc_state->crtc->dev) {
  517. if (encoder->crtc != crtc_state->crtc)
  518. continue;
  519. if (phys_enc->parent != encoder) {
  520. phys_enc->in_clone_mode = true;
  521. break;
  522. }
  523. }
  524. SDE_DEBUG("detect CWB - status:%d\n", phys_enc->in_clone_mode);
  525. }
  526. static int _sde_enc_phys_wb_validate_cwb(struct sde_encoder_phys *phys_enc,
  527. struct drm_crtc_state *crtc_state,
  528. struct drm_connector_state *conn_state)
  529. {
  530. struct sde_crtc_state *cstate = to_sde_crtc_state(crtc_state);
  531. struct sde_rect wb_roi = {0,};
  532. struct sde_rect pu_roi = {0,};
  533. int data_pt;
  534. int ds_outw = 0;
  535. int ds_outh = 0;
  536. int ds_in_use = false;
  537. int i = 0;
  538. int ret = 0;
  539. if (!phys_enc->in_clone_mode) {
  540. SDE_DEBUG("not in CWB mode. early return\n");
  541. goto exit;
  542. }
  543. ret = sde_wb_connector_state_get_output_roi(conn_state, &wb_roi);
  544. if (ret) {
  545. SDE_ERROR("failed to get roi %d\n", ret);
  546. goto exit;
  547. }
  548. data_pt = sde_crtc_get_property(cstate, CRTC_PROP_CAPTURE_OUTPUT);
  549. /* compute cumulative ds output dimensions if in use */
  550. for (i = 0; i < cstate->num_ds; i++)
  551. if (cstate->ds_cfg[i].scl3_cfg.enable) {
  552. ds_in_use = true;
  553. ds_outw += cstate->ds_cfg[i].scl3_cfg.dst_width;
  554. ds_outh = cstate->ds_cfg[i].scl3_cfg.dst_height;
  555. }
  556. /* if ds in use check wb roi against ds output dimensions */
  557. if ((data_pt == CAPTURE_DSPP_OUT) && ds_in_use &&
  558. ((wb_roi.w != ds_outw) || (wb_roi.h != ds_outh))) {
  559. SDE_ERROR("invalid wb roi with dest scalar [%dx%d vs %dx%d]\n",
  560. wb_roi.w, wb_roi.h, ds_outw, ds_outh);
  561. ret = -EINVAL;
  562. goto exit;
  563. }
  564. /* validate conn roi against pu rect */
  565. if (cstate->user_roi_list.num_rects) {
  566. sde_kms_rect_merge_rectangles(&cstate->user_roi_list, &pu_roi);
  567. if (wb_roi.w != pu_roi.w || wb_roi.h != pu_roi.h) {
  568. SDE_ERROR("invalid wb roi with pu [%dx%d vs %dx%d]\n",
  569. wb_roi.w, wb_roi.h, pu_roi.w, pu_roi.h);
  570. ret = -EINVAL;
  571. goto exit;
  572. }
  573. }
  574. exit:
  575. return ret;
  576. }
  577. /**
  578. * sde_encoder_phys_wb_atomic_check - verify and fixup given atomic states
  579. * @phys_enc: Pointer to physical encoder
  580. * @crtc_state: Pointer to CRTC atomic state
  581. * @conn_state: Pointer to connector atomic state
  582. */
  583. static int sde_encoder_phys_wb_atomic_check(
  584. struct sde_encoder_phys *phys_enc,
  585. struct drm_crtc_state *crtc_state,
  586. struct drm_connector_state *conn_state)
  587. {
  588. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  589. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  590. const struct sde_wb_cfg *wb_cfg = hw_wb->caps;
  591. struct drm_framebuffer *fb;
  592. const struct sde_format *fmt;
  593. struct sde_rect wb_roi;
  594. const struct drm_display_mode *mode = &crtc_state->mode;
  595. int rc;
  596. SDE_DEBUG("[atomic_check:%d,\"%s\",%d,%d]\n",
  597. hw_wb->idx - WB_0, mode->name,
  598. mode->hdisplay, mode->vdisplay);
  599. if (!conn_state || !conn_state->connector) {
  600. SDE_ERROR("invalid connector state\n");
  601. return -EINVAL;
  602. } else if (conn_state->connector->status !=
  603. connector_status_connected) {
  604. SDE_ERROR("connector not connected %d\n",
  605. conn_state->connector->status);
  606. return -EINVAL;
  607. }
  608. _sde_enc_phys_wb_detect_cwb(phys_enc, crtc_state);
  609. memset(&wb_roi, 0, sizeof(struct sde_rect));
  610. rc = sde_wb_connector_state_get_output_roi(conn_state, &wb_roi);
  611. if (rc) {
  612. SDE_ERROR("failed to get roi %d\n", rc);
  613. return rc;
  614. }
  615. SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi.x, wb_roi.y,
  616. wb_roi.w, wb_roi.h);
  617. /* bypass check if commit with no framebuffer */
  618. fb = sde_wb_connector_state_get_output_fb(conn_state);
  619. if (!fb) {
  620. SDE_DEBUG("no output framebuffer\n");
  621. return 0;
  622. }
  623. SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
  624. fb->width, fb->height);
  625. fmt = sde_get_sde_format_ext(fb->format->format, fb->modifier);
  626. if (!fmt) {
  627. SDE_ERROR("unsupported output pixel format:%x\n",
  628. fb->format->format);
  629. return -EINVAL;
  630. }
  631. SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->format->format,
  632. fb->modifier);
  633. if (SDE_FORMAT_IS_YUV(fmt) &&
  634. !(wb_cfg->features & BIT(SDE_WB_YUV_CONFIG))) {
  635. SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
  636. return -EINVAL;
  637. }
  638. if (SDE_FORMAT_IS_UBWC(fmt) &&
  639. !(wb_cfg->features & BIT(SDE_WB_UBWC))) {
  640. SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
  641. return -EINVAL;
  642. }
  643. if (SDE_FORMAT_IS_YUV(fmt) != !!phys_enc->hw_cdm)
  644. crtc_state->mode_changed = true;
  645. if (wb_roi.w && wb_roi.h) {
  646. if (wb_roi.w != mode->hdisplay) {
  647. SDE_ERROR("invalid roi w=%d, mode w=%d\n", wb_roi.w,
  648. mode->hdisplay);
  649. return -EINVAL;
  650. } else if (wb_roi.h != mode->vdisplay) {
  651. SDE_ERROR("invalid roi h=%d, mode h=%d\n", wb_roi.h,
  652. mode->vdisplay);
  653. return -EINVAL;
  654. } else if (wb_roi.x + wb_roi.w > fb->width) {
  655. SDE_ERROR("invalid roi x=%d, w=%d, fb w=%d\n",
  656. wb_roi.x, wb_roi.w, fb->width);
  657. return -EINVAL;
  658. } else if (wb_roi.y + wb_roi.h > fb->height) {
  659. SDE_ERROR("invalid roi y=%d, h=%d, fb h=%d\n",
  660. wb_roi.y, wb_roi.h, fb->height);
  661. return -EINVAL;
  662. } else if (wb_roi.w > wb_cfg->sblk->maxlinewidth) {
  663. SDE_ERROR("invalid roi w=%d, maxlinewidth=%u\n",
  664. wb_roi.w, wb_cfg->sblk->maxlinewidth);
  665. return -EINVAL;
  666. }
  667. } else {
  668. if (wb_roi.x || wb_roi.y) {
  669. SDE_ERROR("invalid roi x=%d, y=%d\n",
  670. wb_roi.x, wb_roi.y);
  671. return -EINVAL;
  672. } else if (fb->width != mode->hdisplay) {
  673. SDE_ERROR("invalid fb w=%d, mode w=%d\n", fb->width,
  674. mode->hdisplay);
  675. return -EINVAL;
  676. } else if (fb->height != mode->vdisplay) {
  677. SDE_ERROR("invalid fb h=%d, mode h=%d\n", fb->height,
  678. mode->vdisplay);
  679. return -EINVAL;
  680. } else if (fb->width > wb_cfg->sblk->maxlinewidth) {
  681. SDE_ERROR("invalid fb w=%d, maxlinewidth=%u\n",
  682. fb->width, wb_cfg->sblk->maxlinewidth);
  683. return -EINVAL;
  684. }
  685. }
  686. rc = _sde_enc_phys_wb_validate_cwb(phys_enc, crtc_state, conn_state);
  687. if (rc) {
  688. SDE_ERROR("failed in cwb validation %d\n", rc);
  689. return rc;
  690. }
  691. return rc;
  692. }
  693. static void _sde_encoder_phys_wb_update_cwb_flush(
  694. struct sde_encoder_phys *phys_enc, bool enable)
  695. {
  696. struct sde_encoder_phys_wb *wb_enc;
  697. struct sde_hw_wb *hw_wb;
  698. struct sde_hw_ctl *hw_ctl;
  699. struct sde_hw_cdm *hw_cdm;
  700. struct sde_hw_pingpong *hw_pp;
  701. struct sde_crtc *crtc;
  702. struct sde_crtc_state *crtc_state;
  703. int i = 0;
  704. int cwb_capture_mode = 0;
  705. enum sde_cwb cwb_idx = 0;
  706. enum sde_cwb src_pp_idx = 0;
  707. bool dspp_out = false;
  708. bool need_merge = false;
  709. if (!phys_enc->in_clone_mode) {
  710. SDE_DEBUG("not in CWB mode. early return\n");
  711. return;
  712. }
  713. wb_enc = to_sde_encoder_phys_wb(phys_enc);
  714. crtc = to_sde_crtc(wb_enc->crtc);
  715. crtc_state = to_sde_crtc_state(wb_enc->crtc->state);
  716. cwb_capture_mode = sde_crtc_get_property(crtc_state,
  717. CRTC_PROP_CAPTURE_OUTPUT);
  718. hw_pp = phys_enc->hw_pp;
  719. hw_wb = wb_enc->hw_wb;
  720. hw_cdm = phys_enc->hw_cdm;
  721. /* In CWB mode, program actual source master sde_hw_ctl from crtc */
  722. hw_ctl = crtc->mixers[0].hw_ctl;
  723. if (!hw_ctl || !hw_wb || !hw_pp) {
  724. SDE_ERROR("[wb] HW resource not available for CWB\n");
  725. return;
  726. }
  727. /* treating LM idx of primary display ctl path as source ping-pong idx*/
  728. src_pp_idx = (enum sde_cwb)crtc->mixers[0].hw_lm->idx;
  729. cwb_idx = (enum sde_cwb)hw_pp->idx;
  730. dspp_out = (cwb_capture_mode == CAPTURE_DSPP_OUT);
  731. need_merge = (crtc->num_mixers > 1) ? true : false;
  732. if (src_pp_idx > CWB_0 || ((cwb_idx + crtc->num_mixers) > CWB_MAX)) {
  733. SDE_ERROR("invalid hw config for CWB\n");
  734. return;
  735. }
  736. if (hw_ctl->ops.update_bitmask)
  737. hw_ctl->ops.update_bitmask(hw_ctl, SDE_HW_FLUSH_WB,
  738. hw_wb->idx, 1);
  739. if (hw_ctl->ops.update_bitmask && hw_cdm)
  740. hw_ctl->ops.update_bitmask(hw_ctl, SDE_HW_FLUSH_CDM,
  741. hw_cdm->idx, 1);
  742. if (test_bit(SDE_WB_CWB_CTRL, &hw_wb->caps->features)) {
  743. for (i = 0; i < crtc->num_mixers; i++) {
  744. cwb_idx = (enum sde_cwb) (hw_pp->idx + i);
  745. src_pp_idx = (enum sde_cwb) (src_pp_idx + i);
  746. if (hw_wb->ops.program_cwb_ctrl)
  747. hw_wb->ops.program_cwb_ctrl(hw_wb, cwb_idx,
  748. src_pp_idx, dspp_out, enable);
  749. if (hw_ctl->ops.update_bitmask)
  750. hw_ctl->ops.update_bitmask(hw_ctl,
  751. SDE_HW_FLUSH_CWB, cwb_idx, 1);
  752. }
  753. if (need_merge && hw_ctl->ops.update_bitmask
  754. && hw_pp && hw_pp->merge_3d)
  755. hw_ctl->ops.update_bitmask(hw_ctl,
  756. SDE_HW_FLUSH_MERGE_3D,
  757. hw_pp->merge_3d->idx, 1);
  758. } else {
  759. phys_enc->hw_mdptop->ops.set_cwb_ppb_cntl(phys_enc->hw_mdptop,
  760. need_merge, dspp_out);
  761. }
  762. }
  763. /**
  764. * _sde_encoder_phys_wb_update_flush - flush hardware update
  765. * @phys_enc: Pointer to physical encoder
  766. */
  767. static void _sde_encoder_phys_wb_update_flush(struct sde_encoder_phys *phys_enc)
  768. {
  769. struct sde_encoder_phys_wb *wb_enc;
  770. struct sde_hw_wb *hw_wb;
  771. struct sde_hw_ctl *hw_ctl;
  772. struct sde_hw_cdm *hw_cdm;
  773. struct sde_hw_pingpong *hw_pp;
  774. struct sde_ctl_flush_cfg pending_flush = {0,};
  775. if (!phys_enc)
  776. return;
  777. wb_enc = to_sde_encoder_phys_wb(phys_enc);
  778. hw_wb = wb_enc->hw_wb;
  779. hw_cdm = phys_enc->hw_cdm;
  780. hw_pp = phys_enc->hw_pp;
  781. hw_ctl = phys_enc->hw_ctl;
  782. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  783. if (phys_enc->in_clone_mode) {
  784. SDE_DEBUG("in CWB mode. early return\n");
  785. return;
  786. }
  787. if (!hw_ctl) {
  788. SDE_DEBUG("[wb:%d] no ctl assigned\n", hw_wb->idx - WB_0);
  789. return;
  790. }
  791. if (hw_ctl->ops.update_bitmask)
  792. hw_ctl->ops.update_bitmask(hw_ctl, SDE_HW_FLUSH_WB,
  793. hw_wb->idx, 1);
  794. if (hw_ctl->ops.update_bitmask && hw_cdm)
  795. hw_ctl->ops.update_bitmask(hw_ctl, SDE_HW_FLUSH_CDM,
  796. hw_cdm->idx, 1);
  797. if (hw_ctl->ops.update_bitmask && hw_pp && hw_pp->merge_3d)
  798. hw_ctl->ops.update_bitmask(hw_ctl, SDE_HW_FLUSH_MERGE_3D,
  799. hw_pp->merge_3d->idx, 1);
  800. if (hw_ctl->ops.get_pending_flush)
  801. hw_ctl->ops.get_pending_flush(hw_ctl,
  802. &pending_flush);
  803. SDE_DEBUG("Pending flush mask for CTL_%d is 0x%x, WB %d\n",
  804. hw_ctl->idx - CTL_0, pending_flush.pending_flush_mask,
  805. hw_wb->idx - WB_0);
  806. }
  807. /**
  808. * sde_encoder_phys_wb_setup - setup writeback encoder
  809. * @phys_enc: Pointer to physical encoder
  810. */
  811. static void sde_encoder_phys_wb_setup(
  812. struct sde_encoder_phys *phys_enc)
  813. {
  814. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  815. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  816. struct drm_display_mode mode = phys_enc->cached_mode;
  817. struct drm_framebuffer *fb;
  818. struct sde_rect *wb_roi = &wb_enc->wb_roi;
  819. SDE_DEBUG("[mode_set:%d,\"%s\",%d,%d]\n",
  820. hw_wb->idx - WB_0, mode.name,
  821. mode.hdisplay, mode.vdisplay);
  822. memset(wb_roi, 0, sizeof(struct sde_rect));
  823. /* clear writeback framebuffer - will be updated in setup_fb */
  824. wb_enc->wb_fb = NULL;
  825. wb_enc->wb_aspace = NULL;
  826. if (phys_enc->enable_state == SDE_ENC_DISABLING) {
  827. fb = wb_enc->fb_disable;
  828. wb_roi->w = 0;
  829. wb_roi->h = 0;
  830. } else {
  831. fb = sde_wb_get_output_fb(wb_enc->wb_dev);
  832. sde_wb_get_output_roi(wb_enc->wb_dev, wb_roi);
  833. }
  834. if (!fb) {
  835. SDE_DEBUG("no output framebuffer\n");
  836. return;
  837. }
  838. SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
  839. fb->width, fb->height);
  840. if (wb_roi->w == 0 || wb_roi->h == 0) {
  841. wb_roi->x = 0;
  842. wb_roi->y = 0;
  843. wb_roi->w = fb->width;
  844. wb_roi->h = fb->height;
  845. }
  846. SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi->x, wb_roi->y,
  847. wb_roi->w, wb_roi->h);
  848. wb_enc->wb_fmt = sde_get_sde_format_ext(fb->format->format,
  849. fb->modifier);
  850. if (!wb_enc->wb_fmt) {
  851. SDE_ERROR("unsupported output pixel format: %d\n",
  852. fb->format->format);
  853. return;
  854. }
  855. SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->format->format,
  856. fb->modifier);
  857. sde_encoder_phys_wb_set_ot_limit(phys_enc);
  858. sde_encoder_phys_wb_set_qos_remap(phys_enc);
  859. sde_encoder_phys_wb_set_qos(phys_enc);
  860. sde_encoder_phys_setup_cdm(phys_enc, fb, wb_enc->wb_fmt, wb_roi);
  861. sde_encoder_phys_wb_setup_fb(phys_enc, fb, wb_roi);
  862. sde_encoder_phys_wb_setup_cdp(phys_enc, wb_enc->wb_fmt);
  863. _sde_encoder_phys_wb_setup_cwb(phys_enc, true);
  864. }
  865. static void _sde_encoder_phys_wb_frame_done_helper(void *arg, bool frame_error)
  866. {
  867. struct sde_encoder_phys_wb *wb_enc = arg;
  868. struct sde_encoder_phys *phys_enc = &wb_enc->base;
  869. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  870. u32 event = frame_error ? SDE_ENCODER_FRAME_EVENT_ERROR : 0;
  871. SDE_DEBUG("[wb:%d,%u]\n", hw_wb->idx - WB_0, wb_enc->frame_count);
  872. /* don't notify upper layer for internal commit */
  873. if (phys_enc->enable_state == SDE_ENC_DISABLING)
  874. goto complete;
  875. if (phys_enc->parent_ops.handle_frame_done &&
  876. atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0)) {
  877. event |= SDE_ENCODER_FRAME_EVENT_DONE |
  878. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  879. if (!phys_enc->in_clone_mode)
  880. event |= SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  881. phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
  882. phys_enc, event);
  883. }
  884. if (phys_enc->parent_ops.handle_vblank_virt)
  885. phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
  886. phys_enc);
  887. SDE_EVT32_IRQ(DRMID(phys_enc->parent), hw_wb->idx - WB_0, event,
  888. frame_error);
  889. complete:
  890. wake_up_all(&phys_enc->pending_kickoff_wq);
  891. }
  892. /**
  893. * sde_encoder_phys_wb_done_irq - Pingpong overflow interrupt handler for CWB
  894. * @arg: Pointer to writeback encoder
  895. * @irq_idx: interrupt index
  896. */
  897. static void sde_encoder_phys_cwb_ovflow(void *arg, int irq_idx)
  898. {
  899. _sde_encoder_phys_wb_frame_done_helper(arg, true);
  900. }
  901. /**
  902. * sde_encoder_phys_wb_done_irq - writeback interrupt handler
  903. * @arg: Pointer to writeback encoder
  904. * @irq_idx: interrupt index
  905. */
  906. static void sde_encoder_phys_wb_done_irq(void *arg, int irq_idx)
  907. {
  908. _sde_encoder_phys_wb_frame_done_helper(arg, false);
  909. }
  910. /**
  911. * sde_encoder_phys_wb_irq_ctrl - irq control of WB
  912. * @phys: Pointer to physical encoder
  913. * @enable: indicates enable or disable interrupts
  914. */
  915. static void sde_encoder_phys_wb_irq_ctrl(
  916. struct sde_encoder_phys *phys, bool enable)
  917. {
  918. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys);
  919. int index = 0, refcount;
  920. int ret = 0, pp = 0;
  921. if (!wb_enc)
  922. return;
  923. if (wb_enc->bypass_irqreg)
  924. return;
  925. pp = phys->hw_pp->idx - PINGPONG_0;
  926. if ((pp + CRTC_DUAL_MIXERS) >= PINGPONG_MAX) {
  927. SDE_ERROR("invalid pingpong index for WB or CWB\n");
  928. return;
  929. }
  930. refcount = atomic_read(&phys->wbirq_refcount);
  931. if (enable && atomic_inc_return(&phys->wbirq_refcount) == 1) {
  932. sde_encoder_helper_register_irq(phys, INTR_IDX_WB_DONE);
  933. if (ret)
  934. atomic_dec_return(&phys->wbirq_refcount);
  935. for (index = 0; index < CRTC_DUAL_MIXERS; index++)
  936. if (cwb_irq_tbl[index + pp] != SDE_NONE)
  937. sde_encoder_helper_register_irq(phys,
  938. cwb_irq_tbl[index + pp]);
  939. } else if (!enable &&
  940. atomic_dec_return(&phys->wbirq_refcount) == 0) {
  941. sde_encoder_helper_unregister_irq(phys, INTR_IDX_WB_DONE);
  942. if (ret)
  943. atomic_inc_return(&phys->wbirq_refcount);
  944. for (index = 0; index < CRTC_DUAL_MIXERS; index++)
  945. if (cwb_irq_tbl[index + pp] != SDE_NONE)
  946. sde_encoder_helper_unregister_irq(phys,
  947. cwb_irq_tbl[index + pp]);
  948. }
  949. }
  950. /**
  951. * sde_encoder_phys_wb_mode_set - set display mode
  952. * @phys_enc: Pointer to physical encoder
  953. * @mode: Pointer to requested display mode
  954. * @adj_mode: Pointer to adjusted display mode
  955. */
  956. static void sde_encoder_phys_wb_mode_set(
  957. struct sde_encoder_phys *phys_enc,
  958. struct drm_display_mode *mode,
  959. struct drm_display_mode *adj_mode)
  960. {
  961. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  962. struct sde_rm *rm = &phys_enc->sde_kms->rm;
  963. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  964. struct sde_rm_hw_iter iter;
  965. int i, instance;
  966. phys_enc->cached_mode = *adj_mode;
  967. instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
  968. SDE_DEBUG("[mode_set_cache:%d,\"%s\",%d,%d]\n",
  969. hw_wb->idx - WB_0, mode->name,
  970. mode->hdisplay, mode->vdisplay);
  971. phys_enc->hw_ctl = NULL;
  972. phys_enc->hw_cdm = NULL;
  973. /* Retrieve previously allocated HW Resources. CTL shouldn't fail */
  974. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
  975. for (i = 0; i <= instance; i++) {
  976. sde_rm_get_hw(rm, &iter);
  977. if (i == instance)
  978. phys_enc->hw_ctl = (struct sde_hw_ctl *) iter.hw;
  979. }
  980. if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
  981. SDE_ERROR("failed init ctl: %ld\n",
  982. (!phys_enc->hw_ctl) ?
  983. -EINVAL : PTR_ERR(phys_enc->hw_ctl));
  984. phys_enc->hw_ctl = NULL;
  985. return;
  986. }
  987. /* CDM is optional */
  988. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CDM);
  989. for (i = 0; i <= instance; i++) {
  990. sde_rm_get_hw(rm, &iter);
  991. if (i == instance)
  992. phys_enc->hw_cdm = (struct sde_hw_cdm *) iter.hw;
  993. }
  994. if (IS_ERR(phys_enc->hw_cdm)) {
  995. SDE_ERROR("CDM required but not allocated: %ld\n",
  996. PTR_ERR(phys_enc->hw_cdm));
  997. phys_enc->hw_cdm = NULL;
  998. }
  999. }
  1000. static int sde_encoder_phys_wb_frame_timeout(struct sde_encoder_phys *phys_enc)
  1001. {
  1002. u32 event = 0;
  1003. while (atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0) &&
  1004. phys_enc->parent_ops.handle_frame_done) {
  1005. event = SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE
  1006. | SDE_ENCODER_FRAME_EVENT_ERROR;
  1007. if (!phys_enc->in_clone_mode)
  1008. event |= SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  1009. phys_enc->parent_ops.handle_frame_done(
  1010. phys_enc->parent, phys_enc, event);
  1011. SDE_EVT32(DRMID(phys_enc->parent), event,
  1012. atomic_read(&phys_enc->pending_retire_fence_cnt));
  1013. }
  1014. return event;
  1015. }
  1016. static int _sde_encoder_phys_wb_wait_for_commit_done(
  1017. struct sde_encoder_phys *phys_enc, bool is_disable)
  1018. {
  1019. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1020. u32 event = 0;
  1021. u64 wb_time = 0;
  1022. int rc = 0;
  1023. struct sde_encoder_wait_info wait_info = {0};
  1024. /* Return EWOULDBLOCK since we know the wait isn't necessary */
  1025. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1026. SDE_ERROR("encoder already disabled\n");
  1027. return -EWOULDBLOCK;
  1028. }
  1029. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
  1030. wb_enc->kickoff_count, !!wb_enc->wb_fb, is_disable,
  1031. phys_enc->in_clone_mode);
  1032. if (!is_disable && phys_enc->in_clone_mode &&
  1033. (atomic_read(&phys_enc->pending_retire_fence_cnt) <= 1))
  1034. goto skip_wait;
  1035. /* signal completion if commit with no framebuffer */
  1036. if (!wb_enc->wb_fb) {
  1037. SDE_DEBUG("no output framebuffer\n");
  1038. _sde_encoder_phys_wb_frame_done_helper(wb_enc, false);
  1039. }
  1040. wait_info.wq = &phys_enc->pending_kickoff_wq;
  1041. wait_info.atomic_cnt = &phys_enc->pending_retire_fence_cnt;
  1042. wait_info.timeout_ms = max_t(u32, wb_enc->wbdone_timeout,
  1043. KICKOFF_TIMEOUT_MS);
  1044. rc = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_WB_DONE,
  1045. &wait_info);
  1046. if (rc == -ETIMEDOUT) {
  1047. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
  1048. wb_enc->frame_count, SDE_EVTLOG_ERROR);
  1049. SDE_ERROR("wb:%d kickoff timed out\n", WBID(wb_enc));
  1050. event = sde_encoder_phys_wb_frame_timeout(phys_enc);
  1051. }
  1052. /* cleanup writeback framebuffer */
  1053. if (wb_enc->wb_fb && wb_enc->wb_aspace) {
  1054. msm_framebuffer_cleanup(wb_enc->wb_fb, wb_enc->wb_aspace);
  1055. drm_framebuffer_put(wb_enc->wb_fb);
  1056. wb_enc->wb_fb = NULL;
  1057. wb_enc->wb_aspace = NULL;
  1058. }
  1059. skip_wait:
  1060. /* remove vote for iommu/clk/bus */
  1061. wb_enc->frame_count++;
  1062. if (!rc) {
  1063. wb_enc->end_time = ktime_get();
  1064. wb_time = (u64)ktime_to_us(wb_enc->end_time) -
  1065. (u64)ktime_to_us(wb_enc->start_time);
  1066. SDE_DEBUG("wb:%d took %llu us\n", WBID(wb_enc), wb_time);
  1067. }
  1068. /* cleanup previous buffer if pending */
  1069. if (wb_enc->cwb_old_fb && wb_enc->cwb_old_aspace) {
  1070. msm_framebuffer_cleanup(wb_enc->cwb_old_fb, wb_enc->cwb_old_aspace);
  1071. drm_framebuffer_put(wb_enc->cwb_old_fb);
  1072. wb_enc->cwb_old_fb = NULL;
  1073. wb_enc->cwb_old_aspace = NULL;
  1074. }
  1075. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
  1076. wb_time, event, rc);
  1077. return rc;
  1078. }
  1079. /**
  1080. * sde_encoder_phys_wb_wait_for_commit_done - wait until request is committed
  1081. * @phys_enc: Pointer to physical encoder
  1082. */
  1083. static int sde_encoder_phys_wb_wait_for_commit_done(
  1084. struct sde_encoder_phys *phys_enc)
  1085. {
  1086. return _sde_encoder_phys_wb_wait_for_commit_done(phys_enc, false);
  1087. }
  1088. /**
  1089. * sde_encoder_phys_wb_prepare_for_kickoff - pre-kickoff processing
  1090. * @phys_enc: Pointer to physical encoder
  1091. * @params: kickoff parameters
  1092. * Returns: Zero on success
  1093. */
  1094. static int sde_encoder_phys_wb_prepare_for_kickoff(
  1095. struct sde_encoder_phys *phys_enc,
  1096. struct sde_encoder_kickoff_params *params)
  1097. {
  1098. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1099. SDE_DEBUG("[wb:%d,%u]\n", wb_enc->hw_wb->idx - WB_0,
  1100. wb_enc->kickoff_count);
  1101. if (phys_enc->in_clone_mode) {
  1102. wb_enc->cwb_old_fb = wb_enc->wb_fb;
  1103. wb_enc->cwb_old_aspace = wb_enc->wb_aspace;
  1104. }
  1105. wb_enc->kickoff_count++;
  1106. /* set OT limit & enable traffic shaper */
  1107. sde_encoder_phys_wb_setup(phys_enc);
  1108. _sde_encoder_phys_wb_update_flush(phys_enc);
  1109. _sde_encoder_phys_wb_update_cwb_flush(phys_enc, true);
  1110. /* vote for iommu/clk/bus */
  1111. wb_enc->start_time = ktime_get();
  1112. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
  1113. wb_enc->kickoff_count, wb_enc->frame_count,
  1114. phys_enc->in_clone_mode);
  1115. return 0;
  1116. }
  1117. /**
  1118. * sde_encoder_phys_wb_trigger_flush - trigger flush processing
  1119. * @phys_enc: Pointer to physical encoder
  1120. */
  1121. static void sde_encoder_phys_wb_trigger_flush(struct sde_encoder_phys *phys_enc)
  1122. {
  1123. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1124. if (!phys_enc || !wb_enc->hw_wb) {
  1125. SDE_ERROR("invalid encoder\n");
  1126. return;
  1127. }
  1128. /*
  1129. * Bail out iff in CWB mode. In case of CWB, primary control-path
  1130. * which is actually driving would trigger the flush
  1131. */
  1132. if (phys_enc->in_clone_mode) {
  1133. SDE_DEBUG("in CWB mode. early return\n");
  1134. return;
  1135. }
  1136. SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
  1137. /* clear pending flush if commit with no framebuffer */
  1138. if (!wb_enc->wb_fb) {
  1139. SDE_DEBUG("no output framebuffer\n");
  1140. return;
  1141. }
  1142. sde_encoder_helper_trigger_flush(phys_enc);
  1143. }
  1144. /**
  1145. * sde_encoder_phys_wb_handle_post_kickoff - post-kickoff processing
  1146. * @phys_enc: Pointer to physical encoder
  1147. */
  1148. static void sde_encoder_phys_wb_handle_post_kickoff(
  1149. struct sde_encoder_phys *phys_enc)
  1150. {
  1151. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1152. SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
  1153. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc));
  1154. }
  1155. /**
  1156. * _sde_encoder_phys_wb_init_internal_fb - create fb for internal commit
  1157. * @wb_enc: Pointer to writeback encoder
  1158. * @pixel_format: DRM pixel format
  1159. * @width: Desired fb width
  1160. * @height: Desired fb height
  1161. * @pitch: Desired fb pitch
  1162. */
  1163. static int _sde_encoder_phys_wb_init_internal_fb(
  1164. struct sde_encoder_phys_wb *wb_enc,
  1165. uint32_t pixel_format, uint32_t width,
  1166. uint32_t height, uint32_t pitch)
  1167. {
  1168. struct drm_device *dev;
  1169. struct drm_framebuffer *fb;
  1170. struct drm_mode_fb_cmd2 mode_cmd;
  1171. uint32_t size;
  1172. int nplanes, i, ret;
  1173. struct msm_gem_address_space *aspace;
  1174. const struct drm_format_info *info;
  1175. if (!wb_enc || !wb_enc->base.parent || !wb_enc->base.sde_kms) {
  1176. SDE_ERROR("invalid params\n");
  1177. return -EINVAL;
  1178. }
  1179. aspace = wb_enc->base.sde_kms->aspace[SDE_IOMMU_DOMAIN_UNSECURE];
  1180. if (!aspace) {
  1181. SDE_ERROR("invalid address space\n");
  1182. return -EINVAL;
  1183. }
  1184. dev = wb_enc->base.sde_kms->dev;
  1185. if (!dev) {
  1186. SDE_ERROR("invalid dev\n");
  1187. return -EINVAL;
  1188. }
  1189. memset(&mode_cmd, 0, sizeof(mode_cmd));
  1190. mode_cmd.pixel_format = pixel_format;
  1191. mode_cmd.width = width;
  1192. mode_cmd.height = height;
  1193. mode_cmd.pitches[0] = pitch;
  1194. size = sde_format_get_framebuffer_size(pixel_format,
  1195. mode_cmd.width, mode_cmd.height,
  1196. mode_cmd.pitches, 0);
  1197. if (!size) {
  1198. SDE_DEBUG("not creating zero size buffer\n");
  1199. return -EINVAL;
  1200. }
  1201. /* allocate gem tracking object */
  1202. info = drm_get_format_info(dev, &mode_cmd);
  1203. nplanes = info->num_planes;
  1204. if (nplanes >= SDE_MAX_PLANES) {
  1205. SDE_ERROR("requested format has too many planes\n");
  1206. return -EINVAL;
  1207. }
  1208. wb_enc->bo_disable[0] = msm_gem_new(dev, size,
  1209. MSM_BO_SCANOUT | MSM_BO_WC);
  1210. if (IS_ERR_OR_NULL(wb_enc->bo_disable[0])) {
  1211. ret = PTR_ERR(wb_enc->bo_disable[0]);
  1212. wb_enc->bo_disable[0] = NULL;
  1213. SDE_ERROR("failed to create bo, %d\n", ret);
  1214. return ret;
  1215. }
  1216. for (i = 0; i < nplanes; ++i) {
  1217. wb_enc->bo_disable[i] = wb_enc->bo_disable[0];
  1218. mode_cmd.pitches[i] = width * info->cpp[i];
  1219. }
  1220. fb = msm_framebuffer_init(dev, &mode_cmd, wb_enc->bo_disable);
  1221. if (IS_ERR_OR_NULL(fb)) {
  1222. ret = PTR_ERR(fb);
  1223. drm_gem_object_put(wb_enc->bo_disable[0]);
  1224. wb_enc->bo_disable[0] = NULL;
  1225. SDE_ERROR("failed to init fb, %d\n", ret);
  1226. return ret;
  1227. }
  1228. /* prepare the backing buffer now so that it's available later */
  1229. ret = msm_framebuffer_prepare(fb, aspace);
  1230. if (!ret)
  1231. wb_enc->fb_disable = fb;
  1232. return ret;
  1233. }
  1234. /**
  1235. * _sde_encoder_phys_wb_destroy_internal_fb - deconstruct internal fb
  1236. * @wb_enc: Pointer to writeback encoder
  1237. */
  1238. static void _sde_encoder_phys_wb_destroy_internal_fb(
  1239. struct sde_encoder_phys_wb *wb_enc)
  1240. {
  1241. if (!wb_enc)
  1242. return;
  1243. if (wb_enc->fb_disable) {
  1244. drm_framebuffer_unregister_private(wb_enc->fb_disable);
  1245. drm_framebuffer_remove(wb_enc->fb_disable);
  1246. wb_enc->fb_disable = NULL;
  1247. }
  1248. if (wb_enc->bo_disable[0]) {
  1249. drm_gem_object_put(wb_enc->bo_disable[0]);
  1250. wb_enc->bo_disable[0] = NULL;
  1251. }
  1252. }
  1253. /**
  1254. * sde_encoder_phys_wb_enable - enable writeback encoder
  1255. * @phys_enc: Pointer to physical encoder
  1256. */
  1257. static void sde_encoder_phys_wb_enable(struct sde_encoder_phys *phys_enc)
  1258. {
  1259. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1260. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  1261. struct drm_device *dev;
  1262. struct drm_connector *connector;
  1263. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  1264. if (!wb_enc->base.parent || !wb_enc->base.parent->dev) {
  1265. SDE_ERROR("invalid drm device\n");
  1266. return;
  1267. }
  1268. dev = wb_enc->base.parent->dev;
  1269. /* find associated writeback connector */
  1270. connector = phys_enc->connector;
  1271. if (!connector || connector->encoder != phys_enc->parent) {
  1272. SDE_ERROR("failed to find writeback connector\n");
  1273. return;
  1274. }
  1275. wb_enc->wb_dev = sde_wb_connector_get_wb(connector);
  1276. phys_enc->enable_state = SDE_ENC_ENABLED;
  1277. /*
  1278. * cache the crtc in wb_enc on enable for duration of use case
  1279. * for correctly servicing asynchronous irq events and timers
  1280. */
  1281. wb_enc->crtc = phys_enc->parent->crtc;
  1282. }
  1283. /**
  1284. * sde_encoder_phys_wb_disable - disable writeback encoder
  1285. * @phys_enc: Pointer to physical encoder
  1286. */
  1287. static void sde_encoder_phys_wb_disable(struct sde_encoder_phys *phys_enc)
  1288. {
  1289. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1290. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  1291. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  1292. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1293. SDE_ERROR("encoder is already disabled\n");
  1294. return;
  1295. }
  1296. SDE_DEBUG("[wait_for_done: wb:%d, frame:%u, kickoff:%u]\n",
  1297. hw_wb->idx - WB_0, wb_enc->frame_count,
  1298. wb_enc->kickoff_count);
  1299. _sde_encoder_phys_wb_wait_for_commit_done(phys_enc, true);
  1300. if (!phys_enc->hw_ctl || !phys_enc->parent ||
  1301. !phys_enc->sde_kms || !wb_enc->fb_disable) {
  1302. SDE_DEBUG("invalid enc, skipping extra commit\n");
  1303. goto exit;
  1304. }
  1305. /* avoid reset frame for CWB */
  1306. if (phys_enc->in_clone_mode) {
  1307. _sde_encoder_phys_wb_setup_cwb(phys_enc, false);
  1308. _sde_encoder_phys_wb_update_cwb_flush(phys_enc, false);
  1309. phys_enc->in_clone_mode = false;
  1310. goto exit;
  1311. }
  1312. /* reset h/w before final flush */
  1313. if (phys_enc->hw_ctl->ops.clear_pending_flush)
  1314. phys_enc->hw_ctl->ops.clear_pending_flush(phys_enc->hw_ctl);
  1315. /*
  1316. * New CTL reset sequence from 5.0 MDP onwards.
  1317. * If has_3d_merge_reset is not set, legacy reset
  1318. * sequence is executed.
  1319. */
  1320. if (hw_wb->catalog->has_3d_merge_reset) {
  1321. sde_encoder_helper_phys_disable(phys_enc, wb_enc);
  1322. goto exit;
  1323. }
  1324. if (sde_encoder_helper_reset_mixers(phys_enc, NULL))
  1325. goto exit;
  1326. phys_enc->enable_state = SDE_ENC_DISABLING;
  1327. sde_encoder_phys_wb_prepare_for_kickoff(phys_enc, NULL);
  1328. sde_encoder_phys_wb_irq_ctrl(phys_enc, true);
  1329. if (phys_enc->hw_ctl->ops.trigger_flush)
  1330. phys_enc->hw_ctl->ops.trigger_flush(phys_enc->hw_ctl);
  1331. sde_encoder_helper_trigger_start(phys_enc);
  1332. _sde_encoder_phys_wb_wait_for_commit_done(phys_enc, true);
  1333. sde_encoder_phys_wb_irq_ctrl(phys_enc, false);
  1334. exit:
  1335. /*
  1336. * frame count and kickoff count are only used for debug purpose. Frame
  1337. * count can be more than kickoff count at the end of disable call due
  1338. * to extra frame_done wait. It does not cause any issue because
  1339. * frame_done wait is based on retire_fence count. Leaving these
  1340. * counters for debugging purpose.
  1341. */
  1342. if (wb_enc->frame_count != wb_enc->kickoff_count) {
  1343. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
  1344. wb_enc->kickoff_count, wb_enc->frame_count,
  1345. phys_enc->in_clone_mode);
  1346. wb_enc->frame_count = wb_enc->kickoff_count;
  1347. }
  1348. phys_enc->enable_state = SDE_ENC_DISABLED;
  1349. wb_enc->crtc = NULL;
  1350. }
  1351. /**
  1352. * sde_encoder_phys_wb_get_hw_resources - get hardware resources
  1353. * @phys_enc: Pointer to physical encoder
  1354. * @hw_res: Pointer to encoder resources
  1355. */
  1356. static void sde_encoder_phys_wb_get_hw_resources(
  1357. struct sde_encoder_phys *phys_enc,
  1358. struct sde_encoder_hw_resources *hw_res,
  1359. struct drm_connector_state *conn_state)
  1360. {
  1361. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1362. struct sde_hw_wb *hw_wb;
  1363. struct drm_framebuffer *fb;
  1364. const struct sde_format *fmt = NULL;
  1365. if (!phys_enc) {
  1366. SDE_ERROR("invalid encoder\n");
  1367. return;
  1368. }
  1369. fb = sde_wb_connector_state_get_output_fb(conn_state);
  1370. if (fb) {
  1371. fmt = sde_get_sde_format_ext(fb->format->format, fb->modifier);
  1372. if (!fmt) {
  1373. SDE_ERROR("unsupported output pixel format:%d\n",
  1374. fb->format->format);
  1375. return;
  1376. }
  1377. }
  1378. hw_wb = wb_enc->hw_wb;
  1379. hw_res->wbs[hw_wb->idx - WB_0] = phys_enc->intf_mode;
  1380. hw_res->needs_cdm = fmt ? SDE_FORMAT_IS_YUV(fmt) : false;
  1381. SDE_DEBUG("[wb:%d] intf_mode=%d needs_cdm=%d\n", hw_wb->idx - WB_0,
  1382. hw_res->wbs[hw_wb->idx - WB_0],
  1383. hw_res->needs_cdm);
  1384. }
  1385. #ifdef CONFIG_DEBUG_FS
  1386. /**
  1387. * sde_encoder_phys_wb_init_debugfs - initialize writeback encoder debugfs
  1388. * @phys_enc: Pointer to physical encoder
  1389. * @debugfs_root: Pointer to virtual encoder's debugfs_root dir
  1390. */
  1391. static int sde_encoder_phys_wb_init_debugfs(
  1392. struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
  1393. {
  1394. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1395. if (!phys_enc || !wb_enc->hw_wb || !debugfs_root)
  1396. return -EINVAL;
  1397. if (!debugfs_create_u32("wbdone_timeout", 0600,
  1398. debugfs_root, &wb_enc->wbdone_timeout)) {
  1399. SDE_ERROR("failed to create debugfs/wbdone_timeout\n");
  1400. return -ENOMEM;
  1401. }
  1402. return 0;
  1403. }
  1404. #else
  1405. static int sde_encoder_phys_wb_init_debugfs(
  1406. struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
  1407. {
  1408. return 0;
  1409. }
  1410. #endif
  1411. static int sde_encoder_phys_wb_late_register(struct sde_encoder_phys *phys_enc,
  1412. struct dentry *debugfs_root)
  1413. {
  1414. return sde_encoder_phys_wb_init_debugfs(phys_enc, debugfs_root);
  1415. }
  1416. /**
  1417. * sde_encoder_phys_wb_destroy - destroy writeback encoder
  1418. * @phys_enc: Pointer to physical encoder
  1419. */
  1420. static void sde_encoder_phys_wb_destroy(struct sde_encoder_phys *phys_enc)
  1421. {
  1422. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1423. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  1424. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  1425. if (!phys_enc)
  1426. return;
  1427. _sde_encoder_phys_wb_destroy_internal_fb(wb_enc);
  1428. kfree(wb_enc);
  1429. }
  1430. /**
  1431. * sde_encoder_phys_wb_init_ops - initialize writeback operations
  1432. * @ops: Pointer to encoder operation table
  1433. */
  1434. static void sde_encoder_phys_wb_init_ops(struct sde_encoder_phys_ops *ops)
  1435. {
  1436. ops->late_register = sde_encoder_phys_wb_late_register;
  1437. ops->is_master = sde_encoder_phys_wb_is_master;
  1438. ops->mode_set = sde_encoder_phys_wb_mode_set;
  1439. ops->enable = sde_encoder_phys_wb_enable;
  1440. ops->disable = sde_encoder_phys_wb_disable;
  1441. ops->destroy = sde_encoder_phys_wb_destroy;
  1442. ops->atomic_check = sde_encoder_phys_wb_atomic_check;
  1443. ops->get_hw_resources = sde_encoder_phys_wb_get_hw_resources;
  1444. ops->wait_for_commit_done = sde_encoder_phys_wb_wait_for_commit_done;
  1445. ops->prepare_for_kickoff = sde_encoder_phys_wb_prepare_for_kickoff;
  1446. ops->handle_post_kickoff = sde_encoder_phys_wb_handle_post_kickoff;
  1447. ops->trigger_flush = sde_encoder_phys_wb_trigger_flush;
  1448. ops->trigger_start = sde_encoder_helper_trigger_start;
  1449. ops->hw_reset = sde_encoder_helper_hw_reset;
  1450. ops->irq_control = sde_encoder_phys_wb_irq_ctrl;
  1451. }
  1452. /**
  1453. * sde_encoder_phys_wb_init - initialize writeback encoder
  1454. * @init: Pointer to init info structure with initialization params
  1455. */
  1456. struct sde_encoder_phys *sde_encoder_phys_wb_init(
  1457. struct sde_enc_phys_init_params *p)
  1458. {
  1459. struct sde_encoder_phys *phys_enc;
  1460. struct sde_encoder_phys_wb *wb_enc;
  1461. struct sde_hw_mdp *hw_mdp;
  1462. struct sde_encoder_irq *irq;
  1463. int ret = 0;
  1464. SDE_DEBUG("\n");
  1465. if (!p || !p->parent) {
  1466. SDE_ERROR("invalid params\n");
  1467. ret = -EINVAL;
  1468. goto fail_alloc;
  1469. }
  1470. wb_enc = kzalloc(sizeof(*wb_enc), GFP_KERNEL);
  1471. if (!wb_enc) {
  1472. SDE_ERROR("failed to allocate wb enc\n");
  1473. ret = -ENOMEM;
  1474. goto fail_alloc;
  1475. }
  1476. wb_enc->wbdone_timeout = KICKOFF_TIMEOUT_MS;
  1477. phys_enc = &wb_enc->base;
  1478. if (p->sde_kms->vbif[VBIF_NRT]) {
  1479. wb_enc->aspace[SDE_IOMMU_DOMAIN_UNSECURE] =
  1480. p->sde_kms->aspace[MSM_SMMU_DOMAIN_NRT_UNSECURE];
  1481. wb_enc->aspace[SDE_IOMMU_DOMAIN_SECURE] =
  1482. p->sde_kms->aspace[MSM_SMMU_DOMAIN_NRT_SECURE];
  1483. } else {
  1484. wb_enc->aspace[SDE_IOMMU_DOMAIN_UNSECURE] =
  1485. p->sde_kms->aspace[MSM_SMMU_DOMAIN_UNSECURE];
  1486. wb_enc->aspace[SDE_IOMMU_DOMAIN_SECURE] =
  1487. p->sde_kms->aspace[MSM_SMMU_DOMAIN_SECURE];
  1488. }
  1489. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  1490. if (IS_ERR_OR_NULL(hw_mdp)) {
  1491. ret = PTR_ERR(hw_mdp);
  1492. SDE_ERROR("failed to init hw_top: %d\n", ret);
  1493. goto fail_mdp_init;
  1494. }
  1495. phys_enc->hw_mdptop = hw_mdp;
  1496. /**
  1497. * hw_wb resource permanently assigned to this encoder
  1498. * Other resources allocated at atomic commit time by use case
  1499. */
  1500. if (p->wb_idx != SDE_NONE) {
  1501. struct sde_rm_hw_iter iter;
  1502. sde_rm_init_hw_iter(&iter, 0, SDE_HW_BLK_WB);
  1503. while (sde_rm_get_hw(&p->sde_kms->rm, &iter)) {
  1504. struct sde_hw_wb *hw_wb = (struct sde_hw_wb *)iter.hw;
  1505. if (hw_wb->idx == p->wb_idx) {
  1506. wb_enc->hw_wb = hw_wb;
  1507. break;
  1508. }
  1509. }
  1510. if (!wb_enc->hw_wb) {
  1511. ret = -EINVAL;
  1512. SDE_ERROR("failed to init hw_wb%d\n", p->wb_idx - WB_0);
  1513. goto fail_wb_init;
  1514. }
  1515. } else {
  1516. ret = -EINVAL;
  1517. SDE_ERROR("invalid wb_idx\n");
  1518. goto fail_wb_check;
  1519. }
  1520. sde_encoder_phys_wb_init_ops(&phys_enc->ops);
  1521. phys_enc->parent = p->parent;
  1522. phys_enc->parent_ops = p->parent_ops;
  1523. phys_enc->sde_kms = p->sde_kms;
  1524. phys_enc->split_role = p->split_role;
  1525. phys_enc->intf_mode = INTF_MODE_WB_LINE;
  1526. phys_enc->intf_idx = p->intf_idx;
  1527. phys_enc->enc_spinlock = p->enc_spinlock;
  1528. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  1529. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  1530. atomic_set(&phys_enc->wbirq_refcount, 0);
  1531. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  1532. irq = &phys_enc->irq[INTR_IDX_WB_DONE];
  1533. INIT_LIST_HEAD(&irq->cb.list);
  1534. irq->name = "wb_done";
  1535. irq->hw_idx = wb_enc->hw_wb->idx;
  1536. irq->irq_idx = -1;
  1537. irq->intr_type = sde_encoder_phys_wb_get_intr_type(wb_enc->hw_wb);
  1538. irq->intr_idx = INTR_IDX_WB_DONE;
  1539. irq->cb.arg = wb_enc;
  1540. irq->cb.func = sde_encoder_phys_wb_done_irq;
  1541. irq = &phys_enc->irq[INTR_IDX_PP1_OVFL];
  1542. INIT_LIST_HEAD(&irq->cb.list);
  1543. irq->name = "pp1_overflow";
  1544. irq->hw_idx = CWB_1;
  1545. irq->irq_idx = -1;
  1546. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1547. irq->intr_idx = INTR_IDX_PP1_OVFL;
  1548. irq->cb.arg = wb_enc;
  1549. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1550. irq = &phys_enc->irq[INTR_IDX_PP2_OVFL];
  1551. INIT_LIST_HEAD(&irq->cb.list);
  1552. irq->name = "pp2_overflow";
  1553. irq->hw_idx = CWB_2;
  1554. irq->irq_idx = -1;
  1555. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1556. irq->intr_idx = INTR_IDX_PP2_OVFL;
  1557. irq->cb.arg = wb_enc;
  1558. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1559. irq = &phys_enc->irq[INTR_IDX_PP3_OVFL];
  1560. INIT_LIST_HEAD(&irq->cb.list);
  1561. irq->name = "pp3_overflow";
  1562. irq->hw_idx = CWB_3;
  1563. irq->irq_idx = -1;
  1564. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1565. irq->intr_idx = INTR_IDX_PP3_OVFL;
  1566. irq->cb.arg = wb_enc;
  1567. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1568. irq = &phys_enc->irq[INTR_IDX_PP4_OVFL];
  1569. INIT_LIST_HEAD(&irq->cb.list);
  1570. irq->name = "pp4_overflow";
  1571. irq->hw_idx = CWB_4;
  1572. irq->irq_idx = -1;
  1573. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1574. irq->intr_idx = INTR_IDX_PP4_OVFL;
  1575. irq->cb.arg = wb_enc;
  1576. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1577. irq = &phys_enc->irq[INTR_IDX_PP5_OVFL];
  1578. INIT_LIST_HEAD(&irq->cb.list);
  1579. irq->name = "pp5_overflow";
  1580. irq->hw_idx = CWB_5;
  1581. irq->irq_idx = -1;
  1582. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1583. irq->intr_idx = INTR_IDX_PP5_OVFL;
  1584. irq->cb.arg = wb_enc;
  1585. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1586. /* create internal buffer for disable logic */
  1587. if (_sde_encoder_phys_wb_init_internal_fb(wb_enc,
  1588. DRM_FORMAT_RGB888, 2, 1, 6)) {
  1589. SDE_ERROR("failed to init internal fb\n");
  1590. goto fail_wb_init;
  1591. }
  1592. SDE_DEBUG("Created sde_encoder_phys_wb for wb %d\n",
  1593. wb_enc->hw_wb->idx - WB_0);
  1594. return phys_enc;
  1595. fail_wb_init:
  1596. fail_wb_check:
  1597. fail_mdp_init:
  1598. kfree(wb_enc);
  1599. fail_alloc:
  1600. return ERR_PTR(ret);
  1601. }