amdgpu_dm.c 141 KB

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