amdgpu_device.c 50 KB

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