amdgpu_device.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527
  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/power_supply.h>
  29. #include <linux/kthread.h>
  30. #include <linux/console.h>
  31. #include <linux/slab.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/vega12_gpu_info.bin");
  62. MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
  63. MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
  64. MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
  65. #define AMDGPU_RESUME_MS 2000
  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. "VEGAM",
  86. "VEGA10",
  87. "VEGA12",
  88. "VEGA20",
  89. "RAVEN",
  90. "LAST",
  91. };
  92. static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
  93. /**
  94. * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control
  95. *
  96. * @dev: drm_device pointer
  97. *
  98. * Returns true if the device is a dGPU with HG/PX power control,
  99. * otherwise return false.
  100. */
  101. bool amdgpu_device_is_px(struct drm_device *dev)
  102. {
  103. struct amdgpu_device *adev = dev->dev_private;
  104. if (adev->flags & AMD_IS_PX)
  105. return true;
  106. return false;
  107. }
  108. /*
  109. * MMIO register access helper functions.
  110. */
  111. /**
  112. * amdgpu_mm_rreg - read a memory mapped IO register
  113. *
  114. * @adev: amdgpu_device pointer
  115. * @reg: dword aligned register offset
  116. * @acc_flags: access flags which require special behavior
  117. *
  118. * Returns the 32 bit value from the offset specified.
  119. */
  120. uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
  121. uint32_t acc_flags)
  122. {
  123. uint32_t ret;
  124. if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
  125. return amdgpu_virt_kiq_rreg(adev, reg);
  126. if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
  127. ret = readl(((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. ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  133. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  134. }
  135. trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
  136. return ret;
  137. }
  138. /*
  139. * MMIO register read with bytes helper functions
  140. * @offset:bytes offset from MMIO start
  141. *
  142. */
  143. /**
  144. * amdgpu_mm_rreg8 - read a memory mapped IO register
  145. *
  146. * @adev: amdgpu_device pointer
  147. * @offset: byte aligned register offset
  148. *
  149. * Returns the 8 bit value from the offset specified.
  150. */
  151. uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
  152. if (offset < adev->rmmio_size)
  153. return (readb(adev->rmmio + offset));
  154. BUG();
  155. }
  156. /*
  157. * MMIO register write with bytes helper functions
  158. * @offset:bytes offset from MMIO start
  159. * @value: the value want to be written to the register
  160. *
  161. */
  162. /**
  163. * amdgpu_mm_wreg8 - read a memory mapped IO register
  164. *
  165. * @adev: amdgpu_device pointer
  166. * @offset: byte aligned register offset
  167. * @value: 8 bit value to write
  168. *
  169. * Writes the value specified to the offset specified.
  170. */
  171. void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
  172. if (offset < adev->rmmio_size)
  173. writeb(value, adev->rmmio + offset);
  174. else
  175. BUG();
  176. }
  177. /**
  178. * amdgpu_mm_wreg - write to a memory mapped IO register
  179. *
  180. * @adev: amdgpu_device pointer
  181. * @reg: dword aligned register offset
  182. * @v: 32 bit value to write to the register
  183. * @acc_flags: access flags which require special behavior
  184. *
  185. * Writes the value specified to the offset specified.
  186. */
  187. void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
  188. uint32_t acc_flags)
  189. {
  190. trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
  191. if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
  192. adev->last_mm_index = v;
  193. }
  194. if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
  195. return amdgpu_virt_kiq_wreg(adev, reg, v);
  196. if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
  197. writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
  198. else {
  199. unsigned long flags;
  200. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  201. writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
  202. writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  203. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  204. }
  205. if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
  206. udelay(500);
  207. }
  208. }
  209. /**
  210. * amdgpu_io_rreg - read an IO register
  211. *
  212. * @adev: amdgpu_device pointer
  213. * @reg: dword aligned register offset
  214. *
  215. * Returns the 32 bit value from the offset specified.
  216. */
  217. u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
  218. {
  219. if ((reg * 4) < adev->rio_mem_size)
  220. return ioread32(adev->rio_mem + (reg * 4));
  221. else {
  222. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  223. return ioread32(adev->rio_mem + (mmMM_DATA * 4));
  224. }
  225. }
  226. /**
  227. * amdgpu_io_wreg - write to an IO register
  228. *
  229. * @adev: amdgpu_device pointer
  230. * @reg: dword aligned register offset
  231. * @v: 32 bit value to write to the register
  232. *
  233. * Writes the value specified to the offset specified.
  234. */
  235. void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
  236. {
  237. if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
  238. adev->last_mm_index = v;
  239. }
  240. if ((reg * 4) < adev->rio_mem_size)
  241. iowrite32(v, adev->rio_mem + (reg * 4));
  242. else {
  243. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  244. iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
  245. }
  246. if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
  247. udelay(500);
  248. }
  249. }
  250. /**
  251. * amdgpu_mm_rdoorbell - read a doorbell dword
  252. *
  253. * @adev: amdgpu_device pointer
  254. * @index: doorbell index
  255. *
  256. * Returns the value in the doorbell aperture at the
  257. * requested doorbell index (CIK).
  258. */
  259. u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
  260. {
  261. if (index < adev->doorbell.num_doorbells) {
  262. return readl(adev->doorbell.ptr + index);
  263. } else {
  264. DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
  265. return 0;
  266. }
  267. }
  268. /**
  269. * amdgpu_mm_wdoorbell - write a doorbell dword
  270. *
  271. * @adev: amdgpu_device pointer
  272. * @index: doorbell index
  273. * @v: value to write
  274. *
  275. * Writes @v to the doorbell aperture at the
  276. * requested doorbell index (CIK).
  277. */
  278. void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
  279. {
  280. if (index < adev->doorbell.num_doorbells) {
  281. writel(v, adev->doorbell.ptr + index);
  282. } else {
  283. DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  284. }
  285. }
  286. /**
  287. * amdgpu_mm_rdoorbell64 - read a doorbell Qword
  288. *
  289. * @adev: amdgpu_device pointer
  290. * @index: doorbell index
  291. *
  292. * Returns the value in the doorbell aperture at the
  293. * requested doorbell index (VEGA10+).
  294. */
  295. u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
  296. {
  297. if (index < adev->doorbell.num_doorbells) {
  298. return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
  299. } else {
  300. DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
  301. return 0;
  302. }
  303. }
  304. /**
  305. * amdgpu_mm_wdoorbell64 - write a doorbell Qword
  306. *
  307. * @adev: amdgpu_device pointer
  308. * @index: doorbell index
  309. * @v: value to write
  310. *
  311. * Writes @v to the doorbell aperture at the
  312. * requested doorbell index (VEGA10+).
  313. */
  314. void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
  315. {
  316. if (index < adev->doorbell.num_doorbells) {
  317. atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
  318. } else {
  319. DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  320. }
  321. }
  322. /**
  323. * amdgpu_invalid_rreg - dummy reg read function
  324. *
  325. * @adev: amdgpu device pointer
  326. * @reg: offset of register
  327. *
  328. * Dummy register read function. Used for register blocks
  329. * that certain asics don't have (all asics).
  330. * Returns the value in the register.
  331. */
  332. static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
  333. {
  334. DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
  335. BUG();
  336. return 0;
  337. }
  338. /**
  339. * amdgpu_invalid_wreg - dummy reg write function
  340. *
  341. * @adev: amdgpu device pointer
  342. * @reg: offset of register
  343. * @v: value to write to the register
  344. *
  345. * Dummy register read function. Used for register blocks
  346. * that certain asics don't have (all asics).
  347. */
  348. static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
  349. {
  350. DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
  351. reg, v);
  352. BUG();
  353. }
  354. /**
  355. * amdgpu_block_invalid_rreg - dummy reg read function
  356. *
  357. * @adev: amdgpu device pointer
  358. * @block: offset of instance
  359. * @reg: offset of register
  360. *
  361. * Dummy register read function. Used for register blocks
  362. * that certain asics don't have (all asics).
  363. * Returns the value in the register.
  364. */
  365. static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
  366. uint32_t block, uint32_t reg)
  367. {
  368. DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
  369. reg, block);
  370. BUG();
  371. return 0;
  372. }
  373. /**
  374. * amdgpu_block_invalid_wreg - dummy reg write function
  375. *
  376. * @adev: amdgpu device pointer
  377. * @block: offset of instance
  378. * @reg: offset of register
  379. * @v: value to write to the register
  380. *
  381. * Dummy register read function. Used for register blocks
  382. * that certain asics don't have (all asics).
  383. */
  384. static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
  385. uint32_t block,
  386. uint32_t reg, uint32_t v)
  387. {
  388. DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
  389. reg, block, v);
  390. BUG();
  391. }
  392. /**
  393. * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
  394. *
  395. * @adev: amdgpu device pointer
  396. *
  397. * Allocates a scratch page of VRAM for use by various things in the
  398. * driver.
  399. */
  400. static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
  401. {
  402. return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
  403. PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
  404. &adev->vram_scratch.robj,
  405. &adev->vram_scratch.gpu_addr,
  406. (void **)&adev->vram_scratch.ptr);
  407. }
  408. /**
  409. * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
  410. *
  411. * @adev: amdgpu device pointer
  412. *
  413. * Frees the VRAM scratch page.
  414. */
  415. static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
  416. {
  417. amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
  418. }
  419. /**
  420. * amdgpu_device_program_register_sequence - program an array of registers.
  421. *
  422. * @adev: amdgpu_device pointer
  423. * @registers: pointer to the register array
  424. * @array_size: size of the register array
  425. *
  426. * Programs an array or registers with and and or masks.
  427. * This is a helper for setting golden registers.
  428. */
  429. void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
  430. const u32 *registers,
  431. const u32 array_size)
  432. {
  433. u32 tmp, reg, and_mask, or_mask;
  434. int i;
  435. if (array_size % 3)
  436. return;
  437. for (i = 0; i < array_size; i +=3) {
  438. reg = registers[i + 0];
  439. and_mask = registers[i + 1];
  440. or_mask = registers[i + 2];
  441. if (and_mask == 0xffffffff) {
  442. tmp = or_mask;
  443. } else {
  444. tmp = RREG32(reg);
  445. tmp &= ~and_mask;
  446. tmp |= or_mask;
  447. }
  448. WREG32(reg, tmp);
  449. }
  450. }
  451. /**
  452. * amdgpu_device_pci_config_reset - reset the GPU
  453. *
  454. * @adev: amdgpu_device pointer
  455. *
  456. * Resets the GPU using the pci config reset sequence.
  457. * Only applicable to asics prior to vega10.
  458. */
  459. void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
  460. {
  461. pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
  462. }
  463. /*
  464. * GPU doorbell aperture helpers function.
  465. */
  466. /**
  467. * amdgpu_device_doorbell_init - Init doorbell driver information.
  468. *
  469. * @adev: amdgpu_device pointer
  470. *
  471. * Init doorbell driver information (CIK)
  472. * Returns 0 on success, error on failure.
  473. */
  474. static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
  475. {
  476. /* No doorbell on SI hardware generation */
  477. if (adev->asic_type < CHIP_BONAIRE) {
  478. adev->doorbell.base = 0;
  479. adev->doorbell.size = 0;
  480. adev->doorbell.num_doorbells = 0;
  481. adev->doorbell.ptr = NULL;
  482. return 0;
  483. }
  484. if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
  485. return -EINVAL;
  486. /* doorbell bar mapping */
  487. adev->doorbell.base = pci_resource_start(adev->pdev, 2);
  488. adev->doorbell.size = pci_resource_len(adev->pdev, 2);
  489. adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
  490. AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
  491. if (adev->doorbell.num_doorbells == 0)
  492. return -EINVAL;
  493. adev->doorbell.ptr = ioremap(adev->doorbell.base,
  494. adev->doorbell.num_doorbells *
  495. sizeof(u32));
  496. if (adev->doorbell.ptr == NULL)
  497. return -ENOMEM;
  498. return 0;
  499. }
  500. /**
  501. * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
  502. *
  503. * @adev: amdgpu_device pointer
  504. *
  505. * Tear down doorbell driver information (CIK)
  506. */
  507. static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
  508. {
  509. iounmap(adev->doorbell.ptr);
  510. adev->doorbell.ptr = NULL;
  511. }
  512. /*
  513. * amdgpu_device_wb_*()
  514. * Writeback is the method by which the GPU updates special pages in memory
  515. * with the status of certain GPU events (fences, ring pointers,etc.).
  516. */
  517. /**
  518. * amdgpu_device_wb_fini - Disable Writeback and free memory
  519. *
  520. * @adev: amdgpu_device pointer
  521. *
  522. * Disables Writeback and frees the Writeback memory (all asics).
  523. * Used at driver shutdown.
  524. */
  525. static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
  526. {
  527. if (adev->wb.wb_obj) {
  528. amdgpu_bo_free_kernel(&adev->wb.wb_obj,
  529. &adev->wb.gpu_addr,
  530. (void **)&adev->wb.wb);
  531. adev->wb.wb_obj = NULL;
  532. }
  533. }
  534. /**
  535. * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
  536. *
  537. * @adev: amdgpu_device pointer
  538. *
  539. * Initializes writeback and allocates writeback memory (all asics).
  540. * Used at driver startup.
  541. * Returns 0 on success or an -error on failure.
  542. */
  543. static int amdgpu_device_wb_init(struct amdgpu_device *adev)
  544. {
  545. int r;
  546. if (adev->wb.wb_obj == NULL) {
  547. /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
  548. r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
  549. PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
  550. &adev->wb.wb_obj, &adev->wb.gpu_addr,
  551. (void **)&adev->wb.wb);
  552. if (r) {
  553. dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
  554. return r;
  555. }
  556. adev->wb.num_wb = AMDGPU_MAX_WB;
  557. memset(&adev->wb.used, 0, sizeof(adev->wb.used));
  558. /* clear wb memory */
  559. memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
  560. }
  561. return 0;
  562. }
  563. /**
  564. * amdgpu_device_wb_get - Allocate a wb entry
  565. *
  566. * @adev: amdgpu_device pointer
  567. * @wb: wb index
  568. *
  569. * Allocate a wb slot for use by the driver (all asics).
  570. * Returns 0 on success or -EINVAL on failure.
  571. */
  572. int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
  573. {
  574. unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
  575. if (offset < adev->wb.num_wb) {
  576. __set_bit(offset, adev->wb.used);
  577. *wb = offset << 3; /* convert to dw offset */
  578. return 0;
  579. } else {
  580. return -EINVAL;
  581. }
  582. }
  583. /**
  584. * amdgpu_device_wb_free - Free a wb entry
  585. *
  586. * @adev: amdgpu_device pointer
  587. * @wb: wb index
  588. *
  589. * Free a wb slot allocated for use by the driver (all asics)
  590. */
  591. void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
  592. {
  593. wb >>= 3;
  594. if (wb < adev->wb.num_wb)
  595. __clear_bit(wb, adev->wb.used);
  596. }
  597. /**
  598. * amdgpu_device_resize_fb_bar - try to resize FB BAR
  599. *
  600. * @adev: amdgpu_device pointer
  601. *
  602. * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
  603. * to fail, but if any of the BARs is not accessible after the size we abort
  604. * driver loading by returning -ENODEV.
  605. */
  606. int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
  607. {
  608. u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
  609. u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
  610. struct pci_bus *root;
  611. struct resource *res;
  612. unsigned i;
  613. u16 cmd;
  614. int r;
  615. /* Bypass for VF */
  616. if (amdgpu_sriov_vf(adev))
  617. return 0;
  618. /* Check if the root BUS has 64bit memory resources */
  619. root = adev->pdev->bus;
  620. while (root->parent)
  621. root = root->parent;
  622. pci_bus_for_each_resource(root, res, i) {
  623. if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
  624. res->start > 0x100000000ull)
  625. break;
  626. }
  627. /* Trying to resize is pointless without a root hub window above 4GB */
  628. if (!res)
  629. return 0;
  630. /* Disable memory decoding while we change the BAR addresses and size */
  631. pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
  632. pci_write_config_word(adev->pdev, PCI_COMMAND,
  633. cmd & ~PCI_COMMAND_MEMORY);
  634. /* Free the VRAM and doorbell BAR, we most likely need to move both. */
  635. amdgpu_device_doorbell_fini(adev);
  636. if (adev->asic_type >= CHIP_BONAIRE)
  637. pci_release_resource(adev->pdev, 2);
  638. pci_release_resource(adev->pdev, 0);
  639. r = pci_resize_resource(adev->pdev, 0, rbar_size);
  640. if (r == -ENOSPC)
  641. DRM_INFO("Not enough PCI address space for a large BAR.");
  642. else if (r && r != -ENOTSUPP)
  643. DRM_ERROR("Problem resizing BAR0 (%d).", r);
  644. pci_assign_unassigned_bus_resources(adev->pdev->bus);
  645. /* When the doorbell or fb BAR isn't available we have no chance of
  646. * using the device.
  647. */
  648. r = amdgpu_device_doorbell_init(adev);
  649. if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
  650. return -ENODEV;
  651. pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
  652. return 0;
  653. }
  654. /*
  655. * GPU helpers function.
  656. */
  657. /**
  658. * amdgpu_device_need_post - check if the hw need post or not
  659. *
  660. * @adev: amdgpu_device pointer
  661. *
  662. * Check if the asic has been initialized (all asics) at driver startup
  663. * or post is needed if hw reset is performed.
  664. * Returns true if need or false if not.
  665. */
  666. bool amdgpu_device_need_post(struct amdgpu_device *adev)
  667. {
  668. uint32_t reg;
  669. if (amdgpu_sriov_vf(adev))
  670. return false;
  671. if (amdgpu_passthrough(adev)) {
  672. /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
  673. * some old smc fw still need driver do vPost otherwise gpu hang, while
  674. * those smc fw version above 22.15 doesn't have this flaw, so we force
  675. * vpost executed for smc version below 22.15
  676. */
  677. if (adev->asic_type == CHIP_FIJI) {
  678. int err;
  679. uint32_t fw_ver;
  680. err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
  681. /* force vPost if error occured */
  682. if (err)
  683. return true;
  684. fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
  685. if (fw_ver < 0x00160e00)
  686. return true;
  687. }
  688. }
  689. if (adev->has_hw_reset) {
  690. adev->has_hw_reset = false;
  691. return true;
  692. }
  693. /* bios scratch used on CIK+ */
  694. if (adev->asic_type >= CHIP_BONAIRE)
  695. return amdgpu_atombios_scratch_need_asic_init(adev);
  696. /* check MEM_SIZE for older asics */
  697. reg = amdgpu_asic_get_config_memsize(adev);
  698. if ((reg != 0) && (reg != 0xffffffff))
  699. return false;
  700. return true;
  701. }
  702. /* if we get transitioned to only one device, take VGA back */
  703. /**
  704. * amdgpu_device_vga_set_decode - enable/disable vga decode
  705. *
  706. * @cookie: amdgpu_device pointer
  707. * @state: enable/disable vga decode
  708. *
  709. * Enable/disable vga decode (all asics).
  710. * Returns VGA resource flags.
  711. */
  712. static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
  713. {
  714. struct amdgpu_device *adev = cookie;
  715. amdgpu_asic_set_vga_state(adev, state);
  716. if (state)
  717. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  718. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  719. else
  720. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  721. }
  722. /**
  723. * amdgpu_device_check_block_size - validate the vm block size
  724. *
  725. * @adev: amdgpu_device pointer
  726. *
  727. * Validates the vm block size specified via module parameter.
  728. * The vm block size defines number of bits in page table versus page directory,
  729. * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
  730. * page table and the remaining bits are in the page directory.
  731. */
  732. static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
  733. {
  734. /* defines number of bits in page table versus page directory,
  735. * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
  736. * page table and the remaining bits are in the page directory */
  737. if (amdgpu_vm_block_size == -1)
  738. return;
  739. if (amdgpu_vm_block_size < 9) {
  740. dev_warn(adev->dev, "VM page table size (%d) too small\n",
  741. amdgpu_vm_block_size);
  742. amdgpu_vm_block_size = -1;
  743. }
  744. }
  745. /**
  746. * amdgpu_device_check_vm_size - validate the vm size
  747. *
  748. * @adev: amdgpu_device pointer
  749. *
  750. * Validates the vm size in GB specified via module parameter.
  751. * The VM size is the size of the GPU virtual memory space in GB.
  752. */
  753. static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
  754. {
  755. /* no need to check the default value */
  756. if (amdgpu_vm_size == -1)
  757. return;
  758. if (amdgpu_vm_size < 1) {
  759. dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
  760. amdgpu_vm_size);
  761. amdgpu_vm_size = -1;
  762. }
  763. }
  764. static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
  765. {
  766. struct sysinfo si;
  767. bool is_os_64 = (sizeof(void *) == 8) ? true : false;
  768. uint64_t total_memory;
  769. uint64_t dram_size_seven_GB = 0x1B8000000;
  770. uint64_t dram_size_three_GB = 0xB8000000;
  771. if (amdgpu_smu_memory_pool_size == 0)
  772. return;
  773. if (!is_os_64) {
  774. DRM_WARN("Not 64-bit OS, feature not supported\n");
  775. goto def_value;
  776. }
  777. si_meminfo(&si);
  778. total_memory = (uint64_t)si.totalram * si.mem_unit;
  779. if ((amdgpu_smu_memory_pool_size == 1) ||
  780. (amdgpu_smu_memory_pool_size == 2)) {
  781. if (total_memory < dram_size_three_GB)
  782. goto def_value1;
  783. } else if ((amdgpu_smu_memory_pool_size == 4) ||
  784. (amdgpu_smu_memory_pool_size == 8)) {
  785. if (total_memory < dram_size_seven_GB)
  786. goto def_value1;
  787. } else {
  788. DRM_WARN("Smu memory pool size not supported\n");
  789. goto def_value;
  790. }
  791. adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
  792. return;
  793. def_value1:
  794. DRM_WARN("No enough system memory\n");
  795. def_value:
  796. adev->pm.smu_prv_buffer_size = 0;
  797. }
  798. /**
  799. * amdgpu_device_check_arguments - validate module params
  800. *
  801. * @adev: amdgpu_device pointer
  802. *
  803. * Validates certain module parameters and updates
  804. * the associated values used by the driver (all asics).
  805. */
  806. static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
  807. {
  808. if (amdgpu_sched_jobs < 4) {
  809. dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
  810. amdgpu_sched_jobs);
  811. amdgpu_sched_jobs = 4;
  812. } else if (!is_power_of_2(amdgpu_sched_jobs)){
  813. dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
  814. amdgpu_sched_jobs);
  815. amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
  816. }
  817. if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
  818. /* gart size must be greater or equal to 32M */
  819. dev_warn(adev->dev, "gart size (%d) too small\n",
  820. amdgpu_gart_size);
  821. amdgpu_gart_size = -1;
  822. }
  823. if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
  824. /* gtt size must be greater or equal to 32M */
  825. dev_warn(adev->dev, "gtt size (%d) too small\n",
  826. amdgpu_gtt_size);
  827. amdgpu_gtt_size = -1;
  828. }
  829. /* valid range is between 4 and 9 inclusive */
  830. if (amdgpu_vm_fragment_size != -1 &&
  831. (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
  832. dev_warn(adev->dev, "valid range is between 4 and 9\n");
  833. amdgpu_vm_fragment_size = -1;
  834. }
  835. amdgpu_device_check_smu_prv_buffer_size(adev);
  836. amdgpu_device_check_vm_size(adev);
  837. amdgpu_device_check_block_size(adev);
  838. if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
  839. !is_power_of_2(amdgpu_vram_page_split))) {
  840. dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
  841. amdgpu_vram_page_split);
  842. amdgpu_vram_page_split = 1024;
  843. }
  844. if (amdgpu_lockup_timeout == 0) {
  845. dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
  846. amdgpu_lockup_timeout = 10000;
  847. }
  848. adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
  849. }
  850. /**
  851. * amdgpu_switcheroo_set_state - set switcheroo state
  852. *
  853. * @pdev: pci dev pointer
  854. * @state: vga_switcheroo state
  855. *
  856. * Callback for the switcheroo driver. Suspends or resumes the
  857. * the asics before or after it is powered up using ACPI methods.
  858. */
  859. static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  860. {
  861. struct drm_device *dev = pci_get_drvdata(pdev);
  862. if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
  863. return;
  864. if (state == VGA_SWITCHEROO_ON) {
  865. pr_info("amdgpu: switched on\n");
  866. /* don't suspend or resume card normally */
  867. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  868. amdgpu_device_resume(dev, true, true);
  869. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  870. drm_kms_helper_poll_enable(dev);
  871. } else {
  872. pr_info("amdgpu: switched off\n");
  873. drm_kms_helper_poll_disable(dev);
  874. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  875. amdgpu_device_suspend(dev, true, true);
  876. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  877. }
  878. }
  879. /**
  880. * amdgpu_switcheroo_can_switch - see if switcheroo state can change
  881. *
  882. * @pdev: pci dev pointer
  883. *
  884. * Callback for the switcheroo driver. Check of the switcheroo
  885. * state can be changed.
  886. * Returns true if the state can be changed, false if not.
  887. */
  888. static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
  889. {
  890. struct drm_device *dev = pci_get_drvdata(pdev);
  891. /*
  892. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  893. * locking inversion with the driver load path. And the access here is
  894. * completely racy anyway. So don't bother with locking for now.
  895. */
  896. return dev->open_count == 0;
  897. }
  898. static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
  899. .set_gpu_state = amdgpu_switcheroo_set_state,
  900. .reprobe = NULL,
  901. .can_switch = amdgpu_switcheroo_can_switch,
  902. };
  903. /**
  904. * amdgpu_device_ip_set_clockgating_state - set the CG state
  905. *
  906. * @dev: amdgpu_device pointer
  907. * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
  908. * @state: clockgating state (gate or ungate)
  909. *
  910. * Sets the requested clockgating state for all instances of
  911. * the hardware IP specified.
  912. * Returns the error code from the last instance.
  913. */
  914. int amdgpu_device_ip_set_clockgating_state(void *dev,
  915. enum amd_ip_block_type block_type,
  916. enum amd_clockgating_state state)
  917. {
  918. struct amdgpu_device *adev = dev;
  919. int i, r = 0;
  920. for (i = 0; i < adev->num_ip_blocks; i++) {
  921. if (!adev->ip_blocks[i].status.valid)
  922. continue;
  923. if (adev->ip_blocks[i].version->type != block_type)
  924. continue;
  925. if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
  926. continue;
  927. r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
  928. (void *)adev, state);
  929. if (r)
  930. DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
  931. adev->ip_blocks[i].version->funcs->name, r);
  932. }
  933. return r;
  934. }
  935. /**
  936. * amdgpu_device_ip_set_powergating_state - set the PG state
  937. *
  938. * @dev: amdgpu_device pointer
  939. * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
  940. * @state: powergating state (gate or ungate)
  941. *
  942. * Sets the requested powergating state for all instances of
  943. * the hardware IP specified.
  944. * Returns the error code from the last instance.
  945. */
  946. int amdgpu_device_ip_set_powergating_state(void *dev,
  947. enum amd_ip_block_type block_type,
  948. enum amd_powergating_state state)
  949. {
  950. struct amdgpu_device *adev = dev;
  951. int i, r = 0;
  952. for (i = 0; i < adev->num_ip_blocks; i++) {
  953. if (!adev->ip_blocks[i].status.valid)
  954. continue;
  955. if (adev->ip_blocks[i].version->type != block_type)
  956. continue;
  957. if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
  958. continue;
  959. r = adev->ip_blocks[i].version->funcs->set_powergating_state(
  960. (void *)adev, state);
  961. if (r)
  962. DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
  963. adev->ip_blocks[i].version->funcs->name, r);
  964. }
  965. return r;
  966. }
  967. /**
  968. * amdgpu_device_ip_get_clockgating_state - get the CG state
  969. *
  970. * @adev: amdgpu_device pointer
  971. * @flags: clockgating feature flags
  972. *
  973. * Walks the list of IPs on the device and updates the clockgating
  974. * flags for each IP.
  975. * Updates @flags with the feature flags for each hardware IP where
  976. * clockgating is enabled.
  977. */
  978. void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
  979. u32 *flags)
  980. {
  981. int i;
  982. for (i = 0; i < adev->num_ip_blocks; i++) {
  983. if (!adev->ip_blocks[i].status.valid)
  984. continue;
  985. if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
  986. adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
  987. }
  988. }
  989. /**
  990. * amdgpu_device_ip_wait_for_idle - wait for idle
  991. *
  992. * @adev: amdgpu_device pointer
  993. * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
  994. *
  995. * Waits for the request hardware IP to be idle.
  996. * Returns 0 for success or a negative error code on failure.
  997. */
  998. int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
  999. enum amd_ip_block_type block_type)
  1000. {
  1001. int i, r;
  1002. for (i = 0; i < adev->num_ip_blocks; i++) {
  1003. if (!adev->ip_blocks[i].status.valid)
  1004. continue;
  1005. if (adev->ip_blocks[i].version->type == block_type) {
  1006. r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
  1007. if (r)
  1008. return r;
  1009. break;
  1010. }
  1011. }
  1012. return 0;
  1013. }
  1014. /**
  1015. * amdgpu_device_ip_is_idle - is the hardware IP idle
  1016. *
  1017. * @adev: amdgpu_device pointer
  1018. * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
  1019. *
  1020. * Check if the hardware IP is idle or not.
  1021. * Returns true if it the IP is idle, false if not.
  1022. */
  1023. bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
  1024. enum amd_ip_block_type block_type)
  1025. {
  1026. int i;
  1027. for (i = 0; i < adev->num_ip_blocks; i++) {
  1028. if (!adev->ip_blocks[i].status.valid)
  1029. continue;
  1030. if (adev->ip_blocks[i].version->type == block_type)
  1031. return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
  1032. }
  1033. return true;
  1034. }
  1035. /**
  1036. * amdgpu_device_ip_get_ip_block - get a hw IP pointer
  1037. *
  1038. * @adev: amdgpu_device pointer
  1039. * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
  1040. *
  1041. * Returns a pointer to the hardware IP block structure
  1042. * if it exists for the asic, otherwise NULL.
  1043. */
  1044. struct amdgpu_ip_block *
  1045. amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
  1046. enum amd_ip_block_type type)
  1047. {
  1048. int i;
  1049. for (i = 0; i < adev->num_ip_blocks; i++)
  1050. if (adev->ip_blocks[i].version->type == type)
  1051. return &adev->ip_blocks[i];
  1052. return NULL;
  1053. }
  1054. /**
  1055. * amdgpu_device_ip_block_version_cmp
  1056. *
  1057. * @adev: amdgpu_device pointer
  1058. * @type: enum amd_ip_block_type
  1059. * @major: major version
  1060. * @minor: minor version
  1061. *
  1062. * return 0 if equal or greater
  1063. * return 1 if smaller or the ip_block doesn't exist
  1064. */
  1065. int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
  1066. enum amd_ip_block_type type,
  1067. u32 major, u32 minor)
  1068. {
  1069. struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
  1070. if (ip_block && ((ip_block->version->major > major) ||
  1071. ((ip_block->version->major == major) &&
  1072. (ip_block->version->minor >= minor))))
  1073. return 0;
  1074. return 1;
  1075. }
  1076. /**
  1077. * amdgpu_device_ip_block_add
  1078. *
  1079. * @adev: amdgpu_device pointer
  1080. * @ip_block_version: pointer to the IP to add
  1081. *
  1082. * Adds the IP block driver information to the collection of IPs
  1083. * on the asic.
  1084. */
  1085. int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
  1086. const struct amdgpu_ip_block_version *ip_block_version)
  1087. {
  1088. if (!ip_block_version)
  1089. return -EINVAL;
  1090. DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
  1091. ip_block_version->funcs->name);
  1092. adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
  1093. return 0;
  1094. }
  1095. /**
  1096. * amdgpu_device_enable_virtual_display - enable virtual display feature
  1097. *
  1098. * @adev: amdgpu_device pointer
  1099. *
  1100. * Enabled the virtual display feature if the user has enabled it via
  1101. * the module parameter virtual_display. This feature provides a virtual
  1102. * display hardware on headless boards or in virtualized environments.
  1103. * This function parses and validates the configuration string specified by
  1104. * the user and configues the virtual display configuration (number of
  1105. * virtual connectors, crtcs, etc.) specified.
  1106. */
  1107. static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
  1108. {
  1109. adev->enable_virtual_display = false;
  1110. if (amdgpu_virtual_display) {
  1111. struct drm_device *ddev = adev->ddev;
  1112. const char *pci_address_name = pci_name(ddev->pdev);
  1113. char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
  1114. pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
  1115. pciaddstr_tmp = pciaddstr;
  1116. while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
  1117. pciaddname = strsep(&pciaddname_tmp, ",");
  1118. if (!strcmp("all", pciaddname)
  1119. || !strcmp(pci_address_name, pciaddname)) {
  1120. long num_crtc;
  1121. int res = -1;
  1122. adev->enable_virtual_display = true;
  1123. if (pciaddname_tmp)
  1124. res = kstrtol(pciaddname_tmp, 10,
  1125. &num_crtc);
  1126. if (!res) {
  1127. if (num_crtc < 1)
  1128. num_crtc = 1;
  1129. if (num_crtc > 6)
  1130. num_crtc = 6;
  1131. adev->mode_info.num_crtc = num_crtc;
  1132. } else {
  1133. adev->mode_info.num_crtc = 1;
  1134. }
  1135. break;
  1136. }
  1137. }
  1138. DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
  1139. amdgpu_virtual_display, pci_address_name,
  1140. adev->enable_virtual_display, adev->mode_info.num_crtc);
  1141. kfree(pciaddstr);
  1142. }
  1143. }
  1144. /**
  1145. * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
  1146. *
  1147. * @adev: amdgpu_device pointer
  1148. *
  1149. * Parses the asic configuration parameters specified in the gpu info
  1150. * firmware and makes them availale to the driver for use in configuring
  1151. * the asic.
  1152. * Returns 0 on success, -EINVAL on failure.
  1153. */
  1154. static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
  1155. {
  1156. const char *chip_name;
  1157. char fw_name[30];
  1158. int err;
  1159. const struct gpu_info_firmware_header_v1_0 *hdr;
  1160. adev->firmware.gpu_info_fw = NULL;
  1161. switch (adev->asic_type) {
  1162. case CHIP_TOPAZ:
  1163. case CHIP_TONGA:
  1164. case CHIP_FIJI:
  1165. case CHIP_POLARIS10:
  1166. case CHIP_POLARIS11:
  1167. case CHIP_POLARIS12:
  1168. case CHIP_VEGAM:
  1169. case CHIP_CARRIZO:
  1170. case CHIP_STONEY:
  1171. #ifdef CONFIG_DRM_AMDGPU_SI
  1172. case CHIP_VERDE:
  1173. case CHIP_TAHITI:
  1174. case CHIP_PITCAIRN:
  1175. case CHIP_OLAND:
  1176. case CHIP_HAINAN:
  1177. #endif
  1178. #ifdef CONFIG_DRM_AMDGPU_CIK
  1179. case CHIP_BONAIRE:
  1180. case CHIP_HAWAII:
  1181. case CHIP_KAVERI:
  1182. case CHIP_KABINI:
  1183. case CHIP_MULLINS:
  1184. #endif
  1185. case CHIP_VEGA20:
  1186. default:
  1187. return 0;
  1188. case CHIP_VEGA10:
  1189. chip_name = "vega10";
  1190. break;
  1191. case CHIP_VEGA12:
  1192. chip_name = "vega12";
  1193. break;
  1194. case CHIP_RAVEN:
  1195. if (adev->rev_id >= 8)
  1196. chip_name = "raven2";
  1197. else if (adev->pdev->device == 0x15d8)
  1198. chip_name = "picasso";
  1199. else
  1200. chip_name = "raven";
  1201. break;
  1202. }
  1203. snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
  1204. err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
  1205. if (err) {
  1206. dev_err(adev->dev,
  1207. "Failed to load gpu_info firmware \"%s\"\n",
  1208. fw_name);
  1209. goto out;
  1210. }
  1211. err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
  1212. if (err) {
  1213. dev_err(adev->dev,
  1214. "Failed to validate gpu_info firmware \"%s\"\n",
  1215. fw_name);
  1216. goto out;
  1217. }
  1218. hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
  1219. amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
  1220. switch (hdr->version_major) {
  1221. case 1:
  1222. {
  1223. const struct gpu_info_firmware_v1_0 *gpu_info_fw =
  1224. (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
  1225. le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  1226. adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
  1227. adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
  1228. adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
  1229. adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
  1230. adev->gfx.config.max_texture_channel_caches =
  1231. le32_to_cpu(gpu_info_fw->gc_num_tccs);
  1232. adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
  1233. adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
  1234. adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
  1235. adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
  1236. adev->gfx.config.double_offchip_lds_buf =
  1237. le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
  1238. adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
  1239. adev->gfx.cu_info.max_waves_per_simd =
  1240. le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
  1241. adev->gfx.cu_info.max_scratch_slots_per_cu =
  1242. le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
  1243. adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
  1244. break;
  1245. }
  1246. default:
  1247. dev_err(adev->dev,
  1248. "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
  1249. err = -EINVAL;
  1250. goto out;
  1251. }
  1252. out:
  1253. return err;
  1254. }
  1255. /**
  1256. * amdgpu_device_ip_early_init - run early init for hardware IPs
  1257. *
  1258. * @adev: amdgpu_device pointer
  1259. *
  1260. * Early initialization pass for hardware IPs. The hardware IPs that make
  1261. * up each asic are discovered each IP's early_init callback is run. This
  1262. * is the first stage in initializing the asic.
  1263. * Returns 0 on success, negative error code on failure.
  1264. */
  1265. static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
  1266. {
  1267. int i, r;
  1268. amdgpu_device_enable_virtual_display(adev);
  1269. switch (adev->asic_type) {
  1270. case CHIP_TOPAZ:
  1271. case CHIP_TONGA:
  1272. case CHIP_FIJI:
  1273. case CHIP_POLARIS10:
  1274. case CHIP_POLARIS11:
  1275. case CHIP_POLARIS12:
  1276. case CHIP_VEGAM:
  1277. case CHIP_CARRIZO:
  1278. case CHIP_STONEY:
  1279. if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
  1280. adev->family = AMDGPU_FAMILY_CZ;
  1281. else
  1282. adev->family = AMDGPU_FAMILY_VI;
  1283. r = vi_set_ip_blocks(adev);
  1284. if (r)
  1285. return r;
  1286. break;
  1287. #ifdef CONFIG_DRM_AMDGPU_SI
  1288. case CHIP_VERDE:
  1289. case CHIP_TAHITI:
  1290. case CHIP_PITCAIRN:
  1291. case CHIP_OLAND:
  1292. case CHIP_HAINAN:
  1293. adev->family = AMDGPU_FAMILY_SI;
  1294. r = si_set_ip_blocks(adev);
  1295. if (r)
  1296. return r;
  1297. break;
  1298. #endif
  1299. #ifdef CONFIG_DRM_AMDGPU_CIK
  1300. case CHIP_BONAIRE:
  1301. case CHIP_HAWAII:
  1302. case CHIP_KAVERI:
  1303. case CHIP_KABINI:
  1304. case CHIP_MULLINS:
  1305. if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
  1306. adev->family = AMDGPU_FAMILY_CI;
  1307. else
  1308. adev->family = AMDGPU_FAMILY_KV;
  1309. r = cik_set_ip_blocks(adev);
  1310. if (r)
  1311. return r;
  1312. break;
  1313. #endif
  1314. case CHIP_VEGA10:
  1315. case CHIP_VEGA12:
  1316. case CHIP_VEGA20:
  1317. case CHIP_RAVEN:
  1318. if (adev->asic_type == CHIP_RAVEN)
  1319. adev->family = AMDGPU_FAMILY_RV;
  1320. else
  1321. adev->family = AMDGPU_FAMILY_AI;
  1322. r = soc15_set_ip_blocks(adev);
  1323. if (r)
  1324. return r;
  1325. break;
  1326. default:
  1327. /* FIXME: not supported yet */
  1328. return -EINVAL;
  1329. }
  1330. r = amdgpu_device_parse_gpu_info_fw(adev);
  1331. if (r)
  1332. return r;
  1333. amdgpu_amdkfd_device_probe(adev);
  1334. if (amdgpu_sriov_vf(adev)) {
  1335. r = amdgpu_virt_request_full_gpu(adev, true);
  1336. if (r)
  1337. return -EAGAIN;
  1338. }
  1339. adev->powerplay.pp_feature = amdgpu_pp_feature_mask;
  1340. for (i = 0; i < adev->num_ip_blocks; i++) {
  1341. if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
  1342. DRM_ERROR("disabled ip block: %d <%s>\n",
  1343. i, adev->ip_blocks[i].version->funcs->name);
  1344. adev->ip_blocks[i].status.valid = false;
  1345. } else {
  1346. if (adev->ip_blocks[i].version->funcs->early_init) {
  1347. r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
  1348. if (r == -ENOENT) {
  1349. adev->ip_blocks[i].status.valid = false;
  1350. } else if (r) {
  1351. DRM_ERROR("early_init of IP block <%s> failed %d\n",
  1352. adev->ip_blocks[i].version->funcs->name, r);
  1353. return r;
  1354. } else {
  1355. adev->ip_blocks[i].status.valid = true;
  1356. }
  1357. } else {
  1358. adev->ip_blocks[i].status.valid = true;
  1359. }
  1360. }
  1361. }
  1362. adev->cg_flags &= amdgpu_cg_mask;
  1363. adev->pg_flags &= amdgpu_pg_mask;
  1364. return 0;
  1365. }
  1366. static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
  1367. {
  1368. int i, r;
  1369. for (i = 0; i < adev->num_ip_blocks; i++) {
  1370. if (!adev->ip_blocks[i].status.sw)
  1371. continue;
  1372. if (adev->ip_blocks[i].status.hw)
  1373. continue;
  1374. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1375. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
  1376. r = adev->ip_blocks[i].version->funcs->hw_init(adev);
  1377. if (r) {
  1378. DRM_ERROR("hw_init of IP block <%s> failed %d\n",
  1379. adev->ip_blocks[i].version->funcs->name, r);
  1380. return r;
  1381. }
  1382. adev->ip_blocks[i].status.hw = true;
  1383. }
  1384. }
  1385. return 0;
  1386. }
  1387. static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
  1388. {
  1389. int i, r;
  1390. for (i = 0; i < adev->num_ip_blocks; i++) {
  1391. if (!adev->ip_blocks[i].status.sw)
  1392. continue;
  1393. if (adev->ip_blocks[i].status.hw)
  1394. continue;
  1395. r = adev->ip_blocks[i].version->funcs->hw_init(adev);
  1396. if (r) {
  1397. DRM_ERROR("hw_init of IP block <%s> failed %d\n",
  1398. adev->ip_blocks[i].version->funcs->name, r);
  1399. return r;
  1400. }
  1401. adev->ip_blocks[i].status.hw = true;
  1402. }
  1403. return 0;
  1404. }
  1405. static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
  1406. {
  1407. int r = 0;
  1408. int i;
  1409. if (adev->asic_type >= CHIP_VEGA10) {
  1410. for (i = 0; i < adev->num_ip_blocks; i++) {
  1411. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
  1412. if (adev->in_gpu_reset || adev->in_suspend) {
  1413. if (amdgpu_sriov_vf(adev) && adev->in_gpu_reset)
  1414. break; /* sriov gpu reset, psp need to do hw_init before IH because of hw limit */
  1415. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1416. if (r) {
  1417. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1418. adev->ip_blocks[i].version->funcs->name, r);
  1419. return r;
  1420. }
  1421. } else {
  1422. r = adev->ip_blocks[i].version->funcs->hw_init(adev);
  1423. if (r) {
  1424. DRM_ERROR("hw_init of IP block <%s> failed %d\n",
  1425. adev->ip_blocks[i].version->funcs->name, r);
  1426. return r;
  1427. }
  1428. }
  1429. adev->ip_blocks[i].status.hw = true;
  1430. }
  1431. }
  1432. }
  1433. if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->load_firmware) {
  1434. r = adev->powerplay.pp_funcs->load_firmware(adev->powerplay.pp_handle);
  1435. if (r) {
  1436. pr_err("firmware loading failed\n");
  1437. return r;
  1438. }
  1439. }
  1440. return 0;
  1441. }
  1442. /**
  1443. * amdgpu_device_ip_init - run init for hardware IPs
  1444. *
  1445. * @adev: amdgpu_device pointer
  1446. *
  1447. * Main initialization pass for hardware IPs. The list of all the hardware
  1448. * IPs that make up the asic is walked and the sw_init and hw_init callbacks
  1449. * are run. sw_init initializes the software state associated with each IP
  1450. * and hw_init initializes the hardware associated with each IP.
  1451. * Returns 0 on success, negative error code on failure.
  1452. */
  1453. static int amdgpu_device_ip_init(struct amdgpu_device *adev)
  1454. {
  1455. int i, r;
  1456. for (i = 0; i < adev->num_ip_blocks; i++) {
  1457. if (!adev->ip_blocks[i].status.valid)
  1458. continue;
  1459. r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
  1460. if (r) {
  1461. DRM_ERROR("sw_init of IP block <%s> failed %d\n",
  1462. adev->ip_blocks[i].version->funcs->name, r);
  1463. return r;
  1464. }
  1465. adev->ip_blocks[i].status.sw = true;
  1466. /* need to do gmc hw init early so we can allocate gpu mem */
  1467. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1468. r = amdgpu_device_vram_scratch_init(adev);
  1469. if (r) {
  1470. DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
  1471. return r;
  1472. }
  1473. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1474. if (r) {
  1475. DRM_ERROR("hw_init %d failed %d\n", i, r);
  1476. return r;
  1477. }
  1478. r = amdgpu_device_wb_init(adev);
  1479. if (r) {
  1480. DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
  1481. return r;
  1482. }
  1483. adev->ip_blocks[i].status.hw = true;
  1484. /* right after GMC hw init, we create CSA */
  1485. if (amdgpu_sriov_vf(adev)) {
  1486. r = amdgpu_allocate_static_csa(adev);
  1487. if (r) {
  1488. DRM_ERROR("allocate CSA failed %d\n", r);
  1489. return r;
  1490. }
  1491. }
  1492. }
  1493. }
  1494. r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
  1495. if (r)
  1496. return r;
  1497. r = amdgpu_device_ip_hw_init_phase1(adev);
  1498. if (r)
  1499. return r;
  1500. r = amdgpu_device_fw_loading(adev);
  1501. if (r)
  1502. return r;
  1503. r = amdgpu_device_ip_hw_init_phase2(adev);
  1504. if (r)
  1505. return r;
  1506. amdgpu_xgmi_add_device(adev);
  1507. amdgpu_amdkfd_device_init(adev);
  1508. if (amdgpu_sriov_vf(adev))
  1509. amdgpu_virt_release_full_gpu(adev, true);
  1510. return 0;
  1511. }
  1512. /**
  1513. * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
  1514. *
  1515. * @adev: amdgpu_device pointer
  1516. *
  1517. * Writes a reset magic value to the gart pointer in VRAM. The driver calls
  1518. * this function before a GPU reset. If the value is retained after a
  1519. * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
  1520. */
  1521. static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
  1522. {
  1523. memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
  1524. }
  1525. /**
  1526. * amdgpu_device_check_vram_lost - check if vram is valid
  1527. *
  1528. * @adev: amdgpu_device pointer
  1529. *
  1530. * Checks the reset magic value written to the gart pointer in VRAM.
  1531. * The driver calls this after a GPU reset to see if the contents of
  1532. * VRAM is lost or now.
  1533. * returns true if vram is lost, false if not.
  1534. */
  1535. static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
  1536. {
  1537. return !!memcmp(adev->gart.ptr, adev->reset_magic,
  1538. AMDGPU_RESET_MAGIC_NUM);
  1539. }
  1540. /**
  1541. * amdgpu_device_set_cg_state - set clockgating for amdgpu device
  1542. *
  1543. * @adev: amdgpu_device pointer
  1544. *
  1545. * The list of all the hardware IPs that make up the asic is walked and the
  1546. * set_clockgating_state callbacks are run.
  1547. * Late initialization pass enabling clockgating for hardware IPs.
  1548. * Fini or suspend, pass disabling clockgating for hardware IPs.
  1549. * Returns 0 on success, negative error code on failure.
  1550. */
  1551. static int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
  1552. enum amd_clockgating_state state)
  1553. {
  1554. int i, j, r;
  1555. if (amdgpu_emu_mode == 1)
  1556. return 0;
  1557. for (j = 0; j < adev->num_ip_blocks; j++) {
  1558. i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
  1559. if (!adev->ip_blocks[i].status.late_initialized)
  1560. continue;
  1561. /* skip CG for VCE/UVD, it's handled specially */
  1562. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1563. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
  1564. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
  1565. adev->ip_blocks[i].version->funcs->set_clockgating_state) {
  1566. /* enable clockgating to save power */
  1567. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1568. state);
  1569. if (r) {
  1570. DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
  1571. adev->ip_blocks[i].version->funcs->name, r);
  1572. return r;
  1573. }
  1574. }
  1575. }
  1576. return 0;
  1577. }
  1578. static int amdgpu_device_set_pg_state(struct amdgpu_device *adev, enum amd_powergating_state state)
  1579. {
  1580. int i, j, r;
  1581. if (amdgpu_emu_mode == 1)
  1582. return 0;
  1583. for (j = 0; j < adev->num_ip_blocks; j++) {
  1584. i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
  1585. if (!adev->ip_blocks[i].status.late_initialized)
  1586. continue;
  1587. /* skip CG for VCE/UVD, it's handled specially */
  1588. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1589. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
  1590. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
  1591. adev->ip_blocks[i].version->funcs->set_powergating_state) {
  1592. /* enable powergating to save power */
  1593. r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
  1594. state);
  1595. if (r) {
  1596. DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
  1597. adev->ip_blocks[i].version->funcs->name, r);
  1598. return r;
  1599. }
  1600. }
  1601. }
  1602. return 0;
  1603. }
  1604. /**
  1605. * amdgpu_device_ip_late_init - run late init for hardware IPs
  1606. *
  1607. * @adev: amdgpu_device pointer
  1608. *
  1609. * Late initialization pass for hardware IPs. The list of all the hardware
  1610. * IPs that make up the asic is walked and the late_init callbacks are run.
  1611. * late_init covers any special initialization that an IP requires
  1612. * after all of the have been initialized or something that needs to happen
  1613. * late in the init process.
  1614. * Returns 0 on success, negative error code on failure.
  1615. */
  1616. static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
  1617. {
  1618. int i = 0, r;
  1619. for (i = 0; i < adev->num_ip_blocks; i++) {
  1620. if (!adev->ip_blocks[i].status.hw)
  1621. continue;
  1622. if (adev->ip_blocks[i].version->funcs->late_init) {
  1623. r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
  1624. if (r) {
  1625. DRM_ERROR("late_init of IP block <%s> failed %d\n",
  1626. adev->ip_blocks[i].version->funcs->name, r);
  1627. return r;
  1628. }
  1629. }
  1630. adev->ip_blocks[i].status.late_initialized = true;
  1631. }
  1632. amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
  1633. amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
  1634. queue_delayed_work(system_wq, &adev->late_init_work,
  1635. msecs_to_jiffies(AMDGPU_RESUME_MS));
  1636. amdgpu_device_fill_reset_magic(adev);
  1637. return 0;
  1638. }
  1639. /**
  1640. * amdgpu_device_ip_fini - run fini for hardware IPs
  1641. *
  1642. * @adev: amdgpu_device pointer
  1643. *
  1644. * Main teardown pass for hardware IPs. The list of all the hardware
  1645. * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
  1646. * are run. hw_fini tears down the hardware associated with each IP
  1647. * and sw_fini tears down any software state associated with each IP.
  1648. * Returns 0 on success, negative error code on failure.
  1649. */
  1650. static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
  1651. {
  1652. int i, r;
  1653. amdgpu_amdkfd_device_fini(adev);
  1654. amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
  1655. amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
  1656. /* need to disable SMC first */
  1657. for (i = 0; i < adev->num_ip_blocks; i++) {
  1658. if (!adev->ip_blocks[i].status.hw)
  1659. continue;
  1660. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
  1661. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1662. /* XXX handle errors */
  1663. if (r) {
  1664. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1665. adev->ip_blocks[i].version->funcs->name, r);
  1666. }
  1667. adev->ip_blocks[i].status.hw = false;
  1668. break;
  1669. }
  1670. }
  1671. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1672. if (!adev->ip_blocks[i].status.hw)
  1673. continue;
  1674. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1675. /* XXX handle errors */
  1676. if (r) {
  1677. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1678. adev->ip_blocks[i].version->funcs->name, r);
  1679. }
  1680. adev->ip_blocks[i].status.hw = false;
  1681. }
  1682. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1683. if (!adev->ip_blocks[i].status.sw)
  1684. continue;
  1685. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1686. amdgpu_ucode_free_bo(adev);
  1687. amdgpu_free_static_csa(adev);
  1688. amdgpu_device_wb_fini(adev);
  1689. amdgpu_device_vram_scratch_fini(adev);
  1690. }
  1691. r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
  1692. /* XXX handle errors */
  1693. if (r) {
  1694. DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
  1695. adev->ip_blocks[i].version->funcs->name, r);
  1696. }
  1697. adev->ip_blocks[i].status.sw = false;
  1698. adev->ip_blocks[i].status.valid = false;
  1699. }
  1700. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1701. if (!adev->ip_blocks[i].status.late_initialized)
  1702. continue;
  1703. if (adev->ip_blocks[i].version->funcs->late_fini)
  1704. adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
  1705. adev->ip_blocks[i].status.late_initialized = false;
  1706. }
  1707. if (amdgpu_sriov_vf(adev))
  1708. if (amdgpu_virt_release_full_gpu(adev, false))
  1709. DRM_ERROR("failed to release exclusive mode on fini\n");
  1710. return 0;
  1711. }
  1712. static int amdgpu_device_enable_mgpu_fan_boost(void)
  1713. {
  1714. struct amdgpu_gpu_instance *gpu_ins;
  1715. struct amdgpu_device *adev;
  1716. int i, ret = 0;
  1717. mutex_lock(&mgpu_info.mutex);
  1718. /*
  1719. * MGPU fan boost feature should be enabled
  1720. * only when there are two or more dGPUs in
  1721. * the system
  1722. */
  1723. if (mgpu_info.num_dgpu < 2)
  1724. goto out;
  1725. for (i = 0; i < mgpu_info.num_dgpu; i++) {
  1726. gpu_ins = &(mgpu_info.gpu_ins[i]);
  1727. adev = gpu_ins->adev;
  1728. if (!(adev->flags & AMD_IS_APU) &&
  1729. !gpu_ins->mgpu_fan_enabled &&
  1730. adev->powerplay.pp_funcs &&
  1731. adev->powerplay.pp_funcs->enable_mgpu_fan_boost) {
  1732. ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
  1733. if (ret)
  1734. break;
  1735. gpu_ins->mgpu_fan_enabled = 1;
  1736. }
  1737. }
  1738. out:
  1739. mutex_unlock(&mgpu_info.mutex);
  1740. return ret;
  1741. }
  1742. /**
  1743. * amdgpu_device_ip_late_init_func_handler - work handler for ib test
  1744. *
  1745. * @work: work_struct.
  1746. */
  1747. static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
  1748. {
  1749. struct amdgpu_device *adev =
  1750. container_of(work, struct amdgpu_device, late_init_work.work);
  1751. int r;
  1752. r = amdgpu_ib_ring_tests(adev);
  1753. if (r)
  1754. DRM_ERROR("ib ring test failed (%d).\n", r);
  1755. r = amdgpu_device_enable_mgpu_fan_boost();
  1756. if (r)
  1757. DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
  1758. }
  1759. static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
  1760. {
  1761. struct amdgpu_device *adev =
  1762. container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
  1763. mutex_lock(&adev->gfx.gfx_off_mutex);
  1764. if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
  1765. if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
  1766. adev->gfx.gfx_off_state = true;
  1767. }
  1768. mutex_unlock(&adev->gfx.gfx_off_mutex);
  1769. }
  1770. /**
  1771. * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
  1772. *
  1773. * @adev: amdgpu_device pointer
  1774. *
  1775. * Main suspend function for hardware IPs. The list of all the hardware
  1776. * IPs that make up the asic is walked, clockgating is disabled and the
  1777. * suspend callbacks are run. suspend puts the hardware and software state
  1778. * in each IP into a state suitable for suspend.
  1779. * Returns 0 on success, negative error code on failure.
  1780. */
  1781. static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
  1782. {
  1783. int i, r;
  1784. amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
  1785. amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
  1786. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1787. if (!adev->ip_blocks[i].status.valid)
  1788. continue;
  1789. /* displays are handled separately */
  1790. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) {
  1791. /* XXX handle errors */
  1792. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1793. /* XXX handle errors */
  1794. if (r) {
  1795. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1796. adev->ip_blocks[i].version->funcs->name, r);
  1797. }
  1798. }
  1799. }
  1800. return 0;
  1801. }
  1802. /**
  1803. * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
  1804. *
  1805. * @adev: amdgpu_device pointer
  1806. *
  1807. * Main suspend function for hardware IPs. The list of all the hardware
  1808. * IPs that make up the asic is walked, clockgating is disabled and the
  1809. * suspend callbacks are run. suspend puts the hardware and software state
  1810. * in each IP into a state suitable for suspend.
  1811. * Returns 0 on success, negative error code on failure.
  1812. */
  1813. static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
  1814. {
  1815. int i, r;
  1816. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1817. if (!adev->ip_blocks[i].status.valid)
  1818. continue;
  1819. /* displays are handled in phase1 */
  1820. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
  1821. continue;
  1822. /* XXX handle errors */
  1823. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1824. /* XXX handle errors */
  1825. if (r) {
  1826. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1827. adev->ip_blocks[i].version->funcs->name, r);
  1828. }
  1829. }
  1830. return 0;
  1831. }
  1832. /**
  1833. * amdgpu_device_ip_suspend - run suspend for hardware IPs
  1834. *
  1835. * @adev: amdgpu_device pointer
  1836. *
  1837. * Main suspend function for hardware IPs. The list of all the hardware
  1838. * IPs that make up the asic is walked, clockgating is disabled and the
  1839. * suspend callbacks are run. suspend puts the hardware and software state
  1840. * in each IP into a state suitable for suspend.
  1841. * Returns 0 on success, negative error code on failure.
  1842. */
  1843. int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
  1844. {
  1845. int r;
  1846. if (amdgpu_sriov_vf(adev))
  1847. amdgpu_virt_request_full_gpu(adev, false);
  1848. r = amdgpu_device_ip_suspend_phase1(adev);
  1849. if (r)
  1850. return r;
  1851. r = amdgpu_device_ip_suspend_phase2(adev);
  1852. if (amdgpu_sriov_vf(adev))
  1853. amdgpu_virt_release_full_gpu(adev, false);
  1854. return r;
  1855. }
  1856. static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
  1857. {
  1858. int i, r;
  1859. static enum amd_ip_block_type ip_order[] = {
  1860. AMD_IP_BLOCK_TYPE_GMC,
  1861. AMD_IP_BLOCK_TYPE_COMMON,
  1862. AMD_IP_BLOCK_TYPE_PSP,
  1863. AMD_IP_BLOCK_TYPE_IH,
  1864. };
  1865. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1866. int j;
  1867. struct amdgpu_ip_block *block;
  1868. for (j = 0; j < adev->num_ip_blocks; j++) {
  1869. block = &adev->ip_blocks[j];
  1870. if (block->version->type != ip_order[i] ||
  1871. !block->status.valid)
  1872. continue;
  1873. r = block->version->funcs->hw_init(adev);
  1874. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
  1875. if (r)
  1876. return r;
  1877. }
  1878. }
  1879. return 0;
  1880. }
  1881. static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
  1882. {
  1883. int i, r;
  1884. static enum amd_ip_block_type ip_order[] = {
  1885. AMD_IP_BLOCK_TYPE_SMC,
  1886. AMD_IP_BLOCK_TYPE_DCE,
  1887. AMD_IP_BLOCK_TYPE_GFX,
  1888. AMD_IP_BLOCK_TYPE_SDMA,
  1889. AMD_IP_BLOCK_TYPE_UVD,
  1890. AMD_IP_BLOCK_TYPE_VCE
  1891. };
  1892. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1893. int j;
  1894. struct amdgpu_ip_block *block;
  1895. for (j = 0; j < adev->num_ip_blocks; j++) {
  1896. block = &adev->ip_blocks[j];
  1897. if (block->version->type != ip_order[i] ||
  1898. !block->status.valid)
  1899. continue;
  1900. r = block->version->funcs->hw_init(adev);
  1901. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
  1902. if (r)
  1903. return r;
  1904. }
  1905. }
  1906. return 0;
  1907. }
  1908. /**
  1909. * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
  1910. *
  1911. * @adev: amdgpu_device pointer
  1912. *
  1913. * First resume function for hardware IPs. The list of all the hardware
  1914. * IPs that make up the asic is walked and the resume callbacks are run for
  1915. * COMMON, GMC, and IH. resume puts the hardware into a functional state
  1916. * after a suspend and updates the software state as necessary. This
  1917. * function is also used for restoring the GPU after a GPU reset.
  1918. * Returns 0 on success, negative error code on failure.
  1919. */
  1920. static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
  1921. {
  1922. int i, r;
  1923. for (i = 0; i < adev->num_ip_blocks; i++) {
  1924. if (!adev->ip_blocks[i].status.valid)
  1925. continue;
  1926. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1927. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1928. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
  1929. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1930. if (r) {
  1931. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1932. adev->ip_blocks[i].version->funcs->name, r);
  1933. return r;
  1934. }
  1935. }
  1936. }
  1937. return 0;
  1938. }
  1939. /**
  1940. * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
  1941. *
  1942. * @adev: amdgpu_device pointer
  1943. *
  1944. * First resume function for hardware IPs. The list of all the hardware
  1945. * IPs that make up the asic is walked and the resume callbacks are run for
  1946. * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
  1947. * functional state after a suspend and updates the software state as
  1948. * necessary. This function is also used for restoring the GPU after a GPU
  1949. * reset.
  1950. * Returns 0 on success, negative error code on failure.
  1951. */
  1952. static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
  1953. {
  1954. int i, r;
  1955. for (i = 0; i < adev->num_ip_blocks; i++) {
  1956. if (!adev->ip_blocks[i].status.valid)
  1957. continue;
  1958. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1959. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1960. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
  1961. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
  1962. continue;
  1963. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1964. if (r) {
  1965. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1966. adev->ip_blocks[i].version->funcs->name, r);
  1967. return r;
  1968. }
  1969. }
  1970. return 0;
  1971. }
  1972. /**
  1973. * amdgpu_device_ip_resume - run resume for hardware IPs
  1974. *
  1975. * @adev: amdgpu_device pointer
  1976. *
  1977. * Main resume function for hardware IPs. The hardware IPs
  1978. * are split into two resume functions because they are
  1979. * are also used in in recovering from a GPU reset and some additional
  1980. * steps need to be take between them. In this case (S3/S4) they are
  1981. * run sequentially.
  1982. * Returns 0 on success, negative error code on failure.
  1983. */
  1984. static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
  1985. {
  1986. int r;
  1987. r = amdgpu_device_ip_resume_phase1(adev);
  1988. if (r)
  1989. return r;
  1990. r = amdgpu_device_fw_loading(adev);
  1991. if (r)
  1992. return r;
  1993. r = amdgpu_device_ip_resume_phase2(adev);
  1994. return r;
  1995. }
  1996. /**
  1997. * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
  1998. *
  1999. * @adev: amdgpu_device pointer
  2000. *
  2001. * Query the VBIOS data tables to determine if the board supports SR-IOV.
  2002. */
  2003. static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
  2004. {
  2005. if (amdgpu_sriov_vf(adev)) {
  2006. if (adev->is_atom_fw) {
  2007. if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
  2008. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  2009. } else {
  2010. if (amdgpu_atombios_has_gpu_virtualization_table(adev))
  2011. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  2012. }
  2013. if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
  2014. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
  2015. }
  2016. }
  2017. /**
  2018. * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
  2019. *
  2020. * @asic_type: AMD asic type
  2021. *
  2022. * Check if there is DC (new modesetting infrastructre) support for an asic.
  2023. * returns true if DC has support, false if not.
  2024. */
  2025. bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
  2026. {
  2027. switch (asic_type) {
  2028. #if defined(CONFIG_DRM_AMD_DC)
  2029. case CHIP_BONAIRE:
  2030. case CHIP_KAVERI:
  2031. case CHIP_KABINI:
  2032. case CHIP_MULLINS:
  2033. /*
  2034. * We have systems in the wild with these ASICs that require
  2035. * LVDS and VGA support which is not supported with DC.
  2036. *
  2037. * Fallback to the non-DC driver here by default so as not to
  2038. * cause regressions.
  2039. */
  2040. return amdgpu_dc > 0;
  2041. case CHIP_HAWAII:
  2042. case CHIP_CARRIZO:
  2043. case CHIP_STONEY:
  2044. case CHIP_POLARIS10:
  2045. case CHIP_POLARIS11:
  2046. case CHIP_POLARIS12:
  2047. case CHIP_VEGAM:
  2048. case CHIP_TONGA:
  2049. case CHIP_FIJI:
  2050. case CHIP_VEGA10:
  2051. case CHIP_VEGA12:
  2052. case CHIP_VEGA20:
  2053. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  2054. case CHIP_RAVEN:
  2055. #endif
  2056. return amdgpu_dc != 0;
  2057. #endif
  2058. default:
  2059. return false;
  2060. }
  2061. }
  2062. /**
  2063. * amdgpu_device_has_dc_support - check if dc is supported
  2064. *
  2065. * @adev: amdgpu_device_pointer
  2066. *
  2067. * Returns true for supported, false for not supported
  2068. */
  2069. bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
  2070. {
  2071. if (amdgpu_sriov_vf(adev))
  2072. return false;
  2073. return amdgpu_device_asic_has_dc_support(adev->asic_type);
  2074. }
  2075. /**
  2076. * amdgpu_device_init - initialize the driver
  2077. *
  2078. * @adev: amdgpu_device pointer
  2079. * @ddev: drm dev pointer
  2080. * @pdev: pci dev pointer
  2081. * @flags: driver flags
  2082. *
  2083. * Initializes the driver info and hw (all asics).
  2084. * Returns 0 for success or an error on failure.
  2085. * Called at driver startup.
  2086. */
  2087. int amdgpu_device_init(struct amdgpu_device *adev,
  2088. struct drm_device *ddev,
  2089. struct pci_dev *pdev,
  2090. uint32_t flags)
  2091. {
  2092. int r, i;
  2093. bool runtime = false;
  2094. u32 max_MBps;
  2095. adev->shutdown = false;
  2096. adev->dev = &pdev->dev;
  2097. adev->ddev = ddev;
  2098. adev->pdev = pdev;
  2099. adev->flags = flags;
  2100. adev->asic_type = flags & AMD_ASIC_MASK;
  2101. adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
  2102. if (amdgpu_emu_mode == 1)
  2103. adev->usec_timeout *= 2;
  2104. adev->gmc.gart_size = 512 * 1024 * 1024;
  2105. adev->accel_working = false;
  2106. adev->num_rings = 0;
  2107. adev->mman.buffer_funcs = NULL;
  2108. adev->mman.buffer_funcs_ring = NULL;
  2109. adev->vm_manager.vm_pte_funcs = NULL;
  2110. adev->vm_manager.vm_pte_num_rqs = 0;
  2111. adev->gmc.gmc_funcs = NULL;
  2112. adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  2113. bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
  2114. adev->smc_rreg = &amdgpu_invalid_rreg;
  2115. adev->smc_wreg = &amdgpu_invalid_wreg;
  2116. adev->pcie_rreg = &amdgpu_invalid_rreg;
  2117. adev->pcie_wreg = &amdgpu_invalid_wreg;
  2118. adev->pciep_rreg = &amdgpu_invalid_rreg;
  2119. adev->pciep_wreg = &amdgpu_invalid_wreg;
  2120. adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
  2121. adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
  2122. adev->didt_rreg = &amdgpu_invalid_rreg;
  2123. adev->didt_wreg = &amdgpu_invalid_wreg;
  2124. adev->gc_cac_rreg = &amdgpu_invalid_rreg;
  2125. adev->gc_cac_wreg = &amdgpu_invalid_wreg;
  2126. adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
  2127. adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
  2128. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
  2129. amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
  2130. pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
  2131. /* mutex initialization are all done here so we
  2132. * can recall function without having locking issues */
  2133. atomic_set(&adev->irq.ih.lock, 0);
  2134. mutex_init(&adev->firmware.mutex);
  2135. mutex_init(&adev->pm.mutex);
  2136. mutex_init(&adev->gfx.gpu_clock_mutex);
  2137. mutex_init(&adev->srbm_mutex);
  2138. mutex_init(&adev->gfx.pipe_reserve_mutex);
  2139. mutex_init(&adev->gfx.gfx_off_mutex);
  2140. mutex_init(&adev->grbm_idx_mutex);
  2141. mutex_init(&adev->mn_lock);
  2142. mutex_init(&adev->virt.vf_errors.lock);
  2143. hash_init(adev->mn_hash);
  2144. mutex_init(&adev->lock_reset);
  2145. amdgpu_device_check_arguments(adev);
  2146. spin_lock_init(&adev->mmio_idx_lock);
  2147. spin_lock_init(&adev->smc_idx_lock);
  2148. spin_lock_init(&adev->pcie_idx_lock);
  2149. spin_lock_init(&adev->uvd_ctx_idx_lock);
  2150. spin_lock_init(&adev->didt_idx_lock);
  2151. spin_lock_init(&adev->gc_cac_idx_lock);
  2152. spin_lock_init(&adev->se_cac_idx_lock);
  2153. spin_lock_init(&adev->audio_endpt_idx_lock);
  2154. spin_lock_init(&adev->mm_stats.lock);
  2155. INIT_LIST_HEAD(&adev->shadow_list);
  2156. mutex_init(&adev->shadow_list_lock);
  2157. INIT_LIST_HEAD(&adev->ring_lru_list);
  2158. spin_lock_init(&adev->ring_lru_list_lock);
  2159. INIT_DELAYED_WORK(&adev->late_init_work,
  2160. amdgpu_device_ip_late_init_func_handler);
  2161. INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
  2162. amdgpu_device_delay_enable_gfx_off);
  2163. adev->gfx.gfx_off_req_count = 1;
  2164. adev->pm.ac_power = power_supply_is_system_supplied() > 0 ? true : false;
  2165. /* Registers mapping */
  2166. /* TODO: block userspace mapping of io register */
  2167. if (adev->asic_type >= CHIP_BONAIRE) {
  2168. adev->rmmio_base = pci_resource_start(adev->pdev, 5);
  2169. adev->rmmio_size = pci_resource_len(adev->pdev, 5);
  2170. } else {
  2171. adev->rmmio_base = pci_resource_start(adev->pdev, 2);
  2172. adev->rmmio_size = pci_resource_len(adev->pdev, 2);
  2173. }
  2174. adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
  2175. if (adev->rmmio == NULL) {
  2176. return -ENOMEM;
  2177. }
  2178. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
  2179. DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  2180. /* doorbell bar mapping */
  2181. amdgpu_device_doorbell_init(adev);
  2182. /* io port mapping */
  2183. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  2184. if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
  2185. adev->rio_mem_size = pci_resource_len(adev->pdev, i);
  2186. adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
  2187. break;
  2188. }
  2189. }
  2190. if (adev->rio_mem == NULL)
  2191. DRM_INFO("PCI I/O BAR is not found.\n");
  2192. amdgpu_device_get_pcie_info(adev);
  2193. /* early init functions */
  2194. r = amdgpu_device_ip_early_init(adev);
  2195. if (r)
  2196. return r;
  2197. /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
  2198. /* this will fail for cards that aren't VGA class devices, just
  2199. * ignore it */
  2200. vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
  2201. if (amdgpu_device_is_px(ddev))
  2202. runtime = true;
  2203. if (!pci_is_thunderbolt_attached(adev->pdev))
  2204. vga_switcheroo_register_client(adev->pdev,
  2205. &amdgpu_switcheroo_ops, runtime);
  2206. if (runtime)
  2207. vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
  2208. if (amdgpu_emu_mode == 1) {
  2209. /* post the asic on emulation mode */
  2210. emu_soc_asic_init(adev);
  2211. goto fence_driver_init;
  2212. }
  2213. /* Read BIOS */
  2214. if (!amdgpu_get_bios(adev)) {
  2215. r = -EINVAL;
  2216. goto failed;
  2217. }
  2218. r = amdgpu_atombios_init(adev);
  2219. if (r) {
  2220. dev_err(adev->dev, "amdgpu_atombios_init failed\n");
  2221. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
  2222. goto failed;
  2223. }
  2224. /* detect if we are with an SRIOV vbios */
  2225. amdgpu_device_detect_sriov_bios(adev);
  2226. /* Post card if necessary */
  2227. if (amdgpu_device_need_post(adev)) {
  2228. if (!adev->bios) {
  2229. dev_err(adev->dev, "no vBIOS found\n");
  2230. r = -EINVAL;
  2231. goto failed;
  2232. }
  2233. DRM_INFO("GPU posting now...\n");
  2234. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2235. if (r) {
  2236. dev_err(adev->dev, "gpu post error!\n");
  2237. goto failed;
  2238. }
  2239. }
  2240. if (adev->is_atom_fw) {
  2241. /* Initialize clocks */
  2242. r = amdgpu_atomfirmware_get_clock_info(adev);
  2243. if (r) {
  2244. dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
  2245. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  2246. goto failed;
  2247. }
  2248. } else {
  2249. /* Initialize clocks */
  2250. r = amdgpu_atombios_get_clock_info(adev);
  2251. if (r) {
  2252. dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
  2253. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  2254. goto failed;
  2255. }
  2256. /* init i2c buses */
  2257. if (!amdgpu_device_has_dc_support(adev))
  2258. amdgpu_atombios_i2c_init(adev);
  2259. }
  2260. fence_driver_init:
  2261. /* Fence driver */
  2262. r = amdgpu_fence_driver_init(adev);
  2263. if (r) {
  2264. dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
  2265. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
  2266. goto failed;
  2267. }
  2268. /* init the mode config */
  2269. drm_mode_config_init(adev->ddev);
  2270. r = amdgpu_device_ip_init(adev);
  2271. if (r) {
  2272. /* failed in exclusive mode due to timeout */
  2273. if (amdgpu_sriov_vf(adev) &&
  2274. !amdgpu_sriov_runtime(adev) &&
  2275. amdgpu_virt_mmio_blocked(adev) &&
  2276. !amdgpu_virt_wait_reset(adev)) {
  2277. dev_err(adev->dev, "VF exclusive mode timeout\n");
  2278. /* Don't send request since VF is inactive. */
  2279. adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
  2280. adev->virt.ops = NULL;
  2281. r = -EAGAIN;
  2282. goto failed;
  2283. }
  2284. dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
  2285. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
  2286. goto failed;
  2287. }
  2288. adev->accel_working = true;
  2289. amdgpu_vm_check_compute_bug(adev);
  2290. /* Initialize the buffer migration limit. */
  2291. if (amdgpu_moverate >= 0)
  2292. max_MBps = amdgpu_moverate;
  2293. else
  2294. max_MBps = 8; /* Allow 8 MB/s. */
  2295. /* Get a log2 for easy divisions. */
  2296. adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
  2297. r = amdgpu_ib_pool_init(adev);
  2298. if (r) {
  2299. dev_err(adev->dev, "IB initialization failed (%d).\n", r);
  2300. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
  2301. goto failed;
  2302. }
  2303. if (amdgpu_sriov_vf(adev))
  2304. amdgpu_virt_init_data_exchange(adev);
  2305. amdgpu_fbdev_init(adev);
  2306. r = amdgpu_pm_sysfs_init(adev);
  2307. if (r)
  2308. DRM_ERROR("registering pm debugfs failed (%d).\n", r);
  2309. r = amdgpu_debugfs_gem_init(adev);
  2310. if (r)
  2311. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  2312. r = amdgpu_debugfs_regs_init(adev);
  2313. if (r)
  2314. DRM_ERROR("registering register debugfs failed (%d).\n", r);
  2315. r = amdgpu_debugfs_firmware_init(adev);
  2316. if (r)
  2317. DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
  2318. r = amdgpu_debugfs_init(adev);
  2319. if (r)
  2320. DRM_ERROR("Creating debugfs files failed (%d).\n", r);
  2321. if ((amdgpu_testing & 1)) {
  2322. if (adev->accel_working)
  2323. amdgpu_test_moves(adev);
  2324. else
  2325. DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
  2326. }
  2327. if (amdgpu_benchmarking) {
  2328. if (adev->accel_working)
  2329. amdgpu_benchmark(adev, amdgpu_benchmarking);
  2330. else
  2331. DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
  2332. }
  2333. /* enable clockgating, etc. after ib tests, etc. since some blocks require
  2334. * explicit gating rather than handling it automatically.
  2335. */
  2336. r = amdgpu_device_ip_late_init(adev);
  2337. if (r) {
  2338. dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
  2339. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
  2340. goto failed;
  2341. }
  2342. return 0;
  2343. failed:
  2344. amdgpu_vf_error_trans_all(adev);
  2345. if (runtime)
  2346. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  2347. return r;
  2348. }
  2349. /**
  2350. * amdgpu_device_fini - tear down the driver
  2351. *
  2352. * @adev: amdgpu_device pointer
  2353. *
  2354. * Tear down the driver info (all asics).
  2355. * Called at driver shutdown.
  2356. */
  2357. void amdgpu_device_fini(struct amdgpu_device *adev)
  2358. {
  2359. int r;
  2360. DRM_INFO("amdgpu: finishing device.\n");
  2361. adev->shutdown = true;
  2362. /* disable all interrupts */
  2363. amdgpu_irq_disable_all(adev);
  2364. if (adev->mode_info.mode_config_initialized){
  2365. if (!amdgpu_device_has_dc_support(adev))
  2366. drm_crtc_force_disable_all(adev->ddev);
  2367. else
  2368. drm_atomic_helper_shutdown(adev->ddev);
  2369. }
  2370. amdgpu_ib_pool_fini(adev);
  2371. amdgpu_fence_driver_fini(adev);
  2372. amdgpu_pm_sysfs_fini(adev);
  2373. amdgpu_fbdev_fini(adev);
  2374. r = amdgpu_device_ip_fini(adev);
  2375. if (adev->firmware.gpu_info_fw) {
  2376. release_firmware(adev->firmware.gpu_info_fw);
  2377. adev->firmware.gpu_info_fw = NULL;
  2378. }
  2379. adev->accel_working = false;
  2380. cancel_delayed_work_sync(&adev->late_init_work);
  2381. /* free i2c buses */
  2382. if (!amdgpu_device_has_dc_support(adev))
  2383. amdgpu_i2c_fini(adev);
  2384. if (amdgpu_emu_mode != 1)
  2385. amdgpu_atombios_fini(adev);
  2386. kfree(adev->bios);
  2387. adev->bios = NULL;
  2388. if (!pci_is_thunderbolt_attached(adev->pdev))
  2389. vga_switcheroo_unregister_client(adev->pdev);
  2390. if (adev->flags & AMD_IS_PX)
  2391. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  2392. vga_client_register(adev->pdev, NULL, NULL, NULL);
  2393. if (adev->rio_mem)
  2394. pci_iounmap(adev->pdev, adev->rio_mem);
  2395. adev->rio_mem = NULL;
  2396. iounmap(adev->rmmio);
  2397. adev->rmmio = NULL;
  2398. amdgpu_device_doorbell_fini(adev);
  2399. amdgpu_debugfs_regs_cleanup(adev);
  2400. }
  2401. /*
  2402. * Suspend & resume.
  2403. */
  2404. /**
  2405. * amdgpu_device_suspend - initiate device suspend
  2406. *
  2407. * @dev: drm dev pointer
  2408. * @suspend: suspend state
  2409. * @fbcon : notify the fbdev of suspend
  2410. *
  2411. * Puts the hw in the suspend state (all asics).
  2412. * Returns 0 for success or an error on failure.
  2413. * Called at driver suspend.
  2414. */
  2415. int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
  2416. {
  2417. struct amdgpu_device *adev;
  2418. struct drm_crtc *crtc;
  2419. struct drm_connector *connector;
  2420. int r;
  2421. if (dev == NULL || dev->dev_private == NULL) {
  2422. return -ENODEV;
  2423. }
  2424. adev = dev->dev_private;
  2425. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2426. return 0;
  2427. adev->in_suspend = true;
  2428. drm_kms_helper_poll_disable(dev);
  2429. if (fbcon)
  2430. amdgpu_fbdev_set_suspend(adev, 1);
  2431. cancel_delayed_work_sync(&adev->late_init_work);
  2432. if (!amdgpu_device_has_dc_support(adev)) {
  2433. /* turn off display hw */
  2434. drm_modeset_lock_all(dev);
  2435. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2436. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  2437. }
  2438. drm_modeset_unlock_all(dev);
  2439. /* unpin the front buffers and cursors */
  2440. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2441. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2442. struct drm_framebuffer *fb = crtc->primary->fb;
  2443. struct amdgpu_bo *robj;
  2444. if (amdgpu_crtc->cursor_bo) {
  2445. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2446. r = amdgpu_bo_reserve(aobj, true);
  2447. if (r == 0) {
  2448. amdgpu_bo_unpin(aobj);
  2449. amdgpu_bo_unreserve(aobj);
  2450. }
  2451. }
  2452. if (fb == NULL || fb->obj[0] == NULL) {
  2453. continue;
  2454. }
  2455. robj = gem_to_amdgpu_bo(fb->obj[0]);
  2456. /* don't unpin kernel fb objects */
  2457. if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
  2458. r = amdgpu_bo_reserve(robj, true);
  2459. if (r == 0) {
  2460. amdgpu_bo_unpin(robj);
  2461. amdgpu_bo_unreserve(robj);
  2462. }
  2463. }
  2464. }
  2465. }
  2466. amdgpu_amdkfd_suspend(adev);
  2467. r = amdgpu_device_ip_suspend_phase1(adev);
  2468. /* evict vram memory */
  2469. amdgpu_bo_evict_vram(adev);
  2470. amdgpu_fence_driver_suspend(adev);
  2471. r = amdgpu_device_ip_suspend_phase2(adev);
  2472. /* evict remaining vram memory
  2473. * This second call to evict vram is to evict the gart page table
  2474. * using the CPU.
  2475. */
  2476. amdgpu_bo_evict_vram(adev);
  2477. pci_save_state(dev->pdev);
  2478. if (suspend) {
  2479. /* Shut down the device */
  2480. pci_disable_device(dev->pdev);
  2481. pci_set_power_state(dev->pdev, PCI_D3hot);
  2482. } else {
  2483. r = amdgpu_asic_reset(adev);
  2484. if (r)
  2485. DRM_ERROR("amdgpu asic reset failed\n");
  2486. }
  2487. return 0;
  2488. }
  2489. /**
  2490. * amdgpu_device_resume - initiate device resume
  2491. *
  2492. * @dev: drm dev pointer
  2493. * @resume: resume state
  2494. * @fbcon : notify the fbdev of resume
  2495. *
  2496. * Bring the hw back to operating state (all asics).
  2497. * Returns 0 for success or an error on failure.
  2498. * Called at driver resume.
  2499. */
  2500. int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
  2501. {
  2502. struct drm_connector *connector;
  2503. struct amdgpu_device *adev = dev->dev_private;
  2504. struct drm_crtc *crtc;
  2505. int r = 0;
  2506. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2507. return 0;
  2508. if (resume) {
  2509. pci_set_power_state(dev->pdev, PCI_D0);
  2510. pci_restore_state(dev->pdev);
  2511. r = pci_enable_device(dev->pdev);
  2512. if (r)
  2513. return r;
  2514. }
  2515. /* post card */
  2516. if (amdgpu_device_need_post(adev)) {
  2517. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2518. if (r)
  2519. DRM_ERROR("amdgpu asic init failed\n");
  2520. }
  2521. r = amdgpu_device_ip_resume(adev);
  2522. if (r) {
  2523. DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
  2524. return r;
  2525. }
  2526. amdgpu_fence_driver_resume(adev);
  2527. r = amdgpu_device_ip_late_init(adev);
  2528. if (r)
  2529. return r;
  2530. if (!amdgpu_device_has_dc_support(adev)) {
  2531. /* pin cursors */
  2532. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2533. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2534. if (amdgpu_crtc->cursor_bo) {
  2535. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2536. r = amdgpu_bo_reserve(aobj, true);
  2537. if (r == 0) {
  2538. r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
  2539. if (r != 0)
  2540. DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
  2541. amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
  2542. amdgpu_bo_unreserve(aobj);
  2543. }
  2544. }
  2545. }
  2546. }
  2547. r = amdgpu_amdkfd_resume(adev);
  2548. if (r)
  2549. return r;
  2550. /* Make sure IB tests flushed */
  2551. flush_delayed_work(&adev->late_init_work);
  2552. /* blat the mode back in */
  2553. if (fbcon) {
  2554. if (!amdgpu_device_has_dc_support(adev)) {
  2555. /* pre DCE11 */
  2556. drm_helper_resume_force_mode(dev);
  2557. /* turn on display hw */
  2558. drm_modeset_lock_all(dev);
  2559. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2560. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  2561. }
  2562. drm_modeset_unlock_all(dev);
  2563. }
  2564. amdgpu_fbdev_set_suspend(adev, 0);
  2565. }
  2566. drm_kms_helper_poll_enable(dev);
  2567. /*
  2568. * Most of the connector probing functions try to acquire runtime pm
  2569. * refs to ensure that the GPU is powered on when connector polling is
  2570. * performed. Since we're calling this from a runtime PM callback,
  2571. * trying to acquire rpm refs will cause us to deadlock.
  2572. *
  2573. * Since we're guaranteed to be holding the rpm lock, it's safe to
  2574. * temporarily disable the rpm helpers so this doesn't deadlock us.
  2575. */
  2576. #ifdef CONFIG_PM
  2577. dev->dev->power.disable_depth++;
  2578. #endif
  2579. if (!amdgpu_device_has_dc_support(adev))
  2580. drm_helper_hpd_irq_event(dev);
  2581. else
  2582. drm_kms_helper_hotplug_event(dev);
  2583. #ifdef CONFIG_PM
  2584. dev->dev->power.disable_depth--;
  2585. #endif
  2586. adev->in_suspend = false;
  2587. return 0;
  2588. }
  2589. /**
  2590. * amdgpu_device_ip_check_soft_reset - did soft reset succeed
  2591. *
  2592. * @adev: amdgpu_device pointer
  2593. *
  2594. * The list of all the hardware IPs that make up the asic is walked and
  2595. * the check_soft_reset callbacks are run. check_soft_reset determines
  2596. * if the asic is still hung or not.
  2597. * Returns true if any of the IPs are still in a hung state, false if not.
  2598. */
  2599. static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
  2600. {
  2601. int i;
  2602. bool asic_hang = false;
  2603. if (amdgpu_sriov_vf(adev))
  2604. return true;
  2605. if (amdgpu_asic_need_full_reset(adev))
  2606. return true;
  2607. for (i = 0; i < adev->num_ip_blocks; i++) {
  2608. if (!adev->ip_blocks[i].status.valid)
  2609. continue;
  2610. if (adev->ip_blocks[i].version->funcs->check_soft_reset)
  2611. adev->ip_blocks[i].status.hang =
  2612. adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
  2613. if (adev->ip_blocks[i].status.hang) {
  2614. DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
  2615. asic_hang = true;
  2616. }
  2617. }
  2618. return asic_hang;
  2619. }
  2620. /**
  2621. * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
  2622. *
  2623. * @adev: amdgpu_device pointer
  2624. *
  2625. * The list of all the hardware IPs that make up the asic is walked and the
  2626. * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
  2627. * handles any IP specific hardware or software state changes that are
  2628. * necessary for a soft reset to succeed.
  2629. * Returns 0 on success, negative error code on failure.
  2630. */
  2631. static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
  2632. {
  2633. int i, r = 0;
  2634. for (i = 0; i < adev->num_ip_blocks; i++) {
  2635. if (!adev->ip_blocks[i].status.valid)
  2636. continue;
  2637. if (adev->ip_blocks[i].status.hang &&
  2638. adev->ip_blocks[i].version->funcs->pre_soft_reset) {
  2639. r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
  2640. if (r)
  2641. return r;
  2642. }
  2643. }
  2644. return 0;
  2645. }
  2646. /**
  2647. * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
  2648. *
  2649. * @adev: amdgpu_device pointer
  2650. *
  2651. * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
  2652. * reset is necessary to recover.
  2653. * Returns true if a full asic reset is required, false if not.
  2654. */
  2655. static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
  2656. {
  2657. int i;
  2658. if (amdgpu_asic_need_full_reset(adev))
  2659. return true;
  2660. for (i = 0; i < adev->num_ip_blocks; i++) {
  2661. if (!adev->ip_blocks[i].status.valid)
  2662. continue;
  2663. if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
  2664. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
  2665. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
  2666. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
  2667. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
  2668. if (adev->ip_blocks[i].status.hang) {
  2669. DRM_INFO("Some block need full reset!\n");
  2670. return true;
  2671. }
  2672. }
  2673. }
  2674. return false;
  2675. }
  2676. /**
  2677. * amdgpu_device_ip_soft_reset - do a soft reset
  2678. *
  2679. * @adev: amdgpu_device pointer
  2680. *
  2681. * The list of all the hardware IPs that make up the asic is walked and the
  2682. * soft_reset callbacks are run if the block is hung. soft_reset handles any
  2683. * IP specific hardware or software state changes that are necessary to soft
  2684. * reset the IP.
  2685. * Returns 0 on success, negative error code on failure.
  2686. */
  2687. static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
  2688. {
  2689. int i, r = 0;
  2690. for (i = 0; i < adev->num_ip_blocks; i++) {
  2691. if (!adev->ip_blocks[i].status.valid)
  2692. continue;
  2693. if (adev->ip_blocks[i].status.hang &&
  2694. adev->ip_blocks[i].version->funcs->soft_reset) {
  2695. r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
  2696. if (r)
  2697. return r;
  2698. }
  2699. }
  2700. return 0;
  2701. }
  2702. /**
  2703. * amdgpu_device_ip_post_soft_reset - clean up from soft reset
  2704. *
  2705. * @adev: amdgpu_device pointer
  2706. *
  2707. * The list of all the hardware IPs that make up the asic is walked and the
  2708. * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
  2709. * handles any IP specific hardware or software state changes that are
  2710. * necessary after the IP has been soft reset.
  2711. * Returns 0 on success, negative error code on failure.
  2712. */
  2713. static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
  2714. {
  2715. int i, r = 0;
  2716. for (i = 0; i < adev->num_ip_blocks; i++) {
  2717. if (!adev->ip_blocks[i].status.valid)
  2718. continue;
  2719. if (adev->ip_blocks[i].status.hang &&
  2720. adev->ip_blocks[i].version->funcs->post_soft_reset)
  2721. r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
  2722. if (r)
  2723. return r;
  2724. }
  2725. return 0;
  2726. }
  2727. /**
  2728. * amdgpu_device_recover_vram - Recover some VRAM contents
  2729. *
  2730. * @adev: amdgpu_device pointer
  2731. *
  2732. * Restores the contents of VRAM buffers from the shadows in GTT. Used to
  2733. * restore things like GPUVM page tables after a GPU reset where
  2734. * the contents of VRAM might be lost.
  2735. *
  2736. * Returns:
  2737. * 0 on success, negative error code on failure.
  2738. */
  2739. static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
  2740. {
  2741. struct dma_fence *fence = NULL, *next = NULL;
  2742. struct amdgpu_bo *shadow;
  2743. long r = 1, tmo;
  2744. if (amdgpu_sriov_runtime(adev))
  2745. tmo = msecs_to_jiffies(8000);
  2746. else
  2747. tmo = msecs_to_jiffies(100);
  2748. DRM_INFO("recover vram bo from shadow start\n");
  2749. mutex_lock(&adev->shadow_list_lock);
  2750. list_for_each_entry(shadow, &adev->shadow_list, shadow_list) {
  2751. /* No need to recover an evicted BO */
  2752. if (shadow->tbo.mem.mem_type != TTM_PL_TT ||
  2753. shadow->parent->tbo.mem.mem_type != TTM_PL_VRAM)
  2754. continue;
  2755. r = amdgpu_bo_restore_shadow(shadow, &next);
  2756. if (r)
  2757. break;
  2758. if (fence) {
  2759. r = dma_fence_wait_timeout(fence, false, tmo);
  2760. dma_fence_put(fence);
  2761. fence = next;
  2762. if (r <= 0)
  2763. break;
  2764. } else {
  2765. fence = next;
  2766. }
  2767. }
  2768. mutex_unlock(&adev->shadow_list_lock);
  2769. if (fence)
  2770. tmo = dma_fence_wait_timeout(fence, false, tmo);
  2771. dma_fence_put(fence);
  2772. if (r <= 0 || tmo <= 0) {
  2773. DRM_ERROR("recover vram bo from shadow failed\n");
  2774. return -EIO;
  2775. }
  2776. DRM_INFO("recover vram bo from shadow done\n");
  2777. return 0;
  2778. }
  2779. /**
  2780. * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
  2781. *
  2782. * @adev: amdgpu device pointer
  2783. *
  2784. * attempt to do soft-reset or full-reset and reinitialize Asic
  2785. * return 0 means succeeded otherwise failed
  2786. */
  2787. static int amdgpu_device_reset(struct amdgpu_device *adev)
  2788. {
  2789. bool need_full_reset, vram_lost = 0;
  2790. int r;
  2791. need_full_reset = amdgpu_device_ip_need_full_reset(adev);
  2792. if (!need_full_reset) {
  2793. amdgpu_device_ip_pre_soft_reset(adev);
  2794. r = amdgpu_device_ip_soft_reset(adev);
  2795. amdgpu_device_ip_post_soft_reset(adev);
  2796. if (r || amdgpu_device_ip_check_soft_reset(adev)) {
  2797. DRM_INFO("soft reset failed, will fallback to full reset!\n");
  2798. need_full_reset = true;
  2799. }
  2800. }
  2801. if (need_full_reset) {
  2802. r = amdgpu_device_ip_suspend(adev);
  2803. retry:
  2804. r = amdgpu_asic_reset(adev);
  2805. /* post card */
  2806. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2807. if (!r) {
  2808. dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
  2809. r = amdgpu_device_ip_resume_phase1(adev);
  2810. if (r)
  2811. goto out;
  2812. vram_lost = amdgpu_device_check_vram_lost(adev);
  2813. if (vram_lost) {
  2814. DRM_ERROR("VRAM is lost!\n");
  2815. atomic_inc(&adev->vram_lost_counter);
  2816. }
  2817. r = amdgpu_gtt_mgr_recover(
  2818. &adev->mman.bdev.man[TTM_PL_TT]);
  2819. if (r)
  2820. goto out;
  2821. r = amdgpu_device_fw_loading(adev);
  2822. if (r)
  2823. return r;
  2824. r = amdgpu_device_ip_resume_phase2(adev);
  2825. if (r)
  2826. goto out;
  2827. if (vram_lost)
  2828. amdgpu_device_fill_reset_magic(adev);
  2829. }
  2830. }
  2831. out:
  2832. if (!r) {
  2833. amdgpu_irq_gpu_reset_resume_helper(adev);
  2834. r = amdgpu_ib_ring_tests(adev);
  2835. if (r) {
  2836. dev_err(adev->dev, "ib ring test failed (%d).\n", r);
  2837. r = amdgpu_device_ip_suspend(adev);
  2838. need_full_reset = true;
  2839. goto retry;
  2840. }
  2841. }
  2842. if (!r)
  2843. r = amdgpu_device_recover_vram(adev);
  2844. return r;
  2845. }
  2846. /**
  2847. * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
  2848. *
  2849. * @adev: amdgpu device pointer
  2850. * @from_hypervisor: request from hypervisor
  2851. *
  2852. * do VF FLR and reinitialize Asic
  2853. * return 0 means succeeded otherwise failed
  2854. */
  2855. static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
  2856. bool from_hypervisor)
  2857. {
  2858. int r;
  2859. if (from_hypervisor)
  2860. r = amdgpu_virt_request_full_gpu(adev, true);
  2861. else
  2862. r = amdgpu_virt_reset_gpu(adev);
  2863. if (r)
  2864. return r;
  2865. /* Resume IP prior to SMC */
  2866. r = amdgpu_device_ip_reinit_early_sriov(adev);
  2867. if (r)
  2868. goto error;
  2869. /* we need recover gart prior to run SMC/CP/SDMA resume */
  2870. amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
  2871. r = amdgpu_device_fw_loading(adev);
  2872. if (r)
  2873. return r;
  2874. /* now we are okay to resume SMC/CP/SDMA */
  2875. r = amdgpu_device_ip_reinit_late_sriov(adev);
  2876. if (r)
  2877. goto error;
  2878. amdgpu_irq_gpu_reset_resume_helper(adev);
  2879. r = amdgpu_ib_ring_tests(adev);
  2880. error:
  2881. amdgpu_virt_release_full_gpu(adev, true);
  2882. if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
  2883. atomic_inc(&adev->vram_lost_counter);
  2884. r = amdgpu_device_recover_vram(adev);
  2885. }
  2886. return r;
  2887. }
  2888. /**
  2889. * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
  2890. *
  2891. * @adev: amdgpu device pointer
  2892. *
  2893. * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
  2894. * a hung GPU.
  2895. */
  2896. bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
  2897. {
  2898. if (!amdgpu_device_ip_check_soft_reset(adev)) {
  2899. DRM_INFO("Timeout, but no hardware hang detected.\n");
  2900. return false;
  2901. }
  2902. if (amdgpu_gpu_recovery == 0 || (amdgpu_gpu_recovery == -1 &&
  2903. !amdgpu_sriov_vf(adev))) {
  2904. DRM_INFO("GPU recovery disabled.\n");
  2905. return false;
  2906. }
  2907. return true;
  2908. }
  2909. /**
  2910. * amdgpu_device_gpu_recover - reset the asic and recover scheduler
  2911. *
  2912. * @adev: amdgpu device pointer
  2913. * @job: which job trigger hang
  2914. *
  2915. * Attempt to reset the GPU if it has hung (all asics).
  2916. * Returns 0 for success or an error on failure.
  2917. */
  2918. int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  2919. struct amdgpu_job *job)
  2920. {
  2921. int i, r, resched;
  2922. dev_info(adev->dev, "GPU reset begin!\n");
  2923. mutex_lock(&adev->lock_reset);
  2924. atomic_inc(&adev->gpu_reset_counter);
  2925. adev->in_gpu_reset = 1;
  2926. /* Block kfd */
  2927. amdgpu_amdkfd_pre_reset(adev);
  2928. /* block TTM */
  2929. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  2930. /* block all schedulers and reset given job's ring */
  2931. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2932. struct amdgpu_ring *ring = adev->rings[i];
  2933. if (!ring || !ring->sched.thread)
  2934. continue;
  2935. kthread_park(ring->sched.thread);
  2936. if (job && job->base.sched != &ring->sched)
  2937. continue;
  2938. drm_sched_hw_job_reset(&ring->sched, job ? &job->base : NULL);
  2939. /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
  2940. amdgpu_fence_driver_force_completion(ring);
  2941. }
  2942. if (amdgpu_sriov_vf(adev))
  2943. r = amdgpu_device_reset_sriov(adev, job ? false : true);
  2944. else
  2945. r = amdgpu_device_reset(adev);
  2946. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2947. struct amdgpu_ring *ring = adev->rings[i];
  2948. if (!ring || !ring->sched.thread)
  2949. continue;
  2950. /* only need recovery sched of the given job's ring
  2951. * or all rings (in the case @job is NULL)
  2952. * after above amdgpu_reset accomplished
  2953. */
  2954. if ((!job || job->base.sched == &ring->sched) && !r)
  2955. drm_sched_job_recovery(&ring->sched);
  2956. kthread_unpark(ring->sched.thread);
  2957. }
  2958. if (!amdgpu_device_has_dc_support(adev)) {
  2959. drm_helper_resume_force_mode(adev->ddev);
  2960. }
  2961. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  2962. if (r) {
  2963. /* bad news, how to tell it to userspace ? */
  2964. dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
  2965. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
  2966. } else {
  2967. dev_info(adev->dev, "GPU reset(%d) succeeded!\n",atomic_read(&adev->gpu_reset_counter));
  2968. }
  2969. /*unlock kfd */
  2970. amdgpu_amdkfd_post_reset(adev);
  2971. amdgpu_vf_error_trans_all(adev);
  2972. adev->in_gpu_reset = 0;
  2973. mutex_unlock(&adev->lock_reset);
  2974. return r;
  2975. }
  2976. /**
  2977. * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
  2978. *
  2979. * @adev: amdgpu_device pointer
  2980. *
  2981. * Fetchs and stores in the driver the PCIE capabilities (gen speed
  2982. * and lanes) of the slot the device is in. Handles APUs and
  2983. * virtualized environments where PCIE config space may not be available.
  2984. */
  2985. static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
  2986. {
  2987. struct pci_dev *pdev;
  2988. enum pci_bus_speed speed_cap;
  2989. enum pcie_link_width link_width;
  2990. if (amdgpu_pcie_gen_cap)
  2991. adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
  2992. if (amdgpu_pcie_lane_cap)
  2993. adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
  2994. /* covers APUs as well */
  2995. if (pci_is_root_bus(adev->pdev->bus)) {
  2996. if (adev->pm.pcie_gen_mask == 0)
  2997. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2998. if (adev->pm.pcie_mlw_mask == 0)
  2999. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  3000. return;
  3001. }
  3002. if (adev->pm.pcie_gen_mask == 0) {
  3003. /* asic caps */
  3004. pdev = adev->pdev;
  3005. speed_cap = pcie_get_speed_cap(pdev);
  3006. if (speed_cap == PCI_SPEED_UNKNOWN) {
  3007. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  3008. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  3009. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  3010. } else {
  3011. if (speed_cap == PCIE_SPEED_16_0GT)
  3012. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  3013. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  3014. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
  3015. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
  3016. else if (speed_cap == PCIE_SPEED_8_0GT)
  3017. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  3018. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  3019. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  3020. else if (speed_cap == PCIE_SPEED_5_0GT)
  3021. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  3022. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
  3023. else
  3024. adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
  3025. }
  3026. /* platform caps */
  3027. pdev = adev->ddev->pdev->bus->self;
  3028. speed_cap = pcie_get_speed_cap(pdev);
  3029. if (speed_cap == PCI_SPEED_UNKNOWN) {
  3030. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  3031. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
  3032. } else {
  3033. if (speed_cap == PCIE_SPEED_16_0GT)
  3034. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  3035. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  3036. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
  3037. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
  3038. else if (speed_cap == PCIE_SPEED_8_0GT)
  3039. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  3040. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  3041. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
  3042. else if (speed_cap == PCIE_SPEED_5_0GT)
  3043. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  3044. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
  3045. else
  3046. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
  3047. }
  3048. }
  3049. if (adev->pm.pcie_mlw_mask == 0) {
  3050. pdev = adev->ddev->pdev->bus->self;
  3051. link_width = pcie_get_width_cap(pdev);
  3052. if (link_width == PCIE_LNK_WIDTH_UNKNOWN) {
  3053. adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
  3054. } else {
  3055. switch (link_width) {
  3056. case PCIE_LNK_X32:
  3057. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
  3058. CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  3059. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  3060. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  3061. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3062. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3063. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3064. break;
  3065. case PCIE_LNK_X16:
  3066. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  3067. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  3068. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  3069. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3070. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3071. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3072. break;
  3073. case PCIE_LNK_X12:
  3074. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  3075. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  3076. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3077. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3078. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3079. break;
  3080. case PCIE_LNK_X8:
  3081. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  3082. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3083. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3084. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3085. break;
  3086. case PCIE_LNK_X4:
  3087. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3088. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3089. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3090. break;
  3091. case PCIE_LNK_X2:
  3092. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3093. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3094. break;
  3095. case PCIE_LNK_X1:
  3096. adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
  3097. break;
  3098. default:
  3099. break;
  3100. }
  3101. }
  3102. }
  3103. }