amdgpu_device.c 90 KB

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