amdgpu_device.c 72 KB

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