dce_v11_0.c 117 KB

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