dce_v11_0.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871
  1. /*
  2. * Copyright 2014 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. */
  23. #include "drmP.h"
  24. #include "amdgpu.h"
  25. #include "amdgpu_pm.h"
  26. #include "amdgpu_i2c.h"
  27. #include "vid.h"
  28. #include "atom.h"
  29. #include "amdgpu_atombios.h"
  30. #include "atombios_crtc.h"
  31. #include "atombios_encoders.h"
  32. #include "amdgpu_pll.h"
  33. #include "amdgpu_connectors.h"
  34. #include "dce_v11_0.h"
  35. #include "dce/dce_11_0_d.h"
  36. #include "dce/dce_11_0_sh_mask.h"
  37. #include "dce/dce_11_0_enum.h"
  38. #include "oss/oss_3_0_d.h"
  39. #include "oss/oss_3_0_sh_mask.h"
  40. #include "gmc/gmc_8_1_d.h"
  41. #include "gmc/gmc_8_1_sh_mask.h"
  42. static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev);
  43. static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev);
  44. static const u32 crtc_offsets[] =
  45. {
  46. CRTC0_REGISTER_OFFSET,
  47. CRTC1_REGISTER_OFFSET,
  48. CRTC2_REGISTER_OFFSET,
  49. CRTC3_REGISTER_OFFSET,
  50. CRTC4_REGISTER_OFFSET,
  51. CRTC5_REGISTER_OFFSET,
  52. CRTC6_REGISTER_OFFSET
  53. };
  54. static const u32 hpd_offsets[] =
  55. {
  56. HPD0_REGISTER_OFFSET,
  57. HPD1_REGISTER_OFFSET,
  58. HPD2_REGISTER_OFFSET,
  59. HPD3_REGISTER_OFFSET,
  60. HPD4_REGISTER_OFFSET,
  61. HPD5_REGISTER_OFFSET
  62. };
  63. static const uint32_t dig_offsets[] = {
  64. DIG0_REGISTER_OFFSET,
  65. DIG1_REGISTER_OFFSET,
  66. DIG2_REGISTER_OFFSET,
  67. DIG3_REGISTER_OFFSET,
  68. DIG4_REGISTER_OFFSET,
  69. DIG5_REGISTER_OFFSET,
  70. DIG6_REGISTER_OFFSET,
  71. DIG7_REGISTER_OFFSET,
  72. DIG8_REGISTER_OFFSET
  73. };
  74. static const struct {
  75. uint32_t reg;
  76. uint32_t vblank;
  77. uint32_t vline;
  78. uint32_t hpd;
  79. } interrupt_status_offsets[] = { {
  80. .reg = mmDISP_INTERRUPT_STATUS,
  81. .vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
  82. .vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
  83. .hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
  84. }, {
  85. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE,
  86. .vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
  87. .vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
  88. .hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
  89. }, {
  90. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE2,
  91. .vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
  92. .vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
  93. .hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
  94. }, {
  95. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE3,
  96. .vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
  97. .vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
  98. .hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
  99. }, {
  100. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE4,
  101. .vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
  102. .vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
  103. .hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
  104. }, {
  105. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE5,
  106. .vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
  107. .vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
  108. .hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
  109. } };
  110. static const u32 cz_golden_settings_a11[] =
  111. {
  112. mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
  113. mmFBC_MISC, 0x1f311fff, 0x14300000,
  114. };
  115. static const u32 cz_mgcg_cgcg_init[] =
  116. {
  117. mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
  118. mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
  119. };
  120. static const u32 stoney_golden_settings_a11[] =
  121. {
  122. mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
  123. mmFBC_MISC, 0x1f311fff, 0x14302000,
  124. };
  125. static const u32 polaris11_golden_settings_a11[] =
  126. {
  127. mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
  128. mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
  129. mmFBC_DEBUG1, 0xffffffff, 0x00000008,
  130. mmFBC_MISC, 0x9f313fff, 0x14302008,
  131. mmHDMI_CONTROL, 0x313f031f, 0x00000011,
  132. };
  133. static const u32 polaris10_golden_settings_a11[] =
  134. {
  135. mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
  136. mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
  137. mmFBC_MISC, 0x9f313fff, 0x14302008,
  138. mmHDMI_CONTROL, 0x313f031f, 0x00000011,
  139. };
  140. static void dce_v11_0_init_golden_registers(struct amdgpu_device *adev)
  141. {
  142. switch (adev->asic_type) {
  143. case CHIP_CARRIZO:
  144. amdgpu_program_register_sequence(adev,
  145. cz_mgcg_cgcg_init,
  146. (const u32)ARRAY_SIZE(cz_mgcg_cgcg_init));
  147. amdgpu_program_register_sequence(adev,
  148. cz_golden_settings_a11,
  149. (const u32)ARRAY_SIZE(cz_golden_settings_a11));
  150. break;
  151. case CHIP_STONEY:
  152. amdgpu_program_register_sequence(adev,
  153. stoney_golden_settings_a11,
  154. (const u32)ARRAY_SIZE(stoney_golden_settings_a11));
  155. break;
  156. case CHIP_POLARIS11:
  157. amdgpu_program_register_sequence(adev,
  158. polaris11_golden_settings_a11,
  159. (const u32)ARRAY_SIZE(polaris11_golden_settings_a11));
  160. break;
  161. case CHIP_POLARIS10:
  162. amdgpu_program_register_sequence(adev,
  163. polaris10_golden_settings_a11,
  164. (const u32)ARRAY_SIZE(polaris10_golden_settings_a11));
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. static u32 dce_v11_0_audio_endpt_rreg(struct amdgpu_device *adev,
  171. u32 block_offset, u32 reg)
  172. {
  173. unsigned long flags;
  174. u32 r;
  175. spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
  176. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
  177. r = RREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset);
  178. spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
  179. return r;
  180. }
  181. static void dce_v11_0_audio_endpt_wreg(struct amdgpu_device *adev,
  182. u32 block_offset, u32 reg, u32 v)
  183. {
  184. unsigned long flags;
  185. spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
  186. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
  187. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset, v);
  188. spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
  189. }
  190. static bool dce_v11_0_is_in_vblank(struct amdgpu_device *adev, int crtc)
  191. {
  192. if (RREG32(mmCRTC_STATUS + crtc_offsets[crtc]) &
  193. CRTC_V_BLANK_START_END__CRTC_V_BLANK_START_MASK)
  194. return true;
  195. else
  196. return false;
  197. }
  198. static bool dce_v11_0_is_counter_moving(struct amdgpu_device *adev, int crtc)
  199. {
  200. u32 pos1, pos2;
  201. pos1 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  202. pos2 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  203. if (pos1 != pos2)
  204. return true;
  205. else
  206. return false;
  207. }
  208. /**
  209. * dce_v11_0_vblank_wait - vblank wait asic callback.
  210. *
  211. * @adev: amdgpu_device pointer
  212. * @crtc: crtc to wait for vblank on
  213. *
  214. * Wait for vblank on the requested crtc (evergreen+).
  215. */
  216. static void dce_v11_0_vblank_wait(struct amdgpu_device *adev, int crtc)
  217. {
  218. unsigned i = 100;
  219. if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
  220. return;
  221. if (!(RREG32(mmCRTC_CONTROL + crtc_offsets[crtc]) & CRTC_CONTROL__CRTC_MASTER_EN_MASK))
  222. return;
  223. /* depending on when we hit vblank, we may be close to active; if so,
  224. * wait for another frame.
  225. */
  226. while (dce_v11_0_is_in_vblank(adev, crtc)) {
  227. if (i++ == 100) {
  228. i = 0;
  229. if (!dce_v11_0_is_counter_moving(adev, crtc))
  230. break;
  231. }
  232. }
  233. while (!dce_v11_0_is_in_vblank(adev, crtc)) {
  234. if (i++ == 100) {
  235. i = 0;
  236. if (!dce_v11_0_is_counter_moving(adev, crtc))
  237. break;
  238. }
  239. }
  240. }
  241. static u32 dce_v11_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
  242. {
  243. if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
  244. return 0;
  245. else
  246. return RREG32(mmCRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
  247. }
  248. static void dce_v11_0_pageflip_interrupt_init(struct amdgpu_device *adev)
  249. {
  250. unsigned i;
  251. /* Enable pflip interrupts */
  252. for (i = 0; i < adev->mode_info.num_crtc; i++)
  253. amdgpu_irq_get(adev, &adev->pageflip_irq, i);
  254. }
  255. static void dce_v11_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
  256. {
  257. unsigned i;
  258. /* Disable pflip interrupts */
  259. for (i = 0; i < adev->mode_info.num_crtc; i++)
  260. amdgpu_irq_put(adev, &adev->pageflip_irq, i);
  261. }
  262. /**
  263. * dce_v11_0_page_flip - pageflip callback.
  264. *
  265. * @adev: amdgpu_device pointer
  266. * @crtc_id: crtc to cleanup pageflip on
  267. * @crtc_base: new address of the crtc (GPU MC address)
  268. *
  269. * Triggers the actual pageflip by updating the primary
  270. * surface base address.
  271. */
  272. static void dce_v11_0_page_flip(struct amdgpu_device *adev,
  273. int crtc_id, u64 crtc_base, bool async)
  274. {
  275. struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  276. u32 tmp;
  277. /* flip immediate for async, default is vsync */
  278. tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
  279. tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
  280. GRPH_SURFACE_UPDATE_IMMEDIATE_EN, async ? 1 : 0);
  281. WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  282. /* update the scanout addresses */
  283. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  284. upper_32_bits(crtc_base));
  285. /* writing to the low address triggers the update */
  286. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  287. lower_32_bits(crtc_base));
  288. /* post the write */
  289. RREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
  290. }
  291. static int dce_v11_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
  292. u32 *vbl, u32 *position)
  293. {
  294. if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
  295. return -EINVAL;
  296. *vbl = RREG32(mmCRTC_V_BLANK_START_END + crtc_offsets[crtc]);
  297. *position = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  298. return 0;
  299. }
  300. /**
  301. * dce_v11_0_hpd_sense - hpd sense callback.
  302. *
  303. * @adev: amdgpu_device pointer
  304. * @hpd: hpd (hotplug detect) pin
  305. *
  306. * Checks if a digital monitor is connected (evergreen+).
  307. * Returns true if connected, false if not connected.
  308. */
  309. static bool dce_v11_0_hpd_sense(struct amdgpu_device *adev,
  310. enum amdgpu_hpd_id hpd)
  311. {
  312. bool connected = false;
  313. if (hpd >= adev->mode_info.num_hpd)
  314. return connected;
  315. if (RREG32(mmDC_HPD_INT_STATUS + hpd_offsets[hpd]) &
  316. DC_HPD_INT_STATUS__DC_HPD_SENSE_MASK)
  317. connected = true;
  318. return connected;
  319. }
  320. /**
  321. * dce_v11_0_hpd_set_polarity - hpd set polarity callback.
  322. *
  323. * @adev: amdgpu_device pointer
  324. * @hpd: hpd (hotplug detect) pin
  325. *
  326. * Set the polarity of the hpd pin (evergreen+).
  327. */
  328. static void dce_v11_0_hpd_set_polarity(struct amdgpu_device *adev,
  329. enum amdgpu_hpd_id hpd)
  330. {
  331. u32 tmp;
  332. bool connected = dce_v11_0_hpd_sense(adev, hpd);
  333. if (hpd >= adev->mode_info.num_hpd)
  334. return;
  335. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  336. if (connected)
  337. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 0);
  338. else
  339. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 1);
  340. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  341. }
  342. /**
  343. * dce_v11_0_hpd_init - hpd setup callback.
  344. *
  345. * @adev: amdgpu_device pointer
  346. *
  347. * Setup the hpd pins used by the card (evergreen+).
  348. * Enable the pin, set the polarity, and enable the hpd interrupts.
  349. */
  350. static void dce_v11_0_hpd_init(struct amdgpu_device *adev)
  351. {
  352. struct drm_device *dev = adev->ddev;
  353. struct drm_connector *connector;
  354. u32 tmp;
  355. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  356. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  357. if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
  358. continue;
  359. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
  360. connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
  361. /* don't try to enable hpd on eDP or LVDS avoid breaking the
  362. * aux dp channel on imac and help (but not completely fix)
  363. * https://bugzilla.redhat.com/show_bug.cgi?id=726143
  364. * also avoid interrupt storms during dpms.
  365. */
  366. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
  367. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
  368. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
  369. continue;
  370. }
  371. tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
  372. tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 1);
  373. WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
  374. tmp = RREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd]);
  375. tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
  376. DC_HPD_CONNECT_INT_DELAY,
  377. AMDGPU_HPD_CONNECT_INT_DELAY_IN_MS);
  378. tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
  379. DC_HPD_DISCONNECT_INT_DELAY,
  380. AMDGPU_HPD_DISCONNECT_INT_DELAY_IN_MS);
  381. WREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
  382. dce_v11_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
  383. amdgpu_irq_get(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
  384. }
  385. }
  386. /**
  387. * dce_v11_0_hpd_fini - hpd tear down callback.
  388. *
  389. * @adev: amdgpu_device pointer
  390. *
  391. * Tear down the hpd pins used by the card (evergreen+).
  392. * Disable the hpd interrupts.
  393. */
  394. static void dce_v11_0_hpd_fini(struct amdgpu_device *adev)
  395. {
  396. struct drm_device *dev = adev->ddev;
  397. struct drm_connector *connector;
  398. u32 tmp;
  399. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  400. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  401. if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
  402. continue;
  403. tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
  404. tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 0);
  405. WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
  406. amdgpu_irq_put(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
  407. }
  408. }
  409. static u32 dce_v11_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
  410. {
  411. return mmDC_GPIO_HPD_A;
  412. }
  413. static bool dce_v11_0_is_display_hung(struct amdgpu_device *adev)
  414. {
  415. u32 crtc_hung = 0;
  416. u32 crtc_status[6];
  417. u32 i, j, tmp;
  418. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  419. tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
  420. if (REG_GET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN)) {
  421. crtc_status[i] = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
  422. crtc_hung |= (1 << i);
  423. }
  424. }
  425. for (j = 0; j < 10; j++) {
  426. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  427. if (crtc_hung & (1 << i)) {
  428. tmp = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
  429. if (tmp != crtc_status[i])
  430. crtc_hung &= ~(1 << i);
  431. }
  432. }
  433. if (crtc_hung == 0)
  434. return false;
  435. udelay(100);
  436. }
  437. return true;
  438. }
  439. static void dce_v11_0_stop_mc_access(struct amdgpu_device *adev,
  440. struct amdgpu_mode_mc_save *save)
  441. {
  442. u32 crtc_enabled, tmp;
  443. int i;
  444. save->vga_render_control = RREG32(mmVGA_RENDER_CONTROL);
  445. save->vga_hdp_control = RREG32(mmVGA_HDP_CONTROL);
  446. /* disable VGA render */
  447. tmp = RREG32(mmVGA_RENDER_CONTROL);
  448. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
  449. WREG32(mmVGA_RENDER_CONTROL, tmp);
  450. /* blank the display controllers */
  451. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  452. crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
  453. CRTC_CONTROL, CRTC_MASTER_EN);
  454. if (crtc_enabled) {
  455. #if 1
  456. save->crtc_enabled[i] = true;
  457. tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
  458. if (REG_GET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN) == 0) {
  459. /*it is correct only for RGB ; black is 0*/
  460. WREG32(mmCRTC_BLANK_DATA_COLOR + crtc_offsets[i], 0);
  461. tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 1);
  462. WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
  463. }
  464. #else
  465. /* XXX this is a hack to avoid strange behavior with EFI on certain systems */
  466. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
  467. tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
  468. tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
  469. WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
  470. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
  471. save->crtc_enabled[i] = false;
  472. /* ***** */
  473. #endif
  474. } else {
  475. save->crtc_enabled[i] = false;
  476. }
  477. }
  478. }
  479. static void dce_v11_0_resume_mc_access(struct amdgpu_device *adev,
  480. struct amdgpu_mode_mc_save *save)
  481. {
  482. u32 tmp;
  483. int i;
  484. /* update crtc base addresses */
  485. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  486. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + crtc_offsets[i],
  487. upper_32_bits(adev->mc.vram_start));
  488. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + crtc_offsets[i],
  489. (u32)adev->mc.vram_start);
  490. if (save->crtc_enabled[i]) {
  491. tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
  492. tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 0);
  493. WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
  494. }
  495. }
  496. WREG32(mmVGA_MEMORY_BASE_ADDRESS_HIGH, upper_32_bits(adev->mc.vram_start));
  497. WREG32(mmVGA_MEMORY_BASE_ADDRESS, lower_32_bits(adev->mc.vram_start));
  498. /* Unlock vga access */
  499. WREG32(mmVGA_HDP_CONTROL, save->vga_hdp_control);
  500. mdelay(1);
  501. WREG32(mmVGA_RENDER_CONTROL, save->vga_render_control);
  502. }
  503. static void dce_v11_0_set_vga_render_state(struct amdgpu_device *adev,
  504. bool render)
  505. {
  506. u32 tmp;
  507. /* Lockout access through VGA aperture*/
  508. tmp = RREG32(mmVGA_HDP_CONTROL);
  509. if (render)
  510. tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 0);
  511. else
  512. tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
  513. WREG32(mmVGA_HDP_CONTROL, tmp);
  514. /* disable VGA render */
  515. tmp = RREG32(mmVGA_RENDER_CONTROL);
  516. if (render)
  517. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 1);
  518. else
  519. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
  520. WREG32(mmVGA_RENDER_CONTROL, tmp);
  521. }
  522. static int dce_v11_0_get_num_crtc (struct amdgpu_device *adev)
  523. {
  524. int num_crtc = 0;
  525. switch (adev->asic_type) {
  526. case CHIP_CARRIZO:
  527. num_crtc = 3;
  528. break;
  529. case CHIP_STONEY:
  530. num_crtc = 2;
  531. break;
  532. case CHIP_POLARIS10:
  533. num_crtc = 6;
  534. break;
  535. case CHIP_POLARIS11:
  536. num_crtc = 5;
  537. break;
  538. default:
  539. num_crtc = 0;
  540. }
  541. return num_crtc;
  542. }
  543. void dce_v11_0_disable_dce(struct amdgpu_device *adev)
  544. {
  545. /*Disable VGA render and enabled crtc, if has DCE engine*/
  546. if (amdgpu_atombios_has_dce_engine_info(adev)) {
  547. u32 tmp;
  548. int crtc_enabled, i;
  549. dce_v11_0_set_vga_render_state(adev, false);
  550. /*Disable crtc*/
  551. for (i = 0; i < dce_v11_0_get_num_crtc(adev); i++) {
  552. crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
  553. CRTC_CONTROL, CRTC_MASTER_EN);
  554. if (crtc_enabled) {
  555. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
  556. tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
  557. tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
  558. WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
  559. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
  560. }
  561. }
  562. }
  563. }
  564. static void dce_v11_0_program_fmt(struct drm_encoder *encoder)
  565. {
  566. struct drm_device *dev = encoder->dev;
  567. struct amdgpu_device *adev = dev->dev_private;
  568. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  569. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  570. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  571. int bpc = 0;
  572. u32 tmp = 0;
  573. enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
  574. if (connector) {
  575. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  576. bpc = amdgpu_connector_get_monitor_bpc(connector);
  577. dither = amdgpu_connector->dither;
  578. }
  579. /* LVDS/eDP FMT is set up by atom */
  580. if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
  581. return;
  582. /* not needed for analog */
  583. if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1) ||
  584. (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2))
  585. return;
  586. if (bpc == 0)
  587. return;
  588. switch (bpc) {
  589. case 6:
  590. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  591. /* XXX sort out optimal dither settings */
  592. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  593. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  594. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  595. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 0);
  596. } else {
  597. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  598. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 0);
  599. }
  600. break;
  601. case 8:
  602. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  603. /* XXX sort out optimal dither settings */
  604. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  605. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  606. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
  607. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  608. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 1);
  609. } else {
  610. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  611. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 1);
  612. }
  613. break;
  614. case 10:
  615. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  616. /* XXX sort out optimal dither settings */
  617. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  618. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  619. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
  620. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  621. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 2);
  622. } else {
  623. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  624. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 2);
  625. }
  626. break;
  627. default:
  628. /* not needed */
  629. break;
  630. }
  631. WREG32(mmFMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  632. }
  633. /* display watermark setup */
  634. /**
  635. * dce_v11_0_line_buffer_adjust - Set up the line buffer
  636. *
  637. * @adev: amdgpu_device pointer
  638. * @amdgpu_crtc: the selected display controller
  639. * @mode: the current display mode on the selected display
  640. * controller
  641. *
  642. * Setup up the line buffer allocation for
  643. * the selected display controller (CIK).
  644. * Returns the line buffer size in pixels.
  645. */
  646. static u32 dce_v11_0_line_buffer_adjust(struct amdgpu_device *adev,
  647. struct amdgpu_crtc *amdgpu_crtc,
  648. struct drm_display_mode *mode)
  649. {
  650. u32 tmp, buffer_alloc, i, mem_cfg;
  651. u32 pipe_offset = amdgpu_crtc->crtc_id;
  652. /*
  653. * Line Buffer Setup
  654. * There are 6 line buffers, one for each display controllers.
  655. * There are 3 partitions per LB. Select the number of partitions
  656. * to enable based on the display width. For display widths larger
  657. * than 4096, you need use to use 2 display controllers and combine
  658. * them using the stereo blender.
  659. */
  660. if (amdgpu_crtc->base.enabled && mode) {
  661. if (mode->crtc_hdisplay < 1920) {
  662. mem_cfg = 1;
  663. buffer_alloc = 2;
  664. } else if (mode->crtc_hdisplay < 2560) {
  665. mem_cfg = 2;
  666. buffer_alloc = 2;
  667. } else if (mode->crtc_hdisplay < 4096) {
  668. mem_cfg = 0;
  669. buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
  670. } else {
  671. DRM_DEBUG_KMS("Mode too big for LB!\n");
  672. mem_cfg = 0;
  673. buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
  674. }
  675. } else {
  676. mem_cfg = 1;
  677. buffer_alloc = 0;
  678. }
  679. tmp = RREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset);
  680. tmp = REG_SET_FIELD(tmp, LB_MEMORY_CTRL, LB_MEMORY_CONFIG, mem_cfg);
  681. WREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset, tmp);
  682. tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
  683. tmp = REG_SET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATED, buffer_alloc);
  684. WREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset, tmp);
  685. for (i = 0; i < adev->usec_timeout; i++) {
  686. tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
  687. if (REG_GET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATION_COMPLETED))
  688. break;
  689. udelay(1);
  690. }
  691. if (amdgpu_crtc->base.enabled && mode) {
  692. switch (mem_cfg) {
  693. case 0:
  694. default:
  695. return 4096 * 2;
  696. case 1:
  697. return 1920 * 2;
  698. case 2:
  699. return 2560 * 2;
  700. }
  701. }
  702. /* controller not enabled, so no lb used */
  703. return 0;
  704. }
  705. /**
  706. * cik_get_number_of_dram_channels - get the number of dram channels
  707. *
  708. * @adev: amdgpu_device pointer
  709. *
  710. * Look up the number of video ram channels (CIK).
  711. * Used for display watermark bandwidth calculations
  712. * Returns the number of dram channels
  713. */
  714. static u32 cik_get_number_of_dram_channels(struct amdgpu_device *adev)
  715. {
  716. u32 tmp = RREG32(mmMC_SHARED_CHMAP);
  717. switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
  718. case 0:
  719. default:
  720. return 1;
  721. case 1:
  722. return 2;
  723. case 2:
  724. return 4;
  725. case 3:
  726. return 8;
  727. case 4:
  728. return 3;
  729. case 5:
  730. return 6;
  731. case 6:
  732. return 10;
  733. case 7:
  734. return 12;
  735. case 8:
  736. return 16;
  737. }
  738. }
  739. struct dce10_wm_params {
  740. u32 dram_channels; /* number of dram channels */
  741. u32 yclk; /* bandwidth per dram data pin in kHz */
  742. u32 sclk; /* engine clock in kHz */
  743. u32 disp_clk; /* display clock in kHz */
  744. u32 src_width; /* viewport width */
  745. u32 active_time; /* active display time in ns */
  746. u32 blank_time; /* blank time in ns */
  747. bool interlaced; /* mode is interlaced */
  748. fixed20_12 vsc; /* vertical scale ratio */
  749. u32 num_heads; /* number of active crtcs */
  750. u32 bytes_per_pixel; /* bytes per pixel display + overlay */
  751. u32 lb_size; /* line buffer allocated to pipe */
  752. u32 vtaps; /* vertical scaler taps */
  753. };
  754. /**
  755. * dce_v11_0_dram_bandwidth - get the dram bandwidth
  756. *
  757. * @wm: watermark calculation data
  758. *
  759. * Calculate the raw dram bandwidth (CIK).
  760. * Used for display watermark bandwidth calculations
  761. * Returns the dram bandwidth in MBytes/s
  762. */
  763. static u32 dce_v11_0_dram_bandwidth(struct dce10_wm_params *wm)
  764. {
  765. /* Calculate raw DRAM Bandwidth */
  766. fixed20_12 dram_efficiency; /* 0.7 */
  767. fixed20_12 yclk, dram_channels, bandwidth;
  768. fixed20_12 a;
  769. a.full = dfixed_const(1000);
  770. yclk.full = dfixed_const(wm->yclk);
  771. yclk.full = dfixed_div(yclk, a);
  772. dram_channels.full = dfixed_const(wm->dram_channels * 4);
  773. a.full = dfixed_const(10);
  774. dram_efficiency.full = dfixed_const(7);
  775. dram_efficiency.full = dfixed_div(dram_efficiency, a);
  776. bandwidth.full = dfixed_mul(dram_channels, yclk);
  777. bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
  778. return dfixed_trunc(bandwidth);
  779. }
  780. /**
  781. * dce_v11_0_dram_bandwidth_for_display - get the dram bandwidth for display
  782. *
  783. * @wm: watermark calculation data
  784. *
  785. * Calculate the dram bandwidth used for display (CIK).
  786. * Used for display watermark bandwidth calculations
  787. * Returns the dram bandwidth for display in MBytes/s
  788. */
  789. static u32 dce_v11_0_dram_bandwidth_for_display(struct dce10_wm_params *wm)
  790. {
  791. /* Calculate DRAM Bandwidth and the part allocated to display. */
  792. fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
  793. fixed20_12 yclk, dram_channels, bandwidth;
  794. fixed20_12 a;
  795. a.full = dfixed_const(1000);
  796. yclk.full = dfixed_const(wm->yclk);
  797. yclk.full = dfixed_div(yclk, a);
  798. dram_channels.full = dfixed_const(wm->dram_channels * 4);
  799. a.full = dfixed_const(10);
  800. disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
  801. disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
  802. bandwidth.full = dfixed_mul(dram_channels, yclk);
  803. bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
  804. return dfixed_trunc(bandwidth);
  805. }
  806. /**
  807. * dce_v11_0_data_return_bandwidth - get the data return bandwidth
  808. *
  809. * @wm: watermark calculation data
  810. *
  811. * Calculate the data return bandwidth used for display (CIK).
  812. * Used for display watermark bandwidth calculations
  813. * Returns the data return bandwidth in MBytes/s
  814. */
  815. static u32 dce_v11_0_data_return_bandwidth(struct dce10_wm_params *wm)
  816. {
  817. /* Calculate the display Data return Bandwidth */
  818. fixed20_12 return_efficiency; /* 0.8 */
  819. fixed20_12 sclk, bandwidth;
  820. fixed20_12 a;
  821. a.full = dfixed_const(1000);
  822. sclk.full = dfixed_const(wm->sclk);
  823. sclk.full = dfixed_div(sclk, a);
  824. a.full = dfixed_const(10);
  825. return_efficiency.full = dfixed_const(8);
  826. return_efficiency.full = dfixed_div(return_efficiency, a);
  827. a.full = dfixed_const(32);
  828. bandwidth.full = dfixed_mul(a, sclk);
  829. bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
  830. return dfixed_trunc(bandwidth);
  831. }
  832. /**
  833. * dce_v11_0_dmif_request_bandwidth - get the dmif bandwidth
  834. *
  835. * @wm: watermark calculation data
  836. *
  837. * Calculate the dmif bandwidth used for display (CIK).
  838. * Used for display watermark bandwidth calculations
  839. * Returns the dmif bandwidth in MBytes/s
  840. */
  841. static u32 dce_v11_0_dmif_request_bandwidth(struct dce10_wm_params *wm)
  842. {
  843. /* Calculate the DMIF Request Bandwidth */
  844. fixed20_12 disp_clk_request_efficiency; /* 0.8 */
  845. fixed20_12 disp_clk, bandwidth;
  846. fixed20_12 a, b;
  847. a.full = dfixed_const(1000);
  848. disp_clk.full = dfixed_const(wm->disp_clk);
  849. disp_clk.full = dfixed_div(disp_clk, a);
  850. a.full = dfixed_const(32);
  851. b.full = dfixed_mul(a, disp_clk);
  852. a.full = dfixed_const(10);
  853. disp_clk_request_efficiency.full = dfixed_const(8);
  854. disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
  855. bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
  856. return dfixed_trunc(bandwidth);
  857. }
  858. /**
  859. * dce_v11_0_available_bandwidth - get the min available bandwidth
  860. *
  861. * @wm: watermark calculation data
  862. *
  863. * Calculate the min available bandwidth used for display (CIK).
  864. * Used for display watermark bandwidth calculations
  865. * Returns the min available bandwidth in MBytes/s
  866. */
  867. static u32 dce_v11_0_available_bandwidth(struct dce10_wm_params *wm)
  868. {
  869. /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
  870. u32 dram_bandwidth = dce_v11_0_dram_bandwidth(wm);
  871. u32 data_return_bandwidth = dce_v11_0_data_return_bandwidth(wm);
  872. u32 dmif_req_bandwidth = dce_v11_0_dmif_request_bandwidth(wm);
  873. return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
  874. }
  875. /**
  876. * dce_v11_0_average_bandwidth - get the average available bandwidth
  877. *
  878. * @wm: watermark calculation data
  879. *
  880. * Calculate the average available bandwidth used for display (CIK).
  881. * Used for display watermark bandwidth calculations
  882. * Returns the average available bandwidth in MBytes/s
  883. */
  884. static u32 dce_v11_0_average_bandwidth(struct dce10_wm_params *wm)
  885. {
  886. /* Calculate the display mode Average Bandwidth
  887. * DisplayMode should contain the source and destination dimensions,
  888. * timing, etc.
  889. */
  890. fixed20_12 bpp;
  891. fixed20_12 line_time;
  892. fixed20_12 src_width;
  893. fixed20_12 bandwidth;
  894. fixed20_12 a;
  895. a.full = dfixed_const(1000);
  896. line_time.full = dfixed_const(wm->active_time + wm->blank_time);
  897. line_time.full = dfixed_div(line_time, a);
  898. bpp.full = dfixed_const(wm->bytes_per_pixel);
  899. src_width.full = dfixed_const(wm->src_width);
  900. bandwidth.full = dfixed_mul(src_width, bpp);
  901. bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
  902. bandwidth.full = dfixed_div(bandwidth, line_time);
  903. return dfixed_trunc(bandwidth);
  904. }
  905. /**
  906. * dce_v11_0_latency_watermark - get the latency watermark
  907. *
  908. * @wm: watermark calculation data
  909. *
  910. * Calculate the latency watermark (CIK).
  911. * Used for display watermark bandwidth calculations
  912. * Returns the latency watermark in ns
  913. */
  914. static u32 dce_v11_0_latency_watermark(struct dce10_wm_params *wm)
  915. {
  916. /* First calculate the latency in ns */
  917. u32 mc_latency = 2000; /* 2000 ns. */
  918. u32 available_bandwidth = dce_v11_0_available_bandwidth(wm);
  919. u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
  920. u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
  921. u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
  922. u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
  923. (wm->num_heads * cursor_line_pair_return_time);
  924. u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
  925. u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
  926. u32 tmp, dmif_size = 12288;
  927. fixed20_12 a, b, c;
  928. if (wm->num_heads == 0)
  929. return 0;
  930. a.full = dfixed_const(2);
  931. b.full = dfixed_const(1);
  932. if ((wm->vsc.full > a.full) ||
  933. ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
  934. (wm->vtaps >= 5) ||
  935. ((wm->vsc.full >= a.full) && wm->interlaced))
  936. max_src_lines_per_dst_line = 4;
  937. else
  938. max_src_lines_per_dst_line = 2;
  939. a.full = dfixed_const(available_bandwidth);
  940. b.full = dfixed_const(wm->num_heads);
  941. a.full = dfixed_div(a, b);
  942. b.full = dfixed_const(mc_latency + 512);
  943. c.full = dfixed_const(wm->disp_clk);
  944. b.full = dfixed_div(b, c);
  945. c.full = dfixed_const(dmif_size);
  946. b.full = dfixed_div(c, b);
  947. tmp = min(dfixed_trunc(a), dfixed_trunc(b));
  948. b.full = dfixed_const(1000);
  949. c.full = dfixed_const(wm->disp_clk);
  950. b.full = dfixed_div(c, b);
  951. c.full = dfixed_const(wm->bytes_per_pixel);
  952. b.full = dfixed_mul(b, c);
  953. lb_fill_bw = min(tmp, dfixed_trunc(b));
  954. a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
  955. b.full = dfixed_const(1000);
  956. c.full = dfixed_const(lb_fill_bw);
  957. b.full = dfixed_div(c, b);
  958. a.full = dfixed_div(a, b);
  959. line_fill_time = dfixed_trunc(a);
  960. if (line_fill_time < wm->active_time)
  961. return latency;
  962. else
  963. return latency + (line_fill_time - wm->active_time);
  964. }
  965. /**
  966. * dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display - check
  967. * average and available dram bandwidth
  968. *
  969. * @wm: watermark calculation data
  970. *
  971. * Check if the display average bandwidth fits in the display
  972. * dram bandwidth (CIK).
  973. * Used for display watermark bandwidth calculations
  974. * Returns true if the display fits, false if not.
  975. */
  976. static bool dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params *wm)
  977. {
  978. if (dce_v11_0_average_bandwidth(wm) <=
  979. (dce_v11_0_dram_bandwidth_for_display(wm) / wm->num_heads))
  980. return true;
  981. else
  982. return false;
  983. }
  984. /**
  985. * dce_v11_0_average_bandwidth_vs_available_bandwidth - check
  986. * average and available bandwidth
  987. *
  988. * @wm: watermark calculation data
  989. *
  990. * Check if the display average bandwidth fits in the display
  991. * available bandwidth (CIK).
  992. * Used for display watermark bandwidth calculations
  993. * Returns true if the display fits, false if not.
  994. */
  995. static bool dce_v11_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params *wm)
  996. {
  997. if (dce_v11_0_average_bandwidth(wm) <=
  998. (dce_v11_0_available_bandwidth(wm) / wm->num_heads))
  999. return true;
  1000. else
  1001. return false;
  1002. }
  1003. /**
  1004. * dce_v11_0_check_latency_hiding - check latency hiding
  1005. *
  1006. * @wm: watermark calculation data
  1007. *
  1008. * Check latency hiding (CIK).
  1009. * Used for display watermark bandwidth calculations
  1010. * Returns true if the display fits, false if not.
  1011. */
  1012. static bool dce_v11_0_check_latency_hiding(struct dce10_wm_params *wm)
  1013. {
  1014. u32 lb_partitions = wm->lb_size / wm->src_width;
  1015. u32 line_time = wm->active_time + wm->blank_time;
  1016. u32 latency_tolerant_lines;
  1017. u32 latency_hiding;
  1018. fixed20_12 a;
  1019. a.full = dfixed_const(1);
  1020. if (wm->vsc.full > a.full)
  1021. latency_tolerant_lines = 1;
  1022. else {
  1023. if (lb_partitions <= (wm->vtaps + 1))
  1024. latency_tolerant_lines = 1;
  1025. else
  1026. latency_tolerant_lines = 2;
  1027. }
  1028. latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
  1029. if (dce_v11_0_latency_watermark(wm) <= latency_hiding)
  1030. return true;
  1031. else
  1032. return false;
  1033. }
  1034. /**
  1035. * dce_v11_0_program_watermarks - program display watermarks
  1036. *
  1037. * @adev: amdgpu_device pointer
  1038. * @amdgpu_crtc: the selected display controller
  1039. * @lb_size: line buffer size
  1040. * @num_heads: number of display controllers in use
  1041. *
  1042. * Calculate and program the display watermarks for the
  1043. * selected display controller (CIK).
  1044. */
  1045. static void dce_v11_0_program_watermarks(struct amdgpu_device *adev,
  1046. struct amdgpu_crtc *amdgpu_crtc,
  1047. u32 lb_size, u32 num_heads)
  1048. {
  1049. struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
  1050. struct dce10_wm_params wm_low, wm_high;
  1051. u32 pixel_period;
  1052. u32 line_time = 0;
  1053. u32 latency_watermark_a = 0, latency_watermark_b = 0;
  1054. u32 tmp, wm_mask, lb_vblank_lead_lines = 0;
  1055. if (amdgpu_crtc->base.enabled && num_heads && mode) {
  1056. pixel_period = 1000000 / (u32)mode->clock;
  1057. line_time = min((u32)mode->crtc_htotal * pixel_period, (u32)65535);
  1058. /* watermark for high clocks */
  1059. if (adev->pm.dpm_enabled) {
  1060. wm_high.yclk =
  1061. amdgpu_dpm_get_mclk(adev, false) * 10;
  1062. wm_high.sclk =
  1063. amdgpu_dpm_get_sclk(adev, false) * 10;
  1064. } else {
  1065. wm_high.yclk = adev->pm.current_mclk * 10;
  1066. wm_high.sclk = adev->pm.current_sclk * 10;
  1067. }
  1068. wm_high.disp_clk = mode->clock;
  1069. wm_high.src_width = mode->crtc_hdisplay;
  1070. wm_high.active_time = mode->crtc_hdisplay * pixel_period;
  1071. wm_high.blank_time = line_time - wm_high.active_time;
  1072. wm_high.interlaced = false;
  1073. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1074. wm_high.interlaced = true;
  1075. wm_high.vsc = amdgpu_crtc->vsc;
  1076. wm_high.vtaps = 1;
  1077. if (amdgpu_crtc->rmx_type != RMX_OFF)
  1078. wm_high.vtaps = 2;
  1079. wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
  1080. wm_high.lb_size = lb_size;
  1081. wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
  1082. wm_high.num_heads = num_heads;
  1083. /* set for high clocks */
  1084. latency_watermark_a = min(dce_v11_0_latency_watermark(&wm_high), (u32)65535);
  1085. /* possibly force display priority to high */
  1086. /* should really do this at mode validation time... */
  1087. if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
  1088. !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
  1089. !dce_v11_0_check_latency_hiding(&wm_high) ||
  1090. (adev->mode_info.disp_priority == 2)) {
  1091. DRM_DEBUG_KMS("force priority to high\n");
  1092. }
  1093. /* watermark for low clocks */
  1094. if (adev->pm.dpm_enabled) {
  1095. wm_low.yclk =
  1096. amdgpu_dpm_get_mclk(adev, true) * 10;
  1097. wm_low.sclk =
  1098. amdgpu_dpm_get_sclk(adev, true) * 10;
  1099. } else {
  1100. wm_low.yclk = adev->pm.current_mclk * 10;
  1101. wm_low.sclk = adev->pm.current_sclk * 10;
  1102. }
  1103. wm_low.disp_clk = mode->clock;
  1104. wm_low.src_width = mode->crtc_hdisplay;
  1105. wm_low.active_time = mode->crtc_hdisplay * pixel_period;
  1106. wm_low.blank_time = line_time - wm_low.active_time;
  1107. wm_low.interlaced = false;
  1108. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1109. wm_low.interlaced = true;
  1110. wm_low.vsc = amdgpu_crtc->vsc;
  1111. wm_low.vtaps = 1;
  1112. if (amdgpu_crtc->rmx_type != RMX_OFF)
  1113. wm_low.vtaps = 2;
  1114. wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
  1115. wm_low.lb_size = lb_size;
  1116. wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
  1117. wm_low.num_heads = num_heads;
  1118. /* set for low clocks */
  1119. latency_watermark_b = min(dce_v11_0_latency_watermark(&wm_low), (u32)65535);
  1120. /* possibly force display priority to high */
  1121. /* should really do this at mode validation time... */
  1122. if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
  1123. !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
  1124. !dce_v11_0_check_latency_hiding(&wm_low) ||
  1125. (adev->mode_info.disp_priority == 2)) {
  1126. DRM_DEBUG_KMS("force priority to high\n");
  1127. }
  1128. lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
  1129. }
  1130. /* select wm A */
  1131. wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
  1132. tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 1);
  1133. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1134. tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
  1135. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_a);
  1136. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
  1137. WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1138. /* select wm B */
  1139. tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 2);
  1140. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1141. tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
  1142. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_b);
  1143. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
  1144. WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1145. /* restore original selection */
  1146. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
  1147. /* save values for DPM */
  1148. amdgpu_crtc->line_time = line_time;
  1149. amdgpu_crtc->wm_high = latency_watermark_a;
  1150. amdgpu_crtc->wm_low = latency_watermark_b;
  1151. /* Save number of lines the linebuffer leads before the scanout */
  1152. amdgpu_crtc->lb_vblank_lead_lines = lb_vblank_lead_lines;
  1153. }
  1154. /**
  1155. * dce_v11_0_bandwidth_update - program display watermarks
  1156. *
  1157. * @adev: amdgpu_device pointer
  1158. *
  1159. * Calculate and program the display watermarks and line
  1160. * buffer allocation (CIK).
  1161. */
  1162. static void dce_v11_0_bandwidth_update(struct amdgpu_device *adev)
  1163. {
  1164. struct drm_display_mode *mode = NULL;
  1165. u32 num_heads = 0, lb_size;
  1166. int i;
  1167. amdgpu_update_display_priority(adev);
  1168. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1169. if (adev->mode_info.crtcs[i]->base.enabled)
  1170. num_heads++;
  1171. }
  1172. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1173. mode = &adev->mode_info.crtcs[i]->base.mode;
  1174. lb_size = dce_v11_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
  1175. dce_v11_0_program_watermarks(adev, adev->mode_info.crtcs[i],
  1176. lb_size, num_heads);
  1177. }
  1178. }
  1179. static void dce_v11_0_audio_get_connected_pins(struct amdgpu_device *adev)
  1180. {
  1181. int i;
  1182. u32 offset, tmp;
  1183. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1184. offset = adev->mode_info.audio.pin[i].offset;
  1185. tmp = RREG32_AUDIO_ENDPT(offset,
  1186. ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
  1187. if (((tmp &
  1188. AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
  1189. AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
  1190. adev->mode_info.audio.pin[i].connected = false;
  1191. else
  1192. adev->mode_info.audio.pin[i].connected = true;
  1193. }
  1194. }
  1195. static struct amdgpu_audio_pin *dce_v11_0_audio_get_pin(struct amdgpu_device *adev)
  1196. {
  1197. int i;
  1198. dce_v11_0_audio_get_connected_pins(adev);
  1199. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1200. if (adev->mode_info.audio.pin[i].connected)
  1201. return &adev->mode_info.audio.pin[i];
  1202. }
  1203. DRM_ERROR("No connected audio pins found!\n");
  1204. return NULL;
  1205. }
  1206. static void dce_v11_0_afmt_audio_select_pin(struct drm_encoder *encoder)
  1207. {
  1208. struct amdgpu_device *adev = encoder->dev->dev_private;
  1209. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1210. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1211. u32 tmp;
  1212. if (!dig || !dig->afmt || !dig->afmt->pin)
  1213. return;
  1214. tmp = RREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset);
  1215. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_SRC_CONTROL, AFMT_AUDIO_SRC_SELECT, dig->afmt->pin->id);
  1216. WREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, tmp);
  1217. }
  1218. static void dce_v11_0_audio_write_latency_fields(struct drm_encoder *encoder,
  1219. struct drm_display_mode *mode)
  1220. {
  1221. struct amdgpu_device *adev = encoder->dev->dev_private;
  1222. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1223. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1224. struct drm_connector *connector;
  1225. struct amdgpu_connector *amdgpu_connector = NULL;
  1226. u32 tmp;
  1227. int interlace = 0;
  1228. if (!dig || !dig->afmt || !dig->afmt->pin)
  1229. return;
  1230. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1231. if (connector->encoder == encoder) {
  1232. amdgpu_connector = to_amdgpu_connector(connector);
  1233. break;
  1234. }
  1235. }
  1236. if (!amdgpu_connector) {
  1237. DRM_ERROR("Couldn't find encoder's connector\n");
  1238. return;
  1239. }
  1240. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1241. interlace = 1;
  1242. if (connector->latency_present[interlace]) {
  1243. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1244. VIDEO_LIPSYNC, connector->video_latency[interlace]);
  1245. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1246. AUDIO_LIPSYNC, connector->audio_latency[interlace]);
  1247. } else {
  1248. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1249. VIDEO_LIPSYNC, 0);
  1250. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1251. AUDIO_LIPSYNC, 0);
  1252. }
  1253. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1254. ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
  1255. }
  1256. static void dce_v11_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
  1257. {
  1258. struct amdgpu_device *adev = encoder->dev->dev_private;
  1259. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1260. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1261. struct drm_connector *connector;
  1262. struct amdgpu_connector *amdgpu_connector = NULL;
  1263. u32 tmp;
  1264. u8 *sadb = NULL;
  1265. int sad_count;
  1266. if (!dig || !dig->afmt || !dig->afmt->pin)
  1267. return;
  1268. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1269. if (connector->encoder == encoder) {
  1270. amdgpu_connector = to_amdgpu_connector(connector);
  1271. break;
  1272. }
  1273. }
  1274. if (!amdgpu_connector) {
  1275. DRM_ERROR("Couldn't find encoder's connector\n");
  1276. return;
  1277. }
  1278. sad_count = drm_edid_to_speaker_allocation(amdgpu_connector_edid(connector), &sadb);
  1279. if (sad_count < 0) {
  1280. DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
  1281. sad_count = 0;
  1282. }
  1283. /* program the speaker allocation */
  1284. tmp = RREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1285. ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
  1286. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1287. DP_CONNECTION, 0);
  1288. /* set HDMI mode */
  1289. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1290. HDMI_CONNECTION, 1);
  1291. if (sad_count)
  1292. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1293. SPEAKER_ALLOCATION, sadb[0]);
  1294. else
  1295. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1296. SPEAKER_ALLOCATION, 5); /* stereo */
  1297. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1298. ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
  1299. kfree(sadb);
  1300. }
  1301. static void dce_v11_0_audio_write_sad_regs(struct drm_encoder *encoder)
  1302. {
  1303. struct amdgpu_device *adev = encoder->dev->dev_private;
  1304. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1305. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1306. struct drm_connector *connector;
  1307. struct amdgpu_connector *amdgpu_connector = NULL;
  1308. struct cea_sad *sads;
  1309. int i, sad_count;
  1310. static const u16 eld_reg_to_type[][2] = {
  1311. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
  1312. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
  1313. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
  1314. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
  1315. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
  1316. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
  1317. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
  1318. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
  1319. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
  1320. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
  1321. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
  1322. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
  1323. };
  1324. if (!dig || !dig->afmt || !dig->afmt->pin)
  1325. return;
  1326. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1327. if (connector->encoder == encoder) {
  1328. amdgpu_connector = to_amdgpu_connector(connector);
  1329. break;
  1330. }
  1331. }
  1332. if (!amdgpu_connector) {
  1333. DRM_ERROR("Couldn't find encoder's connector\n");
  1334. return;
  1335. }
  1336. sad_count = drm_edid_to_sad(amdgpu_connector_edid(connector), &sads);
  1337. if (sad_count <= 0) {
  1338. DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
  1339. return;
  1340. }
  1341. BUG_ON(!sads);
  1342. for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
  1343. u32 tmp = 0;
  1344. u8 stereo_freqs = 0;
  1345. int max_channels = -1;
  1346. int j;
  1347. for (j = 0; j < sad_count; j++) {
  1348. struct cea_sad *sad = &sads[j];
  1349. if (sad->format == eld_reg_to_type[i][1]) {
  1350. if (sad->channels > max_channels) {
  1351. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1352. MAX_CHANNELS, sad->channels);
  1353. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1354. DESCRIPTOR_BYTE_2, sad->byte2);
  1355. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1356. SUPPORTED_FREQUENCIES, sad->freq);
  1357. max_channels = sad->channels;
  1358. }
  1359. if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
  1360. stereo_freqs |= sad->freq;
  1361. else
  1362. break;
  1363. }
  1364. }
  1365. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1366. SUPPORTED_FREQUENCIES_STEREO, stereo_freqs);
  1367. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset, eld_reg_to_type[i][0], tmp);
  1368. }
  1369. kfree(sads);
  1370. }
  1371. static void dce_v11_0_audio_enable(struct amdgpu_device *adev,
  1372. struct amdgpu_audio_pin *pin,
  1373. bool enable)
  1374. {
  1375. if (!pin)
  1376. return;
  1377. WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
  1378. enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
  1379. }
  1380. static const u32 pin_offsets[] =
  1381. {
  1382. AUD0_REGISTER_OFFSET,
  1383. AUD1_REGISTER_OFFSET,
  1384. AUD2_REGISTER_OFFSET,
  1385. AUD3_REGISTER_OFFSET,
  1386. AUD4_REGISTER_OFFSET,
  1387. AUD5_REGISTER_OFFSET,
  1388. AUD6_REGISTER_OFFSET,
  1389. AUD7_REGISTER_OFFSET,
  1390. };
  1391. static int dce_v11_0_audio_init(struct amdgpu_device *adev)
  1392. {
  1393. int i;
  1394. if (!amdgpu_audio)
  1395. return 0;
  1396. adev->mode_info.audio.enabled = true;
  1397. switch (adev->asic_type) {
  1398. case CHIP_CARRIZO:
  1399. case CHIP_STONEY:
  1400. adev->mode_info.audio.num_pins = 7;
  1401. break;
  1402. case CHIP_POLARIS10:
  1403. adev->mode_info.audio.num_pins = 8;
  1404. break;
  1405. case CHIP_POLARIS11:
  1406. adev->mode_info.audio.num_pins = 6;
  1407. break;
  1408. default:
  1409. return -EINVAL;
  1410. }
  1411. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1412. adev->mode_info.audio.pin[i].channels = -1;
  1413. adev->mode_info.audio.pin[i].rate = -1;
  1414. adev->mode_info.audio.pin[i].bits_per_sample = -1;
  1415. adev->mode_info.audio.pin[i].status_bits = 0;
  1416. adev->mode_info.audio.pin[i].category_code = 0;
  1417. adev->mode_info.audio.pin[i].connected = false;
  1418. adev->mode_info.audio.pin[i].offset = pin_offsets[i];
  1419. adev->mode_info.audio.pin[i].id = i;
  1420. /* disable audio. it will be set up later */
  1421. /* XXX remove once we switch to ip funcs */
  1422. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  1423. }
  1424. return 0;
  1425. }
  1426. static void dce_v11_0_audio_fini(struct amdgpu_device *adev)
  1427. {
  1428. int i;
  1429. if (!amdgpu_audio)
  1430. return;
  1431. if (!adev->mode_info.audio.enabled)
  1432. return;
  1433. for (i = 0; i < adev->mode_info.audio.num_pins; i++)
  1434. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  1435. adev->mode_info.audio.enabled = false;
  1436. }
  1437. /*
  1438. * update the N and CTS parameters for a given pixel clock rate
  1439. */
  1440. static void dce_v11_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
  1441. {
  1442. struct drm_device *dev = encoder->dev;
  1443. struct amdgpu_device *adev = dev->dev_private;
  1444. struct amdgpu_afmt_acr acr = amdgpu_afmt_acr(clock);
  1445. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1446. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1447. u32 tmp;
  1448. tmp = RREG32(mmHDMI_ACR_32_0 + dig->afmt->offset);
  1449. tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_0, HDMI_ACR_CTS_32, acr.cts_32khz);
  1450. WREG32(mmHDMI_ACR_32_0 + dig->afmt->offset, tmp);
  1451. tmp = RREG32(mmHDMI_ACR_32_1 + dig->afmt->offset);
  1452. tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_1, HDMI_ACR_N_32, acr.n_32khz);
  1453. WREG32(mmHDMI_ACR_32_1 + dig->afmt->offset, tmp);
  1454. tmp = RREG32(mmHDMI_ACR_44_0 + dig->afmt->offset);
  1455. tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_0, HDMI_ACR_CTS_44, acr.cts_44_1khz);
  1456. WREG32(mmHDMI_ACR_44_0 + dig->afmt->offset, tmp);
  1457. tmp = RREG32(mmHDMI_ACR_44_1 + dig->afmt->offset);
  1458. tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_1, HDMI_ACR_N_44, acr.n_44_1khz);
  1459. WREG32(mmHDMI_ACR_44_1 + dig->afmt->offset, tmp);
  1460. tmp = RREG32(mmHDMI_ACR_48_0 + dig->afmt->offset);
  1461. tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_0, HDMI_ACR_CTS_48, acr.cts_48khz);
  1462. WREG32(mmHDMI_ACR_48_0 + dig->afmt->offset, tmp);
  1463. tmp = RREG32(mmHDMI_ACR_48_1 + dig->afmt->offset);
  1464. tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_1, HDMI_ACR_N_48, acr.n_48khz);
  1465. WREG32(mmHDMI_ACR_48_1 + dig->afmt->offset, tmp);
  1466. }
  1467. /*
  1468. * build a HDMI Video Info Frame
  1469. */
  1470. static void dce_v11_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
  1471. void *buffer, size_t size)
  1472. {
  1473. struct drm_device *dev = encoder->dev;
  1474. struct amdgpu_device *adev = dev->dev_private;
  1475. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1476. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1477. uint8_t *frame = buffer + 3;
  1478. uint8_t *header = buffer;
  1479. WREG32(mmAFMT_AVI_INFO0 + dig->afmt->offset,
  1480. frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
  1481. WREG32(mmAFMT_AVI_INFO1 + dig->afmt->offset,
  1482. frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
  1483. WREG32(mmAFMT_AVI_INFO2 + dig->afmt->offset,
  1484. frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
  1485. WREG32(mmAFMT_AVI_INFO3 + dig->afmt->offset,
  1486. frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
  1487. }
  1488. static void dce_v11_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
  1489. {
  1490. struct drm_device *dev = encoder->dev;
  1491. struct amdgpu_device *adev = dev->dev_private;
  1492. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1493. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1494. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  1495. u32 dto_phase = 24 * 1000;
  1496. u32 dto_modulo = clock;
  1497. u32 tmp;
  1498. if (!dig || !dig->afmt)
  1499. return;
  1500. /* XXX two dtos; generally use dto0 for hdmi */
  1501. /* Express [24MHz / target pixel clock] as an exact rational
  1502. * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
  1503. * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
  1504. */
  1505. tmp = RREG32(mmDCCG_AUDIO_DTO_SOURCE);
  1506. tmp = REG_SET_FIELD(tmp, DCCG_AUDIO_DTO_SOURCE, DCCG_AUDIO_DTO0_SOURCE_SEL,
  1507. amdgpu_crtc->crtc_id);
  1508. WREG32(mmDCCG_AUDIO_DTO_SOURCE, tmp);
  1509. WREG32(mmDCCG_AUDIO_DTO0_PHASE, dto_phase);
  1510. WREG32(mmDCCG_AUDIO_DTO0_MODULE, dto_modulo);
  1511. }
  1512. /*
  1513. * update the info frames with the data from the current display mode
  1514. */
  1515. static void dce_v11_0_afmt_setmode(struct drm_encoder *encoder,
  1516. struct drm_display_mode *mode)
  1517. {
  1518. struct drm_device *dev = encoder->dev;
  1519. struct amdgpu_device *adev = dev->dev_private;
  1520. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1521. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1522. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  1523. u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
  1524. struct hdmi_avi_infoframe frame;
  1525. ssize_t err;
  1526. u32 tmp;
  1527. int bpc = 8;
  1528. if (!dig || !dig->afmt)
  1529. return;
  1530. /* Silent, r600_hdmi_enable will raise WARN for us */
  1531. if (!dig->afmt->enabled)
  1532. return;
  1533. /* hdmi deep color mode general control packets setup, if bpc > 8 */
  1534. if (encoder->crtc) {
  1535. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  1536. bpc = amdgpu_crtc->bpc;
  1537. }
  1538. /* disable audio prior to setting up hw */
  1539. dig->afmt->pin = dce_v11_0_audio_get_pin(adev);
  1540. dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
  1541. dce_v11_0_audio_set_dto(encoder, mode->clock);
  1542. tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
  1543. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1);
  1544. WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp); /* send null packets when required */
  1545. WREG32(mmAFMT_AUDIO_CRC_CONTROL + dig->afmt->offset, 0x1000);
  1546. tmp = RREG32(mmHDMI_CONTROL + dig->afmt->offset);
  1547. switch (bpc) {
  1548. case 0:
  1549. case 6:
  1550. case 8:
  1551. case 16:
  1552. default:
  1553. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 0);
  1554. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 0);
  1555. DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
  1556. connector->name, bpc);
  1557. break;
  1558. case 10:
  1559. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
  1560. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 1);
  1561. DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
  1562. connector->name);
  1563. break;
  1564. case 12:
  1565. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
  1566. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 2);
  1567. DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
  1568. connector->name);
  1569. break;
  1570. }
  1571. WREG32(mmHDMI_CONTROL + dig->afmt->offset, tmp);
  1572. tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
  1573. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1); /* send null packets when required */
  1574. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_SEND, 1); /* send general control packets */
  1575. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_CONT, 1); /* send general control packets every frame */
  1576. WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp);
  1577. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1578. /* enable audio info frames (frames won't be set until audio is enabled) */
  1579. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_SEND, 1);
  1580. /* required for audio info values to be updated */
  1581. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_CONT, 1);
  1582. WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1583. tmp = RREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1584. /* required for audio info values to be updated */
  1585. tmp = REG_SET_FIELD(tmp, AFMT_INFOFRAME_CONTROL0, AFMT_AUDIO_INFO_UPDATE, 1);
  1586. WREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1587. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
  1588. /* anything other than 0 */
  1589. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AUDIO_INFO_LINE, 2);
  1590. WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
  1591. WREG32(mmHDMI_GC + dig->afmt->offset, 0); /* unset HDMI_GC_AVMUTE */
  1592. tmp = RREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1593. /* set the default audio delay */
  1594. tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_DELAY_EN, 1);
  1595. /* should be suffient for all audio modes and small enough for all hblanks */
  1596. tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_PACKETS_PER_LINE, 3);
  1597. WREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1598. tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1599. /* allow 60958 channel status fields to be updated */
  1600. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_60958_CS_UPDATE, 1);
  1601. WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1602. tmp = RREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset);
  1603. if (bpc > 8)
  1604. /* clear SW CTS value */
  1605. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 0);
  1606. else
  1607. /* select SW CTS value */
  1608. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 1);
  1609. /* allow hw to sent ACR packets when required */
  1610. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_AUTO_SEND, 1);
  1611. WREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset, tmp);
  1612. dce_v11_0_afmt_update_ACR(encoder, mode->clock);
  1613. tmp = RREG32(mmAFMT_60958_0 + dig->afmt->offset);
  1614. tmp = REG_SET_FIELD(tmp, AFMT_60958_0, AFMT_60958_CS_CHANNEL_NUMBER_L, 1);
  1615. WREG32(mmAFMT_60958_0 + dig->afmt->offset, tmp);
  1616. tmp = RREG32(mmAFMT_60958_1 + dig->afmt->offset);
  1617. tmp = REG_SET_FIELD(tmp, AFMT_60958_1, AFMT_60958_CS_CHANNEL_NUMBER_R, 2);
  1618. WREG32(mmAFMT_60958_1 + dig->afmt->offset, tmp);
  1619. tmp = RREG32(mmAFMT_60958_2 + dig->afmt->offset);
  1620. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_2, 3);
  1621. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_3, 4);
  1622. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_4, 5);
  1623. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_5, 6);
  1624. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_6, 7);
  1625. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_7, 8);
  1626. WREG32(mmAFMT_60958_2 + dig->afmt->offset, tmp);
  1627. dce_v11_0_audio_write_speaker_allocation(encoder);
  1628. WREG32(mmAFMT_AUDIO_PACKET_CONTROL2 + dig->afmt->offset,
  1629. (0xff << AFMT_AUDIO_PACKET_CONTROL2__AFMT_AUDIO_CHANNEL_ENABLE__SHIFT));
  1630. dce_v11_0_afmt_audio_select_pin(encoder);
  1631. dce_v11_0_audio_write_sad_regs(encoder);
  1632. dce_v11_0_audio_write_latency_fields(encoder, mode);
  1633. err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
  1634. if (err < 0) {
  1635. DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
  1636. return;
  1637. }
  1638. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  1639. if (err < 0) {
  1640. DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
  1641. return;
  1642. }
  1643. dce_v11_0_afmt_update_avi_infoframe(encoder, buffer, sizeof(buffer));
  1644. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1645. /* enable AVI info frames */
  1646. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_SEND, 1);
  1647. /* required for audio info values to be updated */
  1648. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_CONT, 1);
  1649. WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1650. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
  1651. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AVI_INFO_LINE, 2);
  1652. WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
  1653. tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1654. /* send audio packets */
  1655. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_AUDIO_SAMPLE_SEND, 1);
  1656. WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1657. WREG32(mmAFMT_RAMP_CONTROL0 + dig->afmt->offset, 0x00FFFFFF);
  1658. WREG32(mmAFMT_RAMP_CONTROL1 + dig->afmt->offset, 0x007FFFFF);
  1659. WREG32(mmAFMT_RAMP_CONTROL2 + dig->afmt->offset, 0x00000001);
  1660. WREG32(mmAFMT_RAMP_CONTROL3 + dig->afmt->offset, 0x00000001);
  1661. /* enable audio after to setting up hw */
  1662. dce_v11_0_audio_enable(adev, dig->afmt->pin, true);
  1663. }
  1664. static void dce_v11_0_afmt_enable(struct drm_encoder *encoder, bool enable)
  1665. {
  1666. struct drm_device *dev = encoder->dev;
  1667. struct amdgpu_device *adev = dev->dev_private;
  1668. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1669. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1670. if (!dig || !dig->afmt)
  1671. return;
  1672. /* Silent, r600_hdmi_enable will raise WARN for us */
  1673. if (enable && dig->afmt->enabled)
  1674. return;
  1675. if (!enable && !dig->afmt->enabled)
  1676. return;
  1677. if (!enable && dig->afmt->pin) {
  1678. dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
  1679. dig->afmt->pin = NULL;
  1680. }
  1681. dig->afmt->enabled = enable;
  1682. DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
  1683. enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
  1684. }
  1685. static int dce_v11_0_afmt_init(struct amdgpu_device *adev)
  1686. {
  1687. int i;
  1688. for (i = 0; i < adev->mode_info.num_dig; i++)
  1689. adev->mode_info.afmt[i] = NULL;
  1690. /* DCE11 has audio blocks tied to DIG encoders */
  1691. for (i = 0; i < adev->mode_info.num_dig; i++) {
  1692. adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
  1693. if (adev->mode_info.afmt[i]) {
  1694. adev->mode_info.afmt[i]->offset = dig_offsets[i];
  1695. adev->mode_info.afmt[i]->id = i;
  1696. } else {
  1697. int j;
  1698. for (j = 0; j < i; j++) {
  1699. kfree(adev->mode_info.afmt[j]);
  1700. adev->mode_info.afmt[j] = NULL;
  1701. }
  1702. return -ENOMEM;
  1703. }
  1704. }
  1705. return 0;
  1706. }
  1707. static void dce_v11_0_afmt_fini(struct amdgpu_device *adev)
  1708. {
  1709. int i;
  1710. for (i = 0; i < adev->mode_info.num_dig; i++) {
  1711. kfree(adev->mode_info.afmt[i]);
  1712. adev->mode_info.afmt[i] = NULL;
  1713. }
  1714. }
  1715. static const u32 vga_control_regs[6] =
  1716. {
  1717. mmD1VGA_CONTROL,
  1718. mmD2VGA_CONTROL,
  1719. mmD3VGA_CONTROL,
  1720. mmD4VGA_CONTROL,
  1721. mmD5VGA_CONTROL,
  1722. mmD6VGA_CONTROL,
  1723. };
  1724. static void dce_v11_0_vga_enable(struct drm_crtc *crtc, bool enable)
  1725. {
  1726. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1727. struct drm_device *dev = crtc->dev;
  1728. struct amdgpu_device *adev = dev->dev_private;
  1729. u32 vga_control;
  1730. vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
  1731. if (enable)
  1732. WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
  1733. else
  1734. WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
  1735. }
  1736. static void dce_v11_0_grph_enable(struct drm_crtc *crtc, bool enable)
  1737. {
  1738. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1739. struct drm_device *dev = crtc->dev;
  1740. struct amdgpu_device *adev = dev->dev_private;
  1741. if (enable)
  1742. WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
  1743. else
  1744. WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
  1745. }
  1746. static int dce_v11_0_crtc_do_set_base(struct drm_crtc *crtc,
  1747. struct drm_framebuffer *fb,
  1748. int x, int y, int atomic)
  1749. {
  1750. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1751. struct drm_device *dev = crtc->dev;
  1752. struct amdgpu_device *adev = dev->dev_private;
  1753. struct amdgpu_framebuffer *amdgpu_fb;
  1754. struct drm_framebuffer *target_fb;
  1755. struct drm_gem_object *obj;
  1756. struct amdgpu_bo *abo;
  1757. uint64_t fb_location, tiling_flags;
  1758. uint32_t fb_format, fb_pitch_pixels;
  1759. u32 fb_swap = REG_SET_FIELD(0, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP, ENDIAN_NONE);
  1760. u32 pipe_config;
  1761. u32 tmp, viewport_w, viewport_h;
  1762. int r;
  1763. bool bypass_lut = false;
  1764. struct drm_format_name_buf format_name;
  1765. /* no fb bound */
  1766. if (!atomic && !crtc->primary->fb) {
  1767. DRM_DEBUG_KMS("No FB bound\n");
  1768. return 0;
  1769. }
  1770. if (atomic) {
  1771. amdgpu_fb = to_amdgpu_framebuffer(fb);
  1772. target_fb = fb;
  1773. } else {
  1774. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  1775. target_fb = crtc->primary->fb;
  1776. }
  1777. /* If atomic, assume fb object is pinned & idle & fenced and
  1778. * just update base pointers
  1779. */
  1780. obj = amdgpu_fb->obj;
  1781. abo = gem_to_amdgpu_bo(obj);
  1782. r = amdgpu_bo_reserve(abo, false);
  1783. if (unlikely(r != 0))
  1784. return r;
  1785. if (atomic) {
  1786. fb_location = amdgpu_bo_gpu_offset(abo);
  1787. } else {
  1788. r = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM, &fb_location);
  1789. if (unlikely(r != 0)) {
  1790. amdgpu_bo_unreserve(abo);
  1791. return -EINVAL;
  1792. }
  1793. }
  1794. amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
  1795. amdgpu_bo_unreserve(abo);
  1796. pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
  1797. switch (target_fb->pixel_format) {
  1798. case DRM_FORMAT_C8:
  1799. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 0);
  1800. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1801. break;
  1802. case DRM_FORMAT_XRGB4444:
  1803. case DRM_FORMAT_ARGB4444:
  1804. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1805. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 2);
  1806. #ifdef __BIG_ENDIAN
  1807. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1808. ENDIAN_8IN16);
  1809. #endif
  1810. break;
  1811. case DRM_FORMAT_XRGB1555:
  1812. case DRM_FORMAT_ARGB1555:
  1813. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1814. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1815. #ifdef __BIG_ENDIAN
  1816. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1817. ENDIAN_8IN16);
  1818. #endif
  1819. break;
  1820. case DRM_FORMAT_BGRX5551:
  1821. case DRM_FORMAT_BGRA5551:
  1822. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1823. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 5);
  1824. #ifdef __BIG_ENDIAN
  1825. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1826. ENDIAN_8IN16);
  1827. #endif
  1828. break;
  1829. case DRM_FORMAT_RGB565:
  1830. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1831. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
  1832. #ifdef __BIG_ENDIAN
  1833. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1834. ENDIAN_8IN16);
  1835. #endif
  1836. break;
  1837. case DRM_FORMAT_XRGB8888:
  1838. case DRM_FORMAT_ARGB8888:
  1839. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1840. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1841. #ifdef __BIG_ENDIAN
  1842. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1843. ENDIAN_8IN32);
  1844. #endif
  1845. break;
  1846. case DRM_FORMAT_XRGB2101010:
  1847. case DRM_FORMAT_ARGB2101010:
  1848. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1849. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
  1850. #ifdef __BIG_ENDIAN
  1851. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1852. ENDIAN_8IN32);
  1853. #endif
  1854. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1855. bypass_lut = true;
  1856. break;
  1857. case DRM_FORMAT_BGRX1010102:
  1858. case DRM_FORMAT_BGRA1010102:
  1859. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1860. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 4);
  1861. #ifdef __BIG_ENDIAN
  1862. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1863. ENDIAN_8IN32);
  1864. #endif
  1865. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1866. bypass_lut = true;
  1867. break;
  1868. default:
  1869. DRM_ERROR("Unsupported screen format %s\n",
  1870. drm_get_format_name(target_fb->pixel_format, &format_name));
  1871. return -EINVAL;
  1872. }
  1873. if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
  1874. unsigned bankw, bankh, mtaspect, tile_split, num_banks;
  1875. bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
  1876. bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
  1877. mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
  1878. tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
  1879. num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
  1880. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_NUM_BANKS, num_banks);
  1881. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
  1882. ARRAY_2D_TILED_THIN1);
  1883. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_TILE_SPLIT,
  1884. tile_split);
  1885. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_WIDTH, bankw);
  1886. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_HEIGHT, bankh);
  1887. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MACRO_TILE_ASPECT,
  1888. mtaspect);
  1889. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MICRO_TILE_MODE,
  1890. ADDR_SURF_MICRO_TILING_DISPLAY);
  1891. } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1) {
  1892. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
  1893. ARRAY_1D_TILED_THIN1);
  1894. }
  1895. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_PIPE_CONFIG,
  1896. pipe_config);
  1897. dce_v11_0_vga_enable(crtc, false);
  1898. /* Make sure surface address is updated at vertical blank rather than
  1899. * horizontal blank
  1900. */
  1901. tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
  1902. tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
  1903. GRPH_SURFACE_UPDATE_H_RETRACE_EN, 0);
  1904. WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1905. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  1906. upper_32_bits(fb_location));
  1907. WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  1908. upper_32_bits(fb_location));
  1909. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  1910. (u32)fb_location & GRPH_PRIMARY_SURFACE_ADDRESS__GRPH_PRIMARY_SURFACE_ADDRESS_MASK);
  1911. WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  1912. (u32) fb_location & GRPH_SECONDARY_SURFACE_ADDRESS__GRPH_SECONDARY_SURFACE_ADDRESS_MASK);
  1913. WREG32(mmGRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
  1914. WREG32(mmGRPH_SWAP_CNTL + amdgpu_crtc->crtc_offset, fb_swap);
  1915. /*
  1916. * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
  1917. * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
  1918. * retain the full precision throughout the pipeline.
  1919. */
  1920. tmp = RREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset);
  1921. if (bypass_lut)
  1922. tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 1);
  1923. else
  1924. tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 0);
  1925. WREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset, tmp);
  1926. if (bypass_lut)
  1927. DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
  1928. WREG32(mmGRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
  1929. WREG32(mmGRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
  1930. WREG32(mmGRPH_X_START + amdgpu_crtc->crtc_offset, 0);
  1931. WREG32(mmGRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
  1932. WREG32(mmGRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
  1933. WREG32(mmGRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
  1934. fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
  1935. WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
  1936. dce_v11_0_grph_enable(crtc, true);
  1937. WREG32(mmLB_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
  1938. target_fb->height);
  1939. x &= ~3;
  1940. y &= ~1;
  1941. WREG32(mmVIEWPORT_START + amdgpu_crtc->crtc_offset,
  1942. (x << 16) | y);
  1943. viewport_w = crtc->mode.hdisplay;
  1944. viewport_h = (crtc->mode.vdisplay + 1) & ~1;
  1945. WREG32(mmVIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
  1946. (viewport_w << 16) | viewport_h);
  1947. /* set pageflip to happen anywhere in vblank interval */
  1948. WREG32(mmCRTC_MASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 0);
  1949. if (!atomic && fb && fb != crtc->primary->fb) {
  1950. amdgpu_fb = to_amdgpu_framebuffer(fb);
  1951. abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  1952. r = amdgpu_bo_reserve(abo, false);
  1953. if (unlikely(r != 0))
  1954. return r;
  1955. amdgpu_bo_unpin(abo);
  1956. amdgpu_bo_unreserve(abo);
  1957. }
  1958. /* Bytes per pixel may have changed */
  1959. dce_v11_0_bandwidth_update(adev);
  1960. return 0;
  1961. }
  1962. static void dce_v11_0_set_interleave(struct drm_crtc *crtc,
  1963. struct drm_display_mode *mode)
  1964. {
  1965. struct drm_device *dev = crtc->dev;
  1966. struct amdgpu_device *adev = dev->dev_private;
  1967. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1968. u32 tmp;
  1969. tmp = RREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset);
  1970. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1971. tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 1);
  1972. else
  1973. tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 0);
  1974. WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset, tmp);
  1975. }
  1976. static void dce_v11_0_crtc_load_lut(struct drm_crtc *crtc)
  1977. {
  1978. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1979. struct drm_device *dev = crtc->dev;
  1980. struct amdgpu_device *adev = dev->dev_private;
  1981. int i;
  1982. u32 tmp;
  1983. DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
  1984. tmp = RREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
  1985. tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_GRPH_MODE, 0);
  1986. WREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1987. tmp = RREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset);
  1988. tmp = REG_SET_FIELD(tmp, PRESCALE_GRPH_CONTROL, GRPH_PRESCALE_BYPASS, 1);
  1989. WREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1990. tmp = RREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  1991. tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, GRPH_INPUT_GAMMA_MODE, 0);
  1992. WREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1993. WREG32(mmDC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
  1994. WREG32(mmDC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
  1995. WREG32(mmDC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
  1996. WREG32(mmDC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
  1997. WREG32(mmDC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
  1998. WREG32(mmDC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
  1999. WREG32(mmDC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
  2000. WREG32(mmDC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
  2001. WREG32(mmDC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
  2002. WREG32(mmDC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
  2003. for (i = 0; i < 256; i++) {
  2004. WREG32(mmDC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
  2005. (amdgpu_crtc->lut_r[i] << 20) |
  2006. (amdgpu_crtc->lut_g[i] << 10) |
  2007. (amdgpu_crtc->lut_b[i] << 0));
  2008. }
  2009. tmp = RREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  2010. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, GRPH_DEGAMMA_MODE, 0);
  2011. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR_DEGAMMA_MODE, 0);
  2012. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR2_DEGAMMA_MODE, 0);
  2013. WREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2014. tmp = RREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset);
  2015. tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, GRPH_GAMUT_REMAP_MODE, 0);
  2016. WREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2017. tmp = RREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  2018. tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, GRPH_REGAMMA_MODE, 0);
  2019. WREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2020. tmp = RREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
  2021. tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_GRPH_MODE, 0);
  2022. WREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2023. /* XXX match this to the depth of the crtc fmt block, move to modeset? */
  2024. WREG32(mmDENORM_CONTROL + amdgpu_crtc->crtc_offset, 0);
  2025. /* XXX this only needs to be programmed once per crtc at startup,
  2026. * not sure where the best place for it is
  2027. */
  2028. tmp = RREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset);
  2029. tmp = REG_SET_FIELD(tmp, ALPHA_CONTROL, CURSOR_ALPHA_BLND_ENA, 1);
  2030. WREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2031. }
  2032. static int dce_v11_0_pick_dig_encoder(struct drm_encoder *encoder)
  2033. {
  2034. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2035. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  2036. switch (amdgpu_encoder->encoder_id) {
  2037. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  2038. if (dig->linkb)
  2039. return 1;
  2040. else
  2041. return 0;
  2042. break;
  2043. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  2044. if (dig->linkb)
  2045. return 3;
  2046. else
  2047. return 2;
  2048. break;
  2049. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  2050. if (dig->linkb)
  2051. return 5;
  2052. else
  2053. return 4;
  2054. break;
  2055. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
  2056. return 6;
  2057. break;
  2058. default:
  2059. DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
  2060. return 0;
  2061. }
  2062. }
  2063. /**
  2064. * dce_v11_0_pick_pll - Allocate a PPLL for use by the crtc.
  2065. *
  2066. * @crtc: drm crtc
  2067. *
  2068. * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
  2069. * a single PPLL can be used for all DP crtcs/encoders. For non-DP
  2070. * monitors a dedicated PPLL must be used. If a particular board has
  2071. * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
  2072. * as there is no need to program the PLL itself. If we are not able to
  2073. * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
  2074. * avoid messing up an existing monitor.
  2075. *
  2076. * Asic specific PLL information
  2077. *
  2078. * DCE 10.x
  2079. * Tonga
  2080. * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
  2081. * CI
  2082. * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
  2083. *
  2084. */
  2085. static u32 dce_v11_0_pick_pll(struct drm_crtc *crtc)
  2086. {
  2087. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2088. struct drm_device *dev = crtc->dev;
  2089. struct amdgpu_device *adev = dev->dev_private;
  2090. u32 pll_in_use;
  2091. int pll;
  2092. if ((adev->asic_type == CHIP_POLARIS10) ||
  2093. (adev->asic_type == CHIP_POLARIS11)) {
  2094. struct amdgpu_encoder *amdgpu_encoder =
  2095. to_amdgpu_encoder(amdgpu_crtc->encoder);
  2096. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  2097. if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
  2098. return ATOM_DP_DTO;
  2099. switch (amdgpu_encoder->encoder_id) {
  2100. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  2101. if (dig->linkb)
  2102. return ATOM_COMBOPHY_PLL1;
  2103. else
  2104. return ATOM_COMBOPHY_PLL0;
  2105. break;
  2106. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  2107. if (dig->linkb)
  2108. return ATOM_COMBOPHY_PLL3;
  2109. else
  2110. return ATOM_COMBOPHY_PLL2;
  2111. break;
  2112. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  2113. if (dig->linkb)
  2114. return ATOM_COMBOPHY_PLL5;
  2115. else
  2116. return ATOM_COMBOPHY_PLL4;
  2117. break;
  2118. default:
  2119. DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
  2120. return ATOM_PPLL_INVALID;
  2121. }
  2122. }
  2123. if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
  2124. if (adev->clock.dp_extclk)
  2125. /* skip PPLL programming if using ext clock */
  2126. return ATOM_PPLL_INVALID;
  2127. else {
  2128. /* use the same PPLL for all DP monitors */
  2129. pll = amdgpu_pll_get_shared_dp_ppll(crtc);
  2130. if (pll != ATOM_PPLL_INVALID)
  2131. return pll;
  2132. }
  2133. } else {
  2134. /* use the same PPLL for all monitors with the same clock */
  2135. pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
  2136. if (pll != ATOM_PPLL_INVALID)
  2137. return pll;
  2138. }
  2139. /* XXX need to determine what plls are available on each DCE11 part */
  2140. pll_in_use = amdgpu_pll_get_use_mask(crtc);
  2141. if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY) {
  2142. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  2143. return ATOM_PPLL1;
  2144. if (!(pll_in_use & (1 << ATOM_PPLL0)))
  2145. return ATOM_PPLL0;
  2146. DRM_ERROR("unable to allocate a PPLL\n");
  2147. return ATOM_PPLL_INVALID;
  2148. } else {
  2149. if (!(pll_in_use & (1 << ATOM_PPLL2)))
  2150. return ATOM_PPLL2;
  2151. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  2152. return ATOM_PPLL1;
  2153. if (!(pll_in_use & (1 << ATOM_PPLL0)))
  2154. return ATOM_PPLL0;
  2155. DRM_ERROR("unable to allocate a PPLL\n");
  2156. return ATOM_PPLL_INVALID;
  2157. }
  2158. return ATOM_PPLL_INVALID;
  2159. }
  2160. static void dce_v11_0_lock_cursor(struct drm_crtc *crtc, bool lock)
  2161. {
  2162. struct amdgpu_device *adev = crtc->dev->dev_private;
  2163. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2164. uint32_t cur_lock;
  2165. cur_lock = RREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset);
  2166. if (lock)
  2167. cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 1);
  2168. else
  2169. cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 0);
  2170. WREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
  2171. }
  2172. static void dce_v11_0_hide_cursor(struct drm_crtc *crtc)
  2173. {
  2174. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2175. struct amdgpu_device *adev = crtc->dev->dev_private;
  2176. u32 tmp;
  2177. tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
  2178. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 0);
  2179. WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2180. }
  2181. static void dce_v11_0_show_cursor(struct drm_crtc *crtc)
  2182. {
  2183. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2184. struct amdgpu_device *adev = crtc->dev->dev_private;
  2185. u32 tmp;
  2186. WREG32(mmCUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  2187. upper_32_bits(amdgpu_crtc->cursor_addr));
  2188. WREG32(mmCUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  2189. lower_32_bits(amdgpu_crtc->cursor_addr));
  2190. tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
  2191. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 1);
  2192. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_MODE, 2);
  2193. WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2194. }
  2195. static int dce_v11_0_cursor_move_locked(struct drm_crtc *crtc,
  2196. int x, int y)
  2197. {
  2198. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2199. struct amdgpu_device *adev = crtc->dev->dev_private;
  2200. int xorigin = 0, yorigin = 0;
  2201. /* avivo cursor are offset into the total surface */
  2202. x += crtc->x;
  2203. y += crtc->y;
  2204. DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
  2205. if (x < 0) {
  2206. xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
  2207. x = 0;
  2208. }
  2209. if (y < 0) {
  2210. yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
  2211. y = 0;
  2212. }
  2213. WREG32(mmCUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
  2214. WREG32(mmCUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
  2215. WREG32(mmCUR_SIZE + amdgpu_crtc->crtc_offset,
  2216. ((amdgpu_crtc->cursor_width - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
  2217. amdgpu_crtc->cursor_x = x;
  2218. amdgpu_crtc->cursor_y = y;
  2219. return 0;
  2220. }
  2221. static int dce_v11_0_crtc_cursor_move(struct drm_crtc *crtc,
  2222. int x, int y)
  2223. {
  2224. int ret;
  2225. dce_v11_0_lock_cursor(crtc, true);
  2226. ret = dce_v11_0_cursor_move_locked(crtc, x, y);
  2227. dce_v11_0_lock_cursor(crtc, false);
  2228. return ret;
  2229. }
  2230. static int dce_v11_0_crtc_cursor_set2(struct drm_crtc *crtc,
  2231. struct drm_file *file_priv,
  2232. uint32_t handle,
  2233. uint32_t width,
  2234. uint32_t height,
  2235. int32_t hot_x,
  2236. int32_t hot_y)
  2237. {
  2238. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2239. struct drm_gem_object *obj;
  2240. struct amdgpu_bo *aobj;
  2241. int ret;
  2242. if (!handle) {
  2243. /* turn off cursor */
  2244. dce_v11_0_hide_cursor(crtc);
  2245. obj = NULL;
  2246. goto unpin;
  2247. }
  2248. if ((width > amdgpu_crtc->max_cursor_width) ||
  2249. (height > amdgpu_crtc->max_cursor_height)) {
  2250. DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
  2251. return -EINVAL;
  2252. }
  2253. obj = drm_gem_object_lookup(file_priv, handle);
  2254. if (!obj) {
  2255. DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
  2256. return -ENOENT;
  2257. }
  2258. aobj = gem_to_amdgpu_bo(obj);
  2259. ret = amdgpu_bo_reserve(aobj, false);
  2260. if (ret != 0) {
  2261. drm_gem_object_unreference_unlocked(obj);
  2262. return ret;
  2263. }
  2264. ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM, &amdgpu_crtc->cursor_addr);
  2265. amdgpu_bo_unreserve(aobj);
  2266. if (ret) {
  2267. DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
  2268. drm_gem_object_unreference_unlocked(obj);
  2269. return ret;
  2270. }
  2271. amdgpu_crtc->cursor_width = width;
  2272. amdgpu_crtc->cursor_height = height;
  2273. dce_v11_0_lock_cursor(crtc, true);
  2274. if (hot_x != amdgpu_crtc->cursor_hot_x ||
  2275. hot_y != amdgpu_crtc->cursor_hot_y) {
  2276. int x, y;
  2277. x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
  2278. y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
  2279. dce_v11_0_cursor_move_locked(crtc, x, y);
  2280. amdgpu_crtc->cursor_hot_x = hot_x;
  2281. amdgpu_crtc->cursor_hot_y = hot_y;
  2282. }
  2283. dce_v11_0_show_cursor(crtc);
  2284. dce_v11_0_lock_cursor(crtc, false);
  2285. unpin:
  2286. if (amdgpu_crtc->cursor_bo) {
  2287. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2288. ret = amdgpu_bo_reserve(aobj, false);
  2289. if (likely(ret == 0)) {
  2290. amdgpu_bo_unpin(aobj);
  2291. amdgpu_bo_unreserve(aobj);
  2292. }
  2293. drm_gem_object_unreference_unlocked(amdgpu_crtc->cursor_bo);
  2294. }
  2295. amdgpu_crtc->cursor_bo = obj;
  2296. return 0;
  2297. }
  2298. static void dce_v11_0_cursor_reset(struct drm_crtc *crtc)
  2299. {
  2300. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2301. if (amdgpu_crtc->cursor_bo) {
  2302. dce_v11_0_lock_cursor(crtc, true);
  2303. dce_v11_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
  2304. amdgpu_crtc->cursor_y);
  2305. dce_v11_0_show_cursor(crtc);
  2306. dce_v11_0_lock_cursor(crtc, false);
  2307. }
  2308. }
  2309. static int dce_v11_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  2310. u16 *blue, uint32_t size)
  2311. {
  2312. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2313. int i;
  2314. /* userspace palettes are always correct as is */
  2315. for (i = 0; i < size; i++) {
  2316. amdgpu_crtc->lut_r[i] = red[i] >> 6;
  2317. amdgpu_crtc->lut_g[i] = green[i] >> 6;
  2318. amdgpu_crtc->lut_b[i] = blue[i] >> 6;
  2319. }
  2320. dce_v11_0_crtc_load_lut(crtc);
  2321. return 0;
  2322. }
  2323. static void dce_v11_0_crtc_destroy(struct drm_crtc *crtc)
  2324. {
  2325. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2326. drm_crtc_cleanup(crtc);
  2327. kfree(amdgpu_crtc);
  2328. }
  2329. static const struct drm_crtc_funcs dce_v11_0_crtc_funcs = {
  2330. .cursor_set2 = dce_v11_0_crtc_cursor_set2,
  2331. .cursor_move = dce_v11_0_crtc_cursor_move,
  2332. .gamma_set = dce_v11_0_crtc_gamma_set,
  2333. .set_config = amdgpu_crtc_set_config,
  2334. .destroy = dce_v11_0_crtc_destroy,
  2335. .page_flip_target = amdgpu_crtc_page_flip_target,
  2336. };
  2337. static void dce_v11_0_crtc_dpms(struct drm_crtc *crtc, int mode)
  2338. {
  2339. struct drm_device *dev = crtc->dev;
  2340. struct amdgpu_device *adev = dev->dev_private;
  2341. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2342. unsigned type;
  2343. switch (mode) {
  2344. case DRM_MODE_DPMS_ON:
  2345. amdgpu_crtc->enabled = true;
  2346. amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
  2347. dce_v11_0_vga_enable(crtc, true);
  2348. amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
  2349. dce_v11_0_vga_enable(crtc, false);
  2350. /* Make sure VBLANK and PFLIP interrupts are still enabled */
  2351. type = amdgpu_crtc_idx_to_irq_type(adev, amdgpu_crtc->crtc_id);
  2352. amdgpu_irq_update(adev, &adev->crtc_irq, type);
  2353. amdgpu_irq_update(adev, &adev->pageflip_irq, type);
  2354. drm_crtc_vblank_on(crtc);
  2355. dce_v11_0_crtc_load_lut(crtc);
  2356. break;
  2357. case DRM_MODE_DPMS_STANDBY:
  2358. case DRM_MODE_DPMS_SUSPEND:
  2359. case DRM_MODE_DPMS_OFF:
  2360. drm_crtc_vblank_off(crtc);
  2361. if (amdgpu_crtc->enabled) {
  2362. dce_v11_0_vga_enable(crtc, true);
  2363. amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
  2364. dce_v11_0_vga_enable(crtc, false);
  2365. }
  2366. amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
  2367. amdgpu_crtc->enabled = false;
  2368. break;
  2369. }
  2370. /* adjust pm to dpms */
  2371. amdgpu_pm_compute_clocks(adev);
  2372. }
  2373. static void dce_v11_0_crtc_prepare(struct drm_crtc *crtc)
  2374. {
  2375. /* disable crtc pair power gating before programming */
  2376. amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
  2377. amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
  2378. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  2379. }
  2380. static void dce_v11_0_crtc_commit(struct drm_crtc *crtc)
  2381. {
  2382. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  2383. amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
  2384. }
  2385. static void dce_v11_0_crtc_disable(struct drm_crtc *crtc)
  2386. {
  2387. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2388. struct drm_device *dev = crtc->dev;
  2389. struct amdgpu_device *adev = dev->dev_private;
  2390. struct amdgpu_atom_ss ss;
  2391. int i;
  2392. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  2393. if (crtc->primary->fb) {
  2394. int r;
  2395. struct amdgpu_framebuffer *amdgpu_fb;
  2396. struct amdgpu_bo *abo;
  2397. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  2398. abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  2399. r = amdgpu_bo_reserve(abo, false);
  2400. if (unlikely(r))
  2401. DRM_ERROR("failed to reserve abo before unpin\n");
  2402. else {
  2403. amdgpu_bo_unpin(abo);
  2404. amdgpu_bo_unreserve(abo);
  2405. }
  2406. }
  2407. /* disable the GRPH */
  2408. dce_v11_0_grph_enable(crtc, false);
  2409. amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
  2410. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2411. if (adev->mode_info.crtcs[i] &&
  2412. adev->mode_info.crtcs[i]->enabled &&
  2413. i != amdgpu_crtc->crtc_id &&
  2414. amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
  2415. /* one other crtc is using this pll don't turn
  2416. * off the pll
  2417. */
  2418. goto done;
  2419. }
  2420. }
  2421. switch (amdgpu_crtc->pll_id) {
  2422. case ATOM_PPLL0:
  2423. case ATOM_PPLL1:
  2424. case ATOM_PPLL2:
  2425. /* disable the ppll */
  2426. amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
  2427. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  2428. break;
  2429. case ATOM_COMBOPHY_PLL0:
  2430. case ATOM_COMBOPHY_PLL1:
  2431. case ATOM_COMBOPHY_PLL2:
  2432. case ATOM_COMBOPHY_PLL3:
  2433. case ATOM_COMBOPHY_PLL4:
  2434. case ATOM_COMBOPHY_PLL5:
  2435. /* disable the ppll */
  2436. amdgpu_atombios_crtc_program_pll(crtc, ATOM_CRTC_INVALID, amdgpu_crtc->pll_id,
  2437. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  2438. break;
  2439. default:
  2440. break;
  2441. }
  2442. done:
  2443. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  2444. amdgpu_crtc->adjusted_clock = 0;
  2445. amdgpu_crtc->encoder = NULL;
  2446. amdgpu_crtc->connector = NULL;
  2447. }
  2448. static int dce_v11_0_crtc_mode_set(struct drm_crtc *crtc,
  2449. struct drm_display_mode *mode,
  2450. struct drm_display_mode *adjusted_mode,
  2451. int x, int y, struct drm_framebuffer *old_fb)
  2452. {
  2453. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2454. struct drm_device *dev = crtc->dev;
  2455. struct amdgpu_device *adev = dev->dev_private;
  2456. if (!amdgpu_crtc->adjusted_clock)
  2457. return -EINVAL;
  2458. if ((adev->asic_type == CHIP_POLARIS10) ||
  2459. (adev->asic_type == CHIP_POLARIS11)) {
  2460. struct amdgpu_encoder *amdgpu_encoder =
  2461. to_amdgpu_encoder(amdgpu_crtc->encoder);
  2462. int encoder_mode =
  2463. amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder);
  2464. /* SetPixelClock calculates the plls and ss values now */
  2465. amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id,
  2466. amdgpu_crtc->pll_id,
  2467. encoder_mode, amdgpu_encoder->encoder_id,
  2468. adjusted_mode->clock, 0, 0, 0, 0,
  2469. amdgpu_crtc->bpc, amdgpu_crtc->ss_enabled, &amdgpu_crtc->ss);
  2470. } else {
  2471. amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
  2472. }
  2473. amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
  2474. dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  2475. amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
  2476. amdgpu_atombios_crtc_scaler_setup(crtc);
  2477. dce_v11_0_cursor_reset(crtc);
  2478. /* update the hw version fpr dpm */
  2479. amdgpu_crtc->hw_mode = *adjusted_mode;
  2480. return 0;
  2481. }
  2482. static bool dce_v11_0_crtc_mode_fixup(struct drm_crtc *crtc,
  2483. const struct drm_display_mode *mode,
  2484. struct drm_display_mode *adjusted_mode)
  2485. {
  2486. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2487. struct drm_device *dev = crtc->dev;
  2488. struct drm_encoder *encoder;
  2489. /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
  2490. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  2491. if (encoder->crtc == crtc) {
  2492. amdgpu_crtc->encoder = encoder;
  2493. amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
  2494. break;
  2495. }
  2496. }
  2497. if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
  2498. amdgpu_crtc->encoder = NULL;
  2499. amdgpu_crtc->connector = NULL;
  2500. return false;
  2501. }
  2502. if (!amdgpu_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
  2503. return false;
  2504. if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
  2505. return false;
  2506. /* pick pll */
  2507. amdgpu_crtc->pll_id = dce_v11_0_pick_pll(crtc);
  2508. /* if we can't get a PPLL for a non-DP encoder, fail */
  2509. if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
  2510. !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
  2511. return false;
  2512. return true;
  2513. }
  2514. static int dce_v11_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  2515. struct drm_framebuffer *old_fb)
  2516. {
  2517. return dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  2518. }
  2519. static int dce_v11_0_crtc_set_base_atomic(struct drm_crtc *crtc,
  2520. struct drm_framebuffer *fb,
  2521. int x, int y, enum mode_set_atomic state)
  2522. {
  2523. return dce_v11_0_crtc_do_set_base(crtc, fb, x, y, 1);
  2524. }
  2525. static const struct drm_crtc_helper_funcs dce_v11_0_crtc_helper_funcs = {
  2526. .dpms = dce_v11_0_crtc_dpms,
  2527. .mode_fixup = dce_v11_0_crtc_mode_fixup,
  2528. .mode_set = dce_v11_0_crtc_mode_set,
  2529. .mode_set_base = dce_v11_0_crtc_set_base,
  2530. .mode_set_base_atomic = dce_v11_0_crtc_set_base_atomic,
  2531. .prepare = dce_v11_0_crtc_prepare,
  2532. .commit = dce_v11_0_crtc_commit,
  2533. .load_lut = dce_v11_0_crtc_load_lut,
  2534. .disable = dce_v11_0_crtc_disable,
  2535. };
  2536. static int dce_v11_0_crtc_init(struct amdgpu_device *adev, int index)
  2537. {
  2538. struct amdgpu_crtc *amdgpu_crtc;
  2539. int i;
  2540. amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
  2541. (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  2542. if (amdgpu_crtc == NULL)
  2543. return -ENOMEM;
  2544. drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_v11_0_crtc_funcs);
  2545. drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
  2546. amdgpu_crtc->crtc_id = index;
  2547. adev->mode_info.crtcs[index] = amdgpu_crtc;
  2548. amdgpu_crtc->max_cursor_width = 128;
  2549. amdgpu_crtc->max_cursor_height = 128;
  2550. adev->ddev->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
  2551. adev->ddev->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
  2552. for (i = 0; i < 256; i++) {
  2553. amdgpu_crtc->lut_r[i] = i << 2;
  2554. amdgpu_crtc->lut_g[i] = i << 2;
  2555. amdgpu_crtc->lut_b[i] = i << 2;
  2556. }
  2557. switch (amdgpu_crtc->crtc_id) {
  2558. case 0:
  2559. default:
  2560. amdgpu_crtc->crtc_offset = CRTC0_REGISTER_OFFSET;
  2561. break;
  2562. case 1:
  2563. amdgpu_crtc->crtc_offset = CRTC1_REGISTER_OFFSET;
  2564. break;
  2565. case 2:
  2566. amdgpu_crtc->crtc_offset = CRTC2_REGISTER_OFFSET;
  2567. break;
  2568. case 3:
  2569. amdgpu_crtc->crtc_offset = CRTC3_REGISTER_OFFSET;
  2570. break;
  2571. case 4:
  2572. amdgpu_crtc->crtc_offset = CRTC4_REGISTER_OFFSET;
  2573. break;
  2574. case 5:
  2575. amdgpu_crtc->crtc_offset = CRTC5_REGISTER_OFFSET;
  2576. break;
  2577. }
  2578. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  2579. amdgpu_crtc->adjusted_clock = 0;
  2580. amdgpu_crtc->encoder = NULL;
  2581. amdgpu_crtc->connector = NULL;
  2582. drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v11_0_crtc_helper_funcs);
  2583. return 0;
  2584. }
  2585. static int dce_v11_0_early_init(void *handle)
  2586. {
  2587. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2588. adev->audio_endpt_rreg = &dce_v11_0_audio_endpt_rreg;
  2589. adev->audio_endpt_wreg = &dce_v11_0_audio_endpt_wreg;
  2590. dce_v11_0_set_display_funcs(adev);
  2591. dce_v11_0_set_irq_funcs(adev);
  2592. adev->mode_info.num_crtc = dce_v11_0_get_num_crtc(adev);
  2593. switch (adev->asic_type) {
  2594. case CHIP_CARRIZO:
  2595. adev->mode_info.num_hpd = 6;
  2596. adev->mode_info.num_dig = 9;
  2597. break;
  2598. case CHIP_STONEY:
  2599. adev->mode_info.num_hpd = 6;
  2600. adev->mode_info.num_dig = 9;
  2601. break;
  2602. case CHIP_POLARIS10:
  2603. adev->mode_info.num_hpd = 6;
  2604. adev->mode_info.num_dig = 6;
  2605. break;
  2606. case CHIP_POLARIS11:
  2607. adev->mode_info.num_hpd = 5;
  2608. adev->mode_info.num_dig = 5;
  2609. break;
  2610. default:
  2611. /* FIXME: not supported yet */
  2612. return -EINVAL;
  2613. }
  2614. return 0;
  2615. }
  2616. static int dce_v11_0_sw_init(void *handle)
  2617. {
  2618. int r, i;
  2619. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2620. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2621. r = amdgpu_irq_add_id(adev, i + 1, &adev->crtc_irq);
  2622. if (r)
  2623. return r;
  2624. }
  2625. for (i = 8; i < 20; i += 2) {
  2626. r = amdgpu_irq_add_id(adev, i, &adev->pageflip_irq);
  2627. if (r)
  2628. return r;
  2629. }
  2630. /* HPD hotplug */
  2631. r = amdgpu_irq_add_id(adev, 42, &adev->hpd_irq);
  2632. if (r)
  2633. return r;
  2634. adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
  2635. adev->ddev->mode_config.async_page_flip = true;
  2636. adev->ddev->mode_config.max_width = 16384;
  2637. adev->ddev->mode_config.max_height = 16384;
  2638. adev->ddev->mode_config.preferred_depth = 24;
  2639. adev->ddev->mode_config.prefer_shadow = 1;
  2640. adev->ddev->mode_config.fb_base = adev->mc.aper_base;
  2641. r = amdgpu_modeset_create_props(adev);
  2642. if (r)
  2643. return r;
  2644. adev->ddev->mode_config.max_width = 16384;
  2645. adev->ddev->mode_config.max_height = 16384;
  2646. /* allocate crtcs */
  2647. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2648. r = dce_v11_0_crtc_init(adev, i);
  2649. if (r)
  2650. return r;
  2651. }
  2652. if (amdgpu_atombios_get_connector_info_from_object_table(adev))
  2653. amdgpu_print_display_setup(adev->ddev);
  2654. else
  2655. return -EINVAL;
  2656. /* setup afmt */
  2657. r = dce_v11_0_afmt_init(adev);
  2658. if (r)
  2659. return r;
  2660. r = dce_v11_0_audio_init(adev);
  2661. if (r)
  2662. return r;
  2663. drm_kms_helper_poll_init(adev->ddev);
  2664. adev->mode_info.mode_config_initialized = true;
  2665. return 0;
  2666. }
  2667. static int dce_v11_0_sw_fini(void *handle)
  2668. {
  2669. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2670. kfree(adev->mode_info.bios_hardcoded_edid);
  2671. drm_kms_helper_poll_fini(adev->ddev);
  2672. dce_v11_0_audio_fini(adev);
  2673. dce_v11_0_afmt_fini(adev);
  2674. drm_mode_config_cleanup(adev->ddev);
  2675. adev->mode_info.mode_config_initialized = false;
  2676. return 0;
  2677. }
  2678. static int dce_v11_0_hw_init(void *handle)
  2679. {
  2680. int i;
  2681. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2682. dce_v11_0_init_golden_registers(adev);
  2683. /* init dig PHYs, disp eng pll */
  2684. amdgpu_atombios_crtc_powergate_init(adev);
  2685. amdgpu_atombios_encoder_init_dig(adev);
  2686. if ((adev->asic_type == CHIP_POLARIS10) ||
  2687. (adev->asic_type == CHIP_POLARIS11)) {
  2688. amdgpu_atombios_crtc_set_dce_clock(adev, adev->clock.default_dispclk,
  2689. DCE_CLOCK_TYPE_DISPCLK, ATOM_GCK_DFS);
  2690. amdgpu_atombios_crtc_set_dce_clock(adev, 0,
  2691. DCE_CLOCK_TYPE_DPREFCLK, ATOM_GCK_DFS);
  2692. } else {
  2693. amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
  2694. }
  2695. /* initialize hpd */
  2696. dce_v11_0_hpd_init(adev);
  2697. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2698. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2699. }
  2700. dce_v11_0_pageflip_interrupt_init(adev);
  2701. return 0;
  2702. }
  2703. static int dce_v11_0_hw_fini(void *handle)
  2704. {
  2705. int i;
  2706. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2707. dce_v11_0_hpd_fini(adev);
  2708. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2709. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2710. }
  2711. dce_v11_0_pageflip_interrupt_fini(adev);
  2712. return 0;
  2713. }
  2714. static int dce_v11_0_suspend(void *handle)
  2715. {
  2716. return dce_v11_0_hw_fini(handle);
  2717. }
  2718. static int dce_v11_0_resume(void *handle)
  2719. {
  2720. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2721. int ret;
  2722. ret = dce_v11_0_hw_init(handle);
  2723. /* turn on the BL */
  2724. if (adev->mode_info.bl_encoder) {
  2725. u8 bl_level = amdgpu_display_backlight_get_level(adev,
  2726. adev->mode_info.bl_encoder);
  2727. amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
  2728. bl_level);
  2729. }
  2730. return ret;
  2731. }
  2732. static bool dce_v11_0_is_idle(void *handle)
  2733. {
  2734. return true;
  2735. }
  2736. static int dce_v11_0_wait_for_idle(void *handle)
  2737. {
  2738. return 0;
  2739. }
  2740. static int dce_v11_0_soft_reset(void *handle)
  2741. {
  2742. u32 srbm_soft_reset = 0, tmp;
  2743. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2744. if (dce_v11_0_is_display_hung(adev))
  2745. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
  2746. if (srbm_soft_reset) {
  2747. tmp = RREG32(mmSRBM_SOFT_RESET);
  2748. tmp |= srbm_soft_reset;
  2749. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  2750. WREG32(mmSRBM_SOFT_RESET, tmp);
  2751. tmp = RREG32(mmSRBM_SOFT_RESET);
  2752. udelay(50);
  2753. tmp &= ~srbm_soft_reset;
  2754. WREG32(mmSRBM_SOFT_RESET, tmp);
  2755. tmp = RREG32(mmSRBM_SOFT_RESET);
  2756. /* Wait a little for things to settle down */
  2757. udelay(50);
  2758. }
  2759. return 0;
  2760. }
  2761. static void dce_v11_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
  2762. int crtc,
  2763. enum amdgpu_interrupt_state state)
  2764. {
  2765. u32 lb_interrupt_mask;
  2766. if (crtc >= adev->mode_info.num_crtc) {
  2767. DRM_DEBUG("invalid crtc %d\n", crtc);
  2768. return;
  2769. }
  2770. switch (state) {
  2771. case AMDGPU_IRQ_STATE_DISABLE:
  2772. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2773. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2774. VBLANK_INTERRUPT_MASK, 0);
  2775. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2776. break;
  2777. case AMDGPU_IRQ_STATE_ENABLE:
  2778. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2779. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2780. VBLANK_INTERRUPT_MASK, 1);
  2781. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2782. break;
  2783. default:
  2784. break;
  2785. }
  2786. }
  2787. static void dce_v11_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
  2788. int crtc,
  2789. enum amdgpu_interrupt_state state)
  2790. {
  2791. u32 lb_interrupt_mask;
  2792. if (crtc >= adev->mode_info.num_crtc) {
  2793. DRM_DEBUG("invalid crtc %d\n", crtc);
  2794. return;
  2795. }
  2796. switch (state) {
  2797. case AMDGPU_IRQ_STATE_DISABLE:
  2798. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2799. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2800. VLINE_INTERRUPT_MASK, 0);
  2801. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2802. break;
  2803. case AMDGPU_IRQ_STATE_ENABLE:
  2804. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2805. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2806. VLINE_INTERRUPT_MASK, 1);
  2807. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2808. break;
  2809. default:
  2810. break;
  2811. }
  2812. }
  2813. static int dce_v11_0_set_hpd_irq_state(struct amdgpu_device *adev,
  2814. struct amdgpu_irq_src *source,
  2815. unsigned hpd,
  2816. enum amdgpu_interrupt_state state)
  2817. {
  2818. u32 tmp;
  2819. if (hpd >= adev->mode_info.num_hpd) {
  2820. DRM_DEBUG("invalid hdp %d\n", hpd);
  2821. return 0;
  2822. }
  2823. switch (state) {
  2824. case AMDGPU_IRQ_STATE_DISABLE:
  2825. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2826. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
  2827. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2828. break;
  2829. case AMDGPU_IRQ_STATE_ENABLE:
  2830. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2831. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 1);
  2832. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2833. break;
  2834. default:
  2835. break;
  2836. }
  2837. return 0;
  2838. }
  2839. static int dce_v11_0_set_crtc_irq_state(struct amdgpu_device *adev,
  2840. struct amdgpu_irq_src *source,
  2841. unsigned type,
  2842. enum amdgpu_interrupt_state state)
  2843. {
  2844. switch (type) {
  2845. case AMDGPU_CRTC_IRQ_VBLANK1:
  2846. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 0, state);
  2847. break;
  2848. case AMDGPU_CRTC_IRQ_VBLANK2:
  2849. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 1, state);
  2850. break;
  2851. case AMDGPU_CRTC_IRQ_VBLANK3:
  2852. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 2, state);
  2853. break;
  2854. case AMDGPU_CRTC_IRQ_VBLANK4:
  2855. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 3, state);
  2856. break;
  2857. case AMDGPU_CRTC_IRQ_VBLANK5:
  2858. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 4, state);
  2859. break;
  2860. case AMDGPU_CRTC_IRQ_VBLANK6:
  2861. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 5, state);
  2862. break;
  2863. case AMDGPU_CRTC_IRQ_VLINE1:
  2864. dce_v11_0_set_crtc_vline_interrupt_state(adev, 0, state);
  2865. break;
  2866. case AMDGPU_CRTC_IRQ_VLINE2:
  2867. dce_v11_0_set_crtc_vline_interrupt_state(adev, 1, state);
  2868. break;
  2869. case AMDGPU_CRTC_IRQ_VLINE3:
  2870. dce_v11_0_set_crtc_vline_interrupt_state(adev, 2, state);
  2871. break;
  2872. case AMDGPU_CRTC_IRQ_VLINE4:
  2873. dce_v11_0_set_crtc_vline_interrupt_state(adev, 3, state);
  2874. break;
  2875. case AMDGPU_CRTC_IRQ_VLINE5:
  2876. dce_v11_0_set_crtc_vline_interrupt_state(adev, 4, state);
  2877. break;
  2878. case AMDGPU_CRTC_IRQ_VLINE6:
  2879. dce_v11_0_set_crtc_vline_interrupt_state(adev, 5, state);
  2880. break;
  2881. default:
  2882. break;
  2883. }
  2884. return 0;
  2885. }
  2886. static int dce_v11_0_set_pageflip_irq_state(struct amdgpu_device *adev,
  2887. struct amdgpu_irq_src *src,
  2888. unsigned type,
  2889. enum amdgpu_interrupt_state state)
  2890. {
  2891. u32 reg;
  2892. if (type >= adev->mode_info.num_crtc) {
  2893. DRM_ERROR("invalid pageflip crtc %d\n", type);
  2894. return -EINVAL;
  2895. }
  2896. reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
  2897. if (state == AMDGPU_IRQ_STATE_DISABLE)
  2898. WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
  2899. reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2900. else
  2901. WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
  2902. reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2903. return 0;
  2904. }
  2905. static int dce_v11_0_pageflip_irq(struct amdgpu_device *adev,
  2906. struct amdgpu_irq_src *source,
  2907. struct amdgpu_iv_entry *entry)
  2908. {
  2909. unsigned long flags;
  2910. unsigned crtc_id;
  2911. struct amdgpu_crtc *amdgpu_crtc;
  2912. struct amdgpu_flip_work *works;
  2913. crtc_id = (entry->src_id - 8) >> 1;
  2914. amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  2915. if (crtc_id >= adev->mode_info.num_crtc) {
  2916. DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
  2917. return -EINVAL;
  2918. }
  2919. if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
  2920. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
  2921. WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
  2922. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
  2923. /* IRQ could occur when in initial stage */
  2924. if(amdgpu_crtc == NULL)
  2925. return 0;
  2926. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  2927. works = amdgpu_crtc->pflip_works;
  2928. if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
  2929. DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
  2930. "AMDGPU_FLIP_SUBMITTED(%d)\n",
  2931. amdgpu_crtc->pflip_status,
  2932. AMDGPU_FLIP_SUBMITTED);
  2933. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2934. return 0;
  2935. }
  2936. /* page flip completed. clean up */
  2937. amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
  2938. amdgpu_crtc->pflip_works = NULL;
  2939. /* wakeup usersapce */
  2940. if(works->event)
  2941. drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
  2942. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2943. drm_crtc_vblank_put(&amdgpu_crtc->base);
  2944. schedule_work(&works->unpin_work);
  2945. return 0;
  2946. }
  2947. static void dce_v11_0_hpd_int_ack(struct amdgpu_device *adev,
  2948. int hpd)
  2949. {
  2950. u32 tmp;
  2951. if (hpd >= adev->mode_info.num_hpd) {
  2952. DRM_DEBUG("invalid hdp %d\n", hpd);
  2953. return;
  2954. }
  2955. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2956. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_ACK, 1);
  2957. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2958. }
  2959. static void dce_v11_0_crtc_vblank_int_ack(struct amdgpu_device *adev,
  2960. int crtc)
  2961. {
  2962. u32 tmp;
  2963. if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
  2964. DRM_DEBUG("invalid crtc %d\n", crtc);
  2965. return;
  2966. }
  2967. tmp = RREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc]);
  2968. tmp = REG_SET_FIELD(tmp, LB_VBLANK_STATUS, VBLANK_ACK, 1);
  2969. WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], tmp);
  2970. }
  2971. static void dce_v11_0_crtc_vline_int_ack(struct amdgpu_device *adev,
  2972. int crtc)
  2973. {
  2974. u32 tmp;
  2975. if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
  2976. DRM_DEBUG("invalid crtc %d\n", crtc);
  2977. return;
  2978. }
  2979. tmp = RREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc]);
  2980. tmp = REG_SET_FIELD(tmp, LB_VLINE_STATUS, VLINE_ACK, 1);
  2981. WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], tmp);
  2982. }
  2983. static int dce_v11_0_crtc_irq(struct amdgpu_device *adev,
  2984. struct amdgpu_irq_src *source,
  2985. struct amdgpu_iv_entry *entry)
  2986. {
  2987. unsigned crtc = entry->src_id - 1;
  2988. uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
  2989. unsigned irq_type = amdgpu_crtc_idx_to_irq_type(adev, crtc);
  2990. switch (entry->src_data) {
  2991. case 0: /* vblank */
  2992. if (disp_int & interrupt_status_offsets[crtc].vblank)
  2993. dce_v11_0_crtc_vblank_int_ack(adev, crtc);
  2994. else
  2995. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  2996. if (amdgpu_irq_enabled(adev, source, irq_type)) {
  2997. drm_handle_vblank(adev->ddev, crtc);
  2998. }
  2999. DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
  3000. break;
  3001. case 1: /* vline */
  3002. if (disp_int & interrupt_status_offsets[crtc].vline)
  3003. dce_v11_0_crtc_vline_int_ack(adev, crtc);
  3004. else
  3005. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  3006. DRM_DEBUG("IH: D%d vline\n", crtc + 1);
  3007. break;
  3008. default:
  3009. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
  3010. break;
  3011. }
  3012. return 0;
  3013. }
  3014. static int dce_v11_0_hpd_irq(struct amdgpu_device *adev,
  3015. struct amdgpu_irq_src *source,
  3016. struct amdgpu_iv_entry *entry)
  3017. {
  3018. uint32_t disp_int, mask;
  3019. unsigned hpd;
  3020. if (entry->src_data >= adev->mode_info.num_hpd) {
  3021. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
  3022. return 0;
  3023. }
  3024. hpd = entry->src_data;
  3025. disp_int = RREG32(interrupt_status_offsets[hpd].reg);
  3026. mask = interrupt_status_offsets[hpd].hpd;
  3027. if (disp_int & mask) {
  3028. dce_v11_0_hpd_int_ack(adev, hpd);
  3029. schedule_work(&adev->hotplug_work);
  3030. DRM_DEBUG("IH: HPD%d\n", hpd + 1);
  3031. }
  3032. return 0;
  3033. }
  3034. static int dce_v11_0_set_clockgating_state(void *handle,
  3035. enum amd_clockgating_state state)
  3036. {
  3037. return 0;
  3038. }
  3039. static int dce_v11_0_set_powergating_state(void *handle,
  3040. enum amd_powergating_state state)
  3041. {
  3042. return 0;
  3043. }
  3044. static const struct amd_ip_funcs dce_v11_0_ip_funcs = {
  3045. .name = "dce_v11_0",
  3046. .early_init = dce_v11_0_early_init,
  3047. .late_init = NULL,
  3048. .sw_init = dce_v11_0_sw_init,
  3049. .sw_fini = dce_v11_0_sw_fini,
  3050. .hw_init = dce_v11_0_hw_init,
  3051. .hw_fini = dce_v11_0_hw_fini,
  3052. .suspend = dce_v11_0_suspend,
  3053. .resume = dce_v11_0_resume,
  3054. .is_idle = dce_v11_0_is_idle,
  3055. .wait_for_idle = dce_v11_0_wait_for_idle,
  3056. .soft_reset = dce_v11_0_soft_reset,
  3057. .set_clockgating_state = dce_v11_0_set_clockgating_state,
  3058. .set_powergating_state = dce_v11_0_set_powergating_state,
  3059. };
  3060. static void
  3061. dce_v11_0_encoder_mode_set(struct drm_encoder *encoder,
  3062. struct drm_display_mode *mode,
  3063. struct drm_display_mode *adjusted_mode)
  3064. {
  3065. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3066. amdgpu_encoder->pixel_clock = adjusted_mode->clock;
  3067. /* need to call this here rather than in prepare() since we need some crtc info */
  3068. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  3069. /* set scaler clears this on some chips */
  3070. dce_v11_0_set_interleave(encoder->crtc, mode);
  3071. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
  3072. dce_v11_0_afmt_enable(encoder, true);
  3073. dce_v11_0_afmt_setmode(encoder, adjusted_mode);
  3074. }
  3075. }
  3076. static void dce_v11_0_encoder_prepare(struct drm_encoder *encoder)
  3077. {
  3078. struct amdgpu_device *adev = encoder->dev->dev_private;
  3079. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3080. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  3081. if ((amdgpu_encoder->active_device &
  3082. (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
  3083. (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
  3084. ENCODER_OBJECT_ID_NONE)) {
  3085. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  3086. if (dig) {
  3087. dig->dig_encoder = dce_v11_0_pick_dig_encoder(encoder);
  3088. if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
  3089. dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
  3090. }
  3091. }
  3092. amdgpu_atombios_scratch_regs_lock(adev, true);
  3093. if (connector) {
  3094. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  3095. /* select the clock/data port if it uses a router */
  3096. if (amdgpu_connector->router.cd_valid)
  3097. amdgpu_i2c_router_select_cd_port(amdgpu_connector);
  3098. /* turn eDP panel on for mode set */
  3099. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  3100. amdgpu_atombios_encoder_set_edp_panel_power(connector,
  3101. ATOM_TRANSMITTER_ACTION_POWER_ON);
  3102. }
  3103. /* this is needed for the pll/ss setup to work correctly in some cases */
  3104. amdgpu_atombios_encoder_set_crtc_source(encoder);
  3105. /* set up the FMT blocks */
  3106. dce_v11_0_program_fmt(encoder);
  3107. }
  3108. static void dce_v11_0_encoder_commit(struct drm_encoder *encoder)
  3109. {
  3110. struct drm_device *dev = encoder->dev;
  3111. struct amdgpu_device *adev = dev->dev_private;
  3112. /* need to call this here as we need the crtc set up */
  3113. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  3114. amdgpu_atombios_scratch_regs_lock(adev, false);
  3115. }
  3116. static void dce_v11_0_encoder_disable(struct drm_encoder *encoder)
  3117. {
  3118. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3119. struct amdgpu_encoder_atom_dig *dig;
  3120. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  3121. if (amdgpu_atombios_encoder_is_digital(encoder)) {
  3122. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
  3123. dce_v11_0_afmt_enable(encoder, false);
  3124. dig = amdgpu_encoder->enc_priv;
  3125. dig->dig_encoder = -1;
  3126. }
  3127. amdgpu_encoder->active_device = 0;
  3128. }
  3129. /* these are handled by the primary encoders */
  3130. static void dce_v11_0_ext_prepare(struct drm_encoder *encoder)
  3131. {
  3132. }
  3133. static void dce_v11_0_ext_commit(struct drm_encoder *encoder)
  3134. {
  3135. }
  3136. static void
  3137. dce_v11_0_ext_mode_set(struct drm_encoder *encoder,
  3138. struct drm_display_mode *mode,
  3139. struct drm_display_mode *adjusted_mode)
  3140. {
  3141. }
  3142. static void dce_v11_0_ext_disable(struct drm_encoder *encoder)
  3143. {
  3144. }
  3145. static void
  3146. dce_v11_0_ext_dpms(struct drm_encoder *encoder, int mode)
  3147. {
  3148. }
  3149. static const struct drm_encoder_helper_funcs dce_v11_0_ext_helper_funcs = {
  3150. .dpms = dce_v11_0_ext_dpms,
  3151. .prepare = dce_v11_0_ext_prepare,
  3152. .mode_set = dce_v11_0_ext_mode_set,
  3153. .commit = dce_v11_0_ext_commit,
  3154. .disable = dce_v11_0_ext_disable,
  3155. /* no detect for TMDS/LVDS yet */
  3156. };
  3157. static const struct drm_encoder_helper_funcs dce_v11_0_dig_helper_funcs = {
  3158. .dpms = amdgpu_atombios_encoder_dpms,
  3159. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  3160. .prepare = dce_v11_0_encoder_prepare,
  3161. .mode_set = dce_v11_0_encoder_mode_set,
  3162. .commit = dce_v11_0_encoder_commit,
  3163. .disable = dce_v11_0_encoder_disable,
  3164. .detect = amdgpu_atombios_encoder_dig_detect,
  3165. };
  3166. static const struct drm_encoder_helper_funcs dce_v11_0_dac_helper_funcs = {
  3167. .dpms = amdgpu_atombios_encoder_dpms,
  3168. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  3169. .prepare = dce_v11_0_encoder_prepare,
  3170. .mode_set = dce_v11_0_encoder_mode_set,
  3171. .commit = dce_v11_0_encoder_commit,
  3172. .detect = amdgpu_atombios_encoder_dac_detect,
  3173. };
  3174. static void dce_v11_0_encoder_destroy(struct drm_encoder *encoder)
  3175. {
  3176. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3177. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  3178. amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
  3179. kfree(amdgpu_encoder->enc_priv);
  3180. drm_encoder_cleanup(encoder);
  3181. kfree(amdgpu_encoder);
  3182. }
  3183. static const struct drm_encoder_funcs dce_v11_0_encoder_funcs = {
  3184. .destroy = dce_v11_0_encoder_destroy,
  3185. };
  3186. static void dce_v11_0_encoder_add(struct amdgpu_device *adev,
  3187. uint32_t encoder_enum,
  3188. uint32_t supported_device,
  3189. u16 caps)
  3190. {
  3191. struct drm_device *dev = adev->ddev;
  3192. struct drm_encoder *encoder;
  3193. struct amdgpu_encoder *amdgpu_encoder;
  3194. /* see if we already added it */
  3195. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  3196. amdgpu_encoder = to_amdgpu_encoder(encoder);
  3197. if (amdgpu_encoder->encoder_enum == encoder_enum) {
  3198. amdgpu_encoder->devices |= supported_device;
  3199. return;
  3200. }
  3201. }
  3202. /* add a new one */
  3203. amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
  3204. if (!amdgpu_encoder)
  3205. return;
  3206. encoder = &amdgpu_encoder->base;
  3207. switch (adev->mode_info.num_crtc) {
  3208. case 1:
  3209. encoder->possible_crtcs = 0x1;
  3210. break;
  3211. case 2:
  3212. default:
  3213. encoder->possible_crtcs = 0x3;
  3214. break;
  3215. case 4:
  3216. encoder->possible_crtcs = 0xf;
  3217. break;
  3218. case 6:
  3219. encoder->possible_crtcs = 0x3f;
  3220. break;
  3221. }
  3222. amdgpu_encoder->enc_priv = NULL;
  3223. amdgpu_encoder->encoder_enum = encoder_enum;
  3224. amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
  3225. amdgpu_encoder->devices = supported_device;
  3226. amdgpu_encoder->rmx_type = RMX_OFF;
  3227. amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
  3228. amdgpu_encoder->is_ext_encoder = false;
  3229. amdgpu_encoder->caps = caps;
  3230. switch (amdgpu_encoder->encoder_id) {
  3231. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
  3232. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
  3233. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3234. DRM_MODE_ENCODER_DAC, NULL);
  3235. drm_encoder_helper_add(encoder, &dce_v11_0_dac_helper_funcs);
  3236. break;
  3237. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
  3238. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  3239. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  3240. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  3241. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
  3242. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
  3243. amdgpu_encoder->rmx_type = RMX_FULL;
  3244. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3245. DRM_MODE_ENCODER_LVDS, NULL);
  3246. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
  3247. } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
  3248. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3249. DRM_MODE_ENCODER_DAC, NULL);
  3250. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  3251. } else {
  3252. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3253. DRM_MODE_ENCODER_TMDS, NULL);
  3254. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  3255. }
  3256. drm_encoder_helper_add(encoder, &dce_v11_0_dig_helper_funcs);
  3257. break;
  3258. case ENCODER_OBJECT_ID_SI170B:
  3259. case ENCODER_OBJECT_ID_CH7303:
  3260. case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
  3261. case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
  3262. case ENCODER_OBJECT_ID_TITFP513:
  3263. case ENCODER_OBJECT_ID_VT1623:
  3264. case ENCODER_OBJECT_ID_HDMI_SI1930:
  3265. case ENCODER_OBJECT_ID_TRAVIS:
  3266. case ENCODER_OBJECT_ID_NUTMEG:
  3267. /* these are handled by the primary encoders */
  3268. amdgpu_encoder->is_ext_encoder = true;
  3269. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  3270. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3271. DRM_MODE_ENCODER_LVDS, NULL);
  3272. else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
  3273. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3274. DRM_MODE_ENCODER_DAC, NULL);
  3275. else
  3276. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3277. DRM_MODE_ENCODER_TMDS, NULL);
  3278. drm_encoder_helper_add(encoder, &dce_v11_0_ext_helper_funcs);
  3279. break;
  3280. }
  3281. }
  3282. static const struct amdgpu_display_funcs dce_v11_0_display_funcs = {
  3283. .set_vga_render_state = &dce_v11_0_set_vga_render_state,
  3284. .bandwidth_update = &dce_v11_0_bandwidth_update,
  3285. .vblank_get_counter = &dce_v11_0_vblank_get_counter,
  3286. .vblank_wait = &dce_v11_0_vblank_wait,
  3287. .is_display_hung = &dce_v11_0_is_display_hung,
  3288. .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
  3289. .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
  3290. .hpd_sense = &dce_v11_0_hpd_sense,
  3291. .hpd_set_polarity = &dce_v11_0_hpd_set_polarity,
  3292. .hpd_get_gpio_reg = &dce_v11_0_hpd_get_gpio_reg,
  3293. .page_flip = &dce_v11_0_page_flip,
  3294. .page_flip_get_scanoutpos = &dce_v11_0_crtc_get_scanoutpos,
  3295. .add_encoder = &dce_v11_0_encoder_add,
  3296. .add_connector = &amdgpu_connector_add,
  3297. .stop_mc_access = &dce_v11_0_stop_mc_access,
  3298. .resume_mc_access = &dce_v11_0_resume_mc_access,
  3299. };
  3300. static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev)
  3301. {
  3302. if (adev->mode_info.funcs == NULL)
  3303. adev->mode_info.funcs = &dce_v11_0_display_funcs;
  3304. }
  3305. static const struct amdgpu_irq_src_funcs dce_v11_0_crtc_irq_funcs = {
  3306. .set = dce_v11_0_set_crtc_irq_state,
  3307. .process = dce_v11_0_crtc_irq,
  3308. };
  3309. static const struct amdgpu_irq_src_funcs dce_v11_0_pageflip_irq_funcs = {
  3310. .set = dce_v11_0_set_pageflip_irq_state,
  3311. .process = dce_v11_0_pageflip_irq,
  3312. };
  3313. static const struct amdgpu_irq_src_funcs dce_v11_0_hpd_irq_funcs = {
  3314. .set = dce_v11_0_set_hpd_irq_state,
  3315. .process = dce_v11_0_hpd_irq,
  3316. };
  3317. static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev)
  3318. {
  3319. adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_LAST;
  3320. adev->crtc_irq.funcs = &dce_v11_0_crtc_irq_funcs;
  3321. adev->pageflip_irq.num_types = AMDGPU_PAGEFLIP_IRQ_LAST;
  3322. adev->pageflip_irq.funcs = &dce_v11_0_pageflip_irq_funcs;
  3323. adev->hpd_irq.num_types = AMDGPU_HPD_LAST;
  3324. adev->hpd_irq.funcs = &dce_v11_0_hpd_irq_funcs;
  3325. }
  3326. const struct amdgpu_ip_block_version dce_v11_0_ip_block =
  3327. {
  3328. .type = AMD_IP_BLOCK_TYPE_DCE,
  3329. .major = 11,
  3330. .minor = 0,
  3331. .rev = 0,
  3332. .funcs = &dce_v11_0_ip_funcs,
  3333. };
  3334. const struct amdgpu_ip_block_version dce_v11_2_ip_block =
  3335. {
  3336. .type = AMD_IP_BLOCK_TYPE_DCE,
  3337. .major = 11,
  3338. .minor = 2,
  3339. .rev = 0,
  3340. .funcs = &dce_v11_0_ip_funcs,
  3341. };