amdgpu_dm.c 154 KB

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