dce_v8_0.c 110 KB

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