amdgpu_dm.c 131 KB

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