amdgpu_device.c 89 KB

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