sde_encoder_phys_wb.c 60 KB

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