sde_encoder_phys_wb.c 64 KB

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