amdgpu_device.c 50 KB

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