amdgpu_device.c 90 KB

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