amdgpu_dm.c 148 KB

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