amdgpu_device.c 87 KB

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