amd_iommu.c 97 KB

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