amdgpu_device.c 76 KB

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