amdgpu_device.c 76 KB

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