amdgpu_device.c 60 KB

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