amd_iommu.c 102 KB

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