sde_encoder_phys_wb.c 55 KB

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