sde_encoder_phys_wb.c 63 KB

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