amdgpu_dm.c 125 KB

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