amdgpu_device.c 79 KB

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