amdgpu_device.c 55 KB

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