amdgpu_device.c 66 KB

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