amdgpu_device.c 77 KB

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