sde_encoder_phys_wb.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
  6. #include <linux/debugfs.h>
  7. #include <uapi/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 (enable && 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_cwb_cfg) {
  424. hw_ctl->ops.update_cwb_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,%d,\"%s\",%d,%d]\n",
  597. hw_wb->idx - WB_0, mode->base.id, 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_wb)
  737. hw_ctl->ops.update_bitmask_wb(hw_ctl, hw_wb->idx, 1);
  738. if (hw_ctl->ops.update_bitmask_cdm && hw_cdm)
  739. hw_ctl->ops.update_bitmask_cdm(hw_ctl, hw_cdm->idx, 1);
  740. if (test_bit(SDE_WB_CWB_CTRL, &hw_wb->caps->features)) {
  741. for (i = 0; i < crtc->num_mixers; i++) {
  742. cwb_idx = (enum sde_cwb) (hw_pp->idx + i);
  743. src_pp_idx = (enum sde_cwb) (src_pp_idx + i);
  744. if (hw_wb->ops.program_cwb_ctrl)
  745. hw_wb->ops.program_cwb_ctrl(hw_wb, cwb_idx,
  746. src_pp_idx, dspp_out, enable);
  747. if (hw_ctl->ops.update_bitmask_cwb)
  748. hw_ctl->ops.update_bitmask_cwb(hw_ctl,
  749. cwb_idx, 1);
  750. }
  751. if (need_merge && hw_ctl->ops.update_bitmask_merge3d
  752. && hw_pp && hw_pp->merge_3d)
  753. hw_ctl->ops.update_bitmask_merge3d(hw_ctl,
  754. hw_pp->merge_3d->idx, 1);
  755. } else {
  756. phys_enc->hw_mdptop->ops.set_cwb_ppb_cntl(phys_enc->hw_mdptop,
  757. need_merge, dspp_out);
  758. }
  759. }
  760. /**
  761. * _sde_encoder_phys_wb_update_flush - flush hardware update
  762. * @phys_enc: Pointer to physical encoder
  763. */
  764. static void _sde_encoder_phys_wb_update_flush(struct sde_encoder_phys *phys_enc)
  765. {
  766. struct sde_encoder_phys_wb *wb_enc;
  767. struct sde_hw_wb *hw_wb;
  768. struct sde_hw_ctl *hw_ctl;
  769. struct sde_hw_cdm *hw_cdm;
  770. struct sde_hw_pingpong *hw_pp;
  771. struct sde_ctl_flush_cfg pending_flush = {0,};
  772. if (!phys_enc)
  773. return;
  774. wb_enc = to_sde_encoder_phys_wb(phys_enc);
  775. hw_wb = wb_enc->hw_wb;
  776. hw_cdm = phys_enc->hw_cdm;
  777. hw_pp = phys_enc->hw_pp;
  778. hw_ctl = phys_enc->hw_ctl;
  779. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  780. if (phys_enc->in_clone_mode) {
  781. SDE_DEBUG("in CWB mode. early return\n");
  782. return;
  783. }
  784. if (!hw_ctl) {
  785. SDE_DEBUG("[wb:%d] no ctl assigned\n", hw_wb->idx - WB_0);
  786. return;
  787. }
  788. if (hw_ctl->ops.update_bitmask_wb)
  789. hw_ctl->ops.update_bitmask_wb(hw_ctl, hw_wb->idx, 1);
  790. if (hw_ctl->ops.update_bitmask_cdm && hw_cdm)
  791. hw_ctl->ops.update_bitmask_cdm(hw_ctl, hw_cdm->idx, 1);
  792. if (hw_ctl->ops.update_bitmask_merge3d && hw_pp && hw_pp->merge_3d)
  793. hw_ctl->ops.update_bitmask_merge3d(hw_ctl,
  794. hw_pp->merge_3d->idx, 1);
  795. if (hw_ctl->ops.get_pending_flush)
  796. hw_ctl->ops.get_pending_flush(hw_ctl,
  797. &pending_flush);
  798. SDE_DEBUG("Pending flush mask for CTL_%d is 0x%x, WB %d\n",
  799. hw_ctl->idx - CTL_0, pending_flush.pending_flush_mask,
  800. hw_wb->idx - WB_0);
  801. }
  802. /**
  803. * sde_encoder_phys_wb_setup - setup writeback encoder
  804. * @phys_enc: Pointer to physical encoder
  805. */
  806. static void sde_encoder_phys_wb_setup(
  807. struct sde_encoder_phys *phys_enc)
  808. {
  809. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  810. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  811. struct drm_display_mode mode = phys_enc->cached_mode;
  812. struct drm_framebuffer *fb;
  813. struct sde_rect *wb_roi = &wb_enc->wb_roi;
  814. SDE_DEBUG("[mode_set:%d,%d,\"%s\",%d,%d]\n",
  815. hw_wb->idx - WB_0, mode.base.id, mode.name,
  816. mode.hdisplay, mode.vdisplay);
  817. memset(wb_roi, 0, sizeof(struct sde_rect));
  818. /* clear writeback framebuffer - will be updated in setup_fb */
  819. wb_enc->wb_fb = NULL;
  820. wb_enc->wb_aspace = NULL;
  821. if (phys_enc->enable_state == SDE_ENC_DISABLING) {
  822. fb = wb_enc->fb_disable;
  823. wb_roi->w = 0;
  824. wb_roi->h = 0;
  825. } else {
  826. fb = sde_wb_get_output_fb(wb_enc->wb_dev);
  827. sde_wb_get_output_roi(wb_enc->wb_dev, wb_roi);
  828. }
  829. if (!fb) {
  830. SDE_DEBUG("no output framebuffer\n");
  831. return;
  832. }
  833. SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
  834. fb->width, fb->height);
  835. if (wb_roi->w == 0 || wb_roi->h == 0) {
  836. wb_roi->x = 0;
  837. wb_roi->y = 0;
  838. wb_roi->w = fb->width;
  839. wb_roi->h = fb->height;
  840. }
  841. SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi->x, wb_roi->y,
  842. wb_roi->w, wb_roi->h);
  843. wb_enc->wb_fmt = sde_get_sde_format_ext(fb->format->format,
  844. fb->modifier);
  845. if (!wb_enc->wb_fmt) {
  846. SDE_ERROR("unsupported output pixel format: %d\n",
  847. fb->format->format);
  848. return;
  849. }
  850. SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->format->format,
  851. fb->modifier);
  852. sde_encoder_phys_wb_set_ot_limit(phys_enc);
  853. sde_encoder_phys_wb_set_qos_remap(phys_enc);
  854. sde_encoder_phys_wb_set_qos(phys_enc);
  855. sde_encoder_phys_setup_cdm(phys_enc, fb, wb_enc->wb_fmt, wb_roi);
  856. sde_encoder_phys_wb_setup_fb(phys_enc, fb, wb_roi);
  857. sde_encoder_phys_wb_setup_cdp(phys_enc, wb_enc->wb_fmt);
  858. _sde_encoder_phys_wb_setup_cwb(phys_enc, true);
  859. }
  860. static void _sde_encoder_phys_wb_frame_done_helper(void *arg, bool frame_error)
  861. {
  862. struct sde_encoder_phys_wb *wb_enc = arg;
  863. struct sde_encoder_phys *phys_enc = &wb_enc->base;
  864. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  865. u32 event = frame_error ? SDE_ENCODER_FRAME_EVENT_ERROR : 0;
  866. SDE_DEBUG("[wb:%d,%u]\n", hw_wb->idx - WB_0, wb_enc->frame_count);
  867. /* don't notify upper layer for internal commit */
  868. if (phys_enc->enable_state == SDE_ENC_DISABLING)
  869. goto complete;
  870. if (phys_enc->parent_ops.handle_frame_done &&
  871. atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0)) {
  872. event |= SDE_ENCODER_FRAME_EVENT_DONE |
  873. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  874. if (!phys_enc->in_clone_mode)
  875. event |= SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  876. phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
  877. phys_enc, event);
  878. }
  879. if (phys_enc->parent_ops.handle_vblank_virt)
  880. phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
  881. phys_enc);
  882. SDE_EVT32_IRQ(DRMID(phys_enc->parent), hw_wb->idx - WB_0, event,
  883. frame_error);
  884. complete:
  885. wake_up_all(&phys_enc->pending_kickoff_wq);
  886. }
  887. /**
  888. * sde_encoder_phys_wb_done_irq - Pingpong overflow interrupt handler for CWB
  889. * @arg: Pointer to writeback encoder
  890. * @irq_idx: interrupt index
  891. */
  892. static void sde_encoder_phys_cwb_ovflow(void *arg, int irq_idx)
  893. {
  894. _sde_encoder_phys_wb_frame_done_helper(arg, true);
  895. }
  896. /**
  897. * sde_encoder_phys_wb_done_irq - writeback interrupt handler
  898. * @arg: Pointer to writeback encoder
  899. * @irq_idx: interrupt index
  900. */
  901. static void sde_encoder_phys_wb_done_irq(void *arg, int irq_idx)
  902. {
  903. _sde_encoder_phys_wb_frame_done_helper(arg, false);
  904. }
  905. /**
  906. * sde_encoder_phys_wb_irq_ctrl - irq control of WB
  907. * @phys: Pointer to physical encoder
  908. * @enable: indicates enable or disable interrupts
  909. */
  910. static void sde_encoder_phys_wb_irq_ctrl(
  911. struct sde_encoder_phys *phys, bool enable)
  912. {
  913. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys);
  914. int index = 0, refcount;
  915. int ret = 0, pp = 0;
  916. if (!wb_enc)
  917. return;
  918. if (wb_enc->bypass_irqreg)
  919. return;
  920. pp = phys->hw_pp->idx - PINGPONG_0;
  921. if ((pp + CRTC_DUAL_MIXERS) >= PINGPONG_MAX) {
  922. SDE_ERROR("invalid pingpong index for WB or CWB\n");
  923. return;
  924. }
  925. refcount = atomic_read(&phys->wbirq_refcount);
  926. if (enable && atomic_inc_return(&phys->wbirq_refcount) == 1) {
  927. sde_encoder_helper_register_irq(phys, INTR_IDX_WB_DONE);
  928. if (ret)
  929. atomic_dec_return(&phys->wbirq_refcount);
  930. for (index = 0; index < CRTC_DUAL_MIXERS; index++)
  931. if (cwb_irq_tbl[index + pp] != SDE_NONE)
  932. sde_encoder_helper_register_irq(phys,
  933. cwb_irq_tbl[index + pp]);
  934. } else if (!enable &&
  935. atomic_dec_return(&phys->wbirq_refcount) == 0) {
  936. sde_encoder_helper_unregister_irq(phys, INTR_IDX_WB_DONE);
  937. if (ret)
  938. atomic_inc_return(&phys->wbirq_refcount);
  939. for (index = 0; index < CRTC_DUAL_MIXERS; index++)
  940. if (cwb_irq_tbl[index + pp] != SDE_NONE)
  941. sde_encoder_helper_unregister_irq(phys,
  942. cwb_irq_tbl[index + pp]);
  943. }
  944. }
  945. /**
  946. * sde_encoder_phys_wb_mode_set - set display mode
  947. * @phys_enc: Pointer to physical encoder
  948. * @mode: Pointer to requested display mode
  949. * @adj_mode: Pointer to adjusted display mode
  950. */
  951. static void sde_encoder_phys_wb_mode_set(
  952. struct sde_encoder_phys *phys_enc,
  953. struct drm_display_mode *mode,
  954. struct drm_display_mode *adj_mode)
  955. {
  956. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  957. struct sde_rm *rm = &phys_enc->sde_kms->rm;
  958. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  959. struct sde_rm_hw_iter iter;
  960. int i, instance;
  961. phys_enc->cached_mode = *adj_mode;
  962. instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
  963. SDE_DEBUG("[mode_set_cache:%d,%d,\"%s\",%d,%d]\n",
  964. hw_wb->idx - WB_0, mode->base.id,
  965. mode->name, mode->hdisplay, mode->vdisplay);
  966. phys_enc->hw_ctl = NULL;
  967. phys_enc->hw_cdm = NULL;
  968. /* Retrieve previously allocated HW Resources. CTL shouldn't fail */
  969. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
  970. for (i = 0; i <= instance; i++) {
  971. sde_rm_get_hw(rm, &iter);
  972. if (i == instance)
  973. phys_enc->hw_ctl = (struct sde_hw_ctl *) iter.hw;
  974. }
  975. if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
  976. SDE_ERROR("failed init ctl: %ld\n",
  977. (!phys_enc->hw_ctl) ?
  978. -EINVAL : PTR_ERR(phys_enc->hw_ctl));
  979. phys_enc->hw_ctl = NULL;
  980. return;
  981. }
  982. /* CDM is optional */
  983. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CDM);
  984. for (i = 0; i <= instance; i++) {
  985. sde_rm_get_hw(rm, &iter);
  986. if (i == instance)
  987. phys_enc->hw_cdm = (struct sde_hw_cdm *) iter.hw;
  988. }
  989. if (IS_ERR(phys_enc->hw_cdm)) {
  990. SDE_ERROR("CDM required but not allocated: %ld\n",
  991. PTR_ERR(phys_enc->hw_cdm));
  992. phys_enc->hw_cdm = NULL;
  993. }
  994. }
  995. static int sde_encoder_phys_wb_frame_timeout(struct sde_encoder_phys *phys_enc)
  996. {
  997. u32 event = 0;
  998. while (atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0) &&
  999. phys_enc->parent_ops.handle_frame_done) {
  1000. event = SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE
  1001. | SDE_ENCODER_FRAME_EVENT_ERROR;
  1002. if (!phys_enc->in_clone_mode)
  1003. event |= SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  1004. phys_enc->parent_ops.handle_frame_done(
  1005. phys_enc->parent, phys_enc, event);
  1006. SDE_EVT32(DRMID(phys_enc->parent), event,
  1007. atomic_read(&phys_enc->pending_retire_fence_cnt));
  1008. }
  1009. return event;
  1010. }
  1011. static int _sde_encoder_phys_wb_wait_for_commit_done(
  1012. struct sde_encoder_phys *phys_enc, bool is_disable)
  1013. {
  1014. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1015. u32 event = 0;
  1016. u64 wb_time = 0;
  1017. int rc = 0;
  1018. struct sde_encoder_wait_info wait_info = {0};
  1019. /* Return EWOULDBLOCK since we know the wait isn't necessary */
  1020. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1021. SDE_ERROR("encoder already disabled\n");
  1022. return -EWOULDBLOCK;
  1023. }
  1024. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
  1025. wb_enc->kickoff_count, !!wb_enc->wb_fb, is_disable,
  1026. phys_enc->in_clone_mode);
  1027. if (!is_disable && phys_enc->in_clone_mode &&
  1028. (atomic_read(&phys_enc->pending_retire_fence_cnt) <= 1))
  1029. goto skip_wait;
  1030. /* signal completion if commit with no framebuffer */
  1031. if (!wb_enc->wb_fb) {
  1032. SDE_DEBUG("no output framebuffer\n");
  1033. _sde_encoder_phys_wb_frame_done_helper(wb_enc, false);
  1034. }
  1035. wait_info.wq = &phys_enc->pending_kickoff_wq;
  1036. wait_info.atomic_cnt = &phys_enc->pending_retire_fence_cnt;
  1037. wait_info.timeout_ms = max_t(u32, wb_enc->wbdone_timeout,
  1038. KICKOFF_TIMEOUT_MS);
  1039. rc = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_WB_DONE,
  1040. &wait_info);
  1041. if (rc == -ETIMEDOUT) {
  1042. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
  1043. wb_enc->frame_count, SDE_EVTLOG_ERROR);
  1044. SDE_ERROR("wb:%d kickoff timed out\n", WBID(wb_enc));
  1045. event = sde_encoder_phys_wb_frame_timeout(phys_enc);
  1046. }
  1047. /* cleanup writeback framebuffer */
  1048. if (wb_enc->wb_fb && wb_enc->wb_aspace) {
  1049. msm_framebuffer_cleanup(wb_enc->wb_fb, wb_enc->wb_aspace);
  1050. drm_framebuffer_put(wb_enc->wb_fb);
  1051. wb_enc->wb_fb = NULL;
  1052. wb_enc->wb_aspace = NULL;
  1053. }
  1054. skip_wait:
  1055. /* remove vote for iommu/clk/bus */
  1056. wb_enc->frame_count++;
  1057. if (!rc) {
  1058. wb_enc->end_time = ktime_get();
  1059. wb_time = (u64)ktime_to_us(wb_enc->end_time) -
  1060. (u64)ktime_to_us(wb_enc->start_time);
  1061. SDE_DEBUG("wb:%d took %llu us\n", WBID(wb_enc), wb_time);
  1062. }
  1063. /* cleanup previous buffer if pending */
  1064. if (wb_enc->cwb_old_fb && wb_enc->cwb_old_aspace) {
  1065. msm_framebuffer_cleanup(wb_enc->cwb_old_fb, wb_enc->cwb_old_aspace);
  1066. drm_framebuffer_put(wb_enc->cwb_old_fb);
  1067. wb_enc->cwb_old_fb = NULL;
  1068. wb_enc->cwb_old_aspace = NULL;
  1069. }
  1070. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
  1071. wb_time, event, rc);
  1072. return rc;
  1073. }
  1074. /**
  1075. * sde_encoder_phys_wb_wait_for_commit_done - wait until request is committed
  1076. * @phys_enc: Pointer to physical encoder
  1077. */
  1078. static int sde_encoder_phys_wb_wait_for_commit_done(
  1079. struct sde_encoder_phys *phys_enc)
  1080. {
  1081. return _sde_encoder_phys_wb_wait_for_commit_done(phys_enc, false);
  1082. }
  1083. /**
  1084. * sde_encoder_phys_wb_prepare_for_kickoff - pre-kickoff processing
  1085. * @phys_enc: Pointer to physical encoder
  1086. * @params: kickoff parameters
  1087. * Returns: Zero on success
  1088. */
  1089. static int sde_encoder_phys_wb_prepare_for_kickoff(
  1090. struct sde_encoder_phys *phys_enc,
  1091. struct sde_encoder_kickoff_params *params)
  1092. {
  1093. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1094. SDE_DEBUG("[wb:%d,%u]\n", wb_enc->hw_wb->idx - WB_0,
  1095. wb_enc->kickoff_count);
  1096. if (phys_enc->in_clone_mode) {
  1097. wb_enc->cwb_old_fb = wb_enc->wb_fb;
  1098. wb_enc->cwb_old_aspace = wb_enc->wb_aspace;
  1099. }
  1100. wb_enc->kickoff_count++;
  1101. /* set OT limit & enable traffic shaper */
  1102. sde_encoder_phys_wb_setup(phys_enc);
  1103. _sde_encoder_phys_wb_update_flush(phys_enc);
  1104. _sde_encoder_phys_wb_update_cwb_flush(phys_enc, true);
  1105. /* vote for iommu/clk/bus */
  1106. wb_enc->start_time = ktime_get();
  1107. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
  1108. wb_enc->kickoff_count, wb_enc->frame_count,
  1109. phys_enc->in_clone_mode);
  1110. return 0;
  1111. }
  1112. /**
  1113. * sde_encoder_phys_wb_trigger_flush - trigger flush processing
  1114. * @phys_enc: Pointer to physical encoder
  1115. */
  1116. static void sde_encoder_phys_wb_trigger_flush(struct sde_encoder_phys *phys_enc)
  1117. {
  1118. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1119. if (!phys_enc || !wb_enc->hw_wb) {
  1120. SDE_ERROR("invalid encoder\n");
  1121. return;
  1122. }
  1123. /*
  1124. * Bail out iff in CWB mode. In case of CWB, primary control-path
  1125. * which is actually driving would trigger the flush
  1126. */
  1127. if (phys_enc->in_clone_mode) {
  1128. SDE_DEBUG("in CWB mode. early return\n");
  1129. return;
  1130. }
  1131. SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
  1132. /* clear pending flush if commit with no framebuffer */
  1133. if (!wb_enc->wb_fb) {
  1134. SDE_DEBUG("no output framebuffer\n");
  1135. return;
  1136. }
  1137. sde_encoder_helper_trigger_flush(phys_enc);
  1138. }
  1139. /**
  1140. * sde_encoder_phys_wb_handle_post_kickoff - post-kickoff processing
  1141. * @phys_enc: Pointer to physical encoder
  1142. */
  1143. static void sde_encoder_phys_wb_handle_post_kickoff(
  1144. struct sde_encoder_phys *phys_enc)
  1145. {
  1146. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1147. SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
  1148. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc));
  1149. }
  1150. /**
  1151. * _sde_encoder_phys_wb_init_internal_fb - create fb for internal commit
  1152. * @wb_enc: Pointer to writeback encoder
  1153. * @pixel_format: DRM pixel format
  1154. * @width: Desired fb width
  1155. * @height: Desired fb height
  1156. * @pitch: Desired fb pitch
  1157. */
  1158. static int _sde_encoder_phys_wb_init_internal_fb(
  1159. struct sde_encoder_phys_wb *wb_enc,
  1160. uint32_t pixel_format, uint32_t width,
  1161. uint32_t height, uint32_t pitch)
  1162. {
  1163. struct drm_device *dev;
  1164. struct drm_framebuffer *fb;
  1165. struct drm_mode_fb_cmd2 mode_cmd;
  1166. uint32_t size;
  1167. int nplanes, i, ret;
  1168. struct msm_gem_address_space *aspace;
  1169. if (!wb_enc || !wb_enc->base.parent || !wb_enc->base.sde_kms) {
  1170. SDE_ERROR("invalid params\n");
  1171. return -EINVAL;
  1172. }
  1173. aspace = wb_enc->base.sde_kms->aspace[SDE_IOMMU_DOMAIN_UNSECURE];
  1174. if (!aspace) {
  1175. SDE_ERROR("invalid address space\n");
  1176. return -EINVAL;
  1177. }
  1178. dev = wb_enc->base.sde_kms->dev;
  1179. if (!dev) {
  1180. SDE_ERROR("invalid dev\n");
  1181. return -EINVAL;
  1182. }
  1183. memset(&mode_cmd, 0, sizeof(mode_cmd));
  1184. mode_cmd.pixel_format = pixel_format;
  1185. mode_cmd.width = width;
  1186. mode_cmd.height = height;
  1187. mode_cmd.pitches[0] = pitch;
  1188. size = sde_format_get_framebuffer_size(pixel_format,
  1189. mode_cmd.width, mode_cmd.height,
  1190. mode_cmd.pitches, 0);
  1191. if (!size) {
  1192. SDE_DEBUG("not creating zero size buffer\n");
  1193. return -EINVAL;
  1194. }
  1195. /* allocate gem tracking object */
  1196. nplanes = drm_format_num_planes(pixel_format);
  1197. if (nplanes >= SDE_MAX_PLANES) {
  1198. SDE_ERROR("requested format has too many planes\n");
  1199. return -EINVAL;
  1200. }
  1201. wb_enc->bo_disable[0] = msm_gem_new(dev, size,
  1202. MSM_BO_SCANOUT | MSM_BO_WC);
  1203. if (IS_ERR_OR_NULL(wb_enc->bo_disable[0])) {
  1204. ret = PTR_ERR(wb_enc->bo_disable[0]);
  1205. wb_enc->bo_disable[0] = NULL;
  1206. SDE_ERROR("failed to create bo, %d\n", ret);
  1207. return ret;
  1208. }
  1209. for (i = 0; i < nplanes; ++i) {
  1210. wb_enc->bo_disable[i] = wb_enc->bo_disable[0];
  1211. mode_cmd.pitches[i] = width *
  1212. drm_format_plane_cpp(pixel_format, i);
  1213. }
  1214. fb = msm_framebuffer_init(dev, &mode_cmd, wb_enc->bo_disable);
  1215. if (IS_ERR_OR_NULL(fb)) {
  1216. ret = PTR_ERR(fb);
  1217. drm_gem_object_put(wb_enc->bo_disable[0]);
  1218. wb_enc->bo_disable[0] = NULL;
  1219. SDE_ERROR("failed to init fb, %d\n", ret);
  1220. return ret;
  1221. }
  1222. /* prepare the backing buffer now so that it's available later */
  1223. ret = msm_framebuffer_prepare(fb, aspace);
  1224. if (!ret)
  1225. wb_enc->fb_disable = fb;
  1226. return ret;
  1227. }
  1228. /**
  1229. * _sde_encoder_phys_wb_destroy_internal_fb - deconstruct internal fb
  1230. * @wb_enc: Pointer to writeback encoder
  1231. */
  1232. static void _sde_encoder_phys_wb_destroy_internal_fb(
  1233. struct sde_encoder_phys_wb *wb_enc)
  1234. {
  1235. if (!wb_enc)
  1236. return;
  1237. if (wb_enc->fb_disable) {
  1238. drm_framebuffer_unregister_private(wb_enc->fb_disable);
  1239. drm_framebuffer_remove(wb_enc->fb_disable);
  1240. wb_enc->fb_disable = NULL;
  1241. }
  1242. if (wb_enc->bo_disable[0]) {
  1243. drm_gem_object_put(wb_enc->bo_disable[0]);
  1244. wb_enc->bo_disable[0] = NULL;
  1245. }
  1246. }
  1247. /**
  1248. * sde_encoder_phys_wb_enable - enable writeback encoder
  1249. * @phys_enc: Pointer to physical encoder
  1250. */
  1251. static void sde_encoder_phys_wb_enable(struct sde_encoder_phys *phys_enc)
  1252. {
  1253. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1254. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  1255. struct drm_device *dev;
  1256. struct drm_connector *connector;
  1257. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  1258. if (!wb_enc->base.parent || !wb_enc->base.parent->dev) {
  1259. SDE_ERROR("invalid drm device\n");
  1260. return;
  1261. }
  1262. dev = wb_enc->base.parent->dev;
  1263. /* find associated writeback connector */
  1264. connector = phys_enc->connector;
  1265. if (!connector || connector->encoder != phys_enc->parent) {
  1266. SDE_ERROR("failed to find writeback connector\n");
  1267. return;
  1268. }
  1269. wb_enc->wb_dev = sde_wb_connector_get_wb(connector);
  1270. phys_enc->enable_state = SDE_ENC_ENABLED;
  1271. /*
  1272. * cache the crtc in wb_enc on enable for duration of use case
  1273. * for correctly servicing asynchronous irq events and timers
  1274. */
  1275. wb_enc->crtc = phys_enc->parent->crtc;
  1276. }
  1277. /**
  1278. * sde_encoder_phys_wb_disable - disable writeback encoder
  1279. * @phys_enc: Pointer to physical encoder
  1280. */
  1281. static void sde_encoder_phys_wb_disable(struct sde_encoder_phys *phys_enc)
  1282. {
  1283. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1284. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  1285. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  1286. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1287. SDE_ERROR("encoder is already disabled\n");
  1288. return;
  1289. }
  1290. SDE_DEBUG("[wait_for_done: wb:%d, frame:%u, kickoff:%u]\n",
  1291. hw_wb->idx - WB_0, wb_enc->frame_count,
  1292. wb_enc->kickoff_count);
  1293. _sde_encoder_phys_wb_wait_for_commit_done(phys_enc, true);
  1294. if (!phys_enc->hw_ctl || !phys_enc->parent ||
  1295. !phys_enc->sde_kms || !wb_enc->fb_disable) {
  1296. SDE_DEBUG("invalid enc, skipping extra commit\n");
  1297. goto exit;
  1298. }
  1299. /* avoid reset frame for CWB */
  1300. if (phys_enc->in_clone_mode) {
  1301. _sde_encoder_phys_wb_setup_cwb(phys_enc, false);
  1302. _sde_encoder_phys_wb_update_cwb_flush(phys_enc, false);
  1303. phys_enc->in_clone_mode = false;
  1304. goto exit;
  1305. }
  1306. /* reset h/w before final flush */
  1307. if (phys_enc->hw_ctl->ops.clear_pending_flush)
  1308. phys_enc->hw_ctl->ops.clear_pending_flush(phys_enc->hw_ctl);
  1309. /*
  1310. * New CTL reset sequence from 5.0 MDP onwards.
  1311. * If has_3d_merge_reset is not set, legacy reset
  1312. * sequence is executed.
  1313. */
  1314. if (hw_wb->catalog->has_3d_merge_reset) {
  1315. sde_encoder_helper_phys_disable(phys_enc, wb_enc);
  1316. goto exit;
  1317. }
  1318. if (sde_encoder_helper_reset_mixers(phys_enc, NULL))
  1319. goto exit;
  1320. phys_enc->enable_state = SDE_ENC_DISABLING;
  1321. sde_encoder_phys_wb_prepare_for_kickoff(phys_enc, NULL);
  1322. sde_encoder_phys_wb_irq_ctrl(phys_enc, true);
  1323. if (phys_enc->hw_ctl->ops.trigger_flush)
  1324. phys_enc->hw_ctl->ops.trigger_flush(phys_enc->hw_ctl);
  1325. sde_encoder_helper_trigger_start(phys_enc);
  1326. _sde_encoder_phys_wb_wait_for_commit_done(phys_enc, true);
  1327. sde_encoder_phys_wb_irq_ctrl(phys_enc, false);
  1328. exit:
  1329. /*
  1330. * frame count and kickoff count are only used for debug purpose. Frame
  1331. * count can be more than kickoff count at the end of disable call due
  1332. * to extra frame_done wait. It does not cause any issue because
  1333. * frame_done wait is based on retire_fence count. Leaving these
  1334. * counters for debugging purpose.
  1335. */
  1336. if (wb_enc->frame_count != wb_enc->kickoff_count) {
  1337. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
  1338. wb_enc->kickoff_count, wb_enc->frame_count,
  1339. phys_enc->in_clone_mode);
  1340. wb_enc->frame_count = wb_enc->kickoff_count;
  1341. }
  1342. phys_enc->enable_state = SDE_ENC_DISABLED;
  1343. wb_enc->crtc = NULL;
  1344. }
  1345. /**
  1346. * sde_encoder_phys_wb_get_hw_resources - get hardware resources
  1347. * @phys_enc: Pointer to physical encoder
  1348. * @hw_res: Pointer to encoder resources
  1349. */
  1350. static void sde_encoder_phys_wb_get_hw_resources(
  1351. struct sde_encoder_phys *phys_enc,
  1352. struct sde_encoder_hw_resources *hw_res,
  1353. struct drm_connector_state *conn_state)
  1354. {
  1355. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1356. struct sde_hw_wb *hw_wb;
  1357. struct drm_framebuffer *fb;
  1358. const struct sde_format *fmt = NULL;
  1359. if (!phys_enc) {
  1360. SDE_ERROR("invalid encoder\n");
  1361. return;
  1362. }
  1363. fb = sde_wb_connector_state_get_output_fb(conn_state);
  1364. if (fb) {
  1365. fmt = sde_get_sde_format_ext(fb->format->format, fb->modifier);
  1366. if (!fmt) {
  1367. SDE_ERROR("unsupported output pixel format:%d\n",
  1368. fb->format->format);
  1369. return;
  1370. }
  1371. }
  1372. hw_wb = wb_enc->hw_wb;
  1373. hw_res->wbs[hw_wb->idx - WB_0] = phys_enc->intf_mode;
  1374. hw_res->needs_cdm = fmt ? SDE_FORMAT_IS_YUV(fmt) : false;
  1375. SDE_DEBUG("[wb:%d] intf_mode=%d needs_cdm=%d\n", hw_wb->idx - WB_0,
  1376. hw_res->wbs[hw_wb->idx - WB_0],
  1377. hw_res->needs_cdm);
  1378. }
  1379. #ifdef CONFIG_DEBUG_FS
  1380. /**
  1381. * sde_encoder_phys_wb_init_debugfs - initialize writeback encoder debugfs
  1382. * @phys_enc: Pointer to physical encoder
  1383. * @debugfs_root: Pointer to virtual encoder's debugfs_root dir
  1384. */
  1385. static int sde_encoder_phys_wb_init_debugfs(
  1386. struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
  1387. {
  1388. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1389. if (!phys_enc || !wb_enc->hw_wb || !debugfs_root)
  1390. return -EINVAL;
  1391. if (!debugfs_create_u32("wbdone_timeout", 0600,
  1392. debugfs_root, &wb_enc->wbdone_timeout)) {
  1393. SDE_ERROR("failed to create debugfs/wbdone_timeout\n");
  1394. return -ENOMEM;
  1395. }
  1396. return 0;
  1397. }
  1398. #else
  1399. static int sde_encoder_phys_wb_init_debugfs(
  1400. struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
  1401. {
  1402. return 0;
  1403. }
  1404. #endif
  1405. static int sde_encoder_phys_wb_late_register(struct sde_encoder_phys *phys_enc,
  1406. struct dentry *debugfs_root)
  1407. {
  1408. return sde_encoder_phys_wb_init_debugfs(phys_enc, debugfs_root);
  1409. }
  1410. /**
  1411. * sde_encoder_phys_wb_destroy - destroy writeback encoder
  1412. * @phys_enc: Pointer to physical encoder
  1413. */
  1414. static void sde_encoder_phys_wb_destroy(struct sde_encoder_phys *phys_enc)
  1415. {
  1416. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1417. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  1418. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  1419. if (!phys_enc)
  1420. return;
  1421. _sde_encoder_phys_wb_destroy_internal_fb(wb_enc);
  1422. kfree(wb_enc);
  1423. }
  1424. /**
  1425. * sde_encoder_phys_wb_init_ops - initialize writeback operations
  1426. * @ops: Pointer to encoder operation table
  1427. */
  1428. static void sde_encoder_phys_wb_init_ops(struct sde_encoder_phys_ops *ops)
  1429. {
  1430. ops->late_register = sde_encoder_phys_wb_late_register;
  1431. ops->is_master = sde_encoder_phys_wb_is_master;
  1432. ops->mode_set = sde_encoder_phys_wb_mode_set;
  1433. ops->enable = sde_encoder_phys_wb_enable;
  1434. ops->disable = sde_encoder_phys_wb_disable;
  1435. ops->destroy = sde_encoder_phys_wb_destroy;
  1436. ops->atomic_check = sde_encoder_phys_wb_atomic_check;
  1437. ops->get_hw_resources = sde_encoder_phys_wb_get_hw_resources;
  1438. ops->wait_for_commit_done = sde_encoder_phys_wb_wait_for_commit_done;
  1439. ops->prepare_for_kickoff = sde_encoder_phys_wb_prepare_for_kickoff;
  1440. ops->handle_post_kickoff = sde_encoder_phys_wb_handle_post_kickoff;
  1441. ops->trigger_flush = sde_encoder_phys_wb_trigger_flush;
  1442. ops->trigger_start = sde_encoder_helper_trigger_start;
  1443. ops->hw_reset = sde_encoder_helper_hw_reset;
  1444. ops->irq_control = sde_encoder_phys_wb_irq_ctrl;
  1445. }
  1446. /**
  1447. * sde_encoder_phys_wb_init - initialize writeback encoder
  1448. * @init: Pointer to init info structure with initialization params
  1449. */
  1450. struct sde_encoder_phys *sde_encoder_phys_wb_init(
  1451. struct sde_enc_phys_init_params *p)
  1452. {
  1453. struct sde_encoder_phys *phys_enc;
  1454. struct sde_encoder_phys_wb *wb_enc;
  1455. struct sde_hw_mdp *hw_mdp;
  1456. struct sde_encoder_irq *irq;
  1457. int ret = 0;
  1458. SDE_DEBUG("\n");
  1459. if (!p || !p->parent) {
  1460. SDE_ERROR("invalid params\n");
  1461. ret = -EINVAL;
  1462. goto fail_alloc;
  1463. }
  1464. wb_enc = kzalloc(sizeof(*wb_enc), GFP_KERNEL);
  1465. if (!wb_enc) {
  1466. SDE_ERROR("failed to allocate wb enc\n");
  1467. ret = -ENOMEM;
  1468. goto fail_alloc;
  1469. }
  1470. wb_enc->wbdone_timeout = KICKOFF_TIMEOUT_MS;
  1471. phys_enc = &wb_enc->base;
  1472. if (p->sde_kms->vbif[VBIF_NRT]) {
  1473. wb_enc->aspace[SDE_IOMMU_DOMAIN_UNSECURE] =
  1474. p->sde_kms->aspace[MSM_SMMU_DOMAIN_NRT_UNSECURE];
  1475. wb_enc->aspace[SDE_IOMMU_DOMAIN_SECURE] =
  1476. p->sde_kms->aspace[MSM_SMMU_DOMAIN_NRT_SECURE];
  1477. } else {
  1478. wb_enc->aspace[SDE_IOMMU_DOMAIN_UNSECURE] =
  1479. p->sde_kms->aspace[MSM_SMMU_DOMAIN_UNSECURE];
  1480. wb_enc->aspace[SDE_IOMMU_DOMAIN_SECURE] =
  1481. p->sde_kms->aspace[MSM_SMMU_DOMAIN_SECURE];
  1482. }
  1483. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  1484. if (IS_ERR_OR_NULL(hw_mdp)) {
  1485. ret = PTR_ERR(hw_mdp);
  1486. SDE_ERROR("failed to init hw_top: %d\n", ret);
  1487. goto fail_mdp_init;
  1488. }
  1489. phys_enc->hw_mdptop = hw_mdp;
  1490. /**
  1491. * hw_wb resource permanently assigned to this encoder
  1492. * Other resources allocated at atomic commit time by use case
  1493. */
  1494. if (p->wb_idx != SDE_NONE) {
  1495. struct sde_rm_hw_iter iter;
  1496. sde_rm_init_hw_iter(&iter, 0, SDE_HW_BLK_WB);
  1497. while (sde_rm_get_hw(&p->sde_kms->rm, &iter)) {
  1498. struct sde_hw_wb *hw_wb = (struct sde_hw_wb *)iter.hw;
  1499. if (hw_wb->idx == p->wb_idx) {
  1500. wb_enc->hw_wb = hw_wb;
  1501. break;
  1502. }
  1503. }
  1504. if (!wb_enc->hw_wb) {
  1505. ret = -EINVAL;
  1506. SDE_ERROR("failed to init hw_wb%d\n", p->wb_idx - WB_0);
  1507. goto fail_wb_init;
  1508. }
  1509. } else {
  1510. ret = -EINVAL;
  1511. SDE_ERROR("invalid wb_idx\n");
  1512. goto fail_wb_check;
  1513. }
  1514. sde_encoder_phys_wb_init_ops(&phys_enc->ops);
  1515. phys_enc->parent = p->parent;
  1516. phys_enc->parent_ops = p->parent_ops;
  1517. phys_enc->sde_kms = p->sde_kms;
  1518. phys_enc->split_role = p->split_role;
  1519. phys_enc->intf_mode = INTF_MODE_WB_LINE;
  1520. phys_enc->intf_idx = p->intf_idx;
  1521. phys_enc->enc_spinlock = p->enc_spinlock;
  1522. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  1523. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  1524. atomic_set(&phys_enc->wbirq_refcount, 0);
  1525. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  1526. irq = &phys_enc->irq[INTR_IDX_WB_DONE];
  1527. INIT_LIST_HEAD(&irq->cb.list);
  1528. irq->name = "wb_done";
  1529. irq->hw_idx = wb_enc->hw_wb->idx;
  1530. irq->irq_idx = -1;
  1531. irq->intr_type = sde_encoder_phys_wb_get_intr_type(wb_enc->hw_wb);
  1532. irq->intr_idx = INTR_IDX_WB_DONE;
  1533. irq->cb.arg = wb_enc;
  1534. irq->cb.func = sde_encoder_phys_wb_done_irq;
  1535. irq = &phys_enc->irq[INTR_IDX_PP1_OVFL];
  1536. INIT_LIST_HEAD(&irq->cb.list);
  1537. irq->name = "pp1_overflow";
  1538. irq->hw_idx = CWB_1;
  1539. irq->irq_idx = -1;
  1540. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1541. irq->intr_idx = INTR_IDX_PP1_OVFL;
  1542. irq->cb.arg = wb_enc;
  1543. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1544. irq = &phys_enc->irq[INTR_IDX_PP2_OVFL];
  1545. INIT_LIST_HEAD(&irq->cb.list);
  1546. irq->name = "pp2_overflow";
  1547. irq->hw_idx = CWB_2;
  1548. irq->irq_idx = -1;
  1549. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1550. irq->intr_idx = INTR_IDX_PP2_OVFL;
  1551. irq->cb.arg = wb_enc;
  1552. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1553. irq = &phys_enc->irq[INTR_IDX_PP3_OVFL];
  1554. INIT_LIST_HEAD(&irq->cb.list);
  1555. irq->name = "pp3_overflow";
  1556. irq->hw_idx = CWB_3;
  1557. irq->irq_idx = -1;
  1558. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1559. irq->intr_idx = INTR_IDX_PP3_OVFL;
  1560. irq->cb.arg = wb_enc;
  1561. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1562. irq = &phys_enc->irq[INTR_IDX_PP4_OVFL];
  1563. INIT_LIST_HEAD(&irq->cb.list);
  1564. irq->name = "pp4_overflow";
  1565. irq->hw_idx = CWB_4;
  1566. irq->irq_idx = -1;
  1567. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1568. irq->intr_idx = INTR_IDX_PP4_OVFL;
  1569. irq->cb.arg = wb_enc;
  1570. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1571. irq = &phys_enc->irq[INTR_IDX_PP5_OVFL];
  1572. INIT_LIST_HEAD(&irq->cb.list);
  1573. irq->name = "pp5_overflow";
  1574. irq->hw_idx = CWB_5;
  1575. irq->irq_idx = -1;
  1576. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1577. irq->intr_idx = INTR_IDX_PP5_OVFL;
  1578. irq->cb.arg = wb_enc;
  1579. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1580. /* create internal buffer for disable logic */
  1581. if (_sde_encoder_phys_wb_init_internal_fb(wb_enc,
  1582. DRM_FORMAT_RGB888, 2, 1, 6)) {
  1583. SDE_ERROR("failed to init internal fb\n");
  1584. goto fail_wb_init;
  1585. }
  1586. SDE_DEBUG("Created sde_encoder_phys_wb for wb %d\n",
  1587. wb_enc->hw_wb->idx - WB_0);
  1588. return phys_enc;
  1589. fail_wb_init:
  1590. fail_wb_check:
  1591. fail_mdp_init:
  1592. kfree(wb_enc);
  1593. fail_alloc:
  1594. return ERR_PTR(ret);
  1595. }