amdgpu_device.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426
  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/console.h>
  29. #include <linux/slab.h>
  30. #include <linux/debugfs.h>
  31. #include <drm/drmP.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include <drm/amdgpu_drm.h>
  34. #include <linux/vgaarb.h>
  35. #include <linux/vga_switcheroo.h>
  36. #include <linux/efi.h>
  37. #include "amdgpu.h"
  38. #include "amdgpu_trace.h"
  39. #include "amdgpu_i2c.h"
  40. #include "atom.h"
  41. #include "amdgpu_atombios.h"
  42. #include "amd_pcie.h"
  43. #ifdef CONFIG_DRM_AMDGPU_CIK
  44. #include "cik.h"
  45. #endif
  46. #include "vi.h"
  47. #include "bif/bif_4_1_d.h"
  48. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
  49. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
  50. static const char *amdgpu_asic_name[] = {
  51. "BONAIRE",
  52. "KAVERI",
  53. "KABINI",
  54. "HAWAII",
  55. "MULLINS",
  56. "TOPAZ",
  57. "TONGA",
  58. "FIJI",
  59. "CARRIZO",
  60. "STONEY",
  61. "POLARIS10",
  62. "POLARIS11",
  63. "LAST",
  64. };
  65. bool amdgpu_device_is_px(struct drm_device *dev)
  66. {
  67. struct amdgpu_device *adev = dev->dev_private;
  68. if (adev->flags & AMD_IS_PX)
  69. return true;
  70. return false;
  71. }
  72. /*
  73. * MMIO register access helper functions.
  74. */
  75. uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
  76. bool always_indirect)
  77. {
  78. uint32_t ret;
  79. if ((reg * 4) < adev->rmmio_size && !always_indirect)
  80. ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
  81. else {
  82. unsigned long flags;
  83. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  84. writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
  85. ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  86. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  87. }
  88. trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
  89. return ret;
  90. }
  91. void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
  92. bool always_indirect)
  93. {
  94. trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
  95. if ((reg * 4) < adev->rmmio_size && !always_indirect)
  96. writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
  97. else {
  98. unsigned long flags;
  99. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  100. writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
  101. writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  102. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  103. }
  104. }
  105. u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
  106. {
  107. if ((reg * 4) < adev->rio_mem_size)
  108. return ioread32(adev->rio_mem + (reg * 4));
  109. else {
  110. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  111. return ioread32(adev->rio_mem + (mmMM_DATA * 4));
  112. }
  113. }
  114. void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
  115. {
  116. if ((reg * 4) < adev->rio_mem_size)
  117. iowrite32(v, adev->rio_mem + (reg * 4));
  118. else {
  119. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  120. iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
  121. }
  122. }
  123. /**
  124. * amdgpu_mm_rdoorbell - read a doorbell dword
  125. *
  126. * @adev: amdgpu_device pointer
  127. * @index: doorbell index
  128. *
  129. * Returns the value in the doorbell aperture at the
  130. * requested doorbell index (CIK).
  131. */
  132. u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
  133. {
  134. if (index < adev->doorbell.num_doorbells) {
  135. return readl(adev->doorbell.ptr + index);
  136. } else {
  137. DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
  138. return 0;
  139. }
  140. }
  141. /**
  142. * amdgpu_mm_wdoorbell - write a doorbell dword
  143. *
  144. * @adev: amdgpu_device pointer
  145. * @index: doorbell index
  146. * @v: value to write
  147. *
  148. * Writes @v to the doorbell aperture at the
  149. * requested doorbell index (CIK).
  150. */
  151. void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
  152. {
  153. if (index < adev->doorbell.num_doorbells) {
  154. writel(v, adev->doorbell.ptr + index);
  155. } else {
  156. DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  157. }
  158. }
  159. /**
  160. * amdgpu_invalid_rreg - dummy reg read function
  161. *
  162. * @adev: amdgpu device pointer
  163. * @reg: offset of register
  164. *
  165. * Dummy register read function. Used for register blocks
  166. * that certain asics don't have (all asics).
  167. * Returns the value in the register.
  168. */
  169. static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
  170. {
  171. DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
  172. BUG();
  173. return 0;
  174. }
  175. /**
  176. * amdgpu_invalid_wreg - dummy reg write function
  177. *
  178. * @adev: amdgpu device pointer
  179. * @reg: offset of register
  180. * @v: value to write to the register
  181. *
  182. * Dummy register read function. Used for register blocks
  183. * that certain asics don't have (all asics).
  184. */
  185. static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
  186. {
  187. DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
  188. reg, v);
  189. BUG();
  190. }
  191. /**
  192. * amdgpu_block_invalid_rreg - dummy reg read function
  193. *
  194. * @adev: amdgpu device pointer
  195. * @block: offset of instance
  196. * @reg: offset of register
  197. *
  198. * Dummy register read function. Used for register blocks
  199. * that certain asics don't have (all asics).
  200. * Returns the value in the register.
  201. */
  202. static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
  203. uint32_t block, uint32_t reg)
  204. {
  205. DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
  206. reg, block);
  207. BUG();
  208. return 0;
  209. }
  210. /**
  211. * amdgpu_block_invalid_wreg - dummy reg write function
  212. *
  213. * @adev: amdgpu device pointer
  214. * @block: offset of instance
  215. * @reg: offset of register
  216. * @v: value to write to the register
  217. *
  218. * Dummy register read function. Used for register blocks
  219. * that certain asics don't have (all asics).
  220. */
  221. static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
  222. uint32_t block,
  223. uint32_t reg, uint32_t v)
  224. {
  225. DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
  226. reg, block, v);
  227. BUG();
  228. }
  229. static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
  230. {
  231. int r;
  232. if (adev->vram_scratch.robj == NULL) {
  233. r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
  234. PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
  235. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  236. NULL, NULL, &adev->vram_scratch.robj);
  237. if (r) {
  238. return r;
  239. }
  240. }
  241. r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
  242. if (unlikely(r != 0))
  243. return r;
  244. r = amdgpu_bo_pin(adev->vram_scratch.robj,
  245. AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
  246. if (r) {
  247. amdgpu_bo_unreserve(adev->vram_scratch.robj);
  248. return r;
  249. }
  250. r = amdgpu_bo_kmap(adev->vram_scratch.robj,
  251. (void **)&adev->vram_scratch.ptr);
  252. if (r)
  253. amdgpu_bo_unpin(adev->vram_scratch.robj);
  254. amdgpu_bo_unreserve(adev->vram_scratch.robj);
  255. return r;
  256. }
  257. static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
  258. {
  259. int r;
  260. if (adev->vram_scratch.robj == NULL) {
  261. return;
  262. }
  263. r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
  264. if (likely(r == 0)) {
  265. amdgpu_bo_kunmap(adev->vram_scratch.robj);
  266. amdgpu_bo_unpin(adev->vram_scratch.robj);
  267. amdgpu_bo_unreserve(adev->vram_scratch.robj);
  268. }
  269. amdgpu_bo_unref(&adev->vram_scratch.robj);
  270. }
  271. /**
  272. * amdgpu_program_register_sequence - program an array of registers.
  273. *
  274. * @adev: amdgpu_device pointer
  275. * @registers: pointer to the register array
  276. * @array_size: size of the register array
  277. *
  278. * Programs an array or registers with and and or masks.
  279. * This is a helper for setting golden registers.
  280. */
  281. void amdgpu_program_register_sequence(struct amdgpu_device *adev,
  282. const u32 *registers,
  283. const u32 array_size)
  284. {
  285. u32 tmp, reg, and_mask, or_mask;
  286. int i;
  287. if (array_size % 3)
  288. return;
  289. for (i = 0; i < array_size; i +=3) {
  290. reg = registers[i + 0];
  291. and_mask = registers[i + 1];
  292. or_mask = registers[i + 2];
  293. if (and_mask == 0xffffffff) {
  294. tmp = or_mask;
  295. } else {
  296. tmp = RREG32(reg);
  297. tmp &= ~and_mask;
  298. tmp |= or_mask;
  299. }
  300. WREG32(reg, tmp);
  301. }
  302. }
  303. void amdgpu_pci_config_reset(struct amdgpu_device *adev)
  304. {
  305. pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
  306. }
  307. /*
  308. * GPU doorbell aperture helpers function.
  309. */
  310. /**
  311. * amdgpu_doorbell_init - Init doorbell driver information.
  312. *
  313. * @adev: amdgpu_device pointer
  314. *
  315. * Init doorbell driver information (CIK)
  316. * Returns 0 on success, error on failure.
  317. */
  318. static int amdgpu_doorbell_init(struct amdgpu_device *adev)
  319. {
  320. /* doorbell bar mapping */
  321. adev->doorbell.base = pci_resource_start(adev->pdev, 2);
  322. adev->doorbell.size = pci_resource_len(adev->pdev, 2);
  323. adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
  324. AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
  325. if (adev->doorbell.num_doorbells == 0)
  326. return -EINVAL;
  327. adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
  328. if (adev->doorbell.ptr == NULL) {
  329. return -ENOMEM;
  330. }
  331. DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
  332. DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
  333. return 0;
  334. }
  335. /**
  336. * amdgpu_doorbell_fini - Tear down doorbell driver information.
  337. *
  338. * @adev: amdgpu_device pointer
  339. *
  340. * Tear down doorbell driver information (CIK)
  341. */
  342. static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
  343. {
  344. iounmap(adev->doorbell.ptr);
  345. adev->doorbell.ptr = NULL;
  346. }
  347. /**
  348. * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
  349. * setup amdkfd
  350. *
  351. * @adev: amdgpu_device pointer
  352. * @aperture_base: output returning doorbell aperture base physical address
  353. * @aperture_size: output returning doorbell aperture size in bytes
  354. * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
  355. *
  356. * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
  357. * takes doorbells required for its own rings and reports the setup to amdkfd.
  358. * amdgpu reserved doorbells are at the start of the doorbell aperture.
  359. */
  360. void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
  361. phys_addr_t *aperture_base,
  362. size_t *aperture_size,
  363. size_t *start_offset)
  364. {
  365. /*
  366. * The first num_doorbells are used by amdgpu.
  367. * amdkfd takes whatever's left in the aperture.
  368. */
  369. if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
  370. *aperture_base = adev->doorbell.base;
  371. *aperture_size = adev->doorbell.size;
  372. *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
  373. } else {
  374. *aperture_base = 0;
  375. *aperture_size = 0;
  376. *start_offset = 0;
  377. }
  378. }
  379. /*
  380. * amdgpu_wb_*()
  381. * Writeback is the the method by which the the GPU updates special pages
  382. * in memory with the status of certain GPU events (fences, ring pointers,
  383. * etc.).
  384. */
  385. /**
  386. * amdgpu_wb_fini - Disable Writeback and free memory
  387. *
  388. * @adev: amdgpu_device pointer
  389. *
  390. * Disables Writeback and frees the Writeback memory (all asics).
  391. * Used at driver shutdown.
  392. */
  393. static void amdgpu_wb_fini(struct amdgpu_device *adev)
  394. {
  395. if (adev->wb.wb_obj) {
  396. if (!amdgpu_bo_reserve(adev->wb.wb_obj, false)) {
  397. amdgpu_bo_kunmap(adev->wb.wb_obj);
  398. amdgpu_bo_unpin(adev->wb.wb_obj);
  399. amdgpu_bo_unreserve(adev->wb.wb_obj);
  400. }
  401. amdgpu_bo_unref(&adev->wb.wb_obj);
  402. adev->wb.wb = NULL;
  403. adev->wb.wb_obj = NULL;
  404. }
  405. }
  406. /**
  407. * amdgpu_wb_init- Init Writeback driver info and allocate memory
  408. *
  409. * @adev: amdgpu_device pointer
  410. *
  411. * Disables Writeback and frees the Writeback memory (all asics).
  412. * Used at driver startup.
  413. * Returns 0 on success or an -error on failure.
  414. */
  415. static int amdgpu_wb_init(struct amdgpu_device *adev)
  416. {
  417. int r;
  418. if (adev->wb.wb_obj == NULL) {
  419. r = amdgpu_bo_create(adev, AMDGPU_MAX_WB * 4, PAGE_SIZE, true,
  420. AMDGPU_GEM_DOMAIN_GTT, 0, NULL, NULL,
  421. &adev->wb.wb_obj);
  422. if (r) {
  423. dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
  424. return r;
  425. }
  426. r = amdgpu_bo_reserve(adev->wb.wb_obj, false);
  427. if (unlikely(r != 0)) {
  428. amdgpu_wb_fini(adev);
  429. return r;
  430. }
  431. r = amdgpu_bo_pin(adev->wb.wb_obj, AMDGPU_GEM_DOMAIN_GTT,
  432. &adev->wb.gpu_addr);
  433. if (r) {
  434. amdgpu_bo_unreserve(adev->wb.wb_obj);
  435. dev_warn(adev->dev, "(%d) pin WB bo failed\n", r);
  436. amdgpu_wb_fini(adev);
  437. return r;
  438. }
  439. r = amdgpu_bo_kmap(adev->wb.wb_obj, (void **)&adev->wb.wb);
  440. amdgpu_bo_unreserve(adev->wb.wb_obj);
  441. if (r) {
  442. dev_warn(adev->dev, "(%d) map WB bo failed\n", r);
  443. amdgpu_wb_fini(adev);
  444. return r;
  445. }
  446. adev->wb.num_wb = AMDGPU_MAX_WB;
  447. memset(&adev->wb.used, 0, sizeof(adev->wb.used));
  448. /* clear wb memory */
  449. memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
  450. }
  451. return 0;
  452. }
  453. /**
  454. * amdgpu_wb_get - Allocate a wb entry
  455. *
  456. * @adev: amdgpu_device pointer
  457. * @wb: wb index
  458. *
  459. * Allocate a wb slot for use by the driver (all asics).
  460. * Returns 0 on success or -EINVAL on failure.
  461. */
  462. int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
  463. {
  464. unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
  465. if (offset < adev->wb.num_wb) {
  466. __set_bit(offset, adev->wb.used);
  467. *wb = offset;
  468. return 0;
  469. } else {
  470. return -EINVAL;
  471. }
  472. }
  473. /**
  474. * amdgpu_wb_free - Free a wb entry
  475. *
  476. * @adev: amdgpu_device pointer
  477. * @wb: wb index
  478. *
  479. * Free a wb slot allocated for use by the driver (all asics)
  480. */
  481. void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
  482. {
  483. if (wb < adev->wb.num_wb)
  484. __clear_bit(wb, adev->wb.used);
  485. }
  486. /**
  487. * amdgpu_vram_location - try to find VRAM location
  488. * @adev: amdgpu device structure holding all necessary informations
  489. * @mc: memory controller structure holding memory informations
  490. * @base: base address at which to put VRAM
  491. *
  492. * Function will place try to place VRAM at base address provided
  493. * as parameter (which is so far either PCI aperture address or
  494. * for IGP TOM base address).
  495. *
  496. * If there is not enough space to fit the unvisible VRAM in the 32bits
  497. * address space then we limit the VRAM size to the aperture.
  498. *
  499. * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
  500. * this shouldn't be a problem as we are using the PCI aperture as a reference.
  501. * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
  502. * not IGP.
  503. *
  504. * Note: we use mc_vram_size as on some board we need to program the mc to
  505. * cover the whole aperture even if VRAM size is inferior to aperture size
  506. * Novell bug 204882 + along with lots of ubuntu ones
  507. *
  508. * Note: when limiting vram it's safe to overwritte real_vram_size because
  509. * we are not in case where real_vram_size is inferior to mc_vram_size (ie
  510. * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
  511. * ones)
  512. *
  513. * Note: IGP TOM addr should be the same as the aperture addr, we don't
  514. * explicitly check for that thought.
  515. *
  516. * FIXME: when reducing VRAM size align new size on power of 2.
  517. */
  518. void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
  519. {
  520. uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
  521. mc->vram_start = base;
  522. if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
  523. dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
  524. mc->real_vram_size = mc->aper_size;
  525. mc->mc_vram_size = mc->aper_size;
  526. }
  527. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  528. if (limit && limit < mc->real_vram_size)
  529. mc->real_vram_size = limit;
  530. dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  531. mc->mc_vram_size >> 20, mc->vram_start,
  532. mc->vram_end, mc->real_vram_size >> 20);
  533. }
  534. /**
  535. * amdgpu_gtt_location - try to find GTT location
  536. * @adev: amdgpu device structure holding all necessary informations
  537. * @mc: memory controller structure holding memory informations
  538. *
  539. * Function will place try to place GTT before or after VRAM.
  540. *
  541. * If GTT size is bigger than space left then we ajust GTT size.
  542. * Thus function will never fails.
  543. *
  544. * FIXME: when reducing GTT size align new size on power of 2.
  545. */
  546. void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
  547. {
  548. u64 size_af, size_bf;
  549. size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
  550. size_bf = mc->vram_start & ~mc->gtt_base_align;
  551. if (size_bf > size_af) {
  552. if (mc->gtt_size > size_bf) {
  553. dev_warn(adev->dev, "limiting GTT\n");
  554. mc->gtt_size = size_bf;
  555. }
  556. mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
  557. } else {
  558. if (mc->gtt_size > size_af) {
  559. dev_warn(adev->dev, "limiting GTT\n");
  560. mc->gtt_size = size_af;
  561. }
  562. mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
  563. }
  564. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  565. dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  566. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  567. }
  568. /*
  569. * GPU helpers function.
  570. */
  571. /**
  572. * amdgpu_card_posted - check if the hw has already been initialized
  573. *
  574. * @adev: amdgpu_device pointer
  575. *
  576. * Check if the asic has been initialized (all asics).
  577. * Used at driver startup.
  578. * Returns true if initialized or false if not.
  579. */
  580. bool amdgpu_card_posted(struct amdgpu_device *adev)
  581. {
  582. uint32_t reg;
  583. /* then check MEM_SIZE, in case the crtcs are off */
  584. reg = RREG32(mmCONFIG_MEMSIZE);
  585. if (reg)
  586. return true;
  587. return false;
  588. }
  589. /**
  590. * amdgpu_dummy_page_init - init dummy page used by the driver
  591. *
  592. * @adev: amdgpu_device pointer
  593. *
  594. * Allocate the dummy page used by the driver (all asics).
  595. * This dummy page is used by the driver as a filler for gart entries
  596. * when pages are taken out of the GART
  597. * Returns 0 on sucess, -ENOMEM on failure.
  598. */
  599. int amdgpu_dummy_page_init(struct amdgpu_device *adev)
  600. {
  601. if (adev->dummy_page.page)
  602. return 0;
  603. adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  604. if (adev->dummy_page.page == NULL)
  605. return -ENOMEM;
  606. adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
  607. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  608. if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
  609. dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  610. __free_page(adev->dummy_page.page);
  611. adev->dummy_page.page = NULL;
  612. return -ENOMEM;
  613. }
  614. return 0;
  615. }
  616. /**
  617. * amdgpu_dummy_page_fini - free dummy page used by the driver
  618. *
  619. * @adev: amdgpu_device pointer
  620. *
  621. * Frees the dummy page used by the driver (all asics).
  622. */
  623. void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
  624. {
  625. if (adev->dummy_page.page == NULL)
  626. return;
  627. pci_unmap_page(adev->pdev, adev->dummy_page.addr,
  628. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  629. __free_page(adev->dummy_page.page);
  630. adev->dummy_page.page = NULL;
  631. }
  632. /* ATOM accessor methods */
  633. /*
  634. * ATOM is an interpreted byte code stored in tables in the vbios. The
  635. * driver registers callbacks to access registers and the interpreter
  636. * in the driver parses the tables and executes then to program specific
  637. * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
  638. * atombios.h, and atom.c
  639. */
  640. /**
  641. * cail_pll_read - read PLL register
  642. *
  643. * @info: atom card_info pointer
  644. * @reg: PLL register offset
  645. *
  646. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  647. * Returns the value of the PLL register.
  648. */
  649. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  650. {
  651. return 0;
  652. }
  653. /**
  654. * cail_pll_write - write PLL register
  655. *
  656. * @info: atom card_info pointer
  657. * @reg: PLL register offset
  658. * @val: value to write to the pll register
  659. *
  660. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  661. */
  662. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  663. {
  664. }
  665. /**
  666. * cail_mc_read - read MC (Memory Controller) register
  667. *
  668. * @info: atom card_info pointer
  669. * @reg: MC register offset
  670. *
  671. * Provides an MC register accessor for the atom interpreter (r4xx+).
  672. * Returns the value of the MC register.
  673. */
  674. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  675. {
  676. return 0;
  677. }
  678. /**
  679. * cail_mc_write - write MC (Memory Controller) register
  680. *
  681. * @info: atom card_info pointer
  682. * @reg: MC register offset
  683. * @val: value to write to the pll register
  684. *
  685. * Provides a MC register accessor for the atom interpreter (r4xx+).
  686. */
  687. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  688. {
  689. }
  690. /**
  691. * cail_reg_write - write MMIO register
  692. *
  693. * @info: atom card_info pointer
  694. * @reg: MMIO register offset
  695. * @val: value to write to the pll register
  696. *
  697. * Provides a MMIO register accessor for the atom interpreter (r4xx+).
  698. */
  699. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  700. {
  701. struct amdgpu_device *adev = info->dev->dev_private;
  702. WREG32(reg, val);
  703. }
  704. /**
  705. * cail_reg_read - read MMIO register
  706. *
  707. * @info: atom card_info pointer
  708. * @reg: MMIO register offset
  709. *
  710. * Provides an MMIO register accessor for the atom interpreter (r4xx+).
  711. * Returns the value of the MMIO register.
  712. */
  713. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  714. {
  715. struct amdgpu_device *adev = info->dev->dev_private;
  716. uint32_t r;
  717. r = RREG32(reg);
  718. return r;
  719. }
  720. /**
  721. * cail_ioreg_write - write IO register
  722. *
  723. * @info: atom card_info pointer
  724. * @reg: IO register offset
  725. * @val: value to write to the pll register
  726. *
  727. * Provides a IO register accessor for the atom interpreter (r4xx+).
  728. */
  729. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  730. {
  731. struct amdgpu_device *adev = info->dev->dev_private;
  732. WREG32_IO(reg, val);
  733. }
  734. /**
  735. * cail_ioreg_read - read IO register
  736. *
  737. * @info: atom card_info pointer
  738. * @reg: IO register offset
  739. *
  740. * Provides an IO register accessor for the atom interpreter (r4xx+).
  741. * Returns the value of the IO register.
  742. */
  743. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  744. {
  745. struct amdgpu_device *adev = info->dev->dev_private;
  746. uint32_t r;
  747. r = RREG32_IO(reg);
  748. return r;
  749. }
  750. /**
  751. * amdgpu_atombios_fini - free the driver info and callbacks for atombios
  752. *
  753. * @adev: amdgpu_device pointer
  754. *
  755. * Frees the driver info and register access callbacks for the ATOM
  756. * interpreter (r4xx+).
  757. * Called at driver shutdown.
  758. */
  759. static void amdgpu_atombios_fini(struct amdgpu_device *adev)
  760. {
  761. if (adev->mode_info.atom_context) {
  762. kfree(adev->mode_info.atom_context->scratch);
  763. kfree(adev->mode_info.atom_context->iio);
  764. }
  765. kfree(adev->mode_info.atom_context);
  766. adev->mode_info.atom_context = NULL;
  767. kfree(adev->mode_info.atom_card_info);
  768. adev->mode_info.atom_card_info = NULL;
  769. }
  770. /**
  771. * amdgpu_atombios_init - init the driver info and callbacks for atombios
  772. *
  773. * @adev: amdgpu_device pointer
  774. *
  775. * Initializes the driver info and register access callbacks for the
  776. * ATOM interpreter (r4xx+).
  777. * Returns 0 on sucess, -ENOMEM on failure.
  778. * Called at driver startup.
  779. */
  780. static int amdgpu_atombios_init(struct amdgpu_device *adev)
  781. {
  782. struct card_info *atom_card_info =
  783. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  784. if (!atom_card_info)
  785. return -ENOMEM;
  786. adev->mode_info.atom_card_info = atom_card_info;
  787. atom_card_info->dev = adev->ddev;
  788. atom_card_info->reg_read = cail_reg_read;
  789. atom_card_info->reg_write = cail_reg_write;
  790. /* needed for iio ops */
  791. if (adev->rio_mem) {
  792. atom_card_info->ioreg_read = cail_ioreg_read;
  793. atom_card_info->ioreg_write = cail_ioreg_write;
  794. } else {
  795. DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
  796. atom_card_info->ioreg_read = cail_reg_read;
  797. atom_card_info->ioreg_write = cail_reg_write;
  798. }
  799. atom_card_info->mc_read = cail_mc_read;
  800. atom_card_info->mc_write = cail_mc_write;
  801. atom_card_info->pll_read = cail_pll_read;
  802. atom_card_info->pll_write = cail_pll_write;
  803. adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
  804. if (!adev->mode_info.atom_context) {
  805. amdgpu_atombios_fini(adev);
  806. return -ENOMEM;
  807. }
  808. mutex_init(&adev->mode_info.atom_context->mutex);
  809. amdgpu_atombios_scratch_regs_init(adev);
  810. amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
  811. return 0;
  812. }
  813. /* if we get transitioned to only one device, take VGA back */
  814. /**
  815. * amdgpu_vga_set_decode - enable/disable vga decode
  816. *
  817. * @cookie: amdgpu_device pointer
  818. * @state: enable/disable vga decode
  819. *
  820. * Enable/disable vga decode (all asics).
  821. * Returns VGA resource flags.
  822. */
  823. static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
  824. {
  825. struct amdgpu_device *adev = cookie;
  826. amdgpu_asic_set_vga_state(adev, state);
  827. if (state)
  828. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  829. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  830. else
  831. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  832. }
  833. /**
  834. * amdgpu_check_pot_argument - check that argument is a power of two
  835. *
  836. * @arg: value to check
  837. *
  838. * Validates that a certain argument is a power of two (all asics).
  839. * Returns true if argument is valid.
  840. */
  841. static bool amdgpu_check_pot_argument(int arg)
  842. {
  843. return (arg & (arg - 1)) == 0;
  844. }
  845. /**
  846. * amdgpu_check_arguments - validate module params
  847. *
  848. * @adev: amdgpu_device pointer
  849. *
  850. * Validates certain module parameters and updates
  851. * the associated values used by the driver (all asics).
  852. */
  853. static void amdgpu_check_arguments(struct amdgpu_device *adev)
  854. {
  855. if (amdgpu_sched_jobs < 4) {
  856. dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
  857. amdgpu_sched_jobs);
  858. amdgpu_sched_jobs = 4;
  859. } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
  860. dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
  861. amdgpu_sched_jobs);
  862. amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
  863. }
  864. if (amdgpu_gart_size != -1) {
  865. /* gtt size must be greater or equal to 32M */
  866. if (amdgpu_gart_size < 32) {
  867. dev_warn(adev->dev, "gart size (%d) too small\n",
  868. amdgpu_gart_size);
  869. amdgpu_gart_size = -1;
  870. }
  871. }
  872. if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
  873. dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
  874. amdgpu_vm_size);
  875. amdgpu_vm_size = 8;
  876. }
  877. if (amdgpu_vm_size < 1) {
  878. dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
  879. amdgpu_vm_size);
  880. amdgpu_vm_size = 8;
  881. }
  882. /*
  883. * Max GPUVM size for Cayman, SI and CI are 40 bits.
  884. */
  885. if (amdgpu_vm_size > 1024) {
  886. dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
  887. amdgpu_vm_size);
  888. amdgpu_vm_size = 8;
  889. }
  890. /* defines number of bits in page table versus page directory,
  891. * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
  892. * page table and the remaining bits are in the page directory */
  893. if (amdgpu_vm_block_size == -1) {
  894. /* Total bits covered by PD + PTs */
  895. unsigned bits = ilog2(amdgpu_vm_size) + 18;
  896. /* Make sure the PD is 4K in size up to 8GB address space.
  897. Above that split equal between PD and PTs */
  898. if (amdgpu_vm_size <= 8)
  899. amdgpu_vm_block_size = bits - 9;
  900. else
  901. amdgpu_vm_block_size = (bits + 3) / 2;
  902. } else if (amdgpu_vm_block_size < 9) {
  903. dev_warn(adev->dev, "VM page table size (%d) too small\n",
  904. amdgpu_vm_block_size);
  905. amdgpu_vm_block_size = 9;
  906. }
  907. if (amdgpu_vm_block_size > 24 ||
  908. (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
  909. dev_warn(adev->dev, "VM page table size (%d) too large\n",
  910. amdgpu_vm_block_size);
  911. amdgpu_vm_block_size = 9;
  912. }
  913. }
  914. /**
  915. * amdgpu_switcheroo_set_state - set switcheroo state
  916. *
  917. * @pdev: pci dev pointer
  918. * @state: vga_switcheroo state
  919. *
  920. * Callback for the switcheroo driver. Suspends or resumes the
  921. * the asics before or after it is powered up using ACPI methods.
  922. */
  923. static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  924. {
  925. struct drm_device *dev = pci_get_drvdata(pdev);
  926. if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
  927. return;
  928. if (state == VGA_SWITCHEROO_ON) {
  929. unsigned d3_delay = dev->pdev->d3_delay;
  930. printk(KERN_INFO "amdgpu: switched on\n");
  931. /* don't suspend or resume card normally */
  932. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  933. amdgpu_resume_kms(dev, true, true);
  934. dev->pdev->d3_delay = d3_delay;
  935. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  936. drm_kms_helper_poll_enable(dev);
  937. } else {
  938. printk(KERN_INFO "amdgpu: switched off\n");
  939. drm_kms_helper_poll_disable(dev);
  940. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  941. amdgpu_suspend_kms(dev, true, true);
  942. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  943. }
  944. }
  945. /**
  946. * amdgpu_switcheroo_can_switch - see if switcheroo state can change
  947. *
  948. * @pdev: pci dev pointer
  949. *
  950. * Callback for the switcheroo driver. Check of the switcheroo
  951. * state can be changed.
  952. * Returns true if the state can be changed, false if not.
  953. */
  954. static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
  955. {
  956. struct drm_device *dev = pci_get_drvdata(pdev);
  957. /*
  958. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  959. * locking inversion with the driver load path. And the access here is
  960. * completely racy anyway. So don't bother with locking for now.
  961. */
  962. return dev->open_count == 0;
  963. }
  964. static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
  965. .set_gpu_state = amdgpu_switcheroo_set_state,
  966. .reprobe = NULL,
  967. .can_switch = amdgpu_switcheroo_can_switch,
  968. };
  969. int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
  970. enum amd_ip_block_type block_type,
  971. enum amd_clockgating_state state)
  972. {
  973. int i, r = 0;
  974. for (i = 0; i < adev->num_ip_blocks; i++) {
  975. if (adev->ip_blocks[i].type == block_type) {
  976. r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
  977. state);
  978. if (r)
  979. return r;
  980. }
  981. }
  982. return r;
  983. }
  984. int amdgpu_set_powergating_state(struct amdgpu_device *adev,
  985. enum amd_ip_block_type block_type,
  986. enum amd_powergating_state state)
  987. {
  988. int i, r = 0;
  989. for (i = 0; i < adev->num_ip_blocks; i++) {
  990. if (adev->ip_blocks[i].type == block_type) {
  991. r = adev->ip_blocks[i].funcs->set_powergating_state((void *)adev,
  992. state);
  993. if (r)
  994. return r;
  995. }
  996. }
  997. return r;
  998. }
  999. const struct amdgpu_ip_block_version * amdgpu_get_ip_block(
  1000. struct amdgpu_device *adev,
  1001. enum amd_ip_block_type type)
  1002. {
  1003. int i;
  1004. for (i = 0; i < adev->num_ip_blocks; i++)
  1005. if (adev->ip_blocks[i].type == type)
  1006. return &adev->ip_blocks[i];
  1007. return NULL;
  1008. }
  1009. /**
  1010. * amdgpu_ip_block_version_cmp
  1011. *
  1012. * @adev: amdgpu_device pointer
  1013. * @type: enum amd_ip_block_type
  1014. * @major: major version
  1015. * @minor: minor version
  1016. *
  1017. * return 0 if equal or greater
  1018. * return 1 if smaller or the ip_block doesn't exist
  1019. */
  1020. int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
  1021. enum amd_ip_block_type type,
  1022. u32 major, u32 minor)
  1023. {
  1024. const struct amdgpu_ip_block_version *ip_block;
  1025. ip_block = amdgpu_get_ip_block(adev, type);
  1026. if (ip_block && ((ip_block->major > major) ||
  1027. ((ip_block->major == major) &&
  1028. (ip_block->minor >= minor))))
  1029. return 0;
  1030. return 1;
  1031. }
  1032. static int amdgpu_early_init(struct amdgpu_device *adev)
  1033. {
  1034. int i, r;
  1035. switch (adev->asic_type) {
  1036. case CHIP_TOPAZ:
  1037. case CHIP_TONGA:
  1038. case CHIP_FIJI:
  1039. case CHIP_POLARIS11:
  1040. case CHIP_POLARIS10:
  1041. case CHIP_CARRIZO:
  1042. case CHIP_STONEY:
  1043. if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
  1044. adev->family = AMDGPU_FAMILY_CZ;
  1045. else
  1046. adev->family = AMDGPU_FAMILY_VI;
  1047. r = vi_set_ip_blocks(adev);
  1048. if (r)
  1049. return r;
  1050. break;
  1051. #ifdef CONFIG_DRM_AMDGPU_CIK
  1052. case CHIP_BONAIRE:
  1053. case CHIP_HAWAII:
  1054. case CHIP_KAVERI:
  1055. case CHIP_KABINI:
  1056. case CHIP_MULLINS:
  1057. if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
  1058. adev->family = AMDGPU_FAMILY_CI;
  1059. else
  1060. adev->family = AMDGPU_FAMILY_KV;
  1061. r = cik_set_ip_blocks(adev);
  1062. if (r)
  1063. return r;
  1064. break;
  1065. #endif
  1066. default:
  1067. /* FIXME: not supported yet */
  1068. return -EINVAL;
  1069. }
  1070. adev->ip_block_status = kcalloc(adev->num_ip_blocks,
  1071. sizeof(struct amdgpu_ip_block_status), GFP_KERNEL);
  1072. if (adev->ip_block_status == NULL)
  1073. return -ENOMEM;
  1074. if (adev->ip_blocks == NULL) {
  1075. DRM_ERROR("No IP blocks found!\n");
  1076. return r;
  1077. }
  1078. for (i = 0; i < adev->num_ip_blocks; i++) {
  1079. if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
  1080. DRM_ERROR("disabled ip block: %d\n", i);
  1081. adev->ip_block_status[i].valid = false;
  1082. } else {
  1083. if (adev->ip_blocks[i].funcs->early_init) {
  1084. r = adev->ip_blocks[i].funcs->early_init((void *)adev);
  1085. if (r == -ENOENT) {
  1086. adev->ip_block_status[i].valid = false;
  1087. } else if (r) {
  1088. DRM_ERROR("early_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
  1089. return r;
  1090. } else {
  1091. adev->ip_block_status[i].valid = true;
  1092. }
  1093. } else {
  1094. adev->ip_block_status[i].valid = true;
  1095. }
  1096. }
  1097. }
  1098. adev->cg_flags &= amdgpu_cg_mask;
  1099. adev->pg_flags &= amdgpu_pg_mask;
  1100. return 0;
  1101. }
  1102. static int amdgpu_init(struct amdgpu_device *adev)
  1103. {
  1104. int i, r;
  1105. for (i = 0; i < adev->num_ip_blocks; i++) {
  1106. if (!adev->ip_block_status[i].valid)
  1107. continue;
  1108. r = adev->ip_blocks[i].funcs->sw_init((void *)adev);
  1109. if (r) {
  1110. DRM_ERROR("sw_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
  1111. return r;
  1112. }
  1113. adev->ip_block_status[i].sw = true;
  1114. /* need to do gmc hw init early so we can allocate gpu mem */
  1115. if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
  1116. r = amdgpu_vram_scratch_init(adev);
  1117. if (r) {
  1118. DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
  1119. return r;
  1120. }
  1121. r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
  1122. if (r) {
  1123. DRM_ERROR("hw_init %d failed %d\n", i, r);
  1124. return r;
  1125. }
  1126. r = amdgpu_wb_init(adev);
  1127. if (r) {
  1128. DRM_ERROR("amdgpu_wb_init failed %d\n", r);
  1129. return r;
  1130. }
  1131. adev->ip_block_status[i].hw = true;
  1132. }
  1133. }
  1134. for (i = 0; i < adev->num_ip_blocks; i++) {
  1135. if (!adev->ip_block_status[i].sw)
  1136. continue;
  1137. /* gmc hw init is done early */
  1138. if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC)
  1139. continue;
  1140. r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
  1141. if (r) {
  1142. DRM_ERROR("hw_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
  1143. return r;
  1144. }
  1145. adev->ip_block_status[i].hw = true;
  1146. }
  1147. return 0;
  1148. }
  1149. static int amdgpu_late_init(struct amdgpu_device *adev)
  1150. {
  1151. int i = 0, r;
  1152. for (i = 0; i < adev->num_ip_blocks; i++) {
  1153. if (!adev->ip_block_status[i].valid)
  1154. continue;
  1155. /* enable clockgating to save power */
  1156. r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
  1157. AMD_CG_STATE_GATE);
  1158. if (r) {
  1159. DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
  1160. return r;
  1161. }
  1162. if (adev->ip_blocks[i].funcs->late_init) {
  1163. r = adev->ip_blocks[i].funcs->late_init((void *)adev);
  1164. if (r) {
  1165. DRM_ERROR("late_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
  1166. return r;
  1167. }
  1168. }
  1169. }
  1170. return 0;
  1171. }
  1172. static int amdgpu_fini(struct amdgpu_device *adev)
  1173. {
  1174. int i, r;
  1175. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1176. if (!adev->ip_block_status[i].hw)
  1177. continue;
  1178. if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
  1179. amdgpu_wb_fini(adev);
  1180. amdgpu_vram_scratch_fini(adev);
  1181. }
  1182. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1183. r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
  1184. AMD_CG_STATE_UNGATE);
  1185. if (r) {
  1186. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
  1187. return r;
  1188. }
  1189. r = adev->ip_blocks[i].funcs->hw_fini((void *)adev);
  1190. /* XXX handle errors */
  1191. if (r) {
  1192. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
  1193. }
  1194. adev->ip_block_status[i].hw = false;
  1195. }
  1196. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1197. if (!adev->ip_block_status[i].sw)
  1198. continue;
  1199. r = adev->ip_blocks[i].funcs->sw_fini((void *)adev);
  1200. /* XXX handle errors */
  1201. if (r) {
  1202. DRM_DEBUG("sw_fini of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
  1203. }
  1204. adev->ip_block_status[i].sw = false;
  1205. adev->ip_block_status[i].valid = false;
  1206. }
  1207. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1208. if (adev->ip_blocks[i].funcs->late_fini)
  1209. adev->ip_blocks[i].funcs->late_fini((void *)adev);
  1210. }
  1211. return 0;
  1212. }
  1213. static int amdgpu_suspend(struct amdgpu_device *adev)
  1214. {
  1215. int i, r;
  1216. /* ungate SMC block first */
  1217. r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
  1218. AMD_CG_STATE_UNGATE);
  1219. if (r) {
  1220. DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
  1221. }
  1222. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1223. if (!adev->ip_block_status[i].valid)
  1224. continue;
  1225. /* ungate blocks so that suspend can properly shut them down */
  1226. if (i != AMD_IP_BLOCK_TYPE_SMC) {
  1227. r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
  1228. AMD_CG_STATE_UNGATE);
  1229. if (r) {
  1230. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
  1231. }
  1232. }
  1233. /* XXX handle errors */
  1234. r = adev->ip_blocks[i].funcs->suspend(adev);
  1235. /* XXX handle errors */
  1236. if (r) {
  1237. DRM_ERROR("suspend of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
  1238. }
  1239. }
  1240. return 0;
  1241. }
  1242. static int amdgpu_resume(struct amdgpu_device *adev)
  1243. {
  1244. int i, r;
  1245. for (i = 0; i < adev->num_ip_blocks; i++) {
  1246. if (!adev->ip_block_status[i].valid)
  1247. continue;
  1248. r = adev->ip_blocks[i].funcs->resume(adev);
  1249. if (r) {
  1250. DRM_ERROR("resume of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
  1251. return r;
  1252. }
  1253. }
  1254. return 0;
  1255. }
  1256. static bool amdgpu_device_is_virtual(void)
  1257. {
  1258. #ifdef CONFIG_X86
  1259. return boot_cpu_has(X86_FEATURE_HYPERVISOR);
  1260. #else
  1261. return false;
  1262. #endif
  1263. }
  1264. /**
  1265. * amdgpu_device_init - initialize the driver
  1266. *
  1267. * @adev: amdgpu_device pointer
  1268. * @pdev: drm dev pointer
  1269. * @pdev: pci dev pointer
  1270. * @flags: driver flags
  1271. *
  1272. * Initializes the driver info and hw (all asics).
  1273. * Returns 0 for success or an error on failure.
  1274. * Called at driver startup.
  1275. */
  1276. int amdgpu_device_init(struct amdgpu_device *adev,
  1277. struct drm_device *ddev,
  1278. struct pci_dev *pdev,
  1279. uint32_t flags)
  1280. {
  1281. int r, i;
  1282. bool runtime = false;
  1283. adev->shutdown = false;
  1284. adev->dev = &pdev->dev;
  1285. adev->ddev = ddev;
  1286. adev->pdev = pdev;
  1287. adev->flags = flags;
  1288. adev->asic_type = flags & AMD_ASIC_MASK;
  1289. adev->is_atom_bios = false;
  1290. adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
  1291. adev->mc.gtt_size = 512 * 1024 * 1024;
  1292. adev->accel_working = false;
  1293. adev->num_rings = 0;
  1294. adev->mman.buffer_funcs = NULL;
  1295. adev->mman.buffer_funcs_ring = NULL;
  1296. adev->vm_manager.vm_pte_funcs = NULL;
  1297. adev->vm_manager.vm_pte_num_rings = 0;
  1298. adev->gart.gart_funcs = NULL;
  1299. adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
  1300. adev->smc_rreg = &amdgpu_invalid_rreg;
  1301. adev->smc_wreg = &amdgpu_invalid_wreg;
  1302. adev->pcie_rreg = &amdgpu_invalid_rreg;
  1303. adev->pcie_wreg = &amdgpu_invalid_wreg;
  1304. adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
  1305. adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
  1306. adev->didt_rreg = &amdgpu_invalid_rreg;
  1307. adev->didt_wreg = &amdgpu_invalid_wreg;
  1308. adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
  1309. adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
  1310. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
  1311. amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
  1312. pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
  1313. /* mutex initialization are all done here so we
  1314. * can recall function without having locking issues */
  1315. mutex_init(&adev->vm_manager.lock);
  1316. atomic_set(&adev->irq.ih.lock, 0);
  1317. mutex_init(&adev->pm.mutex);
  1318. mutex_init(&adev->gfx.gpu_clock_mutex);
  1319. mutex_init(&adev->srbm_mutex);
  1320. mutex_init(&adev->grbm_idx_mutex);
  1321. mutex_init(&adev->mn_lock);
  1322. hash_init(adev->mn_hash);
  1323. amdgpu_check_arguments(adev);
  1324. /* Registers mapping */
  1325. /* TODO: block userspace mapping of io register */
  1326. spin_lock_init(&adev->mmio_idx_lock);
  1327. spin_lock_init(&adev->smc_idx_lock);
  1328. spin_lock_init(&adev->pcie_idx_lock);
  1329. spin_lock_init(&adev->uvd_ctx_idx_lock);
  1330. spin_lock_init(&adev->didt_idx_lock);
  1331. spin_lock_init(&adev->audio_endpt_idx_lock);
  1332. adev->rmmio_base = pci_resource_start(adev->pdev, 5);
  1333. adev->rmmio_size = pci_resource_len(adev->pdev, 5);
  1334. adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
  1335. if (adev->rmmio == NULL) {
  1336. return -ENOMEM;
  1337. }
  1338. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
  1339. DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  1340. /* doorbell bar mapping */
  1341. amdgpu_doorbell_init(adev);
  1342. /* io port mapping */
  1343. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1344. if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
  1345. adev->rio_mem_size = pci_resource_len(adev->pdev, i);
  1346. adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
  1347. break;
  1348. }
  1349. }
  1350. if (adev->rio_mem == NULL)
  1351. DRM_ERROR("Unable to find PCI I/O BAR\n");
  1352. /* early init functions */
  1353. r = amdgpu_early_init(adev);
  1354. if (r)
  1355. return r;
  1356. /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
  1357. /* this will fail for cards that aren't VGA class devices, just
  1358. * ignore it */
  1359. vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
  1360. if (amdgpu_runtime_pm == 1)
  1361. runtime = true;
  1362. if (amdgpu_device_is_px(ddev))
  1363. runtime = true;
  1364. vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
  1365. if (runtime)
  1366. vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
  1367. /* Read BIOS */
  1368. if (!amdgpu_get_bios(adev)) {
  1369. r = -EINVAL;
  1370. goto failed;
  1371. }
  1372. /* Must be an ATOMBIOS */
  1373. if (!adev->is_atom_bios) {
  1374. dev_err(adev->dev, "Expecting atombios for GPU\n");
  1375. r = -EINVAL;
  1376. goto failed;
  1377. }
  1378. r = amdgpu_atombios_init(adev);
  1379. if (r) {
  1380. dev_err(adev->dev, "amdgpu_atombios_init failed\n");
  1381. goto failed;
  1382. }
  1383. /* See if the asic supports SR-IOV */
  1384. adev->virtualization.supports_sr_iov =
  1385. amdgpu_atombios_has_gpu_virtualization_table(adev);
  1386. /* Check if we are executing in a virtualized environment */
  1387. adev->virtualization.is_virtual = amdgpu_device_is_virtual();
  1388. adev->virtualization.caps = amdgpu_asic_get_virtual_caps(adev);
  1389. /* Post card if necessary */
  1390. if (!amdgpu_card_posted(adev) ||
  1391. (adev->virtualization.is_virtual &&
  1392. !(adev->virtualization.caps & AMDGPU_VIRT_CAPS_SRIOV_EN))) {
  1393. if (!adev->bios) {
  1394. dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
  1395. r = -EINVAL;
  1396. goto failed;
  1397. }
  1398. DRM_INFO("GPU not posted. posting now...\n");
  1399. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  1400. }
  1401. /* Initialize clocks */
  1402. r = amdgpu_atombios_get_clock_info(adev);
  1403. if (r) {
  1404. dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
  1405. goto failed;
  1406. }
  1407. /* init i2c buses */
  1408. amdgpu_atombios_i2c_init(adev);
  1409. /* Fence driver */
  1410. r = amdgpu_fence_driver_init(adev);
  1411. if (r) {
  1412. dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
  1413. goto failed;
  1414. }
  1415. /* init the mode config */
  1416. drm_mode_config_init(adev->ddev);
  1417. r = amdgpu_init(adev);
  1418. if (r) {
  1419. dev_err(adev->dev, "amdgpu_init failed\n");
  1420. amdgpu_fini(adev);
  1421. goto failed;
  1422. }
  1423. adev->accel_working = true;
  1424. amdgpu_fbdev_init(adev);
  1425. r = amdgpu_ib_pool_init(adev);
  1426. if (r) {
  1427. dev_err(adev->dev, "IB initialization failed (%d).\n", r);
  1428. goto failed;
  1429. }
  1430. r = amdgpu_ib_ring_tests(adev);
  1431. if (r)
  1432. DRM_ERROR("ib ring test failed (%d).\n", r);
  1433. r = amdgpu_gem_debugfs_init(adev);
  1434. if (r) {
  1435. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  1436. }
  1437. r = amdgpu_debugfs_regs_init(adev);
  1438. if (r) {
  1439. DRM_ERROR("registering register debugfs failed (%d).\n", r);
  1440. }
  1441. if ((amdgpu_testing & 1)) {
  1442. if (adev->accel_working)
  1443. amdgpu_test_moves(adev);
  1444. else
  1445. DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
  1446. }
  1447. if ((amdgpu_testing & 2)) {
  1448. if (adev->accel_working)
  1449. amdgpu_test_syncing(adev);
  1450. else
  1451. DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
  1452. }
  1453. if (amdgpu_benchmarking) {
  1454. if (adev->accel_working)
  1455. amdgpu_benchmark(adev, amdgpu_benchmarking);
  1456. else
  1457. DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
  1458. }
  1459. /* enable clockgating, etc. after ib tests, etc. since some blocks require
  1460. * explicit gating rather than handling it automatically.
  1461. */
  1462. r = amdgpu_late_init(adev);
  1463. if (r) {
  1464. dev_err(adev->dev, "amdgpu_late_init failed\n");
  1465. goto failed;
  1466. }
  1467. return 0;
  1468. failed:
  1469. if (runtime)
  1470. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  1471. return r;
  1472. }
  1473. static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
  1474. /**
  1475. * amdgpu_device_fini - tear down the driver
  1476. *
  1477. * @adev: amdgpu_device pointer
  1478. *
  1479. * Tear down the driver info (all asics).
  1480. * Called at driver shutdown.
  1481. */
  1482. void amdgpu_device_fini(struct amdgpu_device *adev)
  1483. {
  1484. int r;
  1485. DRM_INFO("amdgpu: finishing device.\n");
  1486. adev->shutdown = true;
  1487. /* evict vram memory */
  1488. amdgpu_bo_evict_vram(adev);
  1489. amdgpu_ib_pool_fini(adev);
  1490. amdgpu_fence_driver_fini(adev);
  1491. amdgpu_fbdev_fini(adev);
  1492. r = amdgpu_fini(adev);
  1493. kfree(adev->ip_block_status);
  1494. adev->ip_block_status = NULL;
  1495. adev->accel_working = false;
  1496. /* free i2c buses */
  1497. amdgpu_i2c_fini(adev);
  1498. amdgpu_atombios_fini(adev);
  1499. kfree(adev->bios);
  1500. adev->bios = NULL;
  1501. vga_switcheroo_unregister_client(adev->pdev);
  1502. if (adev->flags & AMD_IS_PX)
  1503. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  1504. vga_client_register(adev->pdev, NULL, NULL, NULL);
  1505. if (adev->rio_mem)
  1506. pci_iounmap(adev->pdev, adev->rio_mem);
  1507. adev->rio_mem = NULL;
  1508. iounmap(adev->rmmio);
  1509. adev->rmmio = NULL;
  1510. amdgpu_doorbell_fini(adev);
  1511. amdgpu_debugfs_regs_cleanup(adev);
  1512. amdgpu_debugfs_remove_files(adev);
  1513. }
  1514. /*
  1515. * Suspend & resume.
  1516. */
  1517. /**
  1518. * amdgpu_suspend_kms - initiate device suspend
  1519. *
  1520. * @pdev: drm dev pointer
  1521. * @state: suspend state
  1522. *
  1523. * Puts the hw in the suspend state (all asics).
  1524. * Returns 0 for success or an error on failure.
  1525. * Called at driver suspend.
  1526. */
  1527. int amdgpu_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
  1528. {
  1529. struct amdgpu_device *adev;
  1530. struct drm_crtc *crtc;
  1531. struct drm_connector *connector;
  1532. int r;
  1533. if (dev == NULL || dev->dev_private == NULL) {
  1534. return -ENODEV;
  1535. }
  1536. adev = dev->dev_private;
  1537. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1538. return 0;
  1539. drm_kms_helper_poll_disable(dev);
  1540. /* turn off display hw */
  1541. drm_modeset_lock_all(dev);
  1542. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1543. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  1544. }
  1545. drm_modeset_unlock_all(dev);
  1546. /* unpin the front buffers and cursors */
  1547. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1548. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1549. struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
  1550. struct amdgpu_bo *robj;
  1551. if (amdgpu_crtc->cursor_bo) {
  1552. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  1553. r = amdgpu_bo_reserve(aobj, false);
  1554. if (r == 0) {
  1555. amdgpu_bo_unpin(aobj);
  1556. amdgpu_bo_unreserve(aobj);
  1557. }
  1558. }
  1559. if (rfb == NULL || rfb->obj == NULL) {
  1560. continue;
  1561. }
  1562. robj = gem_to_amdgpu_bo(rfb->obj);
  1563. /* don't unpin kernel fb objects */
  1564. if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
  1565. r = amdgpu_bo_reserve(robj, false);
  1566. if (r == 0) {
  1567. amdgpu_bo_unpin(robj);
  1568. amdgpu_bo_unreserve(robj);
  1569. }
  1570. }
  1571. }
  1572. /* evict vram memory */
  1573. amdgpu_bo_evict_vram(adev);
  1574. amdgpu_fence_driver_suspend(adev);
  1575. r = amdgpu_suspend(adev);
  1576. /* evict remaining vram memory */
  1577. amdgpu_bo_evict_vram(adev);
  1578. pci_save_state(dev->pdev);
  1579. if (suspend) {
  1580. /* Shut down the device */
  1581. pci_disable_device(dev->pdev);
  1582. pci_set_power_state(dev->pdev, PCI_D3hot);
  1583. }
  1584. if (fbcon) {
  1585. console_lock();
  1586. amdgpu_fbdev_set_suspend(adev, 1);
  1587. console_unlock();
  1588. }
  1589. return 0;
  1590. }
  1591. /**
  1592. * amdgpu_resume_kms - initiate device resume
  1593. *
  1594. * @pdev: drm dev pointer
  1595. *
  1596. * Bring the hw back to operating state (all asics).
  1597. * Returns 0 for success or an error on failure.
  1598. * Called at driver resume.
  1599. */
  1600. int amdgpu_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
  1601. {
  1602. struct drm_connector *connector;
  1603. struct amdgpu_device *adev = dev->dev_private;
  1604. struct drm_crtc *crtc;
  1605. int r;
  1606. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1607. return 0;
  1608. if (fbcon) {
  1609. console_lock();
  1610. }
  1611. if (resume) {
  1612. pci_set_power_state(dev->pdev, PCI_D0);
  1613. pci_restore_state(dev->pdev);
  1614. if (pci_enable_device(dev->pdev)) {
  1615. if (fbcon)
  1616. console_unlock();
  1617. return -1;
  1618. }
  1619. }
  1620. /* post card */
  1621. if (!amdgpu_card_posted(adev))
  1622. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  1623. r = amdgpu_resume(adev);
  1624. if (r)
  1625. DRM_ERROR("amdgpu_resume failed (%d).\n", r);
  1626. amdgpu_fence_driver_resume(adev);
  1627. if (resume) {
  1628. r = amdgpu_ib_ring_tests(adev);
  1629. if (r)
  1630. DRM_ERROR("ib ring test failed (%d).\n", r);
  1631. }
  1632. r = amdgpu_late_init(adev);
  1633. if (r)
  1634. return r;
  1635. /* pin cursors */
  1636. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1637. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1638. if (amdgpu_crtc->cursor_bo) {
  1639. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  1640. r = amdgpu_bo_reserve(aobj, false);
  1641. if (r == 0) {
  1642. r = amdgpu_bo_pin(aobj,
  1643. AMDGPU_GEM_DOMAIN_VRAM,
  1644. &amdgpu_crtc->cursor_addr);
  1645. if (r != 0)
  1646. DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
  1647. amdgpu_bo_unreserve(aobj);
  1648. }
  1649. }
  1650. }
  1651. /* blat the mode back in */
  1652. if (fbcon) {
  1653. drm_helper_resume_force_mode(dev);
  1654. /* turn on display hw */
  1655. drm_modeset_lock_all(dev);
  1656. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1657. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  1658. }
  1659. drm_modeset_unlock_all(dev);
  1660. }
  1661. drm_kms_helper_poll_enable(dev);
  1662. drm_helper_hpd_irq_event(dev);
  1663. if (fbcon) {
  1664. amdgpu_fbdev_set_suspend(adev, 0);
  1665. console_unlock();
  1666. }
  1667. return 0;
  1668. }
  1669. /**
  1670. * amdgpu_gpu_reset - reset the asic
  1671. *
  1672. * @adev: amdgpu device pointer
  1673. *
  1674. * Attempt the reset the GPU if it has hung (all asics).
  1675. * Returns 0 for success or an error on failure.
  1676. */
  1677. int amdgpu_gpu_reset(struct amdgpu_device *adev)
  1678. {
  1679. unsigned ring_sizes[AMDGPU_MAX_RINGS];
  1680. uint32_t *ring_data[AMDGPU_MAX_RINGS];
  1681. bool saved = false;
  1682. int i, r;
  1683. int resched;
  1684. atomic_inc(&adev->gpu_reset_counter);
  1685. /* block TTM */
  1686. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  1687. r = amdgpu_suspend(adev);
  1688. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1689. struct amdgpu_ring *ring = adev->rings[i];
  1690. if (!ring)
  1691. continue;
  1692. ring_sizes[i] = amdgpu_ring_backup(ring, &ring_data[i]);
  1693. if (ring_sizes[i]) {
  1694. saved = true;
  1695. dev_info(adev->dev, "Saved %d dwords of commands "
  1696. "on ring %d.\n", ring_sizes[i], i);
  1697. }
  1698. }
  1699. retry:
  1700. r = amdgpu_asic_reset(adev);
  1701. /* post card */
  1702. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  1703. if (!r) {
  1704. dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
  1705. r = amdgpu_resume(adev);
  1706. }
  1707. if (!r) {
  1708. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1709. struct amdgpu_ring *ring = adev->rings[i];
  1710. if (!ring)
  1711. continue;
  1712. amdgpu_ring_restore(ring, ring_sizes[i], ring_data[i]);
  1713. ring_sizes[i] = 0;
  1714. ring_data[i] = NULL;
  1715. }
  1716. r = amdgpu_ib_ring_tests(adev);
  1717. if (r) {
  1718. dev_err(adev->dev, "ib ring test failed (%d).\n", r);
  1719. if (saved) {
  1720. saved = false;
  1721. r = amdgpu_suspend(adev);
  1722. goto retry;
  1723. }
  1724. }
  1725. } else {
  1726. amdgpu_fence_driver_force_completion(adev);
  1727. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1728. if (adev->rings[i])
  1729. kfree(ring_data[i]);
  1730. }
  1731. }
  1732. drm_helper_resume_force_mode(adev->ddev);
  1733. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  1734. if (r) {
  1735. /* bad news, how to tell it to userspace ? */
  1736. dev_info(adev->dev, "GPU reset failed\n");
  1737. }
  1738. return r;
  1739. }
  1740. #define AMDGPU_DEFAULT_PCIE_GEN_MASK 0x30007 /* gen: chipset 1/2, asic 1/2/3 */
  1741. #define AMDGPU_DEFAULT_PCIE_MLW_MASK 0x2f0000 /* 1/2/4/8/16 lanes */
  1742. void amdgpu_get_pcie_info(struct amdgpu_device *adev)
  1743. {
  1744. u32 mask;
  1745. int ret;
  1746. if (amdgpu_pcie_gen_cap)
  1747. adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
  1748. if (amdgpu_pcie_lane_cap)
  1749. adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
  1750. /* covers APUs as well */
  1751. if (pci_is_root_bus(adev->pdev->bus)) {
  1752. if (adev->pm.pcie_gen_mask == 0)
  1753. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  1754. if (adev->pm.pcie_mlw_mask == 0)
  1755. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  1756. return;
  1757. }
  1758. if (adev->pm.pcie_gen_mask == 0) {
  1759. ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
  1760. if (!ret) {
  1761. adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  1762. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  1763. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  1764. if (mask & DRM_PCIE_SPEED_25)
  1765. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
  1766. if (mask & DRM_PCIE_SPEED_50)
  1767. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
  1768. if (mask & DRM_PCIE_SPEED_80)
  1769. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
  1770. } else {
  1771. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  1772. }
  1773. }
  1774. if (adev->pm.pcie_mlw_mask == 0) {
  1775. ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
  1776. if (!ret) {
  1777. switch (mask) {
  1778. case 32:
  1779. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
  1780. CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  1781. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  1782. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  1783. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  1784. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  1785. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  1786. break;
  1787. case 16:
  1788. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  1789. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  1790. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  1791. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  1792. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  1793. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  1794. break;
  1795. case 12:
  1796. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  1797. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  1798. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  1799. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  1800. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  1801. break;
  1802. case 8:
  1803. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  1804. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  1805. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  1806. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  1807. break;
  1808. case 4:
  1809. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  1810. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  1811. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  1812. break;
  1813. case 2:
  1814. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  1815. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  1816. break;
  1817. case 1:
  1818. adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
  1819. break;
  1820. default:
  1821. break;
  1822. }
  1823. } else {
  1824. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  1825. }
  1826. }
  1827. }
  1828. /*
  1829. * Debugfs
  1830. */
  1831. int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
  1832. const struct drm_info_list *files,
  1833. unsigned nfiles)
  1834. {
  1835. unsigned i;
  1836. for (i = 0; i < adev->debugfs_count; i++) {
  1837. if (adev->debugfs[i].files == files) {
  1838. /* Already registered */
  1839. return 0;
  1840. }
  1841. }
  1842. i = adev->debugfs_count + 1;
  1843. if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
  1844. DRM_ERROR("Reached maximum number of debugfs components.\n");
  1845. DRM_ERROR("Report so we increase "
  1846. "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
  1847. return -EINVAL;
  1848. }
  1849. adev->debugfs[adev->debugfs_count].files = files;
  1850. adev->debugfs[adev->debugfs_count].num_files = nfiles;
  1851. adev->debugfs_count = i;
  1852. #if defined(CONFIG_DEBUG_FS)
  1853. drm_debugfs_create_files(files, nfiles,
  1854. adev->ddev->control->debugfs_root,
  1855. adev->ddev->control);
  1856. drm_debugfs_create_files(files, nfiles,
  1857. adev->ddev->primary->debugfs_root,
  1858. adev->ddev->primary);
  1859. #endif
  1860. return 0;
  1861. }
  1862. static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
  1863. {
  1864. #if defined(CONFIG_DEBUG_FS)
  1865. unsigned i;
  1866. for (i = 0; i < adev->debugfs_count; i++) {
  1867. drm_debugfs_remove_files(adev->debugfs[i].files,
  1868. adev->debugfs[i].num_files,
  1869. adev->ddev->control);
  1870. drm_debugfs_remove_files(adev->debugfs[i].files,
  1871. adev->debugfs[i].num_files,
  1872. adev->ddev->primary);
  1873. }
  1874. #endif
  1875. }
  1876. #if defined(CONFIG_DEBUG_FS)
  1877. static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
  1878. size_t size, loff_t *pos)
  1879. {
  1880. struct amdgpu_device *adev = f->f_inode->i_private;
  1881. ssize_t result = 0;
  1882. int r;
  1883. if (size & 0x3 || *pos & 0x3)
  1884. return -EINVAL;
  1885. while (size) {
  1886. uint32_t value;
  1887. if (*pos > adev->rmmio_size)
  1888. return result;
  1889. value = RREG32(*pos >> 2);
  1890. r = put_user(value, (uint32_t *)buf);
  1891. if (r)
  1892. return r;
  1893. result += 4;
  1894. buf += 4;
  1895. *pos += 4;
  1896. size -= 4;
  1897. }
  1898. return result;
  1899. }
  1900. static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
  1901. size_t size, loff_t *pos)
  1902. {
  1903. struct amdgpu_device *adev = f->f_inode->i_private;
  1904. ssize_t result = 0;
  1905. int r;
  1906. if (size & 0x3 || *pos & 0x3)
  1907. return -EINVAL;
  1908. while (size) {
  1909. uint32_t value;
  1910. if (*pos > adev->rmmio_size)
  1911. return result;
  1912. r = get_user(value, (uint32_t *)buf);
  1913. if (r)
  1914. return r;
  1915. WREG32(*pos >> 2, value);
  1916. result += 4;
  1917. buf += 4;
  1918. *pos += 4;
  1919. size -= 4;
  1920. }
  1921. return result;
  1922. }
  1923. static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
  1924. size_t size, loff_t *pos)
  1925. {
  1926. struct amdgpu_device *adev = f->f_inode->i_private;
  1927. ssize_t result = 0;
  1928. int r;
  1929. if (size & 0x3 || *pos & 0x3)
  1930. return -EINVAL;
  1931. while (size) {
  1932. uint32_t value;
  1933. value = RREG32_PCIE(*pos >> 2);
  1934. r = put_user(value, (uint32_t *)buf);
  1935. if (r)
  1936. return r;
  1937. result += 4;
  1938. buf += 4;
  1939. *pos += 4;
  1940. size -= 4;
  1941. }
  1942. return result;
  1943. }
  1944. static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
  1945. size_t size, loff_t *pos)
  1946. {
  1947. struct amdgpu_device *adev = f->f_inode->i_private;
  1948. ssize_t result = 0;
  1949. int r;
  1950. if (size & 0x3 || *pos & 0x3)
  1951. return -EINVAL;
  1952. while (size) {
  1953. uint32_t value;
  1954. r = get_user(value, (uint32_t *)buf);
  1955. if (r)
  1956. return r;
  1957. WREG32_PCIE(*pos >> 2, value);
  1958. result += 4;
  1959. buf += 4;
  1960. *pos += 4;
  1961. size -= 4;
  1962. }
  1963. return result;
  1964. }
  1965. static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
  1966. size_t size, loff_t *pos)
  1967. {
  1968. struct amdgpu_device *adev = f->f_inode->i_private;
  1969. ssize_t result = 0;
  1970. int r;
  1971. if (size & 0x3 || *pos & 0x3)
  1972. return -EINVAL;
  1973. while (size) {
  1974. uint32_t value;
  1975. value = RREG32_DIDT(*pos >> 2);
  1976. r = put_user(value, (uint32_t *)buf);
  1977. if (r)
  1978. return r;
  1979. result += 4;
  1980. buf += 4;
  1981. *pos += 4;
  1982. size -= 4;
  1983. }
  1984. return result;
  1985. }
  1986. static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
  1987. size_t size, loff_t *pos)
  1988. {
  1989. struct amdgpu_device *adev = f->f_inode->i_private;
  1990. ssize_t result = 0;
  1991. int r;
  1992. if (size & 0x3 || *pos & 0x3)
  1993. return -EINVAL;
  1994. while (size) {
  1995. uint32_t value;
  1996. r = get_user(value, (uint32_t *)buf);
  1997. if (r)
  1998. return r;
  1999. WREG32_DIDT(*pos >> 2, value);
  2000. result += 4;
  2001. buf += 4;
  2002. *pos += 4;
  2003. size -= 4;
  2004. }
  2005. return result;
  2006. }
  2007. static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
  2008. size_t size, loff_t *pos)
  2009. {
  2010. struct amdgpu_device *adev = f->f_inode->i_private;
  2011. ssize_t result = 0;
  2012. int r;
  2013. if (size & 0x3 || *pos & 0x3)
  2014. return -EINVAL;
  2015. while (size) {
  2016. uint32_t value;
  2017. value = RREG32_SMC(*pos >> 2);
  2018. r = put_user(value, (uint32_t *)buf);
  2019. if (r)
  2020. return r;
  2021. result += 4;
  2022. buf += 4;
  2023. *pos += 4;
  2024. size -= 4;
  2025. }
  2026. return result;
  2027. }
  2028. static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
  2029. size_t size, loff_t *pos)
  2030. {
  2031. struct amdgpu_device *adev = f->f_inode->i_private;
  2032. ssize_t result = 0;
  2033. int r;
  2034. if (size & 0x3 || *pos & 0x3)
  2035. return -EINVAL;
  2036. while (size) {
  2037. uint32_t value;
  2038. r = get_user(value, (uint32_t *)buf);
  2039. if (r)
  2040. return r;
  2041. WREG32_SMC(*pos >> 2, value);
  2042. result += 4;
  2043. buf += 4;
  2044. *pos += 4;
  2045. size -= 4;
  2046. }
  2047. return result;
  2048. }
  2049. static const struct file_operations amdgpu_debugfs_regs_fops = {
  2050. .owner = THIS_MODULE,
  2051. .read = amdgpu_debugfs_regs_read,
  2052. .write = amdgpu_debugfs_regs_write,
  2053. .llseek = default_llseek
  2054. };
  2055. static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
  2056. .owner = THIS_MODULE,
  2057. .read = amdgpu_debugfs_regs_didt_read,
  2058. .write = amdgpu_debugfs_regs_didt_write,
  2059. .llseek = default_llseek
  2060. };
  2061. static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
  2062. .owner = THIS_MODULE,
  2063. .read = amdgpu_debugfs_regs_pcie_read,
  2064. .write = amdgpu_debugfs_regs_pcie_write,
  2065. .llseek = default_llseek
  2066. };
  2067. static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
  2068. .owner = THIS_MODULE,
  2069. .read = amdgpu_debugfs_regs_smc_read,
  2070. .write = amdgpu_debugfs_regs_smc_write,
  2071. .llseek = default_llseek
  2072. };
  2073. static const struct file_operations *debugfs_regs[] = {
  2074. &amdgpu_debugfs_regs_fops,
  2075. &amdgpu_debugfs_regs_didt_fops,
  2076. &amdgpu_debugfs_regs_pcie_fops,
  2077. &amdgpu_debugfs_regs_smc_fops,
  2078. };
  2079. static const char *debugfs_regs_names[] = {
  2080. "amdgpu_regs",
  2081. "amdgpu_regs_didt",
  2082. "amdgpu_regs_pcie",
  2083. "amdgpu_regs_smc",
  2084. };
  2085. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
  2086. {
  2087. struct drm_minor *minor = adev->ddev->primary;
  2088. struct dentry *ent, *root = minor->debugfs_root;
  2089. unsigned i, j;
  2090. for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
  2091. ent = debugfs_create_file(debugfs_regs_names[i],
  2092. S_IFREG | S_IRUGO, root,
  2093. adev, debugfs_regs[i]);
  2094. if (IS_ERR(ent)) {
  2095. for (j = 0; j < i; j++) {
  2096. debugfs_remove(adev->debugfs_regs[i]);
  2097. adev->debugfs_regs[i] = NULL;
  2098. }
  2099. return PTR_ERR(ent);
  2100. }
  2101. if (!i)
  2102. i_size_write(ent->d_inode, adev->rmmio_size);
  2103. adev->debugfs_regs[i] = ent;
  2104. }
  2105. return 0;
  2106. }
  2107. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
  2108. {
  2109. unsigned i;
  2110. for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
  2111. if (adev->debugfs_regs[i]) {
  2112. debugfs_remove(adev->debugfs_regs[i]);
  2113. adev->debugfs_regs[i] = NULL;
  2114. }
  2115. }
  2116. }
  2117. int amdgpu_debugfs_init(struct drm_minor *minor)
  2118. {
  2119. return 0;
  2120. }
  2121. void amdgpu_debugfs_cleanup(struct drm_minor *minor)
  2122. {
  2123. }
  2124. #else
  2125. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
  2126. {
  2127. return 0;
  2128. }
  2129. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
  2130. #endif