amdgpu_dm.c 134 KB

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