amdgpu_dm.c 131 KB

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