amdgpu_device.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470
  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. if (amdgpu_sriov_vf(adev))
  1341. adev->powerplay.pp_feature &= ~PP_GFXOFF_MASK;
  1342. for (i = 0; i < adev->num_ip_blocks; i++) {
  1343. if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
  1344. DRM_ERROR("disabled ip block: %d <%s>\n",
  1345. i, adev->ip_blocks[i].version->funcs->name);
  1346. adev->ip_blocks[i].status.valid = false;
  1347. } else {
  1348. if (adev->ip_blocks[i].version->funcs->early_init) {
  1349. r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
  1350. if (r == -ENOENT) {
  1351. adev->ip_blocks[i].status.valid = false;
  1352. } else if (r) {
  1353. DRM_ERROR("early_init of IP block <%s> failed %d\n",
  1354. adev->ip_blocks[i].version->funcs->name, r);
  1355. return r;
  1356. } else {
  1357. adev->ip_blocks[i].status.valid = true;
  1358. }
  1359. } else {
  1360. adev->ip_blocks[i].status.valid = true;
  1361. }
  1362. }
  1363. }
  1364. adev->cg_flags &= amdgpu_cg_mask;
  1365. adev->pg_flags &= amdgpu_pg_mask;
  1366. return 0;
  1367. }
  1368. static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
  1369. {
  1370. int i, r;
  1371. for (i = 0; i < adev->num_ip_blocks; i++) {
  1372. if (!adev->ip_blocks[i].status.sw)
  1373. continue;
  1374. if (adev->ip_blocks[i].status.hw)
  1375. continue;
  1376. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1377. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
  1378. r = adev->ip_blocks[i].version->funcs->hw_init(adev);
  1379. if (r) {
  1380. DRM_ERROR("hw_init of IP block <%s> failed %d\n",
  1381. adev->ip_blocks[i].version->funcs->name, r);
  1382. return r;
  1383. }
  1384. adev->ip_blocks[i].status.hw = true;
  1385. }
  1386. }
  1387. return 0;
  1388. }
  1389. static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
  1390. {
  1391. int i, r;
  1392. for (i = 0; i < adev->num_ip_blocks; i++) {
  1393. if (!adev->ip_blocks[i].status.sw)
  1394. continue;
  1395. if (adev->ip_blocks[i].status.hw)
  1396. continue;
  1397. r = adev->ip_blocks[i].version->funcs->hw_init(adev);
  1398. if (r) {
  1399. DRM_ERROR("hw_init of IP block <%s> failed %d\n",
  1400. adev->ip_blocks[i].version->funcs->name, r);
  1401. return r;
  1402. }
  1403. adev->ip_blocks[i].status.hw = true;
  1404. }
  1405. return 0;
  1406. }
  1407. /**
  1408. * amdgpu_device_ip_init - run init for hardware IPs
  1409. *
  1410. * @adev: amdgpu_device pointer
  1411. *
  1412. * Main initialization pass for hardware IPs. The list of all the hardware
  1413. * IPs that make up the asic is walked and the sw_init and hw_init callbacks
  1414. * are run. sw_init initializes the software state associated with each IP
  1415. * and hw_init initializes the hardware associated with each IP.
  1416. * Returns 0 on success, negative error code on failure.
  1417. */
  1418. static int amdgpu_device_ip_init(struct amdgpu_device *adev)
  1419. {
  1420. int i, r;
  1421. for (i = 0; i < adev->num_ip_blocks; i++) {
  1422. if (!adev->ip_blocks[i].status.valid)
  1423. continue;
  1424. r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
  1425. if (r) {
  1426. DRM_ERROR("sw_init of IP block <%s> failed %d\n",
  1427. adev->ip_blocks[i].version->funcs->name, r);
  1428. return r;
  1429. }
  1430. adev->ip_blocks[i].status.sw = true;
  1431. /* need to do gmc hw init early so we can allocate gpu mem */
  1432. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1433. r = amdgpu_device_vram_scratch_init(adev);
  1434. if (r) {
  1435. DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
  1436. return r;
  1437. }
  1438. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1439. if (r) {
  1440. DRM_ERROR("hw_init %d failed %d\n", i, r);
  1441. return r;
  1442. }
  1443. r = amdgpu_device_wb_init(adev);
  1444. if (r) {
  1445. DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
  1446. return r;
  1447. }
  1448. adev->ip_blocks[i].status.hw = true;
  1449. /* right after GMC hw init, we create CSA */
  1450. if (amdgpu_sriov_vf(adev)) {
  1451. r = amdgpu_allocate_static_csa(adev);
  1452. if (r) {
  1453. DRM_ERROR("allocate CSA failed %d\n", r);
  1454. return r;
  1455. }
  1456. }
  1457. }
  1458. }
  1459. r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
  1460. if (r)
  1461. return r;
  1462. r = amdgpu_device_ip_hw_init_phase1(adev);
  1463. if (r)
  1464. return r;
  1465. r = amdgpu_device_ip_hw_init_phase2(adev);
  1466. if (r)
  1467. return r;
  1468. amdgpu_xgmi_add_device(adev);
  1469. amdgpu_amdkfd_device_init(adev);
  1470. if (amdgpu_sriov_vf(adev))
  1471. amdgpu_virt_release_full_gpu(adev, true);
  1472. return 0;
  1473. }
  1474. /**
  1475. * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
  1476. *
  1477. * @adev: amdgpu_device pointer
  1478. *
  1479. * Writes a reset magic value to the gart pointer in VRAM. The driver calls
  1480. * this function before a GPU reset. If the value is retained after a
  1481. * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
  1482. */
  1483. static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
  1484. {
  1485. memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
  1486. }
  1487. /**
  1488. * amdgpu_device_check_vram_lost - check if vram is valid
  1489. *
  1490. * @adev: amdgpu_device pointer
  1491. *
  1492. * Checks the reset magic value written to the gart pointer in VRAM.
  1493. * The driver calls this after a GPU reset to see if the contents of
  1494. * VRAM is lost or now.
  1495. * returns true if vram is lost, false if not.
  1496. */
  1497. static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
  1498. {
  1499. return !!memcmp(adev->gart.ptr, adev->reset_magic,
  1500. AMDGPU_RESET_MAGIC_NUM);
  1501. }
  1502. /**
  1503. * amdgpu_device_set_cg_state - set clockgating for amdgpu device
  1504. *
  1505. * @adev: amdgpu_device pointer
  1506. *
  1507. * The list of all the hardware IPs that make up the asic is walked and the
  1508. * set_clockgating_state callbacks are run.
  1509. * Late initialization pass enabling clockgating for hardware IPs.
  1510. * Fini or suspend, pass disabling clockgating for hardware IPs.
  1511. * Returns 0 on success, negative error code on failure.
  1512. */
  1513. static int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
  1514. enum amd_clockgating_state state)
  1515. {
  1516. int i, j, r;
  1517. if (amdgpu_emu_mode == 1)
  1518. return 0;
  1519. for (j = 0; j < adev->num_ip_blocks; j++) {
  1520. i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
  1521. if (!adev->ip_blocks[i].status.late_initialized)
  1522. continue;
  1523. /* skip CG for VCE/UVD, it's handled specially */
  1524. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1525. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
  1526. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
  1527. adev->ip_blocks[i].version->funcs->set_clockgating_state) {
  1528. /* enable clockgating to save power */
  1529. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1530. state);
  1531. if (r) {
  1532. DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
  1533. adev->ip_blocks[i].version->funcs->name, r);
  1534. return r;
  1535. }
  1536. }
  1537. }
  1538. return 0;
  1539. }
  1540. static int amdgpu_device_set_pg_state(struct amdgpu_device *adev, enum amd_powergating_state state)
  1541. {
  1542. int i, j, r;
  1543. if (amdgpu_emu_mode == 1)
  1544. return 0;
  1545. for (j = 0; j < adev->num_ip_blocks; j++) {
  1546. i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
  1547. if (!adev->ip_blocks[i].status.late_initialized)
  1548. continue;
  1549. /* skip CG for VCE/UVD, it's handled specially */
  1550. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1551. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
  1552. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
  1553. adev->ip_blocks[i].version->funcs->set_powergating_state) {
  1554. /* enable powergating to save power */
  1555. r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
  1556. state);
  1557. if (r) {
  1558. DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
  1559. adev->ip_blocks[i].version->funcs->name, r);
  1560. return r;
  1561. }
  1562. }
  1563. }
  1564. return 0;
  1565. }
  1566. /**
  1567. * amdgpu_device_ip_late_init - run late init for hardware IPs
  1568. *
  1569. * @adev: amdgpu_device pointer
  1570. *
  1571. * Late initialization pass for hardware IPs. The list of all the hardware
  1572. * IPs that make up the asic is walked and the late_init callbacks are run.
  1573. * late_init covers any special initialization that an IP requires
  1574. * after all of the have been initialized or something that needs to happen
  1575. * late in the init process.
  1576. * Returns 0 on success, negative error code on failure.
  1577. */
  1578. static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
  1579. {
  1580. int i = 0, r;
  1581. for (i = 0; i < adev->num_ip_blocks; i++) {
  1582. if (!adev->ip_blocks[i].status.hw)
  1583. continue;
  1584. if (adev->ip_blocks[i].version->funcs->late_init) {
  1585. r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
  1586. if (r) {
  1587. DRM_ERROR("late_init of IP block <%s> failed %d\n",
  1588. adev->ip_blocks[i].version->funcs->name, r);
  1589. return r;
  1590. }
  1591. }
  1592. adev->ip_blocks[i].status.late_initialized = true;
  1593. }
  1594. amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
  1595. amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
  1596. queue_delayed_work(system_wq, &adev->late_init_work,
  1597. msecs_to_jiffies(AMDGPU_RESUME_MS));
  1598. amdgpu_device_fill_reset_magic(adev);
  1599. return 0;
  1600. }
  1601. /**
  1602. * amdgpu_device_ip_fini - run fini for hardware IPs
  1603. *
  1604. * @adev: amdgpu_device pointer
  1605. *
  1606. * Main teardown pass for hardware IPs. The list of all the hardware
  1607. * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
  1608. * are run. hw_fini tears down the hardware associated with each IP
  1609. * and sw_fini tears down any software state associated with each IP.
  1610. * Returns 0 on success, negative error code on failure.
  1611. */
  1612. static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
  1613. {
  1614. int i, r;
  1615. amdgpu_amdkfd_device_fini(adev);
  1616. amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
  1617. amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
  1618. /* need to disable SMC first */
  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->type == AMD_IP_BLOCK_TYPE_SMC) {
  1623. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1624. /* XXX handle errors */
  1625. if (r) {
  1626. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1627. adev->ip_blocks[i].version->funcs->name, r);
  1628. }
  1629. adev->ip_blocks[i].status.hw = false;
  1630. break;
  1631. }
  1632. }
  1633. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1634. if (!adev->ip_blocks[i].status.hw)
  1635. continue;
  1636. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1637. /* XXX handle errors */
  1638. if (r) {
  1639. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1640. adev->ip_blocks[i].version->funcs->name, r);
  1641. }
  1642. adev->ip_blocks[i].status.hw = false;
  1643. }
  1644. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1645. if (!adev->ip_blocks[i].status.sw)
  1646. continue;
  1647. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1648. amdgpu_ucode_free_bo(adev);
  1649. amdgpu_free_static_csa(adev);
  1650. amdgpu_device_wb_fini(adev);
  1651. amdgpu_device_vram_scratch_fini(adev);
  1652. }
  1653. r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
  1654. /* XXX handle errors */
  1655. if (r) {
  1656. DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
  1657. adev->ip_blocks[i].version->funcs->name, r);
  1658. }
  1659. adev->ip_blocks[i].status.sw = false;
  1660. adev->ip_blocks[i].status.valid = false;
  1661. }
  1662. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1663. if (!adev->ip_blocks[i].status.late_initialized)
  1664. continue;
  1665. if (adev->ip_blocks[i].version->funcs->late_fini)
  1666. adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
  1667. adev->ip_blocks[i].status.late_initialized = false;
  1668. }
  1669. if (amdgpu_sriov_vf(adev))
  1670. if (amdgpu_virt_release_full_gpu(adev, false))
  1671. DRM_ERROR("failed to release exclusive mode on fini\n");
  1672. return 0;
  1673. }
  1674. static int amdgpu_device_enable_mgpu_fan_boost(void)
  1675. {
  1676. struct amdgpu_gpu_instance *gpu_ins;
  1677. struct amdgpu_device *adev;
  1678. int i, ret = 0;
  1679. mutex_lock(&mgpu_info.mutex);
  1680. /*
  1681. * MGPU fan boost feature should be enabled
  1682. * only when there are two or more dGPUs in
  1683. * the system
  1684. */
  1685. if (mgpu_info.num_dgpu < 2)
  1686. goto out;
  1687. for (i = 0; i < mgpu_info.num_dgpu; i++) {
  1688. gpu_ins = &(mgpu_info.gpu_ins[i]);
  1689. adev = gpu_ins->adev;
  1690. if (!(adev->flags & AMD_IS_APU) &&
  1691. !gpu_ins->mgpu_fan_enabled &&
  1692. adev->powerplay.pp_funcs &&
  1693. adev->powerplay.pp_funcs->enable_mgpu_fan_boost) {
  1694. ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
  1695. if (ret)
  1696. break;
  1697. gpu_ins->mgpu_fan_enabled = 1;
  1698. }
  1699. }
  1700. out:
  1701. mutex_unlock(&mgpu_info.mutex);
  1702. return ret;
  1703. }
  1704. /**
  1705. * amdgpu_device_ip_late_init_func_handler - work handler for ib test
  1706. *
  1707. * @work: work_struct.
  1708. */
  1709. static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
  1710. {
  1711. struct amdgpu_device *adev =
  1712. container_of(work, struct amdgpu_device, late_init_work.work);
  1713. int r;
  1714. r = amdgpu_ib_ring_tests(adev);
  1715. if (r)
  1716. DRM_ERROR("ib ring test failed (%d).\n", r);
  1717. r = amdgpu_device_enable_mgpu_fan_boost();
  1718. if (r)
  1719. DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
  1720. }
  1721. static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
  1722. {
  1723. struct amdgpu_device *adev =
  1724. container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
  1725. mutex_lock(&adev->gfx.gfx_off_mutex);
  1726. if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
  1727. if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
  1728. adev->gfx.gfx_off_state = true;
  1729. }
  1730. mutex_unlock(&adev->gfx.gfx_off_mutex);
  1731. }
  1732. /**
  1733. * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
  1734. *
  1735. * @adev: amdgpu_device pointer
  1736. *
  1737. * Main suspend function for hardware IPs. The list of all the hardware
  1738. * IPs that make up the asic is walked, clockgating is disabled and the
  1739. * suspend callbacks are run. suspend puts the hardware and software state
  1740. * in each IP into a state suitable for suspend.
  1741. * Returns 0 on success, negative error code on failure.
  1742. */
  1743. static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
  1744. {
  1745. int i, r;
  1746. amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
  1747. amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
  1748. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1749. if (!adev->ip_blocks[i].status.valid)
  1750. continue;
  1751. /* displays are handled separately */
  1752. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) {
  1753. /* XXX handle errors */
  1754. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1755. /* XXX handle errors */
  1756. if (r) {
  1757. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1758. adev->ip_blocks[i].version->funcs->name, r);
  1759. }
  1760. }
  1761. }
  1762. return 0;
  1763. }
  1764. /**
  1765. * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
  1766. *
  1767. * @adev: amdgpu_device pointer
  1768. *
  1769. * Main suspend function for hardware IPs. The list of all the hardware
  1770. * IPs that make up the asic is walked, clockgating is disabled and the
  1771. * suspend callbacks are run. suspend puts the hardware and software state
  1772. * in each IP into a state suitable for suspend.
  1773. * Returns 0 on success, negative error code on failure.
  1774. */
  1775. static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
  1776. {
  1777. int i, r;
  1778. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1779. if (!adev->ip_blocks[i].status.valid)
  1780. continue;
  1781. /* displays are handled in phase1 */
  1782. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
  1783. continue;
  1784. /* XXX handle errors */
  1785. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1786. /* XXX handle errors */
  1787. if (r) {
  1788. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1789. adev->ip_blocks[i].version->funcs->name, r);
  1790. }
  1791. }
  1792. return 0;
  1793. }
  1794. /**
  1795. * amdgpu_device_ip_suspend - run suspend for hardware IPs
  1796. *
  1797. * @adev: amdgpu_device pointer
  1798. *
  1799. * Main suspend function for hardware IPs. The list of all the hardware
  1800. * IPs that make up the asic is walked, clockgating is disabled and the
  1801. * suspend callbacks are run. suspend puts the hardware and software state
  1802. * in each IP into a state suitable for suspend.
  1803. * Returns 0 on success, negative error code on failure.
  1804. */
  1805. int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
  1806. {
  1807. int r;
  1808. if (amdgpu_sriov_vf(adev))
  1809. amdgpu_virt_request_full_gpu(adev, false);
  1810. r = amdgpu_device_ip_suspend_phase1(adev);
  1811. if (r)
  1812. return r;
  1813. r = amdgpu_device_ip_suspend_phase2(adev);
  1814. if (amdgpu_sriov_vf(adev))
  1815. amdgpu_virt_release_full_gpu(adev, false);
  1816. return r;
  1817. }
  1818. static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
  1819. {
  1820. int i, r;
  1821. static enum amd_ip_block_type ip_order[] = {
  1822. AMD_IP_BLOCK_TYPE_GMC,
  1823. AMD_IP_BLOCK_TYPE_COMMON,
  1824. AMD_IP_BLOCK_TYPE_PSP,
  1825. AMD_IP_BLOCK_TYPE_IH,
  1826. };
  1827. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1828. int j;
  1829. struct amdgpu_ip_block *block;
  1830. for (j = 0; j < adev->num_ip_blocks; j++) {
  1831. block = &adev->ip_blocks[j];
  1832. if (block->version->type != ip_order[i] ||
  1833. !block->status.valid)
  1834. continue;
  1835. r = block->version->funcs->hw_init(adev);
  1836. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
  1837. if (r)
  1838. return r;
  1839. }
  1840. }
  1841. return 0;
  1842. }
  1843. static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
  1844. {
  1845. int i, r;
  1846. static enum amd_ip_block_type ip_order[] = {
  1847. AMD_IP_BLOCK_TYPE_SMC,
  1848. AMD_IP_BLOCK_TYPE_DCE,
  1849. AMD_IP_BLOCK_TYPE_GFX,
  1850. AMD_IP_BLOCK_TYPE_SDMA,
  1851. AMD_IP_BLOCK_TYPE_UVD,
  1852. AMD_IP_BLOCK_TYPE_VCE
  1853. };
  1854. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1855. int j;
  1856. struct amdgpu_ip_block *block;
  1857. for (j = 0; j < adev->num_ip_blocks; j++) {
  1858. block = &adev->ip_blocks[j];
  1859. if (block->version->type != ip_order[i] ||
  1860. !block->status.valid)
  1861. continue;
  1862. r = block->version->funcs->hw_init(adev);
  1863. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
  1864. if (r)
  1865. return r;
  1866. }
  1867. }
  1868. return 0;
  1869. }
  1870. /**
  1871. * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
  1872. *
  1873. * @adev: amdgpu_device pointer
  1874. *
  1875. * First resume function for hardware IPs. The list of all the hardware
  1876. * IPs that make up the asic is walked and the resume callbacks are run for
  1877. * COMMON, GMC, and IH. resume puts the hardware into a functional state
  1878. * after a suspend and updates the software state as necessary. This
  1879. * function is also used for restoring the GPU after a GPU reset.
  1880. * Returns 0 on success, negative error code on failure.
  1881. */
  1882. static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
  1883. {
  1884. int i, r;
  1885. for (i = 0; i < adev->num_ip_blocks; i++) {
  1886. if (!adev->ip_blocks[i].status.valid)
  1887. continue;
  1888. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1889. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1890. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
  1891. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1892. if (r) {
  1893. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1894. adev->ip_blocks[i].version->funcs->name, r);
  1895. return r;
  1896. }
  1897. }
  1898. }
  1899. return 0;
  1900. }
  1901. /**
  1902. * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
  1903. *
  1904. * @adev: amdgpu_device pointer
  1905. *
  1906. * First resume function for hardware IPs. The list of all the hardware
  1907. * IPs that make up the asic is walked and the resume callbacks are run for
  1908. * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
  1909. * functional state after a suspend and updates the software state as
  1910. * necessary. This function is also used for restoring the GPU after a GPU
  1911. * reset.
  1912. * Returns 0 on success, negative error code on failure.
  1913. */
  1914. static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
  1915. {
  1916. int i, r;
  1917. for (i = 0; i < adev->num_ip_blocks; i++) {
  1918. if (!adev->ip_blocks[i].status.valid)
  1919. continue;
  1920. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1921. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1922. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
  1923. continue;
  1924. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1925. if (r) {
  1926. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1927. adev->ip_blocks[i].version->funcs->name, r);
  1928. return r;
  1929. }
  1930. }
  1931. return 0;
  1932. }
  1933. /**
  1934. * amdgpu_device_ip_resume - run resume for hardware IPs
  1935. *
  1936. * @adev: amdgpu_device pointer
  1937. *
  1938. * Main resume function for hardware IPs. The hardware IPs
  1939. * are split into two resume functions because they are
  1940. * are also used in in recovering from a GPU reset and some additional
  1941. * steps need to be take between them. In this case (S3/S4) they are
  1942. * run sequentially.
  1943. * Returns 0 on success, negative error code on failure.
  1944. */
  1945. static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
  1946. {
  1947. int r;
  1948. r = amdgpu_device_ip_resume_phase1(adev);
  1949. if (r)
  1950. return r;
  1951. r = amdgpu_device_ip_resume_phase2(adev);
  1952. return r;
  1953. }
  1954. /**
  1955. * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
  1956. *
  1957. * @adev: amdgpu_device pointer
  1958. *
  1959. * Query the VBIOS data tables to determine if the board supports SR-IOV.
  1960. */
  1961. static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
  1962. {
  1963. if (amdgpu_sriov_vf(adev)) {
  1964. if (adev->is_atom_fw) {
  1965. if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
  1966. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1967. } else {
  1968. if (amdgpu_atombios_has_gpu_virtualization_table(adev))
  1969. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1970. }
  1971. if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
  1972. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
  1973. }
  1974. }
  1975. /**
  1976. * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
  1977. *
  1978. * @asic_type: AMD asic type
  1979. *
  1980. * Check if there is DC (new modesetting infrastructre) support for an asic.
  1981. * returns true if DC has support, false if not.
  1982. */
  1983. bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
  1984. {
  1985. switch (asic_type) {
  1986. #if defined(CONFIG_DRM_AMD_DC)
  1987. case CHIP_BONAIRE:
  1988. case CHIP_KAVERI:
  1989. case CHIP_KABINI:
  1990. case CHIP_MULLINS:
  1991. /*
  1992. * We have systems in the wild with these ASICs that require
  1993. * LVDS and VGA support which is not supported with DC.
  1994. *
  1995. * Fallback to the non-DC driver here by default so as not to
  1996. * cause regressions.
  1997. */
  1998. return amdgpu_dc > 0;
  1999. case CHIP_HAWAII:
  2000. case CHIP_CARRIZO:
  2001. case CHIP_STONEY:
  2002. case CHIP_POLARIS10:
  2003. case CHIP_POLARIS11:
  2004. case CHIP_POLARIS12:
  2005. case CHIP_VEGAM:
  2006. case CHIP_TONGA:
  2007. case CHIP_FIJI:
  2008. case CHIP_VEGA10:
  2009. case CHIP_VEGA12:
  2010. case CHIP_VEGA20:
  2011. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  2012. case CHIP_RAVEN:
  2013. #endif
  2014. return amdgpu_dc != 0;
  2015. #endif
  2016. default:
  2017. return false;
  2018. }
  2019. }
  2020. /**
  2021. * amdgpu_device_has_dc_support - check if dc is supported
  2022. *
  2023. * @adev: amdgpu_device_pointer
  2024. *
  2025. * Returns true for supported, false for not supported
  2026. */
  2027. bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
  2028. {
  2029. if (amdgpu_sriov_vf(adev))
  2030. return false;
  2031. return amdgpu_device_asic_has_dc_support(adev->asic_type);
  2032. }
  2033. /**
  2034. * amdgpu_device_init - initialize the driver
  2035. *
  2036. * @adev: amdgpu_device pointer
  2037. * @ddev: drm dev pointer
  2038. * @pdev: pci dev pointer
  2039. * @flags: driver flags
  2040. *
  2041. * Initializes the driver info and hw (all asics).
  2042. * Returns 0 for success or an error on failure.
  2043. * Called at driver startup.
  2044. */
  2045. int amdgpu_device_init(struct amdgpu_device *adev,
  2046. struct drm_device *ddev,
  2047. struct pci_dev *pdev,
  2048. uint32_t flags)
  2049. {
  2050. int r, i;
  2051. bool runtime = false;
  2052. u32 max_MBps;
  2053. adev->shutdown = false;
  2054. adev->dev = &pdev->dev;
  2055. adev->ddev = ddev;
  2056. adev->pdev = pdev;
  2057. adev->flags = flags;
  2058. adev->asic_type = flags & AMD_ASIC_MASK;
  2059. adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
  2060. if (amdgpu_emu_mode == 1)
  2061. adev->usec_timeout *= 2;
  2062. adev->gmc.gart_size = 512 * 1024 * 1024;
  2063. adev->accel_working = false;
  2064. adev->num_rings = 0;
  2065. adev->mman.buffer_funcs = NULL;
  2066. adev->mman.buffer_funcs_ring = NULL;
  2067. adev->vm_manager.vm_pte_funcs = NULL;
  2068. adev->vm_manager.vm_pte_num_rqs = 0;
  2069. adev->gmc.gmc_funcs = NULL;
  2070. adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  2071. bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
  2072. adev->smc_rreg = &amdgpu_invalid_rreg;
  2073. adev->smc_wreg = &amdgpu_invalid_wreg;
  2074. adev->pcie_rreg = &amdgpu_invalid_rreg;
  2075. adev->pcie_wreg = &amdgpu_invalid_wreg;
  2076. adev->pciep_rreg = &amdgpu_invalid_rreg;
  2077. adev->pciep_wreg = &amdgpu_invalid_wreg;
  2078. adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
  2079. adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
  2080. adev->didt_rreg = &amdgpu_invalid_rreg;
  2081. adev->didt_wreg = &amdgpu_invalid_wreg;
  2082. adev->gc_cac_rreg = &amdgpu_invalid_rreg;
  2083. adev->gc_cac_wreg = &amdgpu_invalid_wreg;
  2084. adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
  2085. adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
  2086. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
  2087. amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
  2088. pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
  2089. /* mutex initialization are all done here so we
  2090. * can recall function without having locking issues */
  2091. atomic_set(&adev->irq.ih.lock, 0);
  2092. mutex_init(&adev->firmware.mutex);
  2093. mutex_init(&adev->pm.mutex);
  2094. mutex_init(&adev->gfx.gpu_clock_mutex);
  2095. mutex_init(&adev->srbm_mutex);
  2096. mutex_init(&adev->gfx.pipe_reserve_mutex);
  2097. mutex_init(&adev->gfx.gfx_off_mutex);
  2098. mutex_init(&adev->grbm_idx_mutex);
  2099. mutex_init(&adev->mn_lock);
  2100. mutex_init(&adev->virt.vf_errors.lock);
  2101. hash_init(adev->mn_hash);
  2102. mutex_init(&adev->lock_reset);
  2103. amdgpu_device_check_arguments(adev);
  2104. spin_lock_init(&adev->mmio_idx_lock);
  2105. spin_lock_init(&adev->smc_idx_lock);
  2106. spin_lock_init(&adev->pcie_idx_lock);
  2107. spin_lock_init(&adev->uvd_ctx_idx_lock);
  2108. spin_lock_init(&adev->didt_idx_lock);
  2109. spin_lock_init(&adev->gc_cac_idx_lock);
  2110. spin_lock_init(&adev->se_cac_idx_lock);
  2111. spin_lock_init(&adev->audio_endpt_idx_lock);
  2112. spin_lock_init(&adev->mm_stats.lock);
  2113. INIT_LIST_HEAD(&adev->shadow_list);
  2114. mutex_init(&adev->shadow_list_lock);
  2115. INIT_LIST_HEAD(&adev->ring_lru_list);
  2116. spin_lock_init(&adev->ring_lru_list_lock);
  2117. INIT_DELAYED_WORK(&adev->late_init_work,
  2118. amdgpu_device_ip_late_init_func_handler);
  2119. INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
  2120. amdgpu_device_delay_enable_gfx_off);
  2121. adev->gfx.gfx_off_req_count = 1;
  2122. adev->pm.ac_power = power_supply_is_system_supplied() > 0 ? true : false;
  2123. /* Registers mapping */
  2124. /* TODO: block userspace mapping of io register */
  2125. if (adev->asic_type >= CHIP_BONAIRE) {
  2126. adev->rmmio_base = pci_resource_start(adev->pdev, 5);
  2127. adev->rmmio_size = pci_resource_len(adev->pdev, 5);
  2128. } else {
  2129. adev->rmmio_base = pci_resource_start(adev->pdev, 2);
  2130. adev->rmmio_size = pci_resource_len(adev->pdev, 2);
  2131. }
  2132. adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
  2133. if (adev->rmmio == NULL) {
  2134. return -ENOMEM;
  2135. }
  2136. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
  2137. DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  2138. /* doorbell bar mapping */
  2139. amdgpu_device_doorbell_init(adev);
  2140. /* io port mapping */
  2141. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  2142. if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
  2143. adev->rio_mem_size = pci_resource_len(adev->pdev, i);
  2144. adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
  2145. break;
  2146. }
  2147. }
  2148. if (adev->rio_mem == NULL)
  2149. DRM_INFO("PCI I/O BAR is not found.\n");
  2150. amdgpu_device_get_pcie_info(adev);
  2151. /* early init functions */
  2152. r = amdgpu_device_ip_early_init(adev);
  2153. if (r)
  2154. return r;
  2155. /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
  2156. /* this will fail for cards that aren't VGA class devices, just
  2157. * ignore it */
  2158. vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
  2159. if (amdgpu_device_is_px(ddev))
  2160. runtime = true;
  2161. if (!pci_is_thunderbolt_attached(adev->pdev))
  2162. vga_switcheroo_register_client(adev->pdev,
  2163. &amdgpu_switcheroo_ops, runtime);
  2164. if (runtime)
  2165. vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
  2166. if (amdgpu_emu_mode == 1) {
  2167. /* post the asic on emulation mode */
  2168. emu_soc_asic_init(adev);
  2169. goto fence_driver_init;
  2170. }
  2171. /* Read BIOS */
  2172. if (!amdgpu_get_bios(adev)) {
  2173. r = -EINVAL;
  2174. goto failed;
  2175. }
  2176. r = amdgpu_atombios_init(adev);
  2177. if (r) {
  2178. dev_err(adev->dev, "amdgpu_atombios_init failed\n");
  2179. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
  2180. goto failed;
  2181. }
  2182. /* detect if we are with an SRIOV vbios */
  2183. amdgpu_device_detect_sriov_bios(adev);
  2184. /* Post card if necessary */
  2185. if (amdgpu_device_need_post(adev)) {
  2186. if (!adev->bios) {
  2187. dev_err(adev->dev, "no vBIOS found\n");
  2188. r = -EINVAL;
  2189. goto failed;
  2190. }
  2191. DRM_INFO("GPU posting now...\n");
  2192. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2193. if (r) {
  2194. dev_err(adev->dev, "gpu post error!\n");
  2195. goto failed;
  2196. }
  2197. }
  2198. if (adev->is_atom_fw) {
  2199. /* Initialize clocks */
  2200. r = amdgpu_atomfirmware_get_clock_info(adev);
  2201. if (r) {
  2202. dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
  2203. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  2204. goto failed;
  2205. }
  2206. } else {
  2207. /* Initialize clocks */
  2208. r = amdgpu_atombios_get_clock_info(adev);
  2209. if (r) {
  2210. dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
  2211. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  2212. goto failed;
  2213. }
  2214. /* init i2c buses */
  2215. if (!amdgpu_device_has_dc_support(adev))
  2216. amdgpu_atombios_i2c_init(adev);
  2217. }
  2218. fence_driver_init:
  2219. /* Fence driver */
  2220. r = amdgpu_fence_driver_init(adev);
  2221. if (r) {
  2222. dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
  2223. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
  2224. goto failed;
  2225. }
  2226. /* init the mode config */
  2227. drm_mode_config_init(adev->ddev);
  2228. r = amdgpu_device_ip_init(adev);
  2229. if (r) {
  2230. /* failed in exclusive mode due to timeout */
  2231. if (amdgpu_sriov_vf(adev) &&
  2232. !amdgpu_sriov_runtime(adev) &&
  2233. amdgpu_virt_mmio_blocked(adev) &&
  2234. !amdgpu_virt_wait_reset(adev)) {
  2235. dev_err(adev->dev, "VF exclusive mode timeout\n");
  2236. /* Don't send request since VF is inactive. */
  2237. adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
  2238. adev->virt.ops = NULL;
  2239. r = -EAGAIN;
  2240. goto failed;
  2241. }
  2242. dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
  2243. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
  2244. goto failed;
  2245. }
  2246. adev->accel_working = true;
  2247. amdgpu_vm_check_compute_bug(adev);
  2248. /* Initialize the buffer migration limit. */
  2249. if (amdgpu_moverate >= 0)
  2250. max_MBps = amdgpu_moverate;
  2251. else
  2252. max_MBps = 8; /* Allow 8 MB/s. */
  2253. /* Get a log2 for easy divisions. */
  2254. adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
  2255. r = amdgpu_ib_pool_init(adev);
  2256. if (r) {
  2257. dev_err(adev->dev, "IB initialization failed (%d).\n", r);
  2258. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
  2259. goto failed;
  2260. }
  2261. if (amdgpu_sriov_vf(adev))
  2262. amdgpu_virt_init_data_exchange(adev);
  2263. amdgpu_fbdev_init(adev);
  2264. r = amdgpu_pm_sysfs_init(adev);
  2265. if (r)
  2266. DRM_ERROR("registering pm debugfs failed (%d).\n", r);
  2267. r = amdgpu_debugfs_gem_init(adev);
  2268. if (r)
  2269. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  2270. r = amdgpu_debugfs_regs_init(adev);
  2271. if (r)
  2272. DRM_ERROR("registering register debugfs failed (%d).\n", r);
  2273. r = amdgpu_debugfs_firmware_init(adev);
  2274. if (r)
  2275. DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
  2276. r = amdgpu_debugfs_init(adev);
  2277. if (r)
  2278. DRM_ERROR("Creating debugfs files failed (%d).\n", r);
  2279. if ((amdgpu_testing & 1)) {
  2280. if (adev->accel_working)
  2281. amdgpu_test_moves(adev);
  2282. else
  2283. DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
  2284. }
  2285. if (amdgpu_benchmarking) {
  2286. if (adev->accel_working)
  2287. amdgpu_benchmark(adev, amdgpu_benchmarking);
  2288. else
  2289. DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
  2290. }
  2291. /* enable clockgating, etc. after ib tests, etc. since some blocks require
  2292. * explicit gating rather than handling it automatically.
  2293. */
  2294. r = amdgpu_device_ip_late_init(adev);
  2295. if (r) {
  2296. dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
  2297. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
  2298. goto failed;
  2299. }
  2300. return 0;
  2301. failed:
  2302. amdgpu_vf_error_trans_all(adev);
  2303. if (runtime)
  2304. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  2305. return r;
  2306. }
  2307. /**
  2308. * amdgpu_device_fini - tear down the driver
  2309. *
  2310. * @adev: amdgpu_device pointer
  2311. *
  2312. * Tear down the driver info (all asics).
  2313. * Called at driver shutdown.
  2314. */
  2315. void amdgpu_device_fini(struct amdgpu_device *adev)
  2316. {
  2317. int r;
  2318. DRM_INFO("amdgpu: finishing device.\n");
  2319. adev->shutdown = true;
  2320. /* disable all interrupts */
  2321. amdgpu_irq_disable_all(adev);
  2322. if (adev->mode_info.mode_config_initialized){
  2323. if (!amdgpu_device_has_dc_support(adev))
  2324. drm_crtc_force_disable_all(adev->ddev);
  2325. else
  2326. drm_atomic_helper_shutdown(adev->ddev);
  2327. }
  2328. amdgpu_ib_pool_fini(adev);
  2329. amdgpu_fence_driver_fini(adev);
  2330. amdgpu_pm_sysfs_fini(adev);
  2331. amdgpu_fbdev_fini(adev);
  2332. r = amdgpu_device_ip_fini(adev);
  2333. if (adev->firmware.gpu_info_fw) {
  2334. release_firmware(adev->firmware.gpu_info_fw);
  2335. adev->firmware.gpu_info_fw = NULL;
  2336. }
  2337. adev->accel_working = false;
  2338. cancel_delayed_work_sync(&adev->late_init_work);
  2339. /* free i2c buses */
  2340. if (!amdgpu_device_has_dc_support(adev))
  2341. amdgpu_i2c_fini(adev);
  2342. if (amdgpu_emu_mode != 1)
  2343. amdgpu_atombios_fini(adev);
  2344. kfree(adev->bios);
  2345. adev->bios = NULL;
  2346. if (!pci_is_thunderbolt_attached(adev->pdev))
  2347. vga_switcheroo_unregister_client(adev->pdev);
  2348. if (adev->flags & AMD_IS_PX)
  2349. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  2350. vga_client_register(adev->pdev, NULL, NULL, NULL);
  2351. if (adev->rio_mem)
  2352. pci_iounmap(adev->pdev, adev->rio_mem);
  2353. adev->rio_mem = NULL;
  2354. iounmap(adev->rmmio);
  2355. adev->rmmio = NULL;
  2356. amdgpu_device_doorbell_fini(adev);
  2357. amdgpu_debugfs_regs_cleanup(adev);
  2358. }
  2359. /*
  2360. * Suspend & resume.
  2361. */
  2362. /**
  2363. * amdgpu_device_suspend - initiate device suspend
  2364. *
  2365. * @dev: drm dev pointer
  2366. * @suspend: suspend state
  2367. * @fbcon : notify the fbdev of suspend
  2368. *
  2369. * Puts the hw in the suspend state (all asics).
  2370. * Returns 0 for success or an error on failure.
  2371. * Called at driver suspend.
  2372. */
  2373. int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
  2374. {
  2375. struct amdgpu_device *adev;
  2376. struct drm_crtc *crtc;
  2377. struct drm_connector *connector;
  2378. int r;
  2379. if (dev == NULL || dev->dev_private == NULL) {
  2380. return -ENODEV;
  2381. }
  2382. adev = dev->dev_private;
  2383. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2384. return 0;
  2385. adev->in_suspend = true;
  2386. drm_kms_helper_poll_disable(dev);
  2387. if (fbcon)
  2388. amdgpu_fbdev_set_suspend(adev, 1);
  2389. cancel_delayed_work_sync(&adev->late_init_work);
  2390. if (!amdgpu_device_has_dc_support(adev)) {
  2391. /* turn off display hw */
  2392. drm_modeset_lock_all(dev);
  2393. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2394. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  2395. }
  2396. drm_modeset_unlock_all(dev);
  2397. /* unpin the front buffers and cursors */
  2398. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2399. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2400. struct drm_framebuffer *fb = crtc->primary->fb;
  2401. struct amdgpu_bo *robj;
  2402. if (amdgpu_crtc->cursor_bo) {
  2403. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2404. r = amdgpu_bo_reserve(aobj, true);
  2405. if (r == 0) {
  2406. amdgpu_bo_unpin(aobj);
  2407. amdgpu_bo_unreserve(aobj);
  2408. }
  2409. }
  2410. if (fb == NULL || fb->obj[0] == NULL) {
  2411. continue;
  2412. }
  2413. robj = gem_to_amdgpu_bo(fb->obj[0]);
  2414. /* don't unpin kernel fb objects */
  2415. if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
  2416. r = amdgpu_bo_reserve(robj, true);
  2417. if (r == 0) {
  2418. amdgpu_bo_unpin(robj);
  2419. amdgpu_bo_unreserve(robj);
  2420. }
  2421. }
  2422. }
  2423. }
  2424. amdgpu_amdkfd_suspend(adev);
  2425. r = amdgpu_device_ip_suspend_phase1(adev);
  2426. /* evict vram memory */
  2427. amdgpu_bo_evict_vram(adev);
  2428. amdgpu_fence_driver_suspend(adev);
  2429. r = amdgpu_device_ip_suspend_phase2(adev);
  2430. /* evict remaining vram memory
  2431. * This second call to evict vram is to evict the gart page table
  2432. * using the CPU.
  2433. */
  2434. amdgpu_bo_evict_vram(adev);
  2435. pci_save_state(dev->pdev);
  2436. if (suspend) {
  2437. /* Shut down the device */
  2438. pci_disable_device(dev->pdev);
  2439. pci_set_power_state(dev->pdev, PCI_D3hot);
  2440. } else {
  2441. r = amdgpu_asic_reset(adev);
  2442. if (r)
  2443. DRM_ERROR("amdgpu asic reset failed\n");
  2444. }
  2445. return 0;
  2446. }
  2447. /**
  2448. * amdgpu_device_resume - initiate device resume
  2449. *
  2450. * @dev: drm dev pointer
  2451. * @resume: resume state
  2452. * @fbcon : notify the fbdev of resume
  2453. *
  2454. * Bring the hw back to operating state (all asics).
  2455. * Returns 0 for success or an error on failure.
  2456. * Called at driver resume.
  2457. */
  2458. int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
  2459. {
  2460. struct drm_connector *connector;
  2461. struct amdgpu_device *adev = dev->dev_private;
  2462. struct drm_crtc *crtc;
  2463. int r = 0;
  2464. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2465. return 0;
  2466. if (resume) {
  2467. pci_set_power_state(dev->pdev, PCI_D0);
  2468. pci_restore_state(dev->pdev);
  2469. r = pci_enable_device(dev->pdev);
  2470. if (r)
  2471. return r;
  2472. }
  2473. /* post card */
  2474. if (amdgpu_device_need_post(adev)) {
  2475. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2476. if (r)
  2477. DRM_ERROR("amdgpu asic init failed\n");
  2478. }
  2479. r = amdgpu_device_ip_resume(adev);
  2480. if (r) {
  2481. DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
  2482. return r;
  2483. }
  2484. amdgpu_fence_driver_resume(adev);
  2485. r = amdgpu_device_ip_late_init(adev);
  2486. if (r)
  2487. return r;
  2488. if (!amdgpu_device_has_dc_support(adev)) {
  2489. /* pin cursors */
  2490. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2491. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2492. if (amdgpu_crtc->cursor_bo) {
  2493. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2494. r = amdgpu_bo_reserve(aobj, true);
  2495. if (r == 0) {
  2496. r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
  2497. if (r != 0)
  2498. DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
  2499. amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
  2500. amdgpu_bo_unreserve(aobj);
  2501. }
  2502. }
  2503. }
  2504. }
  2505. r = amdgpu_amdkfd_resume(adev);
  2506. if (r)
  2507. return r;
  2508. /* Make sure IB tests flushed */
  2509. flush_delayed_work(&adev->late_init_work);
  2510. /* blat the mode back in */
  2511. if (fbcon) {
  2512. if (!amdgpu_device_has_dc_support(adev)) {
  2513. /* pre DCE11 */
  2514. drm_helper_resume_force_mode(dev);
  2515. /* turn on display hw */
  2516. drm_modeset_lock_all(dev);
  2517. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2518. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  2519. }
  2520. drm_modeset_unlock_all(dev);
  2521. }
  2522. amdgpu_fbdev_set_suspend(adev, 0);
  2523. }
  2524. drm_kms_helper_poll_enable(dev);
  2525. /*
  2526. * Most of the connector probing functions try to acquire runtime pm
  2527. * refs to ensure that the GPU is powered on when connector polling is
  2528. * performed. Since we're calling this from a runtime PM callback,
  2529. * trying to acquire rpm refs will cause us to deadlock.
  2530. *
  2531. * Since we're guaranteed to be holding the rpm lock, it's safe to
  2532. * temporarily disable the rpm helpers so this doesn't deadlock us.
  2533. */
  2534. #ifdef CONFIG_PM
  2535. dev->dev->power.disable_depth++;
  2536. #endif
  2537. if (!amdgpu_device_has_dc_support(adev))
  2538. drm_helper_hpd_irq_event(dev);
  2539. else
  2540. drm_kms_helper_hotplug_event(dev);
  2541. #ifdef CONFIG_PM
  2542. dev->dev->power.disable_depth--;
  2543. #endif
  2544. adev->in_suspend = false;
  2545. return 0;
  2546. }
  2547. /**
  2548. * amdgpu_device_ip_check_soft_reset - did soft reset succeed
  2549. *
  2550. * @adev: amdgpu_device pointer
  2551. *
  2552. * The list of all the hardware IPs that make up the asic is walked and
  2553. * the check_soft_reset callbacks are run. check_soft_reset determines
  2554. * if the asic is still hung or not.
  2555. * Returns true if any of the IPs are still in a hung state, false if not.
  2556. */
  2557. static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
  2558. {
  2559. int i;
  2560. bool asic_hang = false;
  2561. if (amdgpu_sriov_vf(adev))
  2562. return true;
  2563. if (amdgpu_asic_need_full_reset(adev))
  2564. return true;
  2565. for (i = 0; i < adev->num_ip_blocks; i++) {
  2566. if (!adev->ip_blocks[i].status.valid)
  2567. continue;
  2568. if (adev->ip_blocks[i].version->funcs->check_soft_reset)
  2569. adev->ip_blocks[i].status.hang =
  2570. adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
  2571. if (adev->ip_blocks[i].status.hang) {
  2572. DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
  2573. asic_hang = true;
  2574. }
  2575. }
  2576. return asic_hang;
  2577. }
  2578. /**
  2579. * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
  2580. *
  2581. * @adev: amdgpu_device pointer
  2582. *
  2583. * The list of all the hardware IPs that make up the asic is walked and the
  2584. * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
  2585. * handles any IP specific hardware or software state changes that are
  2586. * necessary for a soft reset to succeed.
  2587. * Returns 0 on success, negative error code on failure.
  2588. */
  2589. static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
  2590. {
  2591. int i, r = 0;
  2592. for (i = 0; i < adev->num_ip_blocks; i++) {
  2593. if (!adev->ip_blocks[i].status.valid)
  2594. continue;
  2595. if (adev->ip_blocks[i].status.hang &&
  2596. adev->ip_blocks[i].version->funcs->pre_soft_reset) {
  2597. r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
  2598. if (r)
  2599. return r;
  2600. }
  2601. }
  2602. return 0;
  2603. }
  2604. /**
  2605. * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
  2606. *
  2607. * @adev: amdgpu_device pointer
  2608. *
  2609. * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
  2610. * reset is necessary to recover.
  2611. * Returns true if a full asic reset is required, false if not.
  2612. */
  2613. static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
  2614. {
  2615. int i;
  2616. if (amdgpu_asic_need_full_reset(adev))
  2617. return true;
  2618. for (i = 0; i < adev->num_ip_blocks; i++) {
  2619. if (!adev->ip_blocks[i].status.valid)
  2620. continue;
  2621. if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
  2622. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
  2623. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
  2624. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
  2625. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
  2626. if (adev->ip_blocks[i].status.hang) {
  2627. DRM_INFO("Some block need full reset!\n");
  2628. return true;
  2629. }
  2630. }
  2631. }
  2632. return false;
  2633. }
  2634. /**
  2635. * amdgpu_device_ip_soft_reset - do a soft reset
  2636. *
  2637. * @adev: amdgpu_device pointer
  2638. *
  2639. * The list of all the hardware IPs that make up the asic is walked and the
  2640. * soft_reset callbacks are run if the block is hung. soft_reset handles any
  2641. * IP specific hardware or software state changes that are necessary to soft
  2642. * reset the IP.
  2643. * Returns 0 on success, negative error code on failure.
  2644. */
  2645. static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
  2646. {
  2647. int i, r = 0;
  2648. for (i = 0; i < adev->num_ip_blocks; i++) {
  2649. if (!adev->ip_blocks[i].status.valid)
  2650. continue;
  2651. if (adev->ip_blocks[i].status.hang &&
  2652. adev->ip_blocks[i].version->funcs->soft_reset) {
  2653. r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
  2654. if (r)
  2655. return r;
  2656. }
  2657. }
  2658. return 0;
  2659. }
  2660. /**
  2661. * amdgpu_device_ip_post_soft_reset - clean up from soft reset
  2662. *
  2663. * @adev: amdgpu_device pointer
  2664. *
  2665. * The list of all the hardware IPs that make up the asic is walked and the
  2666. * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
  2667. * handles any IP specific hardware or software state changes that are
  2668. * necessary after the IP has been soft reset.
  2669. * Returns 0 on success, negative error code on failure.
  2670. */
  2671. static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
  2672. {
  2673. int i, r = 0;
  2674. for (i = 0; i < adev->num_ip_blocks; i++) {
  2675. if (!adev->ip_blocks[i].status.valid)
  2676. continue;
  2677. if (adev->ip_blocks[i].status.hang &&
  2678. adev->ip_blocks[i].version->funcs->post_soft_reset)
  2679. r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
  2680. if (r)
  2681. return r;
  2682. }
  2683. return 0;
  2684. }
  2685. /**
  2686. * amdgpu_device_recover_vram - Recover some VRAM contents
  2687. *
  2688. * @adev: amdgpu_device pointer
  2689. *
  2690. * Restores the contents of VRAM buffers from the shadows in GTT. Used to
  2691. * restore things like GPUVM page tables after a GPU reset where
  2692. * the contents of VRAM might be lost.
  2693. *
  2694. * Returns:
  2695. * 0 on success, negative error code on failure.
  2696. */
  2697. static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
  2698. {
  2699. struct dma_fence *fence = NULL, *next = NULL;
  2700. struct amdgpu_bo *shadow;
  2701. long r = 1, tmo;
  2702. if (amdgpu_sriov_runtime(adev))
  2703. tmo = msecs_to_jiffies(8000);
  2704. else
  2705. tmo = msecs_to_jiffies(100);
  2706. DRM_INFO("recover vram bo from shadow start\n");
  2707. mutex_lock(&adev->shadow_list_lock);
  2708. list_for_each_entry(shadow, &adev->shadow_list, shadow_list) {
  2709. /* No need to recover an evicted BO */
  2710. if (shadow->tbo.mem.mem_type != TTM_PL_TT ||
  2711. shadow->parent->tbo.mem.mem_type != TTM_PL_VRAM)
  2712. continue;
  2713. r = amdgpu_bo_restore_shadow(shadow, &next);
  2714. if (r)
  2715. break;
  2716. if (fence) {
  2717. r = dma_fence_wait_timeout(fence, false, tmo);
  2718. dma_fence_put(fence);
  2719. fence = next;
  2720. if (r <= 0)
  2721. break;
  2722. } else {
  2723. fence = next;
  2724. }
  2725. }
  2726. mutex_unlock(&adev->shadow_list_lock);
  2727. if (fence)
  2728. tmo = dma_fence_wait_timeout(fence, false, tmo);
  2729. dma_fence_put(fence);
  2730. if (r <= 0 || tmo <= 0) {
  2731. DRM_ERROR("recover vram bo from shadow failed\n");
  2732. return -EIO;
  2733. }
  2734. DRM_INFO("recover vram bo from shadow done\n");
  2735. return 0;
  2736. }
  2737. /**
  2738. * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
  2739. *
  2740. * @adev: amdgpu device pointer
  2741. *
  2742. * attempt to do soft-reset or full-reset and reinitialize Asic
  2743. * return 0 means succeeded otherwise failed
  2744. */
  2745. static int amdgpu_device_reset(struct amdgpu_device *adev)
  2746. {
  2747. bool need_full_reset, vram_lost = 0;
  2748. int r;
  2749. need_full_reset = amdgpu_device_ip_need_full_reset(adev);
  2750. if (!need_full_reset) {
  2751. amdgpu_device_ip_pre_soft_reset(adev);
  2752. r = amdgpu_device_ip_soft_reset(adev);
  2753. amdgpu_device_ip_post_soft_reset(adev);
  2754. if (r || amdgpu_device_ip_check_soft_reset(adev)) {
  2755. DRM_INFO("soft reset failed, will fallback to full reset!\n");
  2756. need_full_reset = true;
  2757. }
  2758. }
  2759. if (need_full_reset) {
  2760. r = amdgpu_device_ip_suspend(adev);
  2761. retry:
  2762. r = amdgpu_asic_reset(adev);
  2763. /* post card */
  2764. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2765. if (!r) {
  2766. dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
  2767. r = amdgpu_device_ip_resume_phase1(adev);
  2768. if (r)
  2769. goto out;
  2770. vram_lost = amdgpu_device_check_vram_lost(adev);
  2771. if (vram_lost) {
  2772. DRM_ERROR("VRAM is lost!\n");
  2773. atomic_inc(&adev->vram_lost_counter);
  2774. }
  2775. r = amdgpu_gtt_mgr_recover(
  2776. &adev->mman.bdev.man[TTM_PL_TT]);
  2777. if (r)
  2778. goto out;
  2779. r = amdgpu_device_ip_resume_phase2(adev);
  2780. if (r)
  2781. goto out;
  2782. if (vram_lost)
  2783. amdgpu_device_fill_reset_magic(adev);
  2784. }
  2785. }
  2786. out:
  2787. if (!r) {
  2788. amdgpu_irq_gpu_reset_resume_helper(adev);
  2789. r = amdgpu_ib_ring_tests(adev);
  2790. if (r) {
  2791. dev_err(adev->dev, "ib ring test failed (%d).\n", r);
  2792. r = amdgpu_device_ip_suspend(adev);
  2793. need_full_reset = true;
  2794. goto retry;
  2795. }
  2796. }
  2797. if (!r)
  2798. r = amdgpu_device_recover_vram(adev);
  2799. return r;
  2800. }
  2801. /**
  2802. * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
  2803. *
  2804. * @adev: amdgpu device pointer
  2805. * @from_hypervisor: request from hypervisor
  2806. *
  2807. * do VF FLR and reinitialize Asic
  2808. * return 0 means succeeded otherwise failed
  2809. */
  2810. static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
  2811. bool from_hypervisor)
  2812. {
  2813. int r;
  2814. if (from_hypervisor)
  2815. r = amdgpu_virt_request_full_gpu(adev, true);
  2816. else
  2817. r = amdgpu_virt_reset_gpu(adev);
  2818. if (r)
  2819. return r;
  2820. /* Resume IP prior to SMC */
  2821. r = amdgpu_device_ip_reinit_early_sriov(adev);
  2822. if (r)
  2823. goto error;
  2824. /* we need recover gart prior to run SMC/CP/SDMA resume */
  2825. amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
  2826. /* now we are okay to resume SMC/CP/SDMA */
  2827. r = amdgpu_device_ip_reinit_late_sriov(adev);
  2828. if (r)
  2829. goto error;
  2830. amdgpu_irq_gpu_reset_resume_helper(adev);
  2831. r = amdgpu_ib_ring_tests(adev);
  2832. error:
  2833. amdgpu_virt_release_full_gpu(adev, true);
  2834. if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
  2835. atomic_inc(&adev->vram_lost_counter);
  2836. r = amdgpu_device_recover_vram(adev);
  2837. }
  2838. return r;
  2839. }
  2840. /**
  2841. * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
  2842. *
  2843. * @adev: amdgpu device pointer
  2844. *
  2845. * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
  2846. * a hung GPU.
  2847. */
  2848. bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
  2849. {
  2850. if (!amdgpu_device_ip_check_soft_reset(adev)) {
  2851. DRM_INFO("Timeout, but no hardware hang detected.\n");
  2852. return false;
  2853. }
  2854. if (amdgpu_gpu_recovery == 0 || (amdgpu_gpu_recovery == -1 &&
  2855. !amdgpu_sriov_vf(adev))) {
  2856. DRM_INFO("GPU recovery disabled.\n");
  2857. return false;
  2858. }
  2859. return true;
  2860. }
  2861. /**
  2862. * amdgpu_device_gpu_recover - reset the asic and recover scheduler
  2863. *
  2864. * @adev: amdgpu device pointer
  2865. * @job: which job trigger hang
  2866. *
  2867. * Attempt to reset the GPU if it has hung (all asics).
  2868. * Returns 0 for success or an error on failure.
  2869. */
  2870. int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  2871. struct amdgpu_job *job)
  2872. {
  2873. int i, r, resched;
  2874. dev_info(adev->dev, "GPU reset begin!\n");
  2875. mutex_lock(&adev->lock_reset);
  2876. atomic_inc(&adev->gpu_reset_counter);
  2877. adev->in_gpu_reset = 1;
  2878. /* Block kfd */
  2879. amdgpu_amdkfd_pre_reset(adev);
  2880. /* block TTM */
  2881. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  2882. /* block all schedulers and reset given job's ring */
  2883. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2884. struct amdgpu_ring *ring = adev->rings[i];
  2885. if (!ring || !ring->sched.thread)
  2886. continue;
  2887. kthread_park(ring->sched.thread);
  2888. if (job && job->base.sched == &ring->sched)
  2889. continue;
  2890. drm_sched_hw_job_reset(&ring->sched, job ? &job->base : NULL);
  2891. /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
  2892. amdgpu_fence_driver_force_completion(ring);
  2893. }
  2894. if (amdgpu_sriov_vf(adev))
  2895. r = amdgpu_device_reset_sriov(adev, job ? false : true);
  2896. else
  2897. r = amdgpu_device_reset(adev);
  2898. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2899. struct amdgpu_ring *ring = adev->rings[i];
  2900. if (!ring || !ring->sched.thread)
  2901. continue;
  2902. /* only need recovery sched of the given job's ring
  2903. * or all rings (in the case @job is NULL)
  2904. * after above amdgpu_reset accomplished
  2905. */
  2906. if ((!job || job->base.sched == &ring->sched) && !r)
  2907. drm_sched_job_recovery(&ring->sched);
  2908. kthread_unpark(ring->sched.thread);
  2909. }
  2910. if (!amdgpu_device_has_dc_support(adev)) {
  2911. drm_helper_resume_force_mode(adev->ddev);
  2912. }
  2913. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  2914. if (r) {
  2915. /* bad news, how to tell it to userspace ? */
  2916. dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
  2917. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
  2918. } else {
  2919. dev_info(adev->dev, "GPU reset(%d) succeeded!\n",atomic_read(&adev->gpu_reset_counter));
  2920. }
  2921. /*unlock kfd */
  2922. amdgpu_amdkfd_post_reset(adev);
  2923. amdgpu_vf_error_trans_all(adev);
  2924. adev->in_gpu_reset = 0;
  2925. mutex_unlock(&adev->lock_reset);
  2926. return r;
  2927. }
  2928. /**
  2929. * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
  2930. *
  2931. * @adev: amdgpu_device pointer
  2932. *
  2933. * Fetchs and stores in the driver the PCIE capabilities (gen speed
  2934. * and lanes) of the slot the device is in. Handles APUs and
  2935. * virtualized environments where PCIE config space may not be available.
  2936. */
  2937. static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
  2938. {
  2939. struct pci_dev *pdev;
  2940. enum pci_bus_speed speed_cap;
  2941. enum pcie_link_width link_width;
  2942. if (amdgpu_pcie_gen_cap)
  2943. adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
  2944. if (amdgpu_pcie_lane_cap)
  2945. adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
  2946. /* covers APUs as well */
  2947. if (pci_is_root_bus(adev->pdev->bus)) {
  2948. if (adev->pm.pcie_gen_mask == 0)
  2949. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2950. if (adev->pm.pcie_mlw_mask == 0)
  2951. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2952. return;
  2953. }
  2954. if (adev->pm.pcie_gen_mask == 0) {
  2955. /* asic caps */
  2956. pdev = adev->pdev;
  2957. speed_cap = pcie_get_speed_cap(pdev);
  2958. if (speed_cap == PCI_SPEED_UNKNOWN) {
  2959. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2960. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2961. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2962. } else {
  2963. if (speed_cap == PCIE_SPEED_16_0GT)
  2964. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2965. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2966. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
  2967. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
  2968. else if (speed_cap == PCIE_SPEED_8_0GT)
  2969. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2970. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2971. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2972. else if (speed_cap == PCIE_SPEED_5_0GT)
  2973. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2974. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
  2975. else
  2976. adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
  2977. }
  2978. /* platform caps */
  2979. pdev = adev->ddev->pdev->bus->self;
  2980. speed_cap = pcie_get_speed_cap(pdev);
  2981. if (speed_cap == PCI_SPEED_UNKNOWN) {
  2982. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2983. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
  2984. } else {
  2985. if (speed_cap == PCIE_SPEED_16_0GT)
  2986. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2987. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2988. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
  2989. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
  2990. else if (speed_cap == PCIE_SPEED_8_0GT)
  2991. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2992. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2993. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2994. else if (speed_cap == PCIE_SPEED_5_0GT)
  2995. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2996. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
  2997. else
  2998. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
  2999. }
  3000. }
  3001. if (adev->pm.pcie_mlw_mask == 0) {
  3002. pdev = adev->ddev->pdev->bus->self;
  3003. link_width = pcie_get_width_cap(pdev);
  3004. if (link_width == PCIE_LNK_WIDTH_UNKNOWN) {
  3005. adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
  3006. } else {
  3007. switch (link_width) {
  3008. case PCIE_LNK_X32:
  3009. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
  3010. CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  3011. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  3012. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  3013. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3014. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3015. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3016. break;
  3017. case PCIE_LNK_X16:
  3018. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  3019. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  3020. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  3021. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3022. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3023. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3024. break;
  3025. case PCIE_LNK_X12:
  3026. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  3027. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  3028. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3029. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3030. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3031. break;
  3032. case PCIE_LNK_X8:
  3033. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  3034. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3035. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3036. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3037. break;
  3038. case PCIE_LNK_X4:
  3039. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3040. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3041. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3042. break;
  3043. case PCIE_LNK_X2:
  3044. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3045. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3046. break;
  3047. case PCIE_LNK_X1:
  3048. adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
  3049. break;
  3050. default:
  3051. break;
  3052. }
  3053. }
  3054. }
  3055. }