sde_encoder_phys_wb.c 63 KB

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