sde_encoder_phys_wb.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  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. SDE_DEBUG("[atomic_check:%d,\"%s\",%d,%d]\n",
  619. hw_wb->idx - WB_0, mode->name,
  620. mode->hdisplay, mode->vdisplay);
  621. if (!conn_state || !conn_state->connector) {
  622. SDE_ERROR("invalid connector state\n");
  623. return -EINVAL;
  624. } else if (conn_state->connector->status !=
  625. connector_status_connected) {
  626. SDE_ERROR("connector not connected %d\n",
  627. conn_state->connector->status);
  628. return -EINVAL;
  629. }
  630. _sde_enc_phys_wb_detect_cwb(phys_enc, crtc_state);
  631. memset(&wb_roi, 0, sizeof(struct sde_rect));
  632. rc = sde_wb_connector_state_get_output_roi(conn_state, &wb_roi);
  633. if (rc) {
  634. SDE_ERROR("failed to get roi %d\n", rc);
  635. return rc;
  636. }
  637. SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi.x, wb_roi.y,
  638. wb_roi.w, wb_roi.h);
  639. /* bypass check if commit with no framebuffer */
  640. fb = sde_wb_connector_state_get_output_fb(conn_state);
  641. if (!fb) {
  642. SDE_DEBUG("no output framebuffer\n");
  643. return 0;
  644. }
  645. SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
  646. fb->width, fb->height);
  647. fmt = sde_get_sde_format_ext(fb->format->format, fb->modifier);
  648. if (!fmt) {
  649. SDE_ERROR("unsupported output pixel format:%x\n",
  650. fb->format->format);
  651. return -EINVAL;
  652. }
  653. SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->format->format,
  654. fb->modifier);
  655. if (SDE_FORMAT_IS_YUV(fmt) &&
  656. !(wb_cfg->features & BIT(SDE_WB_YUV_CONFIG))) {
  657. SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
  658. return -EINVAL;
  659. }
  660. if (SDE_FORMAT_IS_UBWC(fmt) &&
  661. !(wb_cfg->features & BIT(SDE_WB_UBWC))) {
  662. SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
  663. return -EINVAL;
  664. }
  665. if (SDE_FORMAT_IS_YUV(fmt) != !!phys_enc->hw_cdm)
  666. crtc_state->mode_changed = true;
  667. /* if in clone mode, return after cwb validation */
  668. if (phys_enc->in_clone_mode) {
  669. rc = _sde_enc_phys_wb_validate_cwb(phys_enc, crtc_state,
  670. conn_state);
  671. if (rc)
  672. SDE_ERROR("failed in cwb validation %d\n", rc);
  673. return rc;
  674. }
  675. if (wb_roi.w && wb_roi.h) {
  676. if (wb_roi.w != mode->hdisplay) {
  677. SDE_ERROR("invalid roi w=%d, mode w=%d\n", wb_roi.w,
  678. mode->hdisplay);
  679. return -EINVAL;
  680. } else if (wb_roi.h != mode->vdisplay) {
  681. SDE_ERROR("invalid roi h=%d, mode h=%d\n", wb_roi.h,
  682. mode->vdisplay);
  683. return -EINVAL;
  684. } else if (wb_roi.x + wb_roi.w > fb->width) {
  685. SDE_ERROR("invalid roi x=%d, w=%d, fb w=%d\n",
  686. wb_roi.x, wb_roi.w, fb->width);
  687. return -EINVAL;
  688. } else if (wb_roi.y + wb_roi.h > fb->height) {
  689. SDE_ERROR("invalid roi y=%d, h=%d, fb h=%d\n",
  690. wb_roi.y, wb_roi.h, fb->height);
  691. return -EINVAL;
  692. } else if (wb_roi.w > SDE_WB_MAX_LINEWIDTH(fmt, wb_cfg)) {
  693. SDE_ERROR("invalid roi ubwc=%d w=%d, maxlinewidth=%u\n",
  694. SDE_FORMAT_IS_UBWC(fmt), wb_roi.w,
  695. SDE_WB_MAX_LINEWIDTH(fmt, wb_cfg));
  696. return -EINVAL;
  697. }
  698. } else {
  699. if (wb_roi.x || wb_roi.y) {
  700. SDE_ERROR("invalid roi x=%d, y=%d\n",
  701. wb_roi.x, wb_roi.y);
  702. return -EINVAL;
  703. } else if (fb->width != mode->hdisplay) {
  704. SDE_ERROR("invalid fb w=%d, mode w=%d\n", fb->width,
  705. mode->hdisplay);
  706. return -EINVAL;
  707. } else if (fb->height != mode->vdisplay) {
  708. SDE_ERROR("invalid fb h=%d, mode h=%d\n", fb->height,
  709. mode->vdisplay);
  710. return -EINVAL;
  711. } else if (fb->width > SDE_WB_MAX_LINEWIDTH(fmt, wb_cfg)) {
  712. SDE_ERROR("invalid fb ubwc=%d w=%d, maxlinewidth=%u\n",
  713. SDE_FORMAT_IS_UBWC(fmt), fb->width,
  714. SDE_WB_MAX_LINEWIDTH(fmt, wb_cfg));
  715. return -EINVAL;
  716. }
  717. }
  718. return rc;
  719. }
  720. static void _sde_encoder_phys_wb_update_cwb_flush(
  721. struct sde_encoder_phys *phys_enc, bool enable)
  722. {
  723. struct sde_encoder_phys_wb *wb_enc;
  724. struct sde_hw_wb *hw_wb;
  725. struct sde_hw_ctl *hw_ctl;
  726. struct sde_hw_cdm *hw_cdm;
  727. struct sde_hw_pingpong *hw_pp;
  728. struct sde_crtc *crtc;
  729. struct sde_crtc_state *crtc_state;
  730. int i = 0;
  731. int cwb_capture_mode = 0;
  732. enum sde_cwb cwb_idx = 0;
  733. enum sde_cwb src_pp_idx = 0;
  734. bool dspp_out = false;
  735. bool need_merge = false;
  736. if (!phys_enc->in_clone_mode) {
  737. SDE_DEBUG("not in CWB mode. early return\n");
  738. return;
  739. }
  740. wb_enc = to_sde_encoder_phys_wb(phys_enc);
  741. crtc = to_sde_crtc(wb_enc->crtc);
  742. crtc_state = to_sde_crtc_state(wb_enc->crtc->state);
  743. cwb_capture_mode = sde_crtc_get_property(crtc_state,
  744. CRTC_PROP_CAPTURE_OUTPUT);
  745. hw_pp = phys_enc->hw_pp;
  746. hw_wb = wb_enc->hw_wb;
  747. hw_cdm = phys_enc->hw_cdm;
  748. /* In CWB mode, program actual source master sde_hw_ctl from crtc */
  749. hw_ctl = crtc->mixers[0].hw_ctl;
  750. if (!hw_ctl || !hw_wb || !hw_pp) {
  751. SDE_ERROR("[wb] HW resource not available for CWB\n");
  752. return;
  753. }
  754. /* treating LM idx of primary display ctl path as source ping-pong idx*/
  755. src_pp_idx = (enum sde_cwb)crtc->mixers[0].hw_lm->idx;
  756. cwb_idx = (enum sde_cwb)hw_pp->idx;
  757. dspp_out = (cwb_capture_mode == CAPTURE_DSPP_OUT);
  758. need_merge = (crtc->num_mixers > 1) ? true : false;
  759. if (src_pp_idx > CWB_0 || ((cwb_idx + crtc->num_mixers) > CWB_MAX)) {
  760. SDE_ERROR("invalid hw config for CWB\n");
  761. return;
  762. }
  763. if (hw_ctl->ops.update_bitmask)
  764. hw_ctl->ops.update_bitmask(hw_ctl, SDE_HW_FLUSH_WB,
  765. hw_wb->idx, 1);
  766. if (hw_ctl->ops.update_bitmask && hw_cdm)
  767. hw_ctl->ops.update_bitmask(hw_ctl, SDE_HW_FLUSH_CDM,
  768. hw_cdm->idx, 1);
  769. if (test_bit(SDE_WB_CWB_CTRL, &hw_wb->caps->features)) {
  770. for (i = 0; i < crtc->num_mixers; i++) {
  771. cwb_idx = (enum sde_cwb) (hw_pp->idx + i);
  772. src_pp_idx = (enum sde_cwb) (src_pp_idx + i);
  773. if (hw_wb->ops.program_cwb_ctrl)
  774. hw_wb->ops.program_cwb_ctrl(hw_wb, cwb_idx,
  775. src_pp_idx, dspp_out, enable);
  776. if (hw_ctl->ops.update_bitmask)
  777. hw_ctl->ops.update_bitmask(hw_ctl,
  778. SDE_HW_FLUSH_CWB, cwb_idx, 1);
  779. }
  780. if (need_merge && hw_ctl->ops.update_bitmask
  781. && hw_pp && hw_pp->merge_3d)
  782. hw_ctl->ops.update_bitmask(hw_ctl,
  783. SDE_HW_FLUSH_MERGE_3D,
  784. hw_pp->merge_3d->idx, 1);
  785. } else {
  786. phys_enc->hw_mdptop->ops.set_cwb_ppb_cntl(phys_enc->hw_mdptop,
  787. need_merge, dspp_out);
  788. }
  789. }
  790. /**
  791. * _sde_encoder_phys_wb_update_flush - flush hardware update
  792. * @phys_enc: Pointer to physical encoder
  793. */
  794. static void _sde_encoder_phys_wb_update_flush(struct sde_encoder_phys *phys_enc)
  795. {
  796. struct sde_encoder_phys_wb *wb_enc;
  797. struct sde_hw_wb *hw_wb;
  798. struct sde_hw_ctl *hw_ctl;
  799. struct sde_hw_cdm *hw_cdm;
  800. struct sde_hw_pingpong *hw_pp;
  801. struct sde_ctl_flush_cfg pending_flush = {0,};
  802. if (!phys_enc)
  803. return;
  804. wb_enc = to_sde_encoder_phys_wb(phys_enc);
  805. hw_wb = wb_enc->hw_wb;
  806. hw_cdm = phys_enc->hw_cdm;
  807. hw_pp = phys_enc->hw_pp;
  808. hw_ctl = phys_enc->hw_ctl;
  809. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  810. if (phys_enc->in_clone_mode) {
  811. SDE_DEBUG("in CWB mode. early return\n");
  812. return;
  813. }
  814. if (!hw_ctl) {
  815. SDE_DEBUG("[wb:%d] no ctl assigned\n", hw_wb->idx - WB_0);
  816. return;
  817. }
  818. if (hw_ctl->ops.update_bitmask)
  819. hw_ctl->ops.update_bitmask(hw_ctl, SDE_HW_FLUSH_WB,
  820. hw_wb->idx, 1);
  821. if (hw_ctl->ops.update_bitmask && hw_cdm)
  822. hw_ctl->ops.update_bitmask(hw_ctl, SDE_HW_FLUSH_CDM,
  823. hw_cdm->idx, 1);
  824. if (hw_ctl->ops.update_bitmask && hw_pp && hw_pp->merge_3d)
  825. hw_ctl->ops.update_bitmask(hw_ctl, SDE_HW_FLUSH_MERGE_3D,
  826. hw_pp->merge_3d->idx, 1);
  827. if (hw_ctl->ops.get_pending_flush)
  828. hw_ctl->ops.get_pending_flush(hw_ctl,
  829. &pending_flush);
  830. SDE_DEBUG("Pending flush mask for CTL_%d is 0x%x, WB %d\n",
  831. hw_ctl->idx - CTL_0, pending_flush.pending_flush_mask,
  832. hw_wb->idx - WB_0);
  833. }
  834. /**
  835. * sde_encoder_phys_wb_setup - setup writeback encoder
  836. * @phys_enc: Pointer to physical encoder
  837. */
  838. static void sde_encoder_phys_wb_setup(
  839. struct sde_encoder_phys *phys_enc)
  840. {
  841. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  842. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  843. struct drm_display_mode mode = phys_enc->cached_mode;
  844. struct drm_framebuffer *fb;
  845. struct sde_rect *wb_roi = &wb_enc->wb_roi;
  846. SDE_DEBUG("[mode_set:%d,\"%s\",%d,%d]\n",
  847. hw_wb->idx - WB_0, mode.name,
  848. mode.hdisplay, mode.vdisplay);
  849. memset(wb_roi, 0, sizeof(struct sde_rect));
  850. /* clear writeback framebuffer - will be updated in setup_fb */
  851. wb_enc->wb_fb = NULL;
  852. wb_enc->wb_aspace = NULL;
  853. if (phys_enc->enable_state == SDE_ENC_DISABLING) {
  854. fb = wb_enc->fb_disable;
  855. wb_roi->w = 0;
  856. wb_roi->h = 0;
  857. } else {
  858. fb = sde_wb_get_output_fb(wb_enc->wb_dev);
  859. sde_wb_get_output_roi(wb_enc->wb_dev, wb_roi);
  860. }
  861. if (!fb) {
  862. SDE_DEBUG("no output framebuffer\n");
  863. return;
  864. }
  865. SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
  866. fb->width, fb->height);
  867. if (wb_roi->w == 0 || wb_roi->h == 0) {
  868. wb_roi->x = 0;
  869. wb_roi->y = 0;
  870. wb_roi->w = fb->width;
  871. wb_roi->h = fb->height;
  872. }
  873. SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi->x, wb_roi->y,
  874. wb_roi->w, wb_roi->h);
  875. wb_enc->wb_fmt = sde_get_sde_format_ext(fb->format->format,
  876. fb->modifier);
  877. if (!wb_enc->wb_fmt) {
  878. SDE_ERROR("unsupported output pixel format: %d\n",
  879. fb->format->format);
  880. return;
  881. }
  882. SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->format->format,
  883. fb->modifier);
  884. sde_encoder_phys_wb_set_ot_limit(phys_enc);
  885. sde_encoder_phys_wb_set_qos_remap(phys_enc);
  886. sde_encoder_phys_wb_set_qos(phys_enc);
  887. sde_encoder_phys_setup_cdm(phys_enc, fb, wb_enc->wb_fmt, wb_roi);
  888. sde_encoder_phys_wb_setup_fb(phys_enc, fb, wb_roi);
  889. sde_encoder_phys_wb_setup_cdp(phys_enc, wb_enc->wb_fmt);
  890. _sde_encoder_phys_wb_setup_cwb(phys_enc, true);
  891. }
  892. static void _sde_encoder_phys_wb_frame_done_helper(void *arg, bool frame_error)
  893. {
  894. struct sde_encoder_phys_wb *wb_enc = arg;
  895. struct sde_encoder_phys *phys_enc = &wb_enc->base;
  896. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  897. u32 event = frame_error ? SDE_ENCODER_FRAME_EVENT_ERROR : 0;
  898. SDE_DEBUG("[wb:%d,%u]\n", hw_wb->idx - WB_0, wb_enc->frame_count);
  899. /* don't notify upper layer for internal commit */
  900. if (phys_enc->enable_state == SDE_ENC_DISABLING)
  901. goto complete;
  902. if (phys_enc->parent_ops.handle_frame_done &&
  903. atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0)) {
  904. event |= SDE_ENCODER_FRAME_EVENT_DONE |
  905. SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE;
  906. if (phys_enc->in_clone_mode)
  907. event |= SDE_ENCODER_FRAME_EVENT_CWB_DONE;
  908. else
  909. event |= SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  910. phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
  911. phys_enc, event);
  912. }
  913. if (!phys_enc->in_clone_mode && phys_enc->parent_ops.handle_vblank_virt)
  914. phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
  915. phys_enc);
  916. SDE_EVT32_IRQ(DRMID(phys_enc->parent), hw_wb->idx - WB_0, event,
  917. frame_error);
  918. complete:
  919. wake_up_all(&phys_enc->pending_kickoff_wq);
  920. }
  921. /**
  922. * sde_encoder_phys_wb_done_irq - Pingpong overflow interrupt handler for CWB
  923. * @arg: Pointer to writeback encoder
  924. * @irq_idx: interrupt index
  925. */
  926. static void sde_encoder_phys_cwb_ovflow(void *arg, int irq_idx)
  927. {
  928. _sde_encoder_phys_wb_frame_done_helper(arg, true);
  929. }
  930. /**
  931. * sde_encoder_phys_wb_done_irq - writeback interrupt handler
  932. * @arg: Pointer to writeback encoder
  933. * @irq_idx: interrupt index
  934. */
  935. static void sde_encoder_phys_wb_done_irq(void *arg, int irq_idx)
  936. {
  937. _sde_encoder_phys_wb_frame_done_helper(arg, false);
  938. }
  939. /**
  940. * sde_encoder_phys_wb_irq_ctrl - irq control of WB
  941. * @phys: Pointer to physical encoder
  942. * @enable: indicates enable or disable interrupts
  943. */
  944. static void sde_encoder_phys_wb_irq_ctrl(
  945. struct sde_encoder_phys *phys, bool enable)
  946. {
  947. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys);
  948. int index = 0, refcount;
  949. int ret = 0, pp = 0;
  950. if (!wb_enc)
  951. return;
  952. if (wb_enc->bypass_irqreg)
  953. return;
  954. pp = phys->hw_pp->idx - PINGPONG_0;
  955. if ((pp + CRTC_DUAL_MIXERS_ONLY) >= PINGPONG_MAX) {
  956. SDE_ERROR("invalid pingpong index for WB or CWB\n");
  957. return;
  958. }
  959. refcount = atomic_read(&phys->wbirq_refcount);
  960. if (enable && atomic_inc_return(&phys->wbirq_refcount) == 1) {
  961. sde_encoder_helper_register_irq(phys, INTR_IDX_WB_DONE);
  962. if (ret)
  963. atomic_dec_return(&phys->wbirq_refcount);
  964. for (index = 0; index < CRTC_DUAL_MIXERS_ONLY; index++)
  965. if (cwb_irq_tbl[index + pp] != SDE_NONE)
  966. sde_encoder_helper_register_irq(phys,
  967. cwb_irq_tbl[index + pp]);
  968. } else if (!enable &&
  969. atomic_dec_return(&phys->wbirq_refcount) == 0) {
  970. sde_encoder_helper_unregister_irq(phys, INTR_IDX_WB_DONE);
  971. if (ret)
  972. atomic_inc_return(&phys->wbirq_refcount);
  973. for (index = 0; index < CRTC_DUAL_MIXERS_ONLY; index++)
  974. if (cwb_irq_tbl[index + pp] != SDE_NONE)
  975. sde_encoder_helper_unregister_irq(phys,
  976. cwb_irq_tbl[index + pp]);
  977. }
  978. }
  979. /**
  980. * sde_encoder_phys_wb_mode_set - set display mode
  981. * @phys_enc: Pointer to physical encoder
  982. * @mode: Pointer to requested display mode
  983. * @adj_mode: Pointer to adjusted display mode
  984. */
  985. static void sde_encoder_phys_wb_mode_set(
  986. struct sde_encoder_phys *phys_enc,
  987. struct drm_display_mode *mode,
  988. struct drm_display_mode *adj_mode)
  989. {
  990. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  991. struct sde_rm *rm = &phys_enc->sde_kms->rm;
  992. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  993. struct sde_rm_hw_iter iter;
  994. int i, instance;
  995. phys_enc->cached_mode = *adj_mode;
  996. instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
  997. SDE_DEBUG("[mode_set_cache:%d,\"%s\",%d,%d]\n",
  998. hw_wb->idx - WB_0, mode->name,
  999. mode->hdisplay, mode->vdisplay);
  1000. phys_enc->hw_ctl = NULL;
  1001. phys_enc->hw_cdm = NULL;
  1002. /* Retrieve previously allocated HW Resources. CTL shouldn't fail */
  1003. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
  1004. for (i = 0; i <= instance; i++) {
  1005. sde_rm_get_hw(rm, &iter);
  1006. if (i == instance)
  1007. phys_enc->hw_ctl = (struct sde_hw_ctl *) iter.hw;
  1008. }
  1009. if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
  1010. SDE_ERROR("failed init ctl: %ld\n",
  1011. (!phys_enc->hw_ctl) ?
  1012. -EINVAL : PTR_ERR(phys_enc->hw_ctl));
  1013. phys_enc->hw_ctl = NULL;
  1014. return;
  1015. }
  1016. /* CDM is optional */
  1017. sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CDM);
  1018. for (i = 0; i <= instance; i++) {
  1019. sde_rm_get_hw(rm, &iter);
  1020. if (i == instance)
  1021. phys_enc->hw_cdm = (struct sde_hw_cdm *) iter.hw;
  1022. }
  1023. if (IS_ERR(phys_enc->hw_cdm)) {
  1024. SDE_ERROR("CDM required but not allocated: %ld\n",
  1025. PTR_ERR(phys_enc->hw_cdm));
  1026. phys_enc->hw_cdm = NULL;
  1027. }
  1028. }
  1029. static int sde_encoder_phys_wb_frame_timeout(struct sde_encoder_phys *phys_enc)
  1030. {
  1031. u32 event = 0;
  1032. while (atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0) &&
  1033. phys_enc->parent_ops.handle_frame_done) {
  1034. event = SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE
  1035. | SDE_ENCODER_FRAME_EVENT_ERROR;
  1036. if (phys_enc->in_clone_mode)
  1037. event |= SDE_ENCODER_FRAME_EVENT_CWB_DONE;
  1038. else
  1039. event |= SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE;
  1040. phys_enc->parent_ops.handle_frame_done(
  1041. phys_enc->parent, phys_enc, event);
  1042. SDE_EVT32(DRMID(phys_enc->parent), event,
  1043. atomic_read(&phys_enc->pending_retire_fence_cnt));
  1044. }
  1045. return event;
  1046. }
  1047. static bool _sde_encoder_phys_wb_is_idle(
  1048. struct sde_encoder_phys *phys_enc)
  1049. {
  1050. bool ret = false;
  1051. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1052. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  1053. struct sde_vbif_get_xin_status_params xin_status = {0};
  1054. xin_status.vbif_idx = hw_wb->caps->vbif_idx;
  1055. xin_status.xin_id = hw_wb->caps->xin_id;
  1056. xin_status.clk_ctrl = hw_wb->caps->clk_ctrl;
  1057. if (sde_vbif_get_xin_status(phys_enc->sde_kms, &xin_status)) {
  1058. _sde_encoder_phys_wb_frame_done_helper(wb_enc, false);
  1059. ret = true;
  1060. }
  1061. return ret;
  1062. }
  1063. static int _sde_encoder_phys_wb_wait_for_commit_done(
  1064. struct sde_encoder_phys *phys_enc, bool is_disable)
  1065. {
  1066. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1067. u32 event = 0;
  1068. u64 wb_time = 0;
  1069. int rc = 0;
  1070. struct sde_encoder_wait_info wait_info = {0};
  1071. /* Return EWOULDBLOCK since we know the wait isn't necessary */
  1072. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1073. SDE_ERROR("encoder already disabled\n");
  1074. return -EWOULDBLOCK;
  1075. }
  1076. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
  1077. wb_enc->kickoff_count, !!wb_enc->wb_fb, is_disable,
  1078. phys_enc->in_clone_mode);
  1079. if (!is_disable && phys_enc->in_clone_mode &&
  1080. (atomic_read(&phys_enc->pending_retire_fence_cnt) <= 1))
  1081. goto skip_wait;
  1082. /* signal completion if commit with no framebuffer */
  1083. if (!wb_enc->wb_fb) {
  1084. SDE_DEBUG("no output framebuffer\n");
  1085. _sde_encoder_phys_wb_frame_done_helper(wb_enc, false);
  1086. }
  1087. wait_info.wq = &phys_enc->pending_kickoff_wq;
  1088. wait_info.atomic_cnt = &phys_enc->pending_retire_fence_cnt;
  1089. wait_info.timeout_ms = max_t(u32, wb_enc->wbdone_timeout,
  1090. KICKOFF_TIMEOUT_MS);
  1091. rc = sde_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_WB_DONE,
  1092. &wait_info);
  1093. if (rc == -ETIMEDOUT && _sde_encoder_phys_wb_is_idle(phys_enc)) {
  1094. rc = 0;
  1095. } else if (rc == -ETIMEDOUT) {
  1096. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
  1097. wb_enc->frame_count, SDE_EVTLOG_ERROR);
  1098. SDE_ERROR("wb:%d kickoff timed out\n", WBID(wb_enc));
  1099. event = sde_encoder_phys_wb_frame_timeout(phys_enc);
  1100. }
  1101. /* cleanup writeback framebuffer */
  1102. if (wb_enc->wb_fb && wb_enc->wb_aspace) {
  1103. msm_framebuffer_cleanup(wb_enc->wb_fb, wb_enc->wb_aspace);
  1104. drm_framebuffer_put(wb_enc->wb_fb);
  1105. wb_enc->wb_fb = NULL;
  1106. wb_enc->wb_aspace = NULL;
  1107. }
  1108. skip_wait:
  1109. /* remove vote for iommu/clk/bus */
  1110. wb_enc->frame_count++;
  1111. if (!rc) {
  1112. wb_enc->end_time = ktime_get();
  1113. wb_time = (u64)ktime_to_us(wb_enc->end_time) -
  1114. (u64)ktime_to_us(wb_enc->start_time);
  1115. SDE_DEBUG("wb:%d took %llu us\n", WBID(wb_enc), wb_time);
  1116. }
  1117. /* cleanup previous buffer if pending */
  1118. if (wb_enc->cwb_old_fb && wb_enc->cwb_old_aspace) {
  1119. msm_framebuffer_cleanup(wb_enc->cwb_old_fb, wb_enc->cwb_old_aspace);
  1120. drm_framebuffer_put(wb_enc->cwb_old_fb);
  1121. wb_enc->cwb_old_fb = NULL;
  1122. wb_enc->cwb_old_aspace = NULL;
  1123. }
  1124. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
  1125. wb_time, event, rc);
  1126. return rc;
  1127. }
  1128. /**
  1129. * sde_encoder_phys_wb_wait_for_commit_done - wait until request is committed
  1130. * @phys_enc: Pointer to physical encoder
  1131. */
  1132. static int sde_encoder_phys_wb_wait_for_commit_done(
  1133. struct sde_encoder_phys *phys_enc)
  1134. {
  1135. return _sde_encoder_phys_wb_wait_for_commit_done(phys_enc, false);
  1136. }
  1137. static int sde_encoder_phys_wb_wait_for_tx_complete(
  1138. struct sde_encoder_phys *phys_enc)
  1139. {
  1140. if (!atomic_read(&phys_enc->pending_retire_fence_cnt))
  1141. return 0;
  1142. return _sde_encoder_phys_wb_wait_for_commit_done(phys_enc, true);
  1143. }
  1144. /**
  1145. * sde_encoder_phys_wb_prepare_for_kickoff - pre-kickoff processing
  1146. * @phys_enc: Pointer to physical encoder
  1147. * @params: kickoff parameters
  1148. * Returns: Zero on success
  1149. */
  1150. static int sde_encoder_phys_wb_prepare_for_kickoff(
  1151. struct sde_encoder_phys *phys_enc,
  1152. struct sde_encoder_kickoff_params *params)
  1153. {
  1154. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1155. SDE_DEBUG("[wb:%d,%u]\n", wb_enc->hw_wb->idx - WB_0,
  1156. wb_enc->kickoff_count);
  1157. if (phys_enc->in_clone_mode) {
  1158. wb_enc->cwb_old_fb = wb_enc->wb_fb;
  1159. wb_enc->cwb_old_aspace = wb_enc->wb_aspace;
  1160. }
  1161. wb_enc->kickoff_count++;
  1162. /* set OT limit & enable traffic shaper */
  1163. sde_encoder_phys_wb_setup(phys_enc);
  1164. _sde_encoder_phys_wb_update_flush(phys_enc);
  1165. _sde_encoder_phys_wb_update_cwb_flush(phys_enc, true);
  1166. /* vote for iommu/clk/bus */
  1167. wb_enc->start_time = ktime_get();
  1168. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
  1169. wb_enc->kickoff_count, wb_enc->frame_count,
  1170. phys_enc->in_clone_mode);
  1171. return 0;
  1172. }
  1173. /**
  1174. * sde_encoder_phys_wb_trigger_flush - trigger flush processing
  1175. * @phys_enc: Pointer to physical encoder
  1176. */
  1177. static void sde_encoder_phys_wb_trigger_flush(struct sde_encoder_phys *phys_enc)
  1178. {
  1179. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1180. if (!phys_enc || !wb_enc->hw_wb) {
  1181. SDE_ERROR("invalid encoder\n");
  1182. return;
  1183. }
  1184. /*
  1185. * Bail out iff in CWB mode. In case of CWB, primary control-path
  1186. * which is actually driving would trigger the flush
  1187. */
  1188. if (phys_enc->in_clone_mode) {
  1189. SDE_DEBUG("in CWB mode. early return\n");
  1190. return;
  1191. }
  1192. SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
  1193. /* clear pending flush if commit with no framebuffer */
  1194. if (!wb_enc->wb_fb) {
  1195. SDE_DEBUG("no output framebuffer\n");
  1196. return;
  1197. }
  1198. sde_encoder_helper_trigger_flush(phys_enc);
  1199. }
  1200. /**
  1201. * sde_encoder_phys_wb_handle_post_kickoff - post-kickoff processing
  1202. * @phys_enc: Pointer to physical encoder
  1203. */
  1204. static void sde_encoder_phys_wb_handle_post_kickoff(
  1205. struct sde_encoder_phys *phys_enc)
  1206. {
  1207. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1208. SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
  1209. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc));
  1210. }
  1211. /**
  1212. * _sde_encoder_phys_wb_init_internal_fb - create fb for internal commit
  1213. * @wb_enc: Pointer to writeback encoder
  1214. * @pixel_format: DRM pixel format
  1215. * @width: Desired fb width
  1216. * @height: Desired fb height
  1217. * @pitch: Desired fb pitch
  1218. */
  1219. static int _sde_encoder_phys_wb_init_internal_fb(
  1220. struct sde_encoder_phys_wb *wb_enc,
  1221. uint32_t pixel_format, uint32_t width,
  1222. uint32_t height, uint32_t pitch)
  1223. {
  1224. struct drm_device *dev;
  1225. struct drm_framebuffer *fb;
  1226. struct drm_mode_fb_cmd2 mode_cmd;
  1227. uint32_t size;
  1228. int nplanes, i, ret;
  1229. struct msm_gem_address_space *aspace;
  1230. const struct drm_format_info *info;
  1231. if (!wb_enc || !wb_enc->base.parent || !wb_enc->base.sde_kms) {
  1232. SDE_ERROR("invalid params\n");
  1233. return -EINVAL;
  1234. }
  1235. aspace = wb_enc->base.sde_kms->aspace[SDE_IOMMU_DOMAIN_UNSECURE];
  1236. if (!aspace) {
  1237. SDE_ERROR("invalid address space\n");
  1238. return -EINVAL;
  1239. }
  1240. dev = wb_enc->base.sde_kms->dev;
  1241. if (!dev) {
  1242. SDE_ERROR("invalid dev\n");
  1243. return -EINVAL;
  1244. }
  1245. memset(&mode_cmd, 0, sizeof(mode_cmd));
  1246. mode_cmd.pixel_format = pixel_format;
  1247. mode_cmd.width = width;
  1248. mode_cmd.height = height;
  1249. mode_cmd.pitches[0] = pitch;
  1250. size = sde_format_get_framebuffer_size(pixel_format,
  1251. mode_cmd.width, mode_cmd.height,
  1252. mode_cmd.pitches, 0);
  1253. if (!size) {
  1254. SDE_DEBUG("not creating zero size buffer\n");
  1255. return -EINVAL;
  1256. }
  1257. /* allocate gem tracking object */
  1258. info = drm_get_format_info(dev, &mode_cmd);
  1259. nplanes = info->num_planes;
  1260. if (nplanes >= SDE_MAX_PLANES) {
  1261. SDE_ERROR("requested format has too many planes\n");
  1262. return -EINVAL;
  1263. }
  1264. wb_enc->bo_disable[0] = msm_gem_new(dev, size,
  1265. MSM_BO_SCANOUT | MSM_BO_WC);
  1266. if (IS_ERR_OR_NULL(wb_enc->bo_disable[0])) {
  1267. ret = PTR_ERR(wb_enc->bo_disable[0]);
  1268. wb_enc->bo_disable[0] = NULL;
  1269. SDE_ERROR("failed to create bo, %d\n", ret);
  1270. return ret;
  1271. }
  1272. for (i = 0; i < nplanes; ++i) {
  1273. wb_enc->bo_disable[i] = wb_enc->bo_disable[0];
  1274. mode_cmd.pitches[i] = width * info->cpp[i];
  1275. }
  1276. fb = msm_framebuffer_init(dev, &mode_cmd, wb_enc->bo_disable);
  1277. if (IS_ERR_OR_NULL(fb)) {
  1278. ret = PTR_ERR(fb);
  1279. drm_gem_object_put(wb_enc->bo_disable[0]);
  1280. wb_enc->bo_disable[0] = NULL;
  1281. SDE_ERROR("failed to init fb, %d\n", ret);
  1282. return ret;
  1283. }
  1284. /* prepare the backing buffer now so that it's available later */
  1285. ret = msm_framebuffer_prepare(fb, aspace);
  1286. if (!ret)
  1287. wb_enc->fb_disable = fb;
  1288. return ret;
  1289. }
  1290. /**
  1291. * _sde_encoder_phys_wb_destroy_internal_fb - deconstruct internal fb
  1292. * @wb_enc: Pointer to writeback encoder
  1293. */
  1294. static void _sde_encoder_phys_wb_destroy_internal_fb(
  1295. struct sde_encoder_phys_wb *wb_enc)
  1296. {
  1297. if (!wb_enc)
  1298. return;
  1299. if (wb_enc->fb_disable) {
  1300. drm_framebuffer_unregister_private(wb_enc->fb_disable);
  1301. drm_framebuffer_remove(wb_enc->fb_disable);
  1302. wb_enc->fb_disable = NULL;
  1303. }
  1304. if (wb_enc->bo_disable[0]) {
  1305. drm_gem_object_put(wb_enc->bo_disable[0]);
  1306. wb_enc->bo_disable[0] = NULL;
  1307. }
  1308. }
  1309. /**
  1310. * sde_encoder_phys_wb_enable - enable writeback encoder
  1311. * @phys_enc: Pointer to physical encoder
  1312. */
  1313. static void sde_encoder_phys_wb_enable(struct sde_encoder_phys *phys_enc)
  1314. {
  1315. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1316. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  1317. struct drm_device *dev;
  1318. struct drm_connector *connector;
  1319. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  1320. if (!wb_enc->base.parent || !wb_enc->base.parent->dev) {
  1321. SDE_ERROR("invalid drm device\n");
  1322. return;
  1323. }
  1324. dev = wb_enc->base.parent->dev;
  1325. /* find associated writeback connector */
  1326. connector = phys_enc->connector;
  1327. if (!connector || connector->encoder != phys_enc->parent) {
  1328. SDE_ERROR("failed to find writeback connector\n");
  1329. return;
  1330. }
  1331. wb_enc->wb_dev = sde_wb_connector_get_wb(connector);
  1332. phys_enc->enable_state = SDE_ENC_ENABLED;
  1333. /*
  1334. * cache the crtc in wb_enc on enable for duration of use case
  1335. * for correctly servicing asynchronous irq events and timers
  1336. */
  1337. wb_enc->crtc = phys_enc->parent->crtc;
  1338. }
  1339. /**
  1340. * sde_encoder_phys_wb_disable - disable writeback encoder
  1341. * @phys_enc: Pointer to physical encoder
  1342. */
  1343. static void sde_encoder_phys_wb_disable(struct sde_encoder_phys *phys_enc)
  1344. {
  1345. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1346. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  1347. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  1348. if (phys_enc->enable_state == SDE_ENC_DISABLED) {
  1349. SDE_ERROR("encoder is already disabled\n");
  1350. return;
  1351. }
  1352. SDE_DEBUG("[wait_for_done: wb:%d, frame:%u, kickoff:%u]\n",
  1353. hw_wb->idx - WB_0, wb_enc->frame_count,
  1354. wb_enc->kickoff_count);
  1355. _sde_encoder_phys_wb_wait_for_commit_done(phys_enc, true);
  1356. if (!phys_enc->hw_ctl || !phys_enc->parent ||
  1357. !phys_enc->sde_kms || !wb_enc->fb_disable) {
  1358. SDE_DEBUG("invalid enc, skipping extra commit\n");
  1359. goto exit;
  1360. }
  1361. /* avoid reset frame for CWB */
  1362. if (phys_enc->in_clone_mode) {
  1363. _sde_encoder_phys_wb_setup_cwb(phys_enc, false);
  1364. _sde_encoder_phys_wb_update_cwb_flush(phys_enc, false);
  1365. phys_enc->in_clone_mode = false;
  1366. goto exit;
  1367. }
  1368. /* reset h/w before final flush */
  1369. if (phys_enc->hw_ctl->ops.clear_pending_flush)
  1370. phys_enc->hw_ctl->ops.clear_pending_flush(phys_enc->hw_ctl);
  1371. /*
  1372. * New CTL reset sequence from 5.0 MDP onwards.
  1373. * If has_3d_merge_reset is not set, legacy reset
  1374. * sequence is executed.
  1375. */
  1376. if (hw_wb->catalog->has_3d_merge_reset) {
  1377. sde_encoder_helper_phys_disable(phys_enc, wb_enc);
  1378. goto exit;
  1379. }
  1380. if (sde_encoder_helper_reset_mixers(phys_enc, NULL))
  1381. goto exit;
  1382. phys_enc->enable_state = SDE_ENC_DISABLING;
  1383. sde_encoder_phys_wb_prepare_for_kickoff(phys_enc, NULL);
  1384. sde_encoder_phys_wb_irq_ctrl(phys_enc, true);
  1385. if (phys_enc->hw_ctl->ops.trigger_flush)
  1386. phys_enc->hw_ctl->ops.trigger_flush(phys_enc->hw_ctl);
  1387. sde_encoder_helper_trigger_start(phys_enc);
  1388. _sde_encoder_phys_wb_wait_for_commit_done(phys_enc, true);
  1389. sde_encoder_phys_wb_irq_ctrl(phys_enc, false);
  1390. exit:
  1391. /*
  1392. * frame count and kickoff count are only used for debug purpose. Frame
  1393. * count can be more than kickoff count at the end of disable call due
  1394. * to extra frame_done wait. It does not cause any issue because
  1395. * frame_done wait is based on retire_fence count. Leaving these
  1396. * counters for debugging purpose.
  1397. */
  1398. if (wb_enc->frame_count != wb_enc->kickoff_count) {
  1399. SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
  1400. wb_enc->kickoff_count, wb_enc->frame_count,
  1401. phys_enc->in_clone_mode);
  1402. wb_enc->frame_count = wb_enc->kickoff_count;
  1403. }
  1404. phys_enc->enable_state = SDE_ENC_DISABLED;
  1405. wb_enc->crtc = NULL;
  1406. phys_enc->hw_cdm = NULL;
  1407. phys_enc->hw_ctl = NULL;
  1408. }
  1409. /**
  1410. * sde_encoder_phys_wb_get_hw_resources - get hardware resources
  1411. * @phys_enc: Pointer to physical encoder
  1412. * @hw_res: Pointer to encoder resources
  1413. */
  1414. static void sde_encoder_phys_wb_get_hw_resources(
  1415. struct sde_encoder_phys *phys_enc,
  1416. struct sde_encoder_hw_resources *hw_res,
  1417. struct drm_connector_state *conn_state)
  1418. {
  1419. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1420. struct sde_hw_wb *hw_wb;
  1421. struct drm_framebuffer *fb;
  1422. const struct sde_format *fmt = NULL;
  1423. if (!phys_enc) {
  1424. SDE_ERROR("invalid encoder\n");
  1425. return;
  1426. }
  1427. fb = sde_wb_connector_state_get_output_fb(conn_state);
  1428. if (fb) {
  1429. fmt = sde_get_sde_format_ext(fb->format->format, fb->modifier);
  1430. if (!fmt) {
  1431. SDE_ERROR("unsupported output pixel format:%d\n",
  1432. fb->format->format);
  1433. return;
  1434. }
  1435. }
  1436. hw_wb = wb_enc->hw_wb;
  1437. hw_res->wbs[hw_wb->idx - WB_0] = phys_enc->intf_mode;
  1438. hw_res->needs_cdm = fmt ? SDE_FORMAT_IS_YUV(fmt) : false;
  1439. SDE_DEBUG("[wb:%d] intf_mode=%d needs_cdm=%d\n", hw_wb->idx - WB_0,
  1440. hw_res->wbs[hw_wb->idx - WB_0],
  1441. hw_res->needs_cdm);
  1442. }
  1443. #ifdef CONFIG_DEBUG_FS
  1444. /**
  1445. * sde_encoder_phys_wb_init_debugfs - initialize writeback encoder debugfs
  1446. * @phys_enc: Pointer to physical encoder
  1447. * @debugfs_root: Pointer to virtual encoder's debugfs_root dir
  1448. */
  1449. static int sde_encoder_phys_wb_init_debugfs(
  1450. struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
  1451. {
  1452. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1453. if (!phys_enc || !wb_enc->hw_wb || !debugfs_root)
  1454. return -EINVAL;
  1455. if (!debugfs_create_u32("wbdone_timeout", 0600,
  1456. debugfs_root, &wb_enc->wbdone_timeout)) {
  1457. SDE_ERROR("failed to create debugfs/wbdone_timeout\n");
  1458. return -ENOMEM;
  1459. }
  1460. return 0;
  1461. }
  1462. #else
  1463. static int sde_encoder_phys_wb_init_debugfs(
  1464. struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
  1465. {
  1466. return 0;
  1467. }
  1468. #endif
  1469. static int sde_encoder_phys_wb_late_register(struct sde_encoder_phys *phys_enc,
  1470. struct dentry *debugfs_root)
  1471. {
  1472. return sde_encoder_phys_wb_init_debugfs(phys_enc, debugfs_root);
  1473. }
  1474. /**
  1475. * sde_encoder_phys_wb_destroy - destroy writeback encoder
  1476. * @phys_enc: Pointer to physical encoder
  1477. */
  1478. static void sde_encoder_phys_wb_destroy(struct sde_encoder_phys *phys_enc)
  1479. {
  1480. struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
  1481. struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
  1482. SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
  1483. if (!phys_enc)
  1484. return;
  1485. _sde_encoder_phys_wb_destroy_internal_fb(wb_enc);
  1486. kfree(wb_enc);
  1487. }
  1488. /**
  1489. * sde_encoder_phys_wb_init_ops - initialize writeback operations
  1490. * @ops: Pointer to encoder operation table
  1491. */
  1492. static void sde_encoder_phys_wb_init_ops(struct sde_encoder_phys_ops *ops)
  1493. {
  1494. ops->late_register = sde_encoder_phys_wb_late_register;
  1495. ops->is_master = sde_encoder_phys_wb_is_master;
  1496. ops->mode_set = sde_encoder_phys_wb_mode_set;
  1497. ops->enable = sde_encoder_phys_wb_enable;
  1498. ops->disable = sde_encoder_phys_wb_disable;
  1499. ops->destroy = sde_encoder_phys_wb_destroy;
  1500. ops->atomic_check = sde_encoder_phys_wb_atomic_check;
  1501. ops->get_hw_resources = sde_encoder_phys_wb_get_hw_resources;
  1502. ops->wait_for_commit_done = sde_encoder_phys_wb_wait_for_commit_done;
  1503. ops->wait_for_tx_complete = sde_encoder_phys_wb_wait_for_tx_complete;
  1504. ops->prepare_for_kickoff = sde_encoder_phys_wb_prepare_for_kickoff;
  1505. ops->handle_post_kickoff = sde_encoder_phys_wb_handle_post_kickoff;
  1506. ops->trigger_flush = sde_encoder_phys_wb_trigger_flush;
  1507. ops->trigger_start = sde_encoder_helper_trigger_start;
  1508. ops->hw_reset = sde_encoder_helper_hw_reset;
  1509. ops->irq_control = sde_encoder_phys_wb_irq_ctrl;
  1510. }
  1511. /**
  1512. * sde_encoder_phys_wb_init - initialize writeback encoder
  1513. * @init: Pointer to init info structure with initialization params
  1514. */
  1515. struct sde_encoder_phys *sde_encoder_phys_wb_init(
  1516. struct sde_enc_phys_init_params *p)
  1517. {
  1518. struct sde_encoder_phys *phys_enc;
  1519. struct sde_encoder_phys_wb *wb_enc;
  1520. struct sde_hw_mdp *hw_mdp;
  1521. struct sde_encoder_irq *irq;
  1522. int ret = 0;
  1523. SDE_DEBUG("\n");
  1524. if (!p || !p->parent) {
  1525. SDE_ERROR("invalid params\n");
  1526. ret = -EINVAL;
  1527. goto fail_alloc;
  1528. }
  1529. wb_enc = kzalloc(sizeof(*wb_enc), GFP_KERNEL);
  1530. if (!wb_enc) {
  1531. SDE_ERROR("failed to allocate wb enc\n");
  1532. ret = -ENOMEM;
  1533. goto fail_alloc;
  1534. }
  1535. wb_enc->wbdone_timeout = KICKOFF_TIMEOUT_MS;
  1536. phys_enc = &wb_enc->base;
  1537. if (p->sde_kms->vbif[VBIF_NRT]) {
  1538. wb_enc->aspace[SDE_IOMMU_DOMAIN_UNSECURE] =
  1539. p->sde_kms->aspace[MSM_SMMU_DOMAIN_NRT_UNSECURE];
  1540. wb_enc->aspace[SDE_IOMMU_DOMAIN_SECURE] =
  1541. p->sde_kms->aspace[MSM_SMMU_DOMAIN_NRT_SECURE];
  1542. } else {
  1543. wb_enc->aspace[SDE_IOMMU_DOMAIN_UNSECURE] =
  1544. p->sde_kms->aspace[MSM_SMMU_DOMAIN_UNSECURE];
  1545. wb_enc->aspace[SDE_IOMMU_DOMAIN_SECURE] =
  1546. p->sde_kms->aspace[MSM_SMMU_DOMAIN_SECURE];
  1547. }
  1548. hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
  1549. if (IS_ERR_OR_NULL(hw_mdp)) {
  1550. ret = PTR_ERR(hw_mdp);
  1551. SDE_ERROR("failed to init hw_top: %d\n", ret);
  1552. goto fail_mdp_init;
  1553. }
  1554. phys_enc->hw_mdptop = hw_mdp;
  1555. /**
  1556. * hw_wb resource permanently assigned to this encoder
  1557. * Other resources allocated at atomic commit time by use case
  1558. */
  1559. if (p->wb_idx != SDE_NONE) {
  1560. struct sde_rm_hw_iter iter;
  1561. sde_rm_init_hw_iter(&iter, 0, SDE_HW_BLK_WB);
  1562. while (sde_rm_get_hw(&p->sde_kms->rm, &iter)) {
  1563. struct sde_hw_wb *hw_wb = (struct sde_hw_wb *)iter.hw;
  1564. if (hw_wb->idx == p->wb_idx) {
  1565. wb_enc->hw_wb = hw_wb;
  1566. break;
  1567. }
  1568. }
  1569. if (!wb_enc->hw_wb) {
  1570. ret = -EINVAL;
  1571. SDE_ERROR("failed to init hw_wb%d\n", p->wb_idx - WB_0);
  1572. goto fail_wb_init;
  1573. }
  1574. } else {
  1575. ret = -EINVAL;
  1576. SDE_ERROR("invalid wb_idx\n");
  1577. goto fail_wb_check;
  1578. }
  1579. sde_encoder_phys_wb_init_ops(&phys_enc->ops);
  1580. phys_enc->parent = p->parent;
  1581. phys_enc->parent_ops = p->parent_ops;
  1582. phys_enc->sde_kms = p->sde_kms;
  1583. phys_enc->split_role = p->split_role;
  1584. phys_enc->intf_mode = INTF_MODE_WB_LINE;
  1585. phys_enc->intf_idx = p->intf_idx;
  1586. phys_enc->enc_spinlock = p->enc_spinlock;
  1587. phys_enc->vblank_ctl_lock = p->vblank_ctl_lock;
  1588. atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
  1589. atomic_set(&phys_enc->wbirq_refcount, 0);
  1590. init_waitqueue_head(&phys_enc->pending_kickoff_wq);
  1591. irq = &phys_enc->irq[INTR_IDX_WB_DONE];
  1592. INIT_LIST_HEAD(&irq->cb.list);
  1593. irq->name = "wb_done";
  1594. irq->hw_idx = wb_enc->hw_wb->idx;
  1595. irq->irq_idx = -1;
  1596. irq->intr_type = sde_encoder_phys_wb_get_intr_type(wb_enc->hw_wb);
  1597. irq->intr_idx = INTR_IDX_WB_DONE;
  1598. irq->cb.arg = wb_enc;
  1599. irq->cb.func = sde_encoder_phys_wb_done_irq;
  1600. irq = &phys_enc->irq[INTR_IDX_PP1_OVFL];
  1601. INIT_LIST_HEAD(&irq->cb.list);
  1602. irq->name = "pp1_overflow";
  1603. irq->hw_idx = CWB_1;
  1604. irq->irq_idx = -1;
  1605. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1606. irq->intr_idx = INTR_IDX_PP1_OVFL;
  1607. irq->cb.arg = wb_enc;
  1608. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1609. irq = &phys_enc->irq[INTR_IDX_PP2_OVFL];
  1610. INIT_LIST_HEAD(&irq->cb.list);
  1611. irq->name = "pp2_overflow";
  1612. irq->hw_idx = CWB_2;
  1613. irq->irq_idx = -1;
  1614. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1615. irq->intr_idx = INTR_IDX_PP2_OVFL;
  1616. irq->cb.arg = wb_enc;
  1617. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1618. irq = &phys_enc->irq[INTR_IDX_PP3_OVFL];
  1619. INIT_LIST_HEAD(&irq->cb.list);
  1620. irq->name = "pp3_overflow";
  1621. irq->hw_idx = CWB_3;
  1622. irq->irq_idx = -1;
  1623. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1624. irq->intr_idx = INTR_IDX_PP3_OVFL;
  1625. irq->cb.arg = wb_enc;
  1626. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1627. irq = &phys_enc->irq[INTR_IDX_PP4_OVFL];
  1628. INIT_LIST_HEAD(&irq->cb.list);
  1629. irq->name = "pp4_overflow";
  1630. irq->hw_idx = CWB_4;
  1631. irq->irq_idx = -1;
  1632. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1633. irq->intr_idx = INTR_IDX_PP4_OVFL;
  1634. irq->cb.arg = wb_enc;
  1635. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1636. irq = &phys_enc->irq[INTR_IDX_PP5_OVFL];
  1637. INIT_LIST_HEAD(&irq->cb.list);
  1638. irq->name = "pp5_overflow";
  1639. irq->hw_idx = CWB_5;
  1640. irq->irq_idx = -1;
  1641. irq->intr_type = SDE_IRQ_TYPE_CWB_OVERFLOW;
  1642. irq->intr_idx = INTR_IDX_PP5_OVFL;
  1643. irq->cb.arg = wb_enc;
  1644. irq->cb.func = sde_encoder_phys_cwb_ovflow;
  1645. /* create internal buffer for disable logic */
  1646. if (_sde_encoder_phys_wb_init_internal_fb(wb_enc,
  1647. DRM_FORMAT_RGB888, 2, 1, 6)) {
  1648. SDE_ERROR("failed to init internal fb\n");
  1649. goto fail_wb_init;
  1650. }
  1651. SDE_DEBUG("Created sde_encoder_phys_wb for wb %d\n",
  1652. wb_enc->hw_wb->idx - WB_0);
  1653. return phys_enc;
  1654. fail_wb_init:
  1655. fail_wb_check:
  1656. fail_mdp_init:
  1657. kfree(wb_enc);
  1658. fail_alloc:
  1659. return ERR_PTR(ret);
  1660. }