amdgpu_device.c 88 KB

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