dce_v11_0.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include "drmP.h"
  24. #include "amdgpu.h"
  25. #include "amdgpu_pm.h"
  26. #include "amdgpu_i2c.h"
  27. #include "vid.h"
  28. #include "atom.h"
  29. #include "amdgpu_atombios.h"
  30. #include "atombios_crtc.h"
  31. #include "atombios_encoders.h"
  32. #include "amdgpu_pll.h"
  33. #include "amdgpu_connectors.h"
  34. #include "dce/dce_11_0_d.h"
  35. #include "dce/dce_11_0_sh_mask.h"
  36. #include "dce/dce_11_0_enum.h"
  37. #include "oss/oss_3_0_d.h"
  38. #include "oss/oss_3_0_sh_mask.h"
  39. #include "gmc/gmc_8_1_d.h"
  40. #include "gmc/gmc_8_1_sh_mask.h"
  41. static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev);
  42. static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev);
  43. static const u32 crtc_offsets[] =
  44. {
  45. CRTC0_REGISTER_OFFSET,
  46. CRTC1_REGISTER_OFFSET,
  47. CRTC2_REGISTER_OFFSET,
  48. CRTC3_REGISTER_OFFSET,
  49. CRTC4_REGISTER_OFFSET,
  50. CRTC5_REGISTER_OFFSET,
  51. CRTC6_REGISTER_OFFSET
  52. };
  53. static const u32 hpd_offsets[] =
  54. {
  55. HPD0_REGISTER_OFFSET,
  56. HPD1_REGISTER_OFFSET,
  57. HPD2_REGISTER_OFFSET,
  58. HPD3_REGISTER_OFFSET,
  59. HPD4_REGISTER_OFFSET,
  60. HPD5_REGISTER_OFFSET
  61. };
  62. static const uint32_t dig_offsets[] = {
  63. DIG0_REGISTER_OFFSET,
  64. DIG1_REGISTER_OFFSET,
  65. DIG2_REGISTER_OFFSET,
  66. DIG3_REGISTER_OFFSET,
  67. DIG4_REGISTER_OFFSET,
  68. DIG5_REGISTER_OFFSET,
  69. DIG6_REGISTER_OFFSET,
  70. DIG7_REGISTER_OFFSET,
  71. DIG8_REGISTER_OFFSET
  72. };
  73. static const struct {
  74. uint32_t reg;
  75. uint32_t vblank;
  76. uint32_t vline;
  77. uint32_t hpd;
  78. } interrupt_status_offsets[] = { {
  79. .reg = mmDISP_INTERRUPT_STATUS,
  80. .vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
  81. .vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
  82. .hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
  83. }, {
  84. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE,
  85. .vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
  86. .vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
  87. .hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
  88. }, {
  89. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE2,
  90. .vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
  91. .vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
  92. .hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
  93. }, {
  94. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE3,
  95. .vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
  96. .vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
  97. .hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
  98. }, {
  99. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE4,
  100. .vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
  101. .vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
  102. .hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
  103. }, {
  104. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE5,
  105. .vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
  106. .vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
  107. .hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
  108. } };
  109. static const u32 cz_golden_settings_a11[] =
  110. {
  111. mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
  112. mmFBC_MISC, 0x1f311fff, 0x14300000,
  113. };
  114. static const u32 cz_mgcg_cgcg_init[] =
  115. {
  116. mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
  117. mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
  118. };
  119. static const u32 stoney_golden_settings_a11[] =
  120. {
  121. mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
  122. mmFBC_MISC, 0x1f311fff, 0x14302000,
  123. };
  124. static const u32 polaris11_golden_settings_a11[] =
  125. {
  126. mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
  127. mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
  128. mmFBC_DEBUG1, 0xffffffff, 0x00000008,
  129. mmFBC_MISC, 0x9f313fff, 0x14300008,
  130. mmHDMI_CONTROL, 0x313f031f, 0x00000011,
  131. };
  132. static const u32 polaris10_golden_settings_a11[] =
  133. {
  134. mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
  135. mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
  136. mmFBC_MISC, 0x9f313fff, 0x14300008,
  137. mmHDMI_CONTROL, 0x313f031f, 0x00000011,
  138. };
  139. static void dce_v11_0_init_golden_registers(struct amdgpu_device *adev)
  140. {
  141. switch (adev->asic_type) {
  142. case CHIP_CARRIZO:
  143. amdgpu_program_register_sequence(adev,
  144. cz_mgcg_cgcg_init,
  145. (const u32)ARRAY_SIZE(cz_mgcg_cgcg_init));
  146. amdgpu_program_register_sequence(adev,
  147. cz_golden_settings_a11,
  148. (const u32)ARRAY_SIZE(cz_golden_settings_a11));
  149. break;
  150. case CHIP_STONEY:
  151. amdgpu_program_register_sequence(adev,
  152. stoney_golden_settings_a11,
  153. (const u32)ARRAY_SIZE(stoney_golden_settings_a11));
  154. break;
  155. case CHIP_POLARIS11:
  156. amdgpu_program_register_sequence(adev,
  157. polaris11_golden_settings_a11,
  158. (const u32)ARRAY_SIZE(polaris11_golden_settings_a11));
  159. break;
  160. case CHIP_POLARIS10:
  161. amdgpu_program_register_sequence(adev,
  162. polaris10_golden_settings_a11,
  163. (const u32)ARRAY_SIZE(polaris10_golden_settings_a11));
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. static u32 dce_v11_0_audio_endpt_rreg(struct amdgpu_device *adev,
  170. u32 block_offset, u32 reg)
  171. {
  172. unsigned long flags;
  173. u32 r;
  174. spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
  175. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
  176. r = RREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset);
  177. spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
  178. return r;
  179. }
  180. static void dce_v11_0_audio_endpt_wreg(struct amdgpu_device *adev,
  181. u32 block_offset, u32 reg, u32 v)
  182. {
  183. unsigned long flags;
  184. spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
  185. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
  186. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset, v);
  187. spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
  188. }
  189. static bool dce_v11_0_is_in_vblank(struct amdgpu_device *adev, int crtc)
  190. {
  191. if (RREG32(mmCRTC_STATUS + crtc_offsets[crtc]) &
  192. CRTC_V_BLANK_START_END__CRTC_V_BLANK_START_MASK)
  193. return true;
  194. else
  195. return false;
  196. }
  197. static bool dce_v11_0_is_counter_moving(struct amdgpu_device *adev, int crtc)
  198. {
  199. u32 pos1, pos2;
  200. pos1 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  201. pos2 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  202. if (pos1 != pos2)
  203. return true;
  204. else
  205. return false;
  206. }
  207. /**
  208. * dce_v11_0_vblank_wait - vblank wait asic callback.
  209. *
  210. * @adev: amdgpu_device pointer
  211. * @crtc: crtc to wait for vblank on
  212. *
  213. * Wait for vblank on the requested crtc (evergreen+).
  214. */
  215. static void dce_v11_0_vblank_wait(struct amdgpu_device *adev, int crtc)
  216. {
  217. unsigned i = 100;
  218. if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
  219. return;
  220. if (!(RREG32(mmCRTC_CONTROL + crtc_offsets[crtc]) & CRTC_CONTROL__CRTC_MASTER_EN_MASK))
  221. return;
  222. /* depending on when we hit vblank, we may be close to active; if so,
  223. * wait for another frame.
  224. */
  225. while (dce_v11_0_is_in_vblank(adev, crtc)) {
  226. if (i++ == 100) {
  227. i = 0;
  228. if (!dce_v11_0_is_counter_moving(adev, crtc))
  229. break;
  230. }
  231. }
  232. while (!dce_v11_0_is_in_vblank(adev, crtc)) {
  233. if (i++ == 100) {
  234. i = 0;
  235. if (!dce_v11_0_is_counter_moving(adev, crtc))
  236. break;
  237. }
  238. }
  239. }
  240. static u32 dce_v11_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
  241. {
  242. if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
  243. return 0;
  244. else
  245. return RREG32(mmCRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
  246. }
  247. static void dce_v11_0_pageflip_interrupt_init(struct amdgpu_device *adev)
  248. {
  249. unsigned i;
  250. /* Enable pflip interrupts */
  251. for (i = 0; i < adev->mode_info.num_crtc; i++)
  252. amdgpu_irq_get(adev, &adev->pageflip_irq, i);
  253. }
  254. static void dce_v11_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
  255. {
  256. unsigned i;
  257. /* Disable pflip interrupts */
  258. for (i = 0; i < adev->mode_info.num_crtc; i++)
  259. amdgpu_irq_put(adev, &adev->pageflip_irq, i);
  260. }
  261. /**
  262. * dce_v11_0_page_flip - pageflip callback.
  263. *
  264. * @adev: amdgpu_device pointer
  265. * @crtc_id: crtc to cleanup pageflip on
  266. * @crtc_base: new address of the crtc (GPU MC address)
  267. *
  268. * Triggers the actual pageflip by updating the primary
  269. * surface base address.
  270. */
  271. static void dce_v11_0_page_flip(struct amdgpu_device *adev,
  272. int crtc_id, u64 crtc_base, bool async)
  273. {
  274. struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  275. u32 tmp;
  276. /* flip at hsync for async, default is vsync */
  277. /* use UPDATE_IMMEDIATE_EN instead for async? */
  278. tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
  279. tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
  280. GRPH_SURFACE_UPDATE_H_RETRACE_EN, async ? 1 : 0);
  281. WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  282. /* update the scanout addresses */
  283. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  284. upper_32_bits(crtc_base));
  285. /* writing to the low address triggers the update */
  286. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  287. lower_32_bits(crtc_base));
  288. /* post the write */
  289. RREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
  290. }
  291. static int dce_v11_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
  292. u32 *vbl, u32 *position)
  293. {
  294. if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
  295. return -EINVAL;
  296. *vbl = RREG32(mmCRTC_V_BLANK_START_END + crtc_offsets[crtc]);
  297. *position = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  298. return 0;
  299. }
  300. /**
  301. * dce_v11_0_hpd_sense - hpd sense callback.
  302. *
  303. * @adev: amdgpu_device pointer
  304. * @hpd: hpd (hotplug detect) pin
  305. *
  306. * Checks if a digital monitor is connected (evergreen+).
  307. * Returns true if connected, false if not connected.
  308. */
  309. static bool dce_v11_0_hpd_sense(struct amdgpu_device *adev,
  310. enum amdgpu_hpd_id hpd)
  311. {
  312. int idx;
  313. bool connected = false;
  314. switch (hpd) {
  315. case AMDGPU_HPD_1:
  316. idx = 0;
  317. break;
  318. case AMDGPU_HPD_2:
  319. idx = 1;
  320. break;
  321. case AMDGPU_HPD_3:
  322. idx = 2;
  323. break;
  324. case AMDGPU_HPD_4:
  325. idx = 3;
  326. break;
  327. case AMDGPU_HPD_5:
  328. idx = 4;
  329. break;
  330. case AMDGPU_HPD_6:
  331. idx = 5;
  332. break;
  333. default:
  334. return connected;
  335. }
  336. if (RREG32(mmDC_HPD_INT_STATUS + hpd_offsets[idx]) &
  337. DC_HPD_INT_STATUS__DC_HPD_SENSE_MASK)
  338. connected = true;
  339. return connected;
  340. }
  341. /**
  342. * dce_v11_0_hpd_set_polarity - hpd set polarity callback.
  343. *
  344. * @adev: amdgpu_device pointer
  345. * @hpd: hpd (hotplug detect) pin
  346. *
  347. * Set the polarity of the hpd pin (evergreen+).
  348. */
  349. static void dce_v11_0_hpd_set_polarity(struct amdgpu_device *adev,
  350. enum amdgpu_hpd_id hpd)
  351. {
  352. u32 tmp;
  353. bool connected = dce_v11_0_hpd_sense(adev, hpd);
  354. int idx;
  355. switch (hpd) {
  356. case AMDGPU_HPD_1:
  357. idx = 0;
  358. break;
  359. case AMDGPU_HPD_2:
  360. idx = 1;
  361. break;
  362. case AMDGPU_HPD_3:
  363. idx = 2;
  364. break;
  365. case AMDGPU_HPD_4:
  366. idx = 3;
  367. break;
  368. case AMDGPU_HPD_5:
  369. idx = 4;
  370. break;
  371. case AMDGPU_HPD_6:
  372. idx = 5;
  373. break;
  374. default:
  375. return;
  376. }
  377. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[idx]);
  378. if (connected)
  379. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 0);
  380. else
  381. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 1);
  382. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[idx], tmp);
  383. }
  384. /**
  385. * dce_v11_0_hpd_init - hpd setup callback.
  386. *
  387. * @adev: amdgpu_device pointer
  388. *
  389. * Setup the hpd pins used by the card (evergreen+).
  390. * Enable the pin, set the polarity, and enable the hpd interrupts.
  391. */
  392. static void dce_v11_0_hpd_init(struct amdgpu_device *adev)
  393. {
  394. struct drm_device *dev = adev->ddev;
  395. struct drm_connector *connector;
  396. u32 tmp;
  397. int idx;
  398. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  399. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  400. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
  401. connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
  402. /* don't try to enable hpd on eDP or LVDS avoid breaking the
  403. * aux dp channel on imac and help (but not completely fix)
  404. * https://bugzilla.redhat.com/show_bug.cgi?id=726143
  405. * also avoid interrupt storms during dpms.
  406. */
  407. continue;
  408. }
  409. switch (amdgpu_connector->hpd.hpd) {
  410. case AMDGPU_HPD_1:
  411. idx = 0;
  412. break;
  413. case AMDGPU_HPD_2:
  414. idx = 1;
  415. break;
  416. case AMDGPU_HPD_3:
  417. idx = 2;
  418. break;
  419. case AMDGPU_HPD_4:
  420. idx = 3;
  421. break;
  422. case AMDGPU_HPD_5:
  423. idx = 4;
  424. break;
  425. case AMDGPU_HPD_6:
  426. idx = 5;
  427. break;
  428. default:
  429. continue;
  430. }
  431. tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[idx]);
  432. tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 1);
  433. WREG32(mmDC_HPD_CONTROL + hpd_offsets[idx], tmp);
  434. tmp = RREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[idx]);
  435. tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
  436. DC_HPD_CONNECT_INT_DELAY,
  437. AMDGPU_HPD_CONNECT_INT_DELAY_IN_MS);
  438. tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
  439. DC_HPD_DISCONNECT_INT_DELAY,
  440. AMDGPU_HPD_DISCONNECT_INT_DELAY_IN_MS);
  441. WREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[idx], tmp);
  442. dce_v11_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
  443. amdgpu_irq_get(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
  444. }
  445. }
  446. /**
  447. * dce_v11_0_hpd_fini - hpd tear down callback.
  448. *
  449. * @adev: amdgpu_device pointer
  450. *
  451. * Tear down the hpd pins used by the card (evergreen+).
  452. * Disable the hpd interrupts.
  453. */
  454. static void dce_v11_0_hpd_fini(struct amdgpu_device *adev)
  455. {
  456. struct drm_device *dev = adev->ddev;
  457. struct drm_connector *connector;
  458. u32 tmp;
  459. int idx;
  460. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  461. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  462. switch (amdgpu_connector->hpd.hpd) {
  463. case AMDGPU_HPD_1:
  464. idx = 0;
  465. break;
  466. case AMDGPU_HPD_2:
  467. idx = 1;
  468. break;
  469. case AMDGPU_HPD_3:
  470. idx = 2;
  471. break;
  472. case AMDGPU_HPD_4:
  473. idx = 3;
  474. break;
  475. case AMDGPU_HPD_5:
  476. idx = 4;
  477. break;
  478. case AMDGPU_HPD_6:
  479. idx = 5;
  480. break;
  481. default:
  482. continue;
  483. }
  484. tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[idx]);
  485. tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 0);
  486. WREG32(mmDC_HPD_CONTROL + hpd_offsets[idx], tmp);
  487. amdgpu_irq_put(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
  488. }
  489. }
  490. static u32 dce_v11_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
  491. {
  492. return mmDC_GPIO_HPD_A;
  493. }
  494. static bool dce_v11_0_is_display_hung(struct amdgpu_device *adev)
  495. {
  496. u32 crtc_hung = 0;
  497. u32 crtc_status[6];
  498. u32 i, j, tmp;
  499. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  500. tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
  501. if (REG_GET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN)) {
  502. crtc_status[i] = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
  503. crtc_hung |= (1 << i);
  504. }
  505. }
  506. for (j = 0; j < 10; j++) {
  507. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  508. if (crtc_hung & (1 << i)) {
  509. tmp = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
  510. if (tmp != crtc_status[i])
  511. crtc_hung &= ~(1 << i);
  512. }
  513. }
  514. if (crtc_hung == 0)
  515. return false;
  516. udelay(100);
  517. }
  518. return true;
  519. }
  520. static void dce_v11_0_stop_mc_access(struct amdgpu_device *adev,
  521. struct amdgpu_mode_mc_save *save)
  522. {
  523. u32 crtc_enabled, tmp;
  524. int i;
  525. save->vga_render_control = RREG32(mmVGA_RENDER_CONTROL);
  526. save->vga_hdp_control = RREG32(mmVGA_HDP_CONTROL);
  527. /* disable VGA render */
  528. tmp = RREG32(mmVGA_RENDER_CONTROL);
  529. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
  530. WREG32(mmVGA_RENDER_CONTROL, tmp);
  531. /* blank the display controllers */
  532. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  533. crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
  534. CRTC_CONTROL, CRTC_MASTER_EN);
  535. if (crtc_enabled) {
  536. #if 1
  537. save->crtc_enabled[i] = true;
  538. tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
  539. if (REG_GET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN) == 0) {
  540. /*it is correct only for RGB ; black is 0*/
  541. WREG32(mmCRTC_BLANK_DATA_COLOR + crtc_offsets[i], 0);
  542. tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 1);
  543. WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
  544. }
  545. #else
  546. /* XXX this is a hack to avoid strange behavior with EFI on certain systems */
  547. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
  548. tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
  549. tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
  550. WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
  551. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
  552. save->crtc_enabled[i] = false;
  553. /* ***** */
  554. #endif
  555. } else {
  556. save->crtc_enabled[i] = false;
  557. }
  558. }
  559. }
  560. static void dce_v11_0_resume_mc_access(struct amdgpu_device *adev,
  561. struct amdgpu_mode_mc_save *save)
  562. {
  563. u32 tmp;
  564. int i;
  565. /* update crtc base addresses */
  566. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  567. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + crtc_offsets[i],
  568. upper_32_bits(adev->mc.vram_start));
  569. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + crtc_offsets[i],
  570. (u32)adev->mc.vram_start);
  571. if (save->crtc_enabled[i]) {
  572. tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
  573. tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 0);
  574. WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
  575. }
  576. }
  577. WREG32(mmVGA_MEMORY_BASE_ADDRESS_HIGH, upper_32_bits(adev->mc.vram_start));
  578. WREG32(mmVGA_MEMORY_BASE_ADDRESS, lower_32_bits(adev->mc.vram_start));
  579. /* Unlock vga access */
  580. WREG32(mmVGA_HDP_CONTROL, save->vga_hdp_control);
  581. mdelay(1);
  582. WREG32(mmVGA_RENDER_CONTROL, save->vga_render_control);
  583. }
  584. static void dce_v11_0_set_vga_render_state(struct amdgpu_device *adev,
  585. bool render)
  586. {
  587. u32 tmp;
  588. /* Lockout access through VGA aperture*/
  589. tmp = RREG32(mmVGA_HDP_CONTROL);
  590. if (render)
  591. tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 0);
  592. else
  593. tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
  594. WREG32(mmVGA_HDP_CONTROL, tmp);
  595. /* disable VGA render */
  596. tmp = RREG32(mmVGA_RENDER_CONTROL);
  597. if (render)
  598. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 1);
  599. else
  600. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
  601. WREG32(mmVGA_RENDER_CONTROL, tmp);
  602. }
  603. static void dce_v11_0_program_fmt(struct drm_encoder *encoder)
  604. {
  605. struct drm_device *dev = encoder->dev;
  606. struct amdgpu_device *adev = dev->dev_private;
  607. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  608. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  609. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  610. int bpc = 0;
  611. u32 tmp = 0;
  612. enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
  613. if (connector) {
  614. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  615. bpc = amdgpu_connector_get_monitor_bpc(connector);
  616. dither = amdgpu_connector->dither;
  617. }
  618. /* LVDS/eDP FMT is set up by atom */
  619. if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
  620. return;
  621. /* not needed for analog */
  622. if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1) ||
  623. (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2))
  624. return;
  625. if (bpc == 0)
  626. return;
  627. switch (bpc) {
  628. case 6:
  629. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  630. /* XXX sort out optimal dither settings */
  631. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  632. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  633. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  634. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 0);
  635. } else {
  636. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  637. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 0);
  638. }
  639. break;
  640. case 8:
  641. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  642. /* XXX sort out optimal dither settings */
  643. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  644. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  645. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
  646. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  647. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 1);
  648. } else {
  649. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  650. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 1);
  651. }
  652. break;
  653. case 10:
  654. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  655. /* XXX sort out optimal dither settings */
  656. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  657. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  658. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
  659. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  660. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 2);
  661. } else {
  662. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  663. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 2);
  664. }
  665. break;
  666. default:
  667. /* not needed */
  668. break;
  669. }
  670. WREG32(mmFMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  671. }
  672. /* display watermark setup */
  673. /**
  674. * dce_v11_0_line_buffer_adjust - Set up the line buffer
  675. *
  676. * @adev: amdgpu_device pointer
  677. * @amdgpu_crtc: the selected display controller
  678. * @mode: the current display mode on the selected display
  679. * controller
  680. *
  681. * Setup up the line buffer allocation for
  682. * the selected display controller (CIK).
  683. * Returns the line buffer size in pixels.
  684. */
  685. static u32 dce_v11_0_line_buffer_adjust(struct amdgpu_device *adev,
  686. struct amdgpu_crtc *amdgpu_crtc,
  687. struct drm_display_mode *mode)
  688. {
  689. u32 tmp, buffer_alloc, i, mem_cfg;
  690. u32 pipe_offset = amdgpu_crtc->crtc_id;
  691. /*
  692. * Line Buffer Setup
  693. * There are 6 line buffers, one for each display controllers.
  694. * There are 3 partitions per LB. Select the number of partitions
  695. * to enable based on the display width. For display widths larger
  696. * than 4096, you need use to use 2 display controllers and combine
  697. * them using the stereo blender.
  698. */
  699. if (amdgpu_crtc->base.enabled && mode) {
  700. if (mode->crtc_hdisplay < 1920) {
  701. mem_cfg = 1;
  702. buffer_alloc = 2;
  703. } else if (mode->crtc_hdisplay < 2560) {
  704. mem_cfg = 2;
  705. buffer_alloc = 2;
  706. } else if (mode->crtc_hdisplay < 4096) {
  707. mem_cfg = 0;
  708. buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
  709. } else {
  710. DRM_DEBUG_KMS("Mode too big for LB!\n");
  711. mem_cfg = 0;
  712. buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
  713. }
  714. } else {
  715. mem_cfg = 1;
  716. buffer_alloc = 0;
  717. }
  718. tmp = RREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset);
  719. tmp = REG_SET_FIELD(tmp, LB_MEMORY_CTRL, LB_MEMORY_CONFIG, mem_cfg);
  720. WREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset, tmp);
  721. tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
  722. tmp = REG_SET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATED, buffer_alloc);
  723. WREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset, tmp);
  724. for (i = 0; i < adev->usec_timeout; i++) {
  725. tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
  726. if (REG_GET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATION_COMPLETED))
  727. break;
  728. udelay(1);
  729. }
  730. if (amdgpu_crtc->base.enabled && mode) {
  731. switch (mem_cfg) {
  732. case 0:
  733. default:
  734. return 4096 * 2;
  735. case 1:
  736. return 1920 * 2;
  737. case 2:
  738. return 2560 * 2;
  739. }
  740. }
  741. /* controller not enabled, so no lb used */
  742. return 0;
  743. }
  744. /**
  745. * cik_get_number_of_dram_channels - get the number of dram channels
  746. *
  747. * @adev: amdgpu_device pointer
  748. *
  749. * Look up the number of video ram channels (CIK).
  750. * Used for display watermark bandwidth calculations
  751. * Returns the number of dram channels
  752. */
  753. static u32 cik_get_number_of_dram_channels(struct amdgpu_device *adev)
  754. {
  755. u32 tmp = RREG32(mmMC_SHARED_CHMAP);
  756. switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
  757. case 0:
  758. default:
  759. return 1;
  760. case 1:
  761. return 2;
  762. case 2:
  763. return 4;
  764. case 3:
  765. return 8;
  766. case 4:
  767. return 3;
  768. case 5:
  769. return 6;
  770. case 6:
  771. return 10;
  772. case 7:
  773. return 12;
  774. case 8:
  775. return 16;
  776. }
  777. }
  778. struct dce10_wm_params {
  779. u32 dram_channels; /* number of dram channels */
  780. u32 yclk; /* bandwidth per dram data pin in kHz */
  781. u32 sclk; /* engine clock in kHz */
  782. u32 disp_clk; /* display clock in kHz */
  783. u32 src_width; /* viewport width */
  784. u32 active_time; /* active display time in ns */
  785. u32 blank_time; /* blank time in ns */
  786. bool interlaced; /* mode is interlaced */
  787. fixed20_12 vsc; /* vertical scale ratio */
  788. u32 num_heads; /* number of active crtcs */
  789. u32 bytes_per_pixel; /* bytes per pixel display + overlay */
  790. u32 lb_size; /* line buffer allocated to pipe */
  791. u32 vtaps; /* vertical scaler taps */
  792. };
  793. /**
  794. * dce_v11_0_dram_bandwidth - get the dram bandwidth
  795. *
  796. * @wm: watermark calculation data
  797. *
  798. * Calculate the raw dram bandwidth (CIK).
  799. * Used for display watermark bandwidth calculations
  800. * Returns the dram bandwidth in MBytes/s
  801. */
  802. static u32 dce_v11_0_dram_bandwidth(struct dce10_wm_params *wm)
  803. {
  804. /* Calculate raw DRAM Bandwidth */
  805. fixed20_12 dram_efficiency; /* 0.7 */
  806. fixed20_12 yclk, dram_channels, bandwidth;
  807. fixed20_12 a;
  808. a.full = dfixed_const(1000);
  809. yclk.full = dfixed_const(wm->yclk);
  810. yclk.full = dfixed_div(yclk, a);
  811. dram_channels.full = dfixed_const(wm->dram_channels * 4);
  812. a.full = dfixed_const(10);
  813. dram_efficiency.full = dfixed_const(7);
  814. dram_efficiency.full = dfixed_div(dram_efficiency, a);
  815. bandwidth.full = dfixed_mul(dram_channels, yclk);
  816. bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
  817. return dfixed_trunc(bandwidth);
  818. }
  819. /**
  820. * dce_v11_0_dram_bandwidth_for_display - get the dram bandwidth for display
  821. *
  822. * @wm: watermark calculation data
  823. *
  824. * Calculate the dram bandwidth used for display (CIK).
  825. * Used for display watermark bandwidth calculations
  826. * Returns the dram bandwidth for display in MBytes/s
  827. */
  828. static u32 dce_v11_0_dram_bandwidth_for_display(struct dce10_wm_params *wm)
  829. {
  830. /* Calculate DRAM Bandwidth and the part allocated to display. */
  831. fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
  832. fixed20_12 yclk, dram_channels, bandwidth;
  833. fixed20_12 a;
  834. a.full = dfixed_const(1000);
  835. yclk.full = dfixed_const(wm->yclk);
  836. yclk.full = dfixed_div(yclk, a);
  837. dram_channels.full = dfixed_const(wm->dram_channels * 4);
  838. a.full = dfixed_const(10);
  839. disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
  840. disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
  841. bandwidth.full = dfixed_mul(dram_channels, yclk);
  842. bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
  843. return dfixed_trunc(bandwidth);
  844. }
  845. /**
  846. * dce_v11_0_data_return_bandwidth - get the data return bandwidth
  847. *
  848. * @wm: watermark calculation data
  849. *
  850. * Calculate the data return bandwidth used for display (CIK).
  851. * Used for display watermark bandwidth calculations
  852. * Returns the data return bandwidth in MBytes/s
  853. */
  854. static u32 dce_v11_0_data_return_bandwidth(struct dce10_wm_params *wm)
  855. {
  856. /* Calculate the display Data return Bandwidth */
  857. fixed20_12 return_efficiency; /* 0.8 */
  858. fixed20_12 sclk, bandwidth;
  859. fixed20_12 a;
  860. a.full = dfixed_const(1000);
  861. sclk.full = dfixed_const(wm->sclk);
  862. sclk.full = dfixed_div(sclk, a);
  863. a.full = dfixed_const(10);
  864. return_efficiency.full = dfixed_const(8);
  865. return_efficiency.full = dfixed_div(return_efficiency, a);
  866. a.full = dfixed_const(32);
  867. bandwidth.full = dfixed_mul(a, sclk);
  868. bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
  869. return dfixed_trunc(bandwidth);
  870. }
  871. /**
  872. * dce_v11_0_dmif_request_bandwidth - get the dmif bandwidth
  873. *
  874. * @wm: watermark calculation data
  875. *
  876. * Calculate the dmif bandwidth used for display (CIK).
  877. * Used for display watermark bandwidth calculations
  878. * Returns the dmif bandwidth in MBytes/s
  879. */
  880. static u32 dce_v11_0_dmif_request_bandwidth(struct dce10_wm_params *wm)
  881. {
  882. /* Calculate the DMIF Request Bandwidth */
  883. fixed20_12 disp_clk_request_efficiency; /* 0.8 */
  884. fixed20_12 disp_clk, bandwidth;
  885. fixed20_12 a, b;
  886. a.full = dfixed_const(1000);
  887. disp_clk.full = dfixed_const(wm->disp_clk);
  888. disp_clk.full = dfixed_div(disp_clk, a);
  889. a.full = dfixed_const(32);
  890. b.full = dfixed_mul(a, disp_clk);
  891. a.full = dfixed_const(10);
  892. disp_clk_request_efficiency.full = dfixed_const(8);
  893. disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
  894. bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
  895. return dfixed_trunc(bandwidth);
  896. }
  897. /**
  898. * dce_v11_0_available_bandwidth - get the min available bandwidth
  899. *
  900. * @wm: watermark calculation data
  901. *
  902. * Calculate the min available bandwidth used for display (CIK).
  903. * Used for display watermark bandwidth calculations
  904. * Returns the min available bandwidth in MBytes/s
  905. */
  906. static u32 dce_v11_0_available_bandwidth(struct dce10_wm_params *wm)
  907. {
  908. /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
  909. u32 dram_bandwidth = dce_v11_0_dram_bandwidth(wm);
  910. u32 data_return_bandwidth = dce_v11_0_data_return_bandwidth(wm);
  911. u32 dmif_req_bandwidth = dce_v11_0_dmif_request_bandwidth(wm);
  912. return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
  913. }
  914. /**
  915. * dce_v11_0_average_bandwidth - get the average available bandwidth
  916. *
  917. * @wm: watermark calculation data
  918. *
  919. * Calculate the average available bandwidth used for display (CIK).
  920. * Used for display watermark bandwidth calculations
  921. * Returns the average available bandwidth in MBytes/s
  922. */
  923. static u32 dce_v11_0_average_bandwidth(struct dce10_wm_params *wm)
  924. {
  925. /* Calculate the display mode Average Bandwidth
  926. * DisplayMode should contain the source and destination dimensions,
  927. * timing, etc.
  928. */
  929. fixed20_12 bpp;
  930. fixed20_12 line_time;
  931. fixed20_12 src_width;
  932. fixed20_12 bandwidth;
  933. fixed20_12 a;
  934. a.full = dfixed_const(1000);
  935. line_time.full = dfixed_const(wm->active_time + wm->blank_time);
  936. line_time.full = dfixed_div(line_time, a);
  937. bpp.full = dfixed_const(wm->bytes_per_pixel);
  938. src_width.full = dfixed_const(wm->src_width);
  939. bandwidth.full = dfixed_mul(src_width, bpp);
  940. bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
  941. bandwidth.full = dfixed_div(bandwidth, line_time);
  942. return dfixed_trunc(bandwidth);
  943. }
  944. /**
  945. * dce_v11_0_latency_watermark - get the latency watermark
  946. *
  947. * @wm: watermark calculation data
  948. *
  949. * Calculate the latency watermark (CIK).
  950. * Used for display watermark bandwidth calculations
  951. * Returns the latency watermark in ns
  952. */
  953. static u32 dce_v11_0_latency_watermark(struct dce10_wm_params *wm)
  954. {
  955. /* First calculate the latency in ns */
  956. u32 mc_latency = 2000; /* 2000 ns. */
  957. u32 available_bandwidth = dce_v11_0_available_bandwidth(wm);
  958. u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
  959. u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
  960. u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
  961. u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
  962. (wm->num_heads * cursor_line_pair_return_time);
  963. u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
  964. u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
  965. u32 tmp, dmif_size = 12288;
  966. fixed20_12 a, b, c;
  967. if (wm->num_heads == 0)
  968. return 0;
  969. a.full = dfixed_const(2);
  970. b.full = dfixed_const(1);
  971. if ((wm->vsc.full > a.full) ||
  972. ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
  973. (wm->vtaps >= 5) ||
  974. ((wm->vsc.full >= a.full) && wm->interlaced))
  975. max_src_lines_per_dst_line = 4;
  976. else
  977. max_src_lines_per_dst_line = 2;
  978. a.full = dfixed_const(available_bandwidth);
  979. b.full = dfixed_const(wm->num_heads);
  980. a.full = dfixed_div(a, b);
  981. b.full = dfixed_const(mc_latency + 512);
  982. c.full = dfixed_const(wm->disp_clk);
  983. b.full = dfixed_div(b, c);
  984. c.full = dfixed_const(dmif_size);
  985. b.full = dfixed_div(c, b);
  986. tmp = min(dfixed_trunc(a), dfixed_trunc(b));
  987. b.full = dfixed_const(1000);
  988. c.full = dfixed_const(wm->disp_clk);
  989. b.full = dfixed_div(c, b);
  990. c.full = dfixed_const(wm->bytes_per_pixel);
  991. b.full = dfixed_mul(b, c);
  992. lb_fill_bw = min(tmp, dfixed_trunc(b));
  993. a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
  994. b.full = dfixed_const(1000);
  995. c.full = dfixed_const(lb_fill_bw);
  996. b.full = dfixed_div(c, b);
  997. a.full = dfixed_div(a, b);
  998. line_fill_time = dfixed_trunc(a);
  999. if (line_fill_time < wm->active_time)
  1000. return latency;
  1001. else
  1002. return latency + (line_fill_time - wm->active_time);
  1003. }
  1004. /**
  1005. * dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display - check
  1006. * average and available dram bandwidth
  1007. *
  1008. * @wm: watermark calculation data
  1009. *
  1010. * Check if the display average bandwidth fits in the display
  1011. * dram bandwidth (CIK).
  1012. * Used for display watermark bandwidth calculations
  1013. * Returns true if the display fits, false if not.
  1014. */
  1015. static bool dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params *wm)
  1016. {
  1017. if (dce_v11_0_average_bandwidth(wm) <=
  1018. (dce_v11_0_dram_bandwidth_for_display(wm) / wm->num_heads))
  1019. return true;
  1020. else
  1021. return false;
  1022. }
  1023. /**
  1024. * dce_v11_0_average_bandwidth_vs_available_bandwidth - check
  1025. * average and available bandwidth
  1026. *
  1027. * @wm: watermark calculation data
  1028. *
  1029. * Check if the display average bandwidth fits in the display
  1030. * available bandwidth (CIK).
  1031. * Used for display watermark bandwidth calculations
  1032. * Returns true if the display fits, false if not.
  1033. */
  1034. static bool dce_v11_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params *wm)
  1035. {
  1036. if (dce_v11_0_average_bandwidth(wm) <=
  1037. (dce_v11_0_available_bandwidth(wm) / wm->num_heads))
  1038. return true;
  1039. else
  1040. return false;
  1041. }
  1042. /**
  1043. * dce_v11_0_check_latency_hiding - check latency hiding
  1044. *
  1045. * @wm: watermark calculation data
  1046. *
  1047. * Check latency hiding (CIK).
  1048. * Used for display watermark bandwidth calculations
  1049. * Returns true if the display fits, false if not.
  1050. */
  1051. static bool dce_v11_0_check_latency_hiding(struct dce10_wm_params *wm)
  1052. {
  1053. u32 lb_partitions = wm->lb_size / wm->src_width;
  1054. u32 line_time = wm->active_time + wm->blank_time;
  1055. u32 latency_tolerant_lines;
  1056. u32 latency_hiding;
  1057. fixed20_12 a;
  1058. a.full = dfixed_const(1);
  1059. if (wm->vsc.full > a.full)
  1060. latency_tolerant_lines = 1;
  1061. else {
  1062. if (lb_partitions <= (wm->vtaps + 1))
  1063. latency_tolerant_lines = 1;
  1064. else
  1065. latency_tolerant_lines = 2;
  1066. }
  1067. latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
  1068. if (dce_v11_0_latency_watermark(wm) <= latency_hiding)
  1069. return true;
  1070. else
  1071. return false;
  1072. }
  1073. /**
  1074. * dce_v11_0_program_watermarks - program display watermarks
  1075. *
  1076. * @adev: amdgpu_device pointer
  1077. * @amdgpu_crtc: the selected display controller
  1078. * @lb_size: line buffer size
  1079. * @num_heads: number of display controllers in use
  1080. *
  1081. * Calculate and program the display watermarks for the
  1082. * selected display controller (CIK).
  1083. */
  1084. static void dce_v11_0_program_watermarks(struct amdgpu_device *adev,
  1085. struct amdgpu_crtc *amdgpu_crtc,
  1086. u32 lb_size, u32 num_heads)
  1087. {
  1088. struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
  1089. struct dce10_wm_params wm_low, wm_high;
  1090. u32 pixel_period;
  1091. u32 line_time = 0;
  1092. u32 latency_watermark_a = 0, latency_watermark_b = 0;
  1093. u32 tmp, wm_mask, lb_vblank_lead_lines = 0;
  1094. if (amdgpu_crtc->base.enabled && num_heads && mode) {
  1095. pixel_period = 1000000 / (u32)mode->clock;
  1096. line_time = min((u32)mode->crtc_htotal * pixel_period, (u32)65535);
  1097. /* watermark for high clocks */
  1098. if (adev->pm.dpm_enabled) {
  1099. wm_high.yclk =
  1100. amdgpu_dpm_get_mclk(adev, false) * 10;
  1101. wm_high.sclk =
  1102. amdgpu_dpm_get_sclk(adev, false) * 10;
  1103. } else {
  1104. wm_high.yclk = adev->pm.current_mclk * 10;
  1105. wm_high.sclk = adev->pm.current_sclk * 10;
  1106. }
  1107. wm_high.disp_clk = mode->clock;
  1108. wm_high.src_width = mode->crtc_hdisplay;
  1109. wm_high.active_time = mode->crtc_hdisplay * pixel_period;
  1110. wm_high.blank_time = line_time - wm_high.active_time;
  1111. wm_high.interlaced = false;
  1112. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1113. wm_high.interlaced = true;
  1114. wm_high.vsc = amdgpu_crtc->vsc;
  1115. wm_high.vtaps = 1;
  1116. if (amdgpu_crtc->rmx_type != RMX_OFF)
  1117. wm_high.vtaps = 2;
  1118. wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
  1119. wm_high.lb_size = lb_size;
  1120. wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
  1121. wm_high.num_heads = num_heads;
  1122. /* set for high clocks */
  1123. latency_watermark_a = min(dce_v11_0_latency_watermark(&wm_high), (u32)65535);
  1124. /* possibly force display priority to high */
  1125. /* should really do this at mode validation time... */
  1126. if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
  1127. !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
  1128. !dce_v11_0_check_latency_hiding(&wm_high) ||
  1129. (adev->mode_info.disp_priority == 2)) {
  1130. DRM_DEBUG_KMS("force priority to high\n");
  1131. }
  1132. /* watermark for low clocks */
  1133. if (adev->pm.dpm_enabled) {
  1134. wm_low.yclk =
  1135. amdgpu_dpm_get_mclk(adev, true) * 10;
  1136. wm_low.sclk =
  1137. amdgpu_dpm_get_sclk(adev, true) * 10;
  1138. } else {
  1139. wm_low.yclk = adev->pm.current_mclk * 10;
  1140. wm_low.sclk = adev->pm.current_sclk * 10;
  1141. }
  1142. wm_low.disp_clk = mode->clock;
  1143. wm_low.src_width = mode->crtc_hdisplay;
  1144. wm_low.active_time = mode->crtc_hdisplay * pixel_period;
  1145. wm_low.blank_time = line_time - wm_low.active_time;
  1146. wm_low.interlaced = false;
  1147. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1148. wm_low.interlaced = true;
  1149. wm_low.vsc = amdgpu_crtc->vsc;
  1150. wm_low.vtaps = 1;
  1151. if (amdgpu_crtc->rmx_type != RMX_OFF)
  1152. wm_low.vtaps = 2;
  1153. wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
  1154. wm_low.lb_size = lb_size;
  1155. wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
  1156. wm_low.num_heads = num_heads;
  1157. /* set for low clocks */
  1158. latency_watermark_b = min(dce_v11_0_latency_watermark(&wm_low), (u32)65535);
  1159. /* possibly force display priority to high */
  1160. /* should really do this at mode validation time... */
  1161. if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
  1162. !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
  1163. !dce_v11_0_check_latency_hiding(&wm_low) ||
  1164. (adev->mode_info.disp_priority == 2)) {
  1165. DRM_DEBUG_KMS("force priority to high\n");
  1166. }
  1167. lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
  1168. }
  1169. /* select wm A */
  1170. wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
  1171. tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 1);
  1172. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1173. tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
  1174. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_a);
  1175. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
  1176. WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1177. /* select wm B */
  1178. tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 2);
  1179. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1180. tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
  1181. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_b);
  1182. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
  1183. WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1184. /* restore original selection */
  1185. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
  1186. /* save values for DPM */
  1187. amdgpu_crtc->line_time = line_time;
  1188. amdgpu_crtc->wm_high = latency_watermark_a;
  1189. amdgpu_crtc->wm_low = latency_watermark_b;
  1190. /* Save number of lines the linebuffer leads before the scanout */
  1191. amdgpu_crtc->lb_vblank_lead_lines = lb_vblank_lead_lines;
  1192. }
  1193. /**
  1194. * dce_v11_0_bandwidth_update - program display watermarks
  1195. *
  1196. * @adev: amdgpu_device pointer
  1197. *
  1198. * Calculate and program the display watermarks and line
  1199. * buffer allocation (CIK).
  1200. */
  1201. static void dce_v11_0_bandwidth_update(struct amdgpu_device *adev)
  1202. {
  1203. struct drm_display_mode *mode = NULL;
  1204. u32 num_heads = 0, lb_size;
  1205. int i;
  1206. amdgpu_update_display_priority(adev);
  1207. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1208. if (adev->mode_info.crtcs[i]->base.enabled)
  1209. num_heads++;
  1210. }
  1211. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1212. mode = &adev->mode_info.crtcs[i]->base.mode;
  1213. lb_size = dce_v11_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
  1214. dce_v11_0_program_watermarks(adev, adev->mode_info.crtcs[i],
  1215. lb_size, num_heads);
  1216. }
  1217. }
  1218. static void dce_v11_0_audio_get_connected_pins(struct amdgpu_device *adev)
  1219. {
  1220. int i;
  1221. u32 offset, tmp;
  1222. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1223. offset = adev->mode_info.audio.pin[i].offset;
  1224. tmp = RREG32_AUDIO_ENDPT(offset,
  1225. ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
  1226. if (((tmp &
  1227. AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
  1228. AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
  1229. adev->mode_info.audio.pin[i].connected = false;
  1230. else
  1231. adev->mode_info.audio.pin[i].connected = true;
  1232. }
  1233. }
  1234. static struct amdgpu_audio_pin *dce_v11_0_audio_get_pin(struct amdgpu_device *adev)
  1235. {
  1236. int i;
  1237. dce_v11_0_audio_get_connected_pins(adev);
  1238. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1239. if (adev->mode_info.audio.pin[i].connected)
  1240. return &adev->mode_info.audio.pin[i];
  1241. }
  1242. DRM_ERROR("No connected audio pins found!\n");
  1243. return NULL;
  1244. }
  1245. static void dce_v11_0_afmt_audio_select_pin(struct drm_encoder *encoder)
  1246. {
  1247. struct amdgpu_device *adev = encoder->dev->dev_private;
  1248. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1249. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1250. u32 tmp;
  1251. if (!dig || !dig->afmt || !dig->afmt->pin)
  1252. return;
  1253. tmp = RREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset);
  1254. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_SRC_CONTROL, AFMT_AUDIO_SRC_SELECT, dig->afmt->pin->id);
  1255. WREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, tmp);
  1256. }
  1257. static void dce_v11_0_audio_write_latency_fields(struct drm_encoder *encoder,
  1258. struct drm_display_mode *mode)
  1259. {
  1260. struct amdgpu_device *adev = encoder->dev->dev_private;
  1261. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1262. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1263. struct drm_connector *connector;
  1264. struct amdgpu_connector *amdgpu_connector = NULL;
  1265. u32 tmp;
  1266. int interlace = 0;
  1267. if (!dig || !dig->afmt || !dig->afmt->pin)
  1268. return;
  1269. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1270. if (connector->encoder == encoder) {
  1271. amdgpu_connector = to_amdgpu_connector(connector);
  1272. break;
  1273. }
  1274. }
  1275. if (!amdgpu_connector) {
  1276. DRM_ERROR("Couldn't find encoder's connector\n");
  1277. return;
  1278. }
  1279. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1280. interlace = 1;
  1281. if (connector->latency_present[interlace]) {
  1282. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1283. VIDEO_LIPSYNC, connector->video_latency[interlace]);
  1284. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1285. AUDIO_LIPSYNC, connector->audio_latency[interlace]);
  1286. } else {
  1287. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1288. VIDEO_LIPSYNC, 0);
  1289. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1290. AUDIO_LIPSYNC, 0);
  1291. }
  1292. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1293. ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
  1294. }
  1295. static void dce_v11_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
  1296. {
  1297. struct amdgpu_device *adev = encoder->dev->dev_private;
  1298. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1299. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1300. struct drm_connector *connector;
  1301. struct amdgpu_connector *amdgpu_connector = NULL;
  1302. u32 tmp;
  1303. u8 *sadb = NULL;
  1304. int sad_count;
  1305. if (!dig || !dig->afmt || !dig->afmt->pin)
  1306. return;
  1307. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1308. if (connector->encoder == encoder) {
  1309. amdgpu_connector = to_amdgpu_connector(connector);
  1310. break;
  1311. }
  1312. }
  1313. if (!amdgpu_connector) {
  1314. DRM_ERROR("Couldn't find encoder's connector\n");
  1315. return;
  1316. }
  1317. sad_count = drm_edid_to_speaker_allocation(amdgpu_connector_edid(connector), &sadb);
  1318. if (sad_count < 0) {
  1319. DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
  1320. sad_count = 0;
  1321. }
  1322. /* program the speaker allocation */
  1323. tmp = RREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1324. ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
  1325. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1326. DP_CONNECTION, 0);
  1327. /* set HDMI mode */
  1328. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1329. HDMI_CONNECTION, 1);
  1330. if (sad_count)
  1331. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1332. SPEAKER_ALLOCATION, sadb[0]);
  1333. else
  1334. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1335. SPEAKER_ALLOCATION, 5); /* stereo */
  1336. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1337. ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
  1338. kfree(sadb);
  1339. }
  1340. static void dce_v11_0_audio_write_sad_regs(struct drm_encoder *encoder)
  1341. {
  1342. struct amdgpu_device *adev = encoder->dev->dev_private;
  1343. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1344. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1345. struct drm_connector *connector;
  1346. struct amdgpu_connector *amdgpu_connector = NULL;
  1347. struct cea_sad *sads;
  1348. int i, sad_count;
  1349. static const u16 eld_reg_to_type[][2] = {
  1350. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
  1351. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
  1352. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
  1353. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
  1354. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
  1355. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
  1356. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
  1357. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
  1358. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
  1359. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
  1360. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
  1361. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
  1362. };
  1363. if (!dig || !dig->afmt || !dig->afmt->pin)
  1364. return;
  1365. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1366. if (connector->encoder == encoder) {
  1367. amdgpu_connector = to_amdgpu_connector(connector);
  1368. break;
  1369. }
  1370. }
  1371. if (!amdgpu_connector) {
  1372. DRM_ERROR("Couldn't find encoder's connector\n");
  1373. return;
  1374. }
  1375. sad_count = drm_edid_to_sad(amdgpu_connector_edid(connector), &sads);
  1376. if (sad_count <= 0) {
  1377. DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
  1378. return;
  1379. }
  1380. BUG_ON(!sads);
  1381. for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
  1382. u32 tmp = 0;
  1383. u8 stereo_freqs = 0;
  1384. int max_channels = -1;
  1385. int j;
  1386. for (j = 0; j < sad_count; j++) {
  1387. struct cea_sad *sad = &sads[j];
  1388. if (sad->format == eld_reg_to_type[i][1]) {
  1389. if (sad->channels > max_channels) {
  1390. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1391. MAX_CHANNELS, sad->channels);
  1392. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1393. DESCRIPTOR_BYTE_2, sad->byte2);
  1394. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1395. SUPPORTED_FREQUENCIES, sad->freq);
  1396. max_channels = sad->channels;
  1397. }
  1398. if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
  1399. stereo_freqs |= sad->freq;
  1400. else
  1401. break;
  1402. }
  1403. }
  1404. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1405. SUPPORTED_FREQUENCIES_STEREO, stereo_freqs);
  1406. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset, eld_reg_to_type[i][0], tmp);
  1407. }
  1408. kfree(sads);
  1409. }
  1410. static void dce_v11_0_audio_enable(struct amdgpu_device *adev,
  1411. struct amdgpu_audio_pin *pin,
  1412. bool enable)
  1413. {
  1414. if (!pin)
  1415. return;
  1416. WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
  1417. enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
  1418. }
  1419. static const u32 pin_offsets[] =
  1420. {
  1421. AUD0_REGISTER_OFFSET,
  1422. AUD1_REGISTER_OFFSET,
  1423. AUD2_REGISTER_OFFSET,
  1424. AUD3_REGISTER_OFFSET,
  1425. AUD4_REGISTER_OFFSET,
  1426. AUD5_REGISTER_OFFSET,
  1427. AUD6_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(crtc->dev, 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 void dce_v11_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  2347. u16 *blue, uint32_t start, uint32_t size)
  2348. {
  2349. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2350. int end = (start + size > 256) ? 256 : start + size, i;
  2351. /* userspace palettes are always correct as is */
  2352. for (i = start; i < end; 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. }
  2359. static void dce_v11_0_crtc_destroy(struct drm_crtc *crtc)
  2360. {
  2361. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2362. drm_crtc_cleanup(crtc);
  2363. kfree(amdgpu_crtc);
  2364. }
  2365. static const struct drm_crtc_funcs dce_v11_0_crtc_funcs = {
  2366. .cursor_set2 = dce_v11_0_crtc_cursor_set2,
  2367. .cursor_move = dce_v11_0_crtc_cursor_move,
  2368. .gamma_set = dce_v11_0_crtc_gamma_set,
  2369. .set_config = amdgpu_crtc_set_config,
  2370. .destroy = dce_v11_0_crtc_destroy,
  2371. .page_flip = amdgpu_crtc_page_flip,
  2372. };
  2373. static void dce_v11_0_crtc_dpms(struct drm_crtc *crtc, int mode)
  2374. {
  2375. struct drm_device *dev = crtc->dev;
  2376. struct amdgpu_device *adev = dev->dev_private;
  2377. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2378. unsigned type;
  2379. switch (mode) {
  2380. case DRM_MODE_DPMS_ON:
  2381. amdgpu_crtc->enabled = true;
  2382. amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
  2383. dce_v11_0_vga_enable(crtc, true);
  2384. amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
  2385. dce_v11_0_vga_enable(crtc, false);
  2386. /* Make sure VBLANK and PFLIP interrupts are still enabled */
  2387. type = amdgpu_crtc_idx_to_irq_type(adev, amdgpu_crtc->crtc_id);
  2388. amdgpu_irq_update(adev, &adev->crtc_irq, type);
  2389. amdgpu_irq_update(adev, &adev->pageflip_irq, type);
  2390. drm_vblank_on(dev, amdgpu_crtc->crtc_id);
  2391. dce_v11_0_crtc_load_lut(crtc);
  2392. break;
  2393. case DRM_MODE_DPMS_STANDBY:
  2394. case DRM_MODE_DPMS_SUSPEND:
  2395. case DRM_MODE_DPMS_OFF:
  2396. drm_vblank_off(dev, amdgpu_crtc->crtc_id);
  2397. if (amdgpu_crtc->enabled) {
  2398. dce_v11_0_vga_enable(crtc, true);
  2399. amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
  2400. dce_v11_0_vga_enable(crtc, false);
  2401. }
  2402. amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
  2403. amdgpu_crtc->enabled = false;
  2404. break;
  2405. }
  2406. /* adjust pm to dpms */
  2407. amdgpu_pm_compute_clocks(adev);
  2408. }
  2409. static void dce_v11_0_crtc_prepare(struct drm_crtc *crtc)
  2410. {
  2411. /* disable crtc pair power gating before programming */
  2412. amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
  2413. amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
  2414. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  2415. }
  2416. static void dce_v11_0_crtc_commit(struct drm_crtc *crtc)
  2417. {
  2418. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  2419. amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
  2420. }
  2421. static void dce_v11_0_crtc_disable(struct drm_crtc *crtc)
  2422. {
  2423. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2424. struct drm_device *dev = crtc->dev;
  2425. struct amdgpu_device *adev = dev->dev_private;
  2426. struct amdgpu_atom_ss ss;
  2427. int i;
  2428. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  2429. if (crtc->primary->fb) {
  2430. int r;
  2431. struct amdgpu_framebuffer *amdgpu_fb;
  2432. struct amdgpu_bo *rbo;
  2433. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  2434. rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  2435. r = amdgpu_bo_reserve(rbo, false);
  2436. if (unlikely(r))
  2437. DRM_ERROR("failed to reserve rbo before unpin\n");
  2438. else {
  2439. amdgpu_bo_unpin(rbo);
  2440. amdgpu_bo_unreserve(rbo);
  2441. }
  2442. }
  2443. /* disable the GRPH */
  2444. dce_v11_0_grph_enable(crtc, false);
  2445. amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
  2446. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2447. if (adev->mode_info.crtcs[i] &&
  2448. adev->mode_info.crtcs[i]->enabled &&
  2449. i != amdgpu_crtc->crtc_id &&
  2450. amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
  2451. /* one other crtc is using this pll don't turn
  2452. * off the pll
  2453. */
  2454. goto done;
  2455. }
  2456. }
  2457. switch (amdgpu_crtc->pll_id) {
  2458. case ATOM_PPLL0:
  2459. case ATOM_PPLL1:
  2460. case ATOM_PPLL2:
  2461. /* disable the ppll */
  2462. amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
  2463. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  2464. break;
  2465. case ATOM_COMBOPHY_PLL0:
  2466. case ATOM_COMBOPHY_PLL1:
  2467. case ATOM_COMBOPHY_PLL2:
  2468. case ATOM_COMBOPHY_PLL3:
  2469. case ATOM_COMBOPHY_PLL4:
  2470. case ATOM_COMBOPHY_PLL5:
  2471. /* disable the ppll */
  2472. amdgpu_atombios_crtc_program_pll(crtc, ATOM_CRTC_INVALID, amdgpu_crtc->pll_id,
  2473. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  2474. break;
  2475. default:
  2476. break;
  2477. }
  2478. done:
  2479. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  2480. amdgpu_crtc->adjusted_clock = 0;
  2481. amdgpu_crtc->encoder = NULL;
  2482. amdgpu_crtc->connector = NULL;
  2483. }
  2484. static int dce_v11_0_crtc_mode_set(struct drm_crtc *crtc,
  2485. struct drm_display_mode *mode,
  2486. struct drm_display_mode *adjusted_mode,
  2487. int x, int y, struct drm_framebuffer *old_fb)
  2488. {
  2489. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2490. struct drm_device *dev = crtc->dev;
  2491. struct amdgpu_device *adev = dev->dev_private;
  2492. if (!amdgpu_crtc->adjusted_clock)
  2493. return -EINVAL;
  2494. if ((adev->asic_type == CHIP_POLARIS10) ||
  2495. (adev->asic_type == CHIP_POLARIS11)) {
  2496. struct amdgpu_encoder *amdgpu_encoder =
  2497. to_amdgpu_encoder(amdgpu_crtc->encoder);
  2498. int encoder_mode =
  2499. amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder);
  2500. /* SetPixelClock calculates the plls and ss values now */
  2501. amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id,
  2502. amdgpu_crtc->pll_id,
  2503. encoder_mode, amdgpu_encoder->encoder_id,
  2504. adjusted_mode->clock, 0, 0, 0, 0,
  2505. amdgpu_crtc->bpc, amdgpu_crtc->ss_enabled, &amdgpu_crtc->ss);
  2506. } else {
  2507. amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
  2508. }
  2509. amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
  2510. dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  2511. amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
  2512. amdgpu_atombios_crtc_scaler_setup(crtc);
  2513. dce_v11_0_cursor_reset(crtc);
  2514. /* update the hw version fpr dpm */
  2515. amdgpu_crtc->hw_mode = *adjusted_mode;
  2516. return 0;
  2517. }
  2518. static bool dce_v11_0_crtc_mode_fixup(struct drm_crtc *crtc,
  2519. const struct drm_display_mode *mode,
  2520. struct drm_display_mode *adjusted_mode)
  2521. {
  2522. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2523. struct drm_device *dev = crtc->dev;
  2524. struct drm_encoder *encoder;
  2525. /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
  2526. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  2527. if (encoder->crtc == crtc) {
  2528. amdgpu_crtc->encoder = encoder;
  2529. amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
  2530. break;
  2531. }
  2532. }
  2533. if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
  2534. amdgpu_crtc->encoder = NULL;
  2535. amdgpu_crtc->connector = NULL;
  2536. return false;
  2537. }
  2538. if (!amdgpu_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
  2539. return false;
  2540. if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
  2541. return false;
  2542. /* pick pll */
  2543. amdgpu_crtc->pll_id = dce_v11_0_pick_pll(crtc);
  2544. /* if we can't get a PPLL for a non-DP encoder, fail */
  2545. if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
  2546. !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
  2547. return false;
  2548. return true;
  2549. }
  2550. static int dce_v11_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  2551. struct drm_framebuffer *old_fb)
  2552. {
  2553. return dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  2554. }
  2555. static int dce_v11_0_crtc_set_base_atomic(struct drm_crtc *crtc,
  2556. struct drm_framebuffer *fb,
  2557. int x, int y, enum mode_set_atomic state)
  2558. {
  2559. return dce_v11_0_crtc_do_set_base(crtc, fb, x, y, 1);
  2560. }
  2561. static const struct drm_crtc_helper_funcs dce_v11_0_crtc_helper_funcs = {
  2562. .dpms = dce_v11_0_crtc_dpms,
  2563. .mode_fixup = dce_v11_0_crtc_mode_fixup,
  2564. .mode_set = dce_v11_0_crtc_mode_set,
  2565. .mode_set_base = dce_v11_0_crtc_set_base,
  2566. .mode_set_base_atomic = dce_v11_0_crtc_set_base_atomic,
  2567. .prepare = dce_v11_0_crtc_prepare,
  2568. .commit = dce_v11_0_crtc_commit,
  2569. .load_lut = dce_v11_0_crtc_load_lut,
  2570. .disable = dce_v11_0_crtc_disable,
  2571. };
  2572. static int dce_v11_0_crtc_init(struct amdgpu_device *adev, int index)
  2573. {
  2574. struct amdgpu_crtc *amdgpu_crtc;
  2575. int i;
  2576. amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
  2577. (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  2578. if (amdgpu_crtc == NULL)
  2579. return -ENOMEM;
  2580. drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_v11_0_crtc_funcs);
  2581. drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
  2582. amdgpu_crtc->crtc_id = index;
  2583. adev->mode_info.crtcs[index] = amdgpu_crtc;
  2584. amdgpu_crtc->max_cursor_width = 128;
  2585. amdgpu_crtc->max_cursor_height = 128;
  2586. adev->ddev->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
  2587. adev->ddev->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
  2588. for (i = 0; i < 256; i++) {
  2589. amdgpu_crtc->lut_r[i] = i << 2;
  2590. amdgpu_crtc->lut_g[i] = i << 2;
  2591. amdgpu_crtc->lut_b[i] = i << 2;
  2592. }
  2593. switch (amdgpu_crtc->crtc_id) {
  2594. case 0:
  2595. default:
  2596. amdgpu_crtc->crtc_offset = CRTC0_REGISTER_OFFSET;
  2597. break;
  2598. case 1:
  2599. amdgpu_crtc->crtc_offset = CRTC1_REGISTER_OFFSET;
  2600. break;
  2601. case 2:
  2602. amdgpu_crtc->crtc_offset = CRTC2_REGISTER_OFFSET;
  2603. break;
  2604. case 3:
  2605. amdgpu_crtc->crtc_offset = CRTC3_REGISTER_OFFSET;
  2606. break;
  2607. case 4:
  2608. amdgpu_crtc->crtc_offset = CRTC4_REGISTER_OFFSET;
  2609. break;
  2610. case 5:
  2611. amdgpu_crtc->crtc_offset = CRTC5_REGISTER_OFFSET;
  2612. break;
  2613. }
  2614. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  2615. amdgpu_crtc->adjusted_clock = 0;
  2616. amdgpu_crtc->encoder = NULL;
  2617. amdgpu_crtc->connector = NULL;
  2618. drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v11_0_crtc_helper_funcs);
  2619. return 0;
  2620. }
  2621. static int dce_v11_0_early_init(void *handle)
  2622. {
  2623. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2624. adev->audio_endpt_rreg = &dce_v11_0_audio_endpt_rreg;
  2625. adev->audio_endpt_wreg = &dce_v11_0_audio_endpt_wreg;
  2626. dce_v11_0_set_display_funcs(adev);
  2627. dce_v11_0_set_irq_funcs(adev);
  2628. switch (adev->asic_type) {
  2629. case CHIP_CARRIZO:
  2630. adev->mode_info.num_crtc = 3;
  2631. adev->mode_info.num_hpd = 6;
  2632. adev->mode_info.num_dig = 9;
  2633. break;
  2634. case CHIP_STONEY:
  2635. adev->mode_info.num_crtc = 2;
  2636. adev->mode_info.num_hpd = 6;
  2637. adev->mode_info.num_dig = 9;
  2638. break;
  2639. case CHIP_POLARIS10:
  2640. adev->mode_info.num_crtc = 6;
  2641. adev->mode_info.num_hpd = 6;
  2642. adev->mode_info.num_dig = 6;
  2643. break;
  2644. case CHIP_POLARIS11:
  2645. adev->mode_info.num_crtc = 5;
  2646. adev->mode_info.num_hpd = 5;
  2647. adev->mode_info.num_dig = 5;
  2648. break;
  2649. default:
  2650. /* FIXME: not supported yet */
  2651. return -EINVAL;
  2652. }
  2653. return 0;
  2654. }
  2655. static int dce_v11_0_sw_init(void *handle)
  2656. {
  2657. int r, i;
  2658. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2659. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2660. r = amdgpu_irq_add_id(adev, i + 1, &adev->crtc_irq);
  2661. if (r)
  2662. return r;
  2663. }
  2664. for (i = 8; i < 20; i += 2) {
  2665. r = amdgpu_irq_add_id(adev, i, &adev->pageflip_irq);
  2666. if (r)
  2667. return r;
  2668. }
  2669. /* HPD hotplug */
  2670. r = amdgpu_irq_add_id(adev, 42, &adev->hpd_irq);
  2671. if (r)
  2672. return r;
  2673. adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
  2674. adev->ddev->mode_config.async_page_flip = true;
  2675. adev->ddev->mode_config.max_width = 16384;
  2676. adev->ddev->mode_config.max_height = 16384;
  2677. adev->ddev->mode_config.preferred_depth = 24;
  2678. adev->ddev->mode_config.prefer_shadow = 1;
  2679. adev->ddev->mode_config.fb_base = adev->mc.aper_base;
  2680. r = amdgpu_modeset_create_props(adev);
  2681. if (r)
  2682. return r;
  2683. adev->ddev->mode_config.max_width = 16384;
  2684. adev->ddev->mode_config.max_height = 16384;
  2685. /* allocate crtcs */
  2686. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2687. r = dce_v11_0_crtc_init(adev, i);
  2688. if (r)
  2689. return r;
  2690. }
  2691. if (amdgpu_atombios_get_connector_info_from_object_table(adev))
  2692. amdgpu_print_display_setup(adev->ddev);
  2693. else
  2694. return -EINVAL;
  2695. /* setup afmt */
  2696. r = dce_v11_0_afmt_init(adev);
  2697. if (r)
  2698. return r;
  2699. r = dce_v11_0_audio_init(adev);
  2700. if (r)
  2701. return r;
  2702. drm_kms_helper_poll_init(adev->ddev);
  2703. adev->mode_info.mode_config_initialized = true;
  2704. return 0;
  2705. }
  2706. static int dce_v11_0_sw_fini(void *handle)
  2707. {
  2708. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2709. kfree(adev->mode_info.bios_hardcoded_edid);
  2710. drm_kms_helper_poll_fini(adev->ddev);
  2711. dce_v11_0_audio_fini(adev);
  2712. dce_v11_0_afmt_fini(adev);
  2713. adev->mode_info.mode_config_initialized = false;
  2714. return 0;
  2715. }
  2716. static int dce_v11_0_hw_init(void *handle)
  2717. {
  2718. int i;
  2719. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2720. dce_v11_0_init_golden_registers(adev);
  2721. /* init dig PHYs, disp eng pll */
  2722. amdgpu_atombios_crtc_powergate_init(adev);
  2723. amdgpu_atombios_encoder_init_dig(adev);
  2724. if ((adev->asic_type == CHIP_POLARIS10) ||
  2725. (adev->asic_type == CHIP_POLARIS11)) {
  2726. amdgpu_atombios_crtc_set_dce_clock(adev, adev->clock.default_dispclk,
  2727. DCE_CLOCK_TYPE_DISPCLK, ATOM_GCK_DFS);
  2728. amdgpu_atombios_crtc_set_dce_clock(adev, 0,
  2729. DCE_CLOCK_TYPE_DPREFCLK, ATOM_GCK_DFS);
  2730. } else {
  2731. amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
  2732. }
  2733. /* initialize hpd */
  2734. dce_v11_0_hpd_init(adev);
  2735. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2736. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2737. }
  2738. dce_v11_0_pageflip_interrupt_init(adev);
  2739. return 0;
  2740. }
  2741. static int dce_v11_0_hw_fini(void *handle)
  2742. {
  2743. int i;
  2744. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2745. dce_v11_0_hpd_fini(adev);
  2746. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2747. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2748. }
  2749. dce_v11_0_pageflip_interrupt_fini(adev);
  2750. return 0;
  2751. }
  2752. static int dce_v11_0_suspend(void *handle)
  2753. {
  2754. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2755. amdgpu_atombios_scratch_regs_save(adev);
  2756. return dce_v11_0_hw_fini(handle);
  2757. }
  2758. static int dce_v11_0_resume(void *handle)
  2759. {
  2760. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2761. int ret;
  2762. ret = dce_v11_0_hw_init(handle);
  2763. amdgpu_atombios_scratch_regs_restore(adev);
  2764. /* turn on the BL */
  2765. if (adev->mode_info.bl_encoder) {
  2766. u8 bl_level = amdgpu_display_backlight_get_level(adev,
  2767. adev->mode_info.bl_encoder);
  2768. amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
  2769. bl_level);
  2770. }
  2771. return ret;
  2772. }
  2773. static bool dce_v11_0_is_idle(void *handle)
  2774. {
  2775. return true;
  2776. }
  2777. static int dce_v11_0_wait_for_idle(void *handle)
  2778. {
  2779. return 0;
  2780. }
  2781. static int dce_v11_0_soft_reset(void *handle)
  2782. {
  2783. u32 srbm_soft_reset = 0, tmp;
  2784. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2785. if (dce_v11_0_is_display_hung(adev))
  2786. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
  2787. if (srbm_soft_reset) {
  2788. tmp = RREG32(mmSRBM_SOFT_RESET);
  2789. tmp |= srbm_soft_reset;
  2790. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  2791. WREG32(mmSRBM_SOFT_RESET, tmp);
  2792. tmp = RREG32(mmSRBM_SOFT_RESET);
  2793. udelay(50);
  2794. tmp &= ~srbm_soft_reset;
  2795. WREG32(mmSRBM_SOFT_RESET, tmp);
  2796. tmp = RREG32(mmSRBM_SOFT_RESET);
  2797. /* Wait a little for things to settle down */
  2798. udelay(50);
  2799. }
  2800. return 0;
  2801. }
  2802. static void dce_v11_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
  2803. int crtc,
  2804. enum amdgpu_interrupt_state state)
  2805. {
  2806. u32 lb_interrupt_mask;
  2807. if (crtc >= adev->mode_info.num_crtc) {
  2808. DRM_DEBUG("invalid crtc %d\n", crtc);
  2809. return;
  2810. }
  2811. switch (state) {
  2812. case AMDGPU_IRQ_STATE_DISABLE:
  2813. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2814. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2815. VBLANK_INTERRUPT_MASK, 0);
  2816. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2817. break;
  2818. case AMDGPU_IRQ_STATE_ENABLE:
  2819. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2820. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2821. VBLANK_INTERRUPT_MASK, 1);
  2822. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2823. break;
  2824. default:
  2825. break;
  2826. }
  2827. }
  2828. static void dce_v11_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
  2829. int crtc,
  2830. enum amdgpu_interrupt_state state)
  2831. {
  2832. u32 lb_interrupt_mask;
  2833. if (crtc >= adev->mode_info.num_crtc) {
  2834. DRM_DEBUG("invalid crtc %d\n", crtc);
  2835. return;
  2836. }
  2837. switch (state) {
  2838. case AMDGPU_IRQ_STATE_DISABLE:
  2839. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2840. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2841. VLINE_INTERRUPT_MASK, 0);
  2842. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2843. break;
  2844. case AMDGPU_IRQ_STATE_ENABLE:
  2845. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2846. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2847. VLINE_INTERRUPT_MASK, 1);
  2848. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2849. break;
  2850. default:
  2851. break;
  2852. }
  2853. }
  2854. static int dce_v11_0_set_hpd_irq_state(struct amdgpu_device *adev,
  2855. struct amdgpu_irq_src *source,
  2856. unsigned hpd,
  2857. enum amdgpu_interrupt_state state)
  2858. {
  2859. u32 tmp;
  2860. if (hpd >= adev->mode_info.num_hpd) {
  2861. DRM_DEBUG("invalid hdp %d\n", hpd);
  2862. return 0;
  2863. }
  2864. switch (state) {
  2865. case AMDGPU_IRQ_STATE_DISABLE:
  2866. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2867. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
  2868. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2869. break;
  2870. case AMDGPU_IRQ_STATE_ENABLE:
  2871. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2872. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 1);
  2873. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2874. break;
  2875. default:
  2876. break;
  2877. }
  2878. return 0;
  2879. }
  2880. static int dce_v11_0_set_crtc_irq_state(struct amdgpu_device *adev,
  2881. struct amdgpu_irq_src *source,
  2882. unsigned type,
  2883. enum amdgpu_interrupt_state state)
  2884. {
  2885. switch (type) {
  2886. case AMDGPU_CRTC_IRQ_VBLANK1:
  2887. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 0, state);
  2888. break;
  2889. case AMDGPU_CRTC_IRQ_VBLANK2:
  2890. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 1, state);
  2891. break;
  2892. case AMDGPU_CRTC_IRQ_VBLANK3:
  2893. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 2, state);
  2894. break;
  2895. case AMDGPU_CRTC_IRQ_VBLANK4:
  2896. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 3, state);
  2897. break;
  2898. case AMDGPU_CRTC_IRQ_VBLANK5:
  2899. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 4, state);
  2900. break;
  2901. case AMDGPU_CRTC_IRQ_VBLANK6:
  2902. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 5, state);
  2903. break;
  2904. case AMDGPU_CRTC_IRQ_VLINE1:
  2905. dce_v11_0_set_crtc_vline_interrupt_state(adev, 0, state);
  2906. break;
  2907. case AMDGPU_CRTC_IRQ_VLINE2:
  2908. dce_v11_0_set_crtc_vline_interrupt_state(adev, 1, state);
  2909. break;
  2910. case AMDGPU_CRTC_IRQ_VLINE3:
  2911. dce_v11_0_set_crtc_vline_interrupt_state(adev, 2, state);
  2912. break;
  2913. case AMDGPU_CRTC_IRQ_VLINE4:
  2914. dce_v11_0_set_crtc_vline_interrupt_state(adev, 3, state);
  2915. break;
  2916. case AMDGPU_CRTC_IRQ_VLINE5:
  2917. dce_v11_0_set_crtc_vline_interrupt_state(adev, 4, state);
  2918. break;
  2919. case AMDGPU_CRTC_IRQ_VLINE6:
  2920. dce_v11_0_set_crtc_vline_interrupt_state(adev, 5, state);
  2921. break;
  2922. default:
  2923. break;
  2924. }
  2925. return 0;
  2926. }
  2927. static int dce_v11_0_set_pageflip_irq_state(struct amdgpu_device *adev,
  2928. struct amdgpu_irq_src *src,
  2929. unsigned type,
  2930. enum amdgpu_interrupt_state state)
  2931. {
  2932. u32 reg;
  2933. if (type >= adev->mode_info.num_crtc) {
  2934. DRM_ERROR("invalid pageflip crtc %d\n", type);
  2935. return -EINVAL;
  2936. }
  2937. reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
  2938. if (state == AMDGPU_IRQ_STATE_DISABLE)
  2939. WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
  2940. reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2941. else
  2942. WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
  2943. reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2944. return 0;
  2945. }
  2946. static int dce_v11_0_pageflip_irq(struct amdgpu_device *adev,
  2947. struct amdgpu_irq_src *source,
  2948. struct amdgpu_iv_entry *entry)
  2949. {
  2950. unsigned long flags;
  2951. unsigned crtc_id;
  2952. struct amdgpu_crtc *amdgpu_crtc;
  2953. struct amdgpu_flip_work *works;
  2954. crtc_id = (entry->src_id - 8) >> 1;
  2955. amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  2956. if (crtc_id >= adev->mode_info.num_crtc) {
  2957. DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
  2958. return -EINVAL;
  2959. }
  2960. if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
  2961. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
  2962. WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
  2963. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
  2964. /* IRQ could occur when in initial stage */
  2965. if(amdgpu_crtc == NULL)
  2966. return 0;
  2967. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  2968. works = amdgpu_crtc->pflip_works;
  2969. if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
  2970. DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
  2971. "AMDGPU_FLIP_SUBMITTED(%d)\n",
  2972. amdgpu_crtc->pflip_status,
  2973. AMDGPU_FLIP_SUBMITTED);
  2974. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2975. return 0;
  2976. }
  2977. /* page flip completed. clean up */
  2978. amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
  2979. amdgpu_crtc->pflip_works = NULL;
  2980. /* wakeup usersapce */
  2981. if(works->event)
  2982. drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
  2983. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2984. drm_vblank_put(adev->ddev, amdgpu_crtc->crtc_id);
  2985. schedule_work(&works->unpin_work);
  2986. return 0;
  2987. }
  2988. static void dce_v11_0_hpd_int_ack(struct amdgpu_device *adev,
  2989. int hpd)
  2990. {
  2991. u32 tmp;
  2992. if (hpd >= adev->mode_info.num_hpd) {
  2993. DRM_DEBUG("invalid hdp %d\n", hpd);
  2994. return;
  2995. }
  2996. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2997. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_ACK, 1);
  2998. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2999. }
  3000. static void dce_v11_0_crtc_vblank_int_ack(struct amdgpu_device *adev,
  3001. int crtc)
  3002. {
  3003. u32 tmp;
  3004. if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
  3005. DRM_DEBUG("invalid crtc %d\n", crtc);
  3006. return;
  3007. }
  3008. tmp = RREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc]);
  3009. tmp = REG_SET_FIELD(tmp, LB_VBLANK_STATUS, VBLANK_ACK, 1);
  3010. WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], tmp);
  3011. }
  3012. static void dce_v11_0_crtc_vline_int_ack(struct amdgpu_device *adev,
  3013. int crtc)
  3014. {
  3015. u32 tmp;
  3016. if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
  3017. DRM_DEBUG("invalid crtc %d\n", crtc);
  3018. return;
  3019. }
  3020. tmp = RREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc]);
  3021. tmp = REG_SET_FIELD(tmp, LB_VLINE_STATUS, VLINE_ACK, 1);
  3022. WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], tmp);
  3023. }
  3024. static int dce_v11_0_crtc_irq(struct amdgpu_device *adev,
  3025. struct amdgpu_irq_src *source,
  3026. struct amdgpu_iv_entry *entry)
  3027. {
  3028. unsigned crtc = entry->src_id - 1;
  3029. uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
  3030. unsigned irq_type = amdgpu_crtc_idx_to_irq_type(adev, crtc);
  3031. switch (entry->src_data) {
  3032. case 0: /* vblank */
  3033. if (disp_int & interrupt_status_offsets[crtc].vblank)
  3034. dce_v11_0_crtc_vblank_int_ack(adev, crtc);
  3035. else
  3036. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  3037. if (amdgpu_irq_enabled(adev, source, irq_type)) {
  3038. drm_handle_vblank(adev->ddev, crtc);
  3039. }
  3040. DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
  3041. break;
  3042. case 1: /* vline */
  3043. if (disp_int & interrupt_status_offsets[crtc].vline)
  3044. dce_v11_0_crtc_vline_int_ack(adev, crtc);
  3045. else
  3046. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  3047. DRM_DEBUG("IH: D%d vline\n", crtc + 1);
  3048. break;
  3049. default:
  3050. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
  3051. break;
  3052. }
  3053. return 0;
  3054. }
  3055. static int dce_v11_0_hpd_irq(struct amdgpu_device *adev,
  3056. struct amdgpu_irq_src *source,
  3057. struct amdgpu_iv_entry *entry)
  3058. {
  3059. uint32_t disp_int, mask;
  3060. unsigned hpd;
  3061. if (entry->src_data >= adev->mode_info.num_hpd) {
  3062. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
  3063. return 0;
  3064. }
  3065. hpd = entry->src_data;
  3066. disp_int = RREG32(interrupt_status_offsets[hpd].reg);
  3067. mask = interrupt_status_offsets[hpd].hpd;
  3068. if (disp_int & mask) {
  3069. dce_v11_0_hpd_int_ack(adev, hpd);
  3070. schedule_work(&adev->hotplug_work);
  3071. DRM_DEBUG("IH: HPD%d\n", hpd + 1);
  3072. }
  3073. return 0;
  3074. }
  3075. static int dce_v11_0_set_clockgating_state(void *handle,
  3076. enum amd_clockgating_state state)
  3077. {
  3078. return 0;
  3079. }
  3080. static int dce_v11_0_set_powergating_state(void *handle,
  3081. enum amd_powergating_state state)
  3082. {
  3083. return 0;
  3084. }
  3085. const struct amd_ip_funcs dce_v11_0_ip_funcs = {
  3086. .early_init = dce_v11_0_early_init,
  3087. .late_init = NULL,
  3088. .sw_init = dce_v11_0_sw_init,
  3089. .sw_fini = dce_v11_0_sw_fini,
  3090. .hw_init = dce_v11_0_hw_init,
  3091. .hw_fini = dce_v11_0_hw_fini,
  3092. .suspend = dce_v11_0_suspend,
  3093. .resume = dce_v11_0_resume,
  3094. .is_idle = dce_v11_0_is_idle,
  3095. .wait_for_idle = dce_v11_0_wait_for_idle,
  3096. .soft_reset = dce_v11_0_soft_reset,
  3097. .set_clockgating_state = dce_v11_0_set_clockgating_state,
  3098. .set_powergating_state = dce_v11_0_set_powergating_state,
  3099. };
  3100. static void
  3101. dce_v11_0_encoder_mode_set(struct drm_encoder *encoder,
  3102. struct drm_display_mode *mode,
  3103. struct drm_display_mode *adjusted_mode)
  3104. {
  3105. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3106. amdgpu_encoder->pixel_clock = adjusted_mode->clock;
  3107. /* need to call this here rather than in prepare() since we need some crtc info */
  3108. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  3109. /* set scaler clears this on some chips */
  3110. dce_v11_0_set_interleave(encoder->crtc, mode);
  3111. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
  3112. dce_v11_0_afmt_enable(encoder, true);
  3113. dce_v11_0_afmt_setmode(encoder, adjusted_mode);
  3114. }
  3115. }
  3116. static void dce_v11_0_encoder_prepare(struct drm_encoder *encoder)
  3117. {
  3118. struct amdgpu_device *adev = encoder->dev->dev_private;
  3119. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3120. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  3121. if ((amdgpu_encoder->active_device &
  3122. (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
  3123. (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
  3124. ENCODER_OBJECT_ID_NONE)) {
  3125. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  3126. if (dig) {
  3127. dig->dig_encoder = dce_v11_0_pick_dig_encoder(encoder);
  3128. if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
  3129. dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
  3130. }
  3131. }
  3132. amdgpu_atombios_scratch_regs_lock(adev, true);
  3133. if (connector) {
  3134. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  3135. /* select the clock/data port if it uses a router */
  3136. if (amdgpu_connector->router.cd_valid)
  3137. amdgpu_i2c_router_select_cd_port(amdgpu_connector);
  3138. /* turn eDP panel on for mode set */
  3139. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  3140. amdgpu_atombios_encoder_set_edp_panel_power(connector,
  3141. ATOM_TRANSMITTER_ACTION_POWER_ON);
  3142. }
  3143. /* this is needed for the pll/ss setup to work correctly in some cases */
  3144. amdgpu_atombios_encoder_set_crtc_source(encoder);
  3145. /* set up the FMT blocks */
  3146. dce_v11_0_program_fmt(encoder);
  3147. }
  3148. static void dce_v11_0_encoder_commit(struct drm_encoder *encoder)
  3149. {
  3150. struct drm_device *dev = encoder->dev;
  3151. struct amdgpu_device *adev = dev->dev_private;
  3152. /* need to call this here as we need the crtc set up */
  3153. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  3154. amdgpu_atombios_scratch_regs_lock(adev, false);
  3155. }
  3156. static void dce_v11_0_encoder_disable(struct drm_encoder *encoder)
  3157. {
  3158. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3159. struct amdgpu_encoder_atom_dig *dig;
  3160. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  3161. if (amdgpu_atombios_encoder_is_digital(encoder)) {
  3162. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
  3163. dce_v11_0_afmt_enable(encoder, false);
  3164. dig = amdgpu_encoder->enc_priv;
  3165. dig->dig_encoder = -1;
  3166. }
  3167. amdgpu_encoder->active_device = 0;
  3168. }
  3169. /* these are handled by the primary encoders */
  3170. static void dce_v11_0_ext_prepare(struct drm_encoder *encoder)
  3171. {
  3172. }
  3173. static void dce_v11_0_ext_commit(struct drm_encoder *encoder)
  3174. {
  3175. }
  3176. static void
  3177. dce_v11_0_ext_mode_set(struct drm_encoder *encoder,
  3178. struct drm_display_mode *mode,
  3179. struct drm_display_mode *adjusted_mode)
  3180. {
  3181. }
  3182. static void dce_v11_0_ext_disable(struct drm_encoder *encoder)
  3183. {
  3184. }
  3185. static void
  3186. dce_v11_0_ext_dpms(struct drm_encoder *encoder, int mode)
  3187. {
  3188. }
  3189. static const struct drm_encoder_helper_funcs dce_v11_0_ext_helper_funcs = {
  3190. .dpms = dce_v11_0_ext_dpms,
  3191. .prepare = dce_v11_0_ext_prepare,
  3192. .mode_set = dce_v11_0_ext_mode_set,
  3193. .commit = dce_v11_0_ext_commit,
  3194. .disable = dce_v11_0_ext_disable,
  3195. /* no detect for TMDS/LVDS yet */
  3196. };
  3197. static const struct drm_encoder_helper_funcs dce_v11_0_dig_helper_funcs = {
  3198. .dpms = amdgpu_atombios_encoder_dpms,
  3199. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  3200. .prepare = dce_v11_0_encoder_prepare,
  3201. .mode_set = dce_v11_0_encoder_mode_set,
  3202. .commit = dce_v11_0_encoder_commit,
  3203. .disable = dce_v11_0_encoder_disable,
  3204. .detect = amdgpu_atombios_encoder_dig_detect,
  3205. };
  3206. static const struct drm_encoder_helper_funcs dce_v11_0_dac_helper_funcs = {
  3207. .dpms = amdgpu_atombios_encoder_dpms,
  3208. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  3209. .prepare = dce_v11_0_encoder_prepare,
  3210. .mode_set = dce_v11_0_encoder_mode_set,
  3211. .commit = dce_v11_0_encoder_commit,
  3212. .detect = amdgpu_atombios_encoder_dac_detect,
  3213. };
  3214. static void dce_v11_0_encoder_destroy(struct drm_encoder *encoder)
  3215. {
  3216. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3217. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  3218. amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
  3219. kfree(amdgpu_encoder->enc_priv);
  3220. drm_encoder_cleanup(encoder);
  3221. kfree(amdgpu_encoder);
  3222. }
  3223. static const struct drm_encoder_funcs dce_v11_0_encoder_funcs = {
  3224. .destroy = dce_v11_0_encoder_destroy,
  3225. };
  3226. static void dce_v11_0_encoder_add(struct amdgpu_device *adev,
  3227. uint32_t encoder_enum,
  3228. uint32_t supported_device,
  3229. u16 caps)
  3230. {
  3231. struct drm_device *dev = adev->ddev;
  3232. struct drm_encoder *encoder;
  3233. struct amdgpu_encoder *amdgpu_encoder;
  3234. /* see if we already added it */
  3235. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  3236. amdgpu_encoder = to_amdgpu_encoder(encoder);
  3237. if (amdgpu_encoder->encoder_enum == encoder_enum) {
  3238. amdgpu_encoder->devices |= supported_device;
  3239. return;
  3240. }
  3241. }
  3242. /* add a new one */
  3243. amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
  3244. if (!amdgpu_encoder)
  3245. return;
  3246. encoder = &amdgpu_encoder->base;
  3247. switch (adev->mode_info.num_crtc) {
  3248. case 1:
  3249. encoder->possible_crtcs = 0x1;
  3250. break;
  3251. case 2:
  3252. default:
  3253. encoder->possible_crtcs = 0x3;
  3254. break;
  3255. case 4:
  3256. encoder->possible_crtcs = 0xf;
  3257. break;
  3258. case 6:
  3259. encoder->possible_crtcs = 0x3f;
  3260. break;
  3261. }
  3262. amdgpu_encoder->enc_priv = NULL;
  3263. amdgpu_encoder->encoder_enum = encoder_enum;
  3264. amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
  3265. amdgpu_encoder->devices = supported_device;
  3266. amdgpu_encoder->rmx_type = RMX_OFF;
  3267. amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
  3268. amdgpu_encoder->is_ext_encoder = false;
  3269. amdgpu_encoder->caps = caps;
  3270. switch (amdgpu_encoder->encoder_id) {
  3271. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
  3272. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
  3273. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3274. DRM_MODE_ENCODER_DAC, NULL);
  3275. drm_encoder_helper_add(encoder, &dce_v11_0_dac_helper_funcs);
  3276. break;
  3277. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
  3278. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  3279. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  3280. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  3281. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
  3282. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
  3283. amdgpu_encoder->rmx_type = RMX_FULL;
  3284. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3285. DRM_MODE_ENCODER_LVDS, NULL);
  3286. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
  3287. } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
  3288. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3289. DRM_MODE_ENCODER_DAC, NULL);
  3290. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  3291. } else {
  3292. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3293. DRM_MODE_ENCODER_TMDS, NULL);
  3294. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  3295. }
  3296. drm_encoder_helper_add(encoder, &dce_v11_0_dig_helper_funcs);
  3297. break;
  3298. case ENCODER_OBJECT_ID_SI170B:
  3299. case ENCODER_OBJECT_ID_CH7303:
  3300. case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
  3301. case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
  3302. case ENCODER_OBJECT_ID_TITFP513:
  3303. case ENCODER_OBJECT_ID_VT1623:
  3304. case ENCODER_OBJECT_ID_HDMI_SI1930:
  3305. case ENCODER_OBJECT_ID_TRAVIS:
  3306. case ENCODER_OBJECT_ID_NUTMEG:
  3307. /* these are handled by the primary encoders */
  3308. amdgpu_encoder->is_ext_encoder = true;
  3309. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  3310. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3311. DRM_MODE_ENCODER_LVDS, NULL);
  3312. else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
  3313. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3314. DRM_MODE_ENCODER_DAC, NULL);
  3315. else
  3316. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3317. DRM_MODE_ENCODER_TMDS, NULL);
  3318. drm_encoder_helper_add(encoder, &dce_v11_0_ext_helper_funcs);
  3319. break;
  3320. }
  3321. }
  3322. static const struct amdgpu_display_funcs dce_v11_0_display_funcs = {
  3323. .set_vga_render_state = &dce_v11_0_set_vga_render_state,
  3324. .bandwidth_update = &dce_v11_0_bandwidth_update,
  3325. .vblank_get_counter = &dce_v11_0_vblank_get_counter,
  3326. .vblank_wait = &dce_v11_0_vblank_wait,
  3327. .is_display_hung = &dce_v11_0_is_display_hung,
  3328. .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
  3329. .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
  3330. .hpd_sense = &dce_v11_0_hpd_sense,
  3331. .hpd_set_polarity = &dce_v11_0_hpd_set_polarity,
  3332. .hpd_get_gpio_reg = &dce_v11_0_hpd_get_gpio_reg,
  3333. .page_flip = &dce_v11_0_page_flip,
  3334. .page_flip_get_scanoutpos = &dce_v11_0_crtc_get_scanoutpos,
  3335. .add_encoder = &dce_v11_0_encoder_add,
  3336. .add_connector = &amdgpu_connector_add,
  3337. .stop_mc_access = &dce_v11_0_stop_mc_access,
  3338. .resume_mc_access = &dce_v11_0_resume_mc_access,
  3339. };
  3340. static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev)
  3341. {
  3342. if (adev->mode_info.funcs == NULL)
  3343. adev->mode_info.funcs = &dce_v11_0_display_funcs;
  3344. }
  3345. static const struct amdgpu_irq_src_funcs dce_v11_0_crtc_irq_funcs = {
  3346. .set = dce_v11_0_set_crtc_irq_state,
  3347. .process = dce_v11_0_crtc_irq,
  3348. };
  3349. static const struct amdgpu_irq_src_funcs dce_v11_0_pageflip_irq_funcs = {
  3350. .set = dce_v11_0_set_pageflip_irq_state,
  3351. .process = dce_v11_0_pageflip_irq,
  3352. };
  3353. static const struct amdgpu_irq_src_funcs dce_v11_0_hpd_irq_funcs = {
  3354. .set = dce_v11_0_set_hpd_irq_state,
  3355. .process = dce_v11_0_hpd_irq,
  3356. };
  3357. static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev)
  3358. {
  3359. adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_LAST;
  3360. adev->crtc_irq.funcs = &dce_v11_0_crtc_irq_funcs;
  3361. adev->pageflip_irq.num_types = AMDGPU_PAGEFLIP_IRQ_LAST;
  3362. adev->pageflip_irq.funcs = &dce_v11_0_pageflip_irq_funcs;
  3363. adev->hpd_irq.num_types = AMDGPU_HPD_LAST;
  3364. adev->hpd_irq.funcs = &dce_v11_0_hpd_irq_funcs;
  3365. }