amdgpu_device.c 55 KB

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