amd_iommu.c 94 KB

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