amdgpu_dm.c 140 KB

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