sde_encoder_phys_wb.c 94 KB

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