amdgpu_device.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880
  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 <drm/drmP.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include <drm/drm_atomic_helper.h>
  34. #include <drm/amdgpu_drm.h>
  35. #include <linux/vgaarb.h>
  36. #include <linux/vga_switcheroo.h>
  37. #include <linux/efi.h>
  38. #include "amdgpu.h"
  39. #include "amdgpu_trace.h"
  40. #include "amdgpu_i2c.h"
  41. #include "atom.h"
  42. #include "amdgpu_atombios.h"
  43. #include "amdgpu_atomfirmware.h"
  44. #include "amd_pcie.h"
  45. #ifdef CONFIG_DRM_AMDGPU_SI
  46. #include "si.h"
  47. #endif
  48. #ifdef CONFIG_DRM_AMDGPU_CIK
  49. #include "cik.h"
  50. #endif
  51. #include "vi.h"
  52. #include "soc15.h"
  53. #include "bif/bif_4_1_d.h"
  54. #include <linux/pci.h>
  55. #include <linux/firmware.h>
  56. #include "amdgpu_vf_error.h"
  57. #include "amdgpu_amdkfd.h"
  58. #include "amdgpu_pm.h"
  59. MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  60. MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
  61. #define AMDGPU_RESUME_MS 2000
  62. static const char *amdgpu_asic_name[] = {
  63. "TAHITI",
  64. "PITCAIRN",
  65. "VERDE",
  66. "OLAND",
  67. "HAINAN",
  68. "BONAIRE",
  69. "KAVERI",
  70. "KABINI",
  71. "HAWAII",
  72. "MULLINS",
  73. "TOPAZ",
  74. "TONGA",
  75. "FIJI",
  76. "CARRIZO",
  77. "STONEY",
  78. "POLARIS10",
  79. "POLARIS11",
  80. "POLARIS12",
  81. "VEGA10",
  82. "RAVEN",
  83. "LAST",
  84. };
  85. bool amdgpu_device_is_px(struct drm_device *dev)
  86. {
  87. struct amdgpu_device *adev = dev->dev_private;
  88. if (adev->flags & AMD_IS_PX)
  89. return true;
  90. return false;
  91. }
  92. /*
  93. * MMIO register access helper functions.
  94. */
  95. uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
  96. uint32_t acc_flags)
  97. {
  98. uint32_t ret;
  99. if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
  100. return amdgpu_virt_kiq_rreg(adev, reg);
  101. if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
  102. ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
  103. else {
  104. unsigned long flags;
  105. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  106. writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
  107. ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  108. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  109. }
  110. trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
  111. return ret;
  112. }
  113. /*
  114. * MMIO register read with bytes helper functions
  115. * @offset:bytes offset from MMIO start
  116. *
  117. */
  118. uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
  119. if (offset < adev->rmmio_size)
  120. return (readb(adev->rmmio + offset));
  121. BUG();
  122. }
  123. /*
  124. * MMIO register write with bytes helper functions
  125. * @offset:bytes offset from MMIO start
  126. * @value: the value want to be written to the register
  127. *
  128. */
  129. void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
  130. if (offset < adev->rmmio_size)
  131. writeb(value, adev->rmmio + offset);
  132. else
  133. BUG();
  134. }
  135. void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
  136. uint32_t acc_flags)
  137. {
  138. trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
  139. if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
  140. adev->last_mm_index = v;
  141. }
  142. if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
  143. return amdgpu_virt_kiq_wreg(adev, reg, v);
  144. if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
  145. writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
  146. else {
  147. unsigned long flags;
  148. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  149. writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
  150. writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  151. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  152. }
  153. if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
  154. udelay(500);
  155. }
  156. }
  157. u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
  158. {
  159. if ((reg * 4) < adev->rio_mem_size)
  160. return ioread32(adev->rio_mem + (reg * 4));
  161. else {
  162. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  163. return ioread32(adev->rio_mem + (mmMM_DATA * 4));
  164. }
  165. }
  166. void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
  167. {
  168. if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
  169. adev->last_mm_index = v;
  170. }
  171. if ((reg * 4) < adev->rio_mem_size)
  172. iowrite32(v, adev->rio_mem + (reg * 4));
  173. else {
  174. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  175. iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
  176. }
  177. if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
  178. udelay(500);
  179. }
  180. }
  181. /**
  182. * amdgpu_mm_rdoorbell - read a doorbell dword
  183. *
  184. * @adev: amdgpu_device pointer
  185. * @index: doorbell index
  186. *
  187. * Returns the value in the doorbell aperture at the
  188. * requested doorbell index (CIK).
  189. */
  190. u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
  191. {
  192. if (index < adev->doorbell.num_doorbells) {
  193. return readl(adev->doorbell.ptr + index);
  194. } else {
  195. DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
  196. return 0;
  197. }
  198. }
  199. /**
  200. * amdgpu_mm_wdoorbell - write a doorbell dword
  201. *
  202. * @adev: amdgpu_device pointer
  203. * @index: doorbell index
  204. * @v: value to write
  205. *
  206. * Writes @v to the doorbell aperture at the
  207. * requested doorbell index (CIK).
  208. */
  209. void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
  210. {
  211. if (index < adev->doorbell.num_doorbells) {
  212. writel(v, adev->doorbell.ptr + index);
  213. } else {
  214. DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  215. }
  216. }
  217. /**
  218. * amdgpu_mm_rdoorbell64 - read a doorbell Qword
  219. *
  220. * @adev: amdgpu_device pointer
  221. * @index: doorbell index
  222. *
  223. * Returns the value in the doorbell aperture at the
  224. * requested doorbell index (VEGA10+).
  225. */
  226. u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
  227. {
  228. if (index < adev->doorbell.num_doorbells) {
  229. return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
  230. } else {
  231. DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
  232. return 0;
  233. }
  234. }
  235. /**
  236. * amdgpu_mm_wdoorbell64 - write a doorbell Qword
  237. *
  238. * @adev: amdgpu_device pointer
  239. * @index: doorbell index
  240. * @v: value to write
  241. *
  242. * Writes @v to the doorbell aperture at the
  243. * requested doorbell index (VEGA10+).
  244. */
  245. void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
  246. {
  247. if (index < adev->doorbell.num_doorbells) {
  248. atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
  249. } else {
  250. DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  251. }
  252. }
  253. /**
  254. * amdgpu_invalid_rreg - dummy reg read function
  255. *
  256. * @adev: amdgpu device pointer
  257. * @reg: offset of register
  258. *
  259. * Dummy register read function. Used for register blocks
  260. * that certain asics don't have (all asics).
  261. * Returns the value in the register.
  262. */
  263. static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
  264. {
  265. DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
  266. BUG();
  267. return 0;
  268. }
  269. /**
  270. * amdgpu_invalid_wreg - dummy reg write function
  271. *
  272. * @adev: amdgpu device pointer
  273. * @reg: offset of register
  274. * @v: value to write to the register
  275. *
  276. * Dummy register read function. Used for register blocks
  277. * that certain asics don't have (all asics).
  278. */
  279. static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
  280. {
  281. DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
  282. reg, v);
  283. BUG();
  284. }
  285. /**
  286. * amdgpu_block_invalid_rreg - dummy reg read function
  287. *
  288. * @adev: amdgpu device pointer
  289. * @block: offset of instance
  290. * @reg: offset of register
  291. *
  292. * Dummy register read function. Used for register blocks
  293. * that certain asics don't have (all asics).
  294. * Returns the value in the register.
  295. */
  296. static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
  297. uint32_t block, uint32_t reg)
  298. {
  299. DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
  300. reg, block);
  301. BUG();
  302. return 0;
  303. }
  304. /**
  305. * amdgpu_block_invalid_wreg - dummy reg write function
  306. *
  307. * @adev: amdgpu device pointer
  308. * @block: offset of instance
  309. * @reg: offset of register
  310. * @v: value to write to the register
  311. *
  312. * Dummy register read function. Used for register blocks
  313. * that certain asics don't have (all asics).
  314. */
  315. static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
  316. uint32_t block,
  317. uint32_t reg, uint32_t v)
  318. {
  319. DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
  320. reg, block, v);
  321. BUG();
  322. }
  323. static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
  324. {
  325. return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
  326. PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
  327. &adev->vram_scratch.robj,
  328. &adev->vram_scratch.gpu_addr,
  329. (void **)&adev->vram_scratch.ptr);
  330. }
  331. static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
  332. {
  333. amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
  334. }
  335. /**
  336. * amdgpu_device_program_register_sequence - program an array of registers.
  337. *
  338. * @adev: amdgpu_device pointer
  339. * @registers: pointer to the register array
  340. * @array_size: size of the register array
  341. *
  342. * Programs an array or registers with and and or masks.
  343. * This is a helper for setting golden registers.
  344. */
  345. void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
  346. const u32 *registers,
  347. const u32 array_size)
  348. {
  349. u32 tmp, reg, and_mask, or_mask;
  350. int i;
  351. if (array_size % 3)
  352. return;
  353. for (i = 0; i < array_size; i +=3) {
  354. reg = registers[i + 0];
  355. and_mask = registers[i + 1];
  356. or_mask = registers[i + 2];
  357. if (and_mask == 0xffffffff) {
  358. tmp = or_mask;
  359. } else {
  360. tmp = RREG32(reg);
  361. tmp &= ~and_mask;
  362. tmp |= or_mask;
  363. }
  364. WREG32(reg, tmp);
  365. }
  366. }
  367. void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
  368. {
  369. pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
  370. }
  371. /*
  372. * GPU doorbell aperture helpers function.
  373. */
  374. /**
  375. * amdgpu_device_doorbell_init - Init doorbell driver information.
  376. *
  377. * @adev: amdgpu_device pointer
  378. *
  379. * Init doorbell driver information (CIK)
  380. * Returns 0 on success, error on failure.
  381. */
  382. static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
  383. {
  384. /* No doorbell on SI hardware generation */
  385. if (adev->asic_type < CHIP_BONAIRE) {
  386. adev->doorbell.base = 0;
  387. adev->doorbell.size = 0;
  388. adev->doorbell.num_doorbells = 0;
  389. adev->doorbell.ptr = NULL;
  390. return 0;
  391. }
  392. if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
  393. return -EINVAL;
  394. /* doorbell bar mapping */
  395. adev->doorbell.base = pci_resource_start(adev->pdev, 2);
  396. adev->doorbell.size = pci_resource_len(adev->pdev, 2);
  397. adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
  398. AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
  399. if (adev->doorbell.num_doorbells == 0)
  400. return -EINVAL;
  401. adev->doorbell.ptr = ioremap(adev->doorbell.base,
  402. adev->doorbell.num_doorbells *
  403. sizeof(u32));
  404. if (adev->doorbell.ptr == NULL)
  405. return -ENOMEM;
  406. return 0;
  407. }
  408. /**
  409. * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
  410. *
  411. * @adev: amdgpu_device pointer
  412. *
  413. * Tear down doorbell driver information (CIK)
  414. */
  415. static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
  416. {
  417. iounmap(adev->doorbell.ptr);
  418. adev->doorbell.ptr = NULL;
  419. }
  420. /*
  421. * amdgpu_device_wb_*()
  422. * Writeback is the method by which the GPU updates special pages in memory
  423. * with the status of certain GPU events (fences, ring pointers,etc.).
  424. */
  425. /**
  426. * amdgpu_device_wb_fini - Disable Writeback and free memory
  427. *
  428. * @adev: amdgpu_device pointer
  429. *
  430. * Disables Writeback and frees the Writeback memory (all asics).
  431. * Used at driver shutdown.
  432. */
  433. static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
  434. {
  435. if (adev->wb.wb_obj) {
  436. amdgpu_bo_free_kernel(&adev->wb.wb_obj,
  437. &adev->wb.gpu_addr,
  438. (void **)&adev->wb.wb);
  439. adev->wb.wb_obj = NULL;
  440. }
  441. }
  442. /**
  443. * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
  444. *
  445. * @adev: amdgpu_device pointer
  446. *
  447. * Initializes writeback and allocates writeback memory (all asics).
  448. * Used at driver startup.
  449. * Returns 0 on success or an -error on failure.
  450. */
  451. static int amdgpu_device_wb_init(struct amdgpu_device *adev)
  452. {
  453. int r;
  454. if (adev->wb.wb_obj == NULL) {
  455. /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
  456. r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
  457. PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
  458. &adev->wb.wb_obj, &adev->wb.gpu_addr,
  459. (void **)&adev->wb.wb);
  460. if (r) {
  461. dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
  462. return r;
  463. }
  464. adev->wb.num_wb = AMDGPU_MAX_WB;
  465. memset(&adev->wb.used, 0, sizeof(adev->wb.used));
  466. /* clear wb memory */
  467. memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
  468. }
  469. return 0;
  470. }
  471. /**
  472. * amdgpu_device_wb_get - Allocate a wb entry
  473. *
  474. * @adev: amdgpu_device pointer
  475. * @wb: wb index
  476. *
  477. * Allocate a wb slot for use by the driver (all asics).
  478. * Returns 0 on success or -EINVAL on failure.
  479. */
  480. int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
  481. {
  482. unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
  483. if (offset < adev->wb.num_wb) {
  484. __set_bit(offset, adev->wb.used);
  485. *wb = offset << 3; /* convert to dw offset */
  486. return 0;
  487. } else {
  488. return -EINVAL;
  489. }
  490. }
  491. /**
  492. * amdgpu_device_wb_free - Free a wb entry
  493. *
  494. * @adev: amdgpu_device pointer
  495. * @wb: wb index
  496. *
  497. * Free a wb slot allocated for use by the driver (all asics)
  498. */
  499. void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
  500. {
  501. wb >>= 3;
  502. if (wb < adev->wb.num_wb)
  503. __clear_bit(wb, adev->wb.used);
  504. }
  505. /**
  506. * amdgpu_device_vram_location - try to find VRAM location
  507. * @adev: amdgpu device structure holding all necessary informations
  508. * @mc: memory controller structure holding memory informations
  509. * @base: base address at which to put VRAM
  510. *
  511. * Function will try to place VRAM at base address provided
  512. * as parameter.
  513. */
  514. void amdgpu_device_vram_location(struct amdgpu_device *adev,
  515. struct amdgpu_gmc *mc, u64 base)
  516. {
  517. uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
  518. mc->vram_start = base;
  519. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  520. if (limit && limit < mc->real_vram_size)
  521. mc->real_vram_size = limit;
  522. dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  523. mc->mc_vram_size >> 20, mc->vram_start,
  524. mc->vram_end, mc->real_vram_size >> 20);
  525. }
  526. /**
  527. * amdgpu_device_gart_location - try to find GTT location
  528. * @adev: amdgpu device structure holding all necessary informations
  529. * @mc: memory controller structure holding memory informations
  530. *
  531. * Function will place try to place GTT before or after VRAM.
  532. *
  533. * If GTT size is bigger than space left then we ajust GTT size.
  534. * Thus function will never fails.
  535. *
  536. * FIXME: when reducing GTT size align new size on power of 2.
  537. */
  538. void amdgpu_device_gart_location(struct amdgpu_device *adev,
  539. struct amdgpu_gmc *mc)
  540. {
  541. u64 size_af, size_bf;
  542. size_af = adev->gmc.mc_mask - mc->vram_end;
  543. size_bf = mc->vram_start;
  544. if (size_bf > size_af) {
  545. if (mc->gart_size > size_bf) {
  546. dev_warn(adev->dev, "limiting GTT\n");
  547. mc->gart_size = size_bf;
  548. }
  549. mc->gart_start = 0;
  550. } else {
  551. if (mc->gart_size > size_af) {
  552. dev_warn(adev->dev, "limiting GTT\n");
  553. mc->gart_size = size_af;
  554. }
  555. /* VCE doesn't like it when BOs cross a 4GB segment, so align
  556. * the GART base on a 4GB boundary as well.
  557. */
  558. mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
  559. }
  560. mc->gart_end = mc->gart_start + mc->gart_size - 1;
  561. dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  562. mc->gart_size >> 20, mc->gart_start, mc->gart_end);
  563. }
  564. /**
  565. * amdgpu_device_resize_fb_bar - try to resize FB BAR
  566. *
  567. * @adev: amdgpu_device pointer
  568. *
  569. * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
  570. * to fail, but if any of the BARs is not accessible after the size we abort
  571. * driver loading by returning -ENODEV.
  572. */
  573. int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
  574. {
  575. u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
  576. u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
  577. struct pci_bus *root;
  578. struct resource *res;
  579. unsigned i;
  580. u16 cmd;
  581. int r;
  582. /* Bypass for VF */
  583. if (amdgpu_sriov_vf(adev))
  584. return 0;
  585. /* Check if the root BUS has 64bit memory resources */
  586. root = adev->pdev->bus;
  587. while (root->parent)
  588. root = root->parent;
  589. pci_bus_for_each_resource(root, res, i) {
  590. if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
  591. res->start > 0x100000000ull)
  592. break;
  593. }
  594. /* Trying to resize is pointless without a root hub window above 4GB */
  595. if (!res)
  596. return 0;
  597. /* Disable memory decoding while we change the BAR addresses and size */
  598. pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
  599. pci_write_config_word(adev->pdev, PCI_COMMAND,
  600. cmd & ~PCI_COMMAND_MEMORY);
  601. /* Free the VRAM and doorbell BAR, we most likely need to move both. */
  602. amdgpu_device_doorbell_fini(adev);
  603. if (adev->asic_type >= CHIP_BONAIRE)
  604. pci_release_resource(adev->pdev, 2);
  605. pci_release_resource(adev->pdev, 0);
  606. r = pci_resize_resource(adev->pdev, 0, rbar_size);
  607. if (r == -ENOSPC)
  608. DRM_INFO("Not enough PCI address space for a large BAR.");
  609. else if (r && r != -ENOTSUPP)
  610. DRM_ERROR("Problem resizing BAR0 (%d).", r);
  611. pci_assign_unassigned_bus_resources(adev->pdev->bus);
  612. /* When the doorbell or fb BAR isn't available we have no chance of
  613. * using the device.
  614. */
  615. r = amdgpu_device_doorbell_init(adev);
  616. if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
  617. return -ENODEV;
  618. pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
  619. return 0;
  620. }
  621. /*
  622. * GPU helpers function.
  623. */
  624. /**
  625. * amdgpu_device_need_post - check if the hw need post or not
  626. *
  627. * @adev: amdgpu_device pointer
  628. *
  629. * Check if the asic has been initialized (all asics) at driver startup
  630. * or post is needed if hw reset is performed.
  631. * Returns true if need or false if not.
  632. */
  633. bool amdgpu_device_need_post(struct amdgpu_device *adev)
  634. {
  635. uint32_t reg;
  636. if (amdgpu_sriov_vf(adev))
  637. return false;
  638. if (amdgpu_passthrough(adev)) {
  639. /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
  640. * some old smc fw still need driver do vPost otherwise gpu hang, while
  641. * those smc fw version above 22.15 doesn't have this flaw, so we force
  642. * vpost executed for smc version below 22.15
  643. */
  644. if (adev->asic_type == CHIP_FIJI) {
  645. int err;
  646. uint32_t fw_ver;
  647. err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
  648. /* force vPost if error occured */
  649. if (err)
  650. return true;
  651. fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
  652. if (fw_ver < 0x00160e00)
  653. return true;
  654. }
  655. }
  656. if (adev->has_hw_reset) {
  657. adev->has_hw_reset = false;
  658. return true;
  659. }
  660. /* bios scratch used on CIK+ */
  661. if (adev->asic_type >= CHIP_BONAIRE)
  662. return amdgpu_atombios_scratch_need_asic_init(adev);
  663. /* check MEM_SIZE for older asics */
  664. reg = amdgpu_asic_get_config_memsize(adev);
  665. if ((reg != 0) && (reg != 0xffffffff))
  666. return false;
  667. return true;
  668. }
  669. /* if we get transitioned to only one device, take VGA back */
  670. /**
  671. * amdgpu_device_vga_set_decode - enable/disable vga decode
  672. *
  673. * @cookie: amdgpu_device pointer
  674. * @state: enable/disable vga decode
  675. *
  676. * Enable/disable vga decode (all asics).
  677. * Returns VGA resource flags.
  678. */
  679. static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
  680. {
  681. struct amdgpu_device *adev = cookie;
  682. amdgpu_asic_set_vga_state(adev, state);
  683. if (state)
  684. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  685. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  686. else
  687. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  688. }
  689. static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
  690. {
  691. /* defines number of bits in page table versus page directory,
  692. * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
  693. * page table and the remaining bits are in the page directory */
  694. if (amdgpu_vm_block_size == -1)
  695. return;
  696. if (amdgpu_vm_block_size < 9) {
  697. dev_warn(adev->dev, "VM page table size (%d) too small\n",
  698. amdgpu_vm_block_size);
  699. amdgpu_vm_block_size = -1;
  700. }
  701. }
  702. static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
  703. {
  704. /* no need to check the default value */
  705. if (amdgpu_vm_size == -1)
  706. return;
  707. if (amdgpu_vm_size < 1) {
  708. dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
  709. amdgpu_vm_size);
  710. amdgpu_vm_size = -1;
  711. }
  712. }
  713. /**
  714. * amdgpu_device_check_arguments - validate module params
  715. *
  716. * @adev: amdgpu_device pointer
  717. *
  718. * Validates certain module parameters and updates
  719. * the associated values used by the driver (all asics).
  720. */
  721. static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
  722. {
  723. if (amdgpu_sched_jobs < 4) {
  724. dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
  725. amdgpu_sched_jobs);
  726. amdgpu_sched_jobs = 4;
  727. } else if (!is_power_of_2(amdgpu_sched_jobs)){
  728. dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
  729. amdgpu_sched_jobs);
  730. amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
  731. }
  732. if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
  733. /* gart size must be greater or equal to 32M */
  734. dev_warn(adev->dev, "gart size (%d) too small\n",
  735. amdgpu_gart_size);
  736. amdgpu_gart_size = -1;
  737. }
  738. if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
  739. /* gtt size must be greater or equal to 32M */
  740. dev_warn(adev->dev, "gtt size (%d) too small\n",
  741. amdgpu_gtt_size);
  742. amdgpu_gtt_size = -1;
  743. }
  744. /* valid range is between 4 and 9 inclusive */
  745. if (amdgpu_vm_fragment_size != -1 &&
  746. (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
  747. dev_warn(adev->dev, "valid range is between 4 and 9\n");
  748. amdgpu_vm_fragment_size = -1;
  749. }
  750. amdgpu_device_check_vm_size(adev);
  751. amdgpu_device_check_block_size(adev);
  752. if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
  753. !is_power_of_2(amdgpu_vram_page_split))) {
  754. dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
  755. amdgpu_vram_page_split);
  756. amdgpu_vram_page_split = 1024;
  757. }
  758. if (amdgpu_lockup_timeout == 0) {
  759. dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
  760. amdgpu_lockup_timeout = 10000;
  761. }
  762. adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
  763. }
  764. /**
  765. * amdgpu_switcheroo_set_state - set switcheroo state
  766. *
  767. * @pdev: pci dev pointer
  768. * @state: vga_switcheroo state
  769. *
  770. * Callback for the switcheroo driver. Suspends or resumes the
  771. * the asics before or after it is powered up using ACPI methods.
  772. */
  773. static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  774. {
  775. struct drm_device *dev = pci_get_drvdata(pdev);
  776. if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
  777. return;
  778. if (state == VGA_SWITCHEROO_ON) {
  779. pr_info("amdgpu: switched on\n");
  780. /* don't suspend or resume card normally */
  781. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  782. amdgpu_device_resume(dev, true, true);
  783. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  784. drm_kms_helper_poll_enable(dev);
  785. } else {
  786. pr_info("amdgpu: switched off\n");
  787. drm_kms_helper_poll_disable(dev);
  788. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  789. amdgpu_device_suspend(dev, true, true);
  790. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  791. }
  792. }
  793. /**
  794. * amdgpu_switcheroo_can_switch - see if switcheroo state can change
  795. *
  796. * @pdev: pci dev pointer
  797. *
  798. * Callback for the switcheroo driver. Check of the switcheroo
  799. * state can be changed.
  800. * Returns true if the state can be changed, false if not.
  801. */
  802. static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
  803. {
  804. struct drm_device *dev = pci_get_drvdata(pdev);
  805. /*
  806. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  807. * locking inversion with the driver load path. And the access here is
  808. * completely racy anyway. So don't bother with locking for now.
  809. */
  810. return dev->open_count == 0;
  811. }
  812. static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
  813. .set_gpu_state = amdgpu_switcheroo_set_state,
  814. .reprobe = NULL,
  815. .can_switch = amdgpu_switcheroo_can_switch,
  816. };
  817. int amdgpu_device_ip_set_clockgating_state(struct amdgpu_device *adev,
  818. enum amd_ip_block_type block_type,
  819. enum amd_clockgating_state state)
  820. {
  821. int i, r = 0;
  822. for (i = 0; i < adev->num_ip_blocks; i++) {
  823. if (!adev->ip_blocks[i].status.valid)
  824. continue;
  825. if (adev->ip_blocks[i].version->type != block_type)
  826. continue;
  827. if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
  828. continue;
  829. r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
  830. (void *)adev, state);
  831. if (r)
  832. DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
  833. adev->ip_blocks[i].version->funcs->name, r);
  834. }
  835. return r;
  836. }
  837. int amdgpu_device_ip_set_powergating_state(struct amdgpu_device *adev,
  838. enum amd_ip_block_type block_type,
  839. enum amd_powergating_state state)
  840. {
  841. int i, r = 0;
  842. for (i = 0; i < adev->num_ip_blocks; i++) {
  843. if (!adev->ip_blocks[i].status.valid)
  844. continue;
  845. if (adev->ip_blocks[i].version->type != block_type)
  846. continue;
  847. if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
  848. continue;
  849. r = adev->ip_blocks[i].version->funcs->set_powergating_state(
  850. (void *)adev, state);
  851. if (r)
  852. DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
  853. adev->ip_blocks[i].version->funcs->name, r);
  854. }
  855. return r;
  856. }
  857. void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
  858. u32 *flags)
  859. {
  860. int i;
  861. for (i = 0; i < adev->num_ip_blocks; i++) {
  862. if (!adev->ip_blocks[i].status.valid)
  863. continue;
  864. if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
  865. adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
  866. }
  867. }
  868. int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
  869. enum amd_ip_block_type block_type)
  870. {
  871. int i, r;
  872. for (i = 0; i < adev->num_ip_blocks; i++) {
  873. if (!adev->ip_blocks[i].status.valid)
  874. continue;
  875. if (adev->ip_blocks[i].version->type == block_type) {
  876. r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
  877. if (r)
  878. return r;
  879. break;
  880. }
  881. }
  882. return 0;
  883. }
  884. bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
  885. enum amd_ip_block_type block_type)
  886. {
  887. int i;
  888. for (i = 0; i < adev->num_ip_blocks; i++) {
  889. if (!adev->ip_blocks[i].status.valid)
  890. continue;
  891. if (adev->ip_blocks[i].version->type == block_type)
  892. return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
  893. }
  894. return true;
  895. }
  896. struct amdgpu_ip_block *
  897. amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
  898. enum amd_ip_block_type type)
  899. {
  900. int i;
  901. for (i = 0; i < adev->num_ip_blocks; i++)
  902. if (adev->ip_blocks[i].version->type == type)
  903. return &adev->ip_blocks[i];
  904. return NULL;
  905. }
  906. /**
  907. * amdgpu_device_ip_block_version_cmp
  908. *
  909. * @adev: amdgpu_device pointer
  910. * @type: enum amd_ip_block_type
  911. * @major: major version
  912. * @minor: minor version
  913. *
  914. * return 0 if equal or greater
  915. * return 1 if smaller or the ip_block doesn't exist
  916. */
  917. int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
  918. enum amd_ip_block_type type,
  919. u32 major, u32 minor)
  920. {
  921. struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
  922. if (ip_block && ((ip_block->version->major > major) ||
  923. ((ip_block->version->major == major) &&
  924. (ip_block->version->minor >= minor))))
  925. return 0;
  926. return 1;
  927. }
  928. /**
  929. * amdgpu_device_ip_block_add
  930. *
  931. * @adev: amdgpu_device pointer
  932. * @ip_block_version: pointer to the IP to add
  933. *
  934. * Adds the IP block driver information to the collection of IPs
  935. * on the asic.
  936. */
  937. int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
  938. const struct amdgpu_ip_block_version *ip_block_version)
  939. {
  940. if (!ip_block_version)
  941. return -EINVAL;
  942. DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
  943. ip_block_version->funcs->name);
  944. adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
  945. return 0;
  946. }
  947. static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
  948. {
  949. adev->enable_virtual_display = false;
  950. if (amdgpu_virtual_display) {
  951. struct drm_device *ddev = adev->ddev;
  952. const char *pci_address_name = pci_name(ddev->pdev);
  953. char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
  954. pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
  955. pciaddstr_tmp = pciaddstr;
  956. while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
  957. pciaddname = strsep(&pciaddname_tmp, ",");
  958. if (!strcmp("all", pciaddname)
  959. || !strcmp(pci_address_name, pciaddname)) {
  960. long num_crtc;
  961. int res = -1;
  962. adev->enable_virtual_display = true;
  963. if (pciaddname_tmp)
  964. res = kstrtol(pciaddname_tmp, 10,
  965. &num_crtc);
  966. if (!res) {
  967. if (num_crtc < 1)
  968. num_crtc = 1;
  969. if (num_crtc > 6)
  970. num_crtc = 6;
  971. adev->mode_info.num_crtc = num_crtc;
  972. } else {
  973. adev->mode_info.num_crtc = 1;
  974. }
  975. break;
  976. }
  977. }
  978. DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
  979. amdgpu_virtual_display, pci_address_name,
  980. adev->enable_virtual_display, adev->mode_info.num_crtc);
  981. kfree(pciaddstr);
  982. }
  983. }
  984. static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
  985. {
  986. const char *chip_name;
  987. char fw_name[30];
  988. int err;
  989. const struct gpu_info_firmware_header_v1_0 *hdr;
  990. adev->firmware.gpu_info_fw = NULL;
  991. switch (adev->asic_type) {
  992. case CHIP_TOPAZ:
  993. case CHIP_TONGA:
  994. case CHIP_FIJI:
  995. case CHIP_POLARIS11:
  996. case CHIP_POLARIS10:
  997. case CHIP_POLARIS12:
  998. case CHIP_CARRIZO:
  999. case CHIP_STONEY:
  1000. #ifdef CONFIG_DRM_AMDGPU_SI
  1001. case CHIP_VERDE:
  1002. case CHIP_TAHITI:
  1003. case CHIP_PITCAIRN:
  1004. case CHIP_OLAND:
  1005. case CHIP_HAINAN:
  1006. #endif
  1007. #ifdef CONFIG_DRM_AMDGPU_CIK
  1008. case CHIP_BONAIRE:
  1009. case CHIP_HAWAII:
  1010. case CHIP_KAVERI:
  1011. case CHIP_KABINI:
  1012. case CHIP_MULLINS:
  1013. #endif
  1014. default:
  1015. return 0;
  1016. case CHIP_VEGA10:
  1017. chip_name = "vega10";
  1018. break;
  1019. case CHIP_RAVEN:
  1020. chip_name = "raven";
  1021. break;
  1022. }
  1023. snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
  1024. err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
  1025. if (err) {
  1026. dev_err(adev->dev,
  1027. "Failed to load gpu_info firmware \"%s\"\n",
  1028. fw_name);
  1029. goto out;
  1030. }
  1031. err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
  1032. if (err) {
  1033. dev_err(adev->dev,
  1034. "Failed to validate gpu_info firmware \"%s\"\n",
  1035. fw_name);
  1036. goto out;
  1037. }
  1038. hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
  1039. amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
  1040. switch (hdr->version_major) {
  1041. case 1:
  1042. {
  1043. const struct gpu_info_firmware_v1_0 *gpu_info_fw =
  1044. (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
  1045. le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  1046. adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
  1047. adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
  1048. adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
  1049. adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
  1050. adev->gfx.config.max_texture_channel_caches =
  1051. le32_to_cpu(gpu_info_fw->gc_num_tccs);
  1052. adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
  1053. adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
  1054. adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
  1055. adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
  1056. adev->gfx.config.double_offchip_lds_buf =
  1057. le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
  1058. adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
  1059. adev->gfx.cu_info.max_waves_per_simd =
  1060. le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
  1061. adev->gfx.cu_info.max_scratch_slots_per_cu =
  1062. le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
  1063. adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
  1064. break;
  1065. }
  1066. default:
  1067. dev_err(adev->dev,
  1068. "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
  1069. err = -EINVAL;
  1070. goto out;
  1071. }
  1072. out:
  1073. return err;
  1074. }
  1075. static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
  1076. {
  1077. int i, r;
  1078. amdgpu_device_enable_virtual_display(adev);
  1079. switch (adev->asic_type) {
  1080. case CHIP_TOPAZ:
  1081. case CHIP_TONGA:
  1082. case CHIP_FIJI:
  1083. case CHIP_POLARIS11:
  1084. case CHIP_POLARIS10:
  1085. case CHIP_POLARIS12:
  1086. case CHIP_CARRIZO:
  1087. case CHIP_STONEY:
  1088. if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
  1089. adev->family = AMDGPU_FAMILY_CZ;
  1090. else
  1091. adev->family = AMDGPU_FAMILY_VI;
  1092. r = vi_set_ip_blocks(adev);
  1093. if (r)
  1094. return r;
  1095. break;
  1096. #ifdef CONFIG_DRM_AMDGPU_SI
  1097. case CHIP_VERDE:
  1098. case CHIP_TAHITI:
  1099. case CHIP_PITCAIRN:
  1100. case CHIP_OLAND:
  1101. case CHIP_HAINAN:
  1102. adev->family = AMDGPU_FAMILY_SI;
  1103. r = si_set_ip_blocks(adev);
  1104. if (r)
  1105. return r;
  1106. break;
  1107. #endif
  1108. #ifdef CONFIG_DRM_AMDGPU_CIK
  1109. case CHIP_BONAIRE:
  1110. case CHIP_HAWAII:
  1111. case CHIP_KAVERI:
  1112. case CHIP_KABINI:
  1113. case CHIP_MULLINS:
  1114. if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
  1115. adev->family = AMDGPU_FAMILY_CI;
  1116. else
  1117. adev->family = AMDGPU_FAMILY_KV;
  1118. r = cik_set_ip_blocks(adev);
  1119. if (r)
  1120. return r;
  1121. break;
  1122. #endif
  1123. case CHIP_VEGA10:
  1124. case CHIP_RAVEN:
  1125. if (adev->asic_type == CHIP_RAVEN)
  1126. adev->family = AMDGPU_FAMILY_RV;
  1127. else
  1128. adev->family = AMDGPU_FAMILY_AI;
  1129. r = soc15_set_ip_blocks(adev);
  1130. if (r)
  1131. return r;
  1132. break;
  1133. default:
  1134. /* FIXME: not supported yet */
  1135. return -EINVAL;
  1136. }
  1137. r = amdgpu_device_parse_gpu_info_fw(adev);
  1138. if (r)
  1139. return r;
  1140. amdgpu_amdkfd_device_probe(adev);
  1141. if (amdgpu_sriov_vf(adev)) {
  1142. r = amdgpu_virt_request_full_gpu(adev, true);
  1143. if (r)
  1144. return -EAGAIN;
  1145. }
  1146. for (i = 0; i < adev->num_ip_blocks; i++) {
  1147. if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
  1148. DRM_ERROR("disabled ip block: %d <%s>\n",
  1149. i, adev->ip_blocks[i].version->funcs->name);
  1150. adev->ip_blocks[i].status.valid = false;
  1151. } else {
  1152. if (adev->ip_blocks[i].version->funcs->early_init) {
  1153. r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
  1154. if (r == -ENOENT) {
  1155. adev->ip_blocks[i].status.valid = false;
  1156. } else if (r) {
  1157. DRM_ERROR("early_init of IP block <%s> failed %d\n",
  1158. adev->ip_blocks[i].version->funcs->name, r);
  1159. return r;
  1160. } else {
  1161. adev->ip_blocks[i].status.valid = true;
  1162. }
  1163. } else {
  1164. adev->ip_blocks[i].status.valid = true;
  1165. }
  1166. }
  1167. }
  1168. adev->cg_flags &= amdgpu_cg_mask;
  1169. adev->pg_flags &= amdgpu_pg_mask;
  1170. return 0;
  1171. }
  1172. static int amdgpu_device_ip_init(struct amdgpu_device *adev)
  1173. {
  1174. int i, r;
  1175. for (i = 0; i < adev->num_ip_blocks; i++) {
  1176. if (!adev->ip_blocks[i].status.valid)
  1177. continue;
  1178. r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
  1179. if (r) {
  1180. DRM_ERROR("sw_init of IP block <%s> failed %d\n",
  1181. adev->ip_blocks[i].version->funcs->name, r);
  1182. return r;
  1183. }
  1184. adev->ip_blocks[i].status.sw = true;
  1185. /* need to do gmc hw init early so we can allocate gpu mem */
  1186. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1187. r = amdgpu_device_vram_scratch_init(adev);
  1188. if (r) {
  1189. DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
  1190. return r;
  1191. }
  1192. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1193. if (r) {
  1194. DRM_ERROR("hw_init %d failed %d\n", i, r);
  1195. return r;
  1196. }
  1197. r = amdgpu_device_wb_init(adev);
  1198. if (r) {
  1199. DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
  1200. return r;
  1201. }
  1202. adev->ip_blocks[i].status.hw = true;
  1203. /* right after GMC hw init, we create CSA */
  1204. if (amdgpu_sriov_vf(adev)) {
  1205. r = amdgpu_allocate_static_csa(adev);
  1206. if (r) {
  1207. DRM_ERROR("allocate CSA failed %d\n", r);
  1208. return r;
  1209. }
  1210. }
  1211. }
  1212. }
  1213. for (i = 0; i < adev->num_ip_blocks; i++) {
  1214. if (!adev->ip_blocks[i].status.sw)
  1215. continue;
  1216. if (adev->ip_blocks[i].status.hw)
  1217. continue;
  1218. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1219. if (r) {
  1220. DRM_ERROR("hw_init of IP block <%s> failed %d\n",
  1221. adev->ip_blocks[i].version->funcs->name, r);
  1222. return r;
  1223. }
  1224. adev->ip_blocks[i].status.hw = true;
  1225. }
  1226. amdgpu_amdkfd_device_init(adev);
  1227. if (amdgpu_sriov_vf(adev))
  1228. amdgpu_virt_release_full_gpu(adev, true);
  1229. return 0;
  1230. }
  1231. static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
  1232. {
  1233. memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
  1234. }
  1235. static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
  1236. {
  1237. return !!memcmp(adev->gart.ptr, adev->reset_magic,
  1238. AMDGPU_RESET_MAGIC_NUM);
  1239. }
  1240. static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
  1241. {
  1242. int i = 0, r;
  1243. if (amdgpu_emu_mode == 1)
  1244. return 0;
  1245. for (i = 0; i < adev->num_ip_blocks; i++) {
  1246. if (!adev->ip_blocks[i].status.valid)
  1247. continue;
  1248. /* skip CG for VCE/UVD, it's handled specially */
  1249. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1250. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
  1251. /* enable clockgating to save power */
  1252. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1253. AMD_CG_STATE_GATE);
  1254. if (r) {
  1255. DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
  1256. adev->ip_blocks[i].version->funcs->name, r);
  1257. return r;
  1258. }
  1259. }
  1260. }
  1261. return 0;
  1262. }
  1263. static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
  1264. {
  1265. int i = 0, r;
  1266. for (i = 0; i < adev->num_ip_blocks; i++) {
  1267. if (!adev->ip_blocks[i].status.valid)
  1268. continue;
  1269. if (adev->ip_blocks[i].version->funcs->late_init) {
  1270. r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
  1271. if (r) {
  1272. DRM_ERROR("late_init of IP block <%s> failed %d\n",
  1273. adev->ip_blocks[i].version->funcs->name, r);
  1274. return r;
  1275. }
  1276. adev->ip_blocks[i].status.late_initialized = true;
  1277. }
  1278. }
  1279. mod_delayed_work(system_wq, &adev->late_init_work,
  1280. msecs_to_jiffies(AMDGPU_RESUME_MS));
  1281. amdgpu_device_fill_reset_magic(adev);
  1282. return 0;
  1283. }
  1284. static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
  1285. {
  1286. int i, r;
  1287. amdgpu_amdkfd_device_fini(adev);
  1288. /* need to disable SMC first */
  1289. for (i = 0; i < adev->num_ip_blocks; i++) {
  1290. if (!adev->ip_blocks[i].status.hw)
  1291. continue;
  1292. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
  1293. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1294. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1295. AMD_CG_STATE_UNGATE);
  1296. if (r) {
  1297. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1298. adev->ip_blocks[i].version->funcs->name, r);
  1299. return r;
  1300. }
  1301. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1302. /* XXX handle errors */
  1303. if (r) {
  1304. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1305. adev->ip_blocks[i].version->funcs->name, r);
  1306. }
  1307. adev->ip_blocks[i].status.hw = false;
  1308. break;
  1309. }
  1310. }
  1311. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1312. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC &&
  1313. adev->firmware.load_type == AMDGPU_FW_LOAD_SMU)
  1314. amdgpu_ucode_fini_bo(adev);
  1315. if (!adev->ip_blocks[i].status.hw)
  1316. continue;
  1317. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1318. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
  1319. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1320. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1321. AMD_CG_STATE_UNGATE);
  1322. if (r) {
  1323. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1324. adev->ip_blocks[i].version->funcs->name, r);
  1325. return r;
  1326. }
  1327. }
  1328. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1329. /* XXX handle errors */
  1330. if (r) {
  1331. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1332. adev->ip_blocks[i].version->funcs->name, r);
  1333. }
  1334. adev->ip_blocks[i].status.hw = false;
  1335. }
  1336. /* disable all interrupts */
  1337. amdgpu_irq_disable_all(adev);
  1338. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1339. if (!adev->ip_blocks[i].status.sw)
  1340. continue;
  1341. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1342. amdgpu_free_static_csa(adev);
  1343. amdgpu_device_wb_fini(adev);
  1344. amdgpu_device_vram_scratch_fini(adev);
  1345. }
  1346. r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
  1347. /* XXX handle errors */
  1348. if (r) {
  1349. DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
  1350. adev->ip_blocks[i].version->funcs->name, r);
  1351. }
  1352. adev->ip_blocks[i].status.sw = false;
  1353. adev->ip_blocks[i].status.valid = false;
  1354. }
  1355. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1356. if (!adev->ip_blocks[i].status.late_initialized)
  1357. continue;
  1358. if (adev->ip_blocks[i].version->funcs->late_fini)
  1359. adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
  1360. adev->ip_blocks[i].status.late_initialized = false;
  1361. }
  1362. if (amdgpu_sriov_vf(adev))
  1363. if (amdgpu_virt_release_full_gpu(adev, false))
  1364. DRM_ERROR("failed to release exclusive mode on fini\n");
  1365. return 0;
  1366. }
  1367. static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
  1368. {
  1369. struct amdgpu_device *adev =
  1370. container_of(work, struct amdgpu_device, late_init_work.work);
  1371. amdgpu_device_ip_late_set_cg_state(adev);
  1372. }
  1373. int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
  1374. {
  1375. int i, r;
  1376. if (amdgpu_sriov_vf(adev))
  1377. amdgpu_virt_request_full_gpu(adev, false);
  1378. /* ungate SMC block first */
  1379. r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
  1380. AMD_CG_STATE_UNGATE);
  1381. if (r) {
  1382. DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
  1383. }
  1384. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1385. if (!adev->ip_blocks[i].status.valid)
  1386. continue;
  1387. /* ungate blocks so that suspend can properly shut them down */
  1388. if (i != AMD_IP_BLOCK_TYPE_SMC) {
  1389. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1390. AMD_CG_STATE_UNGATE);
  1391. if (r) {
  1392. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1393. adev->ip_blocks[i].version->funcs->name, r);
  1394. }
  1395. }
  1396. /* XXX handle errors */
  1397. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1398. /* XXX handle errors */
  1399. if (r) {
  1400. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1401. adev->ip_blocks[i].version->funcs->name, r);
  1402. }
  1403. }
  1404. if (amdgpu_sriov_vf(adev))
  1405. amdgpu_virt_release_full_gpu(adev, false);
  1406. return 0;
  1407. }
  1408. static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
  1409. {
  1410. int i, r;
  1411. static enum amd_ip_block_type ip_order[] = {
  1412. AMD_IP_BLOCK_TYPE_GMC,
  1413. AMD_IP_BLOCK_TYPE_COMMON,
  1414. AMD_IP_BLOCK_TYPE_IH,
  1415. };
  1416. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1417. int j;
  1418. struct amdgpu_ip_block *block;
  1419. for (j = 0; j < adev->num_ip_blocks; j++) {
  1420. block = &adev->ip_blocks[j];
  1421. if (block->version->type != ip_order[i] ||
  1422. !block->status.valid)
  1423. continue;
  1424. r = block->version->funcs->hw_init(adev);
  1425. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
  1426. if (r)
  1427. return r;
  1428. }
  1429. }
  1430. return 0;
  1431. }
  1432. static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
  1433. {
  1434. int i, r;
  1435. static enum amd_ip_block_type ip_order[] = {
  1436. AMD_IP_BLOCK_TYPE_SMC,
  1437. AMD_IP_BLOCK_TYPE_PSP,
  1438. AMD_IP_BLOCK_TYPE_DCE,
  1439. AMD_IP_BLOCK_TYPE_GFX,
  1440. AMD_IP_BLOCK_TYPE_SDMA,
  1441. AMD_IP_BLOCK_TYPE_UVD,
  1442. AMD_IP_BLOCK_TYPE_VCE
  1443. };
  1444. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1445. int j;
  1446. struct amdgpu_ip_block *block;
  1447. for (j = 0; j < adev->num_ip_blocks; j++) {
  1448. block = &adev->ip_blocks[j];
  1449. if (block->version->type != ip_order[i] ||
  1450. !block->status.valid)
  1451. continue;
  1452. r = block->version->funcs->hw_init(adev);
  1453. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
  1454. if (r)
  1455. return r;
  1456. }
  1457. }
  1458. return 0;
  1459. }
  1460. static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
  1461. {
  1462. int i, r;
  1463. for (i = 0; i < adev->num_ip_blocks; i++) {
  1464. if (!adev->ip_blocks[i].status.valid)
  1465. continue;
  1466. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1467. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1468. adev->ip_blocks[i].version->type ==
  1469. AMD_IP_BLOCK_TYPE_IH) {
  1470. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1471. if (r) {
  1472. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1473. adev->ip_blocks[i].version->funcs->name, r);
  1474. return r;
  1475. }
  1476. }
  1477. }
  1478. return 0;
  1479. }
  1480. static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
  1481. {
  1482. int i, r;
  1483. for (i = 0; i < adev->num_ip_blocks; i++) {
  1484. if (!adev->ip_blocks[i].status.valid)
  1485. continue;
  1486. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1487. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1488. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
  1489. continue;
  1490. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1491. if (r) {
  1492. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1493. adev->ip_blocks[i].version->funcs->name, r);
  1494. return r;
  1495. }
  1496. }
  1497. return 0;
  1498. }
  1499. static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
  1500. {
  1501. int r;
  1502. r = amdgpu_device_ip_resume_phase1(adev);
  1503. if (r)
  1504. return r;
  1505. r = amdgpu_device_ip_resume_phase2(adev);
  1506. return r;
  1507. }
  1508. static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
  1509. {
  1510. if (amdgpu_sriov_vf(adev)) {
  1511. if (adev->is_atom_fw) {
  1512. if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
  1513. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1514. } else {
  1515. if (amdgpu_atombios_has_gpu_virtualization_table(adev))
  1516. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1517. }
  1518. if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
  1519. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
  1520. }
  1521. }
  1522. bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
  1523. {
  1524. switch (asic_type) {
  1525. #if defined(CONFIG_DRM_AMD_DC)
  1526. case CHIP_BONAIRE:
  1527. case CHIP_HAWAII:
  1528. case CHIP_KAVERI:
  1529. case CHIP_KABINI:
  1530. case CHIP_MULLINS:
  1531. case CHIP_CARRIZO:
  1532. case CHIP_STONEY:
  1533. case CHIP_POLARIS11:
  1534. case CHIP_POLARIS10:
  1535. case CHIP_POLARIS12:
  1536. case CHIP_TONGA:
  1537. case CHIP_FIJI:
  1538. #if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
  1539. return amdgpu_dc != 0;
  1540. #endif
  1541. case CHIP_VEGA10:
  1542. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  1543. case CHIP_RAVEN:
  1544. #endif
  1545. return amdgpu_dc != 0;
  1546. #endif
  1547. default:
  1548. return false;
  1549. }
  1550. }
  1551. /**
  1552. * amdgpu_device_has_dc_support - check if dc is supported
  1553. *
  1554. * @adev: amdgpu_device_pointer
  1555. *
  1556. * Returns true for supported, false for not supported
  1557. */
  1558. bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
  1559. {
  1560. if (amdgpu_sriov_vf(adev))
  1561. return false;
  1562. return amdgpu_device_asic_has_dc_support(adev->asic_type);
  1563. }
  1564. /**
  1565. * amdgpu_device_init - initialize the driver
  1566. *
  1567. * @adev: amdgpu_device pointer
  1568. * @pdev: drm dev pointer
  1569. * @pdev: pci dev pointer
  1570. * @flags: driver flags
  1571. *
  1572. * Initializes the driver info and hw (all asics).
  1573. * Returns 0 for success or an error on failure.
  1574. * Called at driver startup.
  1575. */
  1576. int amdgpu_device_init(struct amdgpu_device *adev,
  1577. struct drm_device *ddev,
  1578. struct pci_dev *pdev,
  1579. uint32_t flags)
  1580. {
  1581. int r, i;
  1582. bool runtime = false;
  1583. u32 max_MBps;
  1584. adev->shutdown = false;
  1585. adev->dev = &pdev->dev;
  1586. adev->ddev = ddev;
  1587. adev->pdev = pdev;
  1588. adev->flags = flags;
  1589. adev->asic_type = flags & AMD_ASIC_MASK;
  1590. adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
  1591. if (amdgpu_emu_mode == 1)
  1592. adev->usec_timeout *= 2;
  1593. adev->gmc.gart_size = 512 * 1024 * 1024;
  1594. adev->accel_working = false;
  1595. adev->num_rings = 0;
  1596. adev->mman.buffer_funcs = NULL;
  1597. adev->mman.buffer_funcs_ring = NULL;
  1598. adev->vm_manager.vm_pte_funcs = NULL;
  1599. adev->vm_manager.vm_pte_num_rings = 0;
  1600. adev->gmc.gmc_funcs = NULL;
  1601. adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  1602. bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
  1603. adev->smc_rreg = &amdgpu_invalid_rreg;
  1604. adev->smc_wreg = &amdgpu_invalid_wreg;
  1605. adev->pcie_rreg = &amdgpu_invalid_rreg;
  1606. adev->pcie_wreg = &amdgpu_invalid_wreg;
  1607. adev->pciep_rreg = &amdgpu_invalid_rreg;
  1608. adev->pciep_wreg = &amdgpu_invalid_wreg;
  1609. adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
  1610. adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
  1611. adev->didt_rreg = &amdgpu_invalid_rreg;
  1612. adev->didt_wreg = &amdgpu_invalid_wreg;
  1613. adev->gc_cac_rreg = &amdgpu_invalid_rreg;
  1614. adev->gc_cac_wreg = &amdgpu_invalid_wreg;
  1615. adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
  1616. adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
  1617. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
  1618. amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
  1619. pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
  1620. /* mutex initialization are all done here so we
  1621. * can recall function without having locking issues */
  1622. atomic_set(&adev->irq.ih.lock, 0);
  1623. mutex_init(&adev->firmware.mutex);
  1624. mutex_init(&adev->pm.mutex);
  1625. mutex_init(&adev->gfx.gpu_clock_mutex);
  1626. mutex_init(&adev->srbm_mutex);
  1627. mutex_init(&adev->gfx.pipe_reserve_mutex);
  1628. mutex_init(&adev->grbm_idx_mutex);
  1629. mutex_init(&adev->mn_lock);
  1630. mutex_init(&adev->virt.vf_errors.lock);
  1631. hash_init(adev->mn_hash);
  1632. mutex_init(&adev->lock_reset);
  1633. amdgpu_device_check_arguments(adev);
  1634. spin_lock_init(&adev->mmio_idx_lock);
  1635. spin_lock_init(&adev->smc_idx_lock);
  1636. spin_lock_init(&adev->pcie_idx_lock);
  1637. spin_lock_init(&adev->uvd_ctx_idx_lock);
  1638. spin_lock_init(&adev->didt_idx_lock);
  1639. spin_lock_init(&adev->gc_cac_idx_lock);
  1640. spin_lock_init(&adev->se_cac_idx_lock);
  1641. spin_lock_init(&adev->audio_endpt_idx_lock);
  1642. spin_lock_init(&adev->mm_stats.lock);
  1643. INIT_LIST_HEAD(&adev->shadow_list);
  1644. mutex_init(&adev->shadow_list_lock);
  1645. INIT_LIST_HEAD(&adev->ring_lru_list);
  1646. spin_lock_init(&adev->ring_lru_list_lock);
  1647. INIT_DELAYED_WORK(&adev->late_init_work,
  1648. amdgpu_device_ip_late_init_func_handler);
  1649. /* Registers mapping */
  1650. /* TODO: block userspace mapping of io register */
  1651. if (adev->asic_type >= CHIP_BONAIRE) {
  1652. adev->rmmio_base = pci_resource_start(adev->pdev, 5);
  1653. adev->rmmio_size = pci_resource_len(adev->pdev, 5);
  1654. } else {
  1655. adev->rmmio_base = pci_resource_start(adev->pdev, 2);
  1656. adev->rmmio_size = pci_resource_len(adev->pdev, 2);
  1657. }
  1658. adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
  1659. if (adev->rmmio == NULL) {
  1660. return -ENOMEM;
  1661. }
  1662. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
  1663. DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  1664. /* doorbell bar mapping */
  1665. amdgpu_device_doorbell_init(adev);
  1666. /* io port mapping */
  1667. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1668. if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
  1669. adev->rio_mem_size = pci_resource_len(adev->pdev, i);
  1670. adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
  1671. break;
  1672. }
  1673. }
  1674. if (adev->rio_mem == NULL)
  1675. DRM_INFO("PCI I/O BAR is not found.\n");
  1676. /* early init functions */
  1677. r = amdgpu_device_ip_early_init(adev);
  1678. if (r)
  1679. return r;
  1680. /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
  1681. /* this will fail for cards that aren't VGA class devices, just
  1682. * ignore it */
  1683. vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
  1684. if (amdgpu_device_is_px(ddev))
  1685. runtime = true;
  1686. if (!pci_is_thunderbolt_attached(adev->pdev))
  1687. vga_switcheroo_register_client(adev->pdev,
  1688. &amdgpu_switcheroo_ops, runtime);
  1689. if (runtime)
  1690. vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
  1691. if (amdgpu_emu_mode == 1) {
  1692. /* post the asic on emulation mode */
  1693. emu_soc_asic_init(adev);
  1694. goto fence_driver_init;
  1695. }
  1696. /* Read BIOS */
  1697. if (!amdgpu_get_bios(adev)) {
  1698. r = -EINVAL;
  1699. goto failed;
  1700. }
  1701. r = amdgpu_atombios_init(adev);
  1702. if (r) {
  1703. dev_err(adev->dev, "amdgpu_atombios_init failed\n");
  1704. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
  1705. goto failed;
  1706. }
  1707. /* detect if we are with an SRIOV vbios */
  1708. amdgpu_device_detect_sriov_bios(adev);
  1709. /* Post card if necessary */
  1710. if (amdgpu_device_need_post(adev)) {
  1711. if (!adev->bios) {
  1712. dev_err(adev->dev, "no vBIOS found\n");
  1713. r = -EINVAL;
  1714. goto failed;
  1715. }
  1716. DRM_INFO("GPU posting now...\n");
  1717. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  1718. if (r) {
  1719. dev_err(adev->dev, "gpu post error!\n");
  1720. goto failed;
  1721. }
  1722. }
  1723. if (adev->is_atom_fw) {
  1724. /* Initialize clocks */
  1725. r = amdgpu_atomfirmware_get_clock_info(adev);
  1726. if (r) {
  1727. dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
  1728. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  1729. goto failed;
  1730. }
  1731. } else {
  1732. /* Initialize clocks */
  1733. r = amdgpu_atombios_get_clock_info(adev);
  1734. if (r) {
  1735. dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
  1736. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  1737. goto failed;
  1738. }
  1739. /* init i2c buses */
  1740. if (!amdgpu_device_has_dc_support(adev))
  1741. amdgpu_atombios_i2c_init(adev);
  1742. }
  1743. fence_driver_init:
  1744. /* Fence driver */
  1745. r = amdgpu_fence_driver_init(adev);
  1746. if (r) {
  1747. dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
  1748. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
  1749. goto failed;
  1750. }
  1751. /* init the mode config */
  1752. drm_mode_config_init(adev->ddev);
  1753. r = amdgpu_device_ip_init(adev);
  1754. if (r) {
  1755. /* failed in exclusive mode due to timeout */
  1756. if (amdgpu_sriov_vf(adev) &&
  1757. !amdgpu_sriov_runtime(adev) &&
  1758. amdgpu_virt_mmio_blocked(adev) &&
  1759. !amdgpu_virt_wait_reset(adev)) {
  1760. dev_err(adev->dev, "VF exclusive mode timeout\n");
  1761. /* Don't send request since VF is inactive. */
  1762. adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
  1763. adev->virt.ops = NULL;
  1764. r = -EAGAIN;
  1765. goto failed;
  1766. }
  1767. dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
  1768. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
  1769. amdgpu_device_ip_fini(adev);
  1770. goto failed;
  1771. }
  1772. adev->accel_working = true;
  1773. amdgpu_vm_check_compute_bug(adev);
  1774. /* Initialize the buffer migration limit. */
  1775. if (amdgpu_moverate >= 0)
  1776. max_MBps = amdgpu_moverate;
  1777. else
  1778. max_MBps = 8; /* Allow 8 MB/s. */
  1779. /* Get a log2 for easy divisions. */
  1780. adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
  1781. r = amdgpu_ib_pool_init(adev);
  1782. if (r) {
  1783. dev_err(adev->dev, "IB initialization failed (%d).\n", r);
  1784. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
  1785. goto failed;
  1786. }
  1787. r = amdgpu_ib_ring_tests(adev);
  1788. if (r)
  1789. DRM_ERROR("ib ring test failed (%d).\n", r);
  1790. if (amdgpu_sriov_vf(adev))
  1791. amdgpu_virt_init_data_exchange(adev);
  1792. amdgpu_fbdev_init(adev);
  1793. r = amdgpu_pm_sysfs_init(adev);
  1794. if (r)
  1795. DRM_ERROR("registering pm debugfs failed (%d).\n", r);
  1796. r = amdgpu_debugfs_gem_init(adev);
  1797. if (r)
  1798. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  1799. r = amdgpu_debugfs_regs_init(adev);
  1800. if (r)
  1801. DRM_ERROR("registering register debugfs failed (%d).\n", r);
  1802. r = amdgpu_debugfs_firmware_init(adev);
  1803. if (r)
  1804. DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
  1805. r = amdgpu_debugfs_init(adev);
  1806. if (r)
  1807. DRM_ERROR("Creating debugfs files failed (%d).\n", r);
  1808. if ((amdgpu_testing & 1)) {
  1809. if (adev->accel_working)
  1810. amdgpu_test_moves(adev);
  1811. else
  1812. DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
  1813. }
  1814. if (amdgpu_benchmarking) {
  1815. if (adev->accel_working)
  1816. amdgpu_benchmark(adev, amdgpu_benchmarking);
  1817. else
  1818. DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
  1819. }
  1820. /* enable clockgating, etc. after ib tests, etc. since some blocks require
  1821. * explicit gating rather than handling it automatically.
  1822. */
  1823. r = amdgpu_device_ip_late_init(adev);
  1824. if (r) {
  1825. dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
  1826. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
  1827. goto failed;
  1828. }
  1829. return 0;
  1830. failed:
  1831. amdgpu_vf_error_trans_all(adev);
  1832. if (runtime)
  1833. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  1834. return r;
  1835. }
  1836. /**
  1837. * amdgpu_device_fini - tear down the driver
  1838. *
  1839. * @adev: amdgpu_device pointer
  1840. *
  1841. * Tear down the driver info (all asics).
  1842. * Called at driver shutdown.
  1843. */
  1844. void amdgpu_device_fini(struct amdgpu_device *adev)
  1845. {
  1846. int r;
  1847. DRM_INFO("amdgpu: finishing device.\n");
  1848. adev->shutdown = true;
  1849. if (adev->mode_info.mode_config_initialized)
  1850. drm_crtc_force_disable_all(adev->ddev);
  1851. amdgpu_ib_pool_fini(adev);
  1852. amdgpu_fence_driver_fini(adev);
  1853. amdgpu_pm_sysfs_fini(adev);
  1854. amdgpu_fbdev_fini(adev);
  1855. r = amdgpu_device_ip_fini(adev);
  1856. if (adev->firmware.gpu_info_fw) {
  1857. release_firmware(adev->firmware.gpu_info_fw);
  1858. adev->firmware.gpu_info_fw = NULL;
  1859. }
  1860. adev->accel_working = false;
  1861. cancel_delayed_work_sync(&adev->late_init_work);
  1862. /* free i2c buses */
  1863. if (!amdgpu_device_has_dc_support(adev))
  1864. amdgpu_i2c_fini(adev);
  1865. if (amdgpu_emu_mode != 1)
  1866. amdgpu_atombios_fini(adev);
  1867. kfree(adev->bios);
  1868. adev->bios = NULL;
  1869. if (!pci_is_thunderbolt_attached(adev->pdev))
  1870. vga_switcheroo_unregister_client(adev->pdev);
  1871. if (adev->flags & AMD_IS_PX)
  1872. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  1873. vga_client_register(adev->pdev, NULL, NULL, NULL);
  1874. if (adev->rio_mem)
  1875. pci_iounmap(adev->pdev, adev->rio_mem);
  1876. adev->rio_mem = NULL;
  1877. iounmap(adev->rmmio);
  1878. adev->rmmio = NULL;
  1879. amdgpu_device_doorbell_fini(adev);
  1880. amdgpu_debugfs_regs_cleanup(adev);
  1881. }
  1882. /*
  1883. * Suspend & resume.
  1884. */
  1885. /**
  1886. * amdgpu_device_suspend - initiate device suspend
  1887. *
  1888. * @pdev: drm dev pointer
  1889. * @state: suspend state
  1890. *
  1891. * Puts the hw in the suspend state (all asics).
  1892. * Returns 0 for success or an error on failure.
  1893. * Called at driver suspend.
  1894. */
  1895. int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
  1896. {
  1897. struct amdgpu_device *adev;
  1898. struct drm_crtc *crtc;
  1899. struct drm_connector *connector;
  1900. int r;
  1901. if (dev == NULL || dev->dev_private == NULL) {
  1902. return -ENODEV;
  1903. }
  1904. adev = dev->dev_private;
  1905. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1906. return 0;
  1907. drm_kms_helper_poll_disable(dev);
  1908. if (!amdgpu_device_has_dc_support(adev)) {
  1909. /* turn off display hw */
  1910. drm_modeset_lock_all(dev);
  1911. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1912. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  1913. }
  1914. drm_modeset_unlock_all(dev);
  1915. }
  1916. amdgpu_amdkfd_suspend(adev);
  1917. /* unpin the front buffers and cursors */
  1918. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1919. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1920. struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
  1921. struct amdgpu_bo *robj;
  1922. if (amdgpu_crtc->cursor_bo) {
  1923. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  1924. r = amdgpu_bo_reserve(aobj, true);
  1925. if (r == 0) {
  1926. amdgpu_bo_unpin(aobj);
  1927. amdgpu_bo_unreserve(aobj);
  1928. }
  1929. }
  1930. if (rfb == NULL || rfb->obj == NULL) {
  1931. continue;
  1932. }
  1933. robj = gem_to_amdgpu_bo(rfb->obj);
  1934. /* don't unpin kernel fb objects */
  1935. if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
  1936. r = amdgpu_bo_reserve(robj, true);
  1937. if (r == 0) {
  1938. amdgpu_bo_unpin(robj);
  1939. amdgpu_bo_unreserve(robj);
  1940. }
  1941. }
  1942. }
  1943. /* evict vram memory */
  1944. amdgpu_bo_evict_vram(adev);
  1945. amdgpu_fence_driver_suspend(adev);
  1946. r = amdgpu_device_ip_suspend(adev);
  1947. /* evict remaining vram memory
  1948. * This second call to evict vram is to evict the gart page table
  1949. * using the CPU.
  1950. */
  1951. amdgpu_bo_evict_vram(adev);
  1952. pci_save_state(dev->pdev);
  1953. if (suspend) {
  1954. /* Shut down the device */
  1955. pci_disable_device(dev->pdev);
  1956. pci_set_power_state(dev->pdev, PCI_D3hot);
  1957. } else {
  1958. r = amdgpu_asic_reset(adev);
  1959. if (r)
  1960. DRM_ERROR("amdgpu asic reset failed\n");
  1961. }
  1962. if (fbcon) {
  1963. console_lock();
  1964. amdgpu_fbdev_set_suspend(adev, 1);
  1965. console_unlock();
  1966. }
  1967. return 0;
  1968. }
  1969. /**
  1970. * amdgpu_device_resume - initiate device resume
  1971. *
  1972. * @pdev: drm dev pointer
  1973. *
  1974. * Bring the hw back to operating state (all asics).
  1975. * Returns 0 for success or an error on failure.
  1976. * Called at driver resume.
  1977. */
  1978. int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
  1979. {
  1980. struct drm_connector *connector;
  1981. struct amdgpu_device *adev = dev->dev_private;
  1982. struct drm_crtc *crtc;
  1983. int r = 0;
  1984. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1985. return 0;
  1986. if (fbcon)
  1987. console_lock();
  1988. if (resume) {
  1989. pci_set_power_state(dev->pdev, PCI_D0);
  1990. pci_restore_state(dev->pdev);
  1991. r = pci_enable_device(dev->pdev);
  1992. if (r)
  1993. goto unlock;
  1994. }
  1995. /* post card */
  1996. if (amdgpu_device_need_post(adev)) {
  1997. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  1998. if (r)
  1999. DRM_ERROR("amdgpu asic init failed\n");
  2000. }
  2001. r = amdgpu_device_ip_resume(adev);
  2002. if (r) {
  2003. DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
  2004. goto unlock;
  2005. }
  2006. amdgpu_fence_driver_resume(adev);
  2007. if (resume) {
  2008. r = amdgpu_ib_ring_tests(adev);
  2009. if (r)
  2010. DRM_ERROR("ib ring test failed (%d).\n", r);
  2011. }
  2012. r = amdgpu_device_ip_late_init(adev);
  2013. if (r)
  2014. goto unlock;
  2015. /* pin cursors */
  2016. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2017. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2018. if (amdgpu_crtc->cursor_bo) {
  2019. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2020. r = amdgpu_bo_reserve(aobj, true);
  2021. if (r == 0) {
  2022. r = amdgpu_bo_pin(aobj,
  2023. AMDGPU_GEM_DOMAIN_VRAM,
  2024. &amdgpu_crtc->cursor_addr);
  2025. if (r != 0)
  2026. DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
  2027. amdgpu_bo_unreserve(aobj);
  2028. }
  2029. }
  2030. }
  2031. r = amdgpu_amdkfd_resume(adev);
  2032. if (r)
  2033. return r;
  2034. /* blat the mode back in */
  2035. if (fbcon) {
  2036. if (!amdgpu_device_has_dc_support(adev)) {
  2037. /* pre DCE11 */
  2038. drm_helper_resume_force_mode(dev);
  2039. /* turn on display hw */
  2040. drm_modeset_lock_all(dev);
  2041. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2042. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  2043. }
  2044. drm_modeset_unlock_all(dev);
  2045. }
  2046. }
  2047. drm_kms_helper_poll_enable(dev);
  2048. /*
  2049. * Most of the connector probing functions try to acquire runtime pm
  2050. * refs to ensure that the GPU is powered on when connector polling is
  2051. * performed. Since we're calling this from a runtime PM callback,
  2052. * trying to acquire rpm refs will cause us to deadlock.
  2053. *
  2054. * Since we're guaranteed to be holding the rpm lock, it's safe to
  2055. * temporarily disable the rpm helpers so this doesn't deadlock us.
  2056. */
  2057. #ifdef CONFIG_PM
  2058. dev->dev->power.disable_depth++;
  2059. #endif
  2060. if (!amdgpu_device_has_dc_support(adev))
  2061. drm_helper_hpd_irq_event(dev);
  2062. else
  2063. drm_kms_helper_hotplug_event(dev);
  2064. #ifdef CONFIG_PM
  2065. dev->dev->power.disable_depth--;
  2066. #endif
  2067. if (fbcon)
  2068. amdgpu_fbdev_set_suspend(adev, 0);
  2069. unlock:
  2070. if (fbcon)
  2071. console_unlock();
  2072. return r;
  2073. }
  2074. static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
  2075. {
  2076. int i;
  2077. bool asic_hang = false;
  2078. if (amdgpu_sriov_vf(adev))
  2079. return true;
  2080. for (i = 0; i < adev->num_ip_blocks; i++) {
  2081. if (!adev->ip_blocks[i].status.valid)
  2082. continue;
  2083. if (adev->ip_blocks[i].version->funcs->check_soft_reset)
  2084. adev->ip_blocks[i].status.hang =
  2085. adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
  2086. if (adev->ip_blocks[i].status.hang) {
  2087. DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
  2088. asic_hang = true;
  2089. }
  2090. }
  2091. return asic_hang;
  2092. }
  2093. static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
  2094. {
  2095. int i, r = 0;
  2096. for (i = 0; i < adev->num_ip_blocks; i++) {
  2097. if (!adev->ip_blocks[i].status.valid)
  2098. continue;
  2099. if (adev->ip_blocks[i].status.hang &&
  2100. adev->ip_blocks[i].version->funcs->pre_soft_reset) {
  2101. r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
  2102. if (r)
  2103. return r;
  2104. }
  2105. }
  2106. return 0;
  2107. }
  2108. static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
  2109. {
  2110. int i;
  2111. for (i = 0; i < adev->num_ip_blocks; i++) {
  2112. if (!adev->ip_blocks[i].status.valid)
  2113. continue;
  2114. if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
  2115. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
  2116. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
  2117. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
  2118. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
  2119. if (adev->ip_blocks[i].status.hang) {
  2120. DRM_INFO("Some block need full reset!\n");
  2121. return true;
  2122. }
  2123. }
  2124. }
  2125. return false;
  2126. }
  2127. static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
  2128. {
  2129. int i, r = 0;
  2130. for (i = 0; i < adev->num_ip_blocks; i++) {
  2131. if (!adev->ip_blocks[i].status.valid)
  2132. continue;
  2133. if (adev->ip_blocks[i].status.hang &&
  2134. adev->ip_blocks[i].version->funcs->soft_reset) {
  2135. r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
  2136. if (r)
  2137. return r;
  2138. }
  2139. }
  2140. return 0;
  2141. }
  2142. static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
  2143. {
  2144. int i, r = 0;
  2145. for (i = 0; i < adev->num_ip_blocks; i++) {
  2146. if (!adev->ip_blocks[i].status.valid)
  2147. continue;
  2148. if (adev->ip_blocks[i].status.hang &&
  2149. adev->ip_blocks[i].version->funcs->post_soft_reset)
  2150. r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
  2151. if (r)
  2152. return r;
  2153. }
  2154. return 0;
  2155. }
  2156. static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
  2157. struct amdgpu_ring *ring,
  2158. struct amdgpu_bo *bo,
  2159. struct dma_fence **fence)
  2160. {
  2161. uint32_t domain;
  2162. int r;
  2163. if (!bo->shadow)
  2164. return 0;
  2165. r = amdgpu_bo_reserve(bo, true);
  2166. if (r)
  2167. return r;
  2168. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  2169. /* if bo has been evicted, then no need to recover */
  2170. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  2171. r = amdgpu_bo_validate(bo->shadow);
  2172. if (r) {
  2173. DRM_ERROR("bo validate failed!\n");
  2174. goto err;
  2175. }
  2176. r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
  2177. NULL, fence, true);
  2178. if (r) {
  2179. DRM_ERROR("recover page table failed!\n");
  2180. goto err;
  2181. }
  2182. }
  2183. err:
  2184. amdgpu_bo_unreserve(bo);
  2185. return r;
  2186. }
  2187. static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
  2188. {
  2189. struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  2190. struct amdgpu_bo *bo, *tmp;
  2191. struct dma_fence *fence = NULL, *next = NULL;
  2192. long r = 1;
  2193. int i = 0;
  2194. long tmo;
  2195. if (amdgpu_sriov_runtime(adev))
  2196. tmo = msecs_to_jiffies(amdgpu_lockup_timeout);
  2197. else
  2198. tmo = msecs_to_jiffies(100);
  2199. DRM_INFO("recover vram bo from shadow start\n");
  2200. mutex_lock(&adev->shadow_list_lock);
  2201. list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
  2202. next = NULL;
  2203. amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
  2204. if (fence) {
  2205. r = dma_fence_wait_timeout(fence, false, tmo);
  2206. if (r == 0)
  2207. pr_err("wait fence %p[%d] timeout\n", fence, i);
  2208. else if (r < 0)
  2209. pr_err("wait fence %p[%d] interrupted\n", fence, i);
  2210. if (r < 1) {
  2211. dma_fence_put(fence);
  2212. fence = next;
  2213. break;
  2214. }
  2215. i++;
  2216. }
  2217. dma_fence_put(fence);
  2218. fence = next;
  2219. }
  2220. mutex_unlock(&adev->shadow_list_lock);
  2221. if (fence) {
  2222. r = dma_fence_wait_timeout(fence, false, tmo);
  2223. if (r == 0)
  2224. pr_err("wait fence %p[%d] timeout\n", fence, i);
  2225. else if (r < 0)
  2226. pr_err("wait fence %p[%d] interrupted\n", fence, i);
  2227. }
  2228. dma_fence_put(fence);
  2229. if (r > 0)
  2230. DRM_INFO("recover vram bo from shadow done\n");
  2231. else
  2232. DRM_ERROR("recover vram bo from shadow failed\n");
  2233. return (r > 0?0:1);
  2234. }
  2235. /*
  2236. * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
  2237. *
  2238. * @adev: amdgpu device pointer
  2239. *
  2240. * attempt to do soft-reset or full-reset and reinitialize Asic
  2241. * return 0 means successed otherwise failed
  2242. */
  2243. static int amdgpu_device_reset(struct amdgpu_device *adev)
  2244. {
  2245. bool need_full_reset, vram_lost = 0;
  2246. int r;
  2247. need_full_reset = amdgpu_device_ip_need_full_reset(adev);
  2248. if (!need_full_reset) {
  2249. amdgpu_device_ip_pre_soft_reset(adev);
  2250. r = amdgpu_device_ip_soft_reset(adev);
  2251. amdgpu_device_ip_post_soft_reset(adev);
  2252. if (r || amdgpu_device_ip_check_soft_reset(adev)) {
  2253. DRM_INFO("soft reset failed, will fallback to full reset!\n");
  2254. need_full_reset = true;
  2255. }
  2256. }
  2257. if (need_full_reset) {
  2258. r = amdgpu_device_ip_suspend(adev);
  2259. retry:
  2260. r = amdgpu_asic_reset(adev);
  2261. /* post card */
  2262. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2263. if (!r) {
  2264. dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
  2265. r = amdgpu_device_ip_resume_phase1(adev);
  2266. if (r)
  2267. goto out;
  2268. vram_lost = amdgpu_device_check_vram_lost(adev);
  2269. if (vram_lost) {
  2270. DRM_ERROR("VRAM is lost!\n");
  2271. atomic_inc(&adev->vram_lost_counter);
  2272. }
  2273. r = amdgpu_gtt_mgr_recover(
  2274. &adev->mman.bdev.man[TTM_PL_TT]);
  2275. if (r)
  2276. goto out;
  2277. r = amdgpu_device_ip_resume_phase2(adev);
  2278. if (r)
  2279. goto out;
  2280. if (vram_lost)
  2281. amdgpu_device_fill_reset_magic(adev);
  2282. }
  2283. }
  2284. out:
  2285. if (!r) {
  2286. amdgpu_irq_gpu_reset_resume_helper(adev);
  2287. r = amdgpu_ib_ring_tests(adev);
  2288. if (r) {
  2289. dev_err(adev->dev, "ib ring test failed (%d).\n", r);
  2290. r = amdgpu_device_ip_suspend(adev);
  2291. need_full_reset = true;
  2292. goto retry;
  2293. }
  2294. }
  2295. if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
  2296. r = amdgpu_device_handle_vram_lost(adev);
  2297. return r;
  2298. }
  2299. /*
  2300. * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
  2301. *
  2302. * @adev: amdgpu device pointer
  2303. *
  2304. * do VF FLR and reinitialize Asic
  2305. * return 0 means successed otherwise failed
  2306. */
  2307. static int amdgpu_device_reset_sriov(struct amdgpu_device *adev, bool from_hypervisor)
  2308. {
  2309. int r;
  2310. if (from_hypervisor)
  2311. r = amdgpu_virt_request_full_gpu(adev, true);
  2312. else
  2313. r = amdgpu_virt_reset_gpu(adev);
  2314. if (r)
  2315. return r;
  2316. /* Resume IP prior to SMC */
  2317. r = amdgpu_device_ip_reinit_early_sriov(adev);
  2318. if (r)
  2319. goto error;
  2320. /* we need recover gart prior to run SMC/CP/SDMA resume */
  2321. amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
  2322. /* now we are okay to resume SMC/CP/SDMA */
  2323. r = amdgpu_device_ip_reinit_late_sriov(adev);
  2324. amdgpu_virt_release_full_gpu(adev, true);
  2325. if (r)
  2326. goto error;
  2327. amdgpu_irq_gpu_reset_resume_helper(adev);
  2328. r = amdgpu_ib_ring_tests(adev);
  2329. if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
  2330. atomic_inc(&adev->vram_lost_counter);
  2331. r = amdgpu_device_handle_vram_lost(adev);
  2332. }
  2333. error:
  2334. return r;
  2335. }
  2336. /**
  2337. * amdgpu_device_gpu_recover - reset the asic and recover scheduler
  2338. *
  2339. * @adev: amdgpu device pointer
  2340. * @job: which job trigger hang
  2341. * @force forces reset regardless of amdgpu_gpu_recovery
  2342. *
  2343. * Attempt to reset the GPU if it has hung (all asics).
  2344. * Returns 0 for success or an error on failure.
  2345. */
  2346. int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  2347. struct amdgpu_job *job, bool force)
  2348. {
  2349. struct drm_atomic_state *state = NULL;
  2350. int i, r, resched;
  2351. if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
  2352. DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
  2353. return 0;
  2354. }
  2355. if (!force && (amdgpu_gpu_recovery == 0 ||
  2356. (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
  2357. DRM_INFO("GPU recovery disabled.\n");
  2358. return 0;
  2359. }
  2360. dev_info(adev->dev, "GPU reset begin!\n");
  2361. mutex_lock(&adev->lock_reset);
  2362. atomic_inc(&adev->gpu_reset_counter);
  2363. adev->in_gpu_reset = 1;
  2364. /* block TTM */
  2365. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  2366. /* store modesetting */
  2367. if (amdgpu_device_has_dc_support(adev))
  2368. state = drm_atomic_helper_suspend(adev->ddev);
  2369. /* block all schedulers and reset given job's ring */
  2370. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2371. struct amdgpu_ring *ring = adev->rings[i];
  2372. if (!ring || !ring->sched.thread)
  2373. continue;
  2374. kthread_park(ring->sched.thread);
  2375. if (job && job->ring->idx != i)
  2376. continue;
  2377. drm_sched_hw_job_reset(&ring->sched, &job->base);
  2378. /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
  2379. amdgpu_fence_driver_force_completion(ring);
  2380. }
  2381. if (amdgpu_sriov_vf(adev))
  2382. r = amdgpu_device_reset_sriov(adev, job ? false : true);
  2383. else
  2384. r = amdgpu_device_reset(adev);
  2385. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2386. struct amdgpu_ring *ring = adev->rings[i];
  2387. if (!ring || !ring->sched.thread)
  2388. continue;
  2389. /* only need recovery sched of the given job's ring
  2390. * or all rings (in the case @job is NULL)
  2391. * after above amdgpu_reset accomplished
  2392. */
  2393. if ((!job || job->ring->idx == i) && !r)
  2394. drm_sched_job_recovery(&ring->sched);
  2395. kthread_unpark(ring->sched.thread);
  2396. }
  2397. if (amdgpu_device_has_dc_support(adev)) {
  2398. if (drm_atomic_helper_resume(adev->ddev, state))
  2399. dev_info(adev->dev, "drm resume failed:%d\n", r);
  2400. } else {
  2401. drm_helper_resume_force_mode(adev->ddev);
  2402. }
  2403. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  2404. if (r) {
  2405. /* bad news, how to tell it to userspace ? */
  2406. dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
  2407. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
  2408. } else {
  2409. dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
  2410. }
  2411. amdgpu_vf_error_trans_all(adev);
  2412. adev->in_gpu_reset = 0;
  2413. mutex_unlock(&adev->lock_reset);
  2414. return r;
  2415. }
  2416. void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
  2417. {
  2418. u32 mask;
  2419. int ret;
  2420. if (amdgpu_pcie_gen_cap)
  2421. adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
  2422. if (amdgpu_pcie_lane_cap)
  2423. adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
  2424. /* covers APUs as well */
  2425. if (pci_is_root_bus(adev->pdev->bus)) {
  2426. if (adev->pm.pcie_gen_mask == 0)
  2427. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2428. if (adev->pm.pcie_mlw_mask == 0)
  2429. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2430. return;
  2431. }
  2432. if (adev->pm.pcie_gen_mask == 0) {
  2433. ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
  2434. if (!ret) {
  2435. adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2436. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2437. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2438. if (mask & DRM_PCIE_SPEED_25)
  2439. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
  2440. if (mask & DRM_PCIE_SPEED_50)
  2441. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
  2442. if (mask & DRM_PCIE_SPEED_80)
  2443. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
  2444. } else {
  2445. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2446. }
  2447. }
  2448. if (adev->pm.pcie_mlw_mask == 0) {
  2449. ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
  2450. if (!ret) {
  2451. switch (mask) {
  2452. case 32:
  2453. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
  2454. CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2455. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2456. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2457. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2458. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2459. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2460. break;
  2461. case 16:
  2462. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2463. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2464. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2465. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2466. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2467. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2468. break;
  2469. case 12:
  2470. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2471. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2472. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2473. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2474. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2475. break;
  2476. case 8:
  2477. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2478. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2479. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2480. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2481. break;
  2482. case 4:
  2483. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2484. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2485. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2486. break;
  2487. case 2:
  2488. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2489. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2490. break;
  2491. case 1:
  2492. adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
  2493. break;
  2494. default:
  2495. break;
  2496. }
  2497. } else {
  2498. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2499. }
  2500. }
  2501. }