amdgpu_device.c 96 KB

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