sde_encoder_phys_wb.c 84 KB

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