amdgpu_dm.c 137 KB

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