amdgpu_dm.c 131 KB

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