amdgpu_device.c 88 KB

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