amdgpu_device.c 90 KB

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