amdgpu_device.c 72 KB

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