amdgpu_dm.c 128 KB

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