amdgpu_device.c 73 KB

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