amdgpu_device.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361
  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 powergating */
  1706. if (adev->powerplay.pp_feature & PP_GFXOFF_MASK)
  1707. amdgpu_device_ip_set_powergating_state(adev,
  1708. AMD_IP_BLOCK_TYPE_SMC,
  1709. AMD_CG_STATE_UNGATE);
  1710. /* ungate SMC block first */
  1711. r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
  1712. AMD_CG_STATE_UNGATE);
  1713. if (r) {
  1714. DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
  1715. }
  1716. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1717. if (!adev->ip_blocks[i].status.valid)
  1718. continue;
  1719. /* ungate blocks so that suspend can properly shut them down */
  1720. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_SMC &&
  1721. adev->ip_blocks[i].version->funcs->set_clockgating_state) {
  1722. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1723. AMD_CG_STATE_UNGATE);
  1724. if (r) {
  1725. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1726. adev->ip_blocks[i].version->funcs->name, r);
  1727. }
  1728. }
  1729. /* XXX handle errors */
  1730. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1731. /* XXX handle errors */
  1732. if (r) {
  1733. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1734. adev->ip_blocks[i].version->funcs->name, r);
  1735. }
  1736. }
  1737. if (amdgpu_sriov_vf(adev))
  1738. amdgpu_virt_release_full_gpu(adev, false);
  1739. return 0;
  1740. }
  1741. static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
  1742. {
  1743. int i, r;
  1744. static enum amd_ip_block_type ip_order[] = {
  1745. AMD_IP_BLOCK_TYPE_GMC,
  1746. AMD_IP_BLOCK_TYPE_COMMON,
  1747. AMD_IP_BLOCK_TYPE_IH,
  1748. };
  1749. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1750. int j;
  1751. struct amdgpu_ip_block *block;
  1752. for (j = 0; j < adev->num_ip_blocks; j++) {
  1753. block = &adev->ip_blocks[j];
  1754. if (block->version->type != ip_order[i] ||
  1755. !block->status.valid)
  1756. continue;
  1757. r = block->version->funcs->hw_init(adev);
  1758. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
  1759. if (r)
  1760. return r;
  1761. }
  1762. }
  1763. return 0;
  1764. }
  1765. static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
  1766. {
  1767. int i, r;
  1768. static enum amd_ip_block_type ip_order[] = {
  1769. AMD_IP_BLOCK_TYPE_SMC,
  1770. AMD_IP_BLOCK_TYPE_PSP,
  1771. AMD_IP_BLOCK_TYPE_DCE,
  1772. AMD_IP_BLOCK_TYPE_GFX,
  1773. AMD_IP_BLOCK_TYPE_SDMA,
  1774. AMD_IP_BLOCK_TYPE_UVD,
  1775. AMD_IP_BLOCK_TYPE_VCE
  1776. };
  1777. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1778. int j;
  1779. struct amdgpu_ip_block *block;
  1780. for (j = 0; j < adev->num_ip_blocks; j++) {
  1781. block = &adev->ip_blocks[j];
  1782. if (block->version->type != ip_order[i] ||
  1783. !block->status.valid)
  1784. continue;
  1785. r = block->version->funcs->hw_init(adev);
  1786. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
  1787. if (r)
  1788. return r;
  1789. }
  1790. }
  1791. return 0;
  1792. }
  1793. /**
  1794. * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
  1795. *
  1796. * @adev: amdgpu_device pointer
  1797. *
  1798. * First resume function for hardware IPs. The list of all the hardware
  1799. * IPs that make up the asic is walked and the resume callbacks are run for
  1800. * COMMON, GMC, and IH. resume puts the hardware into a functional state
  1801. * after a suspend and updates the software state as necessary. This
  1802. * function is also used for restoring the GPU after a GPU reset.
  1803. * Returns 0 on success, negative error code on failure.
  1804. */
  1805. static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
  1806. {
  1807. int i, r;
  1808. for (i = 0; i < adev->num_ip_blocks; i++) {
  1809. if (!adev->ip_blocks[i].status.valid)
  1810. continue;
  1811. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1812. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1813. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
  1814. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1815. if (r) {
  1816. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1817. adev->ip_blocks[i].version->funcs->name, r);
  1818. return r;
  1819. }
  1820. }
  1821. }
  1822. return 0;
  1823. }
  1824. /**
  1825. * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
  1826. *
  1827. * @adev: amdgpu_device pointer
  1828. *
  1829. * First resume function for hardware IPs. The list of all the hardware
  1830. * IPs that make up the asic is walked and the resume callbacks are run for
  1831. * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
  1832. * functional state after a suspend and updates the software state as
  1833. * necessary. This function is also used for restoring the GPU after a GPU
  1834. * reset.
  1835. * Returns 0 on success, negative error code on failure.
  1836. */
  1837. static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
  1838. {
  1839. int i, r;
  1840. for (i = 0; i < adev->num_ip_blocks; i++) {
  1841. if (!adev->ip_blocks[i].status.valid)
  1842. continue;
  1843. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1844. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1845. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
  1846. continue;
  1847. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1848. if (r) {
  1849. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1850. adev->ip_blocks[i].version->funcs->name, r);
  1851. return r;
  1852. }
  1853. }
  1854. return 0;
  1855. }
  1856. /**
  1857. * amdgpu_device_ip_resume - run resume for hardware IPs
  1858. *
  1859. * @adev: amdgpu_device pointer
  1860. *
  1861. * Main resume function for hardware IPs. The hardware IPs
  1862. * are split into two resume functions because they are
  1863. * are also used in in recovering from a GPU reset and some additional
  1864. * steps need to be take between them. In this case (S3/S4) they are
  1865. * run sequentially.
  1866. * Returns 0 on success, negative error code on failure.
  1867. */
  1868. static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
  1869. {
  1870. int r;
  1871. r = amdgpu_device_ip_resume_phase1(adev);
  1872. if (r)
  1873. return r;
  1874. r = amdgpu_device_ip_resume_phase2(adev);
  1875. return r;
  1876. }
  1877. /**
  1878. * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
  1879. *
  1880. * @adev: amdgpu_device pointer
  1881. *
  1882. * Query the VBIOS data tables to determine if the board supports SR-IOV.
  1883. */
  1884. static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
  1885. {
  1886. if (amdgpu_sriov_vf(adev)) {
  1887. if (adev->is_atom_fw) {
  1888. if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
  1889. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1890. } else {
  1891. if (amdgpu_atombios_has_gpu_virtualization_table(adev))
  1892. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1893. }
  1894. if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
  1895. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
  1896. }
  1897. }
  1898. /**
  1899. * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
  1900. *
  1901. * @asic_type: AMD asic type
  1902. *
  1903. * Check if there is DC (new modesetting infrastructre) support for an asic.
  1904. * returns true if DC has support, false if not.
  1905. */
  1906. bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
  1907. {
  1908. switch (asic_type) {
  1909. #if defined(CONFIG_DRM_AMD_DC)
  1910. case CHIP_BONAIRE:
  1911. case CHIP_HAWAII:
  1912. case CHIP_KAVERI:
  1913. case CHIP_KABINI:
  1914. case CHIP_MULLINS:
  1915. case CHIP_CARRIZO:
  1916. case CHIP_STONEY:
  1917. case CHIP_POLARIS11:
  1918. case CHIP_POLARIS10:
  1919. case CHIP_POLARIS12:
  1920. case CHIP_TONGA:
  1921. case CHIP_FIJI:
  1922. case CHIP_VEGA10:
  1923. case CHIP_VEGA12:
  1924. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  1925. case CHIP_RAVEN:
  1926. #endif
  1927. return amdgpu_dc != 0;
  1928. #endif
  1929. default:
  1930. return false;
  1931. }
  1932. }
  1933. /**
  1934. * amdgpu_device_has_dc_support - check if dc is supported
  1935. *
  1936. * @adev: amdgpu_device_pointer
  1937. *
  1938. * Returns true for supported, false for not supported
  1939. */
  1940. bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
  1941. {
  1942. if (amdgpu_sriov_vf(adev))
  1943. return false;
  1944. return amdgpu_device_asic_has_dc_support(adev->asic_type);
  1945. }
  1946. /**
  1947. * amdgpu_device_init - initialize the driver
  1948. *
  1949. * @adev: amdgpu_device pointer
  1950. * @pdev: drm dev pointer
  1951. * @pdev: pci dev pointer
  1952. * @flags: driver flags
  1953. *
  1954. * Initializes the driver info and hw (all asics).
  1955. * Returns 0 for success or an error on failure.
  1956. * Called at driver startup.
  1957. */
  1958. int amdgpu_device_init(struct amdgpu_device *adev,
  1959. struct drm_device *ddev,
  1960. struct pci_dev *pdev,
  1961. uint32_t flags)
  1962. {
  1963. int r, i;
  1964. bool runtime = false;
  1965. u32 max_MBps;
  1966. adev->shutdown = false;
  1967. adev->dev = &pdev->dev;
  1968. adev->ddev = ddev;
  1969. adev->pdev = pdev;
  1970. adev->flags = flags;
  1971. adev->asic_type = flags & AMD_ASIC_MASK;
  1972. adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
  1973. if (amdgpu_emu_mode == 1)
  1974. adev->usec_timeout *= 2;
  1975. adev->gmc.gart_size = 512 * 1024 * 1024;
  1976. adev->accel_working = false;
  1977. adev->num_rings = 0;
  1978. adev->mman.buffer_funcs = NULL;
  1979. adev->mman.buffer_funcs_ring = NULL;
  1980. adev->vm_manager.vm_pte_funcs = NULL;
  1981. adev->vm_manager.vm_pte_num_rings = 0;
  1982. adev->gmc.gmc_funcs = NULL;
  1983. adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  1984. bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
  1985. adev->smc_rreg = &amdgpu_invalid_rreg;
  1986. adev->smc_wreg = &amdgpu_invalid_wreg;
  1987. adev->pcie_rreg = &amdgpu_invalid_rreg;
  1988. adev->pcie_wreg = &amdgpu_invalid_wreg;
  1989. adev->pciep_rreg = &amdgpu_invalid_rreg;
  1990. adev->pciep_wreg = &amdgpu_invalid_wreg;
  1991. adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
  1992. adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
  1993. adev->didt_rreg = &amdgpu_invalid_rreg;
  1994. adev->didt_wreg = &amdgpu_invalid_wreg;
  1995. adev->gc_cac_rreg = &amdgpu_invalid_rreg;
  1996. adev->gc_cac_wreg = &amdgpu_invalid_wreg;
  1997. adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
  1998. adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
  1999. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
  2000. amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
  2001. pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
  2002. /* mutex initialization are all done here so we
  2003. * can recall function without having locking issues */
  2004. atomic_set(&adev->irq.ih.lock, 0);
  2005. mutex_init(&adev->firmware.mutex);
  2006. mutex_init(&adev->pm.mutex);
  2007. mutex_init(&adev->gfx.gpu_clock_mutex);
  2008. mutex_init(&adev->srbm_mutex);
  2009. mutex_init(&adev->gfx.pipe_reserve_mutex);
  2010. mutex_init(&adev->grbm_idx_mutex);
  2011. mutex_init(&adev->mn_lock);
  2012. mutex_init(&adev->virt.vf_errors.lock);
  2013. hash_init(adev->mn_hash);
  2014. mutex_init(&adev->lock_reset);
  2015. amdgpu_device_check_arguments(adev);
  2016. spin_lock_init(&adev->mmio_idx_lock);
  2017. spin_lock_init(&adev->smc_idx_lock);
  2018. spin_lock_init(&adev->pcie_idx_lock);
  2019. spin_lock_init(&adev->uvd_ctx_idx_lock);
  2020. spin_lock_init(&adev->didt_idx_lock);
  2021. spin_lock_init(&adev->gc_cac_idx_lock);
  2022. spin_lock_init(&adev->se_cac_idx_lock);
  2023. spin_lock_init(&adev->audio_endpt_idx_lock);
  2024. spin_lock_init(&adev->mm_stats.lock);
  2025. INIT_LIST_HEAD(&adev->shadow_list);
  2026. mutex_init(&adev->shadow_list_lock);
  2027. INIT_LIST_HEAD(&adev->ring_lru_list);
  2028. spin_lock_init(&adev->ring_lru_list_lock);
  2029. INIT_DELAYED_WORK(&adev->late_init_work,
  2030. amdgpu_device_ip_late_init_func_handler);
  2031. /* Registers mapping */
  2032. /* TODO: block userspace mapping of io register */
  2033. if (adev->asic_type >= CHIP_BONAIRE) {
  2034. adev->rmmio_base = pci_resource_start(adev->pdev, 5);
  2035. adev->rmmio_size = pci_resource_len(adev->pdev, 5);
  2036. } else {
  2037. adev->rmmio_base = pci_resource_start(adev->pdev, 2);
  2038. adev->rmmio_size = pci_resource_len(adev->pdev, 2);
  2039. }
  2040. adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
  2041. if (adev->rmmio == NULL) {
  2042. return -ENOMEM;
  2043. }
  2044. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
  2045. DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  2046. /* doorbell bar mapping */
  2047. amdgpu_device_doorbell_init(adev);
  2048. /* io port mapping */
  2049. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  2050. if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
  2051. adev->rio_mem_size = pci_resource_len(adev->pdev, i);
  2052. adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
  2053. break;
  2054. }
  2055. }
  2056. if (adev->rio_mem == NULL)
  2057. DRM_INFO("PCI I/O BAR is not found.\n");
  2058. amdgpu_device_get_pcie_info(adev);
  2059. /* early init functions */
  2060. r = amdgpu_device_ip_early_init(adev);
  2061. if (r)
  2062. return r;
  2063. /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
  2064. /* this will fail for cards that aren't VGA class devices, just
  2065. * ignore it */
  2066. vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
  2067. if (amdgpu_device_is_px(ddev))
  2068. runtime = true;
  2069. if (!pci_is_thunderbolt_attached(adev->pdev))
  2070. vga_switcheroo_register_client(adev->pdev,
  2071. &amdgpu_switcheroo_ops, runtime);
  2072. if (runtime)
  2073. vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
  2074. if (amdgpu_emu_mode == 1) {
  2075. /* post the asic on emulation mode */
  2076. emu_soc_asic_init(adev);
  2077. goto fence_driver_init;
  2078. }
  2079. /* Read BIOS */
  2080. if (!amdgpu_get_bios(adev)) {
  2081. r = -EINVAL;
  2082. goto failed;
  2083. }
  2084. r = amdgpu_atombios_init(adev);
  2085. if (r) {
  2086. dev_err(adev->dev, "amdgpu_atombios_init failed\n");
  2087. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
  2088. goto failed;
  2089. }
  2090. /* detect if we are with an SRIOV vbios */
  2091. amdgpu_device_detect_sriov_bios(adev);
  2092. /* Post card if necessary */
  2093. if (amdgpu_device_need_post(adev)) {
  2094. if (!adev->bios) {
  2095. dev_err(adev->dev, "no vBIOS found\n");
  2096. r = -EINVAL;
  2097. goto failed;
  2098. }
  2099. DRM_INFO("GPU posting now...\n");
  2100. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2101. if (r) {
  2102. dev_err(adev->dev, "gpu post error!\n");
  2103. goto failed;
  2104. }
  2105. }
  2106. if (adev->is_atom_fw) {
  2107. /* Initialize clocks */
  2108. r = amdgpu_atomfirmware_get_clock_info(adev);
  2109. if (r) {
  2110. dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
  2111. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  2112. goto failed;
  2113. }
  2114. } else {
  2115. /* Initialize clocks */
  2116. r = amdgpu_atombios_get_clock_info(adev);
  2117. if (r) {
  2118. dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
  2119. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  2120. goto failed;
  2121. }
  2122. /* init i2c buses */
  2123. if (!amdgpu_device_has_dc_support(adev))
  2124. amdgpu_atombios_i2c_init(adev);
  2125. }
  2126. fence_driver_init:
  2127. /* Fence driver */
  2128. r = amdgpu_fence_driver_init(adev);
  2129. if (r) {
  2130. dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
  2131. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
  2132. goto failed;
  2133. }
  2134. /* init the mode config */
  2135. drm_mode_config_init(adev->ddev);
  2136. r = amdgpu_device_ip_init(adev);
  2137. if (r) {
  2138. /* failed in exclusive mode due to timeout */
  2139. if (amdgpu_sriov_vf(adev) &&
  2140. !amdgpu_sriov_runtime(adev) &&
  2141. amdgpu_virt_mmio_blocked(adev) &&
  2142. !amdgpu_virt_wait_reset(adev)) {
  2143. dev_err(adev->dev, "VF exclusive mode timeout\n");
  2144. /* Don't send request since VF is inactive. */
  2145. adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
  2146. adev->virt.ops = NULL;
  2147. r = -EAGAIN;
  2148. goto failed;
  2149. }
  2150. dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
  2151. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
  2152. goto failed;
  2153. }
  2154. adev->accel_working = true;
  2155. amdgpu_vm_check_compute_bug(adev);
  2156. /* Initialize the buffer migration limit. */
  2157. if (amdgpu_moverate >= 0)
  2158. max_MBps = amdgpu_moverate;
  2159. else
  2160. max_MBps = 8; /* Allow 8 MB/s. */
  2161. /* Get a log2 for easy divisions. */
  2162. adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
  2163. r = amdgpu_ib_pool_init(adev);
  2164. if (r) {
  2165. dev_err(adev->dev, "IB initialization failed (%d).\n", r);
  2166. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
  2167. goto failed;
  2168. }
  2169. if (amdgpu_sriov_vf(adev))
  2170. amdgpu_virt_init_data_exchange(adev);
  2171. amdgpu_fbdev_init(adev);
  2172. r = amdgpu_pm_sysfs_init(adev);
  2173. if (r)
  2174. DRM_ERROR("registering pm debugfs failed (%d).\n", r);
  2175. r = amdgpu_debugfs_gem_init(adev);
  2176. if (r)
  2177. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  2178. r = amdgpu_debugfs_regs_init(adev);
  2179. if (r)
  2180. DRM_ERROR("registering register debugfs failed (%d).\n", r);
  2181. r = amdgpu_debugfs_firmware_init(adev);
  2182. if (r)
  2183. DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
  2184. r = amdgpu_debugfs_init(adev);
  2185. if (r)
  2186. DRM_ERROR("Creating debugfs files failed (%d).\n", r);
  2187. if ((amdgpu_testing & 1)) {
  2188. if (adev->accel_working)
  2189. amdgpu_test_moves(adev);
  2190. else
  2191. DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
  2192. }
  2193. if (amdgpu_benchmarking) {
  2194. if (adev->accel_working)
  2195. amdgpu_benchmark(adev, amdgpu_benchmarking);
  2196. else
  2197. DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
  2198. }
  2199. /* enable clockgating, etc. after ib tests, etc. since some blocks require
  2200. * explicit gating rather than handling it automatically.
  2201. */
  2202. r = amdgpu_device_ip_late_init(adev);
  2203. if (r) {
  2204. dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
  2205. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
  2206. goto failed;
  2207. }
  2208. return 0;
  2209. failed:
  2210. amdgpu_vf_error_trans_all(adev);
  2211. if (runtime)
  2212. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  2213. return r;
  2214. }
  2215. /**
  2216. * amdgpu_device_fini - tear down the driver
  2217. *
  2218. * @adev: amdgpu_device pointer
  2219. *
  2220. * Tear down the driver info (all asics).
  2221. * Called at driver shutdown.
  2222. */
  2223. void amdgpu_device_fini(struct amdgpu_device *adev)
  2224. {
  2225. int r;
  2226. DRM_INFO("amdgpu: finishing device.\n");
  2227. adev->shutdown = true;
  2228. /* disable all interrupts */
  2229. amdgpu_irq_disable_all(adev);
  2230. if (adev->mode_info.mode_config_initialized){
  2231. if (!amdgpu_device_has_dc_support(adev))
  2232. drm_crtc_force_disable_all(adev->ddev);
  2233. else
  2234. drm_atomic_helper_shutdown(adev->ddev);
  2235. }
  2236. amdgpu_ib_pool_fini(adev);
  2237. amdgpu_fence_driver_fini(adev);
  2238. amdgpu_pm_sysfs_fini(adev);
  2239. amdgpu_fbdev_fini(adev);
  2240. r = amdgpu_device_ip_fini(adev);
  2241. if (adev->firmware.gpu_info_fw) {
  2242. release_firmware(adev->firmware.gpu_info_fw);
  2243. adev->firmware.gpu_info_fw = NULL;
  2244. }
  2245. adev->accel_working = false;
  2246. cancel_delayed_work_sync(&adev->late_init_work);
  2247. /* free i2c buses */
  2248. if (!amdgpu_device_has_dc_support(adev))
  2249. amdgpu_i2c_fini(adev);
  2250. if (amdgpu_emu_mode != 1)
  2251. amdgpu_atombios_fini(adev);
  2252. kfree(adev->bios);
  2253. adev->bios = NULL;
  2254. if (!pci_is_thunderbolt_attached(adev->pdev))
  2255. vga_switcheroo_unregister_client(adev->pdev);
  2256. if (adev->flags & AMD_IS_PX)
  2257. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  2258. vga_client_register(adev->pdev, NULL, NULL, NULL);
  2259. if (adev->rio_mem)
  2260. pci_iounmap(adev->pdev, adev->rio_mem);
  2261. adev->rio_mem = NULL;
  2262. iounmap(adev->rmmio);
  2263. adev->rmmio = NULL;
  2264. amdgpu_device_doorbell_fini(adev);
  2265. amdgpu_debugfs_regs_cleanup(adev);
  2266. }
  2267. /*
  2268. * Suspend & resume.
  2269. */
  2270. /**
  2271. * amdgpu_device_suspend - initiate device suspend
  2272. *
  2273. * @pdev: drm dev pointer
  2274. * @state: suspend state
  2275. *
  2276. * Puts the hw in the suspend state (all asics).
  2277. * Returns 0 for success or an error on failure.
  2278. * Called at driver suspend.
  2279. */
  2280. int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
  2281. {
  2282. struct amdgpu_device *adev;
  2283. struct drm_crtc *crtc;
  2284. struct drm_connector *connector;
  2285. int r;
  2286. if (dev == NULL || dev->dev_private == NULL) {
  2287. return -ENODEV;
  2288. }
  2289. adev = dev->dev_private;
  2290. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2291. return 0;
  2292. drm_kms_helper_poll_disable(dev);
  2293. if (!amdgpu_device_has_dc_support(adev)) {
  2294. /* turn off display hw */
  2295. drm_modeset_lock_all(dev);
  2296. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2297. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  2298. }
  2299. drm_modeset_unlock_all(dev);
  2300. }
  2301. amdgpu_amdkfd_suspend(adev);
  2302. /* unpin the front buffers and cursors */
  2303. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2304. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2305. struct drm_framebuffer *fb = crtc->primary->fb;
  2306. struct amdgpu_bo *robj;
  2307. if (amdgpu_crtc->cursor_bo) {
  2308. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2309. r = amdgpu_bo_reserve(aobj, true);
  2310. if (r == 0) {
  2311. amdgpu_bo_unpin(aobj);
  2312. amdgpu_bo_unreserve(aobj);
  2313. }
  2314. }
  2315. if (fb == NULL || fb->obj[0] == NULL) {
  2316. continue;
  2317. }
  2318. robj = gem_to_amdgpu_bo(fb->obj[0]);
  2319. /* don't unpin kernel fb objects */
  2320. if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
  2321. r = amdgpu_bo_reserve(robj, true);
  2322. if (r == 0) {
  2323. amdgpu_bo_unpin(robj);
  2324. amdgpu_bo_unreserve(robj);
  2325. }
  2326. }
  2327. }
  2328. /* evict vram memory */
  2329. amdgpu_bo_evict_vram(adev);
  2330. amdgpu_fence_driver_suspend(adev);
  2331. r = amdgpu_device_ip_suspend(adev);
  2332. /* evict remaining vram memory
  2333. * This second call to evict vram is to evict the gart page table
  2334. * using the CPU.
  2335. */
  2336. amdgpu_bo_evict_vram(adev);
  2337. pci_save_state(dev->pdev);
  2338. if (suspend) {
  2339. /* Shut down the device */
  2340. pci_disable_device(dev->pdev);
  2341. pci_set_power_state(dev->pdev, PCI_D3hot);
  2342. } else {
  2343. r = amdgpu_asic_reset(adev);
  2344. if (r)
  2345. DRM_ERROR("amdgpu asic reset failed\n");
  2346. }
  2347. if (fbcon) {
  2348. console_lock();
  2349. amdgpu_fbdev_set_suspend(adev, 1);
  2350. console_unlock();
  2351. }
  2352. return 0;
  2353. }
  2354. /**
  2355. * amdgpu_device_resume - initiate device resume
  2356. *
  2357. * @pdev: drm dev pointer
  2358. *
  2359. * Bring the hw back to operating state (all asics).
  2360. * Returns 0 for success or an error on failure.
  2361. * Called at driver resume.
  2362. */
  2363. int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
  2364. {
  2365. struct drm_connector *connector;
  2366. struct amdgpu_device *adev = dev->dev_private;
  2367. struct drm_crtc *crtc;
  2368. int r = 0;
  2369. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2370. return 0;
  2371. if (fbcon)
  2372. console_lock();
  2373. if (resume) {
  2374. pci_set_power_state(dev->pdev, PCI_D0);
  2375. pci_restore_state(dev->pdev);
  2376. r = pci_enable_device(dev->pdev);
  2377. if (r)
  2378. goto unlock;
  2379. }
  2380. /* post card */
  2381. if (amdgpu_device_need_post(adev)) {
  2382. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2383. if (r)
  2384. DRM_ERROR("amdgpu asic init failed\n");
  2385. }
  2386. r = amdgpu_device_ip_resume(adev);
  2387. if (r) {
  2388. DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
  2389. goto unlock;
  2390. }
  2391. amdgpu_fence_driver_resume(adev);
  2392. r = amdgpu_device_ip_late_init(adev);
  2393. if (r)
  2394. goto unlock;
  2395. /* pin cursors */
  2396. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2397. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2398. if (amdgpu_crtc->cursor_bo) {
  2399. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2400. r = amdgpu_bo_reserve(aobj, true);
  2401. if (r == 0) {
  2402. r = amdgpu_bo_pin(aobj,
  2403. AMDGPU_GEM_DOMAIN_VRAM,
  2404. &amdgpu_crtc->cursor_addr);
  2405. if (r != 0)
  2406. DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
  2407. amdgpu_bo_unreserve(aobj);
  2408. }
  2409. }
  2410. }
  2411. r = amdgpu_amdkfd_resume(adev);
  2412. if (r)
  2413. return r;
  2414. /* blat the mode back in */
  2415. if (fbcon) {
  2416. if (!amdgpu_device_has_dc_support(adev)) {
  2417. /* pre DCE11 */
  2418. drm_helper_resume_force_mode(dev);
  2419. /* turn on display hw */
  2420. drm_modeset_lock_all(dev);
  2421. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2422. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  2423. }
  2424. drm_modeset_unlock_all(dev);
  2425. }
  2426. }
  2427. drm_kms_helper_poll_enable(dev);
  2428. /*
  2429. * Most of the connector probing functions try to acquire runtime pm
  2430. * refs to ensure that the GPU is powered on when connector polling is
  2431. * performed. Since we're calling this from a runtime PM callback,
  2432. * trying to acquire rpm refs will cause us to deadlock.
  2433. *
  2434. * Since we're guaranteed to be holding the rpm lock, it's safe to
  2435. * temporarily disable the rpm helpers so this doesn't deadlock us.
  2436. */
  2437. #ifdef CONFIG_PM
  2438. dev->dev->power.disable_depth++;
  2439. #endif
  2440. if (!amdgpu_device_has_dc_support(adev))
  2441. drm_helper_hpd_irq_event(dev);
  2442. else
  2443. drm_kms_helper_hotplug_event(dev);
  2444. #ifdef CONFIG_PM
  2445. dev->dev->power.disable_depth--;
  2446. #endif
  2447. if (fbcon)
  2448. amdgpu_fbdev_set_suspend(adev, 0);
  2449. unlock:
  2450. if (fbcon)
  2451. console_unlock();
  2452. return r;
  2453. }
  2454. /**
  2455. * amdgpu_device_ip_check_soft_reset - did soft reset succeed
  2456. *
  2457. * @adev: amdgpu_device pointer
  2458. *
  2459. * The list of all the hardware IPs that make up the asic is walked and
  2460. * the check_soft_reset callbacks are run. check_soft_reset determines
  2461. * if the asic is still hung or not.
  2462. * Returns true if any of the IPs are still in a hung state, false if not.
  2463. */
  2464. static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
  2465. {
  2466. int i;
  2467. bool asic_hang = false;
  2468. if (amdgpu_sriov_vf(adev))
  2469. return true;
  2470. if (amdgpu_asic_need_full_reset(adev))
  2471. return true;
  2472. for (i = 0; i < adev->num_ip_blocks; i++) {
  2473. if (!adev->ip_blocks[i].status.valid)
  2474. continue;
  2475. if (adev->ip_blocks[i].version->funcs->check_soft_reset)
  2476. adev->ip_blocks[i].status.hang =
  2477. adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
  2478. if (adev->ip_blocks[i].status.hang) {
  2479. DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
  2480. asic_hang = true;
  2481. }
  2482. }
  2483. return asic_hang;
  2484. }
  2485. /**
  2486. * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
  2487. *
  2488. * @adev: amdgpu_device pointer
  2489. *
  2490. * The list of all the hardware IPs that make up the asic is walked and the
  2491. * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
  2492. * handles any IP specific hardware or software state changes that are
  2493. * necessary for a soft reset to succeed.
  2494. * Returns 0 on success, negative error code on failure.
  2495. */
  2496. static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
  2497. {
  2498. int i, r = 0;
  2499. for (i = 0; i < adev->num_ip_blocks; i++) {
  2500. if (!adev->ip_blocks[i].status.valid)
  2501. continue;
  2502. if (adev->ip_blocks[i].status.hang &&
  2503. adev->ip_blocks[i].version->funcs->pre_soft_reset) {
  2504. r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
  2505. if (r)
  2506. return r;
  2507. }
  2508. }
  2509. return 0;
  2510. }
  2511. /**
  2512. * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
  2513. *
  2514. * @adev: amdgpu_device pointer
  2515. *
  2516. * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
  2517. * reset is necessary to recover.
  2518. * Returns true if a full asic reset is required, false if not.
  2519. */
  2520. static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
  2521. {
  2522. int i;
  2523. if (amdgpu_asic_need_full_reset(adev))
  2524. return true;
  2525. for (i = 0; i < adev->num_ip_blocks; i++) {
  2526. if (!adev->ip_blocks[i].status.valid)
  2527. continue;
  2528. if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
  2529. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
  2530. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
  2531. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
  2532. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
  2533. if (adev->ip_blocks[i].status.hang) {
  2534. DRM_INFO("Some block need full reset!\n");
  2535. return true;
  2536. }
  2537. }
  2538. }
  2539. return false;
  2540. }
  2541. /**
  2542. * amdgpu_device_ip_soft_reset - do a soft reset
  2543. *
  2544. * @adev: amdgpu_device pointer
  2545. *
  2546. * The list of all the hardware IPs that make up the asic is walked and the
  2547. * soft_reset callbacks are run if the block is hung. soft_reset handles any
  2548. * IP specific hardware or software state changes that are necessary to soft
  2549. * reset the IP.
  2550. * Returns 0 on success, negative error code on failure.
  2551. */
  2552. static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
  2553. {
  2554. int i, r = 0;
  2555. for (i = 0; i < adev->num_ip_blocks; i++) {
  2556. if (!adev->ip_blocks[i].status.valid)
  2557. continue;
  2558. if (adev->ip_blocks[i].status.hang &&
  2559. adev->ip_blocks[i].version->funcs->soft_reset) {
  2560. r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
  2561. if (r)
  2562. return r;
  2563. }
  2564. }
  2565. return 0;
  2566. }
  2567. /**
  2568. * amdgpu_device_ip_post_soft_reset - clean up from soft reset
  2569. *
  2570. * @adev: amdgpu_device pointer
  2571. *
  2572. * The list of all the hardware IPs that make up the asic is walked and the
  2573. * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
  2574. * handles any IP specific hardware or software state changes that are
  2575. * necessary after the IP has been soft reset.
  2576. * Returns 0 on success, negative error code on failure.
  2577. */
  2578. static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
  2579. {
  2580. int i, r = 0;
  2581. for (i = 0; i < adev->num_ip_blocks; i++) {
  2582. if (!adev->ip_blocks[i].status.valid)
  2583. continue;
  2584. if (adev->ip_blocks[i].status.hang &&
  2585. adev->ip_blocks[i].version->funcs->post_soft_reset)
  2586. r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
  2587. if (r)
  2588. return r;
  2589. }
  2590. return 0;
  2591. }
  2592. /**
  2593. * amdgpu_device_recover_vram_from_shadow - restore shadowed VRAM buffers
  2594. *
  2595. * @adev: amdgpu_device pointer
  2596. * @ring: amdgpu_ring for the engine handling the buffer operations
  2597. * @bo: amdgpu_bo buffer whose shadow is being restored
  2598. * @fence: dma_fence associated with the operation
  2599. *
  2600. * Restores the VRAM buffer contents from the shadow in GTT. Used to
  2601. * restore things like GPUVM page tables after a GPU reset where
  2602. * the contents of VRAM might be lost.
  2603. * Returns 0 on success, negative error code on failure.
  2604. */
  2605. static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
  2606. struct amdgpu_ring *ring,
  2607. struct amdgpu_bo *bo,
  2608. struct dma_fence **fence)
  2609. {
  2610. uint32_t domain;
  2611. int r;
  2612. if (!bo->shadow)
  2613. return 0;
  2614. r = amdgpu_bo_reserve(bo, true);
  2615. if (r)
  2616. return r;
  2617. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  2618. /* if bo has been evicted, then no need to recover */
  2619. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  2620. r = amdgpu_bo_validate(bo->shadow);
  2621. if (r) {
  2622. DRM_ERROR("bo validate failed!\n");
  2623. goto err;
  2624. }
  2625. r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
  2626. NULL, fence, true);
  2627. if (r) {
  2628. DRM_ERROR("recover page table failed!\n");
  2629. goto err;
  2630. }
  2631. }
  2632. err:
  2633. amdgpu_bo_unreserve(bo);
  2634. return r;
  2635. }
  2636. /**
  2637. * amdgpu_device_handle_vram_lost - Handle the loss of VRAM contents
  2638. *
  2639. * @adev: amdgpu_device pointer
  2640. *
  2641. * Restores the contents of VRAM buffers from the shadows in GTT. Used to
  2642. * restore things like GPUVM page tables after a GPU reset where
  2643. * the contents of VRAM might be lost.
  2644. * Returns 0 on success, 1 on failure.
  2645. */
  2646. static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
  2647. {
  2648. struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  2649. struct amdgpu_bo *bo, *tmp;
  2650. struct dma_fence *fence = NULL, *next = NULL;
  2651. long r = 1;
  2652. int i = 0;
  2653. long tmo;
  2654. if (amdgpu_sriov_runtime(adev))
  2655. tmo = msecs_to_jiffies(amdgpu_lockup_timeout);
  2656. else
  2657. tmo = msecs_to_jiffies(100);
  2658. DRM_INFO("recover vram bo from shadow start\n");
  2659. mutex_lock(&adev->shadow_list_lock);
  2660. list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
  2661. next = NULL;
  2662. amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
  2663. if (fence) {
  2664. r = dma_fence_wait_timeout(fence, false, tmo);
  2665. if (r == 0)
  2666. pr_err("wait fence %p[%d] timeout\n", fence, i);
  2667. else if (r < 0)
  2668. pr_err("wait fence %p[%d] interrupted\n", fence, i);
  2669. if (r < 1) {
  2670. dma_fence_put(fence);
  2671. fence = next;
  2672. break;
  2673. }
  2674. i++;
  2675. }
  2676. dma_fence_put(fence);
  2677. fence = next;
  2678. }
  2679. mutex_unlock(&adev->shadow_list_lock);
  2680. if (fence) {
  2681. r = dma_fence_wait_timeout(fence, false, tmo);
  2682. if (r == 0)
  2683. pr_err("wait fence %p[%d] timeout\n", fence, i);
  2684. else if (r < 0)
  2685. pr_err("wait fence %p[%d] interrupted\n", fence, i);
  2686. }
  2687. dma_fence_put(fence);
  2688. if (r > 0)
  2689. DRM_INFO("recover vram bo from shadow done\n");
  2690. else
  2691. DRM_ERROR("recover vram bo from shadow failed\n");
  2692. return (r > 0) ? 0 : 1;
  2693. }
  2694. /**
  2695. * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
  2696. *
  2697. * @adev: amdgpu device pointer
  2698. *
  2699. * attempt to do soft-reset or full-reset and reinitialize Asic
  2700. * return 0 means successed otherwise failed
  2701. */
  2702. static int amdgpu_device_reset(struct amdgpu_device *adev)
  2703. {
  2704. bool need_full_reset, vram_lost = 0;
  2705. int r;
  2706. need_full_reset = amdgpu_device_ip_need_full_reset(adev);
  2707. if (!need_full_reset) {
  2708. amdgpu_device_ip_pre_soft_reset(adev);
  2709. r = amdgpu_device_ip_soft_reset(adev);
  2710. amdgpu_device_ip_post_soft_reset(adev);
  2711. if (r || amdgpu_device_ip_check_soft_reset(adev)) {
  2712. DRM_INFO("soft reset failed, will fallback to full reset!\n");
  2713. need_full_reset = true;
  2714. }
  2715. }
  2716. if (need_full_reset) {
  2717. r = amdgpu_device_ip_suspend(adev);
  2718. retry:
  2719. r = amdgpu_asic_reset(adev);
  2720. /* post card */
  2721. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2722. if (!r) {
  2723. dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
  2724. r = amdgpu_device_ip_resume_phase1(adev);
  2725. if (r)
  2726. goto out;
  2727. vram_lost = amdgpu_device_check_vram_lost(adev);
  2728. if (vram_lost) {
  2729. DRM_ERROR("VRAM is lost!\n");
  2730. atomic_inc(&adev->vram_lost_counter);
  2731. }
  2732. r = amdgpu_gtt_mgr_recover(
  2733. &adev->mman.bdev.man[TTM_PL_TT]);
  2734. if (r)
  2735. goto out;
  2736. r = amdgpu_device_ip_resume_phase2(adev);
  2737. if (r)
  2738. goto out;
  2739. if (vram_lost)
  2740. amdgpu_device_fill_reset_magic(adev);
  2741. }
  2742. }
  2743. out:
  2744. if (!r) {
  2745. amdgpu_irq_gpu_reset_resume_helper(adev);
  2746. r = amdgpu_ib_ring_tests(adev);
  2747. if (r) {
  2748. dev_err(adev->dev, "ib ring test failed (%d).\n", r);
  2749. r = amdgpu_device_ip_suspend(adev);
  2750. need_full_reset = true;
  2751. goto retry;
  2752. }
  2753. }
  2754. if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
  2755. r = amdgpu_device_handle_vram_lost(adev);
  2756. return r;
  2757. }
  2758. /**
  2759. * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
  2760. *
  2761. * @adev: amdgpu device pointer
  2762. *
  2763. * do VF FLR and reinitialize Asic
  2764. * return 0 means successed otherwise failed
  2765. */
  2766. static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
  2767. bool from_hypervisor)
  2768. {
  2769. int r;
  2770. if (from_hypervisor)
  2771. r = amdgpu_virt_request_full_gpu(adev, true);
  2772. else
  2773. r = amdgpu_virt_reset_gpu(adev);
  2774. if (r)
  2775. return r;
  2776. /* Resume IP prior to SMC */
  2777. r = amdgpu_device_ip_reinit_early_sriov(adev);
  2778. if (r)
  2779. goto error;
  2780. /* we need recover gart prior to run SMC/CP/SDMA resume */
  2781. amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
  2782. /* now we are okay to resume SMC/CP/SDMA */
  2783. r = amdgpu_device_ip_reinit_late_sriov(adev);
  2784. amdgpu_virt_release_full_gpu(adev, true);
  2785. if (r)
  2786. goto error;
  2787. amdgpu_irq_gpu_reset_resume_helper(adev);
  2788. r = amdgpu_ib_ring_tests(adev);
  2789. if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
  2790. atomic_inc(&adev->vram_lost_counter);
  2791. r = amdgpu_device_handle_vram_lost(adev);
  2792. }
  2793. error:
  2794. return r;
  2795. }
  2796. /**
  2797. * amdgpu_device_gpu_recover - reset the asic and recover scheduler
  2798. *
  2799. * @adev: amdgpu device pointer
  2800. * @job: which job trigger hang
  2801. * @force forces reset regardless of amdgpu_gpu_recovery
  2802. *
  2803. * Attempt to reset the GPU if it has hung (all asics).
  2804. * Returns 0 for success or an error on failure.
  2805. */
  2806. int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  2807. struct amdgpu_job *job, bool force)
  2808. {
  2809. struct drm_atomic_state *state = NULL;
  2810. int i, r, resched;
  2811. if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
  2812. DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
  2813. return 0;
  2814. }
  2815. if (!force && (amdgpu_gpu_recovery == 0 ||
  2816. (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
  2817. DRM_INFO("GPU recovery disabled.\n");
  2818. return 0;
  2819. }
  2820. dev_info(adev->dev, "GPU reset begin!\n");
  2821. mutex_lock(&adev->lock_reset);
  2822. atomic_inc(&adev->gpu_reset_counter);
  2823. adev->in_gpu_reset = 1;
  2824. /* block TTM */
  2825. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  2826. /* store modesetting */
  2827. if (amdgpu_device_has_dc_support(adev))
  2828. state = drm_atomic_helper_suspend(adev->ddev);
  2829. /* block all schedulers and reset given job's ring */
  2830. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2831. struct amdgpu_ring *ring = adev->rings[i];
  2832. if (!ring || !ring->sched.thread)
  2833. continue;
  2834. kthread_park(ring->sched.thread);
  2835. if (job && job->ring->idx != i)
  2836. continue;
  2837. drm_sched_hw_job_reset(&ring->sched, &job->base);
  2838. /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
  2839. amdgpu_fence_driver_force_completion(ring);
  2840. }
  2841. if (amdgpu_sriov_vf(adev))
  2842. r = amdgpu_device_reset_sriov(adev, job ? false : true);
  2843. else
  2844. r = amdgpu_device_reset(adev);
  2845. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2846. struct amdgpu_ring *ring = adev->rings[i];
  2847. if (!ring || !ring->sched.thread)
  2848. continue;
  2849. /* only need recovery sched of the given job's ring
  2850. * or all rings (in the case @job is NULL)
  2851. * after above amdgpu_reset accomplished
  2852. */
  2853. if ((!job || job->ring->idx == i) && !r)
  2854. drm_sched_job_recovery(&ring->sched);
  2855. kthread_unpark(ring->sched.thread);
  2856. }
  2857. if (amdgpu_device_has_dc_support(adev)) {
  2858. if (drm_atomic_helper_resume(adev->ddev, state))
  2859. dev_info(adev->dev, "drm resume failed:%d\n", r);
  2860. } else {
  2861. drm_helper_resume_force_mode(adev->ddev);
  2862. }
  2863. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  2864. if (r) {
  2865. /* bad news, how to tell it to userspace ? */
  2866. dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
  2867. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
  2868. } else {
  2869. dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
  2870. }
  2871. amdgpu_vf_error_trans_all(adev);
  2872. adev->in_gpu_reset = 0;
  2873. mutex_unlock(&adev->lock_reset);
  2874. return r;
  2875. }
  2876. /**
  2877. * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
  2878. *
  2879. * @adev: amdgpu_device pointer
  2880. *
  2881. * Fetchs and stores in the driver the PCIE capabilities (gen speed
  2882. * and lanes) of the slot the device is in. Handles APUs and
  2883. * virtualized environments where PCIE config space may not be available.
  2884. */
  2885. static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
  2886. {
  2887. u32 mask;
  2888. int ret;
  2889. if (amdgpu_pcie_gen_cap)
  2890. adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
  2891. if (amdgpu_pcie_lane_cap)
  2892. adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
  2893. /* covers APUs as well */
  2894. if (pci_is_root_bus(adev->pdev->bus)) {
  2895. if (adev->pm.pcie_gen_mask == 0)
  2896. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2897. if (adev->pm.pcie_mlw_mask == 0)
  2898. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2899. return;
  2900. }
  2901. if (adev->pm.pcie_gen_mask == 0) {
  2902. ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
  2903. if (!ret) {
  2904. adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2905. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2906. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2907. if (mask & DRM_PCIE_SPEED_25)
  2908. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
  2909. if (mask & DRM_PCIE_SPEED_50)
  2910. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
  2911. if (mask & DRM_PCIE_SPEED_80)
  2912. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
  2913. } else {
  2914. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2915. }
  2916. }
  2917. if (adev->pm.pcie_mlw_mask == 0) {
  2918. ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
  2919. if (!ret) {
  2920. switch (mask) {
  2921. case 32:
  2922. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
  2923. CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2924. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2925. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2926. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2927. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2928. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2929. break;
  2930. case 16:
  2931. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2932. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2933. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2934. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2935. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2936. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2937. break;
  2938. case 12:
  2939. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2940. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2941. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2942. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2943. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2944. break;
  2945. case 8:
  2946. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2947. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2948. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2949. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2950. break;
  2951. case 4:
  2952. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2953. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2954. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2955. break;
  2956. case 2:
  2957. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2958. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2959. break;
  2960. case 1:
  2961. adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
  2962. break;
  2963. default:
  2964. break;
  2965. }
  2966. } else {
  2967. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2968. }
  2969. }
  2970. }