sde_encoder_phys_wb.c 82 KB

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