amdgpu_device.c 88 KB

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