amdgpu_device.c 95 KB

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