amdgpu_device.c 91 KB

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