sde_encoder_phys_wb.c 84 KB

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