amdgpu_device.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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/console.h>
  29. #include <linux/slab.h>
  30. #include <linux/debugfs.h>
  31. #include <drm/drmP.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include <drm/amdgpu_drm.h>
  34. #include <linux/vgaarb.h>
  35. #include <linux/vga_switcheroo.h>
  36. #include <linux/efi.h>
  37. #include "amdgpu.h"
  38. #include "amdgpu_i2c.h"
  39. #include "atom.h"
  40. #include "amdgpu_atombios.h"
  41. #include "bif/bif_4_1_d.h"
  42. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
  43. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
  44. static const char *amdgpu_asic_name[] = {
  45. "BONAIRE",
  46. "KAVERI",
  47. "KABINI",
  48. "HAWAII",
  49. "MULLINS",
  50. "TOPAZ",
  51. "TONGA",
  52. "CARRIZO",
  53. "LAST",
  54. };
  55. bool amdgpu_device_is_px(struct drm_device *dev)
  56. {
  57. struct amdgpu_device *adev = dev->dev_private;
  58. if (adev->flags & AMDGPU_IS_PX)
  59. return true;
  60. return false;
  61. }
  62. /*
  63. * MMIO register access helper functions.
  64. */
  65. uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
  66. bool always_indirect)
  67. {
  68. if ((reg * 4) < adev->rmmio_size && !always_indirect)
  69. return readl(((void __iomem *)adev->rmmio) + (reg * 4));
  70. else {
  71. unsigned long flags;
  72. uint32_t ret;
  73. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  74. writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
  75. ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  76. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  77. return ret;
  78. }
  79. }
  80. void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
  81. bool always_indirect)
  82. {
  83. if ((reg * 4) < adev->rmmio_size && !always_indirect)
  84. writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
  85. else {
  86. unsigned long flags;
  87. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  88. writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
  89. writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  90. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  91. }
  92. }
  93. u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
  94. {
  95. if ((reg * 4) < adev->rio_mem_size)
  96. return ioread32(adev->rio_mem + (reg * 4));
  97. else {
  98. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  99. return ioread32(adev->rio_mem + (mmMM_DATA * 4));
  100. }
  101. }
  102. void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
  103. {
  104. if ((reg * 4) < adev->rio_mem_size)
  105. iowrite32(v, adev->rio_mem + (reg * 4));
  106. else {
  107. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  108. iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
  109. }
  110. }
  111. /**
  112. * amdgpu_mm_rdoorbell - read a doorbell dword
  113. *
  114. * @adev: amdgpu_device pointer
  115. * @index: doorbell index
  116. *
  117. * Returns the value in the doorbell aperture at the
  118. * requested doorbell index (CIK).
  119. */
  120. u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
  121. {
  122. if (index < adev->doorbell.num_doorbells) {
  123. return readl(adev->doorbell.ptr + index);
  124. } else {
  125. DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
  126. return 0;
  127. }
  128. }
  129. /**
  130. * amdgpu_mm_wdoorbell - write a doorbell dword
  131. *
  132. * @adev: amdgpu_device pointer
  133. * @index: doorbell index
  134. * @v: value to write
  135. *
  136. * Writes @v to the doorbell aperture at the
  137. * requested doorbell index (CIK).
  138. */
  139. void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
  140. {
  141. if (index < adev->doorbell.num_doorbells) {
  142. writel(v, adev->doorbell.ptr + index);
  143. } else {
  144. DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  145. }
  146. }
  147. /**
  148. * amdgpu_invalid_rreg - dummy reg read function
  149. *
  150. * @adev: amdgpu device pointer
  151. * @reg: offset of register
  152. *
  153. * Dummy register read function. Used for register blocks
  154. * that certain asics don't have (all asics).
  155. * Returns the value in the register.
  156. */
  157. static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
  158. {
  159. DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
  160. BUG();
  161. return 0;
  162. }
  163. /**
  164. * amdgpu_invalid_wreg - dummy reg write function
  165. *
  166. * @adev: amdgpu device pointer
  167. * @reg: offset of register
  168. * @v: value to write to the register
  169. *
  170. * Dummy register read function. Used for register blocks
  171. * that certain asics don't have (all asics).
  172. */
  173. static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
  174. {
  175. DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
  176. reg, v);
  177. BUG();
  178. }
  179. /**
  180. * amdgpu_block_invalid_rreg - dummy reg read function
  181. *
  182. * @adev: amdgpu device pointer
  183. * @block: offset of instance
  184. * @reg: offset of register
  185. *
  186. * Dummy register read function. Used for register blocks
  187. * that certain asics don't have (all asics).
  188. * Returns the value in the register.
  189. */
  190. static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
  191. uint32_t block, uint32_t reg)
  192. {
  193. DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
  194. reg, block);
  195. BUG();
  196. return 0;
  197. }
  198. /**
  199. * amdgpu_block_invalid_wreg - dummy reg write function
  200. *
  201. * @adev: amdgpu device pointer
  202. * @block: offset of instance
  203. * @reg: offset of register
  204. * @v: value to write to the register
  205. *
  206. * Dummy register read function. Used for register blocks
  207. * that certain asics don't have (all asics).
  208. */
  209. static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
  210. uint32_t block,
  211. uint32_t reg, uint32_t v)
  212. {
  213. DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
  214. reg, block, v);
  215. BUG();
  216. }
  217. static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
  218. {
  219. int r;
  220. if (adev->vram_scratch.robj == NULL) {
  221. r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
  222. PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM, 0,
  223. NULL, &adev->vram_scratch.robj);
  224. if (r) {
  225. return r;
  226. }
  227. }
  228. r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
  229. if (unlikely(r != 0))
  230. return r;
  231. r = amdgpu_bo_pin(adev->vram_scratch.robj,
  232. AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
  233. if (r) {
  234. amdgpu_bo_unreserve(adev->vram_scratch.robj);
  235. return r;
  236. }
  237. r = amdgpu_bo_kmap(adev->vram_scratch.robj,
  238. (void **)&adev->vram_scratch.ptr);
  239. if (r)
  240. amdgpu_bo_unpin(adev->vram_scratch.robj);
  241. amdgpu_bo_unreserve(adev->vram_scratch.robj);
  242. return r;
  243. }
  244. static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
  245. {
  246. int r;
  247. if (adev->vram_scratch.robj == NULL) {
  248. return;
  249. }
  250. r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
  251. if (likely(r == 0)) {
  252. amdgpu_bo_kunmap(adev->vram_scratch.robj);
  253. amdgpu_bo_unpin(adev->vram_scratch.robj);
  254. amdgpu_bo_unreserve(adev->vram_scratch.robj);
  255. }
  256. amdgpu_bo_unref(&adev->vram_scratch.robj);
  257. }
  258. /**
  259. * amdgpu_program_register_sequence - program an array of registers.
  260. *
  261. * @adev: amdgpu_device pointer
  262. * @registers: pointer to the register array
  263. * @array_size: size of the register array
  264. *
  265. * Programs an array or registers with and and or masks.
  266. * This is a helper for setting golden registers.
  267. */
  268. void amdgpu_program_register_sequence(struct amdgpu_device *adev,
  269. const u32 *registers,
  270. const u32 array_size)
  271. {
  272. u32 tmp, reg, and_mask, or_mask;
  273. int i;
  274. if (array_size % 3)
  275. return;
  276. for (i = 0; i < array_size; i +=3) {
  277. reg = registers[i + 0];
  278. and_mask = registers[i + 1];
  279. or_mask = registers[i + 2];
  280. if (and_mask == 0xffffffff) {
  281. tmp = or_mask;
  282. } else {
  283. tmp = RREG32(reg);
  284. tmp &= ~and_mask;
  285. tmp |= or_mask;
  286. }
  287. WREG32(reg, tmp);
  288. }
  289. }
  290. void amdgpu_pci_config_reset(struct amdgpu_device *adev)
  291. {
  292. pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
  293. }
  294. /*
  295. * GPU doorbell aperture helpers function.
  296. */
  297. /**
  298. * amdgpu_doorbell_init - Init doorbell driver information.
  299. *
  300. * @adev: amdgpu_device pointer
  301. *
  302. * Init doorbell driver information (CIK)
  303. * Returns 0 on success, error on failure.
  304. */
  305. static int amdgpu_doorbell_init(struct amdgpu_device *adev)
  306. {
  307. /* doorbell bar mapping */
  308. adev->doorbell.base = pci_resource_start(adev->pdev, 2);
  309. adev->doorbell.size = pci_resource_len(adev->pdev, 2);
  310. adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
  311. AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
  312. if (adev->doorbell.num_doorbells == 0)
  313. return -EINVAL;
  314. adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
  315. if (adev->doorbell.ptr == NULL) {
  316. return -ENOMEM;
  317. }
  318. DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
  319. DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
  320. return 0;
  321. }
  322. /**
  323. * amdgpu_doorbell_fini - Tear down doorbell driver information.
  324. *
  325. * @adev: amdgpu_device pointer
  326. *
  327. * Tear down doorbell driver information (CIK)
  328. */
  329. static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
  330. {
  331. iounmap(adev->doorbell.ptr);
  332. adev->doorbell.ptr = NULL;
  333. }
  334. /**
  335. * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
  336. * setup amdkfd
  337. *
  338. * @adev: amdgpu_device pointer
  339. * @aperture_base: output returning doorbell aperture base physical address
  340. * @aperture_size: output returning doorbell aperture size in bytes
  341. * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
  342. *
  343. * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
  344. * takes doorbells required for its own rings and reports the setup to amdkfd.
  345. * amdgpu reserved doorbells are at the start of the doorbell aperture.
  346. */
  347. void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
  348. phys_addr_t *aperture_base,
  349. size_t *aperture_size,
  350. size_t *start_offset)
  351. {
  352. /*
  353. * The first num_doorbells are used by amdgpu.
  354. * amdkfd takes whatever's left in the aperture.
  355. */
  356. if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
  357. *aperture_base = adev->doorbell.base;
  358. *aperture_size = adev->doorbell.size;
  359. *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
  360. } else {
  361. *aperture_base = 0;
  362. *aperture_size = 0;
  363. *start_offset = 0;
  364. }
  365. }
  366. /*
  367. * amdgpu_wb_*()
  368. * Writeback is the the method by which the the GPU updates special pages
  369. * in memory with the status of certain GPU events (fences, ring pointers,
  370. * etc.).
  371. */
  372. /**
  373. * amdgpu_wb_fini - Disable Writeback and free memory
  374. *
  375. * @adev: amdgpu_device pointer
  376. *
  377. * Disables Writeback and frees the Writeback memory (all asics).
  378. * Used at driver shutdown.
  379. */
  380. static void amdgpu_wb_fini(struct amdgpu_device *adev)
  381. {
  382. if (adev->wb.wb_obj) {
  383. if (!amdgpu_bo_reserve(adev->wb.wb_obj, false)) {
  384. amdgpu_bo_kunmap(adev->wb.wb_obj);
  385. amdgpu_bo_unpin(adev->wb.wb_obj);
  386. amdgpu_bo_unreserve(adev->wb.wb_obj);
  387. }
  388. amdgpu_bo_unref(&adev->wb.wb_obj);
  389. adev->wb.wb = NULL;
  390. adev->wb.wb_obj = NULL;
  391. }
  392. }
  393. /**
  394. * amdgpu_wb_init- Init Writeback driver info and allocate memory
  395. *
  396. * @adev: amdgpu_device pointer
  397. *
  398. * Disables Writeback and frees the Writeback memory (all asics).
  399. * Used at driver startup.
  400. * Returns 0 on success or an -error on failure.
  401. */
  402. static int amdgpu_wb_init(struct amdgpu_device *adev)
  403. {
  404. int r;
  405. if (adev->wb.wb_obj == NULL) {
  406. r = amdgpu_bo_create(adev, AMDGPU_MAX_WB * 4, PAGE_SIZE, true,
  407. AMDGPU_GEM_DOMAIN_GTT, 0, NULL, &adev->wb.wb_obj);
  408. if (r) {
  409. dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
  410. return r;
  411. }
  412. r = amdgpu_bo_reserve(adev->wb.wb_obj, false);
  413. if (unlikely(r != 0)) {
  414. amdgpu_wb_fini(adev);
  415. return r;
  416. }
  417. r = amdgpu_bo_pin(adev->wb.wb_obj, AMDGPU_GEM_DOMAIN_GTT,
  418. &adev->wb.gpu_addr);
  419. if (r) {
  420. amdgpu_bo_unreserve(adev->wb.wb_obj);
  421. dev_warn(adev->dev, "(%d) pin WB bo failed\n", r);
  422. amdgpu_wb_fini(adev);
  423. return r;
  424. }
  425. r = amdgpu_bo_kmap(adev->wb.wb_obj, (void **)&adev->wb.wb);
  426. amdgpu_bo_unreserve(adev->wb.wb_obj);
  427. if (r) {
  428. dev_warn(adev->dev, "(%d) map WB bo failed\n", r);
  429. amdgpu_wb_fini(adev);
  430. return r;
  431. }
  432. adev->wb.num_wb = AMDGPU_MAX_WB;
  433. memset(&adev->wb.used, 0, sizeof(adev->wb.used));
  434. /* clear wb memory */
  435. memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
  436. }
  437. return 0;
  438. }
  439. /**
  440. * amdgpu_wb_get - Allocate a wb entry
  441. *
  442. * @adev: amdgpu_device pointer
  443. * @wb: wb index
  444. *
  445. * Allocate a wb slot for use by the driver (all asics).
  446. * Returns 0 on success or -EINVAL on failure.
  447. */
  448. int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
  449. {
  450. unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
  451. if (offset < adev->wb.num_wb) {
  452. __set_bit(offset, adev->wb.used);
  453. *wb = offset;
  454. return 0;
  455. } else {
  456. return -EINVAL;
  457. }
  458. }
  459. /**
  460. * amdgpu_wb_free - Free a wb entry
  461. *
  462. * @adev: amdgpu_device pointer
  463. * @wb: wb index
  464. *
  465. * Free a wb slot allocated for use by the driver (all asics)
  466. */
  467. void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
  468. {
  469. if (wb < adev->wb.num_wb)
  470. __clear_bit(wb, adev->wb.used);
  471. }
  472. /**
  473. * amdgpu_vram_location - try to find VRAM location
  474. * @adev: amdgpu device structure holding all necessary informations
  475. * @mc: memory controller structure holding memory informations
  476. * @base: base address at which to put VRAM
  477. *
  478. * Function will place try to place VRAM at base address provided
  479. * as parameter (which is so far either PCI aperture address or
  480. * for IGP TOM base address).
  481. *
  482. * If there is not enough space to fit the unvisible VRAM in the 32bits
  483. * address space then we limit the VRAM size to the aperture.
  484. *
  485. * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
  486. * this shouldn't be a problem as we are using the PCI aperture as a reference.
  487. * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
  488. * not IGP.
  489. *
  490. * Note: we use mc_vram_size as on some board we need to program the mc to
  491. * cover the whole aperture even if VRAM size is inferior to aperture size
  492. * Novell bug 204882 + along with lots of ubuntu ones
  493. *
  494. * Note: when limiting vram it's safe to overwritte real_vram_size because
  495. * we are not in case where real_vram_size is inferior to mc_vram_size (ie
  496. * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
  497. * ones)
  498. *
  499. * Note: IGP TOM addr should be the same as the aperture addr, we don't
  500. * explicitly check for that thought.
  501. *
  502. * FIXME: when reducing VRAM size align new size on power of 2.
  503. */
  504. void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
  505. {
  506. uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
  507. mc->vram_start = base;
  508. if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
  509. dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
  510. mc->real_vram_size = mc->aper_size;
  511. mc->mc_vram_size = mc->aper_size;
  512. }
  513. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  514. if (limit && limit < mc->real_vram_size)
  515. mc->real_vram_size = limit;
  516. dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  517. mc->mc_vram_size >> 20, mc->vram_start,
  518. mc->vram_end, mc->real_vram_size >> 20);
  519. }
  520. /**
  521. * amdgpu_gtt_location - try to find GTT location
  522. * @adev: amdgpu device structure holding all necessary informations
  523. * @mc: memory controller structure holding memory informations
  524. *
  525. * Function will place try to place GTT before or after VRAM.
  526. *
  527. * If GTT size is bigger than space left then we ajust GTT size.
  528. * Thus function will never fails.
  529. *
  530. * FIXME: when reducing GTT size align new size on power of 2.
  531. */
  532. void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
  533. {
  534. u64 size_af, size_bf;
  535. size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
  536. size_bf = mc->vram_start & ~mc->gtt_base_align;
  537. if (size_bf > size_af) {
  538. if (mc->gtt_size > size_bf) {
  539. dev_warn(adev->dev, "limiting GTT\n");
  540. mc->gtt_size = size_bf;
  541. }
  542. mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
  543. } else {
  544. if (mc->gtt_size > size_af) {
  545. dev_warn(adev->dev, "limiting GTT\n");
  546. mc->gtt_size = size_af;
  547. }
  548. mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
  549. }
  550. mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
  551. dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  552. mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
  553. }
  554. /*
  555. * GPU helpers function.
  556. */
  557. /**
  558. * amdgpu_card_posted - check if the hw has already been initialized
  559. *
  560. * @adev: amdgpu_device pointer
  561. *
  562. * Check if the asic has been initialized (all asics).
  563. * Used at driver startup.
  564. * Returns true if initialized or false if not.
  565. */
  566. bool amdgpu_card_posted(struct amdgpu_device *adev)
  567. {
  568. uint32_t reg;
  569. /* then check MEM_SIZE, in case the crtcs are off */
  570. reg = RREG32(mmCONFIG_MEMSIZE);
  571. if (reg)
  572. return true;
  573. return false;
  574. }
  575. /**
  576. * amdgpu_boot_test_post_card - check and possibly initialize the hw
  577. *
  578. * @adev: amdgpu_device pointer
  579. *
  580. * Check if the asic is initialized and if not, attempt to initialize
  581. * it (all asics).
  582. * Returns true if initialized or false if not.
  583. */
  584. bool amdgpu_boot_test_post_card(struct amdgpu_device *adev)
  585. {
  586. if (amdgpu_card_posted(adev))
  587. return true;
  588. if (adev->bios) {
  589. DRM_INFO("GPU not posted. posting now...\n");
  590. if (adev->is_atom_bios)
  591. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  592. return true;
  593. } else {
  594. dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
  595. return false;
  596. }
  597. }
  598. /**
  599. * amdgpu_dummy_page_init - init dummy page used by the driver
  600. *
  601. * @adev: amdgpu_device pointer
  602. *
  603. * Allocate the dummy page used by the driver (all asics).
  604. * This dummy page is used by the driver as a filler for gart entries
  605. * when pages are taken out of the GART
  606. * Returns 0 on sucess, -ENOMEM on failure.
  607. */
  608. int amdgpu_dummy_page_init(struct amdgpu_device *adev)
  609. {
  610. if (adev->dummy_page.page)
  611. return 0;
  612. adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  613. if (adev->dummy_page.page == NULL)
  614. return -ENOMEM;
  615. adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
  616. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  617. if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
  618. dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  619. __free_page(adev->dummy_page.page);
  620. adev->dummy_page.page = NULL;
  621. return -ENOMEM;
  622. }
  623. return 0;
  624. }
  625. /**
  626. * amdgpu_dummy_page_fini - free dummy page used by the driver
  627. *
  628. * @adev: amdgpu_device pointer
  629. *
  630. * Frees the dummy page used by the driver (all asics).
  631. */
  632. void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
  633. {
  634. if (adev->dummy_page.page == NULL)
  635. return;
  636. pci_unmap_page(adev->pdev, adev->dummy_page.addr,
  637. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  638. __free_page(adev->dummy_page.page);
  639. adev->dummy_page.page = NULL;
  640. }
  641. /* ATOM accessor methods */
  642. /*
  643. * ATOM is an interpreted byte code stored in tables in the vbios. The
  644. * driver registers callbacks to access registers and the interpreter
  645. * in the driver parses the tables and executes then to program specific
  646. * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
  647. * atombios.h, and atom.c
  648. */
  649. /**
  650. * cail_pll_read - read PLL register
  651. *
  652. * @info: atom card_info pointer
  653. * @reg: PLL register offset
  654. *
  655. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  656. * Returns the value of the PLL register.
  657. */
  658. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  659. {
  660. return 0;
  661. }
  662. /**
  663. * cail_pll_write - write PLL register
  664. *
  665. * @info: atom card_info pointer
  666. * @reg: PLL register offset
  667. * @val: value to write to the pll register
  668. *
  669. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  670. */
  671. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  672. {
  673. }
  674. /**
  675. * cail_mc_read - read MC (Memory Controller) register
  676. *
  677. * @info: atom card_info pointer
  678. * @reg: MC register offset
  679. *
  680. * Provides an MC register accessor for the atom interpreter (r4xx+).
  681. * Returns the value of the MC register.
  682. */
  683. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  684. {
  685. return 0;
  686. }
  687. /**
  688. * cail_mc_write - write MC (Memory Controller) register
  689. *
  690. * @info: atom card_info pointer
  691. * @reg: MC register offset
  692. * @val: value to write to the pll register
  693. *
  694. * Provides a MC register accessor for the atom interpreter (r4xx+).
  695. */
  696. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  697. {
  698. }
  699. /**
  700. * cail_reg_write - write MMIO register
  701. *
  702. * @info: atom card_info pointer
  703. * @reg: MMIO register offset
  704. * @val: value to write to the pll register
  705. *
  706. * Provides a MMIO register accessor for the atom interpreter (r4xx+).
  707. */
  708. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  709. {
  710. struct amdgpu_device *adev = info->dev->dev_private;
  711. WREG32(reg, val);
  712. }
  713. /**
  714. * cail_reg_read - read MMIO register
  715. *
  716. * @info: atom card_info pointer
  717. * @reg: MMIO register offset
  718. *
  719. * Provides an MMIO register accessor for the atom interpreter (r4xx+).
  720. * Returns the value of the MMIO register.
  721. */
  722. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  723. {
  724. struct amdgpu_device *adev = info->dev->dev_private;
  725. uint32_t r;
  726. r = RREG32(reg);
  727. return r;
  728. }
  729. /**
  730. * cail_ioreg_write - write IO register
  731. *
  732. * @info: atom card_info pointer
  733. * @reg: IO register offset
  734. * @val: value to write to the pll register
  735. *
  736. * Provides a IO register accessor for the atom interpreter (r4xx+).
  737. */
  738. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  739. {
  740. struct amdgpu_device *adev = info->dev->dev_private;
  741. WREG32_IO(reg, val);
  742. }
  743. /**
  744. * cail_ioreg_read - read IO register
  745. *
  746. * @info: atom card_info pointer
  747. * @reg: IO register offset
  748. *
  749. * Provides an IO register accessor for the atom interpreter (r4xx+).
  750. * Returns the value of the IO register.
  751. */
  752. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  753. {
  754. struct amdgpu_device *adev = info->dev->dev_private;
  755. uint32_t r;
  756. r = RREG32_IO(reg);
  757. return r;
  758. }
  759. /**
  760. * amdgpu_atombios_fini - free the driver info and callbacks for atombios
  761. *
  762. * @adev: amdgpu_device pointer
  763. *
  764. * Frees the driver info and register access callbacks for the ATOM
  765. * interpreter (r4xx+).
  766. * Called at driver shutdown.
  767. */
  768. static void amdgpu_atombios_fini(struct amdgpu_device *adev)
  769. {
  770. if (adev->mode_info.atom_context)
  771. kfree(adev->mode_info.atom_context->scratch);
  772. kfree(adev->mode_info.atom_context);
  773. adev->mode_info.atom_context = NULL;
  774. kfree(adev->mode_info.atom_card_info);
  775. adev->mode_info.atom_card_info = NULL;
  776. }
  777. /**
  778. * amdgpu_atombios_init - init the driver info and callbacks for atombios
  779. *
  780. * @adev: amdgpu_device pointer
  781. *
  782. * Initializes the driver info and register access callbacks for the
  783. * ATOM interpreter (r4xx+).
  784. * Returns 0 on sucess, -ENOMEM on failure.
  785. * Called at driver startup.
  786. */
  787. static int amdgpu_atombios_init(struct amdgpu_device *adev)
  788. {
  789. struct card_info *atom_card_info =
  790. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  791. if (!atom_card_info)
  792. return -ENOMEM;
  793. adev->mode_info.atom_card_info = atom_card_info;
  794. atom_card_info->dev = adev->ddev;
  795. atom_card_info->reg_read = cail_reg_read;
  796. atom_card_info->reg_write = cail_reg_write;
  797. /* needed for iio ops */
  798. if (adev->rio_mem) {
  799. atom_card_info->ioreg_read = cail_ioreg_read;
  800. atom_card_info->ioreg_write = cail_ioreg_write;
  801. } else {
  802. DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
  803. atom_card_info->ioreg_read = cail_reg_read;
  804. atom_card_info->ioreg_write = cail_reg_write;
  805. }
  806. atom_card_info->mc_read = cail_mc_read;
  807. atom_card_info->mc_write = cail_mc_write;
  808. atom_card_info->pll_read = cail_pll_read;
  809. atom_card_info->pll_write = cail_pll_write;
  810. adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
  811. if (!adev->mode_info.atom_context) {
  812. amdgpu_atombios_fini(adev);
  813. return -ENOMEM;
  814. }
  815. mutex_init(&adev->mode_info.atom_context->mutex);
  816. amdgpu_atombios_scratch_regs_init(adev);
  817. amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
  818. return 0;
  819. }
  820. /* if we get transitioned to only one device, take VGA back */
  821. /**
  822. * amdgpu_vga_set_decode - enable/disable vga decode
  823. *
  824. * @cookie: amdgpu_device pointer
  825. * @state: enable/disable vga decode
  826. *
  827. * Enable/disable vga decode (all asics).
  828. * Returns VGA resource flags.
  829. */
  830. static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
  831. {
  832. struct amdgpu_device *adev = cookie;
  833. amdgpu_asic_set_vga_state(adev, state);
  834. if (state)
  835. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  836. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  837. else
  838. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  839. }
  840. /**
  841. * amdgpu_check_pot_argument - check that argument is a power of two
  842. *
  843. * @arg: value to check
  844. *
  845. * Validates that a certain argument is a power of two (all asics).
  846. * Returns true if argument is valid.
  847. */
  848. static bool amdgpu_check_pot_argument(int arg)
  849. {
  850. return (arg & (arg - 1)) == 0;
  851. }
  852. /**
  853. * amdgpu_check_arguments - validate module params
  854. *
  855. * @adev: amdgpu_device pointer
  856. *
  857. * Validates certain module parameters and updates
  858. * the associated values used by the driver (all asics).
  859. */
  860. static void amdgpu_check_arguments(struct amdgpu_device *adev)
  861. {
  862. /* vramlimit must be a power of two */
  863. if (!amdgpu_check_pot_argument(amdgpu_vram_limit)) {
  864. dev_warn(adev->dev, "vram limit (%d) must be a power of 2\n",
  865. amdgpu_vram_limit);
  866. amdgpu_vram_limit = 0;
  867. }
  868. if (amdgpu_gart_size != -1) {
  869. /* gtt size must be power of two and greater or equal to 32M */
  870. if (amdgpu_gart_size < 32) {
  871. dev_warn(adev->dev, "gart size (%d) too small\n",
  872. amdgpu_gart_size);
  873. amdgpu_gart_size = -1;
  874. } else if (!amdgpu_check_pot_argument(amdgpu_gart_size)) {
  875. dev_warn(adev->dev, "gart size (%d) must be a power of 2\n",
  876. amdgpu_gart_size);
  877. amdgpu_gart_size = -1;
  878. }
  879. }
  880. if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
  881. dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
  882. amdgpu_vm_size);
  883. amdgpu_vm_size = 4;
  884. }
  885. if (amdgpu_vm_size < 1) {
  886. dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
  887. amdgpu_vm_size);
  888. amdgpu_vm_size = 4;
  889. }
  890. /*
  891. * Max GPUVM size for Cayman, SI and CI are 40 bits.
  892. */
  893. if (amdgpu_vm_size > 1024) {
  894. dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
  895. amdgpu_vm_size);
  896. amdgpu_vm_size = 4;
  897. }
  898. /* defines number of bits in page table versus page directory,
  899. * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
  900. * page table and the remaining bits are in the page directory */
  901. if (amdgpu_vm_block_size == -1) {
  902. /* Total bits covered by PD + PTs */
  903. unsigned bits = ilog2(amdgpu_vm_size) + 18;
  904. /* Make sure the PD is 4K in size up to 8GB address space.
  905. Above that split equal between PD and PTs */
  906. if (amdgpu_vm_size <= 8)
  907. amdgpu_vm_block_size = bits - 9;
  908. else
  909. amdgpu_vm_block_size = (bits + 3) / 2;
  910. } else if (amdgpu_vm_block_size < 9) {
  911. dev_warn(adev->dev, "VM page table size (%d) too small\n",
  912. amdgpu_vm_block_size);
  913. amdgpu_vm_block_size = 9;
  914. }
  915. if (amdgpu_vm_block_size > 24 ||
  916. (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
  917. dev_warn(adev->dev, "VM page table size (%d) too large\n",
  918. amdgpu_vm_block_size);
  919. amdgpu_vm_block_size = 9;
  920. }
  921. }
  922. /**
  923. * amdgpu_switcheroo_set_state - set switcheroo state
  924. *
  925. * @pdev: pci dev pointer
  926. * @state: vga switcheroo state
  927. *
  928. * Callback for the switcheroo driver. Suspends or resumes the
  929. * the asics before or after it is powered up using ACPI methods.
  930. */
  931. static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  932. {
  933. struct drm_device *dev = pci_get_drvdata(pdev);
  934. if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
  935. return;
  936. if (state == VGA_SWITCHEROO_ON) {
  937. unsigned d3_delay = dev->pdev->d3_delay;
  938. printk(KERN_INFO "amdgpu: switched on\n");
  939. /* don't suspend or resume card normally */
  940. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  941. amdgpu_resume_kms(dev, true, true);
  942. dev->pdev->d3_delay = d3_delay;
  943. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  944. drm_kms_helper_poll_enable(dev);
  945. } else {
  946. printk(KERN_INFO "amdgpu: switched off\n");
  947. drm_kms_helper_poll_disable(dev);
  948. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  949. amdgpu_suspend_kms(dev, true, true);
  950. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  951. }
  952. }
  953. /**
  954. * amdgpu_switcheroo_can_switch - see if switcheroo state can change
  955. *
  956. * @pdev: pci dev pointer
  957. *
  958. * Callback for the switcheroo driver. Check of the switcheroo
  959. * state can be changed.
  960. * Returns true if the state can be changed, false if not.
  961. */
  962. static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
  963. {
  964. struct drm_device *dev = pci_get_drvdata(pdev);
  965. /*
  966. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  967. * locking inversion with the driver load path. And the access here is
  968. * completely racy anyway. So don't bother with locking for now.
  969. */
  970. return dev->open_count == 0;
  971. }
  972. static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
  973. .set_gpu_state = amdgpu_switcheroo_set_state,
  974. .reprobe = NULL,
  975. .can_switch = amdgpu_switcheroo_can_switch,
  976. };
  977. int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
  978. enum amdgpu_ip_block_type block_type,
  979. enum amdgpu_clockgating_state state)
  980. {
  981. int i, r = 0;
  982. for (i = 0; i < adev->num_ip_blocks; i++) {
  983. if (adev->ip_blocks[i].type == block_type) {
  984. r = adev->ip_blocks[i].funcs->set_clockgating_state(adev,
  985. state);
  986. if (r)
  987. return r;
  988. }
  989. }
  990. return r;
  991. }
  992. int amdgpu_set_powergating_state(struct amdgpu_device *adev,
  993. enum amdgpu_ip_block_type block_type,
  994. enum amdgpu_powergating_state state)
  995. {
  996. int i, r = 0;
  997. for (i = 0; i < adev->num_ip_blocks; i++) {
  998. if (adev->ip_blocks[i].type == block_type) {
  999. r = adev->ip_blocks[i].funcs->set_powergating_state(adev,
  1000. state);
  1001. if (r)
  1002. return r;
  1003. }
  1004. }
  1005. return r;
  1006. }
  1007. const struct amdgpu_ip_block_version * amdgpu_get_ip_block(
  1008. struct amdgpu_device *adev,
  1009. enum amdgpu_ip_block_type type)
  1010. {
  1011. int i;
  1012. for (i = 0; i < adev->num_ip_blocks; i++)
  1013. if (adev->ip_blocks[i].type == type)
  1014. return &adev->ip_blocks[i];
  1015. return NULL;
  1016. }
  1017. /**
  1018. * amdgpu_ip_block_version_cmp
  1019. *
  1020. * @adev: amdgpu_device pointer
  1021. * @type: enum amdgpu_ip_block_type
  1022. * @major: major version
  1023. * @minor: minor version
  1024. *
  1025. * return 0 if equal or greater
  1026. * return 1 if smaller or the ip_block doesn't exist
  1027. */
  1028. int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
  1029. enum amdgpu_ip_block_type type,
  1030. u32 major, u32 minor)
  1031. {
  1032. const struct amdgpu_ip_block_version *ip_block;
  1033. ip_block = amdgpu_get_ip_block(adev, type);
  1034. if (ip_block && ((ip_block->major > major) ||
  1035. ((ip_block->major == major) &&
  1036. (ip_block->minor >= minor))))
  1037. return 0;
  1038. return 1;
  1039. }
  1040. static int amdgpu_early_init(struct amdgpu_device *adev)
  1041. {
  1042. int i, r = -EINVAL;
  1043. switch (adev->asic_type) {
  1044. default:
  1045. /* FIXME: not supported yet */
  1046. return -EINVAL;
  1047. }
  1048. if (adev->ip_blocks == NULL) {
  1049. DRM_ERROR("No IP blocks found!\n");
  1050. return r;
  1051. }
  1052. for (i = 0; i < adev->num_ip_blocks; i++) {
  1053. if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
  1054. DRM_ERROR("disabled ip block: %d\n", i);
  1055. adev->ip_block_enabled[i] = false;
  1056. } else {
  1057. if (adev->ip_blocks[i].funcs->early_init) {
  1058. r = adev->ip_blocks[i].funcs->early_init(adev);
  1059. if (r)
  1060. return r;
  1061. }
  1062. adev->ip_block_enabled[i] = true;
  1063. }
  1064. }
  1065. return 0;
  1066. }
  1067. static int amdgpu_init(struct amdgpu_device *adev)
  1068. {
  1069. int i, r;
  1070. for (i = 0; i < adev->num_ip_blocks; i++) {
  1071. if (!adev->ip_block_enabled[i])
  1072. continue;
  1073. r = adev->ip_blocks[i].funcs->sw_init(adev);
  1074. if (r)
  1075. return r;
  1076. /* need to do gmc hw init early so we can allocate gpu mem */
  1077. if (adev->ip_blocks[i].type == AMDGPU_IP_BLOCK_TYPE_GMC) {
  1078. r = amdgpu_vram_scratch_init(adev);
  1079. if (r)
  1080. return r;
  1081. r = adev->ip_blocks[i].funcs->hw_init(adev);
  1082. if (r)
  1083. return r;
  1084. r = amdgpu_wb_init(adev);
  1085. if (r)
  1086. return r;
  1087. }
  1088. }
  1089. for (i = 0; i < adev->num_ip_blocks; i++) {
  1090. if (!adev->ip_block_enabled[i])
  1091. continue;
  1092. /* gmc hw init is done early */
  1093. if (adev->ip_blocks[i].type == AMDGPU_IP_BLOCK_TYPE_GMC)
  1094. continue;
  1095. r = adev->ip_blocks[i].funcs->hw_init(adev);
  1096. if (r)
  1097. return r;
  1098. }
  1099. return 0;
  1100. }
  1101. static int amdgpu_late_init(struct amdgpu_device *adev)
  1102. {
  1103. int i = 0, r;
  1104. for (i = 0; i < adev->num_ip_blocks; i++) {
  1105. if (!adev->ip_block_enabled[i])
  1106. continue;
  1107. /* enable clockgating to save power */
  1108. r = adev->ip_blocks[i].funcs->set_clockgating_state(adev,
  1109. AMDGPU_CG_STATE_GATE);
  1110. if (r)
  1111. return r;
  1112. if (adev->ip_blocks[i].funcs->late_init) {
  1113. r = adev->ip_blocks[i].funcs->late_init(adev);
  1114. if (r)
  1115. return r;
  1116. }
  1117. }
  1118. return 0;
  1119. }
  1120. static int amdgpu_fini(struct amdgpu_device *adev)
  1121. {
  1122. int i, r;
  1123. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1124. if (!adev->ip_block_enabled[i])
  1125. continue;
  1126. if (adev->ip_blocks[i].type == AMDGPU_IP_BLOCK_TYPE_GMC) {
  1127. amdgpu_wb_fini(adev);
  1128. amdgpu_vram_scratch_fini(adev);
  1129. }
  1130. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1131. r = adev->ip_blocks[i].funcs->set_clockgating_state(adev,
  1132. AMDGPU_CG_STATE_UNGATE);
  1133. if (r)
  1134. return r;
  1135. r = adev->ip_blocks[i].funcs->hw_fini(adev);
  1136. /* XXX handle errors */
  1137. }
  1138. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1139. if (!adev->ip_block_enabled[i])
  1140. continue;
  1141. r = adev->ip_blocks[i].funcs->sw_fini(adev);
  1142. /* XXX handle errors */
  1143. adev->ip_block_enabled[i] = false;
  1144. }
  1145. return 0;
  1146. }
  1147. static int amdgpu_suspend(struct amdgpu_device *adev)
  1148. {
  1149. int i, r;
  1150. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1151. if (!adev->ip_block_enabled[i])
  1152. continue;
  1153. /* ungate blocks so that suspend can properly shut them down */
  1154. r = adev->ip_blocks[i].funcs->set_clockgating_state(adev,
  1155. AMDGPU_CG_STATE_UNGATE);
  1156. /* XXX handle errors */
  1157. r = adev->ip_blocks[i].funcs->suspend(adev);
  1158. /* XXX handle errors */
  1159. }
  1160. return 0;
  1161. }
  1162. static int amdgpu_resume(struct amdgpu_device *adev)
  1163. {
  1164. int i, r;
  1165. for (i = 0; i < adev->num_ip_blocks; i++) {
  1166. if (!adev->ip_block_enabled[i])
  1167. continue;
  1168. r = adev->ip_blocks[i].funcs->resume(adev);
  1169. if (r)
  1170. return r;
  1171. }
  1172. return 0;
  1173. }
  1174. /**
  1175. * amdgpu_device_init - initialize the driver
  1176. *
  1177. * @adev: amdgpu_device pointer
  1178. * @pdev: drm dev pointer
  1179. * @pdev: pci dev pointer
  1180. * @flags: driver flags
  1181. *
  1182. * Initializes the driver info and hw (all asics).
  1183. * Returns 0 for success or an error on failure.
  1184. * Called at driver startup.
  1185. */
  1186. int amdgpu_device_init(struct amdgpu_device *adev,
  1187. struct drm_device *ddev,
  1188. struct pci_dev *pdev,
  1189. uint32_t flags)
  1190. {
  1191. int r, i;
  1192. bool runtime = false;
  1193. adev->shutdown = false;
  1194. adev->dev = &pdev->dev;
  1195. adev->ddev = ddev;
  1196. adev->pdev = pdev;
  1197. adev->flags = flags;
  1198. adev->asic_type = flags & AMDGPU_ASIC_MASK;
  1199. adev->is_atom_bios = false;
  1200. adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
  1201. adev->mc.gtt_size = 512 * 1024 * 1024;
  1202. adev->accel_working = false;
  1203. adev->num_rings = 0;
  1204. adev->mman.buffer_funcs = NULL;
  1205. adev->mman.buffer_funcs_ring = NULL;
  1206. adev->vm_manager.vm_pte_funcs = NULL;
  1207. adev->vm_manager.vm_pte_funcs_ring = NULL;
  1208. adev->gart.gart_funcs = NULL;
  1209. adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
  1210. adev->smc_rreg = &amdgpu_invalid_rreg;
  1211. adev->smc_wreg = &amdgpu_invalid_wreg;
  1212. adev->pcie_rreg = &amdgpu_invalid_rreg;
  1213. adev->pcie_wreg = &amdgpu_invalid_wreg;
  1214. adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
  1215. adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
  1216. adev->didt_rreg = &amdgpu_invalid_rreg;
  1217. adev->didt_wreg = &amdgpu_invalid_wreg;
  1218. adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
  1219. adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
  1220. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
  1221. amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
  1222. pdev->subsystem_vendor, pdev->subsystem_device);
  1223. /* mutex initialization are all done here so we
  1224. * can recall function without having locking issues */
  1225. mutex_init(&adev->ring_lock);
  1226. atomic_set(&adev->irq.ih.lock, 0);
  1227. mutex_init(&adev->gem.mutex);
  1228. mutex_init(&adev->pm.mutex);
  1229. mutex_init(&adev->gfx.gpu_clock_mutex);
  1230. mutex_init(&adev->srbm_mutex);
  1231. mutex_init(&adev->grbm_idx_mutex);
  1232. init_rwsem(&adev->pm.mclk_lock);
  1233. init_rwsem(&adev->exclusive_lock);
  1234. mutex_init(&adev->mn_lock);
  1235. hash_init(adev->mn_hash);
  1236. amdgpu_check_arguments(adev);
  1237. /* Registers mapping */
  1238. /* TODO: block userspace mapping of io register */
  1239. spin_lock_init(&adev->mmio_idx_lock);
  1240. spin_lock_init(&adev->smc_idx_lock);
  1241. spin_lock_init(&adev->pcie_idx_lock);
  1242. spin_lock_init(&adev->uvd_ctx_idx_lock);
  1243. spin_lock_init(&adev->didt_idx_lock);
  1244. spin_lock_init(&adev->audio_endpt_idx_lock);
  1245. adev->rmmio_base = pci_resource_start(adev->pdev, 5);
  1246. adev->rmmio_size = pci_resource_len(adev->pdev, 5);
  1247. adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
  1248. if (adev->rmmio == NULL) {
  1249. return -ENOMEM;
  1250. }
  1251. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
  1252. DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  1253. /* doorbell bar mapping */
  1254. amdgpu_doorbell_init(adev);
  1255. /* io port mapping */
  1256. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1257. if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
  1258. adev->rio_mem_size = pci_resource_len(adev->pdev, i);
  1259. adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
  1260. break;
  1261. }
  1262. }
  1263. if (adev->rio_mem == NULL)
  1264. DRM_ERROR("Unable to find PCI I/O BAR\n");
  1265. /* early init functions */
  1266. r = amdgpu_early_init(adev);
  1267. if (r)
  1268. return r;
  1269. /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
  1270. /* this will fail for cards that aren't VGA class devices, just
  1271. * ignore it */
  1272. vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
  1273. if (amdgpu_runtime_pm == 1)
  1274. runtime = true;
  1275. if (amdgpu_device_is_px(ddev))
  1276. runtime = true;
  1277. vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
  1278. if (runtime)
  1279. vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
  1280. /* Read BIOS */
  1281. if (!amdgpu_get_bios(adev))
  1282. return -EINVAL;
  1283. /* Must be an ATOMBIOS */
  1284. if (!adev->is_atom_bios) {
  1285. dev_err(adev->dev, "Expecting atombios for GPU\n");
  1286. return -EINVAL;
  1287. }
  1288. r = amdgpu_atombios_init(adev);
  1289. if (r)
  1290. return r;
  1291. /* Post card if necessary */
  1292. if (!amdgpu_card_posted(adev)) {
  1293. if (!adev->bios) {
  1294. dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
  1295. return -EINVAL;
  1296. }
  1297. DRM_INFO("GPU not posted. posting now...\n");
  1298. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  1299. }
  1300. /* Initialize clocks */
  1301. r = amdgpu_atombios_get_clock_info(adev);
  1302. if (r)
  1303. return r;
  1304. /* init i2c buses */
  1305. amdgpu_atombios_i2c_init(adev);
  1306. /* Fence driver */
  1307. r = amdgpu_fence_driver_init(adev);
  1308. if (r)
  1309. return r;
  1310. /* init the mode config */
  1311. drm_mode_config_init(adev->ddev);
  1312. r = amdgpu_init(adev);
  1313. if (r) {
  1314. amdgpu_fini(adev);
  1315. return r;
  1316. }
  1317. adev->accel_working = true;
  1318. amdgpu_fbdev_init(adev);
  1319. r = amdgpu_ib_pool_init(adev);
  1320. if (r) {
  1321. dev_err(adev->dev, "IB initialization failed (%d).\n", r);
  1322. return r;
  1323. }
  1324. r = amdgpu_ib_ring_tests(adev);
  1325. if (r)
  1326. DRM_ERROR("ib ring test failed (%d).\n", r);
  1327. r = amdgpu_gem_debugfs_init(adev);
  1328. if (r) {
  1329. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  1330. }
  1331. r = amdgpu_debugfs_regs_init(adev);
  1332. if (r) {
  1333. DRM_ERROR("registering register debugfs failed (%d).\n", r);
  1334. }
  1335. if ((amdgpu_testing & 1)) {
  1336. if (adev->accel_working)
  1337. amdgpu_test_moves(adev);
  1338. else
  1339. DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
  1340. }
  1341. if ((amdgpu_testing & 2)) {
  1342. if (adev->accel_working)
  1343. amdgpu_test_syncing(adev);
  1344. else
  1345. DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
  1346. }
  1347. if (amdgpu_benchmarking) {
  1348. if (adev->accel_working)
  1349. amdgpu_benchmark(adev, amdgpu_benchmarking);
  1350. else
  1351. DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
  1352. }
  1353. /* enable clockgating, etc. after ib tests, etc. since some blocks require
  1354. * explicit gating rather than handling it automatically.
  1355. */
  1356. r = amdgpu_late_init(adev);
  1357. if (r)
  1358. return r;
  1359. return 0;
  1360. }
  1361. static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
  1362. /**
  1363. * amdgpu_device_fini - tear down the driver
  1364. *
  1365. * @adev: amdgpu_device pointer
  1366. *
  1367. * Tear down the driver info (all asics).
  1368. * Called at driver shutdown.
  1369. */
  1370. void amdgpu_device_fini(struct amdgpu_device *adev)
  1371. {
  1372. int r;
  1373. DRM_INFO("amdgpu: finishing device.\n");
  1374. adev->shutdown = true;
  1375. /* evict vram memory */
  1376. amdgpu_bo_evict_vram(adev);
  1377. amdgpu_ib_pool_fini(adev);
  1378. amdgpu_fence_driver_fini(adev);
  1379. amdgpu_fbdev_fini(adev);
  1380. r = amdgpu_fini(adev);
  1381. if (adev->ip_block_enabled)
  1382. kfree(adev->ip_block_enabled);
  1383. adev->ip_block_enabled = NULL;
  1384. adev->accel_working = false;
  1385. /* free i2c buses */
  1386. amdgpu_i2c_fini(adev);
  1387. amdgpu_atombios_fini(adev);
  1388. kfree(adev->bios);
  1389. adev->bios = NULL;
  1390. vga_switcheroo_unregister_client(adev->pdev);
  1391. vga_client_register(adev->pdev, NULL, NULL, NULL);
  1392. if (adev->rio_mem)
  1393. pci_iounmap(adev->pdev, adev->rio_mem);
  1394. adev->rio_mem = NULL;
  1395. iounmap(adev->rmmio);
  1396. adev->rmmio = NULL;
  1397. amdgpu_doorbell_fini(adev);
  1398. amdgpu_debugfs_regs_cleanup(adev);
  1399. amdgpu_debugfs_remove_files(adev);
  1400. }
  1401. /*
  1402. * Suspend & resume.
  1403. */
  1404. /**
  1405. * amdgpu_suspend_kms - initiate device suspend
  1406. *
  1407. * @pdev: drm dev pointer
  1408. * @state: suspend state
  1409. *
  1410. * Puts the hw in the suspend state (all asics).
  1411. * Returns 0 for success or an error on failure.
  1412. * Called at driver suspend.
  1413. */
  1414. int amdgpu_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
  1415. {
  1416. struct amdgpu_device *adev;
  1417. struct drm_crtc *crtc;
  1418. struct drm_connector *connector;
  1419. int i, r;
  1420. bool force_completion = false;
  1421. if (dev == NULL || dev->dev_private == NULL) {
  1422. return -ENODEV;
  1423. }
  1424. adev = dev->dev_private;
  1425. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1426. return 0;
  1427. drm_kms_helper_poll_disable(dev);
  1428. /* turn off display hw */
  1429. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1430. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  1431. }
  1432. /* unpin the front buffers */
  1433. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1434. struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
  1435. struct amdgpu_bo *robj;
  1436. if (rfb == NULL || rfb->obj == NULL) {
  1437. continue;
  1438. }
  1439. robj = gem_to_amdgpu_bo(rfb->obj);
  1440. /* don't unpin kernel fb objects */
  1441. if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
  1442. r = amdgpu_bo_reserve(robj, false);
  1443. if (r == 0) {
  1444. amdgpu_bo_unpin(robj);
  1445. amdgpu_bo_unreserve(robj);
  1446. }
  1447. }
  1448. }
  1449. /* evict vram memory */
  1450. amdgpu_bo_evict_vram(adev);
  1451. /* wait for gpu to finish processing current batch */
  1452. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  1453. struct amdgpu_ring *ring = adev->rings[i];
  1454. if (!ring)
  1455. continue;
  1456. r = amdgpu_fence_wait_empty(ring);
  1457. if (r) {
  1458. /* delay GPU reset to resume */
  1459. force_completion = true;
  1460. }
  1461. }
  1462. if (force_completion) {
  1463. amdgpu_fence_driver_force_completion(adev);
  1464. }
  1465. r = amdgpu_suspend(adev);
  1466. /* evict remaining vram memory */
  1467. amdgpu_bo_evict_vram(adev);
  1468. pci_save_state(dev->pdev);
  1469. if (suspend) {
  1470. /* Shut down the device */
  1471. pci_disable_device(dev->pdev);
  1472. pci_set_power_state(dev->pdev, PCI_D3hot);
  1473. }
  1474. if (fbcon) {
  1475. console_lock();
  1476. amdgpu_fbdev_set_suspend(adev, 1);
  1477. console_unlock();
  1478. }
  1479. return 0;
  1480. }
  1481. /**
  1482. * amdgpu_resume_kms - initiate device resume
  1483. *
  1484. * @pdev: drm dev pointer
  1485. *
  1486. * Bring the hw back to operating state (all asics).
  1487. * Returns 0 for success or an error on failure.
  1488. * Called at driver resume.
  1489. */
  1490. int amdgpu_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
  1491. {
  1492. struct drm_connector *connector;
  1493. struct amdgpu_device *adev = dev->dev_private;
  1494. int r;
  1495. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1496. return 0;
  1497. if (fbcon) {
  1498. console_lock();
  1499. }
  1500. if (resume) {
  1501. pci_set_power_state(dev->pdev, PCI_D0);
  1502. pci_restore_state(dev->pdev);
  1503. if (pci_enable_device(dev->pdev)) {
  1504. if (fbcon)
  1505. console_unlock();
  1506. return -1;
  1507. }
  1508. }
  1509. /* post card */
  1510. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  1511. r = amdgpu_resume(adev);
  1512. r = amdgpu_ib_ring_tests(adev);
  1513. if (r)
  1514. DRM_ERROR("ib ring test failed (%d).\n", r);
  1515. r = amdgpu_late_init(adev);
  1516. if (r)
  1517. return r;
  1518. /* blat the mode back in */
  1519. if (fbcon) {
  1520. drm_helper_resume_force_mode(dev);
  1521. /* turn on display hw */
  1522. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1523. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  1524. }
  1525. }
  1526. drm_kms_helper_poll_enable(dev);
  1527. if (fbcon) {
  1528. amdgpu_fbdev_set_suspend(adev, 0);
  1529. console_unlock();
  1530. }
  1531. return 0;
  1532. }
  1533. /**
  1534. * amdgpu_gpu_reset - reset the asic
  1535. *
  1536. * @adev: amdgpu device pointer
  1537. *
  1538. * Attempt the reset the GPU if it has hung (all asics).
  1539. * Returns 0 for success or an error on failure.
  1540. */
  1541. int amdgpu_gpu_reset(struct amdgpu_device *adev)
  1542. {
  1543. unsigned ring_sizes[AMDGPU_MAX_RINGS];
  1544. uint32_t *ring_data[AMDGPU_MAX_RINGS];
  1545. bool saved = false;
  1546. int i, r;
  1547. int resched;
  1548. down_write(&adev->exclusive_lock);
  1549. if (!adev->needs_reset) {
  1550. up_write(&adev->exclusive_lock);
  1551. return 0;
  1552. }
  1553. adev->needs_reset = false;
  1554. /* block TTM */
  1555. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  1556. r = amdgpu_suspend(adev);
  1557. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1558. struct amdgpu_ring *ring = adev->rings[i];
  1559. if (!ring)
  1560. continue;
  1561. ring_sizes[i] = amdgpu_ring_backup(ring, &ring_data[i]);
  1562. if (ring_sizes[i]) {
  1563. saved = true;
  1564. dev_info(adev->dev, "Saved %d dwords of commands "
  1565. "on ring %d.\n", ring_sizes[i], i);
  1566. }
  1567. }
  1568. retry:
  1569. r = amdgpu_asic_reset(adev);
  1570. if (!r) {
  1571. dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
  1572. r = amdgpu_resume(adev);
  1573. }
  1574. if (!r) {
  1575. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1576. struct amdgpu_ring *ring = adev->rings[i];
  1577. if (!ring)
  1578. continue;
  1579. amdgpu_ring_restore(ring, ring_sizes[i], ring_data[i]);
  1580. ring_sizes[i] = 0;
  1581. ring_data[i] = NULL;
  1582. }
  1583. r = amdgpu_ib_ring_tests(adev);
  1584. if (r) {
  1585. dev_err(adev->dev, "ib ring test failed (%d).\n", r);
  1586. if (saved) {
  1587. saved = false;
  1588. r = amdgpu_suspend(adev);
  1589. goto retry;
  1590. }
  1591. }
  1592. } else {
  1593. amdgpu_fence_driver_force_completion(adev);
  1594. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1595. if (adev->rings[i])
  1596. kfree(ring_data[i]);
  1597. }
  1598. }
  1599. drm_helper_resume_force_mode(adev->ddev);
  1600. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  1601. if (r) {
  1602. /* bad news, how to tell it to userspace ? */
  1603. dev_info(adev->dev, "GPU reset failed\n");
  1604. }
  1605. up_write(&adev->exclusive_lock);
  1606. return r;
  1607. }
  1608. /*
  1609. * Debugfs
  1610. */
  1611. int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
  1612. struct drm_info_list *files,
  1613. unsigned nfiles)
  1614. {
  1615. unsigned i;
  1616. for (i = 0; i < adev->debugfs_count; i++) {
  1617. if (adev->debugfs[i].files == files) {
  1618. /* Already registered */
  1619. return 0;
  1620. }
  1621. }
  1622. i = adev->debugfs_count + 1;
  1623. if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
  1624. DRM_ERROR("Reached maximum number of debugfs components.\n");
  1625. DRM_ERROR("Report so we increase "
  1626. "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
  1627. return -EINVAL;
  1628. }
  1629. adev->debugfs[adev->debugfs_count].files = files;
  1630. adev->debugfs[adev->debugfs_count].num_files = nfiles;
  1631. adev->debugfs_count = i;
  1632. #if defined(CONFIG_DEBUG_FS)
  1633. drm_debugfs_create_files(files, nfiles,
  1634. adev->ddev->control->debugfs_root,
  1635. adev->ddev->control);
  1636. drm_debugfs_create_files(files, nfiles,
  1637. adev->ddev->primary->debugfs_root,
  1638. adev->ddev->primary);
  1639. #endif
  1640. return 0;
  1641. }
  1642. static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
  1643. {
  1644. #if defined(CONFIG_DEBUG_FS)
  1645. unsigned i;
  1646. for (i = 0; i < adev->debugfs_count; i++) {
  1647. drm_debugfs_remove_files(adev->debugfs[i].files,
  1648. adev->debugfs[i].num_files,
  1649. adev->ddev->control);
  1650. drm_debugfs_remove_files(adev->debugfs[i].files,
  1651. adev->debugfs[i].num_files,
  1652. adev->ddev->primary);
  1653. }
  1654. #endif
  1655. }
  1656. #if defined(CONFIG_DEBUG_FS)
  1657. static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
  1658. size_t size, loff_t *pos)
  1659. {
  1660. struct amdgpu_device *adev = f->f_inode->i_private;
  1661. ssize_t result = 0;
  1662. int r;
  1663. if (size & 0x3 || *pos & 0x3)
  1664. return -EINVAL;
  1665. while (size) {
  1666. uint32_t value;
  1667. if (*pos > adev->rmmio_size)
  1668. return result;
  1669. value = RREG32(*pos >> 2);
  1670. r = put_user(value, (uint32_t *)buf);
  1671. if (r)
  1672. return r;
  1673. result += 4;
  1674. buf += 4;
  1675. *pos += 4;
  1676. size -= 4;
  1677. }
  1678. return result;
  1679. }
  1680. static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
  1681. size_t size, loff_t *pos)
  1682. {
  1683. struct amdgpu_device *adev = f->f_inode->i_private;
  1684. ssize_t result = 0;
  1685. int r;
  1686. if (size & 0x3 || *pos & 0x3)
  1687. return -EINVAL;
  1688. while (size) {
  1689. uint32_t value;
  1690. if (*pos > adev->rmmio_size)
  1691. return result;
  1692. r = get_user(value, (uint32_t *)buf);
  1693. if (r)
  1694. return r;
  1695. WREG32(*pos >> 2, value);
  1696. result += 4;
  1697. buf += 4;
  1698. *pos += 4;
  1699. size -= 4;
  1700. }
  1701. return result;
  1702. }
  1703. static const struct file_operations amdgpu_debugfs_regs_fops = {
  1704. .owner = THIS_MODULE,
  1705. .read = amdgpu_debugfs_regs_read,
  1706. .write = amdgpu_debugfs_regs_write,
  1707. .llseek = default_llseek
  1708. };
  1709. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
  1710. {
  1711. struct drm_minor *minor = adev->ddev->primary;
  1712. struct dentry *ent, *root = minor->debugfs_root;
  1713. ent = debugfs_create_file("amdgpu_regs", S_IFREG | S_IRUGO, root,
  1714. adev, &amdgpu_debugfs_regs_fops);
  1715. if (IS_ERR(ent))
  1716. return PTR_ERR(ent);
  1717. i_size_write(ent->d_inode, adev->rmmio_size);
  1718. adev->debugfs_regs = ent;
  1719. return 0;
  1720. }
  1721. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
  1722. {
  1723. debugfs_remove(adev->debugfs_regs);
  1724. adev->debugfs_regs = NULL;
  1725. }
  1726. int amdgpu_debugfs_init(struct drm_minor *minor)
  1727. {
  1728. return 0;
  1729. }
  1730. void amdgpu_debugfs_cleanup(struct drm_minor *minor)
  1731. {
  1732. }
  1733. #endif