amdgpu_device.c 100 KB

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