amdgpu_dm.c 150 KB

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