amdgpu_device.c 55 KB

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