sde_encoder_phys_wb.c 63 KB

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