amdgpu_device.c 88 KB

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