amdgpu_dm.c 142 KB

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