amdgpu_device.c 50 KB

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