amdgpu_device.c 101 KB

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