amd_iommu.c 101 KB

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