amdgpu_device.c 55 KB

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