habanalabs.h 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962
  1. /* SPDX-License-Identifier: GPL-2.0
  2. *
  3. * Copyright 2016-2022 HabanaLabs, Ltd.
  4. * All Rights Reserved.
  5. *
  6. */
  7. #ifndef HABANALABSP_H_
  8. #define HABANALABSP_H_
  9. #include "../include/common/cpucp_if.h"
  10. #include "../include/common/qman_if.h"
  11. #include "../include/hw_ip/mmu/mmu_general.h"
  12. #include <uapi/misc/habanalabs.h>
  13. #include <linux/cdev.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/irqreturn.h>
  16. #include <linux/dma-direction.h>
  17. #include <linux/scatterlist.h>
  18. #include <linux/hashtable.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/rwsem.h>
  21. #include <linux/eventfd.h>
  22. #include <linux/bitfield.h>
  23. #include <linux/genalloc.h>
  24. #include <linux/sched/signal.h>
  25. #include <linux/io-64-nonatomic-lo-hi.h>
  26. #include <linux/coresight.h>
  27. #include <linux/dma-buf.h>
  28. #define HL_NAME "habanalabs"
  29. struct hl_device;
  30. struct hl_fpriv;
  31. #define PCI_VENDOR_ID_HABANALABS 0x1da3
  32. /* Use upper bits of mmap offset to store habana driver specific information.
  33. * bits[63:59] - Encode mmap type
  34. * bits[45:0] - mmap offset value
  35. *
  36. * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these
  37. * defines are w.r.t to PAGE_SIZE
  38. */
  39. #define HL_MMAP_TYPE_SHIFT (59 - PAGE_SHIFT)
  40. #define HL_MMAP_TYPE_MASK (0x1full << HL_MMAP_TYPE_SHIFT)
  41. #define HL_MMAP_TYPE_TS_BUFF (0x10ull << HL_MMAP_TYPE_SHIFT)
  42. #define HL_MMAP_TYPE_BLOCK (0x4ull << HL_MMAP_TYPE_SHIFT)
  43. #define HL_MMAP_TYPE_CB (0x2ull << HL_MMAP_TYPE_SHIFT)
  44. #define HL_MMAP_OFFSET_VALUE_MASK (0x1FFFFFFFFFFFull >> PAGE_SHIFT)
  45. #define HL_MMAP_OFFSET_VALUE_GET(off) (off & HL_MMAP_OFFSET_VALUE_MASK)
  46. #define HL_PENDING_RESET_PER_SEC 10
  47. #define HL_PENDING_RESET_MAX_TRIALS 60 /* 10 minutes */
  48. #define HL_PENDING_RESET_LONG_SEC 60
  49. #define HL_HARD_RESET_MAX_TIMEOUT 120
  50. #define HL_PLDM_HARD_RESET_MAX_TIMEOUT (HL_HARD_RESET_MAX_TIMEOUT * 3)
  51. #define HL_DEVICE_TIMEOUT_USEC 1000000 /* 1 s */
  52. #define HL_HEARTBEAT_PER_USEC 5000000 /* 5 s */
  53. #define HL_PLL_LOW_JOB_FREQ_USEC 5000000 /* 5 s */
  54. #define HL_CPUCP_INFO_TIMEOUT_USEC 10000000 /* 10s */
  55. #define HL_CPUCP_EEPROM_TIMEOUT_USEC 10000000 /* 10s */
  56. #define HL_CPUCP_MON_DUMP_TIMEOUT_USEC 10000000 /* 10s */
  57. #define HL_CPUCP_SEC_ATTEST_INFO_TINEOUT_USEC 10000000 /* 10s */
  58. #define HL_FW_STATUS_POLL_INTERVAL_USEC 10000 /* 10ms */
  59. #define HL_FW_COMMS_STATUS_PLDM_POLL_INTERVAL_USEC 1000000 /* 1s */
  60. #define HL_PCI_ELBI_TIMEOUT_MSEC 10 /* 10ms */
  61. #define HL_SIM_MAX_TIMEOUT_US 100000000 /* 100s */
  62. #define HL_INVALID_QUEUE UINT_MAX
  63. #define HL_COMMON_USER_CQ_INTERRUPT_ID 0xFFF
  64. #define HL_COMMON_DEC_INTERRUPT_ID 0xFFE
  65. #define HL_STATE_DUMP_HIST_LEN 5
  66. /* Default value for device reset trigger , an invalid value */
  67. #define HL_RESET_TRIGGER_DEFAULT 0xFF
  68. #define OBJ_NAMES_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
  69. #define SYNC_TO_ENGINE_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
  70. /* Memory */
  71. #define MEM_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
  72. /* MMU */
  73. #define MMU_HASH_TABLE_BITS 7 /* 1 << 7 buckets */
  74. /**
  75. * enum hl_mmu_page_table_location - mmu page table location
  76. * @MMU_DR_PGT: page-table is located on device DRAM.
  77. * @MMU_HR_PGT: page-table is located on host memory.
  78. * @MMU_NUM_PGT_LOCATIONS: number of page-table locations currently supported.
  79. */
  80. enum hl_mmu_page_table_location {
  81. MMU_DR_PGT = 0, /* device-dram-resident MMU PGT */
  82. MMU_HR_PGT, /* host resident MMU PGT */
  83. MMU_NUM_PGT_LOCATIONS /* num of PGT locations */
  84. };
  85. /**
  86. * enum hl_mmu_enablement - what mmu modules to enable
  87. * @MMU_EN_NONE: mmu disabled.
  88. * @MMU_EN_ALL: enable all.
  89. * @MMU_EN_PMMU_ONLY: Enable only the PMMU leaving the DMMU disabled.
  90. */
  91. enum hl_mmu_enablement {
  92. MMU_EN_NONE = 0,
  93. MMU_EN_ALL = 1,
  94. MMU_EN_PMMU_ONLY = 3, /* N/A for Goya/Gaudi */
  95. };
  96. /*
  97. * HL_RSVD_SOBS 'sync stream' reserved sync objects per QMAN stream
  98. * HL_RSVD_MONS 'sync stream' reserved monitors per QMAN stream
  99. */
  100. #define HL_RSVD_SOBS 2
  101. #define HL_RSVD_MONS 1
  102. /*
  103. * HL_COLLECTIVE_RSVD_MSTR_MONS 'collective' reserved monitors per QMAN stream
  104. */
  105. #define HL_COLLECTIVE_RSVD_MSTR_MONS 2
  106. #define HL_MAX_SOB_VAL (1 << 15)
  107. #define IS_POWER_OF_2(n) (n != 0 && ((n & (n - 1)) == 0))
  108. #define IS_MAX_PENDING_CS_VALID(n) (IS_POWER_OF_2(n) && (n > 1))
  109. #define HL_PCI_NUM_BARS 6
  110. /* Completion queue entry relates to completed job */
  111. #define HL_COMPLETION_MODE_JOB 0
  112. /* Completion queue entry relates to completed command submission */
  113. #define HL_COMPLETION_MODE_CS 1
  114. #define HL_MAX_DCORES 8
  115. /* DMA alloc/free wrappers */
  116. #define hl_asic_dma_alloc_coherent(hdev, size, dma_handle, flags) \
  117. hl_asic_dma_alloc_coherent_caller(hdev, size, dma_handle, flags, __func__)
  118. #define hl_cpu_accessible_dma_pool_alloc(hdev, size, dma_handle) \
  119. hl_cpu_accessible_dma_pool_alloc_caller(hdev, size, dma_handle, __func__)
  120. #define hl_asic_dma_pool_zalloc(hdev, size, mem_flags, dma_handle) \
  121. hl_asic_dma_pool_zalloc_caller(hdev, size, mem_flags, dma_handle, __func__)
  122. #define hl_asic_dma_free_coherent(hdev, size, cpu_addr, dma_handle) \
  123. hl_asic_dma_free_coherent_caller(hdev, size, cpu_addr, dma_handle, __func__)
  124. #define hl_cpu_accessible_dma_pool_free(hdev, size, vaddr) \
  125. hl_cpu_accessible_dma_pool_free_caller(hdev, size, vaddr, __func__)
  126. #define hl_asic_dma_pool_free(hdev, vaddr, dma_addr) \
  127. hl_asic_dma_pool_free_caller(hdev, vaddr, dma_addr, __func__)
  128. /*
  129. * Reset Flags
  130. *
  131. * - HL_DRV_RESET_HARD
  132. * If set do hard reset to all engines. If not set reset just
  133. * compute/DMA engines.
  134. *
  135. * - HL_DRV_RESET_FROM_RESET_THR
  136. * Set if the caller is the hard-reset thread
  137. *
  138. * - HL_DRV_RESET_HEARTBEAT
  139. * Set if reset is due to heartbeat
  140. *
  141. * - HL_DRV_RESET_TDR
  142. * Set if reset is due to TDR
  143. *
  144. * - HL_DRV_RESET_DEV_RELEASE
  145. * Set if reset is due to device release
  146. *
  147. * - HL_DRV_RESET_BYPASS_REQ_TO_FW
  148. * F/W will perform the reset. No need to ask it to reset the device. This is relevant
  149. * only when running with secured f/w
  150. *
  151. * - HL_DRV_RESET_FW_FATAL_ERR
  152. * Set if reset is due to a fatal error from FW
  153. *
  154. * - HL_DRV_RESET_DELAY
  155. * Set if a delay should be added before the reset
  156. */
  157. #define HL_DRV_RESET_HARD (1 << 0)
  158. #define HL_DRV_RESET_FROM_RESET_THR (1 << 1)
  159. #define HL_DRV_RESET_HEARTBEAT (1 << 2)
  160. #define HL_DRV_RESET_TDR (1 << 3)
  161. #define HL_DRV_RESET_DEV_RELEASE (1 << 4)
  162. #define HL_DRV_RESET_BYPASS_REQ_TO_FW (1 << 5)
  163. #define HL_DRV_RESET_FW_FATAL_ERR (1 << 6)
  164. #define HL_DRV_RESET_DELAY (1 << 7)
  165. /*
  166. * Security
  167. */
  168. #define HL_PB_SHARED 1
  169. #define HL_PB_NA 0
  170. #define HL_PB_SINGLE_INSTANCE 1
  171. #define HL_BLOCK_SIZE 0x1000
  172. #define HL_BLOCK_GLBL_ERR_MASK 0xF40
  173. #define HL_BLOCK_GLBL_ERR_ADDR 0xF44
  174. #define HL_BLOCK_GLBL_ERR_CAUSE 0xF48
  175. #define HL_BLOCK_GLBL_SEC_OFFS 0xF80
  176. #define HL_BLOCK_GLBL_SEC_SIZE (HL_BLOCK_SIZE - HL_BLOCK_GLBL_SEC_OFFS)
  177. #define HL_BLOCK_GLBL_SEC_LEN (HL_BLOCK_GLBL_SEC_SIZE / sizeof(u32))
  178. #define UNSET_GLBL_SEC_BIT(array, b) ((array)[((b) / 32)] |= (1 << ((b) % 32)))
  179. enum hl_protection_levels {
  180. SECURED_LVL,
  181. PRIVILEGED_LVL,
  182. NON_SECURED_LVL
  183. };
  184. /**
  185. * struct iterate_module_ctx - HW module iterator
  186. * @fn: function to apply to each HW module instance
  187. * @data: optional internal data to the function iterator
  188. * @rc: return code for optional use of iterator/iterator-caller
  189. */
  190. struct iterate_module_ctx {
  191. /*
  192. * callback for the HW module iterator
  193. * @hdev: pointer to the habanalabs device structure
  194. * @block: block (ASIC specific definition can be dcore/hdcore)
  195. * @inst: HW module instance within the block
  196. * @offset: current HW module instance offset from the 1-st HW module instance
  197. * in the 1-st block
  198. * @ctx: the iterator context.
  199. */
  200. void (*fn)(struct hl_device *hdev, int block, int inst, u32 offset,
  201. struct iterate_module_ctx *ctx);
  202. void *data;
  203. int rc;
  204. };
  205. struct hl_block_glbl_sec {
  206. u32 sec_array[HL_BLOCK_GLBL_SEC_LEN];
  207. };
  208. #define HL_MAX_SOBS_PER_MONITOR 8
  209. /**
  210. * struct hl_gen_wait_properties - properties for generating a wait CB
  211. * @data: command buffer
  212. * @q_idx: queue id is used to extract fence register address
  213. * @size: offset in command buffer
  214. * @sob_base: SOB base to use in this wait CB
  215. * @sob_val: SOB value to wait for
  216. * @mon_id: monitor to use in this wait CB
  217. * @sob_mask: each bit represents a SOB offset from sob_base to be used
  218. */
  219. struct hl_gen_wait_properties {
  220. void *data;
  221. u32 q_idx;
  222. u32 size;
  223. u16 sob_base;
  224. u16 sob_val;
  225. u16 mon_id;
  226. u8 sob_mask;
  227. };
  228. /**
  229. * struct pgt_info - MMU hop page info.
  230. * @node: hash linked-list node for the pgts on host (shadow pgts for device resident MMU and
  231. * actual pgts for host resident MMU).
  232. * @phys_addr: physical address of the pgt.
  233. * @virt_addr: host virtual address of the pgt (see above device/host resident).
  234. * @shadow_addr: shadow hop in the host for device resident MMU.
  235. * @ctx: pointer to the owner ctx.
  236. * @num_of_ptes: indicates how many ptes are used in the pgt. used only for dynamically
  237. * allocated HOPs (all HOPs but HOP0)
  238. *
  239. * The MMU page tables hierarchy can be placed either on the device's DRAM (in which case shadow
  240. * pgts will be stored on host memory) or on host memory (in which case no shadow is required).
  241. *
  242. * When a new level (hop) is needed during mapping this structure will be used to describe
  243. * the newly allocated hop as well as to track number of PTEs in it.
  244. * During unmapping, if no valid PTEs remained in the page of a newly allocated hop, it is
  245. * freed with its pgt_info structure.
  246. */
  247. struct pgt_info {
  248. struct hlist_node node;
  249. u64 phys_addr;
  250. u64 virt_addr;
  251. u64 shadow_addr;
  252. struct hl_ctx *ctx;
  253. int num_of_ptes;
  254. };
  255. /**
  256. * enum hl_pci_match_mode - pci match mode per region
  257. * @PCI_ADDRESS_MATCH_MODE: address match mode
  258. * @PCI_BAR_MATCH_MODE: bar match mode
  259. */
  260. enum hl_pci_match_mode {
  261. PCI_ADDRESS_MATCH_MODE,
  262. PCI_BAR_MATCH_MODE
  263. };
  264. /**
  265. * enum hl_fw_component - F/W components to read version through registers.
  266. * @FW_COMP_BOOT_FIT: boot fit.
  267. * @FW_COMP_PREBOOT: preboot.
  268. * @FW_COMP_LINUX: linux.
  269. */
  270. enum hl_fw_component {
  271. FW_COMP_BOOT_FIT,
  272. FW_COMP_PREBOOT,
  273. FW_COMP_LINUX,
  274. };
  275. /**
  276. * enum hl_fw_types - F/W types present in the system
  277. * @FW_TYPE_NONE: no FW component indication
  278. * @FW_TYPE_LINUX: Linux image for device CPU
  279. * @FW_TYPE_BOOT_CPU: Boot image for device CPU
  280. * @FW_TYPE_PREBOOT_CPU: Indicates pre-loaded CPUs are present in the system
  281. * (preboot, ppboot etc...)
  282. * @FW_TYPE_ALL_TYPES: Mask for all types
  283. */
  284. enum hl_fw_types {
  285. FW_TYPE_NONE = 0x0,
  286. FW_TYPE_LINUX = 0x1,
  287. FW_TYPE_BOOT_CPU = 0x2,
  288. FW_TYPE_PREBOOT_CPU = 0x4,
  289. FW_TYPE_ALL_TYPES =
  290. (FW_TYPE_LINUX | FW_TYPE_BOOT_CPU | FW_TYPE_PREBOOT_CPU)
  291. };
  292. /**
  293. * enum hl_queue_type - Supported QUEUE types.
  294. * @QUEUE_TYPE_NA: queue is not available.
  295. * @QUEUE_TYPE_EXT: external queue which is a DMA channel that may access the
  296. * host.
  297. * @QUEUE_TYPE_INT: internal queue that performs DMA inside the device's
  298. * memories and/or operates the compute engines.
  299. * @QUEUE_TYPE_CPU: S/W queue for communication with the device's CPU.
  300. * @QUEUE_TYPE_HW: queue of DMA and compute engines jobs, for which completion
  301. * notifications are sent by H/W.
  302. */
  303. enum hl_queue_type {
  304. QUEUE_TYPE_NA,
  305. QUEUE_TYPE_EXT,
  306. QUEUE_TYPE_INT,
  307. QUEUE_TYPE_CPU,
  308. QUEUE_TYPE_HW
  309. };
  310. enum hl_cs_type {
  311. CS_TYPE_DEFAULT,
  312. CS_TYPE_SIGNAL,
  313. CS_TYPE_WAIT,
  314. CS_TYPE_COLLECTIVE_WAIT,
  315. CS_RESERVE_SIGNALS,
  316. CS_UNRESERVE_SIGNALS,
  317. CS_TYPE_ENGINE_CORE
  318. };
  319. /*
  320. * struct hl_inbound_pci_region - inbound region descriptor
  321. * @mode: pci match mode for this region
  322. * @addr: region target address
  323. * @size: region size in bytes
  324. * @offset_in_bar: offset within bar (address match mode)
  325. * @bar: bar id
  326. */
  327. struct hl_inbound_pci_region {
  328. enum hl_pci_match_mode mode;
  329. u64 addr;
  330. u64 size;
  331. u64 offset_in_bar;
  332. u8 bar;
  333. };
  334. /*
  335. * struct hl_outbound_pci_region - outbound region descriptor
  336. * @addr: region target address
  337. * @size: region size in bytes
  338. */
  339. struct hl_outbound_pci_region {
  340. u64 addr;
  341. u64 size;
  342. };
  343. /*
  344. * enum queue_cb_alloc_flags - Indicates queue support for CBs that
  345. * allocated by Kernel or by User
  346. * @CB_ALLOC_KERNEL: support only CBs that allocated by Kernel
  347. * @CB_ALLOC_USER: support only CBs that allocated by User
  348. */
  349. enum queue_cb_alloc_flags {
  350. CB_ALLOC_KERNEL = 0x1,
  351. CB_ALLOC_USER = 0x2
  352. };
  353. /*
  354. * struct hl_hw_sob - H/W SOB info.
  355. * @hdev: habanalabs device structure.
  356. * @kref: refcount of this SOB. The SOB will reset once the refcount is zero.
  357. * @sob_id: id of this SOB.
  358. * @sob_addr: the sob offset from the base address.
  359. * @q_idx: the H/W queue that uses this SOB.
  360. * @need_reset: reset indication set when switching to the other sob.
  361. */
  362. struct hl_hw_sob {
  363. struct hl_device *hdev;
  364. struct kref kref;
  365. u32 sob_id;
  366. u32 sob_addr;
  367. u32 q_idx;
  368. bool need_reset;
  369. };
  370. enum hl_collective_mode {
  371. HL_COLLECTIVE_NOT_SUPPORTED = 0x0,
  372. HL_COLLECTIVE_MASTER = 0x1,
  373. HL_COLLECTIVE_SLAVE = 0x2
  374. };
  375. /**
  376. * struct hw_queue_properties - queue information.
  377. * @type: queue type.
  378. * @cb_alloc_flags: bitmap which indicates if the hw queue supports CB
  379. * that allocated by the Kernel driver and therefore,
  380. * a CB handle can be provided for jobs on this queue.
  381. * Otherwise, a CB address must be provided.
  382. * @collective_mode: collective mode of current queue
  383. * @driver_only: true if only the driver is allowed to send a job to this queue,
  384. * false otherwise.
  385. * @binned: True if the queue is binned out and should not be used
  386. * @supports_sync_stream: True if queue supports sync stream
  387. */
  388. struct hw_queue_properties {
  389. enum hl_queue_type type;
  390. enum queue_cb_alloc_flags cb_alloc_flags;
  391. enum hl_collective_mode collective_mode;
  392. u8 driver_only;
  393. u8 binned;
  394. u8 supports_sync_stream;
  395. };
  396. /**
  397. * enum vm_type - virtual memory mapping request information.
  398. * @VM_TYPE_USERPTR: mapping of user memory to device virtual address.
  399. * @VM_TYPE_PHYS_PACK: mapping of DRAM memory to device virtual address.
  400. */
  401. enum vm_type {
  402. VM_TYPE_USERPTR = 0x1,
  403. VM_TYPE_PHYS_PACK = 0x2
  404. };
  405. /**
  406. * enum mmu_op_flags - mmu operation relevant information.
  407. * @MMU_OP_USERPTR: operation on user memory (host resident).
  408. * @MMU_OP_PHYS_PACK: operation on DRAM (device resident).
  409. * @MMU_OP_CLEAR_MEMCACHE: operation has to clear memcache.
  410. * @MMU_OP_SKIP_LOW_CACHE_INV: operation is allowed to skip parts of cache invalidation.
  411. */
  412. enum mmu_op_flags {
  413. MMU_OP_USERPTR = 0x1,
  414. MMU_OP_PHYS_PACK = 0x2,
  415. MMU_OP_CLEAR_MEMCACHE = 0x4,
  416. MMU_OP_SKIP_LOW_CACHE_INV = 0x8,
  417. };
  418. /**
  419. * enum hl_device_hw_state - H/W device state. use this to understand whether
  420. * to do reset before hw_init or not
  421. * @HL_DEVICE_HW_STATE_CLEAN: H/W state is clean. i.e. after hard reset
  422. * @HL_DEVICE_HW_STATE_DIRTY: H/W state is dirty. i.e. we started to execute
  423. * hw_init
  424. */
  425. enum hl_device_hw_state {
  426. HL_DEVICE_HW_STATE_CLEAN = 0,
  427. HL_DEVICE_HW_STATE_DIRTY
  428. };
  429. #define HL_MMU_VA_ALIGNMENT_NOT_NEEDED 0
  430. /**
  431. * struct hl_mmu_properties - ASIC specific MMU address translation properties.
  432. * @start_addr: virtual start address of the memory region.
  433. * @end_addr: virtual end address of the memory region.
  434. * @hop_shifts: array holds HOPs shifts.
  435. * @hop_masks: array holds HOPs masks.
  436. * @last_mask: mask to get the bit indicating this is the last hop.
  437. * @pgt_size: size for page tables.
  438. * @supported_pages_mask: bitmask for supported page size (relevant only for MMUs
  439. * supporting multiple page size).
  440. * @page_size: default page size used to allocate memory.
  441. * @num_hops: The amount of hops supported by the translation table.
  442. * @hop_table_size: HOP table size.
  443. * @hop0_tables_total_size: total size for all HOP0 tables.
  444. * @host_resident: Should the MMU page table reside in host memory or in the
  445. * device DRAM.
  446. */
  447. struct hl_mmu_properties {
  448. u64 start_addr;
  449. u64 end_addr;
  450. u64 hop_shifts[MMU_HOP_MAX];
  451. u64 hop_masks[MMU_HOP_MAX];
  452. u64 last_mask;
  453. u64 pgt_size;
  454. u64 supported_pages_mask;
  455. u32 page_size;
  456. u32 num_hops;
  457. u32 hop_table_size;
  458. u32 hop0_tables_total_size;
  459. u8 host_resident;
  460. };
  461. /**
  462. * struct hl_hints_range - hint addresses reserved va range.
  463. * @start_addr: start address of the va range.
  464. * @end_addr: end address of the va range.
  465. */
  466. struct hl_hints_range {
  467. u64 start_addr;
  468. u64 end_addr;
  469. };
  470. /**
  471. * struct asic_fixed_properties - ASIC specific immutable properties.
  472. * @hw_queues_props: H/W queues properties.
  473. * @cpucp_info: received various information from CPU-CP regarding the H/W, e.g.
  474. * available sensors.
  475. * @uboot_ver: F/W U-boot version.
  476. * @preboot_ver: F/W Preboot version.
  477. * @dmmu: DRAM MMU address translation properties.
  478. * @pmmu: PCI (host) MMU address translation properties.
  479. * @pmmu_huge: PCI (host) MMU address translation properties for memory
  480. * allocated with huge pages.
  481. * @hints_dram_reserved_va_range: dram hint addresses reserved range.
  482. * @hints_host_reserved_va_range: host hint addresses reserved range.
  483. * @hints_host_hpage_reserved_va_range: host huge page hint addresses reserved
  484. * range.
  485. * @sram_base_address: SRAM physical start address.
  486. * @sram_end_address: SRAM physical end address.
  487. * @sram_user_base_address - SRAM physical start address for user access.
  488. * @dram_base_address: DRAM physical start address.
  489. * @dram_end_address: DRAM physical end address.
  490. * @dram_user_base_address: DRAM physical start address for user access.
  491. * @dram_size: DRAM total size.
  492. * @dram_pci_bar_size: size of PCI bar towards DRAM.
  493. * @max_power_default: max power of the device after reset.
  494. * @dc_power_default: power consumed by the device in mode idle.
  495. * @dram_size_for_default_page_mapping: DRAM size needed to map to avoid page
  496. * fault.
  497. * @pcie_dbi_base_address: Base address of the PCIE_DBI block.
  498. * @pcie_aux_dbi_reg_addr: Address of the PCIE_AUX DBI register.
  499. * @mmu_pgt_addr: base physical address in DRAM of MMU page tables.
  500. * @mmu_dram_default_page_addr: DRAM default page physical address.
  501. * @tpc_enabled_mask: which TPCs are enabled.
  502. * @tpc_binning_mask: which TPCs are binned. 0 means usable and 1 means binned.
  503. * @dram_enabled_mask: which DRAMs are enabled.
  504. * @dram_binning_mask: which DRAMs are binned. 0 means usable, 1 means binned.
  505. * @dram_hints_align_mask: dram va hint addresses alignment mask which is used
  506. * for hints validity check.
  507. * @cfg_base_address: config space base address.
  508. * @mmu_cache_mng_addr: address of the MMU cache.
  509. * @mmu_cache_mng_size: size of the MMU cache.
  510. * @device_dma_offset_for_host_access: the offset to add to host DMA addresses
  511. * to enable the device to access them.
  512. * @host_base_address: host physical start address for host DMA from device
  513. * @host_end_address: host physical end address for host DMA from device
  514. * @max_freq_value: current max clk frequency.
  515. * @clk_pll_index: clock PLL index that specify which PLL determines the clock
  516. * we display to the user
  517. * @mmu_pgt_size: MMU page tables total size.
  518. * @mmu_pte_size: PTE size in MMU page tables.
  519. * @mmu_hop_table_size: MMU hop table size.
  520. * @mmu_hop0_tables_total_size: total size of MMU hop0 tables.
  521. * @dram_page_size: page size for MMU DRAM allocation.
  522. * @cfg_size: configuration space size on SRAM.
  523. * @sram_size: total size of SRAM.
  524. * @max_asid: maximum number of open contexts (ASIDs).
  525. * @num_of_events: number of possible internal H/W IRQs.
  526. * @psoc_pci_pll_nr: PCI PLL NR value.
  527. * @psoc_pci_pll_nf: PCI PLL NF value.
  528. * @psoc_pci_pll_od: PCI PLL OD value.
  529. * @psoc_pci_pll_div_factor: PCI PLL DIV FACTOR 1 value.
  530. * @psoc_timestamp_frequency: frequency of the psoc timestamp clock.
  531. * @high_pll: high PLL frequency used by the device.
  532. * @cb_pool_cb_cnt: number of CBs in the CB pool.
  533. * @cb_pool_cb_size: size of each CB in the CB pool.
  534. * @decoder_enabled_mask: which decoders are enabled.
  535. * @decoder_binning_mask: which decoders are binned, 0 means usable and 1
  536. * means binned (at most one binned decoder per dcore).
  537. * @edma_enabled_mask: which EDMAs are enabled.
  538. * @edma_binning_mask: which EDMAs are binned, 0 means usable and 1 means
  539. * binned (at most one binned DMA).
  540. * @max_pending_cs: maximum of concurrent pending command submissions
  541. * @max_queues: maximum amount of queues in the system
  542. * @fw_preboot_cpu_boot_dev_sts0: bitmap representation of preboot cpu
  543. * capabilities reported by FW, bit description
  544. * can be found in CPU_BOOT_DEV_STS0
  545. * @fw_preboot_cpu_boot_dev_sts1: bitmap representation of preboot cpu
  546. * capabilities reported by FW, bit description
  547. * can be found in CPU_BOOT_DEV_STS1
  548. * @fw_bootfit_cpu_boot_dev_sts0: bitmap representation of boot cpu security
  549. * status reported by FW, bit description can be
  550. * found in CPU_BOOT_DEV_STS0
  551. * @fw_bootfit_cpu_boot_dev_sts1: bitmap representation of boot cpu security
  552. * status reported by FW, bit description can be
  553. * found in CPU_BOOT_DEV_STS1
  554. * @fw_app_cpu_boot_dev_sts0: bitmap representation of application security
  555. * status reported by FW, bit description can be
  556. * found in CPU_BOOT_DEV_STS0
  557. * @fw_app_cpu_boot_dev_sts1: bitmap representation of application security
  558. * status reported by FW, bit description can be
  559. * found in CPU_BOOT_DEV_STS1
  560. * @max_dec: maximum number of decoders
  561. * @hmmu_hif_enabled_mask: mask of HMMUs/HIFs that are not isolated (enabled)
  562. * 1- enabled, 0- isolated.
  563. * @faulty_dram_cluster_map: mask of faulty DRAM cluster.
  564. * 1- faulty cluster, 0- good cluster.
  565. * @xbar_edge_enabled_mask: mask of XBAR_EDGEs that are not isolated (enabled)
  566. * 1- enabled, 0- isolated.
  567. * @device_mem_alloc_default_page_size: may be different than dram_page_size only for ASICs for
  568. * which the property supports_user_set_page_size is true
  569. * (i.e. the DRAM supports multiple page sizes), otherwise
  570. * it will shall be equal to dram_page_size.
  571. * @num_engine_cores: number of engine cpu cores
  572. * @collective_first_sob: first sync object available for collective use
  573. * @collective_first_mon: first monitor available for collective use
  574. * @sync_stream_first_sob: first sync object available for sync stream use
  575. * @sync_stream_first_mon: first monitor available for sync stream use
  576. * @first_available_user_sob: first sob available for the user
  577. * @first_available_user_mon: first monitor available for the user
  578. * @first_available_user_interrupt: first available interrupt reserved for the user
  579. * @first_available_cq: first available CQ for the user.
  580. * @user_interrupt_count: number of user interrupts.
  581. * @user_dec_intr_count: number of decoder interrupts exposed to user.
  582. * @cache_line_size: device cache line size.
  583. * @server_type: Server type that the ASIC is currently installed in.
  584. * The value is according to enum hl_server_type in uapi file.
  585. * @completion_queues_count: number of completion queues.
  586. * @completion_mode: 0 - job based completion, 1 - cs based completion
  587. * @mme_master_slave_mode: 0 - Each MME works independently, 1 - MME works
  588. * in Master/Slave mode
  589. * @fw_security_enabled: true if security measures are enabled in firmware,
  590. * false otherwise
  591. * @fw_cpu_boot_dev_sts0_valid: status bits are valid and can be fetched from
  592. * BOOT_DEV_STS0
  593. * @fw_cpu_boot_dev_sts1_valid: status bits are valid and can be fetched from
  594. * BOOT_DEV_STS1
  595. * @dram_supports_virtual_memory: is there an MMU towards the DRAM
  596. * @hard_reset_done_by_fw: true if firmware is handling hard reset flow
  597. * @num_functional_hbms: number of functional HBMs in each DCORE.
  598. * @hints_range_reservation: device support hint addresses range reservation.
  599. * @iatu_done_by_fw: true if iATU configuration is being done by FW.
  600. * @dynamic_fw_load: is dynamic FW load is supported.
  601. * @gic_interrupts_enable: true if FW is not blocking GIC controller,
  602. * false otherwise.
  603. * @use_get_power_for_reset_history: To support backward compatibility for Goya
  604. * and Gaudi
  605. * @supports_compute_reset: is a reset which is not a hard-reset supported by this asic.
  606. * @allow_inference_soft_reset: true if the ASIC supports soft reset that is
  607. * initiated by user or TDR. This is only true
  608. * in inference ASICs, as there is no real-world
  609. * use-case of doing soft-reset in training (due
  610. * to the fact that training runs on multiple
  611. * devices)
  612. * @configurable_stop_on_err: is stop-on-error option configurable via debugfs.
  613. * @set_max_power_on_device_init: true if need to set max power in F/W on device init.
  614. * @supports_user_set_page_size: true if user can set the allocation page size.
  615. * @dma_mask: the dma mask to be set for this device
  616. * @supports_advanced_cpucp_rc: true if new cpucp opcodes are supported.
  617. */
  618. struct asic_fixed_properties {
  619. struct hw_queue_properties *hw_queues_props;
  620. struct cpucp_info cpucp_info;
  621. char uboot_ver[VERSION_MAX_LEN];
  622. char preboot_ver[VERSION_MAX_LEN];
  623. struct hl_mmu_properties dmmu;
  624. struct hl_mmu_properties pmmu;
  625. struct hl_mmu_properties pmmu_huge;
  626. struct hl_hints_range hints_dram_reserved_va_range;
  627. struct hl_hints_range hints_host_reserved_va_range;
  628. struct hl_hints_range hints_host_hpage_reserved_va_range;
  629. u64 sram_base_address;
  630. u64 sram_end_address;
  631. u64 sram_user_base_address;
  632. u64 dram_base_address;
  633. u64 dram_end_address;
  634. u64 dram_user_base_address;
  635. u64 dram_size;
  636. u64 dram_pci_bar_size;
  637. u64 max_power_default;
  638. u64 dc_power_default;
  639. u64 dram_size_for_default_page_mapping;
  640. u64 pcie_dbi_base_address;
  641. u64 pcie_aux_dbi_reg_addr;
  642. u64 mmu_pgt_addr;
  643. u64 mmu_dram_default_page_addr;
  644. u64 tpc_enabled_mask;
  645. u64 tpc_binning_mask;
  646. u64 dram_enabled_mask;
  647. u64 dram_binning_mask;
  648. u64 dram_hints_align_mask;
  649. u64 cfg_base_address;
  650. u64 mmu_cache_mng_addr;
  651. u64 mmu_cache_mng_size;
  652. u64 device_dma_offset_for_host_access;
  653. u64 host_base_address;
  654. u64 host_end_address;
  655. u64 max_freq_value;
  656. u32 clk_pll_index;
  657. u32 mmu_pgt_size;
  658. u32 mmu_pte_size;
  659. u32 mmu_hop_table_size;
  660. u32 mmu_hop0_tables_total_size;
  661. u32 dram_page_size;
  662. u32 cfg_size;
  663. u32 sram_size;
  664. u32 max_asid;
  665. u32 num_of_events;
  666. u32 psoc_pci_pll_nr;
  667. u32 psoc_pci_pll_nf;
  668. u32 psoc_pci_pll_od;
  669. u32 psoc_pci_pll_div_factor;
  670. u32 psoc_timestamp_frequency;
  671. u32 high_pll;
  672. u32 cb_pool_cb_cnt;
  673. u32 cb_pool_cb_size;
  674. u32 decoder_enabled_mask;
  675. u32 decoder_binning_mask;
  676. u32 edma_enabled_mask;
  677. u32 edma_binning_mask;
  678. u32 max_pending_cs;
  679. u32 max_queues;
  680. u32 fw_preboot_cpu_boot_dev_sts0;
  681. u32 fw_preboot_cpu_boot_dev_sts1;
  682. u32 fw_bootfit_cpu_boot_dev_sts0;
  683. u32 fw_bootfit_cpu_boot_dev_sts1;
  684. u32 fw_app_cpu_boot_dev_sts0;
  685. u32 fw_app_cpu_boot_dev_sts1;
  686. u32 max_dec;
  687. u32 hmmu_hif_enabled_mask;
  688. u32 faulty_dram_cluster_map;
  689. u32 xbar_edge_enabled_mask;
  690. u32 device_mem_alloc_default_page_size;
  691. u32 num_engine_cores;
  692. u16 collective_first_sob;
  693. u16 collective_first_mon;
  694. u16 sync_stream_first_sob;
  695. u16 sync_stream_first_mon;
  696. u16 first_available_user_sob[HL_MAX_DCORES];
  697. u16 first_available_user_mon[HL_MAX_DCORES];
  698. u16 first_available_user_interrupt;
  699. u16 first_available_cq[HL_MAX_DCORES];
  700. u16 user_interrupt_count;
  701. u16 user_dec_intr_count;
  702. u16 cache_line_size;
  703. u16 server_type;
  704. u8 completion_queues_count;
  705. u8 completion_mode;
  706. u8 mme_master_slave_mode;
  707. u8 fw_security_enabled;
  708. u8 fw_cpu_boot_dev_sts0_valid;
  709. u8 fw_cpu_boot_dev_sts1_valid;
  710. u8 dram_supports_virtual_memory;
  711. u8 hard_reset_done_by_fw;
  712. u8 num_functional_hbms;
  713. u8 hints_range_reservation;
  714. u8 iatu_done_by_fw;
  715. u8 dynamic_fw_load;
  716. u8 gic_interrupts_enable;
  717. u8 use_get_power_for_reset_history;
  718. u8 supports_compute_reset;
  719. u8 allow_inference_soft_reset;
  720. u8 configurable_stop_on_err;
  721. u8 set_max_power_on_device_init;
  722. u8 supports_user_set_page_size;
  723. u8 dma_mask;
  724. u8 supports_advanced_cpucp_rc;
  725. };
  726. /**
  727. * struct hl_fence - software synchronization primitive
  728. * @completion: fence is implemented using completion
  729. * @refcount: refcount for this fence
  730. * @cs_sequence: sequence of the corresponding command submission
  731. * @stream_master_qid_map: streams masters QID bitmap to represent all streams
  732. * masters QIDs that multi cs is waiting on
  733. * @error: mark this fence with error
  734. * @timestamp: timestamp upon completion
  735. * @mcs_handling_done: indicates that corresponding command submission has
  736. * finished msc handling, this does not mean it was part
  737. * of the mcs
  738. */
  739. struct hl_fence {
  740. struct completion completion;
  741. struct kref refcount;
  742. u64 cs_sequence;
  743. u32 stream_master_qid_map;
  744. int error;
  745. ktime_t timestamp;
  746. u8 mcs_handling_done;
  747. };
  748. /**
  749. * struct hl_cs_compl - command submission completion object.
  750. * @base_fence: hl fence object.
  751. * @lock: spinlock to protect fence.
  752. * @hdev: habanalabs device structure.
  753. * @hw_sob: the H/W SOB used in this signal/wait CS.
  754. * @encaps_sig_hdl: encaps signals handler.
  755. * @cs_seq: command submission sequence number.
  756. * @type: type of the CS - signal/wait.
  757. * @sob_val: the SOB value that is used in this signal/wait CS.
  758. * @sob_group: the SOB group that is used in this collective wait CS.
  759. * @encaps_signals: indication whether it's a completion object of cs with
  760. * encaps signals or not.
  761. */
  762. struct hl_cs_compl {
  763. struct hl_fence base_fence;
  764. spinlock_t lock;
  765. struct hl_device *hdev;
  766. struct hl_hw_sob *hw_sob;
  767. struct hl_cs_encaps_sig_handle *encaps_sig_hdl;
  768. u64 cs_seq;
  769. enum hl_cs_type type;
  770. u16 sob_val;
  771. u16 sob_group;
  772. bool encaps_signals;
  773. };
  774. /*
  775. * Command Buffers
  776. */
  777. /**
  778. * struct hl_ts_buff - describes a timestamp buffer.
  779. * @kernel_buff_address: Holds the internal buffer's kernel virtual address.
  780. * @user_buff_address: Holds the user buffer's kernel virtual address.
  781. * @kernel_buff_size: Holds the internal kernel buffer size.
  782. */
  783. struct hl_ts_buff {
  784. void *kernel_buff_address;
  785. void *user_buff_address;
  786. u32 kernel_buff_size;
  787. };
  788. struct hl_mmap_mem_buf;
  789. /**
  790. * struct hl_mem_mgr - describes unified memory manager for mappable memory chunks.
  791. * @dev: back pointer to the owning device
  792. * @lock: protects handles
  793. * @handles: an idr holding all active handles to the memory buffers in the system.
  794. */
  795. struct hl_mem_mgr {
  796. struct device *dev;
  797. spinlock_t lock;
  798. struct idr handles;
  799. };
  800. /**
  801. * struct hl_mmap_mem_buf_behavior - describes unified memory manager buffer behavior
  802. * @topic: string identifier used for logging
  803. * @mem_id: memory type identifier, embedded in the handle and used to identify
  804. * the memory type by handle.
  805. * @alloc: callback executed on buffer allocation, shall allocate the memory,
  806. * set it under buffer private, and set mappable size.
  807. * @mmap: callback executed on mmap, must map the buffer to vma
  808. * @release: callback executed on release, must free the resources used by the buffer
  809. */
  810. struct hl_mmap_mem_buf_behavior {
  811. const char *topic;
  812. u64 mem_id;
  813. int (*alloc)(struct hl_mmap_mem_buf *buf, gfp_t gfp, void *args);
  814. int (*mmap)(struct hl_mmap_mem_buf *buf, struct vm_area_struct *vma, void *args);
  815. void (*release)(struct hl_mmap_mem_buf *buf);
  816. };
  817. /**
  818. * struct hl_mmap_mem_buf - describes a single unified memory buffer
  819. * @behavior: buffer behavior
  820. * @mmg: back pointer to the unified memory manager
  821. * @refcount: reference counter for buffer users
  822. * @private: pointer to buffer behavior private data
  823. * @mmap: atomic boolean indicating whether or not the buffer is mapped right now
  824. * @real_mapped_size: the actual size of buffer mapped, after part of it may be released,
  825. * may change at runtime.
  826. * @mappable_size: the original mappable size of the buffer, does not change after
  827. * the allocation.
  828. * @handle: the buffer id in mmg handles store
  829. */
  830. struct hl_mmap_mem_buf {
  831. struct hl_mmap_mem_buf_behavior *behavior;
  832. struct hl_mem_mgr *mmg;
  833. struct kref refcount;
  834. void *private;
  835. atomic_t mmap;
  836. u64 real_mapped_size;
  837. u64 mappable_size;
  838. u64 handle;
  839. };
  840. /**
  841. * struct hl_cb - describes a Command Buffer.
  842. * @hdev: pointer to device this CB belongs to.
  843. * @ctx: pointer to the CB owner's context.
  844. * @buf: back pointer to the parent mappable memory buffer
  845. * @debugfs_list: node in debugfs list of command buffers.
  846. * @pool_list: node in pool list of command buffers.
  847. * @kernel_address: Holds the CB's kernel virtual address.
  848. * @virtual_addr: Holds the CB's virtual address.
  849. * @bus_address: Holds the CB's DMA address.
  850. * @size: holds the CB's size.
  851. * @roundup_size: holds the cb size after roundup to page size.
  852. * @cs_cnt: holds number of CS that this CB participates in.
  853. * @is_pool: true if CB was acquired from the pool, false otherwise.
  854. * @is_internal: internally allocated
  855. * @is_mmu_mapped: true if the CB is mapped to the device's MMU.
  856. */
  857. struct hl_cb {
  858. struct hl_device *hdev;
  859. struct hl_ctx *ctx;
  860. struct hl_mmap_mem_buf *buf;
  861. struct list_head debugfs_list;
  862. struct list_head pool_list;
  863. void *kernel_address;
  864. u64 virtual_addr;
  865. dma_addr_t bus_address;
  866. u32 size;
  867. u32 roundup_size;
  868. atomic_t cs_cnt;
  869. u8 is_pool;
  870. u8 is_internal;
  871. u8 is_mmu_mapped;
  872. };
  873. /*
  874. * QUEUES
  875. */
  876. struct hl_cs_job;
  877. /* Queue length of external and HW queues */
  878. #define HL_QUEUE_LENGTH 4096
  879. #define HL_QUEUE_SIZE_IN_BYTES (HL_QUEUE_LENGTH * HL_BD_SIZE)
  880. #if (HL_MAX_JOBS_PER_CS > HL_QUEUE_LENGTH)
  881. #error "HL_QUEUE_LENGTH must be greater than HL_MAX_JOBS_PER_CS"
  882. #endif
  883. /* HL_CQ_LENGTH is in units of struct hl_cq_entry */
  884. #define HL_CQ_LENGTH HL_QUEUE_LENGTH
  885. #define HL_CQ_SIZE_IN_BYTES (HL_CQ_LENGTH * HL_CQ_ENTRY_SIZE)
  886. /* Must be power of 2 */
  887. #define HL_EQ_LENGTH 64
  888. #define HL_EQ_SIZE_IN_BYTES (HL_EQ_LENGTH * HL_EQ_ENTRY_SIZE)
  889. /* Host <-> CPU-CP shared memory size */
  890. #define HL_CPU_ACCESSIBLE_MEM_SIZE SZ_2M
  891. /**
  892. * struct hl_sync_stream_properties -
  893. * describes a H/W queue sync stream properties
  894. * @hw_sob: array of the used H/W SOBs by this H/W queue.
  895. * @next_sob_val: the next value to use for the currently used SOB.
  896. * @base_sob_id: the base SOB id of the SOBs used by this queue.
  897. * @base_mon_id: the base MON id of the MONs used by this queue.
  898. * @collective_mstr_mon_id: the MON ids of the MONs used by this master queue
  899. * in order to sync with all slave queues.
  900. * @collective_slave_mon_id: the MON id used by this slave queue in order to
  901. * sync with its master queue.
  902. * @collective_sob_id: current SOB id used by this collective slave queue
  903. * to signal its collective master queue upon completion.
  904. * @curr_sob_offset: the id offset to the currently used SOB from the
  905. * HL_RSVD_SOBS that are being used by this queue.
  906. */
  907. struct hl_sync_stream_properties {
  908. struct hl_hw_sob hw_sob[HL_RSVD_SOBS];
  909. u16 next_sob_val;
  910. u16 base_sob_id;
  911. u16 base_mon_id;
  912. u16 collective_mstr_mon_id[HL_COLLECTIVE_RSVD_MSTR_MONS];
  913. u16 collective_slave_mon_id;
  914. u16 collective_sob_id;
  915. u8 curr_sob_offset;
  916. };
  917. /**
  918. * struct hl_encaps_signals_mgr - describes sync stream encapsulated signals
  919. * handlers manager
  920. * @lock: protects handles.
  921. * @handles: an idr to hold all encapsulated signals handles.
  922. */
  923. struct hl_encaps_signals_mgr {
  924. spinlock_t lock;
  925. struct idr handles;
  926. };
  927. /**
  928. * struct hl_hw_queue - describes a H/W transport queue.
  929. * @shadow_queue: pointer to a shadow queue that holds pointers to jobs.
  930. * @sync_stream_prop: sync stream queue properties
  931. * @queue_type: type of queue.
  932. * @collective_mode: collective mode of current queue
  933. * @kernel_address: holds the queue's kernel virtual address.
  934. * @bus_address: holds the queue's DMA address.
  935. * @pi: holds the queue's pi value.
  936. * @ci: holds the queue's ci value, AS CALCULATED BY THE DRIVER (not real ci).
  937. * @hw_queue_id: the id of the H/W queue.
  938. * @cq_id: the id for the corresponding CQ for this H/W queue.
  939. * @msi_vec: the IRQ number of the H/W queue.
  940. * @int_queue_len: length of internal queue (number of entries).
  941. * @valid: is the queue valid (we have array of 32 queues, not all of them
  942. * exist).
  943. * @supports_sync_stream: True if queue supports sync stream
  944. */
  945. struct hl_hw_queue {
  946. struct hl_cs_job **shadow_queue;
  947. struct hl_sync_stream_properties sync_stream_prop;
  948. enum hl_queue_type queue_type;
  949. enum hl_collective_mode collective_mode;
  950. void *kernel_address;
  951. dma_addr_t bus_address;
  952. u32 pi;
  953. atomic_t ci;
  954. u32 hw_queue_id;
  955. u32 cq_id;
  956. u32 msi_vec;
  957. u16 int_queue_len;
  958. u8 valid;
  959. u8 supports_sync_stream;
  960. };
  961. /**
  962. * struct hl_cq - describes a completion queue
  963. * @hdev: pointer to the device structure
  964. * @kernel_address: holds the queue's kernel virtual address
  965. * @bus_address: holds the queue's DMA address
  966. * @cq_idx: completion queue index in array
  967. * @hw_queue_id: the id of the matching H/W queue
  968. * @ci: ci inside the queue
  969. * @pi: pi inside the queue
  970. * @free_slots_cnt: counter of free slots in queue
  971. */
  972. struct hl_cq {
  973. struct hl_device *hdev;
  974. void *kernel_address;
  975. dma_addr_t bus_address;
  976. u32 cq_idx;
  977. u32 hw_queue_id;
  978. u32 ci;
  979. u32 pi;
  980. atomic_t free_slots_cnt;
  981. };
  982. /**
  983. * struct hl_user_interrupt - holds user interrupt information
  984. * @hdev: pointer to the device structure
  985. * @wait_list_head: head to the list of user threads pending on this interrupt
  986. * @wait_list_lock: protects wait_list_head
  987. * @interrupt_id: msix interrupt id
  988. * @is_decoder: whether this entry represents a decoder interrupt
  989. */
  990. struct hl_user_interrupt {
  991. struct hl_device *hdev;
  992. struct list_head wait_list_head;
  993. spinlock_t wait_list_lock;
  994. u32 interrupt_id;
  995. bool is_decoder;
  996. };
  997. /**
  998. * struct timestamp_reg_free_node - holds the timestamp registration free objects node
  999. * @free_objects_node: node in the list free_obj_jobs
  1000. * @cq_cb: pointer to cq command buffer to be freed
  1001. * @buf: pointer to timestamp buffer to be freed
  1002. */
  1003. struct timestamp_reg_free_node {
  1004. struct list_head free_objects_node;
  1005. struct hl_cb *cq_cb;
  1006. struct hl_mmap_mem_buf *buf;
  1007. };
  1008. /* struct timestamp_reg_work_obj - holds the timestamp registration free objects job
  1009. * the job will be to pass over the free_obj_jobs list and put refcount to objects
  1010. * in each node of the list
  1011. * @free_obj: workqueue object to free timestamp registration node objects
  1012. * @hdev: pointer to the device structure
  1013. * @free_obj_head: list of free jobs nodes (node type timestamp_reg_free_node)
  1014. */
  1015. struct timestamp_reg_work_obj {
  1016. struct work_struct free_obj;
  1017. struct hl_device *hdev;
  1018. struct list_head *free_obj_head;
  1019. };
  1020. /* struct timestamp_reg_info - holds the timestamp registration related data.
  1021. * @buf: pointer to the timestamp buffer which include both user/kernel buffers.
  1022. * relevant only when doing timestamps records registration.
  1023. * @cq_cb: pointer to CQ counter CB.
  1024. * @timestamp_kernel_addr: timestamp handle address, where to set timestamp
  1025. * relevant only when doing timestamps records
  1026. * registration.
  1027. * @in_use: indicates if the node already in use. relevant only when doing
  1028. * timestamps records registration, since in this case the driver
  1029. * will have it's own buffer which serve as a records pool instead of
  1030. * allocating records dynamically.
  1031. */
  1032. struct timestamp_reg_info {
  1033. struct hl_mmap_mem_buf *buf;
  1034. struct hl_cb *cq_cb;
  1035. u64 *timestamp_kernel_addr;
  1036. u8 in_use;
  1037. };
  1038. /**
  1039. * struct hl_user_pending_interrupt - holds a context to a user thread
  1040. * pending on an interrupt
  1041. * @ts_reg_info: holds the timestamps registration nodes info
  1042. * @wait_list_node: node in the list of user threads pending on an interrupt
  1043. * @fence: hl fence object for interrupt completion
  1044. * @cq_target_value: CQ target value
  1045. * @cq_kernel_addr: CQ kernel address, to be used in the cq interrupt
  1046. * handler for target value comparison
  1047. */
  1048. struct hl_user_pending_interrupt {
  1049. struct timestamp_reg_info ts_reg_info;
  1050. struct list_head wait_list_node;
  1051. struct hl_fence fence;
  1052. u64 cq_target_value;
  1053. u64 *cq_kernel_addr;
  1054. };
  1055. /**
  1056. * struct hl_eq - describes the event queue (single one per device)
  1057. * @hdev: pointer to the device structure
  1058. * @kernel_address: holds the queue's kernel virtual address
  1059. * @bus_address: holds the queue's DMA address
  1060. * @ci: ci inside the queue
  1061. * @prev_eqe_index: the index of the previous event queue entry. The index of
  1062. * the current entry's index must be +1 of the previous one.
  1063. * @check_eqe_index: do we need to check the index of the current entry vs. the
  1064. * previous one. This is for backward compatibility with older
  1065. * firmwares
  1066. */
  1067. struct hl_eq {
  1068. struct hl_device *hdev;
  1069. void *kernel_address;
  1070. dma_addr_t bus_address;
  1071. u32 ci;
  1072. u32 prev_eqe_index;
  1073. bool check_eqe_index;
  1074. };
  1075. /**
  1076. * struct hl_dec - describes a decoder sw instance.
  1077. * @hdev: pointer to the device structure.
  1078. * @completion_abnrm_work: workqueue object to run when decoder generates an error interrupt
  1079. * @core_id: ID of the decoder.
  1080. * @base_addr: base address of the decoder.
  1081. */
  1082. struct hl_dec {
  1083. struct hl_device *hdev;
  1084. struct work_struct completion_abnrm_work;
  1085. u32 core_id;
  1086. u32 base_addr;
  1087. };
  1088. /**
  1089. * enum hl_asic_type - supported ASIC types.
  1090. * @ASIC_INVALID: Invalid ASIC type.
  1091. * @ASIC_GOYA: Goya device (HL-1000).
  1092. * @ASIC_GAUDI: Gaudi device (HL-2000).
  1093. * @ASIC_GAUDI_SEC: Gaudi secured device (HL-2000).
  1094. * @ASIC_GAUDI2: Gaudi2 device.
  1095. * @ASIC_GAUDI2_SEC: Gaudi2 secured device.
  1096. */
  1097. enum hl_asic_type {
  1098. ASIC_INVALID,
  1099. ASIC_GOYA,
  1100. ASIC_GAUDI,
  1101. ASIC_GAUDI_SEC,
  1102. ASIC_GAUDI2,
  1103. ASIC_GAUDI2_SEC,
  1104. };
  1105. struct hl_cs_parser;
  1106. /**
  1107. * enum hl_pm_mng_profile - power management profile.
  1108. * @PM_AUTO: internal clock is set by the Linux driver.
  1109. * @PM_MANUAL: internal clock is set by the user.
  1110. * @PM_LAST: last power management type.
  1111. */
  1112. enum hl_pm_mng_profile {
  1113. PM_AUTO = 1,
  1114. PM_MANUAL,
  1115. PM_LAST
  1116. };
  1117. /**
  1118. * enum hl_pll_frequency - PLL frequency.
  1119. * @PLL_HIGH: high frequency.
  1120. * @PLL_LOW: low frequency.
  1121. * @PLL_LAST: last frequency values that were configured by the user.
  1122. */
  1123. enum hl_pll_frequency {
  1124. PLL_HIGH = 1,
  1125. PLL_LOW,
  1126. PLL_LAST
  1127. };
  1128. #define PLL_REF_CLK 50
  1129. enum div_select_defs {
  1130. DIV_SEL_REF_CLK = 0,
  1131. DIV_SEL_PLL_CLK = 1,
  1132. DIV_SEL_DIVIDED_REF = 2,
  1133. DIV_SEL_DIVIDED_PLL = 3,
  1134. };
  1135. enum debugfs_access_type {
  1136. DEBUGFS_READ8,
  1137. DEBUGFS_WRITE8,
  1138. DEBUGFS_READ32,
  1139. DEBUGFS_WRITE32,
  1140. DEBUGFS_READ64,
  1141. DEBUGFS_WRITE64,
  1142. };
  1143. enum pci_region {
  1144. PCI_REGION_CFG,
  1145. PCI_REGION_SRAM,
  1146. PCI_REGION_DRAM,
  1147. PCI_REGION_SP_SRAM,
  1148. PCI_REGION_NUMBER,
  1149. };
  1150. /**
  1151. * struct pci_mem_region - describe memory region in a PCI bar
  1152. * @region_base: region base address
  1153. * @region_size: region size
  1154. * @bar_size: size of the BAR
  1155. * @offset_in_bar: region offset into the bar
  1156. * @bar_id: bar ID of the region
  1157. * @used: if used 1, otherwise 0
  1158. */
  1159. struct pci_mem_region {
  1160. u64 region_base;
  1161. u64 region_size;
  1162. u64 bar_size;
  1163. u64 offset_in_bar;
  1164. u8 bar_id;
  1165. u8 used;
  1166. };
  1167. /**
  1168. * struct static_fw_load_mgr - static FW load manager
  1169. * @preboot_version_max_off: max offset to preboot version
  1170. * @boot_fit_version_max_off: max offset to boot fit version
  1171. * @kmd_msg_to_cpu_reg: register address for KDM->CPU messages
  1172. * @cpu_cmd_status_to_host_reg: register address for CPU command status response
  1173. * @cpu_boot_status_reg: boot status register
  1174. * @cpu_boot_dev_status0_reg: boot device status register 0
  1175. * @cpu_boot_dev_status1_reg: boot device status register 1
  1176. * @boot_err0_reg: boot error register 0
  1177. * @boot_err1_reg: boot error register 1
  1178. * @preboot_version_offset_reg: SRAM offset to preboot version register
  1179. * @boot_fit_version_offset_reg: SRAM offset to boot fit version register
  1180. * @sram_offset_mask: mask for getting offset into the SRAM
  1181. * @cpu_reset_wait_msec: used when setting WFE via kmd_msg_to_cpu_reg
  1182. */
  1183. struct static_fw_load_mgr {
  1184. u64 preboot_version_max_off;
  1185. u64 boot_fit_version_max_off;
  1186. u32 kmd_msg_to_cpu_reg;
  1187. u32 cpu_cmd_status_to_host_reg;
  1188. u32 cpu_boot_status_reg;
  1189. u32 cpu_boot_dev_status0_reg;
  1190. u32 cpu_boot_dev_status1_reg;
  1191. u32 boot_err0_reg;
  1192. u32 boot_err1_reg;
  1193. u32 preboot_version_offset_reg;
  1194. u32 boot_fit_version_offset_reg;
  1195. u32 sram_offset_mask;
  1196. u32 cpu_reset_wait_msec;
  1197. };
  1198. /**
  1199. * struct fw_response - FW response to LKD command
  1200. * @ram_offset: descriptor offset into the RAM
  1201. * @ram_type: RAM type containing the descriptor (SRAM/DRAM)
  1202. * @status: command status
  1203. */
  1204. struct fw_response {
  1205. u32 ram_offset;
  1206. u8 ram_type;
  1207. u8 status;
  1208. };
  1209. /**
  1210. * struct dynamic_fw_load_mgr - dynamic FW load manager
  1211. * @response: FW to LKD response
  1212. * @comm_desc: the communication descriptor with FW
  1213. * @image_region: region to copy the FW image to
  1214. * @fw_image_size: size of FW image to load
  1215. * @wait_for_bl_timeout: timeout for waiting for boot loader to respond
  1216. * @fw_desc_valid: true if FW descriptor has been validated and hence the data can be used
  1217. */
  1218. struct dynamic_fw_load_mgr {
  1219. struct fw_response response;
  1220. struct lkd_fw_comms_desc comm_desc;
  1221. struct pci_mem_region *image_region;
  1222. size_t fw_image_size;
  1223. u32 wait_for_bl_timeout;
  1224. bool fw_desc_valid;
  1225. };
  1226. /**
  1227. * struct pre_fw_load_props - needed properties for pre-FW load
  1228. * @cpu_boot_status_reg: cpu_boot_status register address
  1229. * @sts_boot_dev_sts0_reg: sts_boot_dev_sts0 register address
  1230. * @sts_boot_dev_sts1_reg: sts_boot_dev_sts1 register address
  1231. * @boot_err0_reg: boot_err0 register address
  1232. * @boot_err1_reg: boot_err1 register address
  1233. * @wait_for_preboot_timeout: timeout to poll for preboot ready
  1234. */
  1235. struct pre_fw_load_props {
  1236. u32 cpu_boot_status_reg;
  1237. u32 sts_boot_dev_sts0_reg;
  1238. u32 sts_boot_dev_sts1_reg;
  1239. u32 boot_err0_reg;
  1240. u32 boot_err1_reg;
  1241. u32 wait_for_preboot_timeout;
  1242. };
  1243. /**
  1244. * struct fw_image_props - properties of FW image
  1245. * @image_name: name of the image
  1246. * @src_off: offset in src FW to copy from
  1247. * @copy_size: amount of bytes to copy (0 to copy the whole binary)
  1248. */
  1249. struct fw_image_props {
  1250. char *image_name;
  1251. u32 src_off;
  1252. u32 copy_size;
  1253. };
  1254. /**
  1255. * struct fw_load_mgr - manager FW loading process
  1256. * @dynamic_loader: specific structure for dynamic load
  1257. * @static_loader: specific structure for static load
  1258. * @pre_fw_load_props: parameter for pre FW load
  1259. * @boot_fit_img: boot fit image properties
  1260. * @linux_img: linux image properties
  1261. * @cpu_timeout: CPU response timeout in usec
  1262. * @boot_fit_timeout: Boot fit load timeout in usec
  1263. * @skip_bmc: should BMC be skipped
  1264. * @sram_bar_id: SRAM bar ID
  1265. * @dram_bar_id: DRAM bar ID
  1266. * @fw_comp_loaded: bitmask of loaded FW components. set bit meaning loaded
  1267. * component. values are set according to enum hl_fw_types.
  1268. */
  1269. struct fw_load_mgr {
  1270. union {
  1271. struct dynamic_fw_load_mgr dynamic_loader;
  1272. struct static_fw_load_mgr static_loader;
  1273. };
  1274. struct pre_fw_load_props pre_fw_load;
  1275. struct fw_image_props boot_fit_img;
  1276. struct fw_image_props linux_img;
  1277. u32 cpu_timeout;
  1278. u32 boot_fit_timeout;
  1279. u8 skip_bmc;
  1280. u8 sram_bar_id;
  1281. u8 dram_bar_id;
  1282. u8 fw_comp_loaded;
  1283. };
  1284. struct hl_cs;
  1285. /**
  1286. * struct engines_data - asic engines data
  1287. * @buf: buffer for engines data in ascii
  1288. * @actual_size: actual size of data that was written by the driver to the allocated buffer
  1289. * @allocated_buf_size: total size of allocated buffer
  1290. */
  1291. struct engines_data {
  1292. char *buf;
  1293. int actual_size;
  1294. u32 allocated_buf_size;
  1295. };
  1296. /**
  1297. * struct hl_asic_funcs - ASIC specific functions that are can be called from
  1298. * common code.
  1299. * @early_init: sets up early driver state (pre sw_init), doesn't configure H/W.
  1300. * @early_fini: tears down what was done in early_init.
  1301. * @late_init: sets up late driver/hw state (post hw_init) - Optional.
  1302. * @late_fini: tears down what was done in late_init (pre hw_fini) - Optional.
  1303. * @sw_init: sets up driver state, does not configure H/W.
  1304. * @sw_fini: tears down driver state, does not configure H/W.
  1305. * @hw_init: sets up the H/W state.
  1306. * @hw_fini: tears down the H/W state.
  1307. * @halt_engines: halt engines, needed for reset sequence. This also disables
  1308. * interrupts from the device. Should be called before
  1309. * hw_fini and before CS rollback.
  1310. * @suspend: handles IP specific H/W or SW changes for suspend.
  1311. * @resume: handles IP specific H/W or SW changes for resume.
  1312. * @mmap: maps a memory.
  1313. * @ring_doorbell: increment PI on a given QMAN.
  1314. * @pqe_write: Write the PQ entry to the PQ. This is ASIC-specific
  1315. * function because the PQs are located in different memory areas
  1316. * per ASIC (SRAM, DRAM, Host memory) and therefore, the method of
  1317. * writing the PQE must match the destination memory area
  1318. * properties.
  1319. * @asic_dma_alloc_coherent: Allocate coherent DMA memory by calling
  1320. * dma_alloc_coherent(). This is ASIC function because
  1321. * its implementation is not trivial when the driver
  1322. * is loaded in simulation mode (not upstreamed).
  1323. * @asic_dma_free_coherent: Free coherent DMA memory by calling
  1324. * dma_free_coherent(). This is ASIC function because
  1325. * its implementation is not trivial when the driver
  1326. * is loaded in simulation mode (not upstreamed).
  1327. * @scrub_device_mem: Scrub the entire SRAM and DRAM.
  1328. * @scrub_device_dram: Scrub the dram memory of the device.
  1329. * @get_int_queue_base: get the internal queue base address.
  1330. * @test_queues: run simple test on all queues for sanity check.
  1331. * @asic_dma_pool_zalloc: small DMA allocation of coherent memory from DMA pool.
  1332. * size of allocation is HL_DMA_POOL_BLK_SIZE.
  1333. * @asic_dma_pool_free: free small DMA allocation from pool.
  1334. * @cpu_accessible_dma_pool_alloc: allocate CPU PQ packet from DMA pool.
  1335. * @cpu_accessible_dma_pool_free: free CPU PQ packet from DMA pool.
  1336. * @asic_dma_unmap_single: unmap a single DMA buffer
  1337. * @asic_dma_map_single: map a single buffer to a DMA
  1338. * @hl_dma_unmap_sgtable: DMA unmap scatter-gather table.
  1339. * @cs_parser: parse Command Submission.
  1340. * @asic_dma_map_sgtable: DMA map scatter-gather table.
  1341. * @add_end_of_cb_packets: Add packets to the end of CB, if device requires it.
  1342. * @update_eq_ci: update event queue CI.
  1343. * @context_switch: called upon ASID context switch.
  1344. * @restore_phase_topology: clear all SOBs amd MONs.
  1345. * @debugfs_read_dma: debug interface for reading up to 2MB from the device's
  1346. * internal memory via DMA engine.
  1347. * @add_device_attr: add ASIC specific device attributes.
  1348. * @handle_eqe: handle event queue entry (IRQ) from CPU-CP.
  1349. * @get_events_stat: retrieve event queue entries histogram.
  1350. * @read_pte: read MMU page table entry from DRAM.
  1351. * @write_pte: write MMU page table entry to DRAM.
  1352. * @mmu_invalidate_cache: flush MMU STLB host/DRAM cache, either with soft
  1353. * (L1 only) or hard (L0 & L1) flush.
  1354. * @mmu_invalidate_cache_range: flush specific MMU STLB cache lines with ASID-VA-size mask.
  1355. * @mmu_prefetch_cache_range: pre-fetch specific MMU STLB cache lines with ASID-VA-size mask.
  1356. * @send_heartbeat: send is-alive packet to CPU-CP and verify response.
  1357. * @debug_coresight: perform certain actions on Coresight for debugging.
  1358. * @is_device_idle: return true if device is idle, false otherwise.
  1359. * @compute_reset_late_init: perform certain actions needed after a compute reset
  1360. * @hw_queues_lock: acquire H/W queues lock.
  1361. * @hw_queues_unlock: release H/W queues lock.
  1362. * @get_pci_id: retrieve PCI ID.
  1363. * @get_eeprom_data: retrieve EEPROM data from F/W.
  1364. * @get_monitor_dump: retrieve monitor registers dump from F/W.
  1365. * @send_cpu_message: send message to F/W. If the message is timedout, the
  1366. * driver will eventually reset the device. The timeout can
  1367. * be determined by the calling function or it can be 0 and
  1368. * then the timeout is the default timeout for the specific
  1369. * ASIC
  1370. * @get_hw_state: retrieve the H/W state
  1371. * @pci_bars_map: Map PCI BARs.
  1372. * @init_iatu: Initialize the iATU unit inside the PCI controller.
  1373. * @rreg: Read a register. Needed for simulator support.
  1374. * @wreg: Write a register. Needed for simulator support.
  1375. * @halt_coresight: stop the ETF and ETR traces.
  1376. * @ctx_init: context dependent initialization.
  1377. * @ctx_fini: context dependent cleanup.
  1378. * @pre_schedule_cs: Perform pre-CS-scheduling operations.
  1379. * @get_queue_id_for_cq: Get the H/W queue id related to the given CQ index.
  1380. * @load_firmware_to_device: load the firmware to the device's memory
  1381. * @load_boot_fit_to_device: load boot fit to device's memory
  1382. * @get_signal_cb_size: Get signal CB size.
  1383. * @get_wait_cb_size: Get wait CB size.
  1384. * @gen_signal_cb: Generate a signal CB.
  1385. * @gen_wait_cb: Generate a wait CB.
  1386. * @reset_sob: Reset a SOB.
  1387. * @reset_sob_group: Reset SOB group
  1388. * @get_device_time: Get the device time.
  1389. * @pb_print_security_errors: print security errors according block and cause
  1390. * @collective_wait_init_cs: Generate collective master/slave packets
  1391. * and place them in the relevant cs jobs
  1392. * @collective_wait_create_jobs: allocate collective wait cs jobs
  1393. * @get_dec_base_addr: get the base address of a given decoder.
  1394. * @scramble_addr: Routine to scramble the address prior of mapping it
  1395. * in the MMU.
  1396. * @descramble_addr: Routine to de-scramble the address prior of
  1397. * showing it to users.
  1398. * @ack_protection_bits_errors: ack and dump all security violations
  1399. * @get_hw_block_id: retrieve a HW block id to be used by the user to mmap it.
  1400. * also returns the size of the block if caller supplies
  1401. * a valid pointer for it
  1402. * @hw_block_mmap: mmap a HW block with a given id.
  1403. * @enable_events_from_fw: send interrupt to firmware to notify them the
  1404. * driver is ready to receive asynchronous events. This
  1405. * function should be called during the first init and
  1406. * after every hard-reset of the device
  1407. * @ack_mmu_errors: check and ack mmu errors, page fault, access violation.
  1408. * @get_msi_info: Retrieve asic-specific MSI ID of the f/w async event
  1409. * @map_pll_idx_to_fw_idx: convert driver specific per asic PLL index to
  1410. * generic f/w compatible PLL Indexes
  1411. * @init_firmware_preload_params: initialize pre FW-load parameters.
  1412. * @init_firmware_loader: initialize data for FW loader.
  1413. * @init_cpu_scrambler_dram: Enable CPU specific DRAM scrambling
  1414. * @state_dump_init: initialize constants required for state dump
  1415. * @get_sob_addr: get SOB base address offset.
  1416. * @set_pci_memory_regions: setting properties of PCI memory regions
  1417. * @get_stream_master_qid_arr: get pointer to stream masters QID array
  1418. * @check_if_razwi_happened: check if there was a razwi due to RR violation.
  1419. * @access_dev_mem: access device memory
  1420. * @set_dram_bar_base: set the base of the DRAM BAR
  1421. * @set_engine_cores: set a config command to enigne cores
  1422. * @send_device_activity: indication to FW about device availability
  1423. */
  1424. struct hl_asic_funcs {
  1425. int (*early_init)(struct hl_device *hdev);
  1426. int (*early_fini)(struct hl_device *hdev);
  1427. int (*late_init)(struct hl_device *hdev);
  1428. void (*late_fini)(struct hl_device *hdev);
  1429. int (*sw_init)(struct hl_device *hdev);
  1430. int (*sw_fini)(struct hl_device *hdev);
  1431. int (*hw_init)(struct hl_device *hdev);
  1432. void (*hw_fini)(struct hl_device *hdev, bool hard_reset, bool fw_reset);
  1433. void (*halt_engines)(struct hl_device *hdev, bool hard_reset, bool fw_reset);
  1434. int (*suspend)(struct hl_device *hdev);
  1435. int (*resume)(struct hl_device *hdev);
  1436. int (*mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
  1437. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  1438. void (*ring_doorbell)(struct hl_device *hdev, u32 hw_queue_id, u32 pi);
  1439. void (*pqe_write)(struct hl_device *hdev, __le64 *pqe,
  1440. struct hl_bd *bd);
  1441. void* (*asic_dma_alloc_coherent)(struct hl_device *hdev, size_t size,
  1442. dma_addr_t *dma_handle, gfp_t flag);
  1443. void (*asic_dma_free_coherent)(struct hl_device *hdev, size_t size,
  1444. void *cpu_addr, dma_addr_t dma_handle);
  1445. int (*scrub_device_mem)(struct hl_device *hdev);
  1446. int (*scrub_device_dram)(struct hl_device *hdev, u64 val);
  1447. void* (*get_int_queue_base)(struct hl_device *hdev, u32 queue_id,
  1448. dma_addr_t *dma_handle, u16 *queue_len);
  1449. int (*test_queues)(struct hl_device *hdev);
  1450. void* (*asic_dma_pool_zalloc)(struct hl_device *hdev, size_t size,
  1451. gfp_t mem_flags, dma_addr_t *dma_handle);
  1452. void (*asic_dma_pool_free)(struct hl_device *hdev, void *vaddr,
  1453. dma_addr_t dma_addr);
  1454. void* (*cpu_accessible_dma_pool_alloc)(struct hl_device *hdev,
  1455. size_t size, dma_addr_t *dma_handle);
  1456. void (*cpu_accessible_dma_pool_free)(struct hl_device *hdev,
  1457. size_t size, void *vaddr);
  1458. void (*asic_dma_unmap_single)(struct hl_device *hdev,
  1459. dma_addr_t dma_addr, int len,
  1460. enum dma_data_direction dir);
  1461. dma_addr_t (*asic_dma_map_single)(struct hl_device *hdev,
  1462. void *addr, int len,
  1463. enum dma_data_direction dir);
  1464. void (*hl_dma_unmap_sgtable)(struct hl_device *hdev,
  1465. struct sg_table *sgt,
  1466. enum dma_data_direction dir);
  1467. int (*cs_parser)(struct hl_device *hdev, struct hl_cs_parser *parser);
  1468. int (*asic_dma_map_sgtable)(struct hl_device *hdev, struct sg_table *sgt,
  1469. enum dma_data_direction dir);
  1470. void (*add_end_of_cb_packets)(struct hl_device *hdev,
  1471. void *kernel_address, u32 len,
  1472. u32 original_len,
  1473. u64 cq_addr, u32 cq_val, u32 msix_num,
  1474. bool eb);
  1475. void (*update_eq_ci)(struct hl_device *hdev, u32 val);
  1476. int (*context_switch)(struct hl_device *hdev, u32 asid);
  1477. void (*restore_phase_topology)(struct hl_device *hdev);
  1478. int (*debugfs_read_dma)(struct hl_device *hdev, u64 addr, u32 size,
  1479. void *blob_addr);
  1480. void (*add_device_attr)(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp,
  1481. struct attribute_group *dev_vrm_attr_grp);
  1482. void (*handle_eqe)(struct hl_device *hdev,
  1483. struct hl_eq_entry *eq_entry);
  1484. void* (*get_events_stat)(struct hl_device *hdev, bool aggregate,
  1485. u32 *size);
  1486. u64 (*read_pte)(struct hl_device *hdev, u64 addr);
  1487. void (*write_pte)(struct hl_device *hdev, u64 addr, u64 val);
  1488. int (*mmu_invalidate_cache)(struct hl_device *hdev, bool is_hard,
  1489. u32 flags);
  1490. int (*mmu_invalidate_cache_range)(struct hl_device *hdev, bool is_hard,
  1491. u32 flags, u32 asid, u64 va, u64 size);
  1492. int (*mmu_prefetch_cache_range)(struct hl_ctx *ctx, u32 flags, u32 asid, u64 va, u64 size);
  1493. int (*send_heartbeat)(struct hl_device *hdev);
  1494. int (*debug_coresight)(struct hl_device *hdev, struct hl_ctx *ctx, void *data);
  1495. bool (*is_device_idle)(struct hl_device *hdev, u64 *mask_arr, u8 mask_len,
  1496. struct engines_data *e);
  1497. int (*compute_reset_late_init)(struct hl_device *hdev);
  1498. void (*hw_queues_lock)(struct hl_device *hdev);
  1499. void (*hw_queues_unlock)(struct hl_device *hdev);
  1500. u32 (*get_pci_id)(struct hl_device *hdev);
  1501. int (*get_eeprom_data)(struct hl_device *hdev, void *data, size_t max_size);
  1502. int (*get_monitor_dump)(struct hl_device *hdev, void *data);
  1503. int (*send_cpu_message)(struct hl_device *hdev, u32 *msg,
  1504. u16 len, u32 timeout, u64 *result);
  1505. int (*pci_bars_map)(struct hl_device *hdev);
  1506. int (*init_iatu)(struct hl_device *hdev);
  1507. u32 (*rreg)(struct hl_device *hdev, u32 reg);
  1508. void (*wreg)(struct hl_device *hdev, u32 reg, u32 val);
  1509. void (*halt_coresight)(struct hl_device *hdev, struct hl_ctx *ctx);
  1510. int (*ctx_init)(struct hl_ctx *ctx);
  1511. void (*ctx_fini)(struct hl_ctx *ctx);
  1512. int (*pre_schedule_cs)(struct hl_cs *cs);
  1513. u32 (*get_queue_id_for_cq)(struct hl_device *hdev, u32 cq_idx);
  1514. int (*load_firmware_to_device)(struct hl_device *hdev);
  1515. int (*load_boot_fit_to_device)(struct hl_device *hdev);
  1516. u32 (*get_signal_cb_size)(struct hl_device *hdev);
  1517. u32 (*get_wait_cb_size)(struct hl_device *hdev);
  1518. u32 (*gen_signal_cb)(struct hl_device *hdev, void *data, u16 sob_id,
  1519. u32 size, bool eb);
  1520. u32 (*gen_wait_cb)(struct hl_device *hdev,
  1521. struct hl_gen_wait_properties *prop);
  1522. void (*reset_sob)(struct hl_device *hdev, void *data);
  1523. void (*reset_sob_group)(struct hl_device *hdev, u16 sob_group);
  1524. u64 (*get_device_time)(struct hl_device *hdev);
  1525. void (*pb_print_security_errors)(struct hl_device *hdev,
  1526. u32 block_addr, u32 cause, u32 offended_addr);
  1527. int (*collective_wait_init_cs)(struct hl_cs *cs);
  1528. int (*collective_wait_create_jobs)(struct hl_device *hdev,
  1529. struct hl_ctx *ctx, struct hl_cs *cs,
  1530. u32 wait_queue_id, u32 collective_engine_id,
  1531. u32 encaps_signal_offset);
  1532. u32 (*get_dec_base_addr)(struct hl_device *hdev, u32 core_id);
  1533. u64 (*scramble_addr)(struct hl_device *hdev, u64 addr);
  1534. u64 (*descramble_addr)(struct hl_device *hdev, u64 addr);
  1535. void (*ack_protection_bits_errors)(struct hl_device *hdev);
  1536. int (*get_hw_block_id)(struct hl_device *hdev, u64 block_addr,
  1537. u32 *block_size, u32 *block_id);
  1538. int (*hw_block_mmap)(struct hl_device *hdev, struct vm_area_struct *vma,
  1539. u32 block_id, u32 block_size);
  1540. void (*enable_events_from_fw)(struct hl_device *hdev);
  1541. int (*ack_mmu_errors)(struct hl_device *hdev, u64 mmu_cap_mask);
  1542. void (*get_msi_info)(__le32 *table);
  1543. int (*map_pll_idx_to_fw_idx)(u32 pll_idx);
  1544. void (*init_firmware_preload_params)(struct hl_device *hdev);
  1545. void (*init_firmware_loader)(struct hl_device *hdev);
  1546. void (*init_cpu_scrambler_dram)(struct hl_device *hdev);
  1547. void (*state_dump_init)(struct hl_device *hdev);
  1548. u32 (*get_sob_addr)(struct hl_device *hdev, u32 sob_id);
  1549. void (*set_pci_memory_regions)(struct hl_device *hdev);
  1550. u32* (*get_stream_master_qid_arr)(void);
  1551. void (*check_if_razwi_happened)(struct hl_device *hdev);
  1552. int (*mmu_get_real_page_size)(struct hl_device *hdev, struct hl_mmu_properties *mmu_prop,
  1553. u32 page_size, u32 *real_page_size, bool is_dram_addr);
  1554. int (*access_dev_mem)(struct hl_device *hdev, enum pci_region region_type,
  1555. u64 addr, u64 *val, enum debugfs_access_type acc_type);
  1556. u64 (*set_dram_bar_base)(struct hl_device *hdev, u64 addr);
  1557. int (*set_engine_cores)(struct hl_device *hdev, u32 *core_ids,
  1558. u32 num_cores, u32 core_command);
  1559. int (*send_device_activity)(struct hl_device *hdev, bool open);
  1560. };
  1561. /*
  1562. * CONTEXTS
  1563. */
  1564. #define HL_KERNEL_ASID_ID 0
  1565. /**
  1566. * enum hl_va_range_type - virtual address range type.
  1567. * @HL_VA_RANGE_TYPE_HOST: range type of host pages
  1568. * @HL_VA_RANGE_TYPE_HOST_HUGE: range type of host huge pages
  1569. * @HL_VA_RANGE_TYPE_DRAM: range type of dram pages
  1570. */
  1571. enum hl_va_range_type {
  1572. HL_VA_RANGE_TYPE_HOST,
  1573. HL_VA_RANGE_TYPE_HOST_HUGE,
  1574. HL_VA_RANGE_TYPE_DRAM,
  1575. HL_VA_RANGE_TYPE_MAX
  1576. };
  1577. /**
  1578. * struct hl_va_range - virtual addresses range.
  1579. * @lock: protects the virtual addresses list.
  1580. * @list: list of virtual addresses blocks available for mappings.
  1581. * @start_addr: range start address.
  1582. * @end_addr: range end address.
  1583. * @page_size: page size of this va range.
  1584. */
  1585. struct hl_va_range {
  1586. struct mutex lock;
  1587. struct list_head list;
  1588. u64 start_addr;
  1589. u64 end_addr;
  1590. u32 page_size;
  1591. };
  1592. /**
  1593. * struct hl_cs_counters_atomic - command submission counters
  1594. * @out_of_mem_drop_cnt: dropped due to memory allocation issue
  1595. * @parsing_drop_cnt: dropped due to error in packet parsing
  1596. * @queue_full_drop_cnt: dropped due to queue full
  1597. * @device_in_reset_drop_cnt: dropped due to device in reset
  1598. * @max_cs_in_flight_drop_cnt: dropped due to maximum CS in-flight
  1599. * @validation_drop_cnt: dropped due to error in validation
  1600. */
  1601. struct hl_cs_counters_atomic {
  1602. atomic64_t out_of_mem_drop_cnt;
  1603. atomic64_t parsing_drop_cnt;
  1604. atomic64_t queue_full_drop_cnt;
  1605. atomic64_t device_in_reset_drop_cnt;
  1606. atomic64_t max_cs_in_flight_drop_cnt;
  1607. atomic64_t validation_drop_cnt;
  1608. };
  1609. /**
  1610. * struct hl_dmabuf_priv - a dma-buf private object.
  1611. * @dmabuf: pointer to dma-buf object.
  1612. * @ctx: pointer to the dma-buf owner's context.
  1613. * @phys_pg_pack: pointer to physical page pack if the dma-buf was exported for
  1614. * memory allocation handle.
  1615. * @device_address: physical address of the device's memory. Relevant only
  1616. * if phys_pg_pack is NULL (dma-buf was exported from address).
  1617. * The total size can be taken from the dmabuf object.
  1618. */
  1619. struct hl_dmabuf_priv {
  1620. struct dma_buf *dmabuf;
  1621. struct hl_ctx *ctx;
  1622. struct hl_vm_phys_pg_pack *phys_pg_pack;
  1623. uint64_t device_address;
  1624. };
  1625. #define HL_CS_OUTCOME_HISTORY_LEN 256
  1626. /**
  1627. * struct hl_cs_outcome - represents a single completed CS outcome
  1628. * @list_link: link to either container's used list or free list
  1629. * @map_link: list to the container hash map
  1630. * @ts: completion ts
  1631. * @seq: the original cs sequence
  1632. * @error: error code cs completed with, if any
  1633. */
  1634. struct hl_cs_outcome {
  1635. struct list_head list_link;
  1636. struct hlist_node map_link;
  1637. ktime_t ts;
  1638. u64 seq;
  1639. int error;
  1640. };
  1641. /**
  1642. * struct hl_cs_outcome_store - represents a limited store of completed CS outcomes
  1643. * @outcome_map: index of completed CS searchable by sequence number
  1644. * @used_list: list of outcome objects currently in use
  1645. * @free_list: list of outcome objects currently not in use
  1646. * @nodes_pool: a static pool of pre-allocated outcome objects
  1647. * @db_lock: any operation on the store must take this lock
  1648. */
  1649. struct hl_cs_outcome_store {
  1650. DECLARE_HASHTABLE(outcome_map, 8);
  1651. struct list_head used_list;
  1652. struct list_head free_list;
  1653. struct hl_cs_outcome nodes_pool[HL_CS_OUTCOME_HISTORY_LEN];
  1654. spinlock_t db_lock;
  1655. };
  1656. /**
  1657. * struct hl_ctx - user/kernel context.
  1658. * @mem_hash: holds mapping from virtual address to virtual memory area
  1659. * descriptor (hl_vm_phys_pg_list or hl_userptr).
  1660. * @mmu_shadow_hash: holds a mapping from shadow address to pgt_info structure.
  1661. * @hr_mmu_phys_hash: if host-resident MMU is used, holds a mapping from
  1662. * MMU-hop-page physical address to its host-resident
  1663. * pgt_info structure.
  1664. * @hpriv: pointer to the private (Kernel Driver) data of the process (fd).
  1665. * @hdev: pointer to the device structure.
  1666. * @refcount: reference counter for the context. Context is released only when
  1667. * this hits 0l. It is incremented on CS and CS_WAIT.
  1668. * @cs_pending: array of hl fence objects representing pending CS.
  1669. * @outcome_store: storage data structure used to remember outcomes of completed
  1670. * command submissions for a long time after CS id wraparound.
  1671. * @va_range: holds available virtual addresses for host and dram mappings.
  1672. * @mem_hash_lock: protects the mem_hash.
  1673. * @hw_block_list_lock: protects the HW block memory list.
  1674. * @debugfs_list: node in debugfs list of contexts.
  1675. * @hw_block_mem_list: list of HW block virtual mapped addresses.
  1676. * @cs_counters: context command submission counters.
  1677. * @cb_va_pool: device VA pool for command buffers which are mapped to the
  1678. * device's MMU.
  1679. * @sig_mgr: encaps signals handle manager.
  1680. * @cb_va_pool_base: the base address for the device VA pool
  1681. * @cs_sequence: sequence number for CS. Value is assigned to a CS and passed
  1682. * to user so user could inquire about CS. It is used as
  1683. * index to cs_pending array.
  1684. * @dram_default_hops: array that holds all hops addresses needed for default
  1685. * DRAM mapping.
  1686. * @cs_lock: spinlock to protect cs_sequence.
  1687. * @dram_phys_mem: amount of used physical DRAM memory by this context.
  1688. * @thread_ctx_switch_token: token to prevent multiple threads of the same
  1689. * context from running the context switch phase.
  1690. * Only a single thread should run it.
  1691. * @thread_ctx_switch_wait_token: token to prevent the threads that didn't run
  1692. * the context switch phase from moving to their
  1693. * execution phase before the context switch phase
  1694. * has finished.
  1695. * @asid: context's unique address space ID in the device's MMU.
  1696. * @handle: context's opaque handle for user
  1697. */
  1698. struct hl_ctx {
  1699. DECLARE_HASHTABLE(mem_hash, MEM_HASH_TABLE_BITS);
  1700. DECLARE_HASHTABLE(mmu_shadow_hash, MMU_HASH_TABLE_BITS);
  1701. DECLARE_HASHTABLE(hr_mmu_phys_hash, MMU_HASH_TABLE_BITS);
  1702. struct hl_fpriv *hpriv;
  1703. struct hl_device *hdev;
  1704. struct kref refcount;
  1705. struct hl_fence **cs_pending;
  1706. struct hl_cs_outcome_store outcome_store;
  1707. struct hl_va_range *va_range[HL_VA_RANGE_TYPE_MAX];
  1708. struct mutex mem_hash_lock;
  1709. struct mutex hw_block_list_lock;
  1710. struct list_head debugfs_list;
  1711. struct list_head hw_block_mem_list;
  1712. struct hl_cs_counters_atomic cs_counters;
  1713. struct gen_pool *cb_va_pool;
  1714. struct hl_encaps_signals_mgr sig_mgr;
  1715. u64 cb_va_pool_base;
  1716. u64 cs_sequence;
  1717. u64 *dram_default_hops;
  1718. spinlock_t cs_lock;
  1719. atomic64_t dram_phys_mem;
  1720. atomic_t thread_ctx_switch_token;
  1721. u32 thread_ctx_switch_wait_token;
  1722. u32 asid;
  1723. u32 handle;
  1724. };
  1725. /**
  1726. * struct hl_ctx_mgr - for handling multiple contexts.
  1727. * @lock: protects ctx_handles.
  1728. * @handles: idr to hold all ctx handles.
  1729. */
  1730. struct hl_ctx_mgr {
  1731. struct mutex lock;
  1732. struct idr handles;
  1733. };
  1734. /*
  1735. * COMMAND SUBMISSIONS
  1736. */
  1737. /**
  1738. * struct hl_userptr - memory mapping chunk information
  1739. * @vm_type: type of the VM.
  1740. * @job_node: linked-list node for hanging the object on the Job's list.
  1741. * @pages: pointer to struct page array
  1742. * @npages: size of @pages array
  1743. * @sgt: pointer to the scatter-gather table that holds the pages.
  1744. * @dir: for DMA unmapping, the direction must be supplied, so save it.
  1745. * @debugfs_list: node in debugfs list of command submissions.
  1746. * @pid: the pid of the user process owning the memory
  1747. * @addr: user-space virtual address of the start of the memory area.
  1748. * @size: size of the memory area to pin & map.
  1749. * @dma_mapped: true if the SG was mapped to DMA addresses, false otherwise.
  1750. */
  1751. struct hl_userptr {
  1752. enum vm_type vm_type; /* must be first */
  1753. struct list_head job_node;
  1754. struct page **pages;
  1755. unsigned int npages;
  1756. struct sg_table *sgt;
  1757. enum dma_data_direction dir;
  1758. struct list_head debugfs_list;
  1759. pid_t pid;
  1760. u64 addr;
  1761. u64 size;
  1762. u8 dma_mapped;
  1763. };
  1764. /**
  1765. * struct hl_cs - command submission.
  1766. * @jobs_in_queue_cnt: per each queue, maintain counter of submitted jobs.
  1767. * @ctx: the context this CS belongs to.
  1768. * @job_list: list of the CS's jobs in the various queues.
  1769. * @job_lock: spinlock for the CS's jobs list. Needed for free_job.
  1770. * @refcount: reference counter for usage of the CS.
  1771. * @fence: pointer to the fence object of this CS.
  1772. * @signal_fence: pointer to the fence object of the signal CS (used by wait
  1773. * CS only).
  1774. * @finish_work: workqueue object to run when CS is completed by H/W.
  1775. * @work_tdr: delayed work node for TDR.
  1776. * @mirror_node : node in device mirror list of command submissions.
  1777. * @staged_cs_node: node in the staged cs list.
  1778. * @debugfs_list: node in debugfs list of command submissions.
  1779. * @encaps_sig_hdl: holds the encaps signals handle.
  1780. * @sequence: the sequence number of this CS.
  1781. * @staged_sequence: the sequence of the staged submission this CS is part of,
  1782. * relevant only if staged_cs is set.
  1783. * @timeout_jiffies: cs timeout in jiffies.
  1784. * @submission_time_jiffies: submission time of the cs
  1785. * @type: CS_TYPE_*.
  1786. * @jobs_cnt: counter of submitted jobs on all queues.
  1787. * @encaps_sig_hdl_id: encaps signals handle id, set for the first staged cs.
  1788. * @sob_addr_offset: sob offset from the configuration base address.
  1789. * @initial_sob_count: count of completed signals in SOB before current submission of signal or
  1790. * cs with encaps signals.
  1791. * @submitted: true if CS was submitted to H/W.
  1792. * @completed: true if CS was completed by device.
  1793. * @timedout : true if CS was timedout.
  1794. * @tdr_active: true if TDR was activated for this CS (to prevent
  1795. * double TDR activation).
  1796. * @aborted: true if CS was aborted due to some device error.
  1797. * @timestamp: true if a timestamp must be captured upon completion.
  1798. * @staged_last: true if this is the last staged CS and needs completion.
  1799. * @staged_first: true if this is the first staged CS and we need to receive
  1800. * timeout for this CS.
  1801. * @staged_cs: true if this CS is part of a staged submission.
  1802. * @skip_reset_on_timeout: true if we shall not reset the device in case
  1803. * timeout occurs (debug scenario).
  1804. * @encaps_signals: true if this CS has encaps reserved signals.
  1805. */
  1806. struct hl_cs {
  1807. u16 *jobs_in_queue_cnt;
  1808. struct hl_ctx *ctx;
  1809. struct list_head job_list;
  1810. spinlock_t job_lock;
  1811. struct kref refcount;
  1812. struct hl_fence *fence;
  1813. struct hl_fence *signal_fence;
  1814. struct work_struct finish_work;
  1815. struct delayed_work work_tdr;
  1816. struct list_head mirror_node;
  1817. struct list_head staged_cs_node;
  1818. struct list_head debugfs_list;
  1819. struct hl_cs_encaps_sig_handle *encaps_sig_hdl;
  1820. u64 sequence;
  1821. u64 staged_sequence;
  1822. u64 timeout_jiffies;
  1823. u64 submission_time_jiffies;
  1824. enum hl_cs_type type;
  1825. u32 jobs_cnt;
  1826. u32 encaps_sig_hdl_id;
  1827. u32 sob_addr_offset;
  1828. u16 initial_sob_count;
  1829. u8 submitted;
  1830. u8 completed;
  1831. u8 timedout;
  1832. u8 tdr_active;
  1833. u8 aborted;
  1834. u8 timestamp;
  1835. u8 staged_last;
  1836. u8 staged_first;
  1837. u8 staged_cs;
  1838. u8 skip_reset_on_timeout;
  1839. u8 encaps_signals;
  1840. };
  1841. /**
  1842. * struct hl_cs_job - command submission job.
  1843. * @cs_node: the node to hang on the CS jobs list.
  1844. * @cs: the CS this job belongs to.
  1845. * @user_cb: the CB we got from the user.
  1846. * @patched_cb: in case of patching, this is internal CB which is submitted on
  1847. * the queue instead of the CB we got from the IOCTL.
  1848. * @finish_work: workqueue object to run when job is completed.
  1849. * @userptr_list: linked-list of userptr mappings that belong to this job and
  1850. * wait for completion.
  1851. * @debugfs_list: node in debugfs list of command submission jobs.
  1852. * @refcount: reference counter for usage of the CS job.
  1853. * @queue_type: the type of the H/W queue this job is submitted to.
  1854. * @id: the id of this job inside a CS.
  1855. * @hw_queue_id: the id of the H/W queue this job is submitted to.
  1856. * @user_cb_size: the actual size of the CB we got from the user.
  1857. * @job_cb_size: the actual size of the CB that we put on the queue.
  1858. * @encaps_sig_wait_offset: encapsulated signals offset, which allow user
  1859. * to wait on part of the reserved signals.
  1860. * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
  1861. * handle to a kernel-allocated CB object, false
  1862. * otherwise (SRAM/DRAM/host address).
  1863. * @contains_dma_pkt: whether the JOB contains at least one DMA packet. This
  1864. * info is needed later, when adding the 2xMSG_PROT at the
  1865. * end of the JOB, to know which barriers to put in the
  1866. * MSG_PROT packets. Relevant only for GAUDI as GOYA doesn't
  1867. * have streams so the engine can't be busy by another
  1868. * stream.
  1869. */
  1870. struct hl_cs_job {
  1871. struct list_head cs_node;
  1872. struct hl_cs *cs;
  1873. struct hl_cb *user_cb;
  1874. struct hl_cb *patched_cb;
  1875. struct work_struct finish_work;
  1876. struct list_head userptr_list;
  1877. struct list_head debugfs_list;
  1878. struct kref refcount;
  1879. enum hl_queue_type queue_type;
  1880. u32 id;
  1881. u32 hw_queue_id;
  1882. u32 user_cb_size;
  1883. u32 job_cb_size;
  1884. u32 encaps_sig_wait_offset;
  1885. u8 is_kernel_allocated_cb;
  1886. u8 contains_dma_pkt;
  1887. };
  1888. /**
  1889. * struct hl_cs_parser - command submission parser properties.
  1890. * @user_cb: the CB we got from the user.
  1891. * @patched_cb: in case of patching, this is internal CB which is submitted on
  1892. * the queue instead of the CB we got from the IOCTL.
  1893. * @job_userptr_list: linked-list of userptr mappings that belong to the related
  1894. * job and wait for completion.
  1895. * @cs_sequence: the sequence number of the related CS.
  1896. * @queue_type: the type of the H/W queue this job is submitted to.
  1897. * @ctx_id: the ID of the context the related CS belongs to.
  1898. * @hw_queue_id: the id of the H/W queue this job is submitted to.
  1899. * @user_cb_size: the actual size of the CB we got from the user.
  1900. * @patched_cb_size: the size of the CB after parsing.
  1901. * @job_id: the id of the related job inside the related CS.
  1902. * @is_kernel_allocated_cb: true if the CB handle we got from the user holds a
  1903. * handle to a kernel-allocated CB object, false
  1904. * otherwise (SRAM/DRAM/host address).
  1905. * @contains_dma_pkt: whether the JOB contains at least one DMA packet. This
  1906. * info is needed later, when adding the 2xMSG_PROT at the
  1907. * end of the JOB, to know which barriers to put in the
  1908. * MSG_PROT packets. Relevant only for GAUDI as GOYA doesn't
  1909. * have streams so the engine can't be busy by another
  1910. * stream.
  1911. * @completion: true if we need completion for this CS.
  1912. */
  1913. struct hl_cs_parser {
  1914. struct hl_cb *user_cb;
  1915. struct hl_cb *patched_cb;
  1916. struct list_head *job_userptr_list;
  1917. u64 cs_sequence;
  1918. enum hl_queue_type queue_type;
  1919. u32 ctx_id;
  1920. u32 hw_queue_id;
  1921. u32 user_cb_size;
  1922. u32 patched_cb_size;
  1923. u8 job_id;
  1924. u8 is_kernel_allocated_cb;
  1925. u8 contains_dma_pkt;
  1926. u8 completion;
  1927. };
  1928. /*
  1929. * MEMORY STRUCTURE
  1930. */
  1931. /**
  1932. * struct hl_vm_hash_node - hash element from virtual address to virtual
  1933. * memory area descriptor (hl_vm_phys_pg_list or
  1934. * hl_userptr).
  1935. * @node: node to hang on the hash table in context object.
  1936. * @vaddr: key virtual address.
  1937. * @ptr: value pointer (hl_vm_phys_pg_list or hl_userptr).
  1938. */
  1939. struct hl_vm_hash_node {
  1940. struct hlist_node node;
  1941. u64 vaddr;
  1942. void *ptr;
  1943. };
  1944. /**
  1945. * struct hl_vm_hw_block_list_node - list element from user virtual address to
  1946. * HW block id.
  1947. * @node: node to hang on the list in context object.
  1948. * @ctx: the context this node belongs to.
  1949. * @vaddr: virtual address of the HW block.
  1950. * @block_size: size of the block.
  1951. * @mapped_size: size of the block which is mapped. May change if partial un-mappings are done.
  1952. * @id: HW block id (handle).
  1953. */
  1954. struct hl_vm_hw_block_list_node {
  1955. struct list_head node;
  1956. struct hl_ctx *ctx;
  1957. unsigned long vaddr;
  1958. u32 block_size;
  1959. u32 mapped_size;
  1960. u32 id;
  1961. };
  1962. /**
  1963. * struct hl_vm_phys_pg_pack - physical page pack.
  1964. * @vm_type: describes the type of the virtual area descriptor.
  1965. * @pages: the physical page array.
  1966. * @npages: num physical pages in the pack.
  1967. * @total_size: total size of all the pages in this list.
  1968. * @node: used to attach to deletion list that is used when all the allocations are cleared
  1969. * at the teardown of the context.
  1970. * @mapping_cnt: number of shared mappings.
  1971. * @exporting_cnt: number of dma-buf exporting.
  1972. * @asid: the context related to this list.
  1973. * @page_size: size of each page in the pack.
  1974. * @flags: HL_MEM_* flags related to this list.
  1975. * @handle: the provided handle related to this list.
  1976. * @offset: offset from the first page.
  1977. * @contiguous: is contiguous physical memory.
  1978. * @created_from_userptr: is product of host virtual address.
  1979. */
  1980. struct hl_vm_phys_pg_pack {
  1981. enum vm_type vm_type; /* must be first */
  1982. u64 *pages;
  1983. u64 npages;
  1984. u64 total_size;
  1985. struct list_head node;
  1986. atomic_t mapping_cnt;
  1987. u32 exporting_cnt;
  1988. u32 asid;
  1989. u32 page_size;
  1990. u32 flags;
  1991. u32 handle;
  1992. u32 offset;
  1993. u8 contiguous;
  1994. u8 created_from_userptr;
  1995. };
  1996. /**
  1997. * struct hl_vm_va_block - virtual range block information.
  1998. * @node: node to hang on the virtual range list in context object.
  1999. * @start: virtual range start address.
  2000. * @end: virtual range end address.
  2001. * @size: virtual range size.
  2002. */
  2003. struct hl_vm_va_block {
  2004. struct list_head node;
  2005. u64 start;
  2006. u64 end;
  2007. u64 size;
  2008. };
  2009. /**
  2010. * struct hl_vm - virtual memory manager for MMU.
  2011. * @dram_pg_pool: pool for DRAM physical pages of 2MB.
  2012. * @dram_pg_pool_refcount: reference counter for the pool usage.
  2013. * @idr_lock: protects the phys_pg_list_handles.
  2014. * @phys_pg_pack_handles: idr to hold all device allocations handles.
  2015. * @init_done: whether initialization was done. We need this because VM
  2016. * initialization might be skipped during device initialization.
  2017. */
  2018. struct hl_vm {
  2019. struct gen_pool *dram_pg_pool;
  2020. struct kref dram_pg_pool_refcount;
  2021. spinlock_t idr_lock;
  2022. struct idr phys_pg_pack_handles;
  2023. u8 init_done;
  2024. };
  2025. /*
  2026. * DEBUG, PROFILING STRUCTURE
  2027. */
  2028. /**
  2029. * struct hl_debug_params - Coresight debug parameters.
  2030. * @input: pointer to component specific input parameters.
  2031. * @output: pointer to component specific output parameters.
  2032. * @output_size: size of output buffer.
  2033. * @reg_idx: relevant register ID.
  2034. * @op: component operation to execute.
  2035. * @enable: true if to enable component debugging, false otherwise.
  2036. */
  2037. struct hl_debug_params {
  2038. void *input;
  2039. void *output;
  2040. u32 output_size;
  2041. u32 reg_idx;
  2042. u32 op;
  2043. bool enable;
  2044. };
  2045. /**
  2046. * struct hl_notifier_event - holds the notifier data structure
  2047. * @eventfd: the event file descriptor to raise the notifications
  2048. * @lock: mutex lock to protect the notifier data flows
  2049. * @events_mask: indicates the bitmap events
  2050. */
  2051. struct hl_notifier_event {
  2052. struct eventfd_ctx *eventfd;
  2053. struct mutex lock;
  2054. u64 events_mask;
  2055. };
  2056. /*
  2057. * FILE PRIVATE STRUCTURE
  2058. */
  2059. /**
  2060. * struct hl_fpriv - process information stored in FD private data.
  2061. * @hdev: habanalabs device structure.
  2062. * @filp: pointer to the given file structure.
  2063. * @taskpid: current process ID.
  2064. * @ctx: current executing context. TODO: remove for multiple ctx per process
  2065. * @ctx_mgr: context manager to handle multiple context for this FD.
  2066. * @mem_mgr: manager descriptor for memory exportable via mmap
  2067. * @notifier_event: notifier eventfd towards user process
  2068. * @debugfs_list: list of relevant ASIC debugfs.
  2069. * @dev_node: node in the device list of file private data
  2070. * @refcount: number of related contexts.
  2071. * @restore_phase_mutex: lock for context switch and restore phase.
  2072. * @ctx_lock: protects the pointer to current executing context pointer. TODO: remove for multiple
  2073. * ctx per process.
  2074. */
  2075. struct hl_fpriv {
  2076. struct hl_device *hdev;
  2077. struct file *filp;
  2078. struct pid *taskpid;
  2079. struct hl_ctx *ctx;
  2080. struct hl_ctx_mgr ctx_mgr;
  2081. struct hl_mem_mgr mem_mgr;
  2082. struct hl_notifier_event notifier_event;
  2083. struct list_head debugfs_list;
  2084. struct list_head dev_node;
  2085. struct kref refcount;
  2086. struct mutex restore_phase_mutex;
  2087. struct mutex ctx_lock;
  2088. };
  2089. /*
  2090. * DebugFS
  2091. */
  2092. /**
  2093. * struct hl_info_list - debugfs file ops.
  2094. * @name: file name.
  2095. * @show: function to output information.
  2096. * @write: function to write to the file.
  2097. */
  2098. struct hl_info_list {
  2099. const char *name;
  2100. int (*show)(struct seq_file *s, void *data);
  2101. ssize_t (*write)(struct file *file, const char __user *buf,
  2102. size_t count, loff_t *f_pos);
  2103. };
  2104. /**
  2105. * struct hl_debugfs_entry - debugfs dentry wrapper.
  2106. * @info_ent: dentry related ops.
  2107. * @dev_entry: ASIC specific debugfs manager.
  2108. */
  2109. struct hl_debugfs_entry {
  2110. const struct hl_info_list *info_ent;
  2111. struct hl_dbg_device_entry *dev_entry;
  2112. };
  2113. /**
  2114. * struct hl_dbg_device_entry - ASIC specific debugfs manager.
  2115. * @root: root dentry.
  2116. * @hdev: habanalabs device structure.
  2117. * @entry_arr: array of available hl_debugfs_entry.
  2118. * @file_list: list of available debugfs files.
  2119. * @file_mutex: protects file_list.
  2120. * @cb_list: list of available CBs.
  2121. * @cb_spinlock: protects cb_list.
  2122. * @cs_list: list of available CSs.
  2123. * @cs_spinlock: protects cs_list.
  2124. * @cs_job_list: list of available CB jobs.
  2125. * @cs_job_spinlock: protects cs_job_list.
  2126. * @userptr_list: list of available userptrs (virtual memory chunk descriptor).
  2127. * @userptr_spinlock: protects userptr_list.
  2128. * @ctx_mem_hash_list: list of available contexts with MMU mappings.
  2129. * @ctx_mem_hash_spinlock: protects cb_list.
  2130. * @data_dma_blob_desc: data DMA descriptor of blob.
  2131. * @mon_dump_blob_desc: monitor dump descriptor of blob.
  2132. * @state_dump: data of the system states in case of a bad cs.
  2133. * @state_dump_sem: protects state_dump.
  2134. * @addr: next address to read/write from/to in read/write32.
  2135. * @mmu_addr: next virtual address to translate to physical address in mmu_show.
  2136. * @mmu_cap_mask: mmu hw capability mask, to be used in mmu_ack_error.
  2137. * @userptr_lookup: the target user ptr to look up for on demand.
  2138. * @mmu_asid: ASID to use while translating in mmu_show.
  2139. * @state_dump_head: index of the latest state dump
  2140. * @i2c_bus: generic u8 debugfs file for bus value to use in i2c_data_read.
  2141. * @i2c_addr: generic u8 debugfs file for address value to use in i2c_data_read.
  2142. * @i2c_reg: generic u8 debugfs file for register value to use in i2c_data_read.
  2143. * @i2c_len: generic u8 debugfs file for length value to use in i2c_data_read.
  2144. */
  2145. struct hl_dbg_device_entry {
  2146. struct dentry *root;
  2147. struct hl_device *hdev;
  2148. struct hl_debugfs_entry *entry_arr;
  2149. struct list_head file_list;
  2150. struct mutex file_mutex;
  2151. struct list_head cb_list;
  2152. spinlock_t cb_spinlock;
  2153. struct list_head cs_list;
  2154. spinlock_t cs_spinlock;
  2155. struct list_head cs_job_list;
  2156. spinlock_t cs_job_spinlock;
  2157. struct list_head userptr_list;
  2158. spinlock_t userptr_spinlock;
  2159. struct list_head ctx_mem_hash_list;
  2160. spinlock_t ctx_mem_hash_spinlock;
  2161. struct debugfs_blob_wrapper data_dma_blob_desc;
  2162. struct debugfs_blob_wrapper mon_dump_blob_desc;
  2163. char *state_dump[HL_STATE_DUMP_HIST_LEN];
  2164. struct rw_semaphore state_dump_sem;
  2165. u64 addr;
  2166. u64 mmu_addr;
  2167. u64 mmu_cap_mask;
  2168. u64 userptr_lookup;
  2169. u32 mmu_asid;
  2170. u32 state_dump_head;
  2171. u8 i2c_bus;
  2172. u8 i2c_addr;
  2173. u8 i2c_reg;
  2174. u8 i2c_len;
  2175. };
  2176. /**
  2177. * struct hl_hw_obj_name_entry - single hw object name, member of
  2178. * hl_state_dump_specs
  2179. * @node: link to the containing hash table
  2180. * @name: hw object name
  2181. * @id: object identifier
  2182. */
  2183. struct hl_hw_obj_name_entry {
  2184. struct hlist_node node;
  2185. const char *name;
  2186. u32 id;
  2187. };
  2188. enum hl_state_dump_specs_props {
  2189. SP_SYNC_OBJ_BASE_ADDR,
  2190. SP_NEXT_SYNC_OBJ_ADDR,
  2191. SP_SYNC_OBJ_AMOUNT,
  2192. SP_MON_OBJ_WR_ADDR_LOW,
  2193. SP_MON_OBJ_WR_ADDR_HIGH,
  2194. SP_MON_OBJ_WR_DATA,
  2195. SP_MON_OBJ_ARM_DATA,
  2196. SP_MON_OBJ_STATUS,
  2197. SP_MONITORS_AMOUNT,
  2198. SP_TPC0_CMDQ,
  2199. SP_TPC0_CFG_SO,
  2200. SP_NEXT_TPC,
  2201. SP_MME_CMDQ,
  2202. SP_MME_CFG_SO,
  2203. SP_NEXT_MME,
  2204. SP_DMA_CMDQ,
  2205. SP_DMA_CFG_SO,
  2206. SP_DMA_QUEUES_OFFSET,
  2207. SP_NUM_OF_MME_ENGINES,
  2208. SP_SUB_MME_ENG_NUM,
  2209. SP_NUM_OF_DMA_ENGINES,
  2210. SP_NUM_OF_TPC_ENGINES,
  2211. SP_ENGINE_NUM_OF_QUEUES,
  2212. SP_ENGINE_NUM_OF_STREAMS,
  2213. SP_ENGINE_NUM_OF_FENCES,
  2214. SP_FENCE0_CNT_OFFSET,
  2215. SP_FENCE0_RDATA_OFFSET,
  2216. SP_CP_STS_OFFSET,
  2217. SP_NUM_CORES,
  2218. SP_MAX
  2219. };
  2220. enum hl_sync_engine_type {
  2221. ENGINE_TPC,
  2222. ENGINE_DMA,
  2223. ENGINE_MME,
  2224. };
  2225. /**
  2226. * struct hl_mon_state_dump - represents a state dump of a single monitor
  2227. * @id: monitor id
  2228. * @wr_addr_low: address monitor will write to, low bits
  2229. * @wr_addr_high: address monitor will write to, high bits
  2230. * @wr_data: data monitor will write
  2231. * @arm_data: register value containing monitor configuration
  2232. * @status: monitor status
  2233. */
  2234. struct hl_mon_state_dump {
  2235. u32 id;
  2236. u32 wr_addr_low;
  2237. u32 wr_addr_high;
  2238. u32 wr_data;
  2239. u32 arm_data;
  2240. u32 status;
  2241. };
  2242. /**
  2243. * struct hl_sync_to_engine_map_entry - sync object id to engine mapping entry
  2244. * @engine_type: type of the engine
  2245. * @engine_id: id of the engine
  2246. * @sync_id: id of the sync object
  2247. */
  2248. struct hl_sync_to_engine_map_entry {
  2249. struct hlist_node node;
  2250. enum hl_sync_engine_type engine_type;
  2251. u32 engine_id;
  2252. u32 sync_id;
  2253. };
  2254. /**
  2255. * struct hl_sync_to_engine_map - maps sync object id to associated engine id
  2256. * @tb: hash table containing the mapping, each element is of type
  2257. * struct hl_sync_to_engine_map_entry
  2258. */
  2259. struct hl_sync_to_engine_map {
  2260. DECLARE_HASHTABLE(tb, SYNC_TO_ENGINE_HASH_TABLE_BITS);
  2261. };
  2262. /**
  2263. * struct hl_state_dump_specs_funcs - virtual functions used by the state dump
  2264. * @gen_sync_to_engine_map: generate a hash map from sync obj id to its engine
  2265. * @print_single_monitor: format monitor data as string
  2266. * @monitor_valid: return true if given monitor dump is valid
  2267. * @print_fences_single_engine: format fences data as string
  2268. */
  2269. struct hl_state_dump_specs_funcs {
  2270. int (*gen_sync_to_engine_map)(struct hl_device *hdev,
  2271. struct hl_sync_to_engine_map *map);
  2272. int (*print_single_monitor)(char **buf, size_t *size, size_t *offset,
  2273. struct hl_device *hdev,
  2274. struct hl_mon_state_dump *mon);
  2275. int (*monitor_valid)(struct hl_mon_state_dump *mon);
  2276. int (*print_fences_single_engine)(struct hl_device *hdev,
  2277. u64 base_offset,
  2278. u64 status_base_offset,
  2279. enum hl_sync_engine_type engine_type,
  2280. u32 engine_id, char **buf,
  2281. size_t *size, size_t *offset);
  2282. };
  2283. /**
  2284. * struct hl_state_dump_specs - defines ASIC known hw objects names
  2285. * @so_id_to_str_tb: sync objects names index table
  2286. * @monitor_id_to_str_tb: monitors names index table
  2287. * @funcs: virtual functions used for state dump
  2288. * @sync_namager_names: readable names for sync manager if available (ex: N_E)
  2289. * @props: pointer to a per asic const props array required for state dump
  2290. */
  2291. struct hl_state_dump_specs {
  2292. DECLARE_HASHTABLE(so_id_to_str_tb, OBJ_NAMES_HASH_TABLE_BITS);
  2293. DECLARE_HASHTABLE(monitor_id_to_str_tb, OBJ_NAMES_HASH_TABLE_BITS);
  2294. struct hl_state_dump_specs_funcs funcs;
  2295. const char * const *sync_namager_names;
  2296. s64 *props;
  2297. };
  2298. /*
  2299. * DEVICES
  2300. */
  2301. #define HL_STR_MAX 32
  2302. #define HL_DEV_STS_MAX (HL_DEVICE_STATUS_LAST + 1)
  2303. /* Theoretical limit only. A single host can only contain up to 4 or 8 PCIe
  2304. * x16 cards. In extreme cases, there are hosts that can accommodate 16 cards.
  2305. */
  2306. #define HL_MAX_MINORS 256
  2307. /*
  2308. * Registers read & write functions.
  2309. */
  2310. u32 hl_rreg(struct hl_device *hdev, u32 reg);
  2311. void hl_wreg(struct hl_device *hdev, u32 reg, u32 val);
  2312. #define RREG32(reg) hdev->asic_funcs->rreg(hdev, (reg))
  2313. #define WREG32(reg, v) hdev->asic_funcs->wreg(hdev, (reg), (v))
  2314. #define DREG32(reg) pr_info("REGISTER: " #reg " : 0x%08X\n", \
  2315. hdev->asic_funcs->rreg(hdev, (reg)))
  2316. #define WREG32_P(reg, val, mask) \
  2317. do { \
  2318. u32 tmp_ = RREG32(reg); \
  2319. tmp_ &= (mask); \
  2320. tmp_ |= ((val) & ~(mask)); \
  2321. WREG32(reg, tmp_); \
  2322. } while (0)
  2323. #define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
  2324. #define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))
  2325. #define RMWREG32(reg, val, mask) \
  2326. do { \
  2327. u32 tmp_ = RREG32(reg); \
  2328. tmp_ &= ~(mask); \
  2329. tmp_ |= ((val) << __ffs(mask)); \
  2330. WREG32(reg, tmp_); \
  2331. } while (0)
  2332. #define RREG32_MASK(reg, mask) ((RREG32(reg) & mask) >> __ffs(mask))
  2333. #define REG_FIELD_SHIFT(reg, field) reg##_##field##_SHIFT
  2334. #define REG_FIELD_MASK(reg, field) reg##_##field##_MASK
  2335. #define WREG32_FIELD(reg, offset, field, val) \
  2336. WREG32(mm##reg + offset, (RREG32(mm##reg + offset) & \
  2337. ~REG_FIELD_MASK(reg, field)) | \
  2338. (val) << REG_FIELD_SHIFT(reg, field))
  2339. /* Timeout should be longer when working with simulator but cap the
  2340. * increased timeout to some maximum
  2341. */
  2342. #define hl_poll_timeout_common(hdev, addr, val, cond, sleep_us, timeout_us, elbi) \
  2343. ({ \
  2344. ktime_t __timeout; \
  2345. u32 __elbi_read; \
  2346. int __rc = 0; \
  2347. if (hdev->pdev) \
  2348. __timeout = ktime_add_us(ktime_get(), timeout_us); \
  2349. else \
  2350. __timeout = ktime_add_us(ktime_get(),\
  2351. min((u64)(timeout_us * 10), \
  2352. (u64) HL_SIM_MAX_TIMEOUT_US)); \
  2353. might_sleep_if(sleep_us); \
  2354. for (;;) { \
  2355. if (elbi) { \
  2356. __rc = hl_pci_elbi_read(hdev, addr, &__elbi_read); \
  2357. if (__rc) \
  2358. break; \
  2359. (val) = __elbi_read; \
  2360. } else {\
  2361. (val) = RREG32((u32)(addr)); \
  2362. } \
  2363. if (cond) \
  2364. break; \
  2365. if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
  2366. if (elbi) { \
  2367. __rc = hl_pci_elbi_read(hdev, addr, &__elbi_read); \
  2368. if (__rc) \
  2369. break; \
  2370. (val) = __elbi_read; \
  2371. } else {\
  2372. (val) = RREG32((u32)(addr)); \
  2373. } \
  2374. break; \
  2375. } \
  2376. if (sleep_us) \
  2377. usleep_range((sleep_us >> 2) + 1, sleep_us); \
  2378. } \
  2379. __rc ? __rc : ((cond) ? 0 : -ETIMEDOUT); \
  2380. })
  2381. #define hl_poll_timeout(hdev, addr, val, cond, sleep_us, timeout_us) \
  2382. hl_poll_timeout_common(hdev, addr, val, cond, sleep_us, timeout_us, false)
  2383. #define hl_poll_timeout_elbi(hdev, addr, val, cond, sleep_us, timeout_us) \
  2384. hl_poll_timeout_common(hdev, addr, val, cond, sleep_us, timeout_us, true)
  2385. /*
  2386. * poll array of register addresses.
  2387. * condition is satisfied if all registers values match the expected value.
  2388. * once some register in the array satisfies the condition it will not be polled again,
  2389. * this is done both for efficiency and due to some registers are "clear on read".
  2390. * TODO: use read from PCI bar in other places in the code (SW-91406)
  2391. */
  2392. #define hl_poll_reg_array_timeout_common(hdev, addr_arr, arr_size, expected_val, sleep_us, \
  2393. timeout_us, elbi) \
  2394. ({ \
  2395. ktime_t __timeout; \
  2396. u64 __elem_bitmask; \
  2397. u32 __read_val; \
  2398. u8 __arr_idx; \
  2399. int __rc = 0; \
  2400. \
  2401. if (hdev->pdev) \
  2402. __timeout = ktime_add_us(ktime_get(), timeout_us); \
  2403. else \
  2404. __timeout = ktime_add_us(ktime_get(),\
  2405. min(((u64)timeout_us * 10), \
  2406. (u64) HL_SIM_MAX_TIMEOUT_US)); \
  2407. \
  2408. might_sleep_if(sleep_us); \
  2409. if (arr_size >= 64) \
  2410. __rc = -EINVAL; \
  2411. else \
  2412. __elem_bitmask = BIT_ULL(arr_size) - 1; \
  2413. for (;;) { \
  2414. if (__rc) \
  2415. break; \
  2416. for (__arr_idx = 0; __arr_idx < (arr_size); __arr_idx++) { \
  2417. if (!(__elem_bitmask & BIT_ULL(__arr_idx))) \
  2418. continue; \
  2419. if (elbi) { \
  2420. __rc = hl_pci_elbi_read(hdev, (addr_arr)[__arr_idx], &__read_val); \
  2421. if (__rc) \
  2422. break; \
  2423. } else { \
  2424. __read_val = RREG32((u32)(addr_arr)[__arr_idx]); \
  2425. } \
  2426. if (__read_val == (expected_val)) \
  2427. __elem_bitmask &= ~BIT_ULL(__arr_idx); \
  2428. } \
  2429. if (__rc || (__elem_bitmask == 0)) \
  2430. break; \
  2431. if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) \
  2432. break; \
  2433. if (sleep_us) \
  2434. usleep_range((sleep_us >> 2) + 1, sleep_us); \
  2435. } \
  2436. __rc ? __rc : ((__elem_bitmask == 0) ? 0 : -ETIMEDOUT); \
  2437. })
  2438. #define hl_poll_reg_array_timeout(hdev, addr_arr, arr_size, expected_val, sleep_us, \
  2439. timeout_us) \
  2440. hl_poll_reg_array_timeout_common(hdev, addr_arr, arr_size, expected_val, sleep_us, \
  2441. timeout_us, false)
  2442. #define hl_poll_reg_array_timeout_elbi(hdev, addr_arr, arr_size, expected_val, sleep_us, \
  2443. timeout_us) \
  2444. hl_poll_reg_array_timeout_common(hdev, addr_arr, arr_size, expected_val, sleep_us, \
  2445. timeout_us, true)
  2446. /*
  2447. * address in this macro points always to a memory location in the
  2448. * host's (server's) memory. That location is updated asynchronously
  2449. * either by the direct access of the device or by another core.
  2450. *
  2451. * To work both in LE and BE architectures, we need to distinguish between the
  2452. * two states (device or another core updates the memory location). Therefore,
  2453. * if mem_written_by_device is true, the host memory being polled will be
  2454. * updated directly by the device. If false, the host memory being polled will
  2455. * be updated by host CPU. Required so host knows whether or not the memory
  2456. * might need to be byte-swapped before returning value to caller.
  2457. */
  2458. #define hl_poll_timeout_memory(hdev, addr, val, cond, sleep_us, timeout_us, \
  2459. mem_written_by_device) \
  2460. ({ \
  2461. ktime_t __timeout; \
  2462. if (hdev->pdev) \
  2463. __timeout = ktime_add_us(ktime_get(), timeout_us); \
  2464. else \
  2465. __timeout = ktime_add_us(ktime_get(),\
  2466. min((u64)(timeout_us * 100), \
  2467. (u64) HL_SIM_MAX_TIMEOUT_US)); \
  2468. might_sleep_if(sleep_us); \
  2469. for (;;) { \
  2470. /* Verify we read updates done by other cores or by device */ \
  2471. mb(); \
  2472. (val) = *((u32 *)(addr)); \
  2473. if (mem_written_by_device) \
  2474. (val) = le32_to_cpu(*(__le32 *) &(val)); \
  2475. if (cond) \
  2476. break; \
  2477. if (timeout_us && ktime_compare(ktime_get(), __timeout) > 0) { \
  2478. (val) = *((u32 *)(addr)); \
  2479. if (mem_written_by_device) \
  2480. (val) = le32_to_cpu(*(__le32 *) &(val)); \
  2481. break; \
  2482. } \
  2483. if (sleep_us) \
  2484. usleep_range((sleep_us >> 2) + 1, sleep_us); \
  2485. } \
  2486. (cond) ? 0 : -ETIMEDOUT; \
  2487. })
  2488. #define HL_USR_MAPPED_BLK_INIT(blk, base, sz) \
  2489. ({ \
  2490. struct user_mapped_block *p = blk; \
  2491. \
  2492. p->address = base; \
  2493. p->size = sz; \
  2494. })
  2495. #define HL_USR_INTR_STRUCT_INIT(usr_intr, hdev, intr_id, decoder) \
  2496. ({ \
  2497. usr_intr.hdev = hdev; \
  2498. usr_intr.interrupt_id = intr_id; \
  2499. usr_intr.is_decoder = decoder; \
  2500. INIT_LIST_HEAD(&usr_intr.wait_list_head); \
  2501. spin_lock_init(&usr_intr.wait_list_lock); \
  2502. })
  2503. struct hwmon_chip_info;
  2504. /**
  2505. * struct hl_device_reset_work - reset workqueue task wrapper.
  2506. * @wq: work queue for device reset procedure.
  2507. * @reset_work: reset work to be done.
  2508. * @hdev: habanalabs device structure.
  2509. * @flags: reset flags.
  2510. */
  2511. struct hl_device_reset_work {
  2512. struct workqueue_struct *wq;
  2513. struct delayed_work reset_work;
  2514. struct hl_device *hdev;
  2515. u32 flags;
  2516. };
  2517. /**
  2518. * struct hl_mmu_hr_pgt_priv - used for holding per-device mmu host-resident
  2519. * page-table internal information.
  2520. * @mmu_pgt_pool: pool of page tables used by a host-resident MMU for
  2521. * allocating hops.
  2522. * @mmu_asid_hop0: per-ASID array of host-resident hop0 tables.
  2523. */
  2524. struct hl_mmu_hr_priv {
  2525. struct gen_pool *mmu_pgt_pool;
  2526. struct pgt_info *mmu_asid_hop0;
  2527. };
  2528. /**
  2529. * struct hl_mmu_dr_pgt_priv - used for holding per-device mmu device-resident
  2530. * page-table internal information.
  2531. * @mmu_pgt_pool: pool of page tables used by MMU for allocating hops.
  2532. * @mmu_shadow_hop0: shadow array of hop0 tables.
  2533. */
  2534. struct hl_mmu_dr_priv {
  2535. struct gen_pool *mmu_pgt_pool;
  2536. void *mmu_shadow_hop0;
  2537. };
  2538. /**
  2539. * struct hl_mmu_priv - used for holding per-device mmu internal information.
  2540. * @dr: information on the device-resident MMU, when exists.
  2541. * @hr: information on the host-resident MMU, when exists.
  2542. */
  2543. struct hl_mmu_priv {
  2544. struct hl_mmu_dr_priv dr;
  2545. struct hl_mmu_hr_priv hr;
  2546. };
  2547. /**
  2548. * struct hl_mmu_per_hop_info - A structure describing one TLB HOP and its entry
  2549. * that was created in order to translate a virtual address to a
  2550. * physical one.
  2551. * @hop_addr: The address of the hop.
  2552. * @hop_pte_addr: The address of the hop entry.
  2553. * @hop_pte_val: The value in the hop entry.
  2554. */
  2555. struct hl_mmu_per_hop_info {
  2556. u64 hop_addr;
  2557. u64 hop_pte_addr;
  2558. u64 hop_pte_val;
  2559. };
  2560. /**
  2561. * struct hl_mmu_hop_info - A structure describing the TLB hops and their
  2562. * hop-entries that were created in order to translate a virtual address to a
  2563. * physical one.
  2564. * @scrambled_vaddr: The value of the virtual address after scrambling. This
  2565. * address replaces the original virtual-address when mapped
  2566. * in the MMU tables.
  2567. * @unscrambled_paddr: The un-scrambled physical address.
  2568. * @hop_info: Array holding the per-hop information used for the translation.
  2569. * @used_hops: The number of hops used for the translation.
  2570. * @range_type: virtual address range type.
  2571. */
  2572. struct hl_mmu_hop_info {
  2573. u64 scrambled_vaddr;
  2574. u64 unscrambled_paddr;
  2575. struct hl_mmu_per_hop_info hop_info[MMU_ARCH_6_HOPS];
  2576. u32 used_hops;
  2577. enum hl_va_range_type range_type;
  2578. };
  2579. /**
  2580. * struct hl_hr_mmu_funcs - Device related host resident MMU functions.
  2581. * @get_hop0_pgt_info: get page table info structure for HOP0.
  2582. * @get_pgt_info: get page table info structure for HOP other than HOP0.
  2583. * @add_pgt_info: add page table info structure to hash.
  2584. * @get_tlb_mapping_params: get mapping parameters needed for getting TLB info for specific mapping.
  2585. */
  2586. struct hl_hr_mmu_funcs {
  2587. struct pgt_info *(*get_hop0_pgt_info)(struct hl_ctx *ctx);
  2588. struct pgt_info *(*get_pgt_info)(struct hl_ctx *ctx, u64 phys_hop_addr);
  2589. void (*add_pgt_info)(struct hl_ctx *ctx, struct pgt_info *pgt_info, dma_addr_t phys_addr);
  2590. int (*get_tlb_mapping_params)(struct hl_device *hdev, struct hl_mmu_properties **mmu_prop,
  2591. struct hl_mmu_hop_info *hops,
  2592. u64 virt_addr, bool *is_huge);
  2593. };
  2594. /**
  2595. * struct hl_mmu_funcs - Device related MMU functions.
  2596. * @init: initialize the MMU module.
  2597. * @fini: release the MMU module.
  2598. * @ctx_init: Initialize a context for using the MMU module.
  2599. * @ctx_fini: disable a ctx from using the mmu module.
  2600. * @map: maps a virtual address to physical address for a context.
  2601. * @unmap: unmap a virtual address of a context.
  2602. * @flush: flush all writes from all cores to reach device MMU.
  2603. * @swap_out: marks all mapping of the given context as swapped out.
  2604. * @swap_in: marks all mapping of the given context as swapped in.
  2605. * @get_tlb_info: returns the list of hops and hop-entries used that were
  2606. * created in order to translate the giver virtual address to a
  2607. * physical one.
  2608. * @hr_funcs: functions specific to host resident MMU.
  2609. */
  2610. struct hl_mmu_funcs {
  2611. int (*init)(struct hl_device *hdev);
  2612. void (*fini)(struct hl_device *hdev);
  2613. int (*ctx_init)(struct hl_ctx *ctx);
  2614. void (*ctx_fini)(struct hl_ctx *ctx);
  2615. int (*map)(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr, u32 page_size,
  2616. bool is_dram_addr);
  2617. int (*unmap)(struct hl_ctx *ctx, u64 virt_addr, bool is_dram_addr);
  2618. void (*flush)(struct hl_ctx *ctx);
  2619. void (*swap_out)(struct hl_ctx *ctx);
  2620. void (*swap_in)(struct hl_ctx *ctx);
  2621. int (*get_tlb_info)(struct hl_ctx *ctx, u64 virt_addr, struct hl_mmu_hop_info *hops);
  2622. struct hl_hr_mmu_funcs hr_funcs;
  2623. };
  2624. /**
  2625. * struct hl_prefetch_work - prefetch work structure handler
  2626. * @pf_work: actual work struct.
  2627. * @ctx: compute context.
  2628. * @va: virtual address to pre-fetch.
  2629. * @size: pre-fetch size.
  2630. * @flags: operation flags.
  2631. * @asid: ASID for maintenance operation.
  2632. */
  2633. struct hl_prefetch_work {
  2634. struct work_struct pf_work;
  2635. struct hl_ctx *ctx;
  2636. u64 va;
  2637. u64 size;
  2638. u32 flags;
  2639. u32 asid;
  2640. };
  2641. /*
  2642. * number of user contexts allowed to call wait_for_multi_cs ioctl in
  2643. * parallel
  2644. */
  2645. #define MULTI_CS_MAX_USER_CTX 2
  2646. /**
  2647. * struct multi_cs_completion - multi CS wait completion.
  2648. * @completion: completion of any of the CS in the list
  2649. * @lock: spinlock for the completion structure
  2650. * @timestamp: timestamp for the multi-CS completion
  2651. * @stream_master_qid_map: bitmap of all stream masters on which the multi-CS
  2652. * is waiting
  2653. * @used: 1 if in use, otherwise 0
  2654. */
  2655. struct multi_cs_completion {
  2656. struct completion completion;
  2657. spinlock_t lock;
  2658. s64 timestamp;
  2659. u32 stream_master_qid_map;
  2660. u8 used;
  2661. };
  2662. /**
  2663. * struct multi_cs_data - internal data for multi CS call
  2664. * @ctx: pointer to the context structure
  2665. * @fence_arr: array of fences of all CSs
  2666. * @seq_arr: array of CS sequence numbers
  2667. * @timeout_jiffies: timeout in jiffies for waiting for CS to complete
  2668. * @timestamp: timestamp of first completed CS
  2669. * @wait_status: wait for CS status
  2670. * @completion_bitmap: bitmap of completed CSs (1- completed, otherwise 0)
  2671. * @arr_len: fence_arr and seq_arr array length
  2672. * @gone_cs: indication of gone CS (1- there was gone CS, otherwise 0)
  2673. * @update_ts: update timestamp. 1- update the timestamp, otherwise 0.
  2674. */
  2675. struct multi_cs_data {
  2676. struct hl_ctx *ctx;
  2677. struct hl_fence **fence_arr;
  2678. u64 *seq_arr;
  2679. s64 timeout_jiffies;
  2680. s64 timestamp;
  2681. long wait_status;
  2682. u32 completion_bitmap;
  2683. u8 arr_len;
  2684. u8 gone_cs;
  2685. u8 update_ts;
  2686. };
  2687. /**
  2688. * struct hl_clk_throttle_timestamp - current/last clock throttling timestamp
  2689. * @start: timestamp taken when 'start' event is received in driver
  2690. * @end: timestamp taken when 'end' event is received in driver
  2691. */
  2692. struct hl_clk_throttle_timestamp {
  2693. ktime_t start;
  2694. ktime_t end;
  2695. };
  2696. /**
  2697. * struct hl_clk_throttle - keeps current/last clock throttling timestamps
  2698. * @timestamp: timestamp taken by driver and firmware, index 0 refers to POWER
  2699. * index 1 refers to THERMAL
  2700. * @lock: protects this structure as it can be accessed from both event queue
  2701. * context and info_ioctl context
  2702. * @current_reason: bitmask represents the current clk throttling reasons
  2703. * @aggregated_reason: bitmask represents aggregated clk throttling reasons since driver load
  2704. */
  2705. struct hl_clk_throttle {
  2706. struct hl_clk_throttle_timestamp timestamp[HL_CLK_THROTTLE_TYPE_MAX];
  2707. struct mutex lock;
  2708. u32 current_reason;
  2709. u32 aggregated_reason;
  2710. };
  2711. /**
  2712. * struct user_mapped_block - describes a hw block allowed to be mmapped by user
  2713. * @address: physical HW block address
  2714. * @size: allowed size for mmap
  2715. */
  2716. struct user_mapped_block {
  2717. u32 address;
  2718. u32 size;
  2719. };
  2720. /**
  2721. * struct cs_timeout_info - info of last CS timeout occurred.
  2722. * @timestamp: CS timeout timestamp.
  2723. * @write_enable: if set writing to CS parameters in the structure is enabled. otherwise - disabled,
  2724. * so the first (root cause) CS timeout will not be overwritten.
  2725. * @seq: CS timeout sequence number.
  2726. */
  2727. struct cs_timeout_info {
  2728. ktime_t timestamp;
  2729. atomic_t write_enable;
  2730. u64 seq;
  2731. };
  2732. /**
  2733. * struct razwi_info - info about last razwi error occurred.
  2734. * @timestamp: razwi timestamp.
  2735. * @write_enable: if set writing to razwi parameters in the structure is enabled.
  2736. * otherwise - disabled, so the first (root cause) razwi will not be overwritten.
  2737. * @addr: address that caused razwi.
  2738. * @engine_id_1: engine id of the razwi initiator, if it was initiated by engine that does
  2739. * not have engine id it will be set to U16_MAX.
  2740. * @engine_id_2: second engine id of razwi initiator. Might happen that razwi have 2 possible
  2741. * engines which one them caused the razwi. In that case, it will contain the
  2742. * second possible engine id, otherwise it will be set to U16_MAX.
  2743. * @non_engine_initiator: in case the initiator of the razwi does not have engine id.
  2744. * @type: cause of razwi, page fault or access error, otherwise it will be set to U8_MAX.
  2745. */
  2746. struct razwi_info {
  2747. ktime_t timestamp;
  2748. atomic_t write_enable;
  2749. u64 addr;
  2750. u16 engine_id_1;
  2751. u16 engine_id_2;
  2752. u8 non_engine_initiator;
  2753. u8 type;
  2754. };
  2755. #define MAX_QMAN_STREAMS_INFO 4
  2756. #define OPCODE_INFO_MAX_ADDR_SIZE 8
  2757. /**
  2758. * struct undefined_opcode_info - info about last undefined opcode error
  2759. * @timestamp: timestamp of the undefined opcode error
  2760. * @cb_addr_streams: CB addresses (per stream) that are currently exists in the PQ
  2761. * entries. In case all streams array entries are
  2762. * filled with values, it means the execution was in Lower-CP.
  2763. * @cq_addr: the address of the current handled command buffer
  2764. * @cq_size: the size of the current handled command buffer
  2765. * @cb_addr_streams_len: num of streams - actual len of cb_addr_streams array.
  2766. * should be equal to 1 incase of undefined opcode
  2767. * in Upper-CP (specific stream) and equal to 4 incase
  2768. * of undefined opcode in Lower-CP.
  2769. * @engine_id: engine-id that the error occurred on
  2770. * @stream_id: the stream id the error occurred on. In case the stream equals to
  2771. * MAX_QMAN_STREAMS_INFO it means the error occurred on a Lower-CP.
  2772. * @write_enable: if set, writing to undefined opcode parameters in the structure
  2773. * is enable so the first (root cause) undefined opcode will not be
  2774. * overwritten.
  2775. */
  2776. struct undefined_opcode_info {
  2777. ktime_t timestamp;
  2778. u64 cb_addr_streams[MAX_QMAN_STREAMS_INFO][OPCODE_INFO_MAX_ADDR_SIZE];
  2779. u64 cq_addr;
  2780. u32 cq_size;
  2781. u32 cb_addr_streams_len;
  2782. u32 engine_id;
  2783. u32 stream_id;
  2784. bool write_enable;
  2785. };
  2786. /**
  2787. * struct hl_error_info - holds information collected during an error.
  2788. * @cs_timeout: CS timeout error information.
  2789. * @razwi: razwi information.
  2790. * @undef_opcode: undefined opcode information
  2791. */
  2792. struct hl_error_info {
  2793. struct cs_timeout_info cs_timeout;
  2794. struct razwi_info razwi;
  2795. struct undefined_opcode_info undef_opcode;
  2796. };
  2797. /**
  2798. * struct hl_reset_info - holds current device reset information.
  2799. * @lock: lock to protect critical reset flows.
  2800. * @compute_reset_cnt: number of compute resets since the driver was loaded.
  2801. * @hard_reset_cnt: number of hard resets since the driver was loaded.
  2802. * @hard_reset_schedule_flags: hard reset is scheduled to after current compute reset,
  2803. * here we hold the hard reset flags.
  2804. * @in_reset: is device in reset flow.
  2805. * @in_compute_reset: Device is currently in reset but not in hard-reset.
  2806. * @needs_reset: true if reset_on_lockup is false and device should be reset
  2807. * due to lockup.
  2808. * @hard_reset_pending: is there a hard reset work pending.
  2809. * @curr_reset_cause: saves an enumerated reset cause when a hard reset is
  2810. * triggered, and cleared after it is shared with preboot.
  2811. * @prev_reset_trigger: saves the previous trigger which caused a reset, overridden
  2812. * with a new value on next reset
  2813. * @reset_trigger_repeated: set if device reset is triggered more than once with
  2814. * same cause.
  2815. * @skip_reset_on_timeout: Skip device reset if CS has timed out, wait for it to
  2816. * complete instead.
  2817. */
  2818. struct hl_reset_info {
  2819. spinlock_t lock;
  2820. u32 compute_reset_cnt;
  2821. u32 hard_reset_cnt;
  2822. u32 hard_reset_schedule_flags;
  2823. u8 in_reset;
  2824. u8 in_compute_reset;
  2825. u8 needs_reset;
  2826. u8 hard_reset_pending;
  2827. u8 curr_reset_cause;
  2828. u8 prev_reset_trigger;
  2829. u8 reset_trigger_repeated;
  2830. u8 skip_reset_on_timeout;
  2831. };
  2832. /**
  2833. * struct hl_device - habanalabs device structure.
  2834. * @pdev: pointer to PCI device, can be NULL in case of simulator device.
  2835. * @pcie_bar_phys: array of available PCIe bars physical addresses.
  2836. * (required only for PCI address match mode)
  2837. * @pcie_bar: array of available PCIe bars virtual addresses.
  2838. * @rmmio: configuration area address on SRAM.
  2839. * @cdev: related char device.
  2840. * @cdev_ctrl: char device for control operations only (INFO IOCTL)
  2841. * @dev: related kernel basic device structure.
  2842. * @dev_ctrl: related kernel device structure for the control device
  2843. * @work_heartbeat: delayed work for CPU-CP is-alive check.
  2844. * @device_reset_work: delayed work which performs hard reset
  2845. * @asic_name: ASIC specific name.
  2846. * @asic_type: ASIC specific type.
  2847. * @completion_queue: array of hl_cq.
  2848. * @user_interrupt: array of hl_user_interrupt. upon the corresponding user
  2849. * interrupt, driver will monitor the list of fences
  2850. * registered to this interrupt.
  2851. * @common_user_cq_interrupt: common user CQ interrupt for all user CQ interrupts.
  2852. * upon any user CQ interrupt, driver will monitor the
  2853. * list of fences registered to this common structure.
  2854. * @common_decoder_interrupt: common decoder interrupt for all user decoder interrupts.
  2855. * @shadow_cs_queue: pointer to a shadow queue that holds pointers to
  2856. * outstanding command submissions.
  2857. * @cq_wq: work queues of completion queues for executing work in process
  2858. * context.
  2859. * @eq_wq: work queue of event queue for executing work in process context.
  2860. * @cs_cmplt_wq: work queue of CS completions for executing work in process
  2861. * context.
  2862. * @ts_free_obj_wq: work queue for timestamp registration objects release.
  2863. * @pf_wq: work queue for MMU pre-fetch operations.
  2864. * @kernel_ctx: Kernel driver context structure.
  2865. * @kernel_queues: array of hl_hw_queue.
  2866. * @cs_mirror_list: CS mirror list for TDR.
  2867. * @cs_mirror_lock: protects cs_mirror_list.
  2868. * @kernel_mem_mgr: memory manager for memory buffers with lifespan of driver.
  2869. * @event_queue: event queue for IRQ from CPU-CP.
  2870. * @dma_pool: DMA pool for small allocations.
  2871. * @cpu_accessible_dma_mem: Host <-> CPU-CP shared memory CPU address.
  2872. * @cpu_accessible_dma_address: Host <-> CPU-CP shared memory DMA address.
  2873. * @cpu_accessible_dma_pool: Host <-> CPU-CP shared memory pool.
  2874. * @asid_bitmap: holds used/available ASIDs.
  2875. * @asid_mutex: protects asid_bitmap.
  2876. * @send_cpu_message_lock: enforces only one message in Host <-> CPU-CP queue.
  2877. * @debug_lock: protects critical section of setting debug mode for device
  2878. * @mmu_lock: protects the MMU page tables and invalidation h/w. Although the
  2879. * page tables are per context, the invalidation h/w is per MMU.
  2880. * Therefore, we can't allow multiple contexts (we only have two,
  2881. * user and kernel) to access the invalidation h/w at the same time.
  2882. * In addition, any change to the PGT, modifying the MMU hash or
  2883. * walking the PGT requires talking this lock.
  2884. * @asic_prop: ASIC specific immutable properties.
  2885. * @asic_funcs: ASIC specific functions.
  2886. * @asic_specific: ASIC specific information to use only from ASIC files.
  2887. * @vm: virtual memory manager for MMU.
  2888. * @hwmon_dev: H/W monitor device.
  2889. * @hl_chip_info: ASIC's sensors information.
  2890. * @device_status_description: device status description.
  2891. * @hl_debugfs: device's debugfs manager.
  2892. * @cb_pool: list of pre allocated CBs.
  2893. * @cb_pool_lock: protects the CB pool.
  2894. * @internal_cb_pool_virt_addr: internal command buffer pool virtual address.
  2895. * @internal_cb_pool_dma_addr: internal command buffer pool dma address.
  2896. * @internal_cb_pool: internal command buffer memory pool.
  2897. * @internal_cb_va_base: internal cb pool mmu virtual address base
  2898. * @fpriv_list: list of file private data structures. Each structure is created
  2899. * when a user opens the device
  2900. * @fpriv_ctrl_list: list of file private data structures. Each structure is created
  2901. * when a user opens the control device
  2902. * @fpriv_list_lock: protects the fpriv_list
  2903. * @fpriv_ctrl_list_lock: protects the fpriv_ctrl_list
  2904. * @aggregated_cs_counters: aggregated cs counters among all contexts
  2905. * @mmu_priv: device-specific MMU data.
  2906. * @mmu_func: device-related MMU functions.
  2907. * @dec: list of decoder sw instance
  2908. * @fw_loader: FW loader manager.
  2909. * @pci_mem_region: array of memory regions in the PCI
  2910. * @state_dump_specs: constants and dictionaries needed to dump system state.
  2911. * @multi_cs_completion: array of multi-CS completion.
  2912. * @clk_throttling: holds information about current/previous clock throttling events
  2913. * @captured_err_info: holds information about errors.
  2914. * @reset_info: holds current device reset information.
  2915. * @stream_master_qid_arr: pointer to array with QIDs of master streams.
  2916. * @fw_major_version: major version of current loaded preboot.
  2917. * @fw_minor_version: minor version of current loaded preboot.
  2918. * @dram_used_mem: current DRAM memory consumption.
  2919. * @memory_scrub_val: the value to which the dram will be scrubbed to using cb scrub_device_dram
  2920. * @timeout_jiffies: device CS timeout value.
  2921. * @max_power: the max power of the device, as configured by the sysadmin. This
  2922. * value is saved so in case of hard-reset, the driver will restore
  2923. * this value and update the F/W after the re-initialization
  2924. * @boot_error_status_mask: contains a mask of the device boot error status.
  2925. * Each bit represents a different error, according to
  2926. * the defines in hl_boot_if.h. If the bit is cleared,
  2927. * the error will be ignored by the driver during
  2928. * device initialization. Mainly used to debug and
  2929. * workaround firmware bugs
  2930. * @dram_pci_bar_start: start bus address of PCIe bar towards DRAM.
  2931. * @last_successful_open_ktime: timestamp (ktime) of the last successful device open.
  2932. * @last_successful_open_jif: timestamp (jiffies) of the last successful
  2933. * device open.
  2934. * @last_open_session_duration_jif: duration (jiffies) of the last device open
  2935. * session.
  2936. * @open_counter: number of successful device open operations.
  2937. * @fw_poll_interval_usec: FW status poll interval in usec.
  2938. * used for CPU boot status
  2939. * @fw_comms_poll_interval_usec: FW comms/protocol poll interval in usec.
  2940. * used for COMMs protocols cmds(COMMS_STS_*)
  2941. * @dram_binning: contains mask of drams that is received from the f/w which indicates which
  2942. * drams are binned-out
  2943. * @tpc_binning: contains mask of tpc engines that is received from the f/w which indicates which
  2944. * tpc engines are binned-out
  2945. * @card_type: Various ASICs have several card types. This indicates the card
  2946. * type of the current device.
  2947. * @major: habanalabs kernel driver major.
  2948. * @high_pll: high PLL profile frequency.
  2949. * @decoder_binning: contains mask of decoder engines that is received from the f/w which
  2950. * indicates which decoder engines are binned-out
  2951. * @edma_binning: contains mask of edma engines that is received from the f/w which
  2952. * indicates which edma engines are binned-out
  2953. * @id: device minor.
  2954. * @id_control: minor of the control device.
  2955. * @cdev_idx: char device index. Used for setting its name.
  2956. * @cpu_pci_msb_addr: 50-bit extension bits for the device CPU's 40-bit
  2957. * addresses.
  2958. * @is_in_dram_scrub: true if dram scrub operation is on going.
  2959. * @disabled: is device disabled.
  2960. * @late_init_done: is late init stage was done during initialization.
  2961. * @hwmon_initialized: is H/W monitor sensors was initialized.
  2962. * @reset_on_lockup: true if a reset should be done in case of stuck CS, false
  2963. * otherwise.
  2964. * @dram_default_page_mapping: is DRAM default page mapping enabled.
  2965. * @memory_scrub: true to perform device memory scrub in various locations,
  2966. * such as context-switch, context close, page free, etc.
  2967. * @pmmu_huge_range: is a different virtual addresses range used for PMMU with
  2968. * huge pages.
  2969. * @init_done: is the initialization of the device done.
  2970. * @device_cpu_disabled: is the device CPU disabled (due to timeouts)
  2971. * @in_debug: whether the device is in a state where the profiling/tracing infrastructure
  2972. * can be used. This indication is needed because in some ASICs we need to do
  2973. * specific operations to enable that infrastructure.
  2974. * @cdev_sysfs_created: were char devices and sysfs nodes created.
  2975. * @stop_on_err: true if engines should stop on error.
  2976. * @supports_sync_stream: is sync stream supported.
  2977. * @sync_stream_queue_idx: helper index for sync stream queues initialization.
  2978. * @collective_mon_idx: helper index for collective initialization
  2979. * @supports_coresight: is CoreSight supported.
  2980. * @supports_cb_mapping: is mapping a CB to the device's MMU supported.
  2981. * @process_kill_trial_cnt: number of trials reset thread tried killing
  2982. * user processes
  2983. * @device_fini_pending: true if device_fini was called and might be
  2984. * waiting for the reset thread to finish
  2985. * @supports_staged_submission: true if staged submissions are supported
  2986. * @device_cpu_is_halted: Flag to indicate whether the device CPU was already
  2987. * halted. We can't halt it again because the COMMS
  2988. * protocol will throw an error. Relevant only for
  2989. * cases where Linux was not loaded to device CPU
  2990. * @supports_wait_for_multi_cs: true if wait for multi CS is supported
  2991. * @is_compute_ctx_active: Whether there is an active compute context executing.
  2992. * @compute_ctx_in_release: true if the current compute context is being released.
  2993. * @supports_mmu_prefetch: true if prefetch is supported, otherwise false.
  2994. * @reset_upon_device_release: reset the device when the user closes the file descriptor of the
  2995. * device.
  2996. * @nic_ports_mask: Controls which NIC ports are enabled. Used only for testing.
  2997. * @fw_components: Controls which f/w components to load to the device. There are multiple f/w
  2998. * stages and sometimes we want to stop at a certain stage. Used only for testing.
  2999. * @mmu_enable: Whether to enable or disable the device MMU(s). Used only for testing.
  3000. * @cpu_queues_enable: Whether to enable queues communication vs. the f/w. Used only for testing.
  3001. * @pldm: Whether we are running in Palladium environment. Used only for testing.
  3002. * @hard_reset_on_fw_events: Whether to do device hard-reset when a fatal event is received from
  3003. * the f/w. Used only for testing.
  3004. * @bmc_enable: Whether we are running in a box with BMC. Used only for testing.
  3005. * @reset_on_preboot_fail: Whether to reset the device if preboot f/w fails to load.
  3006. * Used only for testing.
  3007. * @heartbeat: Controls if we want to enable the heartbeat mechanism vs. the f/w, which verifies
  3008. * that the f/w is always alive. Used only for testing.
  3009. * @supports_ctx_switch: true if a ctx switch is required upon first submission.
  3010. */
  3011. struct hl_device {
  3012. struct pci_dev *pdev;
  3013. u64 pcie_bar_phys[HL_PCI_NUM_BARS];
  3014. void __iomem *pcie_bar[HL_PCI_NUM_BARS];
  3015. void __iomem *rmmio;
  3016. struct cdev cdev;
  3017. struct cdev cdev_ctrl;
  3018. struct device *dev;
  3019. struct device *dev_ctrl;
  3020. struct delayed_work work_heartbeat;
  3021. struct hl_device_reset_work device_reset_work;
  3022. char asic_name[HL_STR_MAX];
  3023. char status[HL_DEV_STS_MAX][HL_STR_MAX];
  3024. enum hl_asic_type asic_type;
  3025. struct hl_cq *completion_queue;
  3026. struct hl_user_interrupt *user_interrupt;
  3027. struct hl_user_interrupt common_user_cq_interrupt;
  3028. struct hl_user_interrupt common_decoder_interrupt;
  3029. struct hl_cs **shadow_cs_queue;
  3030. struct workqueue_struct **cq_wq;
  3031. struct workqueue_struct *eq_wq;
  3032. struct workqueue_struct *cs_cmplt_wq;
  3033. struct workqueue_struct *ts_free_obj_wq;
  3034. struct workqueue_struct *pf_wq;
  3035. struct hl_ctx *kernel_ctx;
  3036. struct hl_hw_queue *kernel_queues;
  3037. struct list_head cs_mirror_list;
  3038. spinlock_t cs_mirror_lock;
  3039. struct hl_mem_mgr kernel_mem_mgr;
  3040. struct hl_eq event_queue;
  3041. struct dma_pool *dma_pool;
  3042. void *cpu_accessible_dma_mem;
  3043. dma_addr_t cpu_accessible_dma_address;
  3044. struct gen_pool *cpu_accessible_dma_pool;
  3045. unsigned long *asid_bitmap;
  3046. struct mutex asid_mutex;
  3047. struct mutex send_cpu_message_lock;
  3048. struct mutex debug_lock;
  3049. struct mutex mmu_lock;
  3050. struct asic_fixed_properties asic_prop;
  3051. const struct hl_asic_funcs *asic_funcs;
  3052. void *asic_specific;
  3053. struct hl_vm vm;
  3054. struct device *hwmon_dev;
  3055. struct hwmon_chip_info *hl_chip_info;
  3056. struct hl_dbg_device_entry hl_debugfs;
  3057. struct list_head cb_pool;
  3058. spinlock_t cb_pool_lock;
  3059. void *internal_cb_pool_virt_addr;
  3060. dma_addr_t internal_cb_pool_dma_addr;
  3061. struct gen_pool *internal_cb_pool;
  3062. u64 internal_cb_va_base;
  3063. struct list_head fpriv_list;
  3064. struct list_head fpriv_ctrl_list;
  3065. struct mutex fpriv_list_lock;
  3066. struct mutex fpriv_ctrl_list_lock;
  3067. struct hl_cs_counters_atomic aggregated_cs_counters;
  3068. struct hl_mmu_priv mmu_priv;
  3069. struct hl_mmu_funcs mmu_func[MMU_NUM_PGT_LOCATIONS];
  3070. struct hl_dec *dec;
  3071. struct fw_load_mgr fw_loader;
  3072. struct pci_mem_region pci_mem_region[PCI_REGION_NUMBER];
  3073. struct hl_state_dump_specs state_dump_specs;
  3074. struct multi_cs_completion multi_cs_completion[
  3075. MULTI_CS_MAX_USER_CTX];
  3076. struct hl_clk_throttle clk_throttling;
  3077. struct hl_error_info captured_err_info;
  3078. struct hl_reset_info reset_info;
  3079. u32 *stream_master_qid_arr;
  3080. u32 fw_major_version;
  3081. u32 fw_minor_version;
  3082. atomic64_t dram_used_mem;
  3083. u64 memory_scrub_val;
  3084. u64 timeout_jiffies;
  3085. u64 max_power;
  3086. u64 boot_error_status_mask;
  3087. u64 dram_pci_bar_start;
  3088. u64 last_successful_open_jif;
  3089. u64 last_open_session_duration_jif;
  3090. u64 open_counter;
  3091. u64 fw_poll_interval_usec;
  3092. ktime_t last_successful_open_ktime;
  3093. u64 fw_comms_poll_interval_usec;
  3094. u64 dram_binning;
  3095. u64 tpc_binning;
  3096. enum cpucp_card_types card_type;
  3097. u32 major;
  3098. u32 high_pll;
  3099. u32 decoder_binning;
  3100. u32 edma_binning;
  3101. u16 id;
  3102. u16 id_control;
  3103. u16 cdev_idx;
  3104. u16 cpu_pci_msb_addr;
  3105. u8 is_in_dram_scrub;
  3106. u8 disabled;
  3107. u8 late_init_done;
  3108. u8 hwmon_initialized;
  3109. u8 reset_on_lockup;
  3110. u8 dram_default_page_mapping;
  3111. u8 memory_scrub;
  3112. u8 pmmu_huge_range;
  3113. u8 init_done;
  3114. u8 device_cpu_disabled;
  3115. u8 in_debug;
  3116. u8 cdev_sysfs_created;
  3117. u8 stop_on_err;
  3118. u8 supports_sync_stream;
  3119. u8 sync_stream_queue_idx;
  3120. u8 collective_mon_idx;
  3121. u8 supports_coresight;
  3122. u8 supports_cb_mapping;
  3123. u8 process_kill_trial_cnt;
  3124. u8 device_fini_pending;
  3125. u8 supports_staged_submission;
  3126. u8 device_cpu_is_halted;
  3127. u8 supports_wait_for_multi_cs;
  3128. u8 stream_master_qid_arr_size;
  3129. u8 is_compute_ctx_active;
  3130. u8 compute_ctx_in_release;
  3131. u8 supports_mmu_prefetch;
  3132. u8 reset_upon_device_release;
  3133. u8 supports_ctx_switch;
  3134. /* Parameters for bring-up */
  3135. u64 nic_ports_mask;
  3136. u64 fw_components;
  3137. u8 mmu_enable;
  3138. u8 cpu_queues_enable;
  3139. u8 pldm;
  3140. u8 hard_reset_on_fw_events;
  3141. u8 bmc_enable;
  3142. u8 reset_on_preboot_fail;
  3143. u8 heartbeat;
  3144. };
  3145. /**
  3146. * struct hl_cs_encaps_sig_handle - encapsulated signals handle structure
  3147. * @refcount: refcount used to protect removing this id when several
  3148. * wait cs are used to wait of the reserved encaps signals.
  3149. * @hdev: pointer to habanalabs device structure.
  3150. * @hw_sob: pointer to H/W SOB used in the reservation.
  3151. * @ctx: pointer to the user's context data structure
  3152. * @cs_seq: staged cs sequence which contains encapsulated signals
  3153. * @id: idr handler id to be used to fetch the handler info
  3154. * @q_idx: stream queue index
  3155. * @pre_sob_val: current SOB value before reservation
  3156. * @count: signals number
  3157. */
  3158. struct hl_cs_encaps_sig_handle {
  3159. struct kref refcount;
  3160. struct hl_device *hdev;
  3161. struct hl_hw_sob *hw_sob;
  3162. struct hl_ctx *ctx;
  3163. u64 cs_seq;
  3164. u32 id;
  3165. u32 q_idx;
  3166. u32 pre_sob_val;
  3167. u32 count;
  3168. };
  3169. /*
  3170. * IOCTLs
  3171. */
  3172. /**
  3173. * typedef hl_ioctl_t - typedef for ioctl function in the driver
  3174. * @hpriv: pointer to the FD's private data, which contains state of
  3175. * user process
  3176. * @data: pointer to the input/output arguments structure of the IOCTL
  3177. *
  3178. * Return: 0 for success, negative value for error
  3179. */
  3180. typedef int hl_ioctl_t(struct hl_fpriv *hpriv, void *data);
  3181. /**
  3182. * struct hl_ioctl_desc - describes an IOCTL entry of the driver.
  3183. * @cmd: the IOCTL code as created by the kernel macros.
  3184. * @func: pointer to the driver's function that should be called for this IOCTL.
  3185. */
  3186. struct hl_ioctl_desc {
  3187. unsigned int cmd;
  3188. hl_ioctl_t *func;
  3189. };
  3190. /*
  3191. * Kernel module functions that can be accessed by entire module
  3192. */
  3193. /**
  3194. * hl_get_sg_info() - get number of pages and the DMA address from SG list.
  3195. * @sg: the SG list.
  3196. * @dma_addr: pointer to DMA address to return.
  3197. *
  3198. * Calculate the number of consecutive pages described by the SG list. Take the
  3199. * offset of the address in the first page, add to it the length and round it up
  3200. * to the number of needed pages.
  3201. */
  3202. static inline u32 hl_get_sg_info(struct scatterlist *sg, dma_addr_t *dma_addr)
  3203. {
  3204. *dma_addr = sg_dma_address(sg);
  3205. return ((((*dma_addr) & (PAGE_SIZE - 1)) + sg_dma_len(sg)) +
  3206. (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  3207. }
  3208. /**
  3209. * hl_mem_area_inside_range() - Checks whether address+size are inside a range.
  3210. * @address: The start address of the area we want to validate.
  3211. * @size: The size in bytes of the area we want to validate.
  3212. * @range_start_address: The start address of the valid range.
  3213. * @range_end_address: The end address of the valid range.
  3214. *
  3215. * Return: true if the area is inside the valid range, false otherwise.
  3216. */
  3217. static inline bool hl_mem_area_inside_range(u64 address, u64 size,
  3218. u64 range_start_address, u64 range_end_address)
  3219. {
  3220. u64 end_address = address + size;
  3221. if ((address >= range_start_address) &&
  3222. (end_address <= range_end_address) &&
  3223. (end_address > address))
  3224. return true;
  3225. return false;
  3226. }
  3227. /**
  3228. * hl_mem_area_crosses_range() - Checks whether address+size crossing a range.
  3229. * @address: The start address of the area we want to validate.
  3230. * @size: The size in bytes of the area we want to validate.
  3231. * @range_start_address: The start address of the valid range.
  3232. * @range_end_address: The end address of the valid range.
  3233. *
  3234. * Return: true if the area overlaps part or all of the valid range,
  3235. * false otherwise.
  3236. */
  3237. static inline bool hl_mem_area_crosses_range(u64 address, u32 size,
  3238. u64 range_start_address, u64 range_end_address)
  3239. {
  3240. u64 end_address = address + size - 1;
  3241. return ((address <= range_end_address) && (range_start_address <= end_address));
  3242. }
  3243. uint64_t hl_set_dram_bar_default(struct hl_device *hdev, u64 addr);
  3244. void *hl_asic_dma_alloc_coherent_caller(struct hl_device *hdev, size_t size, dma_addr_t *dma_handle,
  3245. gfp_t flag, const char *caller);
  3246. void hl_asic_dma_free_coherent_caller(struct hl_device *hdev, size_t size, void *cpu_addr,
  3247. dma_addr_t dma_handle, const char *caller);
  3248. void *hl_cpu_accessible_dma_pool_alloc_caller(struct hl_device *hdev, size_t size,
  3249. dma_addr_t *dma_handle, const char *caller);
  3250. void hl_cpu_accessible_dma_pool_free_caller(struct hl_device *hdev, size_t size, void *vaddr,
  3251. const char *caller);
  3252. void *hl_asic_dma_pool_zalloc_caller(struct hl_device *hdev, size_t size, gfp_t mem_flags,
  3253. dma_addr_t *dma_handle, const char *caller);
  3254. void hl_asic_dma_pool_free_caller(struct hl_device *hdev, void *vaddr, dma_addr_t dma_addr,
  3255. const char *caller);
  3256. int hl_dma_map_sgtable(struct hl_device *hdev, struct sg_table *sgt, enum dma_data_direction dir);
  3257. void hl_dma_unmap_sgtable(struct hl_device *hdev, struct sg_table *sgt,
  3258. enum dma_data_direction dir);
  3259. int hl_access_cfg_region(struct hl_device *hdev, u64 addr, u64 *val,
  3260. enum debugfs_access_type acc_type);
  3261. int hl_access_dev_mem(struct hl_device *hdev, enum pci_region region_type,
  3262. u64 addr, u64 *val, enum debugfs_access_type acc_type);
  3263. int hl_device_open(struct inode *inode, struct file *filp);
  3264. int hl_device_open_ctrl(struct inode *inode, struct file *filp);
  3265. bool hl_device_operational(struct hl_device *hdev,
  3266. enum hl_device_status *status);
  3267. enum hl_device_status hl_device_status(struct hl_device *hdev);
  3268. int hl_device_set_debug_mode(struct hl_device *hdev, struct hl_ctx *ctx, bool enable);
  3269. int hl_hw_queues_create(struct hl_device *hdev);
  3270. void hl_hw_queues_destroy(struct hl_device *hdev);
  3271. int hl_hw_queue_send_cb_no_cmpl(struct hl_device *hdev, u32 hw_queue_id,
  3272. u32 cb_size, u64 cb_ptr);
  3273. void hl_hw_queue_submit_bd(struct hl_device *hdev, struct hl_hw_queue *q,
  3274. u32 ctl, u32 len, u64 ptr);
  3275. int hl_hw_queue_schedule_cs(struct hl_cs *cs);
  3276. u32 hl_hw_queue_add_ptr(u32 ptr, u16 val);
  3277. void hl_hw_queue_inc_ci_kernel(struct hl_device *hdev, u32 hw_queue_id);
  3278. void hl_hw_queue_update_ci(struct hl_cs *cs);
  3279. void hl_hw_queue_reset(struct hl_device *hdev, bool hard_reset);
  3280. #define hl_queue_inc_ptr(p) hl_hw_queue_add_ptr(p, 1)
  3281. #define hl_pi_2_offset(pi) ((pi) & (HL_QUEUE_LENGTH - 1))
  3282. int hl_cq_init(struct hl_device *hdev, struct hl_cq *q, u32 hw_queue_id);
  3283. void hl_cq_fini(struct hl_device *hdev, struct hl_cq *q);
  3284. int hl_eq_init(struct hl_device *hdev, struct hl_eq *q);
  3285. void hl_eq_fini(struct hl_device *hdev, struct hl_eq *q);
  3286. void hl_cq_reset(struct hl_device *hdev, struct hl_cq *q);
  3287. void hl_eq_reset(struct hl_device *hdev, struct hl_eq *q);
  3288. irqreturn_t hl_irq_handler_cq(int irq, void *arg);
  3289. irqreturn_t hl_irq_handler_eq(int irq, void *arg);
  3290. irqreturn_t hl_irq_handler_dec_abnrm(int irq, void *arg);
  3291. irqreturn_t hl_irq_handler_user_interrupt(int irq, void *arg);
  3292. irqreturn_t hl_irq_handler_default(int irq, void *arg);
  3293. u32 hl_cq_inc_ptr(u32 ptr);
  3294. int hl_asid_init(struct hl_device *hdev);
  3295. void hl_asid_fini(struct hl_device *hdev);
  3296. unsigned long hl_asid_alloc(struct hl_device *hdev);
  3297. void hl_asid_free(struct hl_device *hdev, unsigned long asid);
  3298. int hl_ctx_create(struct hl_device *hdev, struct hl_fpriv *hpriv);
  3299. void hl_ctx_free(struct hl_device *hdev, struct hl_ctx *ctx);
  3300. int hl_ctx_init(struct hl_device *hdev, struct hl_ctx *ctx, bool is_kernel_ctx);
  3301. void hl_ctx_do_release(struct kref *ref);
  3302. void hl_ctx_get(struct hl_ctx *ctx);
  3303. int hl_ctx_put(struct hl_ctx *ctx);
  3304. struct hl_ctx *hl_get_compute_ctx(struct hl_device *hdev);
  3305. struct hl_fence *hl_ctx_get_fence(struct hl_ctx *ctx, u64 seq);
  3306. int hl_ctx_get_fences(struct hl_ctx *ctx, u64 *seq_arr,
  3307. struct hl_fence **fence, u32 arr_len);
  3308. void hl_ctx_mgr_init(struct hl_ctx_mgr *mgr);
  3309. void hl_ctx_mgr_fini(struct hl_device *hdev, struct hl_ctx_mgr *mgr);
  3310. int hl_device_init(struct hl_device *hdev, struct class *hclass);
  3311. void hl_device_fini(struct hl_device *hdev);
  3312. int hl_device_suspend(struct hl_device *hdev);
  3313. int hl_device_resume(struct hl_device *hdev);
  3314. int hl_device_reset(struct hl_device *hdev, u32 flags);
  3315. void hl_hpriv_get(struct hl_fpriv *hpriv);
  3316. int hl_hpriv_put(struct hl_fpriv *hpriv);
  3317. int hl_device_utilization(struct hl_device *hdev, u32 *utilization);
  3318. int hl_build_hwmon_channel_info(struct hl_device *hdev,
  3319. struct cpucp_sensor *sensors_arr);
  3320. void hl_notifier_event_send_all(struct hl_device *hdev, u64 event_mask);
  3321. int hl_sysfs_init(struct hl_device *hdev);
  3322. void hl_sysfs_fini(struct hl_device *hdev);
  3323. int hl_hwmon_init(struct hl_device *hdev);
  3324. void hl_hwmon_fini(struct hl_device *hdev);
  3325. void hl_hwmon_release_resources(struct hl_device *hdev);
  3326. int hl_cb_create(struct hl_device *hdev, struct hl_mem_mgr *mmg,
  3327. struct hl_ctx *ctx, u32 cb_size, bool internal_cb,
  3328. bool map_cb, u64 *handle);
  3329. int hl_cb_destroy(struct hl_mem_mgr *mmg, u64 cb_handle);
  3330. int hl_hw_block_mmap(struct hl_fpriv *hpriv, struct vm_area_struct *vma);
  3331. struct hl_cb *hl_cb_get(struct hl_mem_mgr *mmg, u64 handle);
  3332. void hl_cb_put(struct hl_cb *cb);
  3333. struct hl_cb *hl_cb_kernel_create(struct hl_device *hdev, u32 cb_size,
  3334. bool internal_cb);
  3335. int hl_cb_pool_init(struct hl_device *hdev);
  3336. int hl_cb_pool_fini(struct hl_device *hdev);
  3337. int hl_cb_va_pool_init(struct hl_ctx *ctx);
  3338. void hl_cb_va_pool_fini(struct hl_ctx *ctx);
  3339. void hl_cs_rollback_all(struct hl_device *hdev, bool skip_wq_flush);
  3340. struct hl_cs_job *hl_cs_allocate_job(struct hl_device *hdev,
  3341. enum hl_queue_type queue_type, bool is_kernel_allocated_cb);
  3342. void hl_sob_reset_error(struct kref *ref);
  3343. int hl_gen_sob_mask(u16 sob_base, u8 sob_mask, u8 *mask);
  3344. void hl_fence_put(struct hl_fence *fence);
  3345. void hl_fences_put(struct hl_fence **fence, int len);
  3346. void hl_fence_get(struct hl_fence *fence);
  3347. void cs_get(struct hl_cs *cs);
  3348. bool cs_needs_completion(struct hl_cs *cs);
  3349. bool cs_needs_timeout(struct hl_cs *cs);
  3350. bool is_staged_cs_last_exists(struct hl_device *hdev, struct hl_cs *cs);
  3351. struct hl_cs *hl_staged_cs_find_first(struct hl_device *hdev, u64 cs_seq);
  3352. void hl_multi_cs_completion_init(struct hl_device *hdev);
  3353. void goya_set_asic_funcs(struct hl_device *hdev);
  3354. void gaudi_set_asic_funcs(struct hl_device *hdev);
  3355. void gaudi2_set_asic_funcs(struct hl_device *hdev);
  3356. int hl_vm_ctx_init(struct hl_ctx *ctx);
  3357. void hl_vm_ctx_fini(struct hl_ctx *ctx);
  3358. int hl_vm_init(struct hl_device *hdev);
  3359. void hl_vm_fini(struct hl_device *hdev);
  3360. void hl_hw_block_mem_init(struct hl_ctx *ctx);
  3361. void hl_hw_block_mem_fini(struct hl_ctx *ctx);
  3362. u64 hl_reserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
  3363. enum hl_va_range_type type, u64 size, u32 alignment);
  3364. int hl_unreserve_va_block(struct hl_device *hdev, struct hl_ctx *ctx,
  3365. u64 start_addr, u64 size);
  3366. int hl_pin_host_memory(struct hl_device *hdev, u64 addr, u64 size,
  3367. struct hl_userptr *userptr);
  3368. void hl_unpin_host_memory(struct hl_device *hdev, struct hl_userptr *userptr);
  3369. void hl_userptr_delete_list(struct hl_device *hdev,
  3370. struct list_head *userptr_list);
  3371. bool hl_userptr_is_pinned(struct hl_device *hdev, u64 addr, u32 size,
  3372. struct list_head *userptr_list,
  3373. struct hl_userptr **userptr);
  3374. int hl_mmu_init(struct hl_device *hdev);
  3375. void hl_mmu_fini(struct hl_device *hdev);
  3376. int hl_mmu_ctx_init(struct hl_ctx *ctx);
  3377. void hl_mmu_ctx_fini(struct hl_ctx *ctx);
  3378. int hl_mmu_map_page(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
  3379. u32 page_size, bool flush_pte);
  3380. int hl_mmu_get_real_page_size(struct hl_device *hdev, struct hl_mmu_properties *mmu_prop,
  3381. u32 page_size, u32 *real_page_size, bool is_dram_addr);
  3382. int hl_mmu_unmap_page(struct hl_ctx *ctx, u64 virt_addr, u32 page_size,
  3383. bool flush_pte);
  3384. int hl_mmu_map_contiguous(struct hl_ctx *ctx, u64 virt_addr,
  3385. u64 phys_addr, u32 size);
  3386. int hl_mmu_unmap_contiguous(struct hl_ctx *ctx, u64 virt_addr, u32 size);
  3387. int hl_mmu_invalidate_cache(struct hl_device *hdev, bool is_hard, u32 flags);
  3388. int hl_mmu_invalidate_cache_range(struct hl_device *hdev, bool is_hard,
  3389. u32 flags, u32 asid, u64 va, u64 size);
  3390. int hl_mmu_prefetch_cache_range(struct hl_ctx *ctx, u32 flags, u32 asid, u64 va, u64 size);
  3391. u64 hl_mmu_get_next_hop_addr(struct hl_ctx *ctx, u64 curr_pte);
  3392. u64 hl_mmu_get_hop_pte_phys_addr(struct hl_ctx *ctx, struct hl_mmu_properties *mmu_prop,
  3393. u8 hop_idx, u64 hop_addr, u64 virt_addr);
  3394. void hl_mmu_hr_flush(struct hl_ctx *ctx);
  3395. int hl_mmu_hr_init(struct hl_device *hdev, struct hl_mmu_hr_priv *hr_priv, u32 hop_table_size,
  3396. u64 pgt_size);
  3397. void hl_mmu_hr_fini(struct hl_device *hdev, struct hl_mmu_hr_priv *hr_priv, u32 hop_table_size);
  3398. void hl_mmu_hr_free_hop_remove_pgt(struct pgt_info *pgt_info, struct hl_mmu_hr_priv *hr_priv,
  3399. u32 hop_table_size);
  3400. u64 hl_mmu_hr_pte_phys_to_virt(struct hl_ctx *ctx, struct pgt_info *pgt, u64 phys_pte_addr,
  3401. u32 hop_table_size);
  3402. void hl_mmu_hr_write_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info, u64 phys_pte_addr,
  3403. u64 val, u32 hop_table_size);
  3404. void hl_mmu_hr_clear_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info, u64 phys_pte_addr,
  3405. u32 hop_table_size);
  3406. int hl_mmu_hr_put_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info, struct hl_mmu_hr_priv *hr_priv,
  3407. u32 hop_table_size);
  3408. void hl_mmu_hr_get_pte(struct hl_ctx *ctx, struct hl_hr_mmu_funcs *hr_func, u64 phys_hop_addr);
  3409. struct pgt_info *hl_mmu_hr_get_next_hop_pgt_info(struct hl_ctx *ctx,
  3410. struct hl_hr_mmu_funcs *hr_func,
  3411. u64 curr_pte);
  3412. struct pgt_info *hl_mmu_hr_alloc_hop(struct hl_ctx *ctx, struct hl_mmu_hr_priv *hr_priv,
  3413. struct hl_hr_mmu_funcs *hr_func,
  3414. struct hl_mmu_properties *mmu_prop);
  3415. struct pgt_info *hl_mmu_hr_get_alloc_next_hop(struct hl_ctx *ctx,
  3416. struct hl_mmu_hr_priv *hr_priv,
  3417. struct hl_hr_mmu_funcs *hr_func,
  3418. struct hl_mmu_properties *mmu_prop,
  3419. u64 curr_pte, bool *is_new_hop);
  3420. int hl_mmu_hr_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr, struct hl_mmu_hop_info *hops,
  3421. struct hl_hr_mmu_funcs *hr_func);
  3422. void hl_mmu_swap_out(struct hl_ctx *ctx);
  3423. void hl_mmu_swap_in(struct hl_ctx *ctx);
  3424. int hl_mmu_if_set_funcs(struct hl_device *hdev);
  3425. void hl_mmu_v1_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu);
  3426. void hl_mmu_v2_hr_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu);
  3427. int hl_mmu_va_to_pa(struct hl_ctx *ctx, u64 virt_addr, u64 *phys_addr);
  3428. int hl_mmu_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr,
  3429. struct hl_mmu_hop_info *hops);
  3430. u64 hl_mmu_scramble_addr(struct hl_device *hdev, u64 addr);
  3431. u64 hl_mmu_descramble_addr(struct hl_device *hdev, u64 addr);
  3432. bool hl_is_dram_va(struct hl_device *hdev, u64 virt_addr);
  3433. int hl_fw_load_fw_to_device(struct hl_device *hdev, const char *fw_name,
  3434. void __iomem *dst, u32 src_offset, u32 size);
  3435. int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode, u64 value);
  3436. int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
  3437. u16 len, u32 timeout, u64 *result);
  3438. int hl_fw_unmask_irq(struct hl_device *hdev, u16 event_type);
  3439. int hl_fw_unmask_irq_arr(struct hl_device *hdev, const u32 *irq_arr,
  3440. size_t irq_arr_size);
  3441. int hl_fw_test_cpu_queue(struct hl_device *hdev);
  3442. void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
  3443. dma_addr_t *dma_handle);
  3444. void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
  3445. void *vaddr);
  3446. int hl_fw_send_heartbeat(struct hl_device *hdev);
  3447. int hl_fw_cpucp_info_get(struct hl_device *hdev,
  3448. u32 sts_boot_dev_sts0_reg,
  3449. u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
  3450. u32 boot_err1_reg);
  3451. int hl_fw_cpucp_handshake(struct hl_device *hdev,
  3452. u32 sts_boot_dev_sts0_reg,
  3453. u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
  3454. u32 boot_err1_reg);
  3455. int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size);
  3456. int hl_fw_get_monitor_dump(struct hl_device *hdev, void *data);
  3457. int hl_fw_cpucp_pci_counters_get(struct hl_device *hdev,
  3458. struct hl_info_pci_counters *counters);
  3459. int hl_fw_cpucp_total_energy_get(struct hl_device *hdev,
  3460. u64 *total_energy);
  3461. int get_used_pll_index(struct hl_device *hdev, u32 input_pll_index,
  3462. enum pll_index *pll_index);
  3463. int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u32 pll_index,
  3464. u16 *pll_freq_arr);
  3465. int hl_fw_cpucp_power_get(struct hl_device *hdev, u64 *power);
  3466. void hl_fw_ask_hard_reset_without_linux(struct hl_device *hdev);
  3467. void hl_fw_ask_halt_machine_without_linux(struct hl_device *hdev);
  3468. int hl_fw_init_cpu(struct hl_device *hdev);
  3469. int hl_fw_read_preboot_status(struct hl_device *hdev);
  3470. int hl_fw_dynamic_send_protocol_cmd(struct hl_device *hdev,
  3471. struct fw_load_mgr *fw_loader,
  3472. enum comms_cmd cmd, unsigned int size,
  3473. bool wait_ok, u32 timeout);
  3474. int hl_fw_dram_replaced_row_get(struct hl_device *hdev,
  3475. struct cpucp_hbm_row_info *info);
  3476. int hl_fw_dram_pending_row_get(struct hl_device *hdev, u32 *pend_rows_num);
  3477. int hl_fw_cpucp_engine_core_asid_set(struct hl_device *hdev, u32 asid);
  3478. int hl_fw_send_device_activity(struct hl_device *hdev, bool open);
  3479. int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
  3480. bool is_wc[3]);
  3481. int hl_pci_elbi_read(struct hl_device *hdev, u64 addr, u32 *data);
  3482. int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data);
  3483. int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
  3484. struct hl_inbound_pci_region *pci_region);
  3485. int hl_pci_set_outbound_region(struct hl_device *hdev,
  3486. struct hl_outbound_pci_region *pci_region);
  3487. enum pci_region hl_get_pci_memory_region(struct hl_device *hdev, u64 addr);
  3488. int hl_pci_init(struct hl_device *hdev);
  3489. void hl_pci_fini(struct hl_device *hdev);
  3490. long hl_fw_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr);
  3491. void hl_fw_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq);
  3492. int hl_get_temperature(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
  3493. int hl_set_temperature(struct hl_device *hdev, int sensor_index, u32 attr, long value);
  3494. int hl_get_voltage(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
  3495. int hl_get_current(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
  3496. int hl_get_fan_speed(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
  3497. int hl_get_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
  3498. void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr, long value);
  3499. long hl_fw_get_max_power(struct hl_device *hdev);
  3500. void hl_fw_set_max_power(struct hl_device *hdev);
  3501. int hl_fw_get_sec_attest_info(struct hl_device *hdev, struct cpucp_sec_attest_info *sec_attest_info,
  3502. u32 nonce);
  3503. int hl_set_voltage(struct hl_device *hdev, int sensor_index, u32 attr, long value);
  3504. int hl_set_current(struct hl_device *hdev, int sensor_index, u32 attr, long value);
  3505. int hl_set_power(struct hl_device *hdev, int sensor_index, u32 attr, long value);
  3506. int hl_get_power(struct hl_device *hdev, int sensor_index, u32 attr, long *value);
  3507. int hl_fw_get_clk_rate(struct hl_device *hdev, u32 *cur_clk, u32 *max_clk);
  3508. void hl_fw_set_pll_profile(struct hl_device *hdev);
  3509. void hl_sysfs_add_dev_clk_attr(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp);
  3510. void hl_sysfs_add_dev_vrm_attr(struct hl_device *hdev, struct attribute_group *dev_vrm_attr_grp);
  3511. void hw_sob_get(struct hl_hw_sob *hw_sob);
  3512. void hw_sob_put(struct hl_hw_sob *hw_sob);
  3513. void hl_encaps_handle_do_release(struct kref *ref);
  3514. void hl_hw_queue_encaps_sig_set_sob_info(struct hl_device *hdev,
  3515. struct hl_cs *cs, struct hl_cs_job *job,
  3516. struct hl_cs_compl *cs_cmpl);
  3517. int hl_dec_init(struct hl_device *hdev);
  3518. void hl_dec_fini(struct hl_device *hdev);
  3519. void hl_dec_ctx_fini(struct hl_ctx *ctx);
  3520. void hl_release_pending_user_interrupts(struct hl_device *hdev);
  3521. int hl_cs_signal_sob_wraparound_handler(struct hl_device *hdev, u32 q_idx,
  3522. struct hl_hw_sob **hw_sob, u32 count, bool encaps_sig);
  3523. int hl_state_dump(struct hl_device *hdev);
  3524. const char *hl_state_dump_get_sync_name(struct hl_device *hdev, u32 sync_id);
  3525. const char *hl_state_dump_get_monitor_name(struct hl_device *hdev,
  3526. struct hl_mon_state_dump *mon);
  3527. void hl_state_dump_free_sync_to_engine_map(struct hl_sync_to_engine_map *map);
  3528. __printf(4, 5) int hl_snprintf_resize(char **buf, size_t *size, size_t *offset,
  3529. const char *format, ...);
  3530. char *hl_format_as_binary(char *buf, size_t buf_len, u32 n);
  3531. const char *hl_sync_engine_to_string(enum hl_sync_engine_type engine_type);
  3532. void hl_mem_mgr_init(struct device *dev, struct hl_mem_mgr *mmg);
  3533. void hl_mem_mgr_fini(struct hl_mem_mgr *mmg);
  3534. int hl_mem_mgr_mmap(struct hl_mem_mgr *mmg, struct vm_area_struct *vma,
  3535. void *args);
  3536. struct hl_mmap_mem_buf *hl_mmap_mem_buf_get(struct hl_mem_mgr *mmg,
  3537. u64 handle);
  3538. int hl_mmap_mem_buf_put_handle(struct hl_mem_mgr *mmg, u64 handle);
  3539. int hl_mmap_mem_buf_put(struct hl_mmap_mem_buf *buf);
  3540. struct hl_mmap_mem_buf *
  3541. hl_mmap_mem_buf_alloc(struct hl_mem_mgr *mmg,
  3542. struct hl_mmap_mem_buf_behavior *behavior, gfp_t gfp,
  3543. void *args);
  3544. __printf(2, 3) void hl_engine_data_sprintf(struct engines_data *e, const char *fmt, ...);
  3545. #ifdef CONFIG_DEBUG_FS
  3546. void hl_debugfs_init(void);
  3547. void hl_debugfs_fini(void);
  3548. void hl_debugfs_add_device(struct hl_device *hdev);
  3549. void hl_debugfs_remove_device(struct hl_device *hdev);
  3550. void hl_debugfs_add_file(struct hl_fpriv *hpriv);
  3551. void hl_debugfs_remove_file(struct hl_fpriv *hpriv);
  3552. void hl_debugfs_add_cb(struct hl_cb *cb);
  3553. void hl_debugfs_remove_cb(struct hl_cb *cb);
  3554. void hl_debugfs_add_cs(struct hl_cs *cs);
  3555. void hl_debugfs_remove_cs(struct hl_cs *cs);
  3556. void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job);
  3557. void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job);
  3558. void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr);
  3559. void hl_debugfs_remove_userptr(struct hl_device *hdev,
  3560. struct hl_userptr *userptr);
  3561. void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
  3562. void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx);
  3563. void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
  3564. unsigned long length);
  3565. #else
  3566. static inline void __init hl_debugfs_init(void)
  3567. {
  3568. }
  3569. static inline void hl_debugfs_fini(void)
  3570. {
  3571. }
  3572. static inline void hl_debugfs_add_device(struct hl_device *hdev)
  3573. {
  3574. }
  3575. static inline void hl_debugfs_remove_device(struct hl_device *hdev)
  3576. {
  3577. }
  3578. static inline void hl_debugfs_add_file(struct hl_fpriv *hpriv)
  3579. {
  3580. }
  3581. static inline void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
  3582. {
  3583. }
  3584. static inline void hl_debugfs_add_cb(struct hl_cb *cb)
  3585. {
  3586. }
  3587. static inline void hl_debugfs_remove_cb(struct hl_cb *cb)
  3588. {
  3589. }
  3590. static inline void hl_debugfs_add_cs(struct hl_cs *cs)
  3591. {
  3592. }
  3593. static inline void hl_debugfs_remove_cs(struct hl_cs *cs)
  3594. {
  3595. }
  3596. static inline void hl_debugfs_add_job(struct hl_device *hdev,
  3597. struct hl_cs_job *job)
  3598. {
  3599. }
  3600. static inline void hl_debugfs_remove_job(struct hl_device *hdev,
  3601. struct hl_cs_job *job)
  3602. {
  3603. }
  3604. static inline void hl_debugfs_add_userptr(struct hl_device *hdev,
  3605. struct hl_userptr *userptr)
  3606. {
  3607. }
  3608. static inline void hl_debugfs_remove_userptr(struct hl_device *hdev,
  3609. struct hl_userptr *userptr)
  3610. {
  3611. }
  3612. static inline void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev,
  3613. struct hl_ctx *ctx)
  3614. {
  3615. }
  3616. static inline void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev,
  3617. struct hl_ctx *ctx)
  3618. {
  3619. }
  3620. static inline void hl_debugfs_set_state_dump(struct hl_device *hdev,
  3621. char *data, unsigned long length)
  3622. {
  3623. }
  3624. #endif
  3625. /* Security */
  3626. int hl_unsecure_register(struct hl_device *hdev, u32 mm_reg_addr, int offset,
  3627. const u32 pb_blocks[], struct hl_block_glbl_sec sgs_array[],
  3628. int array_size);
  3629. int hl_unsecure_registers(struct hl_device *hdev, const u32 mm_reg_array[],
  3630. int mm_array_size, int offset, const u32 pb_blocks[],
  3631. struct hl_block_glbl_sec sgs_array[], int blocks_array_size);
  3632. void hl_config_glbl_sec(struct hl_device *hdev, const u32 pb_blocks[],
  3633. struct hl_block_glbl_sec sgs_array[], u32 block_offset,
  3634. int array_size);
  3635. void hl_secure_block(struct hl_device *hdev,
  3636. struct hl_block_glbl_sec sgs_array[], int array_size);
  3637. int hl_init_pb_with_mask(struct hl_device *hdev, u32 num_dcores,
  3638. u32 dcore_offset, u32 num_instances, u32 instance_offset,
  3639. const u32 pb_blocks[], u32 blocks_array_size,
  3640. const u32 *regs_array, u32 regs_array_size, u64 mask);
  3641. int hl_init_pb(struct hl_device *hdev, u32 num_dcores, u32 dcore_offset,
  3642. u32 num_instances, u32 instance_offset,
  3643. const u32 pb_blocks[], u32 blocks_array_size,
  3644. const u32 *regs_array, u32 regs_array_size);
  3645. int hl_init_pb_ranges_with_mask(struct hl_device *hdev, u32 num_dcores,
  3646. u32 dcore_offset, u32 num_instances, u32 instance_offset,
  3647. const u32 pb_blocks[], u32 blocks_array_size,
  3648. const struct range *regs_range_array, u32 regs_range_array_size,
  3649. u64 mask);
  3650. int hl_init_pb_ranges(struct hl_device *hdev, u32 num_dcores,
  3651. u32 dcore_offset, u32 num_instances, u32 instance_offset,
  3652. const u32 pb_blocks[], u32 blocks_array_size,
  3653. const struct range *regs_range_array,
  3654. u32 regs_range_array_size);
  3655. int hl_init_pb_single_dcore(struct hl_device *hdev, u32 dcore_offset,
  3656. u32 num_instances, u32 instance_offset,
  3657. const u32 pb_blocks[], u32 blocks_array_size,
  3658. const u32 *regs_array, u32 regs_array_size);
  3659. int hl_init_pb_ranges_single_dcore(struct hl_device *hdev, u32 dcore_offset,
  3660. u32 num_instances, u32 instance_offset,
  3661. const u32 pb_blocks[], u32 blocks_array_size,
  3662. const struct range *regs_range_array,
  3663. u32 regs_range_array_size);
  3664. void hl_ack_pb(struct hl_device *hdev, u32 num_dcores, u32 dcore_offset,
  3665. u32 num_instances, u32 instance_offset,
  3666. const u32 pb_blocks[], u32 blocks_array_size);
  3667. void hl_ack_pb_with_mask(struct hl_device *hdev, u32 num_dcores,
  3668. u32 dcore_offset, u32 num_instances, u32 instance_offset,
  3669. const u32 pb_blocks[], u32 blocks_array_size, u64 mask);
  3670. void hl_ack_pb_single_dcore(struct hl_device *hdev, u32 dcore_offset,
  3671. u32 num_instances, u32 instance_offset,
  3672. const u32 pb_blocks[], u32 blocks_array_size);
  3673. /* IOCTLs */
  3674. long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
  3675. long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg);
  3676. int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data);
  3677. int hl_cs_ioctl(struct hl_fpriv *hpriv, void *data);
  3678. int hl_wait_ioctl(struct hl_fpriv *hpriv, void *data);
  3679. int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data);
  3680. #endif /* HABANALABSP_H_ */