amdgpu_device.c 75 KB

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