amdgpu_dm.c 131 KB

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