amdgpu_device.c 75 KB

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