amdgpu_device.c 93 KB

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