sde_encoder_phys_wb.c 84 KB

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