amdgpu_device.c 94 KB

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