amdgpu_device.c 88 KB

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