iommu.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  4. * Author: Joerg Roedel <jroedel@suse.de>
  5. * Leo Duran <leo.duran@amd.com>
  6. */
  7. #define pr_fmt(fmt) "AMD-Vi: " fmt
  8. #define dev_fmt(fmt) pr_fmt(fmt)
  9. #include <linux/ratelimit.h>
  10. #include <linux/pci.h>
  11. #include <linux/acpi.h>
  12. #include <linux/pci-ats.h>
  13. #include <linux/bitmap.h>
  14. #include <linux/slab.h>
  15. #include <linux/string_choices.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/scatterlist.h>
  18. #include <linux/dma-map-ops.h>
  19. #include <linux/dma-direct.h>
  20. #include <linux/idr.h>
  21. #include <linux/iommu-helper.h>
  22. #include <linux/delay.h>
  23. #include <linux/amd-iommu.h>
  24. #include <linux/notifier.h>
  25. #include <linux/export.h>
  26. #include <linux/irq.h>
  27. #include <linux/irqchip/irq-msi-lib.h>
  28. #include <linux/msi.h>
  29. #include <linux/irqdomain.h>
  30. #include <linux/percpu.h>
  31. #include <linux/cc_platform.h>
  32. #include <asm/irq_remapping.h>
  33. #include <asm/io_apic.h>
  34. #include <asm/apic.h>
  35. #include <asm/hw_irq.h>
  36. #include <asm/proto.h>
  37. #include <asm/iommu.h>
  38. #include <asm/gart.h>
  39. #include <asm/dma.h>
  40. #include <uapi/linux/iommufd.h>
  41. #include <linux/generic_pt/iommu.h>
  42. #include "amd_iommu.h"
  43. #include "iommufd.h"
  44. #include "../irq_remapping.h"
  45. #include "../iommu-pages.h"
  46. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  47. /* Reserved IOVA ranges */
  48. #define MSI_RANGE_START (0xfee00000)
  49. #define MSI_RANGE_END (0xfeefffff)
  50. #define HT_RANGE_START (0xfd00000000ULL)
  51. #define HT_RANGE_END (0xffffffffffULL)
  52. LIST_HEAD(ioapic_map);
  53. LIST_HEAD(hpet_map);
  54. LIST_HEAD(acpihid_map);
  55. const struct iommu_ops amd_iommu_ops;
  56. int amd_iommu_max_glx_val = -1;
  57. /*
  58. * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
  59. * to know which ones are already in use.
  60. */
  61. DEFINE_IDA(pdom_ids);
  62. static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev,
  63. struct iommu_domain *old);
  64. static void set_dte_entry(struct amd_iommu *iommu,
  65. struct iommu_dev_data *dev_data,
  66. phys_addr_t top_paddr, unsigned int top_level);
  67. static int device_flush_dte(struct iommu_dev_data *dev_data);
  68. static void amd_iommu_change_top(struct pt_iommu *iommu_table,
  69. phys_addr_t top_paddr, unsigned int top_level);
  70. static void iommu_flush_dte_sync(struct amd_iommu *iommu, u16 devid);
  71. static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid);
  72. static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain);
  73. static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain,
  74. bool enable);
  75. static void clone_aliases(struct amd_iommu *iommu, struct device *dev);
  76. static int iommu_completion_wait(struct amd_iommu *iommu);
  77. /****************************************************************************
  78. *
  79. * Helper functions
  80. *
  81. ****************************************************************************/
  82. static __always_inline void amd_iommu_atomic128_set(__int128 *ptr, __int128 val)
  83. {
  84. /*
  85. * Note:
  86. * We use arch_cmpxchg128_local() because:
  87. * - Need cmpxchg16b instruction mainly for 128-bit store to DTE
  88. * (not necessary for cmpxchg since this function is already
  89. * protected by a spin_lock for this DTE).
  90. * - Neither need LOCK_PREFIX nor try loop because of the spin_lock.
  91. */
  92. arch_cmpxchg128_local(ptr, *ptr, val);
  93. }
  94. static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_entry *new)
  95. {
  96. struct dev_table_entry old;
  97. old.data128[1] = ptr->data128[1];
  98. /*
  99. * Preserve DTE_DATA2_INTR_MASK. This needs to be
  100. * done here since it requires to be inside
  101. * spin_lock(&dev_data->dte_lock) context.
  102. */
  103. new->data[2] &= ~DTE_DATA2_INTR_MASK;
  104. new->data[2] |= old.data[2] & DTE_DATA2_INTR_MASK;
  105. amd_iommu_atomic128_set(&ptr->data128[1], new->data128[1]);
  106. }
  107. static void write_dte_lower128(struct dev_table_entry *ptr, struct dev_table_entry *new)
  108. {
  109. amd_iommu_atomic128_set(&ptr->data128[0], new->data128[0]);
  110. }
  111. /*
  112. * Note:
  113. * IOMMU reads the entire Device Table entry in a single 256-bit transaction
  114. * but the driver is programming DTE using 2 128-bit cmpxchg. So, the driver
  115. * need to ensure the following:
  116. * - DTE[V|GV] bit is being written last when setting.
  117. * - DTE[V|GV] bit is being written first when clearing.
  118. *
  119. * This function is used only by code, which updates DMA translation part of the DTE.
  120. * So, only consider control bits related to DMA when updating the entry.
  121. */
  122. static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data,
  123. struct dev_table_entry *new)
  124. {
  125. unsigned long flags;
  126. struct dev_table_entry *dev_table = get_dev_table(iommu);
  127. struct dev_table_entry *ptr = &dev_table[dev_data->devid];
  128. spin_lock_irqsave(&dev_data->dte_lock, flags);
  129. if (!(ptr->data[0] & DTE_FLAG_V)) {
  130. /* Existing DTE is not valid. */
  131. write_dte_upper128(ptr, new);
  132. write_dte_lower128(ptr, new);
  133. iommu_flush_dte_sync(iommu, dev_data->devid);
  134. } else if (!(new->data[0] & DTE_FLAG_V)) {
  135. /* Existing DTE is valid. New DTE is not valid. */
  136. write_dte_lower128(ptr, new);
  137. write_dte_upper128(ptr, new);
  138. iommu_flush_dte_sync(iommu, dev_data->devid);
  139. } else if (!FIELD_GET(DTE_FLAG_GV, ptr->data[0])) {
  140. /*
  141. * Both DTEs are valid.
  142. * Existing DTE has no guest page table.
  143. */
  144. write_dte_upper128(ptr, new);
  145. write_dte_lower128(ptr, new);
  146. iommu_flush_dte_sync(iommu, dev_data->devid);
  147. } else if (!FIELD_GET(DTE_FLAG_GV, new->data[0])) {
  148. /*
  149. * Both DTEs are valid.
  150. * Existing DTE has guest page table,
  151. * new DTE has no guest page table,
  152. */
  153. write_dte_lower128(ptr, new);
  154. write_dte_upper128(ptr, new);
  155. iommu_flush_dte_sync(iommu, dev_data->devid);
  156. } else if (FIELD_GET(DTE_GPT_LEVEL_MASK, ptr->data[2]) !=
  157. FIELD_GET(DTE_GPT_LEVEL_MASK, new->data[2])) {
  158. /*
  159. * Both DTEs are valid and have guest page table,
  160. * but have different number of levels. So, we need
  161. * to upadte both upper and lower 128-bit value, which
  162. * require disabling and flushing.
  163. */
  164. struct dev_table_entry clear = {};
  165. /* First disable DTE */
  166. write_dte_lower128(ptr, &clear);
  167. iommu_flush_dte_sync(iommu, dev_data->devid);
  168. /* Then update DTE */
  169. write_dte_upper128(ptr, new);
  170. write_dte_lower128(ptr, new);
  171. iommu_flush_dte_sync(iommu, dev_data->devid);
  172. } else {
  173. /*
  174. * Both DTEs are valid and have guest page table,
  175. * and same number of levels. We just need to only
  176. * update the lower 128-bit. So no need to disable DTE.
  177. */
  178. write_dte_lower128(ptr, new);
  179. }
  180. spin_unlock_irqrestore(&dev_data->dte_lock, flags);
  181. }
  182. void amd_iommu_update_dte(struct amd_iommu *iommu,
  183. struct iommu_dev_data *dev_data,
  184. struct dev_table_entry *new)
  185. {
  186. update_dte256(iommu, dev_data, new);
  187. clone_aliases(iommu, dev_data->dev);
  188. device_flush_dte(dev_data);
  189. iommu_completion_wait(iommu);
  190. }
  191. static void get_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data,
  192. struct dev_table_entry *dte)
  193. {
  194. unsigned long flags;
  195. struct dev_table_entry *ptr;
  196. struct dev_table_entry *dev_table = get_dev_table(iommu);
  197. ptr = &dev_table[dev_data->devid];
  198. spin_lock_irqsave(&dev_data->dte_lock, flags);
  199. dte->data128[0] = ptr->data128[0];
  200. dte->data128[1] = ptr->data128[1];
  201. spin_unlock_irqrestore(&dev_data->dte_lock, flags);
  202. }
  203. static inline bool pdom_is_v2_pgtbl_mode(struct protection_domain *pdom)
  204. {
  205. return (pdom && (pdom->pd_mode == PD_MODE_V2));
  206. }
  207. static inline bool pdom_is_in_pt_mode(struct protection_domain *pdom)
  208. {
  209. return (pdom->domain.type == IOMMU_DOMAIN_IDENTITY);
  210. }
  211. /*
  212. * We cannot support PASID w/ existing v1 page table in the same domain
  213. * since it will be nested. However, existing domain w/ v2 page table
  214. * or passthrough mode can be used for PASID.
  215. */
  216. static inline bool pdom_is_sva_capable(struct protection_domain *pdom)
  217. {
  218. return pdom_is_v2_pgtbl_mode(pdom) || pdom_is_in_pt_mode(pdom);
  219. }
  220. static inline int get_acpihid_device_id(struct device *dev,
  221. struct acpihid_map_entry **entry)
  222. {
  223. struct acpi_device *adev = ACPI_COMPANION(dev);
  224. struct acpihid_map_entry *p, *p1 = NULL;
  225. int hid_count = 0;
  226. bool fw_bug;
  227. if (!adev)
  228. return -ENODEV;
  229. list_for_each_entry(p, &acpihid_map, list) {
  230. if (acpi_dev_hid_uid_match(adev, p->hid,
  231. p->uid[0] ? p->uid : NULL)) {
  232. p1 = p;
  233. fw_bug = false;
  234. hid_count = 1;
  235. break;
  236. }
  237. /*
  238. * Count HID matches w/o UID, raise FW_BUG but allow exactly one match
  239. */
  240. if (acpi_dev_hid_match(adev, p->hid)) {
  241. p1 = p;
  242. hid_count++;
  243. fw_bug = true;
  244. }
  245. }
  246. if (!p1)
  247. return -EINVAL;
  248. if (fw_bug)
  249. dev_err_once(dev, FW_BUG "No ACPI device matched UID, but %d device%s matched HID.\n",
  250. hid_count, str_plural(hid_count));
  251. if (hid_count > 1)
  252. return -EINVAL;
  253. if (entry)
  254. *entry = p1;
  255. return p1->devid;
  256. }
  257. static inline int get_device_sbdf_id(struct device *dev)
  258. {
  259. int sbdf;
  260. if (dev_is_pci(dev))
  261. sbdf = get_pci_sbdf_id(to_pci_dev(dev));
  262. else
  263. sbdf = get_acpihid_device_id(dev, NULL);
  264. return sbdf;
  265. }
  266. struct dev_table_entry *get_dev_table(struct amd_iommu *iommu)
  267. {
  268. struct dev_table_entry *dev_table;
  269. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  270. BUG_ON(pci_seg == NULL);
  271. dev_table = pci_seg->dev_table;
  272. BUG_ON(dev_table == NULL);
  273. return dev_table;
  274. }
  275. static inline u16 get_device_segment(struct device *dev)
  276. {
  277. u16 seg;
  278. if (dev_is_pci(dev)) {
  279. struct pci_dev *pdev = to_pci_dev(dev);
  280. seg = pci_domain_nr(pdev->bus);
  281. } else {
  282. u32 devid = get_acpihid_device_id(dev, NULL);
  283. seg = PCI_SBDF_TO_SEGID(devid);
  284. }
  285. return seg;
  286. }
  287. /* Writes the specific IOMMU for a device into the PCI segment rlookup table */
  288. void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid)
  289. {
  290. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  291. pci_seg->rlookup_table[devid] = iommu;
  292. }
  293. static struct amd_iommu *__rlookup_amd_iommu(u16 seg, u16 devid)
  294. {
  295. struct amd_iommu_pci_seg *pci_seg;
  296. for_each_pci_segment(pci_seg) {
  297. if (pci_seg->id == seg)
  298. return pci_seg->rlookup_table[devid];
  299. }
  300. return NULL;
  301. }
  302. static struct amd_iommu *rlookup_amd_iommu(struct device *dev)
  303. {
  304. u16 seg = get_device_segment(dev);
  305. int devid = get_device_sbdf_id(dev);
  306. if (devid < 0)
  307. return NULL;
  308. return __rlookup_amd_iommu(seg, PCI_SBDF_TO_DEVID(devid));
  309. }
  310. static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid)
  311. {
  312. struct iommu_dev_data *dev_data;
  313. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  314. dev_data = kzalloc_obj(*dev_data);
  315. if (!dev_data)
  316. return NULL;
  317. mutex_init(&dev_data->mutex);
  318. spin_lock_init(&dev_data->dte_lock);
  319. dev_data->devid = devid;
  320. ratelimit_default_init(&dev_data->rs);
  321. llist_add(&dev_data->dev_data_list, &pci_seg->dev_data_list);
  322. return dev_data;
  323. }
  324. struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid)
  325. {
  326. struct iommu_dev_data *dev_data;
  327. struct llist_node *node;
  328. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  329. if (llist_empty(&pci_seg->dev_data_list))
  330. return NULL;
  331. node = pci_seg->dev_data_list.first;
  332. llist_for_each_entry(dev_data, node, dev_data_list) {
  333. if (dev_data->devid == devid)
  334. return dev_data;
  335. }
  336. return NULL;
  337. }
  338. static int clone_alias(struct pci_dev *pdev, u16 alias, void *data)
  339. {
  340. struct dev_table_entry new;
  341. struct amd_iommu *iommu;
  342. struct iommu_dev_data *dev_data, *alias_data;
  343. u16 devid = pci_dev_id(pdev);
  344. int ret = 0;
  345. if (devid == alias)
  346. return 0;
  347. iommu = rlookup_amd_iommu(&pdev->dev);
  348. if (!iommu)
  349. return 0;
  350. /* Copy the data from pdev */
  351. dev_data = dev_iommu_priv_get(&pdev->dev);
  352. if (!dev_data) {
  353. pr_err("%s : Failed to get dev_data for 0x%x\n", __func__, devid);
  354. ret = -EINVAL;
  355. goto out;
  356. }
  357. get_dte256(iommu, dev_data, &new);
  358. /* Setup alias */
  359. alias_data = find_dev_data(iommu, alias);
  360. if (!alias_data) {
  361. pr_err("%s : Failed to get alias dev_data for 0x%x\n", __func__, alias);
  362. ret = -EINVAL;
  363. goto out;
  364. }
  365. update_dte256(iommu, alias_data, &new);
  366. amd_iommu_set_rlookup_table(iommu, alias);
  367. out:
  368. return ret;
  369. }
  370. static void clone_aliases(struct amd_iommu *iommu, struct device *dev)
  371. {
  372. struct pci_dev *pdev;
  373. if (!dev_is_pci(dev))
  374. return;
  375. pdev = to_pci_dev(dev);
  376. /*
  377. * The IVRS alias stored in the alias table may not be
  378. * part of the PCI DMA aliases if it's bus differs
  379. * from the original device.
  380. */
  381. clone_alias(pdev, iommu->pci_seg->alias_table[pci_dev_id(pdev)], NULL);
  382. pci_for_each_dma_alias(pdev, clone_alias, NULL);
  383. }
  384. static void setup_aliases(struct amd_iommu *iommu, struct device *dev)
  385. {
  386. struct pci_dev *pdev = to_pci_dev(dev);
  387. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  388. u16 ivrs_alias;
  389. /* For ACPI HID devices, there are no aliases */
  390. if (!dev_is_pci(dev))
  391. return;
  392. /*
  393. * Add the IVRS alias to the pci aliases if it is on the same
  394. * bus. The IVRS table may know about a quirk that we don't.
  395. */
  396. ivrs_alias = pci_seg->alias_table[pci_dev_id(pdev)];
  397. if (ivrs_alias != pci_dev_id(pdev) &&
  398. PCI_BUS_NUM(ivrs_alias) == pdev->bus->number)
  399. pci_add_dma_alias(pdev, ivrs_alias & 0xff, 1);
  400. clone_aliases(iommu, dev);
  401. }
  402. static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid)
  403. {
  404. struct iommu_dev_data *dev_data;
  405. dev_data = search_dev_data(iommu, devid);
  406. if (dev_data == NULL) {
  407. dev_data = alloc_dev_data(iommu, devid);
  408. if (!dev_data)
  409. return NULL;
  410. if (translation_pre_enabled(iommu))
  411. dev_data->defer_attach = true;
  412. }
  413. return dev_data;
  414. }
  415. /*
  416. * Find or create an IOMMU group for a acpihid device.
  417. */
  418. static struct iommu_group *acpihid_device_group(struct device *dev)
  419. {
  420. struct acpihid_map_entry *p, *entry = NULL;
  421. int devid;
  422. devid = get_acpihid_device_id(dev, &entry);
  423. if (devid < 0)
  424. return ERR_PTR(devid);
  425. list_for_each_entry(p, &acpihid_map, list) {
  426. if ((devid == p->devid) && p->group)
  427. entry->group = p->group;
  428. }
  429. if (!entry->group)
  430. entry->group = generic_device_group(dev);
  431. else
  432. iommu_group_ref_get(entry->group);
  433. return entry->group;
  434. }
  435. static inline bool pdev_pasid_supported(struct iommu_dev_data *dev_data)
  436. {
  437. return (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP);
  438. }
  439. static u32 pdev_get_caps(struct pci_dev *pdev)
  440. {
  441. int features;
  442. u32 flags = 0;
  443. if (pci_ats_supported(pdev))
  444. flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
  445. if (pci_pri_supported(pdev))
  446. flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
  447. features = pci_pasid_features(pdev);
  448. if (features >= 0) {
  449. flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
  450. if (features & PCI_PASID_CAP_EXEC)
  451. flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
  452. if (features & PCI_PASID_CAP_PRIV)
  453. flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
  454. }
  455. return flags;
  456. }
  457. static inline int pdev_enable_cap_ats(struct pci_dev *pdev)
  458. {
  459. struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
  460. int ret = -EINVAL;
  461. if (dev_data->ats_enabled)
  462. return 0;
  463. if (amd_iommu_iotlb_sup &&
  464. (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP)) {
  465. ret = pci_enable_ats(pdev, PAGE_SHIFT);
  466. if (!ret) {
  467. dev_data->ats_enabled = 1;
  468. dev_data->ats_qdep = pci_ats_queue_depth(pdev);
  469. }
  470. }
  471. return ret;
  472. }
  473. static inline void pdev_disable_cap_ats(struct pci_dev *pdev)
  474. {
  475. struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
  476. if (dev_data->ats_enabled) {
  477. pci_disable_ats(pdev);
  478. dev_data->ats_enabled = 0;
  479. }
  480. }
  481. static inline int pdev_enable_cap_pri(struct pci_dev *pdev)
  482. {
  483. struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
  484. int ret = -EINVAL;
  485. if (dev_data->pri_enabled)
  486. return 0;
  487. if (!dev_data->ats_enabled)
  488. return 0;
  489. if (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) {
  490. /*
  491. * First reset the PRI state of the device.
  492. * FIXME: Hardcode number of outstanding requests for now
  493. */
  494. if (!pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32)) {
  495. dev_data->pri_enabled = 1;
  496. dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev);
  497. ret = 0;
  498. }
  499. }
  500. return ret;
  501. }
  502. static inline void pdev_disable_cap_pri(struct pci_dev *pdev)
  503. {
  504. struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
  505. if (dev_data->pri_enabled) {
  506. pci_disable_pri(pdev);
  507. dev_data->pri_enabled = 0;
  508. }
  509. }
  510. static inline int pdev_enable_cap_pasid(struct pci_dev *pdev)
  511. {
  512. struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
  513. int ret = -EINVAL;
  514. if (dev_data->pasid_enabled)
  515. return 0;
  516. if (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) {
  517. /* Only allow access to user-accessible pages */
  518. ret = pci_enable_pasid(pdev, 0);
  519. if (!ret)
  520. dev_data->pasid_enabled = 1;
  521. }
  522. return ret;
  523. }
  524. static inline void pdev_disable_cap_pasid(struct pci_dev *pdev)
  525. {
  526. struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev);
  527. if (dev_data->pasid_enabled) {
  528. pci_disable_pasid(pdev);
  529. dev_data->pasid_enabled = 0;
  530. }
  531. }
  532. static void pdev_enable_caps(struct pci_dev *pdev)
  533. {
  534. pdev_enable_cap_pasid(pdev);
  535. pdev_enable_cap_ats(pdev);
  536. pdev_enable_cap_pri(pdev);
  537. }
  538. static void pdev_disable_caps(struct pci_dev *pdev)
  539. {
  540. pdev_disable_cap_ats(pdev);
  541. pdev_disable_cap_pasid(pdev);
  542. pdev_disable_cap_pri(pdev);
  543. }
  544. /*
  545. * This function checks if the driver got a valid device from the caller to
  546. * avoid dereferencing invalid pointers.
  547. */
  548. static bool check_device(struct device *dev)
  549. {
  550. struct amd_iommu_pci_seg *pci_seg;
  551. struct amd_iommu *iommu;
  552. int devid, sbdf;
  553. if (!dev)
  554. return false;
  555. sbdf = get_device_sbdf_id(dev);
  556. if (sbdf < 0)
  557. return false;
  558. devid = PCI_SBDF_TO_DEVID(sbdf);
  559. iommu = rlookup_amd_iommu(dev);
  560. if (!iommu)
  561. return false;
  562. /* Out of our scope? */
  563. pci_seg = iommu->pci_seg;
  564. if (devid > pci_seg->last_bdf)
  565. return false;
  566. return true;
  567. }
  568. static int iommu_init_device(struct amd_iommu *iommu, struct device *dev)
  569. {
  570. struct iommu_dev_data *dev_data;
  571. int devid, sbdf;
  572. if (dev_iommu_priv_get(dev))
  573. return 0;
  574. sbdf = get_device_sbdf_id(dev);
  575. if (sbdf < 0)
  576. return sbdf;
  577. devid = PCI_SBDF_TO_DEVID(sbdf);
  578. dev_data = find_dev_data(iommu, devid);
  579. if (!dev_data)
  580. return -ENOMEM;
  581. dev_data->dev = dev;
  582. /*
  583. * The dev_iommu_priv_set() needes to be called before setup_aliases.
  584. * Otherwise, subsequent call to dev_iommu_priv_get() will fail.
  585. */
  586. dev_iommu_priv_set(dev, dev_data);
  587. setup_aliases(iommu, dev);
  588. /*
  589. * By default we use passthrough mode for IOMMUv2 capable device.
  590. * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
  591. * invalid address), we ignore the capability for the device so
  592. * it'll be forced to go into translation mode.
  593. */
  594. if ((iommu_default_passthrough() || !amd_iommu_force_isolation) &&
  595. dev_is_pci(dev) && amd_iommu_gt_ppr_supported()) {
  596. dev_data->flags = pdev_get_caps(to_pci_dev(dev));
  597. }
  598. return 0;
  599. }
  600. static void iommu_ignore_device(struct amd_iommu *iommu, struct device *dev)
  601. {
  602. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  603. struct dev_table_entry *dev_table = get_dev_table(iommu);
  604. int devid, sbdf;
  605. sbdf = get_device_sbdf_id(dev);
  606. if (sbdf < 0)
  607. return;
  608. devid = PCI_SBDF_TO_DEVID(sbdf);
  609. pci_seg->rlookup_table[devid] = NULL;
  610. memset(&dev_table[devid], 0, sizeof(struct dev_table_entry));
  611. setup_aliases(iommu, dev);
  612. }
  613. /****************************************************************************
  614. *
  615. * Interrupt handling functions
  616. *
  617. ****************************************************************************/
  618. static void dump_dte_entry(struct amd_iommu *iommu, u16 devid)
  619. {
  620. int i;
  621. struct dev_table_entry dte;
  622. struct iommu_dev_data *dev_data = find_dev_data(iommu, devid);
  623. get_dte256(iommu, dev_data, &dte);
  624. for (i = 0; i < 4; ++i)
  625. pr_err("DTE[%d]: %016llx\n", i, dte.data[i]);
  626. }
  627. static void dump_command(unsigned long phys_addr)
  628. {
  629. struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
  630. int i;
  631. for (i = 0; i < 4; ++i)
  632. pr_err("CMD[%d]: %08x\n", i, cmd->data[i]);
  633. }
  634. static void amd_iommu_report_rmp_hw_error(struct amd_iommu *iommu, volatile u32 *event)
  635. {
  636. struct iommu_dev_data *dev_data = NULL;
  637. int devid, vmg_tag, flags;
  638. struct pci_dev *pdev;
  639. u64 spa;
  640. devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  641. vmg_tag = (event[1]) & 0xFFFF;
  642. flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  643. spa = ((u64)event[3] << 32) | (event[2] & 0xFFFFFFF8);
  644. pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
  645. devid & 0xff);
  646. if (pdev)
  647. dev_data = dev_iommu_priv_get(&pdev->dev);
  648. if (dev_data) {
  649. if (__ratelimit(&dev_data->rs)) {
  650. pci_err(pdev, "Event logged [RMP_HW_ERROR vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
  651. vmg_tag, spa, flags);
  652. }
  653. } else {
  654. pr_err_ratelimited("Event logged [RMP_HW_ERROR device=%04x:%02x:%02x.%x, vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n",
  655. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  656. vmg_tag, spa, flags);
  657. }
  658. if (pdev)
  659. pci_dev_put(pdev);
  660. }
  661. static void amd_iommu_report_rmp_fault(struct amd_iommu *iommu, volatile u32 *event)
  662. {
  663. struct iommu_dev_data *dev_data = NULL;
  664. int devid, flags_rmp, vmg_tag, flags;
  665. struct pci_dev *pdev;
  666. u64 gpa;
  667. devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  668. flags_rmp = (event[0] >> EVENT_FLAGS_SHIFT) & 0xFF;
  669. vmg_tag = (event[1]) & 0xFFFF;
  670. flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  671. gpa = ((u64)event[3] << 32) | event[2];
  672. pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
  673. devid & 0xff);
  674. if (pdev)
  675. dev_data = dev_iommu_priv_get(&pdev->dev);
  676. if (dev_data) {
  677. if (__ratelimit(&dev_data->rs)) {
  678. pci_err(pdev, "Event logged [RMP_PAGE_FAULT vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
  679. vmg_tag, gpa, flags_rmp, flags);
  680. }
  681. } else {
  682. pr_err_ratelimited("Event logged [RMP_PAGE_FAULT device=%04x:%02x:%02x.%x, vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n",
  683. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  684. vmg_tag, gpa, flags_rmp, flags);
  685. }
  686. if (pdev)
  687. pci_dev_put(pdev);
  688. }
  689. #define IS_IOMMU_MEM_TRANSACTION(flags) \
  690. (((flags) & EVENT_FLAG_I) == 0)
  691. #define IS_WRITE_REQUEST(flags) \
  692. ((flags) & EVENT_FLAG_RW)
  693. static void amd_iommu_report_page_fault(struct amd_iommu *iommu,
  694. u16 devid, u16 domain_id,
  695. u64 address, int flags)
  696. {
  697. struct iommu_dev_data *dev_data = NULL;
  698. struct pci_dev *pdev;
  699. pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid),
  700. devid & 0xff);
  701. if (pdev)
  702. dev_data = dev_iommu_priv_get(&pdev->dev);
  703. if (dev_data) {
  704. /*
  705. * If this is a DMA fault (for which the I(nterrupt)
  706. * bit will be unset), allow report_iommu_fault() to
  707. * prevent logging it.
  708. */
  709. if (IS_IOMMU_MEM_TRANSACTION(flags)) {
  710. /* Device not attached to domain properly */
  711. if (dev_data->domain == NULL) {
  712. pr_err_ratelimited("Event logged [Device not attached to domain properly]\n");
  713. pr_err_ratelimited(" device=%04x:%02x:%02x.%x domain=0x%04x\n",
  714. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid),
  715. PCI_FUNC(devid), domain_id);
  716. goto out;
  717. }
  718. if (!report_iommu_fault(&dev_data->domain->domain,
  719. &pdev->dev, address,
  720. IS_WRITE_REQUEST(flags) ?
  721. IOMMU_FAULT_WRITE :
  722. IOMMU_FAULT_READ))
  723. goto out;
  724. }
  725. if (__ratelimit(&dev_data->rs)) {
  726. pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n",
  727. domain_id, address, flags);
  728. }
  729. } else {
  730. pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n",
  731. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  732. domain_id, address, flags);
  733. }
  734. out:
  735. if (pdev)
  736. pci_dev_put(pdev);
  737. }
  738. static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  739. {
  740. struct device *dev = iommu->iommu.dev;
  741. int type, devid, flags, tag;
  742. volatile u32 *event = __evt;
  743. int count = 0;
  744. u64 address, ctrl;
  745. u32 pasid;
  746. retry:
  747. type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  748. devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  749. pasid = (event[0] & EVENT_DOMID_MASK_HI) |
  750. (event[1] & EVENT_DOMID_MASK_LO);
  751. flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  752. address = (u64)(((u64)event[3]) << 32) | event[2];
  753. ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
  754. if (type == 0) {
  755. /* Did we hit the erratum? */
  756. if (++count == LOOP_TIMEOUT) {
  757. pr_err("No event written to event log\n");
  758. return;
  759. }
  760. udelay(1);
  761. goto retry;
  762. }
  763. if (type == EVENT_TYPE_IO_FAULT) {
  764. amd_iommu_report_page_fault(iommu, devid, pasid, address, flags);
  765. return;
  766. }
  767. switch (type) {
  768. case EVENT_TYPE_ILL_DEV:
  769. dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
  770. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  771. pasid, address, flags);
  772. dev_err(dev, "Control Reg : 0x%llx\n", ctrl);
  773. dump_dte_entry(iommu, devid);
  774. break;
  775. case EVENT_TYPE_DEV_TAB_ERR:
  776. dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x "
  777. "address=0x%llx flags=0x%04x]\n",
  778. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  779. address, flags);
  780. break;
  781. case EVENT_TYPE_PAGE_TAB_ERR:
  782. dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n",
  783. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  784. pasid, address, flags);
  785. break;
  786. case EVENT_TYPE_ILL_CMD:
  787. dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address);
  788. dump_command(address);
  789. break;
  790. case EVENT_TYPE_CMD_HARD_ERR:
  791. dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n",
  792. address, flags);
  793. break;
  794. case EVENT_TYPE_IOTLB_INV_TO:
  795. dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%04x:%02x:%02x.%x address=0x%llx]\n",
  796. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  797. address);
  798. break;
  799. case EVENT_TYPE_INV_DEV_REQ:
  800. dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n",
  801. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  802. pasid, address, flags);
  803. break;
  804. case EVENT_TYPE_RMP_FAULT:
  805. amd_iommu_report_rmp_fault(iommu, event);
  806. break;
  807. case EVENT_TYPE_RMP_HW_ERR:
  808. amd_iommu_report_rmp_hw_error(iommu, event);
  809. break;
  810. case EVENT_TYPE_INV_PPR_REQ:
  811. pasid = PPR_PASID(*((u64 *)__evt));
  812. tag = event[1] & 0x03FF;
  813. dev_err(dev, "Event logged [INVALID_PPR_REQUEST device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x tag=0x%03x]\n",
  814. iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  815. pasid, address, flags, tag);
  816. break;
  817. default:
  818. dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
  819. event[0], event[1], event[2], event[3]);
  820. }
  821. /*
  822. * To detect the hardware errata 732 we need to clear the
  823. * entry back to zero. This issue does not exist on SNP
  824. * enabled system. Also this buffer is not writeable on
  825. * SNP enabled system.
  826. */
  827. if (!amd_iommu_snp_en)
  828. memset(__evt, 0, 4 * sizeof(u32));
  829. }
  830. static void iommu_poll_events(struct amd_iommu *iommu)
  831. {
  832. u32 head, tail;
  833. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  834. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  835. while (head != tail) {
  836. iommu_print_event(iommu, iommu->evt_buf + head);
  837. /* Update head pointer of hardware ring-buffer */
  838. head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
  839. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  840. }
  841. }
  842. #ifdef CONFIG_IRQ_REMAP
  843. static int (*iommu_ga_log_notifier)(u32);
  844. int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
  845. {
  846. iommu_ga_log_notifier = notifier;
  847. /*
  848. * Ensure all in-flight IRQ handlers run to completion before returning
  849. * to the caller, e.g. to ensure module code isn't unloaded while it's
  850. * being executed in the IRQ handler.
  851. */
  852. if (!notifier)
  853. synchronize_rcu();
  854. return 0;
  855. }
  856. EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
  857. static void iommu_poll_ga_log(struct amd_iommu *iommu)
  858. {
  859. u32 head, tail;
  860. if (iommu->ga_log == NULL)
  861. return;
  862. head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
  863. tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
  864. while (head != tail) {
  865. volatile u64 *raw;
  866. u64 log_entry;
  867. raw = (u64 *)(iommu->ga_log + head);
  868. /* Avoid memcpy function-call overhead */
  869. log_entry = *raw;
  870. /* Update head pointer of hardware ring-buffer */
  871. head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
  872. writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
  873. /* Handle GA entry */
  874. switch (GA_REQ_TYPE(log_entry)) {
  875. case GA_GUEST_NR:
  876. if (!iommu_ga_log_notifier)
  877. break;
  878. pr_debug("%s: devid=%#x, ga_tag=%#x\n",
  879. __func__, GA_DEVID(log_entry),
  880. GA_TAG(log_entry));
  881. if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
  882. pr_err("GA log notifier failed.\n");
  883. break;
  884. default:
  885. break;
  886. }
  887. }
  888. }
  889. static void
  890. amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu)
  891. {
  892. if (!irq_remapping_enabled || !dev_is_pci(dev) ||
  893. !pci_dev_has_default_msi_parent_domain(to_pci_dev(dev)))
  894. return;
  895. dev_set_msi_domain(dev, iommu->ir_domain);
  896. }
  897. #else /* CONFIG_IRQ_REMAP */
  898. static inline void
  899. amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu) { }
  900. #endif /* !CONFIG_IRQ_REMAP */
  901. static void amd_iommu_handle_irq(void *data, const char *evt_type,
  902. u32 int_mask, u32 overflow_mask,
  903. void (*int_handler)(struct amd_iommu *),
  904. void (*overflow_handler)(struct amd_iommu *))
  905. {
  906. struct amd_iommu *iommu = (struct amd_iommu *) data;
  907. u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  908. u32 mask = int_mask | overflow_mask;
  909. while (status & mask) {
  910. /* Enable interrupt sources again */
  911. writel(mask, iommu->mmio_base + MMIO_STATUS_OFFSET);
  912. if (int_handler) {
  913. pr_devel("Processing IOMMU (ivhd%d) %s Log\n",
  914. iommu->index, evt_type);
  915. int_handler(iommu);
  916. }
  917. if ((status & overflow_mask) && overflow_handler)
  918. overflow_handler(iommu);
  919. /*
  920. * Hardware bug: ERBT1312
  921. * When re-enabling interrupt (by writing 1
  922. * to clear the bit), the hardware might also try to set
  923. * the interrupt bit in the event status register.
  924. * In this scenario, the bit will be set, and disable
  925. * subsequent interrupts.
  926. *
  927. * Workaround: The IOMMU driver should read back the
  928. * status register and check if the interrupt bits are cleared.
  929. * If not, driver will need to go through the interrupt handler
  930. * again and re-clear the bits
  931. */
  932. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  933. }
  934. }
  935. irqreturn_t amd_iommu_int_thread_evtlog(int irq, void *data)
  936. {
  937. amd_iommu_handle_irq(data, "Evt", MMIO_STATUS_EVT_INT_MASK,
  938. MMIO_STATUS_EVT_OVERFLOW_MASK,
  939. iommu_poll_events, amd_iommu_restart_event_logging);
  940. return IRQ_HANDLED;
  941. }
  942. irqreturn_t amd_iommu_int_thread_pprlog(int irq, void *data)
  943. {
  944. amd_iommu_handle_irq(data, "PPR", MMIO_STATUS_PPR_INT_MASK,
  945. MMIO_STATUS_PPR_OVERFLOW_MASK,
  946. amd_iommu_poll_ppr_log, amd_iommu_restart_ppr_log);
  947. return IRQ_HANDLED;
  948. }
  949. irqreturn_t amd_iommu_int_thread_galog(int irq, void *data)
  950. {
  951. #ifdef CONFIG_IRQ_REMAP
  952. amd_iommu_handle_irq(data, "GA", MMIO_STATUS_GALOG_INT_MASK,
  953. MMIO_STATUS_GALOG_OVERFLOW_MASK,
  954. iommu_poll_ga_log, amd_iommu_restart_ga_log);
  955. #endif
  956. return IRQ_HANDLED;
  957. }
  958. irqreturn_t amd_iommu_int_thread(int irq, void *data)
  959. {
  960. amd_iommu_int_thread_evtlog(irq, data);
  961. amd_iommu_int_thread_pprlog(irq, data);
  962. amd_iommu_int_thread_galog(irq, data);
  963. return IRQ_HANDLED;
  964. }
  965. /****************************************************************************
  966. *
  967. * IOMMU command queuing functions
  968. *
  969. ****************************************************************************/
  970. static void dump_command_buffer(struct amd_iommu *iommu)
  971. {
  972. struct iommu_cmd *cmd;
  973. u32 head, tail;
  974. int i;
  975. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  976. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  977. pr_err("CMD Buffer head=%llu tail=%llu\n", MMIO_CMD_BUFFER_HEAD(head),
  978. MMIO_CMD_BUFFER_TAIL(tail));
  979. for (i = 0; i < CMD_BUFFER_ENTRIES; i++) {
  980. cmd = (struct iommu_cmd *)(iommu->cmd_buf + i * sizeof(*cmd));
  981. pr_err("%3d: %08x %08x %08x %08x\n", i, cmd->data[0], cmd->data[1], cmd->data[2],
  982. cmd->data[3]);
  983. }
  984. }
  985. static int wait_on_sem(struct amd_iommu *iommu, u64 data)
  986. {
  987. int i = 0;
  988. /*
  989. * cmd_sem holds a monotonically non-decreasing completion sequence
  990. * number.
  991. */
  992. while ((__s64)(READ_ONCE(*iommu->cmd_sem) - data) < 0 &&
  993. i < LOOP_TIMEOUT) {
  994. udelay(1);
  995. i += 1;
  996. }
  997. if (i == LOOP_TIMEOUT) {
  998. pr_alert("IOMMU %04x:%02x:%02x.%01x: Completion-Wait loop timed out\n",
  999. iommu->pci_seg->id, PCI_BUS_NUM(iommu->devid),
  1000. PCI_SLOT(iommu->devid), PCI_FUNC(iommu->devid));
  1001. if (amd_iommu_dump)
  1002. DO_ONCE_LITE(dump_command_buffer, iommu);
  1003. return -EIO;
  1004. }
  1005. return 0;
  1006. }
  1007. static void copy_cmd_to_buffer(struct amd_iommu *iommu,
  1008. struct iommu_cmd *cmd)
  1009. {
  1010. u8 *target;
  1011. u32 tail;
  1012. /* Copy command to buffer */
  1013. tail = iommu->cmd_buf_tail;
  1014. target = iommu->cmd_buf + tail;
  1015. memcpy(target, cmd, sizeof(*cmd));
  1016. tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
  1017. iommu->cmd_buf_tail = tail;
  1018. /* Tell the IOMMU about it */
  1019. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  1020. }
  1021. static void build_completion_wait(struct iommu_cmd *cmd,
  1022. struct amd_iommu *iommu,
  1023. u64 data)
  1024. {
  1025. u64 paddr = iommu->cmd_sem_paddr;
  1026. memset(cmd, 0, sizeof(*cmd));
  1027. cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
  1028. cmd->data[1] = upper_32_bits(paddr);
  1029. cmd->data[2] = lower_32_bits(data);
  1030. cmd->data[3] = upper_32_bits(data);
  1031. CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
  1032. }
  1033. static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
  1034. {
  1035. memset(cmd, 0, sizeof(*cmd));
  1036. cmd->data[0] = devid;
  1037. CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
  1038. }
  1039. /*
  1040. * Builds an invalidation address which is suitable for one page or multiple
  1041. * pages. Sets the size bit (S) as needed is more than one page is flushed.
  1042. */
  1043. static inline u64 build_inv_address(u64 address, size_t size)
  1044. {
  1045. u64 pages, end, msb_diff;
  1046. pages = iommu_num_pages(address, size, PAGE_SIZE);
  1047. if (pages == 1)
  1048. return address & PAGE_MASK;
  1049. end = address + size - 1;
  1050. /*
  1051. * msb_diff would hold the index of the most significant bit that
  1052. * flipped between the start and end.
  1053. */
  1054. msb_diff = fls64(end ^ address) - 1;
  1055. /*
  1056. * Bits 63:52 are sign extended. If for some reason bit 51 is different
  1057. * between the start and the end, invalidate everything.
  1058. */
  1059. if (unlikely(msb_diff > 51)) {
  1060. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  1061. } else {
  1062. /*
  1063. * The msb-bit must be clear on the address. Just set all the
  1064. * lower bits.
  1065. */
  1066. address |= (1ull << msb_diff) - 1;
  1067. }
  1068. /* Clear bits 11:0 */
  1069. address &= PAGE_MASK;
  1070. /* Set the size bit - we flush more than one 4kb page */
  1071. return address | CMD_INV_IOMMU_PAGES_SIZE_MASK;
  1072. }
  1073. static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  1074. size_t size, u16 domid,
  1075. ioasid_t pasid, bool gn)
  1076. {
  1077. u64 inv_address = build_inv_address(address, size);
  1078. memset(cmd, 0, sizeof(*cmd));
  1079. cmd->data[1] |= domid;
  1080. cmd->data[2] = lower_32_bits(inv_address);
  1081. cmd->data[3] = upper_32_bits(inv_address);
  1082. /* PDE bit - we want to flush everything, not only the PTEs */
  1083. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  1084. if (gn) {
  1085. cmd->data[0] |= pasid;
  1086. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  1087. }
  1088. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  1089. }
  1090. static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
  1091. u64 address, size_t size,
  1092. ioasid_t pasid, bool gn)
  1093. {
  1094. u64 inv_address = build_inv_address(address, size);
  1095. memset(cmd, 0, sizeof(*cmd));
  1096. cmd->data[0] = devid;
  1097. cmd->data[0] |= (qdep & 0xff) << 24;
  1098. cmd->data[1] = devid;
  1099. cmd->data[2] = lower_32_bits(inv_address);
  1100. cmd->data[3] = upper_32_bits(inv_address);
  1101. if (gn) {
  1102. cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
  1103. cmd->data[1] |= (pasid & 0xff) << 16;
  1104. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  1105. }
  1106. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  1107. }
  1108. static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid,
  1109. int status, int tag, u8 gn)
  1110. {
  1111. memset(cmd, 0, sizeof(*cmd));
  1112. cmd->data[0] = devid;
  1113. if (gn) {
  1114. cmd->data[1] = pasid;
  1115. cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
  1116. }
  1117. cmd->data[3] = tag & 0x1ff;
  1118. cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
  1119. CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
  1120. }
  1121. static void build_inv_all(struct iommu_cmd *cmd)
  1122. {
  1123. memset(cmd, 0, sizeof(*cmd));
  1124. CMD_SET_TYPE(cmd, CMD_INV_ALL);
  1125. }
  1126. static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
  1127. {
  1128. memset(cmd, 0, sizeof(*cmd));
  1129. cmd->data[0] = devid;
  1130. CMD_SET_TYPE(cmd, CMD_INV_IRT);
  1131. }
  1132. /*
  1133. * Writes the command to the IOMMUs command buffer and informs the
  1134. * hardware about the new command.
  1135. */
  1136. static int __iommu_queue_command_sync(struct amd_iommu *iommu,
  1137. struct iommu_cmd *cmd,
  1138. bool sync)
  1139. {
  1140. unsigned int count = 0;
  1141. u32 left, next_tail;
  1142. next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
  1143. again:
  1144. left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
  1145. if (left <= 0x20) {
  1146. /* Skip udelay() the first time around */
  1147. if (count++) {
  1148. if (count == LOOP_TIMEOUT) {
  1149. pr_err("Command buffer timeout\n");
  1150. return -EIO;
  1151. }
  1152. udelay(1);
  1153. }
  1154. /* Update head and recheck remaining space */
  1155. iommu->cmd_buf_head = readl(iommu->mmio_base +
  1156. MMIO_CMD_HEAD_OFFSET);
  1157. goto again;
  1158. }
  1159. copy_cmd_to_buffer(iommu, cmd);
  1160. /* Do we need to make sure all commands are processed? */
  1161. iommu->need_sync = sync;
  1162. return 0;
  1163. }
  1164. static int iommu_queue_command_sync(struct amd_iommu *iommu,
  1165. struct iommu_cmd *cmd,
  1166. bool sync)
  1167. {
  1168. unsigned long flags;
  1169. int ret;
  1170. raw_spin_lock_irqsave(&iommu->lock, flags);
  1171. ret = __iommu_queue_command_sync(iommu, cmd, sync);
  1172. raw_spin_unlock_irqrestore(&iommu->lock, flags);
  1173. return ret;
  1174. }
  1175. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  1176. {
  1177. return iommu_queue_command_sync(iommu, cmd, true);
  1178. }
  1179. static u64 get_cmdsem_val(struct amd_iommu *iommu)
  1180. {
  1181. lockdep_assert_held(&iommu->lock);
  1182. return ++iommu->cmd_sem_val;
  1183. }
  1184. /*
  1185. * This function queues a completion wait command into the command
  1186. * buffer of an IOMMU
  1187. */
  1188. static int iommu_completion_wait(struct amd_iommu *iommu)
  1189. {
  1190. struct iommu_cmd cmd;
  1191. unsigned long flags;
  1192. int ret;
  1193. u64 data;
  1194. if (!iommu->need_sync)
  1195. return 0;
  1196. raw_spin_lock_irqsave(&iommu->lock, flags);
  1197. data = get_cmdsem_val(iommu);
  1198. build_completion_wait(&cmd, iommu, data);
  1199. ret = __iommu_queue_command_sync(iommu, &cmd, false);
  1200. raw_spin_unlock_irqrestore(&iommu->lock, flags);
  1201. if (ret)
  1202. return ret;
  1203. ret = wait_on_sem(iommu, data);
  1204. return ret;
  1205. }
  1206. static void domain_flush_complete(struct protection_domain *domain)
  1207. {
  1208. struct pdom_iommu_info *pdom_iommu_info;
  1209. unsigned long i;
  1210. lockdep_assert_held(&domain->lock);
  1211. /*
  1212. * Devices of this domain are behind this IOMMU
  1213. * We need to wait for completion of all commands.
  1214. */
  1215. xa_for_each(&domain->iommu_array, i, pdom_iommu_info)
  1216. iommu_completion_wait(pdom_iommu_info->iommu);
  1217. }
  1218. static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
  1219. {
  1220. struct iommu_cmd cmd;
  1221. build_inv_dte(&cmd, devid);
  1222. return iommu_queue_command(iommu, &cmd);
  1223. }
  1224. static void iommu_flush_dte_sync(struct amd_iommu *iommu, u16 devid)
  1225. {
  1226. int ret;
  1227. ret = iommu_flush_dte(iommu, devid);
  1228. if (!ret)
  1229. iommu_completion_wait(iommu);
  1230. }
  1231. static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
  1232. {
  1233. u32 devid;
  1234. u16 last_bdf = iommu->pci_seg->last_bdf;
  1235. for (devid = 0; devid <= last_bdf; ++devid)
  1236. iommu_flush_dte(iommu, devid);
  1237. iommu_completion_wait(iommu);
  1238. }
  1239. /*
  1240. * This function uses heavy locking and may disable irqs for some time. But
  1241. * this is no issue because it is only called during resume.
  1242. */
  1243. static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
  1244. {
  1245. u32 dom_id;
  1246. u16 last_bdf = iommu->pci_seg->last_bdf;
  1247. for (dom_id = 0; dom_id <= last_bdf; ++dom_id) {
  1248. struct iommu_cmd cmd;
  1249. build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  1250. dom_id, IOMMU_NO_PASID, false);
  1251. iommu_queue_command(iommu, &cmd);
  1252. }
  1253. iommu_completion_wait(iommu);
  1254. }
  1255. static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)
  1256. {
  1257. struct iommu_cmd cmd;
  1258. build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  1259. dom_id, IOMMU_NO_PASID, false);
  1260. iommu_queue_command(iommu, &cmd);
  1261. iommu_completion_wait(iommu);
  1262. }
  1263. static int iommu_flush_pages_v1_hdom_ids(struct protection_domain *pdom, u64 address, size_t size)
  1264. {
  1265. int ret = 0;
  1266. struct amd_iommu_viommu *aviommu;
  1267. list_for_each_entry(aviommu, &pdom->viommu_list, pdom_list) {
  1268. unsigned long i;
  1269. struct guest_domain_mapping_info *gdom_info;
  1270. struct amd_iommu *iommu = container_of(aviommu->core.iommu_dev,
  1271. struct amd_iommu, iommu);
  1272. xa_lock(&aviommu->gdomid_array);
  1273. xa_for_each(&aviommu->gdomid_array, i, gdom_info) {
  1274. struct iommu_cmd cmd;
  1275. pr_debug("%s: iommu=%#x, hdom_id=%#x\n", __func__,
  1276. iommu->devid, gdom_info->hdom_id);
  1277. build_inv_iommu_pages(&cmd, address, size, gdom_info->hdom_id,
  1278. IOMMU_NO_PASID, false);
  1279. ret |= iommu_queue_command(iommu, &cmd);
  1280. }
  1281. xa_unlock(&aviommu->gdomid_array);
  1282. }
  1283. return ret;
  1284. }
  1285. static void amd_iommu_flush_all(struct amd_iommu *iommu)
  1286. {
  1287. struct iommu_cmd cmd;
  1288. build_inv_all(&cmd);
  1289. iommu_queue_command(iommu, &cmd);
  1290. iommu_completion_wait(iommu);
  1291. }
  1292. static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
  1293. {
  1294. struct iommu_cmd cmd;
  1295. build_inv_irt(&cmd, devid);
  1296. iommu_queue_command(iommu, &cmd);
  1297. }
  1298. static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
  1299. {
  1300. u32 devid;
  1301. u16 last_bdf = iommu->pci_seg->last_bdf;
  1302. if (iommu->irtcachedis_enabled)
  1303. return;
  1304. for (devid = 0; devid <= last_bdf; devid++)
  1305. iommu_flush_irt(iommu, devid);
  1306. iommu_completion_wait(iommu);
  1307. }
  1308. void amd_iommu_flush_all_caches(struct amd_iommu *iommu)
  1309. {
  1310. if (check_feature(FEATURE_IA)) {
  1311. amd_iommu_flush_all(iommu);
  1312. } else {
  1313. amd_iommu_flush_dte_all(iommu);
  1314. amd_iommu_flush_irt_all(iommu);
  1315. amd_iommu_flush_tlb_all(iommu);
  1316. }
  1317. }
  1318. /*
  1319. * Command send function for flushing on-device TLB
  1320. */
  1321. static int device_flush_iotlb(struct iommu_dev_data *dev_data, u64 address,
  1322. size_t size, ioasid_t pasid, bool gn)
  1323. {
  1324. struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
  1325. struct iommu_cmd cmd;
  1326. int qdep = dev_data->ats_qdep;
  1327. build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address,
  1328. size, pasid, gn);
  1329. return iommu_queue_command(iommu, &cmd);
  1330. }
  1331. static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)
  1332. {
  1333. struct amd_iommu *iommu = data;
  1334. return iommu_flush_dte(iommu, alias);
  1335. }
  1336. /*
  1337. * Command send function for invalidating a device table entry
  1338. */
  1339. static int device_flush_dte(struct iommu_dev_data *dev_data)
  1340. {
  1341. struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
  1342. struct pci_dev *pdev = NULL;
  1343. struct amd_iommu_pci_seg *pci_seg;
  1344. u16 alias;
  1345. int ret;
  1346. if (dev_is_pci(dev_data->dev))
  1347. pdev = to_pci_dev(dev_data->dev);
  1348. if (pdev)
  1349. ret = pci_for_each_dma_alias(pdev,
  1350. device_flush_dte_alias, iommu);
  1351. else
  1352. ret = iommu_flush_dte(iommu, dev_data->devid);
  1353. if (ret)
  1354. return ret;
  1355. pci_seg = iommu->pci_seg;
  1356. alias = pci_seg->alias_table[dev_data->devid];
  1357. if (alias != dev_data->devid) {
  1358. ret = iommu_flush_dte(iommu, alias);
  1359. if (ret)
  1360. return ret;
  1361. }
  1362. if (dev_data->ats_enabled) {
  1363. /* Invalidate the entire contents of an IOTLB */
  1364. ret = device_flush_iotlb(dev_data, 0, ~0UL,
  1365. IOMMU_NO_PASID, false);
  1366. }
  1367. return ret;
  1368. }
  1369. static int domain_flush_pages_v2(struct protection_domain *pdom,
  1370. u64 address, size_t size)
  1371. {
  1372. struct iommu_dev_data *dev_data;
  1373. struct iommu_cmd cmd;
  1374. int ret = 0;
  1375. lockdep_assert_held(&pdom->lock);
  1376. list_for_each_entry(dev_data, &pdom->dev_list, list) {
  1377. struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev);
  1378. u16 domid = dev_data->gcr3_info.domid;
  1379. build_inv_iommu_pages(&cmd, address, size,
  1380. domid, IOMMU_NO_PASID, true);
  1381. ret |= iommu_queue_command(iommu, &cmd);
  1382. }
  1383. return ret;
  1384. }
  1385. static int domain_flush_pages_v1(struct protection_domain *pdom,
  1386. u64 address, size_t size)
  1387. {
  1388. struct pdom_iommu_info *pdom_iommu_info;
  1389. struct iommu_cmd cmd;
  1390. int ret = 0;
  1391. unsigned long i;
  1392. lockdep_assert_held(&pdom->lock);
  1393. build_inv_iommu_pages(&cmd, address, size,
  1394. pdom->id, IOMMU_NO_PASID, false);
  1395. xa_for_each(&pdom->iommu_array, i, pdom_iommu_info) {
  1396. /*
  1397. * Devices of this domain are behind this IOMMU
  1398. * We need a TLB flush
  1399. */
  1400. ret |= iommu_queue_command(pdom_iommu_info->iommu, &cmd);
  1401. }
  1402. /*
  1403. * A domain w/ v1 table can be a nest parent, which can have
  1404. * multiple nested domains. Each nested domain has 1:1 mapping
  1405. * between gDomID and hDomID. Therefore, flush every hDomID
  1406. * associated to this nest parent domain.
  1407. *
  1408. * See drivers/iommu/amd/nested.c: amd_iommu_alloc_domain_nested()
  1409. */
  1410. if (!list_empty(&pdom->viommu_list))
  1411. ret |= iommu_flush_pages_v1_hdom_ids(pdom, address, size);
  1412. return ret;
  1413. }
  1414. /*
  1415. * TLB invalidation function which is called from the mapping functions.
  1416. * It flushes range of PTEs of the domain.
  1417. */
  1418. static void __domain_flush_pages(struct protection_domain *domain,
  1419. u64 address, size_t size)
  1420. {
  1421. struct iommu_dev_data *dev_data;
  1422. int ret = 0;
  1423. ioasid_t pasid = IOMMU_NO_PASID;
  1424. bool gn = false;
  1425. lockdep_assert_held(&domain->lock);
  1426. if (pdom_is_v2_pgtbl_mode(domain)) {
  1427. gn = true;
  1428. ret = domain_flush_pages_v2(domain, address, size);
  1429. } else {
  1430. ret = domain_flush_pages_v1(domain, address, size);
  1431. }
  1432. list_for_each_entry(dev_data, &domain->dev_list, list) {
  1433. if (!dev_data->ats_enabled)
  1434. continue;
  1435. ret |= device_flush_iotlb(dev_data, address, size, pasid, gn);
  1436. }
  1437. WARN_ON(ret);
  1438. }
  1439. void amd_iommu_domain_flush_pages(struct protection_domain *domain,
  1440. u64 address, size_t size)
  1441. {
  1442. lockdep_assert_held(&domain->lock);
  1443. if (likely(!amd_iommu_np_cache)) {
  1444. __domain_flush_pages(domain, address, size);
  1445. /* Wait until IOMMU TLB and all device IOTLB flushes are complete */
  1446. domain_flush_complete(domain);
  1447. return;
  1448. }
  1449. /*
  1450. * When NpCache is on, we infer that we run in a VM and use a vIOMMU.
  1451. * In such setups it is best to avoid flushes of ranges which are not
  1452. * naturally aligned, since it would lead to flushes of unmodified
  1453. * PTEs. Such flushes would require the hypervisor to do more work than
  1454. * necessary. Therefore, perform repeated flushes of aligned ranges
  1455. * until you cover the range. Each iteration flushes the smaller
  1456. * between the natural alignment of the address that we flush and the
  1457. * greatest naturally aligned region that fits in the range.
  1458. */
  1459. while (size != 0) {
  1460. int addr_alignment = __ffs(address);
  1461. int size_alignment = __fls(size);
  1462. int min_alignment;
  1463. size_t flush_size;
  1464. /*
  1465. * size is always non-zero, but address might be zero, causing
  1466. * addr_alignment to be negative. As the casting of the
  1467. * argument in __ffs(address) to long might trim the high bits
  1468. * of the address on x86-32, cast to long when doing the check.
  1469. */
  1470. if (likely((unsigned long)address != 0))
  1471. min_alignment = min(addr_alignment, size_alignment);
  1472. else
  1473. min_alignment = size_alignment;
  1474. flush_size = 1ul << min_alignment;
  1475. __domain_flush_pages(domain, address, flush_size);
  1476. address += flush_size;
  1477. size -= flush_size;
  1478. }
  1479. /* Wait until IOMMU TLB and all device IOTLB flushes are complete */
  1480. domain_flush_complete(domain);
  1481. }
  1482. /* Flush the whole IO/TLB for a given protection domain - including PDE */
  1483. static void amd_iommu_domain_flush_all(struct protection_domain *domain)
  1484. {
  1485. amd_iommu_domain_flush_pages(domain, 0,
  1486. CMD_INV_IOMMU_ALL_PAGES_ADDRESS);
  1487. }
  1488. void amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data *dev_data,
  1489. ioasid_t pasid, u64 address, size_t size)
  1490. {
  1491. struct iommu_cmd cmd;
  1492. struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev);
  1493. build_inv_iommu_pages(&cmd, address, size,
  1494. dev_data->gcr3_info.domid, pasid, true);
  1495. iommu_queue_command(iommu, &cmd);
  1496. if (dev_data->ats_enabled)
  1497. device_flush_iotlb(dev_data, address, size, pasid, true);
  1498. iommu_completion_wait(iommu);
  1499. }
  1500. static void dev_flush_pasid_all(struct iommu_dev_data *dev_data,
  1501. ioasid_t pasid)
  1502. {
  1503. amd_iommu_dev_flush_pasid_pages(dev_data, pasid, 0,
  1504. CMD_INV_IOMMU_ALL_PAGES_ADDRESS);
  1505. }
  1506. int amd_iommu_complete_ppr(struct device *dev, u32 pasid, int status, int tag)
  1507. {
  1508. struct iommu_dev_data *dev_data;
  1509. struct amd_iommu *iommu;
  1510. struct iommu_cmd cmd;
  1511. dev_data = dev_iommu_priv_get(dev);
  1512. iommu = get_amd_iommu_from_dev(dev);
  1513. build_complete_ppr(&cmd, dev_data->devid, pasid, status,
  1514. tag, dev_data->pri_tlp);
  1515. return iommu_queue_command(iommu, &cmd);
  1516. }
  1517. /****************************************************************************
  1518. *
  1519. * The next functions belong to the domain allocation. A domain is
  1520. * allocated for every IOMMU as the default domain. If device isolation
  1521. * is enabled, every device get its own domain. The most important thing
  1522. * about domains is the page table mapping the DMA address space they
  1523. * contain.
  1524. *
  1525. ****************************************************************************/
  1526. int amd_iommu_pdom_id_alloc(void)
  1527. {
  1528. return ida_alloc_range(&pdom_ids, 1, MAX_DOMAIN_ID - 1, GFP_ATOMIC);
  1529. }
  1530. int amd_iommu_pdom_id_reserve(u16 id, gfp_t gfp)
  1531. {
  1532. return ida_alloc_range(&pdom_ids, id, id, gfp);
  1533. }
  1534. void amd_iommu_pdom_id_free(int id)
  1535. {
  1536. ida_free(&pdom_ids, id);
  1537. }
  1538. void amd_iommu_pdom_id_destroy(void)
  1539. {
  1540. ida_destroy(&pdom_ids);
  1541. }
  1542. static void free_gcr3_tbl_level1(u64 *tbl)
  1543. {
  1544. u64 *ptr;
  1545. int i;
  1546. for (i = 0; i < 512; ++i) {
  1547. if (!(tbl[i] & GCR3_VALID))
  1548. continue;
  1549. ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
  1550. iommu_free_pages(ptr);
  1551. }
  1552. }
  1553. static void free_gcr3_tbl_level2(u64 *tbl)
  1554. {
  1555. u64 *ptr;
  1556. int i;
  1557. for (i = 0; i < 512; ++i) {
  1558. if (!(tbl[i] & GCR3_VALID))
  1559. continue;
  1560. ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
  1561. free_gcr3_tbl_level1(ptr);
  1562. }
  1563. }
  1564. static void free_gcr3_table(struct gcr3_tbl_info *gcr3_info)
  1565. {
  1566. if (gcr3_info->glx == 2)
  1567. free_gcr3_tbl_level2(gcr3_info->gcr3_tbl);
  1568. else if (gcr3_info->glx == 1)
  1569. free_gcr3_tbl_level1(gcr3_info->gcr3_tbl);
  1570. else
  1571. WARN_ON_ONCE(gcr3_info->glx != 0);
  1572. gcr3_info->glx = 0;
  1573. /* Free per device domain ID */
  1574. amd_iommu_pdom_id_free(gcr3_info->domid);
  1575. iommu_free_pages(gcr3_info->gcr3_tbl);
  1576. gcr3_info->gcr3_tbl = NULL;
  1577. }
  1578. /*
  1579. * Number of GCR3 table levels required. Level must be 4-Kbyte
  1580. * page and can contain up to 512 entries.
  1581. */
  1582. static int get_gcr3_levels(int pasids)
  1583. {
  1584. int levels;
  1585. if (pasids == -1)
  1586. return amd_iommu_max_glx_val;
  1587. levels = get_count_order(pasids);
  1588. return levels ? (DIV_ROUND_UP(levels, 9) - 1) : levels;
  1589. }
  1590. static int setup_gcr3_table(struct gcr3_tbl_info *gcr3_info,
  1591. struct amd_iommu *iommu, int pasids)
  1592. {
  1593. int levels = get_gcr3_levels(pasids);
  1594. int nid = iommu ? dev_to_node(&iommu->dev->dev) : NUMA_NO_NODE;
  1595. int domid;
  1596. if (levels > amd_iommu_max_glx_val)
  1597. return -EINVAL;
  1598. if (gcr3_info->gcr3_tbl)
  1599. return -EBUSY;
  1600. /* Allocate per device domain ID */
  1601. domid = amd_iommu_pdom_id_alloc();
  1602. if (domid <= 0)
  1603. return -ENOSPC;
  1604. gcr3_info->domid = domid;
  1605. gcr3_info->gcr3_tbl = iommu_alloc_pages_node_sz(nid, GFP_ATOMIC, SZ_4K);
  1606. if (gcr3_info->gcr3_tbl == NULL) {
  1607. amd_iommu_pdom_id_free(domid);
  1608. return -ENOMEM;
  1609. }
  1610. gcr3_info->glx = levels;
  1611. return 0;
  1612. }
  1613. static u64 *__get_gcr3_pte(struct gcr3_tbl_info *gcr3_info,
  1614. ioasid_t pasid, bool alloc)
  1615. {
  1616. int index;
  1617. u64 *pte;
  1618. u64 *root = gcr3_info->gcr3_tbl;
  1619. int level = gcr3_info->glx;
  1620. while (true) {
  1621. index = (pasid >> (9 * level)) & 0x1ff;
  1622. pte = &root[index];
  1623. if (level == 0)
  1624. break;
  1625. if (!(*pte & GCR3_VALID)) {
  1626. if (!alloc)
  1627. return NULL;
  1628. root = (void *)get_zeroed_page(GFP_ATOMIC);
  1629. if (root == NULL)
  1630. return NULL;
  1631. *pte = iommu_virt_to_phys(root) | GCR3_VALID;
  1632. }
  1633. root = iommu_phys_to_virt(*pte & PAGE_MASK);
  1634. level -= 1;
  1635. }
  1636. return pte;
  1637. }
  1638. static int update_gcr3(struct iommu_dev_data *dev_data,
  1639. ioasid_t pasid, unsigned long gcr3, bool set)
  1640. {
  1641. struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
  1642. u64 *pte;
  1643. pte = __get_gcr3_pte(gcr3_info, pasid, true);
  1644. if (pte == NULL)
  1645. return -ENOMEM;
  1646. if (set)
  1647. *pte = (gcr3 & PAGE_MASK) | GCR3_VALID;
  1648. else
  1649. *pte = 0;
  1650. dev_flush_pasid_all(dev_data, pasid);
  1651. return 0;
  1652. }
  1653. int amd_iommu_set_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid,
  1654. unsigned long gcr3)
  1655. {
  1656. struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
  1657. int ret;
  1658. iommu_group_mutex_assert(dev_data->dev);
  1659. ret = update_gcr3(dev_data, pasid, gcr3, true);
  1660. if (ret)
  1661. return ret;
  1662. gcr3_info->pasid_cnt++;
  1663. return ret;
  1664. }
  1665. int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid)
  1666. {
  1667. struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
  1668. int ret;
  1669. iommu_group_mutex_assert(dev_data->dev);
  1670. ret = update_gcr3(dev_data, pasid, 0, false);
  1671. if (ret)
  1672. return ret;
  1673. gcr3_info->pasid_cnt--;
  1674. return ret;
  1675. }
  1676. /*
  1677. * Note:
  1678. * The old value for GCR3 table and GPT have been cleared from caller.
  1679. */
  1680. static void set_dte_gcr3_table(struct iommu_dev_data *dev_data,
  1681. struct dev_table_entry *new)
  1682. {
  1683. struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
  1684. u64 gcr3 = iommu_virt_to_phys(gcr3_info->gcr3_tbl);
  1685. new->data[0] |= DTE_FLAG_TV |
  1686. (dev_data->ppr ? DTE_FLAG_PPR : 0) |
  1687. (pdom_is_v2_pgtbl_mode(dev_data->domain) ? DTE_FLAG_GIOV : 0) |
  1688. DTE_FLAG_GV |
  1689. FIELD_PREP(DTE_GLX, gcr3_info->glx) |
  1690. FIELD_PREP(DTE_GCR3_14_12, gcr3 >> 12) |
  1691. DTE_FLAG_IR | DTE_FLAG_IW;
  1692. new->data[1] |= FIELD_PREP(DTE_DOMID_MASK, dev_data->gcr3_info.domid) |
  1693. FIELD_PREP(DTE_GCR3_30_15, gcr3 >> 15) |
  1694. (dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0) |
  1695. FIELD_PREP(DTE_GCR3_51_31, gcr3 >> 31);
  1696. /* Guest page table can only support 4 and 5 levels */
  1697. if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL)
  1698. new->data[2] |= FIELD_PREP(DTE_GPT_LEVEL_MASK, GUEST_PGTABLE_5_LEVEL);
  1699. else
  1700. new->data[2] |= FIELD_PREP(DTE_GPT_LEVEL_MASK, GUEST_PGTABLE_4_LEVEL);
  1701. }
  1702. void amd_iommu_set_dte_v1(struct iommu_dev_data *dev_data,
  1703. struct protection_domain *domain, u16 domid,
  1704. struct pt_iommu_amdv1_hw_info *pt_info,
  1705. struct dev_table_entry *new)
  1706. {
  1707. u64 host_pt_root = __sme_set(pt_info->host_pt_root);
  1708. /* Note Dirty tracking is used for v1 table only for now */
  1709. new->data[0] |= DTE_FLAG_TV |
  1710. FIELD_PREP(DTE_MODE_MASK, pt_info->mode) |
  1711. (domain->dirty_tracking ? DTE_FLAG_HAD : 0) |
  1712. FIELD_PREP(DTE_HOST_TRP, host_pt_root >> 12) |
  1713. DTE_FLAG_IR | DTE_FLAG_IW;
  1714. new->data[1] |= FIELD_PREP(DTE_DOMID_MASK, domid) |
  1715. (dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0);
  1716. }
  1717. static void set_dte_v1(struct iommu_dev_data *dev_data,
  1718. struct protection_domain *domain, u16 domid,
  1719. phys_addr_t top_paddr, unsigned int top_level,
  1720. struct dev_table_entry *new)
  1721. {
  1722. struct pt_iommu_amdv1_hw_info pt_info;
  1723. /*
  1724. * When updating the IO pagetable, the new top and level
  1725. * are provided as parameters. For other operations i.e.
  1726. * device attach, retrieve the current pagetable info
  1727. * via the IOMMU PT API.
  1728. */
  1729. if (top_paddr) {
  1730. pt_info.host_pt_root = top_paddr;
  1731. pt_info.mode = top_level + 1;
  1732. } else {
  1733. WARN_ON(top_paddr || top_level);
  1734. pt_iommu_amdv1_hw_info(&domain->amdv1, &pt_info);
  1735. }
  1736. amd_iommu_set_dte_v1(dev_data, domain, domid, &pt_info, new);
  1737. }
  1738. static void set_dte_passthrough(struct iommu_dev_data *dev_data,
  1739. struct protection_domain *domain,
  1740. struct dev_table_entry *new)
  1741. {
  1742. new->data[0] |= DTE_FLAG_TV | DTE_FLAG_IR | DTE_FLAG_IW;
  1743. new->data[1] |= FIELD_PREP(DTE_DOMID_MASK, domain->id) |
  1744. (dev_data->ats_enabled) ? DTE_FLAG_IOTLB : 0;
  1745. }
  1746. static void set_dte_entry(struct amd_iommu *iommu,
  1747. struct iommu_dev_data *dev_data,
  1748. phys_addr_t top_paddr, unsigned int top_level)
  1749. {
  1750. u32 old_domid;
  1751. struct dev_table_entry new = {};
  1752. struct protection_domain *domain = dev_data->domain;
  1753. struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
  1754. struct dev_table_entry *dte = &get_dev_table(iommu)[dev_data->devid];
  1755. amd_iommu_make_clear_dte(dev_data, &new);
  1756. old_domid = READ_ONCE(dte->data[1]) & DTE_DOMID_MASK;
  1757. if (gcr3_info->gcr3_tbl)
  1758. set_dte_gcr3_table(dev_data, &new);
  1759. else if (domain->domain.type == IOMMU_DOMAIN_IDENTITY)
  1760. set_dte_passthrough(dev_data, domain, &new);
  1761. else if ((domain->domain.type & __IOMMU_DOMAIN_PAGING) &&
  1762. domain->pd_mode == PD_MODE_V1)
  1763. set_dte_v1(dev_data, domain, domain->id, top_paddr, top_level, &new);
  1764. else
  1765. WARN_ON(true);
  1766. amd_iommu_update_dte(iommu, dev_data, &new);
  1767. /*
  1768. * A kdump kernel might be replacing a domain ID that was copied from
  1769. * the previous kernel--if so, it needs to flush the translation cache
  1770. * entries for the old domain ID that is being overwritten
  1771. */
  1772. if (old_domid) {
  1773. amd_iommu_flush_tlb_domid(iommu, old_domid);
  1774. }
  1775. }
  1776. /*
  1777. * Clear DMA-remap related flags to block all DMA (blockeded domain)
  1778. */
  1779. static void clear_dte_entry(struct amd_iommu *iommu, struct iommu_dev_data *dev_data)
  1780. {
  1781. struct dev_table_entry new = {};
  1782. amd_iommu_make_clear_dte(dev_data, &new);
  1783. amd_iommu_update_dte(iommu, dev_data, &new);
  1784. }
  1785. /* Update and flush DTE for the given device */
  1786. static void dev_update_dte(struct iommu_dev_data *dev_data, bool set)
  1787. {
  1788. struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev);
  1789. if (set)
  1790. set_dte_entry(iommu, dev_data, 0, 0);
  1791. else
  1792. clear_dte_entry(iommu, dev_data);
  1793. }
  1794. /*
  1795. * If domain is SVA capable then initialize GCR3 table. Also if domain is
  1796. * in v2 page table mode then update GCR3[0].
  1797. */
  1798. static int init_gcr3_table(struct iommu_dev_data *dev_data,
  1799. struct protection_domain *pdom)
  1800. {
  1801. struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
  1802. int max_pasids = dev_data->max_pasids;
  1803. struct pt_iommu_x86_64_hw_info pt_info;
  1804. int ret = 0;
  1805. /*
  1806. * If domain is in pt mode then setup GCR3 table only if device
  1807. * is PASID capable
  1808. */
  1809. if (pdom_is_in_pt_mode(pdom) && !pdev_pasid_supported(dev_data))
  1810. return ret;
  1811. /*
  1812. * By default, setup GCR3 table to support MAX PASIDs
  1813. * supported by the device/IOMMU.
  1814. */
  1815. ret = setup_gcr3_table(&dev_data->gcr3_info, iommu,
  1816. max_pasids > 0 ? max_pasids : 1);
  1817. if (ret)
  1818. return ret;
  1819. /* Setup GCR3[0] only if domain is setup with v2 page table mode */
  1820. if (!pdom_is_v2_pgtbl_mode(pdom))
  1821. return ret;
  1822. pt_iommu_x86_64_hw_info(&pdom->amdv2, &pt_info);
  1823. ret = update_gcr3(dev_data, 0, __sme_set(pt_info.gcr3_pt), true);
  1824. if (ret)
  1825. free_gcr3_table(&dev_data->gcr3_info);
  1826. return ret;
  1827. }
  1828. static void destroy_gcr3_table(struct iommu_dev_data *dev_data,
  1829. struct protection_domain *pdom)
  1830. {
  1831. struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
  1832. if (pdom_is_v2_pgtbl_mode(pdom))
  1833. update_gcr3(dev_data, 0, 0, false);
  1834. if (gcr3_info->gcr3_tbl == NULL)
  1835. return;
  1836. free_gcr3_table(gcr3_info);
  1837. }
  1838. static int pdom_attach_iommu(struct amd_iommu *iommu,
  1839. struct protection_domain *pdom)
  1840. {
  1841. struct pdom_iommu_info *pdom_iommu_info, *curr;
  1842. unsigned long flags;
  1843. int ret = 0;
  1844. spin_lock_irqsave(&pdom->lock, flags);
  1845. pdom_iommu_info = xa_load(&pdom->iommu_array, iommu->index);
  1846. if (pdom_iommu_info) {
  1847. pdom_iommu_info->refcnt++;
  1848. goto out_unlock;
  1849. }
  1850. pdom_iommu_info = kzalloc_obj(*pdom_iommu_info, GFP_ATOMIC);
  1851. if (!pdom_iommu_info) {
  1852. ret = -ENOMEM;
  1853. goto out_unlock;
  1854. }
  1855. pdom_iommu_info->iommu = iommu;
  1856. pdom_iommu_info->refcnt = 1;
  1857. curr = xa_cmpxchg(&pdom->iommu_array, iommu->index,
  1858. NULL, pdom_iommu_info, GFP_ATOMIC);
  1859. if (curr) {
  1860. kfree(pdom_iommu_info);
  1861. ret = -ENOSPC;
  1862. goto out_unlock;
  1863. }
  1864. out_unlock:
  1865. spin_unlock_irqrestore(&pdom->lock, flags);
  1866. return ret;
  1867. }
  1868. static void pdom_detach_iommu(struct amd_iommu *iommu,
  1869. struct protection_domain *pdom)
  1870. {
  1871. struct pdom_iommu_info *pdom_iommu_info;
  1872. unsigned long flags;
  1873. spin_lock_irqsave(&pdom->lock, flags);
  1874. pdom_iommu_info = xa_load(&pdom->iommu_array, iommu->index);
  1875. if (!pdom_iommu_info) {
  1876. spin_unlock_irqrestore(&pdom->lock, flags);
  1877. return;
  1878. }
  1879. pdom_iommu_info->refcnt--;
  1880. if (pdom_iommu_info->refcnt == 0) {
  1881. xa_erase(&pdom->iommu_array, iommu->index);
  1882. kfree(pdom_iommu_info);
  1883. }
  1884. spin_unlock_irqrestore(&pdom->lock, flags);
  1885. }
  1886. /*
  1887. * If a device is not yet associated with a domain, this function makes the
  1888. * device visible in the domain
  1889. */
  1890. static int attach_device(struct device *dev,
  1891. struct protection_domain *domain)
  1892. {
  1893. struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
  1894. struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
  1895. struct pci_dev *pdev;
  1896. unsigned long flags;
  1897. int ret = 0;
  1898. mutex_lock(&dev_data->mutex);
  1899. if (dev_data->domain != NULL) {
  1900. ret = -EBUSY;
  1901. goto out;
  1902. }
  1903. /* Do reference counting */
  1904. ret = pdom_attach_iommu(iommu, domain);
  1905. if (ret)
  1906. goto out;
  1907. /* Setup GCR3 table */
  1908. if (pdom_is_sva_capable(domain)) {
  1909. ret = init_gcr3_table(dev_data, domain);
  1910. if (ret) {
  1911. pdom_detach_iommu(iommu, domain);
  1912. goto out;
  1913. }
  1914. }
  1915. pdev = dev_is_pci(dev_data->dev) ? to_pci_dev(dev_data->dev) : NULL;
  1916. if (pdev && pdom_is_sva_capable(domain)) {
  1917. pdev_enable_caps(pdev);
  1918. /*
  1919. * Device can continue to function even if IOPF
  1920. * enablement failed. Hence in error path just
  1921. * disable device PRI support.
  1922. */
  1923. if (amd_iommu_iopf_add_device(iommu, dev_data))
  1924. pdev_disable_cap_pri(pdev);
  1925. } else if (pdev) {
  1926. pdev_enable_cap_ats(pdev);
  1927. }
  1928. /* Update data structures */
  1929. dev_data->domain = domain;
  1930. spin_lock_irqsave(&domain->lock, flags);
  1931. list_add(&dev_data->list, &domain->dev_list);
  1932. spin_unlock_irqrestore(&domain->lock, flags);
  1933. /* Update device table */
  1934. dev_update_dte(dev_data, true);
  1935. out:
  1936. mutex_unlock(&dev_data->mutex);
  1937. return ret;
  1938. }
  1939. /*
  1940. * Removes a device from a protection domain (with devtable_lock held)
  1941. */
  1942. static void detach_device(struct device *dev)
  1943. {
  1944. struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
  1945. struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data);
  1946. struct protection_domain *domain = dev_data->domain;
  1947. unsigned long flags;
  1948. mutex_lock(&dev_data->mutex);
  1949. /*
  1950. * First check if the device is still attached. It might already
  1951. * be detached from its domain because the generic
  1952. * iommu_detach_group code detached it and we try again here in
  1953. * our alias handling.
  1954. */
  1955. if (WARN_ON(!dev_data->domain))
  1956. goto out;
  1957. /* Remove IOPF handler */
  1958. if (dev_data->ppr) {
  1959. iopf_queue_flush_dev(dev);
  1960. amd_iommu_iopf_remove_device(iommu, dev_data);
  1961. }
  1962. if (dev_is_pci(dev))
  1963. pdev_disable_caps(to_pci_dev(dev));
  1964. /* Clear DTE and flush the entry */
  1965. dev_update_dte(dev_data, false);
  1966. /* Flush IOTLB and wait for the flushes to finish */
  1967. spin_lock_irqsave(&domain->lock, flags);
  1968. amd_iommu_domain_flush_all(domain);
  1969. list_del(&dev_data->list);
  1970. spin_unlock_irqrestore(&domain->lock, flags);
  1971. /* Clear GCR3 table */
  1972. if (pdom_is_sva_capable(domain))
  1973. destroy_gcr3_table(dev_data, domain);
  1974. /* Update data structures */
  1975. dev_data->domain = NULL;
  1976. /* decrease reference counters - needs to happen after the flushes */
  1977. pdom_detach_iommu(iommu, domain);
  1978. out:
  1979. mutex_unlock(&dev_data->mutex);
  1980. }
  1981. static struct iommu_device *amd_iommu_probe_device(struct device *dev)
  1982. {
  1983. struct iommu_device *iommu_dev;
  1984. struct amd_iommu *iommu;
  1985. struct iommu_dev_data *dev_data;
  1986. int ret;
  1987. if (!check_device(dev))
  1988. return ERR_PTR(-ENODEV);
  1989. iommu = rlookup_amd_iommu(dev);
  1990. if (!iommu)
  1991. return ERR_PTR(-ENODEV);
  1992. /* Not registered yet? */
  1993. if (!iommu->iommu.ops)
  1994. return ERR_PTR(-ENODEV);
  1995. if (dev_iommu_priv_get(dev))
  1996. return &iommu->iommu;
  1997. ret = iommu_init_device(iommu, dev);
  1998. if (ret) {
  1999. dev_err(dev, "Failed to initialize - trying to proceed anyway\n");
  2000. iommu_dev = ERR_PTR(ret);
  2001. iommu_ignore_device(iommu, dev);
  2002. goto out_err;
  2003. }
  2004. amd_iommu_set_pci_msi_domain(dev, iommu);
  2005. iommu_dev = &iommu->iommu;
  2006. /*
  2007. * If IOMMU and device supports PASID then it will contain max
  2008. * supported PASIDs, else it will be zero.
  2009. */
  2010. dev_data = dev_iommu_priv_get(dev);
  2011. if (amd_iommu_pasid_supported() && dev_is_pci(dev) &&
  2012. pdev_pasid_supported(dev_data)) {
  2013. dev_data->max_pasids = min_t(u32, iommu->iommu.max_pasids,
  2014. pci_max_pasids(to_pci_dev(dev)));
  2015. }
  2016. if (amd_iommu_pgtable == PD_MODE_NONE) {
  2017. pr_warn_once("%s: DMA translation not supported by iommu.\n",
  2018. __func__);
  2019. iommu_dev = ERR_PTR(-ENODEV);
  2020. goto out_err;
  2021. }
  2022. iommu_completion_wait(iommu);
  2023. if (FEATURE_NUM_INT_REMAP_SUP_2K(amd_iommu_efr2))
  2024. dev_data->max_irqs = MAX_IRQS_PER_TABLE_2K;
  2025. else
  2026. dev_data->max_irqs = MAX_IRQS_PER_TABLE_512;
  2027. if (dev_is_pci(dev))
  2028. pci_prepare_ats(to_pci_dev(dev), PAGE_SHIFT);
  2029. out_err:
  2030. return iommu_dev;
  2031. }
  2032. static void amd_iommu_release_device(struct device *dev)
  2033. {
  2034. struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
  2035. WARN_ON(dev_data->domain);
  2036. /*
  2037. * We keep dev_data around for unplugged devices and reuse it when the
  2038. * device is re-plugged - not doing so would introduce a ton of races.
  2039. */
  2040. }
  2041. static struct iommu_group *amd_iommu_device_group(struct device *dev)
  2042. {
  2043. if (dev_is_pci(dev))
  2044. return pci_device_group(dev);
  2045. return acpihid_device_group(dev);
  2046. }
  2047. /*****************************************************************************
  2048. *
  2049. * The following functions belong to the exported interface of AMD IOMMU
  2050. *
  2051. * This interface allows access to lower level functions of the IOMMU
  2052. * like protection domain handling and assignement of devices to domains
  2053. * which is not possible with the dma_ops interface.
  2054. *
  2055. *****************************************************************************/
  2056. static void protection_domain_init(struct protection_domain *domain)
  2057. {
  2058. spin_lock_init(&domain->lock);
  2059. INIT_LIST_HEAD(&domain->dev_list);
  2060. INIT_LIST_HEAD(&domain->dev_data_list);
  2061. INIT_LIST_HEAD(&domain->viommu_list);
  2062. xa_init(&domain->iommu_array);
  2063. }
  2064. struct protection_domain *protection_domain_alloc(void)
  2065. {
  2066. struct protection_domain *domain;
  2067. int domid;
  2068. domain = kzalloc_obj(*domain);
  2069. if (!domain)
  2070. return NULL;
  2071. domid = amd_iommu_pdom_id_alloc();
  2072. if (domid <= 0) {
  2073. kfree(domain);
  2074. return NULL;
  2075. }
  2076. domain->id = domid;
  2077. protection_domain_init(domain);
  2078. return domain;
  2079. }
  2080. static bool amd_iommu_hd_support(struct amd_iommu *iommu)
  2081. {
  2082. if (amd_iommu_hatdis)
  2083. return false;
  2084. return iommu && (iommu->features & FEATURE_HDSUP);
  2085. }
  2086. static spinlock_t *amd_iommu_get_top_lock(struct pt_iommu *iommupt)
  2087. {
  2088. struct protection_domain *pdom =
  2089. container_of(iommupt, struct protection_domain, iommu);
  2090. return &pdom->lock;
  2091. }
  2092. /*
  2093. * Update all HW references to the domain with a new pgtable configuration.
  2094. */
  2095. static void amd_iommu_change_top(struct pt_iommu *iommu_table,
  2096. phys_addr_t top_paddr, unsigned int top_level)
  2097. {
  2098. struct protection_domain *pdom =
  2099. container_of(iommu_table, struct protection_domain, iommu);
  2100. struct iommu_dev_data *dev_data;
  2101. lockdep_assert_held(&pdom->lock);
  2102. /* Update the DTE for all devices attached to this domain */
  2103. list_for_each_entry(dev_data, &pdom->dev_list, list) {
  2104. struct amd_iommu *iommu = rlookup_amd_iommu(dev_data->dev);
  2105. /* Update the HW references with the new level and top ptr */
  2106. set_dte_entry(iommu, dev_data, top_paddr, top_level);
  2107. clone_aliases(iommu, dev_data->dev);
  2108. }
  2109. list_for_each_entry(dev_data, &pdom->dev_list, list)
  2110. device_flush_dte(dev_data);
  2111. domain_flush_complete(pdom);
  2112. }
  2113. /*
  2114. * amd_iommu_iotlb_sync_map() is used to generate flushes for non-present to
  2115. * present (ie mapping) operations. It is a NOP if the IOMMU doesn't have non
  2116. * present caching (like hypervisor shadowing).
  2117. */
  2118. static int amd_iommu_iotlb_sync_map(struct iommu_domain *dom,
  2119. unsigned long iova, size_t size)
  2120. {
  2121. struct protection_domain *domain = to_pdomain(dom);
  2122. unsigned long flags;
  2123. if (likely(!amd_iommu_np_cache))
  2124. return 0;
  2125. spin_lock_irqsave(&domain->lock, flags);
  2126. amd_iommu_domain_flush_pages(domain, iova, size);
  2127. spin_unlock_irqrestore(&domain->lock, flags);
  2128. return 0;
  2129. }
  2130. static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
  2131. {
  2132. struct protection_domain *dom = to_pdomain(domain);
  2133. unsigned long flags;
  2134. spin_lock_irqsave(&dom->lock, flags);
  2135. amd_iommu_domain_flush_all(dom);
  2136. spin_unlock_irqrestore(&dom->lock, flags);
  2137. }
  2138. static void amd_iommu_iotlb_sync(struct iommu_domain *domain,
  2139. struct iommu_iotlb_gather *gather)
  2140. {
  2141. struct protection_domain *dom = to_pdomain(domain);
  2142. unsigned long flags;
  2143. spin_lock_irqsave(&dom->lock, flags);
  2144. amd_iommu_domain_flush_pages(dom, gather->start,
  2145. gather->end - gather->start + 1);
  2146. spin_unlock_irqrestore(&dom->lock, flags);
  2147. iommu_put_pages_list(&gather->freelist);
  2148. }
  2149. static const struct pt_iommu_driver_ops amd_hw_driver_ops_v1 = {
  2150. .get_top_lock = amd_iommu_get_top_lock,
  2151. .change_top = amd_iommu_change_top,
  2152. };
  2153. static const struct iommu_domain_ops amdv1_ops = {
  2154. IOMMU_PT_DOMAIN_OPS(amdv1),
  2155. .iotlb_sync_map = amd_iommu_iotlb_sync_map,
  2156. .flush_iotlb_all = amd_iommu_flush_iotlb_all,
  2157. .iotlb_sync = amd_iommu_iotlb_sync,
  2158. .attach_dev = amd_iommu_attach_device,
  2159. .free = amd_iommu_domain_free,
  2160. .enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
  2161. };
  2162. static const struct iommu_dirty_ops amdv1_dirty_ops = {
  2163. IOMMU_PT_DIRTY_OPS(amdv1),
  2164. .set_dirty_tracking = amd_iommu_set_dirty_tracking,
  2165. };
  2166. static struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device *dev,
  2167. u32 flags)
  2168. {
  2169. struct pt_iommu_amdv1_cfg cfg = {};
  2170. struct protection_domain *domain;
  2171. int ret;
  2172. if (amd_iommu_hatdis)
  2173. return ERR_PTR(-EOPNOTSUPP);
  2174. domain = protection_domain_alloc();
  2175. if (!domain)
  2176. return ERR_PTR(-ENOMEM);
  2177. domain->pd_mode = PD_MODE_V1;
  2178. domain->iommu.driver_ops = &amd_hw_driver_ops_v1;
  2179. domain->iommu.nid = dev_to_node(dev);
  2180. if (flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING)
  2181. domain->domain.dirty_ops = &amdv1_dirty_ops;
  2182. /*
  2183. * Someday FORCE_COHERENCE should be set by
  2184. * amd_iommu_enforce_cache_coherency() like VT-d does.
  2185. */
  2186. cfg.common.features = BIT(PT_FEAT_DYNAMIC_TOP) |
  2187. BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES) |
  2188. BIT(PT_FEAT_AMDV1_FORCE_COHERENCE);
  2189. /*
  2190. * AMD's IOMMU can flush as many pages as necessary in a single flush.
  2191. * Unless we run in a virtual machine, which can be inferred according
  2192. * to whether "non-present cache" is on, it is probably best to prefer
  2193. * (potentially) too extensive TLB flushing (i.e., more misses) over
  2194. * multiple TLB flushes (i.e., more flushes). For virtual machines the
  2195. * hypervisor needs to synchronize the host IOMMU PTEs with those of
  2196. * the guest, and the trade-off is different: unnecessary TLB flushes
  2197. * should be avoided.
  2198. */
  2199. if (amd_iommu_np_cache)
  2200. cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE_NO_GAPS);
  2201. else
  2202. cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE);
  2203. cfg.common.hw_max_vasz_lg2 =
  2204. min(64, (amd_iommu_hpt_level - 1) * 9 + 21);
  2205. cfg.common.hw_max_oasz_lg2 = 52;
  2206. cfg.starting_level = 2;
  2207. domain->domain.ops = &amdv1_ops;
  2208. ret = pt_iommu_amdv1_init(&domain->amdv1, &cfg, GFP_KERNEL);
  2209. if (ret) {
  2210. amd_iommu_domain_free(&domain->domain);
  2211. return ERR_PTR(ret);
  2212. }
  2213. /*
  2214. * Narrow the supported page sizes to those selected by the kernel
  2215. * command line.
  2216. */
  2217. domain->domain.pgsize_bitmap &= amd_iommu_pgsize_bitmap;
  2218. return &domain->domain;
  2219. }
  2220. static const struct iommu_domain_ops amdv2_ops = {
  2221. IOMMU_PT_DOMAIN_OPS(x86_64),
  2222. .iotlb_sync_map = amd_iommu_iotlb_sync_map,
  2223. .flush_iotlb_all = amd_iommu_flush_iotlb_all,
  2224. .iotlb_sync = amd_iommu_iotlb_sync,
  2225. .attach_dev = amd_iommu_attach_device,
  2226. .free = amd_iommu_domain_free,
  2227. /*
  2228. * Note the AMDv2 page table format does not support a Force Coherency
  2229. * bit, so enforce_cache_coherency should not be set. However VFIO is
  2230. * not prepared to handle a case where some domains will support
  2231. * enforcement and others do not. VFIO and iommufd will have to be fixed
  2232. * before it can fully use the V2 page table. See the comment in
  2233. * iommufd_hwpt_paging_alloc(). For now leave things as they have
  2234. * historically been and lie about enforce_cache_coherencey.
  2235. */
  2236. .enforce_cache_coherency = amd_iommu_enforce_cache_coherency,
  2237. };
  2238. static struct iommu_domain *amd_iommu_domain_alloc_paging_v2(struct device *dev,
  2239. u32 flags)
  2240. {
  2241. struct pt_iommu_x86_64_cfg cfg = {};
  2242. struct protection_domain *domain;
  2243. int ret;
  2244. if (!amd_iommu_v2_pgtbl_supported())
  2245. return ERR_PTR(-EOPNOTSUPP);
  2246. domain = protection_domain_alloc();
  2247. if (!domain)
  2248. return ERR_PTR(-ENOMEM);
  2249. domain->pd_mode = PD_MODE_V2;
  2250. domain->iommu.nid = dev_to_node(dev);
  2251. cfg.common.features = BIT(PT_FEAT_X86_64_AMD_ENCRYPT_TABLES);
  2252. if (amd_iommu_np_cache)
  2253. cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE_NO_GAPS);
  2254. else
  2255. cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE);
  2256. /*
  2257. * The v2 table behaves differently if it is attached to PASID 0 vs a
  2258. * non-zero PASID. On PASID 0 it has no sign extension and the full
  2259. * 57/48 bits decode the lower addresses. Otherwise it behaves like a
  2260. * normal sign extended x86 page table. Since we want the domain to work
  2261. * in both modes the top bit is removed and PT_FEAT_SIGN_EXTEND is not
  2262. * set which creates a table that is compatible in both modes.
  2263. */
  2264. if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL) {
  2265. cfg.common.hw_max_vasz_lg2 = 56;
  2266. cfg.top_level = 4;
  2267. } else {
  2268. cfg.common.hw_max_vasz_lg2 = 47;
  2269. cfg.top_level = 3;
  2270. }
  2271. cfg.common.hw_max_oasz_lg2 = 52;
  2272. domain->domain.ops = &amdv2_ops;
  2273. ret = pt_iommu_x86_64_init(&domain->amdv2, &cfg, GFP_KERNEL);
  2274. if (ret) {
  2275. amd_iommu_domain_free(&domain->domain);
  2276. return ERR_PTR(ret);
  2277. }
  2278. return &domain->domain;
  2279. }
  2280. static inline bool is_nest_parent_supported(u32 flags)
  2281. {
  2282. /* Only allow nest parent when these features are supported */
  2283. return check_feature(FEATURE_GT) &&
  2284. check_feature(FEATURE_GIOSUP) &&
  2285. check_feature2(FEATURE_GCR3TRPMODE);
  2286. }
  2287. static struct iommu_domain *
  2288. amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,
  2289. const struct iommu_user_data *user_data)
  2290. {
  2291. struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
  2292. const u32 supported_flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
  2293. IOMMU_HWPT_ALLOC_PASID |
  2294. IOMMU_HWPT_ALLOC_NEST_PARENT;
  2295. if ((flags & ~supported_flags) || user_data)
  2296. return ERR_PTR(-EOPNOTSUPP);
  2297. switch (flags & supported_flags) {
  2298. case IOMMU_HWPT_ALLOC_DIRTY_TRACKING:
  2299. case IOMMU_HWPT_ALLOC_NEST_PARENT:
  2300. case IOMMU_HWPT_ALLOC_DIRTY_TRACKING | IOMMU_HWPT_ALLOC_NEST_PARENT:
  2301. /*
  2302. * Allocate domain with v1 page table for dirty tracking
  2303. * and/or Nest parent.
  2304. */
  2305. if ((flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING) &&
  2306. !amd_iommu_hd_support(iommu))
  2307. break;
  2308. if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) &&
  2309. !is_nest_parent_supported(flags))
  2310. break;
  2311. return amd_iommu_domain_alloc_paging_v1(dev, flags);
  2312. case IOMMU_HWPT_ALLOC_PASID:
  2313. /* Allocate domain with v2 page table if IOMMU supports PASID. */
  2314. if (!amd_iommu_pasid_supported())
  2315. break;
  2316. return amd_iommu_domain_alloc_paging_v2(dev, flags);
  2317. case 0: {
  2318. struct iommu_domain *ret;
  2319. /* If nothing specific is required use the kernel commandline default */
  2320. if (amd_iommu_pgtable == PD_MODE_V1) {
  2321. ret = amd_iommu_domain_alloc_paging_v1(dev, flags);
  2322. if (ret != ERR_PTR(-EOPNOTSUPP))
  2323. return ret;
  2324. return amd_iommu_domain_alloc_paging_v2(dev, flags);
  2325. }
  2326. ret = amd_iommu_domain_alloc_paging_v2(dev, flags);
  2327. if (ret != ERR_PTR(-EOPNOTSUPP))
  2328. return ret;
  2329. return amd_iommu_domain_alloc_paging_v1(dev, flags);
  2330. }
  2331. default:
  2332. break;
  2333. }
  2334. return ERR_PTR(-EOPNOTSUPP);
  2335. }
  2336. void amd_iommu_domain_free(struct iommu_domain *dom)
  2337. {
  2338. struct protection_domain *domain = to_pdomain(dom);
  2339. WARN_ON(!list_empty(&domain->dev_list));
  2340. pt_iommu_deinit(&domain->iommu);
  2341. amd_iommu_pdom_id_free(domain->id);
  2342. kfree(domain);
  2343. }
  2344. static int blocked_domain_attach_device(struct iommu_domain *domain,
  2345. struct device *dev,
  2346. struct iommu_domain *old)
  2347. {
  2348. struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
  2349. if (dev_data->domain)
  2350. detach_device(dev);
  2351. /* Clear DTE and flush the entry */
  2352. mutex_lock(&dev_data->mutex);
  2353. dev_update_dte(dev_data, false);
  2354. mutex_unlock(&dev_data->mutex);
  2355. return 0;
  2356. }
  2357. static int blocked_domain_set_dev_pasid(struct iommu_domain *domain,
  2358. struct device *dev, ioasid_t pasid,
  2359. struct iommu_domain *old)
  2360. {
  2361. amd_iommu_remove_dev_pasid(dev, pasid, old);
  2362. return 0;
  2363. }
  2364. static struct iommu_domain blocked_domain = {
  2365. .type = IOMMU_DOMAIN_BLOCKED,
  2366. .ops = &(const struct iommu_domain_ops) {
  2367. .attach_dev = blocked_domain_attach_device,
  2368. .set_dev_pasid = blocked_domain_set_dev_pasid,
  2369. }
  2370. };
  2371. static struct protection_domain identity_domain;
  2372. static int amd_iommu_identity_attach(struct iommu_domain *dom, struct device *dev,
  2373. struct iommu_domain *old)
  2374. {
  2375. /*
  2376. * Don't allow attaching a device to the identity domain if SNP is
  2377. * enabled.
  2378. */
  2379. if (amd_iommu_snp_en)
  2380. return -EINVAL;
  2381. return amd_iommu_attach_device(dom, dev, old);
  2382. }
  2383. static const struct iommu_domain_ops identity_domain_ops = {
  2384. .attach_dev = amd_iommu_identity_attach,
  2385. };
  2386. void amd_iommu_init_identity_domain(void)
  2387. {
  2388. struct iommu_domain *domain = &identity_domain.domain;
  2389. domain->type = IOMMU_DOMAIN_IDENTITY;
  2390. domain->ops = &identity_domain_ops;
  2391. domain->owner = &amd_iommu_ops;
  2392. identity_domain.id = amd_iommu_pdom_id_alloc();
  2393. protection_domain_init(&identity_domain);
  2394. }
  2395. static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev,
  2396. struct iommu_domain *old)
  2397. {
  2398. struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
  2399. struct protection_domain *domain = to_pdomain(dom);
  2400. struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
  2401. int ret;
  2402. /*
  2403. * Skip attach device to domain if new domain is same as
  2404. * devices current domain
  2405. */
  2406. if (dev_data->domain == domain)
  2407. return 0;
  2408. dev_data->defer_attach = false;
  2409. /*
  2410. * Restrict to devices with compatible IOMMU hardware support
  2411. * when enforcement of dirty tracking is enabled.
  2412. */
  2413. if (dom->dirty_ops && !amd_iommu_hd_support(iommu))
  2414. return -EINVAL;
  2415. if (dev_data->domain)
  2416. detach_device(dev);
  2417. ret = attach_device(dev, domain);
  2418. #ifdef CONFIG_IRQ_REMAP
  2419. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
  2420. if (dom->type == IOMMU_DOMAIN_UNMANAGED)
  2421. dev_data->use_vapic = 1;
  2422. else
  2423. dev_data->use_vapic = 0;
  2424. }
  2425. #endif
  2426. return ret;
  2427. }
  2428. static bool amd_iommu_capable(struct device *dev, enum iommu_cap cap)
  2429. {
  2430. switch (cap) {
  2431. case IOMMU_CAP_CACHE_COHERENCY:
  2432. return true;
  2433. case IOMMU_CAP_NOEXEC:
  2434. return false;
  2435. case IOMMU_CAP_PRE_BOOT_PROTECTION:
  2436. return amdr_ivrs_remap_support;
  2437. case IOMMU_CAP_ENFORCE_CACHE_COHERENCY:
  2438. return true;
  2439. case IOMMU_CAP_DEFERRED_FLUSH:
  2440. return true;
  2441. case IOMMU_CAP_DIRTY_TRACKING: {
  2442. struct amd_iommu *iommu = get_amd_iommu_from_dev(dev);
  2443. return amd_iommu_hd_support(iommu);
  2444. }
  2445. default:
  2446. break;
  2447. }
  2448. return false;
  2449. }
  2450. static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain,
  2451. bool enable)
  2452. {
  2453. struct protection_domain *pdomain = to_pdomain(domain);
  2454. struct dev_table_entry *dte;
  2455. struct iommu_dev_data *dev_data;
  2456. bool domain_flush = false;
  2457. struct amd_iommu *iommu;
  2458. unsigned long flags;
  2459. u64 new;
  2460. spin_lock_irqsave(&pdomain->lock, flags);
  2461. if (!(pdomain->dirty_tracking ^ enable)) {
  2462. spin_unlock_irqrestore(&pdomain->lock, flags);
  2463. return 0;
  2464. }
  2465. list_for_each_entry(dev_data, &pdomain->dev_list, list) {
  2466. spin_lock(&dev_data->dte_lock);
  2467. iommu = get_amd_iommu_from_dev_data(dev_data);
  2468. dte = &get_dev_table(iommu)[dev_data->devid];
  2469. new = dte->data[0];
  2470. new = (enable ? new | DTE_FLAG_HAD : new & ~DTE_FLAG_HAD);
  2471. dte->data[0] = new;
  2472. spin_unlock(&dev_data->dte_lock);
  2473. /* Flush device DTE */
  2474. device_flush_dte(dev_data);
  2475. domain_flush = true;
  2476. }
  2477. /* Flush IOTLB to mark IOPTE dirty on the next translation(s) */
  2478. if (domain_flush)
  2479. amd_iommu_domain_flush_all(pdomain);
  2480. pdomain->dirty_tracking = enable;
  2481. spin_unlock_irqrestore(&pdomain->lock, flags);
  2482. return 0;
  2483. }
  2484. static void amd_iommu_get_resv_regions(struct device *dev,
  2485. struct list_head *head)
  2486. {
  2487. struct iommu_resv_region *region;
  2488. struct unity_map_entry *entry;
  2489. struct amd_iommu *iommu;
  2490. struct amd_iommu_pci_seg *pci_seg;
  2491. int devid, sbdf;
  2492. sbdf = get_device_sbdf_id(dev);
  2493. if (sbdf < 0)
  2494. return;
  2495. devid = PCI_SBDF_TO_DEVID(sbdf);
  2496. iommu = get_amd_iommu_from_dev(dev);
  2497. pci_seg = iommu->pci_seg;
  2498. list_for_each_entry(entry, &pci_seg->unity_map, list) {
  2499. int type, prot = 0;
  2500. size_t length;
  2501. if (devid < entry->devid_start || devid > entry->devid_end)
  2502. continue;
  2503. type = IOMMU_RESV_DIRECT;
  2504. length = entry->address_end - entry->address_start;
  2505. if (entry->prot & IOMMU_PROT_IR)
  2506. prot |= IOMMU_READ;
  2507. if (entry->prot & IOMMU_PROT_IW)
  2508. prot |= IOMMU_WRITE;
  2509. if (entry->prot & IOMMU_UNITY_MAP_FLAG_EXCL_RANGE)
  2510. /* Exclusion range */
  2511. type = IOMMU_RESV_RESERVED;
  2512. region = iommu_alloc_resv_region(entry->address_start,
  2513. length, prot, type,
  2514. GFP_KERNEL);
  2515. if (!region) {
  2516. dev_err(dev, "Out of memory allocating dm-regions\n");
  2517. return;
  2518. }
  2519. list_add_tail(&region->list, head);
  2520. }
  2521. region = iommu_alloc_resv_region(MSI_RANGE_START,
  2522. MSI_RANGE_END - MSI_RANGE_START + 1,
  2523. 0, IOMMU_RESV_MSI, GFP_KERNEL);
  2524. if (!region)
  2525. return;
  2526. list_add_tail(&region->list, head);
  2527. if (amd_iommu_ht_range_ignore())
  2528. return;
  2529. region = iommu_alloc_resv_region(HT_RANGE_START,
  2530. HT_RANGE_END - HT_RANGE_START + 1,
  2531. 0, IOMMU_RESV_RESERVED, GFP_KERNEL);
  2532. if (!region)
  2533. return;
  2534. list_add_tail(&region->list, head);
  2535. }
  2536. static bool amd_iommu_is_attach_deferred(struct device *dev)
  2537. {
  2538. struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
  2539. return dev_data->defer_attach;
  2540. }
  2541. static int amd_iommu_def_domain_type(struct device *dev)
  2542. {
  2543. struct iommu_dev_data *dev_data;
  2544. dev_data = dev_iommu_priv_get(dev);
  2545. if (!dev_data)
  2546. return 0;
  2547. /* Always use DMA domain for untrusted device */
  2548. if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted)
  2549. return IOMMU_DOMAIN_DMA;
  2550. /*
  2551. * Do not identity map IOMMUv2 capable devices when:
  2552. * - memory encryption is active, because some of those devices
  2553. * (AMD GPUs) don't have the encryption bit in their DMA-mask
  2554. * and require remapping.
  2555. * - SNP is enabled, because it prohibits DTE[Mode]=0.
  2556. */
  2557. if (pdev_pasid_supported(dev_data) &&
  2558. !cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&
  2559. !amd_iommu_snp_en) {
  2560. return IOMMU_DOMAIN_IDENTITY;
  2561. }
  2562. return 0;
  2563. }
  2564. static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain)
  2565. {
  2566. /* IOMMU_PTE_FC is always set */
  2567. return true;
  2568. }
  2569. const struct iommu_ops amd_iommu_ops = {
  2570. .capable = amd_iommu_capable,
  2571. .hw_info = amd_iommufd_hw_info,
  2572. .blocked_domain = &blocked_domain,
  2573. .release_domain = &blocked_domain,
  2574. .identity_domain = &identity_domain.domain,
  2575. .domain_alloc_paging_flags = amd_iommu_domain_alloc_paging_flags,
  2576. .domain_alloc_sva = amd_iommu_domain_alloc_sva,
  2577. .probe_device = amd_iommu_probe_device,
  2578. .release_device = amd_iommu_release_device,
  2579. .device_group = amd_iommu_device_group,
  2580. .get_resv_regions = amd_iommu_get_resv_regions,
  2581. .is_attach_deferred = amd_iommu_is_attach_deferred,
  2582. .def_domain_type = amd_iommu_def_domain_type,
  2583. .page_response = amd_iommu_page_response,
  2584. .get_viommu_size = amd_iommufd_get_viommu_size,
  2585. .viommu_init = amd_iommufd_viommu_init,
  2586. };
  2587. #ifdef CONFIG_IRQ_REMAP
  2588. /*****************************************************************************
  2589. *
  2590. * Interrupt Remapping Implementation
  2591. *
  2592. *****************************************************************************/
  2593. static struct irq_chip amd_ir_chip;
  2594. static DEFINE_SPINLOCK(iommu_table_lock);
  2595. static void iommu_flush_irt_and_complete(struct amd_iommu *iommu, u16 devid)
  2596. {
  2597. int ret;
  2598. u64 data;
  2599. unsigned long flags;
  2600. struct iommu_cmd cmd, cmd2;
  2601. if (iommu->irtcachedis_enabled)
  2602. return;
  2603. build_inv_irt(&cmd, devid);
  2604. raw_spin_lock_irqsave(&iommu->lock, flags);
  2605. data = get_cmdsem_val(iommu);
  2606. build_completion_wait(&cmd2, iommu, data);
  2607. ret = __iommu_queue_command_sync(iommu, &cmd, true);
  2608. if (ret)
  2609. goto out_err;
  2610. ret = __iommu_queue_command_sync(iommu, &cmd2, false);
  2611. if (ret)
  2612. goto out_err;
  2613. raw_spin_unlock_irqrestore(&iommu->lock, flags);
  2614. wait_on_sem(iommu, data);
  2615. return;
  2616. out_err:
  2617. raw_spin_unlock_irqrestore(&iommu->lock, flags);
  2618. }
  2619. static inline u8 iommu_get_int_tablen(struct iommu_dev_data *dev_data)
  2620. {
  2621. if (dev_data && dev_data->max_irqs == MAX_IRQS_PER_TABLE_2K)
  2622. return DTE_INTTABLEN_2K;
  2623. return DTE_INTTABLEN_512;
  2624. }
  2625. static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid,
  2626. struct irq_remap_table *table)
  2627. {
  2628. u64 new;
  2629. struct dev_table_entry *dte = &get_dev_table(iommu)[devid];
  2630. struct iommu_dev_data *dev_data = search_dev_data(iommu, devid);
  2631. if (dev_data)
  2632. spin_lock(&dev_data->dte_lock);
  2633. new = READ_ONCE(dte->data[2]);
  2634. new &= ~DTE_IRQ_PHYS_ADDR_MASK;
  2635. new |= iommu_virt_to_phys(table->table);
  2636. new |= DTE_IRQ_REMAP_INTCTL;
  2637. new |= iommu_get_int_tablen(dev_data);
  2638. new |= DTE_IRQ_REMAP_ENABLE;
  2639. WRITE_ONCE(dte->data[2], new);
  2640. if (dev_data)
  2641. spin_unlock(&dev_data->dte_lock);
  2642. }
  2643. static struct irq_remap_table *get_irq_table(struct amd_iommu *iommu, u16 devid)
  2644. {
  2645. struct irq_remap_table *table;
  2646. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  2647. if (WARN_ONCE(!pci_seg->rlookup_table[devid],
  2648. "%s: no iommu for devid %x:%x\n",
  2649. __func__, pci_seg->id, devid))
  2650. return NULL;
  2651. table = pci_seg->irq_lookup_table[devid];
  2652. if (WARN_ONCE(!table, "%s: no table for devid %x:%x\n",
  2653. __func__, pci_seg->id, devid))
  2654. return NULL;
  2655. return table;
  2656. }
  2657. static struct irq_remap_table *__alloc_irq_table(int nid, size_t size)
  2658. {
  2659. struct irq_remap_table *table;
  2660. table = kzalloc_obj(*table);
  2661. if (!table)
  2662. return NULL;
  2663. table->table = iommu_alloc_pages_node_sz(
  2664. nid, GFP_KERNEL, max(DTE_INTTAB_ALIGNMENT, size));
  2665. if (!table->table) {
  2666. kfree(table);
  2667. return NULL;
  2668. }
  2669. raw_spin_lock_init(&table->lock);
  2670. return table;
  2671. }
  2672. static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
  2673. struct irq_remap_table *table)
  2674. {
  2675. struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
  2676. pci_seg->irq_lookup_table[devid] = table;
  2677. set_dte_irq_entry(iommu, devid, table);
  2678. iommu_flush_dte(iommu, devid);
  2679. }
  2680. static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias,
  2681. void *data)
  2682. {
  2683. struct irq_remap_table *table = data;
  2684. struct amd_iommu_pci_seg *pci_seg;
  2685. struct amd_iommu *iommu = rlookup_amd_iommu(&pdev->dev);
  2686. if (!iommu)
  2687. return -EINVAL;
  2688. pci_seg = iommu->pci_seg;
  2689. pci_seg->irq_lookup_table[alias] = table;
  2690. set_dte_irq_entry(iommu, alias, table);
  2691. iommu_flush_dte(pci_seg->rlookup_table[alias], alias);
  2692. return 0;
  2693. }
  2694. static inline size_t get_irq_table_size(unsigned int max_irqs)
  2695. {
  2696. if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
  2697. return max_irqs * sizeof(u32);
  2698. return max_irqs * (sizeof(u64) * 2);
  2699. }
  2700. static struct irq_remap_table *alloc_irq_table(struct amd_iommu *iommu,
  2701. u16 devid, struct pci_dev *pdev,
  2702. unsigned int max_irqs)
  2703. {
  2704. struct irq_remap_table *table = NULL;
  2705. struct irq_remap_table *new_table = NULL;
  2706. struct amd_iommu_pci_seg *pci_seg;
  2707. unsigned long flags;
  2708. int nid = iommu->dev ? dev_to_node(&iommu->dev->dev) : NUMA_NO_NODE;
  2709. u16 alias;
  2710. spin_lock_irqsave(&iommu_table_lock, flags);
  2711. pci_seg = iommu->pci_seg;
  2712. table = pci_seg->irq_lookup_table[devid];
  2713. if (table)
  2714. goto out_unlock;
  2715. alias = pci_seg->alias_table[devid];
  2716. table = pci_seg->irq_lookup_table[alias];
  2717. if (table) {
  2718. set_remap_table_entry(iommu, devid, table);
  2719. goto out_wait;
  2720. }
  2721. spin_unlock_irqrestore(&iommu_table_lock, flags);
  2722. /* Nothing there yet, allocate new irq remapping table */
  2723. new_table = __alloc_irq_table(nid, get_irq_table_size(max_irqs));
  2724. if (!new_table)
  2725. return NULL;
  2726. spin_lock_irqsave(&iommu_table_lock, flags);
  2727. table = pci_seg->irq_lookup_table[devid];
  2728. if (table)
  2729. goto out_unlock;
  2730. table = pci_seg->irq_lookup_table[alias];
  2731. if (table) {
  2732. set_remap_table_entry(iommu, devid, table);
  2733. goto out_wait;
  2734. }
  2735. table = new_table;
  2736. new_table = NULL;
  2737. if (pdev)
  2738. pci_for_each_dma_alias(pdev, set_remap_table_entry_alias,
  2739. table);
  2740. else
  2741. set_remap_table_entry(iommu, devid, table);
  2742. if (devid != alias)
  2743. set_remap_table_entry(iommu, alias, table);
  2744. out_wait:
  2745. iommu_completion_wait(iommu);
  2746. out_unlock:
  2747. spin_unlock_irqrestore(&iommu_table_lock, flags);
  2748. if (new_table) {
  2749. iommu_free_pages(new_table->table);
  2750. kfree(new_table);
  2751. }
  2752. return table;
  2753. }
  2754. static int alloc_irq_index(struct amd_iommu *iommu, u16 devid, int count,
  2755. bool align, struct pci_dev *pdev,
  2756. unsigned long max_irqs)
  2757. {
  2758. struct irq_remap_table *table;
  2759. int index, c, alignment = 1;
  2760. unsigned long flags;
  2761. table = alloc_irq_table(iommu, devid, pdev, max_irqs);
  2762. if (!table)
  2763. return -ENODEV;
  2764. if (align)
  2765. alignment = roundup_pow_of_two(count);
  2766. raw_spin_lock_irqsave(&table->lock, flags);
  2767. /* Scan table for free entries */
  2768. for (index = ALIGN(table->min_index, alignment), c = 0;
  2769. index < max_irqs;) {
  2770. if (!iommu->irte_ops->is_allocated(table, index)) {
  2771. c += 1;
  2772. } else {
  2773. c = 0;
  2774. index = ALIGN(index + 1, alignment);
  2775. continue;
  2776. }
  2777. if (c == count) {
  2778. for (; c != 0; --c)
  2779. iommu->irte_ops->set_allocated(table, index - c + 1);
  2780. index -= count - 1;
  2781. goto out;
  2782. }
  2783. index++;
  2784. }
  2785. index = -ENOSPC;
  2786. out:
  2787. raw_spin_unlock_irqrestore(&table->lock, flags);
  2788. return index;
  2789. }
  2790. static int __modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index,
  2791. struct irte_ga *irte)
  2792. {
  2793. struct irq_remap_table *table;
  2794. struct irte_ga *entry;
  2795. unsigned long flags;
  2796. u128 old;
  2797. table = get_irq_table(iommu, devid);
  2798. if (!table)
  2799. return -ENOMEM;
  2800. raw_spin_lock_irqsave(&table->lock, flags);
  2801. entry = (struct irte_ga *)table->table;
  2802. entry = &entry[index];
  2803. /*
  2804. * We use cmpxchg16 to atomically update the 128-bit IRTE,
  2805. * and it cannot be updated by the hardware or other processors
  2806. * behind us, so the return value of cmpxchg16 should be the
  2807. * same as the old value.
  2808. */
  2809. old = entry->irte;
  2810. WARN_ON(!try_cmpxchg128(&entry->irte, &old, irte->irte));
  2811. raw_spin_unlock_irqrestore(&table->lock, flags);
  2812. return 0;
  2813. }
  2814. static int modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index,
  2815. struct irte_ga *irte)
  2816. {
  2817. int ret;
  2818. ret = __modify_irte_ga(iommu, devid, index, irte);
  2819. if (ret)
  2820. return ret;
  2821. iommu_flush_irt_and_complete(iommu, devid);
  2822. return 0;
  2823. }
  2824. static int modify_irte(struct amd_iommu *iommu,
  2825. u16 devid, int index, union irte *irte)
  2826. {
  2827. struct irq_remap_table *table;
  2828. unsigned long flags;
  2829. table = get_irq_table(iommu, devid);
  2830. if (!table)
  2831. return -ENOMEM;
  2832. raw_spin_lock_irqsave(&table->lock, flags);
  2833. table->table[index] = irte->val;
  2834. raw_spin_unlock_irqrestore(&table->lock, flags);
  2835. iommu_flush_irt_and_complete(iommu, devid);
  2836. return 0;
  2837. }
  2838. static void free_irte(struct amd_iommu *iommu, u16 devid, int index)
  2839. {
  2840. struct irq_remap_table *table;
  2841. unsigned long flags;
  2842. table = get_irq_table(iommu, devid);
  2843. if (!table)
  2844. return;
  2845. raw_spin_lock_irqsave(&table->lock, flags);
  2846. iommu->irte_ops->clear_allocated(table, index);
  2847. raw_spin_unlock_irqrestore(&table->lock, flags);
  2848. iommu_flush_irt_and_complete(iommu, devid);
  2849. }
  2850. static void irte_prepare(void *entry,
  2851. u32 delivery_mode, bool dest_mode,
  2852. u8 vector, u32 dest_apicid, int devid)
  2853. {
  2854. union irte *irte = (union irte *) entry;
  2855. irte->val = 0;
  2856. irte->fields.vector = vector;
  2857. irte->fields.int_type = delivery_mode;
  2858. irte->fields.destination = dest_apicid;
  2859. irte->fields.dm = dest_mode;
  2860. irte->fields.valid = 1;
  2861. }
  2862. static void irte_ga_prepare(void *entry,
  2863. u32 delivery_mode, bool dest_mode,
  2864. u8 vector, u32 dest_apicid, int devid)
  2865. {
  2866. struct irte_ga *irte = (struct irte_ga *) entry;
  2867. irte->lo.val = 0;
  2868. irte->hi.val = 0;
  2869. irte->lo.fields_remap.int_type = delivery_mode;
  2870. irte->lo.fields_remap.dm = dest_mode;
  2871. irte->hi.fields.vector = vector;
  2872. irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
  2873. irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
  2874. irte->lo.fields_remap.valid = 1;
  2875. }
  2876. static void irte_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
  2877. {
  2878. union irte *irte = (union irte *) entry;
  2879. irte->fields.valid = 1;
  2880. modify_irte(iommu, devid, index, irte);
  2881. }
  2882. static void irte_ga_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
  2883. {
  2884. struct irte_ga *irte = (struct irte_ga *) entry;
  2885. irte->lo.fields_remap.valid = 1;
  2886. modify_irte_ga(iommu, devid, index, irte);
  2887. }
  2888. static void irte_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
  2889. {
  2890. union irte *irte = (union irte *) entry;
  2891. irte->fields.valid = 0;
  2892. modify_irte(iommu, devid, index, irte);
  2893. }
  2894. static void irte_ga_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index)
  2895. {
  2896. struct irte_ga *irte = (struct irte_ga *) entry;
  2897. irte->lo.fields_remap.valid = 0;
  2898. modify_irte_ga(iommu, devid, index, irte);
  2899. }
  2900. static void irte_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
  2901. u8 vector, u32 dest_apicid)
  2902. {
  2903. union irte *irte = (union irte *) entry;
  2904. irte->fields.vector = vector;
  2905. irte->fields.destination = dest_apicid;
  2906. modify_irte(iommu, devid, index, irte);
  2907. }
  2908. static void irte_ga_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index,
  2909. u8 vector, u32 dest_apicid)
  2910. {
  2911. struct irte_ga *irte = (struct irte_ga *) entry;
  2912. if (!irte->lo.fields_remap.guest_mode) {
  2913. irte->hi.fields.vector = vector;
  2914. irte->lo.fields_remap.destination =
  2915. APICID_TO_IRTE_DEST_LO(dest_apicid);
  2916. irte->hi.fields.destination =
  2917. APICID_TO_IRTE_DEST_HI(dest_apicid);
  2918. modify_irte_ga(iommu, devid, index, irte);
  2919. }
  2920. }
  2921. #define IRTE_ALLOCATED (~1U)
  2922. static void irte_set_allocated(struct irq_remap_table *table, int index)
  2923. {
  2924. table->table[index] = IRTE_ALLOCATED;
  2925. }
  2926. static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
  2927. {
  2928. struct irte_ga *ptr = (struct irte_ga *)table->table;
  2929. struct irte_ga *irte = &ptr[index];
  2930. memset(&irte->lo.val, 0, sizeof(u64));
  2931. memset(&irte->hi.val, 0, sizeof(u64));
  2932. irte->hi.fields.vector = 0xff;
  2933. }
  2934. static bool irte_is_allocated(struct irq_remap_table *table, int index)
  2935. {
  2936. union irte *ptr = (union irte *)table->table;
  2937. union irte *irte = &ptr[index];
  2938. return irte->val != 0;
  2939. }
  2940. static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
  2941. {
  2942. struct irte_ga *ptr = (struct irte_ga *)table->table;
  2943. struct irte_ga *irte = &ptr[index];
  2944. return irte->hi.fields.vector != 0;
  2945. }
  2946. static void irte_clear_allocated(struct irq_remap_table *table, int index)
  2947. {
  2948. table->table[index] = 0;
  2949. }
  2950. static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
  2951. {
  2952. struct irte_ga *ptr = (struct irte_ga *)table->table;
  2953. struct irte_ga *irte = &ptr[index];
  2954. memset(&irte->lo.val, 0, sizeof(u64));
  2955. memset(&irte->hi.val, 0, sizeof(u64));
  2956. }
  2957. static int get_devid(struct irq_alloc_info *info)
  2958. {
  2959. switch (info->type) {
  2960. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  2961. return get_ioapic_devid(info->devid);
  2962. case X86_IRQ_ALLOC_TYPE_HPET:
  2963. return get_hpet_devid(info->devid);
  2964. case X86_IRQ_ALLOC_TYPE_PCI_MSI:
  2965. case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
  2966. return get_device_sbdf_id(msi_desc_to_dev(info->desc));
  2967. default:
  2968. WARN_ON_ONCE(1);
  2969. return -1;
  2970. }
  2971. }
  2972. struct irq_remap_ops amd_iommu_irq_ops = {
  2973. .prepare = amd_iommu_prepare,
  2974. .enable = amd_iommu_enable,
  2975. .disable = amd_iommu_disable,
  2976. .reenable = amd_iommu_reenable,
  2977. .enable_faulting = amd_iommu_enable_faulting,
  2978. };
  2979. static void fill_msi_msg(struct msi_msg *msg, u32 index)
  2980. {
  2981. msg->data = index;
  2982. msg->address_lo = 0;
  2983. msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
  2984. /*
  2985. * The struct msi_msg.dest_mode_logical is used to set the DM bit
  2986. * in MSI Message Address Register. For device w/ 2K int-remap support,
  2987. * this is bit must be set to 1 regardless of the actual destination
  2988. * mode, which is signified by the IRTE[DM].
  2989. */
  2990. if (FEATURE_NUM_INT_REMAP_SUP_2K(amd_iommu_efr2))
  2991. msg->arch_addr_lo.dest_mode_logical = true;
  2992. msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
  2993. }
  2994. static void irq_remapping_prepare_irte(struct amd_ir_data *data,
  2995. struct irq_cfg *irq_cfg,
  2996. struct irq_alloc_info *info,
  2997. int devid, int index, int sub_handle)
  2998. {
  2999. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3000. struct amd_iommu *iommu = data->iommu;
  3001. if (!iommu)
  3002. return;
  3003. data->irq_2_irte.devid = devid;
  3004. data->irq_2_irte.index = index + sub_handle;
  3005. iommu->irte_ops->prepare(data->entry, APIC_DELIVERY_MODE_FIXED,
  3006. apic->dest_mode_logical, irq_cfg->vector,
  3007. irq_cfg->dest_apicid, devid);
  3008. switch (info->type) {
  3009. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  3010. case X86_IRQ_ALLOC_TYPE_HPET:
  3011. case X86_IRQ_ALLOC_TYPE_PCI_MSI:
  3012. case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
  3013. fill_msi_msg(&data->msi_entry, irte_info->index);
  3014. break;
  3015. default:
  3016. BUG_ON(1);
  3017. break;
  3018. }
  3019. }
  3020. struct amd_irte_ops irte_32_ops = {
  3021. .prepare = irte_prepare,
  3022. .activate = irte_activate,
  3023. .deactivate = irte_deactivate,
  3024. .set_affinity = irte_set_affinity,
  3025. .set_allocated = irte_set_allocated,
  3026. .is_allocated = irte_is_allocated,
  3027. .clear_allocated = irte_clear_allocated,
  3028. };
  3029. struct amd_irte_ops irte_128_ops = {
  3030. .prepare = irte_ga_prepare,
  3031. .activate = irte_ga_activate,
  3032. .deactivate = irte_ga_deactivate,
  3033. .set_affinity = irte_ga_set_affinity,
  3034. .set_allocated = irte_ga_set_allocated,
  3035. .is_allocated = irte_ga_is_allocated,
  3036. .clear_allocated = irte_ga_clear_allocated,
  3037. };
  3038. static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
  3039. unsigned int nr_irqs, void *arg)
  3040. {
  3041. struct irq_alloc_info *info = arg;
  3042. struct irq_data *irq_data;
  3043. struct amd_ir_data *data = NULL;
  3044. struct amd_iommu *iommu;
  3045. struct irq_cfg *cfg;
  3046. struct iommu_dev_data *dev_data;
  3047. unsigned long max_irqs;
  3048. int i, ret, devid, seg, sbdf;
  3049. int index;
  3050. if (!info)
  3051. return -EINVAL;
  3052. if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI)
  3053. return -EINVAL;
  3054. sbdf = get_devid(info);
  3055. if (sbdf < 0)
  3056. return -EINVAL;
  3057. seg = PCI_SBDF_TO_SEGID(sbdf);
  3058. devid = PCI_SBDF_TO_DEVID(sbdf);
  3059. iommu = __rlookup_amd_iommu(seg, devid);
  3060. if (!iommu)
  3061. return -EINVAL;
  3062. dev_data = search_dev_data(iommu, devid);
  3063. max_irqs = dev_data ? dev_data->max_irqs : MAX_IRQS_PER_TABLE_512;
  3064. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  3065. if (ret < 0)
  3066. return ret;
  3067. if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
  3068. struct irq_remap_table *table;
  3069. table = alloc_irq_table(iommu, devid, NULL, max_irqs);
  3070. if (table) {
  3071. if (!table->min_index) {
  3072. /*
  3073. * Keep the first 32 indexes free for IOAPIC
  3074. * interrupts.
  3075. */
  3076. table->min_index = 32;
  3077. for (i = 0; i < 32; ++i)
  3078. iommu->irte_ops->set_allocated(table, i);
  3079. }
  3080. WARN_ON(table->min_index != 32);
  3081. index = info->ioapic.pin;
  3082. } else {
  3083. index = -ENOMEM;
  3084. }
  3085. } else if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI ||
  3086. info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX) {
  3087. bool align = (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI);
  3088. index = alloc_irq_index(iommu, devid, nr_irqs, align,
  3089. msi_desc_to_pci_dev(info->desc),
  3090. max_irqs);
  3091. } else {
  3092. index = alloc_irq_index(iommu, devid, nr_irqs, false, NULL,
  3093. max_irqs);
  3094. }
  3095. if (index < 0) {
  3096. pr_warn("Failed to allocate IRTE\n");
  3097. ret = index;
  3098. goto out_free_parent;
  3099. }
  3100. for (i = 0; i < nr_irqs; i++) {
  3101. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3102. cfg = irq_data ? irqd_cfg(irq_data) : NULL;
  3103. if (!cfg) {
  3104. ret = -EINVAL;
  3105. goto out_free_data;
  3106. }
  3107. ret = -ENOMEM;
  3108. data = kzalloc_obj(*data);
  3109. if (!data)
  3110. goto out_free_data;
  3111. if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
  3112. data->entry = kzalloc_obj(union irte);
  3113. else
  3114. data->entry = kzalloc_obj(struct irte_ga);
  3115. if (!data->entry) {
  3116. kfree(data);
  3117. goto out_free_data;
  3118. }
  3119. data->iommu = iommu;
  3120. irq_data->hwirq = (devid << 16) + i;
  3121. irq_data->chip_data = data;
  3122. irq_data->chip = &amd_ir_chip;
  3123. irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
  3124. }
  3125. return 0;
  3126. out_free_data:
  3127. for (i--; i >= 0; i--) {
  3128. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3129. if (irq_data)
  3130. kfree(irq_data->chip_data);
  3131. }
  3132. for (i = 0; i < nr_irqs; i++)
  3133. free_irte(iommu, devid, index + i);
  3134. out_free_parent:
  3135. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  3136. return ret;
  3137. }
  3138. static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
  3139. unsigned int nr_irqs)
  3140. {
  3141. struct irq_2_irte *irte_info;
  3142. struct irq_data *irq_data;
  3143. struct amd_ir_data *data;
  3144. int i;
  3145. for (i = 0; i < nr_irqs; i++) {
  3146. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3147. if (irq_data && irq_data->chip_data) {
  3148. data = irq_data->chip_data;
  3149. irte_info = &data->irq_2_irte;
  3150. free_irte(data->iommu, irte_info->devid, irte_info->index);
  3151. kfree(data->entry);
  3152. kfree(data);
  3153. }
  3154. }
  3155. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  3156. }
  3157. static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
  3158. struct amd_ir_data *ir_data,
  3159. struct irq_2_irte *irte_info,
  3160. struct irq_cfg *cfg);
  3161. static int irq_remapping_activate(struct irq_domain *domain,
  3162. struct irq_data *irq_data, bool reserve)
  3163. {
  3164. struct amd_ir_data *data = irq_data->chip_data;
  3165. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3166. struct amd_iommu *iommu = data->iommu;
  3167. struct irq_cfg *cfg = irqd_cfg(irq_data);
  3168. if (!iommu)
  3169. return 0;
  3170. iommu->irte_ops->activate(iommu, data->entry, irte_info->devid,
  3171. irte_info->index);
  3172. amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
  3173. return 0;
  3174. }
  3175. static void irq_remapping_deactivate(struct irq_domain *domain,
  3176. struct irq_data *irq_data)
  3177. {
  3178. struct amd_ir_data *data = irq_data->chip_data;
  3179. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3180. struct amd_iommu *iommu = data->iommu;
  3181. if (iommu)
  3182. iommu->irte_ops->deactivate(iommu, data->entry, irte_info->devid,
  3183. irte_info->index);
  3184. }
  3185. static int irq_remapping_select(struct irq_domain *d, struct irq_fwspec *fwspec,
  3186. enum irq_domain_bus_token bus_token)
  3187. {
  3188. struct amd_iommu *iommu;
  3189. int devid = -1;
  3190. if (!amd_iommu_irq_remap)
  3191. return 0;
  3192. if (x86_fwspec_is_ioapic(fwspec))
  3193. devid = get_ioapic_devid(fwspec->param[0]);
  3194. else if (x86_fwspec_is_hpet(fwspec))
  3195. devid = get_hpet_devid(fwspec->param[0]);
  3196. if (devid < 0)
  3197. return 0;
  3198. iommu = __rlookup_amd_iommu((devid >> 16), (devid & 0xffff));
  3199. return iommu && iommu->ir_domain == d;
  3200. }
  3201. static const struct irq_domain_ops amd_ir_domain_ops = {
  3202. .select = irq_remapping_select,
  3203. .alloc = irq_remapping_alloc,
  3204. .free = irq_remapping_free,
  3205. .activate = irq_remapping_activate,
  3206. .deactivate = irq_remapping_deactivate,
  3207. };
  3208. static void __amd_iommu_update_ga(struct irte_ga *entry, int cpu,
  3209. bool ga_log_intr)
  3210. {
  3211. if (cpu >= 0) {
  3212. entry->lo.fields_vapic.destination =
  3213. APICID_TO_IRTE_DEST_LO(cpu);
  3214. entry->hi.fields.destination =
  3215. APICID_TO_IRTE_DEST_HI(cpu);
  3216. entry->lo.fields_vapic.is_run = true;
  3217. entry->lo.fields_vapic.ga_log_intr = false;
  3218. } else {
  3219. entry->lo.fields_vapic.is_run = false;
  3220. entry->lo.fields_vapic.ga_log_intr = ga_log_intr;
  3221. }
  3222. }
  3223. /*
  3224. * Update the pCPU information for an IRTE that is configured to post IRQs to
  3225. * a vCPU, without issuing an IOMMU invalidation for the IRTE.
  3226. *
  3227. * If the vCPU is associated with a pCPU (@cpu >= 0), configure the Destination
  3228. * with the pCPU's APIC ID, set IsRun, and clear GALogIntr. If the vCPU isn't
  3229. * associated with a pCPU (@cpu < 0), clear IsRun and set/clear GALogIntr based
  3230. * on input from the caller (e.g. KVM only requests GALogIntr when the vCPU is
  3231. * blocking and requires a notification wake event). I.e. treat vCPUs that are
  3232. * associated with a pCPU as running. This API is intended to be used when a
  3233. * vCPU is scheduled in/out (or stops running for any reason), to do a fast
  3234. * update of IsRun, GALogIntr, and (conditionally) Destination.
  3235. *
  3236. * Per the IOMMU spec, the Destination, IsRun, and GATag fields are not cached
  3237. * and thus don't require an invalidation to ensure the IOMMU consumes fresh
  3238. * information.
  3239. */
  3240. int amd_iommu_update_ga(void *data, int cpu, bool ga_log_intr)
  3241. {
  3242. struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
  3243. struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
  3244. if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))
  3245. return -EINVAL;
  3246. if (!entry || !entry->lo.fields_vapic.guest_mode)
  3247. return 0;
  3248. if (!ir_data->iommu)
  3249. return -ENODEV;
  3250. __amd_iommu_update_ga(entry, cpu, ga_log_intr);
  3251. return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
  3252. ir_data->irq_2_irte.index, entry);
  3253. }
  3254. EXPORT_SYMBOL(amd_iommu_update_ga);
  3255. int amd_iommu_activate_guest_mode(void *data, int cpu, bool ga_log_intr)
  3256. {
  3257. struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
  3258. struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
  3259. u64 valid;
  3260. if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))
  3261. return -EINVAL;
  3262. if (!entry)
  3263. return 0;
  3264. valid = entry->lo.fields_vapic.valid;
  3265. entry->lo.val = 0;
  3266. entry->hi.val = 0;
  3267. entry->lo.fields_vapic.valid = valid;
  3268. entry->lo.fields_vapic.guest_mode = 1;
  3269. entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr;
  3270. entry->hi.fields.vector = ir_data->ga_vector;
  3271. entry->lo.fields_vapic.ga_tag = ir_data->ga_tag;
  3272. __amd_iommu_update_ga(entry, cpu, ga_log_intr);
  3273. return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
  3274. ir_data->irq_2_irte.index, entry);
  3275. }
  3276. EXPORT_SYMBOL(amd_iommu_activate_guest_mode);
  3277. int amd_iommu_deactivate_guest_mode(void *data)
  3278. {
  3279. struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
  3280. struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
  3281. struct irq_cfg *cfg = ir_data->cfg;
  3282. u64 valid;
  3283. if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))
  3284. return -EINVAL;
  3285. if (!entry || !entry->lo.fields_vapic.guest_mode)
  3286. return 0;
  3287. valid = entry->lo.fields_remap.valid;
  3288. entry->lo.val = 0;
  3289. entry->hi.val = 0;
  3290. entry->lo.fields_remap.valid = valid;
  3291. entry->lo.fields_remap.dm = apic->dest_mode_logical;
  3292. entry->lo.fields_remap.int_type = APIC_DELIVERY_MODE_FIXED;
  3293. entry->hi.fields.vector = cfg->vector;
  3294. entry->lo.fields_remap.destination =
  3295. APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
  3296. entry->hi.fields.destination =
  3297. APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
  3298. return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid,
  3299. ir_data->irq_2_irte.index, entry);
  3300. }
  3301. EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode);
  3302. static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *info)
  3303. {
  3304. int ret;
  3305. struct amd_iommu_pi_data *pi_data = info;
  3306. struct amd_ir_data *ir_data = data->chip_data;
  3307. struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
  3308. struct iommu_dev_data *dev_data;
  3309. if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)))
  3310. return -EINVAL;
  3311. if (ir_data->iommu == NULL)
  3312. return -EINVAL;
  3313. dev_data = search_dev_data(ir_data->iommu, irte_info->devid);
  3314. /* Note:
  3315. * This device has never been set up for guest mode.
  3316. * we should not modify the IRTE
  3317. */
  3318. if (!dev_data || !dev_data->use_vapic)
  3319. return -EINVAL;
  3320. ir_data->cfg = irqd_cfg(data);
  3321. if (pi_data) {
  3322. pi_data->ir_data = ir_data;
  3323. ir_data->ga_root_ptr = (pi_data->vapic_addr >> 12);
  3324. ir_data->ga_vector = pi_data->vector;
  3325. ir_data->ga_tag = pi_data->ga_tag;
  3326. if (pi_data->is_guest_mode)
  3327. ret = amd_iommu_activate_guest_mode(ir_data, pi_data->cpu,
  3328. pi_data->ga_log_intr);
  3329. else
  3330. ret = amd_iommu_deactivate_guest_mode(ir_data);
  3331. } else {
  3332. ret = amd_iommu_deactivate_guest_mode(ir_data);
  3333. }
  3334. return ret;
  3335. }
  3336. static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
  3337. struct amd_ir_data *ir_data,
  3338. struct irq_2_irte *irte_info,
  3339. struct irq_cfg *cfg)
  3340. {
  3341. /*
  3342. * Atomically updates the IRTE with the new destination, vector
  3343. * and flushes the interrupt entry cache.
  3344. */
  3345. iommu->irte_ops->set_affinity(iommu, ir_data->entry, irte_info->devid,
  3346. irte_info->index, cfg->vector,
  3347. cfg->dest_apicid);
  3348. }
  3349. static int amd_ir_set_affinity(struct irq_data *data,
  3350. const struct cpumask *mask, bool force)
  3351. {
  3352. struct amd_ir_data *ir_data = data->chip_data;
  3353. struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
  3354. struct irq_cfg *cfg = irqd_cfg(data);
  3355. struct irq_data *parent = data->parent_data;
  3356. struct amd_iommu *iommu = ir_data->iommu;
  3357. int ret;
  3358. if (!iommu)
  3359. return -ENODEV;
  3360. ret = parent->chip->irq_set_affinity(parent, mask, force);
  3361. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  3362. return ret;
  3363. amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
  3364. /*
  3365. * After this point, all the interrupts will start arriving
  3366. * at the new destination. So, time to cleanup the previous
  3367. * vector allocation.
  3368. */
  3369. vector_schedule_cleanup(cfg);
  3370. return IRQ_SET_MASK_OK_DONE;
  3371. }
  3372. static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
  3373. {
  3374. struct amd_ir_data *ir_data = irq_data->chip_data;
  3375. *msg = ir_data->msi_entry;
  3376. }
  3377. static struct irq_chip amd_ir_chip = {
  3378. .name = "AMD-IR",
  3379. .irq_ack = apic_ack_irq,
  3380. .irq_set_affinity = amd_ir_set_affinity,
  3381. .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
  3382. .irq_compose_msi_msg = ir_compose_msi_msg,
  3383. };
  3384. static const struct msi_parent_ops amdvi_msi_parent_ops = {
  3385. .supported_flags = X86_VECTOR_MSI_FLAGS_SUPPORTED | MSI_FLAG_MULTI_PCI_MSI,
  3386. .bus_select_token = DOMAIN_BUS_AMDVI,
  3387. .bus_select_mask = MATCH_PCI_MSI,
  3388. .prefix = "IR-",
  3389. .init_dev_msi_info = msi_parent_init_dev_msi_info,
  3390. };
  3391. int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
  3392. {
  3393. struct irq_domain_info info = {
  3394. .fwnode = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index),
  3395. .ops = &amd_ir_domain_ops,
  3396. .domain_flags = IRQ_DOMAIN_FLAG_ISOLATED_MSI,
  3397. .host_data = iommu,
  3398. .parent = arch_get_ir_parent_domain(),
  3399. };
  3400. if (!info.fwnode)
  3401. return -ENOMEM;
  3402. iommu->ir_domain = msi_create_parent_irq_domain(&info, &amdvi_msi_parent_ops);
  3403. if (!iommu->ir_domain) {
  3404. irq_domain_free_fwnode(info.fwnode);
  3405. return -ENOMEM;
  3406. }
  3407. return 0;
  3408. }
  3409. #endif
  3410. MODULE_IMPORT_NS("GENERIC_PT_IOMMU");