amdgpu_device.c 78 KB

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