amdgpu_dm.c 154 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641
  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. uint32_t bpc = connector->display_info.bpc;
  1924. switch (bpc) {
  1925. case 0:
  1926. /*
  1927. * Temporary Work around, DRM doesn't parse color depth for
  1928. * EDID revision before 1.4
  1929. * TODO: Fix edid parsing
  1930. */
  1931. return COLOR_DEPTH_888;
  1932. case 6:
  1933. return COLOR_DEPTH_666;
  1934. case 8:
  1935. return COLOR_DEPTH_888;
  1936. case 10:
  1937. return COLOR_DEPTH_101010;
  1938. case 12:
  1939. return COLOR_DEPTH_121212;
  1940. case 14:
  1941. return COLOR_DEPTH_141414;
  1942. case 16:
  1943. return COLOR_DEPTH_161616;
  1944. default:
  1945. return COLOR_DEPTH_UNDEFINED;
  1946. }
  1947. }
  1948. static enum dc_aspect_ratio
  1949. get_aspect_ratio(const struct drm_display_mode *mode_in)
  1950. {
  1951. /* 1-1 mapping, since both enums follow the HDMI spec. */
  1952. return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
  1953. }
  1954. static enum dc_color_space
  1955. get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
  1956. {
  1957. enum dc_color_space color_space = COLOR_SPACE_SRGB;
  1958. switch (dc_crtc_timing->pixel_encoding) {
  1959. case PIXEL_ENCODING_YCBCR422:
  1960. case PIXEL_ENCODING_YCBCR444:
  1961. case PIXEL_ENCODING_YCBCR420:
  1962. {
  1963. /*
  1964. * 27030khz is the separation point between HDTV and SDTV
  1965. * according to HDMI spec, we use YCbCr709 and YCbCr601
  1966. * respectively
  1967. */
  1968. if (dc_crtc_timing->pix_clk_khz > 27030) {
  1969. if (dc_crtc_timing->flags.Y_ONLY)
  1970. color_space =
  1971. COLOR_SPACE_YCBCR709_LIMITED;
  1972. else
  1973. color_space = COLOR_SPACE_YCBCR709;
  1974. } else {
  1975. if (dc_crtc_timing->flags.Y_ONLY)
  1976. color_space =
  1977. COLOR_SPACE_YCBCR601_LIMITED;
  1978. else
  1979. color_space = COLOR_SPACE_YCBCR601;
  1980. }
  1981. }
  1982. break;
  1983. case PIXEL_ENCODING_RGB:
  1984. color_space = COLOR_SPACE_SRGB;
  1985. break;
  1986. default:
  1987. WARN_ON(1);
  1988. break;
  1989. }
  1990. return color_space;
  1991. }
  1992. static void reduce_mode_colour_depth(struct dc_crtc_timing *timing_out)
  1993. {
  1994. if (timing_out->display_color_depth <= COLOR_DEPTH_888)
  1995. return;
  1996. timing_out->display_color_depth--;
  1997. }
  1998. static void adjust_colour_depth_from_display_info(struct dc_crtc_timing *timing_out,
  1999. const struct drm_display_info *info)
  2000. {
  2001. int normalized_clk;
  2002. if (timing_out->display_color_depth <= COLOR_DEPTH_888)
  2003. return;
  2004. do {
  2005. normalized_clk = timing_out->pix_clk_khz;
  2006. /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
  2007. if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
  2008. normalized_clk /= 2;
  2009. /* Adjusting pix clock following on HDMI spec based on colour depth */
  2010. switch (timing_out->display_color_depth) {
  2011. case COLOR_DEPTH_101010:
  2012. normalized_clk = (normalized_clk * 30) / 24;
  2013. break;
  2014. case COLOR_DEPTH_121212:
  2015. normalized_clk = (normalized_clk * 36) / 24;
  2016. break;
  2017. case COLOR_DEPTH_161616:
  2018. normalized_clk = (normalized_clk * 48) / 24;
  2019. break;
  2020. default:
  2021. return;
  2022. }
  2023. if (normalized_clk <= info->max_tmds_clock)
  2024. return;
  2025. reduce_mode_colour_depth(timing_out);
  2026. } while (timing_out->display_color_depth > COLOR_DEPTH_888);
  2027. }
  2028. static void
  2029. fill_stream_properties_from_drm_display_mode(struct dc_stream_state *stream,
  2030. const struct drm_display_mode *mode_in,
  2031. const struct drm_connector *connector)
  2032. {
  2033. struct dc_crtc_timing *timing_out = &stream->timing;
  2034. const struct drm_display_info *info = &connector->display_info;
  2035. memset(timing_out, 0, sizeof(struct dc_crtc_timing));
  2036. timing_out->h_border_left = 0;
  2037. timing_out->h_border_right = 0;
  2038. timing_out->v_border_top = 0;
  2039. timing_out->v_border_bottom = 0;
  2040. /* TODO: un-hardcode */
  2041. if (drm_mode_is_420_only(info, mode_in)
  2042. && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
  2043. timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
  2044. else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
  2045. && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
  2046. timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
  2047. else
  2048. timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
  2049. timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
  2050. timing_out->display_color_depth = convert_color_depth_from_display_info(
  2051. connector);
  2052. timing_out->scan_type = SCANNING_TYPE_NODATA;
  2053. timing_out->hdmi_vic = 0;
  2054. timing_out->vic = drm_match_cea_mode(mode_in);
  2055. timing_out->h_addressable = mode_in->crtc_hdisplay;
  2056. timing_out->h_total = mode_in->crtc_htotal;
  2057. timing_out->h_sync_width =
  2058. mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
  2059. timing_out->h_front_porch =
  2060. mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
  2061. timing_out->v_total = mode_in->crtc_vtotal;
  2062. timing_out->v_addressable = mode_in->crtc_vdisplay;
  2063. timing_out->v_front_porch =
  2064. mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
  2065. timing_out->v_sync_width =
  2066. mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
  2067. timing_out->pix_clk_khz = mode_in->crtc_clock;
  2068. timing_out->aspect_ratio = get_aspect_ratio(mode_in);
  2069. if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
  2070. timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
  2071. if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
  2072. timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
  2073. stream->output_color_space = get_output_color_space(timing_out);
  2074. stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
  2075. stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
  2076. if (stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
  2077. adjust_colour_depth_from_display_info(timing_out, info);
  2078. }
  2079. static void fill_audio_info(struct audio_info *audio_info,
  2080. const struct drm_connector *drm_connector,
  2081. const struct dc_sink *dc_sink)
  2082. {
  2083. int i = 0;
  2084. int cea_revision = 0;
  2085. const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
  2086. audio_info->manufacture_id = edid_caps->manufacturer_id;
  2087. audio_info->product_id = edid_caps->product_id;
  2088. cea_revision = drm_connector->display_info.cea_rev;
  2089. strncpy(audio_info->display_name,
  2090. edid_caps->display_name,
  2091. AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS - 1);
  2092. if (cea_revision >= 3) {
  2093. audio_info->mode_count = edid_caps->audio_mode_count;
  2094. for (i = 0; i < audio_info->mode_count; ++i) {
  2095. audio_info->modes[i].format_code =
  2096. (enum audio_format_code)
  2097. (edid_caps->audio_modes[i].format_code);
  2098. audio_info->modes[i].channel_count =
  2099. edid_caps->audio_modes[i].channel_count;
  2100. audio_info->modes[i].sample_rates.all =
  2101. edid_caps->audio_modes[i].sample_rate;
  2102. audio_info->modes[i].sample_size =
  2103. edid_caps->audio_modes[i].sample_size;
  2104. }
  2105. }
  2106. audio_info->flags.all = edid_caps->speaker_flags;
  2107. /* TODO: We only check for the progressive mode, check for interlace mode too */
  2108. if (drm_connector->latency_present[0]) {
  2109. audio_info->video_latency = drm_connector->video_latency[0];
  2110. audio_info->audio_latency = drm_connector->audio_latency[0];
  2111. }
  2112. /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
  2113. }
  2114. static void
  2115. copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
  2116. struct drm_display_mode *dst_mode)
  2117. {
  2118. dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
  2119. dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
  2120. dst_mode->crtc_clock = src_mode->crtc_clock;
  2121. dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
  2122. dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
  2123. dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
  2124. dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
  2125. dst_mode->crtc_htotal = src_mode->crtc_htotal;
  2126. dst_mode->crtc_hskew = src_mode->crtc_hskew;
  2127. dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
  2128. dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
  2129. dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
  2130. dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
  2131. dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
  2132. }
  2133. static void
  2134. decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
  2135. const struct drm_display_mode *native_mode,
  2136. bool scale_enabled)
  2137. {
  2138. if (scale_enabled) {
  2139. copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
  2140. } else if (native_mode->clock == drm_mode->clock &&
  2141. native_mode->htotal == drm_mode->htotal &&
  2142. native_mode->vtotal == drm_mode->vtotal) {
  2143. copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
  2144. } else {
  2145. /* no scaling nor amdgpu inserted, no need to patch */
  2146. }
  2147. }
  2148. static struct dc_sink *
  2149. create_fake_sink(struct amdgpu_dm_connector *aconnector)
  2150. {
  2151. struct dc_sink_init_data sink_init_data = { 0 };
  2152. struct dc_sink *sink = NULL;
  2153. sink_init_data.link = aconnector->dc_link;
  2154. sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
  2155. sink = dc_sink_create(&sink_init_data);
  2156. if (!sink) {
  2157. DRM_ERROR("Failed to create sink!\n");
  2158. return NULL;
  2159. }
  2160. sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
  2161. return sink;
  2162. }
  2163. static void set_multisync_trigger_params(
  2164. struct dc_stream_state *stream)
  2165. {
  2166. if (stream->triggered_crtc_reset.enabled) {
  2167. stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING;
  2168. stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE;
  2169. }
  2170. }
  2171. static void set_master_stream(struct dc_stream_state *stream_set[],
  2172. int stream_count)
  2173. {
  2174. int j, highest_rfr = 0, master_stream = 0;
  2175. for (j = 0; j < stream_count; j++) {
  2176. if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
  2177. int refresh_rate = 0;
  2178. refresh_rate = (stream_set[j]->timing.pix_clk_khz*1000)/
  2179. (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
  2180. if (refresh_rate > highest_rfr) {
  2181. highest_rfr = refresh_rate;
  2182. master_stream = j;
  2183. }
  2184. }
  2185. }
  2186. for (j = 0; j < stream_count; j++) {
  2187. if (stream_set[j])
  2188. stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
  2189. }
  2190. }
  2191. static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
  2192. {
  2193. int i = 0;
  2194. if (context->stream_count < 2)
  2195. return;
  2196. for (i = 0; i < context->stream_count ; i++) {
  2197. if (!context->streams[i])
  2198. continue;
  2199. /*
  2200. * TODO: add a function to read AMD VSDB bits and set
  2201. * crtc_sync_master.multi_sync_enabled flag
  2202. * For now it's set to false
  2203. */
  2204. set_multisync_trigger_params(context->streams[i]);
  2205. }
  2206. set_master_stream(context->streams, context->stream_count);
  2207. }
  2208. static struct dc_stream_state *
  2209. create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
  2210. const struct drm_display_mode *drm_mode,
  2211. const struct dm_connector_state *dm_state)
  2212. {
  2213. struct drm_display_mode *preferred_mode = NULL;
  2214. struct drm_connector *drm_connector;
  2215. struct dc_stream_state *stream = NULL;
  2216. struct drm_display_mode mode = *drm_mode;
  2217. bool native_mode_found = false;
  2218. struct dc_sink *sink = NULL;
  2219. if (aconnector == NULL) {
  2220. DRM_ERROR("aconnector is NULL!\n");
  2221. return stream;
  2222. }
  2223. drm_connector = &aconnector->base;
  2224. if (!aconnector->dc_sink) {
  2225. if (!aconnector->mst_port) {
  2226. sink = create_fake_sink(aconnector);
  2227. if (!sink)
  2228. return stream;
  2229. }
  2230. } else {
  2231. sink = aconnector->dc_sink;
  2232. }
  2233. stream = dc_create_stream_for_sink(sink);
  2234. if (stream == NULL) {
  2235. DRM_ERROR("Failed to create stream for sink!\n");
  2236. goto finish;
  2237. }
  2238. list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
  2239. /* Search for preferred mode */
  2240. if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
  2241. native_mode_found = true;
  2242. break;
  2243. }
  2244. }
  2245. if (!native_mode_found)
  2246. preferred_mode = list_first_entry_or_null(
  2247. &aconnector->base.modes,
  2248. struct drm_display_mode,
  2249. head);
  2250. if (preferred_mode == NULL) {
  2251. /*
  2252. * This may not be an error, the use case is when we have no
  2253. * usermode calls to reset and set mode upon hotplug. In this
  2254. * case, we call set mode ourselves to restore the previous mode
  2255. * and the modelist may not be filled in in time.
  2256. */
  2257. DRM_DEBUG_DRIVER("No preferred mode found\n");
  2258. } else {
  2259. decide_crtc_timing_for_drm_display_mode(
  2260. &mode, preferred_mode,
  2261. dm_state ? (dm_state->scaling != RMX_OFF) : false);
  2262. }
  2263. if (!dm_state)
  2264. drm_mode_set_crtcinfo(&mode, 0);
  2265. fill_stream_properties_from_drm_display_mode(stream,
  2266. &mode, &aconnector->base);
  2267. update_stream_scaling_settings(&mode, dm_state, stream);
  2268. fill_audio_info(
  2269. &stream->audio_info,
  2270. drm_connector,
  2271. sink);
  2272. update_stream_signal(stream);
  2273. if (dm_state && dm_state->freesync_capable)
  2274. stream->ignore_msa_timing_param = true;
  2275. finish:
  2276. if (sink && sink->sink_signal == SIGNAL_TYPE_VIRTUAL && aconnector->base.force != DRM_FORCE_ON)
  2277. dc_sink_release(sink);
  2278. return stream;
  2279. }
  2280. static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
  2281. {
  2282. drm_crtc_cleanup(crtc);
  2283. kfree(crtc);
  2284. }
  2285. static void dm_crtc_destroy_state(struct drm_crtc *crtc,
  2286. struct drm_crtc_state *state)
  2287. {
  2288. struct dm_crtc_state *cur = to_dm_crtc_state(state);
  2289. /* TODO Destroy dc_stream objects are stream object is flattened */
  2290. if (cur->stream)
  2291. dc_stream_release(cur->stream);
  2292. __drm_atomic_helper_crtc_destroy_state(state);
  2293. kfree(state);
  2294. }
  2295. static void dm_crtc_reset_state(struct drm_crtc *crtc)
  2296. {
  2297. struct dm_crtc_state *state;
  2298. if (crtc->state)
  2299. dm_crtc_destroy_state(crtc, crtc->state);
  2300. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2301. if (WARN_ON(!state))
  2302. return;
  2303. crtc->state = &state->base;
  2304. crtc->state->crtc = crtc;
  2305. }
  2306. static struct drm_crtc_state *
  2307. dm_crtc_duplicate_state(struct drm_crtc *crtc)
  2308. {
  2309. struct dm_crtc_state *state, *cur;
  2310. cur = to_dm_crtc_state(crtc->state);
  2311. if (WARN_ON(!crtc->state))
  2312. return NULL;
  2313. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2314. if (!state)
  2315. return NULL;
  2316. __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
  2317. if (cur->stream) {
  2318. state->stream = cur->stream;
  2319. dc_stream_retain(state->stream);
  2320. }
  2321. state->adjust = cur->adjust;
  2322. state->vrr_infopacket = cur->vrr_infopacket;
  2323. state->freesync_enabled = cur->freesync_enabled;
  2324. /* TODO Duplicate dc_stream after objects are stream object is flattened */
  2325. return &state->base;
  2326. }
  2327. static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
  2328. {
  2329. enum dc_irq_source irq_source;
  2330. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  2331. struct amdgpu_device *adev = crtc->dev->dev_private;
  2332. irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
  2333. return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
  2334. }
  2335. static int dm_enable_vblank(struct drm_crtc *crtc)
  2336. {
  2337. return dm_set_vblank(crtc, true);
  2338. }
  2339. static void dm_disable_vblank(struct drm_crtc *crtc)
  2340. {
  2341. dm_set_vblank(crtc, false);
  2342. }
  2343. /* Implemented only the options currently availible for the driver */
  2344. static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
  2345. .reset = dm_crtc_reset_state,
  2346. .destroy = amdgpu_dm_crtc_destroy,
  2347. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  2348. .set_config = drm_atomic_helper_set_config,
  2349. .page_flip = drm_atomic_helper_page_flip,
  2350. .atomic_duplicate_state = dm_crtc_duplicate_state,
  2351. .atomic_destroy_state = dm_crtc_destroy_state,
  2352. .set_crc_source = amdgpu_dm_crtc_set_crc_source,
  2353. .verify_crc_source = amdgpu_dm_crtc_verify_crc_source,
  2354. .enable_vblank = dm_enable_vblank,
  2355. .disable_vblank = dm_disable_vblank,
  2356. };
  2357. static enum drm_connector_status
  2358. amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
  2359. {
  2360. bool connected;
  2361. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  2362. /*
  2363. * Notes:
  2364. * 1. This interface is NOT called in context of HPD irq.
  2365. * 2. This interface *is called* in context of user-mode ioctl. Which
  2366. * makes it a bad place for *any* MST-related activity.
  2367. */
  2368. if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
  2369. !aconnector->fake_enable)
  2370. connected = (aconnector->dc_sink != NULL);
  2371. else
  2372. connected = (aconnector->base.force == DRM_FORCE_ON);
  2373. return (connected ? connector_status_connected :
  2374. connector_status_disconnected);
  2375. }
  2376. int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
  2377. struct drm_connector_state *connector_state,
  2378. struct drm_property *property,
  2379. uint64_t val)
  2380. {
  2381. struct drm_device *dev = connector->dev;
  2382. struct amdgpu_device *adev = dev->dev_private;
  2383. struct dm_connector_state *dm_old_state =
  2384. to_dm_connector_state(connector->state);
  2385. struct dm_connector_state *dm_new_state =
  2386. to_dm_connector_state(connector_state);
  2387. int ret = -EINVAL;
  2388. if (property == dev->mode_config.scaling_mode_property) {
  2389. enum amdgpu_rmx_type rmx_type;
  2390. switch (val) {
  2391. case DRM_MODE_SCALE_CENTER:
  2392. rmx_type = RMX_CENTER;
  2393. break;
  2394. case DRM_MODE_SCALE_ASPECT:
  2395. rmx_type = RMX_ASPECT;
  2396. break;
  2397. case DRM_MODE_SCALE_FULLSCREEN:
  2398. rmx_type = RMX_FULL;
  2399. break;
  2400. case DRM_MODE_SCALE_NONE:
  2401. default:
  2402. rmx_type = RMX_OFF;
  2403. break;
  2404. }
  2405. if (dm_old_state->scaling == rmx_type)
  2406. return 0;
  2407. dm_new_state->scaling = rmx_type;
  2408. ret = 0;
  2409. } else if (property == adev->mode_info.underscan_hborder_property) {
  2410. dm_new_state->underscan_hborder = val;
  2411. ret = 0;
  2412. } else if (property == adev->mode_info.underscan_vborder_property) {
  2413. dm_new_state->underscan_vborder = val;
  2414. ret = 0;
  2415. } else if (property == adev->mode_info.underscan_property) {
  2416. dm_new_state->underscan_enable = val;
  2417. ret = 0;
  2418. }
  2419. return ret;
  2420. }
  2421. int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
  2422. const struct drm_connector_state *state,
  2423. struct drm_property *property,
  2424. uint64_t *val)
  2425. {
  2426. struct drm_device *dev = connector->dev;
  2427. struct amdgpu_device *adev = dev->dev_private;
  2428. struct dm_connector_state *dm_state =
  2429. to_dm_connector_state(state);
  2430. int ret = -EINVAL;
  2431. if (property == dev->mode_config.scaling_mode_property) {
  2432. switch (dm_state->scaling) {
  2433. case RMX_CENTER:
  2434. *val = DRM_MODE_SCALE_CENTER;
  2435. break;
  2436. case RMX_ASPECT:
  2437. *val = DRM_MODE_SCALE_ASPECT;
  2438. break;
  2439. case RMX_FULL:
  2440. *val = DRM_MODE_SCALE_FULLSCREEN;
  2441. break;
  2442. case RMX_OFF:
  2443. default:
  2444. *val = DRM_MODE_SCALE_NONE;
  2445. break;
  2446. }
  2447. ret = 0;
  2448. } else if (property == adev->mode_info.underscan_hborder_property) {
  2449. *val = dm_state->underscan_hborder;
  2450. ret = 0;
  2451. } else if (property == adev->mode_info.underscan_vborder_property) {
  2452. *val = dm_state->underscan_vborder;
  2453. ret = 0;
  2454. } else if (property == adev->mode_info.underscan_property) {
  2455. *val = dm_state->underscan_enable;
  2456. ret = 0;
  2457. }
  2458. return ret;
  2459. }
  2460. static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
  2461. {
  2462. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  2463. const struct dc_link *link = aconnector->dc_link;
  2464. struct amdgpu_device *adev = connector->dev->dev_private;
  2465. struct amdgpu_display_manager *dm = &adev->dm;
  2466. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
  2467. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  2468. if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
  2469. link->type != dc_connection_none &&
  2470. dm->backlight_dev) {
  2471. backlight_device_unregister(dm->backlight_dev);
  2472. dm->backlight_dev = NULL;
  2473. }
  2474. #endif
  2475. drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
  2476. drm_connector_unregister(connector);
  2477. drm_connector_cleanup(connector);
  2478. kfree(connector);
  2479. }
  2480. void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
  2481. {
  2482. struct dm_connector_state *state =
  2483. to_dm_connector_state(connector->state);
  2484. if (connector->state)
  2485. __drm_atomic_helper_connector_destroy_state(connector->state);
  2486. kfree(state);
  2487. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2488. if (state) {
  2489. state->scaling = RMX_OFF;
  2490. state->underscan_enable = false;
  2491. state->underscan_hborder = 0;
  2492. state->underscan_vborder = 0;
  2493. __drm_atomic_helper_connector_reset(connector, &state->base);
  2494. }
  2495. }
  2496. struct drm_connector_state *
  2497. amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
  2498. {
  2499. struct dm_connector_state *state =
  2500. to_dm_connector_state(connector->state);
  2501. struct dm_connector_state *new_state =
  2502. kmemdup(state, sizeof(*state), GFP_KERNEL);
  2503. if (!new_state)
  2504. return NULL;
  2505. __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
  2506. new_state->freesync_capable = state->freesync_capable;
  2507. new_state->freesync_enable = state->freesync_enable;
  2508. return &new_state->base;
  2509. }
  2510. static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
  2511. .reset = amdgpu_dm_connector_funcs_reset,
  2512. .detect = amdgpu_dm_connector_detect,
  2513. .fill_modes = drm_helper_probe_single_connector_modes,
  2514. .destroy = amdgpu_dm_connector_destroy,
  2515. .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
  2516. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  2517. .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
  2518. .atomic_get_property = amdgpu_dm_connector_atomic_get_property
  2519. };
  2520. static int get_modes(struct drm_connector *connector)
  2521. {
  2522. return amdgpu_dm_connector_get_modes(connector);
  2523. }
  2524. static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
  2525. {
  2526. struct dc_sink_init_data init_params = {
  2527. .link = aconnector->dc_link,
  2528. .sink_signal = SIGNAL_TYPE_VIRTUAL
  2529. };
  2530. struct edid *edid;
  2531. if (!aconnector->base.edid_blob_ptr) {
  2532. DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
  2533. aconnector->base.name);
  2534. aconnector->base.force = DRM_FORCE_OFF;
  2535. aconnector->base.override_edid = false;
  2536. return;
  2537. }
  2538. edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
  2539. aconnector->edid = edid;
  2540. aconnector->dc_em_sink = dc_link_add_remote_sink(
  2541. aconnector->dc_link,
  2542. (uint8_t *)edid,
  2543. (edid->extensions + 1) * EDID_LENGTH,
  2544. &init_params);
  2545. if (aconnector->base.force == DRM_FORCE_ON)
  2546. aconnector->dc_sink = aconnector->dc_link->local_sink ?
  2547. aconnector->dc_link->local_sink :
  2548. aconnector->dc_em_sink;
  2549. }
  2550. static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
  2551. {
  2552. struct dc_link *link = (struct dc_link *)aconnector->dc_link;
  2553. /*
  2554. * In case of headless boot with force on for DP managed connector
  2555. * Those settings have to be != 0 to get initial modeset
  2556. */
  2557. if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
  2558. link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
  2559. link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
  2560. }
  2561. aconnector->base.override_edid = true;
  2562. create_eml_sink(aconnector);
  2563. }
  2564. enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
  2565. struct drm_display_mode *mode)
  2566. {
  2567. int result = MODE_ERROR;
  2568. struct dc_sink *dc_sink;
  2569. struct amdgpu_device *adev = connector->dev->dev_private;
  2570. /* TODO: Unhardcode stream count */
  2571. struct dc_stream_state *stream;
  2572. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  2573. enum dc_status dc_result = DC_OK;
  2574. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  2575. (mode->flags & DRM_MODE_FLAG_DBLSCAN))
  2576. return result;
  2577. /*
  2578. * Only run this the first time mode_valid is called to initilialize
  2579. * EDID mgmt
  2580. */
  2581. if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
  2582. !aconnector->dc_em_sink)
  2583. handle_edid_mgmt(aconnector);
  2584. dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
  2585. if (dc_sink == NULL) {
  2586. DRM_ERROR("dc_sink is NULL!\n");
  2587. goto fail;
  2588. }
  2589. stream = create_stream_for_sink(aconnector, mode, NULL);
  2590. if (stream == NULL) {
  2591. DRM_ERROR("Failed to create stream for sink!\n");
  2592. goto fail;
  2593. }
  2594. dc_result = dc_validate_stream(adev->dm.dc, stream);
  2595. if (dc_result == DC_OK)
  2596. result = MODE_OK;
  2597. else
  2598. DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n",
  2599. mode->vdisplay,
  2600. mode->hdisplay,
  2601. mode->clock,
  2602. dc_result);
  2603. dc_stream_release(stream);
  2604. fail:
  2605. /* TODO: error handling*/
  2606. return result;
  2607. }
  2608. static const struct drm_connector_helper_funcs
  2609. amdgpu_dm_connector_helper_funcs = {
  2610. /*
  2611. * If hotplugging a second bigger display in FB Con mode, bigger resolution
  2612. * modes will be filtered by drm_mode_validate_size(), and those modes
  2613. * are missing after user start lightdm. So we need to renew modes list.
  2614. * in get_modes call back, not just return the modes count
  2615. */
  2616. .get_modes = get_modes,
  2617. .mode_valid = amdgpu_dm_connector_mode_valid,
  2618. .best_encoder = drm_atomic_helper_best_encoder
  2619. };
  2620. static void dm_crtc_helper_disable(struct drm_crtc *crtc)
  2621. {
  2622. }
  2623. static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
  2624. struct drm_crtc_state *state)
  2625. {
  2626. struct amdgpu_device *adev = crtc->dev->dev_private;
  2627. struct dc *dc = adev->dm.dc;
  2628. struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
  2629. int ret = -EINVAL;
  2630. if (unlikely(!dm_crtc_state->stream &&
  2631. modeset_required(state, NULL, dm_crtc_state->stream))) {
  2632. WARN_ON(1);
  2633. return ret;
  2634. }
  2635. /* In some use cases, like reset, no stream is attached */
  2636. if (!dm_crtc_state->stream)
  2637. return 0;
  2638. if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
  2639. return 0;
  2640. return ret;
  2641. }
  2642. static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
  2643. const struct drm_display_mode *mode,
  2644. struct drm_display_mode *adjusted_mode)
  2645. {
  2646. return true;
  2647. }
  2648. static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
  2649. .disable = dm_crtc_helper_disable,
  2650. .atomic_check = dm_crtc_helper_atomic_check,
  2651. .mode_fixup = dm_crtc_helper_mode_fixup
  2652. };
  2653. static void dm_encoder_helper_disable(struct drm_encoder *encoder)
  2654. {
  2655. }
  2656. static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
  2657. struct drm_crtc_state *crtc_state,
  2658. struct drm_connector_state *conn_state)
  2659. {
  2660. return 0;
  2661. }
  2662. const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
  2663. .disable = dm_encoder_helper_disable,
  2664. .atomic_check = dm_encoder_helper_atomic_check
  2665. };
  2666. static void dm_drm_plane_reset(struct drm_plane *plane)
  2667. {
  2668. struct dm_plane_state *amdgpu_state = NULL;
  2669. if (plane->state)
  2670. plane->funcs->atomic_destroy_state(plane, plane->state);
  2671. amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
  2672. WARN_ON(amdgpu_state == NULL);
  2673. if (amdgpu_state) {
  2674. plane->state = &amdgpu_state->base;
  2675. plane->state->plane = plane;
  2676. plane->state->rotation = DRM_MODE_ROTATE_0;
  2677. }
  2678. }
  2679. static struct drm_plane_state *
  2680. dm_drm_plane_duplicate_state(struct drm_plane *plane)
  2681. {
  2682. struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
  2683. old_dm_plane_state = to_dm_plane_state(plane->state);
  2684. dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
  2685. if (!dm_plane_state)
  2686. return NULL;
  2687. __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
  2688. if (old_dm_plane_state->dc_state) {
  2689. dm_plane_state->dc_state = old_dm_plane_state->dc_state;
  2690. dc_plane_state_retain(dm_plane_state->dc_state);
  2691. }
  2692. return &dm_plane_state->base;
  2693. }
  2694. void dm_drm_plane_destroy_state(struct drm_plane *plane,
  2695. struct drm_plane_state *state)
  2696. {
  2697. struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
  2698. if (dm_plane_state->dc_state)
  2699. dc_plane_state_release(dm_plane_state->dc_state);
  2700. drm_atomic_helper_plane_destroy_state(plane, state);
  2701. }
  2702. static const struct drm_plane_funcs dm_plane_funcs = {
  2703. .update_plane = drm_atomic_helper_update_plane,
  2704. .disable_plane = drm_atomic_helper_disable_plane,
  2705. .destroy = drm_primary_helper_destroy,
  2706. .reset = dm_drm_plane_reset,
  2707. .atomic_duplicate_state = dm_drm_plane_duplicate_state,
  2708. .atomic_destroy_state = dm_drm_plane_destroy_state,
  2709. };
  2710. static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
  2711. struct drm_plane_state *new_state)
  2712. {
  2713. struct amdgpu_framebuffer *afb;
  2714. struct drm_gem_object *obj;
  2715. struct amdgpu_device *adev;
  2716. struct amdgpu_bo *rbo;
  2717. uint64_t chroma_addr = 0;
  2718. struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
  2719. unsigned int awidth;
  2720. uint32_t domain;
  2721. int r;
  2722. dm_plane_state_old = to_dm_plane_state(plane->state);
  2723. dm_plane_state_new = to_dm_plane_state(new_state);
  2724. if (!new_state->fb) {
  2725. DRM_DEBUG_DRIVER("No FB bound\n");
  2726. return 0;
  2727. }
  2728. afb = to_amdgpu_framebuffer(new_state->fb);
  2729. obj = new_state->fb->obj[0];
  2730. rbo = gem_to_amdgpu_bo(obj);
  2731. adev = amdgpu_ttm_adev(rbo->tbo.bdev);
  2732. r = amdgpu_bo_reserve(rbo, false);
  2733. if (unlikely(r != 0))
  2734. return r;
  2735. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  2736. domain = amdgpu_display_supported_domains(adev);
  2737. else
  2738. domain = AMDGPU_GEM_DOMAIN_VRAM;
  2739. r = amdgpu_bo_pin(rbo, domain);
  2740. if (unlikely(r != 0)) {
  2741. if (r != -ERESTARTSYS)
  2742. DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
  2743. amdgpu_bo_unreserve(rbo);
  2744. return r;
  2745. }
  2746. r = amdgpu_ttm_alloc_gart(&rbo->tbo);
  2747. if (unlikely(r != 0)) {
  2748. amdgpu_bo_unpin(rbo);
  2749. amdgpu_bo_unreserve(rbo);
  2750. DRM_ERROR("%p bind failed\n", rbo);
  2751. return r;
  2752. }
  2753. amdgpu_bo_unreserve(rbo);
  2754. afb->address = amdgpu_bo_gpu_offset(rbo);
  2755. amdgpu_bo_ref(rbo);
  2756. if (dm_plane_state_new->dc_state &&
  2757. dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
  2758. struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
  2759. if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
  2760. plane_state->address.grph.addr.low_part = lower_32_bits(afb->address);
  2761. plane_state->address.grph.addr.high_part = upper_32_bits(afb->address);
  2762. } else {
  2763. awidth = ALIGN(new_state->fb->width, 64);
  2764. plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
  2765. plane_state->address.video_progressive.luma_addr.low_part
  2766. = lower_32_bits(afb->address);
  2767. plane_state->address.video_progressive.luma_addr.high_part
  2768. = upper_32_bits(afb->address);
  2769. chroma_addr = afb->address + (u64)awidth * new_state->fb->height;
  2770. plane_state->address.video_progressive.chroma_addr.low_part
  2771. = lower_32_bits(chroma_addr);
  2772. plane_state->address.video_progressive.chroma_addr.high_part
  2773. = upper_32_bits(chroma_addr);
  2774. }
  2775. }
  2776. return 0;
  2777. }
  2778. static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
  2779. struct drm_plane_state *old_state)
  2780. {
  2781. struct amdgpu_bo *rbo;
  2782. int r;
  2783. if (!old_state->fb)
  2784. return;
  2785. rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
  2786. r = amdgpu_bo_reserve(rbo, false);
  2787. if (unlikely(r)) {
  2788. DRM_ERROR("failed to reserve rbo before unpin\n");
  2789. return;
  2790. }
  2791. amdgpu_bo_unpin(rbo);
  2792. amdgpu_bo_unreserve(rbo);
  2793. amdgpu_bo_unref(&rbo);
  2794. }
  2795. static int dm_plane_atomic_check(struct drm_plane *plane,
  2796. struct drm_plane_state *state)
  2797. {
  2798. struct amdgpu_device *adev = plane->dev->dev_private;
  2799. struct dc *dc = adev->dm.dc;
  2800. struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
  2801. if (!dm_plane_state->dc_state)
  2802. return 0;
  2803. if (!fill_rects_from_plane_state(state, dm_plane_state->dc_state))
  2804. return -EINVAL;
  2805. if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
  2806. return 0;
  2807. return -EINVAL;
  2808. }
  2809. static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
  2810. .prepare_fb = dm_plane_helper_prepare_fb,
  2811. .cleanup_fb = dm_plane_helper_cleanup_fb,
  2812. .atomic_check = dm_plane_atomic_check,
  2813. };
  2814. /*
  2815. * TODO: these are currently initialized to rgb formats only.
  2816. * For future use cases we should either initialize them dynamically based on
  2817. * plane capabilities, or initialize this array to all formats, so internal drm
  2818. * check will succeed, and let DC implement proper check
  2819. */
  2820. static const uint32_t rgb_formats[] = {
  2821. DRM_FORMAT_RGB888,
  2822. DRM_FORMAT_XRGB8888,
  2823. DRM_FORMAT_ARGB8888,
  2824. DRM_FORMAT_RGBA8888,
  2825. DRM_FORMAT_XRGB2101010,
  2826. DRM_FORMAT_XBGR2101010,
  2827. DRM_FORMAT_ARGB2101010,
  2828. DRM_FORMAT_ABGR2101010,
  2829. DRM_FORMAT_XBGR8888,
  2830. DRM_FORMAT_ABGR8888,
  2831. };
  2832. static const uint32_t yuv_formats[] = {
  2833. DRM_FORMAT_NV12,
  2834. DRM_FORMAT_NV21,
  2835. };
  2836. static const u32 cursor_formats[] = {
  2837. DRM_FORMAT_ARGB8888
  2838. };
  2839. static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
  2840. struct amdgpu_plane *aplane,
  2841. unsigned long possible_crtcs)
  2842. {
  2843. int res = -EPERM;
  2844. switch (aplane->base.type) {
  2845. case DRM_PLANE_TYPE_PRIMARY:
  2846. res = drm_universal_plane_init(
  2847. dm->adev->ddev,
  2848. &aplane->base,
  2849. possible_crtcs,
  2850. &dm_plane_funcs,
  2851. rgb_formats,
  2852. ARRAY_SIZE(rgb_formats),
  2853. NULL, aplane->base.type, NULL);
  2854. break;
  2855. case DRM_PLANE_TYPE_OVERLAY:
  2856. res = drm_universal_plane_init(
  2857. dm->adev->ddev,
  2858. &aplane->base,
  2859. possible_crtcs,
  2860. &dm_plane_funcs,
  2861. yuv_formats,
  2862. ARRAY_SIZE(yuv_formats),
  2863. NULL, aplane->base.type, NULL);
  2864. break;
  2865. case DRM_PLANE_TYPE_CURSOR:
  2866. res = drm_universal_plane_init(
  2867. dm->adev->ddev,
  2868. &aplane->base,
  2869. possible_crtcs,
  2870. &dm_plane_funcs,
  2871. cursor_formats,
  2872. ARRAY_SIZE(cursor_formats),
  2873. NULL, aplane->base.type, NULL);
  2874. break;
  2875. }
  2876. drm_plane_helper_add(&aplane->base, &dm_plane_helper_funcs);
  2877. /* Create (reset) the plane state */
  2878. if (aplane->base.funcs->reset)
  2879. aplane->base.funcs->reset(&aplane->base);
  2880. return res;
  2881. }
  2882. static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
  2883. struct drm_plane *plane,
  2884. uint32_t crtc_index)
  2885. {
  2886. struct amdgpu_crtc *acrtc = NULL;
  2887. struct amdgpu_plane *cursor_plane;
  2888. int res = -ENOMEM;
  2889. cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
  2890. if (!cursor_plane)
  2891. goto fail;
  2892. cursor_plane->base.type = DRM_PLANE_TYPE_CURSOR;
  2893. res = amdgpu_dm_plane_init(dm, cursor_plane, 0);
  2894. acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
  2895. if (!acrtc)
  2896. goto fail;
  2897. res = drm_crtc_init_with_planes(
  2898. dm->ddev,
  2899. &acrtc->base,
  2900. plane,
  2901. &cursor_plane->base,
  2902. &amdgpu_dm_crtc_funcs, NULL);
  2903. if (res)
  2904. goto fail;
  2905. drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
  2906. /* Create (reset) the plane state */
  2907. if (acrtc->base.funcs->reset)
  2908. acrtc->base.funcs->reset(&acrtc->base);
  2909. acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
  2910. acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
  2911. acrtc->crtc_id = crtc_index;
  2912. acrtc->base.enabled = false;
  2913. acrtc->otg_inst = -1;
  2914. dm->adev->mode_info.crtcs[crtc_index] = acrtc;
  2915. drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
  2916. true, MAX_COLOR_LUT_ENTRIES);
  2917. drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
  2918. return 0;
  2919. fail:
  2920. kfree(acrtc);
  2921. kfree(cursor_plane);
  2922. return res;
  2923. }
  2924. static int to_drm_connector_type(enum signal_type st)
  2925. {
  2926. switch (st) {
  2927. case SIGNAL_TYPE_HDMI_TYPE_A:
  2928. return DRM_MODE_CONNECTOR_HDMIA;
  2929. case SIGNAL_TYPE_EDP:
  2930. return DRM_MODE_CONNECTOR_eDP;
  2931. case SIGNAL_TYPE_LVDS:
  2932. return DRM_MODE_CONNECTOR_LVDS;
  2933. case SIGNAL_TYPE_RGB:
  2934. return DRM_MODE_CONNECTOR_VGA;
  2935. case SIGNAL_TYPE_DISPLAY_PORT:
  2936. case SIGNAL_TYPE_DISPLAY_PORT_MST:
  2937. return DRM_MODE_CONNECTOR_DisplayPort;
  2938. case SIGNAL_TYPE_DVI_DUAL_LINK:
  2939. case SIGNAL_TYPE_DVI_SINGLE_LINK:
  2940. return DRM_MODE_CONNECTOR_DVID;
  2941. case SIGNAL_TYPE_VIRTUAL:
  2942. return DRM_MODE_CONNECTOR_VIRTUAL;
  2943. default:
  2944. return DRM_MODE_CONNECTOR_Unknown;
  2945. }
  2946. }
  2947. static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
  2948. {
  2949. const struct drm_connector_helper_funcs *helper =
  2950. connector->helper_private;
  2951. struct drm_encoder *encoder;
  2952. struct amdgpu_encoder *amdgpu_encoder;
  2953. encoder = helper->best_encoder(connector);
  2954. if (encoder == NULL)
  2955. return;
  2956. amdgpu_encoder = to_amdgpu_encoder(encoder);
  2957. amdgpu_encoder->native_mode.clock = 0;
  2958. if (!list_empty(&connector->probed_modes)) {
  2959. struct drm_display_mode *preferred_mode = NULL;
  2960. list_for_each_entry(preferred_mode,
  2961. &connector->probed_modes,
  2962. head) {
  2963. if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
  2964. amdgpu_encoder->native_mode = *preferred_mode;
  2965. break;
  2966. }
  2967. }
  2968. }
  2969. static struct drm_display_mode *
  2970. amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
  2971. char *name,
  2972. int hdisplay, int vdisplay)
  2973. {
  2974. struct drm_device *dev = encoder->dev;
  2975. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2976. struct drm_display_mode *mode = NULL;
  2977. struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
  2978. mode = drm_mode_duplicate(dev, native_mode);
  2979. if (mode == NULL)
  2980. return NULL;
  2981. mode->hdisplay = hdisplay;
  2982. mode->vdisplay = vdisplay;
  2983. mode->type &= ~DRM_MODE_TYPE_PREFERRED;
  2984. strncpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
  2985. return mode;
  2986. }
  2987. static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
  2988. struct drm_connector *connector)
  2989. {
  2990. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2991. struct drm_display_mode *mode = NULL;
  2992. struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
  2993. struct amdgpu_dm_connector *amdgpu_dm_connector =
  2994. to_amdgpu_dm_connector(connector);
  2995. int i;
  2996. int n;
  2997. struct mode_size {
  2998. char name[DRM_DISPLAY_MODE_LEN];
  2999. int w;
  3000. int h;
  3001. } common_modes[] = {
  3002. { "640x480", 640, 480},
  3003. { "800x600", 800, 600},
  3004. { "1024x768", 1024, 768},
  3005. { "1280x720", 1280, 720},
  3006. { "1280x800", 1280, 800},
  3007. {"1280x1024", 1280, 1024},
  3008. { "1440x900", 1440, 900},
  3009. {"1680x1050", 1680, 1050},
  3010. {"1600x1200", 1600, 1200},
  3011. {"1920x1080", 1920, 1080},
  3012. {"1920x1200", 1920, 1200}
  3013. };
  3014. n = ARRAY_SIZE(common_modes);
  3015. for (i = 0; i < n; i++) {
  3016. struct drm_display_mode *curmode = NULL;
  3017. bool mode_existed = false;
  3018. if (common_modes[i].w > native_mode->hdisplay ||
  3019. common_modes[i].h > native_mode->vdisplay ||
  3020. (common_modes[i].w == native_mode->hdisplay &&
  3021. common_modes[i].h == native_mode->vdisplay))
  3022. continue;
  3023. list_for_each_entry(curmode, &connector->probed_modes, head) {
  3024. if (common_modes[i].w == curmode->hdisplay &&
  3025. common_modes[i].h == curmode->vdisplay) {
  3026. mode_existed = true;
  3027. break;
  3028. }
  3029. }
  3030. if (mode_existed)
  3031. continue;
  3032. mode = amdgpu_dm_create_common_mode(encoder,
  3033. common_modes[i].name, common_modes[i].w,
  3034. common_modes[i].h);
  3035. drm_mode_probed_add(connector, mode);
  3036. amdgpu_dm_connector->num_modes++;
  3037. }
  3038. }
  3039. static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
  3040. struct edid *edid)
  3041. {
  3042. struct amdgpu_dm_connector *amdgpu_dm_connector =
  3043. to_amdgpu_dm_connector(connector);
  3044. if (edid) {
  3045. /* empty probed_modes */
  3046. INIT_LIST_HEAD(&connector->probed_modes);
  3047. amdgpu_dm_connector->num_modes =
  3048. drm_add_edid_modes(connector, edid);
  3049. amdgpu_dm_get_native_mode(connector);
  3050. } else {
  3051. amdgpu_dm_connector->num_modes = 0;
  3052. }
  3053. }
  3054. static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
  3055. {
  3056. const struct drm_connector_helper_funcs *helper =
  3057. connector->helper_private;
  3058. struct amdgpu_dm_connector *amdgpu_dm_connector =
  3059. to_amdgpu_dm_connector(connector);
  3060. struct drm_encoder *encoder;
  3061. struct edid *edid = amdgpu_dm_connector->edid;
  3062. encoder = helper->best_encoder(connector);
  3063. if (!edid || !drm_edid_is_valid(edid)) {
  3064. amdgpu_dm_connector->num_modes =
  3065. drm_add_modes_noedid(connector, 640, 480);
  3066. } else {
  3067. amdgpu_dm_connector_ddc_get_modes(connector, edid);
  3068. amdgpu_dm_connector_add_common_modes(encoder, connector);
  3069. }
  3070. amdgpu_dm_fbc_init(connector);
  3071. return amdgpu_dm_connector->num_modes;
  3072. }
  3073. void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
  3074. struct amdgpu_dm_connector *aconnector,
  3075. int connector_type,
  3076. struct dc_link *link,
  3077. int link_index)
  3078. {
  3079. struct amdgpu_device *adev = dm->ddev->dev_private;
  3080. aconnector->connector_id = link_index;
  3081. aconnector->dc_link = link;
  3082. aconnector->base.interlace_allowed = false;
  3083. aconnector->base.doublescan_allowed = false;
  3084. aconnector->base.stereo_allowed = false;
  3085. aconnector->base.dpms = DRM_MODE_DPMS_OFF;
  3086. aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
  3087. mutex_init(&aconnector->hpd_lock);
  3088. /*
  3089. * configure support HPD hot plug connector_>polled default value is 0
  3090. * which means HPD hot plug not supported
  3091. */
  3092. switch (connector_type) {
  3093. case DRM_MODE_CONNECTOR_HDMIA:
  3094. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  3095. aconnector->base.ycbcr_420_allowed =
  3096. link->link_enc->features.ycbcr420_supported ? true : false;
  3097. break;
  3098. case DRM_MODE_CONNECTOR_DisplayPort:
  3099. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  3100. aconnector->base.ycbcr_420_allowed =
  3101. link->link_enc->features.ycbcr420_supported ? true : false;
  3102. break;
  3103. case DRM_MODE_CONNECTOR_DVID:
  3104. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  3105. break;
  3106. default:
  3107. break;
  3108. }
  3109. drm_object_attach_property(&aconnector->base.base,
  3110. dm->ddev->mode_config.scaling_mode_property,
  3111. DRM_MODE_SCALE_NONE);
  3112. drm_object_attach_property(&aconnector->base.base,
  3113. adev->mode_info.underscan_property,
  3114. UNDERSCAN_OFF);
  3115. drm_object_attach_property(&aconnector->base.base,
  3116. adev->mode_info.underscan_hborder_property,
  3117. 0);
  3118. drm_object_attach_property(&aconnector->base.base,
  3119. adev->mode_info.underscan_vborder_property,
  3120. 0);
  3121. }
  3122. static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
  3123. struct i2c_msg *msgs, int num)
  3124. {
  3125. struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
  3126. struct ddc_service *ddc_service = i2c->ddc_service;
  3127. struct i2c_command cmd;
  3128. int i;
  3129. int result = -EIO;
  3130. cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
  3131. if (!cmd.payloads)
  3132. return result;
  3133. cmd.number_of_payloads = num;
  3134. cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
  3135. cmd.speed = 100;
  3136. for (i = 0; i < num; i++) {
  3137. cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
  3138. cmd.payloads[i].address = msgs[i].addr;
  3139. cmd.payloads[i].length = msgs[i].len;
  3140. cmd.payloads[i].data = msgs[i].buf;
  3141. }
  3142. if (dc_submit_i2c(
  3143. ddc_service->ctx->dc,
  3144. ddc_service->ddc_pin->hw_info.ddc_channel,
  3145. &cmd))
  3146. result = num;
  3147. kfree(cmd.payloads);
  3148. return result;
  3149. }
  3150. static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
  3151. {
  3152. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  3153. }
  3154. static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
  3155. .master_xfer = amdgpu_dm_i2c_xfer,
  3156. .functionality = amdgpu_dm_i2c_func,
  3157. };
  3158. static struct amdgpu_i2c_adapter *
  3159. create_i2c(struct ddc_service *ddc_service,
  3160. int link_index,
  3161. int *res)
  3162. {
  3163. struct amdgpu_device *adev = ddc_service->ctx->driver_context;
  3164. struct amdgpu_i2c_adapter *i2c;
  3165. i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
  3166. if (!i2c)
  3167. return NULL;
  3168. i2c->base.owner = THIS_MODULE;
  3169. i2c->base.class = I2C_CLASS_DDC;
  3170. i2c->base.dev.parent = &adev->pdev->dev;
  3171. i2c->base.algo = &amdgpu_dm_i2c_algo;
  3172. snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
  3173. i2c_set_adapdata(&i2c->base, i2c);
  3174. i2c->ddc_service = ddc_service;
  3175. i2c->ddc_service->ddc_pin->hw_info.ddc_channel = link_index;
  3176. return i2c;
  3177. }
  3178. /*
  3179. * Note: this function assumes that dc_link_detect() was called for the
  3180. * dc_link which will be represented by this aconnector.
  3181. */
  3182. static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
  3183. struct amdgpu_dm_connector *aconnector,
  3184. uint32_t link_index,
  3185. struct amdgpu_encoder *aencoder)
  3186. {
  3187. int res = 0;
  3188. int connector_type;
  3189. struct dc *dc = dm->dc;
  3190. struct dc_link *link = dc_get_link_at_index(dc, link_index);
  3191. struct amdgpu_i2c_adapter *i2c;
  3192. link->priv = aconnector;
  3193. DRM_DEBUG_DRIVER("%s()\n", __func__);
  3194. i2c = create_i2c(link->ddc, link->link_index, &res);
  3195. if (!i2c) {
  3196. DRM_ERROR("Failed to create i2c adapter data\n");
  3197. return -ENOMEM;
  3198. }
  3199. aconnector->i2c = i2c;
  3200. res = i2c_add_adapter(&i2c->base);
  3201. if (res) {
  3202. DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
  3203. goto out_free;
  3204. }
  3205. connector_type = to_drm_connector_type(link->connector_signal);
  3206. res = drm_connector_init(
  3207. dm->ddev,
  3208. &aconnector->base,
  3209. &amdgpu_dm_connector_funcs,
  3210. connector_type);
  3211. if (res) {
  3212. DRM_ERROR("connector_init failed\n");
  3213. aconnector->connector_id = -1;
  3214. goto out_free;
  3215. }
  3216. drm_connector_helper_add(
  3217. &aconnector->base,
  3218. &amdgpu_dm_connector_helper_funcs);
  3219. if (aconnector->base.funcs->reset)
  3220. aconnector->base.funcs->reset(&aconnector->base);
  3221. amdgpu_dm_connector_init_helper(
  3222. dm,
  3223. aconnector,
  3224. connector_type,
  3225. link,
  3226. link_index);
  3227. drm_connector_attach_encoder(
  3228. &aconnector->base, &aencoder->base);
  3229. drm_connector_register(&aconnector->base);
  3230. #if defined(CONFIG_DEBUG_FS)
  3231. res = connector_debugfs_init(aconnector);
  3232. if (res) {
  3233. DRM_ERROR("Failed to create debugfs for connector");
  3234. goto out_free;
  3235. }
  3236. #endif
  3237. if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
  3238. || connector_type == DRM_MODE_CONNECTOR_eDP)
  3239. amdgpu_dm_initialize_dp_connector(dm, aconnector);
  3240. out_free:
  3241. if (res) {
  3242. kfree(i2c);
  3243. aconnector->i2c = NULL;
  3244. }
  3245. return res;
  3246. }
  3247. int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
  3248. {
  3249. switch (adev->mode_info.num_crtc) {
  3250. case 1:
  3251. return 0x1;
  3252. case 2:
  3253. return 0x3;
  3254. case 3:
  3255. return 0x7;
  3256. case 4:
  3257. return 0xf;
  3258. case 5:
  3259. return 0x1f;
  3260. case 6:
  3261. default:
  3262. return 0x3f;
  3263. }
  3264. }
  3265. static int amdgpu_dm_encoder_init(struct drm_device *dev,
  3266. struct amdgpu_encoder *aencoder,
  3267. uint32_t link_index)
  3268. {
  3269. struct amdgpu_device *adev = dev->dev_private;
  3270. int res = drm_encoder_init(dev,
  3271. &aencoder->base,
  3272. &amdgpu_dm_encoder_funcs,
  3273. DRM_MODE_ENCODER_TMDS,
  3274. NULL);
  3275. aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
  3276. if (!res)
  3277. aencoder->encoder_id = link_index;
  3278. else
  3279. aencoder->encoder_id = -1;
  3280. drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
  3281. return res;
  3282. }
  3283. static void manage_dm_interrupts(struct amdgpu_device *adev,
  3284. struct amdgpu_crtc *acrtc,
  3285. bool enable)
  3286. {
  3287. /*
  3288. * this is not correct translation but will work as soon as VBLANK
  3289. * constant is the same as PFLIP
  3290. */
  3291. int irq_type =
  3292. amdgpu_display_crtc_idx_to_irq_type(
  3293. adev,
  3294. acrtc->crtc_id);
  3295. if (enable) {
  3296. drm_crtc_vblank_on(&acrtc->base);
  3297. amdgpu_irq_get(
  3298. adev,
  3299. &adev->pageflip_irq,
  3300. irq_type);
  3301. } else {
  3302. amdgpu_irq_put(
  3303. adev,
  3304. &adev->pageflip_irq,
  3305. irq_type);
  3306. drm_crtc_vblank_off(&acrtc->base);
  3307. }
  3308. }
  3309. static bool
  3310. is_scaling_state_different(const struct dm_connector_state *dm_state,
  3311. const struct dm_connector_state *old_dm_state)
  3312. {
  3313. if (dm_state->scaling != old_dm_state->scaling)
  3314. return true;
  3315. if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
  3316. if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
  3317. return true;
  3318. } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
  3319. if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
  3320. return true;
  3321. } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
  3322. dm_state->underscan_vborder != old_dm_state->underscan_vborder)
  3323. return true;
  3324. return false;
  3325. }
  3326. static void remove_stream(struct amdgpu_device *adev,
  3327. struct amdgpu_crtc *acrtc,
  3328. struct dc_stream_state *stream)
  3329. {
  3330. /* this is the update mode case */
  3331. acrtc->otg_inst = -1;
  3332. acrtc->enabled = false;
  3333. }
  3334. static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
  3335. struct dc_cursor_position *position)
  3336. {
  3337. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  3338. int x, y;
  3339. int xorigin = 0, yorigin = 0;
  3340. if (!crtc || !plane->state->fb) {
  3341. position->enable = false;
  3342. position->x = 0;
  3343. position->y = 0;
  3344. return 0;
  3345. }
  3346. if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
  3347. (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
  3348. DRM_ERROR("%s: bad cursor width or height %d x %d\n",
  3349. __func__,
  3350. plane->state->crtc_w,
  3351. plane->state->crtc_h);
  3352. return -EINVAL;
  3353. }
  3354. x = plane->state->crtc_x;
  3355. y = plane->state->crtc_y;
  3356. /* avivo cursor are offset into the total surface */
  3357. x += crtc->primary->state->src_x >> 16;
  3358. y += crtc->primary->state->src_y >> 16;
  3359. if (x < 0) {
  3360. xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
  3361. x = 0;
  3362. }
  3363. if (y < 0) {
  3364. yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
  3365. y = 0;
  3366. }
  3367. position->enable = true;
  3368. position->x = x;
  3369. position->y = y;
  3370. position->x_hotspot = xorigin;
  3371. position->y_hotspot = yorigin;
  3372. return 0;
  3373. }
  3374. static void handle_cursor_update(struct drm_plane *plane,
  3375. struct drm_plane_state *old_plane_state)
  3376. {
  3377. struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
  3378. struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
  3379. struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
  3380. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  3381. uint64_t address = afb ? afb->address : 0;
  3382. struct dc_cursor_position position;
  3383. struct dc_cursor_attributes attributes;
  3384. int ret;
  3385. if (!plane->state->fb && !old_plane_state->fb)
  3386. return;
  3387. DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n",
  3388. __func__,
  3389. amdgpu_crtc->crtc_id,
  3390. plane->state->crtc_w,
  3391. plane->state->crtc_h);
  3392. ret = get_cursor_position(plane, crtc, &position);
  3393. if (ret)
  3394. return;
  3395. if (!position.enable) {
  3396. /* turn off cursor */
  3397. if (crtc_state && crtc_state->stream)
  3398. dc_stream_set_cursor_position(crtc_state->stream,
  3399. &position);
  3400. return;
  3401. }
  3402. amdgpu_crtc->cursor_width = plane->state->crtc_w;
  3403. amdgpu_crtc->cursor_height = plane->state->crtc_h;
  3404. attributes.address.high_part = upper_32_bits(address);
  3405. attributes.address.low_part = lower_32_bits(address);
  3406. attributes.width = plane->state->crtc_w;
  3407. attributes.height = plane->state->crtc_h;
  3408. attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
  3409. attributes.rotation_angle = 0;
  3410. attributes.attribute_flags.value = 0;
  3411. attributes.pitch = attributes.width;
  3412. if (crtc_state->stream) {
  3413. if (!dc_stream_set_cursor_attributes(crtc_state->stream,
  3414. &attributes))
  3415. DRM_ERROR("DC failed to set cursor attributes\n");
  3416. if (!dc_stream_set_cursor_position(crtc_state->stream,
  3417. &position))
  3418. DRM_ERROR("DC failed to set cursor position\n");
  3419. }
  3420. }
  3421. static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
  3422. {
  3423. assert_spin_locked(&acrtc->base.dev->event_lock);
  3424. WARN_ON(acrtc->event);
  3425. acrtc->event = acrtc->base.state->event;
  3426. /* Set the flip status */
  3427. acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
  3428. /* Mark this event as consumed */
  3429. acrtc->base.state->event = NULL;
  3430. DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
  3431. acrtc->crtc_id);
  3432. }
  3433. /*
  3434. * Executes flip
  3435. *
  3436. * Waits on all BO's fences and for proper vblank count
  3437. */
  3438. static void amdgpu_dm_do_flip(struct drm_crtc *crtc,
  3439. struct drm_framebuffer *fb,
  3440. uint32_t target,
  3441. struct dc_state *state)
  3442. {
  3443. unsigned long flags;
  3444. uint32_t target_vblank;
  3445. int r, vpos, hpos;
  3446. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3447. struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
  3448. struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]);
  3449. struct amdgpu_device *adev = crtc->dev->dev_private;
  3450. bool async_flip = (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
  3451. struct dc_flip_addrs addr = { {0} };
  3452. /* TODO eliminate or rename surface_update */
  3453. struct dc_surface_update surface_updates[1] = { {0} };
  3454. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
  3455. struct dc_stream_status *stream_status;
  3456. /* Prepare wait for target vblank early - before the fence-waits */
  3457. target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) +
  3458. amdgpu_get_vblank_counter_kms(crtc->dev, acrtc->crtc_id);
  3459. /*
  3460. * TODO This might fail and hence better not used, wait
  3461. * explicitly on fences instead
  3462. * and in general should be called for
  3463. * blocking commit to as per framework helpers
  3464. */
  3465. r = amdgpu_bo_reserve(abo, true);
  3466. if (unlikely(r != 0)) {
  3467. DRM_ERROR("failed to reserve buffer before flip\n");
  3468. WARN_ON(1);
  3469. }
  3470. /* Wait for all fences on this FB */
  3471. WARN_ON(reservation_object_wait_timeout_rcu(abo->tbo.resv, true, false,
  3472. MAX_SCHEDULE_TIMEOUT) < 0);
  3473. amdgpu_bo_unreserve(abo);
  3474. /*
  3475. * Wait until we're out of the vertical blank period before the one
  3476. * targeted by the flip
  3477. */
  3478. while ((acrtc->enabled &&
  3479. (amdgpu_display_get_crtc_scanoutpos(adev->ddev, acrtc->crtc_id,
  3480. 0, &vpos, &hpos, NULL,
  3481. NULL, &crtc->hwmode)
  3482. & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
  3483. (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
  3484. (int)(target_vblank -
  3485. amdgpu_get_vblank_counter_kms(adev->ddev, acrtc->crtc_id)) > 0)) {
  3486. usleep_range(1000, 1100);
  3487. }
  3488. /* Flip */
  3489. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  3490. WARN_ON(acrtc->pflip_status != AMDGPU_FLIP_NONE);
  3491. WARN_ON(!acrtc_state->stream);
  3492. addr.address.grph.addr.low_part = lower_32_bits(afb->address);
  3493. addr.address.grph.addr.high_part = upper_32_bits(afb->address);
  3494. addr.flip_immediate = async_flip;
  3495. if (acrtc->base.state->event)
  3496. prepare_flip_isr(acrtc);
  3497. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  3498. stream_status = dc_stream_get_status(acrtc_state->stream);
  3499. if (!stream_status) {
  3500. DRM_ERROR("No stream status for CRTC: id=%d\n",
  3501. acrtc->crtc_id);
  3502. return;
  3503. }
  3504. surface_updates->surface = stream_status->plane_states[0];
  3505. if (!surface_updates->surface) {
  3506. DRM_ERROR("No surface for CRTC: id=%d\n",
  3507. acrtc->crtc_id);
  3508. return;
  3509. }
  3510. surface_updates->flip_addr = &addr;
  3511. dc_commit_updates_for_stream(adev->dm.dc,
  3512. surface_updates,
  3513. 1,
  3514. acrtc_state->stream,
  3515. NULL,
  3516. &surface_updates->surface,
  3517. state);
  3518. DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x \n",
  3519. __func__,
  3520. addr.address.grph.addr.high_part,
  3521. addr.address.grph.addr.low_part);
  3522. }
  3523. /*
  3524. * TODO this whole function needs to go
  3525. *
  3526. * dc_surface_update is needlessly complex. See if we can just replace this
  3527. * with a dc_plane_state and follow the atomic model a bit more closely here.
  3528. */
  3529. static bool commit_planes_to_stream(
  3530. struct dc *dc,
  3531. struct dc_plane_state **plane_states,
  3532. uint8_t new_plane_count,
  3533. struct dm_crtc_state *dm_new_crtc_state,
  3534. struct dm_crtc_state *dm_old_crtc_state,
  3535. struct dc_state *state)
  3536. {
  3537. /* no need to dynamically allocate this. it's pretty small */
  3538. struct dc_surface_update updates[MAX_SURFACES];
  3539. struct dc_flip_addrs *flip_addr;
  3540. struct dc_plane_info *plane_info;
  3541. struct dc_scaling_info *scaling_info;
  3542. int i;
  3543. struct dc_stream_state *dc_stream = dm_new_crtc_state->stream;
  3544. struct dc_stream_update *stream_update =
  3545. kzalloc(sizeof(struct dc_stream_update), GFP_KERNEL);
  3546. if (!stream_update) {
  3547. BREAK_TO_DEBUGGER();
  3548. return false;
  3549. }
  3550. flip_addr = kcalloc(MAX_SURFACES, sizeof(struct dc_flip_addrs),
  3551. GFP_KERNEL);
  3552. plane_info = kcalloc(MAX_SURFACES, sizeof(struct dc_plane_info),
  3553. GFP_KERNEL);
  3554. scaling_info = kcalloc(MAX_SURFACES, sizeof(struct dc_scaling_info),
  3555. GFP_KERNEL);
  3556. if (!flip_addr || !plane_info || !scaling_info) {
  3557. kfree(flip_addr);
  3558. kfree(plane_info);
  3559. kfree(scaling_info);
  3560. kfree(stream_update);
  3561. return false;
  3562. }
  3563. memset(updates, 0, sizeof(updates));
  3564. stream_update->src = dc_stream->src;
  3565. stream_update->dst = dc_stream->dst;
  3566. stream_update->out_transfer_func = dc_stream->out_transfer_func;
  3567. if (dm_new_crtc_state->freesync_enabled != dm_old_crtc_state->freesync_enabled) {
  3568. stream_update->vrr_infopacket = &dc_stream->vrr_infopacket;
  3569. stream_update->adjust = &dc_stream->adjust;
  3570. }
  3571. for (i = 0; i < new_plane_count; i++) {
  3572. updates[i].surface = plane_states[i];
  3573. updates[i].gamma =
  3574. (struct dc_gamma *)plane_states[i]->gamma_correction;
  3575. updates[i].in_transfer_func = plane_states[i]->in_transfer_func;
  3576. flip_addr[i].address = plane_states[i]->address;
  3577. flip_addr[i].flip_immediate = plane_states[i]->flip_immediate;
  3578. plane_info[i].color_space = plane_states[i]->color_space;
  3579. plane_info[i].format = plane_states[i]->format;
  3580. plane_info[i].plane_size = plane_states[i]->plane_size;
  3581. plane_info[i].rotation = plane_states[i]->rotation;
  3582. plane_info[i].horizontal_mirror = plane_states[i]->horizontal_mirror;
  3583. plane_info[i].stereo_format = plane_states[i]->stereo_format;
  3584. plane_info[i].tiling_info = plane_states[i]->tiling_info;
  3585. plane_info[i].visible = plane_states[i]->visible;
  3586. plane_info[i].per_pixel_alpha = plane_states[i]->per_pixel_alpha;
  3587. plane_info[i].dcc = plane_states[i]->dcc;
  3588. scaling_info[i].scaling_quality = plane_states[i]->scaling_quality;
  3589. scaling_info[i].src_rect = plane_states[i]->src_rect;
  3590. scaling_info[i].dst_rect = plane_states[i]->dst_rect;
  3591. scaling_info[i].clip_rect = plane_states[i]->clip_rect;
  3592. updates[i].flip_addr = &flip_addr[i];
  3593. updates[i].plane_info = &plane_info[i];
  3594. updates[i].scaling_info = &scaling_info[i];
  3595. }
  3596. dc_commit_updates_for_stream(
  3597. dc,
  3598. updates,
  3599. new_plane_count,
  3600. dc_stream, stream_update, plane_states, state);
  3601. kfree(flip_addr);
  3602. kfree(plane_info);
  3603. kfree(scaling_info);
  3604. kfree(stream_update);
  3605. return true;
  3606. }
  3607. static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
  3608. struct drm_device *dev,
  3609. struct amdgpu_display_manager *dm,
  3610. struct drm_crtc *pcrtc,
  3611. bool *wait_for_vblank)
  3612. {
  3613. uint32_t i;
  3614. struct drm_plane *plane;
  3615. struct drm_plane_state *old_plane_state, *new_plane_state;
  3616. struct dc_stream_state *dc_stream_attach;
  3617. struct dc_plane_state *plane_states_constructed[MAX_SURFACES];
  3618. struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
  3619. struct drm_crtc_state *new_pcrtc_state =
  3620. drm_atomic_get_new_crtc_state(state, pcrtc);
  3621. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
  3622. struct dm_crtc_state *dm_old_crtc_state =
  3623. to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
  3624. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  3625. int planes_count = 0;
  3626. unsigned long flags;
  3627. /* update planes when needed */
  3628. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  3629. struct drm_crtc *crtc = new_plane_state->crtc;
  3630. struct drm_crtc_state *new_crtc_state;
  3631. struct drm_framebuffer *fb = new_plane_state->fb;
  3632. bool pflip_needed;
  3633. struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
  3634. if (plane->type == DRM_PLANE_TYPE_CURSOR) {
  3635. handle_cursor_update(plane, old_plane_state);
  3636. continue;
  3637. }
  3638. if (!fb || !crtc || pcrtc != crtc)
  3639. continue;
  3640. new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  3641. if (!new_crtc_state->active)
  3642. continue;
  3643. pflip_needed = !state->allow_modeset;
  3644. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  3645. if (acrtc_attach->pflip_status != AMDGPU_FLIP_NONE) {
  3646. DRM_ERROR("%s: acrtc %d, already busy\n",
  3647. __func__,
  3648. acrtc_attach->crtc_id);
  3649. /* In commit tail framework this cannot happen */
  3650. WARN_ON(1);
  3651. }
  3652. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  3653. if (!pflip_needed || plane->type == DRM_PLANE_TYPE_OVERLAY) {
  3654. WARN_ON(!dm_new_plane_state->dc_state);
  3655. plane_states_constructed[planes_count] = dm_new_plane_state->dc_state;
  3656. dc_stream_attach = acrtc_state->stream;
  3657. planes_count++;
  3658. } else if (new_crtc_state->planes_changed) {
  3659. /* Assume even ONE crtc with immediate flip means
  3660. * entire can't wait for VBLANK
  3661. * TODO Check if it's correct
  3662. */
  3663. *wait_for_vblank =
  3664. new_pcrtc_state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC ?
  3665. false : true;
  3666. /* TODO: Needs rework for multiplane flip */
  3667. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  3668. drm_crtc_vblank_get(crtc);
  3669. amdgpu_dm_do_flip(
  3670. crtc,
  3671. fb,
  3672. (uint32_t)drm_crtc_vblank_count(crtc) + *wait_for_vblank,
  3673. dm_state->context);
  3674. }
  3675. }
  3676. if (planes_count) {
  3677. unsigned long flags;
  3678. if (new_pcrtc_state->event) {
  3679. drm_crtc_vblank_get(pcrtc);
  3680. spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
  3681. prepare_flip_isr(acrtc_attach);
  3682. spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
  3683. }
  3684. dc_stream_attach->adjust = acrtc_state->adjust;
  3685. dc_stream_attach->vrr_infopacket = acrtc_state->vrr_infopacket;
  3686. if (false == commit_planes_to_stream(dm->dc,
  3687. plane_states_constructed,
  3688. planes_count,
  3689. acrtc_state,
  3690. dm_old_crtc_state,
  3691. dm_state->context))
  3692. dm_error("%s: Failed to attach plane!\n", __func__);
  3693. } else {
  3694. /*TODO BUG Here should go disable planes on CRTC. */
  3695. }
  3696. }
  3697. /*
  3698. * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
  3699. * @crtc_state: the DRM CRTC state
  3700. * @stream_state: the DC stream state.
  3701. *
  3702. * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
  3703. * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
  3704. */
  3705. static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
  3706. struct dc_stream_state *stream_state)
  3707. {
  3708. stream_state->mode_changed = crtc_state->mode_changed;
  3709. }
  3710. static int amdgpu_dm_atomic_commit(struct drm_device *dev,
  3711. struct drm_atomic_state *state,
  3712. bool nonblock)
  3713. {
  3714. struct drm_crtc *crtc;
  3715. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  3716. struct amdgpu_device *adev = dev->dev_private;
  3717. int i;
  3718. /*
  3719. * We evade vblanks and pflips on crtc that
  3720. * should be changed. We do it here to flush & disable
  3721. * interrupts before drm_swap_state is called in drm_atomic_helper_commit
  3722. * it will update crtc->dm_crtc_state->stream pointer which is used in
  3723. * the ISRs.
  3724. */
  3725. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  3726. struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  3727. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3728. if (drm_atomic_crtc_needs_modeset(new_crtc_state) && dm_old_crtc_state->stream)
  3729. manage_dm_interrupts(adev, acrtc, false);
  3730. }
  3731. /*
  3732. * Add check here for SoC's that support hardware cursor plane, to
  3733. * unset legacy_cursor_update
  3734. */
  3735. return drm_atomic_helper_commit(dev, state, nonblock);
  3736. /*TODO Handle EINTR, reenable IRQ*/
  3737. }
  3738. static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
  3739. {
  3740. struct drm_device *dev = state->dev;
  3741. struct amdgpu_device *adev = dev->dev_private;
  3742. struct amdgpu_display_manager *dm = &adev->dm;
  3743. struct dm_atomic_state *dm_state;
  3744. uint32_t i, j;
  3745. struct drm_crtc *crtc;
  3746. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  3747. unsigned long flags;
  3748. bool wait_for_vblank = true;
  3749. struct drm_connector *connector;
  3750. struct drm_connector_state *old_con_state, *new_con_state;
  3751. struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
  3752. int crtc_disable_count = 0;
  3753. drm_atomic_helper_update_legacy_modeset_state(dev, state);
  3754. dm_state = to_dm_atomic_state(state);
  3755. /* update changed items */
  3756. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  3757. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3758. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3759. dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  3760. DRM_DEBUG_DRIVER(
  3761. "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
  3762. "planes_changed:%d, mode_changed:%d,active_changed:%d,"
  3763. "connectors_changed:%d\n",
  3764. acrtc->crtc_id,
  3765. new_crtc_state->enable,
  3766. new_crtc_state->active,
  3767. new_crtc_state->planes_changed,
  3768. new_crtc_state->mode_changed,
  3769. new_crtc_state->active_changed,
  3770. new_crtc_state->connectors_changed);
  3771. /* Copy all transient state flags into dc state */
  3772. if (dm_new_crtc_state->stream) {
  3773. amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
  3774. dm_new_crtc_state->stream);
  3775. }
  3776. /* handles headless hotplug case, updating new_state and
  3777. * aconnector as needed
  3778. */
  3779. if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
  3780. DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
  3781. if (!dm_new_crtc_state->stream) {
  3782. /*
  3783. * this could happen because of issues with
  3784. * userspace notifications delivery.
  3785. * In this case userspace tries to set mode on
  3786. * display which is disconnected in fact.
  3787. * dc_sink is NULL in this case on aconnector.
  3788. * We expect reset mode will come soon.
  3789. *
  3790. * This can also happen when unplug is done
  3791. * during resume sequence ended
  3792. *
  3793. * In this case, we want to pretend we still
  3794. * have a sink to keep the pipe running so that
  3795. * hw state is consistent with the sw state
  3796. */
  3797. DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
  3798. __func__, acrtc->base.base.id);
  3799. continue;
  3800. }
  3801. if (dm_old_crtc_state->stream)
  3802. remove_stream(adev, acrtc, dm_old_crtc_state->stream);
  3803. pm_runtime_get_noresume(dev->dev);
  3804. acrtc->enabled = true;
  3805. acrtc->hw_mode = new_crtc_state->mode;
  3806. crtc->hwmode = new_crtc_state->mode;
  3807. } else if (modereset_required(new_crtc_state)) {
  3808. DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
  3809. /* i.e. reset mode */
  3810. if (dm_old_crtc_state->stream)
  3811. remove_stream(adev, acrtc, dm_old_crtc_state->stream);
  3812. }
  3813. } /* for_each_crtc_in_state() */
  3814. if (dm_state->context) {
  3815. dm_enable_per_frame_crtc_master_sync(dm_state->context);
  3816. WARN_ON(!dc_commit_state(dm->dc, dm_state->context));
  3817. }
  3818. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  3819. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3820. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3821. if (dm_new_crtc_state->stream != NULL) {
  3822. const struct dc_stream_status *status =
  3823. dc_stream_get_status(dm_new_crtc_state->stream);
  3824. if (!status)
  3825. DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
  3826. else
  3827. acrtc->otg_inst = status->primary_otg_inst;
  3828. }
  3829. }
  3830. /* Handle scaling and underscan changes*/
  3831. for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
  3832. struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
  3833. struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
  3834. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
  3835. struct dc_stream_status *status = NULL;
  3836. if (acrtc) {
  3837. new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
  3838. old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
  3839. }
  3840. /* Skip any modesets/resets */
  3841. if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
  3842. continue;
  3843. /* Skip anything that is not scaling or underscan changes */
  3844. if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
  3845. continue;
  3846. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3847. update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
  3848. dm_new_con_state, (struct dc_stream_state *)dm_new_crtc_state->stream);
  3849. if (!dm_new_crtc_state->stream)
  3850. continue;
  3851. status = dc_stream_get_status(dm_new_crtc_state->stream);
  3852. WARN_ON(!status);
  3853. WARN_ON(!status->plane_count);
  3854. dm_new_crtc_state->stream->adjust = dm_new_crtc_state->adjust;
  3855. dm_new_crtc_state->stream->vrr_infopacket = dm_new_crtc_state->vrr_infopacket;
  3856. /*TODO How it works with MPO ?*/
  3857. if (!commit_planes_to_stream(
  3858. dm->dc,
  3859. status->plane_states,
  3860. status->plane_count,
  3861. dm_new_crtc_state,
  3862. to_dm_crtc_state(old_crtc_state),
  3863. dm_state->context))
  3864. dm_error("%s: Failed to update stream scaling!\n", __func__);
  3865. }
  3866. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  3867. new_crtc_state, i) {
  3868. /*
  3869. * loop to enable interrupts on newly arrived crtc
  3870. */
  3871. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3872. bool modeset_needed;
  3873. if (old_crtc_state->active && !new_crtc_state->active)
  3874. crtc_disable_count++;
  3875. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3876. dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  3877. modeset_needed = modeset_required(
  3878. new_crtc_state,
  3879. dm_new_crtc_state->stream,
  3880. dm_old_crtc_state->stream);
  3881. if (dm_new_crtc_state->stream == NULL || !modeset_needed)
  3882. continue;
  3883. manage_dm_interrupts(adev, acrtc, true);
  3884. }
  3885. /* update planes when needed per crtc*/
  3886. for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
  3887. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3888. if (dm_new_crtc_state->stream)
  3889. amdgpu_dm_commit_planes(state, dev, dm, crtc, &wait_for_vblank);
  3890. }
  3891. /*
  3892. * send vblank event on all events not handled in flip and
  3893. * mark consumed event for drm_atomic_helper_commit_hw_done
  3894. */
  3895. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  3896. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  3897. if (new_crtc_state->event)
  3898. drm_send_event_locked(dev, &new_crtc_state->event->base);
  3899. new_crtc_state->event = NULL;
  3900. }
  3901. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  3902. if (wait_for_vblank)
  3903. drm_atomic_helper_wait_for_flip_done(dev, state);
  3904. /*
  3905. * FIXME:
  3906. * Delay hw_done() until flip_done() is signaled. This is to block
  3907. * another commit from freeing the CRTC state while we're still
  3908. * waiting on flip_done.
  3909. */
  3910. drm_atomic_helper_commit_hw_done(state);
  3911. drm_atomic_helper_cleanup_planes(dev, state);
  3912. /*
  3913. * Finally, drop a runtime PM reference for each newly disabled CRTC,
  3914. * so we can put the GPU into runtime suspend if we're not driving any
  3915. * displays anymore
  3916. */
  3917. for (i = 0; i < crtc_disable_count; i++)
  3918. pm_runtime_put_autosuspend(dev->dev);
  3919. pm_runtime_mark_last_busy(dev->dev);
  3920. }
  3921. static int dm_force_atomic_commit(struct drm_connector *connector)
  3922. {
  3923. int ret = 0;
  3924. struct drm_device *ddev = connector->dev;
  3925. struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
  3926. struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
  3927. struct drm_plane *plane = disconnected_acrtc->base.primary;
  3928. struct drm_connector_state *conn_state;
  3929. struct drm_crtc_state *crtc_state;
  3930. struct drm_plane_state *plane_state;
  3931. if (!state)
  3932. return -ENOMEM;
  3933. state->acquire_ctx = ddev->mode_config.acquire_ctx;
  3934. /* Construct an atomic state to restore previous display setting */
  3935. /*
  3936. * Attach connectors to drm_atomic_state
  3937. */
  3938. conn_state = drm_atomic_get_connector_state(state, connector);
  3939. ret = PTR_ERR_OR_ZERO(conn_state);
  3940. if (ret)
  3941. goto err;
  3942. /* Attach crtc to drm_atomic_state*/
  3943. crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
  3944. ret = PTR_ERR_OR_ZERO(crtc_state);
  3945. if (ret)
  3946. goto err;
  3947. /* force a restore */
  3948. crtc_state->mode_changed = true;
  3949. /* Attach plane to drm_atomic_state */
  3950. plane_state = drm_atomic_get_plane_state(state, plane);
  3951. ret = PTR_ERR_OR_ZERO(plane_state);
  3952. if (ret)
  3953. goto err;
  3954. /* Call commit internally with the state we just constructed */
  3955. ret = drm_atomic_commit(state);
  3956. if (!ret)
  3957. return 0;
  3958. err:
  3959. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3960. drm_atomic_state_put(state);
  3961. return ret;
  3962. }
  3963. /*
  3964. * This function handles all cases when set mode does not come upon hotplug.
  3965. * This includes when a display is unplugged then plugged back into the
  3966. * same port and when running without usermode desktop manager supprot
  3967. */
  3968. void dm_restore_drm_connector_state(struct drm_device *dev,
  3969. struct drm_connector *connector)
  3970. {
  3971. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  3972. struct amdgpu_crtc *disconnected_acrtc;
  3973. struct dm_crtc_state *acrtc_state;
  3974. if (!aconnector->dc_sink || !connector->state || !connector->encoder)
  3975. return;
  3976. disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
  3977. if (!disconnected_acrtc)
  3978. return;
  3979. acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
  3980. if (!acrtc_state->stream)
  3981. return;
  3982. /*
  3983. * If the previous sink is not released and different from the current,
  3984. * we deduce we are in a state where we can not rely on usermode call
  3985. * to turn on the display, so we do it here
  3986. */
  3987. if (acrtc_state->stream->sink != aconnector->dc_sink)
  3988. dm_force_atomic_commit(&aconnector->base);
  3989. }
  3990. /*
  3991. * Grabs all modesetting locks to serialize against any blocking commits,
  3992. * Waits for completion of all non blocking commits.
  3993. */
  3994. static int do_aquire_global_lock(struct drm_device *dev,
  3995. struct drm_atomic_state *state)
  3996. {
  3997. struct drm_crtc *crtc;
  3998. struct drm_crtc_commit *commit;
  3999. long ret;
  4000. /*
  4001. * Adding all modeset locks to aquire_ctx will
  4002. * ensure that when the framework release it the
  4003. * extra locks we are locking here will get released to
  4004. */
  4005. ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
  4006. if (ret)
  4007. return ret;
  4008. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4009. spin_lock(&crtc->commit_lock);
  4010. commit = list_first_entry_or_null(&crtc->commit_list,
  4011. struct drm_crtc_commit, commit_entry);
  4012. if (commit)
  4013. drm_crtc_commit_get(commit);
  4014. spin_unlock(&crtc->commit_lock);
  4015. if (!commit)
  4016. continue;
  4017. /*
  4018. * Make sure all pending HW programming completed and
  4019. * page flips done
  4020. */
  4021. ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
  4022. if (ret > 0)
  4023. ret = wait_for_completion_interruptible_timeout(
  4024. &commit->flip_done, 10*HZ);
  4025. if (ret == 0)
  4026. DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
  4027. "timed out\n", crtc->base.id, crtc->name);
  4028. drm_crtc_commit_put(commit);
  4029. }
  4030. return ret < 0 ? ret : 0;
  4031. }
  4032. void set_freesync_on_stream(struct amdgpu_display_manager *dm,
  4033. struct dm_crtc_state *new_crtc_state,
  4034. struct dm_connector_state *new_con_state,
  4035. struct dc_stream_state *new_stream)
  4036. {
  4037. struct mod_freesync_config config = {0};
  4038. struct mod_vrr_params vrr = {0};
  4039. struct dc_info_packet vrr_infopacket = {0};
  4040. struct amdgpu_dm_connector *aconnector =
  4041. to_amdgpu_dm_connector(new_con_state->base.connector);
  4042. if (new_con_state->freesync_capable &&
  4043. new_con_state->freesync_enable) {
  4044. config.state = new_crtc_state->freesync_enabled ?
  4045. VRR_STATE_ACTIVE_VARIABLE :
  4046. VRR_STATE_INACTIVE;
  4047. config.min_refresh_in_uhz =
  4048. aconnector->min_vfreq * 1000000;
  4049. config.max_refresh_in_uhz =
  4050. aconnector->max_vfreq * 1000000;
  4051. config.vsif_supported = true;
  4052. }
  4053. mod_freesync_build_vrr_params(dm->freesync_module,
  4054. new_stream,
  4055. &config, &vrr);
  4056. mod_freesync_build_vrr_infopacket(dm->freesync_module,
  4057. new_stream,
  4058. &vrr,
  4059. packet_type_fs1,
  4060. NULL,
  4061. &vrr_infopacket);
  4062. new_crtc_state->adjust = vrr.adjust;
  4063. new_crtc_state->vrr_infopacket = vrr_infopacket;
  4064. }
  4065. static int dm_update_crtcs_state(struct amdgpu_display_manager *dm,
  4066. struct drm_atomic_state *state,
  4067. bool enable,
  4068. bool *lock_and_validation_needed)
  4069. {
  4070. struct drm_crtc *crtc;
  4071. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  4072. int i;
  4073. struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
  4074. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  4075. struct dc_stream_state *new_stream;
  4076. int ret = 0;
  4077. /*
  4078. * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
  4079. * update changed items
  4080. */
  4081. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  4082. struct amdgpu_crtc *acrtc = NULL;
  4083. struct amdgpu_dm_connector *aconnector = NULL;
  4084. struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
  4085. struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
  4086. struct drm_plane_state *new_plane_state = NULL;
  4087. new_stream = NULL;
  4088. dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  4089. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  4090. acrtc = to_amdgpu_crtc(crtc);
  4091. new_plane_state = drm_atomic_get_new_plane_state(state, new_crtc_state->crtc->primary);
  4092. if (new_crtc_state->enable && new_plane_state && !new_plane_state->fb) {
  4093. ret = -EINVAL;
  4094. goto fail;
  4095. }
  4096. aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
  4097. /* TODO This hack should go away */
  4098. if (aconnector && enable) {
  4099. /* Make sure fake sink is created in plug-in scenario */
  4100. drm_new_conn_state = drm_atomic_get_new_connector_state(state,
  4101. &aconnector->base);
  4102. drm_old_conn_state = drm_atomic_get_old_connector_state(state,
  4103. &aconnector->base);
  4104. if (IS_ERR(drm_new_conn_state)) {
  4105. ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
  4106. break;
  4107. }
  4108. dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
  4109. dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
  4110. new_stream = create_stream_for_sink(aconnector,
  4111. &new_crtc_state->mode,
  4112. dm_new_conn_state);
  4113. /*
  4114. * we can have no stream on ACTION_SET if a display
  4115. * was disconnected during S3, in this case it is not an
  4116. * error, the OS will be updated after detection, and
  4117. * will do the right thing on next atomic commit
  4118. */
  4119. if (!new_stream) {
  4120. DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
  4121. __func__, acrtc->base.base.id);
  4122. break;
  4123. }
  4124. set_freesync_on_stream(dm, dm_new_crtc_state,
  4125. dm_new_conn_state, new_stream);
  4126. if (dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
  4127. dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
  4128. new_crtc_state->mode_changed = false;
  4129. DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
  4130. new_crtc_state->mode_changed);
  4131. }
  4132. }
  4133. if (dm_old_crtc_state->freesync_enabled != dm_new_crtc_state->freesync_enabled)
  4134. new_crtc_state->mode_changed = true;
  4135. if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
  4136. goto next_crtc;
  4137. DRM_DEBUG_DRIVER(
  4138. "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
  4139. "planes_changed:%d, mode_changed:%d,active_changed:%d,"
  4140. "connectors_changed:%d\n",
  4141. acrtc->crtc_id,
  4142. new_crtc_state->enable,
  4143. new_crtc_state->active,
  4144. new_crtc_state->planes_changed,
  4145. new_crtc_state->mode_changed,
  4146. new_crtc_state->active_changed,
  4147. new_crtc_state->connectors_changed);
  4148. /* Remove stream for any changed/disabled CRTC */
  4149. if (!enable) {
  4150. if (!dm_old_crtc_state->stream)
  4151. goto next_crtc;
  4152. DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
  4153. crtc->base.id);
  4154. /* i.e. reset mode */
  4155. if (dc_remove_stream_from_ctx(
  4156. dm->dc,
  4157. dm_state->context,
  4158. dm_old_crtc_state->stream) != DC_OK) {
  4159. ret = -EINVAL;
  4160. goto fail;
  4161. }
  4162. dc_stream_release(dm_old_crtc_state->stream);
  4163. dm_new_crtc_state->stream = NULL;
  4164. *lock_and_validation_needed = true;
  4165. } else {/* Add stream for any updated/enabled CRTC */
  4166. /*
  4167. * Quick fix to prevent NULL pointer on new_stream when
  4168. * added MST connectors not found in existing crtc_state in the chained mode
  4169. * TODO: need to dig out the root cause of that
  4170. */
  4171. if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
  4172. goto next_crtc;
  4173. if (modereset_required(new_crtc_state))
  4174. goto next_crtc;
  4175. if (modeset_required(new_crtc_state, new_stream,
  4176. dm_old_crtc_state->stream)) {
  4177. WARN_ON(dm_new_crtc_state->stream);
  4178. dm_new_crtc_state->stream = new_stream;
  4179. dc_stream_retain(new_stream);
  4180. DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n",
  4181. crtc->base.id);
  4182. if (dc_add_stream_to_ctx(
  4183. dm->dc,
  4184. dm_state->context,
  4185. dm_new_crtc_state->stream) != DC_OK) {
  4186. ret = -EINVAL;
  4187. goto fail;
  4188. }
  4189. *lock_and_validation_needed = true;
  4190. }
  4191. }
  4192. next_crtc:
  4193. /* Release extra reference */
  4194. if (new_stream)
  4195. dc_stream_release(new_stream);
  4196. /*
  4197. * We want to do dc stream updates that do not require a
  4198. * full modeset below.
  4199. */
  4200. if (!(enable && aconnector && new_crtc_state->enable &&
  4201. new_crtc_state->active))
  4202. continue;
  4203. /*
  4204. * Given above conditions, the dc state cannot be NULL because:
  4205. * 1. We're in the process of enabling CRTCs (just been added
  4206. * to the dc context, or already is on the context)
  4207. * 2. Has a valid connector attached, and
  4208. * 3. Is currently active and enabled.
  4209. * => The dc stream state currently exists.
  4210. */
  4211. BUG_ON(dm_new_crtc_state->stream == NULL);
  4212. /* Scaling or underscan settings */
  4213. if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state))
  4214. update_stream_scaling_settings(
  4215. &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
  4216. /*
  4217. * Color management settings. We also update color properties
  4218. * when a modeset is needed, to ensure it gets reprogrammed.
  4219. */
  4220. if (dm_new_crtc_state->base.color_mgmt_changed ||
  4221. drm_atomic_crtc_needs_modeset(new_crtc_state)) {
  4222. ret = amdgpu_dm_set_regamma_lut(dm_new_crtc_state);
  4223. if (ret)
  4224. goto fail;
  4225. amdgpu_dm_set_ctm(dm_new_crtc_state);
  4226. }
  4227. }
  4228. return ret;
  4229. fail:
  4230. if (new_stream)
  4231. dc_stream_release(new_stream);
  4232. return ret;
  4233. }
  4234. static int dm_update_planes_state(struct dc *dc,
  4235. struct drm_atomic_state *state,
  4236. bool enable,
  4237. bool *lock_and_validation_needed)
  4238. {
  4239. struct drm_crtc *new_plane_crtc, *old_plane_crtc;
  4240. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  4241. struct drm_plane *plane;
  4242. struct drm_plane_state *old_plane_state, *new_plane_state;
  4243. struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
  4244. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  4245. struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
  4246. int i ;
  4247. /* TODO return page_flip_needed() function */
  4248. bool pflip_needed = !state->allow_modeset;
  4249. int ret = 0;
  4250. /* Add new planes, in reverse order as DC expectation */
  4251. for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
  4252. new_plane_crtc = new_plane_state->crtc;
  4253. old_plane_crtc = old_plane_state->crtc;
  4254. dm_new_plane_state = to_dm_plane_state(new_plane_state);
  4255. dm_old_plane_state = to_dm_plane_state(old_plane_state);
  4256. /*TODO Implement atomic check for cursor plane */
  4257. if (plane->type == DRM_PLANE_TYPE_CURSOR)
  4258. continue;
  4259. /* Remove any changed/removed planes */
  4260. if (!enable) {
  4261. if (pflip_needed &&
  4262. plane->type != DRM_PLANE_TYPE_OVERLAY)
  4263. continue;
  4264. if (!old_plane_crtc)
  4265. continue;
  4266. old_crtc_state = drm_atomic_get_old_crtc_state(
  4267. state, old_plane_crtc);
  4268. dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  4269. if (!dm_old_crtc_state->stream)
  4270. continue;
  4271. DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
  4272. plane->base.id, old_plane_crtc->base.id);
  4273. if (!dc_remove_plane_from_context(
  4274. dc,
  4275. dm_old_crtc_state->stream,
  4276. dm_old_plane_state->dc_state,
  4277. dm_state->context)) {
  4278. ret = EINVAL;
  4279. return ret;
  4280. }
  4281. dc_plane_state_release(dm_old_plane_state->dc_state);
  4282. dm_new_plane_state->dc_state = NULL;
  4283. *lock_and_validation_needed = true;
  4284. } else { /* Add new planes */
  4285. struct dc_plane_state *dc_new_plane_state;
  4286. if (drm_atomic_plane_disabling(plane->state, new_plane_state))
  4287. continue;
  4288. if (!new_plane_crtc)
  4289. continue;
  4290. new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
  4291. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  4292. if (!dm_new_crtc_state->stream)
  4293. continue;
  4294. if (pflip_needed &&
  4295. plane->type != DRM_PLANE_TYPE_OVERLAY)
  4296. continue;
  4297. WARN_ON(dm_new_plane_state->dc_state);
  4298. dc_new_plane_state = dc_create_plane_state(dc);
  4299. if (!dc_new_plane_state)
  4300. return -ENOMEM;
  4301. DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n",
  4302. plane->base.id, new_plane_crtc->base.id);
  4303. ret = fill_plane_attributes(
  4304. new_plane_crtc->dev->dev_private,
  4305. dc_new_plane_state,
  4306. new_plane_state,
  4307. new_crtc_state);
  4308. if (ret) {
  4309. dc_plane_state_release(dc_new_plane_state);
  4310. return ret;
  4311. }
  4312. /*
  4313. * Any atomic check errors that occur after this will
  4314. * not need a release. The plane state will be attached
  4315. * to the stream, and therefore part of the atomic
  4316. * state. It'll be released when the atomic state is
  4317. * cleaned.
  4318. */
  4319. if (!dc_add_plane_to_context(
  4320. dc,
  4321. dm_new_crtc_state->stream,
  4322. dc_new_plane_state,
  4323. dm_state->context)) {
  4324. dc_plane_state_release(dc_new_plane_state);
  4325. return -EINVAL;
  4326. }
  4327. dm_new_plane_state->dc_state = dc_new_plane_state;
  4328. /* Tell DC to do a full surface update every time there
  4329. * is a plane change. Inefficient, but works for now.
  4330. */
  4331. dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
  4332. *lock_and_validation_needed = true;
  4333. }
  4334. }
  4335. return ret;
  4336. }
  4337. enum surface_update_type dm_determine_update_type_for_commit(struct dc *dc, struct drm_atomic_state *state)
  4338. {
  4339. int i, j, num_plane;
  4340. struct drm_plane_state *old_plane_state, *new_plane_state;
  4341. struct dm_plane_state *new_dm_plane_state, *old_dm_plane_state;
  4342. struct drm_crtc *new_plane_crtc, *old_plane_crtc;
  4343. struct drm_plane *plane;
  4344. struct drm_crtc *crtc;
  4345. struct drm_crtc_state *new_crtc_state, *old_crtc_state;
  4346. struct dm_crtc_state *new_dm_crtc_state, *old_dm_crtc_state;
  4347. struct dc_stream_status *status = NULL;
  4348. struct dc_surface_update *updates = kzalloc(MAX_SURFACES * sizeof(struct dc_surface_update), GFP_KERNEL);
  4349. struct dc_plane_state *surface = kzalloc(MAX_SURFACES * sizeof(struct dc_plane_state), GFP_KERNEL);
  4350. struct dc_stream_update stream_update;
  4351. enum surface_update_type update_type = UPDATE_TYPE_FAST;
  4352. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  4353. new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
  4354. old_dm_crtc_state = to_dm_crtc_state(old_crtc_state);
  4355. num_plane = 0;
  4356. if (new_dm_crtc_state->stream) {
  4357. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, j) {
  4358. new_plane_crtc = new_plane_state->crtc;
  4359. old_plane_crtc = old_plane_state->crtc;
  4360. new_dm_plane_state = to_dm_plane_state(new_plane_state);
  4361. old_dm_plane_state = to_dm_plane_state(old_plane_state);
  4362. if (plane->type == DRM_PLANE_TYPE_CURSOR)
  4363. continue;
  4364. if (!state->allow_modeset)
  4365. continue;
  4366. if (crtc == new_plane_crtc) {
  4367. updates[num_plane].surface = &surface[num_plane];
  4368. if (new_crtc_state->mode_changed) {
  4369. updates[num_plane].surface->src_rect =
  4370. new_dm_plane_state->dc_state->src_rect;
  4371. updates[num_plane].surface->dst_rect =
  4372. new_dm_plane_state->dc_state->dst_rect;
  4373. updates[num_plane].surface->rotation =
  4374. new_dm_plane_state->dc_state->rotation;
  4375. updates[num_plane].surface->in_transfer_func =
  4376. new_dm_plane_state->dc_state->in_transfer_func;
  4377. stream_update.dst = new_dm_crtc_state->stream->dst;
  4378. stream_update.src = new_dm_crtc_state->stream->src;
  4379. }
  4380. if (new_crtc_state->color_mgmt_changed) {
  4381. updates[num_plane].gamma =
  4382. new_dm_plane_state->dc_state->gamma_correction;
  4383. updates[num_plane].in_transfer_func =
  4384. new_dm_plane_state->dc_state->in_transfer_func;
  4385. stream_update.gamut_remap =
  4386. &new_dm_crtc_state->stream->gamut_remap_matrix;
  4387. stream_update.out_transfer_func =
  4388. new_dm_crtc_state->stream->out_transfer_func;
  4389. }
  4390. num_plane++;
  4391. }
  4392. }
  4393. if (num_plane > 0) {
  4394. status = dc_stream_get_status(new_dm_crtc_state->stream);
  4395. update_type = dc_check_update_surfaces_for_stream(dc, updates, num_plane,
  4396. &stream_update, status);
  4397. if (update_type > UPDATE_TYPE_MED) {
  4398. update_type = UPDATE_TYPE_FULL;
  4399. goto ret;
  4400. }
  4401. }
  4402. } else if (!new_dm_crtc_state->stream && old_dm_crtc_state->stream) {
  4403. update_type = UPDATE_TYPE_FULL;
  4404. goto ret;
  4405. }
  4406. }
  4407. ret:
  4408. kfree(updates);
  4409. kfree(surface);
  4410. return update_type;
  4411. }
  4412. static int amdgpu_dm_atomic_check(struct drm_device *dev,
  4413. struct drm_atomic_state *state)
  4414. {
  4415. struct amdgpu_device *adev = dev->dev_private;
  4416. struct dc *dc = adev->dm.dc;
  4417. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  4418. struct drm_connector *connector;
  4419. struct drm_connector_state *old_con_state, *new_con_state;
  4420. struct drm_crtc *crtc;
  4421. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  4422. enum surface_update_type update_type = UPDATE_TYPE_FAST;
  4423. enum surface_update_type overall_update_type = UPDATE_TYPE_FAST;
  4424. int ret, i;
  4425. /*
  4426. * This bool will be set for true for any modeset/reset
  4427. * or plane update which implies non fast surface update.
  4428. */
  4429. bool lock_and_validation_needed = false;
  4430. ret = drm_atomic_helper_check_modeset(dev, state);
  4431. if (ret)
  4432. goto fail;
  4433. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  4434. struct dm_crtc_state *dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  4435. struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  4436. if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
  4437. !new_crtc_state->color_mgmt_changed &&
  4438. (dm_old_crtc_state->freesync_enabled == dm_new_crtc_state->freesync_enabled))
  4439. continue;
  4440. if (!new_crtc_state->enable)
  4441. continue;
  4442. ret = drm_atomic_add_affected_connectors(state, crtc);
  4443. if (ret)
  4444. return ret;
  4445. ret = drm_atomic_add_affected_planes(state, crtc);
  4446. if (ret)
  4447. goto fail;
  4448. }
  4449. dm_state->context = dc_create_state();
  4450. ASSERT(dm_state->context);
  4451. dc_resource_state_copy_construct_current(dc, dm_state->context);
  4452. /* Remove exiting planes if they are modified */
  4453. ret = dm_update_planes_state(dc, state, false, &lock_and_validation_needed);
  4454. if (ret) {
  4455. goto fail;
  4456. }
  4457. /* Disable all crtcs which require disable */
  4458. ret = dm_update_crtcs_state(&adev->dm, state, false, &lock_and_validation_needed);
  4459. if (ret) {
  4460. goto fail;
  4461. }
  4462. /* Enable all crtcs which require enable */
  4463. ret = dm_update_crtcs_state(&adev->dm, state, true, &lock_and_validation_needed);
  4464. if (ret) {
  4465. goto fail;
  4466. }
  4467. /* Add new/modified planes */
  4468. ret = dm_update_planes_state(dc, state, true, &lock_and_validation_needed);
  4469. if (ret) {
  4470. goto fail;
  4471. }
  4472. /* Run this here since we want to validate the streams we created */
  4473. ret = drm_atomic_helper_check_planes(dev, state);
  4474. if (ret)
  4475. goto fail;
  4476. /* Check scaling and underscan changes*/
  4477. /* TODO Removed scaling changes validation due to inability to commit
  4478. * new stream into context w\o causing full reset. Need to
  4479. * decide how to handle.
  4480. */
  4481. for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
  4482. struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
  4483. struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
  4484. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
  4485. /* Skip any modesets/resets */
  4486. if (!acrtc || drm_atomic_crtc_needs_modeset(
  4487. drm_atomic_get_new_crtc_state(state, &acrtc->base)))
  4488. continue;
  4489. /* Skip any thing not scale or underscan changes */
  4490. if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
  4491. continue;
  4492. overall_update_type = UPDATE_TYPE_FULL;
  4493. lock_and_validation_needed = true;
  4494. }
  4495. /*
  4496. * For full updates case when
  4497. * removing/adding/updating streams on one CRTC while flipping
  4498. * on another CRTC,
  4499. * acquiring global lock will guarantee that any such full
  4500. * update commit
  4501. * will wait for completion of any outstanding flip using DRMs
  4502. * synchronization events.
  4503. */
  4504. update_type = dm_determine_update_type_for_commit(dc, state);
  4505. if (overall_update_type < update_type)
  4506. overall_update_type = update_type;
  4507. /*
  4508. * lock_and_validation_needed was an old way to determine if we need to set
  4509. * the global lock. Leaving it in to check if we broke any corner cases
  4510. * lock_and_validation_needed true = UPDATE_TYPE_FULL or UPDATE_TYPE_MED
  4511. * lock_and_validation_needed false = UPDATE_TYPE_FAST
  4512. */
  4513. if (lock_and_validation_needed && overall_update_type <= UPDATE_TYPE_FAST)
  4514. WARN(1, "Global lock should be Set, overall_update_type should be UPDATE_TYPE_MED or UPDATE_TYPE_FULL");
  4515. else if (!lock_and_validation_needed && overall_update_type > UPDATE_TYPE_FAST)
  4516. WARN(1, "Global lock should NOT be set, overall_update_type should be UPDATE_TYPE_FAST");
  4517. if (overall_update_type > UPDATE_TYPE_FAST) {
  4518. ret = do_aquire_global_lock(dev, state);
  4519. if (ret)
  4520. goto fail;
  4521. if (dc_validate_global_state(dc, dm_state->context) != DC_OK) {
  4522. ret = -EINVAL;
  4523. goto fail;
  4524. }
  4525. }
  4526. /* Must be success */
  4527. WARN_ON(ret);
  4528. return ret;
  4529. fail:
  4530. if (ret == -EDEADLK)
  4531. DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
  4532. else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
  4533. DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
  4534. else
  4535. DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
  4536. return ret;
  4537. }
  4538. static bool is_dp_capable_without_timing_msa(struct dc *dc,
  4539. struct amdgpu_dm_connector *amdgpu_dm_connector)
  4540. {
  4541. uint8_t dpcd_data;
  4542. bool capable = false;
  4543. if (amdgpu_dm_connector->dc_link &&
  4544. dm_helpers_dp_read_dpcd(
  4545. NULL,
  4546. amdgpu_dm_connector->dc_link,
  4547. DP_DOWN_STREAM_PORT_COUNT,
  4548. &dpcd_data,
  4549. sizeof(dpcd_data))) {
  4550. capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
  4551. }
  4552. return capable;
  4553. }
  4554. void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
  4555. struct edid *edid)
  4556. {
  4557. int i;
  4558. bool edid_check_required;
  4559. struct detailed_timing *timing;
  4560. struct detailed_non_pixel *data;
  4561. struct detailed_data_monitor_range *range;
  4562. struct amdgpu_dm_connector *amdgpu_dm_connector =
  4563. to_amdgpu_dm_connector(connector);
  4564. struct dm_connector_state *dm_con_state;
  4565. struct drm_device *dev = connector->dev;
  4566. struct amdgpu_device *adev = dev->dev_private;
  4567. if (!connector->state) {
  4568. DRM_ERROR("%s - Connector has no state", __func__);
  4569. return;
  4570. }
  4571. if (!edid) {
  4572. dm_con_state = to_dm_connector_state(connector->state);
  4573. amdgpu_dm_connector->min_vfreq = 0;
  4574. amdgpu_dm_connector->max_vfreq = 0;
  4575. amdgpu_dm_connector->pixel_clock_mhz = 0;
  4576. dm_con_state->freesync_capable = false;
  4577. dm_con_state->freesync_enable = false;
  4578. return;
  4579. }
  4580. dm_con_state = to_dm_connector_state(connector->state);
  4581. edid_check_required = false;
  4582. if (!amdgpu_dm_connector->dc_sink) {
  4583. DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
  4584. return;
  4585. }
  4586. if (!adev->dm.freesync_module)
  4587. return;
  4588. /*
  4589. * if edid non zero restrict freesync only for dp and edp
  4590. */
  4591. if (edid) {
  4592. if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
  4593. || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
  4594. edid_check_required = is_dp_capable_without_timing_msa(
  4595. adev->dm.dc,
  4596. amdgpu_dm_connector);
  4597. }
  4598. }
  4599. dm_con_state->freesync_capable = false;
  4600. if (edid_check_required == true && (edid->version > 1 ||
  4601. (edid->version == 1 && edid->revision > 1))) {
  4602. for (i = 0; i < 4; i++) {
  4603. timing = &edid->detailed_timings[i];
  4604. data = &timing->data.other_data;
  4605. range = &data->data.range;
  4606. /*
  4607. * Check if monitor has continuous frequency mode
  4608. */
  4609. if (data->type != EDID_DETAIL_MONITOR_RANGE)
  4610. continue;
  4611. /*
  4612. * Check for flag range limits only. If flag == 1 then
  4613. * no additional timing information provided.
  4614. * Default GTF, GTF Secondary curve and CVT are not
  4615. * supported
  4616. */
  4617. if (range->flags != 1)
  4618. continue;
  4619. amdgpu_dm_connector->min_vfreq = range->min_vfreq;
  4620. amdgpu_dm_connector->max_vfreq = range->max_vfreq;
  4621. amdgpu_dm_connector->pixel_clock_mhz =
  4622. range->pixel_clock_mhz * 10;
  4623. break;
  4624. }
  4625. if (amdgpu_dm_connector->max_vfreq -
  4626. amdgpu_dm_connector->min_vfreq > 10) {
  4627. dm_con_state->freesync_capable = true;
  4628. }
  4629. }
  4630. }