amdgpu_device.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504
  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)
  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_get_block_size(struct amdgpu_device *adev)
  954. {
  955. /* from AI, asic starts to support multiple level VMPT */
  956. if (adev->family >= AMDGPU_FAMILY_AI) {
  957. if (amdgpu_vm_block_size != 9)
  958. dev_warn(adev->dev, "Multi-VMPT limits block size to"
  959. "one page!\n");
  960. amdgpu_vm_block_size = 9;
  961. return;
  962. }
  963. /* defines number of bits in page table versus page directory,
  964. * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
  965. * page table and the remaining bits are in the page directory */
  966. if (amdgpu_vm_block_size == -1) {
  967. /* Total bits covered by PD + PTs */
  968. unsigned bits = ilog2(amdgpu_vm_size) + 18;
  969. /* Make sure the PD is 4K in size up to 8GB address space.
  970. Above that split equal between PD and PTs */
  971. if (amdgpu_vm_size <= 8)
  972. amdgpu_vm_block_size = bits - 9;
  973. else
  974. amdgpu_vm_block_size = (bits + 3) / 2;
  975. } else if (amdgpu_vm_block_size < 9) {
  976. dev_warn(adev->dev, "VM page table size (%d) too small\n",
  977. amdgpu_vm_block_size);
  978. amdgpu_vm_block_size = 9;
  979. }
  980. if (amdgpu_vm_block_size > 24 ||
  981. (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
  982. dev_warn(adev->dev, "VM page table size (%d) too large\n",
  983. amdgpu_vm_block_size);
  984. amdgpu_vm_block_size = 9;
  985. }
  986. }
  987. /**
  988. * amdgpu_check_arguments - validate module params
  989. *
  990. * @adev: amdgpu_device pointer
  991. *
  992. * Validates certain module parameters and updates
  993. * the associated values used by the driver (all asics).
  994. */
  995. static void amdgpu_check_arguments(struct amdgpu_device *adev)
  996. {
  997. if (amdgpu_sched_jobs < 4) {
  998. dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
  999. amdgpu_sched_jobs);
  1000. amdgpu_sched_jobs = 4;
  1001. } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
  1002. dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
  1003. amdgpu_sched_jobs);
  1004. amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
  1005. }
  1006. if (amdgpu_gart_size != -1) {
  1007. /* gtt size must be greater or equal to 32M */
  1008. if (amdgpu_gart_size < 32) {
  1009. dev_warn(adev->dev, "gart size (%d) too small\n",
  1010. amdgpu_gart_size);
  1011. amdgpu_gart_size = -1;
  1012. }
  1013. }
  1014. if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
  1015. dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
  1016. amdgpu_vm_size);
  1017. amdgpu_vm_size = 8;
  1018. }
  1019. if (amdgpu_vm_size < 1) {
  1020. dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
  1021. amdgpu_vm_size);
  1022. amdgpu_vm_size = 8;
  1023. }
  1024. /*
  1025. * Max GPUVM size for Cayman, SI and CI are 40 bits.
  1026. */
  1027. if (amdgpu_vm_size > 1024) {
  1028. dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
  1029. amdgpu_vm_size);
  1030. amdgpu_vm_size = 8;
  1031. }
  1032. amdgpu_get_block_size(adev);
  1033. if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
  1034. !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
  1035. dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
  1036. amdgpu_vram_page_split);
  1037. amdgpu_vram_page_split = 1024;
  1038. }
  1039. }
  1040. /**
  1041. * amdgpu_switcheroo_set_state - set switcheroo state
  1042. *
  1043. * @pdev: pci dev pointer
  1044. * @state: vga_switcheroo state
  1045. *
  1046. * Callback for the switcheroo driver. Suspends or resumes the
  1047. * the asics before or after it is powered up using ACPI methods.
  1048. */
  1049. static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  1050. {
  1051. struct drm_device *dev = pci_get_drvdata(pdev);
  1052. if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
  1053. return;
  1054. if (state == VGA_SWITCHEROO_ON) {
  1055. unsigned d3_delay = dev->pdev->d3_delay;
  1056. pr_info("amdgpu: switched on\n");
  1057. /* don't suspend or resume card normally */
  1058. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  1059. amdgpu_device_resume(dev, true, true);
  1060. dev->pdev->d3_delay = d3_delay;
  1061. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  1062. drm_kms_helper_poll_enable(dev);
  1063. } else {
  1064. pr_info("amdgpu: switched off\n");
  1065. drm_kms_helper_poll_disable(dev);
  1066. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  1067. amdgpu_device_suspend(dev, true, true);
  1068. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  1069. }
  1070. }
  1071. /**
  1072. * amdgpu_switcheroo_can_switch - see if switcheroo state can change
  1073. *
  1074. * @pdev: pci dev pointer
  1075. *
  1076. * Callback for the switcheroo driver. Check of the switcheroo
  1077. * state can be changed.
  1078. * Returns true if the state can be changed, false if not.
  1079. */
  1080. static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
  1081. {
  1082. struct drm_device *dev = pci_get_drvdata(pdev);
  1083. /*
  1084. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  1085. * locking inversion with the driver load path. And the access here is
  1086. * completely racy anyway. So don't bother with locking for now.
  1087. */
  1088. return dev->open_count == 0;
  1089. }
  1090. static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
  1091. .set_gpu_state = amdgpu_switcheroo_set_state,
  1092. .reprobe = NULL,
  1093. .can_switch = amdgpu_switcheroo_can_switch,
  1094. };
  1095. int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
  1096. enum amd_ip_block_type block_type,
  1097. enum amd_clockgating_state state)
  1098. {
  1099. int i, r = 0;
  1100. for (i = 0; i < adev->num_ip_blocks; i++) {
  1101. if (!adev->ip_blocks[i].status.valid)
  1102. continue;
  1103. if (adev->ip_blocks[i].version->type != block_type)
  1104. continue;
  1105. if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
  1106. continue;
  1107. r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
  1108. (void *)adev, state);
  1109. if (r)
  1110. DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
  1111. adev->ip_blocks[i].version->funcs->name, r);
  1112. }
  1113. return r;
  1114. }
  1115. int amdgpu_set_powergating_state(struct amdgpu_device *adev,
  1116. enum amd_ip_block_type block_type,
  1117. enum amd_powergating_state state)
  1118. {
  1119. int i, r = 0;
  1120. for (i = 0; i < adev->num_ip_blocks; i++) {
  1121. if (!adev->ip_blocks[i].status.valid)
  1122. continue;
  1123. if (adev->ip_blocks[i].version->type != block_type)
  1124. continue;
  1125. if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
  1126. continue;
  1127. r = adev->ip_blocks[i].version->funcs->set_powergating_state(
  1128. (void *)adev, state);
  1129. if (r)
  1130. DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
  1131. adev->ip_blocks[i].version->funcs->name, r);
  1132. }
  1133. return r;
  1134. }
  1135. void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
  1136. {
  1137. int i;
  1138. for (i = 0; i < adev->num_ip_blocks; i++) {
  1139. if (!adev->ip_blocks[i].status.valid)
  1140. continue;
  1141. if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
  1142. adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
  1143. }
  1144. }
  1145. int amdgpu_wait_for_idle(struct amdgpu_device *adev,
  1146. enum amd_ip_block_type block_type)
  1147. {
  1148. int i, r;
  1149. for (i = 0; i < adev->num_ip_blocks; i++) {
  1150. if (!adev->ip_blocks[i].status.valid)
  1151. continue;
  1152. if (adev->ip_blocks[i].version->type == block_type) {
  1153. r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
  1154. if (r)
  1155. return r;
  1156. break;
  1157. }
  1158. }
  1159. return 0;
  1160. }
  1161. bool amdgpu_is_idle(struct amdgpu_device *adev,
  1162. enum amd_ip_block_type block_type)
  1163. {
  1164. int i;
  1165. for (i = 0; i < adev->num_ip_blocks; i++) {
  1166. if (!adev->ip_blocks[i].status.valid)
  1167. continue;
  1168. if (adev->ip_blocks[i].version->type == block_type)
  1169. return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
  1170. }
  1171. return true;
  1172. }
  1173. struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
  1174. enum amd_ip_block_type type)
  1175. {
  1176. int i;
  1177. for (i = 0; i < adev->num_ip_blocks; i++)
  1178. if (adev->ip_blocks[i].version->type == type)
  1179. return &adev->ip_blocks[i];
  1180. return NULL;
  1181. }
  1182. /**
  1183. * amdgpu_ip_block_version_cmp
  1184. *
  1185. * @adev: amdgpu_device pointer
  1186. * @type: enum amd_ip_block_type
  1187. * @major: major version
  1188. * @minor: minor version
  1189. *
  1190. * return 0 if equal or greater
  1191. * return 1 if smaller or the ip_block doesn't exist
  1192. */
  1193. int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
  1194. enum amd_ip_block_type type,
  1195. u32 major, u32 minor)
  1196. {
  1197. struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
  1198. if (ip_block && ((ip_block->version->major > major) ||
  1199. ((ip_block->version->major == major) &&
  1200. (ip_block->version->minor >= minor))))
  1201. return 0;
  1202. return 1;
  1203. }
  1204. /**
  1205. * amdgpu_ip_block_add
  1206. *
  1207. * @adev: amdgpu_device pointer
  1208. * @ip_block_version: pointer to the IP to add
  1209. *
  1210. * Adds the IP block driver information to the collection of IPs
  1211. * on the asic.
  1212. */
  1213. int amdgpu_ip_block_add(struct amdgpu_device *adev,
  1214. const struct amdgpu_ip_block_version *ip_block_version)
  1215. {
  1216. if (!ip_block_version)
  1217. return -EINVAL;
  1218. adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
  1219. return 0;
  1220. }
  1221. static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
  1222. {
  1223. adev->enable_virtual_display = false;
  1224. if (amdgpu_virtual_display) {
  1225. struct drm_device *ddev = adev->ddev;
  1226. const char *pci_address_name = pci_name(ddev->pdev);
  1227. char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
  1228. pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
  1229. pciaddstr_tmp = pciaddstr;
  1230. while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
  1231. pciaddname = strsep(&pciaddname_tmp, ",");
  1232. if (!strcmp("all", pciaddname)
  1233. || !strcmp(pci_address_name, pciaddname)) {
  1234. long num_crtc;
  1235. int res = -1;
  1236. adev->enable_virtual_display = true;
  1237. if (pciaddname_tmp)
  1238. res = kstrtol(pciaddname_tmp, 10,
  1239. &num_crtc);
  1240. if (!res) {
  1241. if (num_crtc < 1)
  1242. num_crtc = 1;
  1243. if (num_crtc > 6)
  1244. num_crtc = 6;
  1245. adev->mode_info.num_crtc = num_crtc;
  1246. } else {
  1247. adev->mode_info.num_crtc = 1;
  1248. }
  1249. break;
  1250. }
  1251. }
  1252. DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
  1253. amdgpu_virtual_display, pci_address_name,
  1254. adev->enable_virtual_display, adev->mode_info.num_crtc);
  1255. kfree(pciaddstr);
  1256. }
  1257. }
  1258. static int amdgpu_early_init(struct amdgpu_device *adev)
  1259. {
  1260. int i, r;
  1261. amdgpu_device_enable_virtual_display(adev);
  1262. switch (adev->asic_type) {
  1263. case CHIP_TOPAZ:
  1264. case CHIP_TONGA:
  1265. case CHIP_FIJI:
  1266. case CHIP_POLARIS11:
  1267. case CHIP_POLARIS10:
  1268. case CHIP_POLARIS12:
  1269. case CHIP_CARRIZO:
  1270. case CHIP_STONEY:
  1271. if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
  1272. adev->family = AMDGPU_FAMILY_CZ;
  1273. else
  1274. adev->family = AMDGPU_FAMILY_VI;
  1275. r = vi_set_ip_blocks(adev);
  1276. if (r)
  1277. return r;
  1278. break;
  1279. #ifdef CONFIG_DRM_AMDGPU_SI
  1280. case CHIP_VERDE:
  1281. case CHIP_TAHITI:
  1282. case CHIP_PITCAIRN:
  1283. case CHIP_OLAND:
  1284. case CHIP_HAINAN:
  1285. adev->family = AMDGPU_FAMILY_SI;
  1286. r = si_set_ip_blocks(adev);
  1287. if (r)
  1288. return r;
  1289. break;
  1290. #endif
  1291. #ifdef CONFIG_DRM_AMDGPU_CIK
  1292. case CHIP_BONAIRE:
  1293. case CHIP_HAWAII:
  1294. case CHIP_KAVERI:
  1295. case CHIP_KABINI:
  1296. case CHIP_MULLINS:
  1297. if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
  1298. adev->family = AMDGPU_FAMILY_CI;
  1299. else
  1300. adev->family = AMDGPU_FAMILY_KV;
  1301. r = cik_set_ip_blocks(adev);
  1302. if (r)
  1303. return r;
  1304. break;
  1305. #endif
  1306. case CHIP_VEGA10:
  1307. adev->family = AMDGPU_FAMILY_AI;
  1308. r = soc15_set_ip_blocks(adev);
  1309. if (r)
  1310. return r;
  1311. break;
  1312. default:
  1313. /* FIXME: not supported yet */
  1314. return -EINVAL;
  1315. }
  1316. if (amdgpu_sriov_vf(adev)) {
  1317. r = amdgpu_virt_request_full_gpu(adev, true);
  1318. if (r)
  1319. return r;
  1320. }
  1321. for (i = 0; i < adev->num_ip_blocks; i++) {
  1322. if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
  1323. DRM_ERROR("disabled ip block: %d\n", i);
  1324. adev->ip_blocks[i].status.valid = false;
  1325. } else {
  1326. if (adev->ip_blocks[i].version->funcs->early_init) {
  1327. r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
  1328. if (r == -ENOENT) {
  1329. adev->ip_blocks[i].status.valid = false;
  1330. } else if (r) {
  1331. DRM_ERROR("early_init of IP block <%s> failed %d\n",
  1332. adev->ip_blocks[i].version->funcs->name, r);
  1333. return r;
  1334. } else {
  1335. adev->ip_blocks[i].status.valid = true;
  1336. }
  1337. } else {
  1338. adev->ip_blocks[i].status.valid = true;
  1339. }
  1340. }
  1341. }
  1342. adev->cg_flags &= amdgpu_cg_mask;
  1343. adev->pg_flags &= amdgpu_pg_mask;
  1344. return 0;
  1345. }
  1346. static int amdgpu_init(struct amdgpu_device *adev)
  1347. {
  1348. int i, r;
  1349. for (i = 0; i < adev->num_ip_blocks; i++) {
  1350. if (!adev->ip_blocks[i].status.valid)
  1351. continue;
  1352. r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
  1353. if (r) {
  1354. DRM_ERROR("sw_init of IP block <%s> failed %d\n",
  1355. adev->ip_blocks[i].version->funcs->name, r);
  1356. return r;
  1357. }
  1358. adev->ip_blocks[i].status.sw = true;
  1359. /* need to do gmc hw init early so we can allocate gpu mem */
  1360. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1361. r = amdgpu_vram_scratch_init(adev);
  1362. if (r) {
  1363. DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
  1364. return r;
  1365. }
  1366. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1367. if (r) {
  1368. DRM_ERROR("hw_init %d failed %d\n", i, r);
  1369. return r;
  1370. }
  1371. r = amdgpu_wb_init(adev);
  1372. if (r) {
  1373. DRM_ERROR("amdgpu_wb_init failed %d\n", r);
  1374. return r;
  1375. }
  1376. adev->ip_blocks[i].status.hw = true;
  1377. /* right after GMC hw init, we create CSA */
  1378. if (amdgpu_sriov_vf(adev)) {
  1379. r = amdgpu_allocate_static_csa(adev);
  1380. if (r) {
  1381. DRM_ERROR("allocate CSA failed %d\n", r);
  1382. return r;
  1383. }
  1384. }
  1385. }
  1386. }
  1387. for (i = 0; i < adev->num_ip_blocks; i++) {
  1388. if (!adev->ip_blocks[i].status.sw)
  1389. continue;
  1390. /* gmc hw init is done early */
  1391. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
  1392. continue;
  1393. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1394. if (r) {
  1395. DRM_ERROR("hw_init of IP block <%s> failed %d\n",
  1396. adev->ip_blocks[i].version->funcs->name, r);
  1397. return r;
  1398. }
  1399. adev->ip_blocks[i].status.hw = true;
  1400. }
  1401. return 0;
  1402. }
  1403. static int amdgpu_late_init(struct amdgpu_device *adev)
  1404. {
  1405. int i = 0, r;
  1406. for (i = 0; i < adev->num_ip_blocks; i++) {
  1407. if (!adev->ip_blocks[i].status.valid)
  1408. continue;
  1409. if (adev->ip_blocks[i].version->funcs->late_init) {
  1410. r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
  1411. if (r) {
  1412. DRM_ERROR("late_init of IP block <%s> failed %d\n",
  1413. adev->ip_blocks[i].version->funcs->name, r);
  1414. return r;
  1415. }
  1416. adev->ip_blocks[i].status.late_initialized = true;
  1417. }
  1418. /* skip CG for VCE/UVD, it's handled specially */
  1419. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1420. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
  1421. /* enable clockgating to save power */
  1422. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1423. AMD_CG_STATE_GATE);
  1424. if (r) {
  1425. DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
  1426. adev->ip_blocks[i].version->funcs->name, r);
  1427. return r;
  1428. }
  1429. }
  1430. }
  1431. amdgpu_dpm_enable_uvd(adev, false);
  1432. amdgpu_dpm_enable_vce(adev, false);
  1433. return 0;
  1434. }
  1435. static int amdgpu_fini(struct amdgpu_device *adev)
  1436. {
  1437. int i, r;
  1438. /* need to disable SMC first */
  1439. for (i = 0; i < adev->num_ip_blocks; i++) {
  1440. if (!adev->ip_blocks[i].status.hw)
  1441. continue;
  1442. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
  1443. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1444. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1445. AMD_CG_STATE_UNGATE);
  1446. if (r) {
  1447. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1448. adev->ip_blocks[i].version->funcs->name, r);
  1449. return r;
  1450. }
  1451. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1452. /* XXX handle errors */
  1453. if (r) {
  1454. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1455. adev->ip_blocks[i].version->funcs->name, r);
  1456. }
  1457. adev->ip_blocks[i].status.hw = false;
  1458. break;
  1459. }
  1460. }
  1461. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1462. if (!adev->ip_blocks[i].status.hw)
  1463. continue;
  1464. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1465. amdgpu_wb_fini(adev);
  1466. amdgpu_vram_scratch_fini(adev);
  1467. }
  1468. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1469. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
  1470. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1471. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1472. AMD_CG_STATE_UNGATE);
  1473. if (r) {
  1474. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1475. adev->ip_blocks[i].version->funcs->name, r);
  1476. return r;
  1477. }
  1478. }
  1479. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1480. /* XXX handle errors */
  1481. if (r) {
  1482. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1483. adev->ip_blocks[i].version->funcs->name, r);
  1484. }
  1485. adev->ip_blocks[i].status.hw = false;
  1486. }
  1487. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1488. if (!adev->ip_blocks[i].status.sw)
  1489. continue;
  1490. r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
  1491. /* XXX handle errors */
  1492. if (r) {
  1493. DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
  1494. adev->ip_blocks[i].version->funcs->name, r);
  1495. }
  1496. adev->ip_blocks[i].status.sw = false;
  1497. adev->ip_blocks[i].status.valid = false;
  1498. }
  1499. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1500. if (!adev->ip_blocks[i].status.late_initialized)
  1501. continue;
  1502. if (adev->ip_blocks[i].version->funcs->late_fini)
  1503. adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
  1504. adev->ip_blocks[i].status.late_initialized = false;
  1505. }
  1506. if (amdgpu_sriov_vf(adev)) {
  1507. amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
  1508. amdgpu_virt_release_full_gpu(adev, false);
  1509. }
  1510. return 0;
  1511. }
  1512. int amdgpu_suspend(struct amdgpu_device *adev)
  1513. {
  1514. int i, r;
  1515. if (amdgpu_sriov_vf(adev))
  1516. amdgpu_virt_request_full_gpu(adev, false);
  1517. /* ungate SMC block first */
  1518. r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
  1519. AMD_CG_STATE_UNGATE);
  1520. if (r) {
  1521. DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
  1522. }
  1523. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1524. if (!adev->ip_blocks[i].status.valid)
  1525. continue;
  1526. /* ungate blocks so that suspend can properly shut them down */
  1527. if (i != AMD_IP_BLOCK_TYPE_SMC) {
  1528. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1529. AMD_CG_STATE_UNGATE);
  1530. if (r) {
  1531. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1532. adev->ip_blocks[i].version->funcs->name, r);
  1533. }
  1534. }
  1535. /* XXX handle errors */
  1536. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1537. /* XXX handle errors */
  1538. if (r) {
  1539. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1540. adev->ip_blocks[i].version->funcs->name, r);
  1541. }
  1542. }
  1543. if (amdgpu_sriov_vf(adev))
  1544. amdgpu_virt_release_full_gpu(adev, false);
  1545. return 0;
  1546. }
  1547. static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
  1548. {
  1549. int i, r;
  1550. for (i = 0; i < adev->num_ip_blocks; i++) {
  1551. if (!adev->ip_blocks[i].status.valid)
  1552. continue;
  1553. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1554. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1555. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
  1556. r = adev->ip_blocks[i].version->funcs->hw_init(adev);
  1557. if (r) {
  1558. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1559. adev->ip_blocks[i].version->funcs->name, r);
  1560. return r;
  1561. }
  1562. }
  1563. return 0;
  1564. }
  1565. static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
  1566. {
  1567. int i, r;
  1568. for (i = 0; i < adev->num_ip_blocks; i++) {
  1569. if (!adev->ip_blocks[i].status.valid)
  1570. continue;
  1571. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1572. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1573. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
  1574. continue;
  1575. r = adev->ip_blocks[i].version->funcs->hw_init(adev);
  1576. if (r) {
  1577. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1578. adev->ip_blocks[i].version->funcs->name, r);
  1579. return r;
  1580. }
  1581. }
  1582. return 0;
  1583. }
  1584. static int amdgpu_resume(struct amdgpu_device *adev)
  1585. {
  1586. int i, r;
  1587. for (i = 0; i < adev->num_ip_blocks; i++) {
  1588. if (!adev->ip_blocks[i].status.valid)
  1589. continue;
  1590. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1591. if (r) {
  1592. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1593. adev->ip_blocks[i].version->funcs->name, r);
  1594. return r;
  1595. }
  1596. }
  1597. return 0;
  1598. }
  1599. static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
  1600. {
  1601. if (adev->is_atom_fw) {
  1602. if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
  1603. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1604. } else {
  1605. if (amdgpu_atombios_has_gpu_virtualization_table(adev))
  1606. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1607. }
  1608. }
  1609. /**
  1610. * amdgpu_device_init - initialize the driver
  1611. *
  1612. * @adev: amdgpu_device pointer
  1613. * @pdev: drm dev pointer
  1614. * @pdev: pci dev pointer
  1615. * @flags: driver flags
  1616. *
  1617. * Initializes the driver info and hw (all asics).
  1618. * Returns 0 for success or an error on failure.
  1619. * Called at driver startup.
  1620. */
  1621. int amdgpu_device_init(struct amdgpu_device *adev,
  1622. struct drm_device *ddev,
  1623. struct pci_dev *pdev,
  1624. uint32_t flags)
  1625. {
  1626. int r, i;
  1627. bool runtime = false;
  1628. u32 max_MBps;
  1629. adev->shutdown = false;
  1630. adev->dev = &pdev->dev;
  1631. adev->ddev = ddev;
  1632. adev->pdev = pdev;
  1633. adev->flags = flags;
  1634. adev->asic_type = flags & AMD_ASIC_MASK;
  1635. adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
  1636. adev->mc.gtt_size = 512 * 1024 * 1024;
  1637. adev->accel_working = false;
  1638. adev->num_rings = 0;
  1639. adev->mman.buffer_funcs = NULL;
  1640. adev->mman.buffer_funcs_ring = NULL;
  1641. adev->vm_manager.vm_pte_funcs = NULL;
  1642. adev->vm_manager.vm_pte_num_rings = 0;
  1643. adev->gart.gart_funcs = NULL;
  1644. adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  1645. adev->smc_rreg = &amdgpu_invalid_rreg;
  1646. adev->smc_wreg = &amdgpu_invalid_wreg;
  1647. adev->pcie_rreg = &amdgpu_invalid_rreg;
  1648. adev->pcie_wreg = &amdgpu_invalid_wreg;
  1649. adev->pciep_rreg = &amdgpu_invalid_rreg;
  1650. adev->pciep_wreg = &amdgpu_invalid_wreg;
  1651. adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
  1652. adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
  1653. adev->didt_rreg = &amdgpu_invalid_rreg;
  1654. adev->didt_wreg = &amdgpu_invalid_wreg;
  1655. adev->gc_cac_rreg = &amdgpu_invalid_rreg;
  1656. adev->gc_cac_wreg = &amdgpu_invalid_wreg;
  1657. adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
  1658. adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
  1659. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
  1660. amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
  1661. pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
  1662. /* mutex initialization are all done here so we
  1663. * can recall function without having locking issues */
  1664. mutex_init(&adev->vm_manager.lock);
  1665. atomic_set(&adev->irq.ih.lock, 0);
  1666. mutex_init(&adev->firmware.mutex);
  1667. mutex_init(&adev->pm.mutex);
  1668. mutex_init(&adev->gfx.gpu_clock_mutex);
  1669. mutex_init(&adev->srbm_mutex);
  1670. mutex_init(&adev->grbm_idx_mutex);
  1671. mutex_init(&adev->mn_lock);
  1672. hash_init(adev->mn_hash);
  1673. amdgpu_check_arguments(adev);
  1674. /* Registers mapping */
  1675. /* TODO: block userspace mapping of io register */
  1676. spin_lock_init(&adev->mmio_idx_lock);
  1677. spin_lock_init(&adev->smc_idx_lock);
  1678. spin_lock_init(&adev->pcie_idx_lock);
  1679. spin_lock_init(&adev->uvd_ctx_idx_lock);
  1680. spin_lock_init(&adev->didt_idx_lock);
  1681. spin_lock_init(&adev->gc_cac_idx_lock);
  1682. spin_lock_init(&adev->audio_endpt_idx_lock);
  1683. spin_lock_init(&adev->mm_stats.lock);
  1684. INIT_LIST_HEAD(&adev->shadow_list);
  1685. mutex_init(&adev->shadow_list_lock);
  1686. INIT_LIST_HEAD(&adev->gtt_list);
  1687. spin_lock_init(&adev->gtt_list_lock);
  1688. if (adev->asic_type >= CHIP_BONAIRE) {
  1689. adev->rmmio_base = pci_resource_start(adev->pdev, 5);
  1690. adev->rmmio_size = pci_resource_len(adev->pdev, 5);
  1691. } else {
  1692. adev->rmmio_base = pci_resource_start(adev->pdev, 2);
  1693. adev->rmmio_size = pci_resource_len(adev->pdev, 2);
  1694. }
  1695. adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
  1696. if (adev->rmmio == NULL) {
  1697. return -ENOMEM;
  1698. }
  1699. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
  1700. DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  1701. if (adev->asic_type >= CHIP_BONAIRE)
  1702. /* doorbell bar mapping */
  1703. amdgpu_doorbell_init(adev);
  1704. /* io port mapping */
  1705. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1706. if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
  1707. adev->rio_mem_size = pci_resource_len(adev->pdev, i);
  1708. adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
  1709. break;
  1710. }
  1711. }
  1712. if (adev->rio_mem == NULL)
  1713. DRM_INFO("PCI I/O BAR is not found.\n");
  1714. /* early init functions */
  1715. r = amdgpu_early_init(adev);
  1716. if (r)
  1717. return r;
  1718. /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
  1719. /* this will fail for cards that aren't VGA class devices, just
  1720. * ignore it */
  1721. vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
  1722. if (amdgpu_runtime_pm == 1)
  1723. runtime = true;
  1724. if (amdgpu_device_is_px(ddev))
  1725. runtime = true;
  1726. vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
  1727. if (runtime)
  1728. vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
  1729. /* Read BIOS */
  1730. if (!amdgpu_get_bios(adev)) {
  1731. r = -EINVAL;
  1732. goto failed;
  1733. }
  1734. r = amdgpu_atombios_init(adev);
  1735. if (r) {
  1736. dev_err(adev->dev, "amdgpu_atombios_init failed\n");
  1737. goto failed;
  1738. }
  1739. /* detect if we are with an SRIOV vbios */
  1740. amdgpu_device_detect_sriov_bios(adev);
  1741. /* Post card if necessary */
  1742. if (amdgpu_vpost_needed(adev)) {
  1743. if (!adev->bios) {
  1744. dev_err(adev->dev, "no vBIOS found\n");
  1745. r = -EINVAL;
  1746. goto failed;
  1747. }
  1748. DRM_INFO("GPU posting now...\n");
  1749. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  1750. if (r) {
  1751. dev_err(adev->dev, "gpu post error!\n");
  1752. goto failed;
  1753. }
  1754. } else {
  1755. DRM_INFO("GPU post is not needed\n");
  1756. }
  1757. if (!adev->is_atom_fw) {
  1758. /* Initialize clocks */
  1759. r = amdgpu_atombios_get_clock_info(adev);
  1760. if (r) {
  1761. dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
  1762. return r;
  1763. }
  1764. /* init i2c buses */
  1765. amdgpu_atombios_i2c_init(adev);
  1766. }
  1767. /* Fence driver */
  1768. r = amdgpu_fence_driver_init(adev);
  1769. if (r) {
  1770. dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
  1771. goto failed;
  1772. }
  1773. /* init the mode config */
  1774. drm_mode_config_init(adev->ddev);
  1775. r = amdgpu_init(adev);
  1776. if (r) {
  1777. dev_err(adev->dev, "amdgpu_init failed\n");
  1778. amdgpu_fini(adev);
  1779. goto failed;
  1780. }
  1781. adev->accel_working = true;
  1782. /* Initialize the buffer migration limit. */
  1783. if (amdgpu_moverate >= 0)
  1784. max_MBps = amdgpu_moverate;
  1785. else
  1786. max_MBps = 8; /* Allow 8 MB/s. */
  1787. /* Get a log2 for easy divisions. */
  1788. adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
  1789. r = amdgpu_ib_pool_init(adev);
  1790. if (r) {
  1791. dev_err(adev->dev, "IB initialization failed (%d).\n", r);
  1792. goto failed;
  1793. }
  1794. r = amdgpu_ib_ring_tests(adev);
  1795. if (r)
  1796. DRM_ERROR("ib ring test failed (%d).\n", r);
  1797. amdgpu_fbdev_init(adev);
  1798. r = amdgpu_gem_debugfs_init(adev);
  1799. if (r)
  1800. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  1801. r = amdgpu_debugfs_regs_init(adev);
  1802. if (r)
  1803. DRM_ERROR("registering register debugfs failed (%d).\n", r);
  1804. r = amdgpu_debugfs_firmware_init(adev);
  1805. if (r)
  1806. DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
  1807. if ((amdgpu_testing & 1)) {
  1808. if (adev->accel_working)
  1809. amdgpu_test_moves(adev);
  1810. else
  1811. DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
  1812. }
  1813. if (amdgpu_benchmarking) {
  1814. if (adev->accel_working)
  1815. amdgpu_benchmark(adev, amdgpu_benchmarking);
  1816. else
  1817. DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
  1818. }
  1819. /* enable clockgating, etc. after ib tests, etc. since some blocks require
  1820. * explicit gating rather than handling it automatically.
  1821. */
  1822. r = amdgpu_late_init(adev);
  1823. if (r) {
  1824. dev_err(adev->dev, "amdgpu_late_init failed\n");
  1825. goto failed;
  1826. }
  1827. return 0;
  1828. failed:
  1829. if (runtime)
  1830. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  1831. return r;
  1832. }
  1833. /**
  1834. * amdgpu_device_fini - tear down the driver
  1835. *
  1836. * @adev: amdgpu_device pointer
  1837. *
  1838. * Tear down the driver info (all asics).
  1839. * Called at driver shutdown.
  1840. */
  1841. void amdgpu_device_fini(struct amdgpu_device *adev)
  1842. {
  1843. int r;
  1844. DRM_INFO("amdgpu: finishing device.\n");
  1845. adev->shutdown = true;
  1846. drm_crtc_force_disable_all(adev->ddev);
  1847. /* evict vram memory */
  1848. amdgpu_bo_evict_vram(adev);
  1849. amdgpu_ib_pool_fini(adev);
  1850. amdgpu_fence_driver_fini(adev);
  1851. amdgpu_fbdev_fini(adev);
  1852. r = amdgpu_fini(adev);
  1853. adev->accel_working = false;
  1854. /* free i2c buses */
  1855. amdgpu_i2c_fini(adev);
  1856. amdgpu_atombios_fini(adev);
  1857. kfree(adev->bios);
  1858. adev->bios = NULL;
  1859. vga_switcheroo_unregister_client(adev->pdev);
  1860. if (adev->flags & AMD_IS_PX)
  1861. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  1862. vga_client_register(adev->pdev, NULL, NULL, NULL);
  1863. if (adev->rio_mem)
  1864. pci_iounmap(adev->pdev, adev->rio_mem);
  1865. adev->rio_mem = NULL;
  1866. iounmap(adev->rmmio);
  1867. adev->rmmio = NULL;
  1868. if (adev->asic_type >= CHIP_BONAIRE)
  1869. amdgpu_doorbell_fini(adev);
  1870. amdgpu_debugfs_regs_cleanup(adev);
  1871. }
  1872. /*
  1873. * Suspend & resume.
  1874. */
  1875. /**
  1876. * amdgpu_device_suspend - initiate device suspend
  1877. *
  1878. * @pdev: drm dev pointer
  1879. * @state: suspend state
  1880. *
  1881. * Puts the hw in the suspend state (all asics).
  1882. * Returns 0 for success or an error on failure.
  1883. * Called at driver suspend.
  1884. */
  1885. int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
  1886. {
  1887. struct amdgpu_device *adev;
  1888. struct drm_crtc *crtc;
  1889. struct drm_connector *connector;
  1890. int r;
  1891. if (dev == NULL || dev->dev_private == NULL) {
  1892. return -ENODEV;
  1893. }
  1894. adev = dev->dev_private;
  1895. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1896. return 0;
  1897. drm_kms_helper_poll_disable(dev);
  1898. /* turn off display hw */
  1899. drm_modeset_lock_all(dev);
  1900. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1901. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  1902. }
  1903. drm_modeset_unlock_all(dev);
  1904. /* unpin the front buffers and cursors */
  1905. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1906. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1907. struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
  1908. struct amdgpu_bo *robj;
  1909. if (amdgpu_crtc->cursor_bo) {
  1910. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  1911. r = amdgpu_bo_reserve(aobj, false);
  1912. if (r == 0) {
  1913. amdgpu_bo_unpin(aobj);
  1914. amdgpu_bo_unreserve(aobj);
  1915. }
  1916. }
  1917. if (rfb == NULL || rfb->obj == NULL) {
  1918. continue;
  1919. }
  1920. robj = gem_to_amdgpu_bo(rfb->obj);
  1921. /* don't unpin kernel fb objects */
  1922. if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
  1923. r = amdgpu_bo_reserve(robj, false);
  1924. if (r == 0) {
  1925. amdgpu_bo_unpin(robj);
  1926. amdgpu_bo_unreserve(robj);
  1927. }
  1928. }
  1929. }
  1930. /* evict vram memory */
  1931. amdgpu_bo_evict_vram(adev);
  1932. amdgpu_fence_driver_suspend(adev);
  1933. r = amdgpu_suspend(adev);
  1934. /* evict remaining vram memory
  1935. * This second call to evict vram is to evict the gart page table
  1936. * using the CPU.
  1937. */
  1938. amdgpu_bo_evict_vram(adev);
  1939. if (adev->is_atom_fw)
  1940. amdgpu_atomfirmware_scratch_regs_save(adev);
  1941. else
  1942. amdgpu_atombios_scratch_regs_save(adev);
  1943. pci_save_state(dev->pdev);
  1944. if (suspend) {
  1945. /* Shut down the device */
  1946. pci_disable_device(dev->pdev);
  1947. pci_set_power_state(dev->pdev, PCI_D3hot);
  1948. } else {
  1949. r = amdgpu_asic_reset(adev);
  1950. if (r)
  1951. DRM_ERROR("amdgpu asic reset failed\n");
  1952. }
  1953. if (fbcon) {
  1954. console_lock();
  1955. amdgpu_fbdev_set_suspend(adev, 1);
  1956. console_unlock();
  1957. }
  1958. return 0;
  1959. }
  1960. /**
  1961. * amdgpu_device_resume - initiate device resume
  1962. *
  1963. * @pdev: drm dev pointer
  1964. *
  1965. * Bring the hw back to operating state (all asics).
  1966. * Returns 0 for success or an error on failure.
  1967. * Called at driver resume.
  1968. */
  1969. int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
  1970. {
  1971. struct drm_connector *connector;
  1972. struct amdgpu_device *adev = dev->dev_private;
  1973. struct drm_crtc *crtc;
  1974. int r;
  1975. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1976. return 0;
  1977. if (fbcon)
  1978. console_lock();
  1979. if (resume) {
  1980. pci_set_power_state(dev->pdev, PCI_D0);
  1981. pci_restore_state(dev->pdev);
  1982. r = pci_enable_device(dev->pdev);
  1983. if (r) {
  1984. if (fbcon)
  1985. console_unlock();
  1986. return r;
  1987. }
  1988. }
  1989. if (adev->is_atom_fw)
  1990. amdgpu_atomfirmware_scratch_regs_restore(adev);
  1991. else
  1992. amdgpu_atombios_scratch_regs_restore(adev);
  1993. /* post card */
  1994. if (amdgpu_need_post(adev)) {
  1995. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  1996. if (r)
  1997. DRM_ERROR("amdgpu asic init failed\n");
  1998. }
  1999. r = amdgpu_resume(adev);
  2000. if (r)
  2001. DRM_ERROR("amdgpu_resume failed (%d).\n", r);
  2002. amdgpu_fence_driver_resume(adev);
  2003. if (resume) {
  2004. r = amdgpu_ib_ring_tests(adev);
  2005. if (r)
  2006. DRM_ERROR("ib ring test failed (%d).\n", r);
  2007. }
  2008. r = amdgpu_late_init(adev);
  2009. if (r) {
  2010. if (fbcon)
  2011. console_unlock();
  2012. return r;
  2013. }
  2014. /* pin cursors */
  2015. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2016. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2017. if (amdgpu_crtc->cursor_bo) {
  2018. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2019. r = amdgpu_bo_reserve(aobj, false);
  2020. if (r == 0) {
  2021. r = amdgpu_bo_pin(aobj,
  2022. AMDGPU_GEM_DOMAIN_VRAM,
  2023. &amdgpu_crtc->cursor_addr);
  2024. if (r != 0)
  2025. DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
  2026. amdgpu_bo_unreserve(aobj);
  2027. }
  2028. }
  2029. }
  2030. /* blat the mode back in */
  2031. if (fbcon) {
  2032. drm_helper_resume_force_mode(dev);
  2033. /* turn on display hw */
  2034. drm_modeset_lock_all(dev);
  2035. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2036. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  2037. }
  2038. drm_modeset_unlock_all(dev);
  2039. }
  2040. drm_kms_helper_poll_enable(dev);
  2041. /*
  2042. * Most of the connector probing functions try to acquire runtime pm
  2043. * refs to ensure that the GPU is powered on when connector polling is
  2044. * performed. Since we're calling this from a runtime PM callback,
  2045. * trying to acquire rpm refs will cause us to deadlock.
  2046. *
  2047. * Since we're guaranteed to be holding the rpm lock, it's safe to
  2048. * temporarily disable the rpm helpers so this doesn't deadlock us.
  2049. */
  2050. #ifdef CONFIG_PM
  2051. dev->dev->power.disable_depth++;
  2052. #endif
  2053. drm_helper_hpd_irq_event(dev);
  2054. #ifdef CONFIG_PM
  2055. dev->dev->power.disable_depth--;
  2056. #endif
  2057. if (fbcon) {
  2058. amdgpu_fbdev_set_suspend(adev, 0);
  2059. console_unlock();
  2060. }
  2061. return 0;
  2062. }
  2063. static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
  2064. {
  2065. int i;
  2066. bool asic_hang = false;
  2067. for (i = 0; i < adev->num_ip_blocks; i++) {
  2068. if (!adev->ip_blocks[i].status.valid)
  2069. continue;
  2070. if (adev->ip_blocks[i].version->funcs->check_soft_reset)
  2071. adev->ip_blocks[i].status.hang =
  2072. adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
  2073. if (adev->ip_blocks[i].status.hang) {
  2074. DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
  2075. asic_hang = true;
  2076. }
  2077. }
  2078. return asic_hang;
  2079. }
  2080. static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
  2081. {
  2082. int i, r = 0;
  2083. for (i = 0; i < adev->num_ip_blocks; i++) {
  2084. if (!adev->ip_blocks[i].status.valid)
  2085. continue;
  2086. if (adev->ip_blocks[i].status.hang &&
  2087. adev->ip_blocks[i].version->funcs->pre_soft_reset) {
  2088. r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
  2089. if (r)
  2090. return r;
  2091. }
  2092. }
  2093. return 0;
  2094. }
  2095. static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
  2096. {
  2097. int i;
  2098. for (i = 0; i < adev->num_ip_blocks; i++) {
  2099. if (!adev->ip_blocks[i].status.valid)
  2100. continue;
  2101. if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
  2102. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
  2103. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
  2104. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
  2105. if (adev->ip_blocks[i].status.hang) {
  2106. DRM_INFO("Some block need full reset!\n");
  2107. return true;
  2108. }
  2109. }
  2110. }
  2111. return false;
  2112. }
  2113. static int amdgpu_soft_reset(struct amdgpu_device *adev)
  2114. {
  2115. int i, r = 0;
  2116. for (i = 0; i < adev->num_ip_blocks; i++) {
  2117. if (!adev->ip_blocks[i].status.valid)
  2118. continue;
  2119. if (adev->ip_blocks[i].status.hang &&
  2120. adev->ip_blocks[i].version->funcs->soft_reset) {
  2121. r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
  2122. if (r)
  2123. return r;
  2124. }
  2125. }
  2126. return 0;
  2127. }
  2128. static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
  2129. {
  2130. int i, r = 0;
  2131. for (i = 0; i < adev->num_ip_blocks; i++) {
  2132. if (!adev->ip_blocks[i].status.valid)
  2133. continue;
  2134. if (adev->ip_blocks[i].status.hang &&
  2135. adev->ip_blocks[i].version->funcs->post_soft_reset)
  2136. r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
  2137. if (r)
  2138. return r;
  2139. }
  2140. return 0;
  2141. }
  2142. bool amdgpu_need_backup(struct amdgpu_device *adev)
  2143. {
  2144. if (adev->flags & AMD_IS_APU)
  2145. return false;
  2146. return amdgpu_lockup_timeout > 0 ? true : false;
  2147. }
  2148. static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
  2149. struct amdgpu_ring *ring,
  2150. struct amdgpu_bo *bo,
  2151. struct dma_fence **fence)
  2152. {
  2153. uint32_t domain;
  2154. int r;
  2155. if (!bo->shadow)
  2156. return 0;
  2157. r = amdgpu_bo_reserve(bo, false);
  2158. if (r)
  2159. return r;
  2160. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  2161. /* if bo has been evicted, then no need to recover */
  2162. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  2163. r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
  2164. NULL, fence, true);
  2165. if (r) {
  2166. DRM_ERROR("recover page table failed!\n");
  2167. goto err;
  2168. }
  2169. }
  2170. err:
  2171. amdgpu_bo_unreserve(bo);
  2172. return r;
  2173. }
  2174. /**
  2175. * amdgpu_sriov_gpu_reset - reset the asic
  2176. *
  2177. * @adev: amdgpu device pointer
  2178. * @voluntary: if this reset is requested by guest.
  2179. * (true means by guest and false means by HYPERVISOR )
  2180. *
  2181. * Attempt the reset the GPU if it has hung (all asics).
  2182. * for SRIOV case.
  2183. * Returns 0 for success or an error on failure.
  2184. */
  2185. int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, bool voluntary)
  2186. {
  2187. int i, r = 0;
  2188. int resched;
  2189. struct amdgpu_bo *bo, *tmp;
  2190. struct amdgpu_ring *ring;
  2191. struct dma_fence *fence = NULL, *next = NULL;
  2192. mutex_lock(&adev->virt.lock_reset);
  2193. atomic_inc(&adev->gpu_reset_counter);
  2194. adev->gfx.in_reset = true;
  2195. /* block TTM */
  2196. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  2197. /* block scheduler */
  2198. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2199. ring = adev->rings[i];
  2200. if (!ring || !ring->sched.thread)
  2201. continue;
  2202. kthread_park(ring->sched.thread);
  2203. amd_sched_hw_job_reset(&ring->sched);
  2204. }
  2205. /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
  2206. amdgpu_fence_driver_force_completion(adev);
  2207. /* request to take full control of GPU before re-initialization */
  2208. if (voluntary)
  2209. amdgpu_virt_reset_gpu(adev);
  2210. else
  2211. amdgpu_virt_request_full_gpu(adev, true);
  2212. /* Resume IP prior to SMC */
  2213. amdgpu_sriov_reinit_early(adev);
  2214. /* we need recover gart prior to run SMC/CP/SDMA resume */
  2215. amdgpu_ttm_recover_gart(adev);
  2216. /* now we are okay to resume SMC/CP/SDMA */
  2217. amdgpu_sriov_reinit_late(adev);
  2218. amdgpu_irq_gpu_reset_resume_helper(adev);
  2219. if (amdgpu_ib_ring_tests(adev))
  2220. dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
  2221. /* release full control of GPU after ib test */
  2222. amdgpu_virt_release_full_gpu(adev, true);
  2223. DRM_INFO("recover vram bo from shadow\n");
  2224. ring = adev->mman.buffer_funcs_ring;
  2225. mutex_lock(&adev->shadow_list_lock);
  2226. list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
  2227. amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
  2228. if (fence) {
  2229. r = dma_fence_wait(fence, false);
  2230. if (r) {
  2231. WARN(r, "recovery from shadow isn't completed\n");
  2232. break;
  2233. }
  2234. }
  2235. dma_fence_put(fence);
  2236. fence = next;
  2237. }
  2238. mutex_unlock(&adev->shadow_list_lock);
  2239. if (fence) {
  2240. r = dma_fence_wait(fence, false);
  2241. if (r)
  2242. WARN(r, "recovery from shadow isn't completed\n");
  2243. }
  2244. dma_fence_put(fence);
  2245. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2246. struct amdgpu_ring *ring = adev->rings[i];
  2247. if (!ring || !ring->sched.thread)
  2248. continue;
  2249. amd_sched_job_recovery(&ring->sched);
  2250. kthread_unpark(ring->sched.thread);
  2251. }
  2252. drm_helper_resume_force_mode(adev->ddev);
  2253. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  2254. if (r) {
  2255. /* bad news, how to tell it to userspace ? */
  2256. dev_info(adev->dev, "GPU reset failed\n");
  2257. }
  2258. adev->gfx.in_reset = false;
  2259. mutex_unlock(&adev->virt.lock_reset);
  2260. return r;
  2261. }
  2262. /**
  2263. * amdgpu_gpu_reset - reset the asic
  2264. *
  2265. * @adev: amdgpu device pointer
  2266. *
  2267. * Attempt the reset the GPU if it has hung (all asics).
  2268. * Returns 0 for success or an error on failure.
  2269. */
  2270. int amdgpu_gpu_reset(struct amdgpu_device *adev)
  2271. {
  2272. int i, r;
  2273. int resched;
  2274. bool need_full_reset;
  2275. if (amdgpu_sriov_vf(adev))
  2276. return amdgpu_sriov_gpu_reset(adev, true);
  2277. if (!amdgpu_check_soft_reset(adev)) {
  2278. DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
  2279. return 0;
  2280. }
  2281. atomic_inc(&adev->gpu_reset_counter);
  2282. /* block TTM */
  2283. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  2284. /* block scheduler */
  2285. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2286. struct amdgpu_ring *ring = adev->rings[i];
  2287. if (!ring)
  2288. continue;
  2289. kthread_park(ring->sched.thread);
  2290. amd_sched_hw_job_reset(&ring->sched);
  2291. }
  2292. /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
  2293. amdgpu_fence_driver_force_completion(adev);
  2294. need_full_reset = amdgpu_need_full_reset(adev);
  2295. if (!need_full_reset) {
  2296. amdgpu_pre_soft_reset(adev);
  2297. r = amdgpu_soft_reset(adev);
  2298. amdgpu_post_soft_reset(adev);
  2299. if (r || amdgpu_check_soft_reset(adev)) {
  2300. DRM_INFO("soft reset failed, will fallback to full reset!\n");
  2301. need_full_reset = true;
  2302. }
  2303. }
  2304. if (need_full_reset) {
  2305. r = amdgpu_suspend(adev);
  2306. retry:
  2307. /* Disable fb access */
  2308. if (adev->mode_info.num_crtc) {
  2309. struct amdgpu_mode_mc_save save;
  2310. amdgpu_display_stop_mc_access(adev, &save);
  2311. amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
  2312. }
  2313. if (adev->is_atom_fw)
  2314. amdgpu_atomfirmware_scratch_regs_save(adev);
  2315. else
  2316. amdgpu_atombios_scratch_regs_save(adev);
  2317. r = amdgpu_asic_reset(adev);
  2318. if (adev->is_atom_fw)
  2319. amdgpu_atomfirmware_scratch_regs_restore(adev);
  2320. else
  2321. amdgpu_atombios_scratch_regs_restore(adev);
  2322. /* post card */
  2323. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2324. if (!r) {
  2325. dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
  2326. r = amdgpu_resume(adev);
  2327. }
  2328. }
  2329. if (!r) {
  2330. amdgpu_irq_gpu_reset_resume_helper(adev);
  2331. if (need_full_reset && amdgpu_need_backup(adev)) {
  2332. r = amdgpu_ttm_recover_gart(adev);
  2333. if (r)
  2334. DRM_ERROR("gart recovery failed!!!\n");
  2335. }
  2336. r = amdgpu_ib_ring_tests(adev);
  2337. if (r) {
  2338. dev_err(adev->dev, "ib ring test failed (%d).\n", r);
  2339. r = amdgpu_suspend(adev);
  2340. need_full_reset = true;
  2341. goto retry;
  2342. }
  2343. /**
  2344. * recovery vm page tables, since we cannot depend on VRAM is
  2345. * consistent after gpu full reset.
  2346. */
  2347. if (need_full_reset && amdgpu_need_backup(adev)) {
  2348. struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  2349. struct amdgpu_bo *bo, *tmp;
  2350. struct dma_fence *fence = NULL, *next = NULL;
  2351. DRM_INFO("recover vram bo from shadow\n");
  2352. mutex_lock(&adev->shadow_list_lock);
  2353. list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
  2354. amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
  2355. if (fence) {
  2356. r = dma_fence_wait(fence, false);
  2357. if (r) {
  2358. WARN(r, "recovery from shadow isn't completed\n");
  2359. break;
  2360. }
  2361. }
  2362. dma_fence_put(fence);
  2363. fence = next;
  2364. }
  2365. mutex_unlock(&adev->shadow_list_lock);
  2366. if (fence) {
  2367. r = dma_fence_wait(fence, false);
  2368. if (r)
  2369. WARN(r, "recovery from shadow isn't completed\n");
  2370. }
  2371. dma_fence_put(fence);
  2372. }
  2373. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2374. struct amdgpu_ring *ring = adev->rings[i];
  2375. if (!ring)
  2376. continue;
  2377. amd_sched_job_recovery(&ring->sched);
  2378. kthread_unpark(ring->sched.thread);
  2379. }
  2380. } else {
  2381. dev_err(adev->dev, "asic resume failed (%d).\n", r);
  2382. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2383. if (adev->rings[i]) {
  2384. kthread_unpark(adev->rings[i]->sched.thread);
  2385. }
  2386. }
  2387. }
  2388. drm_helper_resume_force_mode(adev->ddev);
  2389. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  2390. if (r) {
  2391. /* bad news, how to tell it to userspace ? */
  2392. dev_info(adev->dev, "GPU reset failed\n");
  2393. }
  2394. return r;
  2395. }
  2396. void amdgpu_get_pcie_info(struct amdgpu_device *adev)
  2397. {
  2398. u32 mask;
  2399. int ret;
  2400. if (amdgpu_pcie_gen_cap)
  2401. adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
  2402. if (amdgpu_pcie_lane_cap)
  2403. adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
  2404. /* covers APUs as well */
  2405. if (pci_is_root_bus(adev->pdev->bus)) {
  2406. if (adev->pm.pcie_gen_mask == 0)
  2407. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2408. if (adev->pm.pcie_mlw_mask == 0)
  2409. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2410. return;
  2411. }
  2412. if (adev->pm.pcie_gen_mask == 0) {
  2413. ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
  2414. if (!ret) {
  2415. adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2416. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2417. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2418. if (mask & DRM_PCIE_SPEED_25)
  2419. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
  2420. if (mask & DRM_PCIE_SPEED_50)
  2421. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
  2422. if (mask & DRM_PCIE_SPEED_80)
  2423. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
  2424. } else {
  2425. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2426. }
  2427. }
  2428. if (adev->pm.pcie_mlw_mask == 0) {
  2429. ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
  2430. if (!ret) {
  2431. switch (mask) {
  2432. case 32:
  2433. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
  2434. CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2435. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2436. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2437. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2438. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2439. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2440. break;
  2441. case 16:
  2442. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2443. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2444. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2445. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2446. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2447. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2448. break;
  2449. case 12:
  2450. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2451. 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 8:
  2457. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2458. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2459. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2460. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2461. break;
  2462. case 4:
  2463. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2464. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2465. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2466. break;
  2467. case 2:
  2468. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2469. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2470. break;
  2471. case 1:
  2472. adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
  2473. break;
  2474. default:
  2475. break;
  2476. }
  2477. } else {
  2478. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2479. }
  2480. }
  2481. }
  2482. /*
  2483. * Debugfs
  2484. */
  2485. int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
  2486. const struct drm_info_list *files,
  2487. unsigned nfiles)
  2488. {
  2489. unsigned i;
  2490. for (i = 0; i < adev->debugfs_count; i++) {
  2491. if (adev->debugfs[i].files == files) {
  2492. /* Already registered */
  2493. return 0;
  2494. }
  2495. }
  2496. i = adev->debugfs_count + 1;
  2497. if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
  2498. DRM_ERROR("Reached maximum number of debugfs components.\n");
  2499. DRM_ERROR("Report so we increase "
  2500. "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
  2501. return -EINVAL;
  2502. }
  2503. adev->debugfs[adev->debugfs_count].files = files;
  2504. adev->debugfs[adev->debugfs_count].num_files = nfiles;
  2505. adev->debugfs_count = i;
  2506. #if defined(CONFIG_DEBUG_FS)
  2507. drm_debugfs_create_files(files, nfiles,
  2508. adev->ddev->primary->debugfs_root,
  2509. adev->ddev->primary);
  2510. #endif
  2511. return 0;
  2512. }
  2513. #if defined(CONFIG_DEBUG_FS)
  2514. static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
  2515. size_t size, loff_t *pos)
  2516. {
  2517. struct amdgpu_device *adev = file_inode(f)->i_private;
  2518. ssize_t result = 0;
  2519. int r;
  2520. bool pm_pg_lock, use_bank;
  2521. unsigned instance_bank, sh_bank, se_bank;
  2522. if (size & 0x3 || *pos & 0x3)
  2523. return -EINVAL;
  2524. /* are we reading registers for which a PG lock is necessary? */
  2525. pm_pg_lock = (*pos >> 23) & 1;
  2526. if (*pos & (1ULL << 62)) {
  2527. se_bank = (*pos >> 24) & 0x3FF;
  2528. sh_bank = (*pos >> 34) & 0x3FF;
  2529. instance_bank = (*pos >> 44) & 0x3FF;
  2530. if (se_bank == 0x3FF)
  2531. se_bank = 0xFFFFFFFF;
  2532. if (sh_bank == 0x3FF)
  2533. sh_bank = 0xFFFFFFFF;
  2534. if (instance_bank == 0x3FF)
  2535. instance_bank = 0xFFFFFFFF;
  2536. use_bank = 1;
  2537. } else {
  2538. use_bank = 0;
  2539. }
  2540. *pos &= (1UL << 22) - 1;
  2541. if (use_bank) {
  2542. if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
  2543. (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
  2544. return -EINVAL;
  2545. mutex_lock(&adev->grbm_idx_mutex);
  2546. amdgpu_gfx_select_se_sh(adev, se_bank,
  2547. sh_bank, instance_bank);
  2548. }
  2549. if (pm_pg_lock)
  2550. mutex_lock(&adev->pm.mutex);
  2551. while (size) {
  2552. uint32_t value;
  2553. if (*pos > adev->rmmio_size)
  2554. goto end;
  2555. value = RREG32(*pos >> 2);
  2556. r = put_user(value, (uint32_t *)buf);
  2557. if (r) {
  2558. result = r;
  2559. goto end;
  2560. }
  2561. result += 4;
  2562. buf += 4;
  2563. *pos += 4;
  2564. size -= 4;
  2565. }
  2566. end:
  2567. if (use_bank) {
  2568. amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
  2569. mutex_unlock(&adev->grbm_idx_mutex);
  2570. }
  2571. if (pm_pg_lock)
  2572. mutex_unlock(&adev->pm.mutex);
  2573. return result;
  2574. }
  2575. static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
  2576. size_t size, loff_t *pos)
  2577. {
  2578. struct amdgpu_device *adev = file_inode(f)->i_private;
  2579. ssize_t result = 0;
  2580. int r;
  2581. bool pm_pg_lock, use_bank;
  2582. unsigned instance_bank, sh_bank, se_bank;
  2583. if (size & 0x3 || *pos & 0x3)
  2584. return -EINVAL;
  2585. /* are we reading registers for which a PG lock is necessary? */
  2586. pm_pg_lock = (*pos >> 23) & 1;
  2587. if (*pos & (1ULL << 62)) {
  2588. se_bank = (*pos >> 24) & 0x3FF;
  2589. sh_bank = (*pos >> 34) & 0x3FF;
  2590. instance_bank = (*pos >> 44) & 0x3FF;
  2591. if (se_bank == 0x3FF)
  2592. se_bank = 0xFFFFFFFF;
  2593. if (sh_bank == 0x3FF)
  2594. sh_bank = 0xFFFFFFFF;
  2595. if (instance_bank == 0x3FF)
  2596. instance_bank = 0xFFFFFFFF;
  2597. use_bank = 1;
  2598. } else {
  2599. use_bank = 0;
  2600. }
  2601. *pos &= (1UL << 22) - 1;
  2602. if (use_bank) {
  2603. if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
  2604. (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
  2605. return -EINVAL;
  2606. mutex_lock(&adev->grbm_idx_mutex);
  2607. amdgpu_gfx_select_se_sh(adev, se_bank,
  2608. sh_bank, instance_bank);
  2609. }
  2610. if (pm_pg_lock)
  2611. mutex_lock(&adev->pm.mutex);
  2612. while (size) {
  2613. uint32_t value;
  2614. if (*pos > adev->rmmio_size)
  2615. return result;
  2616. r = get_user(value, (uint32_t *)buf);
  2617. if (r)
  2618. return r;
  2619. WREG32(*pos >> 2, value);
  2620. result += 4;
  2621. buf += 4;
  2622. *pos += 4;
  2623. size -= 4;
  2624. }
  2625. if (use_bank) {
  2626. amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
  2627. mutex_unlock(&adev->grbm_idx_mutex);
  2628. }
  2629. if (pm_pg_lock)
  2630. mutex_unlock(&adev->pm.mutex);
  2631. return result;
  2632. }
  2633. static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
  2634. size_t size, loff_t *pos)
  2635. {
  2636. struct amdgpu_device *adev = file_inode(f)->i_private;
  2637. ssize_t result = 0;
  2638. int r;
  2639. if (size & 0x3 || *pos & 0x3)
  2640. return -EINVAL;
  2641. while (size) {
  2642. uint32_t value;
  2643. value = RREG32_PCIE(*pos >> 2);
  2644. r = put_user(value, (uint32_t *)buf);
  2645. if (r)
  2646. return r;
  2647. result += 4;
  2648. buf += 4;
  2649. *pos += 4;
  2650. size -= 4;
  2651. }
  2652. return result;
  2653. }
  2654. static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
  2655. size_t size, loff_t *pos)
  2656. {
  2657. struct amdgpu_device *adev = file_inode(f)->i_private;
  2658. ssize_t result = 0;
  2659. int r;
  2660. if (size & 0x3 || *pos & 0x3)
  2661. return -EINVAL;
  2662. while (size) {
  2663. uint32_t value;
  2664. r = get_user(value, (uint32_t *)buf);
  2665. if (r)
  2666. return r;
  2667. WREG32_PCIE(*pos >> 2, value);
  2668. result += 4;
  2669. buf += 4;
  2670. *pos += 4;
  2671. size -= 4;
  2672. }
  2673. return result;
  2674. }
  2675. static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
  2676. size_t size, loff_t *pos)
  2677. {
  2678. struct amdgpu_device *adev = file_inode(f)->i_private;
  2679. ssize_t result = 0;
  2680. int r;
  2681. if (size & 0x3 || *pos & 0x3)
  2682. return -EINVAL;
  2683. while (size) {
  2684. uint32_t value;
  2685. value = RREG32_DIDT(*pos >> 2);
  2686. r = put_user(value, (uint32_t *)buf);
  2687. if (r)
  2688. return r;
  2689. result += 4;
  2690. buf += 4;
  2691. *pos += 4;
  2692. size -= 4;
  2693. }
  2694. return result;
  2695. }
  2696. static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
  2697. size_t size, loff_t *pos)
  2698. {
  2699. struct amdgpu_device *adev = file_inode(f)->i_private;
  2700. ssize_t result = 0;
  2701. int r;
  2702. if (size & 0x3 || *pos & 0x3)
  2703. return -EINVAL;
  2704. while (size) {
  2705. uint32_t value;
  2706. r = get_user(value, (uint32_t *)buf);
  2707. if (r)
  2708. return r;
  2709. WREG32_DIDT(*pos >> 2, value);
  2710. result += 4;
  2711. buf += 4;
  2712. *pos += 4;
  2713. size -= 4;
  2714. }
  2715. return result;
  2716. }
  2717. static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
  2718. size_t size, loff_t *pos)
  2719. {
  2720. struct amdgpu_device *adev = file_inode(f)->i_private;
  2721. ssize_t result = 0;
  2722. int r;
  2723. if (size & 0x3 || *pos & 0x3)
  2724. return -EINVAL;
  2725. while (size) {
  2726. uint32_t value;
  2727. value = RREG32_SMC(*pos);
  2728. r = put_user(value, (uint32_t *)buf);
  2729. if (r)
  2730. return r;
  2731. result += 4;
  2732. buf += 4;
  2733. *pos += 4;
  2734. size -= 4;
  2735. }
  2736. return result;
  2737. }
  2738. static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
  2739. size_t size, loff_t *pos)
  2740. {
  2741. struct amdgpu_device *adev = file_inode(f)->i_private;
  2742. ssize_t result = 0;
  2743. int r;
  2744. if (size & 0x3 || *pos & 0x3)
  2745. return -EINVAL;
  2746. while (size) {
  2747. uint32_t value;
  2748. r = get_user(value, (uint32_t *)buf);
  2749. if (r)
  2750. return r;
  2751. WREG32_SMC(*pos, value);
  2752. result += 4;
  2753. buf += 4;
  2754. *pos += 4;
  2755. size -= 4;
  2756. }
  2757. return result;
  2758. }
  2759. static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
  2760. size_t size, loff_t *pos)
  2761. {
  2762. struct amdgpu_device *adev = file_inode(f)->i_private;
  2763. ssize_t result = 0;
  2764. int r;
  2765. uint32_t *config, no_regs = 0;
  2766. if (size & 0x3 || *pos & 0x3)
  2767. return -EINVAL;
  2768. config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
  2769. if (!config)
  2770. return -ENOMEM;
  2771. /* version, increment each time something is added */
  2772. config[no_regs++] = 3;
  2773. config[no_regs++] = adev->gfx.config.max_shader_engines;
  2774. config[no_regs++] = adev->gfx.config.max_tile_pipes;
  2775. config[no_regs++] = adev->gfx.config.max_cu_per_sh;
  2776. config[no_regs++] = adev->gfx.config.max_sh_per_se;
  2777. config[no_regs++] = adev->gfx.config.max_backends_per_se;
  2778. config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
  2779. config[no_regs++] = adev->gfx.config.max_gprs;
  2780. config[no_regs++] = adev->gfx.config.max_gs_threads;
  2781. config[no_regs++] = adev->gfx.config.max_hw_contexts;
  2782. config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
  2783. config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
  2784. config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
  2785. config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
  2786. config[no_regs++] = adev->gfx.config.num_tile_pipes;
  2787. config[no_regs++] = adev->gfx.config.backend_enable_mask;
  2788. config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
  2789. config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
  2790. config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
  2791. config[no_regs++] = adev->gfx.config.num_gpus;
  2792. config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
  2793. config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
  2794. config[no_regs++] = adev->gfx.config.gb_addr_config;
  2795. config[no_regs++] = adev->gfx.config.num_rbs;
  2796. /* rev==1 */
  2797. config[no_regs++] = adev->rev_id;
  2798. config[no_regs++] = adev->pg_flags;
  2799. config[no_regs++] = adev->cg_flags;
  2800. /* rev==2 */
  2801. config[no_regs++] = adev->family;
  2802. config[no_regs++] = adev->external_rev_id;
  2803. /* rev==3 */
  2804. config[no_regs++] = adev->pdev->device;
  2805. config[no_regs++] = adev->pdev->revision;
  2806. config[no_regs++] = adev->pdev->subsystem_device;
  2807. config[no_regs++] = adev->pdev->subsystem_vendor;
  2808. while (size && (*pos < no_regs * 4)) {
  2809. uint32_t value;
  2810. value = config[*pos >> 2];
  2811. r = put_user(value, (uint32_t *)buf);
  2812. if (r) {
  2813. kfree(config);
  2814. return r;
  2815. }
  2816. result += 4;
  2817. buf += 4;
  2818. *pos += 4;
  2819. size -= 4;
  2820. }
  2821. kfree(config);
  2822. return result;
  2823. }
  2824. static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
  2825. size_t size, loff_t *pos)
  2826. {
  2827. struct amdgpu_device *adev = file_inode(f)->i_private;
  2828. int idx, x, outsize, r, valuesize;
  2829. uint32_t values[16];
  2830. if (size & 3 || *pos & 0x3)
  2831. return -EINVAL;
  2832. if (amdgpu_dpm == 0)
  2833. return -EINVAL;
  2834. /* convert offset to sensor number */
  2835. idx = *pos >> 2;
  2836. valuesize = sizeof(values);
  2837. if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
  2838. r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
  2839. else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
  2840. r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
  2841. &valuesize);
  2842. else
  2843. return -EINVAL;
  2844. if (size > valuesize)
  2845. return -EINVAL;
  2846. outsize = 0;
  2847. x = 0;
  2848. if (!r) {
  2849. while (size) {
  2850. r = put_user(values[x++], (int32_t *)buf);
  2851. buf += 4;
  2852. size -= 4;
  2853. outsize += 4;
  2854. }
  2855. }
  2856. return !r ? outsize : r;
  2857. }
  2858. static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
  2859. size_t size, loff_t *pos)
  2860. {
  2861. struct amdgpu_device *adev = f->f_inode->i_private;
  2862. int r, x;
  2863. ssize_t result=0;
  2864. uint32_t offset, se, sh, cu, wave, simd, data[32];
  2865. if (size & 3 || *pos & 3)
  2866. return -EINVAL;
  2867. /* decode offset */
  2868. offset = (*pos & 0x7F);
  2869. se = ((*pos >> 7) & 0xFF);
  2870. sh = ((*pos >> 15) & 0xFF);
  2871. cu = ((*pos >> 23) & 0xFF);
  2872. wave = ((*pos >> 31) & 0xFF);
  2873. simd = ((*pos >> 37) & 0xFF);
  2874. /* switch to the specific se/sh/cu */
  2875. mutex_lock(&adev->grbm_idx_mutex);
  2876. amdgpu_gfx_select_se_sh(adev, se, sh, cu);
  2877. x = 0;
  2878. if (adev->gfx.funcs->read_wave_data)
  2879. adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
  2880. amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
  2881. mutex_unlock(&adev->grbm_idx_mutex);
  2882. if (!x)
  2883. return -EINVAL;
  2884. while (size && (offset < x * 4)) {
  2885. uint32_t value;
  2886. value = data[offset >> 2];
  2887. r = put_user(value, (uint32_t *)buf);
  2888. if (r)
  2889. return r;
  2890. result += 4;
  2891. buf += 4;
  2892. offset += 4;
  2893. size -= 4;
  2894. }
  2895. return result;
  2896. }
  2897. static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
  2898. size_t size, loff_t *pos)
  2899. {
  2900. struct amdgpu_device *adev = f->f_inode->i_private;
  2901. int r;
  2902. ssize_t result = 0;
  2903. uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
  2904. if (size & 3 || *pos & 3)
  2905. return -EINVAL;
  2906. /* decode offset */
  2907. offset = (*pos & 0xFFF); /* in dwords */
  2908. se = ((*pos >> 12) & 0xFF);
  2909. sh = ((*pos >> 20) & 0xFF);
  2910. cu = ((*pos >> 28) & 0xFF);
  2911. wave = ((*pos >> 36) & 0xFF);
  2912. simd = ((*pos >> 44) & 0xFF);
  2913. thread = ((*pos >> 52) & 0xFF);
  2914. bank = ((*pos >> 60) & 1);
  2915. data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
  2916. if (!data)
  2917. return -ENOMEM;
  2918. /* switch to the specific se/sh/cu */
  2919. mutex_lock(&adev->grbm_idx_mutex);
  2920. amdgpu_gfx_select_se_sh(adev, se, sh, cu);
  2921. if (bank == 0) {
  2922. if (adev->gfx.funcs->read_wave_vgprs)
  2923. adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
  2924. } else {
  2925. if (adev->gfx.funcs->read_wave_sgprs)
  2926. adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
  2927. }
  2928. amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
  2929. mutex_unlock(&adev->grbm_idx_mutex);
  2930. while (size) {
  2931. uint32_t value;
  2932. value = data[offset++];
  2933. r = put_user(value, (uint32_t *)buf);
  2934. if (r) {
  2935. result = r;
  2936. goto err;
  2937. }
  2938. result += 4;
  2939. buf += 4;
  2940. size -= 4;
  2941. }
  2942. err:
  2943. kfree(data);
  2944. return result;
  2945. }
  2946. static const struct file_operations amdgpu_debugfs_regs_fops = {
  2947. .owner = THIS_MODULE,
  2948. .read = amdgpu_debugfs_regs_read,
  2949. .write = amdgpu_debugfs_regs_write,
  2950. .llseek = default_llseek
  2951. };
  2952. static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
  2953. .owner = THIS_MODULE,
  2954. .read = amdgpu_debugfs_regs_didt_read,
  2955. .write = amdgpu_debugfs_regs_didt_write,
  2956. .llseek = default_llseek
  2957. };
  2958. static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
  2959. .owner = THIS_MODULE,
  2960. .read = amdgpu_debugfs_regs_pcie_read,
  2961. .write = amdgpu_debugfs_regs_pcie_write,
  2962. .llseek = default_llseek
  2963. };
  2964. static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
  2965. .owner = THIS_MODULE,
  2966. .read = amdgpu_debugfs_regs_smc_read,
  2967. .write = amdgpu_debugfs_regs_smc_write,
  2968. .llseek = default_llseek
  2969. };
  2970. static const struct file_operations amdgpu_debugfs_gca_config_fops = {
  2971. .owner = THIS_MODULE,
  2972. .read = amdgpu_debugfs_gca_config_read,
  2973. .llseek = default_llseek
  2974. };
  2975. static const struct file_operations amdgpu_debugfs_sensors_fops = {
  2976. .owner = THIS_MODULE,
  2977. .read = amdgpu_debugfs_sensor_read,
  2978. .llseek = default_llseek
  2979. };
  2980. static const struct file_operations amdgpu_debugfs_wave_fops = {
  2981. .owner = THIS_MODULE,
  2982. .read = amdgpu_debugfs_wave_read,
  2983. .llseek = default_llseek
  2984. };
  2985. static const struct file_operations amdgpu_debugfs_gpr_fops = {
  2986. .owner = THIS_MODULE,
  2987. .read = amdgpu_debugfs_gpr_read,
  2988. .llseek = default_llseek
  2989. };
  2990. static const struct file_operations *debugfs_regs[] = {
  2991. &amdgpu_debugfs_regs_fops,
  2992. &amdgpu_debugfs_regs_didt_fops,
  2993. &amdgpu_debugfs_regs_pcie_fops,
  2994. &amdgpu_debugfs_regs_smc_fops,
  2995. &amdgpu_debugfs_gca_config_fops,
  2996. &amdgpu_debugfs_sensors_fops,
  2997. &amdgpu_debugfs_wave_fops,
  2998. &amdgpu_debugfs_gpr_fops,
  2999. };
  3000. static const char *debugfs_regs_names[] = {
  3001. "amdgpu_regs",
  3002. "amdgpu_regs_didt",
  3003. "amdgpu_regs_pcie",
  3004. "amdgpu_regs_smc",
  3005. "amdgpu_gca_config",
  3006. "amdgpu_sensors",
  3007. "amdgpu_wave",
  3008. "amdgpu_gpr",
  3009. };
  3010. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
  3011. {
  3012. struct drm_minor *minor = adev->ddev->primary;
  3013. struct dentry *ent, *root = minor->debugfs_root;
  3014. unsigned i, j;
  3015. for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
  3016. ent = debugfs_create_file(debugfs_regs_names[i],
  3017. S_IFREG | S_IRUGO, root,
  3018. adev, debugfs_regs[i]);
  3019. if (IS_ERR(ent)) {
  3020. for (j = 0; j < i; j++) {
  3021. debugfs_remove(adev->debugfs_regs[i]);
  3022. adev->debugfs_regs[i] = NULL;
  3023. }
  3024. return PTR_ERR(ent);
  3025. }
  3026. if (!i)
  3027. i_size_write(ent->d_inode, adev->rmmio_size);
  3028. adev->debugfs_regs[i] = ent;
  3029. }
  3030. return 0;
  3031. }
  3032. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
  3033. {
  3034. unsigned i;
  3035. for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
  3036. if (adev->debugfs_regs[i]) {
  3037. debugfs_remove(adev->debugfs_regs[i]);
  3038. adev->debugfs_regs[i] = NULL;
  3039. }
  3040. }
  3041. }
  3042. int amdgpu_debugfs_init(struct drm_minor *minor)
  3043. {
  3044. return 0;
  3045. }
  3046. #else
  3047. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
  3048. {
  3049. return 0;
  3050. }
  3051. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
  3052. #endif