amdgpu_device.c 51 KB

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