dce_v6_0.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172
  1. /*
  2. * Copyright 2015 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 "atom.h"
  28. #include "amdgpu_atombios.h"
  29. #include "atombios_crtc.h"
  30. #include "atombios_encoders.h"
  31. #include "amdgpu_pll.h"
  32. #include "amdgpu_connectors.h"
  33. #include "si/si_reg.h"
  34. #include "si/sid.h"
  35. static void dce_v6_0_set_display_funcs(struct amdgpu_device *adev);
  36. static void dce_v6_0_set_irq_funcs(struct amdgpu_device *adev);
  37. static const u32 crtc_offsets[6] =
  38. {
  39. SI_CRTC0_REGISTER_OFFSET,
  40. SI_CRTC1_REGISTER_OFFSET,
  41. SI_CRTC2_REGISTER_OFFSET,
  42. SI_CRTC3_REGISTER_OFFSET,
  43. SI_CRTC4_REGISTER_OFFSET,
  44. SI_CRTC5_REGISTER_OFFSET
  45. };
  46. static const uint32_t dig_offsets[] = {
  47. SI_CRTC0_REGISTER_OFFSET,
  48. SI_CRTC1_REGISTER_OFFSET,
  49. SI_CRTC2_REGISTER_OFFSET,
  50. SI_CRTC3_REGISTER_OFFSET,
  51. SI_CRTC4_REGISTER_OFFSET,
  52. SI_CRTC5_REGISTER_OFFSET,
  53. (0x13830 - 0x7030) >> 2,
  54. };
  55. static const struct {
  56. uint32_t reg;
  57. uint32_t vblank;
  58. uint32_t vline;
  59. uint32_t hpd;
  60. } interrupt_status_offsets[6] = { {
  61. .reg = DISP_INTERRUPT_STATUS,
  62. .vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
  63. .vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
  64. .hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
  65. }, {
  66. .reg = DISP_INTERRUPT_STATUS_CONTINUE,
  67. .vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
  68. .vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
  69. .hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
  70. }, {
  71. .reg = DISP_INTERRUPT_STATUS_CONTINUE2,
  72. .vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
  73. .vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
  74. .hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
  75. }, {
  76. .reg = DISP_INTERRUPT_STATUS_CONTINUE3,
  77. .vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
  78. .vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
  79. .hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
  80. }, {
  81. .reg = DISP_INTERRUPT_STATUS_CONTINUE4,
  82. .vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
  83. .vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
  84. .hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
  85. }, {
  86. .reg = DISP_INTERRUPT_STATUS_CONTINUE5,
  87. .vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
  88. .vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
  89. .hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
  90. } };
  91. static const uint32_t hpd_int_control_offsets[6] = {
  92. DC_HPD1_INT_CONTROL,
  93. DC_HPD2_INT_CONTROL,
  94. DC_HPD3_INT_CONTROL,
  95. DC_HPD4_INT_CONTROL,
  96. DC_HPD5_INT_CONTROL,
  97. DC_HPD6_INT_CONTROL,
  98. };
  99. static u32 dce_v6_0_audio_endpt_rreg(struct amdgpu_device *adev,
  100. u32 block_offset, u32 reg)
  101. {
  102. DRM_INFO("xxxx: dce_v6_0_audio_endpt_rreg ----no impl!!!!\n");
  103. return 0;
  104. }
  105. static void dce_v6_0_audio_endpt_wreg(struct amdgpu_device *adev,
  106. u32 block_offset, u32 reg, u32 v)
  107. {
  108. DRM_INFO("xxxx: dce_v6_0_audio_endpt_wreg ----no impl!!!!\n");
  109. }
  110. static bool dce_v6_0_is_in_vblank(struct amdgpu_device *adev, int crtc)
  111. {
  112. if (RREG32(EVERGREEN_CRTC_STATUS + crtc_offsets[crtc]) & EVERGREEN_CRTC_V_BLANK)
  113. return true;
  114. else
  115. return false;
  116. }
  117. static bool dce_v6_0_is_counter_moving(struct amdgpu_device *adev, int crtc)
  118. {
  119. u32 pos1, pos2;
  120. pos1 = RREG32(EVERGREEN_CRTC_STATUS_POSITION + crtc_offsets[crtc]);
  121. pos2 = RREG32(EVERGREEN_CRTC_STATUS_POSITION + crtc_offsets[crtc]);
  122. if (pos1 != pos2)
  123. return true;
  124. else
  125. return false;
  126. }
  127. /**
  128. * dce_v6_0_wait_for_vblank - vblank wait asic callback.
  129. *
  130. * @crtc: crtc to wait for vblank on
  131. *
  132. * Wait for vblank on the requested crtc (evergreen+).
  133. */
  134. static void dce_v6_0_vblank_wait(struct amdgpu_device *adev, int crtc)
  135. {
  136. unsigned i = 100;
  137. if (crtc >= adev->mode_info.num_crtc)
  138. return;
  139. if (!(RREG32(EVERGREEN_CRTC_CONTROL + crtc_offsets[crtc]) & EVERGREEN_CRTC_MASTER_EN))
  140. return;
  141. /* depending on when we hit vblank, we may be close to active; if so,
  142. * wait for another frame.
  143. */
  144. while (dce_v6_0_is_in_vblank(adev, crtc)) {
  145. if (i++ == 100) {
  146. i = 0;
  147. if (!dce_v6_0_is_counter_moving(adev, crtc))
  148. break;
  149. }
  150. }
  151. while (!dce_v6_0_is_in_vblank(adev, crtc)) {
  152. if (i++ == 100) {
  153. i = 0;
  154. if (!dce_v6_0_is_counter_moving(adev, crtc))
  155. break;
  156. }
  157. }
  158. }
  159. static u32 dce_v6_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
  160. {
  161. if (crtc >= adev->mode_info.num_crtc)
  162. return 0;
  163. else
  164. return RREG32(CRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
  165. }
  166. static void dce_v6_0_pageflip_interrupt_init(struct amdgpu_device *adev)
  167. {
  168. unsigned i;
  169. /* Enable pflip interrupts */
  170. for (i = 0; i < adev->mode_info.num_crtc; i++)
  171. amdgpu_irq_get(adev, &adev->pageflip_irq, i);
  172. }
  173. static void dce_v6_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
  174. {
  175. unsigned i;
  176. /* Disable pflip interrupts */
  177. for (i = 0; i < adev->mode_info.num_crtc; i++)
  178. amdgpu_irq_put(adev, &adev->pageflip_irq, i);
  179. }
  180. /**
  181. * dce_v6_0_page_flip - pageflip callback.
  182. *
  183. * @adev: amdgpu_device pointer
  184. * @crtc_id: crtc to cleanup pageflip on
  185. * @crtc_base: new address of the crtc (GPU MC address)
  186. *
  187. * Does the actual pageflip (evergreen+).
  188. * During vblank we take the crtc lock and wait for the update_pending
  189. * bit to go high, when it does, we release the lock, and allow the
  190. * double buffered update to take place.
  191. * Returns the current update pending status.
  192. */
  193. static void dce_v6_0_page_flip(struct amdgpu_device *adev,
  194. int crtc_id, u64 crtc_base, bool async)
  195. {
  196. struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  197. /* flip at hsync for async, default is vsync */
  198. WREG32(EVERGREEN_GRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, async ?
  199. EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN : 0);
  200. /* update the scanout addresses */
  201. WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  202. upper_32_bits(crtc_base));
  203. WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  204. (u32)crtc_base);
  205. /* post the write */
  206. RREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
  207. }
  208. static int dce_v6_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
  209. u32 *vbl, u32 *position)
  210. {
  211. if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
  212. return -EINVAL;
  213. *vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END + crtc_offsets[crtc]);
  214. *position = RREG32(EVERGREEN_CRTC_STATUS_POSITION + crtc_offsets[crtc]);
  215. return 0;
  216. }
  217. /**
  218. * dce_v6_0_hpd_sense - hpd sense callback.
  219. *
  220. * @adev: amdgpu_device pointer
  221. * @hpd: hpd (hotplug detect) pin
  222. *
  223. * Checks if a digital monitor is connected (evergreen+).
  224. * Returns true if connected, false if not connected.
  225. */
  226. static bool dce_v6_0_hpd_sense(struct amdgpu_device *adev,
  227. enum amdgpu_hpd_id hpd)
  228. {
  229. bool connected = false;
  230. switch (hpd) {
  231. case AMDGPU_HPD_1:
  232. if (RREG32(DC_HPD1_INT_STATUS) & DC_HPDx_SENSE)
  233. connected = true;
  234. break;
  235. case AMDGPU_HPD_2:
  236. if (RREG32(DC_HPD2_INT_STATUS) & DC_HPDx_SENSE)
  237. connected = true;
  238. break;
  239. case AMDGPU_HPD_3:
  240. if (RREG32(DC_HPD3_INT_STATUS) & DC_HPDx_SENSE)
  241. connected = true;
  242. break;
  243. case AMDGPU_HPD_4:
  244. if (RREG32(DC_HPD4_INT_STATUS) & DC_HPDx_SENSE)
  245. connected = true;
  246. break;
  247. case AMDGPU_HPD_5:
  248. if (RREG32(DC_HPD5_INT_STATUS) & DC_HPDx_SENSE)
  249. connected = true;
  250. break;
  251. case AMDGPU_HPD_6:
  252. if (RREG32(DC_HPD6_INT_STATUS) & DC_HPDx_SENSE)
  253. connected = true;
  254. break;
  255. default:
  256. break;
  257. }
  258. return connected;
  259. }
  260. /**
  261. * dce_v6_0_hpd_set_polarity - hpd set polarity callback.
  262. *
  263. * @adev: amdgpu_device pointer
  264. * @hpd: hpd (hotplug detect) pin
  265. *
  266. * Set the polarity of the hpd pin (evergreen+).
  267. */
  268. static void dce_v6_0_hpd_set_polarity(struct amdgpu_device *adev,
  269. enum amdgpu_hpd_id hpd)
  270. {
  271. u32 tmp;
  272. bool connected = dce_v6_0_hpd_sense(adev, hpd);
  273. switch (hpd) {
  274. case AMDGPU_HPD_1:
  275. tmp = RREG32(DC_HPD1_INT_CONTROL);
  276. if (connected)
  277. tmp &= ~DC_HPDx_INT_POLARITY;
  278. else
  279. tmp |= DC_HPDx_INT_POLARITY;
  280. WREG32(DC_HPD1_INT_CONTROL, tmp);
  281. break;
  282. case AMDGPU_HPD_2:
  283. tmp = RREG32(DC_HPD2_INT_CONTROL);
  284. if (connected)
  285. tmp &= ~DC_HPDx_INT_POLARITY;
  286. else
  287. tmp |= DC_HPDx_INT_POLARITY;
  288. WREG32(DC_HPD2_INT_CONTROL, tmp);
  289. break;
  290. case AMDGPU_HPD_3:
  291. tmp = RREG32(DC_HPD3_INT_CONTROL);
  292. if (connected)
  293. tmp &= ~DC_HPDx_INT_POLARITY;
  294. else
  295. tmp |= DC_HPDx_INT_POLARITY;
  296. WREG32(DC_HPD3_INT_CONTROL, tmp);
  297. break;
  298. case AMDGPU_HPD_4:
  299. tmp = RREG32(DC_HPD4_INT_CONTROL);
  300. if (connected)
  301. tmp &= ~DC_HPDx_INT_POLARITY;
  302. else
  303. tmp |= DC_HPDx_INT_POLARITY;
  304. WREG32(DC_HPD4_INT_CONTROL, tmp);
  305. break;
  306. case AMDGPU_HPD_5:
  307. tmp = RREG32(DC_HPD5_INT_CONTROL);
  308. if (connected)
  309. tmp &= ~DC_HPDx_INT_POLARITY;
  310. else
  311. tmp |= DC_HPDx_INT_POLARITY;
  312. WREG32(DC_HPD5_INT_CONTROL, tmp);
  313. break;
  314. case AMDGPU_HPD_6:
  315. tmp = RREG32(DC_HPD6_INT_CONTROL);
  316. if (connected)
  317. tmp &= ~DC_HPDx_INT_POLARITY;
  318. else
  319. tmp |= DC_HPDx_INT_POLARITY;
  320. WREG32(DC_HPD6_INT_CONTROL, tmp);
  321. break;
  322. default:
  323. break;
  324. }
  325. }
  326. /**
  327. * dce_v6_0_hpd_init - hpd setup callback.
  328. *
  329. * @adev: amdgpu_device pointer
  330. *
  331. * Setup the hpd pins used by the card (evergreen+).
  332. * Enable the pin, set the polarity, and enable the hpd interrupts.
  333. */
  334. static void dce_v6_0_hpd_init(struct amdgpu_device *adev)
  335. {
  336. struct drm_device *dev = adev->ddev;
  337. struct drm_connector *connector;
  338. u32 tmp = DC_HPDx_CONNECTION_TIMER(0x9c4) |
  339. DC_HPDx_RX_INT_TIMER(0xfa) | DC_HPDx_EN;
  340. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  341. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  342. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
  343. connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
  344. /* don't try to enable hpd on eDP or LVDS avoid breaking the
  345. * aux dp channel on imac and help (but not completely fix)
  346. * https://bugzilla.redhat.com/show_bug.cgi?id=726143
  347. * also avoid interrupt storms during dpms.
  348. */
  349. continue;
  350. }
  351. switch (amdgpu_connector->hpd.hpd) {
  352. case AMDGPU_HPD_1:
  353. WREG32(DC_HPD1_CONTROL, tmp);
  354. break;
  355. case AMDGPU_HPD_2:
  356. WREG32(DC_HPD2_CONTROL, tmp);
  357. break;
  358. case AMDGPU_HPD_3:
  359. WREG32(DC_HPD3_CONTROL, tmp);
  360. break;
  361. case AMDGPU_HPD_4:
  362. WREG32(DC_HPD4_CONTROL, tmp);
  363. break;
  364. case AMDGPU_HPD_5:
  365. WREG32(DC_HPD5_CONTROL, tmp);
  366. break;
  367. case AMDGPU_HPD_6:
  368. WREG32(DC_HPD6_CONTROL, tmp);
  369. break;
  370. default:
  371. break;
  372. }
  373. dce_v6_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
  374. amdgpu_irq_get(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
  375. }
  376. }
  377. /**
  378. * dce_v6_0_hpd_fini - hpd tear down callback.
  379. *
  380. * @adev: amdgpu_device pointer
  381. *
  382. * Tear down the hpd pins used by the card (evergreen+).
  383. * Disable the hpd interrupts.
  384. */
  385. static void dce_v6_0_hpd_fini(struct amdgpu_device *adev)
  386. {
  387. struct drm_device *dev = adev->ddev;
  388. struct drm_connector *connector;
  389. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  390. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  391. switch (amdgpu_connector->hpd.hpd) {
  392. case AMDGPU_HPD_1:
  393. WREG32(DC_HPD1_CONTROL, 0);
  394. break;
  395. case AMDGPU_HPD_2:
  396. WREG32(DC_HPD2_CONTROL, 0);
  397. break;
  398. case AMDGPU_HPD_3:
  399. WREG32(DC_HPD3_CONTROL, 0);
  400. break;
  401. case AMDGPU_HPD_4:
  402. WREG32(DC_HPD4_CONTROL, 0);
  403. break;
  404. case AMDGPU_HPD_5:
  405. WREG32(DC_HPD5_CONTROL, 0);
  406. break;
  407. case AMDGPU_HPD_6:
  408. WREG32(DC_HPD6_CONTROL, 0);
  409. break;
  410. default:
  411. break;
  412. }
  413. amdgpu_irq_put(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
  414. }
  415. }
  416. static u32 dce_v6_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
  417. {
  418. return SI_DC_GPIO_HPD_A;
  419. }
  420. static bool dce_v6_0_is_display_hung(struct amdgpu_device *adev)
  421. {
  422. DRM_INFO("xxxx: dce_v6_0_is_display_hung ----no imp!!!!!\n");
  423. return true;
  424. }
  425. static u32 evergreen_get_vblank_counter(struct amdgpu_device* adev, int crtc)
  426. {
  427. if (crtc >= adev->mode_info.num_crtc)
  428. return 0;
  429. else
  430. return RREG32(CRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
  431. }
  432. static void dce_v6_0_stop_mc_access(struct amdgpu_device *adev,
  433. struct amdgpu_mode_mc_save *save)
  434. {
  435. u32 crtc_enabled, tmp, frame_count;
  436. int i, j;
  437. save->vga_render_control = RREG32(VGA_RENDER_CONTROL);
  438. save->vga_hdp_control = RREG32(VGA_HDP_CONTROL);
  439. /* disable VGA render */
  440. WREG32(VGA_RENDER_CONTROL, 0);
  441. /* blank the display controllers */
  442. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  443. crtc_enabled = RREG32(EVERGREEN_CRTC_CONTROL + crtc_offsets[i]) & EVERGREEN_CRTC_MASTER_EN;
  444. if (crtc_enabled) {
  445. save->crtc_enabled[i] = true;
  446. tmp = RREG32(EVERGREEN_CRTC_BLANK_CONTROL + crtc_offsets[i]);
  447. if (!(tmp & EVERGREEN_CRTC_BLANK_DATA_EN)) {
  448. dce_v6_0_vblank_wait(adev, i);
  449. WREG32(EVERGREEN_CRTC_UPDATE_LOCK + crtc_offsets[i], 1);
  450. tmp |= EVERGREEN_CRTC_BLANK_DATA_EN;
  451. WREG32(EVERGREEN_CRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
  452. WREG32(EVERGREEN_CRTC_UPDATE_LOCK + crtc_offsets[i], 0);
  453. }
  454. /* wait for the next frame */
  455. frame_count = evergreen_get_vblank_counter(adev, i);
  456. for (j = 0; j < adev->usec_timeout; j++) {
  457. if (evergreen_get_vblank_counter(adev, i) != frame_count)
  458. break;
  459. udelay(1);
  460. }
  461. /* XXX this is a hack to avoid strange behavior with EFI on certain systems */
  462. WREG32(EVERGREEN_CRTC_UPDATE_LOCK + crtc_offsets[i], 1);
  463. tmp = RREG32(EVERGREEN_CRTC_CONTROL + crtc_offsets[i]);
  464. tmp &= ~EVERGREEN_CRTC_MASTER_EN;
  465. WREG32(EVERGREEN_CRTC_CONTROL + crtc_offsets[i], tmp);
  466. WREG32(EVERGREEN_CRTC_UPDATE_LOCK + crtc_offsets[i], 0);
  467. save->crtc_enabled[i] = false;
  468. /* ***** */
  469. } else {
  470. save->crtc_enabled[i] = false;
  471. }
  472. }
  473. }
  474. static void dce_v6_0_resume_mc_access(struct amdgpu_device *adev,
  475. struct amdgpu_mode_mc_save *save)
  476. {
  477. u32 tmp;
  478. int i, j;
  479. /* update crtc base addresses */
  480. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  481. WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + crtc_offsets[i],
  482. upper_32_bits(adev->mc.vram_start));
  483. WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + crtc_offsets[i],
  484. upper_32_bits(adev->mc.vram_start));
  485. WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + crtc_offsets[i],
  486. (u32)adev->mc.vram_start);
  487. WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + crtc_offsets[i],
  488. (u32)adev->mc.vram_start);
  489. }
  490. WREG32(EVERGREEN_VGA_MEMORY_BASE_ADDRESS_HIGH, upper_32_bits(adev->mc.vram_start));
  491. WREG32(EVERGREEN_VGA_MEMORY_BASE_ADDRESS, (u32)adev->mc.vram_start);
  492. /* unlock regs and wait for update */
  493. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  494. if (save->crtc_enabled[i]) {
  495. tmp = RREG32(EVERGREEN_MASTER_UPDATE_MODE + crtc_offsets[i]);
  496. if ((tmp & 0x7) != 3) {
  497. tmp &= ~0x7;
  498. tmp |= 0x3;
  499. WREG32(EVERGREEN_MASTER_UPDATE_MODE + crtc_offsets[i], tmp);
  500. }
  501. tmp = RREG32(EVERGREEN_GRPH_UPDATE + crtc_offsets[i]);
  502. if (tmp & EVERGREEN_GRPH_UPDATE_LOCK) {
  503. tmp &= ~EVERGREEN_GRPH_UPDATE_LOCK;
  504. WREG32(EVERGREEN_GRPH_UPDATE + crtc_offsets[i], tmp);
  505. }
  506. tmp = RREG32(EVERGREEN_MASTER_UPDATE_LOCK + crtc_offsets[i]);
  507. if (tmp & 1) {
  508. tmp &= ~1;
  509. WREG32(EVERGREEN_MASTER_UPDATE_LOCK + crtc_offsets[i], tmp);
  510. }
  511. for (j = 0; j < adev->usec_timeout; j++) {
  512. tmp = RREG32(EVERGREEN_GRPH_UPDATE + crtc_offsets[i]);
  513. if ((tmp & EVERGREEN_GRPH_SURFACE_UPDATE_PENDING) == 0)
  514. break;
  515. udelay(1);
  516. }
  517. }
  518. }
  519. /* Unlock vga access */
  520. WREG32(VGA_HDP_CONTROL, save->vga_hdp_control);
  521. mdelay(1);
  522. WREG32(VGA_RENDER_CONTROL, save->vga_render_control);
  523. }
  524. static void dce_v6_0_set_vga_render_state(struct amdgpu_device *adev,
  525. bool render)
  526. {
  527. if (!render)
  528. WREG32(R_000300_VGA_RENDER_CONTROL,
  529. RREG32(R_000300_VGA_RENDER_CONTROL) & C_000300_VGA_VSTATUS_CNTL);
  530. }
  531. static void dce_v6_0_program_fmt(struct drm_encoder *encoder)
  532. {
  533. struct drm_device *dev = encoder->dev;
  534. struct amdgpu_device *adev = dev->dev_private;
  535. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  536. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  537. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  538. int bpc = 0;
  539. u32 tmp = 0;
  540. enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
  541. if (connector) {
  542. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  543. bpc = amdgpu_connector_get_monitor_bpc(connector);
  544. dither = amdgpu_connector->dither;
  545. }
  546. /* LVDS FMT is set up by atom */
  547. if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
  548. return;
  549. if (bpc == 0)
  550. return;
  551. switch (bpc) {
  552. case 6:
  553. if (dither == AMDGPU_FMT_DITHER_ENABLE)
  554. /* XXX sort out optimal dither settings */
  555. tmp |= (FMT_FRAME_RANDOM_ENABLE | FMT_HIGHPASS_RANDOM_ENABLE |
  556. FMT_SPATIAL_DITHER_EN);
  557. else
  558. tmp |= FMT_TRUNCATE_EN;
  559. break;
  560. case 8:
  561. if (dither == AMDGPU_FMT_DITHER_ENABLE)
  562. /* XXX sort out optimal dither settings */
  563. tmp |= (FMT_FRAME_RANDOM_ENABLE | FMT_HIGHPASS_RANDOM_ENABLE |
  564. FMT_RGB_RANDOM_ENABLE |
  565. FMT_SPATIAL_DITHER_EN | FMT_SPATIAL_DITHER_DEPTH);
  566. else
  567. tmp |= (FMT_TRUNCATE_EN | FMT_TRUNCATE_DEPTH);
  568. break;
  569. case 10:
  570. default:
  571. /* not needed */
  572. break;
  573. }
  574. WREG32(FMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  575. }
  576. /**
  577. * cik_get_number_of_dram_channels - get the number of dram channels
  578. *
  579. * @adev: amdgpu_device pointer
  580. *
  581. * Look up the number of video ram channels (CIK).
  582. * Used for display watermark bandwidth calculations
  583. * Returns the number of dram channels
  584. */
  585. static u32 si_get_number_of_dram_channels(struct amdgpu_device *adev)
  586. {
  587. u32 tmp = RREG32(MC_SHARED_CHMAP);
  588. switch ((tmp & MC_SHARED_CHMAP__NOOFCHAN_MASK) >> MC_SHARED_CHMAP__NOOFCHAN__SHIFT) {
  589. case 0:
  590. default:
  591. return 1;
  592. case 1:
  593. return 2;
  594. case 2:
  595. return 4;
  596. case 3:
  597. return 8;
  598. case 4:
  599. return 3;
  600. case 5:
  601. return 6;
  602. case 6:
  603. return 10;
  604. case 7:
  605. return 12;
  606. case 8:
  607. return 16;
  608. }
  609. }
  610. struct dce6_wm_params {
  611. u32 dram_channels; /* number of dram channels */
  612. u32 yclk; /* bandwidth per dram data pin in kHz */
  613. u32 sclk; /* engine clock in kHz */
  614. u32 disp_clk; /* display clock in kHz */
  615. u32 src_width; /* viewport width */
  616. u32 active_time; /* active display time in ns */
  617. u32 blank_time; /* blank time in ns */
  618. bool interlaced; /* mode is interlaced */
  619. fixed20_12 vsc; /* vertical scale ratio */
  620. u32 num_heads; /* number of active crtcs */
  621. u32 bytes_per_pixel; /* bytes per pixel display + overlay */
  622. u32 lb_size; /* line buffer allocated to pipe */
  623. u32 vtaps; /* vertical scaler taps */
  624. };
  625. /**
  626. * dce_v6_0_dram_bandwidth - get the dram bandwidth
  627. *
  628. * @wm: watermark calculation data
  629. *
  630. * Calculate the raw dram bandwidth (CIK).
  631. * Used for display watermark bandwidth calculations
  632. * Returns the dram bandwidth in MBytes/s
  633. */
  634. static u32 dce_v6_0_dram_bandwidth(struct dce6_wm_params *wm)
  635. {
  636. /* Calculate raw DRAM Bandwidth */
  637. fixed20_12 dram_efficiency; /* 0.7 */
  638. fixed20_12 yclk, dram_channels, bandwidth;
  639. fixed20_12 a;
  640. a.full = dfixed_const(1000);
  641. yclk.full = dfixed_const(wm->yclk);
  642. yclk.full = dfixed_div(yclk, a);
  643. dram_channels.full = dfixed_const(wm->dram_channels * 4);
  644. a.full = dfixed_const(10);
  645. dram_efficiency.full = dfixed_const(7);
  646. dram_efficiency.full = dfixed_div(dram_efficiency, a);
  647. bandwidth.full = dfixed_mul(dram_channels, yclk);
  648. bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
  649. return dfixed_trunc(bandwidth);
  650. }
  651. /**
  652. * dce_v6_0_dram_bandwidth_for_display - get the dram bandwidth for display
  653. *
  654. * @wm: watermark calculation data
  655. *
  656. * Calculate the dram bandwidth used for display (CIK).
  657. * Used for display watermark bandwidth calculations
  658. * Returns the dram bandwidth for display in MBytes/s
  659. */
  660. static u32 dce_v6_0_dram_bandwidth_for_display(struct dce6_wm_params *wm)
  661. {
  662. /* Calculate DRAM Bandwidth and the part allocated to display. */
  663. fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
  664. fixed20_12 yclk, dram_channels, bandwidth;
  665. fixed20_12 a;
  666. a.full = dfixed_const(1000);
  667. yclk.full = dfixed_const(wm->yclk);
  668. yclk.full = dfixed_div(yclk, a);
  669. dram_channels.full = dfixed_const(wm->dram_channels * 4);
  670. a.full = dfixed_const(10);
  671. disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
  672. disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
  673. bandwidth.full = dfixed_mul(dram_channels, yclk);
  674. bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
  675. return dfixed_trunc(bandwidth);
  676. }
  677. /**
  678. * dce_v6_0_data_return_bandwidth - get the data return bandwidth
  679. *
  680. * @wm: watermark calculation data
  681. *
  682. * Calculate the data return bandwidth used for display (CIK).
  683. * Used for display watermark bandwidth calculations
  684. * Returns the data return bandwidth in MBytes/s
  685. */
  686. static u32 dce_v6_0_data_return_bandwidth(struct dce6_wm_params *wm)
  687. {
  688. /* Calculate the display Data return Bandwidth */
  689. fixed20_12 return_efficiency; /* 0.8 */
  690. fixed20_12 sclk, bandwidth;
  691. fixed20_12 a;
  692. a.full = dfixed_const(1000);
  693. sclk.full = dfixed_const(wm->sclk);
  694. sclk.full = dfixed_div(sclk, a);
  695. a.full = dfixed_const(10);
  696. return_efficiency.full = dfixed_const(8);
  697. return_efficiency.full = dfixed_div(return_efficiency, a);
  698. a.full = dfixed_const(32);
  699. bandwidth.full = dfixed_mul(a, sclk);
  700. bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
  701. return dfixed_trunc(bandwidth);
  702. }
  703. /**
  704. * dce_v6_0_dmif_request_bandwidth - get the dmif bandwidth
  705. *
  706. * @wm: watermark calculation data
  707. *
  708. * Calculate the dmif bandwidth used for display (CIK).
  709. * Used for display watermark bandwidth calculations
  710. * Returns the dmif bandwidth in MBytes/s
  711. */
  712. static u32 dce_v6_0_dmif_request_bandwidth(struct dce6_wm_params *wm)
  713. {
  714. /* Calculate the DMIF Request Bandwidth */
  715. fixed20_12 disp_clk_request_efficiency; /* 0.8 */
  716. fixed20_12 disp_clk, bandwidth;
  717. fixed20_12 a, b;
  718. a.full = dfixed_const(1000);
  719. disp_clk.full = dfixed_const(wm->disp_clk);
  720. disp_clk.full = dfixed_div(disp_clk, a);
  721. a.full = dfixed_const(32);
  722. b.full = dfixed_mul(a, disp_clk);
  723. a.full = dfixed_const(10);
  724. disp_clk_request_efficiency.full = dfixed_const(8);
  725. disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
  726. bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
  727. return dfixed_trunc(bandwidth);
  728. }
  729. /**
  730. * dce_v6_0_available_bandwidth - get the min available bandwidth
  731. *
  732. * @wm: watermark calculation data
  733. *
  734. * Calculate the min available bandwidth used for display (CIK).
  735. * Used for display watermark bandwidth calculations
  736. * Returns the min available bandwidth in MBytes/s
  737. */
  738. static u32 dce_v6_0_available_bandwidth(struct dce6_wm_params *wm)
  739. {
  740. /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
  741. u32 dram_bandwidth = dce_v6_0_dram_bandwidth(wm);
  742. u32 data_return_bandwidth = dce_v6_0_data_return_bandwidth(wm);
  743. u32 dmif_req_bandwidth = dce_v6_0_dmif_request_bandwidth(wm);
  744. return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
  745. }
  746. /**
  747. * dce_v6_0_average_bandwidth - get the average available bandwidth
  748. *
  749. * @wm: watermark calculation data
  750. *
  751. * Calculate the average available bandwidth used for display (CIK).
  752. * Used for display watermark bandwidth calculations
  753. * Returns the average available bandwidth in MBytes/s
  754. */
  755. static u32 dce_v6_0_average_bandwidth(struct dce6_wm_params *wm)
  756. {
  757. /* Calculate the display mode Average Bandwidth
  758. * DisplayMode should contain the source and destination dimensions,
  759. * timing, etc.
  760. */
  761. fixed20_12 bpp;
  762. fixed20_12 line_time;
  763. fixed20_12 src_width;
  764. fixed20_12 bandwidth;
  765. fixed20_12 a;
  766. a.full = dfixed_const(1000);
  767. line_time.full = dfixed_const(wm->active_time + wm->blank_time);
  768. line_time.full = dfixed_div(line_time, a);
  769. bpp.full = dfixed_const(wm->bytes_per_pixel);
  770. src_width.full = dfixed_const(wm->src_width);
  771. bandwidth.full = dfixed_mul(src_width, bpp);
  772. bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
  773. bandwidth.full = dfixed_div(bandwidth, line_time);
  774. return dfixed_trunc(bandwidth);
  775. }
  776. /**
  777. * dce_v6_0_latency_watermark - get the latency watermark
  778. *
  779. * @wm: watermark calculation data
  780. *
  781. * Calculate the latency watermark (CIK).
  782. * Used for display watermark bandwidth calculations
  783. * Returns the latency watermark in ns
  784. */
  785. static u32 dce_v6_0_latency_watermark(struct dce6_wm_params *wm)
  786. {
  787. /* First calculate the latency in ns */
  788. u32 mc_latency = 2000; /* 2000 ns. */
  789. u32 available_bandwidth = dce_v6_0_available_bandwidth(wm);
  790. u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
  791. u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
  792. u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
  793. u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
  794. (wm->num_heads * cursor_line_pair_return_time);
  795. u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
  796. u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
  797. u32 tmp, dmif_size = 12288;
  798. fixed20_12 a, b, c;
  799. if (wm->num_heads == 0)
  800. return 0;
  801. a.full = dfixed_const(2);
  802. b.full = dfixed_const(1);
  803. if ((wm->vsc.full > a.full) ||
  804. ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
  805. (wm->vtaps >= 5) ||
  806. ((wm->vsc.full >= a.full) && wm->interlaced))
  807. max_src_lines_per_dst_line = 4;
  808. else
  809. max_src_lines_per_dst_line = 2;
  810. a.full = dfixed_const(available_bandwidth);
  811. b.full = dfixed_const(wm->num_heads);
  812. a.full = dfixed_div(a, b);
  813. b.full = dfixed_const(mc_latency + 512);
  814. c.full = dfixed_const(wm->disp_clk);
  815. b.full = dfixed_div(b, c);
  816. c.full = dfixed_const(dmif_size);
  817. b.full = dfixed_div(c, b);
  818. tmp = min(dfixed_trunc(a), dfixed_trunc(b));
  819. b.full = dfixed_const(1000);
  820. c.full = dfixed_const(wm->disp_clk);
  821. b.full = dfixed_div(c, b);
  822. c.full = dfixed_const(wm->bytes_per_pixel);
  823. b.full = dfixed_mul(b, c);
  824. lb_fill_bw = min(tmp, dfixed_trunc(b));
  825. a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
  826. b.full = dfixed_const(1000);
  827. c.full = dfixed_const(lb_fill_bw);
  828. b.full = dfixed_div(c, b);
  829. a.full = dfixed_div(a, b);
  830. line_fill_time = dfixed_trunc(a);
  831. if (line_fill_time < wm->active_time)
  832. return latency;
  833. else
  834. return latency + (line_fill_time - wm->active_time);
  835. }
  836. /**
  837. * dce_v6_0_average_bandwidth_vs_dram_bandwidth_for_display - check
  838. * average and available dram bandwidth
  839. *
  840. * @wm: watermark calculation data
  841. *
  842. * Check if the display average bandwidth fits in the display
  843. * dram bandwidth (CIK).
  844. * Used for display watermark bandwidth calculations
  845. * Returns true if the display fits, false if not.
  846. */
  847. static bool dce_v6_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce6_wm_params *wm)
  848. {
  849. if (dce_v6_0_average_bandwidth(wm) <=
  850. (dce_v6_0_dram_bandwidth_for_display(wm) / wm->num_heads))
  851. return true;
  852. else
  853. return false;
  854. }
  855. /**
  856. * dce_v6_0_average_bandwidth_vs_available_bandwidth - check
  857. * average and available bandwidth
  858. *
  859. * @wm: watermark calculation data
  860. *
  861. * Check if the display average bandwidth fits in the display
  862. * available bandwidth (CIK).
  863. * Used for display watermark bandwidth calculations
  864. * Returns true if the display fits, false if not.
  865. */
  866. static bool dce_v6_0_average_bandwidth_vs_available_bandwidth(struct dce6_wm_params *wm)
  867. {
  868. if (dce_v6_0_average_bandwidth(wm) <=
  869. (dce_v6_0_available_bandwidth(wm) / wm->num_heads))
  870. return true;
  871. else
  872. return false;
  873. }
  874. /**
  875. * dce_v6_0_check_latency_hiding - check latency hiding
  876. *
  877. * @wm: watermark calculation data
  878. *
  879. * Check latency hiding (CIK).
  880. * Used for display watermark bandwidth calculations
  881. * Returns true if the display fits, false if not.
  882. */
  883. static bool dce_v6_0_check_latency_hiding(struct dce6_wm_params *wm)
  884. {
  885. u32 lb_partitions = wm->lb_size / wm->src_width;
  886. u32 line_time = wm->active_time + wm->blank_time;
  887. u32 latency_tolerant_lines;
  888. u32 latency_hiding;
  889. fixed20_12 a;
  890. a.full = dfixed_const(1);
  891. if (wm->vsc.full > a.full)
  892. latency_tolerant_lines = 1;
  893. else {
  894. if (lb_partitions <= (wm->vtaps + 1))
  895. latency_tolerant_lines = 1;
  896. else
  897. latency_tolerant_lines = 2;
  898. }
  899. latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
  900. if (dce_v6_0_latency_watermark(wm) <= latency_hiding)
  901. return true;
  902. else
  903. return false;
  904. }
  905. /**
  906. * dce_v6_0_program_watermarks - program display watermarks
  907. *
  908. * @adev: amdgpu_device pointer
  909. * @amdgpu_crtc: the selected display controller
  910. * @lb_size: line buffer size
  911. * @num_heads: number of display controllers in use
  912. *
  913. * Calculate and program the display watermarks for the
  914. * selected display controller (CIK).
  915. */
  916. static void dce_v6_0_program_watermarks(struct amdgpu_device *adev,
  917. struct amdgpu_crtc *amdgpu_crtc,
  918. u32 lb_size, u32 num_heads)
  919. {
  920. struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
  921. struct dce6_wm_params wm_low, wm_high;
  922. u32 dram_channels;
  923. u32 pixel_period;
  924. u32 line_time = 0;
  925. u32 latency_watermark_a = 0, latency_watermark_b = 0;
  926. u32 priority_a_mark = 0, priority_b_mark = 0;
  927. u32 priority_a_cnt = PRIORITY_OFF;
  928. u32 priority_b_cnt = PRIORITY_OFF;
  929. u32 tmp, arb_control3;
  930. fixed20_12 a, b, c;
  931. if (amdgpu_crtc->base.enabled && num_heads && mode) {
  932. pixel_period = 1000000 / (u32)mode->clock;
  933. line_time = min((u32)mode->crtc_htotal * pixel_period, (u32)65535);
  934. priority_a_cnt = 0;
  935. priority_b_cnt = 0;
  936. dram_channels = si_get_number_of_dram_channels(adev);
  937. /* watermark for high clocks */
  938. if (adev->pm.dpm_enabled) {
  939. wm_high.yclk =
  940. amdgpu_dpm_get_mclk(adev, false) * 10;
  941. wm_high.sclk =
  942. amdgpu_dpm_get_sclk(adev, false) * 10;
  943. } else {
  944. wm_high.yclk = adev->pm.current_mclk * 10;
  945. wm_high.sclk = adev->pm.current_sclk * 10;
  946. }
  947. wm_high.disp_clk = mode->clock;
  948. wm_high.src_width = mode->crtc_hdisplay;
  949. wm_high.active_time = mode->crtc_hdisplay * pixel_period;
  950. wm_high.blank_time = line_time - wm_high.active_time;
  951. wm_high.interlaced = false;
  952. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  953. wm_high.interlaced = true;
  954. wm_high.vsc = amdgpu_crtc->vsc;
  955. wm_high.vtaps = 1;
  956. if (amdgpu_crtc->rmx_type != RMX_OFF)
  957. wm_high.vtaps = 2;
  958. wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
  959. wm_high.lb_size = lb_size;
  960. wm_high.dram_channels = dram_channels;
  961. wm_high.num_heads = num_heads;
  962. if (adev->pm.dpm_enabled) {
  963. /* watermark for low clocks */
  964. wm_low.yclk =
  965. amdgpu_dpm_get_mclk(adev, true) * 10;
  966. wm_low.sclk =
  967. amdgpu_dpm_get_sclk(adev, true) * 10;
  968. } else {
  969. wm_low.yclk = adev->pm.current_mclk * 10;
  970. wm_low.sclk = adev->pm.current_sclk * 10;
  971. }
  972. wm_low.disp_clk = mode->clock;
  973. wm_low.src_width = mode->crtc_hdisplay;
  974. wm_low.active_time = mode->crtc_hdisplay * pixel_period;
  975. wm_low.blank_time = line_time - wm_low.active_time;
  976. wm_low.interlaced = false;
  977. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  978. wm_low.interlaced = true;
  979. wm_low.vsc = amdgpu_crtc->vsc;
  980. wm_low.vtaps = 1;
  981. if (amdgpu_crtc->rmx_type != RMX_OFF)
  982. wm_low.vtaps = 2;
  983. wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
  984. wm_low.lb_size = lb_size;
  985. wm_low.dram_channels = dram_channels;
  986. wm_low.num_heads = num_heads;
  987. /* set for high clocks */
  988. latency_watermark_a = min(dce_v6_0_latency_watermark(&wm_high), (u32)65535);
  989. /* set for low clocks */
  990. latency_watermark_b = min(dce_v6_0_latency_watermark(&wm_low), (u32)65535);
  991. /* possibly force display priority to high */
  992. /* should really do this at mode validation time... */
  993. if (!dce_v6_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
  994. !dce_v6_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
  995. !dce_v6_0_check_latency_hiding(&wm_high) ||
  996. (adev->mode_info.disp_priority == 2)) {
  997. DRM_DEBUG_KMS("force priority to high\n");
  998. priority_a_cnt |= PRIORITY_ALWAYS_ON;
  999. priority_b_cnt |= PRIORITY_ALWAYS_ON;
  1000. }
  1001. if (!dce_v6_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
  1002. !dce_v6_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
  1003. !dce_v6_0_check_latency_hiding(&wm_low) ||
  1004. (adev->mode_info.disp_priority == 2)) {
  1005. DRM_DEBUG_KMS("force priority to high\n");
  1006. priority_a_cnt |= PRIORITY_ALWAYS_ON;
  1007. priority_b_cnt |= PRIORITY_ALWAYS_ON;
  1008. }
  1009. a.full = dfixed_const(1000);
  1010. b.full = dfixed_const(mode->clock);
  1011. b.full = dfixed_div(b, a);
  1012. c.full = dfixed_const(latency_watermark_a);
  1013. c.full = dfixed_mul(c, b);
  1014. c.full = dfixed_mul(c, amdgpu_crtc->hsc);
  1015. c.full = dfixed_div(c, a);
  1016. a.full = dfixed_const(16);
  1017. c.full = dfixed_div(c, a);
  1018. priority_a_mark = dfixed_trunc(c);
  1019. priority_a_cnt |= priority_a_mark & PRIORITY_MARK_MASK;
  1020. a.full = dfixed_const(1000);
  1021. b.full = dfixed_const(mode->clock);
  1022. b.full = dfixed_div(b, a);
  1023. c.full = dfixed_const(latency_watermark_b);
  1024. c.full = dfixed_mul(c, b);
  1025. c.full = dfixed_mul(c, amdgpu_crtc->hsc);
  1026. c.full = dfixed_div(c, a);
  1027. a.full = dfixed_const(16);
  1028. c.full = dfixed_div(c, a);
  1029. priority_b_mark = dfixed_trunc(c);
  1030. priority_b_cnt |= priority_b_mark & PRIORITY_MARK_MASK;
  1031. }
  1032. /* select wm A */
  1033. arb_control3 = RREG32(DPG_PIPE_ARBITRATION_CONTROL3 + amdgpu_crtc->crtc_offset);
  1034. tmp = arb_control3;
  1035. tmp &= ~LATENCY_WATERMARK_MASK(3);
  1036. tmp |= LATENCY_WATERMARK_MASK(1);
  1037. WREG32(DPG_PIPE_ARBITRATION_CONTROL3 + amdgpu_crtc->crtc_offset, tmp);
  1038. WREG32(DPG_PIPE_LATENCY_CONTROL + amdgpu_crtc->crtc_offset,
  1039. (LATENCY_LOW_WATERMARK(latency_watermark_a) |
  1040. LATENCY_HIGH_WATERMARK(line_time)));
  1041. /* select wm B */
  1042. tmp = RREG32(DPG_PIPE_ARBITRATION_CONTROL3 + amdgpu_crtc->crtc_offset);
  1043. tmp &= ~LATENCY_WATERMARK_MASK(3);
  1044. tmp |= LATENCY_WATERMARK_MASK(2);
  1045. WREG32(DPG_PIPE_ARBITRATION_CONTROL3 + amdgpu_crtc->crtc_offset, tmp);
  1046. WREG32(DPG_PIPE_LATENCY_CONTROL + amdgpu_crtc->crtc_offset,
  1047. (LATENCY_LOW_WATERMARK(latency_watermark_b) |
  1048. LATENCY_HIGH_WATERMARK(line_time)));
  1049. /* restore original selection */
  1050. WREG32(DPG_PIPE_ARBITRATION_CONTROL3 + amdgpu_crtc->crtc_offset, arb_control3);
  1051. /* write the priority marks */
  1052. WREG32(PRIORITY_A_CNT + amdgpu_crtc->crtc_offset, priority_a_cnt);
  1053. WREG32(PRIORITY_B_CNT + amdgpu_crtc->crtc_offset, priority_b_cnt);
  1054. /* save values for DPM */
  1055. amdgpu_crtc->line_time = line_time;
  1056. amdgpu_crtc->wm_high = latency_watermark_a;
  1057. }
  1058. /* watermark setup */
  1059. static u32 dce_v6_0_line_buffer_adjust(struct amdgpu_device *adev,
  1060. struct amdgpu_crtc *amdgpu_crtc,
  1061. struct drm_display_mode *mode,
  1062. struct drm_display_mode *other_mode)
  1063. {
  1064. u32 tmp, buffer_alloc, i;
  1065. u32 pipe_offset = amdgpu_crtc->crtc_id * 0x8;
  1066. /*
  1067. * Line Buffer Setup
  1068. * There are 3 line buffers, each one shared by 2 display controllers.
  1069. * DC_LB_MEMORY_SPLIT controls how that line buffer is shared between
  1070. * the display controllers. The paritioning is done via one of four
  1071. * preset allocations specified in bits 21:20:
  1072. * 0 - half lb
  1073. * 2 - whole lb, other crtc must be disabled
  1074. */
  1075. /* this can get tricky if we have two large displays on a paired group
  1076. * of crtcs. Ideally for multiple large displays we'd assign them to
  1077. * non-linked crtcs for maximum line buffer allocation.
  1078. */
  1079. if (amdgpu_crtc->base.enabled && mode) {
  1080. if (other_mode) {
  1081. tmp = 0; /* 1/2 */
  1082. buffer_alloc = 1;
  1083. } else {
  1084. tmp = 2; /* whole */
  1085. buffer_alloc = 2;
  1086. }
  1087. } else {
  1088. tmp = 0;
  1089. buffer_alloc = 0;
  1090. }
  1091. WREG32(DC_LB_MEMORY_SPLIT + amdgpu_crtc->crtc_offset,
  1092. DC_LB_MEMORY_CONFIG(tmp));
  1093. WREG32(PIPE0_DMIF_BUFFER_CONTROL + pipe_offset,
  1094. DMIF_BUFFERS_ALLOCATED(buffer_alloc));
  1095. for (i = 0; i < adev->usec_timeout; i++) {
  1096. if (RREG32(PIPE0_DMIF_BUFFER_CONTROL + pipe_offset) &
  1097. DMIF_BUFFERS_ALLOCATED_COMPLETED)
  1098. break;
  1099. udelay(1);
  1100. }
  1101. if (amdgpu_crtc->base.enabled && mode) {
  1102. switch (tmp) {
  1103. case 0:
  1104. default:
  1105. return 4096 * 2;
  1106. case 2:
  1107. return 8192 * 2;
  1108. }
  1109. }
  1110. /* controller not enabled, so no lb used */
  1111. return 0;
  1112. }
  1113. /**
  1114. *
  1115. * dce_v6_0_bandwidth_update - program display watermarks
  1116. *
  1117. * @adev: amdgpu_device pointer
  1118. *
  1119. * Calculate and program the display watermarks and line
  1120. * buffer allocation (CIK).
  1121. */
  1122. static void dce_v6_0_bandwidth_update(struct amdgpu_device *adev)
  1123. {
  1124. struct drm_display_mode *mode0 = NULL;
  1125. struct drm_display_mode *mode1 = NULL;
  1126. u32 num_heads = 0, lb_size;
  1127. int i;
  1128. if (!adev->mode_info.mode_config_initialized)
  1129. return;
  1130. amdgpu_update_display_priority(adev);
  1131. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1132. if (adev->mode_info.crtcs[i]->base.enabled)
  1133. num_heads++;
  1134. }
  1135. for (i = 0; i < adev->mode_info.num_crtc; i += 2) {
  1136. mode0 = &adev->mode_info.crtcs[i]->base.mode;
  1137. mode1 = &adev->mode_info.crtcs[i+1]->base.mode;
  1138. lb_size = dce_v6_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode0, mode1);
  1139. dce_v6_0_program_watermarks(adev, adev->mode_info.crtcs[i], lb_size, num_heads);
  1140. lb_size = dce_v6_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i+1], mode1, mode0);
  1141. dce_v6_0_program_watermarks(adev, adev->mode_info.crtcs[i+1], lb_size, num_heads);
  1142. }
  1143. }
  1144. /*
  1145. static void dce_v6_0_audio_get_connected_pins(struct amdgpu_device *adev)
  1146. {
  1147. int i;
  1148. u32 offset, tmp;
  1149. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1150. offset = adev->mode_info.audio.pin[i].offset;
  1151. tmp = RREG32_AUDIO_ENDPT(offset,
  1152. AZ_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
  1153. if (((tmp & PORT_CONNECTIVITY_MASK) >> PORT_CONNECTIVITY_SHIFT) == 1)
  1154. adev->mode_info.audio.pin[i].connected = false;
  1155. else
  1156. adev->mode_info.audio.pin[i].connected = true;
  1157. }
  1158. }
  1159. static struct amdgpu_audio_pin *dce_v6_0_audio_get_pin(struct amdgpu_device *adev)
  1160. {
  1161. int i;
  1162. dce_v6_0_audio_get_connected_pins(adev);
  1163. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1164. if (adev->mode_info.audio.pin[i].connected)
  1165. return &adev->mode_info.audio.pin[i];
  1166. }
  1167. DRM_ERROR("No connected audio pins found!\n");
  1168. return NULL;
  1169. }
  1170. static void dce_v6_0_afmt_audio_select_pin(struct drm_encoder *encoder)
  1171. {
  1172. struct amdgpu_device *adev = encoder->dev->dev_private;
  1173. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1174. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1175. u32 offset;
  1176. if (!dig || !dig->afmt || !dig->afmt->pin)
  1177. return;
  1178. offset = dig->afmt->offset;
  1179. WREG32(AFMT_AUDIO_SRC_CONTROL + offset,
  1180. AFMT_AUDIO_SRC_SELECT(dig->afmt->pin->id));
  1181. }
  1182. static void dce_v6_0_audio_write_latency_fields(struct drm_encoder *encoder,
  1183. struct drm_display_mode *mode)
  1184. {
  1185. DRM_INFO("xxxx: dce_v6_0_audio_write_latency_fields---no imp!!!!!\n");
  1186. }
  1187. static void dce_v6_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
  1188. {
  1189. DRM_INFO("xxxx: dce_v6_0_audio_write_speaker_allocation---no imp!!!!!\n");
  1190. }
  1191. static void dce_v6_0_audio_write_sad_regs(struct drm_encoder *encoder)
  1192. {
  1193. DRM_INFO("xxxx: dce_v6_0_audio_write_sad_regs---no imp!!!!!\n");
  1194. }
  1195. */
  1196. static void dce_v6_0_audio_enable(struct amdgpu_device *adev,
  1197. struct amdgpu_audio_pin *pin,
  1198. bool enable)
  1199. {
  1200. DRM_INFO("xxxx: dce_v6_0_audio_enable---no imp!!!!!\n");
  1201. }
  1202. static const u32 pin_offsets[7] =
  1203. {
  1204. (0x1780 - 0x1780),
  1205. (0x1786 - 0x1780),
  1206. (0x178c - 0x1780),
  1207. (0x1792 - 0x1780),
  1208. (0x1798 - 0x1780),
  1209. (0x179d - 0x1780),
  1210. (0x17a4 - 0x1780),
  1211. };
  1212. static int dce_v6_0_audio_init(struct amdgpu_device *adev)
  1213. {
  1214. return 0;
  1215. }
  1216. static void dce_v6_0_audio_fini(struct amdgpu_device *adev)
  1217. {
  1218. }
  1219. /*
  1220. static void dce_v6_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
  1221. {
  1222. DRM_INFO("xxxx: dce_v6_0_afmt_update_ACR---no imp!!!!!\n");
  1223. }
  1224. */
  1225. /*
  1226. * build a HDMI Video Info Frame
  1227. */
  1228. /*
  1229. static void dce_v6_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
  1230. void *buffer, size_t size)
  1231. {
  1232. DRM_INFO("xxxx: dce_v6_0_afmt_update_avi_infoframe---no imp!!!!!\n");
  1233. }
  1234. static void dce_v6_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
  1235. {
  1236. DRM_INFO("xxxx: dce_v6_0_audio_set_dto---no imp!!!!!\n");
  1237. }
  1238. */
  1239. /*
  1240. * update the info frames with the data from the current display mode
  1241. */
  1242. static void dce_v6_0_afmt_setmode(struct drm_encoder *encoder,
  1243. struct drm_display_mode *mode)
  1244. {
  1245. DRM_INFO("xxxx: dce_v6_0_afmt_setmode ----no impl !!!!!!!!\n");
  1246. }
  1247. static void dce_v6_0_afmt_enable(struct drm_encoder *encoder, bool enable)
  1248. {
  1249. struct drm_device *dev = encoder->dev;
  1250. struct amdgpu_device *adev = dev->dev_private;
  1251. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1252. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1253. if (!dig || !dig->afmt)
  1254. return;
  1255. /* Silent, r600_hdmi_enable will raise WARN for us */
  1256. if (enable && dig->afmt->enabled)
  1257. return;
  1258. if (!enable && !dig->afmt->enabled)
  1259. return;
  1260. if (!enable && dig->afmt->pin) {
  1261. dce_v6_0_audio_enable(adev, dig->afmt->pin, false);
  1262. dig->afmt->pin = NULL;
  1263. }
  1264. dig->afmt->enabled = enable;
  1265. DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
  1266. enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
  1267. }
  1268. static int dce_v6_0_afmt_init(struct amdgpu_device *adev)
  1269. {
  1270. int i, j;
  1271. for (i = 0; i < adev->mode_info.num_dig; i++)
  1272. adev->mode_info.afmt[i] = NULL;
  1273. /* DCE6 has audio blocks tied to DIG encoders */
  1274. for (i = 0; i < adev->mode_info.num_dig; i++) {
  1275. adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
  1276. if (adev->mode_info.afmt[i]) {
  1277. adev->mode_info.afmt[i]->offset = dig_offsets[i];
  1278. adev->mode_info.afmt[i]->id = i;
  1279. } else {
  1280. for (j = 0; j < i; j++) {
  1281. kfree(adev->mode_info.afmt[j]);
  1282. adev->mode_info.afmt[j] = NULL;
  1283. }
  1284. DRM_ERROR("Out of memory allocating afmt table\n");
  1285. return -ENOMEM;
  1286. }
  1287. }
  1288. return 0;
  1289. }
  1290. static void dce_v6_0_afmt_fini(struct amdgpu_device *adev)
  1291. {
  1292. int i;
  1293. for (i = 0; i < adev->mode_info.num_dig; i++) {
  1294. kfree(adev->mode_info.afmt[i]);
  1295. adev->mode_info.afmt[i] = NULL;
  1296. }
  1297. }
  1298. static const u32 vga_control_regs[6] =
  1299. {
  1300. AVIVO_D1VGA_CONTROL,
  1301. AVIVO_D2VGA_CONTROL,
  1302. EVERGREEN_D3VGA_CONTROL,
  1303. EVERGREEN_D4VGA_CONTROL,
  1304. EVERGREEN_D5VGA_CONTROL,
  1305. EVERGREEN_D6VGA_CONTROL,
  1306. };
  1307. static void dce_v6_0_vga_enable(struct drm_crtc *crtc, bool enable)
  1308. {
  1309. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1310. struct drm_device *dev = crtc->dev;
  1311. struct amdgpu_device *adev = dev->dev_private;
  1312. u32 vga_control;
  1313. vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
  1314. if (enable)
  1315. WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
  1316. else
  1317. WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
  1318. }
  1319. static void dce_v6_0_grph_enable(struct drm_crtc *crtc, bool enable)
  1320. {
  1321. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1322. struct drm_device *dev = crtc->dev;
  1323. struct amdgpu_device *adev = dev->dev_private;
  1324. if (enable)
  1325. WREG32(EVERGREEN_GRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
  1326. else
  1327. WREG32(EVERGREEN_GRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
  1328. }
  1329. static int dce_v6_0_crtc_do_set_base(struct drm_crtc *crtc,
  1330. struct drm_framebuffer *fb,
  1331. int x, int y, int atomic)
  1332. {
  1333. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1334. struct drm_device *dev = crtc->dev;
  1335. struct amdgpu_device *adev = dev->dev_private;
  1336. struct amdgpu_framebuffer *amdgpu_fb;
  1337. struct drm_framebuffer *target_fb;
  1338. struct drm_gem_object *obj;
  1339. struct amdgpu_bo *rbo;
  1340. uint64_t fb_location, tiling_flags;
  1341. uint32_t fb_format, fb_pitch_pixels, pipe_config;
  1342. u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
  1343. u32 viewport_w, viewport_h;
  1344. int r;
  1345. bool bypass_lut = false;
  1346. /* no fb bound */
  1347. if (!atomic && !crtc->primary->fb) {
  1348. DRM_DEBUG_KMS("No FB bound\n");
  1349. return 0;
  1350. }
  1351. if (atomic) {
  1352. amdgpu_fb = to_amdgpu_framebuffer(fb);
  1353. target_fb = fb;
  1354. }
  1355. else {
  1356. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  1357. target_fb = crtc->primary->fb;
  1358. }
  1359. /* If atomic, assume fb object is pinned & idle & fenced and
  1360. * just update base pointers
  1361. */
  1362. obj = amdgpu_fb->obj;
  1363. rbo = gem_to_amdgpu_bo(obj);
  1364. r = amdgpu_bo_reserve(rbo, false);
  1365. if (unlikely(r != 0))
  1366. return r;
  1367. if (atomic)
  1368. fb_location = amdgpu_bo_gpu_offset(rbo);
  1369. else {
  1370. r = amdgpu_bo_pin(rbo, AMDGPU_GEM_DOMAIN_VRAM, &fb_location);
  1371. if (unlikely(r != 0)) {
  1372. amdgpu_bo_unreserve(rbo);
  1373. return -EINVAL;
  1374. }
  1375. }
  1376. amdgpu_bo_get_tiling_flags(rbo, &tiling_flags);
  1377. amdgpu_bo_unreserve(rbo);
  1378. switch (target_fb->pixel_format) {
  1379. case DRM_FORMAT_C8:
  1380. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
  1381. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
  1382. break;
  1383. case DRM_FORMAT_XRGB4444:
  1384. case DRM_FORMAT_ARGB4444:
  1385. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
  1386. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB4444));
  1387. #ifdef __BIG_ENDIAN
  1388. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
  1389. #endif
  1390. break;
  1391. case DRM_FORMAT_XRGB1555:
  1392. case DRM_FORMAT_ARGB1555:
  1393. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
  1394. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
  1395. #ifdef __BIG_ENDIAN
  1396. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
  1397. #endif
  1398. break;
  1399. case DRM_FORMAT_BGRX5551:
  1400. case DRM_FORMAT_BGRA5551:
  1401. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
  1402. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_BGRA5551));
  1403. #ifdef __BIG_ENDIAN
  1404. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
  1405. #endif
  1406. break;
  1407. case DRM_FORMAT_RGB565:
  1408. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
  1409. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
  1410. #ifdef __BIG_ENDIAN
  1411. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
  1412. #endif
  1413. break;
  1414. case DRM_FORMAT_XRGB8888:
  1415. case DRM_FORMAT_ARGB8888:
  1416. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
  1417. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
  1418. #ifdef __BIG_ENDIAN
  1419. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
  1420. #endif
  1421. break;
  1422. case DRM_FORMAT_XRGB2101010:
  1423. case DRM_FORMAT_ARGB2101010:
  1424. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
  1425. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB2101010));
  1426. #ifdef __BIG_ENDIAN
  1427. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
  1428. #endif
  1429. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1430. bypass_lut = true;
  1431. break;
  1432. case DRM_FORMAT_BGRX1010102:
  1433. case DRM_FORMAT_BGRA1010102:
  1434. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
  1435. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_BGRA1010102));
  1436. #ifdef __BIG_ENDIAN
  1437. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
  1438. #endif
  1439. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1440. bypass_lut = true;
  1441. break;
  1442. default:
  1443. DRM_ERROR("Unsupported screen format %s\n",
  1444. drm_get_format_name(target_fb->pixel_format));
  1445. return -EINVAL;
  1446. }
  1447. if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
  1448. unsigned bankw, bankh, mtaspect, tile_split, num_banks;
  1449. bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
  1450. bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
  1451. mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
  1452. tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
  1453. num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
  1454. fb_format |= EVERGREEN_GRPH_NUM_BANKS(num_banks);
  1455. fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
  1456. fb_format |= EVERGREEN_GRPH_TILE_SPLIT(tile_split);
  1457. fb_format |= EVERGREEN_GRPH_BANK_WIDTH(bankw);
  1458. fb_format |= EVERGREEN_GRPH_BANK_HEIGHT(bankh);
  1459. fb_format |= EVERGREEN_GRPH_MACRO_TILE_ASPECT(mtaspect);
  1460. } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1)
  1461. fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
  1462. pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
  1463. fb_format |= SI_GRPH_PIPE_CONFIG(pipe_config);
  1464. dce_v6_0_vga_enable(crtc, false);
  1465. /* Make sure surface address is updated at vertical blank rather than
  1466. * horizontal blank
  1467. */
  1468. WREG32(EVERGREEN_GRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, 0);
  1469. WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  1470. upper_32_bits(fb_location));
  1471. WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  1472. upper_32_bits(fb_location));
  1473. WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  1474. (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
  1475. WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  1476. (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
  1477. WREG32(EVERGREEN_GRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
  1478. WREG32(EVERGREEN_GRPH_SWAP_CONTROL + amdgpu_crtc->crtc_offset, fb_swap);
  1479. /*
  1480. * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
  1481. * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
  1482. * retain the full precision throughout the pipeline.
  1483. */
  1484. WREG32_P(EVERGREEN_GRPH_LUT_10BIT_BYPASS_CONTROL + amdgpu_crtc->crtc_offset,
  1485. (bypass_lut ? EVERGREEN_LUT_10BIT_BYPASS_EN : 0),
  1486. ~EVERGREEN_LUT_10BIT_BYPASS_EN);
  1487. if (bypass_lut)
  1488. DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
  1489. WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
  1490. WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
  1491. WREG32(EVERGREEN_GRPH_X_START + amdgpu_crtc->crtc_offset, 0);
  1492. WREG32(EVERGREEN_GRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
  1493. WREG32(EVERGREEN_GRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
  1494. WREG32(EVERGREEN_GRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
  1495. fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
  1496. WREG32(EVERGREEN_GRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
  1497. dce_v6_0_grph_enable(crtc, true);
  1498. WREG32(EVERGREEN_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
  1499. target_fb->height);
  1500. x &= ~3;
  1501. y &= ~1;
  1502. WREG32(EVERGREEN_VIEWPORT_START + amdgpu_crtc->crtc_offset,
  1503. (x << 16) | y);
  1504. viewport_w = crtc->mode.hdisplay;
  1505. viewport_h = (crtc->mode.vdisplay + 1) & ~1;
  1506. WREG32(EVERGREEN_VIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
  1507. (viewport_w << 16) | viewport_h);
  1508. /* set pageflip to happen anywhere in vblank interval */
  1509. WREG32(EVERGREEN_MASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 0);
  1510. if (!atomic && fb && fb != crtc->primary->fb) {
  1511. amdgpu_fb = to_amdgpu_framebuffer(fb);
  1512. rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  1513. r = amdgpu_bo_reserve(rbo, false);
  1514. if (unlikely(r != 0))
  1515. return r;
  1516. amdgpu_bo_unpin(rbo);
  1517. amdgpu_bo_unreserve(rbo);
  1518. }
  1519. /* Bytes per pixel may have changed */
  1520. dce_v6_0_bandwidth_update(adev);
  1521. return 0;
  1522. }
  1523. static void dce_v6_0_set_interleave(struct drm_crtc *crtc,
  1524. struct drm_display_mode *mode)
  1525. {
  1526. struct drm_device *dev = crtc->dev;
  1527. struct amdgpu_device *adev = dev->dev_private;
  1528. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1529. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1530. WREG32(EVERGREEN_DATA_FORMAT + amdgpu_crtc->crtc_offset,
  1531. EVERGREEN_INTERLEAVE_EN);
  1532. else
  1533. WREG32(EVERGREEN_DATA_FORMAT + amdgpu_crtc->crtc_offset, 0);
  1534. }
  1535. static void dce_v6_0_crtc_load_lut(struct drm_crtc *crtc)
  1536. {
  1537. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1538. struct drm_device *dev = crtc->dev;
  1539. struct amdgpu_device *adev = dev->dev_private;
  1540. int i;
  1541. DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
  1542. WREG32(NI_INPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset,
  1543. (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
  1544. NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
  1545. WREG32(NI_PRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset,
  1546. NI_GRPH_PRESCALE_BYPASS);
  1547. WREG32(NI_PRESCALE_OVL_CONTROL + amdgpu_crtc->crtc_offset,
  1548. NI_OVL_PRESCALE_BYPASS);
  1549. WREG32(NI_INPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset,
  1550. (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
  1551. NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
  1552. WREG32(EVERGREEN_DC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
  1553. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
  1554. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
  1555. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
  1556. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
  1557. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
  1558. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
  1559. WREG32(EVERGREEN_DC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
  1560. WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
  1561. WREG32(EVERGREEN_DC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
  1562. for (i = 0; i < 256; i++) {
  1563. WREG32(EVERGREEN_DC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
  1564. (amdgpu_crtc->lut_r[i] << 20) |
  1565. (amdgpu_crtc->lut_g[i] << 10) |
  1566. (amdgpu_crtc->lut_b[i] << 0));
  1567. }
  1568. WREG32(NI_DEGAMMA_CONTROL + amdgpu_crtc->crtc_offset,
  1569. (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  1570. NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  1571. NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  1572. NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
  1573. WREG32(NI_GAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset,
  1574. (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
  1575. NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
  1576. WREG32(NI_REGAMMA_CONTROL + amdgpu_crtc->crtc_offset,
  1577. (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
  1578. NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
  1579. WREG32(NI_OUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset,
  1580. (NI_OUTPUT_CSC_GRPH_MODE(0) |
  1581. NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
  1582. /* XXX match this to the depth of the crtc fmt block, move to modeset? */
  1583. WREG32(0x1a50 + amdgpu_crtc->crtc_offset, 0);
  1584. }
  1585. static int dce_v6_0_pick_dig_encoder(struct drm_encoder *encoder)
  1586. {
  1587. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1588. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1589. switch (amdgpu_encoder->encoder_id) {
  1590. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  1591. if (dig->linkb)
  1592. return 1;
  1593. else
  1594. return 0;
  1595. break;
  1596. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  1597. if (dig->linkb)
  1598. return 3;
  1599. else
  1600. return 2;
  1601. break;
  1602. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  1603. if (dig->linkb)
  1604. return 5;
  1605. else
  1606. return 4;
  1607. break;
  1608. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
  1609. return 6;
  1610. break;
  1611. default:
  1612. DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
  1613. return 0;
  1614. }
  1615. }
  1616. /**
  1617. * dce_v6_0_pick_pll - Allocate a PPLL for use by the crtc.
  1618. *
  1619. * @crtc: drm crtc
  1620. *
  1621. * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
  1622. * a single PPLL can be used for all DP crtcs/encoders. For non-DP
  1623. * monitors a dedicated PPLL must be used. If a particular board has
  1624. * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
  1625. * as there is no need to program the PLL itself. If we are not able to
  1626. * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
  1627. * avoid messing up an existing monitor.
  1628. *
  1629. *
  1630. */
  1631. static u32 dce_v6_0_pick_pll(struct drm_crtc *crtc)
  1632. {
  1633. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1634. struct drm_device *dev = crtc->dev;
  1635. struct amdgpu_device *adev = dev->dev_private;
  1636. u32 pll_in_use;
  1637. int pll;
  1638. if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
  1639. if (adev->clock.dp_extclk)
  1640. /* skip PPLL programming if using ext clock */
  1641. return ATOM_PPLL_INVALID;
  1642. else
  1643. return ATOM_PPLL0;
  1644. } else {
  1645. /* use the same PPLL for all monitors with the same clock */
  1646. pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
  1647. if (pll != ATOM_PPLL_INVALID)
  1648. return pll;
  1649. }
  1650. /* PPLL1, and PPLL2 */
  1651. pll_in_use = amdgpu_pll_get_use_mask(crtc);
  1652. if (!(pll_in_use & (1 << ATOM_PPLL2)))
  1653. return ATOM_PPLL2;
  1654. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  1655. return ATOM_PPLL1;
  1656. DRM_ERROR("unable to allocate a PPLL\n");
  1657. return ATOM_PPLL_INVALID;
  1658. }
  1659. static void dce_v6_0_lock_cursor(struct drm_crtc *crtc, bool lock)
  1660. {
  1661. struct amdgpu_device *adev = crtc->dev->dev_private;
  1662. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1663. uint32_t cur_lock;
  1664. cur_lock = RREG32(EVERGREEN_CUR_UPDATE + amdgpu_crtc->crtc_offset);
  1665. if (lock)
  1666. cur_lock |= EVERGREEN_CURSOR_UPDATE_LOCK;
  1667. else
  1668. cur_lock &= ~EVERGREEN_CURSOR_UPDATE_LOCK;
  1669. WREG32(EVERGREEN_CUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
  1670. }
  1671. static void dce_v6_0_hide_cursor(struct drm_crtc *crtc)
  1672. {
  1673. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1674. struct amdgpu_device *adev = crtc->dev->dev_private;
  1675. WREG32_IDX(EVERGREEN_CUR_CONTROL + amdgpu_crtc->crtc_offset,
  1676. EVERGREEN_CURSOR_MODE(EVERGREEN_CURSOR_24_8_PRE_MULT) |
  1677. EVERGREEN_CURSOR_URGENT_CONTROL(EVERGREEN_CURSOR_URGENT_1_2));
  1678. }
  1679. static void dce_v6_0_show_cursor(struct drm_crtc *crtc)
  1680. {
  1681. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1682. struct amdgpu_device *adev = crtc->dev->dev_private;
  1683. WREG32(EVERGREEN_CUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  1684. upper_32_bits(amdgpu_crtc->cursor_addr));
  1685. WREG32(EVERGREEN_CUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  1686. lower_32_bits(amdgpu_crtc->cursor_addr));
  1687. WREG32_IDX(EVERGREEN_CUR_CONTROL + amdgpu_crtc->crtc_offset,
  1688. EVERGREEN_CURSOR_EN |
  1689. EVERGREEN_CURSOR_MODE(EVERGREEN_CURSOR_24_8_PRE_MULT) |
  1690. EVERGREEN_CURSOR_URGENT_CONTROL(EVERGREEN_CURSOR_URGENT_1_2));
  1691. }
  1692. static int dce_v6_0_cursor_move_locked(struct drm_crtc *crtc,
  1693. int x, int y)
  1694. {
  1695. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1696. struct amdgpu_device *adev = crtc->dev->dev_private;
  1697. int xorigin = 0, yorigin = 0;
  1698. int w = amdgpu_crtc->cursor_width;
  1699. /* avivo cursor are offset into the total surface */
  1700. x += crtc->x;
  1701. y += crtc->y;
  1702. DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
  1703. if (x < 0) {
  1704. xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
  1705. x = 0;
  1706. }
  1707. if (y < 0) {
  1708. yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
  1709. y = 0;
  1710. }
  1711. WREG32(EVERGREEN_CUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
  1712. WREG32(EVERGREEN_CUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
  1713. WREG32(EVERGREEN_CUR_SIZE + amdgpu_crtc->crtc_offset,
  1714. ((w - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
  1715. amdgpu_crtc->cursor_x = x;
  1716. amdgpu_crtc->cursor_y = y;
  1717. return 0;
  1718. }
  1719. static int dce_v6_0_crtc_cursor_move(struct drm_crtc *crtc,
  1720. int x, int y)
  1721. {
  1722. int ret;
  1723. dce_v6_0_lock_cursor(crtc, true);
  1724. ret = dce_v6_0_cursor_move_locked(crtc, x, y);
  1725. dce_v6_0_lock_cursor(crtc, false);
  1726. return ret;
  1727. }
  1728. static int dce_v6_0_crtc_cursor_set2(struct drm_crtc *crtc,
  1729. struct drm_file *file_priv,
  1730. uint32_t handle,
  1731. uint32_t width,
  1732. uint32_t height,
  1733. int32_t hot_x,
  1734. int32_t hot_y)
  1735. {
  1736. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1737. struct drm_gem_object *obj;
  1738. struct amdgpu_bo *aobj;
  1739. int ret;
  1740. if (!handle) {
  1741. /* turn off cursor */
  1742. dce_v6_0_hide_cursor(crtc);
  1743. obj = NULL;
  1744. goto unpin;
  1745. }
  1746. if ((width > amdgpu_crtc->max_cursor_width) ||
  1747. (height > amdgpu_crtc->max_cursor_height)) {
  1748. DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
  1749. return -EINVAL;
  1750. }
  1751. obj = drm_gem_object_lookup(file_priv, handle);
  1752. if (!obj) {
  1753. DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
  1754. return -ENOENT;
  1755. }
  1756. aobj = gem_to_amdgpu_bo(obj);
  1757. ret = amdgpu_bo_reserve(aobj, false);
  1758. if (ret != 0) {
  1759. drm_gem_object_unreference_unlocked(obj);
  1760. return ret;
  1761. }
  1762. ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM, &amdgpu_crtc->cursor_addr);
  1763. amdgpu_bo_unreserve(aobj);
  1764. if (ret) {
  1765. DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
  1766. drm_gem_object_unreference_unlocked(obj);
  1767. return ret;
  1768. }
  1769. amdgpu_crtc->cursor_width = width;
  1770. amdgpu_crtc->cursor_height = height;
  1771. dce_v6_0_lock_cursor(crtc, true);
  1772. if (hot_x != amdgpu_crtc->cursor_hot_x ||
  1773. hot_y != amdgpu_crtc->cursor_hot_y) {
  1774. int x, y;
  1775. x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
  1776. y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
  1777. dce_v6_0_cursor_move_locked(crtc, x, y);
  1778. amdgpu_crtc->cursor_hot_x = hot_x;
  1779. amdgpu_crtc->cursor_hot_y = hot_y;
  1780. }
  1781. dce_v6_0_show_cursor(crtc);
  1782. dce_v6_0_lock_cursor(crtc, false);
  1783. unpin:
  1784. if (amdgpu_crtc->cursor_bo) {
  1785. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  1786. ret = amdgpu_bo_reserve(aobj, false);
  1787. if (likely(ret == 0)) {
  1788. amdgpu_bo_unpin(aobj);
  1789. amdgpu_bo_unreserve(aobj);
  1790. }
  1791. drm_gem_object_unreference_unlocked(amdgpu_crtc->cursor_bo);
  1792. }
  1793. amdgpu_crtc->cursor_bo = obj;
  1794. return 0;
  1795. }
  1796. static void dce_v6_0_cursor_reset(struct drm_crtc *crtc)
  1797. {
  1798. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1799. if (amdgpu_crtc->cursor_bo) {
  1800. dce_v6_0_lock_cursor(crtc, true);
  1801. dce_v6_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
  1802. amdgpu_crtc->cursor_y);
  1803. dce_v6_0_show_cursor(crtc);
  1804. dce_v6_0_lock_cursor(crtc, false);
  1805. }
  1806. }
  1807. static int dce_v6_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  1808. u16 *blue, uint32_t size)
  1809. {
  1810. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1811. int i;
  1812. /* userspace palettes are always correct as is */
  1813. for (i = 0; i < size; i++) {
  1814. amdgpu_crtc->lut_r[i] = red[i] >> 6;
  1815. amdgpu_crtc->lut_g[i] = green[i] >> 6;
  1816. amdgpu_crtc->lut_b[i] = blue[i] >> 6;
  1817. }
  1818. dce_v6_0_crtc_load_lut(crtc);
  1819. return 0;
  1820. }
  1821. static void dce_v6_0_crtc_destroy(struct drm_crtc *crtc)
  1822. {
  1823. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1824. drm_crtc_cleanup(crtc);
  1825. kfree(amdgpu_crtc);
  1826. }
  1827. static const struct drm_crtc_funcs dce_v6_0_crtc_funcs = {
  1828. .cursor_set2 = dce_v6_0_crtc_cursor_set2,
  1829. .cursor_move = dce_v6_0_crtc_cursor_move,
  1830. .gamma_set = dce_v6_0_crtc_gamma_set,
  1831. .set_config = amdgpu_crtc_set_config,
  1832. .destroy = dce_v6_0_crtc_destroy,
  1833. .page_flip_target = amdgpu_crtc_page_flip_target,
  1834. };
  1835. static void dce_v6_0_crtc_dpms(struct drm_crtc *crtc, int mode)
  1836. {
  1837. struct drm_device *dev = crtc->dev;
  1838. struct amdgpu_device *adev = dev->dev_private;
  1839. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1840. unsigned type;
  1841. switch (mode) {
  1842. case DRM_MODE_DPMS_ON:
  1843. amdgpu_crtc->enabled = true;
  1844. amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
  1845. amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
  1846. /* Make sure VBLANK and PFLIP interrupts are still enabled */
  1847. type = amdgpu_crtc_idx_to_irq_type(adev, amdgpu_crtc->crtc_id);
  1848. amdgpu_irq_update(adev, &adev->crtc_irq, type);
  1849. amdgpu_irq_update(adev, &adev->pageflip_irq, type);
  1850. drm_vblank_post_modeset(dev, amdgpu_crtc->crtc_id);
  1851. dce_v6_0_crtc_load_lut(crtc);
  1852. break;
  1853. case DRM_MODE_DPMS_STANDBY:
  1854. case DRM_MODE_DPMS_SUSPEND:
  1855. case DRM_MODE_DPMS_OFF:
  1856. drm_vblank_pre_modeset(dev, amdgpu_crtc->crtc_id);
  1857. if (amdgpu_crtc->enabled)
  1858. amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
  1859. amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
  1860. amdgpu_crtc->enabled = false;
  1861. break;
  1862. }
  1863. /* adjust pm to dpms */
  1864. amdgpu_pm_compute_clocks(adev);
  1865. }
  1866. static void dce_v6_0_crtc_prepare(struct drm_crtc *crtc)
  1867. {
  1868. /* disable crtc pair power gating before programming */
  1869. amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
  1870. amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
  1871. dce_v6_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  1872. }
  1873. static void dce_v6_0_crtc_commit(struct drm_crtc *crtc)
  1874. {
  1875. dce_v6_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  1876. amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
  1877. }
  1878. static void dce_v6_0_crtc_disable(struct drm_crtc *crtc)
  1879. {
  1880. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1881. struct drm_device *dev = crtc->dev;
  1882. struct amdgpu_device *adev = dev->dev_private;
  1883. struct amdgpu_atom_ss ss;
  1884. int i;
  1885. dce_v6_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  1886. if (crtc->primary->fb) {
  1887. int r;
  1888. struct amdgpu_framebuffer *amdgpu_fb;
  1889. struct amdgpu_bo *rbo;
  1890. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  1891. rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  1892. r = amdgpu_bo_reserve(rbo, false);
  1893. if (unlikely(r))
  1894. DRM_ERROR("failed to reserve rbo before unpin\n");
  1895. else {
  1896. amdgpu_bo_unpin(rbo);
  1897. amdgpu_bo_unreserve(rbo);
  1898. }
  1899. }
  1900. /* disable the GRPH */
  1901. dce_v6_0_grph_enable(crtc, false);
  1902. amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
  1903. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1904. if (adev->mode_info.crtcs[i] &&
  1905. adev->mode_info.crtcs[i]->enabled &&
  1906. i != amdgpu_crtc->crtc_id &&
  1907. amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
  1908. /* one other crtc is using this pll don't turn
  1909. * off the pll
  1910. */
  1911. goto done;
  1912. }
  1913. }
  1914. switch (amdgpu_crtc->pll_id) {
  1915. case ATOM_PPLL1:
  1916. case ATOM_PPLL2:
  1917. /* disable the ppll */
  1918. amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
  1919. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  1920. break;
  1921. default:
  1922. break;
  1923. }
  1924. done:
  1925. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  1926. amdgpu_crtc->adjusted_clock = 0;
  1927. amdgpu_crtc->encoder = NULL;
  1928. amdgpu_crtc->connector = NULL;
  1929. }
  1930. static int dce_v6_0_crtc_mode_set(struct drm_crtc *crtc,
  1931. struct drm_display_mode *mode,
  1932. struct drm_display_mode *adjusted_mode,
  1933. int x, int y, struct drm_framebuffer *old_fb)
  1934. {
  1935. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1936. if (!amdgpu_crtc->adjusted_clock)
  1937. return -EINVAL;
  1938. amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
  1939. amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
  1940. dce_v6_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  1941. amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
  1942. amdgpu_atombios_crtc_scaler_setup(crtc);
  1943. dce_v6_0_cursor_reset(crtc);
  1944. /* update the hw version fpr dpm */
  1945. amdgpu_crtc->hw_mode = *adjusted_mode;
  1946. return 0;
  1947. }
  1948. static bool dce_v6_0_crtc_mode_fixup(struct drm_crtc *crtc,
  1949. const struct drm_display_mode *mode,
  1950. struct drm_display_mode *adjusted_mode)
  1951. {
  1952. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1953. struct drm_device *dev = crtc->dev;
  1954. struct drm_encoder *encoder;
  1955. /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
  1956. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1957. if (encoder->crtc == crtc) {
  1958. amdgpu_crtc->encoder = encoder;
  1959. amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
  1960. break;
  1961. }
  1962. }
  1963. if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
  1964. amdgpu_crtc->encoder = NULL;
  1965. amdgpu_crtc->connector = NULL;
  1966. return false;
  1967. }
  1968. if (!amdgpu_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
  1969. return false;
  1970. if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
  1971. return false;
  1972. /* pick pll */
  1973. amdgpu_crtc->pll_id = dce_v6_0_pick_pll(crtc);
  1974. /* if we can't get a PPLL for a non-DP encoder, fail */
  1975. if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
  1976. !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
  1977. return false;
  1978. return true;
  1979. }
  1980. static int dce_v6_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  1981. struct drm_framebuffer *old_fb)
  1982. {
  1983. return dce_v6_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  1984. }
  1985. static int dce_v6_0_crtc_set_base_atomic(struct drm_crtc *crtc,
  1986. struct drm_framebuffer *fb,
  1987. int x, int y, enum mode_set_atomic state)
  1988. {
  1989. return dce_v6_0_crtc_do_set_base(crtc, fb, x, y, 1);
  1990. }
  1991. static const struct drm_crtc_helper_funcs dce_v6_0_crtc_helper_funcs = {
  1992. .dpms = dce_v6_0_crtc_dpms,
  1993. .mode_fixup = dce_v6_0_crtc_mode_fixup,
  1994. .mode_set = dce_v6_0_crtc_mode_set,
  1995. .mode_set_base = dce_v6_0_crtc_set_base,
  1996. .mode_set_base_atomic = dce_v6_0_crtc_set_base_atomic,
  1997. .prepare = dce_v6_0_crtc_prepare,
  1998. .commit = dce_v6_0_crtc_commit,
  1999. .load_lut = dce_v6_0_crtc_load_lut,
  2000. .disable = dce_v6_0_crtc_disable,
  2001. };
  2002. static int dce_v6_0_crtc_init(struct amdgpu_device *adev, int index)
  2003. {
  2004. struct amdgpu_crtc *amdgpu_crtc;
  2005. int i;
  2006. amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
  2007. (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  2008. if (amdgpu_crtc == NULL)
  2009. return -ENOMEM;
  2010. drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_v6_0_crtc_funcs);
  2011. drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
  2012. amdgpu_crtc->crtc_id = index;
  2013. adev->mode_info.crtcs[index] = amdgpu_crtc;
  2014. amdgpu_crtc->max_cursor_width = CURSOR_WIDTH;
  2015. amdgpu_crtc->max_cursor_height = CURSOR_HEIGHT;
  2016. adev->ddev->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
  2017. adev->ddev->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
  2018. for (i = 0; i < 256; i++) {
  2019. amdgpu_crtc->lut_r[i] = i << 2;
  2020. amdgpu_crtc->lut_g[i] = i << 2;
  2021. amdgpu_crtc->lut_b[i] = i << 2;
  2022. }
  2023. amdgpu_crtc->crtc_offset = crtc_offsets[amdgpu_crtc->crtc_id];
  2024. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  2025. amdgpu_crtc->adjusted_clock = 0;
  2026. amdgpu_crtc->encoder = NULL;
  2027. amdgpu_crtc->connector = NULL;
  2028. drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v6_0_crtc_helper_funcs);
  2029. return 0;
  2030. }
  2031. static int dce_v6_0_early_init(void *handle)
  2032. {
  2033. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2034. adev->audio_endpt_rreg = &dce_v6_0_audio_endpt_rreg;
  2035. adev->audio_endpt_wreg = &dce_v6_0_audio_endpt_wreg;
  2036. dce_v6_0_set_display_funcs(adev);
  2037. dce_v6_0_set_irq_funcs(adev);
  2038. switch (adev->asic_type) {
  2039. case CHIP_TAHITI:
  2040. case CHIP_PITCAIRN:
  2041. case CHIP_VERDE:
  2042. adev->mode_info.num_crtc = 6;
  2043. adev->mode_info.num_hpd = 6;
  2044. adev->mode_info.num_dig = 6;
  2045. break;
  2046. case CHIP_OLAND:
  2047. adev->mode_info.num_crtc = 2;
  2048. adev->mode_info.num_hpd = 2;
  2049. adev->mode_info.num_dig = 2;
  2050. break;
  2051. default:
  2052. /* FIXME: not supported yet */
  2053. return -EINVAL;
  2054. }
  2055. return 0;
  2056. }
  2057. static int dce_v6_0_sw_init(void *handle)
  2058. {
  2059. int r, i;
  2060. bool ret;
  2061. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2062. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2063. r = amdgpu_irq_add_id(adev, i + 1, &adev->crtc_irq);
  2064. if (r)
  2065. return r;
  2066. }
  2067. for (i = 8; i < 20; i += 2) {
  2068. r = amdgpu_irq_add_id(adev, i, &adev->pageflip_irq);
  2069. if (r)
  2070. return r;
  2071. }
  2072. /* HPD hotplug */
  2073. r = amdgpu_irq_add_id(adev, 42, &adev->hpd_irq);
  2074. if (r)
  2075. return r;
  2076. adev->mode_info.mode_config_initialized = true;
  2077. adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
  2078. adev->ddev->mode_config.async_page_flip = true;
  2079. adev->ddev->mode_config.max_width = 16384;
  2080. adev->ddev->mode_config.max_height = 16384;
  2081. adev->ddev->mode_config.preferred_depth = 24;
  2082. adev->ddev->mode_config.prefer_shadow = 1;
  2083. adev->ddev->mode_config.fb_base = adev->mc.aper_base;
  2084. r = amdgpu_modeset_create_props(adev);
  2085. if (r)
  2086. return r;
  2087. adev->ddev->mode_config.max_width = 16384;
  2088. adev->ddev->mode_config.max_height = 16384;
  2089. /* allocate crtcs */
  2090. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2091. r = dce_v6_0_crtc_init(adev, i);
  2092. if (r)
  2093. return r;
  2094. }
  2095. ret = amdgpu_atombios_get_connector_info_from_object_table(adev);
  2096. if (ret)
  2097. amdgpu_print_display_setup(adev->ddev);
  2098. else
  2099. return -EINVAL;
  2100. /* setup afmt */
  2101. r = dce_v6_0_afmt_init(adev);
  2102. if (r)
  2103. return r;
  2104. r = dce_v6_0_audio_init(adev);
  2105. if (r)
  2106. return r;
  2107. drm_kms_helper_poll_init(adev->ddev);
  2108. return r;
  2109. }
  2110. static int dce_v6_0_sw_fini(void *handle)
  2111. {
  2112. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2113. kfree(adev->mode_info.bios_hardcoded_edid);
  2114. drm_kms_helper_poll_fini(adev->ddev);
  2115. dce_v6_0_audio_fini(adev);
  2116. dce_v6_0_afmt_fini(adev);
  2117. drm_mode_config_cleanup(adev->ddev);
  2118. adev->mode_info.mode_config_initialized = false;
  2119. return 0;
  2120. }
  2121. static int dce_v6_0_hw_init(void *handle)
  2122. {
  2123. int i;
  2124. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2125. /* init dig PHYs, disp eng pll */
  2126. amdgpu_atombios_encoder_init_dig(adev);
  2127. amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
  2128. /* initialize hpd */
  2129. dce_v6_0_hpd_init(adev);
  2130. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2131. dce_v6_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2132. }
  2133. dce_v6_0_pageflip_interrupt_init(adev);
  2134. return 0;
  2135. }
  2136. static int dce_v6_0_hw_fini(void *handle)
  2137. {
  2138. int i;
  2139. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2140. dce_v6_0_hpd_fini(adev);
  2141. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2142. dce_v6_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2143. }
  2144. dce_v6_0_pageflip_interrupt_fini(adev);
  2145. return 0;
  2146. }
  2147. static int dce_v6_0_suspend(void *handle)
  2148. {
  2149. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2150. amdgpu_atombios_scratch_regs_save(adev);
  2151. return dce_v6_0_hw_fini(handle);
  2152. }
  2153. static int dce_v6_0_resume(void *handle)
  2154. {
  2155. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2156. int ret;
  2157. ret = dce_v6_0_hw_init(handle);
  2158. amdgpu_atombios_scratch_regs_restore(adev);
  2159. /* turn on the BL */
  2160. if (adev->mode_info.bl_encoder) {
  2161. u8 bl_level = amdgpu_display_backlight_get_level(adev,
  2162. adev->mode_info.bl_encoder);
  2163. amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
  2164. bl_level);
  2165. }
  2166. return ret;
  2167. }
  2168. static bool dce_v6_0_is_idle(void *handle)
  2169. {
  2170. return true;
  2171. }
  2172. static int dce_v6_0_wait_for_idle(void *handle)
  2173. {
  2174. return 0;
  2175. }
  2176. static int dce_v6_0_soft_reset(void *handle)
  2177. {
  2178. DRM_INFO("xxxx: dce_v6_0_soft_reset --- no impl!!\n");
  2179. return 0;
  2180. }
  2181. static void dce_v6_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
  2182. int crtc,
  2183. enum amdgpu_interrupt_state state)
  2184. {
  2185. u32 reg_block, interrupt_mask;
  2186. if (crtc >= adev->mode_info.num_crtc) {
  2187. DRM_DEBUG("invalid crtc %d\n", crtc);
  2188. return;
  2189. }
  2190. switch (crtc) {
  2191. case 0:
  2192. reg_block = SI_CRTC0_REGISTER_OFFSET;
  2193. break;
  2194. case 1:
  2195. reg_block = SI_CRTC1_REGISTER_OFFSET;
  2196. break;
  2197. case 2:
  2198. reg_block = SI_CRTC2_REGISTER_OFFSET;
  2199. break;
  2200. case 3:
  2201. reg_block = SI_CRTC3_REGISTER_OFFSET;
  2202. break;
  2203. case 4:
  2204. reg_block = SI_CRTC4_REGISTER_OFFSET;
  2205. break;
  2206. case 5:
  2207. reg_block = SI_CRTC5_REGISTER_OFFSET;
  2208. break;
  2209. default:
  2210. DRM_DEBUG("invalid crtc %d\n", crtc);
  2211. return;
  2212. }
  2213. switch (state) {
  2214. case AMDGPU_IRQ_STATE_DISABLE:
  2215. interrupt_mask = RREG32(INT_MASK + reg_block);
  2216. interrupt_mask &= ~VBLANK_INT_MASK;
  2217. WREG32(INT_MASK + reg_block, interrupt_mask);
  2218. break;
  2219. case AMDGPU_IRQ_STATE_ENABLE:
  2220. interrupt_mask = RREG32(INT_MASK + reg_block);
  2221. interrupt_mask |= VBLANK_INT_MASK;
  2222. WREG32(INT_MASK + reg_block, interrupt_mask);
  2223. break;
  2224. default:
  2225. break;
  2226. }
  2227. }
  2228. static void dce_v6_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
  2229. int crtc,
  2230. enum amdgpu_interrupt_state state)
  2231. {
  2232. }
  2233. static int dce_v6_0_set_hpd_interrupt_state(struct amdgpu_device *adev,
  2234. struct amdgpu_irq_src *src,
  2235. unsigned type,
  2236. enum amdgpu_interrupt_state state)
  2237. {
  2238. u32 dc_hpd_int_cntl_reg, dc_hpd_int_cntl;
  2239. switch (type) {
  2240. case AMDGPU_HPD_1:
  2241. dc_hpd_int_cntl_reg = DC_HPD1_INT_CONTROL;
  2242. break;
  2243. case AMDGPU_HPD_2:
  2244. dc_hpd_int_cntl_reg = DC_HPD2_INT_CONTROL;
  2245. break;
  2246. case AMDGPU_HPD_3:
  2247. dc_hpd_int_cntl_reg = DC_HPD3_INT_CONTROL;
  2248. break;
  2249. case AMDGPU_HPD_4:
  2250. dc_hpd_int_cntl_reg = DC_HPD4_INT_CONTROL;
  2251. break;
  2252. case AMDGPU_HPD_5:
  2253. dc_hpd_int_cntl_reg = DC_HPD5_INT_CONTROL;
  2254. break;
  2255. case AMDGPU_HPD_6:
  2256. dc_hpd_int_cntl_reg = DC_HPD6_INT_CONTROL;
  2257. break;
  2258. default:
  2259. DRM_DEBUG("invalid hdp %d\n", type);
  2260. return 0;
  2261. }
  2262. switch (state) {
  2263. case AMDGPU_IRQ_STATE_DISABLE:
  2264. dc_hpd_int_cntl = RREG32(dc_hpd_int_cntl_reg);
  2265. dc_hpd_int_cntl &= ~(DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN);
  2266. WREG32(dc_hpd_int_cntl_reg, dc_hpd_int_cntl);
  2267. break;
  2268. case AMDGPU_IRQ_STATE_ENABLE:
  2269. dc_hpd_int_cntl = RREG32(dc_hpd_int_cntl_reg);
  2270. dc_hpd_int_cntl |= (DC_HPDx_INT_EN | DC_HPDx_RX_INT_EN);
  2271. WREG32(dc_hpd_int_cntl_reg, dc_hpd_int_cntl);
  2272. break;
  2273. default:
  2274. break;
  2275. }
  2276. return 0;
  2277. }
  2278. static int dce_v6_0_set_crtc_interrupt_state(struct amdgpu_device *adev,
  2279. struct amdgpu_irq_src *src,
  2280. unsigned type,
  2281. enum amdgpu_interrupt_state state)
  2282. {
  2283. switch (type) {
  2284. case AMDGPU_CRTC_IRQ_VBLANK1:
  2285. dce_v6_0_set_crtc_vblank_interrupt_state(adev, 0, state);
  2286. break;
  2287. case AMDGPU_CRTC_IRQ_VBLANK2:
  2288. dce_v6_0_set_crtc_vblank_interrupt_state(adev, 1, state);
  2289. break;
  2290. case AMDGPU_CRTC_IRQ_VBLANK3:
  2291. dce_v6_0_set_crtc_vblank_interrupt_state(adev, 2, state);
  2292. break;
  2293. case AMDGPU_CRTC_IRQ_VBLANK4:
  2294. dce_v6_0_set_crtc_vblank_interrupt_state(adev, 3, state);
  2295. break;
  2296. case AMDGPU_CRTC_IRQ_VBLANK5:
  2297. dce_v6_0_set_crtc_vblank_interrupt_state(adev, 4, state);
  2298. break;
  2299. case AMDGPU_CRTC_IRQ_VBLANK6:
  2300. dce_v6_0_set_crtc_vblank_interrupt_state(adev, 5, state);
  2301. break;
  2302. case AMDGPU_CRTC_IRQ_VLINE1:
  2303. dce_v6_0_set_crtc_vline_interrupt_state(adev, 0, state);
  2304. break;
  2305. case AMDGPU_CRTC_IRQ_VLINE2:
  2306. dce_v6_0_set_crtc_vline_interrupt_state(adev, 1, state);
  2307. break;
  2308. case AMDGPU_CRTC_IRQ_VLINE3:
  2309. dce_v6_0_set_crtc_vline_interrupt_state(adev, 2, state);
  2310. break;
  2311. case AMDGPU_CRTC_IRQ_VLINE4:
  2312. dce_v6_0_set_crtc_vline_interrupt_state(adev, 3, state);
  2313. break;
  2314. case AMDGPU_CRTC_IRQ_VLINE5:
  2315. dce_v6_0_set_crtc_vline_interrupt_state(adev, 4, state);
  2316. break;
  2317. case AMDGPU_CRTC_IRQ_VLINE6:
  2318. dce_v6_0_set_crtc_vline_interrupt_state(adev, 5, state);
  2319. break;
  2320. default:
  2321. break;
  2322. }
  2323. return 0;
  2324. }
  2325. static int dce_v6_0_crtc_irq(struct amdgpu_device *adev,
  2326. struct amdgpu_irq_src *source,
  2327. struct amdgpu_iv_entry *entry)
  2328. {
  2329. unsigned crtc = entry->src_id - 1;
  2330. uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
  2331. unsigned irq_type = amdgpu_crtc_idx_to_irq_type(adev, crtc);
  2332. switch (entry->src_data) {
  2333. case 0: /* vblank */
  2334. if (disp_int & interrupt_status_offsets[crtc].vblank)
  2335. WREG32(VBLANK_STATUS + crtc_offsets[crtc], VBLANK_ACK);
  2336. else
  2337. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  2338. if (amdgpu_irq_enabled(adev, source, irq_type)) {
  2339. drm_handle_vblank(adev->ddev, crtc);
  2340. }
  2341. DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
  2342. break;
  2343. case 1: /* vline */
  2344. if (disp_int & interrupt_status_offsets[crtc].vline)
  2345. WREG32(VLINE_STATUS + crtc_offsets[crtc], VLINE_ACK);
  2346. else
  2347. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  2348. DRM_DEBUG("IH: D%d vline\n", crtc + 1);
  2349. break;
  2350. default:
  2351. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
  2352. break;
  2353. }
  2354. return 0;
  2355. }
  2356. static int dce_v6_0_set_pageflip_interrupt_state(struct amdgpu_device *adev,
  2357. struct amdgpu_irq_src *src,
  2358. unsigned type,
  2359. enum amdgpu_interrupt_state state)
  2360. {
  2361. u32 reg;
  2362. if (type >= adev->mode_info.num_crtc) {
  2363. DRM_ERROR("invalid pageflip crtc %d\n", type);
  2364. return -EINVAL;
  2365. }
  2366. reg = RREG32(GRPH_INT_CONTROL + crtc_offsets[type]);
  2367. if (state == AMDGPU_IRQ_STATE_DISABLE)
  2368. WREG32(GRPH_INT_CONTROL + crtc_offsets[type],
  2369. reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2370. else
  2371. WREG32(GRPH_INT_CONTROL + crtc_offsets[type],
  2372. reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2373. return 0;
  2374. }
  2375. static int dce_v6_0_pageflip_irq(struct amdgpu_device *adev,
  2376. struct amdgpu_irq_src *source,
  2377. struct amdgpu_iv_entry *entry)
  2378. {
  2379. unsigned long flags;
  2380. unsigned crtc_id;
  2381. struct amdgpu_crtc *amdgpu_crtc;
  2382. struct amdgpu_flip_work *works;
  2383. crtc_id = (entry->src_id - 8) >> 1;
  2384. amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  2385. if (crtc_id >= adev->mode_info.num_crtc) {
  2386. DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
  2387. return -EINVAL;
  2388. }
  2389. if (RREG32(GRPH_INT_STATUS + crtc_offsets[crtc_id]) &
  2390. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
  2391. WREG32(GRPH_INT_STATUS + crtc_offsets[crtc_id],
  2392. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
  2393. /* IRQ could occur when in initial stage */
  2394. if (amdgpu_crtc == NULL)
  2395. return 0;
  2396. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  2397. works = amdgpu_crtc->pflip_works;
  2398. if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
  2399. DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
  2400. "AMDGPU_FLIP_SUBMITTED(%d)\n",
  2401. amdgpu_crtc->pflip_status,
  2402. AMDGPU_FLIP_SUBMITTED);
  2403. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2404. return 0;
  2405. }
  2406. /* page flip completed. clean up */
  2407. amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
  2408. amdgpu_crtc->pflip_works = NULL;
  2409. /* wakeup usersapce */
  2410. if (works->event)
  2411. drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
  2412. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2413. drm_crtc_vblank_put(&amdgpu_crtc->base);
  2414. schedule_work(&works->unpin_work);
  2415. return 0;
  2416. }
  2417. static int dce_v6_0_hpd_irq(struct amdgpu_device *adev,
  2418. struct amdgpu_irq_src *source,
  2419. struct amdgpu_iv_entry *entry)
  2420. {
  2421. uint32_t disp_int, mask, int_control, tmp;
  2422. unsigned hpd;
  2423. if (entry->src_data >= adev->mode_info.num_hpd) {
  2424. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
  2425. return 0;
  2426. }
  2427. hpd = entry->src_data;
  2428. disp_int = RREG32(interrupt_status_offsets[hpd].reg);
  2429. mask = interrupt_status_offsets[hpd].hpd;
  2430. int_control = hpd_int_control_offsets[hpd];
  2431. if (disp_int & mask) {
  2432. tmp = RREG32(int_control);
  2433. tmp |= DC_HPD1_INT_CONTROL__DC_HPD1_INT_ACK_MASK;
  2434. WREG32(int_control, tmp);
  2435. schedule_work(&adev->hotplug_work);
  2436. DRM_INFO("IH: HPD%d\n", hpd + 1);
  2437. }
  2438. return 0;
  2439. }
  2440. static int dce_v6_0_set_clockgating_state(void *handle,
  2441. enum amd_clockgating_state state)
  2442. {
  2443. return 0;
  2444. }
  2445. static int dce_v6_0_set_powergating_state(void *handle,
  2446. enum amd_powergating_state state)
  2447. {
  2448. return 0;
  2449. }
  2450. const struct amd_ip_funcs dce_v6_0_ip_funcs = {
  2451. .name = "dce_v6_0",
  2452. .early_init = dce_v6_0_early_init,
  2453. .late_init = NULL,
  2454. .sw_init = dce_v6_0_sw_init,
  2455. .sw_fini = dce_v6_0_sw_fini,
  2456. .hw_init = dce_v6_0_hw_init,
  2457. .hw_fini = dce_v6_0_hw_fini,
  2458. .suspend = dce_v6_0_suspend,
  2459. .resume = dce_v6_0_resume,
  2460. .is_idle = dce_v6_0_is_idle,
  2461. .wait_for_idle = dce_v6_0_wait_for_idle,
  2462. .soft_reset = dce_v6_0_soft_reset,
  2463. .set_clockgating_state = dce_v6_0_set_clockgating_state,
  2464. .set_powergating_state = dce_v6_0_set_powergating_state,
  2465. };
  2466. static void
  2467. dce_v6_0_encoder_mode_set(struct drm_encoder *encoder,
  2468. struct drm_display_mode *mode,
  2469. struct drm_display_mode *adjusted_mode)
  2470. {
  2471. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2472. amdgpu_encoder->pixel_clock = adjusted_mode->clock;
  2473. /* need to call this here rather than in prepare() since we need some crtc info */
  2474. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  2475. /* set scaler clears this on some chips */
  2476. dce_v6_0_set_interleave(encoder->crtc, mode);
  2477. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
  2478. dce_v6_0_afmt_enable(encoder, true);
  2479. dce_v6_0_afmt_setmode(encoder, adjusted_mode);
  2480. }
  2481. }
  2482. static void dce_v6_0_encoder_prepare(struct drm_encoder *encoder)
  2483. {
  2484. struct amdgpu_device *adev = encoder->dev->dev_private;
  2485. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2486. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  2487. if ((amdgpu_encoder->active_device &
  2488. (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
  2489. (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
  2490. ENCODER_OBJECT_ID_NONE)) {
  2491. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  2492. if (dig) {
  2493. dig->dig_encoder = dce_v6_0_pick_dig_encoder(encoder);
  2494. if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
  2495. dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
  2496. }
  2497. }
  2498. amdgpu_atombios_scratch_regs_lock(adev, true);
  2499. if (connector) {
  2500. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  2501. /* select the clock/data port if it uses a router */
  2502. if (amdgpu_connector->router.cd_valid)
  2503. amdgpu_i2c_router_select_cd_port(amdgpu_connector);
  2504. /* turn eDP panel on for mode set */
  2505. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  2506. amdgpu_atombios_encoder_set_edp_panel_power(connector,
  2507. ATOM_TRANSMITTER_ACTION_POWER_ON);
  2508. }
  2509. /* this is needed for the pll/ss setup to work correctly in some cases */
  2510. amdgpu_atombios_encoder_set_crtc_source(encoder);
  2511. /* set up the FMT blocks */
  2512. dce_v6_0_program_fmt(encoder);
  2513. }
  2514. static void dce_v6_0_encoder_commit(struct drm_encoder *encoder)
  2515. {
  2516. struct drm_device *dev = encoder->dev;
  2517. struct amdgpu_device *adev = dev->dev_private;
  2518. /* need to call this here as we need the crtc set up */
  2519. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  2520. amdgpu_atombios_scratch_regs_lock(adev, false);
  2521. }
  2522. static void dce_v6_0_encoder_disable(struct drm_encoder *encoder)
  2523. {
  2524. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2525. struct amdgpu_encoder_atom_dig *dig;
  2526. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  2527. if (amdgpu_atombios_encoder_is_digital(encoder)) {
  2528. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
  2529. dce_v6_0_afmt_enable(encoder, false);
  2530. dig = amdgpu_encoder->enc_priv;
  2531. dig->dig_encoder = -1;
  2532. }
  2533. amdgpu_encoder->active_device = 0;
  2534. }
  2535. /* these are handled by the primary encoders */
  2536. static void dce_v6_0_ext_prepare(struct drm_encoder *encoder)
  2537. {
  2538. }
  2539. static void dce_v6_0_ext_commit(struct drm_encoder *encoder)
  2540. {
  2541. }
  2542. static void
  2543. dce_v6_0_ext_mode_set(struct drm_encoder *encoder,
  2544. struct drm_display_mode *mode,
  2545. struct drm_display_mode *adjusted_mode)
  2546. {
  2547. }
  2548. static void dce_v6_0_ext_disable(struct drm_encoder *encoder)
  2549. {
  2550. }
  2551. static void
  2552. dce_v6_0_ext_dpms(struct drm_encoder *encoder, int mode)
  2553. {
  2554. }
  2555. static bool dce_v6_0_ext_mode_fixup(struct drm_encoder *encoder,
  2556. const struct drm_display_mode *mode,
  2557. struct drm_display_mode *adjusted_mode)
  2558. {
  2559. return true;
  2560. }
  2561. static const struct drm_encoder_helper_funcs dce_v6_0_ext_helper_funcs = {
  2562. .dpms = dce_v6_0_ext_dpms,
  2563. .mode_fixup = dce_v6_0_ext_mode_fixup,
  2564. .prepare = dce_v6_0_ext_prepare,
  2565. .mode_set = dce_v6_0_ext_mode_set,
  2566. .commit = dce_v6_0_ext_commit,
  2567. .disable = dce_v6_0_ext_disable,
  2568. /* no detect for TMDS/LVDS yet */
  2569. };
  2570. static const struct drm_encoder_helper_funcs dce_v6_0_dig_helper_funcs = {
  2571. .dpms = amdgpu_atombios_encoder_dpms,
  2572. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  2573. .prepare = dce_v6_0_encoder_prepare,
  2574. .mode_set = dce_v6_0_encoder_mode_set,
  2575. .commit = dce_v6_0_encoder_commit,
  2576. .disable = dce_v6_0_encoder_disable,
  2577. .detect = amdgpu_atombios_encoder_dig_detect,
  2578. };
  2579. static const struct drm_encoder_helper_funcs dce_v6_0_dac_helper_funcs = {
  2580. .dpms = amdgpu_atombios_encoder_dpms,
  2581. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  2582. .prepare = dce_v6_0_encoder_prepare,
  2583. .mode_set = dce_v6_0_encoder_mode_set,
  2584. .commit = dce_v6_0_encoder_commit,
  2585. .detect = amdgpu_atombios_encoder_dac_detect,
  2586. };
  2587. static void dce_v6_0_encoder_destroy(struct drm_encoder *encoder)
  2588. {
  2589. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2590. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  2591. amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
  2592. kfree(amdgpu_encoder->enc_priv);
  2593. drm_encoder_cleanup(encoder);
  2594. kfree(amdgpu_encoder);
  2595. }
  2596. static const struct drm_encoder_funcs dce_v6_0_encoder_funcs = {
  2597. .destroy = dce_v6_0_encoder_destroy,
  2598. };
  2599. static void dce_v6_0_encoder_add(struct amdgpu_device *adev,
  2600. uint32_t encoder_enum,
  2601. uint32_t supported_device,
  2602. u16 caps)
  2603. {
  2604. struct drm_device *dev = adev->ddev;
  2605. struct drm_encoder *encoder;
  2606. struct amdgpu_encoder *amdgpu_encoder;
  2607. /* see if we already added it */
  2608. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  2609. amdgpu_encoder = to_amdgpu_encoder(encoder);
  2610. if (amdgpu_encoder->encoder_enum == encoder_enum) {
  2611. amdgpu_encoder->devices |= supported_device;
  2612. return;
  2613. }
  2614. }
  2615. /* add a new one */
  2616. amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
  2617. if (!amdgpu_encoder)
  2618. return;
  2619. encoder = &amdgpu_encoder->base;
  2620. switch (adev->mode_info.num_crtc) {
  2621. case 1:
  2622. encoder->possible_crtcs = 0x1;
  2623. break;
  2624. case 2:
  2625. default:
  2626. encoder->possible_crtcs = 0x3;
  2627. break;
  2628. case 4:
  2629. encoder->possible_crtcs = 0xf;
  2630. break;
  2631. case 6:
  2632. encoder->possible_crtcs = 0x3f;
  2633. break;
  2634. }
  2635. amdgpu_encoder->enc_priv = NULL;
  2636. amdgpu_encoder->encoder_enum = encoder_enum;
  2637. amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
  2638. amdgpu_encoder->devices = supported_device;
  2639. amdgpu_encoder->rmx_type = RMX_OFF;
  2640. amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
  2641. amdgpu_encoder->is_ext_encoder = false;
  2642. amdgpu_encoder->caps = caps;
  2643. switch (amdgpu_encoder->encoder_id) {
  2644. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
  2645. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
  2646. drm_encoder_init(dev, encoder, &dce_v6_0_encoder_funcs,
  2647. DRM_MODE_ENCODER_DAC, NULL);
  2648. drm_encoder_helper_add(encoder, &dce_v6_0_dac_helper_funcs);
  2649. break;
  2650. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
  2651. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  2652. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  2653. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  2654. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
  2655. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
  2656. amdgpu_encoder->rmx_type = RMX_FULL;
  2657. drm_encoder_init(dev, encoder, &dce_v6_0_encoder_funcs,
  2658. DRM_MODE_ENCODER_LVDS, NULL);
  2659. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
  2660. } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
  2661. drm_encoder_init(dev, encoder, &dce_v6_0_encoder_funcs,
  2662. DRM_MODE_ENCODER_DAC, NULL);
  2663. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  2664. } else {
  2665. drm_encoder_init(dev, encoder, &dce_v6_0_encoder_funcs,
  2666. DRM_MODE_ENCODER_TMDS, NULL);
  2667. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  2668. }
  2669. drm_encoder_helper_add(encoder, &dce_v6_0_dig_helper_funcs);
  2670. break;
  2671. case ENCODER_OBJECT_ID_SI170B:
  2672. case ENCODER_OBJECT_ID_CH7303:
  2673. case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
  2674. case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
  2675. case ENCODER_OBJECT_ID_TITFP513:
  2676. case ENCODER_OBJECT_ID_VT1623:
  2677. case ENCODER_OBJECT_ID_HDMI_SI1930:
  2678. case ENCODER_OBJECT_ID_TRAVIS:
  2679. case ENCODER_OBJECT_ID_NUTMEG:
  2680. /* these are handled by the primary encoders */
  2681. amdgpu_encoder->is_ext_encoder = true;
  2682. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  2683. drm_encoder_init(dev, encoder, &dce_v6_0_encoder_funcs,
  2684. DRM_MODE_ENCODER_LVDS, NULL);
  2685. else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
  2686. drm_encoder_init(dev, encoder, &dce_v6_0_encoder_funcs,
  2687. DRM_MODE_ENCODER_DAC, NULL);
  2688. else
  2689. drm_encoder_init(dev, encoder, &dce_v6_0_encoder_funcs,
  2690. DRM_MODE_ENCODER_TMDS, NULL);
  2691. drm_encoder_helper_add(encoder, &dce_v6_0_ext_helper_funcs);
  2692. break;
  2693. }
  2694. }
  2695. static const struct amdgpu_display_funcs dce_v6_0_display_funcs = {
  2696. .set_vga_render_state = &dce_v6_0_set_vga_render_state,
  2697. .bandwidth_update = &dce_v6_0_bandwidth_update,
  2698. .vblank_get_counter = &dce_v6_0_vblank_get_counter,
  2699. .vblank_wait = &dce_v6_0_vblank_wait,
  2700. .is_display_hung = &dce_v6_0_is_display_hung,
  2701. .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
  2702. .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
  2703. .hpd_sense = &dce_v6_0_hpd_sense,
  2704. .hpd_set_polarity = &dce_v6_0_hpd_set_polarity,
  2705. .hpd_get_gpio_reg = &dce_v6_0_hpd_get_gpio_reg,
  2706. .page_flip = &dce_v6_0_page_flip,
  2707. .page_flip_get_scanoutpos = &dce_v6_0_crtc_get_scanoutpos,
  2708. .add_encoder = &dce_v6_0_encoder_add,
  2709. .add_connector = &amdgpu_connector_add,
  2710. .stop_mc_access = &dce_v6_0_stop_mc_access,
  2711. .resume_mc_access = &dce_v6_0_resume_mc_access,
  2712. };
  2713. static void dce_v6_0_set_display_funcs(struct amdgpu_device *adev)
  2714. {
  2715. if (adev->mode_info.funcs == NULL)
  2716. adev->mode_info.funcs = &dce_v6_0_display_funcs;
  2717. }
  2718. static const struct amdgpu_irq_src_funcs dce_v6_0_crtc_irq_funcs = {
  2719. .set = dce_v6_0_set_crtc_interrupt_state,
  2720. .process = dce_v6_0_crtc_irq,
  2721. };
  2722. static const struct amdgpu_irq_src_funcs dce_v6_0_pageflip_irq_funcs = {
  2723. .set = dce_v6_0_set_pageflip_interrupt_state,
  2724. .process = dce_v6_0_pageflip_irq,
  2725. };
  2726. static const struct amdgpu_irq_src_funcs dce_v6_0_hpd_irq_funcs = {
  2727. .set = dce_v6_0_set_hpd_interrupt_state,
  2728. .process = dce_v6_0_hpd_irq,
  2729. };
  2730. static void dce_v6_0_set_irq_funcs(struct amdgpu_device *adev)
  2731. {
  2732. adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_LAST;
  2733. adev->crtc_irq.funcs = &dce_v6_0_crtc_irq_funcs;
  2734. adev->pageflip_irq.num_types = AMDGPU_PAGEFLIP_IRQ_LAST;
  2735. adev->pageflip_irq.funcs = &dce_v6_0_pageflip_irq_funcs;
  2736. adev->hpd_irq.num_types = AMDGPU_HPD_LAST;
  2737. adev->hpd_irq.funcs = &dce_v6_0_hpd_irq_funcs;
  2738. }