amd_iommu.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497
  1. /*
  2. * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <jroedel@suse.de>
  4. * Leo Duran <leo.duran@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/ratelimit.h>
  20. #include <linux/pci.h>
  21. #include <linux/acpi.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pci-ats.h>
  25. #include <linux/bitmap.h>
  26. #include <linux/slab.h>
  27. #include <linux/debugfs.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/dma-direct.h>
  31. #include <linux/iommu-helper.h>
  32. #include <linux/iommu.h>
  33. #include <linux/delay.h>
  34. #include <linux/amd-iommu.h>
  35. #include <linux/notifier.h>
  36. #include <linux/export.h>
  37. #include <linux/irq.h>
  38. #include <linux/msi.h>
  39. #include <linux/dma-contiguous.h>
  40. #include <linux/irqdomain.h>
  41. #include <linux/percpu.h>
  42. #include <linux/iova.h>
  43. #include <asm/irq_remapping.h>
  44. #include <asm/io_apic.h>
  45. #include <asm/apic.h>
  46. #include <asm/hw_irq.h>
  47. #include <asm/msidef.h>
  48. #include <asm/proto.h>
  49. #include <asm/iommu.h>
  50. #include <asm/gart.h>
  51. #include <asm/dma.h>
  52. #include "amd_iommu_proto.h"
  53. #include "amd_iommu_types.h"
  54. #include "irq_remapping.h"
  55. #define AMD_IOMMU_MAPPING_ERROR 0
  56. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  57. #define LOOP_TIMEOUT 100000
  58. /* IO virtual address start page frame number */
  59. #define IOVA_START_PFN (1)
  60. #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
  61. /* Reserved IOVA ranges */
  62. #define MSI_RANGE_START (0xfee00000)
  63. #define MSI_RANGE_END (0xfeefffff)
  64. #define HT_RANGE_START (0xfd00000000ULL)
  65. #define HT_RANGE_END (0xffffffffffULL)
  66. /*
  67. * This bitmap is used to advertise the page sizes our hardware support
  68. * to the IOMMU core, which will then use this information to split
  69. * physically contiguous memory regions it is mapping into page sizes
  70. * that we support.
  71. *
  72. * 512GB Pages are not supported due to a hardware bug
  73. */
  74. #define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
  75. static DEFINE_SPINLOCK(amd_iommu_devtable_lock);
  76. static DEFINE_SPINLOCK(pd_bitmap_lock);
  77. /* List of all available dev_data structures */
  78. static LLIST_HEAD(dev_data_list);
  79. LIST_HEAD(ioapic_map);
  80. LIST_HEAD(hpet_map);
  81. LIST_HEAD(acpihid_map);
  82. /*
  83. * Domain for untranslated devices - only allocated
  84. * if iommu=pt passed on kernel cmd line.
  85. */
  86. const struct iommu_ops amd_iommu_ops;
  87. static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
  88. int amd_iommu_max_glx_val = -1;
  89. static const struct dma_map_ops amd_iommu_dma_ops;
  90. /*
  91. * general struct to manage commands send to an IOMMU
  92. */
  93. struct iommu_cmd {
  94. u32 data[4];
  95. };
  96. struct kmem_cache *amd_iommu_irq_cache;
  97. static void update_domain(struct protection_domain *domain);
  98. static int protection_domain_init(struct protection_domain *domain);
  99. static void detach_device(struct device *dev);
  100. static void iova_domain_flush_tlb(struct iova_domain *iovad);
  101. /*
  102. * Data container for a dma_ops specific protection domain
  103. */
  104. struct dma_ops_domain {
  105. /* generic protection domain information */
  106. struct protection_domain domain;
  107. /* IOVA RB-Tree */
  108. struct iova_domain iovad;
  109. };
  110. static struct iova_domain reserved_iova_ranges;
  111. static struct lock_class_key reserved_rbtree_key;
  112. /****************************************************************************
  113. *
  114. * Helper functions
  115. *
  116. ****************************************************************************/
  117. static inline int match_hid_uid(struct device *dev,
  118. struct acpihid_map_entry *entry)
  119. {
  120. const char *hid, *uid;
  121. hid = acpi_device_hid(ACPI_COMPANION(dev));
  122. uid = acpi_device_uid(ACPI_COMPANION(dev));
  123. if (!hid || !(*hid))
  124. return -ENODEV;
  125. if (!uid || !(*uid))
  126. return strcmp(hid, entry->hid);
  127. if (!(*entry->uid))
  128. return strcmp(hid, entry->hid);
  129. return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid));
  130. }
  131. static inline u16 get_pci_device_id(struct device *dev)
  132. {
  133. struct pci_dev *pdev = to_pci_dev(dev);
  134. return PCI_DEVID(pdev->bus->number, pdev->devfn);
  135. }
  136. static inline int get_acpihid_device_id(struct device *dev,
  137. struct acpihid_map_entry **entry)
  138. {
  139. struct acpihid_map_entry *p;
  140. list_for_each_entry(p, &acpihid_map, list) {
  141. if (!match_hid_uid(dev, p)) {
  142. if (entry)
  143. *entry = p;
  144. return p->devid;
  145. }
  146. }
  147. return -EINVAL;
  148. }
  149. static inline int get_device_id(struct device *dev)
  150. {
  151. int devid;
  152. if (dev_is_pci(dev))
  153. devid = get_pci_device_id(dev);
  154. else
  155. devid = get_acpihid_device_id(dev, NULL);
  156. return devid;
  157. }
  158. static struct protection_domain *to_pdomain(struct iommu_domain *dom)
  159. {
  160. return container_of(dom, struct protection_domain, domain);
  161. }
  162. static struct dma_ops_domain* to_dma_ops_domain(struct protection_domain *domain)
  163. {
  164. BUG_ON(domain->flags != PD_DMA_OPS_MASK);
  165. return container_of(domain, struct dma_ops_domain, domain);
  166. }
  167. static struct iommu_dev_data *alloc_dev_data(u16 devid)
  168. {
  169. struct iommu_dev_data *dev_data;
  170. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  171. if (!dev_data)
  172. return NULL;
  173. dev_data->devid = devid;
  174. ratelimit_default_init(&dev_data->rs);
  175. llist_add(&dev_data->dev_data_list, &dev_data_list);
  176. return dev_data;
  177. }
  178. static struct iommu_dev_data *search_dev_data(u16 devid)
  179. {
  180. struct iommu_dev_data *dev_data;
  181. struct llist_node *node;
  182. if (llist_empty(&dev_data_list))
  183. return NULL;
  184. node = dev_data_list.first;
  185. llist_for_each_entry(dev_data, node, dev_data_list) {
  186. if (dev_data->devid == devid)
  187. return dev_data;
  188. }
  189. return NULL;
  190. }
  191. static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
  192. {
  193. *(u16 *)data = alias;
  194. return 0;
  195. }
  196. static u16 get_alias(struct device *dev)
  197. {
  198. struct pci_dev *pdev = to_pci_dev(dev);
  199. u16 devid, ivrs_alias, pci_alias;
  200. /* The callers make sure that get_device_id() does not fail here */
  201. devid = get_device_id(dev);
  202. /* For ACPI HID devices, we simply return the devid as such */
  203. if (!dev_is_pci(dev))
  204. return devid;
  205. ivrs_alias = amd_iommu_alias_table[devid];
  206. pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
  207. if (ivrs_alias == pci_alias)
  208. return ivrs_alias;
  209. /*
  210. * DMA alias showdown
  211. *
  212. * The IVRS is fairly reliable in telling us about aliases, but it
  213. * can't know about every screwy device. If we don't have an IVRS
  214. * reported alias, use the PCI reported alias. In that case we may
  215. * still need to initialize the rlookup and dev_table entries if the
  216. * alias is to a non-existent device.
  217. */
  218. if (ivrs_alias == devid) {
  219. if (!amd_iommu_rlookup_table[pci_alias]) {
  220. amd_iommu_rlookup_table[pci_alias] =
  221. amd_iommu_rlookup_table[devid];
  222. memcpy(amd_iommu_dev_table[pci_alias].data,
  223. amd_iommu_dev_table[devid].data,
  224. sizeof(amd_iommu_dev_table[pci_alias].data));
  225. }
  226. return pci_alias;
  227. }
  228. pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
  229. "for device %s[%04x:%04x], kernel reported alias "
  230. "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
  231. PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
  232. PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
  233. PCI_FUNC(pci_alias));
  234. /*
  235. * If we don't have a PCI DMA alias and the IVRS alias is on the same
  236. * bus, then the IVRS table may know about a quirk that we don't.
  237. */
  238. if (pci_alias == devid &&
  239. PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
  240. pci_add_dma_alias(pdev, ivrs_alias & 0xff);
  241. pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
  242. PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
  243. dev_name(dev));
  244. }
  245. return ivrs_alias;
  246. }
  247. static struct iommu_dev_data *find_dev_data(u16 devid)
  248. {
  249. struct iommu_dev_data *dev_data;
  250. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  251. dev_data = search_dev_data(devid);
  252. if (dev_data == NULL) {
  253. dev_data = alloc_dev_data(devid);
  254. if (!dev_data)
  255. return NULL;
  256. if (translation_pre_enabled(iommu))
  257. dev_data->defer_attach = true;
  258. }
  259. return dev_data;
  260. }
  261. struct iommu_dev_data *get_dev_data(struct device *dev)
  262. {
  263. return dev->archdata.iommu;
  264. }
  265. EXPORT_SYMBOL(get_dev_data);
  266. /*
  267. * Find or create an IOMMU group for a acpihid device.
  268. */
  269. static struct iommu_group *acpihid_device_group(struct device *dev)
  270. {
  271. struct acpihid_map_entry *p, *entry = NULL;
  272. int devid;
  273. devid = get_acpihid_device_id(dev, &entry);
  274. if (devid < 0)
  275. return ERR_PTR(devid);
  276. list_for_each_entry(p, &acpihid_map, list) {
  277. if ((devid == p->devid) && p->group)
  278. entry->group = p->group;
  279. }
  280. if (!entry->group)
  281. entry->group = generic_device_group(dev);
  282. else
  283. iommu_group_ref_get(entry->group);
  284. return entry->group;
  285. }
  286. static bool pci_iommuv2_capable(struct pci_dev *pdev)
  287. {
  288. static const int caps[] = {
  289. PCI_EXT_CAP_ID_ATS,
  290. PCI_EXT_CAP_ID_PRI,
  291. PCI_EXT_CAP_ID_PASID,
  292. };
  293. int i, pos;
  294. if (pci_ats_disabled())
  295. return false;
  296. for (i = 0; i < 3; ++i) {
  297. pos = pci_find_ext_capability(pdev, caps[i]);
  298. if (pos == 0)
  299. return false;
  300. }
  301. return true;
  302. }
  303. static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum)
  304. {
  305. struct iommu_dev_data *dev_data;
  306. dev_data = get_dev_data(&pdev->dev);
  307. return dev_data->errata & (1 << erratum) ? true : false;
  308. }
  309. /*
  310. * This function checks if the driver got a valid device from the caller to
  311. * avoid dereferencing invalid pointers.
  312. */
  313. static bool check_device(struct device *dev)
  314. {
  315. int devid;
  316. if (!dev || !dev->dma_mask)
  317. return false;
  318. devid = get_device_id(dev);
  319. if (devid < 0)
  320. return false;
  321. /* Out of our scope? */
  322. if (devid > amd_iommu_last_bdf)
  323. return false;
  324. if (amd_iommu_rlookup_table[devid] == NULL)
  325. return false;
  326. return true;
  327. }
  328. static void init_iommu_group(struct device *dev)
  329. {
  330. struct iommu_group *group;
  331. group = iommu_group_get_for_dev(dev);
  332. if (IS_ERR(group))
  333. return;
  334. iommu_group_put(group);
  335. }
  336. static int iommu_init_device(struct device *dev)
  337. {
  338. struct iommu_dev_data *dev_data;
  339. struct amd_iommu *iommu;
  340. int devid;
  341. if (dev->archdata.iommu)
  342. return 0;
  343. devid = get_device_id(dev);
  344. if (devid < 0)
  345. return devid;
  346. iommu = amd_iommu_rlookup_table[devid];
  347. dev_data = find_dev_data(devid);
  348. if (!dev_data)
  349. return -ENOMEM;
  350. dev_data->alias = get_alias(dev);
  351. /*
  352. * By default we use passthrough mode for IOMMUv2 capable device.
  353. * But if amd_iommu=force_isolation is set (e.g. to debug DMA to
  354. * invalid address), we ignore the capability for the device so
  355. * it'll be forced to go into translation mode.
  356. */
  357. if ((iommu_pass_through || !amd_iommu_force_isolation) &&
  358. dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) {
  359. struct amd_iommu *iommu;
  360. iommu = amd_iommu_rlookup_table[dev_data->devid];
  361. dev_data->iommu_v2 = iommu->is_iommu_v2;
  362. }
  363. dev->archdata.iommu = dev_data;
  364. iommu_device_link(&iommu->iommu, dev);
  365. return 0;
  366. }
  367. static void iommu_ignore_device(struct device *dev)
  368. {
  369. u16 alias;
  370. int devid;
  371. devid = get_device_id(dev);
  372. if (devid < 0)
  373. return;
  374. alias = get_alias(dev);
  375. memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
  376. memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
  377. amd_iommu_rlookup_table[devid] = NULL;
  378. amd_iommu_rlookup_table[alias] = NULL;
  379. }
  380. static void iommu_uninit_device(struct device *dev)
  381. {
  382. struct iommu_dev_data *dev_data;
  383. struct amd_iommu *iommu;
  384. int devid;
  385. devid = get_device_id(dev);
  386. if (devid < 0)
  387. return;
  388. iommu = amd_iommu_rlookup_table[devid];
  389. dev_data = search_dev_data(devid);
  390. if (!dev_data)
  391. return;
  392. if (dev_data->domain)
  393. detach_device(dev);
  394. iommu_device_unlink(&iommu->iommu, dev);
  395. iommu_group_remove_device(dev);
  396. /* Remove dma-ops */
  397. dev->dma_ops = NULL;
  398. /*
  399. * We keep dev_data around for unplugged devices and reuse it when the
  400. * device is re-plugged - not doing so would introduce a ton of races.
  401. */
  402. }
  403. /****************************************************************************
  404. *
  405. * Interrupt handling functions
  406. *
  407. ****************************************************************************/
  408. static void dump_dte_entry(u16 devid)
  409. {
  410. int i;
  411. for (i = 0; i < 4; ++i)
  412. pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
  413. amd_iommu_dev_table[devid].data[i]);
  414. }
  415. static void dump_command(unsigned long phys_addr)
  416. {
  417. struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr);
  418. int i;
  419. for (i = 0; i < 4; ++i)
  420. pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
  421. }
  422. static void amd_iommu_report_page_fault(u16 devid, u16 domain_id,
  423. u64 address, int flags)
  424. {
  425. struct iommu_dev_data *dev_data = NULL;
  426. struct pci_dev *pdev;
  427. pdev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(devid),
  428. devid & 0xff);
  429. if (pdev)
  430. dev_data = get_dev_data(&pdev->dev);
  431. if (dev_data && __ratelimit(&dev_data->rs)) {
  432. dev_err(&pdev->dev, "AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  433. domain_id, address, flags);
  434. } else if (printk_ratelimit()) {
  435. pr_err("AMD-Vi: Event logged [IO_PAGE_FAULT device=%02x:%02x.%x domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  436. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  437. domain_id, address, flags);
  438. }
  439. if (pdev)
  440. pci_dev_put(pdev);
  441. }
  442. static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  443. {
  444. struct device *dev = iommu->iommu.dev;
  445. int type, devid, pasid, flags, tag;
  446. volatile u32 *event = __evt;
  447. int count = 0;
  448. u64 address;
  449. retry:
  450. type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  451. devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  452. pasid = PPR_PASID(*(u64 *)&event[0]);
  453. flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  454. address = (u64)(((u64)event[3]) << 32) | event[2];
  455. if (type == 0) {
  456. /* Did we hit the erratum? */
  457. if (++count == LOOP_TIMEOUT) {
  458. pr_err("AMD-Vi: No event written to event log\n");
  459. return;
  460. }
  461. udelay(1);
  462. goto retry;
  463. }
  464. if (type == EVENT_TYPE_IO_FAULT) {
  465. amd_iommu_report_page_fault(devid, pasid, address, flags);
  466. return;
  467. } else {
  468. dev_err(dev, "AMD-Vi: Event logged [");
  469. }
  470. switch (type) {
  471. case EVENT_TYPE_ILL_DEV:
  472. dev_err(dev, "ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x pasid=0x%05x address=0x%016llx flags=0x%04x]\n",
  473. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  474. pasid, address, flags);
  475. dump_dte_entry(devid);
  476. break;
  477. case EVENT_TYPE_DEV_TAB_ERR:
  478. dev_err(dev, "DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  479. "address=0x%016llx flags=0x%04x]\n",
  480. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  481. address, flags);
  482. break;
  483. case EVENT_TYPE_PAGE_TAB_ERR:
  484. dev_err(dev, "PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  485. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  486. pasid, address, flags);
  487. break;
  488. case EVENT_TYPE_ILL_CMD:
  489. dev_err(dev, "ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  490. dump_command(address);
  491. break;
  492. case EVENT_TYPE_CMD_HARD_ERR:
  493. dev_err(dev, "COMMAND_HARDWARE_ERROR address=0x%016llx flags=0x%04x]\n",
  494. address, flags);
  495. break;
  496. case EVENT_TYPE_IOTLB_INV_TO:
  497. dev_err(dev, "IOTLB_INV_TIMEOUT device=%02x:%02x.%x address=0x%016llx]\n",
  498. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  499. address);
  500. break;
  501. case EVENT_TYPE_INV_DEV_REQ:
  502. dev_err(dev, "INVALID_DEVICE_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%016llx flags=0x%04x]\n",
  503. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  504. pasid, address, flags);
  505. break;
  506. case EVENT_TYPE_INV_PPR_REQ:
  507. pasid = ((event[0] >> 16) & 0xFFFF)
  508. | ((event[1] << 6) & 0xF0000);
  509. tag = event[1] & 0x03FF;
  510. dev_err(dev, "INVALID_PPR_REQUEST device=%02x:%02x.%x pasid=0x%05x address=0x%016llx flags=0x%04x]\n",
  511. PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  512. pasid, address, flags);
  513. break;
  514. default:
  515. dev_err(dev, "UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n",
  516. event[0], event[1], event[2], event[3]);
  517. }
  518. memset(__evt, 0, 4 * sizeof(u32));
  519. }
  520. static void iommu_poll_events(struct amd_iommu *iommu)
  521. {
  522. u32 head, tail;
  523. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  524. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  525. while (head != tail) {
  526. iommu_print_event(iommu, iommu->evt_buf + head);
  527. head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
  528. }
  529. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  530. }
  531. static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw)
  532. {
  533. struct amd_iommu_fault fault;
  534. if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
  535. pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
  536. return;
  537. }
  538. fault.address = raw[1];
  539. fault.pasid = PPR_PASID(raw[0]);
  540. fault.device_id = PPR_DEVID(raw[0]);
  541. fault.tag = PPR_TAG(raw[0]);
  542. fault.flags = PPR_FLAGS(raw[0]);
  543. atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
  544. }
  545. static void iommu_poll_ppr_log(struct amd_iommu *iommu)
  546. {
  547. u32 head, tail;
  548. if (iommu->ppr_log == NULL)
  549. return;
  550. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  551. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  552. while (head != tail) {
  553. volatile u64 *raw;
  554. u64 entry[2];
  555. int i;
  556. raw = (u64 *)(iommu->ppr_log + head);
  557. /*
  558. * Hardware bug: Interrupt may arrive before the entry is
  559. * written to memory. If this happens we need to wait for the
  560. * entry to arrive.
  561. */
  562. for (i = 0; i < LOOP_TIMEOUT; ++i) {
  563. if (PPR_REQ_TYPE(raw[0]) != 0)
  564. break;
  565. udelay(1);
  566. }
  567. /* Avoid memcpy function-call overhead */
  568. entry[0] = raw[0];
  569. entry[1] = raw[1];
  570. /*
  571. * To detect the hardware bug we need to clear the entry
  572. * back to zero.
  573. */
  574. raw[0] = raw[1] = 0UL;
  575. /* Update head pointer of hardware ring-buffer */
  576. head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
  577. writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  578. /* Handle PPR entry */
  579. iommu_handle_ppr_entry(iommu, entry);
  580. /* Refresh ring-buffer information */
  581. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  582. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  583. }
  584. }
  585. #ifdef CONFIG_IRQ_REMAP
  586. static int (*iommu_ga_log_notifier)(u32);
  587. int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
  588. {
  589. iommu_ga_log_notifier = notifier;
  590. return 0;
  591. }
  592. EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier);
  593. static void iommu_poll_ga_log(struct amd_iommu *iommu)
  594. {
  595. u32 head, tail, cnt = 0;
  596. if (iommu->ga_log == NULL)
  597. return;
  598. head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
  599. tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
  600. while (head != tail) {
  601. volatile u64 *raw;
  602. u64 log_entry;
  603. raw = (u64 *)(iommu->ga_log + head);
  604. cnt++;
  605. /* Avoid memcpy function-call overhead */
  606. log_entry = *raw;
  607. /* Update head pointer of hardware ring-buffer */
  608. head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE;
  609. writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
  610. /* Handle GA entry */
  611. switch (GA_REQ_TYPE(log_entry)) {
  612. case GA_GUEST_NR:
  613. if (!iommu_ga_log_notifier)
  614. break;
  615. pr_debug("AMD-Vi: %s: devid=%#x, ga_tag=%#x\n",
  616. __func__, GA_DEVID(log_entry),
  617. GA_TAG(log_entry));
  618. if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
  619. pr_err("AMD-Vi: GA log notifier failed.\n");
  620. break;
  621. default:
  622. break;
  623. }
  624. }
  625. }
  626. #endif /* CONFIG_IRQ_REMAP */
  627. #define AMD_IOMMU_INT_MASK \
  628. (MMIO_STATUS_EVT_INT_MASK | \
  629. MMIO_STATUS_PPR_INT_MASK | \
  630. MMIO_STATUS_GALOG_INT_MASK)
  631. irqreturn_t amd_iommu_int_thread(int irq, void *data)
  632. {
  633. struct amd_iommu *iommu = (struct amd_iommu *) data;
  634. u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  635. while (status & AMD_IOMMU_INT_MASK) {
  636. /* Enable EVT and PPR and GA interrupts again */
  637. writel(AMD_IOMMU_INT_MASK,
  638. iommu->mmio_base + MMIO_STATUS_OFFSET);
  639. if (status & MMIO_STATUS_EVT_INT_MASK) {
  640. pr_devel("AMD-Vi: Processing IOMMU Event Log\n");
  641. iommu_poll_events(iommu);
  642. }
  643. if (status & MMIO_STATUS_PPR_INT_MASK) {
  644. pr_devel("AMD-Vi: Processing IOMMU PPR Log\n");
  645. iommu_poll_ppr_log(iommu);
  646. }
  647. #ifdef CONFIG_IRQ_REMAP
  648. if (status & MMIO_STATUS_GALOG_INT_MASK) {
  649. pr_devel("AMD-Vi: Processing IOMMU GA Log\n");
  650. iommu_poll_ga_log(iommu);
  651. }
  652. #endif
  653. /*
  654. * Hardware bug: ERBT1312
  655. * When re-enabling interrupt (by writing 1
  656. * to clear the bit), the hardware might also try to set
  657. * the interrupt bit in the event status register.
  658. * In this scenario, the bit will be set, and disable
  659. * subsequent interrupts.
  660. *
  661. * Workaround: The IOMMU driver should read back the
  662. * status register and check if the interrupt bits are cleared.
  663. * If not, driver will need to go through the interrupt handler
  664. * again and re-clear the bits
  665. */
  666. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  667. }
  668. return IRQ_HANDLED;
  669. }
  670. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  671. {
  672. return IRQ_WAKE_THREAD;
  673. }
  674. /****************************************************************************
  675. *
  676. * IOMMU command queuing functions
  677. *
  678. ****************************************************************************/
  679. static int wait_on_sem(volatile u64 *sem)
  680. {
  681. int i = 0;
  682. while (*sem == 0 && i < LOOP_TIMEOUT) {
  683. udelay(1);
  684. i += 1;
  685. }
  686. if (i == LOOP_TIMEOUT) {
  687. pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
  688. return -EIO;
  689. }
  690. return 0;
  691. }
  692. static void copy_cmd_to_buffer(struct amd_iommu *iommu,
  693. struct iommu_cmd *cmd)
  694. {
  695. u8 *target;
  696. target = iommu->cmd_buf + iommu->cmd_buf_tail;
  697. iommu->cmd_buf_tail += sizeof(*cmd);
  698. iommu->cmd_buf_tail %= CMD_BUFFER_SIZE;
  699. /* Copy command to buffer */
  700. memcpy(target, cmd, sizeof(*cmd));
  701. /* Tell the IOMMU about it */
  702. writel(iommu->cmd_buf_tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  703. }
  704. static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
  705. {
  706. u64 paddr = iommu_virt_to_phys((void *)address);
  707. WARN_ON(address & 0x7ULL);
  708. memset(cmd, 0, sizeof(*cmd));
  709. cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK;
  710. cmd->data[1] = upper_32_bits(paddr);
  711. cmd->data[2] = 1;
  712. CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
  713. }
  714. static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
  715. {
  716. memset(cmd, 0, sizeof(*cmd));
  717. cmd->data[0] = devid;
  718. CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
  719. }
  720. static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  721. size_t size, u16 domid, int pde)
  722. {
  723. u64 pages;
  724. bool s;
  725. pages = iommu_num_pages(address, size, PAGE_SIZE);
  726. s = false;
  727. if (pages > 1) {
  728. /*
  729. * If we have to flush more than one page, flush all
  730. * TLB entries for this domain
  731. */
  732. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  733. s = true;
  734. }
  735. address &= PAGE_MASK;
  736. memset(cmd, 0, sizeof(*cmd));
  737. cmd->data[1] |= domid;
  738. cmd->data[2] = lower_32_bits(address);
  739. cmd->data[3] = upper_32_bits(address);
  740. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  741. if (s) /* size bit - we flush more than one 4kb page */
  742. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  743. if (pde) /* PDE bit - we want to flush everything, not only the PTEs */
  744. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  745. }
  746. static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
  747. u64 address, size_t size)
  748. {
  749. u64 pages;
  750. bool s;
  751. pages = iommu_num_pages(address, size, PAGE_SIZE);
  752. s = false;
  753. if (pages > 1) {
  754. /*
  755. * If we have to flush more than one page, flush all
  756. * TLB entries for this domain
  757. */
  758. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  759. s = true;
  760. }
  761. address &= PAGE_MASK;
  762. memset(cmd, 0, sizeof(*cmd));
  763. cmd->data[0] = devid;
  764. cmd->data[0] |= (qdep & 0xff) << 24;
  765. cmd->data[1] = devid;
  766. cmd->data[2] = lower_32_bits(address);
  767. cmd->data[3] = upper_32_bits(address);
  768. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  769. if (s)
  770. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  771. }
  772. static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
  773. u64 address, bool size)
  774. {
  775. memset(cmd, 0, sizeof(*cmd));
  776. address &= ~(0xfffULL);
  777. cmd->data[0] = pasid;
  778. cmd->data[1] = domid;
  779. cmd->data[2] = lower_32_bits(address);
  780. cmd->data[3] = upper_32_bits(address);
  781. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  782. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  783. if (size)
  784. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  785. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  786. }
  787. static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
  788. int qdep, u64 address, bool size)
  789. {
  790. memset(cmd, 0, sizeof(*cmd));
  791. address &= ~(0xfffULL);
  792. cmd->data[0] = devid;
  793. cmd->data[0] |= ((pasid >> 8) & 0xff) << 16;
  794. cmd->data[0] |= (qdep & 0xff) << 24;
  795. cmd->data[1] = devid;
  796. cmd->data[1] |= (pasid & 0xff) << 16;
  797. cmd->data[2] = lower_32_bits(address);
  798. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  799. cmd->data[3] = upper_32_bits(address);
  800. if (size)
  801. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  802. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  803. }
  804. static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
  805. int status, int tag, bool gn)
  806. {
  807. memset(cmd, 0, sizeof(*cmd));
  808. cmd->data[0] = devid;
  809. if (gn) {
  810. cmd->data[1] = pasid;
  811. cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
  812. }
  813. cmd->data[3] = tag & 0x1ff;
  814. cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
  815. CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
  816. }
  817. static void build_inv_all(struct iommu_cmd *cmd)
  818. {
  819. memset(cmd, 0, sizeof(*cmd));
  820. CMD_SET_TYPE(cmd, CMD_INV_ALL);
  821. }
  822. static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
  823. {
  824. memset(cmd, 0, sizeof(*cmd));
  825. cmd->data[0] = devid;
  826. CMD_SET_TYPE(cmd, CMD_INV_IRT);
  827. }
  828. /*
  829. * Writes the command to the IOMMUs command buffer and informs the
  830. * hardware about the new command.
  831. */
  832. static int __iommu_queue_command_sync(struct amd_iommu *iommu,
  833. struct iommu_cmd *cmd,
  834. bool sync)
  835. {
  836. unsigned int count = 0;
  837. u32 left, next_tail;
  838. next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
  839. again:
  840. left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE;
  841. if (left <= 0x20) {
  842. /* Skip udelay() the first time around */
  843. if (count++) {
  844. if (count == LOOP_TIMEOUT) {
  845. pr_err("AMD-Vi: Command buffer timeout\n");
  846. return -EIO;
  847. }
  848. udelay(1);
  849. }
  850. /* Update head and recheck remaining space */
  851. iommu->cmd_buf_head = readl(iommu->mmio_base +
  852. MMIO_CMD_HEAD_OFFSET);
  853. goto again;
  854. }
  855. copy_cmd_to_buffer(iommu, cmd);
  856. /* Do we need to make sure all commands are processed? */
  857. iommu->need_sync = sync;
  858. return 0;
  859. }
  860. static int iommu_queue_command_sync(struct amd_iommu *iommu,
  861. struct iommu_cmd *cmd,
  862. bool sync)
  863. {
  864. unsigned long flags;
  865. int ret;
  866. raw_spin_lock_irqsave(&iommu->lock, flags);
  867. ret = __iommu_queue_command_sync(iommu, cmd, sync);
  868. raw_spin_unlock_irqrestore(&iommu->lock, flags);
  869. return ret;
  870. }
  871. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  872. {
  873. return iommu_queue_command_sync(iommu, cmd, true);
  874. }
  875. /*
  876. * This function queues a completion wait command into the command
  877. * buffer of an IOMMU
  878. */
  879. static int iommu_completion_wait(struct amd_iommu *iommu)
  880. {
  881. struct iommu_cmd cmd;
  882. unsigned long flags;
  883. int ret;
  884. if (!iommu->need_sync)
  885. return 0;
  886. build_completion_wait(&cmd, (u64)&iommu->cmd_sem);
  887. raw_spin_lock_irqsave(&iommu->lock, flags);
  888. iommu->cmd_sem = 0;
  889. ret = __iommu_queue_command_sync(iommu, &cmd, false);
  890. if (ret)
  891. goto out_unlock;
  892. ret = wait_on_sem(&iommu->cmd_sem);
  893. out_unlock:
  894. raw_spin_unlock_irqrestore(&iommu->lock, flags);
  895. return ret;
  896. }
  897. static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
  898. {
  899. struct iommu_cmd cmd;
  900. build_inv_dte(&cmd, devid);
  901. return iommu_queue_command(iommu, &cmd);
  902. }
  903. static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
  904. {
  905. u32 devid;
  906. for (devid = 0; devid <= 0xffff; ++devid)
  907. iommu_flush_dte(iommu, devid);
  908. iommu_completion_wait(iommu);
  909. }
  910. /*
  911. * This function uses heavy locking and may disable irqs for some time. But
  912. * this is no issue because it is only called during resume.
  913. */
  914. static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
  915. {
  916. u32 dom_id;
  917. for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
  918. struct iommu_cmd cmd;
  919. build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  920. dom_id, 1);
  921. iommu_queue_command(iommu, &cmd);
  922. }
  923. iommu_completion_wait(iommu);
  924. }
  925. static void amd_iommu_flush_all(struct amd_iommu *iommu)
  926. {
  927. struct iommu_cmd cmd;
  928. build_inv_all(&cmd);
  929. iommu_queue_command(iommu, &cmd);
  930. iommu_completion_wait(iommu);
  931. }
  932. static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
  933. {
  934. struct iommu_cmd cmd;
  935. build_inv_irt(&cmd, devid);
  936. iommu_queue_command(iommu, &cmd);
  937. }
  938. static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
  939. {
  940. u32 devid;
  941. for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++)
  942. iommu_flush_irt(iommu, devid);
  943. iommu_completion_wait(iommu);
  944. }
  945. void iommu_flush_all_caches(struct amd_iommu *iommu)
  946. {
  947. if (iommu_feature(iommu, FEATURE_IA)) {
  948. amd_iommu_flush_all(iommu);
  949. } else {
  950. amd_iommu_flush_dte_all(iommu);
  951. amd_iommu_flush_irt_all(iommu);
  952. amd_iommu_flush_tlb_all(iommu);
  953. }
  954. }
  955. /*
  956. * Command send function for flushing on-device TLB
  957. */
  958. static int device_flush_iotlb(struct iommu_dev_data *dev_data,
  959. u64 address, size_t size)
  960. {
  961. struct amd_iommu *iommu;
  962. struct iommu_cmd cmd;
  963. int qdep;
  964. qdep = dev_data->ats.qdep;
  965. iommu = amd_iommu_rlookup_table[dev_data->devid];
  966. build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
  967. return iommu_queue_command(iommu, &cmd);
  968. }
  969. /*
  970. * Command send function for invalidating a device table entry
  971. */
  972. static int device_flush_dte(struct iommu_dev_data *dev_data)
  973. {
  974. struct amd_iommu *iommu;
  975. u16 alias;
  976. int ret;
  977. iommu = amd_iommu_rlookup_table[dev_data->devid];
  978. alias = dev_data->alias;
  979. ret = iommu_flush_dte(iommu, dev_data->devid);
  980. if (!ret && alias != dev_data->devid)
  981. ret = iommu_flush_dte(iommu, alias);
  982. if (ret)
  983. return ret;
  984. if (dev_data->ats.enabled)
  985. ret = device_flush_iotlb(dev_data, 0, ~0UL);
  986. return ret;
  987. }
  988. /*
  989. * TLB invalidation function which is called from the mapping functions.
  990. * It invalidates a single PTE if the range to flush is within a single
  991. * page. Otherwise it flushes the whole TLB of the IOMMU.
  992. */
  993. static void __domain_flush_pages(struct protection_domain *domain,
  994. u64 address, size_t size, int pde)
  995. {
  996. struct iommu_dev_data *dev_data;
  997. struct iommu_cmd cmd;
  998. int ret = 0, i;
  999. build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
  1000. for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
  1001. if (!domain->dev_iommu[i])
  1002. continue;
  1003. /*
  1004. * Devices of this domain are behind this IOMMU
  1005. * We need a TLB flush
  1006. */
  1007. ret |= iommu_queue_command(amd_iommus[i], &cmd);
  1008. }
  1009. list_for_each_entry(dev_data, &domain->dev_list, list) {
  1010. if (!dev_data->ats.enabled)
  1011. continue;
  1012. ret |= device_flush_iotlb(dev_data, address, size);
  1013. }
  1014. WARN_ON(ret);
  1015. }
  1016. static void domain_flush_pages(struct protection_domain *domain,
  1017. u64 address, size_t size)
  1018. {
  1019. __domain_flush_pages(domain, address, size, 0);
  1020. }
  1021. /* Flush the whole IO/TLB for a given protection domain */
  1022. static void domain_flush_tlb(struct protection_domain *domain)
  1023. {
  1024. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
  1025. }
  1026. /* Flush the whole IO/TLB for a given protection domain - including PDE */
  1027. static void domain_flush_tlb_pde(struct protection_domain *domain)
  1028. {
  1029. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
  1030. }
  1031. static void domain_flush_complete(struct protection_domain *domain)
  1032. {
  1033. int i;
  1034. for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
  1035. if (domain && !domain->dev_iommu[i])
  1036. continue;
  1037. /*
  1038. * Devices of this domain are behind this IOMMU
  1039. * We need to wait for completion of all commands.
  1040. */
  1041. iommu_completion_wait(amd_iommus[i]);
  1042. }
  1043. }
  1044. /*
  1045. * This function flushes the DTEs for all devices in domain
  1046. */
  1047. static void domain_flush_devices(struct protection_domain *domain)
  1048. {
  1049. struct iommu_dev_data *dev_data;
  1050. list_for_each_entry(dev_data, &domain->dev_list, list)
  1051. device_flush_dte(dev_data);
  1052. }
  1053. /****************************************************************************
  1054. *
  1055. * The functions below are used the create the page table mappings for
  1056. * unity mapped regions.
  1057. *
  1058. ****************************************************************************/
  1059. /*
  1060. * This function is used to add another level to an IO page table. Adding
  1061. * another level increases the size of the address space by 9 bits to a size up
  1062. * to 64 bits.
  1063. */
  1064. static bool increase_address_space(struct protection_domain *domain,
  1065. gfp_t gfp)
  1066. {
  1067. u64 *pte;
  1068. if (domain->mode == PAGE_MODE_6_LEVEL)
  1069. /* address space already 64 bit large */
  1070. return false;
  1071. pte = (void *)get_zeroed_page(gfp);
  1072. if (!pte)
  1073. return false;
  1074. *pte = PM_LEVEL_PDE(domain->mode,
  1075. iommu_virt_to_phys(domain->pt_root));
  1076. domain->pt_root = pte;
  1077. domain->mode += 1;
  1078. domain->updated = true;
  1079. return true;
  1080. }
  1081. static u64 *alloc_pte(struct protection_domain *domain,
  1082. unsigned long address,
  1083. unsigned long page_size,
  1084. u64 **pte_page,
  1085. gfp_t gfp)
  1086. {
  1087. int level, end_lvl;
  1088. u64 *pte, *page;
  1089. BUG_ON(!is_power_of_2(page_size));
  1090. while (address > PM_LEVEL_SIZE(domain->mode))
  1091. increase_address_space(domain, gfp);
  1092. level = domain->mode - 1;
  1093. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  1094. address = PAGE_SIZE_ALIGN(address, page_size);
  1095. end_lvl = PAGE_SIZE_LEVEL(page_size);
  1096. while (level > end_lvl) {
  1097. u64 __pte, __npte;
  1098. __pte = *pte;
  1099. if (!IOMMU_PTE_PRESENT(__pte)) {
  1100. page = (u64 *)get_zeroed_page(gfp);
  1101. if (!page)
  1102. return NULL;
  1103. __npte = PM_LEVEL_PDE(level, iommu_virt_to_phys(page));
  1104. /* pte could have been changed somewhere. */
  1105. if (cmpxchg64(pte, __pte, __npte) != __pte) {
  1106. free_page((unsigned long)page);
  1107. continue;
  1108. }
  1109. }
  1110. /* No level skipping support yet */
  1111. if (PM_PTE_LEVEL(*pte) != level)
  1112. return NULL;
  1113. level -= 1;
  1114. pte = IOMMU_PTE_PAGE(*pte);
  1115. if (pte_page && level == end_lvl)
  1116. *pte_page = pte;
  1117. pte = &pte[PM_LEVEL_INDEX(level, address)];
  1118. }
  1119. return pte;
  1120. }
  1121. /*
  1122. * This function checks if there is a PTE for a given dma address. If
  1123. * there is one, it returns the pointer to it.
  1124. */
  1125. static u64 *fetch_pte(struct protection_domain *domain,
  1126. unsigned long address,
  1127. unsigned long *page_size)
  1128. {
  1129. int level;
  1130. u64 *pte;
  1131. *page_size = 0;
  1132. if (address > PM_LEVEL_SIZE(domain->mode))
  1133. return NULL;
  1134. level = domain->mode - 1;
  1135. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  1136. *page_size = PTE_LEVEL_PAGE_SIZE(level);
  1137. while (level > 0) {
  1138. /* Not Present */
  1139. if (!IOMMU_PTE_PRESENT(*pte))
  1140. return NULL;
  1141. /* Large PTE */
  1142. if (PM_PTE_LEVEL(*pte) == 7 ||
  1143. PM_PTE_LEVEL(*pte) == 0)
  1144. break;
  1145. /* No level skipping support yet */
  1146. if (PM_PTE_LEVEL(*pte) != level)
  1147. return NULL;
  1148. level -= 1;
  1149. /* Walk to the next level */
  1150. pte = IOMMU_PTE_PAGE(*pte);
  1151. pte = &pte[PM_LEVEL_INDEX(level, address)];
  1152. *page_size = PTE_LEVEL_PAGE_SIZE(level);
  1153. }
  1154. if (PM_PTE_LEVEL(*pte) == 0x07) {
  1155. unsigned long pte_mask;
  1156. /*
  1157. * If we have a series of large PTEs, make
  1158. * sure to return a pointer to the first one.
  1159. */
  1160. *page_size = pte_mask = PTE_PAGE_SIZE(*pte);
  1161. pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
  1162. pte = (u64 *)(((unsigned long)pte) & pte_mask);
  1163. }
  1164. return pte;
  1165. }
  1166. /*
  1167. * Generic mapping functions. It maps a physical address into a DMA
  1168. * address space. It allocates the page table pages if necessary.
  1169. * In the future it can be extended to a generic mapping function
  1170. * supporting all features of AMD IOMMU page tables like level skipping
  1171. * and full 64 bit address spaces.
  1172. */
  1173. static int iommu_map_page(struct protection_domain *dom,
  1174. unsigned long bus_addr,
  1175. unsigned long phys_addr,
  1176. unsigned long page_size,
  1177. int prot,
  1178. gfp_t gfp)
  1179. {
  1180. u64 __pte, *pte;
  1181. int i, count;
  1182. BUG_ON(!IS_ALIGNED(bus_addr, page_size));
  1183. BUG_ON(!IS_ALIGNED(phys_addr, page_size));
  1184. if (!(prot & IOMMU_PROT_MASK))
  1185. return -EINVAL;
  1186. count = PAGE_SIZE_PTE_COUNT(page_size);
  1187. pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
  1188. if (!pte)
  1189. return -ENOMEM;
  1190. for (i = 0; i < count; ++i)
  1191. if (IOMMU_PTE_PRESENT(pte[i]))
  1192. return -EBUSY;
  1193. if (count > 1) {
  1194. __pte = PAGE_SIZE_PTE(__sme_set(phys_addr), page_size);
  1195. __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC;
  1196. } else
  1197. __pte = __sme_set(phys_addr) | IOMMU_PTE_PR | IOMMU_PTE_FC;
  1198. if (prot & IOMMU_PROT_IR)
  1199. __pte |= IOMMU_PTE_IR;
  1200. if (prot & IOMMU_PROT_IW)
  1201. __pte |= IOMMU_PTE_IW;
  1202. for (i = 0; i < count; ++i)
  1203. pte[i] = __pte;
  1204. update_domain(dom);
  1205. return 0;
  1206. }
  1207. static unsigned long iommu_unmap_page(struct protection_domain *dom,
  1208. unsigned long bus_addr,
  1209. unsigned long page_size)
  1210. {
  1211. unsigned long long unmapped;
  1212. unsigned long unmap_size;
  1213. u64 *pte;
  1214. BUG_ON(!is_power_of_2(page_size));
  1215. unmapped = 0;
  1216. while (unmapped < page_size) {
  1217. pte = fetch_pte(dom, bus_addr, &unmap_size);
  1218. if (pte) {
  1219. int i, count;
  1220. count = PAGE_SIZE_PTE_COUNT(unmap_size);
  1221. for (i = 0; i < count; i++)
  1222. pte[i] = 0ULL;
  1223. }
  1224. bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
  1225. unmapped += unmap_size;
  1226. }
  1227. BUG_ON(unmapped && !is_power_of_2(unmapped));
  1228. return unmapped;
  1229. }
  1230. /****************************************************************************
  1231. *
  1232. * The next functions belong to the address allocator for the dma_ops
  1233. * interface functions.
  1234. *
  1235. ****************************************************************************/
  1236. static unsigned long dma_ops_alloc_iova(struct device *dev,
  1237. struct dma_ops_domain *dma_dom,
  1238. unsigned int pages, u64 dma_mask)
  1239. {
  1240. unsigned long pfn = 0;
  1241. pages = __roundup_pow_of_two(pages);
  1242. if (dma_mask > DMA_BIT_MASK(32))
  1243. pfn = alloc_iova_fast(&dma_dom->iovad, pages,
  1244. IOVA_PFN(DMA_BIT_MASK(32)), false);
  1245. if (!pfn)
  1246. pfn = alloc_iova_fast(&dma_dom->iovad, pages,
  1247. IOVA_PFN(dma_mask), true);
  1248. return (pfn << PAGE_SHIFT);
  1249. }
  1250. static void dma_ops_free_iova(struct dma_ops_domain *dma_dom,
  1251. unsigned long address,
  1252. unsigned int pages)
  1253. {
  1254. pages = __roundup_pow_of_two(pages);
  1255. address >>= PAGE_SHIFT;
  1256. free_iova_fast(&dma_dom->iovad, address, pages);
  1257. }
  1258. /****************************************************************************
  1259. *
  1260. * The next functions belong to the domain allocation. A domain is
  1261. * allocated for every IOMMU as the default domain. If device isolation
  1262. * is enabled, every device get its own domain. The most important thing
  1263. * about domains is the page table mapping the DMA address space they
  1264. * contain.
  1265. *
  1266. ****************************************************************************/
  1267. /*
  1268. * This function adds a protection domain to the global protection domain list
  1269. */
  1270. static void add_domain_to_list(struct protection_domain *domain)
  1271. {
  1272. unsigned long flags;
  1273. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1274. list_add(&domain->list, &amd_iommu_pd_list);
  1275. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1276. }
  1277. /*
  1278. * This function removes a protection domain to the global
  1279. * protection domain list
  1280. */
  1281. static void del_domain_from_list(struct protection_domain *domain)
  1282. {
  1283. unsigned long flags;
  1284. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1285. list_del(&domain->list);
  1286. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1287. }
  1288. static u16 domain_id_alloc(void)
  1289. {
  1290. int id;
  1291. spin_lock(&pd_bitmap_lock);
  1292. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  1293. BUG_ON(id == 0);
  1294. if (id > 0 && id < MAX_DOMAIN_ID)
  1295. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  1296. else
  1297. id = 0;
  1298. spin_unlock(&pd_bitmap_lock);
  1299. return id;
  1300. }
  1301. static void domain_id_free(int id)
  1302. {
  1303. spin_lock(&pd_bitmap_lock);
  1304. if (id > 0 && id < MAX_DOMAIN_ID)
  1305. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  1306. spin_unlock(&pd_bitmap_lock);
  1307. }
  1308. #define DEFINE_FREE_PT_FN(LVL, FN) \
  1309. static void free_pt_##LVL (unsigned long __pt) \
  1310. { \
  1311. unsigned long p; \
  1312. u64 *pt; \
  1313. int i; \
  1314. \
  1315. pt = (u64 *)__pt; \
  1316. \
  1317. for (i = 0; i < 512; ++i) { \
  1318. /* PTE present? */ \
  1319. if (!IOMMU_PTE_PRESENT(pt[i])) \
  1320. continue; \
  1321. \
  1322. /* Large PTE? */ \
  1323. if (PM_PTE_LEVEL(pt[i]) == 0 || \
  1324. PM_PTE_LEVEL(pt[i]) == 7) \
  1325. continue; \
  1326. \
  1327. p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \
  1328. FN(p); \
  1329. } \
  1330. free_page((unsigned long)pt); \
  1331. }
  1332. DEFINE_FREE_PT_FN(l2, free_page)
  1333. DEFINE_FREE_PT_FN(l3, free_pt_l2)
  1334. DEFINE_FREE_PT_FN(l4, free_pt_l3)
  1335. DEFINE_FREE_PT_FN(l5, free_pt_l4)
  1336. DEFINE_FREE_PT_FN(l6, free_pt_l5)
  1337. static void free_pagetable(struct protection_domain *domain)
  1338. {
  1339. unsigned long root = (unsigned long)domain->pt_root;
  1340. switch (domain->mode) {
  1341. case PAGE_MODE_NONE:
  1342. break;
  1343. case PAGE_MODE_1_LEVEL:
  1344. free_page(root);
  1345. break;
  1346. case PAGE_MODE_2_LEVEL:
  1347. free_pt_l2(root);
  1348. break;
  1349. case PAGE_MODE_3_LEVEL:
  1350. free_pt_l3(root);
  1351. break;
  1352. case PAGE_MODE_4_LEVEL:
  1353. free_pt_l4(root);
  1354. break;
  1355. case PAGE_MODE_5_LEVEL:
  1356. free_pt_l5(root);
  1357. break;
  1358. case PAGE_MODE_6_LEVEL:
  1359. free_pt_l6(root);
  1360. break;
  1361. default:
  1362. BUG();
  1363. }
  1364. }
  1365. static void free_gcr3_tbl_level1(u64 *tbl)
  1366. {
  1367. u64 *ptr;
  1368. int i;
  1369. for (i = 0; i < 512; ++i) {
  1370. if (!(tbl[i] & GCR3_VALID))
  1371. continue;
  1372. ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
  1373. free_page((unsigned long)ptr);
  1374. }
  1375. }
  1376. static void free_gcr3_tbl_level2(u64 *tbl)
  1377. {
  1378. u64 *ptr;
  1379. int i;
  1380. for (i = 0; i < 512; ++i) {
  1381. if (!(tbl[i] & GCR3_VALID))
  1382. continue;
  1383. ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK);
  1384. free_gcr3_tbl_level1(ptr);
  1385. }
  1386. }
  1387. static void free_gcr3_table(struct protection_domain *domain)
  1388. {
  1389. if (domain->glx == 2)
  1390. free_gcr3_tbl_level2(domain->gcr3_tbl);
  1391. else if (domain->glx == 1)
  1392. free_gcr3_tbl_level1(domain->gcr3_tbl);
  1393. else
  1394. BUG_ON(domain->glx != 0);
  1395. free_page((unsigned long)domain->gcr3_tbl);
  1396. }
  1397. static void dma_ops_domain_flush_tlb(struct dma_ops_domain *dom)
  1398. {
  1399. domain_flush_tlb(&dom->domain);
  1400. domain_flush_complete(&dom->domain);
  1401. }
  1402. static void iova_domain_flush_tlb(struct iova_domain *iovad)
  1403. {
  1404. struct dma_ops_domain *dom;
  1405. dom = container_of(iovad, struct dma_ops_domain, iovad);
  1406. dma_ops_domain_flush_tlb(dom);
  1407. }
  1408. /*
  1409. * Free a domain, only used if something went wrong in the
  1410. * allocation path and we need to free an already allocated page table
  1411. */
  1412. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  1413. {
  1414. if (!dom)
  1415. return;
  1416. del_domain_from_list(&dom->domain);
  1417. put_iova_domain(&dom->iovad);
  1418. free_pagetable(&dom->domain);
  1419. if (dom->domain.id)
  1420. domain_id_free(dom->domain.id);
  1421. kfree(dom);
  1422. }
  1423. /*
  1424. * Allocates a new protection domain usable for the dma_ops functions.
  1425. * It also initializes the page table and the address allocator data
  1426. * structures required for the dma_ops interface
  1427. */
  1428. static struct dma_ops_domain *dma_ops_domain_alloc(void)
  1429. {
  1430. struct dma_ops_domain *dma_dom;
  1431. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  1432. if (!dma_dom)
  1433. return NULL;
  1434. if (protection_domain_init(&dma_dom->domain))
  1435. goto free_dma_dom;
  1436. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  1437. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1438. dma_dom->domain.flags = PD_DMA_OPS_MASK;
  1439. if (!dma_dom->domain.pt_root)
  1440. goto free_dma_dom;
  1441. init_iova_domain(&dma_dom->iovad, PAGE_SIZE, IOVA_START_PFN);
  1442. if (init_iova_flush_queue(&dma_dom->iovad, iova_domain_flush_tlb, NULL))
  1443. goto free_dma_dom;
  1444. /* Initialize reserved ranges */
  1445. copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad);
  1446. add_domain_to_list(&dma_dom->domain);
  1447. return dma_dom;
  1448. free_dma_dom:
  1449. dma_ops_domain_free(dma_dom);
  1450. return NULL;
  1451. }
  1452. /*
  1453. * little helper function to check whether a given protection domain is a
  1454. * dma_ops domain
  1455. */
  1456. static bool dma_ops_domain(struct protection_domain *domain)
  1457. {
  1458. return domain->flags & PD_DMA_OPS_MASK;
  1459. }
  1460. static void set_dte_entry(u16 devid, struct protection_domain *domain,
  1461. bool ats, bool ppr)
  1462. {
  1463. u64 pte_root = 0;
  1464. u64 flags = 0;
  1465. if (domain->mode != PAGE_MODE_NONE)
  1466. pte_root = iommu_virt_to_phys(domain->pt_root);
  1467. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  1468. << DEV_ENTRY_MODE_SHIFT;
  1469. pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V | DTE_FLAG_TV;
  1470. flags = amd_iommu_dev_table[devid].data[1];
  1471. if (ats)
  1472. flags |= DTE_FLAG_IOTLB;
  1473. if (ppr) {
  1474. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  1475. if (iommu_feature(iommu, FEATURE_EPHSUP))
  1476. pte_root |= 1ULL << DEV_ENTRY_PPR;
  1477. }
  1478. if (domain->flags & PD_IOMMUV2_MASK) {
  1479. u64 gcr3 = iommu_virt_to_phys(domain->gcr3_tbl);
  1480. u64 glx = domain->glx;
  1481. u64 tmp;
  1482. pte_root |= DTE_FLAG_GV;
  1483. pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
  1484. /* First mask out possible old values for GCR3 table */
  1485. tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
  1486. flags &= ~tmp;
  1487. tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
  1488. flags &= ~tmp;
  1489. /* Encode GCR3 table into DTE */
  1490. tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
  1491. pte_root |= tmp;
  1492. tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
  1493. flags |= tmp;
  1494. tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
  1495. flags |= tmp;
  1496. }
  1497. flags &= ~DEV_DOMID_MASK;
  1498. flags |= domain->id;
  1499. amd_iommu_dev_table[devid].data[1] = flags;
  1500. amd_iommu_dev_table[devid].data[0] = pte_root;
  1501. }
  1502. static void clear_dte_entry(u16 devid)
  1503. {
  1504. /* remove entry from the device table seen by the hardware */
  1505. amd_iommu_dev_table[devid].data[0] = DTE_FLAG_V | DTE_FLAG_TV;
  1506. amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
  1507. amd_iommu_apply_erratum_63(devid);
  1508. }
  1509. static void do_attach(struct iommu_dev_data *dev_data,
  1510. struct protection_domain *domain)
  1511. {
  1512. struct amd_iommu *iommu;
  1513. u16 alias;
  1514. bool ats;
  1515. iommu = amd_iommu_rlookup_table[dev_data->devid];
  1516. alias = dev_data->alias;
  1517. ats = dev_data->ats.enabled;
  1518. /* Update data structures */
  1519. dev_data->domain = domain;
  1520. list_add(&dev_data->list, &domain->dev_list);
  1521. /* Do reference counting */
  1522. domain->dev_iommu[iommu->index] += 1;
  1523. domain->dev_cnt += 1;
  1524. /* Update device table */
  1525. set_dte_entry(dev_data->devid, domain, ats, dev_data->iommu_v2);
  1526. if (alias != dev_data->devid)
  1527. set_dte_entry(alias, domain, ats, dev_data->iommu_v2);
  1528. device_flush_dte(dev_data);
  1529. }
  1530. static void do_detach(struct iommu_dev_data *dev_data)
  1531. {
  1532. struct protection_domain *domain = dev_data->domain;
  1533. struct amd_iommu *iommu;
  1534. u16 alias;
  1535. iommu = amd_iommu_rlookup_table[dev_data->devid];
  1536. alias = dev_data->alias;
  1537. /* Update data structures */
  1538. dev_data->domain = NULL;
  1539. list_del(&dev_data->list);
  1540. clear_dte_entry(dev_data->devid);
  1541. if (alias != dev_data->devid)
  1542. clear_dte_entry(alias);
  1543. /* Flush the DTE entry */
  1544. device_flush_dte(dev_data);
  1545. /* Flush IOTLB */
  1546. domain_flush_tlb_pde(domain);
  1547. /* Wait for the flushes to finish */
  1548. domain_flush_complete(domain);
  1549. /* decrease reference counters - needs to happen after the flushes */
  1550. domain->dev_iommu[iommu->index] -= 1;
  1551. domain->dev_cnt -= 1;
  1552. }
  1553. /*
  1554. * If a device is not yet associated with a domain, this function makes the
  1555. * device visible in the domain
  1556. */
  1557. static int __attach_device(struct iommu_dev_data *dev_data,
  1558. struct protection_domain *domain)
  1559. {
  1560. int ret;
  1561. /* lock domain */
  1562. spin_lock(&domain->lock);
  1563. ret = -EBUSY;
  1564. if (dev_data->domain != NULL)
  1565. goto out_unlock;
  1566. /* Attach alias group root */
  1567. do_attach(dev_data, domain);
  1568. ret = 0;
  1569. out_unlock:
  1570. /* ready */
  1571. spin_unlock(&domain->lock);
  1572. return ret;
  1573. }
  1574. static void pdev_iommuv2_disable(struct pci_dev *pdev)
  1575. {
  1576. pci_disable_ats(pdev);
  1577. pci_disable_pri(pdev);
  1578. pci_disable_pasid(pdev);
  1579. }
  1580. /* FIXME: Change generic reset-function to do the same */
  1581. static int pri_reset_while_enabled(struct pci_dev *pdev)
  1582. {
  1583. u16 control;
  1584. int pos;
  1585. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  1586. if (!pos)
  1587. return -EINVAL;
  1588. pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
  1589. control |= PCI_PRI_CTRL_RESET;
  1590. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  1591. return 0;
  1592. }
  1593. static int pdev_iommuv2_enable(struct pci_dev *pdev)
  1594. {
  1595. bool reset_enable;
  1596. int reqs, ret;
  1597. /* FIXME: Hardcode number of outstanding requests for now */
  1598. reqs = 32;
  1599. if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE))
  1600. reqs = 1;
  1601. reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET);
  1602. /* Only allow access to user-accessible pages */
  1603. ret = pci_enable_pasid(pdev, 0);
  1604. if (ret)
  1605. goto out_err;
  1606. /* First reset the PRI state of the device */
  1607. ret = pci_reset_pri(pdev);
  1608. if (ret)
  1609. goto out_err;
  1610. /* Enable PRI */
  1611. ret = pci_enable_pri(pdev, reqs);
  1612. if (ret)
  1613. goto out_err;
  1614. if (reset_enable) {
  1615. ret = pri_reset_while_enabled(pdev);
  1616. if (ret)
  1617. goto out_err;
  1618. }
  1619. ret = pci_enable_ats(pdev, PAGE_SHIFT);
  1620. if (ret)
  1621. goto out_err;
  1622. return 0;
  1623. out_err:
  1624. pci_disable_pri(pdev);
  1625. pci_disable_pasid(pdev);
  1626. return ret;
  1627. }
  1628. /* FIXME: Move this to PCI code */
  1629. #define PCI_PRI_TLP_OFF (1 << 15)
  1630. static bool pci_pri_tlp_required(struct pci_dev *pdev)
  1631. {
  1632. u16 status;
  1633. int pos;
  1634. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  1635. if (!pos)
  1636. return false;
  1637. pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
  1638. return (status & PCI_PRI_TLP_OFF) ? true : false;
  1639. }
  1640. /*
  1641. * If a device is not yet associated with a domain, this function makes the
  1642. * device visible in the domain
  1643. */
  1644. static int attach_device(struct device *dev,
  1645. struct protection_domain *domain)
  1646. {
  1647. struct pci_dev *pdev;
  1648. struct iommu_dev_data *dev_data;
  1649. unsigned long flags;
  1650. int ret;
  1651. dev_data = get_dev_data(dev);
  1652. if (!dev_is_pci(dev))
  1653. goto skip_ats_check;
  1654. pdev = to_pci_dev(dev);
  1655. if (domain->flags & PD_IOMMUV2_MASK) {
  1656. if (!dev_data->passthrough)
  1657. return -EINVAL;
  1658. if (dev_data->iommu_v2) {
  1659. if (pdev_iommuv2_enable(pdev) != 0)
  1660. return -EINVAL;
  1661. dev_data->ats.enabled = true;
  1662. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1663. dev_data->pri_tlp = pci_pri_tlp_required(pdev);
  1664. }
  1665. } else if (amd_iommu_iotlb_sup &&
  1666. pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
  1667. dev_data->ats.enabled = true;
  1668. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1669. }
  1670. skip_ats_check:
  1671. spin_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1672. ret = __attach_device(dev_data, domain);
  1673. spin_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1674. /*
  1675. * We might boot into a crash-kernel here. The crashed kernel
  1676. * left the caches in the IOMMU dirty. So we have to flush
  1677. * here to evict all dirty stuff.
  1678. */
  1679. domain_flush_tlb_pde(domain);
  1680. return ret;
  1681. }
  1682. /*
  1683. * Removes a device from a protection domain (unlocked)
  1684. */
  1685. static void __detach_device(struct iommu_dev_data *dev_data)
  1686. {
  1687. struct protection_domain *domain;
  1688. domain = dev_data->domain;
  1689. spin_lock(&domain->lock);
  1690. do_detach(dev_data);
  1691. spin_unlock(&domain->lock);
  1692. }
  1693. /*
  1694. * Removes a device from a protection domain (with devtable_lock held)
  1695. */
  1696. static void detach_device(struct device *dev)
  1697. {
  1698. struct protection_domain *domain;
  1699. struct iommu_dev_data *dev_data;
  1700. unsigned long flags;
  1701. dev_data = get_dev_data(dev);
  1702. domain = dev_data->domain;
  1703. /*
  1704. * First check if the device is still attached. It might already
  1705. * be detached from its domain because the generic
  1706. * iommu_detach_group code detached it and we try again here in
  1707. * our alias handling.
  1708. */
  1709. if (WARN_ON(!dev_data->domain))
  1710. return;
  1711. /* lock device table */
  1712. spin_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1713. __detach_device(dev_data);
  1714. spin_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1715. if (!dev_is_pci(dev))
  1716. return;
  1717. if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2)
  1718. pdev_iommuv2_disable(to_pci_dev(dev));
  1719. else if (dev_data->ats.enabled)
  1720. pci_disable_ats(to_pci_dev(dev));
  1721. dev_data->ats.enabled = false;
  1722. }
  1723. static int amd_iommu_add_device(struct device *dev)
  1724. {
  1725. struct iommu_dev_data *dev_data;
  1726. struct iommu_domain *domain;
  1727. struct amd_iommu *iommu;
  1728. int ret, devid;
  1729. if (!check_device(dev) || get_dev_data(dev))
  1730. return 0;
  1731. devid = get_device_id(dev);
  1732. if (devid < 0)
  1733. return devid;
  1734. iommu = amd_iommu_rlookup_table[devid];
  1735. ret = iommu_init_device(dev);
  1736. if (ret) {
  1737. if (ret != -ENOTSUPP)
  1738. pr_err("Failed to initialize device %s - trying to proceed anyway\n",
  1739. dev_name(dev));
  1740. iommu_ignore_device(dev);
  1741. dev->dma_ops = &dma_direct_ops;
  1742. goto out;
  1743. }
  1744. init_iommu_group(dev);
  1745. dev_data = get_dev_data(dev);
  1746. BUG_ON(!dev_data);
  1747. if (iommu_pass_through || dev_data->iommu_v2)
  1748. iommu_request_dm_for_dev(dev);
  1749. /* Domains are initialized for this device - have a look what we ended up with */
  1750. domain = iommu_get_domain_for_dev(dev);
  1751. if (domain->type == IOMMU_DOMAIN_IDENTITY)
  1752. dev_data->passthrough = true;
  1753. else
  1754. dev->dma_ops = &amd_iommu_dma_ops;
  1755. out:
  1756. iommu_completion_wait(iommu);
  1757. return 0;
  1758. }
  1759. static void amd_iommu_remove_device(struct device *dev)
  1760. {
  1761. struct amd_iommu *iommu;
  1762. int devid;
  1763. if (!check_device(dev))
  1764. return;
  1765. devid = get_device_id(dev);
  1766. if (devid < 0)
  1767. return;
  1768. iommu = amd_iommu_rlookup_table[devid];
  1769. iommu_uninit_device(dev);
  1770. iommu_completion_wait(iommu);
  1771. }
  1772. static struct iommu_group *amd_iommu_device_group(struct device *dev)
  1773. {
  1774. if (dev_is_pci(dev))
  1775. return pci_device_group(dev);
  1776. return acpihid_device_group(dev);
  1777. }
  1778. /*****************************************************************************
  1779. *
  1780. * The next functions belong to the dma_ops mapping/unmapping code.
  1781. *
  1782. *****************************************************************************/
  1783. /*
  1784. * In the dma_ops path we only have the struct device. This function
  1785. * finds the corresponding IOMMU, the protection domain and the
  1786. * requestor id for a given device.
  1787. * If the device is not yet associated with a domain this is also done
  1788. * in this function.
  1789. */
  1790. static struct protection_domain *get_domain(struct device *dev)
  1791. {
  1792. struct protection_domain *domain;
  1793. struct iommu_domain *io_domain;
  1794. if (!check_device(dev))
  1795. return ERR_PTR(-EINVAL);
  1796. domain = get_dev_data(dev)->domain;
  1797. if (domain == NULL && get_dev_data(dev)->defer_attach) {
  1798. get_dev_data(dev)->defer_attach = false;
  1799. io_domain = iommu_get_domain_for_dev(dev);
  1800. domain = to_pdomain(io_domain);
  1801. attach_device(dev, domain);
  1802. }
  1803. if (domain == NULL)
  1804. return ERR_PTR(-EBUSY);
  1805. if (!dma_ops_domain(domain))
  1806. return ERR_PTR(-EBUSY);
  1807. return domain;
  1808. }
  1809. static void update_device_table(struct protection_domain *domain)
  1810. {
  1811. struct iommu_dev_data *dev_data;
  1812. list_for_each_entry(dev_data, &domain->dev_list, list) {
  1813. set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled,
  1814. dev_data->iommu_v2);
  1815. if (dev_data->devid == dev_data->alias)
  1816. continue;
  1817. /* There is an alias, update device table entry for it */
  1818. set_dte_entry(dev_data->alias, domain, dev_data->ats.enabled,
  1819. dev_data->iommu_v2);
  1820. }
  1821. }
  1822. static void update_domain(struct protection_domain *domain)
  1823. {
  1824. if (!domain->updated)
  1825. return;
  1826. update_device_table(domain);
  1827. domain_flush_devices(domain);
  1828. domain_flush_tlb_pde(domain);
  1829. domain->updated = false;
  1830. }
  1831. static int dir2prot(enum dma_data_direction direction)
  1832. {
  1833. if (direction == DMA_TO_DEVICE)
  1834. return IOMMU_PROT_IR;
  1835. else if (direction == DMA_FROM_DEVICE)
  1836. return IOMMU_PROT_IW;
  1837. else if (direction == DMA_BIDIRECTIONAL)
  1838. return IOMMU_PROT_IW | IOMMU_PROT_IR;
  1839. else
  1840. return 0;
  1841. }
  1842. /*
  1843. * This function contains common code for mapping of a physically
  1844. * contiguous memory region into DMA address space. It is used by all
  1845. * mapping functions provided with this IOMMU driver.
  1846. * Must be called with the domain lock held.
  1847. */
  1848. static dma_addr_t __map_single(struct device *dev,
  1849. struct dma_ops_domain *dma_dom,
  1850. phys_addr_t paddr,
  1851. size_t size,
  1852. enum dma_data_direction direction,
  1853. u64 dma_mask)
  1854. {
  1855. dma_addr_t offset = paddr & ~PAGE_MASK;
  1856. dma_addr_t address, start, ret;
  1857. unsigned int pages;
  1858. int prot = 0;
  1859. int i;
  1860. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1861. paddr &= PAGE_MASK;
  1862. address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask);
  1863. if (address == AMD_IOMMU_MAPPING_ERROR)
  1864. goto out;
  1865. prot = dir2prot(direction);
  1866. start = address;
  1867. for (i = 0; i < pages; ++i) {
  1868. ret = iommu_map_page(&dma_dom->domain, start, paddr,
  1869. PAGE_SIZE, prot, GFP_ATOMIC);
  1870. if (ret)
  1871. goto out_unmap;
  1872. paddr += PAGE_SIZE;
  1873. start += PAGE_SIZE;
  1874. }
  1875. address += offset;
  1876. if (unlikely(amd_iommu_np_cache)) {
  1877. domain_flush_pages(&dma_dom->domain, address, size);
  1878. domain_flush_complete(&dma_dom->domain);
  1879. }
  1880. out:
  1881. return address;
  1882. out_unmap:
  1883. for (--i; i >= 0; --i) {
  1884. start -= PAGE_SIZE;
  1885. iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
  1886. }
  1887. domain_flush_tlb(&dma_dom->domain);
  1888. domain_flush_complete(&dma_dom->domain);
  1889. dma_ops_free_iova(dma_dom, address, pages);
  1890. return AMD_IOMMU_MAPPING_ERROR;
  1891. }
  1892. /*
  1893. * Does the reverse of the __map_single function. Must be called with
  1894. * the domain lock held too
  1895. */
  1896. static void __unmap_single(struct dma_ops_domain *dma_dom,
  1897. dma_addr_t dma_addr,
  1898. size_t size,
  1899. int dir)
  1900. {
  1901. dma_addr_t i, start;
  1902. unsigned int pages;
  1903. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  1904. dma_addr &= PAGE_MASK;
  1905. start = dma_addr;
  1906. for (i = 0; i < pages; ++i) {
  1907. iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE);
  1908. start += PAGE_SIZE;
  1909. }
  1910. if (amd_iommu_unmap_flush) {
  1911. domain_flush_tlb(&dma_dom->domain);
  1912. domain_flush_complete(&dma_dom->domain);
  1913. dma_ops_free_iova(dma_dom, dma_addr, pages);
  1914. } else {
  1915. pages = __roundup_pow_of_two(pages);
  1916. queue_iova(&dma_dom->iovad, dma_addr >> PAGE_SHIFT, pages, 0);
  1917. }
  1918. }
  1919. /*
  1920. * The exported map_single function for dma_ops.
  1921. */
  1922. static dma_addr_t map_page(struct device *dev, struct page *page,
  1923. unsigned long offset, size_t size,
  1924. enum dma_data_direction dir,
  1925. unsigned long attrs)
  1926. {
  1927. phys_addr_t paddr = page_to_phys(page) + offset;
  1928. struct protection_domain *domain;
  1929. struct dma_ops_domain *dma_dom;
  1930. u64 dma_mask;
  1931. domain = get_domain(dev);
  1932. if (PTR_ERR(domain) == -EINVAL)
  1933. return (dma_addr_t)paddr;
  1934. else if (IS_ERR(domain))
  1935. return AMD_IOMMU_MAPPING_ERROR;
  1936. dma_mask = *dev->dma_mask;
  1937. dma_dom = to_dma_ops_domain(domain);
  1938. return __map_single(dev, dma_dom, paddr, size, dir, dma_mask);
  1939. }
  1940. /*
  1941. * The exported unmap_single function for dma_ops.
  1942. */
  1943. static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
  1944. enum dma_data_direction dir, unsigned long attrs)
  1945. {
  1946. struct protection_domain *domain;
  1947. struct dma_ops_domain *dma_dom;
  1948. domain = get_domain(dev);
  1949. if (IS_ERR(domain))
  1950. return;
  1951. dma_dom = to_dma_ops_domain(domain);
  1952. __unmap_single(dma_dom, dma_addr, size, dir);
  1953. }
  1954. static int sg_num_pages(struct device *dev,
  1955. struct scatterlist *sglist,
  1956. int nelems)
  1957. {
  1958. unsigned long mask, boundary_size;
  1959. struct scatterlist *s;
  1960. int i, npages = 0;
  1961. mask = dma_get_seg_boundary(dev);
  1962. boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT :
  1963. 1UL << (BITS_PER_LONG - PAGE_SHIFT);
  1964. for_each_sg(sglist, s, nelems, i) {
  1965. int p, n;
  1966. s->dma_address = npages << PAGE_SHIFT;
  1967. p = npages % boundary_size;
  1968. n = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
  1969. if (p + n > boundary_size)
  1970. npages += boundary_size - p;
  1971. npages += n;
  1972. }
  1973. return npages;
  1974. }
  1975. /*
  1976. * The exported map_sg function for dma_ops (handles scatter-gather
  1977. * lists).
  1978. */
  1979. static int map_sg(struct device *dev, struct scatterlist *sglist,
  1980. int nelems, enum dma_data_direction direction,
  1981. unsigned long attrs)
  1982. {
  1983. int mapped_pages = 0, npages = 0, prot = 0, i;
  1984. struct protection_domain *domain;
  1985. struct dma_ops_domain *dma_dom;
  1986. struct scatterlist *s;
  1987. unsigned long address;
  1988. u64 dma_mask;
  1989. domain = get_domain(dev);
  1990. if (IS_ERR(domain))
  1991. return 0;
  1992. dma_dom = to_dma_ops_domain(domain);
  1993. dma_mask = *dev->dma_mask;
  1994. npages = sg_num_pages(dev, sglist, nelems);
  1995. address = dma_ops_alloc_iova(dev, dma_dom, npages, dma_mask);
  1996. if (address == AMD_IOMMU_MAPPING_ERROR)
  1997. goto out_err;
  1998. prot = dir2prot(direction);
  1999. /* Map all sg entries */
  2000. for_each_sg(sglist, s, nelems, i) {
  2001. int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
  2002. for (j = 0; j < pages; ++j) {
  2003. unsigned long bus_addr, phys_addr;
  2004. int ret;
  2005. bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
  2006. phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT);
  2007. ret = iommu_map_page(domain, bus_addr, phys_addr, PAGE_SIZE, prot, GFP_ATOMIC);
  2008. if (ret)
  2009. goto out_unmap;
  2010. mapped_pages += 1;
  2011. }
  2012. }
  2013. /* Everything is mapped - write the right values into s->dma_address */
  2014. for_each_sg(sglist, s, nelems, i) {
  2015. s->dma_address += address + s->offset;
  2016. s->dma_length = s->length;
  2017. }
  2018. return nelems;
  2019. out_unmap:
  2020. pr_err("%s: IOMMU mapping error in map_sg (io-pages: %d)\n",
  2021. dev_name(dev), npages);
  2022. for_each_sg(sglist, s, nelems, i) {
  2023. int j, pages = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE);
  2024. for (j = 0; j < pages; ++j) {
  2025. unsigned long bus_addr;
  2026. bus_addr = address + s->dma_address + (j << PAGE_SHIFT);
  2027. iommu_unmap_page(domain, bus_addr, PAGE_SIZE);
  2028. if (--mapped_pages == 0)
  2029. goto out_free_iova;
  2030. }
  2031. }
  2032. out_free_iova:
  2033. free_iova_fast(&dma_dom->iovad, address >> PAGE_SHIFT, npages);
  2034. out_err:
  2035. return 0;
  2036. }
  2037. /*
  2038. * The exported map_sg function for dma_ops (handles scatter-gather
  2039. * lists).
  2040. */
  2041. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  2042. int nelems, enum dma_data_direction dir,
  2043. unsigned long attrs)
  2044. {
  2045. struct protection_domain *domain;
  2046. struct dma_ops_domain *dma_dom;
  2047. unsigned long startaddr;
  2048. int npages = 2;
  2049. domain = get_domain(dev);
  2050. if (IS_ERR(domain))
  2051. return;
  2052. startaddr = sg_dma_address(sglist) & PAGE_MASK;
  2053. dma_dom = to_dma_ops_domain(domain);
  2054. npages = sg_num_pages(dev, sglist, nelems);
  2055. __unmap_single(dma_dom, startaddr, npages << PAGE_SHIFT, dir);
  2056. }
  2057. /*
  2058. * The exported alloc_coherent function for dma_ops.
  2059. */
  2060. static void *alloc_coherent(struct device *dev, size_t size,
  2061. dma_addr_t *dma_addr, gfp_t flag,
  2062. unsigned long attrs)
  2063. {
  2064. u64 dma_mask = dev->coherent_dma_mask;
  2065. struct protection_domain *domain;
  2066. struct dma_ops_domain *dma_dom;
  2067. struct page *page;
  2068. domain = get_domain(dev);
  2069. if (PTR_ERR(domain) == -EINVAL) {
  2070. page = alloc_pages(flag, get_order(size));
  2071. *dma_addr = page_to_phys(page);
  2072. return page_address(page);
  2073. } else if (IS_ERR(domain))
  2074. return NULL;
  2075. dma_dom = to_dma_ops_domain(domain);
  2076. size = PAGE_ALIGN(size);
  2077. dma_mask = dev->coherent_dma_mask;
  2078. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  2079. flag |= __GFP_ZERO;
  2080. page = alloc_pages(flag | __GFP_NOWARN, get_order(size));
  2081. if (!page) {
  2082. if (!gfpflags_allow_blocking(flag))
  2083. return NULL;
  2084. page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
  2085. get_order(size), flag & __GFP_NOWARN);
  2086. if (!page)
  2087. return NULL;
  2088. }
  2089. if (!dma_mask)
  2090. dma_mask = *dev->dma_mask;
  2091. *dma_addr = __map_single(dev, dma_dom, page_to_phys(page),
  2092. size, DMA_BIDIRECTIONAL, dma_mask);
  2093. if (*dma_addr == AMD_IOMMU_MAPPING_ERROR)
  2094. goto out_free;
  2095. return page_address(page);
  2096. out_free:
  2097. if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
  2098. __free_pages(page, get_order(size));
  2099. return NULL;
  2100. }
  2101. /*
  2102. * The exported free_coherent function for dma_ops.
  2103. */
  2104. static void free_coherent(struct device *dev, size_t size,
  2105. void *virt_addr, dma_addr_t dma_addr,
  2106. unsigned long attrs)
  2107. {
  2108. struct protection_domain *domain;
  2109. struct dma_ops_domain *dma_dom;
  2110. struct page *page;
  2111. page = virt_to_page(virt_addr);
  2112. size = PAGE_ALIGN(size);
  2113. domain = get_domain(dev);
  2114. if (IS_ERR(domain))
  2115. goto free_mem;
  2116. dma_dom = to_dma_ops_domain(domain);
  2117. __unmap_single(dma_dom, dma_addr, size, DMA_BIDIRECTIONAL);
  2118. free_mem:
  2119. if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
  2120. __free_pages(page, get_order(size));
  2121. }
  2122. /*
  2123. * This function is called by the DMA layer to find out if we can handle a
  2124. * particular device. It is part of the dma_ops.
  2125. */
  2126. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  2127. {
  2128. if (!dma_direct_supported(dev, mask))
  2129. return 0;
  2130. return check_device(dev);
  2131. }
  2132. static int amd_iommu_mapping_error(struct device *dev, dma_addr_t dma_addr)
  2133. {
  2134. return dma_addr == AMD_IOMMU_MAPPING_ERROR;
  2135. }
  2136. static const struct dma_map_ops amd_iommu_dma_ops = {
  2137. .alloc = alloc_coherent,
  2138. .free = free_coherent,
  2139. .map_page = map_page,
  2140. .unmap_page = unmap_page,
  2141. .map_sg = map_sg,
  2142. .unmap_sg = unmap_sg,
  2143. .dma_supported = amd_iommu_dma_supported,
  2144. .mapping_error = amd_iommu_mapping_error,
  2145. };
  2146. static int init_reserved_iova_ranges(void)
  2147. {
  2148. struct pci_dev *pdev = NULL;
  2149. struct iova *val;
  2150. init_iova_domain(&reserved_iova_ranges, PAGE_SIZE, IOVA_START_PFN);
  2151. lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock,
  2152. &reserved_rbtree_key);
  2153. /* MSI memory range */
  2154. val = reserve_iova(&reserved_iova_ranges,
  2155. IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END));
  2156. if (!val) {
  2157. pr_err("Reserving MSI range failed\n");
  2158. return -ENOMEM;
  2159. }
  2160. /* HT memory range */
  2161. val = reserve_iova(&reserved_iova_ranges,
  2162. IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END));
  2163. if (!val) {
  2164. pr_err("Reserving HT range failed\n");
  2165. return -ENOMEM;
  2166. }
  2167. /*
  2168. * Memory used for PCI resources
  2169. * FIXME: Check whether we can reserve the PCI-hole completly
  2170. */
  2171. for_each_pci_dev(pdev) {
  2172. int i;
  2173. for (i = 0; i < PCI_NUM_RESOURCES; ++i) {
  2174. struct resource *r = &pdev->resource[i];
  2175. if (!(r->flags & IORESOURCE_MEM))
  2176. continue;
  2177. val = reserve_iova(&reserved_iova_ranges,
  2178. IOVA_PFN(r->start),
  2179. IOVA_PFN(r->end));
  2180. if (!val) {
  2181. pr_err("Reserve pci-resource range failed\n");
  2182. return -ENOMEM;
  2183. }
  2184. }
  2185. }
  2186. return 0;
  2187. }
  2188. int __init amd_iommu_init_api(void)
  2189. {
  2190. int ret, err = 0;
  2191. ret = iova_cache_get();
  2192. if (ret)
  2193. return ret;
  2194. ret = init_reserved_iova_ranges();
  2195. if (ret)
  2196. return ret;
  2197. err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
  2198. if (err)
  2199. return err;
  2200. #ifdef CONFIG_ARM_AMBA
  2201. err = bus_set_iommu(&amba_bustype, &amd_iommu_ops);
  2202. if (err)
  2203. return err;
  2204. #endif
  2205. err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops);
  2206. if (err)
  2207. return err;
  2208. return 0;
  2209. }
  2210. int __init amd_iommu_init_dma_ops(void)
  2211. {
  2212. swiotlb = (iommu_pass_through || sme_me_mask) ? 1 : 0;
  2213. iommu_detected = 1;
  2214. /*
  2215. * In case we don't initialize SWIOTLB (actually the common case
  2216. * when AMD IOMMU is enabled and SME is not active), make sure there
  2217. * are global dma_ops set as a fall-back for devices not handled by
  2218. * this driver (for example non-PCI devices). When SME is active,
  2219. * make sure that swiotlb variable remains set so the global dma_ops
  2220. * continue to be SWIOTLB.
  2221. */
  2222. if (!swiotlb)
  2223. dma_ops = &dma_direct_ops;
  2224. if (amd_iommu_unmap_flush)
  2225. pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n");
  2226. else
  2227. pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n");
  2228. return 0;
  2229. }
  2230. /*****************************************************************************
  2231. *
  2232. * The following functions belong to the exported interface of AMD IOMMU
  2233. *
  2234. * This interface allows access to lower level functions of the IOMMU
  2235. * like protection domain handling and assignement of devices to domains
  2236. * which is not possible with the dma_ops interface.
  2237. *
  2238. *****************************************************************************/
  2239. static void cleanup_domain(struct protection_domain *domain)
  2240. {
  2241. struct iommu_dev_data *entry;
  2242. unsigned long flags;
  2243. spin_lock_irqsave(&amd_iommu_devtable_lock, flags);
  2244. while (!list_empty(&domain->dev_list)) {
  2245. entry = list_first_entry(&domain->dev_list,
  2246. struct iommu_dev_data, list);
  2247. BUG_ON(!entry->domain);
  2248. __detach_device(entry);
  2249. }
  2250. spin_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  2251. }
  2252. static void protection_domain_free(struct protection_domain *domain)
  2253. {
  2254. if (!domain)
  2255. return;
  2256. del_domain_from_list(domain);
  2257. if (domain->id)
  2258. domain_id_free(domain->id);
  2259. kfree(domain);
  2260. }
  2261. static int protection_domain_init(struct protection_domain *domain)
  2262. {
  2263. spin_lock_init(&domain->lock);
  2264. mutex_init(&domain->api_lock);
  2265. domain->id = domain_id_alloc();
  2266. if (!domain->id)
  2267. return -ENOMEM;
  2268. INIT_LIST_HEAD(&domain->dev_list);
  2269. return 0;
  2270. }
  2271. static struct protection_domain *protection_domain_alloc(void)
  2272. {
  2273. struct protection_domain *domain;
  2274. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  2275. if (!domain)
  2276. return NULL;
  2277. if (protection_domain_init(domain))
  2278. goto out_err;
  2279. add_domain_to_list(domain);
  2280. return domain;
  2281. out_err:
  2282. kfree(domain);
  2283. return NULL;
  2284. }
  2285. static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
  2286. {
  2287. struct protection_domain *pdomain;
  2288. struct dma_ops_domain *dma_domain;
  2289. switch (type) {
  2290. case IOMMU_DOMAIN_UNMANAGED:
  2291. pdomain = protection_domain_alloc();
  2292. if (!pdomain)
  2293. return NULL;
  2294. pdomain->mode = PAGE_MODE_3_LEVEL;
  2295. pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  2296. if (!pdomain->pt_root) {
  2297. protection_domain_free(pdomain);
  2298. return NULL;
  2299. }
  2300. pdomain->domain.geometry.aperture_start = 0;
  2301. pdomain->domain.geometry.aperture_end = ~0ULL;
  2302. pdomain->domain.geometry.force_aperture = true;
  2303. break;
  2304. case IOMMU_DOMAIN_DMA:
  2305. dma_domain = dma_ops_domain_alloc();
  2306. if (!dma_domain) {
  2307. pr_err("AMD-Vi: Failed to allocate\n");
  2308. return NULL;
  2309. }
  2310. pdomain = &dma_domain->domain;
  2311. break;
  2312. case IOMMU_DOMAIN_IDENTITY:
  2313. pdomain = protection_domain_alloc();
  2314. if (!pdomain)
  2315. return NULL;
  2316. pdomain->mode = PAGE_MODE_NONE;
  2317. break;
  2318. default:
  2319. return NULL;
  2320. }
  2321. return &pdomain->domain;
  2322. }
  2323. static void amd_iommu_domain_free(struct iommu_domain *dom)
  2324. {
  2325. struct protection_domain *domain;
  2326. struct dma_ops_domain *dma_dom;
  2327. domain = to_pdomain(dom);
  2328. if (domain->dev_cnt > 0)
  2329. cleanup_domain(domain);
  2330. BUG_ON(domain->dev_cnt != 0);
  2331. if (!dom)
  2332. return;
  2333. switch (dom->type) {
  2334. case IOMMU_DOMAIN_DMA:
  2335. /* Now release the domain */
  2336. dma_dom = to_dma_ops_domain(domain);
  2337. dma_ops_domain_free(dma_dom);
  2338. break;
  2339. default:
  2340. if (domain->mode != PAGE_MODE_NONE)
  2341. free_pagetable(domain);
  2342. if (domain->flags & PD_IOMMUV2_MASK)
  2343. free_gcr3_table(domain);
  2344. protection_domain_free(domain);
  2345. break;
  2346. }
  2347. }
  2348. static void amd_iommu_detach_device(struct iommu_domain *dom,
  2349. struct device *dev)
  2350. {
  2351. struct iommu_dev_data *dev_data = dev->archdata.iommu;
  2352. struct amd_iommu *iommu;
  2353. int devid;
  2354. if (!check_device(dev))
  2355. return;
  2356. devid = get_device_id(dev);
  2357. if (devid < 0)
  2358. return;
  2359. if (dev_data->domain != NULL)
  2360. detach_device(dev);
  2361. iommu = amd_iommu_rlookup_table[devid];
  2362. if (!iommu)
  2363. return;
  2364. #ifdef CONFIG_IRQ_REMAP
  2365. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
  2366. (dom->type == IOMMU_DOMAIN_UNMANAGED))
  2367. dev_data->use_vapic = 0;
  2368. #endif
  2369. iommu_completion_wait(iommu);
  2370. }
  2371. static int amd_iommu_attach_device(struct iommu_domain *dom,
  2372. struct device *dev)
  2373. {
  2374. struct protection_domain *domain = to_pdomain(dom);
  2375. struct iommu_dev_data *dev_data;
  2376. struct amd_iommu *iommu;
  2377. int ret;
  2378. if (!check_device(dev))
  2379. return -EINVAL;
  2380. dev_data = dev->archdata.iommu;
  2381. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2382. if (!iommu)
  2383. return -EINVAL;
  2384. if (dev_data->domain)
  2385. detach_device(dev);
  2386. ret = attach_device(dev, domain);
  2387. #ifdef CONFIG_IRQ_REMAP
  2388. if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
  2389. if (dom->type == IOMMU_DOMAIN_UNMANAGED)
  2390. dev_data->use_vapic = 1;
  2391. else
  2392. dev_data->use_vapic = 0;
  2393. }
  2394. #endif
  2395. iommu_completion_wait(iommu);
  2396. return ret;
  2397. }
  2398. static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
  2399. phys_addr_t paddr, size_t page_size, int iommu_prot)
  2400. {
  2401. struct protection_domain *domain = to_pdomain(dom);
  2402. int prot = 0;
  2403. int ret;
  2404. if (domain->mode == PAGE_MODE_NONE)
  2405. return -EINVAL;
  2406. if (iommu_prot & IOMMU_READ)
  2407. prot |= IOMMU_PROT_IR;
  2408. if (iommu_prot & IOMMU_WRITE)
  2409. prot |= IOMMU_PROT_IW;
  2410. mutex_lock(&domain->api_lock);
  2411. ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL);
  2412. mutex_unlock(&domain->api_lock);
  2413. return ret;
  2414. }
  2415. static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
  2416. size_t page_size)
  2417. {
  2418. struct protection_domain *domain = to_pdomain(dom);
  2419. size_t unmap_size;
  2420. if (domain->mode == PAGE_MODE_NONE)
  2421. return 0;
  2422. mutex_lock(&domain->api_lock);
  2423. unmap_size = iommu_unmap_page(domain, iova, page_size);
  2424. mutex_unlock(&domain->api_lock);
  2425. return unmap_size;
  2426. }
  2427. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  2428. dma_addr_t iova)
  2429. {
  2430. struct protection_domain *domain = to_pdomain(dom);
  2431. unsigned long offset_mask, pte_pgsize;
  2432. u64 *pte, __pte;
  2433. if (domain->mode == PAGE_MODE_NONE)
  2434. return iova;
  2435. pte = fetch_pte(domain, iova, &pte_pgsize);
  2436. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  2437. return 0;
  2438. offset_mask = pte_pgsize - 1;
  2439. __pte = __sme_clr(*pte & PM_ADDR_MASK);
  2440. return (__pte & ~offset_mask) | (iova & offset_mask);
  2441. }
  2442. static bool amd_iommu_capable(enum iommu_cap cap)
  2443. {
  2444. switch (cap) {
  2445. case IOMMU_CAP_CACHE_COHERENCY:
  2446. return true;
  2447. case IOMMU_CAP_INTR_REMAP:
  2448. return (irq_remapping_enabled == 1);
  2449. case IOMMU_CAP_NOEXEC:
  2450. return false;
  2451. }
  2452. return false;
  2453. }
  2454. static void amd_iommu_get_resv_regions(struct device *dev,
  2455. struct list_head *head)
  2456. {
  2457. struct iommu_resv_region *region;
  2458. struct unity_map_entry *entry;
  2459. int devid;
  2460. devid = get_device_id(dev);
  2461. if (devid < 0)
  2462. return;
  2463. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  2464. size_t length;
  2465. int prot = 0;
  2466. if (devid < entry->devid_start || devid > entry->devid_end)
  2467. continue;
  2468. length = entry->address_end - entry->address_start;
  2469. if (entry->prot & IOMMU_PROT_IR)
  2470. prot |= IOMMU_READ;
  2471. if (entry->prot & IOMMU_PROT_IW)
  2472. prot |= IOMMU_WRITE;
  2473. region = iommu_alloc_resv_region(entry->address_start,
  2474. length, prot,
  2475. IOMMU_RESV_DIRECT);
  2476. if (!region) {
  2477. pr_err("Out of memory allocating dm-regions for %s\n",
  2478. dev_name(dev));
  2479. return;
  2480. }
  2481. list_add_tail(&region->list, head);
  2482. }
  2483. region = iommu_alloc_resv_region(MSI_RANGE_START,
  2484. MSI_RANGE_END - MSI_RANGE_START + 1,
  2485. 0, IOMMU_RESV_MSI);
  2486. if (!region)
  2487. return;
  2488. list_add_tail(&region->list, head);
  2489. region = iommu_alloc_resv_region(HT_RANGE_START,
  2490. HT_RANGE_END - HT_RANGE_START + 1,
  2491. 0, IOMMU_RESV_RESERVED);
  2492. if (!region)
  2493. return;
  2494. list_add_tail(&region->list, head);
  2495. }
  2496. static void amd_iommu_put_resv_regions(struct device *dev,
  2497. struct list_head *head)
  2498. {
  2499. struct iommu_resv_region *entry, *next;
  2500. list_for_each_entry_safe(entry, next, head, list)
  2501. kfree(entry);
  2502. }
  2503. static void amd_iommu_apply_resv_region(struct device *dev,
  2504. struct iommu_domain *domain,
  2505. struct iommu_resv_region *region)
  2506. {
  2507. struct dma_ops_domain *dma_dom = to_dma_ops_domain(to_pdomain(domain));
  2508. unsigned long start, end;
  2509. start = IOVA_PFN(region->start);
  2510. end = IOVA_PFN(region->start + region->length - 1);
  2511. WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
  2512. }
  2513. static bool amd_iommu_is_attach_deferred(struct iommu_domain *domain,
  2514. struct device *dev)
  2515. {
  2516. struct iommu_dev_data *dev_data = dev->archdata.iommu;
  2517. return dev_data->defer_attach;
  2518. }
  2519. static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)
  2520. {
  2521. struct protection_domain *dom = to_pdomain(domain);
  2522. domain_flush_tlb_pde(dom);
  2523. domain_flush_complete(dom);
  2524. }
  2525. static void amd_iommu_iotlb_range_add(struct iommu_domain *domain,
  2526. unsigned long iova, size_t size)
  2527. {
  2528. }
  2529. const struct iommu_ops amd_iommu_ops = {
  2530. .capable = amd_iommu_capable,
  2531. .domain_alloc = amd_iommu_domain_alloc,
  2532. .domain_free = amd_iommu_domain_free,
  2533. .attach_dev = amd_iommu_attach_device,
  2534. .detach_dev = amd_iommu_detach_device,
  2535. .map = amd_iommu_map,
  2536. .unmap = amd_iommu_unmap,
  2537. .iova_to_phys = amd_iommu_iova_to_phys,
  2538. .add_device = amd_iommu_add_device,
  2539. .remove_device = amd_iommu_remove_device,
  2540. .device_group = amd_iommu_device_group,
  2541. .get_resv_regions = amd_iommu_get_resv_regions,
  2542. .put_resv_regions = amd_iommu_put_resv_regions,
  2543. .apply_resv_region = amd_iommu_apply_resv_region,
  2544. .is_attach_deferred = amd_iommu_is_attach_deferred,
  2545. .pgsize_bitmap = AMD_IOMMU_PGSIZES,
  2546. .flush_iotlb_all = amd_iommu_flush_iotlb_all,
  2547. .iotlb_range_add = amd_iommu_iotlb_range_add,
  2548. .iotlb_sync = amd_iommu_flush_iotlb_all,
  2549. };
  2550. /*****************************************************************************
  2551. *
  2552. * The next functions do a basic initialization of IOMMU for pass through
  2553. * mode
  2554. *
  2555. * In passthrough mode the IOMMU is initialized and enabled but not used for
  2556. * DMA-API translation.
  2557. *
  2558. *****************************************************************************/
  2559. /* IOMMUv2 specific functions */
  2560. int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
  2561. {
  2562. return atomic_notifier_chain_register(&ppr_notifier, nb);
  2563. }
  2564. EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
  2565. int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
  2566. {
  2567. return atomic_notifier_chain_unregister(&ppr_notifier, nb);
  2568. }
  2569. EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
  2570. void amd_iommu_domain_direct_map(struct iommu_domain *dom)
  2571. {
  2572. struct protection_domain *domain = to_pdomain(dom);
  2573. unsigned long flags;
  2574. spin_lock_irqsave(&domain->lock, flags);
  2575. /* Update data structure */
  2576. domain->mode = PAGE_MODE_NONE;
  2577. domain->updated = true;
  2578. /* Make changes visible to IOMMUs */
  2579. update_domain(domain);
  2580. /* Page-table is not visible to IOMMU anymore, so free it */
  2581. free_pagetable(domain);
  2582. spin_unlock_irqrestore(&domain->lock, flags);
  2583. }
  2584. EXPORT_SYMBOL(amd_iommu_domain_direct_map);
  2585. int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
  2586. {
  2587. struct protection_domain *domain = to_pdomain(dom);
  2588. unsigned long flags;
  2589. int levels, ret;
  2590. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  2591. return -EINVAL;
  2592. /* Number of GCR3 table levels required */
  2593. for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
  2594. levels += 1;
  2595. if (levels > amd_iommu_max_glx_val)
  2596. return -EINVAL;
  2597. spin_lock_irqsave(&domain->lock, flags);
  2598. /*
  2599. * Save us all sanity checks whether devices already in the
  2600. * domain support IOMMUv2. Just force that the domain has no
  2601. * devices attached when it is switched into IOMMUv2 mode.
  2602. */
  2603. ret = -EBUSY;
  2604. if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
  2605. goto out;
  2606. ret = -ENOMEM;
  2607. domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
  2608. if (domain->gcr3_tbl == NULL)
  2609. goto out;
  2610. domain->glx = levels;
  2611. domain->flags |= PD_IOMMUV2_MASK;
  2612. domain->updated = true;
  2613. update_domain(domain);
  2614. ret = 0;
  2615. out:
  2616. spin_unlock_irqrestore(&domain->lock, flags);
  2617. return ret;
  2618. }
  2619. EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
  2620. static int __flush_pasid(struct protection_domain *domain, int pasid,
  2621. u64 address, bool size)
  2622. {
  2623. struct iommu_dev_data *dev_data;
  2624. struct iommu_cmd cmd;
  2625. int i, ret;
  2626. if (!(domain->flags & PD_IOMMUV2_MASK))
  2627. return -EINVAL;
  2628. build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
  2629. /*
  2630. * IOMMU TLB needs to be flushed before Device TLB to
  2631. * prevent device TLB refill from IOMMU TLB
  2632. */
  2633. for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
  2634. if (domain->dev_iommu[i] == 0)
  2635. continue;
  2636. ret = iommu_queue_command(amd_iommus[i], &cmd);
  2637. if (ret != 0)
  2638. goto out;
  2639. }
  2640. /* Wait until IOMMU TLB flushes are complete */
  2641. domain_flush_complete(domain);
  2642. /* Now flush device TLBs */
  2643. list_for_each_entry(dev_data, &domain->dev_list, list) {
  2644. struct amd_iommu *iommu;
  2645. int qdep;
  2646. /*
  2647. There might be non-IOMMUv2 capable devices in an IOMMUv2
  2648. * domain.
  2649. */
  2650. if (!dev_data->ats.enabled)
  2651. continue;
  2652. qdep = dev_data->ats.qdep;
  2653. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2654. build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
  2655. qdep, address, size);
  2656. ret = iommu_queue_command(iommu, &cmd);
  2657. if (ret != 0)
  2658. goto out;
  2659. }
  2660. /* Wait until all device TLBs are flushed */
  2661. domain_flush_complete(domain);
  2662. ret = 0;
  2663. out:
  2664. return ret;
  2665. }
  2666. static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
  2667. u64 address)
  2668. {
  2669. return __flush_pasid(domain, pasid, address, false);
  2670. }
  2671. int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
  2672. u64 address)
  2673. {
  2674. struct protection_domain *domain = to_pdomain(dom);
  2675. unsigned long flags;
  2676. int ret;
  2677. spin_lock_irqsave(&domain->lock, flags);
  2678. ret = __amd_iommu_flush_page(domain, pasid, address);
  2679. spin_unlock_irqrestore(&domain->lock, flags);
  2680. return ret;
  2681. }
  2682. EXPORT_SYMBOL(amd_iommu_flush_page);
  2683. static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
  2684. {
  2685. return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  2686. true);
  2687. }
  2688. int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
  2689. {
  2690. struct protection_domain *domain = to_pdomain(dom);
  2691. unsigned long flags;
  2692. int ret;
  2693. spin_lock_irqsave(&domain->lock, flags);
  2694. ret = __amd_iommu_flush_tlb(domain, pasid);
  2695. spin_unlock_irqrestore(&domain->lock, flags);
  2696. return ret;
  2697. }
  2698. EXPORT_SYMBOL(amd_iommu_flush_tlb);
  2699. static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
  2700. {
  2701. int index;
  2702. u64 *pte;
  2703. while (true) {
  2704. index = (pasid >> (9 * level)) & 0x1ff;
  2705. pte = &root[index];
  2706. if (level == 0)
  2707. break;
  2708. if (!(*pte & GCR3_VALID)) {
  2709. if (!alloc)
  2710. return NULL;
  2711. root = (void *)get_zeroed_page(GFP_ATOMIC);
  2712. if (root == NULL)
  2713. return NULL;
  2714. *pte = iommu_virt_to_phys(root) | GCR3_VALID;
  2715. }
  2716. root = iommu_phys_to_virt(*pte & PAGE_MASK);
  2717. level -= 1;
  2718. }
  2719. return pte;
  2720. }
  2721. static int __set_gcr3(struct protection_domain *domain, int pasid,
  2722. unsigned long cr3)
  2723. {
  2724. u64 *pte;
  2725. if (domain->mode != PAGE_MODE_NONE)
  2726. return -EINVAL;
  2727. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
  2728. if (pte == NULL)
  2729. return -ENOMEM;
  2730. *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
  2731. return __amd_iommu_flush_tlb(domain, pasid);
  2732. }
  2733. static int __clear_gcr3(struct protection_domain *domain, int pasid)
  2734. {
  2735. u64 *pte;
  2736. if (domain->mode != PAGE_MODE_NONE)
  2737. return -EINVAL;
  2738. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
  2739. if (pte == NULL)
  2740. return 0;
  2741. *pte = 0;
  2742. return __amd_iommu_flush_tlb(domain, pasid);
  2743. }
  2744. int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
  2745. unsigned long cr3)
  2746. {
  2747. struct protection_domain *domain = to_pdomain(dom);
  2748. unsigned long flags;
  2749. int ret;
  2750. spin_lock_irqsave(&domain->lock, flags);
  2751. ret = __set_gcr3(domain, pasid, cr3);
  2752. spin_unlock_irqrestore(&domain->lock, flags);
  2753. return ret;
  2754. }
  2755. EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
  2756. int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
  2757. {
  2758. struct protection_domain *domain = to_pdomain(dom);
  2759. unsigned long flags;
  2760. int ret;
  2761. spin_lock_irqsave(&domain->lock, flags);
  2762. ret = __clear_gcr3(domain, pasid);
  2763. spin_unlock_irqrestore(&domain->lock, flags);
  2764. return ret;
  2765. }
  2766. EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
  2767. int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
  2768. int status, int tag)
  2769. {
  2770. struct iommu_dev_data *dev_data;
  2771. struct amd_iommu *iommu;
  2772. struct iommu_cmd cmd;
  2773. dev_data = get_dev_data(&pdev->dev);
  2774. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2775. build_complete_ppr(&cmd, dev_data->devid, pasid, status,
  2776. tag, dev_data->pri_tlp);
  2777. return iommu_queue_command(iommu, &cmd);
  2778. }
  2779. EXPORT_SYMBOL(amd_iommu_complete_ppr);
  2780. struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev)
  2781. {
  2782. struct protection_domain *pdomain;
  2783. pdomain = get_domain(&pdev->dev);
  2784. if (IS_ERR(pdomain))
  2785. return NULL;
  2786. /* Only return IOMMUv2 domains */
  2787. if (!(pdomain->flags & PD_IOMMUV2_MASK))
  2788. return NULL;
  2789. return &pdomain->domain;
  2790. }
  2791. EXPORT_SYMBOL(amd_iommu_get_v2_domain);
  2792. void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum)
  2793. {
  2794. struct iommu_dev_data *dev_data;
  2795. if (!amd_iommu_v2_supported())
  2796. return;
  2797. dev_data = get_dev_data(&pdev->dev);
  2798. dev_data->errata |= (1 << erratum);
  2799. }
  2800. EXPORT_SYMBOL(amd_iommu_enable_device_erratum);
  2801. int amd_iommu_device_info(struct pci_dev *pdev,
  2802. struct amd_iommu_device_info *info)
  2803. {
  2804. int max_pasids;
  2805. int pos;
  2806. if (pdev == NULL || info == NULL)
  2807. return -EINVAL;
  2808. if (!amd_iommu_v2_supported())
  2809. return -EINVAL;
  2810. memset(info, 0, sizeof(*info));
  2811. if (!pci_ats_disabled()) {
  2812. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
  2813. if (pos)
  2814. info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
  2815. }
  2816. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  2817. if (pos)
  2818. info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP;
  2819. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  2820. if (pos) {
  2821. int features;
  2822. max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1));
  2823. max_pasids = min(max_pasids, (1 << 20));
  2824. info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
  2825. info->max_pasids = min(pci_max_pasids(pdev), max_pasids);
  2826. features = pci_pasid_features(pdev);
  2827. if (features & PCI_PASID_CAP_EXEC)
  2828. info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP;
  2829. if (features & PCI_PASID_CAP_PRIV)
  2830. info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP;
  2831. }
  2832. return 0;
  2833. }
  2834. EXPORT_SYMBOL(amd_iommu_device_info);
  2835. #ifdef CONFIG_IRQ_REMAP
  2836. /*****************************************************************************
  2837. *
  2838. * Interrupt Remapping Implementation
  2839. *
  2840. *****************************************************************************/
  2841. static struct irq_chip amd_ir_chip;
  2842. static DEFINE_SPINLOCK(iommu_table_lock);
  2843. static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
  2844. {
  2845. u64 dte;
  2846. dte = amd_iommu_dev_table[devid].data[2];
  2847. dte &= ~DTE_IRQ_PHYS_ADDR_MASK;
  2848. dte |= iommu_virt_to_phys(table->table);
  2849. dte |= DTE_IRQ_REMAP_INTCTL;
  2850. dte |= DTE_IRQ_TABLE_LEN;
  2851. dte |= DTE_IRQ_REMAP_ENABLE;
  2852. amd_iommu_dev_table[devid].data[2] = dte;
  2853. }
  2854. static struct irq_remap_table *get_irq_table(u16 devid)
  2855. {
  2856. struct irq_remap_table *table;
  2857. if (WARN_ONCE(!amd_iommu_rlookup_table[devid],
  2858. "%s: no iommu for devid %x\n", __func__, devid))
  2859. return NULL;
  2860. table = irq_lookup_table[devid];
  2861. if (WARN_ONCE(!table, "%s: no table for devid %x\n", __func__, devid))
  2862. return NULL;
  2863. return table;
  2864. }
  2865. static struct irq_remap_table *__alloc_irq_table(void)
  2866. {
  2867. struct irq_remap_table *table;
  2868. table = kzalloc(sizeof(*table), GFP_KERNEL);
  2869. if (!table)
  2870. return NULL;
  2871. table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_KERNEL);
  2872. if (!table->table) {
  2873. kfree(table);
  2874. return NULL;
  2875. }
  2876. raw_spin_lock_init(&table->lock);
  2877. if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
  2878. memset(table->table, 0,
  2879. MAX_IRQS_PER_TABLE * sizeof(u32));
  2880. else
  2881. memset(table->table, 0,
  2882. (MAX_IRQS_PER_TABLE * (sizeof(u64) * 2)));
  2883. return table;
  2884. }
  2885. static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid,
  2886. struct irq_remap_table *table)
  2887. {
  2888. irq_lookup_table[devid] = table;
  2889. set_dte_irq_entry(devid, table);
  2890. iommu_flush_dte(iommu, devid);
  2891. }
  2892. static struct irq_remap_table *alloc_irq_table(u16 devid)
  2893. {
  2894. struct irq_remap_table *table = NULL;
  2895. struct irq_remap_table *new_table = NULL;
  2896. struct amd_iommu *iommu;
  2897. unsigned long flags;
  2898. u16 alias;
  2899. spin_lock_irqsave(&iommu_table_lock, flags);
  2900. iommu = amd_iommu_rlookup_table[devid];
  2901. if (!iommu)
  2902. goto out_unlock;
  2903. table = irq_lookup_table[devid];
  2904. if (table)
  2905. goto out_unlock;
  2906. alias = amd_iommu_alias_table[devid];
  2907. table = irq_lookup_table[alias];
  2908. if (table) {
  2909. set_remap_table_entry(iommu, devid, table);
  2910. goto out_wait;
  2911. }
  2912. spin_unlock_irqrestore(&iommu_table_lock, flags);
  2913. /* Nothing there yet, allocate new irq remapping table */
  2914. new_table = __alloc_irq_table();
  2915. if (!new_table)
  2916. return NULL;
  2917. spin_lock_irqsave(&iommu_table_lock, flags);
  2918. table = irq_lookup_table[devid];
  2919. if (table)
  2920. goto out_unlock;
  2921. table = irq_lookup_table[alias];
  2922. if (table) {
  2923. set_remap_table_entry(iommu, devid, table);
  2924. goto out_wait;
  2925. }
  2926. table = new_table;
  2927. new_table = NULL;
  2928. set_remap_table_entry(iommu, devid, table);
  2929. if (devid != alias)
  2930. set_remap_table_entry(iommu, alias, table);
  2931. out_wait:
  2932. iommu_completion_wait(iommu);
  2933. out_unlock:
  2934. spin_unlock_irqrestore(&iommu_table_lock, flags);
  2935. if (new_table) {
  2936. kmem_cache_free(amd_iommu_irq_cache, new_table->table);
  2937. kfree(new_table);
  2938. }
  2939. return table;
  2940. }
  2941. static int alloc_irq_index(u16 devid, int count, bool align)
  2942. {
  2943. struct irq_remap_table *table;
  2944. int index, c, alignment = 1;
  2945. unsigned long flags;
  2946. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  2947. if (!iommu)
  2948. return -ENODEV;
  2949. table = alloc_irq_table(devid);
  2950. if (!table)
  2951. return -ENODEV;
  2952. if (align)
  2953. alignment = roundup_pow_of_two(count);
  2954. raw_spin_lock_irqsave(&table->lock, flags);
  2955. /* Scan table for free entries */
  2956. for (index = ALIGN(table->min_index, alignment), c = 0;
  2957. index < MAX_IRQS_PER_TABLE;) {
  2958. if (!iommu->irte_ops->is_allocated(table, index)) {
  2959. c += 1;
  2960. } else {
  2961. c = 0;
  2962. index = ALIGN(index + 1, alignment);
  2963. continue;
  2964. }
  2965. if (c == count) {
  2966. for (; c != 0; --c)
  2967. iommu->irte_ops->set_allocated(table, index - c + 1);
  2968. index -= count - 1;
  2969. goto out;
  2970. }
  2971. index++;
  2972. }
  2973. index = -ENOSPC;
  2974. out:
  2975. raw_spin_unlock_irqrestore(&table->lock, flags);
  2976. return index;
  2977. }
  2978. static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,
  2979. struct amd_ir_data *data)
  2980. {
  2981. struct irq_remap_table *table;
  2982. struct amd_iommu *iommu;
  2983. unsigned long flags;
  2984. struct irte_ga *entry;
  2985. iommu = amd_iommu_rlookup_table[devid];
  2986. if (iommu == NULL)
  2987. return -EINVAL;
  2988. table = get_irq_table(devid);
  2989. if (!table)
  2990. return -ENOMEM;
  2991. raw_spin_lock_irqsave(&table->lock, flags);
  2992. entry = (struct irte_ga *)table->table;
  2993. entry = &entry[index];
  2994. entry->lo.fields_remap.valid = 0;
  2995. entry->hi.val = irte->hi.val;
  2996. entry->lo.val = irte->lo.val;
  2997. entry->lo.fields_remap.valid = 1;
  2998. if (data)
  2999. data->ref = entry;
  3000. raw_spin_unlock_irqrestore(&table->lock, flags);
  3001. iommu_flush_irt(iommu, devid);
  3002. iommu_completion_wait(iommu);
  3003. return 0;
  3004. }
  3005. static int modify_irte(u16 devid, int index, union irte *irte)
  3006. {
  3007. struct irq_remap_table *table;
  3008. struct amd_iommu *iommu;
  3009. unsigned long flags;
  3010. iommu = amd_iommu_rlookup_table[devid];
  3011. if (iommu == NULL)
  3012. return -EINVAL;
  3013. table = get_irq_table(devid);
  3014. if (!table)
  3015. return -ENOMEM;
  3016. raw_spin_lock_irqsave(&table->lock, flags);
  3017. table->table[index] = irte->val;
  3018. raw_spin_unlock_irqrestore(&table->lock, flags);
  3019. iommu_flush_irt(iommu, devid);
  3020. iommu_completion_wait(iommu);
  3021. return 0;
  3022. }
  3023. static void free_irte(u16 devid, int index)
  3024. {
  3025. struct irq_remap_table *table;
  3026. struct amd_iommu *iommu;
  3027. unsigned long flags;
  3028. iommu = amd_iommu_rlookup_table[devid];
  3029. if (iommu == NULL)
  3030. return;
  3031. table = get_irq_table(devid);
  3032. if (!table)
  3033. return;
  3034. raw_spin_lock_irqsave(&table->lock, flags);
  3035. iommu->irte_ops->clear_allocated(table, index);
  3036. raw_spin_unlock_irqrestore(&table->lock, flags);
  3037. iommu_flush_irt(iommu, devid);
  3038. iommu_completion_wait(iommu);
  3039. }
  3040. static void irte_prepare(void *entry,
  3041. u32 delivery_mode, u32 dest_mode,
  3042. u8 vector, u32 dest_apicid, int devid)
  3043. {
  3044. union irte *irte = (union irte *) entry;
  3045. irte->val = 0;
  3046. irte->fields.vector = vector;
  3047. irte->fields.int_type = delivery_mode;
  3048. irte->fields.destination = dest_apicid;
  3049. irte->fields.dm = dest_mode;
  3050. irte->fields.valid = 1;
  3051. }
  3052. static void irte_ga_prepare(void *entry,
  3053. u32 delivery_mode, u32 dest_mode,
  3054. u8 vector, u32 dest_apicid, int devid)
  3055. {
  3056. struct irte_ga *irte = (struct irte_ga *) entry;
  3057. irte->lo.val = 0;
  3058. irte->hi.val = 0;
  3059. irte->lo.fields_remap.int_type = delivery_mode;
  3060. irte->lo.fields_remap.dm = dest_mode;
  3061. irte->hi.fields.vector = vector;
  3062. irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid);
  3063. irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid);
  3064. irte->lo.fields_remap.valid = 1;
  3065. }
  3066. static void irte_activate(void *entry, u16 devid, u16 index)
  3067. {
  3068. union irte *irte = (union irte *) entry;
  3069. irte->fields.valid = 1;
  3070. modify_irte(devid, index, irte);
  3071. }
  3072. static void irte_ga_activate(void *entry, u16 devid, u16 index)
  3073. {
  3074. struct irte_ga *irte = (struct irte_ga *) entry;
  3075. irte->lo.fields_remap.valid = 1;
  3076. modify_irte_ga(devid, index, irte, NULL);
  3077. }
  3078. static void irte_deactivate(void *entry, u16 devid, u16 index)
  3079. {
  3080. union irte *irte = (union irte *) entry;
  3081. irte->fields.valid = 0;
  3082. modify_irte(devid, index, irte);
  3083. }
  3084. static void irte_ga_deactivate(void *entry, u16 devid, u16 index)
  3085. {
  3086. struct irte_ga *irte = (struct irte_ga *) entry;
  3087. irte->lo.fields_remap.valid = 0;
  3088. modify_irte_ga(devid, index, irte, NULL);
  3089. }
  3090. static void irte_set_affinity(void *entry, u16 devid, u16 index,
  3091. u8 vector, u32 dest_apicid)
  3092. {
  3093. union irte *irte = (union irte *) entry;
  3094. irte->fields.vector = vector;
  3095. irte->fields.destination = dest_apicid;
  3096. modify_irte(devid, index, irte);
  3097. }
  3098. static void irte_ga_set_affinity(void *entry, u16 devid, u16 index,
  3099. u8 vector, u32 dest_apicid)
  3100. {
  3101. struct irte_ga *irte = (struct irte_ga *) entry;
  3102. if (!irte->lo.fields_remap.guest_mode) {
  3103. irte->hi.fields.vector = vector;
  3104. irte->lo.fields_remap.destination =
  3105. APICID_TO_IRTE_DEST_LO(dest_apicid);
  3106. irte->hi.fields.destination =
  3107. APICID_TO_IRTE_DEST_HI(dest_apicid);
  3108. modify_irte_ga(devid, index, irte, NULL);
  3109. }
  3110. }
  3111. #define IRTE_ALLOCATED (~1U)
  3112. static void irte_set_allocated(struct irq_remap_table *table, int index)
  3113. {
  3114. table->table[index] = IRTE_ALLOCATED;
  3115. }
  3116. static void irte_ga_set_allocated(struct irq_remap_table *table, int index)
  3117. {
  3118. struct irte_ga *ptr = (struct irte_ga *)table->table;
  3119. struct irte_ga *irte = &ptr[index];
  3120. memset(&irte->lo.val, 0, sizeof(u64));
  3121. memset(&irte->hi.val, 0, sizeof(u64));
  3122. irte->hi.fields.vector = 0xff;
  3123. }
  3124. static bool irte_is_allocated(struct irq_remap_table *table, int index)
  3125. {
  3126. union irte *ptr = (union irte *)table->table;
  3127. union irte *irte = &ptr[index];
  3128. return irte->val != 0;
  3129. }
  3130. static bool irte_ga_is_allocated(struct irq_remap_table *table, int index)
  3131. {
  3132. struct irte_ga *ptr = (struct irte_ga *)table->table;
  3133. struct irte_ga *irte = &ptr[index];
  3134. return irte->hi.fields.vector != 0;
  3135. }
  3136. static void irte_clear_allocated(struct irq_remap_table *table, int index)
  3137. {
  3138. table->table[index] = 0;
  3139. }
  3140. static void irte_ga_clear_allocated(struct irq_remap_table *table, int index)
  3141. {
  3142. struct irte_ga *ptr = (struct irte_ga *)table->table;
  3143. struct irte_ga *irte = &ptr[index];
  3144. memset(&irte->lo.val, 0, sizeof(u64));
  3145. memset(&irte->hi.val, 0, sizeof(u64));
  3146. }
  3147. static int get_devid(struct irq_alloc_info *info)
  3148. {
  3149. int devid = -1;
  3150. switch (info->type) {
  3151. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  3152. devid = get_ioapic_devid(info->ioapic_id);
  3153. break;
  3154. case X86_IRQ_ALLOC_TYPE_HPET:
  3155. devid = get_hpet_devid(info->hpet_id);
  3156. break;
  3157. case X86_IRQ_ALLOC_TYPE_MSI:
  3158. case X86_IRQ_ALLOC_TYPE_MSIX:
  3159. devid = get_device_id(&info->msi_dev->dev);
  3160. break;
  3161. default:
  3162. BUG_ON(1);
  3163. break;
  3164. }
  3165. return devid;
  3166. }
  3167. static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info)
  3168. {
  3169. struct amd_iommu *iommu;
  3170. int devid;
  3171. if (!info)
  3172. return NULL;
  3173. devid = get_devid(info);
  3174. if (devid >= 0) {
  3175. iommu = amd_iommu_rlookup_table[devid];
  3176. if (iommu)
  3177. return iommu->ir_domain;
  3178. }
  3179. return NULL;
  3180. }
  3181. static struct irq_domain *get_irq_domain(struct irq_alloc_info *info)
  3182. {
  3183. struct amd_iommu *iommu;
  3184. int devid;
  3185. if (!info)
  3186. return NULL;
  3187. switch (info->type) {
  3188. case X86_IRQ_ALLOC_TYPE_MSI:
  3189. case X86_IRQ_ALLOC_TYPE_MSIX:
  3190. devid = get_device_id(&info->msi_dev->dev);
  3191. if (devid < 0)
  3192. return NULL;
  3193. iommu = amd_iommu_rlookup_table[devid];
  3194. if (iommu)
  3195. return iommu->msi_domain;
  3196. break;
  3197. default:
  3198. break;
  3199. }
  3200. return NULL;
  3201. }
  3202. struct irq_remap_ops amd_iommu_irq_ops = {
  3203. .prepare = amd_iommu_prepare,
  3204. .enable = amd_iommu_enable,
  3205. .disable = amd_iommu_disable,
  3206. .reenable = amd_iommu_reenable,
  3207. .enable_faulting = amd_iommu_enable_faulting,
  3208. .get_ir_irq_domain = get_ir_irq_domain,
  3209. .get_irq_domain = get_irq_domain,
  3210. };
  3211. static void irq_remapping_prepare_irte(struct amd_ir_data *data,
  3212. struct irq_cfg *irq_cfg,
  3213. struct irq_alloc_info *info,
  3214. int devid, int index, int sub_handle)
  3215. {
  3216. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3217. struct msi_msg *msg = &data->msi_entry;
  3218. struct IO_APIC_route_entry *entry;
  3219. struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
  3220. if (!iommu)
  3221. return;
  3222. data->irq_2_irte.devid = devid;
  3223. data->irq_2_irte.index = index + sub_handle;
  3224. iommu->irte_ops->prepare(data->entry, apic->irq_delivery_mode,
  3225. apic->irq_dest_mode, irq_cfg->vector,
  3226. irq_cfg->dest_apicid, devid);
  3227. switch (info->type) {
  3228. case X86_IRQ_ALLOC_TYPE_IOAPIC:
  3229. /* Setup IOAPIC entry */
  3230. entry = info->ioapic_entry;
  3231. info->ioapic_entry = NULL;
  3232. memset(entry, 0, sizeof(*entry));
  3233. entry->vector = index;
  3234. entry->mask = 0;
  3235. entry->trigger = info->ioapic_trigger;
  3236. entry->polarity = info->ioapic_polarity;
  3237. /* Mask level triggered irqs. */
  3238. if (info->ioapic_trigger)
  3239. entry->mask = 1;
  3240. break;
  3241. case X86_IRQ_ALLOC_TYPE_HPET:
  3242. case X86_IRQ_ALLOC_TYPE_MSI:
  3243. case X86_IRQ_ALLOC_TYPE_MSIX:
  3244. msg->address_hi = MSI_ADDR_BASE_HI;
  3245. msg->address_lo = MSI_ADDR_BASE_LO;
  3246. msg->data = irte_info->index;
  3247. break;
  3248. default:
  3249. BUG_ON(1);
  3250. break;
  3251. }
  3252. }
  3253. struct amd_irte_ops irte_32_ops = {
  3254. .prepare = irte_prepare,
  3255. .activate = irte_activate,
  3256. .deactivate = irte_deactivate,
  3257. .set_affinity = irte_set_affinity,
  3258. .set_allocated = irte_set_allocated,
  3259. .is_allocated = irte_is_allocated,
  3260. .clear_allocated = irte_clear_allocated,
  3261. };
  3262. struct amd_irte_ops irte_128_ops = {
  3263. .prepare = irte_ga_prepare,
  3264. .activate = irte_ga_activate,
  3265. .deactivate = irte_ga_deactivate,
  3266. .set_affinity = irte_ga_set_affinity,
  3267. .set_allocated = irte_ga_set_allocated,
  3268. .is_allocated = irte_ga_is_allocated,
  3269. .clear_allocated = irte_ga_clear_allocated,
  3270. };
  3271. static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
  3272. unsigned int nr_irqs, void *arg)
  3273. {
  3274. struct irq_alloc_info *info = arg;
  3275. struct irq_data *irq_data;
  3276. struct amd_ir_data *data = NULL;
  3277. struct irq_cfg *cfg;
  3278. int i, ret, devid;
  3279. int index;
  3280. if (!info)
  3281. return -EINVAL;
  3282. if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI &&
  3283. info->type != X86_IRQ_ALLOC_TYPE_MSIX)
  3284. return -EINVAL;
  3285. /*
  3286. * With IRQ remapping enabled, don't need contiguous CPU vectors
  3287. * to support multiple MSI interrupts.
  3288. */
  3289. if (info->type == X86_IRQ_ALLOC_TYPE_MSI)
  3290. info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
  3291. devid = get_devid(info);
  3292. if (devid < 0)
  3293. return -EINVAL;
  3294. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  3295. if (ret < 0)
  3296. return ret;
  3297. if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) {
  3298. struct irq_remap_table *table;
  3299. struct amd_iommu *iommu;
  3300. table = alloc_irq_table(devid);
  3301. if (table) {
  3302. if (!table->min_index) {
  3303. /*
  3304. * Keep the first 32 indexes free for IOAPIC
  3305. * interrupts.
  3306. */
  3307. table->min_index = 32;
  3308. iommu = amd_iommu_rlookup_table[devid];
  3309. for (i = 0; i < 32; ++i)
  3310. iommu->irte_ops->set_allocated(table, i);
  3311. }
  3312. WARN_ON(table->min_index != 32);
  3313. index = info->ioapic_pin;
  3314. } else {
  3315. index = -ENOMEM;
  3316. }
  3317. } else {
  3318. bool align = (info->type == X86_IRQ_ALLOC_TYPE_MSI);
  3319. index = alloc_irq_index(devid, nr_irqs, align);
  3320. }
  3321. if (index < 0) {
  3322. pr_warn("Failed to allocate IRTE\n");
  3323. ret = index;
  3324. goto out_free_parent;
  3325. }
  3326. for (i = 0; i < nr_irqs; i++) {
  3327. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3328. cfg = irqd_cfg(irq_data);
  3329. if (!irq_data || !cfg) {
  3330. ret = -EINVAL;
  3331. goto out_free_data;
  3332. }
  3333. ret = -ENOMEM;
  3334. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3335. if (!data)
  3336. goto out_free_data;
  3337. if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
  3338. data->entry = kzalloc(sizeof(union irte), GFP_KERNEL);
  3339. else
  3340. data->entry = kzalloc(sizeof(struct irte_ga),
  3341. GFP_KERNEL);
  3342. if (!data->entry) {
  3343. kfree(data);
  3344. goto out_free_data;
  3345. }
  3346. irq_data->hwirq = (devid << 16) + i;
  3347. irq_data->chip_data = data;
  3348. irq_data->chip = &amd_ir_chip;
  3349. irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
  3350. irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
  3351. }
  3352. return 0;
  3353. out_free_data:
  3354. for (i--; i >= 0; i--) {
  3355. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3356. if (irq_data)
  3357. kfree(irq_data->chip_data);
  3358. }
  3359. for (i = 0; i < nr_irqs; i++)
  3360. free_irte(devid, index + i);
  3361. out_free_parent:
  3362. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  3363. return ret;
  3364. }
  3365. static void irq_remapping_free(struct irq_domain *domain, unsigned int virq,
  3366. unsigned int nr_irqs)
  3367. {
  3368. struct irq_2_irte *irte_info;
  3369. struct irq_data *irq_data;
  3370. struct amd_ir_data *data;
  3371. int i;
  3372. for (i = 0; i < nr_irqs; i++) {
  3373. irq_data = irq_domain_get_irq_data(domain, virq + i);
  3374. if (irq_data && irq_data->chip_data) {
  3375. data = irq_data->chip_data;
  3376. irte_info = &data->irq_2_irte;
  3377. free_irte(irte_info->devid, irte_info->index);
  3378. kfree(data->entry);
  3379. kfree(data);
  3380. }
  3381. }
  3382. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  3383. }
  3384. static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
  3385. struct amd_ir_data *ir_data,
  3386. struct irq_2_irte *irte_info,
  3387. struct irq_cfg *cfg);
  3388. static int irq_remapping_activate(struct irq_domain *domain,
  3389. struct irq_data *irq_data, bool reserve)
  3390. {
  3391. struct amd_ir_data *data = irq_data->chip_data;
  3392. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3393. struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
  3394. struct irq_cfg *cfg = irqd_cfg(irq_data);
  3395. if (!iommu)
  3396. return 0;
  3397. iommu->irte_ops->activate(data->entry, irte_info->devid,
  3398. irte_info->index);
  3399. amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg);
  3400. return 0;
  3401. }
  3402. static void irq_remapping_deactivate(struct irq_domain *domain,
  3403. struct irq_data *irq_data)
  3404. {
  3405. struct amd_ir_data *data = irq_data->chip_data;
  3406. struct irq_2_irte *irte_info = &data->irq_2_irte;
  3407. struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
  3408. if (iommu)
  3409. iommu->irte_ops->deactivate(data->entry, irte_info->devid,
  3410. irte_info->index);
  3411. }
  3412. static const struct irq_domain_ops amd_ir_domain_ops = {
  3413. .alloc = irq_remapping_alloc,
  3414. .free = irq_remapping_free,
  3415. .activate = irq_remapping_activate,
  3416. .deactivate = irq_remapping_deactivate,
  3417. };
  3418. static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
  3419. {
  3420. struct amd_iommu *iommu;
  3421. struct amd_iommu_pi_data *pi_data = vcpu_info;
  3422. struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data;
  3423. struct amd_ir_data *ir_data = data->chip_data;
  3424. struct irte_ga *irte = (struct irte_ga *) ir_data->entry;
  3425. struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
  3426. struct iommu_dev_data *dev_data = search_dev_data(irte_info->devid);
  3427. /* Note:
  3428. * This device has never been set up for guest mode.
  3429. * we should not modify the IRTE
  3430. */
  3431. if (!dev_data || !dev_data->use_vapic)
  3432. return 0;
  3433. pi_data->ir_data = ir_data;
  3434. /* Note:
  3435. * SVM tries to set up for VAPIC mode, but we are in
  3436. * legacy mode. So, we force legacy mode instead.
  3437. */
  3438. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) {
  3439. pr_debug("AMD-Vi: %s: Fall back to using intr legacy remap\n",
  3440. __func__);
  3441. pi_data->is_guest_mode = false;
  3442. }
  3443. iommu = amd_iommu_rlookup_table[irte_info->devid];
  3444. if (iommu == NULL)
  3445. return -EINVAL;
  3446. pi_data->prev_ga_tag = ir_data->cached_ga_tag;
  3447. if (pi_data->is_guest_mode) {
  3448. /* Setting */
  3449. irte->hi.fields.ga_root_ptr = (pi_data->base >> 12);
  3450. irte->hi.fields.vector = vcpu_pi_info->vector;
  3451. irte->lo.fields_vapic.ga_log_intr = 1;
  3452. irte->lo.fields_vapic.guest_mode = 1;
  3453. irte->lo.fields_vapic.ga_tag = pi_data->ga_tag;
  3454. ir_data->cached_ga_tag = pi_data->ga_tag;
  3455. } else {
  3456. /* Un-Setting */
  3457. struct irq_cfg *cfg = irqd_cfg(data);
  3458. irte->hi.val = 0;
  3459. irte->lo.val = 0;
  3460. irte->hi.fields.vector = cfg->vector;
  3461. irte->lo.fields_remap.guest_mode = 0;
  3462. irte->lo.fields_remap.destination =
  3463. APICID_TO_IRTE_DEST_LO(cfg->dest_apicid);
  3464. irte->hi.fields.destination =
  3465. APICID_TO_IRTE_DEST_HI(cfg->dest_apicid);
  3466. irte->lo.fields_remap.int_type = apic->irq_delivery_mode;
  3467. irte->lo.fields_remap.dm = apic->irq_dest_mode;
  3468. /*
  3469. * This communicates the ga_tag back to the caller
  3470. * so that it can do all the necessary clean up.
  3471. */
  3472. ir_data->cached_ga_tag = 0;
  3473. }
  3474. return modify_irte_ga(irte_info->devid, irte_info->index, irte, ir_data);
  3475. }
  3476. static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu,
  3477. struct amd_ir_data *ir_data,
  3478. struct irq_2_irte *irte_info,
  3479. struct irq_cfg *cfg)
  3480. {
  3481. /*
  3482. * Atomically updates the IRTE with the new destination, vector
  3483. * and flushes the interrupt entry cache.
  3484. */
  3485. iommu->irte_ops->set_affinity(ir_data->entry, irte_info->devid,
  3486. irte_info->index, cfg->vector,
  3487. cfg->dest_apicid);
  3488. }
  3489. static int amd_ir_set_affinity(struct irq_data *data,
  3490. const struct cpumask *mask, bool force)
  3491. {
  3492. struct amd_ir_data *ir_data = data->chip_data;
  3493. struct irq_2_irte *irte_info = &ir_data->irq_2_irte;
  3494. struct irq_cfg *cfg = irqd_cfg(data);
  3495. struct irq_data *parent = data->parent_data;
  3496. struct amd_iommu *iommu = amd_iommu_rlookup_table[irte_info->devid];
  3497. int ret;
  3498. if (!iommu)
  3499. return -ENODEV;
  3500. ret = parent->chip->irq_set_affinity(parent, mask, force);
  3501. if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
  3502. return ret;
  3503. amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg);
  3504. /*
  3505. * After this point, all the interrupts will start arriving
  3506. * at the new destination. So, time to cleanup the previous
  3507. * vector allocation.
  3508. */
  3509. send_cleanup_vector(cfg);
  3510. return IRQ_SET_MASK_OK_DONE;
  3511. }
  3512. static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
  3513. {
  3514. struct amd_ir_data *ir_data = irq_data->chip_data;
  3515. *msg = ir_data->msi_entry;
  3516. }
  3517. static struct irq_chip amd_ir_chip = {
  3518. .name = "AMD-IR",
  3519. .irq_ack = apic_ack_irq,
  3520. .irq_set_affinity = amd_ir_set_affinity,
  3521. .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity,
  3522. .irq_compose_msi_msg = ir_compose_msi_msg,
  3523. };
  3524. int amd_iommu_create_irq_domain(struct amd_iommu *iommu)
  3525. {
  3526. struct fwnode_handle *fn;
  3527. fn = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index);
  3528. if (!fn)
  3529. return -ENOMEM;
  3530. iommu->ir_domain = irq_domain_create_tree(fn, &amd_ir_domain_ops, iommu);
  3531. irq_domain_free_fwnode(fn);
  3532. if (!iommu->ir_domain)
  3533. return -ENOMEM;
  3534. iommu->ir_domain->parent = arch_get_ir_parent_domain();
  3535. iommu->msi_domain = arch_create_remap_msi_irq_domain(iommu->ir_domain,
  3536. "AMD-IR-MSI",
  3537. iommu->index);
  3538. return 0;
  3539. }
  3540. int amd_iommu_update_ga(int cpu, bool is_run, void *data)
  3541. {
  3542. unsigned long flags;
  3543. struct amd_iommu *iommu;
  3544. struct irq_remap_table *table;
  3545. struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
  3546. int devid = ir_data->irq_2_irte.devid;
  3547. struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
  3548. struct irte_ga *ref = (struct irte_ga *) ir_data->ref;
  3549. if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
  3550. !ref || !entry || !entry->lo.fields_vapic.guest_mode)
  3551. return 0;
  3552. iommu = amd_iommu_rlookup_table[devid];
  3553. if (!iommu)
  3554. return -ENODEV;
  3555. table = get_irq_table(devid);
  3556. if (!table)
  3557. return -ENODEV;
  3558. raw_spin_lock_irqsave(&table->lock, flags);
  3559. if (ref->lo.fields_vapic.guest_mode) {
  3560. if (cpu >= 0) {
  3561. ref->lo.fields_vapic.destination =
  3562. APICID_TO_IRTE_DEST_LO(cpu);
  3563. ref->hi.fields.destination =
  3564. APICID_TO_IRTE_DEST_HI(cpu);
  3565. }
  3566. ref->lo.fields_vapic.is_run = is_run;
  3567. barrier();
  3568. }
  3569. raw_spin_unlock_irqrestore(&table->lock, flags);
  3570. iommu_flush_irt(iommu, devid);
  3571. iommu_completion_wait(iommu);
  3572. return 0;
  3573. }
  3574. EXPORT_SYMBOL(amd_iommu_update_ga);
  3575. #endif