sde_encoder_phys_wb.c 84 KB

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