sde_encoder_phys_wb.c 52 KB

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