amdgpu_device.c 90 KB

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