amdgpu_dm.c 137 KB

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