sde_encoder_phys_wb.c 80 KB

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