amdgpu_dm.c 141 KB

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