dce_v8_0.c 110 KB

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