amdgpu_dm.c 132 KB

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