sde_encoder_phys_wb.c 81 KB

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