sde_encoder_phys_wb.c 85 KB

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