sde_encoder_phys_wb.c 53 KB

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