amdgpu_device.c 68 KB

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