amdgpu_device.c 92 KB

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