amdgpu_device.c 88 KB

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