amdgpu_dm.c 146 KB

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