amdgpu_device.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/kthread.h>
  29. #include <linux/console.h>
  30. #include <linux/slab.h>
  31. #include <linux/debugfs.h>
  32. #include <drm/drmP.h>
  33. #include <drm/drm_crtc_helper.h>
  34. #include <drm/amdgpu_drm.h>
  35. #include <linux/vgaarb.h>
  36. #include <linux/vga_switcheroo.h>
  37. #include <linux/efi.h>
  38. #include "amdgpu.h"
  39. #include "amdgpu_trace.h"
  40. #include "amdgpu_i2c.h"
  41. #include "atom.h"
  42. #include "amdgpu_atombios.h"
  43. #include "amdgpu_atomfirmware.h"
  44. #include "amd_pcie.h"
  45. #ifdef CONFIG_DRM_AMDGPU_SI
  46. #include "si.h"
  47. #endif
  48. #ifdef CONFIG_DRM_AMDGPU_CIK
  49. #include "cik.h"
  50. #endif
  51. #include "vi.h"
  52. #include "soc15.h"
  53. #include "bif/bif_4_1_d.h"
  54. #include <linux/pci.h>
  55. #include <linux/firmware.h>
  56. MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  57. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
  58. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
  59. static const char *amdgpu_asic_name[] = {
  60. "TAHITI",
  61. "PITCAIRN",
  62. "VERDE",
  63. "OLAND",
  64. "HAINAN",
  65. "BONAIRE",
  66. "KAVERI",
  67. "KABINI",
  68. "HAWAII",
  69. "MULLINS",
  70. "TOPAZ",
  71. "TONGA",
  72. "FIJI",
  73. "CARRIZO",
  74. "STONEY",
  75. "POLARIS10",
  76. "POLARIS11",
  77. "POLARIS12",
  78. "VEGA10",
  79. "LAST",
  80. };
  81. bool amdgpu_device_is_px(struct drm_device *dev)
  82. {
  83. struct amdgpu_device *adev = dev->dev_private;
  84. if (adev->flags & AMD_IS_PX)
  85. return true;
  86. return false;
  87. }
  88. /*
  89. * MMIO register access helper functions.
  90. */
  91. uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
  92. uint32_t acc_flags)
  93. {
  94. uint32_t ret;
  95. if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
  96. BUG_ON(in_interrupt());
  97. return amdgpu_virt_kiq_rreg(adev, reg);
  98. }
  99. if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
  100. ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
  101. else {
  102. unsigned long flags;
  103. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  104. writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
  105. ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  106. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  107. }
  108. trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
  109. return ret;
  110. }
  111. void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
  112. uint32_t acc_flags)
  113. {
  114. trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
  115. if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
  116. BUG_ON(in_interrupt());
  117. return amdgpu_virt_kiq_wreg(adev, reg, v);
  118. }
  119. if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
  120. writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
  121. else {
  122. unsigned long flags;
  123. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  124. writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
  125. writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  126. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  127. }
  128. }
  129. u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
  130. {
  131. if ((reg * 4) < adev->rio_mem_size)
  132. return ioread32(adev->rio_mem + (reg * 4));
  133. else {
  134. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  135. return ioread32(adev->rio_mem + (mmMM_DATA * 4));
  136. }
  137. }
  138. void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
  139. {
  140. if ((reg * 4) < adev->rio_mem_size)
  141. iowrite32(v, adev->rio_mem + (reg * 4));
  142. else {
  143. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  144. iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
  145. }
  146. }
  147. /**
  148. * amdgpu_mm_rdoorbell - read a doorbell dword
  149. *
  150. * @adev: amdgpu_device pointer
  151. * @index: doorbell index
  152. *
  153. * Returns the value in the doorbell aperture at the
  154. * requested doorbell index (CIK).
  155. */
  156. u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
  157. {
  158. if (index < adev->doorbell.num_doorbells) {
  159. return readl(adev->doorbell.ptr + index);
  160. } else {
  161. DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
  162. return 0;
  163. }
  164. }
  165. /**
  166. * amdgpu_mm_wdoorbell - write a doorbell dword
  167. *
  168. * @adev: amdgpu_device pointer
  169. * @index: doorbell index
  170. * @v: value to write
  171. *
  172. * Writes @v to the doorbell aperture at the
  173. * requested doorbell index (CIK).
  174. */
  175. void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
  176. {
  177. if (index < adev->doorbell.num_doorbells) {
  178. writel(v, adev->doorbell.ptr + index);
  179. } else {
  180. DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  181. }
  182. }
  183. /**
  184. * amdgpu_mm_rdoorbell64 - read a doorbell Qword
  185. *
  186. * @adev: amdgpu_device pointer
  187. * @index: doorbell index
  188. *
  189. * Returns the value in the doorbell aperture at the
  190. * requested doorbell index (VEGA10+).
  191. */
  192. u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
  193. {
  194. if (index < adev->doorbell.num_doorbells) {
  195. return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
  196. } else {
  197. DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
  198. return 0;
  199. }
  200. }
  201. /**
  202. * amdgpu_mm_wdoorbell64 - write a doorbell Qword
  203. *
  204. * @adev: amdgpu_device pointer
  205. * @index: doorbell index
  206. * @v: value to write
  207. *
  208. * Writes @v to the doorbell aperture at the
  209. * requested doorbell index (VEGA10+).
  210. */
  211. void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
  212. {
  213. if (index < adev->doorbell.num_doorbells) {
  214. atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
  215. } else {
  216. DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  217. }
  218. }
  219. /**
  220. * amdgpu_invalid_rreg - dummy reg read function
  221. *
  222. * @adev: amdgpu device pointer
  223. * @reg: offset of register
  224. *
  225. * Dummy register read function. Used for register blocks
  226. * that certain asics don't have (all asics).
  227. * Returns the value in the register.
  228. */
  229. static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
  230. {
  231. DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
  232. BUG();
  233. return 0;
  234. }
  235. /**
  236. * amdgpu_invalid_wreg - dummy reg write function
  237. *
  238. * @adev: amdgpu device pointer
  239. * @reg: offset of register
  240. * @v: value to write to the register
  241. *
  242. * Dummy register read function. Used for register blocks
  243. * that certain asics don't have (all asics).
  244. */
  245. static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
  246. {
  247. DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
  248. reg, v);
  249. BUG();
  250. }
  251. /**
  252. * amdgpu_block_invalid_rreg - dummy reg read function
  253. *
  254. * @adev: amdgpu device pointer
  255. * @block: offset of instance
  256. * @reg: offset of register
  257. *
  258. * Dummy register read function. Used for register blocks
  259. * that certain asics don't have (all asics).
  260. * Returns the value in the register.
  261. */
  262. static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
  263. uint32_t block, uint32_t reg)
  264. {
  265. DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
  266. reg, block);
  267. BUG();
  268. return 0;
  269. }
  270. /**
  271. * amdgpu_block_invalid_wreg - dummy reg write function
  272. *
  273. * @adev: amdgpu device pointer
  274. * @block: offset of instance
  275. * @reg: offset of register
  276. * @v: value to write to the register
  277. *
  278. * Dummy register read function. Used for register blocks
  279. * that certain asics don't have (all asics).
  280. */
  281. static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
  282. uint32_t block,
  283. uint32_t reg, uint32_t v)
  284. {
  285. DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
  286. reg, block, v);
  287. BUG();
  288. }
  289. static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
  290. {
  291. int r;
  292. if (adev->vram_scratch.robj == NULL) {
  293. r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
  294. PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
  295. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  296. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  297. NULL, NULL, &adev->vram_scratch.robj);
  298. if (r) {
  299. return r;
  300. }
  301. }
  302. r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
  303. if (unlikely(r != 0))
  304. return r;
  305. r = amdgpu_bo_pin(adev->vram_scratch.robj,
  306. AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
  307. if (r) {
  308. amdgpu_bo_unreserve(adev->vram_scratch.robj);
  309. return r;
  310. }
  311. r = amdgpu_bo_kmap(adev->vram_scratch.robj,
  312. (void **)&adev->vram_scratch.ptr);
  313. if (r)
  314. amdgpu_bo_unpin(adev->vram_scratch.robj);
  315. amdgpu_bo_unreserve(adev->vram_scratch.robj);
  316. return r;
  317. }
  318. static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
  319. {
  320. int r;
  321. if (adev->vram_scratch.robj == NULL) {
  322. return;
  323. }
  324. r = amdgpu_bo_reserve(adev->vram_scratch.robj, true);
  325. if (likely(r == 0)) {
  326. amdgpu_bo_kunmap(adev->vram_scratch.robj);
  327. amdgpu_bo_unpin(adev->vram_scratch.robj);
  328. amdgpu_bo_unreserve(adev->vram_scratch.robj);
  329. }
  330. amdgpu_bo_unref(&adev->vram_scratch.robj);
  331. }
  332. /**
  333. * amdgpu_program_register_sequence - program an array of registers.
  334. *
  335. * @adev: amdgpu_device pointer
  336. * @registers: pointer to the register array
  337. * @array_size: size of the register array
  338. *
  339. * Programs an array or registers with and and or masks.
  340. * This is a helper for setting golden registers.
  341. */
  342. void amdgpu_program_register_sequence(struct amdgpu_device *adev,
  343. const u32 *registers,
  344. const u32 array_size)
  345. {
  346. u32 tmp, reg, and_mask, or_mask;
  347. int i;
  348. if (array_size % 3)
  349. return;
  350. for (i = 0; i < array_size; i +=3) {
  351. reg = registers[i + 0];
  352. and_mask = registers[i + 1];
  353. or_mask = registers[i + 2];
  354. if (and_mask == 0xffffffff) {
  355. tmp = or_mask;
  356. } else {
  357. tmp = RREG32(reg);
  358. tmp &= ~and_mask;
  359. tmp |= or_mask;
  360. }
  361. WREG32(reg, tmp);
  362. }
  363. }
  364. void amdgpu_pci_config_reset(struct amdgpu_device *adev)
  365. {
  366. pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
  367. }
  368. /*
  369. * GPU doorbell aperture helpers function.
  370. */
  371. /**
  372. * amdgpu_doorbell_init - Init doorbell driver information.
  373. *
  374. * @adev: amdgpu_device pointer
  375. *
  376. * Init doorbell driver information (CIK)
  377. * Returns 0 on success, error on failure.
  378. */
  379. static int amdgpu_doorbell_init(struct amdgpu_device *adev)
  380. {
  381. /* doorbell bar mapping */
  382. adev->doorbell.base = pci_resource_start(adev->pdev, 2);
  383. adev->doorbell.size = pci_resource_len(adev->pdev, 2);
  384. adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
  385. AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
  386. if (adev->doorbell.num_doorbells == 0)
  387. return -EINVAL;
  388. adev->doorbell.ptr = ioremap(adev->doorbell.base,
  389. adev->doorbell.num_doorbells *
  390. sizeof(u32));
  391. if (adev->doorbell.ptr == NULL)
  392. return -ENOMEM;
  393. return 0;
  394. }
  395. /**
  396. * amdgpu_doorbell_fini - Tear down doorbell driver information.
  397. *
  398. * @adev: amdgpu_device pointer
  399. *
  400. * Tear down doorbell driver information (CIK)
  401. */
  402. static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
  403. {
  404. iounmap(adev->doorbell.ptr);
  405. adev->doorbell.ptr = NULL;
  406. }
  407. /**
  408. * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
  409. * setup amdkfd
  410. *
  411. * @adev: amdgpu_device pointer
  412. * @aperture_base: output returning doorbell aperture base physical address
  413. * @aperture_size: output returning doorbell aperture size in bytes
  414. * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
  415. *
  416. * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
  417. * takes doorbells required for its own rings and reports the setup to amdkfd.
  418. * amdgpu reserved doorbells are at the start of the doorbell aperture.
  419. */
  420. void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
  421. phys_addr_t *aperture_base,
  422. size_t *aperture_size,
  423. size_t *start_offset)
  424. {
  425. /*
  426. * The first num_doorbells are used by amdgpu.
  427. * amdkfd takes whatever's left in the aperture.
  428. */
  429. if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
  430. *aperture_base = adev->doorbell.base;
  431. *aperture_size = adev->doorbell.size;
  432. *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
  433. } else {
  434. *aperture_base = 0;
  435. *aperture_size = 0;
  436. *start_offset = 0;
  437. }
  438. }
  439. /*
  440. * amdgpu_wb_*()
  441. * Writeback is the the method by which the the GPU updates special pages
  442. * in memory with the status of certain GPU events (fences, ring pointers,
  443. * etc.).
  444. */
  445. /**
  446. * amdgpu_wb_fini - Disable Writeback and free memory
  447. *
  448. * @adev: amdgpu_device pointer
  449. *
  450. * Disables Writeback and frees the Writeback memory (all asics).
  451. * Used at driver shutdown.
  452. */
  453. static void amdgpu_wb_fini(struct amdgpu_device *adev)
  454. {
  455. if (adev->wb.wb_obj) {
  456. amdgpu_bo_free_kernel(&adev->wb.wb_obj,
  457. &adev->wb.gpu_addr,
  458. (void **)&adev->wb.wb);
  459. adev->wb.wb_obj = NULL;
  460. }
  461. }
  462. /**
  463. * amdgpu_wb_init- Init Writeback driver info and allocate memory
  464. *
  465. * @adev: amdgpu_device pointer
  466. *
  467. * Disables Writeback and frees the Writeback memory (all asics).
  468. * Used at driver startup.
  469. * Returns 0 on success or an -error on failure.
  470. */
  471. static int amdgpu_wb_init(struct amdgpu_device *adev)
  472. {
  473. int r;
  474. if (adev->wb.wb_obj == NULL) {
  475. r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
  476. PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
  477. &adev->wb.wb_obj, &adev->wb.gpu_addr,
  478. (void **)&adev->wb.wb);
  479. if (r) {
  480. dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
  481. return r;
  482. }
  483. adev->wb.num_wb = AMDGPU_MAX_WB;
  484. memset(&adev->wb.used, 0, sizeof(adev->wb.used));
  485. /* clear wb memory */
  486. memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
  487. }
  488. return 0;
  489. }
  490. /**
  491. * amdgpu_wb_get - Allocate a wb entry
  492. *
  493. * @adev: amdgpu_device pointer
  494. * @wb: wb index
  495. *
  496. * Allocate a wb slot for use by the driver (all asics).
  497. * Returns 0 on success or -EINVAL on failure.
  498. */
  499. int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
  500. {
  501. unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
  502. if (offset < adev->wb.num_wb) {
  503. __set_bit(offset, adev->wb.used);
  504. *wb = offset;
  505. return 0;
  506. } else {
  507. return -EINVAL;
  508. }
  509. }
  510. /**
  511. * amdgpu_wb_get_64bit - Allocate a wb entry
  512. *
  513. * @adev: amdgpu_device pointer
  514. * @wb: wb index
  515. *
  516. * Allocate a wb slot for use by the driver (all asics).
  517. * Returns 0 on success or -EINVAL on failure.
  518. */
  519. int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
  520. {
  521. unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
  522. adev->wb.num_wb, 0, 2, 7, 0);
  523. if ((offset + 1) < adev->wb.num_wb) {
  524. __set_bit(offset, adev->wb.used);
  525. __set_bit(offset + 1, adev->wb.used);
  526. *wb = offset;
  527. return 0;
  528. } else {
  529. return -EINVAL;
  530. }
  531. }
  532. /**
  533. * amdgpu_wb_free - Free a wb entry
  534. *
  535. * @adev: amdgpu_device pointer
  536. * @wb: wb index
  537. *
  538. * Free a wb slot allocated for use by the driver (all asics)
  539. */
  540. void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
  541. {
  542. if (wb < adev->wb.num_wb)
  543. __clear_bit(wb, adev->wb.used);
  544. }
  545. /**
  546. * amdgpu_wb_free_64bit - Free a wb entry
  547. *
  548. * @adev: amdgpu_device pointer
  549. * @wb: wb index
  550. *
  551. * Free a wb slot allocated for use by the driver (all asics)
  552. */
  553. void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
  554. {
  555. if ((wb + 1) < adev->wb.num_wb) {
  556. __clear_bit(wb, adev->wb.used);
  557. __clear_bit(wb + 1, adev->wb.used);
  558. }
  559. }
  560. /**
  561. * amdgpu_vram_location - try to find VRAM location
  562. * @adev: amdgpu device structure holding all necessary informations
  563. * @mc: memory controller structure holding memory informations
  564. * @base: base address at which to put VRAM
  565. *
  566. * Function will place try to place VRAM at base address provided
  567. * as parameter (which is so far either PCI aperture address or
  568. * for IGP TOM base address).
  569. *
  570. * If there is not enough space to fit the unvisible VRAM in the 32bits
  571. * address space then we limit the VRAM size to the aperture.
  572. *
  573. * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
  574. * this shouldn't be a problem as we are using the PCI aperture as a reference.
  575. * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
  576. * not IGP.
  577. *
  578. * Note: we use mc_vram_size as on some board we need to program the mc to
  579. * cover the whole aperture even if VRAM size is inferior to aperture size
  580. * Novell bug 204882 + along with lots of ubuntu ones
  581. *
  582. * Note: when limiting vram it's safe to overwritte real_vram_size because
  583. * we are not in case where real_vram_size is inferior to mc_vram_size (ie
  584. * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
  585. * ones)
  586. *
  587. * Note: IGP TOM addr should be the same as the aperture addr, we don't
  588. * explicitly check for that thought.
  589. *
  590. * FIXME: when reducing VRAM size align new size on power of 2.
  591. */
  592. void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
  593. {
  594. uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
  595. mc->vram_start = base;
  596. if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
  597. dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
  598. mc->real_vram_size = mc->aper_size;
  599. mc->mc_vram_size = mc->aper_size;
  600. }
  601. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  602. if (limit && limit < mc->real_vram_size)
  603. mc->real_vram_size = limit;
  604. dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  605. mc->mc_vram_size >> 20, mc->vram_start,
  606. mc->vram_end, mc->real_vram_size >> 20);
  607. }
  608. /**
  609. * amdgpu_gtt_location - try to find GTT location
  610. * @adev: amdgpu device structure holding all necessary informations
  611. * @mc: memory controller structure holding memory informations
  612. *
  613. * Function will place try to place GTT before or after VRAM.
  614. *
  615. * If GTT size is bigger than space left then we ajust GTT size.
  616. * Thus function will never fails.
  617. *
  618. * FIXME: when reducing GTT size align new size on power of 2.
  619. */
  620. void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
  621. {
  622. u64 size_af, size_bf;
  623. size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
  624. size_bf = mc->vram_start & ~mc->gtt_base_align;
  625. if (size_bf > size_af) {
  626. if (mc->gtt_size > size_bf) {
  627. dev_warn(adev->dev, "limiting GTT\n");
  628. mc->gtt_size = size_bf;
  629. }
  630. mc->gtt_start = 0;
  631. } else {
  632. if (mc->gtt_size > size_af) {
  633. dev_warn(adev->dev, "limiting GTT\n");
  634. mc->gtt_size = size_af;
  635. }
  636. mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
  637. }
  638. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  639. dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  640. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  641. }
  642. /*
  643. * GPU helpers function.
  644. */
  645. /**
  646. * amdgpu_need_post - check if the hw need post or not
  647. *
  648. * @adev: amdgpu_device pointer
  649. *
  650. * Check if the asic has been initialized (all asics) at driver startup
  651. * or post is needed if hw reset is performed.
  652. * Returns true if need or false if not.
  653. */
  654. bool amdgpu_need_post(struct amdgpu_device *adev)
  655. {
  656. uint32_t reg;
  657. if (adev->has_hw_reset) {
  658. adev->has_hw_reset = false;
  659. return true;
  660. }
  661. /* then check MEM_SIZE, in case the crtcs are off */
  662. reg = amdgpu_asic_get_config_memsize(adev);
  663. if ((reg != 0) && (reg != 0xffffffff))
  664. return false;
  665. return true;
  666. }
  667. static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
  668. {
  669. if (amdgpu_sriov_vf(adev))
  670. return false;
  671. if (amdgpu_passthrough(adev)) {
  672. /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
  673. * some old smc fw still need driver do vPost otherwise gpu hang, while
  674. * those smc fw version above 22.15 doesn't have this flaw, so we force
  675. * vpost executed for smc version below 22.15
  676. */
  677. if (adev->asic_type == CHIP_FIJI) {
  678. int err;
  679. uint32_t fw_ver;
  680. err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
  681. /* force vPost if error occured */
  682. if (err)
  683. return true;
  684. fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
  685. if (fw_ver < 0x00160e00)
  686. return true;
  687. }
  688. }
  689. return amdgpu_need_post(adev);
  690. }
  691. /**
  692. * amdgpu_dummy_page_init - init dummy page used by the driver
  693. *
  694. * @adev: amdgpu_device pointer
  695. *
  696. * Allocate the dummy page used by the driver (all asics).
  697. * This dummy page is used by the driver as a filler for gart entries
  698. * when pages are taken out of the GART
  699. * Returns 0 on sucess, -ENOMEM on failure.
  700. */
  701. int amdgpu_dummy_page_init(struct amdgpu_device *adev)
  702. {
  703. if (adev->dummy_page.page)
  704. return 0;
  705. adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  706. if (adev->dummy_page.page == NULL)
  707. return -ENOMEM;
  708. adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
  709. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  710. if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
  711. dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  712. __free_page(adev->dummy_page.page);
  713. adev->dummy_page.page = NULL;
  714. return -ENOMEM;
  715. }
  716. return 0;
  717. }
  718. /**
  719. * amdgpu_dummy_page_fini - free dummy page used by the driver
  720. *
  721. * @adev: amdgpu_device pointer
  722. *
  723. * Frees the dummy page used by the driver (all asics).
  724. */
  725. void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
  726. {
  727. if (adev->dummy_page.page == NULL)
  728. return;
  729. pci_unmap_page(adev->pdev, adev->dummy_page.addr,
  730. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  731. __free_page(adev->dummy_page.page);
  732. adev->dummy_page.page = NULL;
  733. }
  734. /* ATOM accessor methods */
  735. /*
  736. * ATOM is an interpreted byte code stored in tables in the vbios. The
  737. * driver registers callbacks to access registers and the interpreter
  738. * in the driver parses the tables and executes then to program specific
  739. * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
  740. * atombios.h, and atom.c
  741. */
  742. /**
  743. * cail_pll_read - read PLL register
  744. *
  745. * @info: atom card_info pointer
  746. * @reg: PLL register offset
  747. *
  748. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  749. * Returns the value of the PLL register.
  750. */
  751. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  752. {
  753. return 0;
  754. }
  755. /**
  756. * cail_pll_write - write PLL register
  757. *
  758. * @info: atom card_info pointer
  759. * @reg: PLL register offset
  760. * @val: value to write to the pll register
  761. *
  762. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  763. */
  764. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  765. {
  766. }
  767. /**
  768. * cail_mc_read - read MC (Memory Controller) register
  769. *
  770. * @info: atom card_info pointer
  771. * @reg: MC register offset
  772. *
  773. * Provides an MC register accessor for the atom interpreter (r4xx+).
  774. * Returns the value of the MC register.
  775. */
  776. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  777. {
  778. return 0;
  779. }
  780. /**
  781. * cail_mc_write - write MC (Memory Controller) register
  782. *
  783. * @info: atom card_info pointer
  784. * @reg: MC register offset
  785. * @val: value to write to the pll register
  786. *
  787. * Provides a MC register accessor for the atom interpreter (r4xx+).
  788. */
  789. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  790. {
  791. }
  792. /**
  793. * cail_reg_write - write MMIO register
  794. *
  795. * @info: atom card_info pointer
  796. * @reg: MMIO register offset
  797. * @val: value to write to the pll register
  798. *
  799. * Provides a MMIO register accessor for the atom interpreter (r4xx+).
  800. */
  801. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  802. {
  803. struct amdgpu_device *adev = info->dev->dev_private;
  804. WREG32(reg, val);
  805. }
  806. /**
  807. * cail_reg_read - read MMIO register
  808. *
  809. * @info: atom card_info pointer
  810. * @reg: MMIO register offset
  811. *
  812. * Provides an MMIO register accessor for the atom interpreter (r4xx+).
  813. * Returns the value of the MMIO register.
  814. */
  815. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  816. {
  817. struct amdgpu_device *adev = info->dev->dev_private;
  818. uint32_t r;
  819. r = RREG32(reg);
  820. return r;
  821. }
  822. /**
  823. * cail_ioreg_write - write IO register
  824. *
  825. * @info: atom card_info pointer
  826. * @reg: IO register offset
  827. * @val: value to write to the pll register
  828. *
  829. * Provides a IO register accessor for the atom interpreter (r4xx+).
  830. */
  831. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  832. {
  833. struct amdgpu_device *adev = info->dev->dev_private;
  834. WREG32_IO(reg, val);
  835. }
  836. /**
  837. * cail_ioreg_read - read IO register
  838. *
  839. * @info: atom card_info pointer
  840. * @reg: IO register offset
  841. *
  842. * Provides an IO register accessor for the atom interpreter (r4xx+).
  843. * Returns the value of the IO register.
  844. */
  845. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  846. {
  847. struct amdgpu_device *adev = info->dev->dev_private;
  848. uint32_t r;
  849. r = RREG32_IO(reg);
  850. return r;
  851. }
  852. /**
  853. * amdgpu_atombios_fini - free the driver info and callbacks for atombios
  854. *
  855. * @adev: amdgpu_device pointer
  856. *
  857. * Frees the driver info and register access callbacks for the ATOM
  858. * interpreter (r4xx+).
  859. * Called at driver shutdown.
  860. */
  861. static void amdgpu_atombios_fini(struct amdgpu_device *adev)
  862. {
  863. if (adev->mode_info.atom_context) {
  864. kfree(adev->mode_info.atom_context->scratch);
  865. kfree(adev->mode_info.atom_context->iio);
  866. }
  867. kfree(adev->mode_info.atom_context);
  868. adev->mode_info.atom_context = NULL;
  869. kfree(adev->mode_info.atom_card_info);
  870. adev->mode_info.atom_card_info = NULL;
  871. }
  872. /**
  873. * amdgpu_atombios_init - init the driver info and callbacks for atombios
  874. *
  875. * @adev: amdgpu_device pointer
  876. *
  877. * Initializes the driver info and register access callbacks for the
  878. * ATOM interpreter (r4xx+).
  879. * Returns 0 on sucess, -ENOMEM on failure.
  880. * Called at driver startup.
  881. */
  882. static int amdgpu_atombios_init(struct amdgpu_device *adev)
  883. {
  884. struct card_info *atom_card_info =
  885. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  886. if (!atom_card_info)
  887. return -ENOMEM;
  888. adev->mode_info.atom_card_info = atom_card_info;
  889. atom_card_info->dev = adev->ddev;
  890. atom_card_info->reg_read = cail_reg_read;
  891. atom_card_info->reg_write = cail_reg_write;
  892. /* needed for iio ops */
  893. if (adev->rio_mem) {
  894. atom_card_info->ioreg_read = cail_ioreg_read;
  895. atom_card_info->ioreg_write = cail_ioreg_write;
  896. } else {
  897. DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
  898. atom_card_info->ioreg_read = cail_reg_read;
  899. atom_card_info->ioreg_write = cail_reg_write;
  900. }
  901. atom_card_info->mc_read = cail_mc_read;
  902. atom_card_info->mc_write = cail_mc_write;
  903. atom_card_info->pll_read = cail_pll_read;
  904. atom_card_info->pll_write = cail_pll_write;
  905. adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
  906. if (!adev->mode_info.atom_context) {
  907. amdgpu_atombios_fini(adev);
  908. return -ENOMEM;
  909. }
  910. mutex_init(&adev->mode_info.atom_context->mutex);
  911. if (adev->is_atom_fw) {
  912. amdgpu_atomfirmware_scratch_regs_init(adev);
  913. amdgpu_atomfirmware_allocate_fb_scratch(adev);
  914. } else {
  915. amdgpu_atombios_scratch_regs_init(adev);
  916. amdgpu_atombios_allocate_fb_scratch(adev);
  917. }
  918. return 0;
  919. }
  920. /* if we get transitioned to only one device, take VGA back */
  921. /**
  922. * amdgpu_vga_set_decode - enable/disable vga decode
  923. *
  924. * @cookie: amdgpu_device pointer
  925. * @state: enable/disable vga decode
  926. *
  927. * Enable/disable vga decode (all asics).
  928. * Returns VGA resource flags.
  929. */
  930. static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
  931. {
  932. struct amdgpu_device *adev = cookie;
  933. amdgpu_asic_set_vga_state(adev, state);
  934. if (state)
  935. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  936. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  937. else
  938. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  939. }
  940. /**
  941. * amdgpu_check_pot_argument - check that argument is a power of two
  942. *
  943. * @arg: value to check
  944. *
  945. * Validates that a certain argument is a power of two (all asics).
  946. * Returns true if argument is valid.
  947. */
  948. static bool amdgpu_check_pot_argument(int arg)
  949. {
  950. return (arg & (arg - 1)) == 0;
  951. }
  952. static void amdgpu_check_block_size(struct amdgpu_device *adev)
  953. {
  954. /* defines number of bits in page table versus page directory,
  955. * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
  956. * page table and the remaining bits are in the page directory */
  957. if (amdgpu_vm_block_size == -1)
  958. return;
  959. if (amdgpu_vm_block_size < 9) {
  960. dev_warn(adev->dev, "VM page table size (%d) too small\n",
  961. amdgpu_vm_block_size);
  962. goto def_value;
  963. }
  964. if (amdgpu_vm_block_size > 24 ||
  965. (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
  966. dev_warn(adev->dev, "VM page table size (%d) too large\n",
  967. amdgpu_vm_block_size);
  968. goto def_value;
  969. }
  970. return;
  971. def_value:
  972. amdgpu_vm_block_size = -1;
  973. }
  974. static void amdgpu_check_vm_size(struct amdgpu_device *adev)
  975. {
  976. if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
  977. dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
  978. amdgpu_vm_size);
  979. goto def_value;
  980. }
  981. if (amdgpu_vm_size < 1) {
  982. dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
  983. amdgpu_vm_size);
  984. goto def_value;
  985. }
  986. /*
  987. * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
  988. */
  989. if (amdgpu_vm_size > 1024) {
  990. dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
  991. amdgpu_vm_size);
  992. goto def_value;
  993. }
  994. return;
  995. def_value:
  996. amdgpu_vm_size = -1;
  997. }
  998. /**
  999. * amdgpu_check_arguments - validate module params
  1000. *
  1001. * @adev: amdgpu_device pointer
  1002. *
  1003. * Validates certain module parameters and updates
  1004. * the associated values used by the driver (all asics).
  1005. */
  1006. static void amdgpu_check_arguments(struct amdgpu_device *adev)
  1007. {
  1008. if (amdgpu_sched_jobs < 4) {
  1009. dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
  1010. amdgpu_sched_jobs);
  1011. amdgpu_sched_jobs = 4;
  1012. } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
  1013. dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
  1014. amdgpu_sched_jobs);
  1015. amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
  1016. }
  1017. if (amdgpu_gart_size != -1) {
  1018. /* gtt size must be greater or equal to 32M */
  1019. if (amdgpu_gart_size < 32) {
  1020. dev_warn(adev->dev, "gart size (%d) too small\n",
  1021. amdgpu_gart_size);
  1022. amdgpu_gart_size = -1;
  1023. }
  1024. }
  1025. amdgpu_check_vm_size(adev);
  1026. amdgpu_check_block_size(adev);
  1027. if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
  1028. !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
  1029. dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
  1030. amdgpu_vram_page_split);
  1031. amdgpu_vram_page_split = 1024;
  1032. }
  1033. }
  1034. /**
  1035. * amdgpu_switcheroo_set_state - set switcheroo state
  1036. *
  1037. * @pdev: pci dev pointer
  1038. * @state: vga_switcheroo state
  1039. *
  1040. * Callback for the switcheroo driver. Suspends or resumes the
  1041. * the asics before or after it is powered up using ACPI methods.
  1042. */
  1043. static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  1044. {
  1045. struct drm_device *dev = pci_get_drvdata(pdev);
  1046. if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
  1047. return;
  1048. if (state == VGA_SWITCHEROO_ON) {
  1049. unsigned d3_delay = dev->pdev->d3_delay;
  1050. pr_info("amdgpu: switched on\n");
  1051. /* don't suspend or resume card normally */
  1052. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  1053. amdgpu_device_resume(dev, true, true);
  1054. dev->pdev->d3_delay = d3_delay;
  1055. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  1056. drm_kms_helper_poll_enable(dev);
  1057. } else {
  1058. pr_info("amdgpu: switched off\n");
  1059. drm_kms_helper_poll_disable(dev);
  1060. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  1061. amdgpu_device_suspend(dev, true, true);
  1062. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  1063. }
  1064. }
  1065. /**
  1066. * amdgpu_switcheroo_can_switch - see if switcheroo state can change
  1067. *
  1068. * @pdev: pci dev pointer
  1069. *
  1070. * Callback for the switcheroo driver. Check of the switcheroo
  1071. * state can be changed.
  1072. * Returns true if the state can be changed, false if not.
  1073. */
  1074. static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
  1075. {
  1076. struct drm_device *dev = pci_get_drvdata(pdev);
  1077. /*
  1078. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  1079. * locking inversion with the driver load path. And the access here is
  1080. * completely racy anyway. So don't bother with locking for now.
  1081. */
  1082. return dev->open_count == 0;
  1083. }
  1084. static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
  1085. .set_gpu_state = amdgpu_switcheroo_set_state,
  1086. .reprobe = NULL,
  1087. .can_switch = amdgpu_switcheroo_can_switch,
  1088. };
  1089. int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
  1090. enum amd_ip_block_type block_type,
  1091. enum amd_clockgating_state state)
  1092. {
  1093. int i, r = 0;
  1094. for (i = 0; i < adev->num_ip_blocks; i++) {
  1095. if (!adev->ip_blocks[i].status.valid)
  1096. continue;
  1097. if (adev->ip_blocks[i].version->type != block_type)
  1098. continue;
  1099. if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
  1100. continue;
  1101. r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
  1102. (void *)adev, state);
  1103. if (r)
  1104. DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
  1105. adev->ip_blocks[i].version->funcs->name, r);
  1106. }
  1107. return r;
  1108. }
  1109. int amdgpu_set_powergating_state(struct amdgpu_device *adev,
  1110. enum amd_ip_block_type block_type,
  1111. enum amd_powergating_state state)
  1112. {
  1113. int i, r = 0;
  1114. for (i = 0; i < adev->num_ip_blocks; i++) {
  1115. if (!adev->ip_blocks[i].status.valid)
  1116. continue;
  1117. if (adev->ip_blocks[i].version->type != block_type)
  1118. continue;
  1119. if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
  1120. continue;
  1121. r = adev->ip_blocks[i].version->funcs->set_powergating_state(
  1122. (void *)adev, state);
  1123. if (r)
  1124. DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
  1125. adev->ip_blocks[i].version->funcs->name, r);
  1126. }
  1127. return r;
  1128. }
  1129. void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
  1130. {
  1131. int i;
  1132. for (i = 0; i < adev->num_ip_blocks; i++) {
  1133. if (!adev->ip_blocks[i].status.valid)
  1134. continue;
  1135. if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
  1136. adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
  1137. }
  1138. }
  1139. int amdgpu_wait_for_idle(struct amdgpu_device *adev,
  1140. enum amd_ip_block_type block_type)
  1141. {
  1142. int i, r;
  1143. for (i = 0; i < adev->num_ip_blocks; i++) {
  1144. if (!adev->ip_blocks[i].status.valid)
  1145. continue;
  1146. if (adev->ip_blocks[i].version->type == block_type) {
  1147. r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
  1148. if (r)
  1149. return r;
  1150. break;
  1151. }
  1152. }
  1153. return 0;
  1154. }
  1155. bool amdgpu_is_idle(struct amdgpu_device *adev,
  1156. enum amd_ip_block_type block_type)
  1157. {
  1158. int i;
  1159. for (i = 0; i < adev->num_ip_blocks; i++) {
  1160. if (!adev->ip_blocks[i].status.valid)
  1161. continue;
  1162. if (adev->ip_blocks[i].version->type == block_type)
  1163. return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
  1164. }
  1165. return true;
  1166. }
  1167. struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
  1168. enum amd_ip_block_type type)
  1169. {
  1170. int i;
  1171. for (i = 0; i < adev->num_ip_blocks; i++)
  1172. if (adev->ip_blocks[i].version->type == type)
  1173. return &adev->ip_blocks[i];
  1174. return NULL;
  1175. }
  1176. /**
  1177. * amdgpu_ip_block_version_cmp
  1178. *
  1179. * @adev: amdgpu_device pointer
  1180. * @type: enum amd_ip_block_type
  1181. * @major: major version
  1182. * @minor: minor version
  1183. *
  1184. * return 0 if equal or greater
  1185. * return 1 if smaller or the ip_block doesn't exist
  1186. */
  1187. int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
  1188. enum amd_ip_block_type type,
  1189. u32 major, u32 minor)
  1190. {
  1191. struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
  1192. if (ip_block && ((ip_block->version->major > major) ||
  1193. ((ip_block->version->major == major) &&
  1194. (ip_block->version->minor >= minor))))
  1195. return 0;
  1196. return 1;
  1197. }
  1198. /**
  1199. * amdgpu_ip_block_add
  1200. *
  1201. * @adev: amdgpu_device pointer
  1202. * @ip_block_version: pointer to the IP to add
  1203. *
  1204. * Adds the IP block driver information to the collection of IPs
  1205. * on the asic.
  1206. */
  1207. int amdgpu_ip_block_add(struct amdgpu_device *adev,
  1208. const struct amdgpu_ip_block_version *ip_block_version)
  1209. {
  1210. if (!ip_block_version)
  1211. return -EINVAL;
  1212. adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
  1213. return 0;
  1214. }
  1215. static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
  1216. {
  1217. adev->enable_virtual_display = false;
  1218. if (amdgpu_virtual_display) {
  1219. struct drm_device *ddev = adev->ddev;
  1220. const char *pci_address_name = pci_name(ddev->pdev);
  1221. char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
  1222. pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
  1223. pciaddstr_tmp = pciaddstr;
  1224. while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
  1225. pciaddname = strsep(&pciaddname_tmp, ",");
  1226. if (!strcmp("all", pciaddname)
  1227. || !strcmp(pci_address_name, pciaddname)) {
  1228. long num_crtc;
  1229. int res = -1;
  1230. adev->enable_virtual_display = true;
  1231. if (pciaddname_tmp)
  1232. res = kstrtol(pciaddname_tmp, 10,
  1233. &num_crtc);
  1234. if (!res) {
  1235. if (num_crtc < 1)
  1236. num_crtc = 1;
  1237. if (num_crtc > 6)
  1238. num_crtc = 6;
  1239. adev->mode_info.num_crtc = num_crtc;
  1240. } else {
  1241. adev->mode_info.num_crtc = 1;
  1242. }
  1243. break;
  1244. }
  1245. }
  1246. DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
  1247. amdgpu_virtual_display, pci_address_name,
  1248. adev->enable_virtual_display, adev->mode_info.num_crtc);
  1249. kfree(pciaddstr);
  1250. }
  1251. }
  1252. static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
  1253. {
  1254. const struct firmware *fw;
  1255. const char *chip_name;
  1256. char fw_name[30];
  1257. int err;
  1258. const struct gpu_info_firmware_header_v1_0 *hdr;
  1259. switch (adev->asic_type) {
  1260. case CHIP_TOPAZ:
  1261. case CHIP_TONGA:
  1262. case CHIP_FIJI:
  1263. case CHIP_POLARIS11:
  1264. case CHIP_POLARIS10:
  1265. case CHIP_POLARIS12:
  1266. case CHIP_CARRIZO:
  1267. case CHIP_STONEY:
  1268. #ifdef CONFIG_DRM_AMDGPU_SI
  1269. case CHIP_VERDE:
  1270. case CHIP_TAHITI:
  1271. case CHIP_PITCAIRN:
  1272. case CHIP_OLAND:
  1273. case CHIP_HAINAN:
  1274. #endif
  1275. #ifdef CONFIG_DRM_AMDGPU_CIK
  1276. case CHIP_BONAIRE:
  1277. case CHIP_HAWAII:
  1278. case CHIP_KAVERI:
  1279. case CHIP_KABINI:
  1280. case CHIP_MULLINS:
  1281. #endif
  1282. default:
  1283. return 0;
  1284. case CHIP_VEGA10:
  1285. chip_name = "vega10";
  1286. break;
  1287. }
  1288. snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
  1289. err = request_firmware(&fw, fw_name, adev->dev);
  1290. if (err) {
  1291. dev_err(adev->dev,
  1292. "Failed to load gpu_info firmware \"%s\"\n",
  1293. fw_name);
  1294. goto out;
  1295. }
  1296. err = amdgpu_ucode_validate(fw);
  1297. if (err) {
  1298. dev_err(adev->dev,
  1299. "Failed to validate gpu_info firmware \"%s\"\n",
  1300. fw_name);
  1301. goto out;
  1302. }
  1303. hdr = (const struct gpu_info_firmware_header_v1_0 *)fw->data;
  1304. amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
  1305. switch (hdr->version_major) {
  1306. case 1:
  1307. {
  1308. const struct gpu_info_firmware_v1_0 *gpu_info_fw =
  1309. (const struct gpu_info_firmware_v1_0 *)(fw->data +
  1310. le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  1311. adev->gfx.config.max_shader_engines = gpu_info_fw->gc_num_se;
  1312. adev->gfx.config.max_cu_per_sh = gpu_info_fw->gc_num_cu_per_sh;
  1313. adev->gfx.config.max_sh_per_se = gpu_info_fw->gc_num_sh_per_se;
  1314. adev->gfx.config.max_backends_per_se = gpu_info_fw->gc_num_rb_per_se;
  1315. adev->gfx.config.max_texture_channel_caches =
  1316. gpu_info_fw->gc_num_tccs;
  1317. adev->gfx.config.max_gprs = gpu_info_fw->gc_num_gprs;
  1318. adev->gfx.config.max_gs_threads = gpu_info_fw->gc_num_max_gs_thds;
  1319. adev->gfx.config.gs_vgt_table_depth = gpu_info_fw->gc_gs_table_depth;
  1320. adev->gfx.config.gs_prim_buffer_depth = gpu_info_fw->gc_gsprim_buff_depth;
  1321. adev->gfx.config.double_offchip_lds_buf =
  1322. gpu_info_fw->gc_double_offchip_lds_buffer;
  1323. adev->gfx.cu_info.wave_front_size = gpu_info_fw->gc_wave_size;
  1324. break;
  1325. }
  1326. default:
  1327. dev_err(adev->dev,
  1328. "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
  1329. err = -EINVAL;
  1330. goto out;
  1331. }
  1332. out:
  1333. release_firmware(fw);
  1334. fw = NULL;
  1335. return err;
  1336. }
  1337. static int amdgpu_early_init(struct amdgpu_device *adev)
  1338. {
  1339. int i, r;
  1340. amdgpu_device_enable_virtual_display(adev);
  1341. switch (adev->asic_type) {
  1342. case CHIP_TOPAZ:
  1343. case CHIP_TONGA:
  1344. case CHIP_FIJI:
  1345. case CHIP_POLARIS11:
  1346. case CHIP_POLARIS10:
  1347. case CHIP_POLARIS12:
  1348. case CHIP_CARRIZO:
  1349. case CHIP_STONEY:
  1350. if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
  1351. adev->family = AMDGPU_FAMILY_CZ;
  1352. else
  1353. adev->family = AMDGPU_FAMILY_VI;
  1354. r = vi_set_ip_blocks(adev);
  1355. if (r)
  1356. return r;
  1357. break;
  1358. #ifdef CONFIG_DRM_AMDGPU_SI
  1359. case CHIP_VERDE:
  1360. case CHIP_TAHITI:
  1361. case CHIP_PITCAIRN:
  1362. case CHIP_OLAND:
  1363. case CHIP_HAINAN:
  1364. adev->family = AMDGPU_FAMILY_SI;
  1365. r = si_set_ip_blocks(adev);
  1366. if (r)
  1367. return r;
  1368. break;
  1369. #endif
  1370. #ifdef CONFIG_DRM_AMDGPU_CIK
  1371. case CHIP_BONAIRE:
  1372. case CHIP_HAWAII:
  1373. case CHIP_KAVERI:
  1374. case CHIP_KABINI:
  1375. case CHIP_MULLINS:
  1376. if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
  1377. adev->family = AMDGPU_FAMILY_CI;
  1378. else
  1379. adev->family = AMDGPU_FAMILY_KV;
  1380. r = cik_set_ip_blocks(adev);
  1381. if (r)
  1382. return r;
  1383. break;
  1384. #endif
  1385. case CHIP_VEGA10:
  1386. adev->family = AMDGPU_FAMILY_AI;
  1387. r = soc15_set_ip_blocks(adev);
  1388. if (r)
  1389. return r;
  1390. break;
  1391. default:
  1392. /* FIXME: not supported yet */
  1393. return -EINVAL;
  1394. }
  1395. r = amdgpu_device_parse_gpu_info_fw(adev);
  1396. if (r)
  1397. return r;
  1398. if (amdgpu_sriov_vf(adev)) {
  1399. r = amdgpu_virt_request_full_gpu(adev, true);
  1400. if (r)
  1401. return r;
  1402. }
  1403. for (i = 0; i < adev->num_ip_blocks; i++) {
  1404. if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
  1405. DRM_ERROR("disabled ip block: %d\n", i);
  1406. adev->ip_blocks[i].status.valid = false;
  1407. } else {
  1408. if (adev->ip_blocks[i].version->funcs->early_init) {
  1409. r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
  1410. if (r == -ENOENT) {
  1411. adev->ip_blocks[i].status.valid = false;
  1412. } else if (r) {
  1413. DRM_ERROR("early_init of IP block <%s> failed %d\n",
  1414. adev->ip_blocks[i].version->funcs->name, r);
  1415. return r;
  1416. } else {
  1417. adev->ip_blocks[i].status.valid = true;
  1418. }
  1419. } else {
  1420. adev->ip_blocks[i].status.valid = true;
  1421. }
  1422. }
  1423. }
  1424. adev->cg_flags &= amdgpu_cg_mask;
  1425. adev->pg_flags &= amdgpu_pg_mask;
  1426. return 0;
  1427. }
  1428. static int amdgpu_init(struct amdgpu_device *adev)
  1429. {
  1430. int i, r;
  1431. for (i = 0; i < adev->num_ip_blocks; i++) {
  1432. if (!adev->ip_blocks[i].status.valid)
  1433. continue;
  1434. r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
  1435. if (r) {
  1436. DRM_ERROR("sw_init of IP block <%s> failed %d\n",
  1437. adev->ip_blocks[i].version->funcs->name, r);
  1438. return r;
  1439. }
  1440. adev->ip_blocks[i].status.sw = true;
  1441. /* need to do gmc hw init early so we can allocate gpu mem */
  1442. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1443. r = amdgpu_vram_scratch_init(adev);
  1444. if (r) {
  1445. DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
  1446. return r;
  1447. }
  1448. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1449. if (r) {
  1450. DRM_ERROR("hw_init %d failed %d\n", i, r);
  1451. return r;
  1452. }
  1453. r = amdgpu_wb_init(adev);
  1454. if (r) {
  1455. DRM_ERROR("amdgpu_wb_init failed %d\n", r);
  1456. return r;
  1457. }
  1458. adev->ip_blocks[i].status.hw = true;
  1459. /* right after GMC hw init, we create CSA */
  1460. if (amdgpu_sriov_vf(adev)) {
  1461. r = amdgpu_allocate_static_csa(adev);
  1462. if (r) {
  1463. DRM_ERROR("allocate CSA failed %d\n", r);
  1464. return r;
  1465. }
  1466. }
  1467. }
  1468. }
  1469. for (i = 0; i < adev->num_ip_blocks; i++) {
  1470. if (!adev->ip_blocks[i].status.sw)
  1471. continue;
  1472. /* gmc hw init is done early */
  1473. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
  1474. continue;
  1475. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1476. if (r) {
  1477. DRM_ERROR("hw_init of IP block <%s> failed %d\n",
  1478. adev->ip_blocks[i].version->funcs->name, r);
  1479. return r;
  1480. }
  1481. adev->ip_blocks[i].status.hw = true;
  1482. }
  1483. return 0;
  1484. }
  1485. static int amdgpu_late_init(struct amdgpu_device *adev)
  1486. {
  1487. int i = 0, r;
  1488. for (i = 0; i < adev->num_ip_blocks; i++) {
  1489. if (!adev->ip_blocks[i].status.valid)
  1490. continue;
  1491. if (adev->ip_blocks[i].version->funcs->late_init) {
  1492. r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
  1493. if (r) {
  1494. DRM_ERROR("late_init of IP block <%s> failed %d\n",
  1495. adev->ip_blocks[i].version->funcs->name, r);
  1496. return r;
  1497. }
  1498. adev->ip_blocks[i].status.late_initialized = true;
  1499. }
  1500. /* skip CG for VCE/UVD, it's handled specially */
  1501. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1502. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
  1503. /* enable clockgating to save power */
  1504. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1505. AMD_CG_STATE_GATE);
  1506. if (r) {
  1507. DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
  1508. adev->ip_blocks[i].version->funcs->name, r);
  1509. return r;
  1510. }
  1511. }
  1512. }
  1513. return 0;
  1514. }
  1515. static int amdgpu_fini(struct amdgpu_device *adev)
  1516. {
  1517. int i, r;
  1518. /* need to disable SMC first */
  1519. for (i = 0; i < adev->num_ip_blocks; i++) {
  1520. if (!adev->ip_blocks[i].status.hw)
  1521. continue;
  1522. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
  1523. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1524. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1525. AMD_CG_STATE_UNGATE);
  1526. if (r) {
  1527. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1528. adev->ip_blocks[i].version->funcs->name, r);
  1529. return r;
  1530. }
  1531. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1532. /* XXX handle errors */
  1533. if (r) {
  1534. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1535. adev->ip_blocks[i].version->funcs->name, r);
  1536. }
  1537. adev->ip_blocks[i].status.hw = false;
  1538. break;
  1539. }
  1540. }
  1541. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1542. if (!adev->ip_blocks[i].status.hw)
  1543. continue;
  1544. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1545. amdgpu_wb_fini(adev);
  1546. amdgpu_vram_scratch_fini(adev);
  1547. }
  1548. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1549. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
  1550. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1551. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1552. AMD_CG_STATE_UNGATE);
  1553. if (r) {
  1554. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1555. adev->ip_blocks[i].version->funcs->name, r);
  1556. return r;
  1557. }
  1558. }
  1559. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1560. /* XXX handle errors */
  1561. if (r) {
  1562. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1563. adev->ip_blocks[i].version->funcs->name, r);
  1564. }
  1565. adev->ip_blocks[i].status.hw = false;
  1566. }
  1567. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1568. if (!adev->ip_blocks[i].status.sw)
  1569. continue;
  1570. r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
  1571. /* XXX handle errors */
  1572. if (r) {
  1573. DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
  1574. adev->ip_blocks[i].version->funcs->name, r);
  1575. }
  1576. adev->ip_blocks[i].status.sw = false;
  1577. adev->ip_blocks[i].status.valid = false;
  1578. }
  1579. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1580. if (!adev->ip_blocks[i].status.late_initialized)
  1581. continue;
  1582. if (adev->ip_blocks[i].version->funcs->late_fini)
  1583. adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
  1584. adev->ip_blocks[i].status.late_initialized = false;
  1585. }
  1586. if (amdgpu_sriov_vf(adev)) {
  1587. amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
  1588. amdgpu_virt_release_full_gpu(adev, false);
  1589. }
  1590. return 0;
  1591. }
  1592. int amdgpu_suspend(struct amdgpu_device *adev)
  1593. {
  1594. int i, r;
  1595. if (amdgpu_sriov_vf(adev))
  1596. amdgpu_virt_request_full_gpu(adev, false);
  1597. /* ungate SMC block first */
  1598. r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
  1599. AMD_CG_STATE_UNGATE);
  1600. if (r) {
  1601. DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
  1602. }
  1603. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1604. if (!adev->ip_blocks[i].status.valid)
  1605. continue;
  1606. /* ungate blocks so that suspend can properly shut them down */
  1607. if (i != AMD_IP_BLOCK_TYPE_SMC) {
  1608. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1609. AMD_CG_STATE_UNGATE);
  1610. if (r) {
  1611. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1612. adev->ip_blocks[i].version->funcs->name, r);
  1613. }
  1614. }
  1615. /* XXX handle errors */
  1616. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1617. /* XXX handle errors */
  1618. if (r) {
  1619. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1620. adev->ip_blocks[i].version->funcs->name, r);
  1621. }
  1622. }
  1623. if (amdgpu_sriov_vf(adev))
  1624. amdgpu_virt_release_full_gpu(adev, false);
  1625. return 0;
  1626. }
  1627. static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
  1628. {
  1629. int i, r;
  1630. for (i = 0; i < adev->num_ip_blocks; i++) {
  1631. if (!adev->ip_blocks[i].status.valid)
  1632. continue;
  1633. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1634. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1635. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
  1636. r = adev->ip_blocks[i].version->funcs->hw_init(adev);
  1637. if (r) {
  1638. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1639. adev->ip_blocks[i].version->funcs->name, r);
  1640. return r;
  1641. }
  1642. }
  1643. return 0;
  1644. }
  1645. static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
  1646. {
  1647. int i, r;
  1648. for (i = 0; i < adev->num_ip_blocks; i++) {
  1649. if (!adev->ip_blocks[i].status.valid)
  1650. continue;
  1651. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1652. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1653. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
  1654. continue;
  1655. r = adev->ip_blocks[i].version->funcs->hw_init(adev);
  1656. if (r) {
  1657. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1658. adev->ip_blocks[i].version->funcs->name, r);
  1659. return r;
  1660. }
  1661. }
  1662. return 0;
  1663. }
  1664. static int amdgpu_resume(struct amdgpu_device *adev)
  1665. {
  1666. int i, r;
  1667. for (i = 0; i < adev->num_ip_blocks; i++) {
  1668. if (!adev->ip_blocks[i].status.valid)
  1669. continue;
  1670. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1671. if (r) {
  1672. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1673. adev->ip_blocks[i].version->funcs->name, r);
  1674. return r;
  1675. }
  1676. }
  1677. return 0;
  1678. }
  1679. static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
  1680. {
  1681. if (adev->is_atom_fw) {
  1682. if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
  1683. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1684. } else {
  1685. if (amdgpu_atombios_has_gpu_virtualization_table(adev))
  1686. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1687. }
  1688. }
  1689. /**
  1690. * amdgpu_device_init - initialize the driver
  1691. *
  1692. * @adev: amdgpu_device pointer
  1693. * @pdev: drm dev pointer
  1694. * @pdev: pci dev pointer
  1695. * @flags: driver flags
  1696. *
  1697. * Initializes the driver info and hw (all asics).
  1698. * Returns 0 for success or an error on failure.
  1699. * Called at driver startup.
  1700. */
  1701. int amdgpu_device_init(struct amdgpu_device *adev,
  1702. struct drm_device *ddev,
  1703. struct pci_dev *pdev,
  1704. uint32_t flags)
  1705. {
  1706. int r, i;
  1707. bool runtime = false;
  1708. u32 max_MBps;
  1709. adev->shutdown = false;
  1710. adev->dev = &pdev->dev;
  1711. adev->ddev = ddev;
  1712. adev->pdev = pdev;
  1713. adev->flags = flags;
  1714. adev->asic_type = flags & AMD_ASIC_MASK;
  1715. adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
  1716. adev->mc.gtt_size = 512 * 1024 * 1024;
  1717. adev->accel_working = false;
  1718. adev->num_rings = 0;
  1719. adev->mman.buffer_funcs = NULL;
  1720. adev->mman.buffer_funcs_ring = NULL;
  1721. adev->vm_manager.vm_pte_funcs = NULL;
  1722. adev->vm_manager.vm_pte_num_rings = 0;
  1723. adev->gart.gart_funcs = NULL;
  1724. adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  1725. adev->smc_rreg = &amdgpu_invalid_rreg;
  1726. adev->smc_wreg = &amdgpu_invalid_wreg;
  1727. adev->pcie_rreg = &amdgpu_invalid_rreg;
  1728. adev->pcie_wreg = &amdgpu_invalid_wreg;
  1729. adev->pciep_rreg = &amdgpu_invalid_rreg;
  1730. adev->pciep_wreg = &amdgpu_invalid_wreg;
  1731. adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
  1732. adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
  1733. adev->didt_rreg = &amdgpu_invalid_rreg;
  1734. adev->didt_wreg = &amdgpu_invalid_wreg;
  1735. adev->gc_cac_rreg = &amdgpu_invalid_rreg;
  1736. adev->gc_cac_wreg = &amdgpu_invalid_wreg;
  1737. adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
  1738. adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
  1739. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
  1740. amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
  1741. pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
  1742. /* mutex initialization are all done here so we
  1743. * can recall function without having locking issues */
  1744. atomic_set(&adev->irq.ih.lock, 0);
  1745. mutex_init(&adev->firmware.mutex);
  1746. mutex_init(&adev->pm.mutex);
  1747. mutex_init(&adev->gfx.gpu_clock_mutex);
  1748. mutex_init(&adev->srbm_mutex);
  1749. mutex_init(&adev->grbm_idx_mutex);
  1750. mutex_init(&adev->mn_lock);
  1751. hash_init(adev->mn_hash);
  1752. amdgpu_check_arguments(adev);
  1753. /* Registers mapping */
  1754. /* TODO: block userspace mapping of io register */
  1755. spin_lock_init(&adev->mmio_idx_lock);
  1756. spin_lock_init(&adev->smc_idx_lock);
  1757. spin_lock_init(&adev->pcie_idx_lock);
  1758. spin_lock_init(&adev->uvd_ctx_idx_lock);
  1759. spin_lock_init(&adev->didt_idx_lock);
  1760. spin_lock_init(&adev->gc_cac_idx_lock);
  1761. spin_lock_init(&adev->audio_endpt_idx_lock);
  1762. spin_lock_init(&adev->mm_stats.lock);
  1763. INIT_LIST_HEAD(&adev->shadow_list);
  1764. mutex_init(&adev->shadow_list_lock);
  1765. INIT_LIST_HEAD(&adev->gtt_list);
  1766. spin_lock_init(&adev->gtt_list_lock);
  1767. if (adev->asic_type >= CHIP_BONAIRE) {
  1768. adev->rmmio_base = pci_resource_start(adev->pdev, 5);
  1769. adev->rmmio_size = pci_resource_len(adev->pdev, 5);
  1770. } else {
  1771. adev->rmmio_base = pci_resource_start(adev->pdev, 2);
  1772. adev->rmmio_size = pci_resource_len(adev->pdev, 2);
  1773. }
  1774. adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
  1775. if (adev->rmmio == NULL) {
  1776. return -ENOMEM;
  1777. }
  1778. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
  1779. DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  1780. if (adev->asic_type >= CHIP_BONAIRE)
  1781. /* doorbell bar mapping */
  1782. amdgpu_doorbell_init(adev);
  1783. /* io port mapping */
  1784. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1785. if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
  1786. adev->rio_mem_size = pci_resource_len(adev->pdev, i);
  1787. adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
  1788. break;
  1789. }
  1790. }
  1791. if (adev->rio_mem == NULL)
  1792. DRM_INFO("PCI I/O BAR is not found.\n");
  1793. /* early init functions */
  1794. r = amdgpu_early_init(adev);
  1795. if (r)
  1796. return r;
  1797. /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
  1798. /* this will fail for cards that aren't VGA class devices, just
  1799. * ignore it */
  1800. vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
  1801. if (amdgpu_runtime_pm == 1)
  1802. runtime = true;
  1803. if (amdgpu_device_is_px(ddev))
  1804. runtime = true;
  1805. if (!pci_is_thunderbolt_attached(adev->pdev))
  1806. vga_switcheroo_register_client(adev->pdev,
  1807. &amdgpu_switcheroo_ops, runtime);
  1808. if (runtime)
  1809. vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
  1810. /* Read BIOS */
  1811. if (!amdgpu_get_bios(adev)) {
  1812. r = -EINVAL;
  1813. goto failed;
  1814. }
  1815. r = amdgpu_atombios_init(adev);
  1816. if (r) {
  1817. dev_err(adev->dev, "amdgpu_atombios_init failed\n");
  1818. goto failed;
  1819. }
  1820. /* detect if we are with an SRIOV vbios */
  1821. amdgpu_device_detect_sriov_bios(adev);
  1822. /* Post card if necessary */
  1823. if (amdgpu_vpost_needed(adev)) {
  1824. if (!adev->bios) {
  1825. dev_err(adev->dev, "no vBIOS found\n");
  1826. r = -EINVAL;
  1827. goto failed;
  1828. }
  1829. DRM_INFO("GPU posting now...\n");
  1830. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  1831. if (r) {
  1832. dev_err(adev->dev, "gpu post error!\n");
  1833. goto failed;
  1834. }
  1835. } else {
  1836. DRM_INFO("GPU post is not needed\n");
  1837. }
  1838. if (!adev->is_atom_fw) {
  1839. /* Initialize clocks */
  1840. r = amdgpu_atombios_get_clock_info(adev);
  1841. if (r) {
  1842. dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
  1843. return r;
  1844. }
  1845. /* init i2c buses */
  1846. amdgpu_atombios_i2c_init(adev);
  1847. }
  1848. /* Fence driver */
  1849. r = amdgpu_fence_driver_init(adev);
  1850. if (r) {
  1851. dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
  1852. goto failed;
  1853. }
  1854. /* init the mode config */
  1855. drm_mode_config_init(adev->ddev);
  1856. r = amdgpu_init(adev);
  1857. if (r) {
  1858. dev_err(adev->dev, "amdgpu_init failed\n");
  1859. amdgpu_fini(adev);
  1860. goto failed;
  1861. }
  1862. adev->accel_working = true;
  1863. /* Initialize the buffer migration limit. */
  1864. if (amdgpu_moverate >= 0)
  1865. max_MBps = amdgpu_moverate;
  1866. else
  1867. max_MBps = 8; /* Allow 8 MB/s. */
  1868. /* Get a log2 for easy divisions. */
  1869. adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
  1870. r = amdgpu_ib_pool_init(adev);
  1871. if (r) {
  1872. dev_err(adev->dev, "IB initialization failed (%d).\n", r);
  1873. goto failed;
  1874. }
  1875. r = amdgpu_ib_ring_tests(adev);
  1876. if (r)
  1877. DRM_ERROR("ib ring test failed (%d).\n", r);
  1878. amdgpu_fbdev_init(adev);
  1879. r = amdgpu_gem_debugfs_init(adev);
  1880. if (r)
  1881. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  1882. r = amdgpu_debugfs_regs_init(adev);
  1883. if (r)
  1884. DRM_ERROR("registering register debugfs failed (%d).\n", r);
  1885. r = amdgpu_debugfs_firmware_init(adev);
  1886. if (r)
  1887. DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
  1888. if ((amdgpu_testing & 1)) {
  1889. if (adev->accel_working)
  1890. amdgpu_test_moves(adev);
  1891. else
  1892. DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
  1893. }
  1894. if (amdgpu_benchmarking) {
  1895. if (adev->accel_working)
  1896. amdgpu_benchmark(adev, amdgpu_benchmarking);
  1897. else
  1898. DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
  1899. }
  1900. /* enable clockgating, etc. after ib tests, etc. since some blocks require
  1901. * explicit gating rather than handling it automatically.
  1902. */
  1903. r = amdgpu_late_init(adev);
  1904. if (r) {
  1905. dev_err(adev->dev, "amdgpu_late_init failed\n");
  1906. goto failed;
  1907. }
  1908. return 0;
  1909. failed:
  1910. if (runtime)
  1911. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  1912. return r;
  1913. }
  1914. /**
  1915. * amdgpu_device_fini - tear down the driver
  1916. *
  1917. * @adev: amdgpu_device pointer
  1918. *
  1919. * Tear down the driver info (all asics).
  1920. * Called at driver shutdown.
  1921. */
  1922. void amdgpu_device_fini(struct amdgpu_device *adev)
  1923. {
  1924. int r;
  1925. DRM_INFO("amdgpu: finishing device.\n");
  1926. adev->shutdown = true;
  1927. if (adev->mode_info.mode_config_initialized)
  1928. drm_crtc_force_disable_all(adev->ddev);
  1929. /* evict vram memory */
  1930. amdgpu_bo_evict_vram(adev);
  1931. amdgpu_ib_pool_fini(adev);
  1932. amdgpu_fence_driver_fini(adev);
  1933. amdgpu_fbdev_fini(adev);
  1934. r = amdgpu_fini(adev);
  1935. adev->accel_working = false;
  1936. /* free i2c buses */
  1937. amdgpu_i2c_fini(adev);
  1938. amdgpu_atombios_fini(adev);
  1939. kfree(adev->bios);
  1940. adev->bios = NULL;
  1941. if (!pci_is_thunderbolt_attached(adev->pdev))
  1942. vga_switcheroo_unregister_client(adev->pdev);
  1943. if (adev->flags & AMD_IS_PX)
  1944. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  1945. vga_client_register(adev->pdev, NULL, NULL, NULL);
  1946. if (adev->rio_mem)
  1947. pci_iounmap(adev->pdev, adev->rio_mem);
  1948. adev->rio_mem = NULL;
  1949. iounmap(adev->rmmio);
  1950. adev->rmmio = NULL;
  1951. if (adev->asic_type >= CHIP_BONAIRE)
  1952. amdgpu_doorbell_fini(adev);
  1953. amdgpu_debugfs_regs_cleanup(adev);
  1954. }
  1955. /*
  1956. * Suspend & resume.
  1957. */
  1958. /**
  1959. * amdgpu_device_suspend - initiate device suspend
  1960. *
  1961. * @pdev: drm dev pointer
  1962. * @state: suspend state
  1963. *
  1964. * Puts the hw in the suspend state (all asics).
  1965. * Returns 0 for success or an error on failure.
  1966. * Called at driver suspend.
  1967. */
  1968. int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
  1969. {
  1970. struct amdgpu_device *adev;
  1971. struct drm_crtc *crtc;
  1972. struct drm_connector *connector;
  1973. int r;
  1974. if (dev == NULL || dev->dev_private == NULL) {
  1975. return -ENODEV;
  1976. }
  1977. adev = dev->dev_private;
  1978. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1979. return 0;
  1980. drm_kms_helper_poll_disable(dev);
  1981. /* turn off display hw */
  1982. drm_modeset_lock_all(dev);
  1983. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1984. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  1985. }
  1986. drm_modeset_unlock_all(dev);
  1987. /* unpin the front buffers and cursors */
  1988. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1989. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1990. struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
  1991. struct amdgpu_bo *robj;
  1992. if (amdgpu_crtc->cursor_bo) {
  1993. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  1994. r = amdgpu_bo_reserve(aobj, true);
  1995. if (r == 0) {
  1996. amdgpu_bo_unpin(aobj);
  1997. amdgpu_bo_unreserve(aobj);
  1998. }
  1999. }
  2000. if (rfb == NULL || rfb->obj == NULL) {
  2001. continue;
  2002. }
  2003. robj = gem_to_amdgpu_bo(rfb->obj);
  2004. /* don't unpin kernel fb objects */
  2005. if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
  2006. r = amdgpu_bo_reserve(robj, true);
  2007. if (r == 0) {
  2008. amdgpu_bo_unpin(robj);
  2009. amdgpu_bo_unreserve(robj);
  2010. }
  2011. }
  2012. }
  2013. /* evict vram memory */
  2014. amdgpu_bo_evict_vram(adev);
  2015. amdgpu_fence_driver_suspend(adev);
  2016. r = amdgpu_suspend(adev);
  2017. /* evict remaining vram memory
  2018. * This second call to evict vram is to evict the gart page table
  2019. * using the CPU.
  2020. */
  2021. amdgpu_bo_evict_vram(adev);
  2022. if (adev->is_atom_fw)
  2023. amdgpu_atomfirmware_scratch_regs_save(adev);
  2024. else
  2025. amdgpu_atombios_scratch_regs_save(adev);
  2026. pci_save_state(dev->pdev);
  2027. if (suspend) {
  2028. /* Shut down the device */
  2029. pci_disable_device(dev->pdev);
  2030. pci_set_power_state(dev->pdev, PCI_D3hot);
  2031. } else {
  2032. r = amdgpu_asic_reset(adev);
  2033. if (r)
  2034. DRM_ERROR("amdgpu asic reset failed\n");
  2035. }
  2036. if (fbcon) {
  2037. console_lock();
  2038. amdgpu_fbdev_set_suspend(adev, 1);
  2039. console_unlock();
  2040. }
  2041. return 0;
  2042. }
  2043. /**
  2044. * amdgpu_device_resume - initiate device resume
  2045. *
  2046. * @pdev: drm dev pointer
  2047. *
  2048. * Bring the hw back to operating state (all asics).
  2049. * Returns 0 for success or an error on failure.
  2050. * Called at driver resume.
  2051. */
  2052. int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
  2053. {
  2054. struct drm_connector *connector;
  2055. struct amdgpu_device *adev = dev->dev_private;
  2056. struct drm_crtc *crtc;
  2057. int r = 0;
  2058. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2059. return 0;
  2060. if (fbcon)
  2061. console_lock();
  2062. if (resume) {
  2063. pci_set_power_state(dev->pdev, PCI_D0);
  2064. pci_restore_state(dev->pdev);
  2065. r = pci_enable_device(dev->pdev);
  2066. if (r)
  2067. goto unlock;
  2068. }
  2069. if (adev->is_atom_fw)
  2070. amdgpu_atomfirmware_scratch_regs_restore(adev);
  2071. else
  2072. amdgpu_atombios_scratch_regs_restore(adev);
  2073. /* post card */
  2074. if (amdgpu_need_post(adev)) {
  2075. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2076. if (r)
  2077. DRM_ERROR("amdgpu asic init failed\n");
  2078. }
  2079. r = amdgpu_resume(adev);
  2080. if (r) {
  2081. DRM_ERROR("amdgpu_resume failed (%d).\n", r);
  2082. goto unlock;
  2083. }
  2084. amdgpu_fence_driver_resume(adev);
  2085. if (resume) {
  2086. r = amdgpu_ib_ring_tests(adev);
  2087. if (r)
  2088. DRM_ERROR("ib ring test failed (%d).\n", r);
  2089. }
  2090. r = amdgpu_late_init(adev);
  2091. if (r)
  2092. goto unlock;
  2093. /* pin cursors */
  2094. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2095. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2096. if (amdgpu_crtc->cursor_bo) {
  2097. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2098. r = amdgpu_bo_reserve(aobj, true);
  2099. if (r == 0) {
  2100. r = amdgpu_bo_pin(aobj,
  2101. AMDGPU_GEM_DOMAIN_VRAM,
  2102. &amdgpu_crtc->cursor_addr);
  2103. if (r != 0)
  2104. DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
  2105. amdgpu_bo_unreserve(aobj);
  2106. }
  2107. }
  2108. }
  2109. /* blat the mode back in */
  2110. if (fbcon) {
  2111. drm_helper_resume_force_mode(dev);
  2112. /* turn on display hw */
  2113. drm_modeset_lock_all(dev);
  2114. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2115. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  2116. }
  2117. drm_modeset_unlock_all(dev);
  2118. }
  2119. drm_kms_helper_poll_enable(dev);
  2120. /*
  2121. * Most of the connector probing functions try to acquire runtime pm
  2122. * refs to ensure that the GPU is powered on when connector polling is
  2123. * performed. Since we're calling this from a runtime PM callback,
  2124. * trying to acquire rpm refs will cause us to deadlock.
  2125. *
  2126. * Since we're guaranteed to be holding the rpm lock, it's safe to
  2127. * temporarily disable the rpm helpers so this doesn't deadlock us.
  2128. */
  2129. #ifdef CONFIG_PM
  2130. dev->dev->power.disable_depth++;
  2131. #endif
  2132. drm_helper_hpd_irq_event(dev);
  2133. #ifdef CONFIG_PM
  2134. dev->dev->power.disable_depth--;
  2135. #endif
  2136. if (fbcon)
  2137. amdgpu_fbdev_set_suspend(adev, 0);
  2138. unlock:
  2139. if (fbcon)
  2140. console_unlock();
  2141. return r;
  2142. }
  2143. static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
  2144. {
  2145. int i;
  2146. bool asic_hang = false;
  2147. for (i = 0; i < adev->num_ip_blocks; i++) {
  2148. if (!adev->ip_blocks[i].status.valid)
  2149. continue;
  2150. if (adev->ip_blocks[i].version->funcs->check_soft_reset)
  2151. adev->ip_blocks[i].status.hang =
  2152. adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
  2153. if (adev->ip_blocks[i].status.hang) {
  2154. DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
  2155. asic_hang = true;
  2156. }
  2157. }
  2158. return asic_hang;
  2159. }
  2160. static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
  2161. {
  2162. int i, r = 0;
  2163. for (i = 0; i < adev->num_ip_blocks; i++) {
  2164. if (!adev->ip_blocks[i].status.valid)
  2165. continue;
  2166. if (adev->ip_blocks[i].status.hang &&
  2167. adev->ip_blocks[i].version->funcs->pre_soft_reset) {
  2168. r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
  2169. if (r)
  2170. return r;
  2171. }
  2172. }
  2173. return 0;
  2174. }
  2175. static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
  2176. {
  2177. int i;
  2178. for (i = 0; i < adev->num_ip_blocks; i++) {
  2179. if (!adev->ip_blocks[i].status.valid)
  2180. continue;
  2181. if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
  2182. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
  2183. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
  2184. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
  2185. if (adev->ip_blocks[i].status.hang) {
  2186. DRM_INFO("Some block need full reset!\n");
  2187. return true;
  2188. }
  2189. }
  2190. }
  2191. return false;
  2192. }
  2193. static int amdgpu_soft_reset(struct amdgpu_device *adev)
  2194. {
  2195. int i, r = 0;
  2196. for (i = 0; i < adev->num_ip_blocks; i++) {
  2197. if (!adev->ip_blocks[i].status.valid)
  2198. continue;
  2199. if (adev->ip_blocks[i].status.hang &&
  2200. adev->ip_blocks[i].version->funcs->soft_reset) {
  2201. r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
  2202. if (r)
  2203. return r;
  2204. }
  2205. }
  2206. return 0;
  2207. }
  2208. static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
  2209. {
  2210. int i, r = 0;
  2211. for (i = 0; i < adev->num_ip_blocks; i++) {
  2212. if (!adev->ip_blocks[i].status.valid)
  2213. continue;
  2214. if (adev->ip_blocks[i].status.hang &&
  2215. adev->ip_blocks[i].version->funcs->post_soft_reset)
  2216. r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
  2217. if (r)
  2218. return r;
  2219. }
  2220. return 0;
  2221. }
  2222. bool amdgpu_need_backup(struct amdgpu_device *adev)
  2223. {
  2224. if (adev->flags & AMD_IS_APU)
  2225. return false;
  2226. return amdgpu_lockup_timeout > 0 ? true : false;
  2227. }
  2228. static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
  2229. struct amdgpu_ring *ring,
  2230. struct amdgpu_bo *bo,
  2231. struct dma_fence **fence)
  2232. {
  2233. uint32_t domain;
  2234. int r;
  2235. if (!bo->shadow)
  2236. return 0;
  2237. r = amdgpu_bo_reserve(bo, true);
  2238. if (r)
  2239. return r;
  2240. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  2241. /* if bo has been evicted, then no need to recover */
  2242. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  2243. r = amdgpu_bo_validate(bo->shadow);
  2244. if (r) {
  2245. DRM_ERROR("bo validate failed!\n");
  2246. goto err;
  2247. }
  2248. r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
  2249. if (r) {
  2250. DRM_ERROR("%p bind failed\n", bo->shadow);
  2251. goto err;
  2252. }
  2253. r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
  2254. NULL, fence, true);
  2255. if (r) {
  2256. DRM_ERROR("recover page table failed!\n");
  2257. goto err;
  2258. }
  2259. }
  2260. err:
  2261. amdgpu_bo_unreserve(bo);
  2262. return r;
  2263. }
  2264. /**
  2265. * amdgpu_sriov_gpu_reset - reset the asic
  2266. *
  2267. * @adev: amdgpu device pointer
  2268. * @voluntary: if this reset is requested by guest.
  2269. * (true means by guest and false means by HYPERVISOR )
  2270. *
  2271. * Attempt the reset the GPU if it has hung (all asics).
  2272. * for SRIOV case.
  2273. * Returns 0 for success or an error on failure.
  2274. */
  2275. int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, bool voluntary)
  2276. {
  2277. int i, r = 0;
  2278. int resched;
  2279. struct amdgpu_bo *bo, *tmp;
  2280. struct amdgpu_ring *ring;
  2281. struct dma_fence *fence = NULL, *next = NULL;
  2282. mutex_lock(&adev->virt.lock_reset);
  2283. atomic_inc(&adev->gpu_reset_counter);
  2284. adev->gfx.in_reset = true;
  2285. /* block TTM */
  2286. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  2287. /* block scheduler */
  2288. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2289. ring = adev->rings[i];
  2290. if (!ring || !ring->sched.thread)
  2291. continue;
  2292. kthread_park(ring->sched.thread);
  2293. amd_sched_hw_job_reset(&ring->sched);
  2294. }
  2295. /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
  2296. amdgpu_fence_driver_force_completion(adev);
  2297. /* request to take full control of GPU before re-initialization */
  2298. if (voluntary)
  2299. amdgpu_virt_reset_gpu(adev);
  2300. else
  2301. amdgpu_virt_request_full_gpu(adev, true);
  2302. /* Resume IP prior to SMC */
  2303. amdgpu_sriov_reinit_early(adev);
  2304. /* we need recover gart prior to run SMC/CP/SDMA resume */
  2305. amdgpu_ttm_recover_gart(adev);
  2306. /* now we are okay to resume SMC/CP/SDMA */
  2307. amdgpu_sriov_reinit_late(adev);
  2308. amdgpu_irq_gpu_reset_resume_helper(adev);
  2309. if (amdgpu_ib_ring_tests(adev))
  2310. dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
  2311. /* release full control of GPU after ib test */
  2312. amdgpu_virt_release_full_gpu(adev, true);
  2313. DRM_INFO("recover vram bo from shadow\n");
  2314. ring = adev->mman.buffer_funcs_ring;
  2315. mutex_lock(&adev->shadow_list_lock);
  2316. list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
  2317. next = NULL;
  2318. amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
  2319. if (fence) {
  2320. r = dma_fence_wait(fence, false);
  2321. if (r) {
  2322. WARN(r, "recovery from shadow isn't completed\n");
  2323. break;
  2324. }
  2325. }
  2326. dma_fence_put(fence);
  2327. fence = next;
  2328. }
  2329. mutex_unlock(&adev->shadow_list_lock);
  2330. if (fence) {
  2331. r = dma_fence_wait(fence, false);
  2332. if (r)
  2333. WARN(r, "recovery from shadow isn't completed\n");
  2334. }
  2335. dma_fence_put(fence);
  2336. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2337. struct amdgpu_ring *ring = adev->rings[i];
  2338. if (!ring || !ring->sched.thread)
  2339. continue;
  2340. amd_sched_job_recovery(&ring->sched);
  2341. kthread_unpark(ring->sched.thread);
  2342. }
  2343. drm_helper_resume_force_mode(adev->ddev);
  2344. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  2345. if (r) {
  2346. /* bad news, how to tell it to userspace ? */
  2347. dev_info(adev->dev, "GPU reset failed\n");
  2348. }
  2349. adev->gfx.in_reset = false;
  2350. mutex_unlock(&adev->virt.lock_reset);
  2351. return r;
  2352. }
  2353. /**
  2354. * amdgpu_gpu_reset - reset the asic
  2355. *
  2356. * @adev: amdgpu device pointer
  2357. *
  2358. * Attempt the reset the GPU if it has hung (all asics).
  2359. * Returns 0 for success or an error on failure.
  2360. */
  2361. int amdgpu_gpu_reset(struct amdgpu_device *adev)
  2362. {
  2363. int i, r;
  2364. int resched;
  2365. bool need_full_reset;
  2366. if (amdgpu_sriov_vf(adev))
  2367. return amdgpu_sriov_gpu_reset(adev, true);
  2368. if (!amdgpu_check_soft_reset(adev)) {
  2369. DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
  2370. return 0;
  2371. }
  2372. atomic_inc(&adev->gpu_reset_counter);
  2373. /* block TTM */
  2374. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  2375. /* block scheduler */
  2376. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2377. struct amdgpu_ring *ring = adev->rings[i];
  2378. if (!ring || !ring->sched.thread)
  2379. continue;
  2380. kthread_park(ring->sched.thread);
  2381. amd_sched_hw_job_reset(&ring->sched);
  2382. }
  2383. /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
  2384. amdgpu_fence_driver_force_completion(adev);
  2385. need_full_reset = amdgpu_need_full_reset(adev);
  2386. if (!need_full_reset) {
  2387. amdgpu_pre_soft_reset(adev);
  2388. r = amdgpu_soft_reset(adev);
  2389. amdgpu_post_soft_reset(adev);
  2390. if (r || amdgpu_check_soft_reset(adev)) {
  2391. DRM_INFO("soft reset failed, will fallback to full reset!\n");
  2392. need_full_reset = true;
  2393. }
  2394. }
  2395. if (need_full_reset) {
  2396. r = amdgpu_suspend(adev);
  2397. retry:
  2398. /* Disable fb access */
  2399. if (adev->mode_info.num_crtc) {
  2400. struct amdgpu_mode_mc_save save;
  2401. amdgpu_display_stop_mc_access(adev, &save);
  2402. amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
  2403. }
  2404. if (adev->is_atom_fw)
  2405. amdgpu_atomfirmware_scratch_regs_save(adev);
  2406. else
  2407. amdgpu_atombios_scratch_regs_save(adev);
  2408. r = amdgpu_asic_reset(adev);
  2409. if (adev->is_atom_fw)
  2410. amdgpu_atomfirmware_scratch_regs_restore(adev);
  2411. else
  2412. amdgpu_atombios_scratch_regs_restore(adev);
  2413. /* post card */
  2414. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2415. if (!r) {
  2416. dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
  2417. r = amdgpu_resume(adev);
  2418. }
  2419. }
  2420. if (!r) {
  2421. amdgpu_irq_gpu_reset_resume_helper(adev);
  2422. if (need_full_reset && amdgpu_need_backup(adev)) {
  2423. r = amdgpu_ttm_recover_gart(adev);
  2424. if (r)
  2425. DRM_ERROR("gart recovery failed!!!\n");
  2426. }
  2427. r = amdgpu_ib_ring_tests(adev);
  2428. if (r) {
  2429. dev_err(adev->dev, "ib ring test failed (%d).\n", r);
  2430. r = amdgpu_suspend(adev);
  2431. need_full_reset = true;
  2432. goto retry;
  2433. }
  2434. /**
  2435. * recovery vm page tables, since we cannot depend on VRAM is
  2436. * consistent after gpu full reset.
  2437. */
  2438. if (need_full_reset && amdgpu_need_backup(adev)) {
  2439. struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  2440. struct amdgpu_bo *bo, *tmp;
  2441. struct dma_fence *fence = NULL, *next = NULL;
  2442. DRM_INFO("recover vram bo from shadow\n");
  2443. mutex_lock(&adev->shadow_list_lock);
  2444. list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
  2445. next = NULL;
  2446. amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
  2447. if (fence) {
  2448. r = dma_fence_wait(fence, false);
  2449. if (r) {
  2450. WARN(r, "recovery from shadow isn't completed\n");
  2451. break;
  2452. }
  2453. }
  2454. dma_fence_put(fence);
  2455. fence = next;
  2456. }
  2457. mutex_unlock(&adev->shadow_list_lock);
  2458. if (fence) {
  2459. r = dma_fence_wait(fence, false);
  2460. if (r)
  2461. WARN(r, "recovery from shadow isn't completed\n");
  2462. }
  2463. dma_fence_put(fence);
  2464. }
  2465. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2466. struct amdgpu_ring *ring = adev->rings[i];
  2467. if (!ring || !ring->sched.thread)
  2468. continue;
  2469. amd_sched_job_recovery(&ring->sched);
  2470. kthread_unpark(ring->sched.thread);
  2471. }
  2472. } else {
  2473. dev_err(adev->dev, "asic resume failed (%d).\n", r);
  2474. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2475. if (adev->rings[i] && adev->rings[i]->sched.thread) {
  2476. kthread_unpark(adev->rings[i]->sched.thread);
  2477. }
  2478. }
  2479. }
  2480. drm_helper_resume_force_mode(adev->ddev);
  2481. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  2482. if (r) {
  2483. /* bad news, how to tell it to userspace ? */
  2484. dev_info(adev->dev, "GPU reset failed\n");
  2485. }
  2486. return r;
  2487. }
  2488. void amdgpu_get_pcie_info(struct amdgpu_device *adev)
  2489. {
  2490. u32 mask;
  2491. int ret;
  2492. if (amdgpu_pcie_gen_cap)
  2493. adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
  2494. if (amdgpu_pcie_lane_cap)
  2495. adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
  2496. /* covers APUs as well */
  2497. if (pci_is_root_bus(adev->pdev->bus)) {
  2498. if (adev->pm.pcie_gen_mask == 0)
  2499. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2500. if (adev->pm.pcie_mlw_mask == 0)
  2501. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2502. return;
  2503. }
  2504. if (adev->pm.pcie_gen_mask == 0) {
  2505. ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
  2506. if (!ret) {
  2507. adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2508. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2509. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2510. if (mask & DRM_PCIE_SPEED_25)
  2511. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
  2512. if (mask & DRM_PCIE_SPEED_50)
  2513. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
  2514. if (mask & DRM_PCIE_SPEED_80)
  2515. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
  2516. } else {
  2517. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2518. }
  2519. }
  2520. if (adev->pm.pcie_mlw_mask == 0) {
  2521. ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
  2522. if (!ret) {
  2523. switch (mask) {
  2524. case 32:
  2525. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
  2526. CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2527. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2528. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2529. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2530. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2531. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2532. break;
  2533. case 16:
  2534. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2535. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2536. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2537. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2538. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2539. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2540. break;
  2541. case 12:
  2542. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2543. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2544. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2545. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2546. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2547. break;
  2548. case 8:
  2549. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2550. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2551. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2552. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2553. break;
  2554. case 4:
  2555. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2556. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2557. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2558. break;
  2559. case 2:
  2560. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2561. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2562. break;
  2563. case 1:
  2564. adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
  2565. break;
  2566. default:
  2567. break;
  2568. }
  2569. } else {
  2570. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2571. }
  2572. }
  2573. }
  2574. /*
  2575. * Debugfs
  2576. */
  2577. int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
  2578. const struct drm_info_list *files,
  2579. unsigned nfiles)
  2580. {
  2581. unsigned i;
  2582. for (i = 0; i < adev->debugfs_count; i++) {
  2583. if (adev->debugfs[i].files == files) {
  2584. /* Already registered */
  2585. return 0;
  2586. }
  2587. }
  2588. i = adev->debugfs_count + 1;
  2589. if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
  2590. DRM_ERROR("Reached maximum number of debugfs components.\n");
  2591. DRM_ERROR("Report so we increase "
  2592. "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
  2593. return -EINVAL;
  2594. }
  2595. adev->debugfs[adev->debugfs_count].files = files;
  2596. adev->debugfs[adev->debugfs_count].num_files = nfiles;
  2597. adev->debugfs_count = i;
  2598. #if defined(CONFIG_DEBUG_FS)
  2599. drm_debugfs_create_files(files, nfiles,
  2600. adev->ddev->primary->debugfs_root,
  2601. adev->ddev->primary);
  2602. #endif
  2603. return 0;
  2604. }
  2605. #if defined(CONFIG_DEBUG_FS)
  2606. static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
  2607. size_t size, loff_t *pos)
  2608. {
  2609. struct amdgpu_device *adev = file_inode(f)->i_private;
  2610. ssize_t result = 0;
  2611. int r;
  2612. bool pm_pg_lock, use_bank;
  2613. unsigned instance_bank, sh_bank, se_bank;
  2614. if (size & 0x3 || *pos & 0x3)
  2615. return -EINVAL;
  2616. /* are we reading registers for which a PG lock is necessary? */
  2617. pm_pg_lock = (*pos >> 23) & 1;
  2618. if (*pos & (1ULL << 62)) {
  2619. se_bank = (*pos >> 24) & 0x3FF;
  2620. sh_bank = (*pos >> 34) & 0x3FF;
  2621. instance_bank = (*pos >> 44) & 0x3FF;
  2622. if (se_bank == 0x3FF)
  2623. se_bank = 0xFFFFFFFF;
  2624. if (sh_bank == 0x3FF)
  2625. sh_bank = 0xFFFFFFFF;
  2626. if (instance_bank == 0x3FF)
  2627. instance_bank = 0xFFFFFFFF;
  2628. use_bank = 1;
  2629. } else {
  2630. use_bank = 0;
  2631. }
  2632. *pos &= (1UL << 22) - 1;
  2633. if (use_bank) {
  2634. if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
  2635. (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
  2636. return -EINVAL;
  2637. mutex_lock(&adev->grbm_idx_mutex);
  2638. amdgpu_gfx_select_se_sh(adev, se_bank,
  2639. sh_bank, instance_bank);
  2640. }
  2641. if (pm_pg_lock)
  2642. mutex_lock(&adev->pm.mutex);
  2643. while (size) {
  2644. uint32_t value;
  2645. if (*pos > adev->rmmio_size)
  2646. goto end;
  2647. value = RREG32(*pos >> 2);
  2648. r = put_user(value, (uint32_t *)buf);
  2649. if (r) {
  2650. result = r;
  2651. goto end;
  2652. }
  2653. result += 4;
  2654. buf += 4;
  2655. *pos += 4;
  2656. size -= 4;
  2657. }
  2658. end:
  2659. if (use_bank) {
  2660. amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
  2661. mutex_unlock(&adev->grbm_idx_mutex);
  2662. }
  2663. if (pm_pg_lock)
  2664. mutex_unlock(&adev->pm.mutex);
  2665. return result;
  2666. }
  2667. static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
  2668. size_t size, loff_t *pos)
  2669. {
  2670. struct amdgpu_device *adev = file_inode(f)->i_private;
  2671. ssize_t result = 0;
  2672. int r;
  2673. bool pm_pg_lock, use_bank;
  2674. unsigned instance_bank, sh_bank, se_bank;
  2675. if (size & 0x3 || *pos & 0x3)
  2676. return -EINVAL;
  2677. /* are we reading registers for which a PG lock is necessary? */
  2678. pm_pg_lock = (*pos >> 23) & 1;
  2679. if (*pos & (1ULL << 62)) {
  2680. se_bank = (*pos >> 24) & 0x3FF;
  2681. sh_bank = (*pos >> 34) & 0x3FF;
  2682. instance_bank = (*pos >> 44) & 0x3FF;
  2683. if (se_bank == 0x3FF)
  2684. se_bank = 0xFFFFFFFF;
  2685. if (sh_bank == 0x3FF)
  2686. sh_bank = 0xFFFFFFFF;
  2687. if (instance_bank == 0x3FF)
  2688. instance_bank = 0xFFFFFFFF;
  2689. use_bank = 1;
  2690. } else {
  2691. use_bank = 0;
  2692. }
  2693. *pos &= (1UL << 22) - 1;
  2694. if (use_bank) {
  2695. if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
  2696. (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
  2697. return -EINVAL;
  2698. mutex_lock(&adev->grbm_idx_mutex);
  2699. amdgpu_gfx_select_se_sh(adev, se_bank,
  2700. sh_bank, instance_bank);
  2701. }
  2702. if (pm_pg_lock)
  2703. mutex_lock(&adev->pm.mutex);
  2704. while (size) {
  2705. uint32_t value;
  2706. if (*pos > adev->rmmio_size)
  2707. return result;
  2708. r = get_user(value, (uint32_t *)buf);
  2709. if (r)
  2710. return r;
  2711. WREG32(*pos >> 2, value);
  2712. result += 4;
  2713. buf += 4;
  2714. *pos += 4;
  2715. size -= 4;
  2716. }
  2717. if (use_bank) {
  2718. amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
  2719. mutex_unlock(&adev->grbm_idx_mutex);
  2720. }
  2721. if (pm_pg_lock)
  2722. mutex_unlock(&adev->pm.mutex);
  2723. return result;
  2724. }
  2725. static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
  2726. size_t size, loff_t *pos)
  2727. {
  2728. struct amdgpu_device *adev = file_inode(f)->i_private;
  2729. ssize_t result = 0;
  2730. int r;
  2731. if (size & 0x3 || *pos & 0x3)
  2732. return -EINVAL;
  2733. while (size) {
  2734. uint32_t value;
  2735. value = RREG32_PCIE(*pos >> 2);
  2736. r = put_user(value, (uint32_t *)buf);
  2737. if (r)
  2738. return r;
  2739. result += 4;
  2740. buf += 4;
  2741. *pos += 4;
  2742. size -= 4;
  2743. }
  2744. return result;
  2745. }
  2746. static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
  2747. size_t size, loff_t *pos)
  2748. {
  2749. struct amdgpu_device *adev = file_inode(f)->i_private;
  2750. ssize_t result = 0;
  2751. int r;
  2752. if (size & 0x3 || *pos & 0x3)
  2753. return -EINVAL;
  2754. while (size) {
  2755. uint32_t value;
  2756. r = get_user(value, (uint32_t *)buf);
  2757. if (r)
  2758. return r;
  2759. WREG32_PCIE(*pos >> 2, value);
  2760. result += 4;
  2761. buf += 4;
  2762. *pos += 4;
  2763. size -= 4;
  2764. }
  2765. return result;
  2766. }
  2767. static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
  2768. size_t size, loff_t *pos)
  2769. {
  2770. struct amdgpu_device *adev = file_inode(f)->i_private;
  2771. ssize_t result = 0;
  2772. int r;
  2773. if (size & 0x3 || *pos & 0x3)
  2774. return -EINVAL;
  2775. while (size) {
  2776. uint32_t value;
  2777. value = RREG32_DIDT(*pos >> 2);
  2778. r = put_user(value, (uint32_t *)buf);
  2779. if (r)
  2780. return r;
  2781. result += 4;
  2782. buf += 4;
  2783. *pos += 4;
  2784. size -= 4;
  2785. }
  2786. return result;
  2787. }
  2788. static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
  2789. size_t size, loff_t *pos)
  2790. {
  2791. struct amdgpu_device *adev = file_inode(f)->i_private;
  2792. ssize_t result = 0;
  2793. int r;
  2794. if (size & 0x3 || *pos & 0x3)
  2795. return -EINVAL;
  2796. while (size) {
  2797. uint32_t value;
  2798. r = get_user(value, (uint32_t *)buf);
  2799. if (r)
  2800. return r;
  2801. WREG32_DIDT(*pos >> 2, value);
  2802. result += 4;
  2803. buf += 4;
  2804. *pos += 4;
  2805. size -= 4;
  2806. }
  2807. return result;
  2808. }
  2809. static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
  2810. size_t size, loff_t *pos)
  2811. {
  2812. struct amdgpu_device *adev = file_inode(f)->i_private;
  2813. ssize_t result = 0;
  2814. int r;
  2815. if (size & 0x3 || *pos & 0x3)
  2816. return -EINVAL;
  2817. while (size) {
  2818. uint32_t value;
  2819. value = RREG32_SMC(*pos);
  2820. r = put_user(value, (uint32_t *)buf);
  2821. if (r)
  2822. return r;
  2823. result += 4;
  2824. buf += 4;
  2825. *pos += 4;
  2826. size -= 4;
  2827. }
  2828. return result;
  2829. }
  2830. static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
  2831. size_t size, loff_t *pos)
  2832. {
  2833. struct amdgpu_device *adev = file_inode(f)->i_private;
  2834. ssize_t result = 0;
  2835. int r;
  2836. if (size & 0x3 || *pos & 0x3)
  2837. return -EINVAL;
  2838. while (size) {
  2839. uint32_t value;
  2840. r = get_user(value, (uint32_t *)buf);
  2841. if (r)
  2842. return r;
  2843. WREG32_SMC(*pos, value);
  2844. result += 4;
  2845. buf += 4;
  2846. *pos += 4;
  2847. size -= 4;
  2848. }
  2849. return result;
  2850. }
  2851. static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
  2852. size_t size, loff_t *pos)
  2853. {
  2854. struct amdgpu_device *adev = file_inode(f)->i_private;
  2855. ssize_t result = 0;
  2856. int r;
  2857. uint32_t *config, no_regs = 0;
  2858. if (size & 0x3 || *pos & 0x3)
  2859. return -EINVAL;
  2860. config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
  2861. if (!config)
  2862. return -ENOMEM;
  2863. /* version, increment each time something is added */
  2864. config[no_regs++] = 3;
  2865. config[no_regs++] = adev->gfx.config.max_shader_engines;
  2866. config[no_regs++] = adev->gfx.config.max_tile_pipes;
  2867. config[no_regs++] = adev->gfx.config.max_cu_per_sh;
  2868. config[no_regs++] = adev->gfx.config.max_sh_per_se;
  2869. config[no_regs++] = adev->gfx.config.max_backends_per_se;
  2870. config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
  2871. config[no_regs++] = adev->gfx.config.max_gprs;
  2872. config[no_regs++] = adev->gfx.config.max_gs_threads;
  2873. config[no_regs++] = adev->gfx.config.max_hw_contexts;
  2874. config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
  2875. config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
  2876. config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
  2877. config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
  2878. config[no_regs++] = adev->gfx.config.num_tile_pipes;
  2879. config[no_regs++] = adev->gfx.config.backend_enable_mask;
  2880. config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
  2881. config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
  2882. config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
  2883. config[no_regs++] = adev->gfx.config.num_gpus;
  2884. config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
  2885. config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
  2886. config[no_regs++] = adev->gfx.config.gb_addr_config;
  2887. config[no_regs++] = adev->gfx.config.num_rbs;
  2888. /* rev==1 */
  2889. config[no_regs++] = adev->rev_id;
  2890. config[no_regs++] = adev->pg_flags;
  2891. config[no_regs++] = adev->cg_flags;
  2892. /* rev==2 */
  2893. config[no_regs++] = adev->family;
  2894. config[no_regs++] = adev->external_rev_id;
  2895. /* rev==3 */
  2896. config[no_regs++] = adev->pdev->device;
  2897. config[no_regs++] = adev->pdev->revision;
  2898. config[no_regs++] = adev->pdev->subsystem_device;
  2899. config[no_regs++] = adev->pdev->subsystem_vendor;
  2900. while (size && (*pos < no_regs * 4)) {
  2901. uint32_t value;
  2902. value = config[*pos >> 2];
  2903. r = put_user(value, (uint32_t *)buf);
  2904. if (r) {
  2905. kfree(config);
  2906. return r;
  2907. }
  2908. result += 4;
  2909. buf += 4;
  2910. *pos += 4;
  2911. size -= 4;
  2912. }
  2913. kfree(config);
  2914. return result;
  2915. }
  2916. static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
  2917. size_t size, loff_t *pos)
  2918. {
  2919. struct amdgpu_device *adev = file_inode(f)->i_private;
  2920. int idx, x, outsize, r, valuesize;
  2921. uint32_t values[16];
  2922. if (size & 3 || *pos & 0x3)
  2923. return -EINVAL;
  2924. if (amdgpu_dpm == 0)
  2925. return -EINVAL;
  2926. /* convert offset to sensor number */
  2927. idx = *pos >> 2;
  2928. valuesize = sizeof(values);
  2929. if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
  2930. r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
  2931. else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
  2932. r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
  2933. &valuesize);
  2934. else
  2935. return -EINVAL;
  2936. if (size > valuesize)
  2937. return -EINVAL;
  2938. outsize = 0;
  2939. x = 0;
  2940. if (!r) {
  2941. while (size) {
  2942. r = put_user(values[x++], (int32_t *)buf);
  2943. buf += 4;
  2944. size -= 4;
  2945. outsize += 4;
  2946. }
  2947. }
  2948. return !r ? outsize : r;
  2949. }
  2950. static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
  2951. size_t size, loff_t *pos)
  2952. {
  2953. struct amdgpu_device *adev = f->f_inode->i_private;
  2954. int r, x;
  2955. ssize_t result=0;
  2956. uint32_t offset, se, sh, cu, wave, simd, data[32];
  2957. if (size & 3 || *pos & 3)
  2958. return -EINVAL;
  2959. /* decode offset */
  2960. offset = (*pos & 0x7F);
  2961. se = ((*pos >> 7) & 0xFF);
  2962. sh = ((*pos >> 15) & 0xFF);
  2963. cu = ((*pos >> 23) & 0xFF);
  2964. wave = ((*pos >> 31) & 0xFF);
  2965. simd = ((*pos >> 37) & 0xFF);
  2966. /* switch to the specific se/sh/cu */
  2967. mutex_lock(&adev->grbm_idx_mutex);
  2968. amdgpu_gfx_select_se_sh(adev, se, sh, cu);
  2969. x = 0;
  2970. if (adev->gfx.funcs->read_wave_data)
  2971. adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
  2972. amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
  2973. mutex_unlock(&adev->grbm_idx_mutex);
  2974. if (!x)
  2975. return -EINVAL;
  2976. while (size && (offset < x * 4)) {
  2977. uint32_t value;
  2978. value = data[offset >> 2];
  2979. r = put_user(value, (uint32_t *)buf);
  2980. if (r)
  2981. return r;
  2982. result += 4;
  2983. buf += 4;
  2984. offset += 4;
  2985. size -= 4;
  2986. }
  2987. return result;
  2988. }
  2989. static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
  2990. size_t size, loff_t *pos)
  2991. {
  2992. struct amdgpu_device *adev = f->f_inode->i_private;
  2993. int r;
  2994. ssize_t result = 0;
  2995. uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
  2996. if (size & 3 || *pos & 3)
  2997. return -EINVAL;
  2998. /* decode offset */
  2999. offset = (*pos & 0xFFF); /* in dwords */
  3000. se = ((*pos >> 12) & 0xFF);
  3001. sh = ((*pos >> 20) & 0xFF);
  3002. cu = ((*pos >> 28) & 0xFF);
  3003. wave = ((*pos >> 36) & 0xFF);
  3004. simd = ((*pos >> 44) & 0xFF);
  3005. thread = ((*pos >> 52) & 0xFF);
  3006. bank = ((*pos >> 60) & 1);
  3007. data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
  3008. if (!data)
  3009. return -ENOMEM;
  3010. /* switch to the specific se/sh/cu */
  3011. mutex_lock(&adev->grbm_idx_mutex);
  3012. amdgpu_gfx_select_se_sh(adev, se, sh, cu);
  3013. if (bank == 0) {
  3014. if (adev->gfx.funcs->read_wave_vgprs)
  3015. adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
  3016. } else {
  3017. if (adev->gfx.funcs->read_wave_sgprs)
  3018. adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
  3019. }
  3020. amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
  3021. mutex_unlock(&adev->grbm_idx_mutex);
  3022. while (size) {
  3023. uint32_t value;
  3024. value = data[offset++];
  3025. r = put_user(value, (uint32_t *)buf);
  3026. if (r) {
  3027. result = r;
  3028. goto err;
  3029. }
  3030. result += 4;
  3031. buf += 4;
  3032. size -= 4;
  3033. }
  3034. err:
  3035. kfree(data);
  3036. return result;
  3037. }
  3038. static const struct file_operations amdgpu_debugfs_regs_fops = {
  3039. .owner = THIS_MODULE,
  3040. .read = amdgpu_debugfs_regs_read,
  3041. .write = amdgpu_debugfs_regs_write,
  3042. .llseek = default_llseek
  3043. };
  3044. static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
  3045. .owner = THIS_MODULE,
  3046. .read = amdgpu_debugfs_regs_didt_read,
  3047. .write = amdgpu_debugfs_regs_didt_write,
  3048. .llseek = default_llseek
  3049. };
  3050. static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
  3051. .owner = THIS_MODULE,
  3052. .read = amdgpu_debugfs_regs_pcie_read,
  3053. .write = amdgpu_debugfs_regs_pcie_write,
  3054. .llseek = default_llseek
  3055. };
  3056. static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
  3057. .owner = THIS_MODULE,
  3058. .read = amdgpu_debugfs_regs_smc_read,
  3059. .write = amdgpu_debugfs_regs_smc_write,
  3060. .llseek = default_llseek
  3061. };
  3062. static const struct file_operations amdgpu_debugfs_gca_config_fops = {
  3063. .owner = THIS_MODULE,
  3064. .read = amdgpu_debugfs_gca_config_read,
  3065. .llseek = default_llseek
  3066. };
  3067. static const struct file_operations amdgpu_debugfs_sensors_fops = {
  3068. .owner = THIS_MODULE,
  3069. .read = amdgpu_debugfs_sensor_read,
  3070. .llseek = default_llseek
  3071. };
  3072. static const struct file_operations amdgpu_debugfs_wave_fops = {
  3073. .owner = THIS_MODULE,
  3074. .read = amdgpu_debugfs_wave_read,
  3075. .llseek = default_llseek
  3076. };
  3077. static const struct file_operations amdgpu_debugfs_gpr_fops = {
  3078. .owner = THIS_MODULE,
  3079. .read = amdgpu_debugfs_gpr_read,
  3080. .llseek = default_llseek
  3081. };
  3082. static const struct file_operations *debugfs_regs[] = {
  3083. &amdgpu_debugfs_regs_fops,
  3084. &amdgpu_debugfs_regs_didt_fops,
  3085. &amdgpu_debugfs_regs_pcie_fops,
  3086. &amdgpu_debugfs_regs_smc_fops,
  3087. &amdgpu_debugfs_gca_config_fops,
  3088. &amdgpu_debugfs_sensors_fops,
  3089. &amdgpu_debugfs_wave_fops,
  3090. &amdgpu_debugfs_gpr_fops,
  3091. };
  3092. static const char *debugfs_regs_names[] = {
  3093. "amdgpu_regs",
  3094. "amdgpu_regs_didt",
  3095. "amdgpu_regs_pcie",
  3096. "amdgpu_regs_smc",
  3097. "amdgpu_gca_config",
  3098. "amdgpu_sensors",
  3099. "amdgpu_wave",
  3100. "amdgpu_gpr",
  3101. };
  3102. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
  3103. {
  3104. struct drm_minor *minor = adev->ddev->primary;
  3105. struct dentry *ent, *root = minor->debugfs_root;
  3106. unsigned i, j;
  3107. for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
  3108. ent = debugfs_create_file(debugfs_regs_names[i],
  3109. S_IFREG | S_IRUGO, root,
  3110. adev, debugfs_regs[i]);
  3111. if (IS_ERR(ent)) {
  3112. for (j = 0; j < i; j++) {
  3113. debugfs_remove(adev->debugfs_regs[i]);
  3114. adev->debugfs_regs[i] = NULL;
  3115. }
  3116. return PTR_ERR(ent);
  3117. }
  3118. if (!i)
  3119. i_size_write(ent->d_inode, adev->rmmio_size);
  3120. adev->debugfs_regs[i] = ent;
  3121. }
  3122. return 0;
  3123. }
  3124. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
  3125. {
  3126. unsigned i;
  3127. for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
  3128. if (adev->debugfs_regs[i]) {
  3129. debugfs_remove(adev->debugfs_regs[i]);
  3130. adev->debugfs_regs[i] = NULL;
  3131. }
  3132. }
  3133. }
  3134. int amdgpu_debugfs_init(struct drm_minor *minor)
  3135. {
  3136. return 0;
  3137. }
  3138. #else
  3139. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
  3140. {
  3141. return 0;
  3142. }
  3143. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
  3144. #endif