amd_iommu.c 97 KB

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