amdgpu_dm.c 140 KB

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