amdgpu_dm.c 150 KB

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