amdgpu_device.c 69 KB

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