amdgpu_device.c 68 KB

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