amdgpu_dm.c 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886
  1. /*
  2. * Copyright 2015 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: AMD
  23. *
  24. */
  25. #include "dm_services_types.h"
  26. #include "dc.h"
  27. #include "vid.h"
  28. #include "amdgpu.h"
  29. #include "amdgpu_display.h"
  30. #include "atom.h"
  31. #include "amdgpu_dm.h"
  32. #include "amdgpu_pm.h"
  33. #include "amd_shared.h"
  34. #include "amdgpu_dm_irq.h"
  35. #include "dm_helpers.h"
  36. #include "dm_services_types.h"
  37. #include "amdgpu_dm_mst_types.h"
  38. #include "ivsrcid/ivsrcid_vislands30.h"
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/version.h>
  42. #include <linux/types.h>
  43. #include <drm/drmP.h>
  44. #include <drm/drm_atomic.h>
  45. #include <drm/drm_atomic_helper.h>
  46. #include <drm/drm_dp_mst_helper.h>
  47. #include <drm/drm_fb_helper.h>
  48. #include <drm/drm_edid.h>
  49. #include "modules/inc/mod_freesync.h"
  50. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  51. #include "ivsrcid/irqsrcs_dcn_1_0.h"
  52. #include "raven1/DCN/dcn_1_0_offset.h"
  53. #include "raven1/DCN/dcn_1_0_sh_mask.h"
  54. #include "vega10/soc15ip.h"
  55. #include "soc15_common.h"
  56. #endif
  57. #include "modules/inc/mod_freesync.h"
  58. #include "i2caux_interface.h"
  59. static enum drm_plane_type dm_plane_type_default[AMDGPU_MAX_PLANES] = {
  60. DRM_PLANE_TYPE_PRIMARY,
  61. DRM_PLANE_TYPE_PRIMARY,
  62. DRM_PLANE_TYPE_PRIMARY,
  63. DRM_PLANE_TYPE_PRIMARY,
  64. DRM_PLANE_TYPE_PRIMARY,
  65. DRM_PLANE_TYPE_PRIMARY,
  66. };
  67. static enum drm_plane_type dm_plane_type_carizzo[AMDGPU_MAX_PLANES] = {
  68. DRM_PLANE_TYPE_PRIMARY,
  69. DRM_PLANE_TYPE_PRIMARY,
  70. DRM_PLANE_TYPE_PRIMARY,
  71. DRM_PLANE_TYPE_OVERLAY,/* YUV Capable Underlay */
  72. };
  73. static enum drm_plane_type dm_plane_type_stoney[AMDGPU_MAX_PLANES] = {
  74. DRM_PLANE_TYPE_PRIMARY,
  75. DRM_PLANE_TYPE_PRIMARY,
  76. DRM_PLANE_TYPE_OVERLAY, /* YUV Capable Underlay */
  77. };
  78. /*
  79. * dm_vblank_get_counter
  80. *
  81. * @brief
  82. * Get counter for number of vertical blanks
  83. *
  84. * @param
  85. * struct amdgpu_device *adev - [in] desired amdgpu device
  86. * int disp_idx - [in] which CRTC to get the counter from
  87. *
  88. * @return
  89. * Counter for vertical blanks
  90. */
  91. static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
  92. {
  93. if (crtc >= adev->mode_info.num_crtc)
  94. return 0;
  95. else {
  96. struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
  97. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
  98. acrtc->base.state);
  99. if (acrtc_state->stream == NULL) {
  100. DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
  101. crtc);
  102. return 0;
  103. }
  104. return dc_stream_get_vblank_counter(acrtc_state->stream);
  105. }
  106. }
  107. static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
  108. u32 *vbl, u32 *position)
  109. {
  110. uint32_t v_blank_start, v_blank_end, h_position, v_position;
  111. if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
  112. return -EINVAL;
  113. else {
  114. struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
  115. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
  116. acrtc->base.state);
  117. if (acrtc_state->stream == NULL) {
  118. DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
  119. crtc);
  120. return 0;
  121. }
  122. /*
  123. * TODO rework base driver to use values directly.
  124. * for now parse it back into reg-format
  125. */
  126. dc_stream_get_scanoutpos(acrtc_state->stream,
  127. &v_blank_start,
  128. &v_blank_end,
  129. &h_position,
  130. &v_position);
  131. *position = v_position | (h_position << 16);
  132. *vbl = v_blank_start | (v_blank_end << 16);
  133. }
  134. return 0;
  135. }
  136. static bool dm_is_idle(void *handle)
  137. {
  138. /* XXX todo */
  139. return true;
  140. }
  141. static int dm_wait_for_idle(void *handle)
  142. {
  143. /* XXX todo */
  144. return 0;
  145. }
  146. static bool dm_check_soft_reset(void *handle)
  147. {
  148. return false;
  149. }
  150. static int dm_soft_reset(void *handle)
  151. {
  152. /* XXX todo */
  153. return 0;
  154. }
  155. static struct amdgpu_crtc *get_crtc_by_otg_inst(
  156. struct amdgpu_device *adev,
  157. int otg_inst)
  158. {
  159. struct drm_device *dev = adev->ddev;
  160. struct drm_crtc *crtc;
  161. struct amdgpu_crtc *amdgpu_crtc;
  162. /*
  163. * following if is check inherited from both functions where this one is
  164. * used now. Need to be checked why it could happen.
  165. */
  166. if (otg_inst == -1) {
  167. WARN_ON(1);
  168. return adev->mode_info.crtcs[0];
  169. }
  170. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  171. amdgpu_crtc = to_amdgpu_crtc(crtc);
  172. if (amdgpu_crtc->otg_inst == otg_inst)
  173. return amdgpu_crtc;
  174. }
  175. return NULL;
  176. }
  177. static void dm_pflip_high_irq(void *interrupt_params)
  178. {
  179. struct amdgpu_crtc *amdgpu_crtc;
  180. struct common_irq_params *irq_params = interrupt_params;
  181. struct amdgpu_device *adev = irq_params->adev;
  182. unsigned long flags;
  183. amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
  184. /* IRQ could occur when in initial stage */
  185. /*TODO work and BO cleanup */
  186. if (amdgpu_crtc == NULL) {
  187. DRM_DEBUG_DRIVER("CRTC is null, returning.\n");
  188. return;
  189. }
  190. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  191. if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
  192. DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
  193. amdgpu_crtc->pflip_status,
  194. AMDGPU_FLIP_SUBMITTED,
  195. amdgpu_crtc->crtc_id,
  196. amdgpu_crtc);
  197. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  198. return;
  199. }
  200. /* wakeup usersapce */
  201. if (amdgpu_crtc->event) {
  202. /* Update to correct count/ts if racing with vblank irq */
  203. drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
  204. drm_crtc_send_vblank_event(&amdgpu_crtc->base, amdgpu_crtc->event);
  205. /* page flip completed. clean up */
  206. amdgpu_crtc->event = NULL;
  207. } else
  208. WARN_ON(1);
  209. amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
  210. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  211. DRM_DEBUG_DRIVER("%s - crtc :%d[%p], pflip_stat:AMDGPU_FLIP_NONE\n",
  212. __func__, amdgpu_crtc->crtc_id, amdgpu_crtc);
  213. drm_crtc_vblank_put(&amdgpu_crtc->base);
  214. }
  215. static void dm_crtc_high_irq(void *interrupt_params)
  216. {
  217. struct common_irq_params *irq_params = interrupt_params;
  218. struct amdgpu_device *adev = irq_params->adev;
  219. uint8_t crtc_index = 0;
  220. struct amdgpu_crtc *acrtc;
  221. acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
  222. if (acrtc)
  223. crtc_index = acrtc->crtc_id;
  224. drm_handle_vblank(adev->ddev, crtc_index);
  225. }
  226. static int dm_set_clockgating_state(void *handle,
  227. enum amd_clockgating_state state)
  228. {
  229. return 0;
  230. }
  231. static int dm_set_powergating_state(void *handle,
  232. enum amd_powergating_state state)
  233. {
  234. return 0;
  235. }
  236. /* Prototypes of private functions */
  237. static int dm_early_init(void* handle);
  238. static void hotplug_notify_work_func(struct work_struct *work)
  239. {
  240. struct amdgpu_display_manager *dm = container_of(work, struct amdgpu_display_manager, mst_hotplug_work);
  241. struct drm_device *dev = dm->ddev;
  242. drm_kms_helper_hotplug_event(dev);
  243. }
  244. #ifdef ENABLE_FBC
  245. #include "dal_asic_id.h"
  246. /* Allocate memory for FBC compressed data */
  247. /* TODO: Dynamic allocation */
  248. #define AMDGPU_FBC_SIZE (3840 * 2160 * 4)
  249. void amdgpu_dm_initialize_fbc(struct amdgpu_device *adev)
  250. {
  251. int r;
  252. struct dm_comressor_info *compressor = &adev->dm.compressor;
  253. if (!compressor->bo_ptr) {
  254. r = amdgpu_bo_create_kernel(adev, AMDGPU_FBC_SIZE, PAGE_SIZE,
  255. AMDGPU_GEM_DOMAIN_VRAM, &compressor->bo_ptr,
  256. &compressor->gpu_addr, &compressor->cpu_addr);
  257. if (r)
  258. DRM_ERROR("DM: Failed to initialize fbc\n");
  259. }
  260. }
  261. #endif
  262. /* Init display KMS
  263. *
  264. * Returns 0 on success
  265. */
  266. int amdgpu_dm_init(struct amdgpu_device *adev)
  267. {
  268. struct dc_init_data init_data;
  269. adev->dm.ddev = adev->ddev;
  270. adev->dm.adev = adev;
  271. DRM_INFO("DAL is enabled\n");
  272. /* Zero all the fields */
  273. memset(&init_data, 0, sizeof(init_data));
  274. /* initialize DAL's lock (for SYNC context use) */
  275. spin_lock_init(&adev->dm.dal_lock);
  276. /* initialize DAL's mutex */
  277. mutex_init(&adev->dm.dal_mutex);
  278. if(amdgpu_dm_irq_init(adev)) {
  279. DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
  280. goto error;
  281. }
  282. init_data.asic_id.chip_family = adev->family;
  283. init_data.asic_id.pci_revision_id = adev->rev_id;
  284. init_data.asic_id.hw_internal_rev = adev->external_rev_id;
  285. init_data.asic_id.vram_width = adev->mc.vram_width;
  286. /* TODO: initialize init_data.asic_id.vram_type here!!!! */
  287. init_data.asic_id.atombios_base_address =
  288. adev->mode_info.atom_context->bios;
  289. init_data.driver = adev;
  290. adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
  291. if (!adev->dm.cgs_device) {
  292. DRM_ERROR("amdgpu: failed to create cgs device.\n");
  293. goto error;
  294. }
  295. init_data.cgs_device = adev->dm.cgs_device;
  296. adev->dm.dal = NULL;
  297. init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
  298. #ifdef ENABLE_FBC
  299. if (adev->family == FAMILY_CZ)
  300. amdgpu_dm_initialize_fbc(adev);
  301. init_data.fbc_gpu_addr = adev->dm.compressor.gpu_addr;
  302. #endif
  303. /* Display Core create. */
  304. adev->dm.dc = dc_create(&init_data);
  305. if (!adev->dm.dc)
  306. DRM_INFO("Display Core failed to initialize!\n");
  307. INIT_WORK(&adev->dm.mst_hotplug_work, hotplug_notify_work_func);
  308. adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
  309. if (!adev->dm.freesync_module) {
  310. DRM_ERROR(
  311. "amdgpu: failed to initialize freesync_module.\n");
  312. } else
  313. DRM_INFO("amdgpu: freesync_module init done %p.\n",
  314. adev->dm.freesync_module);
  315. if (amdgpu_dm_initialize_drm_device(adev)) {
  316. DRM_ERROR(
  317. "amdgpu: failed to initialize sw for display support.\n");
  318. goto error;
  319. }
  320. /* Update the actual used number of crtc */
  321. adev->mode_info.num_crtc = adev->dm.display_indexes_num;
  322. /* TODO: Add_display_info? */
  323. /* TODO use dynamic cursor width */
  324. adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
  325. adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
  326. if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
  327. DRM_ERROR(
  328. "amdgpu: failed to initialize sw for display support.\n");
  329. goto error;
  330. }
  331. DRM_INFO("KMS initialized.\n");
  332. return 0;
  333. error:
  334. amdgpu_dm_fini(adev);
  335. return -1;
  336. }
  337. void amdgpu_dm_fini(struct amdgpu_device *adev)
  338. {
  339. amdgpu_dm_destroy_drm_device(&adev->dm);
  340. /*
  341. * TODO: pageflip, vlank interrupt
  342. *
  343. * amdgpu_dm_irq_fini(adev);
  344. */
  345. if (adev->dm.cgs_device) {
  346. amdgpu_cgs_destroy_device(adev->dm.cgs_device);
  347. adev->dm.cgs_device = NULL;
  348. }
  349. if (adev->dm.freesync_module) {
  350. mod_freesync_destroy(adev->dm.freesync_module);
  351. adev->dm.freesync_module = NULL;
  352. }
  353. /* DC Destroy TODO: Replace destroy DAL */
  354. if (adev->dm.dc)
  355. dc_destroy(&adev->dm.dc);
  356. return;
  357. }
  358. /* moved from amdgpu_dm_kms.c */
  359. void amdgpu_dm_destroy()
  360. {
  361. }
  362. static int dm_sw_init(void *handle)
  363. {
  364. return 0;
  365. }
  366. static int dm_sw_fini(void *handle)
  367. {
  368. return 0;
  369. }
  370. static int detect_mst_link_for_all_connectors(struct drm_device *dev)
  371. {
  372. struct amdgpu_connector *aconnector;
  373. struct drm_connector *connector;
  374. int ret = 0;
  375. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  376. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  377. aconnector = to_amdgpu_connector(connector);
  378. if (aconnector->dc_link->type == dc_connection_mst_branch) {
  379. DRM_INFO("DM_MST: starting TM on aconnector: %p [id: %d]\n",
  380. aconnector, aconnector->base.base.id);
  381. ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
  382. if (ret < 0) {
  383. DRM_ERROR("DM_MST: Failed to start MST\n");
  384. ((struct dc_link *)aconnector->dc_link)->type = dc_connection_single;
  385. return ret;
  386. }
  387. }
  388. }
  389. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  390. return ret;
  391. }
  392. static int dm_late_init(void *handle)
  393. {
  394. struct drm_device *dev = ((struct amdgpu_device *)handle)->ddev;
  395. int r = detect_mst_link_for_all_connectors(dev);
  396. return r;
  397. }
  398. static void s3_handle_mst(struct drm_device *dev, bool suspend)
  399. {
  400. struct amdgpu_connector *aconnector;
  401. struct drm_connector *connector;
  402. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  403. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  404. aconnector = to_amdgpu_connector(connector);
  405. if (aconnector->dc_link->type == dc_connection_mst_branch &&
  406. !aconnector->mst_port) {
  407. if (suspend)
  408. drm_dp_mst_topology_mgr_suspend(&aconnector->mst_mgr);
  409. else
  410. drm_dp_mst_topology_mgr_resume(&aconnector->mst_mgr);
  411. }
  412. }
  413. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  414. }
  415. static int dm_hw_init(void *handle)
  416. {
  417. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  418. /* Create DAL display manager */
  419. amdgpu_dm_init(adev);
  420. amdgpu_dm_hpd_init(adev);
  421. return 0;
  422. }
  423. static int dm_hw_fini(void *handle)
  424. {
  425. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  426. amdgpu_dm_hpd_fini(adev);
  427. amdgpu_dm_irq_fini(adev);
  428. amdgpu_dm_fini(adev);
  429. return 0;
  430. }
  431. static int dm_suspend(void *handle)
  432. {
  433. struct amdgpu_device *adev = handle;
  434. struct amdgpu_display_manager *dm = &adev->dm;
  435. int ret = 0;
  436. s3_handle_mst(adev->ddev, true);
  437. amdgpu_dm_irq_suspend(adev);
  438. WARN_ON(adev->dm.cached_state);
  439. adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev);
  440. dc_set_power_state(
  441. dm->dc,
  442. DC_ACPI_CM_POWER_STATE_D3
  443. );
  444. return ret;
  445. }
  446. struct amdgpu_connector *amdgpu_dm_find_first_crct_matching_connector(
  447. struct drm_atomic_state *state,
  448. struct drm_crtc *crtc,
  449. bool from_state_var)
  450. {
  451. uint32_t i;
  452. struct drm_connector_state *conn_state;
  453. struct drm_connector *connector;
  454. struct drm_crtc *crtc_from_state;
  455. for_each_connector_in_state(
  456. state,
  457. connector,
  458. conn_state,
  459. i) {
  460. crtc_from_state =
  461. from_state_var ?
  462. conn_state->crtc :
  463. connector->state->crtc;
  464. if (crtc_from_state == crtc)
  465. return to_amdgpu_connector(connector);
  466. }
  467. return NULL;
  468. }
  469. static int dm_resume(void *handle)
  470. {
  471. struct amdgpu_device *adev = handle;
  472. struct amdgpu_display_manager *dm = &adev->dm;
  473. /* power on hardware */
  474. dc_set_power_state(
  475. dm->dc,
  476. DC_ACPI_CM_POWER_STATE_D0
  477. );
  478. return 0;
  479. }
  480. int amdgpu_dm_display_resume(struct amdgpu_device *adev )
  481. {
  482. struct drm_device *ddev = adev->ddev;
  483. struct amdgpu_display_manager *dm = &adev->dm;
  484. struct amdgpu_connector *aconnector;
  485. struct drm_connector *connector;
  486. struct drm_crtc *crtc;
  487. struct drm_crtc_state *crtc_state;
  488. int ret = 0;
  489. int i;
  490. /* program HPD filter */
  491. dc_resume(dm->dc);
  492. /* On resume we need to rewrite the MSTM control bits to enamble MST*/
  493. s3_handle_mst(ddev, false);
  494. /*
  495. * early enable HPD Rx IRQ, should be done before set mode as short
  496. * pulse interrupts are used for MST
  497. */
  498. amdgpu_dm_irq_resume_early(adev);
  499. /* Do detection*/
  500. list_for_each_entry(connector,
  501. &ddev->mode_config.connector_list, head) {
  502. aconnector = to_amdgpu_connector(connector);
  503. /*
  504. * this is the case when traversing through already created
  505. * MST connectors, should be skipped
  506. */
  507. if (aconnector->mst_port)
  508. continue;
  509. mutex_lock(&aconnector->hpd_lock);
  510. dc_link_detect(aconnector->dc_link, false);
  511. aconnector->dc_sink = NULL;
  512. amdgpu_dm_update_connector_after_detect(aconnector);
  513. mutex_unlock(&aconnector->hpd_lock);
  514. }
  515. /* Force mode set in atomic comit */
  516. for_each_crtc_in_state(adev->dm.cached_state, crtc, crtc_state, i)
  517. crtc_state->active_changed = true;
  518. ret = drm_atomic_helper_resume(ddev, adev->dm.cached_state);
  519. drm_atomic_state_put(adev->dm.cached_state);
  520. adev->dm.cached_state = NULL;
  521. amdgpu_dm_irq_resume_late(adev);
  522. return ret;
  523. }
  524. static const struct amd_ip_funcs amdgpu_dm_funcs = {
  525. .name = "dm",
  526. .early_init = dm_early_init,
  527. .late_init = dm_late_init,
  528. .sw_init = dm_sw_init,
  529. .sw_fini = dm_sw_fini,
  530. .hw_init = dm_hw_init,
  531. .hw_fini = dm_hw_fini,
  532. .suspend = dm_suspend,
  533. .resume = dm_resume,
  534. .is_idle = dm_is_idle,
  535. .wait_for_idle = dm_wait_for_idle,
  536. .check_soft_reset = dm_check_soft_reset,
  537. .soft_reset = dm_soft_reset,
  538. .set_clockgating_state = dm_set_clockgating_state,
  539. .set_powergating_state = dm_set_powergating_state,
  540. };
  541. const struct amdgpu_ip_block_version dm_ip_block =
  542. {
  543. .type = AMD_IP_BLOCK_TYPE_DCE,
  544. .major = 1,
  545. .minor = 0,
  546. .rev = 0,
  547. .funcs = &amdgpu_dm_funcs,
  548. };
  549. struct drm_atomic_state *
  550. dm_atomic_state_alloc(struct drm_device *dev)
  551. {
  552. struct dm_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
  553. if (!state || drm_atomic_state_init(dev, &state->base) < 0) {
  554. kfree(state);
  555. return NULL;
  556. }
  557. return &state->base;
  558. }
  559. static void
  560. dm_atomic_state_clear(struct drm_atomic_state *state)
  561. {
  562. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  563. if (dm_state->context) {
  564. dc_release_validate_context(dm_state->context);
  565. dm_state->context = NULL;
  566. }
  567. drm_atomic_state_default_clear(state);
  568. }
  569. static void
  570. dm_atomic_state_alloc_free(struct drm_atomic_state *state)
  571. {
  572. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  573. drm_atomic_state_default_release(state);
  574. kfree(dm_state);
  575. }
  576. static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
  577. .fb_create = amdgpu_user_framebuffer_create,
  578. .output_poll_changed = amdgpu_output_poll_changed,
  579. .atomic_check = amdgpu_dm_atomic_check,
  580. .atomic_commit = amdgpu_dm_atomic_commit,
  581. .atomic_state_alloc = dm_atomic_state_alloc,
  582. .atomic_state_clear = dm_atomic_state_clear,
  583. .atomic_state_free = dm_atomic_state_alloc_free
  584. };
  585. static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
  586. .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
  587. };
  588. void amdgpu_dm_update_connector_after_detect(
  589. struct amdgpu_connector *aconnector)
  590. {
  591. struct drm_connector *connector = &aconnector->base;
  592. struct drm_device *dev = connector->dev;
  593. struct dc_sink *sink;
  594. /* MST handled by drm_mst framework */
  595. if (aconnector->mst_mgr.mst_state == true)
  596. return;
  597. sink = aconnector->dc_link->local_sink;
  598. /* Edid mgmt connector gets first update only in mode_valid hook and then
  599. * the connector sink is set to either fake or physical sink depends on link status.
  600. * don't do it here if u are during boot
  601. */
  602. if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
  603. && aconnector->dc_em_sink) {
  604. /* For S3 resume with headless use eml_sink to fake stream
  605. * because on resume connecotr->sink is set ti NULL
  606. */
  607. mutex_lock(&dev->mode_config.mutex);
  608. if (sink) {
  609. if (aconnector->dc_sink) {
  610. amdgpu_dm_remove_sink_from_freesync_module(
  611. connector);
  612. /* retain and release bellow are used for
  613. * bump up refcount for sink because the link don't point
  614. * to it anymore after disconnect so on next crtc to connector
  615. * reshuffle by UMD we will get into unwanted dc_sink release
  616. */
  617. if (aconnector->dc_sink != aconnector->dc_em_sink)
  618. dc_sink_release(aconnector->dc_sink);
  619. }
  620. aconnector->dc_sink = sink;
  621. amdgpu_dm_add_sink_to_freesync_module(
  622. connector, aconnector->edid);
  623. } else {
  624. amdgpu_dm_remove_sink_from_freesync_module(connector);
  625. if (!aconnector->dc_sink)
  626. aconnector->dc_sink = aconnector->dc_em_sink;
  627. else if (aconnector->dc_sink != aconnector->dc_em_sink)
  628. dc_sink_retain(aconnector->dc_sink);
  629. }
  630. mutex_unlock(&dev->mode_config.mutex);
  631. return;
  632. }
  633. /*
  634. * TODO: temporary guard to look for proper fix
  635. * if this sink is MST sink, we should not do anything
  636. */
  637. if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
  638. return;
  639. if (aconnector->dc_sink == sink) {
  640. /* We got a DP short pulse (Link Loss, DP CTS, etc...).
  641. * Do nothing!! */
  642. DRM_INFO("DCHPD: connector_id=%d: dc_sink didn't change.\n",
  643. aconnector->connector_id);
  644. return;
  645. }
  646. DRM_INFO("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
  647. aconnector->connector_id, aconnector->dc_sink, sink);
  648. mutex_lock(&dev->mode_config.mutex);
  649. /* 1. Update status of the drm connector
  650. * 2. Send an event and let userspace tell us what to do */
  651. if (sink) {
  652. /* TODO: check if we still need the S3 mode update workaround.
  653. * If yes, put it here. */
  654. if (aconnector->dc_sink)
  655. amdgpu_dm_remove_sink_from_freesync_module(
  656. connector);
  657. aconnector->dc_sink = sink;
  658. if (sink->dc_edid.length == 0)
  659. aconnector->edid = NULL;
  660. else {
  661. aconnector->edid =
  662. (struct edid *) sink->dc_edid.raw_edid;
  663. drm_mode_connector_update_edid_property(connector,
  664. aconnector->edid);
  665. }
  666. amdgpu_dm_add_sink_to_freesync_module(connector, aconnector->edid);
  667. } else {
  668. amdgpu_dm_remove_sink_from_freesync_module(connector);
  669. drm_mode_connector_update_edid_property(connector, NULL);
  670. aconnector->num_modes = 0;
  671. aconnector->dc_sink = NULL;
  672. }
  673. mutex_unlock(&dev->mode_config.mutex);
  674. }
  675. static void handle_hpd_irq(void *param)
  676. {
  677. struct amdgpu_connector *aconnector = (struct amdgpu_connector *)param;
  678. struct drm_connector *connector = &aconnector->base;
  679. struct drm_device *dev = connector->dev;
  680. /* In case of failure or MST no need to update connector status or notify the OS
  681. * since (for MST case) MST does this in it's own context.
  682. */
  683. mutex_lock(&aconnector->hpd_lock);
  684. if (dc_link_detect(aconnector->dc_link, false)) {
  685. amdgpu_dm_update_connector_after_detect(aconnector);
  686. drm_modeset_lock_all(dev);
  687. dm_restore_drm_connector_state(dev, connector);
  688. drm_modeset_unlock_all(dev);
  689. if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
  690. drm_kms_helper_hotplug_event(dev);
  691. }
  692. mutex_unlock(&aconnector->hpd_lock);
  693. }
  694. static void dm_handle_hpd_rx_irq(struct amdgpu_connector *aconnector)
  695. {
  696. uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
  697. uint8_t dret;
  698. bool new_irq_handled = false;
  699. int dpcd_addr;
  700. int dpcd_bytes_to_read;
  701. const int max_process_count = 30;
  702. int process_count = 0;
  703. const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
  704. if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
  705. dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
  706. /* DPCD 0x200 - 0x201 for downstream IRQ */
  707. dpcd_addr = DP_SINK_COUNT;
  708. } else {
  709. dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
  710. /* DPCD 0x2002 - 0x2005 for downstream IRQ */
  711. dpcd_addr = DP_SINK_COUNT_ESI;
  712. }
  713. dret = drm_dp_dpcd_read(
  714. &aconnector->dm_dp_aux.aux,
  715. dpcd_addr,
  716. esi,
  717. dpcd_bytes_to_read);
  718. while (dret == dpcd_bytes_to_read &&
  719. process_count < max_process_count) {
  720. uint8_t retry;
  721. dret = 0;
  722. process_count++;
  723. DRM_DEBUG_KMS("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
  724. /* handle HPD short pulse irq */
  725. if (aconnector->mst_mgr.mst_state)
  726. drm_dp_mst_hpd_irq(
  727. &aconnector->mst_mgr,
  728. esi,
  729. &new_irq_handled);
  730. if (new_irq_handled) {
  731. /* ACK at DPCD to notify down stream */
  732. const int ack_dpcd_bytes_to_write =
  733. dpcd_bytes_to_read - 1;
  734. for (retry = 0; retry < 3; retry++) {
  735. uint8_t wret;
  736. wret = drm_dp_dpcd_write(
  737. &aconnector->dm_dp_aux.aux,
  738. dpcd_addr + 1,
  739. &esi[1],
  740. ack_dpcd_bytes_to_write);
  741. if (wret == ack_dpcd_bytes_to_write)
  742. break;
  743. }
  744. /* check if there is new irq to be handle */
  745. dret = drm_dp_dpcd_read(
  746. &aconnector->dm_dp_aux.aux,
  747. dpcd_addr,
  748. esi,
  749. dpcd_bytes_to_read);
  750. new_irq_handled = false;
  751. } else
  752. break;
  753. }
  754. if (process_count == max_process_count)
  755. DRM_DEBUG_KMS("Loop exceeded max iterations\n");
  756. }
  757. static void handle_hpd_rx_irq(void *param)
  758. {
  759. struct amdgpu_connector *aconnector = (struct amdgpu_connector *)param;
  760. struct drm_connector *connector = &aconnector->base;
  761. struct drm_device *dev = connector->dev;
  762. const struct dc_link *dc_link = aconnector->dc_link;
  763. bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
  764. /* TODO:Temporary add mutex to protect hpd interrupt not have a gpio
  765. * conflict, after implement i2c helper, this mutex should be
  766. * retired.
  767. */
  768. if (aconnector->dc_link->type != dc_connection_mst_branch)
  769. mutex_lock(&aconnector->hpd_lock);
  770. if (dc_link_handle_hpd_rx_irq(aconnector->dc_link, NULL) &&
  771. !is_mst_root_connector) {
  772. /* Downstream Port status changed. */
  773. if (dc_link_detect(aconnector->dc_link, false)) {
  774. amdgpu_dm_update_connector_after_detect(aconnector);
  775. drm_modeset_lock_all(dev);
  776. dm_restore_drm_connector_state(dev, connector);
  777. drm_modeset_unlock_all(dev);
  778. drm_kms_helper_hotplug_event(dev);
  779. }
  780. }
  781. if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
  782. (dc_link->type == dc_connection_mst_branch))
  783. dm_handle_hpd_rx_irq(aconnector);
  784. if (aconnector->dc_link->type != dc_connection_mst_branch)
  785. mutex_unlock(&aconnector->hpd_lock);
  786. }
  787. static void register_hpd_handlers(struct amdgpu_device *adev)
  788. {
  789. struct drm_device *dev = adev->ddev;
  790. struct drm_connector *connector;
  791. struct amdgpu_connector *aconnector;
  792. const struct dc_link *dc_link;
  793. struct dc_interrupt_params int_params = {0};
  794. int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
  795. int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
  796. list_for_each_entry(connector,
  797. &dev->mode_config.connector_list, head) {
  798. aconnector = to_amdgpu_connector(connector);
  799. dc_link = aconnector->dc_link;
  800. if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
  801. int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
  802. int_params.irq_source = dc_link->irq_source_hpd;
  803. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  804. handle_hpd_irq,
  805. (void *) aconnector);
  806. }
  807. if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
  808. /* Also register for DP short pulse (hpd_rx). */
  809. int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
  810. int_params.irq_source = dc_link->irq_source_hpd_rx;
  811. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  812. handle_hpd_rx_irq,
  813. (void *) aconnector);
  814. }
  815. }
  816. }
  817. /* Register IRQ sources and initialize IRQ callbacks */
  818. static int dce110_register_irq_handlers(struct amdgpu_device *adev)
  819. {
  820. struct dc *dc = adev->dm.dc;
  821. struct common_irq_params *c_irq_params;
  822. struct dc_interrupt_params int_params = {0};
  823. int r;
  824. int i;
  825. unsigned client_id = AMDGPU_IH_CLIENTID_LEGACY;
  826. if (adev->asic_type == CHIP_VEGA10 ||
  827. adev->asic_type == CHIP_RAVEN)
  828. client_id = AMDGPU_IH_CLIENTID_DCE;
  829. int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
  830. int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
  831. /* Actions of amdgpu_irq_add_id():
  832. * 1. Register a set() function with base driver.
  833. * Base driver will call set() function to enable/disable an
  834. * interrupt in DC hardware.
  835. * 2. Register amdgpu_dm_irq_handler().
  836. * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
  837. * coming from DC hardware.
  838. * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
  839. * for acknowledging and handling. */
  840. /* Use VBLANK interrupt */
  841. for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
  842. r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
  843. if (r) {
  844. DRM_ERROR("Failed to add crtc irq id!\n");
  845. return r;
  846. }
  847. int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
  848. int_params.irq_source =
  849. dc_interrupt_to_irq_source(dc, i, 0);
  850. c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
  851. c_irq_params->adev = adev;
  852. c_irq_params->irq_src = int_params.irq_source;
  853. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  854. dm_crtc_high_irq, c_irq_params);
  855. }
  856. /* Use GRPH_PFLIP interrupt */
  857. for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
  858. i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
  859. r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
  860. if (r) {
  861. DRM_ERROR("Failed to add page flip irq id!\n");
  862. return r;
  863. }
  864. int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
  865. int_params.irq_source =
  866. dc_interrupt_to_irq_source(dc, i, 0);
  867. c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
  868. c_irq_params->adev = adev;
  869. c_irq_params->irq_src = int_params.irq_source;
  870. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  871. dm_pflip_high_irq, c_irq_params);
  872. }
  873. /* HPD */
  874. r = amdgpu_irq_add_id(adev, client_id,
  875. VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
  876. if (r) {
  877. DRM_ERROR("Failed to add hpd irq id!\n");
  878. return r;
  879. }
  880. register_hpd_handlers(adev);
  881. return 0;
  882. }
  883. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  884. /* Register IRQ sources and initialize IRQ callbacks */
  885. static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
  886. {
  887. struct dc *dc = adev->dm.dc;
  888. struct common_irq_params *c_irq_params;
  889. struct dc_interrupt_params int_params = {0};
  890. int r;
  891. int i;
  892. int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
  893. int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
  894. /* Actions of amdgpu_irq_add_id():
  895. * 1. Register a set() function with base driver.
  896. * Base driver will call set() function to enable/disable an
  897. * interrupt in DC hardware.
  898. * 2. Register amdgpu_dm_irq_handler().
  899. * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
  900. * coming from DC hardware.
  901. * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
  902. * for acknowledging and handling.
  903. * */
  904. /* Use VSTARTUP interrupt */
  905. for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
  906. i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
  907. i++) {
  908. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_DCE, i, &adev->crtc_irq);
  909. if (r) {
  910. DRM_ERROR("Failed to add crtc irq id!\n");
  911. return r;
  912. }
  913. int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
  914. int_params.irq_source =
  915. dc_interrupt_to_irq_source(dc, i, 0);
  916. c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
  917. c_irq_params->adev = adev;
  918. c_irq_params->irq_src = int_params.irq_source;
  919. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  920. dm_crtc_high_irq, c_irq_params);
  921. }
  922. /* Use GRPH_PFLIP interrupt */
  923. for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
  924. i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1;
  925. i++) {
  926. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
  927. if (r) {
  928. DRM_ERROR("Failed to add page flip irq id!\n");
  929. return r;
  930. }
  931. int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
  932. int_params.irq_source =
  933. dc_interrupt_to_irq_source(dc, i, 0);
  934. c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
  935. c_irq_params->adev = adev;
  936. c_irq_params->irq_src = int_params.irq_source;
  937. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  938. dm_pflip_high_irq, c_irq_params);
  939. }
  940. /* HPD */
  941. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
  942. &adev->hpd_irq);
  943. if (r) {
  944. DRM_ERROR("Failed to add hpd irq id!\n");
  945. return r;
  946. }
  947. register_hpd_handlers(adev);
  948. return 0;
  949. }
  950. #endif
  951. static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
  952. {
  953. int r;
  954. adev->mode_info.mode_config_initialized = true;
  955. adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
  956. adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
  957. adev->ddev->mode_config.max_width = 16384;
  958. adev->ddev->mode_config.max_height = 16384;
  959. adev->ddev->mode_config.preferred_depth = 24;
  960. adev->ddev->mode_config.prefer_shadow = 1;
  961. /* indicate support of immediate flip */
  962. adev->ddev->mode_config.async_page_flip = true;
  963. adev->ddev->mode_config.fb_base = adev->mc.aper_base;
  964. r = amdgpu_modeset_create_props(adev);
  965. if (r)
  966. return r;
  967. return 0;
  968. }
  969. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
  970. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  971. static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
  972. {
  973. struct amdgpu_display_manager *dm = bl_get_data(bd);
  974. if (dc_link_set_backlight_level(dm->backlight_link,
  975. bd->props.brightness, 0, 0))
  976. return 0;
  977. else
  978. return 1;
  979. }
  980. static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
  981. {
  982. return bd->props.brightness;
  983. }
  984. static const struct backlight_ops amdgpu_dm_backlight_ops = {
  985. .get_brightness = amdgpu_dm_backlight_get_brightness,
  986. .update_status = amdgpu_dm_backlight_update_status,
  987. };
  988. void amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
  989. {
  990. char bl_name[16];
  991. struct backlight_properties props = { 0 };
  992. props.max_brightness = AMDGPU_MAX_BL_LEVEL;
  993. props.type = BACKLIGHT_RAW;
  994. snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
  995. dm->adev->ddev->primary->index);
  996. dm->backlight_dev = backlight_device_register(bl_name,
  997. dm->adev->ddev->dev,
  998. dm,
  999. &amdgpu_dm_backlight_ops,
  1000. &props);
  1001. if (NULL == dm->backlight_dev)
  1002. DRM_ERROR("DM: Backlight registration failed!\n");
  1003. else
  1004. DRM_INFO("DM: Registered Backlight device: %s\n", bl_name);
  1005. }
  1006. #endif
  1007. /* In this architecture, the association
  1008. * connector -> encoder -> crtc
  1009. * id not really requried. The crtc and connector will hold the
  1010. * display_index as an abstraction to use with DAL component
  1011. *
  1012. * Returns 0 on success
  1013. */
  1014. int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
  1015. {
  1016. struct amdgpu_display_manager *dm = &adev->dm;
  1017. uint32_t i;
  1018. struct amdgpu_connector *aconnector = NULL;
  1019. struct amdgpu_encoder *aencoder = NULL;
  1020. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  1021. uint32_t link_cnt;
  1022. unsigned long possible_crtcs;
  1023. link_cnt = dm->dc->caps.max_links;
  1024. if (amdgpu_dm_mode_config_init(dm->adev)) {
  1025. DRM_ERROR("DM: Failed to initialize mode config\n");
  1026. return -1;
  1027. }
  1028. for (i = 0; i < dm->dc->caps.max_planes; i++) {
  1029. mode_info->planes[i] = kzalloc(sizeof(struct amdgpu_plane),
  1030. GFP_KERNEL);
  1031. if (!mode_info->planes[i]) {
  1032. DRM_ERROR("KMS: Failed to allocate plane\n");
  1033. goto fail_free_planes;
  1034. }
  1035. mode_info->planes[i]->base.type = mode_info->plane_type[i];
  1036. /*
  1037. * HACK: IGT tests expect that each plane can only have one
  1038. * one possible CRTC. For now, set one CRTC for each
  1039. * plane that is not an underlay, but still allow multiple
  1040. * CRTCs for underlay planes.
  1041. */
  1042. possible_crtcs = 1 << i;
  1043. if (i >= dm->dc->caps.max_streams)
  1044. possible_crtcs = 0xff;
  1045. if (amdgpu_dm_plane_init(dm, mode_info->planes[i], possible_crtcs)) {
  1046. DRM_ERROR("KMS: Failed to initialize plane\n");
  1047. goto fail_free_planes;
  1048. }
  1049. }
  1050. for (i = 0; i < dm->dc->caps.max_streams; i++)
  1051. if (amdgpu_dm_crtc_init(dm, &mode_info->planes[i]->base, i)) {
  1052. DRM_ERROR("KMS: Failed to initialize crtc\n");
  1053. goto fail_free_planes;
  1054. }
  1055. dm->display_indexes_num = dm->dc->caps.max_streams;
  1056. /* loops over all connectors on the board */
  1057. for (i = 0; i < link_cnt; i++) {
  1058. if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
  1059. DRM_ERROR(
  1060. "KMS: Cannot support more than %d display indexes\n",
  1061. AMDGPU_DM_MAX_DISPLAY_INDEX);
  1062. continue;
  1063. }
  1064. aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
  1065. if (!aconnector)
  1066. goto fail_free_planes;
  1067. aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
  1068. if (!aencoder) {
  1069. goto fail_free_connector;
  1070. }
  1071. if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
  1072. DRM_ERROR("KMS: Failed to initialize encoder\n");
  1073. goto fail_free_encoder;
  1074. }
  1075. if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
  1076. DRM_ERROR("KMS: Failed to initialize connector\n");
  1077. goto fail_free_encoder;
  1078. }
  1079. if (dc_link_detect(dc_get_link_at_index(dm->dc, i), true))
  1080. amdgpu_dm_update_connector_after_detect(aconnector);
  1081. }
  1082. /* Software is initialized. Now we can register interrupt handlers. */
  1083. switch (adev->asic_type) {
  1084. case CHIP_BONAIRE:
  1085. case CHIP_HAWAII:
  1086. case CHIP_TONGA:
  1087. case CHIP_FIJI:
  1088. case CHIP_CARRIZO:
  1089. case CHIP_STONEY:
  1090. case CHIP_POLARIS11:
  1091. case CHIP_POLARIS10:
  1092. case CHIP_POLARIS12:
  1093. case CHIP_VEGA10:
  1094. if (dce110_register_irq_handlers(dm->adev)) {
  1095. DRM_ERROR("DM: Failed to initialize IRQ\n");
  1096. goto fail_free_encoder;
  1097. }
  1098. break;
  1099. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  1100. case CHIP_RAVEN:
  1101. if (dcn10_register_irq_handlers(dm->adev)) {
  1102. DRM_ERROR("DM: Failed to initialize IRQ\n");
  1103. goto fail_free_encoder;
  1104. }
  1105. break;
  1106. #endif
  1107. default:
  1108. DRM_ERROR("Usupported ASIC type: 0x%X\n", adev->asic_type);
  1109. goto fail_free_encoder;
  1110. }
  1111. drm_mode_config_reset(dm->ddev);
  1112. return 0;
  1113. fail_free_encoder:
  1114. kfree(aencoder);
  1115. fail_free_connector:
  1116. kfree(aconnector);
  1117. fail_free_planes:
  1118. for (i = 0; i < dm->dc->caps.max_planes; i++)
  1119. kfree(mode_info->planes[i]);
  1120. return -1;
  1121. }
  1122. void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
  1123. {
  1124. drm_mode_config_cleanup(dm->ddev);
  1125. return;
  1126. }
  1127. /******************************************************************************
  1128. * amdgpu_display_funcs functions
  1129. *****************************************************************************/
  1130. /**
  1131. * dm_bandwidth_update - program display watermarks
  1132. *
  1133. * @adev: amdgpu_device pointer
  1134. *
  1135. * Calculate and program the display watermarks and line buffer allocation.
  1136. */
  1137. static void dm_bandwidth_update(struct amdgpu_device *adev)
  1138. {
  1139. /* TODO: implement later */
  1140. }
  1141. static void dm_set_backlight_level(struct amdgpu_encoder *amdgpu_encoder,
  1142. u8 level)
  1143. {
  1144. /* TODO: translate amdgpu_encoder to display_index and call DAL */
  1145. }
  1146. static u8 dm_get_backlight_level(struct amdgpu_encoder *amdgpu_encoder)
  1147. {
  1148. /* TODO: translate amdgpu_encoder to display_index and call DAL */
  1149. return 0;
  1150. }
  1151. static int amdgpu_notify_freesync(struct drm_device *dev, void *data,
  1152. struct drm_file *filp)
  1153. {
  1154. struct mod_freesync_params freesync_params;
  1155. uint8_t num_streams;
  1156. uint8_t i;
  1157. struct amdgpu_device *adev = dev->dev_private;
  1158. int r = 0;
  1159. /* Get freesync enable flag from DRM */
  1160. num_streams = dc_get_current_stream_count(adev->dm.dc);
  1161. for (i = 0; i < num_streams; i++) {
  1162. struct dc_stream_state *stream;
  1163. stream = dc_get_stream_at_index(adev->dm.dc, i);
  1164. mod_freesync_update_state(adev->dm.freesync_module,
  1165. &stream, 1, &freesync_params);
  1166. }
  1167. return r;
  1168. }
  1169. static const struct amdgpu_display_funcs dm_display_funcs = {
  1170. .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
  1171. .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
  1172. .vblank_wait = NULL,
  1173. .backlight_set_level =
  1174. dm_set_backlight_level,/* called unconditionally */
  1175. .backlight_get_level =
  1176. dm_get_backlight_level,/* called unconditionally */
  1177. .hpd_sense = NULL,/* called unconditionally */
  1178. .hpd_set_polarity = NULL, /* called unconditionally */
  1179. .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
  1180. .page_flip_get_scanoutpos =
  1181. dm_crtc_get_scanoutpos,/* called unconditionally */
  1182. .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
  1183. .add_connector = NULL, /* VBIOS parsing. DAL does it. */
  1184. .notify_freesync = amdgpu_notify_freesync,
  1185. };
  1186. #if defined(CONFIG_DEBUG_KERNEL_DC)
  1187. static ssize_t s3_debug_store(
  1188. struct device *device,
  1189. struct device_attribute *attr,
  1190. const char *buf,
  1191. size_t count)
  1192. {
  1193. int ret;
  1194. int s3_state;
  1195. struct pci_dev *pdev = to_pci_dev(device);
  1196. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  1197. struct amdgpu_device *adev = drm_dev->dev_private;
  1198. ret = kstrtoint(buf, 0, &s3_state);
  1199. if (ret == 0) {
  1200. if (s3_state) {
  1201. dm_resume(adev);
  1202. amdgpu_dm_display_resume(adev);
  1203. drm_kms_helper_hotplug_event(adev->ddev);
  1204. } else
  1205. dm_suspend(adev);
  1206. }
  1207. return ret == 0 ? count : 0;
  1208. }
  1209. DEVICE_ATTR_WO(s3_debug);
  1210. #endif
  1211. static int dm_early_init(void *handle)
  1212. {
  1213. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  1214. adev->ddev->driver->driver_features |= DRIVER_ATOMIC;
  1215. amdgpu_dm_set_irq_funcs(adev);
  1216. switch (adev->asic_type) {
  1217. case CHIP_BONAIRE:
  1218. case CHIP_HAWAII:
  1219. adev->mode_info.num_crtc = 6;
  1220. adev->mode_info.num_hpd = 6;
  1221. adev->mode_info.num_dig = 6;
  1222. adev->mode_info.plane_type = dm_plane_type_default;
  1223. break;
  1224. case CHIP_FIJI:
  1225. case CHIP_TONGA:
  1226. adev->mode_info.num_crtc = 6;
  1227. adev->mode_info.num_hpd = 6;
  1228. adev->mode_info.num_dig = 7;
  1229. adev->mode_info.plane_type = dm_plane_type_default;
  1230. break;
  1231. case CHIP_CARRIZO:
  1232. adev->mode_info.num_crtc = 3;
  1233. adev->mode_info.num_hpd = 6;
  1234. adev->mode_info.num_dig = 9;
  1235. adev->mode_info.plane_type = dm_plane_type_carizzo;
  1236. break;
  1237. case CHIP_STONEY:
  1238. adev->mode_info.num_crtc = 2;
  1239. adev->mode_info.num_hpd = 6;
  1240. adev->mode_info.num_dig = 9;
  1241. adev->mode_info.plane_type = dm_plane_type_stoney;
  1242. break;
  1243. case CHIP_POLARIS11:
  1244. case CHIP_POLARIS12:
  1245. adev->mode_info.num_crtc = 5;
  1246. adev->mode_info.num_hpd = 5;
  1247. adev->mode_info.num_dig = 5;
  1248. adev->mode_info.plane_type = dm_plane_type_default;
  1249. break;
  1250. case CHIP_POLARIS10:
  1251. adev->mode_info.num_crtc = 6;
  1252. adev->mode_info.num_hpd = 6;
  1253. adev->mode_info.num_dig = 6;
  1254. adev->mode_info.plane_type = dm_plane_type_default;
  1255. break;
  1256. case CHIP_VEGA10:
  1257. adev->mode_info.num_crtc = 6;
  1258. adev->mode_info.num_hpd = 6;
  1259. adev->mode_info.num_dig = 6;
  1260. adev->mode_info.plane_type = dm_plane_type_default;
  1261. break;
  1262. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  1263. case CHIP_RAVEN:
  1264. adev->mode_info.num_crtc = 4;
  1265. adev->mode_info.num_hpd = 4;
  1266. adev->mode_info.num_dig = 4;
  1267. adev->mode_info.plane_type = dm_plane_type_default;
  1268. break;
  1269. #endif
  1270. default:
  1271. DRM_ERROR("Usupported ASIC type: 0x%X\n", adev->asic_type);
  1272. return -EINVAL;
  1273. }
  1274. if (adev->mode_info.funcs == NULL)
  1275. adev->mode_info.funcs = &dm_display_funcs;
  1276. /* Note: Do NOT change adev->audio_endpt_rreg and
  1277. * adev->audio_endpt_wreg because they are initialised in
  1278. * amdgpu_device_init() */
  1279. #if defined(CONFIG_DEBUG_KERNEL_DC)
  1280. device_create_file(
  1281. adev->ddev->dev,
  1282. &dev_attr_s3_debug);
  1283. #endif
  1284. return 0;
  1285. }
  1286. bool amdgpu_dm_acquire_dal_lock(struct amdgpu_display_manager *dm)
  1287. {
  1288. /* TODO */
  1289. return true;
  1290. }
  1291. bool amdgpu_dm_release_dal_lock(struct amdgpu_display_manager *dm)
  1292. {
  1293. /* TODO */ return true;
  1294. }
  1295. struct dm_connector_state {
  1296. struct drm_connector_state base;
  1297. enum amdgpu_rmx_type scaling;
  1298. uint8_t underscan_vborder;
  1299. uint8_t underscan_hborder;
  1300. bool underscan_enable;
  1301. };
  1302. #define to_dm_connector_state(x)\
  1303. container_of((x), struct dm_connector_state, base)
  1304. static bool modeset_required(struct drm_crtc_state *crtc_state,
  1305. struct dc_stream_state *new_stream,
  1306. struct dc_stream_state *old_stream)
  1307. {
  1308. if (dc_is_stream_unchanged(new_stream, old_stream)) {
  1309. crtc_state->mode_changed = false;
  1310. DRM_DEBUG_KMS("Mode change not required, setting mode_changed to %d",
  1311. crtc_state->mode_changed);
  1312. }
  1313. if (!drm_atomic_crtc_needs_modeset(crtc_state))
  1314. return false;
  1315. if (!crtc_state->enable)
  1316. return false;
  1317. return crtc_state->active;
  1318. }
  1319. static bool modereset_required(struct drm_crtc_state *crtc_state)
  1320. {
  1321. if (!drm_atomic_crtc_needs_modeset(crtc_state))
  1322. return false;
  1323. return !crtc_state->enable || !crtc_state->active;
  1324. }
  1325. void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
  1326. {
  1327. drm_encoder_cleanup(encoder);
  1328. kfree(encoder);
  1329. }
  1330. static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
  1331. .destroy = amdgpu_dm_encoder_destroy,
  1332. };
  1333. static void dm_set_cursor(
  1334. struct amdgpu_crtc *amdgpu_crtc,
  1335. uint64_t gpu_addr,
  1336. uint32_t width,
  1337. uint32_t height)
  1338. {
  1339. struct dc_cursor_attributes attributes;
  1340. struct dc_cursor_position position;
  1341. struct drm_crtc *crtc = &amdgpu_crtc->base;
  1342. int x, y;
  1343. int xorigin = 0, yorigin = 0;
  1344. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
  1345. amdgpu_crtc->cursor_width = width;
  1346. amdgpu_crtc->cursor_height = height;
  1347. attributes.address.high_part = upper_32_bits(gpu_addr);
  1348. attributes.address.low_part = lower_32_bits(gpu_addr);
  1349. attributes.width = width;
  1350. attributes.height = height;
  1351. attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
  1352. attributes.rotation_angle = 0;
  1353. attributes.attribute_flags.value = 0;
  1354. attributes.pitch = attributes.width;
  1355. x = amdgpu_crtc->cursor_x;
  1356. y = amdgpu_crtc->cursor_y;
  1357. /* avivo cursor are offset into the total surface */
  1358. x += crtc->primary->state->src_x >> 16;
  1359. y += crtc->primary->state->src_y >> 16;
  1360. if (x < 0) {
  1361. xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
  1362. x = 0;
  1363. }
  1364. if (y < 0) {
  1365. yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
  1366. y = 0;
  1367. }
  1368. position.enable = true;
  1369. position.x = x;
  1370. position.y = y;
  1371. position.x_hotspot = xorigin;
  1372. position.y_hotspot = yorigin;
  1373. if (!dc_stream_set_cursor_attributes(
  1374. acrtc_state->stream,
  1375. &attributes)) {
  1376. DRM_ERROR("DC failed to set cursor attributes\n");
  1377. }
  1378. if (!dc_stream_set_cursor_position(
  1379. acrtc_state->stream,
  1380. &position)) {
  1381. DRM_ERROR("DC failed to set cursor position\n");
  1382. }
  1383. }
  1384. static int dm_crtc_cursor_set(
  1385. struct drm_crtc *crtc,
  1386. uint64_t address,
  1387. uint32_t width,
  1388. uint32_t height)
  1389. {
  1390. struct dc_cursor_position position;
  1391. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
  1392. int ret;
  1393. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1394. ret = EINVAL;
  1395. DRM_DEBUG_KMS("%s: crtc_id=%d with size %d to %d \n",
  1396. __func__,
  1397. amdgpu_crtc->crtc_id,
  1398. width,
  1399. height);
  1400. if (!address) {
  1401. /* turn off cursor */
  1402. position.enable = false;
  1403. position.x = 0;
  1404. position.y = 0;
  1405. if (acrtc_state->stream) {
  1406. /*set cursor visible false*/
  1407. dc_stream_set_cursor_position(
  1408. acrtc_state->stream,
  1409. &position);
  1410. }
  1411. goto release;
  1412. }
  1413. if ((width > amdgpu_crtc->max_cursor_width) ||
  1414. (height > amdgpu_crtc->max_cursor_height)) {
  1415. DRM_ERROR(
  1416. "%s: bad cursor width or height %d x %d\n",
  1417. __func__,
  1418. width,
  1419. height);
  1420. goto release;
  1421. }
  1422. /*program new cursor bo to hardware*/
  1423. dm_set_cursor(amdgpu_crtc, address, width, height);
  1424. release:
  1425. return ret;
  1426. }
  1427. static int dm_crtc_cursor_move(struct drm_crtc *crtc,
  1428. int x, int y)
  1429. {
  1430. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1431. int xorigin = 0, yorigin = 0;
  1432. struct dc_cursor_position position;
  1433. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
  1434. amdgpu_crtc->cursor_x = x;
  1435. amdgpu_crtc->cursor_y = y;
  1436. /* avivo cursor are offset into the total surface */
  1437. x += crtc->primary->state->src_x >> 16;
  1438. y += crtc->primary->state->src_y >> 16;
  1439. /*
  1440. * TODO: for cursor debugging unguard the following
  1441. */
  1442. #if 0
  1443. DRM_DEBUG_KMS(
  1444. "%s: x %d y %d c->x %d c->y %d\n",
  1445. __func__,
  1446. x,
  1447. y,
  1448. crtc->x,
  1449. crtc->y);
  1450. #endif
  1451. if (x < 0) {
  1452. xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
  1453. x = 0;
  1454. }
  1455. if (y < 0) {
  1456. yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
  1457. y = 0;
  1458. }
  1459. position.enable = true;
  1460. position.x = x;
  1461. position.y = y;
  1462. position.x_hotspot = xorigin;
  1463. position.y_hotspot = yorigin;
  1464. if (acrtc_state->stream) {
  1465. if (!dc_stream_set_cursor_position(
  1466. acrtc_state->stream,
  1467. &position)) {
  1468. DRM_ERROR("DC failed to set cursor position\n");
  1469. return -EINVAL;
  1470. }
  1471. }
  1472. return 0;
  1473. }
  1474. static bool fill_rects_from_plane_state(
  1475. const struct drm_plane_state *state,
  1476. struct dc_plane_state *plane_state)
  1477. {
  1478. plane_state->src_rect.x = state->src_x >> 16;
  1479. plane_state->src_rect.y = state->src_y >> 16;
  1480. /*we ignore for now mantissa and do not to deal with floating pixels :(*/
  1481. plane_state->src_rect.width = state->src_w >> 16;
  1482. if (plane_state->src_rect.width == 0)
  1483. return false;
  1484. plane_state->src_rect.height = state->src_h >> 16;
  1485. if (plane_state->src_rect.height == 0)
  1486. return false;
  1487. plane_state->dst_rect.x = state->crtc_x;
  1488. plane_state->dst_rect.y = state->crtc_y;
  1489. if (state->crtc_w == 0)
  1490. return false;
  1491. plane_state->dst_rect.width = state->crtc_w;
  1492. if (state->crtc_h == 0)
  1493. return false;
  1494. plane_state->dst_rect.height = state->crtc_h;
  1495. plane_state->clip_rect = plane_state->dst_rect;
  1496. switch (state->rotation & DRM_MODE_ROTATE_MASK) {
  1497. case DRM_MODE_ROTATE_0:
  1498. plane_state->rotation = ROTATION_ANGLE_0;
  1499. break;
  1500. case DRM_MODE_ROTATE_90:
  1501. plane_state->rotation = ROTATION_ANGLE_90;
  1502. break;
  1503. case DRM_MODE_ROTATE_180:
  1504. plane_state->rotation = ROTATION_ANGLE_180;
  1505. break;
  1506. case DRM_MODE_ROTATE_270:
  1507. plane_state->rotation = ROTATION_ANGLE_270;
  1508. break;
  1509. default:
  1510. plane_state->rotation = ROTATION_ANGLE_0;
  1511. break;
  1512. }
  1513. return true;
  1514. }
  1515. static int get_fb_info(
  1516. const struct amdgpu_framebuffer *amdgpu_fb,
  1517. uint64_t *tiling_flags,
  1518. uint64_t *fb_location)
  1519. {
  1520. struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  1521. int r = amdgpu_bo_reserve(rbo, false);
  1522. if (unlikely(r)) {
  1523. DRM_ERROR("Unable to reserve buffer\n");
  1524. return r;
  1525. }
  1526. if (fb_location)
  1527. *fb_location = amdgpu_bo_gpu_offset(rbo);
  1528. if (tiling_flags)
  1529. amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
  1530. amdgpu_bo_unreserve(rbo);
  1531. return r;
  1532. }
  1533. static int fill_plane_attributes_from_fb(
  1534. struct amdgpu_device *adev,
  1535. struct dc_plane_state *plane_state,
  1536. const struct amdgpu_framebuffer *amdgpu_fb, bool addReq)
  1537. {
  1538. uint64_t tiling_flags;
  1539. uint64_t fb_location = 0;
  1540. unsigned int awidth;
  1541. const struct drm_framebuffer *fb = &amdgpu_fb->base;
  1542. int ret = 0;
  1543. struct drm_format_name_buf format_name;
  1544. ret = get_fb_info(
  1545. amdgpu_fb,
  1546. &tiling_flags,
  1547. addReq == true ? &fb_location:NULL);
  1548. if (ret)
  1549. return ret;
  1550. switch (fb->format->format) {
  1551. case DRM_FORMAT_C8:
  1552. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
  1553. break;
  1554. case DRM_FORMAT_RGB565:
  1555. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
  1556. break;
  1557. case DRM_FORMAT_XRGB8888:
  1558. case DRM_FORMAT_ARGB8888:
  1559. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
  1560. break;
  1561. case DRM_FORMAT_XRGB2101010:
  1562. case DRM_FORMAT_ARGB2101010:
  1563. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
  1564. break;
  1565. case DRM_FORMAT_XBGR2101010:
  1566. case DRM_FORMAT_ABGR2101010:
  1567. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
  1568. break;
  1569. case DRM_FORMAT_NV21:
  1570. plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
  1571. break;
  1572. case DRM_FORMAT_NV12:
  1573. plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
  1574. break;
  1575. default:
  1576. DRM_ERROR("Unsupported screen format %s\n",
  1577. drm_get_format_name(fb->format->format, &format_name));
  1578. return -EINVAL;
  1579. }
  1580. if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
  1581. plane_state->address.type = PLN_ADDR_TYPE_GRAPHICS;
  1582. plane_state->address.grph.addr.low_part = lower_32_bits(fb_location);
  1583. plane_state->address.grph.addr.high_part = upper_32_bits(fb_location);
  1584. plane_state->plane_size.grph.surface_size.x = 0;
  1585. plane_state->plane_size.grph.surface_size.y = 0;
  1586. plane_state->plane_size.grph.surface_size.width = fb->width;
  1587. plane_state->plane_size.grph.surface_size.height = fb->height;
  1588. plane_state->plane_size.grph.surface_pitch =
  1589. fb->pitches[0] / fb->format->cpp[0];
  1590. /* TODO: unhardcode */
  1591. plane_state->color_space = COLOR_SPACE_SRGB;
  1592. } else {
  1593. awidth = ALIGN(fb->width, 64);
  1594. plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
  1595. plane_state->address.video_progressive.luma_addr.low_part
  1596. = lower_32_bits(fb_location);
  1597. plane_state->address.video_progressive.chroma_addr.low_part
  1598. = lower_32_bits(fb_location) +
  1599. (awidth * fb->height);
  1600. plane_state->plane_size.video.luma_size.x = 0;
  1601. plane_state->plane_size.video.luma_size.y = 0;
  1602. plane_state->plane_size.video.luma_size.width = awidth;
  1603. plane_state->plane_size.video.luma_size.height = fb->height;
  1604. /* TODO: unhardcode */
  1605. plane_state->plane_size.video.luma_pitch = awidth;
  1606. plane_state->plane_size.video.chroma_size.x = 0;
  1607. plane_state->plane_size.video.chroma_size.y = 0;
  1608. plane_state->plane_size.video.chroma_size.width = awidth;
  1609. plane_state->plane_size.video.chroma_size.height = fb->height;
  1610. plane_state->plane_size.video.chroma_pitch = awidth / 2;
  1611. /* TODO: unhardcode */
  1612. plane_state->color_space = COLOR_SPACE_YCBCR709;
  1613. }
  1614. memset(&plane_state->tiling_info, 0, sizeof(plane_state->tiling_info));
  1615. /* Fill GFX8 params */
  1616. if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
  1617. unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
  1618. bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
  1619. bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
  1620. mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
  1621. tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
  1622. num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
  1623. /* XXX fix me for VI */
  1624. plane_state->tiling_info.gfx8.num_banks = num_banks;
  1625. plane_state->tiling_info.gfx8.array_mode =
  1626. DC_ARRAY_2D_TILED_THIN1;
  1627. plane_state->tiling_info.gfx8.tile_split = tile_split;
  1628. plane_state->tiling_info.gfx8.bank_width = bankw;
  1629. plane_state->tiling_info.gfx8.bank_height = bankh;
  1630. plane_state->tiling_info.gfx8.tile_aspect = mtaspect;
  1631. plane_state->tiling_info.gfx8.tile_mode =
  1632. DC_ADDR_SURF_MICRO_TILING_DISPLAY;
  1633. } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
  1634. == DC_ARRAY_1D_TILED_THIN1) {
  1635. plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
  1636. }
  1637. plane_state->tiling_info.gfx8.pipe_config =
  1638. AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
  1639. if (adev->asic_type == CHIP_VEGA10 ||
  1640. adev->asic_type == CHIP_RAVEN) {
  1641. /* Fill GFX9 params */
  1642. plane_state->tiling_info.gfx9.num_pipes =
  1643. adev->gfx.config.gb_addr_config_fields.num_pipes;
  1644. plane_state->tiling_info.gfx9.num_banks =
  1645. adev->gfx.config.gb_addr_config_fields.num_banks;
  1646. plane_state->tiling_info.gfx9.pipe_interleave =
  1647. adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
  1648. plane_state->tiling_info.gfx9.num_shader_engines =
  1649. adev->gfx.config.gb_addr_config_fields.num_se;
  1650. plane_state->tiling_info.gfx9.max_compressed_frags =
  1651. adev->gfx.config.gb_addr_config_fields.max_compress_frags;
  1652. plane_state->tiling_info.gfx9.num_rb_per_se =
  1653. adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
  1654. plane_state->tiling_info.gfx9.swizzle =
  1655. AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
  1656. plane_state->tiling_info.gfx9.shaderEnable = 1;
  1657. }
  1658. plane_state->visible = true;
  1659. plane_state->scaling_quality.h_taps_c = 0;
  1660. plane_state->scaling_quality.v_taps_c = 0;
  1661. /* is this needed? is plane_state zeroed at allocation? */
  1662. plane_state->scaling_quality.h_taps = 0;
  1663. plane_state->scaling_quality.v_taps = 0;
  1664. plane_state->stereo_format = PLANE_STEREO_FORMAT_NONE;
  1665. return ret;
  1666. }
  1667. #define NUM_OF_RAW_GAMMA_RAMP_RGB_256 256
  1668. static void fill_gamma_from_crtc_state(
  1669. const struct drm_crtc_state *crtc_state,
  1670. struct dc_plane_state *plane_state)
  1671. {
  1672. int i;
  1673. struct dc_gamma *gamma;
  1674. struct drm_color_lut *lut = (struct drm_color_lut *) crtc_state->gamma_lut->data;
  1675. gamma = dc_create_gamma();
  1676. if (gamma == NULL) {
  1677. WARN_ON(1);
  1678. return;
  1679. }
  1680. for (i = 0; i < NUM_OF_RAW_GAMMA_RAMP_RGB_256; i++) {
  1681. gamma->red[i] = lut[i].red;
  1682. gamma->green[i] = lut[i].green;
  1683. gamma->blue[i] = lut[i].blue;
  1684. }
  1685. plane_state->gamma_correction = gamma;
  1686. }
  1687. static int fill_plane_attributes(
  1688. struct amdgpu_device *adev,
  1689. struct dc_plane_state *dc_plane_state,
  1690. struct drm_plane_state *plane_state,
  1691. struct drm_crtc_state *crtc_state,
  1692. bool addrReq)
  1693. {
  1694. const struct amdgpu_framebuffer *amdgpu_fb =
  1695. to_amdgpu_framebuffer(plane_state->fb);
  1696. const struct drm_crtc *crtc = plane_state->crtc;
  1697. struct dc_transfer_func *input_tf;
  1698. int ret = 0;
  1699. if (!fill_rects_from_plane_state(plane_state, dc_plane_state))
  1700. return -EINVAL;
  1701. ret = fill_plane_attributes_from_fb(
  1702. crtc->dev->dev_private,
  1703. dc_plane_state,
  1704. amdgpu_fb,
  1705. addrReq);
  1706. if (ret)
  1707. return ret;
  1708. input_tf = dc_create_transfer_func();
  1709. if (input_tf == NULL)
  1710. return -ENOMEM;
  1711. input_tf->type = TF_TYPE_PREDEFINED;
  1712. input_tf->tf = TRANSFER_FUNCTION_SRGB;
  1713. dc_plane_state->in_transfer_func = input_tf;
  1714. /* In case of gamma set, update gamma value */
  1715. if (crtc_state->gamma_lut)
  1716. fill_gamma_from_crtc_state(crtc_state, dc_plane_state);
  1717. return ret;
  1718. }
  1719. /*****************************************************************************/
  1720. struct amdgpu_connector *aconnector_from_drm_crtc_id(
  1721. const struct drm_crtc *crtc)
  1722. {
  1723. struct drm_device *dev = crtc->dev;
  1724. struct drm_connector *connector;
  1725. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  1726. struct amdgpu_connector *aconnector;
  1727. list_for_each_entry(connector,
  1728. &dev->mode_config.connector_list, head) {
  1729. aconnector = to_amdgpu_connector(connector);
  1730. if (aconnector->base.state->crtc != &acrtc->base)
  1731. continue;
  1732. /* Found the connector */
  1733. return aconnector;
  1734. }
  1735. /* If we get here, not found. */
  1736. return NULL;
  1737. }
  1738. static void update_stream_scaling_settings(
  1739. const struct drm_display_mode *mode,
  1740. const struct dm_connector_state *dm_state,
  1741. struct dc_stream_state *stream)
  1742. {
  1743. enum amdgpu_rmx_type rmx_type;
  1744. struct rect src = { 0 }; /* viewport in composition space*/
  1745. struct rect dst = { 0 }; /* stream addressable area */
  1746. /* no mode. nothing to be done */
  1747. if (!mode)
  1748. return;
  1749. /* Full screen scaling by default */
  1750. src.width = mode->hdisplay;
  1751. src.height = mode->vdisplay;
  1752. dst.width = stream->timing.h_addressable;
  1753. dst.height = stream->timing.v_addressable;
  1754. rmx_type = dm_state->scaling;
  1755. if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
  1756. if (src.width * dst.height <
  1757. src.height * dst.width) {
  1758. /* height needs less upscaling/more downscaling */
  1759. dst.width = src.width *
  1760. dst.height / src.height;
  1761. } else {
  1762. /* width needs less upscaling/more downscaling */
  1763. dst.height = src.height *
  1764. dst.width / src.width;
  1765. }
  1766. } else if (rmx_type == RMX_CENTER) {
  1767. dst = src;
  1768. }
  1769. dst.x = (stream->timing.h_addressable - dst.width) / 2;
  1770. dst.y = (stream->timing.v_addressable - dst.height) / 2;
  1771. if (dm_state->underscan_enable) {
  1772. dst.x += dm_state->underscan_hborder / 2;
  1773. dst.y += dm_state->underscan_vborder / 2;
  1774. dst.width -= dm_state->underscan_hborder;
  1775. dst.height -= dm_state->underscan_vborder;
  1776. }
  1777. stream->src = src;
  1778. stream->dst = dst;
  1779. DRM_DEBUG_KMS("Destination Rectangle x:%d y:%d width:%d height:%d\n",
  1780. dst.x, dst.y, dst.width, dst.height);
  1781. }
  1782. static enum dc_color_depth convert_color_depth_from_display_info(
  1783. const struct drm_connector *connector)
  1784. {
  1785. uint32_t bpc = connector->display_info.bpc;
  1786. /* Limited color depth to 8bit
  1787. * TODO: Still need to handle deep color
  1788. */
  1789. if (bpc > 8)
  1790. bpc = 8;
  1791. switch (bpc) {
  1792. case 0:
  1793. /* Temporary Work around, DRM don't parse color depth for
  1794. * EDID revision before 1.4
  1795. * TODO: Fix edid parsing
  1796. */
  1797. return COLOR_DEPTH_888;
  1798. case 6:
  1799. return COLOR_DEPTH_666;
  1800. case 8:
  1801. return COLOR_DEPTH_888;
  1802. case 10:
  1803. return COLOR_DEPTH_101010;
  1804. case 12:
  1805. return COLOR_DEPTH_121212;
  1806. case 14:
  1807. return COLOR_DEPTH_141414;
  1808. case 16:
  1809. return COLOR_DEPTH_161616;
  1810. default:
  1811. return COLOR_DEPTH_UNDEFINED;
  1812. }
  1813. }
  1814. static enum dc_aspect_ratio get_aspect_ratio(
  1815. const struct drm_display_mode *mode_in)
  1816. {
  1817. int32_t width = mode_in->crtc_hdisplay * 9;
  1818. int32_t height = mode_in->crtc_vdisplay * 16;
  1819. if ((width - height) < 10 && (width - height) > -10)
  1820. return ASPECT_RATIO_16_9;
  1821. else
  1822. return ASPECT_RATIO_4_3;
  1823. }
  1824. static enum dc_color_space get_output_color_space(
  1825. const struct dc_crtc_timing *dc_crtc_timing)
  1826. {
  1827. enum dc_color_space color_space = COLOR_SPACE_SRGB;
  1828. switch (dc_crtc_timing->pixel_encoding) {
  1829. case PIXEL_ENCODING_YCBCR422:
  1830. case PIXEL_ENCODING_YCBCR444:
  1831. case PIXEL_ENCODING_YCBCR420:
  1832. {
  1833. /*
  1834. * 27030khz is the separation point between HDTV and SDTV
  1835. * according to HDMI spec, we use YCbCr709 and YCbCr601
  1836. * respectively
  1837. */
  1838. if (dc_crtc_timing->pix_clk_khz > 27030) {
  1839. if (dc_crtc_timing->flags.Y_ONLY)
  1840. color_space =
  1841. COLOR_SPACE_YCBCR709_LIMITED;
  1842. else
  1843. color_space = COLOR_SPACE_YCBCR709;
  1844. } else {
  1845. if (dc_crtc_timing->flags.Y_ONLY)
  1846. color_space =
  1847. COLOR_SPACE_YCBCR601_LIMITED;
  1848. else
  1849. color_space = COLOR_SPACE_YCBCR601;
  1850. }
  1851. }
  1852. break;
  1853. case PIXEL_ENCODING_RGB:
  1854. color_space = COLOR_SPACE_SRGB;
  1855. break;
  1856. default:
  1857. WARN_ON(1);
  1858. break;
  1859. }
  1860. return color_space;
  1861. }
  1862. /*****************************************************************************/
  1863. static void fill_stream_properties_from_drm_display_mode(
  1864. struct dc_stream_state *stream,
  1865. const struct drm_display_mode *mode_in,
  1866. const struct drm_connector *connector)
  1867. {
  1868. struct dc_crtc_timing *timing_out = &stream->timing;
  1869. memset(timing_out, 0, sizeof(struct dc_crtc_timing));
  1870. timing_out->h_border_left = 0;
  1871. timing_out->h_border_right = 0;
  1872. timing_out->v_border_top = 0;
  1873. timing_out->v_border_bottom = 0;
  1874. /* TODO: un-hardcode */
  1875. if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
  1876. && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
  1877. timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
  1878. else
  1879. timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
  1880. timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
  1881. timing_out->display_color_depth = convert_color_depth_from_display_info(
  1882. connector);
  1883. timing_out->scan_type = SCANNING_TYPE_NODATA;
  1884. timing_out->hdmi_vic = 0;
  1885. timing_out->vic = drm_match_cea_mode(mode_in);
  1886. timing_out->h_addressable = mode_in->crtc_hdisplay;
  1887. timing_out->h_total = mode_in->crtc_htotal;
  1888. timing_out->h_sync_width =
  1889. mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
  1890. timing_out->h_front_porch =
  1891. mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
  1892. timing_out->v_total = mode_in->crtc_vtotal;
  1893. timing_out->v_addressable = mode_in->crtc_vdisplay;
  1894. timing_out->v_front_porch =
  1895. mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
  1896. timing_out->v_sync_width =
  1897. mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
  1898. timing_out->pix_clk_khz = mode_in->crtc_clock;
  1899. timing_out->aspect_ratio = get_aspect_ratio(mode_in);
  1900. if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
  1901. timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
  1902. if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
  1903. timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
  1904. stream->output_color_space = get_output_color_space(timing_out);
  1905. {
  1906. struct dc_transfer_func *tf = dc_create_transfer_func();
  1907. tf->type = TF_TYPE_PREDEFINED;
  1908. tf->tf = TRANSFER_FUNCTION_SRGB;
  1909. stream->out_transfer_func = tf;
  1910. }
  1911. }
  1912. static void fill_audio_info(
  1913. struct audio_info *audio_info,
  1914. const struct drm_connector *drm_connector,
  1915. const struct dc_sink *dc_sink)
  1916. {
  1917. int i = 0;
  1918. int cea_revision = 0;
  1919. const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
  1920. audio_info->manufacture_id = edid_caps->manufacturer_id;
  1921. audio_info->product_id = edid_caps->product_id;
  1922. cea_revision = drm_connector->display_info.cea_rev;
  1923. while (i < AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS &&
  1924. edid_caps->display_name[i]) {
  1925. audio_info->display_name[i] = edid_caps->display_name[i];
  1926. i++;
  1927. }
  1928. if (cea_revision >= 3) {
  1929. audio_info->mode_count = edid_caps->audio_mode_count;
  1930. for (i = 0; i < audio_info->mode_count; ++i) {
  1931. audio_info->modes[i].format_code =
  1932. (enum audio_format_code)
  1933. (edid_caps->audio_modes[i].format_code);
  1934. audio_info->modes[i].channel_count =
  1935. edid_caps->audio_modes[i].channel_count;
  1936. audio_info->modes[i].sample_rates.all =
  1937. edid_caps->audio_modes[i].sample_rate;
  1938. audio_info->modes[i].sample_size =
  1939. edid_caps->audio_modes[i].sample_size;
  1940. }
  1941. }
  1942. audio_info->flags.all = edid_caps->speaker_flags;
  1943. /* TODO: We only check for the progressive mode, check for interlace mode too */
  1944. if (drm_connector->latency_present[0]) {
  1945. audio_info->video_latency = drm_connector->video_latency[0];
  1946. audio_info->audio_latency = drm_connector->audio_latency[0];
  1947. }
  1948. /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
  1949. }
  1950. static void copy_crtc_timing_for_drm_display_mode(
  1951. const struct drm_display_mode *src_mode,
  1952. struct drm_display_mode *dst_mode)
  1953. {
  1954. dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
  1955. dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
  1956. dst_mode->crtc_clock = src_mode->crtc_clock;
  1957. dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
  1958. dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
  1959. dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
  1960. dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
  1961. dst_mode->crtc_htotal = src_mode->crtc_htotal;
  1962. dst_mode->crtc_hskew = src_mode->crtc_hskew;
  1963. dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
  1964. dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
  1965. dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
  1966. dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
  1967. dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
  1968. }
  1969. static void decide_crtc_timing_for_drm_display_mode(
  1970. struct drm_display_mode *drm_mode,
  1971. const struct drm_display_mode *native_mode,
  1972. bool scale_enabled)
  1973. {
  1974. if (scale_enabled) {
  1975. copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
  1976. } else if (native_mode->clock == drm_mode->clock &&
  1977. native_mode->htotal == drm_mode->htotal &&
  1978. native_mode->vtotal == drm_mode->vtotal) {
  1979. copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
  1980. } else {
  1981. /* no scaling nor amdgpu inserted, no need to patch */
  1982. }
  1983. }
  1984. static struct dc_stream_state *create_stream_for_sink(
  1985. struct amdgpu_connector *aconnector,
  1986. const struct drm_display_mode *drm_mode,
  1987. const struct dm_connector_state *dm_state)
  1988. {
  1989. struct drm_display_mode *preferred_mode = NULL;
  1990. const struct drm_connector *drm_connector;
  1991. struct dc_stream_state *stream = NULL;
  1992. struct drm_display_mode mode = *drm_mode;
  1993. bool native_mode_found = false;
  1994. if (aconnector == NULL) {
  1995. DRM_ERROR("aconnector is NULL!\n");
  1996. goto drm_connector_null;
  1997. }
  1998. if (dm_state == NULL) {
  1999. DRM_ERROR("dm_state is NULL!\n");
  2000. goto dm_state_null;
  2001. }
  2002. drm_connector = &aconnector->base;
  2003. stream = dc_create_stream_for_sink(aconnector->dc_sink);
  2004. if (stream == NULL) {
  2005. DRM_ERROR("Failed to create stream for sink!\n");
  2006. goto stream_create_fail;
  2007. }
  2008. list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
  2009. /* Search for preferred mode */
  2010. if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
  2011. native_mode_found = true;
  2012. break;
  2013. }
  2014. }
  2015. if (!native_mode_found)
  2016. preferred_mode = list_first_entry_or_null(
  2017. &aconnector->base.modes,
  2018. struct drm_display_mode,
  2019. head);
  2020. if (preferred_mode == NULL) {
  2021. /* This may not be an error, the use case is when we we have no
  2022. * usermode calls to reset and set mode upon hotplug. In this
  2023. * case, we call set mode ourselves to restore the previous mode
  2024. * and the modelist may not be filled in in time.
  2025. */
  2026. DRM_INFO("No preferred mode found\n");
  2027. } else {
  2028. decide_crtc_timing_for_drm_display_mode(
  2029. &mode, preferred_mode,
  2030. dm_state->scaling != RMX_OFF);
  2031. }
  2032. fill_stream_properties_from_drm_display_mode(stream,
  2033. &mode, &aconnector->base);
  2034. update_stream_scaling_settings(&mode, dm_state, stream);
  2035. fill_audio_info(
  2036. &stream->audio_info,
  2037. drm_connector,
  2038. aconnector->dc_sink);
  2039. stream_create_fail:
  2040. dm_state_null:
  2041. drm_connector_null:
  2042. return stream;
  2043. }
  2044. void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
  2045. {
  2046. drm_crtc_cleanup(crtc);
  2047. kfree(crtc);
  2048. }
  2049. static void dm_crtc_destroy_state(struct drm_crtc *crtc,
  2050. struct drm_crtc_state *state)
  2051. {
  2052. struct dm_crtc_state *cur = to_dm_crtc_state(state);
  2053. /* TODO Destroy dc_stream objects are stream object is flattened */
  2054. if (cur->stream)
  2055. dc_stream_release(cur->stream);
  2056. __drm_atomic_helper_crtc_destroy_state(state);
  2057. kfree(state);
  2058. }
  2059. static void dm_crtc_reset_state(struct drm_crtc *crtc)
  2060. {
  2061. struct dm_crtc_state *state;
  2062. if (crtc->state)
  2063. dm_crtc_destroy_state(crtc, crtc->state);
  2064. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2065. if (WARN_ON(!state))
  2066. return;
  2067. crtc->state = &state->base;
  2068. crtc->state->crtc = crtc;
  2069. }
  2070. static struct drm_crtc_state *
  2071. dm_crtc_duplicate_state(struct drm_crtc *crtc)
  2072. {
  2073. struct dm_crtc_state *state, *cur;
  2074. cur = to_dm_crtc_state(crtc->state);
  2075. if (WARN_ON(!crtc->state))
  2076. return NULL;
  2077. state = dm_alloc(sizeof(*state));
  2078. __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
  2079. if (cur->stream) {
  2080. state->stream = cur->stream;
  2081. dc_stream_retain(state->stream);
  2082. }
  2083. /* TODO Duplicate dc_stream after objects are stream object is flattened */
  2084. return &state->base;
  2085. }
  2086. /* Implemented only the options currently availible for the driver */
  2087. static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
  2088. .reset = dm_crtc_reset_state,
  2089. .destroy = amdgpu_dm_crtc_destroy,
  2090. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  2091. .set_config = drm_atomic_helper_set_config,
  2092. .page_flip = drm_atomic_helper_page_flip,
  2093. .atomic_duplicate_state = dm_crtc_duplicate_state,
  2094. .atomic_destroy_state = dm_crtc_destroy_state,
  2095. };
  2096. static enum drm_connector_status
  2097. amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
  2098. {
  2099. bool connected;
  2100. struct amdgpu_connector *aconnector = to_amdgpu_connector(connector);
  2101. /* Notes:
  2102. * 1. This interface is NOT called in context of HPD irq.
  2103. * 2. This interface *is called* in context of user-mode ioctl. Which
  2104. * makes it a bad place for *any* MST-related activit. */
  2105. if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
  2106. connected = (aconnector->dc_sink != NULL);
  2107. else
  2108. connected = (aconnector->base.force == DRM_FORCE_ON);
  2109. return (connected ? connector_status_connected :
  2110. connector_status_disconnected);
  2111. }
  2112. int amdgpu_dm_connector_atomic_set_property(
  2113. struct drm_connector *connector,
  2114. struct drm_connector_state *connector_state,
  2115. struct drm_property *property,
  2116. uint64_t val)
  2117. {
  2118. struct drm_device *dev = connector->dev;
  2119. struct amdgpu_device *adev = dev->dev_private;
  2120. struct dm_connector_state *dm_old_state =
  2121. to_dm_connector_state(connector->state);
  2122. struct dm_connector_state *dm_new_state =
  2123. to_dm_connector_state(connector_state);
  2124. int ret = -EINVAL;
  2125. if (property == dev->mode_config.scaling_mode_property) {
  2126. enum amdgpu_rmx_type rmx_type;
  2127. switch (val) {
  2128. case DRM_MODE_SCALE_CENTER:
  2129. rmx_type = RMX_CENTER;
  2130. break;
  2131. case DRM_MODE_SCALE_ASPECT:
  2132. rmx_type = RMX_ASPECT;
  2133. break;
  2134. case DRM_MODE_SCALE_FULLSCREEN:
  2135. rmx_type = RMX_FULL;
  2136. break;
  2137. case DRM_MODE_SCALE_NONE:
  2138. default:
  2139. rmx_type = RMX_OFF;
  2140. break;
  2141. }
  2142. if (dm_old_state->scaling == rmx_type)
  2143. return 0;
  2144. dm_new_state->scaling = rmx_type;
  2145. ret = 0;
  2146. } else if (property == adev->mode_info.underscan_hborder_property) {
  2147. dm_new_state->underscan_hborder = val;
  2148. ret = 0;
  2149. } else if (property == adev->mode_info.underscan_vborder_property) {
  2150. dm_new_state->underscan_vborder = val;
  2151. ret = 0;
  2152. } else if (property == adev->mode_info.underscan_property) {
  2153. dm_new_state->underscan_enable = val;
  2154. ret = 0;
  2155. }
  2156. return ret;
  2157. }
  2158. int amdgpu_dm_connector_atomic_get_property(
  2159. struct drm_connector *connector,
  2160. const struct drm_connector_state *state,
  2161. struct drm_property *property,
  2162. uint64_t *val)
  2163. {
  2164. struct drm_device *dev = connector->dev;
  2165. struct amdgpu_device *adev = dev->dev_private;
  2166. struct dm_connector_state *dm_state =
  2167. to_dm_connector_state(state);
  2168. int ret = -EINVAL;
  2169. if (property == dev->mode_config.scaling_mode_property) {
  2170. switch (dm_state->scaling) {
  2171. case RMX_CENTER:
  2172. *val = DRM_MODE_SCALE_CENTER;
  2173. break;
  2174. case RMX_ASPECT:
  2175. *val = DRM_MODE_SCALE_ASPECT;
  2176. break;
  2177. case RMX_FULL:
  2178. *val = DRM_MODE_SCALE_FULLSCREEN;
  2179. break;
  2180. case RMX_OFF:
  2181. default:
  2182. *val = DRM_MODE_SCALE_NONE;
  2183. break;
  2184. }
  2185. ret = 0;
  2186. } else if (property == adev->mode_info.underscan_hborder_property) {
  2187. *val = dm_state->underscan_hborder;
  2188. ret = 0;
  2189. } else if (property == adev->mode_info.underscan_vborder_property) {
  2190. *val = dm_state->underscan_vborder;
  2191. ret = 0;
  2192. } else if (property == adev->mode_info.underscan_property) {
  2193. *val = dm_state->underscan_enable;
  2194. ret = 0;
  2195. }
  2196. return ret;
  2197. }
  2198. void amdgpu_dm_connector_destroy(struct drm_connector *connector)
  2199. {
  2200. struct amdgpu_connector *aconnector = to_amdgpu_connector(connector);
  2201. const struct dc_link *link = aconnector->dc_link;
  2202. struct amdgpu_device *adev = connector->dev->dev_private;
  2203. struct amdgpu_display_manager *dm = &adev->dm;
  2204. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
  2205. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  2206. if (link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) {
  2207. amdgpu_dm_register_backlight_device(dm);
  2208. if (dm->backlight_dev) {
  2209. backlight_device_unregister(dm->backlight_dev);
  2210. dm->backlight_dev = NULL;
  2211. }
  2212. }
  2213. #endif
  2214. drm_connector_unregister(connector);
  2215. drm_connector_cleanup(connector);
  2216. kfree(connector);
  2217. }
  2218. void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
  2219. {
  2220. struct dm_connector_state *state =
  2221. to_dm_connector_state(connector->state);
  2222. kfree(state);
  2223. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2224. if (state) {
  2225. state->scaling = RMX_OFF;
  2226. state->underscan_enable = false;
  2227. state->underscan_hborder = 0;
  2228. state->underscan_vborder = 0;
  2229. connector->state = &state->base;
  2230. connector->state->connector = connector;
  2231. }
  2232. }
  2233. struct drm_connector_state *amdgpu_dm_connector_atomic_duplicate_state(
  2234. struct drm_connector *connector)
  2235. {
  2236. struct dm_connector_state *state =
  2237. to_dm_connector_state(connector->state);
  2238. struct dm_connector_state *new_state =
  2239. kmemdup(state, sizeof(*state), GFP_KERNEL);
  2240. if (new_state) {
  2241. __drm_atomic_helper_connector_duplicate_state(connector,
  2242. &new_state->base);
  2243. return &new_state->base;
  2244. }
  2245. return NULL;
  2246. }
  2247. static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
  2248. .reset = amdgpu_dm_connector_funcs_reset,
  2249. .detect = amdgpu_dm_connector_detect,
  2250. .fill_modes = drm_helper_probe_single_connector_modes,
  2251. .destroy = amdgpu_dm_connector_destroy,
  2252. .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
  2253. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  2254. .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
  2255. .atomic_get_property = amdgpu_dm_connector_atomic_get_property
  2256. };
  2257. static struct drm_encoder *best_encoder(struct drm_connector *connector)
  2258. {
  2259. int enc_id = connector->encoder_ids[0];
  2260. struct drm_mode_object *obj;
  2261. struct drm_encoder *encoder;
  2262. DRM_DEBUG_KMS("Finding the best encoder\n");
  2263. /* pick the encoder ids */
  2264. if (enc_id) {
  2265. obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
  2266. if (!obj) {
  2267. DRM_ERROR("Couldn't find a matching encoder for our connector\n");
  2268. return NULL;
  2269. }
  2270. encoder = obj_to_encoder(obj);
  2271. return encoder;
  2272. }
  2273. DRM_ERROR("No encoder id\n");
  2274. return NULL;
  2275. }
  2276. static int get_modes(struct drm_connector *connector)
  2277. {
  2278. return amdgpu_dm_connector_get_modes(connector);
  2279. }
  2280. static void create_eml_sink(struct amdgpu_connector *aconnector)
  2281. {
  2282. struct dc_sink_init_data init_params = {
  2283. .link = aconnector->dc_link,
  2284. .sink_signal = SIGNAL_TYPE_VIRTUAL
  2285. };
  2286. struct edid *edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
  2287. if (!aconnector->base.edid_blob_ptr ||
  2288. !aconnector->base.edid_blob_ptr->data) {
  2289. DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
  2290. aconnector->base.name);
  2291. aconnector->base.force = DRM_FORCE_OFF;
  2292. aconnector->base.override_edid = false;
  2293. return;
  2294. }
  2295. aconnector->edid = edid;
  2296. aconnector->dc_em_sink = dc_link_add_remote_sink(
  2297. aconnector->dc_link,
  2298. (uint8_t *)edid,
  2299. (edid->extensions + 1) * EDID_LENGTH,
  2300. &init_params);
  2301. if (aconnector->base.force
  2302. == DRM_FORCE_ON)
  2303. aconnector->dc_sink = aconnector->dc_link->local_sink ?
  2304. aconnector->dc_link->local_sink :
  2305. aconnector->dc_em_sink;
  2306. }
  2307. static void handle_edid_mgmt(struct amdgpu_connector *aconnector)
  2308. {
  2309. struct dc_link *link = (struct dc_link *)aconnector->dc_link;
  2310. /* In case of headless boot with force on for DP managed connector
  2311. * Those settings have to be != 0 to get initial modeset
  2312. */
  2313. if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
  2314. link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
  2315. link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
  2316. }
  2317. aconnector->base.override_edid = true;
  2318. create_eml_sink(aconnector);
  2319. }
  2320. int amdgpu_dm_connector_mode_valid(
  2321. struct drm_connector *connector,
  2322. struct drm_display_mode *mode)
  2323. {
  2324. int result = MODE_ERROR;
  2325. struct dc_sink *dc_sink;
  2326. struct amdgpu_device *adev = connector->dev->dev_private;
  2327. /* TODO: Unhardcode stream count */
  2328. struct dc_stream_state *stream;
  2329. struct amdgpu_connector *aconnector = to_amdgpu_connector(connector);
  2330. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  2331. (mode->flags & DRM_MODE_FLAG_DBLSCAN))
  2332. return result;
  2333. /* Only run this the first time mode_valid is called to initilialize
  2334. * EDID mgmt
  2335. */
  2336. if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
  2337. !aconnector->dc_em_sink)
  2338. handle_edid_mgmt(aconnector);
  2339. dc_sink = to_amdgpu_connector(connector)->dc_sink;
  2340. if (dc_sink == NULL) {
  2341. DRM_ERROR("dc_sink is NULL!\n");
  2342. goto fail;
  2343. }
  2344. stream = dc_create_stream_for_sink(dc_sink);
  2345. if (stream == NULL) {
  2346. DRM_ERROR("Failed to create stream for sink!\n");
  2347. goto fail;
  2348. }
  2349. drm_mode_set_crtcinfo(mode, 0);
  2350. fill_stream_properties_from_drm_display_mode(stream, mode, connector);
  2351. stream->src.width = mode->hdisplay;
  2352. stream->src.height = mode->vdisplay;
  2353. stream->dst = stream->src;
  2354. if (dc_validate_stream(adev->dm.dc, stream))
  2355. result = MODE_OK;
  2356. dc_stream_release(stream);
  2357. fail:
  2358. /* TODO: error handling*/
  2359. return result;
  2360. }
  2361. static const struct drm_connector_helper_funcs
  2362. amdgpu_dm_connector_helper_funcs = {
  2363. /*
  2364. * If hotplug a second bigger display in FB Con mode, bigger resolution
  2365. * modes will be filtered by drm_mode_validate_size(), and those modes
  2366. * is missing after user start lightdm. So we need to renew modes list.
  2367. * in get_modes call back, not just return the modes count
  2368. */
  2369. .get_modes = get_modes,
  2370. .mode_valid = amdgpu_dm_connector_mode_valid,
  2371. .best_encoder = best_encoder
  2372. };
  2373. static void dm_crtc_helper_disable(struct drm_crtc *crtc)
  2374. {
  2375. }
  2376. static int dm_crtc_helper_atomic_check(
  2377. struct drm_crtc *crtc,
  2378. struct drm_crtc_state *state)
  2379. {
  2380. struct amdgpu_device *adev = crtc->dev->dev_private;
  2381. struct dc *dc = adev->dm.dc;
  2382. struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
  2383. int ret = -EINVAL;
  2384. if (unlikely(!dm_crtc_state->stream &&
  2385. modeset_required(state, NULL, dm_crtc_state->stream))) {
  2386. WARN_ON(1);
  2387. return ret;
  2388. }
  2389. /* In some use cases, like reset, no stream is attached */
  2390. if (!dm_crtc_state->stream)
  2391. return 0;
  2392. if (dc_validate_stream(dc, dm_crtc_state->stream))
  2393. return 0;
  2394. return ret;
  2395. }
  2396. static bool dm_crtc_helper_mode_fixup(
  2397. struct drm_crtc *crtc,
  2398. const struct drm_display_mode *mode,
  2399. struct drm_display_mode *adjusted_mode)
  2400. {
  2401. return true;
  2402. }
  2403. static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
  2404. .disable = dm_crtc_helper_disable,
  2405. .atomic_check = dm_crtc_helper_atomic_check,
  2406. .mode_fixup = dm_crtc_helper_mode_fixup
  2407. };
  2408. static void dm_encoder_helper_disable(struct drm_encoder *encoder)
  2409. {
  2410. }
  2411. static int dm_encoder_helper_atomic_check(
  2412. struct drm_encoder *encoder,
  2413. struct drm_crtc_state *crtc_state,
  2414. struct drm_connector_state *conn_state)
  2415. {
  2416. return 0;
  2417. }
  2418. const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
  2419. .disable = dm_encoder_helper_disable,
  2420. .atomic_check = dm_encoder_helper_atomic_check
  2421. };
  2422. static void dm_drm_plane_reset(struct drm_plane *plane)
  2423. {
  2424. struct dm_plane_state *amdgpu_state = NULL;
  2425. if (plane->state)
  2426. plane->funcs->atomic_destroy_state(plane, plane->state);
  2427. amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
  2428. if (amdgpu_state) {
  2429. plane->state = &amdgpu_state->base;
  2430. plane->state->plane = plane;
  2431. plane->state->rotation = DRM_MODE_ROTATE_0;
  2432. } else
  2433. WARN_ON(1);
  2434. }
  2435. static struct drm_plane_state *
  2436. dm_drm_plane_duplicate_state(struct drm_plane *plane)
  2437. {
  2438. struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
  2439. old_dm_plane_state = to_dm_plane_state(plane->state);
  2440. dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
  2441. if (!dm_plane_state)
  2442. return NULL;
  2443. __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
  2444. if (old_dm_plane_state->dc_state) {
  2445. dm_plane_state->dc_state = old_dm_plane_state->dc_state;
  2446. dc_plane_state_retain(dm_plane_state->dc_state);
  2447. }
  2448. return &dm_plane_state->base;
  2449. }
  2450. void dm_drm_plane_destroy_state(struct drm_plane *plane,
  2451. struct drm_plane_state *state)
  2452. {
  2453. struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
  2454. if (dm_plane_state->dc_state)
  2455. dc_plane_state_release(dm_plane_state->dc_state);
  2456. __drm_atomic_helper_plane_destroy_state(state);
  2457. kfree(dm_plane_state);
  2458. }
  2459. static const struct drm_plane_funcs dm_plane_funcs = {
  2460. .update_plane = drm_atomic_helper_update_plane,
  2461. .disable_plane = drm_atomic_helper_disable_plane,
  2462. .destroy = drm_plane_cleanup,
  2463. .reset = dm_drm_plane_reset,
  2464. .atomic_duplicate_state = dm_drm_plane_duplicate_state,
  2465. .atomic_destroy_state = dm_drm_plane_destroy_state,
  2466. };
  2467. static int dm_plane_helper_prepare_fb(
  2468. struct drm_plane *plane,
  2469. struct drm_plane_state *new_state)
  2470. {
  2471. struct amdgpu_framebuffer *afb;
  2472. struct drm_gem_object *obj;
  2473. struct amdgpu_bo *rbo;
  2474. int r;
  2475. struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
  2476. unsigned int awidth;
  2477. dm_plane_state_old = to_dm_plane_state(plane->state);
  2478. dm_plane_state_new = to_dm_plane_state(new_state);
  2479. if (!new_state->fb) {
  2480. DRM_DEBUG_KMS("No FB bound\n");
  2481. return 0;
  2482. }
  2483. afb = to_amdgpu_framebuffer(new_state->fb);
  2484. obj = afb->obj;
  2485. rbo = gem_to_amdgpu_bo(obj);
  2486. r = amdgpu_bo_reserve(rbo, false);
  2487. if (unlikely(r != 0))
  2488. return r;
  2489. r = amdgpu_bo_pin(rbo, AMDGPU_GEM_DOMAIN_VRAM, &afb->address);
  2490. amdgpu_bo_unreserve(rbo);
  2491. if (unlikely(r != 0)) {
  2492. DRM_ERROR("Failed to pin framebuffer\n");
  2493. return r;
  2494. }
  2495. amdgpu_bo_ref(rbo);
  2496. if (dm_plane_state_new->dc_state &&
  2497. dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
  2498. struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
  2499. if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
  2500. plane_state->address.grph.addr.low_part = lower_32_bits(afb->address);
  2501. plane_state->address.grph.addr.high_part = upper_32_bits(afb->address);
  2502. } else {
  2503. awidth = ALIGN(new_state->fb->width, 64);
  2504. plane_state->address.video_progressive.luma_addr.low_part
  2505. = lower_32_bits(afb->address);
  2506. plane_state->address.video_progressive.chroma_addr.low_part
  2507. = lower_32_bits(afb->address) +
  2508. (awidth * new_state->fb->height);
  2509. }
  2510. }
  2511. /* It's a hack for s3 since in 4.9 kernel filter out cursor buffer
  2512. * prepare and cleanup in drm_atomic_helper_prepare_planes
  2513. * and drm_atomic_helper_cleanup_planes because fb doens't in s3.
  2514. * IN 4.10 kernel this code should be removed and amdgpu_device_suspend
  2515. * code touching fram buffers should be avoided for DC.
  2516. */
  2517. if (plane->type == DRM_PLANE_TYPE_CURSOR) {
  2518. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_state->crtc);
  2519. acrtc->cursor_bo = obj;
  2520. }
  2521. return 0;
  2522. }
  2523. static void dm_plane_helper_cleanup_fb(
  2524. struct drm_plane *plane,
  2525. struct drm_plane_state *old_state)
  2526. {
  2527. struct amdgpu_bo *rbo;
  2528. struct amdgpu_framebuffer *afb;
  2529. int r;
  2530. if (!old_state->fb)
  2531. return;
  2532. afb = to_amdgpu_framebuffer(old_state->fb);
  2533. rbo = gem_to_amdgpu_bo(afb->obj);
  2534. r = amdgpu_bo_reserve(rbo, false);
  2535. if (unlikely(r)) {
  2536. DRM_ERROR("failed to reserve rbo before unpin\n");
  2537. return;
  2538. }
  2539. amdgpu_bo_unpin(rbo);
  2540. amdgpu_bo_unreserve(rbo);
  2541. amdgpu_bo_unref(&rbo);
  2542. }
  2543. int dm_create_validation_set_for_connector(struct drm_connector *connector,
  2544. struct drm_display_mode *mode, struct dc_validation_set *val_set)
  2545. {
  2546. int result = MODE_ERROR;
  2547. struct dc_sink *dc_sink =
  2548. to_amdgpu_connector(connector)->dc_sink;
  2549. /* TODO: Unhardcode stream count */
  2550. struct dc_stream_state *stream;
  2551. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  2552. (mode->flags & DRM_MODE_FLAG_DBLSCAN))
  2553. return result;
  2554. if (dc_sink == NULL) {
  2555. DRM_ERROR("dc_sink is NULL!\n");
  2556. return result;
  2557. }
  2558. stream = dc_create_stream_for_sink(dc_sink);
  2559. if (stream == NULL) {
  2560. DRM_ERROR("Failed to create stream for sink!\n");
  2561. return result;
  2562. }
  2563. drm_mode_set_crtcinfo(mode, 0);
  2564. fill_stream_properties_from_drm_display_mode(stream, mode, connector);
  2565. val_set->stream = stream;
  2566. stream->src.width = mode->hdisplay;
  2567. stream->src.height = mode->vdisplay;
  2568. stream->dst = stream->src;
  2569. return MODE_OK;
  2570. }
  2571. int dm_plane_atomic_check(struct drm_plane *plane,
  2572. struct drm_plane_state *state)
  2573. {
  2574. struct amdgpu_device *adev = plane->dev->dev_private;
  2575. struct dc *dc = adev->dm.dc;
  2576. struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
  2577. if (!dm_plane_state->dc_state)
  2578. return true;
  2579. if (dc_validate_plane(dc, dm_plane_state->dc_state))
  2580. return 0;
  2581. return -EINVAL;
  2582. }
  2583. static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
  2584. .prepare_fb = dm_plane_helper_prepare_fb,
  2585. .cleanup_fb = dm_plane_helper_cleanup_fb,
  2586. .atomic_check = dm_plane_atomic_check,
  2587. };
  2588. /*
  2589. * TODO: these are currently initialized to rgb formats only.
  2590. * For future use cases we should either initialize them dynamically based on
  2591. * plane capabilities, or initialize this array to all formats, so internal drm
  2592. * check will succeed, and let DC to implement proper check
  2593. */
  2594. static uint32_t rgb_formats[] = {
  2595. DRM_FORMAT_RGB888,
  2596. DRM_FORMAT_XRGB8888,
  2597. DRM_FORMAT_ARGB8888,
  2598. DRM_FORMAT_RGBA8888,
  2599. DRM_FORMAT_XRGB2101010,
  2600. DRM_FORMAT_XBGR2101010,
  2601. DRM_FORMAT_ARGB2101010,
  2602. DRM_FORMAT_ABGR2101010,
  2603. };
  2604. static uint32_t yuv_formats[] = {
  2605. DRM_FORMAT_NV12,
  2606. DRM_FORMAT_NV21,
  2607. };
  2608. static const u32 cursor_formats[] = {
  2609. DRM_FORMAT_ARGB8888
  2610. };
  2611. int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
  2612. struct amdgpu_plane *aplane,
  2613. unsigned long possible_crtcs)
  2614. {
  2615. int res = -EPERM;
  2616. switch (aplane->base.type) {
  2617. case DRM_PLANE_TYPE_PRIMARY:
  2618. aplane->base.format_default = true;
  2619. res = drm_universal_plane_init(
  2620. dm->adev->ddev,
  2621. &aplane->base,
  2622. possible_crtcs,
  2623. &dm_plane_funcs,
  2624. rgb_formats,
  2625. ARRAY_SIZE(rgb_formats),
  2626. NULL, aplane->base.type, NULL);
  2627. break;
  2628. case DRM_PLANE_TYPE_OVERLAY:
  2629. res = drm_universal_plane_init(
  2630. dm->adev->ddev,
  2631. &aplane->base,
  2632. possible_crtcs,
  2633. &dm_plane_funcs,
  2634. yuv_formats,
  2635. ARRAY_SIZE(yuv_formats),
  2636. NULL, aplane->base.type, NULL);
  2637. break;
  2638. case DRM_PLANE_TYPE_CURSOR:
  2639. res = drm_universal_plane_init(
  2640. dm->adev->ddev,
  2641. &aplane->base,
  2642. possible_crtcs,
  2643. &dm_plane_funcs,
  2644. cursor_formats,
  2645. ARRAY_SIZE(cursor_formats),
  2646. NULL, aplane->base.type, NULL);
  2647. break;
  2648. }
  2649. drm_plane_helper_add(&aplane->base, &dm_plane_helper_funcs);
  2650. return res;
  2651. }
  2652. int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
  2653. struct drm_plane *plane,
  2654. uint32_t crtc_index)
  2655. {
  2656. struct amdgpu_crtc *acrtc = NULL;
  2657. struct amdgpu_plane *cursor_plane;
  2658. int res = -ENOMEM;
  2659. cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
  2660. if (!cursor_plane)
  2661. goto fail;
  2662. cursor_plane->base.type = DRM_PLANE_TYPE_CURSOR;
  2663. res = amdgpu_dm_plane_init(dm, cursor_plane, 0);
  2664. acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
  2665. if (!acrtc)
  2666. goto fail;
  2667. res = drm_crtc_init_with_planes(
  2668. dm->ddev,
  2669. &acrtc->base,
  2670. plane,
  2671. &cursor_plane->base,
  2672. &amdgpu_dm_crtc_funcs, NULL);
  2673. if (res)
  2674. goto fail;
  2675. drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
  2676. acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
  2677. acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
  2678. acrtc->crtc_id = crtc_index;
  2679. acrtc->base.enabled = false;
  2680. dm->adev->mode_info.crtcs[crtc_index] = acrtc;
  2681. drm_mode_crtc_set_gamma_size(&acrtc->base, 256);
  2682. return 0;
  2683. fail:
  2684. kfree(acrtc);
  2685. kfree(cursor_plane);
  2686. acrtc->crtc_id = -1;
  2687. return res;
  2688. }
  2689. static int to_drm_connector_type(enum signal_type st)
  2690. {
  2691. switch (st) {
  2692. case SIGNAL_TYPE_HDMI_TYPE_A:
  2693. return DRM_MODE_CONNECTOR_HDMIA;
  2694. case SIGNAL_TYPE_EDP:
  2695. return DRM_MODE_CONNECTOR_eDP;
  2696. case SIGNAL_TYPE_RGB:
  2697. return DRM_MODE_CONNECTOR_VGA;
  2698. case SIGNAL_TYPE_DISPLAY_PORT:
  2699. case SIGNAL_TYPE_DISPLAY_PORT_MST:
  2700. return DRM_MODE_CONNECTOR_DisplayPort;
  2701. case SIGNAL_TYPE_DVI_DUAL_LINK:
  2702. case SIGNAL_TYPE_DVI_SINGLE_LINK:
  2703. return DRM_MODE_CONNECTOR_DVID;
  2704. case SIGNAL_TYPE_VIRTUAL:
  2705. return DRM_MODE_CONNECTOR_VIRTUAL;
  2706. default:
  2707. return DRM_MODE_CONNECTOR_Unknown;
  2708. }
  2709. }
  2710. static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
  2711. {
  2712. const struct drm_connector_helper_funcs *helper =
  2713. connector->helper_private;
  2714. struct drm_encoder *encoder;
  2715. struct amdgpu_encoder *amdgpu_encoder;
  2716. encoder = helper->best_encoder(connector);
  2717. if (encoder == NULL)
  2718. return;
  2719. amdgpu_encoder = to_amdgpu_encoder(encoder);
  2720. amdgpu_encoder->native_mode.clock = 0;
  2721. if (!list_empty(&connector->probed_modes)) {
  2722. struct drm_display_mode *preferred_mode = NULL;
  2723. list_for_each_entry(preferred_mode,
  2724. &connector->probed_modes,
  2725. head) {
  2726. if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
  2727. amdgpu_encoder->native_mode = *preferred_mode;
  2728. break;
  2729. }
  2730. }
  2731. }
  2732. static struct drm_display_mode *amdgpu_dm_create_common_mode(
  2733. struct drm_encoder *encoder, char *name,
  2734. int hdisplay, int vdisplay)
  2735. {
  2736. struct drm_device *dev = encoder->dev;
  2737. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2738. struct drm_display_mode *mode = NULL;
  2739. struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
  2740. mode = drm_mode_duplicate(dev, native_mode);
  2741. if (mode == NULL)
  2742. return NULL;
  2743. mode->hdisplay = hdisplay;
  2744. mode->vdisplay = vdisplay;
  2745. mode->type &= ~DRM_MODE_TYPE_PREFERRED;
  2746. strncpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
  2747. return mode;
  2748. }
  2749. static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
  2750. struct drm_connector *connector)
  2751. {
  2752. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2753. struct drm_display_mode *mode = NULL;
  2754. struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
  2755. struct amdgpu_connector *amdgpu_connector =
  2756. to_amdgpu_connector(connector);
  2757. int i;
  2758. int n;
  2759. struct mode_size {
  2760. char name[DRM_DISPLAY_MODE_LEN];
  2761. int w;
  2762. int h;
  2763. } common_modes[] = {
  2764. { "640x480", 640, 480},
  2765. { "800x600", 800, 600},
  2766. { "1024x768", 1024, 768},
  2767. { "1280x720", 1280, 720},
  2768. { "1280x800", 1280, 800},
  2769. {"1280x1024", 1280, 1024},
  2770. { "1440x900", 1440, 900},
  2771. {"1680x1050", 1680, 1050},
  2772. {"1600x1200", 1600, 1200},
  2773. {"1920x1080", 1920, 1080},
  2774. {"1920x1200", 1920, 1200}
  2775. };
  2776. n = ARRAY_SIZE(common_modes);
  2777. for (i = 0; i < n; i++) {
  2778. struct drm_display_mode *curmode = NULL;
  2779. bool mode_existed = false;
  2780. if (common_modes[i].w > native_mode->hdisplay ||
  2781. common_modes[i].h > native_mode->vdisplay ||
  2782. (common_modes[i].w == native_mode->hdisplay &&
  2783. common_modes[i].h == native_mode->vdisplay))
  2784. continue;
  2785. list_for_each_entry(curmode, &connector->probed_modes, head) {
  2786. if (common_modes[i].w == curmode->hdisplay &&
  2787. common_modes[i].h == curmode->vdisplay) {
  2788. mode_existed = true;
  2789. break;
  2790. }
  2791. }
  2792. if (mode_existed)
  2793. continue;
  2794. mode = amdgpu_dm_create_common_mode(encoder,
  2795. common_modes[i].name, common_modes[i].w,
  2796. common_modes[i].h);
  2797. drm_mode_probed_add(connector, mode);
  2798. amdgpu_connector->num_modes++;
  2799. }
  2800. }
  2801. static void amdgpu_dm_connector_ddc_get_modes(
  2802. struct drm_connector *connector,
  2803. struct edid *edid)
  2804. {
  2805. struct amdgpu_connector *amdgpu_connector =
  2806. to_amdgpu_connector(connector);
  2807. if (edid) {
  2808. /* empty probed_modes */
  2809. INIT_LIST_HEAD(&connector->probed_modes);
  2810. amdgpu_connector->num_modes =
  2811. drm_add_edid_modes(connector, edid);
  2812. drm_edid_to_eld(connector, edid);
  2813. amdgpu_dm_get_native_mode(connector);
  2814. } else
  2815. amdgpu_connector->num_modes = 0;
  2816. }
  2817. int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
  2818. {
  2819. const struct drm_connector_helper_funcs *helper =
  2820. connector->helper_private;
  2821. struct amdgpu_connector *amdgpu_connector =
  2822. to_amdgpu_connector(connector);
  2823. struct drm_encoder *encoder;
  2824. struct edid *edid = amdgpu_connector->edid;
  2825. encoder = helper->best_encoder(connector);
  2826. amdgpu_dm_connector_ddc_get_modes(connector, edid);
  2827. amdgpu_dm_connector_add_common_modes(encoder, connector);
  2828. return amdgpu_connector->num_modes;
  2829. }
  2830. void amdgpu_dm_connector_init_helper(
  2831. struct amdgpu_display_manager *dm,
  2832. struct amdgpu_connector *aconnector,
  2833. int connector_type,
  2834. struct dc_link *link,
  2835. int link_index)
  2836. {
  2837. struct amdgpu_device *adev = dm->ddev->dev_private;
  2838. aconnector->connector_id = link_index;
  2839. aconnector->dc_link = link;
  2840. aconnector->base.interlace_allowed = false;
  2841. aconnector->base.doublescan_allowed = false;
  2842. aconnector->base.stereo_allowed = false;
  2843. aconnector->base.dpms = DRM_MODE_DPMS_OFF;
  2844. aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
  2845. mutex_init(&aconnector->hpd_lock);
  2846. /* configure support HPD hot plug connector_>polled default value is 0
  2847. * which means HPD hot plug not supported
  2848. */
  2849. switch (connector_type) {
  2850. case DRM_MODE_CONNECTOR_HDMIA:
  2851. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  2852. break;
  2853. case DRM_MODE_CONNECTOR_DisplayPort:
  2854. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  2855. break;
  2856. case DRM_MODE_CONNECTOR_DVID:
  2857. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  2858. break;
  2859. default:
  2860. break;
  2861. }
  2862. drm_object_attach_property(&aconnector->base.base,
  2863. dm->ddev->mode_config.scaling_mode_property,
  2864. DRM_MODE_SCALE_NONE);
  2865. drm_object_attach_property(&aconnector->base.base,
  2866. adev->mode_info.underscan_property,
  2867. UNDERSCAN_OFF);
  2868. drm_object_attach_property(&aconnector->base.base,
  2869. adev->mode_info.underscan_hborder_property,
  2870. 0);
  2871. drm_object_attach_property(&aconnector->base.base,
  2872. adev->mode_info.underscan_vborder_property,
  2873. 0);
  2874. }
  2875. int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
  2876. struct i2c_msg *msgs, int num)
  2877. {
  2878. struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
  2879. struct ddc_service *ddc_service = i2c->ddc_service;
  2880. struct i2c_command cmd;
  2881. int i;
  2882. int result = -EIO;
  2883. cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
  2884. if (!cmd.payloads)
  2885. return result;
  2886. cmd.number_of_payloads = num;
  2887. cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
  2888. cmd.speed = 100;
  2889. for (i = 0; i < num; i++) {
  2890. cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
  2891. cmd.payloads[i].address = msgs[i].addr;
  2892. cmd.payloads[i].length = msgs[i].len;
  2893. cmd.payloads[i].data = msgs[i].buf;
  2894. }
  2895. if (dal_i2caux_submit_i2c_command(
  2896. ddc_service->ctx->i2caux,
  2897. ddc_service->ddc_pin,
  2898. &cmd))
  2899. result = num;
  2900. kfree(cmd.payloads);
  2901. return result;
  2902. }
  2903. u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
  2904. {
  2905. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  2906. }
  2907. static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
  2908. .master_xfer = amdgpu_dm_i2c_xfer,
  2909. .functionality = amdgpu_dm_i2c_func,
  2910. };
  2911. static struct amdgpu_i2c_adapter *create_i2c(
  2912. struct ddc_service *ddc_service,
  2913. int link_index,
  2914. int *res)
  2915. {
  2916. struct amdgpu_device *adev = ddc_service->ctx->driver_context;
  2917. struct amdgpu_i2c_adapter *i2c;
  2918. i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
  2919. i2c->base.owner = THIS_MODULE;
  2920. i2c->base.class = I2C_CLASS_DDC;
  2921. i2c->base.dev.parent = &adev->pdev->dev;
  2922. i2c->base.algo = &amdgpu_dm_i2c_algo;
  2923. snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
  2924. i2c_set_adapdata(&i2c->base, i2c);
  2925. i2c->ddc_service = ddc_service;
  2926. return i2c;
  2927. }
  2928. /* Note: this function assumes that dc_link_detect() was called for the
  2929. * dc_link which will be represented by this aconnector.
  2930. */
  2931. int amdgpu_dm_connector_init(
  2932. struct amdgpu_display_manager *dm,
  2933. struct amdgpu_connector *aconnector,
  2934. uint32_t link_index,
  2935. struct amdgpu_encoder *aencoder)
  2936. {
  2937. int res = 0;
  2938. int connector_type;
  2939. struct dc *dc = dm->dc;
  2940. struct dc_link *link = dc_get_link_at_index(dc, link_index);
  2941. struct amdgpu_i2c_adapter *i2c;
  2942. ((struct dc_link *)link)->priv = aconnector;
  2943. DRM_DEBUG_KMS("%s()\n", __func__);
  2944. i2c = create_i2c(link->ddc, link->link_index, &res);
  2945. aconnector->i2c = i2c;
  2946. res = i2c_add_adapter(&i2c->base);
  2947. if (res) {
  2948. DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
  2949. goto out_free;
  2950. }
  2951. connector_type = to_drm_connector_type(link->connector_signal);
  2952. res = drm_connector_init(
  2953. dm->ddev,
  2954. &aconnector->base,
  2955. &amdgpu_dm_connector_funcs,
  2956. connector_type);
  2957. if (res) {
  2958. DRM_ERROR("connector_init failed\n");
  2959. aconnector->connector_id = -1;
  2960. goto out_free;
  2961. }
  2962. drm_connector_helper_add(
  2963. &aconnector->base,
  2964. &amdgpu_dm_connector_helper_funcs);
  2965. amdgpu_dm_connector_init_helper(
  2966. dm,
  2967. aconnector,
  2968. connector_type,
  2969. link,
  2970. link_index);
  2971. drm_mode_connector_attach_encoder(
  2972. &aconnector->base, &aencoder->base);
  2973. drm_connector_register(&aconnector->base);
  2974. if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
  2975. || connector_type == DRM_MODE_CONNECTOR_eDP)
  2976. amdgpu_dm_initialize_dp_connector(dm, aconnector);
  2977. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
  2978. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  2979. /* NOTE: this currently will create backlight device even if a panel
  2980. * is not connected to the eDP/LVDS connector.
  2981. *
  2982. * This is less than ideal but we don't have sink information at this
  2983. * stage since detection happens after. We can't do detection earlier
  2984. * since MST detection needs connectors to be created first.
  2985. */
  2986. if (link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) {
  2987. /* Event if registration failed, we should continue with
  2988. * DM initialization because not having a backlight control
  2989. * is better then a black screen.
  2990. */
  2991. amdgpu_dm_register_backlight_device(dm);
  2992. if (dm->backlight_dev)
  2993. dm->backlight_link = link;
  2994. }
  2995. #endif
  2996. out_free:
  2997. if (res) {
  2998. kfree(i2c);
  2999. aconnector->i2c = NULL;
  3000. }
  3001. return res;
  3002. }
  3003. int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
  3004. {
  3005. switch (adev->mode_info.num_crtc) {
  3006. case 1:
  3007. return 0x1;
  3008. case 2:
  3009. return 0x3;
  3010. case 3:
  3011. return 0x7;
  3012. case 4:
  3013. return 0xf;
  3014. case 5:
  3015. return 0x1f;
  3016. case 6:
  3017. default:
  3018. return 0x3f;
  3019. }
  3020. }
  3021. int amdgpu_dm_encoder_init(
  3022. struct drm_device *dev,
  3023. struct amdgpu_encoder *aencoder,
  3024. uint32_t link_index)
  3025. {
  3026. struct amdgpu_device *adev = dev->dev_private;
  3027. int res = drm_encoder_init(dev,
  3028. &aencoder->base,
  3029. &amdgpu_dm_encoder_funcs,
  3030. DRM_MODE_ENCODER_TMDS,
  3031. NULL);
  3032. aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
  3033. if (!res)
  3034. aencoder->encoder_id = link_index;
  3035. else
  3036. aencoder->encoder_id = -1;
  3037. drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
  3038. return res;
  3039. }
  3040. static void manage_dm_interrupts(
  3041. struct amdgpu_device *adev,
  3042. struct amdgpu_crtc *acrtc,
  3043. bool enable)
  3044. {
  3045. /*
  3046. * this is not correct translation but will work as soon as VBLANK
  3047. * constant is the same as PFLIP
  3048. */
  3049. int irq_type =
  3050. amdgpu_crtc_idx_to_irq_type(
  3051. adev,
  3052. acrtc->crtc_id);
  3053. if (enable) {
  3054. drm_crtc_vblank_on(&acrtc->base);
  3055. amdgpu_irq_get(
  3056. adev,
  3057. &adev->pageflip_irq,
  3058. irq_type);
  3059. } else {
  3060. amdgpu_irq_put(
  3061. adev,
  3062. &adev->pageflip_irq,
  3063. irq_type);
  3064. drm_crtc_vblank_off(&acrtc->base);
  3065. }
  3066. }
  3067. static bool is_scaling_state_different(
  3068. const struct dm_connector_state *dm_state,
  3069. const struct dm_connector_state *old_dm_state)
  3070. {
  3071. if (dm_state->scaling != old_dm_state->scaling)
  3072. return true;
  3073. if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
  3074. if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
  3075. return true;
  3076. } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
  3077. if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
  3078. return true;
  3079. } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
  3080. dm_state->underscan_vborder != old_dm_state->underscan_vborder)
  3081. return true;
  3082. return false;
  3083. }
  3084. static void remove_stream(
  3085. struct amdgpu_device *adev,
  3086. struct amdgpu_crtc *acrtc,
  3087. struct dc_stream_state *stream)
  3088. {
  3089. /* this is the update mode case */
  3090. if (adev->dm.freesync_module)
  3091. mod_freesync_remove_stream(adev->dm.freesync_module, stream);
  3092. acrtc->otg_inst = -1;
  3093. acrtc->enabled = false;
  3094. }
  3095. static void handle_cursor_update(
  3096. struct drm_plane *plane,
  3097. struct drm_plane_state *old_plane_state)
  3098. {
  3099. if (!plane->state->fb && !old_plane_state->fb)
  3100. return;
  3101. /* Check if it's a cursor on/off update or just cursor move*/
  3102. if (plane->state->fb == old_plane_state->fb)
  3103. dm_crtc_cursor_move(
  3104. plane->state->crtc,
  3105. plane->state->crtc_x,
  3106. plane->state->crtc_y);
  3107. else {
  3108. struct amdgpu_framebuffer *afb =
  3109. to_amdgpu_framebuffer(plane->state->fb);
  3110. dm_crtc_cursor_set(
  3111. (!!plane->state->fb) ?
  3112. plane->state->crtc :
  3113. old_plane_state->crtc,
  3114. (!!plane->state->fb) ?
  3115. afb->address :
  3116. 0,
  3117. plane->state->crtc_w,
  3118. plane->state->crtc_h);
  3119. }
  3120. }
  3121. static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
  3122. {
  3123. assert_spin_locked(&acrtc->base.dev->event_lock);
  3124. WARN_ON(acrtc->event);
  3125. acrtc->event = acrtc->base.state->event;
  3126. /* Set the flip status */
  3127. acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
  3128. /* Mark this event as consumed */
  3129. acrtc->base.state->event = NULL;
  3130. DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
  3131. acrtc->crtc_id);
  3132. }
  3133. /*
  3134. * Executes flip
  3135. *
  3136. * Waits on all BO's fences and for proper vblank count
  3137. */
  3138. static void amdgpu_dm_do_flip(
  3139. struct drm_crtc *crtc,
  3140. struct drm_framebuffer *fb,
  3141. uint32_t target)
  3142. {
  3143. unsigned long flags;
  3144. uint32_t target_vblank;
  3145. int r, vpos, hpos;
  3146. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3147. struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
  3148. struct amdgpu_bo *abo = gem_to_amdgpu_bo(afb->obj);
  3149. struct amdgpu_device *adev = crtc->dev->dev_private;
  3150. bool async_flip = (acrtc->flip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
  3151. struct dc_flip_addrs addr = { {0} };
  3152. /* TODO eliminate or rename surface_update */
  3153. struct dc_surface_update surface_updates[1] = { {0} };
  3154. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
  3155. /* Prepare wait for target vblank early - before the fence-waits */
  3156. target_vblank = target - drm_crtc_vblank_count(crtc) +
  3157. amdgpu_get_vblank_counter_kms(crtc->dev, acrtc->crtc_id);
  3158. /* TODO This might fail and hence better not used, wait
  3159. * explicitly on fences instead
  3160. * and in general should be called for
  3161. * blocking commit to as per framework helpers
  3162. */
  3163. r = amdgpu_bo_reserve(abo, true);
  3164. if (unlikely(r != 0)) {
  3165. DRM_ERROR("failed to reserve buffer before flip\n");
  3166. WARN_ON(1);
  3167. }
  3168. /* Wait for all fences on this FB */
  3169. WARN_ON(reservation_object_wait_timeout_rcu(abo->tbo.resv, true, false,
  3170. MAX_SCHEDULE_TIMEOUT) < 0);
  3171. amdgpu_bo_unreserve(abo);
  3172. /* Wait until we're out of the vertical blank period before the one
  3173. * targeted by the flip
  3174. */
  3175. while ((acrtc->enabled &&
  3176. (amdgpu_get_crtc_scanoutpos(adev->ddev, acrtc->crtc_id, 0,
  3177. &vpos, &hpos, NULL, NULL,
  3178. &crtc->hwmode)
  3179. & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
  3180. (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
  3181. (int)(target_vblank -
  3182. amdgpu_get_vblank_counter_kms(adev->ddev, acrtc->crtc_id)) > 0)) {
  3183. usleep_range(1000, 1100);
  3184. }
  3185. /* Flip */
  3186. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  3187. /* update crtc fb */
  3188. crtc->primary->fb = fb;
  3189. WARN_ON(acrtc->pflip_status != AMDGPU_FLIP_NONE);
  3190. WARN_ON(!acrtc_state->stream);
  3191. addr.address.grph.addr.low_part = lower_32_bits(afb->address);
  3192. addr.address.grph.addr.high_part = upper_32_bits(afb->address);
  3193. addr.flip_immediate = async_flip;
  3194. if (acrtc->base.state->event)
  3195. prepare_flip_isr(acrtc);
  3196. surface_updates->surface = dc_stream_get_status(acrtc_state->stream)->plane_states[0];
  3197. surface_updates->flip_addr = &addr;
  3198. dc_update_planes_and_stream(adev->dm.dc, surface_updates, 1, acrtc_state->stream, NULL);
  3199. DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x \n",
  3200. __func__,
  3201. addr.address.grph.addr.high_part,
  3202. addr.address.grph.addr.low_part);
  3203. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  3204. }
  3205. static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
  3206. struct drm_device *dev,
  3207. struct amdgpu_display_manager *dm,
  3208. struct drm_crtc *pcrtc,
  3209. bool *wait_for_vblank)
  3210. {
  3211. uint32_t i;
  3212. struct drm_plane *plane;
  3213. struct drm_plane_state *old_plane_state;
  3214. struct dc_stream_state *dc_stream_attach;
  3215. struct dc_plane_state *plane_states_constructed[MAX_SURFACES];
  3216. struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
  3217. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(pcrtc->state);
  3218. int planes_count = 0;
  3219. unsigned long flags;
  3220. /* update planes when needed */
  3221. for_each_plane_in_state(state, plane, old_plane_state, i) {
  3222. struct drm_plane_state *plane_state = plane->state;
  3223. struct drm_crtc *crtc = plane_state->crtc;
  3224. struct drm_framebuffer *fb = plane_state->fb;
  3225. bool pflip_needed;
  3226. struct dm_plane_state *dm_plane_state = to_dm_plane_state(plane_state);
  3227. if (plane->type == DRM_PLANE_TYPE_CURSOR) {
  3228. handle_cursor_update(plane, old_plane_state);
  3229. continue;
  3230. }
  3231. if (!fb || !crtc || pcrtc != crtc || !crtc->state->active ||
  3232. (!crtc->state->planes_changed &&
  3233. !pcrtc->state->color_mgmt_changed))
  3234. continue;
  3235. pflip_needed = !state->allow_modeset;
  3236. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  3237. if (acrtc_attach->pflip_status != AMDGPU_FLIP_NONE) {
  3238. DRM_ERROR("%s: acrtc %d, already busy\n",
  3239. __func__,
  3240. acrtc_attach->crtc_id);
  3241. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  3242. /* In commit tail framework this cannot happen */
  3243. WARN_ON(1);
  3244. }
  3245. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  3246. if (!pflip_needed) {
  3247. WARN_ON(!dm_plane_state->dc_state);
  3248. plane_states_constructed[planes_count] = dm_plane_state->dc_state;
  3249. dc_stream_attach = acrtc_state->stream;
  3250. planes_count++;
  3251. } else if (crtc->state->planes_changed) {
  3252. /* Assume even ONE crtc with immediate flip means
  3253. * entire can't wait for VBLANK
  3254. * TODO Check if it's correct
  3255. */
  3256. *wait_for_vblank =
  3257. acrtc_attach->flip_flags & DRM_MODE_PAGE_FLIP_ASYNC ?
  3258. false : true;
  3259. /* TODO: Needs rework for multiplane flip */
  3260. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  3261. drm_crtc_vblank_get(crtc);
  3262. amdgpu_dm_do_flip(
  3263. crtc,
  3264. fb,
  3265. drm_crtc_vblank_count(crtc) + *wait_for_vblank);
  3266. /*TODO BUG remove ASAP in 4.12 to avoid race between worker and flip IOCTL */
  3267. /*clean up the flags for next usage*/
  3268. acrtc_attach->flip_flags = 0;
  3269. }
  3270. }
  3271. if (planes_count) {
  3272. unsigned long flags;
  3273. if (pcrtc->state->event) {
  3274. drm_crtc_vblank_get(pcrtc);
  3275. spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
  3276. prepare_flip_isr(acrtc_attach);
  3277. spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
  3278. }
  3279. if (false == dc_commit_planes_to_stream(dm->dc,
  3280. plane_states_constructed,
  3281. planes_count,
  3282. dc_stream_attach))
  3283. dm_error("%s: Failed to attach plane!\n", __func__);
  3284. } else {
  3285. /*TODO BUG Here should go disable planes on CRTC. */
  3286. }
  3287. }
  3288. int amdgpu_dm_atomic_commit(
  3289. struct drm_device *dev,
  3290. struct drm_atomic_state *state,
  3291. bool nonblock)
  3292. {
  3293. struct drm_crtc *crtc;
  3294. struct drm_crtc_state *new_state;
  3295. struct amdgpu_device *adev = dev->dev_private;
  3296. int i;
  3297. /*
  3298. * We evade vblanks and pflips on crtc that
  3299. * should be changed. We do it here to flush & disable
  3300. * interrupts before drm_swap_state is called in drm_atomic_helper_commit
  3301. * it will update crtc->dm_crtc_state->stream pointer which is used in
  3302. * the ISRs.
  3303. */
  3304. for_each_crtc_in_state(state, crtc, new_state, i) {
  3305. struct dm_crtc_state *old_acrtc_state = to_dm_crtc_state(crtc->state);
  3306. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3307. if (drm_atomic_crtc_needs_modeset(new_state) && old_acrtc_state->stream)
  3308. manage_dm_interrupts(adev, acrtc, false);
  3309. }
  3310. return drm_atomic_helper_commit(dev, state, nonblock);
  3311. /*TODO Handle EINTR, reenable IRQ*/
  3312. }
  3313. void amdgpu_dm_atomic_commit_tail(
  3314. struct drm_atomic_state *state)
  3315. {
  3316. struct drm_device *dev = state->dev;
  3317. struct amdgpu_device *adev = dev->dev_private;
  3318. struct amdgpu_display_manager *dm = &adev->dm;
  3319. struct dm_atomic_state *dm_state;
  3320. uint32_t i, j;
  3321. uint32_t new_crtcs_count = 0;
  3322. struct drm_crtc *crtc, *pcrtc;
  3323. struct drm_crtc_state *old_crtc_state;
  3324. struct amdgpu_crtc *new_crtcs[MAX_STREAMS];
  3325. struct dc_stream_state *new_stream = NULL;
  3326. unsigned long flags;
  3327. bool wait_for_vblank = true;
  3328. struct drm_connector *connector;
  3329. struct drm_connector_state *old_conn_state;
  3330. struct dm_crtc_state *old_acrtc_state, *new_acrtc_state;
  3331. drm_atomic_helper_update_legacy_modeset_state(dev, state);
  3332. dm_state = to_dm_atomic_state(state);
  3333. /* update changed items */
  3334. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  3335. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3336. struct drm_crtc_state *new_state = crtc->state;
  3337. new_acrtc_state = to_dm_crtc_state(new_state);
  3338. old_acrtc_state = to_dm_crtc_state(old_crtc_state);
  3339. DRM_DEBUG_KMS(
  3340. "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
  3341. "planes_changed:%d, mode_changed:%d,active_changed:%d,"
  3342. "connectors_changed:%d\n",
  3343. acrtc->crtc_id,
  3344. new_state->enable,
  3345. new_state->active,
  3346. new_state->planes_changed,
  3347. new_state->mode_changed,
  3348. new_state->active_changed,
  3349. new_state->connectors_changed);
  3350. /* handles headless hotplug case, updating new_state and
  3351. * aconnector as needed
  3352. */
  3353. if (modeset_required(new_state, new_acrtc_state->stream, old_acrtc_state->stream)) {
  3354. DRM_INFO("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
  3355. if (!new_acrtc_state->stream) {
  3356. /*
  3357. * this could happen because of issues with
  3358. * userspace notifications delivery.
  3359. * In this case userspace tries to set mode on
  3360. * display which is disconnect in fact.
  3361. * dc_sink in NULL in this case on aconnector.
  3362. * We expect reset mode will come soon.
  3363. *
  3364. * This can also happen when unplug is done
  3365. * during resume sequence ended
  3366. *
  3367. * In this case, we want to pretend we still
  3368. * have a sink to keep the pipe running so that
  3369. * hw state is consistent with the sw state
  3370. */
  3371. DRM_DEBUG_KMS("%s: Failed to create new stream for crtc %d\n",
  3372. __func__, acrtc->base.base.id);
  3373. continue;
  3374. }
  3375. if (old_acrtc_state->stream)
  3376. remove_stream(adev, acrtc, old_acrtc_state->stream);
  3377. /*
  3378. * this loop saves set mode crtcs
  3379. * we needed to enable vblanks once all
  3380. * resources acquired in dc after dc_commit_streams
  3381. */
  3382. /*TODO move all this into dm_crtc_state, get rid of
  3383. * new_crtcs array and use old and new atomic states
  3384. * instead
  3385. */
  3386. new_crtcs[new_crtcs_count] = acrtc;
  3387. new_crtcs_count++;
  3388. acrtc->enabled = true;
  3389. acrtc->hw_mode = crtc->state->mode;
  3390. crtc->hwmode = crtc->state->mode;
  3391. } else if (modereset_required(new_state)) {
  3392. DRM_INFO("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
  3393. /* i.e. reset mode */
  3394. if (old_acrtc_state->stream)
  3395. remove_stream(adev, acrtc, old_acrtc_state->stream);
  3396. }
  3397. } /* for_each_crtc_in_state() */
  3398. /*
  3399. * Add streams after required streams from new and replaced streams
  3400. * are removed from freesync module
  3401. */
  3402. if (adev->dm.freesync_module) {
  3403. for (i = 0; i < new_crtcs_count; i++) {
  3404. struct amdgpu_connector *aconnector = NULL;
  3405. new_acrtc_state = to_dm_crtc_state(new_crtcs[i]->base.state);
  3406. new_stream = new_acrtc_state->stream;
  3407. aconnector =
  3408. amdgpu_dm_find_first_crct_matching_connector(
  3409. state,
  3410. &new_crtcs[i]->base,
  3411. false);
  3412. if (!aconnector) {
  3413. DRM_INFO("Atomic commit: Failed to find connector for acrtc id:%d "
  3414. "skipping freesync init\n",
  3415. new_crtcs[i]->crtc_id);
  3416. continue;
  3417. }
  3418. mod_freesync_add_stream(adev->dm.freesync_module,
  3419. new_stream, &aconnector->caps);
  3420. }
  3421. }
  3422. if (dm_state->context)
  3423. WARN_ON(!dc_commit_context(dm->dc, dm_state->context));
  3424. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3425. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3426. new_acrtc_state = to_dm_crtc_state(crtc->state);
  3427. if (new_acrtc_state->stream != NULL) {
  3428. const struct dc_stream_status *status =
  3429. dc_stream_get_status(new_acrtc_state->stream);
  3430. if (!status)
  3431. DC_ERR("got no status for stream %p on acrtc%p\n", new_acrtc_state->stream, acrtc);
  3432. else
  3433. acrtc->otg_inst = status->primary_otg_inst;
  3434. }
  3435. }
  3436. /* Handle scaling and undersacn changes*/
  3437. for_each_connector_in_state(state, connector, old_conn_state, i) {
  3438. struct amdgpu_connector *aconnector = to_amdgpu_connector(connector);
  3439. struct dm_connector_state *con_new_state =
  3440. to_dm_connector_state(aconnector->base.state);
  3441. struct dm_connector_state *con_old_state =
  3442. to_dm_connector_state(old_conn_state);
  3443. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(con_new_state->base.crtc);
  3444. struct dc_stream_status *status = NULL;
  3445. /* Skip any modesets/resets */
  3446. if (!acrtc || drm_atomic_crtc_needs_modeset(acrtc->base.state))
  3447. continue;
  3448. /* Skip any thing not scale or underscan changes */
  3449. if (!is_scaling_state_different(con_new_state, con_old_state))
  3450. continue;
  3451. new_acrtc_state = to_dm_crtc_state(acrtc->base.state);
  3452. update_stream_scaling_settings(&con_new_state->base.crtc->mode,
  3453. con_new_state, (struct dc_stream_state *)new_acrtc_state->stream);
  3454. status = dc_stream_get_status(new_acrtc_state->stream);
  3455. WARN_ON(!status);
  3456. WARN_ON(!status->plane_count);
  3457. if (!new_acrtc_state->stream)
  3458. continue;
  3459. /*TODO How it works with MPO ?*/
  3460. if (!dc_commit_planes_to_stream(
  3461. dm->dc,
  3462. status->plane_states,
  3463. status->plane_count,
  3464. new_acrtc_state->stream))
  3465. dm_error("%s: Failed to update stream scaling!\n", __func__);
  3466. }
  3467. for (i = 0; i < new_crtcs_count; i++) {
  3468. /*
  3469. * loop to enable interrupts on newly arrived crtc
  3470. */
  3471. struct amdgpu_crtc *acrtc = new_crtcs[i];
  3472. new_acrtc_state = to_dm_crtc_state(acrtc->base.state);
  3473. if (adev->dm.freesync_module)
  3474. mod_freesync_notify_mode_change(
  3475. adev->dm.freesync_module, &new_acrtc_state->stream, 1);
  3476. manage_dm_interrupts(adev, acrtc, true);
  3477. }
  3478. /* update planes when needed per crtc*/
  3479. for_each_crtc_in_state(state, pcrtc, old_crtc_state, j) {
  3480. new_acrtc_state = to_dm_crtc_state(pcrtc->state);
  3481. if (new_acrtc_state->stream)
  3482. amdgpu_dm_commit_planes(state, dev, dm, pcrtc, &wait_for_vblank);
  3483. }
  3484. /*
  3485. * send vblank event on all events not handled in flip and
  3486. * mark consumed event for drm_atomic_helper_commit_hw_done
  3487. */
  3488. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  3489. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  3490. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3491. if (acrtc->base.state->event)
  3492. drm_send_event_locked(dev, &crtc->state->event->base);
  3493. acrtc->base.state->event = NULL;
  3494. }
  3495. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  3496. /* Signal HW programming completion */
  3497. drm_atomic_helper_commit_hw_done(state);
  3498. if (wait_for_vblank)
  3499. drm_atomic_helper_wait_for_vblanks(dev, state);
  3500. drm_atomic_helper_cleanup_planes(dev, state);
  3501. }
  3502. static int dm_force_atomic_commit(struct drm_connector *connector)
  3503. {
  3504. int ret = 0;
  3505. struct drm_device *ddev = connector->dev;
  3506. struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
  3507. struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
  3508. struct drm_plane *plane = disconnected_acrtc->base.primary;
  3509. struct drm_connector_state *conn_state;
  3510. struct drm_crtc_state *crtc_state;
  3511. struct drm_plane_state *plane_state;
  3512. if (!state)
  3513. return -ENOMEM;
  3514. state->acquire_ctx = ddev->mode_config.acquire_ctx;
  3515. /* Construct an atomic state to restore previous display setting */
  3516. /*
  3517. * Attach connectors to drm_atomic_state
  3518. */
  3519. conn_state = drm_atomic_get_connector_state(state, connector);
  3520. ret = PTR_ERR_OR_ZERO(conn_state);
  3521. if (ret)
  3522. goto err;
  3523. /* Attach crtc to drm_atomic_state*/
  3524. crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
  3525. ret = PTR_ERR_OR_ZERO(crtc_state);
  3526. if (ret)
  3527. goto err;
  3528. /* force a restore */
  3529. crtc_state->mode_changed = true;
  3530. /* Attach plane to drm_atomic_state */
  3531. plane_state = drm_atomic_get_plane_state(state, plane);
  3532. ret = PTR_ERR_OR_ZERO(plane_state);
  3533. if (ret)
  3534. goto err;
  3535. /* Call commit internally with the state we just constructed */
  3536. ret = drm_atomic_commit(state);
  3537. if (!ret)
  3538. return 0;
  3539. err:
  3540. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3541. drm_atomic_state_put(state);
  3542. return ret;
  3543. }
  3544. /*
  3545. * This functions handle all cases when set mode does not come upon hotplug.
  3546. * This include when the same display is unplugged then plugged back into the
  3547. * same port and when we are running without usermode desktop manager supprot
  3548. */
  3549. void dm_restore_drm_connector_state(struct drm_device *dev, struct drm_connector *connector)
  3550. {
  3551. struct amdgpu_connector *aconnector = to_amdgpu_connector(connector);
  3552. struct amdgpu_crtc *disconnected_acrtc;
  3553. struct dm_crtc_state *acrtc_state;
  3554. if (!aconnector->dc_sink || !connector->state || !connector->encoder)
  3555. return;
  3556. disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
  3557. acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
  3558. if (!disconnected_acrtc || !acrtc_state->stream)
  3559. return;
  3560. /*
  3561. * If the previous sink is not released and different from the current,
  3562. * we deduce we are in a state where we can not rely on usermode call
  3563. * to turn on the display, so we do it here
  3564. */
  3565. if (acrtc_state->stream->sink != aconnector->dc_sink)
  3566. dm_force_atomic_commit(&aconnector->base);
  3567. }
  3568. static uint32_t add_val_sets_plane(
  3569. struct dc_validation_set *val_sets,
  3570. uint32_t set_count,
  3571. const struct dc_stream_state *stream,
  3572. struct dc_plane_state *plane_state)
  3573. {
  3574. uint32_t i = 0, j = 0;
  3575. while (i < set_count) {
  3576. if (val_sets[i].stream == stream) {
  3577. while (val_sets[i].plane_states[j])
  3578. j++;
  3579. break;
  3580. }
  3581. ++i;
  3582. }
  3583. val_sets[i].plane_states[j] = plane_state;
  3584. val_sets[i].plane_count++;
  3585. return val_sets[i].plane_count;
  3586. }
  3587. static uint32_t update_in_val_sets_stream(
  3588. struct dc_validation_set *val_sets,
  3589. uint32_t set_count,
  3590. struct dc_stream_state *old_stream,
  3591. struct dc_stream_state *new_stream,
  3592. struct drm_crtc *crtc)
  3593. {
  3594. uint32_t i = 0;
  3595. while (i < set_count) {
  3596. if (val_sets[i].stream == old_stream)
  3597. break;
  3598. ++i;
  3599. }
  3600. val_sets[i].stream = new_stream;
  3601. if (i == set_count)
  3602. /* nothing found. add new one to the end */
  3603. return set_count + 1;
  3604. return set_count;
  3605. }
  3606. static uint32_t remove_from_val_sets(
  3607. struct dc_validation_set *val_sets,
  3608. uint32_t set_count,
  3609. const struct dc_stream_state *stream)
  3610. {
  3611. int i;
  3612. for (i = 0; i < set_count; i++)
  3613. if (val_sets[i].stream == stream)
  3614. break;
  3615. if (i == set_count) {
  3616. /* nothing found */
  3617. return set_count;
  3618. }
  3619. set_count--;
  3620. for (; i < set_count; i++)
  3621. val_sets[i] = val_sets[i + 1];
  3622. return set_count;
  3623. }
  3624. /*`
  3625. * Grabs all modesetting locks to serialize against any blocking commits,
  3626. * Waits for completion of all non blocking commits.
  3627. */
  3628. static int do_aquire_global_lock(
  3629. struct drm_device *dev,
  3630. struct drm_atomic_state *state)
  3631. {
  3632. struct drm_crtc *crtc;
  3633. struct drm_crtc_commit *commit;
  3634. long ret;
  3635. /* Adding all modeset locks to aquire_ctx will
  3636. * ensure that when the framework release it the
  3637. * extra locks we are locking here will get released to
  3638. */
  3639. ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
  3640. if (ret)
  3641. return ret;
  3642. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3643. spin_lock(&crtc->commit_lock);
  3644. commit = list_first_entry_or_null(&crtc->commit_list,
  3645. struct drm_crtc_commit, commit_entry);
  3646. if (commit)
  3647. drm_crtc_commit_get(commit);
  3648. spin_unlock(&crtc->commit_lock);
  3649. if (!commit)
  3650. continue;
  3651. /* Make sure all pending HW programming completed and
  3652. * page flips done
  3653. */
  3654. ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
  3655. if (ret > 0)
  3656. ret = wait_for_completion_interruptible_timeout(
  3657. &commit->flip_done, 10*HZ);
  3658. if (ret == 0)
  3659. DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
  3660. "timed out\n", crtc->base.id, crtc->name);
  3661. drm_crtc_commit_put(commit);
  3662. }
  3663. return ret < 0 ? ret : 0;
  3664. }
  3665. int amdgpu_dm_atomic_check(struct drm_device *dev,
  3666. struct drm_atomic_state *state)
  3667. {
  3668. struct dm_atomic_state *dm_state;
  3669. struct drm_crtc *crtc;
  3670. struct drm_crtc_state *crtc_state;
  3671. struct drm_plane *plane;
  3672. struct drm_plane_state *plane_state;
  3673. int i, j;
  3674. int ret;
  3675. struct amdgpu_device *adev = dev->dev_private;
  3676. struct dc *dc = adev->dm.dc;
  3677. struct drm_connector *connector;
  3678. struct drm_connector_state *conn_state;
  3679. int set_count;
  3680. struct dc_validation_set set[MAX_STREAMS] = { { 0 } };
  3681. struct dm_crtc_state *old_acrtc_state, *new_acrtc_state;
  3682. /*
  3683. * This bool will be set for true for any modeset/reset
  3684. * or plane update which implies non fast surface update.
  3685. */
  3686. bool lock_and_validation_needed = false;
  3687. ret = drm_atomic_helper_check_modeset(dev, state);
  3688. if (ret) {
  3689. DRM_ERROR("Atomic state validation failed with error :%d !\n", ret);
  3690. return ret;
  3691. }
  3692. dm_state = to_dm_atomic_state(state);
  3693. /* copy existing configuration */
  3694. set_count = 0;
  3695. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3696. old_acrtc_state = to_dm_crtc_state(crtc->state);
  3697. if (old_acrtc_state->stream) {
  3698. dc_stream_retain(old_acrtc_state->stream);
  3699. set[set_count].stream = old_acrtc_state->stream;
  3700. ++set_count;
  3701. }
  3702. }
  3703. /*TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set */
  3704. /* update changed items */
  3705. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3706. struct amdgpu_crtc *acrtc = NULL;
  3707. struct amdgpu_connector *aconnector = NULL;
  3708. struct dc_stream_state *new_stream = NULL;
  3709. struct drm_connector_state *conn_state = NULL;
  3710. struct dm_connector_state *dm_conn_state = NULL;
  3711. old_acrtc_state = to_dm_crtc_state(crtc->state);
  3712. new_acrtc_state = to_dm_crtc_state(crtc_state);
  3713. acrtc = to_amdgpu_crtc(crtc);
  3714. aconnector = amdgpu_dm_find_first_crct_matching_connector(state, crtc, true);
  3715. DRM_DEBUG_KMS(
  3716. "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
  3717. "planes_changed:%d, mode_changed:%d,active_changed:%d,"
  3718. "connectors_changed:%d\n",
  3719. acrtc->crtc_id,
  3720. crtc_state->enable,
  3721. crtc_state->active,
  3722. crtc_state->planes_changed,
  3723. crtc_state->mode_changed,
  3724. crtc_state->active_changed,
  3725. crtc_state->connectors_changed);
  3726. if (modereset_required(crtc_state)) {
  3727. /* i.e. reset mode */
  3728. if (new_acrtc_state->stream) {
  3729. set_count = remove_from_val_sets(
  3730. set,
  3731. set_count,
  3732. new_acrtc_state->stream);
  3733. dc_stream_release(new_acrtc_state->stream);
  3734. new_acrtc_state->stream = NULL;
  3735. lock_and_validation_needed = true;
  3736. }
  3737. } else {
  3738. if (aconnector) {
  3739. conn_state = drm_atomic_get_connector_state(state,
  3740. &aconnector->base);
  3741. if (IS_ERR(conn_state)) {
  3742. ret = PTR_ERR_OR_ZERO(conn_state);
  3743. goto fail;
  3744. }
  3745. dm_conn_state = to_dm_connector_state(conn_state);
  3746. new_stream = create_stream_for_sink(aconnector,
  3747. &crtc_state->mode,
  3748. dm_conn_state);
  3749. if (!new_stream) {
  3750. DRM_DEBUG_KMS("%s: Failed to create new stream for crtc %d\n",
  3751. __func__, acrtc->base.base.id);
  3752. break;
  3753. }
  3754. }
  3755. if (modeset_required(crtc_state, new_stream,
  3756. old_acrtc_state->stream)) {
  3757. /*
  3758. * we can have no stream on ACTION_SET if a display
  3759. * was disconnected during S3, in this case it not and
  3760. * error, the OS will be updated after detection, and
  3761. * do the right thing on next atomic commit
  3762. */
  3763. if (new_acrtc_state->stream)
  3764. dc_stream_release(new_acrtc_state->stream);
  3765. new_acrtc_state->stream = new_stream;
  3766. set_count = update_in_val_sets_stream(
  3767. set,
  3768. set_count,
  3769. old_acrtc_state->stream,
  3770. new_acrtc_state->stream,
  3771. crtc);
  3772. lock_and_validation_needed = true;
  3773. } else {
  3774. /*
  3775. * The new stream is unused, so we release it
  3776. */
  3777. if (new_stream)
  3778. dc_stream_release(new_stream);
  3779. }
  3780. }
  3781. /*
  3782. * Hack: Commit needs planes right now, specifically for gamma
  3783. * TODO rework commit to check CRTC for gamma change
  3784. */
  3785. if (crtc_state->color_mgmt_changed) {
  3786. ret = drm_atomic_add_affected_planes(state, crtc);
  3787. if (ret)
  3788. goto fail;
  3789. }
  3790. }
  3791. /* Check scaling and undersacn changes*/
  3792. /*TODO Removed scaling changes validation due to inability to commit
  3793. * new stream into context w\o causing full reset. Need to
  3794. * decide how to handle.
  3795. */
  3796. for_each_connector_in_state(state, connector, conn_state, i) {
  3797. struct amdgpu_connector *aconnector = to_amdgpu_connector(connector);
  3798. struct dm_connector_state *con_old_state =
  3799. to_dm_connector_state(aconnector->base.state);
  3800. struct dm_connector_state *con_new_state =
  3801. to_dm_connector_state(conn_state);
  3802. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(con_new_state->base.crtc);
  3803. /* Skip any modesets/resets */
  3804. if (!acrtc || drm_atomic_crtc_needs_modeset(acrtc->base.state))
  3805. continue;
  3806. /* Skip any thing not scale or underscan changes */
  3807. if (!is_scaling_state_different(con_new_state, con_old_state))
  3808. continue;
  3809. lock_and_validation_needed = true;
  3810. }
  3811. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3812. new_acrtc_state = to_dm_crtc_state(crtc_state);
  3813. for_each_plane_in_state(state, plane, plane_state, j) {
  3814. struct drm_crtc *plane_crtc = plane_state->crtc;
  3815. struct drm_framebuffer *fb = plane_state->fb;
  3816. bool pflip_needed;
  3817. struct dm_plane_state *dm_plane_state = to_dm_plane_state(plane_state);
  3818. /*TODO Implement atomic check for cursor plane */
  3819. if (plane->type == DRM_PLANE_TYPE_CURSOR)
  3820. continue;
  3821. if (!fb || !plane_crtc || crtc != plane_crtc || !crtc_state->active)
  3822. continue;
  3823. WARN_ON(!new_acrtc_state->stream);
  3824. pflip_needed = !state->allow_modeset;
  3825. if (!pflip_needed) {
  3826. struct dc_plane_state *dc_plane_state;
  3827. dc_plane_state = dc_create_plane_state(dc);
  3828. ret = fill_plane_attributes(
  3829. plane_crtc->dev->dev_private,
  3830. dc_plane_state,
  3831. plane_state,
  3832. crtc_state,
  3833. false);
  3834. if (ret)
  3835. goto fail;
  3836. if (dm_plane_state->dc_state)
  3837. dc_plane_state_release(dm_plane_state->dc_state);
  3838. dm_plane_state->dc_state = dc_plane_state;
  3839. add_val_sets_plane(set,
  3840. set_count,
  3841. new_acrtc_state->stream,
  3842. dc_plane_state);
  3843. lock_and_validation_needed = true;
  3844. }
  3845. }
  3846. }
  3847. /* Run this here since we want to validate the streams we created */
  3848. ret = drm_atomic_helper_check_planes(dev, state);
  3849. if (ret)
  3850. goto fail;
  3851. /*
  3852. * For full updates case when
  3853. * removing/adding/updating streams on once CRTC while flipping
  3854. * on another CRTC,
  3855. * acquiring global lock will guarantee that any such full
  3856. * update commit
  3857. * will wait for completion of any outstanding flip using DRMs
  3858. * synchronization events.
  3859. */
  3860. if (lock_and_validation_needed) {
  3861. ret = do_aquire_global_lock(dev, state);
  3862. if (ret)
  3863. goto fail;
  3864. WARN_ON(dm_state->context);
  3865. dm_state->context = dc_get_validate_context(dc, set, set_count);
  3866. if (!dm_state->context) {
  3867. ret = -EINVAL;
  3868. goto fail;
  3869. }
  3870. }
  3871. /* Must be success */
  3872. WARN_ON(ret);
  3873. return ret;
  3874. fail:
  3875. if (ret == -EDEADLK)
  3876. DRM_DEBUG_KMS("Atomic check stopped due to to deadlock.\n");
  3877. else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
  3878. DRM_DEBUG_KMS("Atomic check stopped due to to signal.\n");
  3879. else
  3880. DRM_ERROR("Atomic check failed with err: %d .\n", ret);
  3881. return ret;
  3882. }
  3883. static bool is_dp_capable_without_timing_msa(
  3884. struct dc *dc,
  3885. struct amdgpu_connector *amdgpu_connector)
  3886. {
  3887. uint8_t dpcd_data;
  3888. bool capable = false;
  3889. if (amdgpu_connector->dc_link &&
  3890. dm_helpers_dp_read_dpcd(
  3891. NULL,
  3892. amdgpu_connector->dc_link,
  3893. DP_DOWN_STREAM_PORT_COUNT,
  3894. &dpcd_data,
  3895. sizeof(dpcd_data))) {
  3896. capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
  3897. }
  3898. return capable;
  3899. }
  3900. void amdgpu_dm_add_sink_to_freesync_module(
  3901. struct drm_connector *connector,
  3902. struct edid *edid)
  3903. {
  3904. int i;
  3905. uint64_t val_capable;
  3906. bool edid_check_required;
  3907. struct detailed_timing *timing;
  3908. struct detailed_non_pixel *data;
  3909. struct detailed_data_monitor_range *range;
  3910. struct amdgpu_connector *amdgpu_connector =
  3911. to_amdgpu_connector(connector);
  3912. struct drm_device *dev = connector->dev;
  3913. struct amdgpu_device *adev = dev->dev_private;
  3914. edid_check_required = false;
  3915. if (!amdgpu_connector->dc_sink) {
  3916. DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
  3917. return;
  3918. }
  3919. if (!adev->dm.freesync_module)
  3920. return;
  3921. /*
  3922. * if edid non zero restrict freesync only for dp and edp
  3923. */
  3924. if (edid) {
  3925. if (amdgpu_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
  3926. || amdgpu_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
  3927. edid_check_required = is_dp_capable_without_timing_msa(
  3928. adev->dm.dc,
  3929. amdgpu_connector);
  3930. }
  3931. }
  3932. val_capable = 0;
  3933. if (edid_check_required == true && (edid->version > 1 ||
  3934. (edid->version == 1 && edid->revision > 1))) {
  3935. for (i = 0; i < 4; i++) {
  3936. timing = &edid->detailed_timings[i];
  3937. data = &timing->data.other_data;
  3938. range = &data->data.range;
  3939. /*
  3940. * Check if monitor has continuous frequency mode
  3941. */
  3942. if (data->type != EDID_DETAIL_MONITOR_RANGE)
  3943. continue;
  3944. /*
  3945. * Check for flag range limits only. If flag == 1 then
  3946. * no additional timing information provided.
  3947. * Default GTF, GTF Secondary curve and CVT are not
  3948. * supported
  3949. */
  3950. if (range->flags != 1)
  3951. continue;
  3952. amdgpu_connector->min_vfreq = range->min_vfreq;
  3953. amdgpu_connector->max_vfreq = range->max_vfreq;
  3954. amdgpu_connector->pixel_clock_mhz =
  3955. range->pixel_clock_mhz * 10;
  3956. break;
  3957. }
  3958. if (amdgpu_connector->max_vfreq -
  3959. amdgpu_connector->min_vfreq > 10) {
  3960. amdgpu_connector->caps.supported = true;
  3961. amdgpu_connector->caps.min_refresh_in_micro_hz =
  3962. amdgpu_connector->min_vfreq * 1000000;
  3963. amdgpu_connector->caps.max_refresh_in_micro_hz =
  3964. amdgpu_connector->max_vfreq * 1000000;
  3965. val_capable = 1;
  3966. }
  3967. }
  3968. /*
  3969. * TODO figure out how to notify user-mode or DRM of freesync caps
  3970. * once we figure out how to deal with freesync in an upstreamable
  3971. * fashion
  3972. */
  3973. }
  3974. void amdgpu_dm_remove_sink_from_freesync_module(
  3975. struct drm_connector *connector)
  3976. {
  3977. /*
  3978. * TODO fill in once we figure out how to deal with freesync in
  3979. * an upstreamable fashion
  3980. */
  3981. }