amdgpu_device.c 89 KB

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