amdgpu_dm.c 154 KB

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