dce_v11_0.c 117 KB

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