amdgpu_device.c 52 KB

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