amdgpu_device.c 88 KB

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