sde_encoder_phys_wb.c 55 KB

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