amdgpu_device.c 86 KB

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