amdgpu_dm.c 150 KB

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