dce_v11_0.c 116 KB

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