amdgpu_device.c 51 KB

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