sde_encoder_phys_wb.c 52 KB

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