amdgpu_device.c 90 KB

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