amd_iommu.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465
  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 <asm/msidef.h>
  34. #include <asm/proto.h>
  35. #include <asm/iommu.h>
  36. #include <asm/gart.h>
  37. #include <asm/dma.h>
  38. #include "amd_iommu_proto.h"
  39. #include "amd_iommu_types.h"
  40. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  41. #define LOOP_TIMEOUT 100000
  42. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  43. /* A list of preallocated protection domains */
  44. static LIST_HEAD(iommu_pd_list);
  45. static DEFINE_SPINLOCK(iommu_pd_list_lock);
  46. /* List of all available dev_data structures */
  47. static LIST_HEAD(dev_data_list);
  48. static DEFINE_SPINLOCK(dev_data_list_lock);
  49. /*
  50. * Domain for untranslated devices - only allocated
  51. * if iommu=pt passed on kernel cmd line.
  52. */
  53. static struct protection_domain *pt_domain;
  54. static struct iommu_ops amd_iommu_ops;
  55. static ATOMIC_NOTIFIER_HEAD(ppr_notifier);
  56. int amd_iommu_max_glx_val = -1;
  57. /*
  58. * general struct to manage commands send to an IOMMU
  59. */
  60. struct iommu_cmd {
  61. u32 data[4];
  62. };
  63. static void update_domain(struct protection_domain *domain);
  64. static int __init alloc_passthrough_domain(void);
  65. /****************************************************************************
  66. *
  67. * Helper functions
  68. *
  69. ****************************************************************************/
  70. static struct iommu_dev_data *alloc_dev_data(u16 devid)
  71. {
  72. struct iommu_dev_data *dev_data;
  73. unsigned long flags;
  74. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  75. if (!dev_data)
  76. return NULL;
  77. dev_data->devid = devid;
  78. atomic_set(&dev_data->bind, 0);
  79. spin_lock_irqsave(&dev_data_list_lock, flags);
  80. list_add_tail(&dev_data->dev_data_list, &dev_data_list);
  81. spin_unlock_irqrestore(&dev_data_list_lock, flags);
  82. return dev_data;
  83. }
  84. static void free_dev_data(struct iommu_dev_data *dev_data)
  85. {
  86. unsigned long flags;
  87. spin_lock_irqsave(&dev_data_list_lock, flags);
  88. list_del(&dev_data->dev_data_list);
  89. spin_unlock_irqrestore(&dev_data_list_lock, flags);
  90. kfree(dev_data);
  91. }
  92. static struct iommu_dev_data *search_dev_data(u16 devid)
  93. {
  94. struct iommu_dev_data *dev_data;
  95. unsigned long flags;
  96. spin_lock_irqsave(&dev_data_list_lock, flags);
  97. list_for_each_entry(dev_data, &dev_data_list, dev_data_list) {
  98. if (dev_data->devid == devid)
  99. goto out_unlock;
  100. }
  101. dev_data = NULL;
  102. out_unlock:
  103. spin_unlock_irqrestore(&dev_data_list_lock, flags);
  104. return dev_data;
  105. }
  106. static struct iommu_dev_data *find_dev_data(u16 devid)
  107. {
  108. struct iommu_dev_data *dev_data;
  109. dev_data = search_dev_data(devid);
  110. if (dev_data == NULL)
  111. dev_data = alloc_dev_data(devid);
  112. return dev_data;
  113. }
  114. static inline u16 get_device_id(struct device *dev)
  115. {
  116. struct pci_dev *pdev = to_pci_dev(dev);
  117. return calc_devid(pdev->bus->number, pdev->devfn);
  118. }
  119. static struct iommu_dev_data *get_dev_data(struct device *dev)
  120. {
  121. return dev->archdata.iommu;
  122. }
  123. static bool pci_iommuv2_capable(struct pci_dev *pdev)
  124. {
  125. static const int caps[] = {
  126. PCI_EXT_CAP_ID_ATS,
  127. PCI_PRI_CAP,
  128. PCI_PASID_CAP,
  129. };
  130. int i, pos;
  131. for (i = 0; i < 3; ++i) {
  132. pos = pci_find_ext_capability(pdev, caps[i]);
  133. if (pos == 0)
  134. return false;
  135. }
  136. return true;
  137. }
  138. /*
  139. * In this function the list of preallocated protection domains is traversed to
  140. * find the domain for a specific device
  141. */
  142. static struct dma_ops_domain *find_protection_domain(u16 devid)
  143. {
  144. struct dma_ops_domain *entry, *ret = NULL;
  145. unsigned long flags;
  146. u16 alias = amd_iommu_alias_table[devid];
  147. if (list_empty(&iommu_pd_list))
  148. return NULL;
  149. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  150. list_for_each_entry(entry, &iommu_pd_list, list) {
  151. if (entry->target_dev == devid ||
  152. entry->target_dev == alias) {
  153. ret = entry;
  154. break;
  155. }
  156. }
  157. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  158. return ret;
  159. }
  160. /*
  161. * This function checks if the driver got a valid device from the caller to
  162. * avoid dereferencing invalid pointers.
  163. */
  164. static bool check_device(struct device *dev)
  165. {
  166. u16 devid;
  167. if (!dev || !dev->dma_mask)
  168. return false;
  169. /* No device or no PCI device */
  170. if (dev->bus != &pci_bus_type)
  171. return false;
  172. devid = get_device_id(dev);
  173. /* Out of our scope? */
  174. if (devid > amd_iommu_last_bdf)
  175. return false;
  176. if (amd_iommu_rlookup_table[devid] == NULL)
  177. return false;
  178. return true;
  179. }
  180. static int iommu_init_device(struct device *dev)
  181. {
  182. struct pci_dev *pdev = to_pci_dev(dev);
  183. struct iommu_dev_data *dev_data;
  184. u16 alias;
  185. if (dev->archdata.iommu)
  186. return 0;
  187. dev_data = find_dev_data(get_device_id(dev));
  188. if (!dev_data)
  189. return -ENOMEM;
  190. alias = amd_iommu_alias_table[dev_data->devid];
  191. if (alias != dev_data->devid) {
  192. struct iommu_dev_data *alias_data;
  193. alias_data = find_dev_data(alias);
  194. if (alias_data == NULL) {
  195. pr_err("AMD-Vi: Warning: Unhandled device %s\n",
  196. dev_name(dev));
  197. free_dev_data(dev_data);
  198. return -ENOTSUPP;
  199. }
  200. dev_data->alias_data = alias_data;
  201. }
  202. if (pci_iommuv2_capable(pdev)) {
  203. struct amd_iommu *iommu;
  204. iommu = amd_iommu_rlookup_table[dev_data->devid];
  205. dev_data->iommu_v2 = iommu->is_iommu_v2;
  206. }
  207. dev->archdata.iommu = dev_data;
  208. return 0;
  209. }
  210. static void iommu_ignore_device(struct device *dev)
  211. {
  212. u16 devid, alias;
  213. devid = get_device_id(dev);
  214. alias = amd_iommu_alias_table[devid];
  215. memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry));
  216. memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry));
  217. amd_iommu_rlookup_table[devid] = NULL;
  218. amd_iommu_rlookup_table[alias] = NULL;
  219. }
  220. static void iommu_uninit_device(struct device *dev)
  221. {
  222. /*
  223. * Nothing to do here - we keep dev_data around for unplugged devices
  224. * and reuse it when the device is re-plugged - not doing so would
  225. * introduce a ton of races.
  226. */
  227. }
  228. void __init amd_iommu_uninit_devices(void)
  229. {
  230. struct iommu_dev_data *dev_data, *n;
  231. struct pci_dev *pdev = NULL;
  232. for_each_pci_dev(pdev) {
  233. if (!check_device(&pdev->dev))
  234. continue;
  235. iommu_uninit_device(&pdev->dev);
  236. }
  237. /* Free all of our dev_data structures */
  238. list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list)
  239. free_dev_data(dev_data);
  240. }
  241. int __init amd_iommu_init_devices(void)
  242. {
  243. struct pci_dev *pdev = NULL;
  244. int ret = 0;
  245. for_each_pci_dev(pdev) {
  246. if (!check_device(&pdev->dev))
  247. continue;
  248. ret = iommu_init_device(&pdev->dev);
  249. if (ret == -ENOTSUPP)
  250. iommu_ignore_device(&pdev->dev);
  251. else if (ret)
  252. goto out_free;
  253. }
  254. return 0;
  255. out_free:
  256. amd_iommu_uninit_devices();
  257. return ret;
  258. }
  259. #ifdef CONFIG_AMD_IOMMU_STATS
  260. /*
  261. * Initialization code for statistics collection
  262. */
  263. DECLARE_STATS_COUNTER(compl_wait);
  264. DECLARE_STATS_COUNTER(cnt_map_single);
  265. DECLARE_STATS_COUNTER(cnt_unmap_single);
  266. DECLARE_STATS_COUNTER(cnt_map_sg);
  267. DECLARE_STATS_COUNTER(cnt_unmap_sg);
  268. DECLARE_STATS_COUNTER(cnt_alloc_coherent);
  269. DECLARE_STATS_COUNTER(cnt_free_coherent);
  270. DECLARE_STATS_COUNTER(cross_page);
  271. DECLARE_STATS_COUNTER(domain_flush_single);
  272. DECLARE_STATS_COUNTER(domain_flush_all);
  273. DECLARE_STATS_COUNTER(alloced_io_mem);
  274. DECLARE_STATS_COUNTER(total_map_requests);
  275. static struct dentry *stats_dir;
  276. static struct dentry *de_fflush;
  277. static void amd_iommu_stats_add(struct __iommu_counter *cnt)
  278. {
  279. if (stats_dir == NULL)
  280. return;
  281. cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
  282. &cnt->value);
  283. }
  284. static void amd_iommu_stats_init(void)
  285. {
  286. stats_dir = debugfs_create_dir("amd-iommu", NULL);
  287. if (stats_dir == NULL)
  288. return;
  289. de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
  290. (u32 *)&amd_iommu_unmap_flush);
  291. amd_iommu_stats_add(&compl_wait);
  292. amd_iommu_stats_add(&cnt_map_single);
  293. amd_iommu_stats_add(&cnt_unmap_single);
  294. amd_iommu_stats_add(&cnt_map_sg);
  295. amd_iommu_stats_add(&cnt_unmap_sg);
  296. amd_iommu_stats_add(&cnt_alloc_coherent);
  297. amd_iommu_stats_add(&cnt_free_coherent);
  298. amd_iommu_stats_add(&cross_page);
  299. amd_iommu_stats_add(&domain_flush_single);
  300. amd_iommu_stats_add(&domain_flush_all);
  301. amd_iommu_stats_add(&alloced_io_mem);
  302. amd_iommu_stats_add(&total_map_requests);
  303. }
  304. #endif
  305. /****************************************************************************
  306. *
  307. * Interrupt handling functions
  308. *
  309. ****************************************************************************/
  310. static void dump_dte_entry(u16 devid)
  311. {
  312. int i;
  313. for (i = 0; i < 4; ++i)
  314. pr_err("AMD-Vi: DTE[%d]: %016llx\n", i,
  315. amd_iommu_dev_table[devid].data[i]);
  316. }
  317. static void dump_command(unsigned long phys_addr)
  318. {
  319. struct iommu_cmd *cmd = phys_to_virt(phys_addr);
  320. int i;
  321. for (i = 0; i < 4; ++i)
  322. pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]);
  323. }
  324. static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
  325. {
  326. u32 *event = __evt;
  327. int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  328. int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  329. int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
  330. int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  331. u64 address = (u64)(((u64)event[3]) << 32) | event[2];
  332. printk(KERN_ERR "AMD-Vi: Event logged [");
  333. switch (type) {
  334. case EVENT_TYPE_ILL_DEV:
  335. printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
  336. "address=0x%016llx flags=0x%04x]\n",
  337. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  338. address, flags);
  339. dump_dte_entry(devid);
  340. break;
  341. case EVENT_TYPE_IO_FAULT:
  342. printk("IO_PAGE_FAULT device=%02x:%02x.%x "
  343. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  344. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  345. domid, address, flags);
  346. break;
  347. case EVENT_TYPE_DEV_TAB_ERR:
  348. printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  349. "address=0x%016llx flags=0x%04x]\n",
  350. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  351. address, flags);
  352. break;
  353. case EVENT_TYPE_PAGE_TAB_ERR:
  354. printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  355. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  356. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  357. domid, address, flags);
  358. break;
  359. case EVENT_TYPE_ILL_CMD:
  360. printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  361. dump_command(address);
  362. break;
  363. case EVENT_TYPE_CMD_HARD_ERR:
  364. printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
  365. "flags=0x%04x]\n", address, flags);
  366. break;
  367. case EVENT_TYPE_IOTLB_INV_TO:
  368. printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
  369. "address=0x%016llx]\n",
  370. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  371. address);
  372. break;
  373. case EVENT_TYPE_INV_DEV_REQ:
  374. printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
  375. "address=0x%016llx flags=0x%04x]\n",
  376. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  377. address, flags);
  378. break;
  379. default:
  380. printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
  381. }
  382. }
  383. static void iommu_poll_events(struct amd_iommu *iommu)
  384. {
  385. u32 head, tail;
  386. unsigned long flags;
  387. spin_lock_irqsave(&iommu->lock, flags);
  388. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  389. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  390. while (head != tail) {
  391. iommu_print_event(iommu, iommu->evt_buf + head);
  392. head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
  393. }
  394. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  395. spin_unlock_irqrestore(&iommu->lock, flags);
  396. }
  397. static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u32 head)
  398. {
  399. struct amd_iommu_fault fault;
  400. volatile u64 *raw;
  401. int i;
  402. raw = (u64 *)(iommu->ppr_log + head);
  403. /*
  404. * Hardware bug: Interrupt may arrive before the entry is written to
  405. * memory. If this happens we need to wait for the entry to arrive.
  406. */
  407. for (i = 0; i < LOOP_TIMEOUT; ++i) {
  408. if (PPR_REQ_TYPE(raw[0]) != 0)
  409. break;
  410. udelay(1);
  411. }
  412. if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) {
  413. pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n");
  414. return;
  415. }
  416. fault.address = raw[1];
  417. fault.pasid = PPR_PASID(raw[0]);
  418. fault.device_id = PPR_DEVID(raw[0]);
  419. fault.tag = PPR_TAG(raw[0]);
  420. fault.flags = PPR_FLAGS(raw[0]);
  421. /*
  422. * To detect the hardware bug we need to clear the entry
  423. * to back to zero.
  424. */
  425. raw[0] = raw[1] = 0;
  426. atomic_notifier_call_chain(&ppr_notifier, 0, &fault);
  427. }
  428. static void iommu_poll_ppr_log(struct amd_iommu *iommu)
  429. {
  430. unsigned long flags;
  431. u32 head, tail;
  432. if (iommu->ppr_log == NULL)
  433. return;
  434. spin_lock_irqsave(&iommu->lock, flags);
  435. head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  436. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  437. while (head != tail) {
  438. /* Handle PPR entry */
  439. iommu_handle_ppr_entry(iommu, head);
  440. /* Update and refresh ring-buffer state*/
  441. head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE;
  442. writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
  443. tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
  444. }
  445. /* enable ppr interrupts again */
  446. writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET);
  447. spin_unlock_irqrestore(&iommu->lock, flags);
  448. }
  449. irqreturn_t amd_iommu_int_thread(int irq, void *data)
  450. {
  451. struct amd_iommu *iommu;
  452. for_each_iommu(iommu) {
  453. iommu_poll_events(iommu);
  454. iommu_poll_ppr_log(iommu);
  455. }
  456. return IRQ_HANDLED;
  457. }
  458. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  459. {
  460. return IRQ_WAKE_THREAD;
  461. }
  462. /****************************************************************************
  463. *
  464. * IOMMU command queuing functions
  465. *
  466. ****************************************************************************/
  467. static int wait_on_sem(volatile u64 *sem)
  468. {
  469. int i = 0;
  470. while (*sem == 0 && i < LOOP_TIMEOUT) {
  471. udelay(1);
  472. i += 1;
  473. }
  474. if (i == LOOP_TIMEOUT) {
  475. pr_alert("AMD-Vi: Completion-Wait loop timed out\n");
  476. return -EIO;
  477. }
  478. return 0;
  479. }
  480. static void copy_cmd_to_buffer(struct amd_iommu *iommu,
  481. struct iommu_cmd *cmd,
  482. u32 tail)
  483. {
  484. u8 *target;
  485. target = iommu->cmd_buf + tail;
  486. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  487. /* Copy command to buffer */
  488. memcpy(target, cmd, sizeof(*cmd));
  489. /* Tell the IOMMU about it */
  490. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  491. }
  492. static void build_completion_wait(struct iommu_cmd *cmd, u64 address)
  493. {
  494. WARN_ON(address & 0x7ULL);
  495. memset(cmd, 0, sizeof(*cmd));
  496. cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK;
  497. cmd->data[1] = upper_32_bits(__pa(address));
  498. cmd->data[2] = 1;
  499. CMD_SET_TYPE(cmd, CMD_COMPL_WAIT);
  500. }
  501. static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)
  502. {
  503. memset(cmd, 0, sizeof(*cmd));
  504. cmd->data[0] = devid;
  505. CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY);
  506. }
  507. static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  508. size_t size, u16 domid, int pde)
  509. {
  510. u64 pages;
  511. int s;
  512. pages = iommu_num_pages(address, size, PAGE_SIZE);
  513. s = 0;
  514. if (pages > 1) {
  515. /*
  516. * If we have to flush more than one page, flush all
  517. * TLB entries for this domain
  518. */
  519. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  520. s = 1;
  521. }
  522. address &= PAGE_MASK;
  523. memset(cmd, 0, sizeof(*cmd));
  524. cmd->data[1] |= domid;
  525. cmd->data[2] = lower_32_bits(address);
  526. cmd->data[3] = upper_32_bits(address);
  527. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  528. if (s) /* size bit - we flush more than one 4kb page */
  529. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  530. if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
  531. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  532. }
  533. static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep,
  534. u64 address, size_t size)
  535. {
  536. u64 pages;
  537. int s;
  538. pages = iommu_num_pages(address, size, PAGE_SIZE);
  539. s = 0;
  540. if (pages > 1) {
  541. /*
  542. * If we have to flush more than one page, flush all
  543. * TLB entries for this domain
  544. */
  545. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  546. s = 1;
  547. }
  548. address &= PAGE_MASK;
  549. memset(cmd, 0, sizeof(*cmd));
  550. cmd->data[0] = devid;
  551. cmd->data[0] |= (qdep & 0xff) << 24;
  552. cmd->data[1] = devid;
  553. cmd->data[2] = lower_32_bits(address);
  554. cmd->data[3] = upper_32_bits(address);
  555. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  556. if (s)
  557. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  558. }
  559. static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid,
  560. u64 address, bool size)
  561. {
  562. memset(cmd, 0, sizeof(*cmd));
  563. address &= ~(0xfffULL);
  564. cmd->data[0] = pasid & PASID_MASK;
  565. cmd->data[1] = domid;
  566. cmd->data[2] = lower_32_bits(address);
  567. cmd->data[3] = upper_32_bits(address);
  568. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  569. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  570. if (size)
  571. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  572. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  573. }
  574. static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid,
  575. int qdep, u64 address, bool size)
  576. {
  577. memset(cmd, 0, sizeof(*cmd));
  578. address &= ~(0xfffULL);
  579. cmd->data[0] = devid;
  580. cmd->data[0] |= (pasid & 0xff) << 16;
  581. cmd->data[0] |= (qdep & 0xff) << 24;
  582. cmd->data[1] = devid;
  583. cmd->data[1] |= ((pasid >> 8) & 0xfff) << 16;
  584. cmd->data[2] = lower_32_bits(address);
  585. cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK;
  586. cmd->data[3] = upper_32_bits(address);
  587. if (size)
  588. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  589. CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES);
  590. }
  591. static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid,
  592. int status, int tag, bool gn)
  593. {
  594. memset(cmd, 0, sizeof(*cmd));
  595. cmd->data[0] = devid;
  596. if (gn) {
  597. cmd->data[1] = pasid & PASID_MASK;
  598. cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK;
  599. }
  600. cmd->data[3] = tag & 0x1ff;
  601. cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT;
  602. CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR);
  603. }
  604. static void build_inv_all(struct iommu_cmd *cmd)
  605. {
  606. memset(cmd, 0, sizeof(*cmd));
  607. CMD_SET_TYPE(cmd, CMD_INV_ALL);
  608. }
  609. /*
  610. * Writes the command to the IOMMUs command buffer and informs the
  611. * hardware about the new command.
  612. */
  613. static int iommu_queue_command_sync(struct amd_iommu *iommu,
  614. struct iommu_cmd *cmd,
  615. bool sync)
  616. {
  617. u32 left, tail, head, next_tail;
  618. unsigned long flags;
  619. WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
  620. again:
  621. spin_lock_irqsave(&iommu->lock, flags);
  622. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  623. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  624. next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  625. left = (head - next_tail) % iommu->cmd_buf_size;
  626. if (left <= 2) {
  627. struct iommu_cmd sync_cmd;
  628. volatile u64 sem = 0;
  629. int ret;
  630. build_completion_wait(&sync_cmd, (u64)&sem);
  631. copy_cmd_to_buffer(iommu, &sync_cmd, tail);
  632. spin_unlock_irqrestore(&iommu->lock, flags);
  633. if ((ret = wait_on_sem(&sem)) != 0)
  634. return ret;
  635. goto again;
  636. }
  637. copy_cmd_to_buffer(iommu, cmd, tail);
  638. /* We need to sync now to make sure all commands are processed */
  639. iommu->need_sync = sync;
  640. spin_unlock_irqrestore(&iommu->lock, flags);
  641. return 0;
  642. }
  643. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  644. {
  645. return iommu_queue_command_sync(iommu, cmd, true);
  646. }
  647. /*
  648. * This function queues a completion wait command into the command
  649. * buffer of an IOMMU
  650. */
  651. static int iommu_completion_wait(struct amd_iommu *iommu)
  652. {
  653. struct iommu_cmd cmd;
  654. volatile u64 sem = 0;
  655. int ret;
  656. if (!iommu->need_sync)
  657. return 0;
  658. build_completion_wait(&cmd, (u64)&sem);
  659. ret = iommu_queue_command_sync(iommu, &cmd, false);
  660. if (ret)
  661. return ret;
  662. return wait_on_sem(&sem);
  663. }
  664. static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
  665. {
  666. struct iommu_cmd cmd;
  667. build_inv_dte(&cmd, devid);
  668. return iommu_queue_command(iommu, &cmd);
  669. }
  670. static void iommu_flush_dte_all(struct amd_iommu *iommu)
  671. {
  672. u32 devid;
  673. for (devid = 0; devid <= 0xffff; ++devid)
  674. iommu_flush_dte(iommu, devid);
  675. iommu_completion_wait(iommu);
  676. }
  677. /*
  678. * This function uses heavy locking and may disable irqs for some time. But
  679. * this is no issue because it is only called during resume.
  680. */
  681. static void iommu_flush_tlb_all(struct amd_iommu *iommu)
  682. {
  683. u32 dom_id;
  684. for (dom_id = 0; dom_id <= 0xffff; ++dom_id) {
  685. struct iommu_cmd cmd;
  686. build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  687. dom_id, 1);
  688. iommu_queue_command(iommu, &cmd);
  689. }
  690. iommu_completion_wait(iommu);
  691. }
  692. static void iommu_flush_all(struct amd_iommu *iommu)
  693. {
  694. struct iommu_cmd cmd;
  695. build_inv_all(&cmd);
  696. iommu_queue_command(iommu, &cmd);
  697. iommu_completion_wait(iommu);
  698. }
  699. void iommu_flush_all_caches(struct amd_iommu *iommu)
  700. {
  701. if (iommu_feature(iommu, FEATURE_IA)) {
  702. iommu_flush_all(iommu);
  703. } else {
  704. iommu_flush_dte_all(iommu);
  705. iommu_flush_tlb_all(iommu);
  706. }
  707. }
  708. /*
  709. * Command send function for flushing on-device TLB
  710. */
  711. static int device_flush_iotlb(struct iommu_dev_data *dev_data,
  712. u64 address, size_t size)
  713. {
  714. struct amd_iommu *iommu;
  715. struct iommu_cmd cmd;
  716. int qdep;
  717. qdep = dev_data->ats.qdep;
  718. iommu = amd_iommu_rlookup_table[dev_data->devid];
  719. build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size);
  720. return iommu_queue_command(iommu, &cmd);
  721. }
  722. /*
  723. * Command send function for invalidating a device table entry
  724. */
  725. static int device_flush_dte(struct iommu_dev_data *dev_data)
  726. {
  727. struct amd_iommu *iommu;
  728. int ret;
  729. iommu = amd_iommu_rlookup_table[dev_data->devid];
  730. ret = iommu_flush_dte(iommu, dev_data->devid);
  731. if (ret)
  732. return ret;
  733. if (dev_data->ats.enabled)
  734. ret = device_flush_iotlb(dev_data, 0, ~0UL);
  735. return ret;
  736. }
  737. /*
  738. * TLB invalidation function which is called from the mapping functions.
  739. * It invalidates a single PTE if the range to flush is within a single
  740. * page. Otherwise it flushes the whole TLB of the IOMMU.
  741. */
  742. static void __domain_flush_pages(struct protection_domain *domain,
  743. u64 address, size_t size, int pde)
  744. {
  745. struct iommu_dev_data *dev_data;
  746. struct iommu_cmd cmd;
  747. int ret = 0, i;
  748. build_inv_iommu_pages(&cmd, address, size, domain->id, pde);
  749. for (i = 0; i < amd_iommus_present; ++i) {
  750. if (!domain->dev_iommu[i])
  751. continue;
  752. /*
  753. * Devices of this domain are behind this IOMMU
  754. * We need a TLB flush
  755. */
  756. ret |= iommu_queue_command(amd_iommus[i], &cmd);
  757. }
  758. list_for_each_entry(dev_data, &domain->dev_list, list) {
  759. if (!dev_data->ats.enabled)
  760. continue;
  761. ret |= device_flush_iotlb(dev_data, address, size);
  762. }
  763. WARN_ON(ret);
  764. }
  765. static void domain_flush_pages(struct protection_domain *domain,
  766. u64 address, size_t size)
  767. {
  768. __domain_flush_pages(domain, address, size, 0);
  769. }
  770. /* Flush the whole IO/TLB for a given protection domain */
  771. static void domain_flush_tlb(struct protection_domain *domain)
  772. {
  773. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
  774. }
  775. /* Flush the whole IO/TLB for a given protection domain - including PDE */
  776. static void domain_flush_tlb_pde(struct protection_domain *domain)
  777. {
  778. __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
  779. }
  780. static void domain_flush_complete(struct protection_domain *domain)
  781. {
  782. int i;
  783. for (i = 0; i < amd_iommus_present; ++i) {
  784. if (!domain->dev_iommu[i])
  785. continue;
  786. /*
  787. * Devices of this domain are behind this IOMMU
  788. * We need to wait for completion of all commands.
  789. */
  790. iommu_completion_wait(amd_iommus[i]);
  791. }
  792. }
  793. /*
  794. * This function flushes the DTEs for all devices in domain
  795. */
  796. static void domain_flush_devices(struct protection_domain *domain)
  797. {
  798. struct iommu_dev_data *dev_data;
  799. list_for_each_entry(dev_data, &domain->dev_list, list)
  800. device_flush_dte(dev_data);
  801. }
  802. /****************************************************************************
  803. *
  804. * The functions below are used the create the page table mappings for
  805. * unity mapped regions.
  806. *
  807. ****************************************************************************/
  808. /*
  809. * This function is used to add another level to an IO page table. Adding
  810. * another level increases the size of the address space by 9 bits to a size up
  811. * to 64 bits.
  812. */
  813. static bool increase_address_space(struct protection_domain *domain,
  814. gfp_t gfp)
  815. {
  816. u64 *pte;
  817. if (domain->mode == PAGE_MODE_6_LEVEL)
  818. /* address space already 64 bit large */
  819. return false;
  820. pte = (void *)get_zeroed_page(gfp);
  821. if (!pte)
  822. return false;
  823. *pte = PM_LEVEL_PDE(domain->mode,
  824. virt_to_phys(domain->pt_root));
  825. domain->pt_root = pte;
  826. domain->mode += 1;
  827. domain->updated = true;
  828. return true;
  829. }
  830. static u64 *alloc_pte(struct protection_domain *domain,
  831. unsigned long address,
  832. unsigned long page_size,
  833. u64 **pte_page,
  834. gfp_t gfp)
  835. {
  836. int level, end_lvl;
  837. u64 *pte, *page;
  838. BUG_ON(!is_power_of_2(page_size));
  839. while (address > PM_LEVEL_SIZE(domain->mode))
  840. increase_address_space(domain, gfp);
  841. level = domain->mode - 1;
  842. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  843. address = PAGE_SIZE_ALIGN(address, page_size);
  844. end_lvl = PAGE_SIZE_LEVEL(page_size);
  845. while (level > end_lvl) {
  846. if (!IOMMU_PTE_PRESENT(*pte)) {
  847. page = (u64 *)get_zeroed_page(gfp);
  848. if (!page)
  849. return NULL;
  850. *pte = PM_LEVEL_PDE(level, virt_to_phys(page));
  851. }
  852. /* No level skipping support yet */
  853. if (PM_PTE_LEVEL(*pte) != level)
  854. return NULL;
  855. level -= 1;
  856. pte = IOMMU_PTE_PAGE(*pte);
  857. if (pte_page && level == end_lvl)
  858. *pte_page = pte;
  859. pte = &pte[PM_LEVEL_INDEX(level, address)];
  860. }
  861. return pte;
  862. }
  863. /*
  864. * This function checks if there is a PTE for a given dma address. If
  865. * there is one, it returns the pointer to it.
  866. */
  867. static u64 *fetch_pte(struct protection_domain *domain, unsigned long address)
  868. {
  869. int level;
  870. u64 *pte;
  871. if (address > PM_LEVEL_SIZE(domain->mode))
  872. return NULL;
  873. level = domain->mode - 1;
  874. pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)];
  875. while (level > 0) {
  876. /* Not Present */
  877. if (!IOMMU_PTE_PRESENT(*pte))
  878. return NULL;
  879. /* Large PTE */
  880. if (PM_PTE_LEVEL(*pte) == 0x07) {
  881. unsigned long pte_mask, __pte;
  882. /*
  883. * If we have a series of large PTEs, make
  884. * sure to return a pointer to the first one.
  885. */
  886. pte_mask = PTE_PAGE_SIZE(*pte);
  887. pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1);
  888. __pte = ((unsigned long)pte) & pte_mask;
  889. return (u64 *)__pte;
  890. }
  891. /* No level skipping support yet */
  892. if (PM_PTE_LEVEL(*pte) != level)
  893. return NULL;
  894. level -= 1;
  895. /* Walk to the next level */
  896. pte = IOMMU_PTE_PAGE(*pte);
  897. pte = &pte[PM_LEVEL_INDEX(level, address)];
  898. }
  899. return pte;
  900. }
  901. /*
  902. * Generic mapping functions. It maps a physical address into a DMA
  903. * address space. It allocates the page table pages if necessary.
  904. * In the future it can be extended to a generic mapping function
  905. * supporting all features of AMD IOMMU page tables like level skipping
  906. * and full 64 bit address spaces.
  907. */
  908. static int iommu_map_page(struct protection_domain *dom,
  909. unsigned long bus_addr,
  910. unsigned long phys_addr,
  911. int prot,
  912. unsigned long page_size)
  913. {
  914. u64 __pte, *pte;
  915. int i, count;
  916. if (!(prot & IOMMU_PROT_MASK))
  917. return -EINVAL;
  918. bus_addr = PAGE_ALIGN(bus_addr);
  919. phys_addr = PAGE_ALIGN(phys_addr);
  920. count = PAGE_SIZE_PTE_COUNT(page_size);
  921. pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL);
  922. for (i = 0; i < count; ++i)
  923. if (IOMMU_PTE_PRESENT(pte[i]))
  924. return -EBUSY;
  925. if (page_size > PAGE_SIZE) {
  926. __pte = PAGE_SIZE_PTE(phys_addr, page_size);
  927. __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
  928. } else
  929. __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
  930. if (prot & IOMMU_PROT_IR)
  931. __pte |= IOMMU_PTE_IR;
  932. if (prot & IOMMU_PROT_IW)
  933. __pte |= IOMMU_PTE_IW;
  934. for (i = 0; i < count; ++i)
  935. pte[i] = __pte;
  936. update_domain(dom);
  937. return 0;
  938. }
  939. static unsigned long iommu_unmap_page(struct protection_domain *dom,
  940. unsigned long bus_addr,
  941. unsigned long page_size)
  942. {
  943. unsigned long long unmap_size, unmapped;
  944. u64 *pte;
  945. BUG_ON(!is_power_of_2(page_size));
  946. unmapped = 0;
  947. while (unmapped < page_size) {
  948. pte = fetch_pte(dom, bus_addr);
  949. if (!pte) {
  950. /*
  951. * No PTE for this address
  952. * move forward in 4kb steps
  953. */
  954. unmap_size = PAGE_SIZE;
  955. } else if (PM_PTE_LEVEL(*pte) == 0) {
  956. /* 4kb PTE found for this address */
  957. unmap_size = PAGE_SIZE;
  958. *pte = 0ULL;
  959. } else {
  960. int count, i;
  961. /* Large PTE found which maps this address */
  962. unmap_size = PTE_PAGE_SIZE(*pte);
  963. count = PAGE_SIZE_PTE_COUNT(unmap_size);
  964. for (i = 0; i < count; i++)
  965. pte[i] = 0ULL;
  966. }
  967. bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size;
  968. unmapped += unmap_size;
  969. }
  970. BUG_ON(!is_power_of_2(unmapped));
  971. return unmapped;
  972. }
  973. /*
  974. * This function checks if a specific unity mapping entry is needed for
  975. * this specific IOMMU.
  976. */
  977. static int iommu_for_unity_map(struct amd_iommu *iommu,
  978. struct unity_map_entry *entry)
  979. {
  980. u16 bdf, i;
  981. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  982. bdf = amd_iommu_alias_table[i];
  983. if (amd_iommu_rlookup_table[bdf] == iommu)
  984. return 1;
  985. }
  986. return 0;
  987. }
  988. /*
  989. * This function actually applies the mapping to the page table of the
  990. * dma_ops domain.
  991. */
  992. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  993. struct unity_map_entry *e)
  994. {
  995. u64 addr;
  996. int ret;
  997. for (addr = e->address_start; addr < e->address_end;
  998. addr += PAGE_SIZE) {
  999. ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot,
  1000. PAGE_SIZE);
  1001. if (ret)
  1002. return ret;
  1003. /*
  1004. * if unity mapping is in aperture range mark the page
  1005. * as allocated in the aperture
  1006. */
  1007. if (addr < dma_dom->aperture_size)
  1008. __set_bit(addr >> PAGE_SHIFT,
  1009. dma_dom->aperture[0]->bitmap);
  1010. }
  1011. return 0;
  1012. }
  1013. /*
  1014. * Init the unity mappings for a specific IOMMU in the system
  1015. *
  1016. * Basically iterates over all unity mapping entries and applies them to
  1017. * the default domain DMA of that IOMMU if necessary.
  1018. */
  1019. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  1020. {
  1021. struct unity_map_entry *entry;
  1022. int ret;
  1023. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  1024. if (!iommu_for_unity_map(iommu, entry))
  1025. continue;
  1026. ret = dma_ops_unity_map(iommu->default_dom, entry);
  1027. if (ret)
  1028. return ret;
  1029. }
  1030. return 0;
  1031. }
  1032. /*
  1033. * Inits the unity mappings required for a specific device
  1034. */
  1035. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  1036. u16 devid)
  1037. {
  1038. struct unity_map_entry *e;
  1039. int ret;
  1040. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  1041. if (!(devid >= e->devid_start && devid <= e->devid_end))
  1042. continue;
  1043. ret = dma_ops_unity_map(dma_dom, e);
  1044. if (ret)
  1045. return ret;
  1046. }
  1047. return 0;
  1048. }
  1049. /****************************************************************************
  1050. *
  1051. * The next functions belong to the address allocator for the dma_ops
  1052. * interface functions. They work like the allocators in the other IOMMU
  1053. * drivers. Its basically a bitmap which marks the allocated pages in
  1054. * the aperture. Maybe it could be enhanced in the future to a more
  1055. * efficient allocator.
  1056. *
  1057. ****************************************************************************/
  1058. /*
  1059. * The address allocator core functions.
  1060. *
  1061. * called with domain->lock held
  1062. */
  1063. /*
  1064. * Used to reserve address ranges in the aperture (e.g. for exclusion
  1065. * ranges.
  1066. */
  1067. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  1068. unsigned long start_page,
  1069. unsigned int pages)
  1070. {
  1071. unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT;
  1072. if (start_page + pages > last_page)
  1073. pages = last_page - start_page;
  1074. for (i = start_page; i < start_page + pages; ++i) {
  1075. int index = i / APERTURE_RANGE_PAGES;
  1076. int page = i % APERTURE_RANGE_PAGES;
  1077. __set_bit(page, dom->aperture[index]->bitmap);
  1078. }
  1079. }
  1080. /*
  1081. * This function is used to add a new aperture range to an existing
  1082. * aperture in case of dma_ops domain allocation or address allocation
  1083. * failure.
  1084. */
  1085. static int alloc_new_range(struct dma_ops_domain *dma_dom,
  1086. bool populate, gfp_t gfp)
  1087. {
  1088. int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
  1089. struct amd_iommu *iommu;
  1090. unsigned long i, old_size;
  1091. #ifdef CONFIG_IOMMU_STRESS
  1092. populate = false;
  1093. #endif
  1094. if (index >= APERTURE_MAX_RANGES)
  1095. return -ENOMEM;
  1096. dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp);
  1097. if (!dma_dom->aperture[index])
  1098. return -ENOMEM;
  1099. dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp);
  1100. if (!dma_dom->aperture[index]->bitmap)
  1101. goto out_free;
  1102. dma_dom->aperture[index]->offset = dma_dom->aperture_size;
  1103. if (populate) {
  1104. unsigned long address = dma_dom->aperture_size;
  1105. int i, num_ptes = APERTURE_RANGE_PAGES / 512;
  1106. u64 *pte, *pte_page;
  1107. for (i = 0; i < num_ptes; ++i) {
  1108. pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE,
  1109. &pte_page, gfp);
  1110. if (!pte)
  1111. goto out_free;
  1112. dma_dom->aperture[index]->pte_pages[i] = pte_page;
  1113. address += APERTURE_RANGE_SIZE / 64;
  1114. }
  1115. }
  1116. old_size = dma_dom->aperture_size;
  1117. dma_dom->aperture_size += APERTURE_RANGE_SIZE;
  1118. /* Reserve address range used for MSI messages */
  1119. if (old_size < MSI_ADDR_BASE_LO &&
  1120. dma_dom->aperture_size > MSI_ADDR_BASE_LO) {
  1121. unsigned long spage;
  1122. int pages;
  1123. pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE);
  1124. spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT;
  1125. dma_ops_reserve_addresses(dma_dom, spage, pages);
  1126. }
  1127. /* Initialize the exclusion range if necessary */
  1128. for_each_iommu(iommu) {
  1129. if (iommu->exclusion_start &&
  1130. iommu->exclusion_start >= dma_dom->aperture[index]->offset
  1131. && iommu->exclusion_start < dma_dom->aperture_size) {
  1132. unsigned long startpage;
  1133. int pages = iommu_num_pages(iommu->exclusion_start,
  1134. iommu->exclusion_length,
  1135. PAGE_SIZE);
  1136. startpage = iommu->exclusion_start >> PAGE_SHIFT;
  1137. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  1138. }
  1139. }
  1140. /*
  1141. * Check for areas already mapped as present in the new aperture
  1142. * range and mark those pages as reserved in the allocator. Such
  1143. * mappings may already exist as a result of requested unity
  1144. * mappings for devices.
  1145. */
  1146. for (i = dma_dom->aperture[index]->offset;
  1147. i < dma_dom->aperture_size;
  1148. i += PAGE_SIZE) {
  1149. u64 *pte = fetch_pte(&dma_dom->domain, i);
  1150. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  1151. continue;
  1152. dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1);
  1153. }
  1154. update_domain(&dma_dom->domain);
  1155. return 0;
  1156. out_free:
  1157. update_domain(&dma_dom->domain);
  1158. free_page((unsigned long)dma_dom->aperture[index]->bitmap);
  1159. kfree(dma_dom->aperture[index]);
  1160. dma_dom->aperture[index] = NULL;
  1161. return -ENOMEM;
  1162. }
  1163. static unsigned long dma_ops_area_alloc(struct device *dev,
  1164. struct dma_ops_domain *dom,
  1165. unsigned int pages,
  1166. unsigned long align_mask,
  1167. u64 dma_mask,
  1168. unsigned long start)
  1169. {
  1170. unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE;
  1171. int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT;
  1172. int i = start >> APERTURE_RANGE_SHIFT;
  1173. unsigned long boundary_size;
  1174. unsigned long address = -1;
  1175. unsigned long limit;
  1176. next_bit >>= PAGE_SHIFT;
  1177. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  1178. PAGE_SIZE) >> PAGE_SHIFT;
  1179. for (;i < max_index; ++i) {
  1180. unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT;
  1181. if (dom->aperture[i]->offset >= dma_mask)
  1182. break;
  1183. limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset,
  1184. dma_mask >> PAGE_SHIFT);
  1185. address = iommu_area_alloc(dom->aperture[i]->bitmap,
  1186. limit, next_bit, pages, 0,
  1187. boundary_size, align_mask);
  1188. if (address != -1) {
  1189. address = dom->aperture[i]->offset +
  1190. (address << PAGE_SHIFT);
  1191. dom->next_address = address + (pages << PAGE_SHIFT);
  1192. break;
  1193. }
  1194. next_bit = 0;
  1195. }
  1196. return address;
  1197. }
  1198. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  1199. struct dma_ops_domain *dom,
  1200. unsigned int pages,
  1201. unsigned long align_mask,
  1202. u64 dma_mask)
  1203. {
  1204. unsigned long address;
  1205. #ifdef CONFIG_IOMMU_STRESS
  1206. dom->next_address = 0;
  1207. dom->need_flush = true;
  1208. #endif
  1209. address = dma_ops_area_alloc(dev, dom, pages, align_mask,
  1210. dma_mask, dom->next_address);
  1211. if (address == -1) {
  1212. dom->next_address = 0;
  1213. address = dma_ops_area_alloc(dev, dom, pages, align_mask,
  1214. dma_mask, 0);
  1215. dom->need_flush = true;
  1216. }
  1217. if (unlikely(address == -1))
  1218. address = DMA_ERROR_CODE;
  1219. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  1220. return address;
  1221. }
  1222. /*
  1223. * The address free function.
  1224. *
  1225. * called with domain->lock held
  1226. */
  1227. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  1228. unsigned long address,
  1229. unsigned int pages)
  1230. {
  1231. unsigned i = address >> APERTURE_RANGE_SHIFT;
  1232. struct aperture_range *range = dom->aperture[i];
  1233. BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL);
  1234. #ifdef CONFIG_IOMMU_STRESS
  1235. if (i < 4)
  1236. return;
  1237. #endif
  1238. if (address >= dom->next_address)
  1239. dom->need_flush = true;
  1240. address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT;
  1241. bitmap_clear(range->bitmap, address, pages);
  1242. }
  1243. /****************************************************************************
  1244. *
  1245. * The next functions belong to the domain allocation. A domain is
  1246. * allocated for every IOMMU as the default domain. If device isolation
  1247. * is enabled, every device get its own domain. The most important thing
  1248. * about domains is the page table mapping the DMA address space they
  1249. * contain.
  1250. *
  1251. ****************************************************************************/
  1252. /*
  1253. * This function adds a protection domain to the global protection domain list
  1254. */
  1255. static void add_domain_to_list(struct protection_domain *domain)
  1256. {
  1257. unsigned long flags;
  1258. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1259. list_add(&domain->list, &amd_iommu_pd_list);
  1260. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1261. }
  1262. /*
  1263. * This function removes a protection domain to the global
  1264. * protection domain list
  1265. */
  1266. static void del_domain_from_list(struct protection_domain *domain)
  1267. {
  1268. unsigned long flags;
  1269. spin_lock_irqsave(&amd_iommu_pd_lock, flags);
  1270. list_del(&domain->list);
  1271. spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
  1272. }
  1273. static u16 domain_id_alloc(void)
  1274. {
  1275. unsigned long flags;
  1276. int id;
  1277. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1278. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  1279. BUG_ON(id == 0);
  1280. if (id > 0 && id < MAX_DOMAIN_ID)
  1281. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  1282. else
  1283. id = 0;
  1284. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1285. return id;
  1286. }
  1287. static void domain_id_free(int id)
  1288. {
  1289. unsigned long flags;
  1290. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1291. if (id > 0 && id < MAX_DOMAIN_ID)
  1292. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  1293. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1294. }
  1295. static void free_pagetable(struct protection_domain *domain)
  1296. {
  1297. int i, j;
  1298. u64 *p1, *p2, *p3;
  1299. p1 = domain->pt_root;
  1300. if (!p1)
  1301. return;
  1302. for (i = 0; i < 512; ++i) {
  1303. if (!IOMMU_PTE_PRESENT(p1[i]))
  1304. continue;
  1305. p2 = IOMMU_PTE_PAGE(p1[i]);
  1306. for (j = 0; j < 512; ++j) {
  1307. if (!IOMMU_PTE_PRESENT(p2[j]))
  1308. continue;
  1309. p3 = IOMMU_PTE_PAGE(p2[j]);
  1310. free_page((unsigned long)p3);
  1311. }
  1312. free_page((unsigned long)p2);
  1313. }
  1314. free_page((unsigned long)p1);
  1315. domain->pt_root = NULL;
  1316. }
  1317. static void free_gcr3_tbl_level1(u64 *tbl)
  1318. {
  1319. u64 *ptr;
  1320. int i;
  1321. for (i = 0; i < 512; ++i) {
  1322. if (!(tbl[i] & GCR3_VALID))
  1323. continue;
  1324. ptr = __va(tbl[i] & PAGE_MASK);
  1325. free_page((unsigned long)ptr);
  1326. }
  1327. }
  1328. static void free_gcr3_tbl_level2(u64 *tbl)
  1329. {
  1330. u64 *ptr;
  1331. int i;
  1332. for (i = 0; i < 512; ++i) {
  1333. if (!(tbl[i] & GCR3_VALID))
  1334. continue;
  1335. ptr = __va(tbl[i] & PAGE_MASK);
  1336. free_gcr3_tbl_level1(ptr);
  1337. }
  1338. }
  1339. static void free_gcr3_table(struct protection_domain *domain)
  1340. {
  1341. if (domain->glx == 2)
  1342. free_gcr3_tbl_level2(domain->gcr3_tbl);
  1343. else if (domain->glx == 1)
  1344. free_gcr3_tbl_level1(domain->gcr3_tbl);
  1345. else if (domain->glx != 0)
  1346. BUG();
  1347. free_page((unsigned long)domain->gcr3_tbl);
  1348. }
  1349. /*
  1350. * Free a domain, only used if something went wrong in the
  1351. * allocation path and we need to free an already allocated page table
  1352. */
  1353. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  1354. {
  1355. int i;
  1356. if (!dom)
  1357. return;
  1358. del_domain_from_list(&dom->domain);
  1359. free_pagetable(&dom->domain);
  1360. for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
  1361. if (!dom->aperture[i])
  1362. continue;
  1363. free_page((unsigned long)dom->aperture[i]->bitmap);
  1364. kfree(dom->aperture[i]);
  1365. }
  1366. kfree(dom);
  1367. }
  1368. /*
  1369. * Allocates a new protection domain usable for the dma_ops functions.
  1370. * It also initializes the page table and the address allocator data
  1371. * structures required for the dma_ops interface
  1372. */
  1373. static struct dma_ops_domain *dma_ops_domain_alloc(void)
  1374. {
  1375. struct dma_ops_domain *dma_dom;
  1376. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  1377. if (!dma_dom)
  1378. return NULL;
  1379. spin_lock_init(&dma_dom->domain.lock);
  1380. dma_dom->domain.id = domain_id_alloc();
  1381. if (dma_dom->domain.id == 0)
  1382. goto free_dma_dom;
  1383. INIT_LIST_HEAD(&dma_dom->domain.dev_list);
  1384. dma_dom->domain.mode = PAGE_MODE_2_LEVEL;
  1385. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1386. dma_dom->domain.flags = PD_DMA_OPS_MASK;
  1387. dma_dom->domain.priv = dma_dom;
  1388. if (!dma_dom->domain.pt_root)
  1389. goto free_dma_dom;
  1390. dma_dom->need_flush = false;
  1391. dma_dom->target_dev = 0xffff;
  1392. add_domain_to_list(&dma_dom->domain);
  1393. if (alloc_new_range(dma_dom, true, GFP_KERNEL))
  1394. goto free_dma_dom;
  1395. /*
  1396. * mark the first page as allocated so we never return 0 as
  1397. * a valid dma-address. So we can use 0 as error value
  1398. */
  1399. dma_dom->aperture[0]->bitmap[0] = 1;
  1400. dma_dom->next_address = 0;
  1401. return dma_dom;
  1402. free_dma_dom:
  1403. dma_ops_domain_free(dma_dom);
  1404. return NULL;
  1405. }
  1406. /*
  1407. * little helper function to check whether a given protection domain is a
  1408. * dma_ops domain
  1409. */
  1410. static bool dma_ops_domain(struct protection_domain *domain)
  1411. {
  1412. return domain->flags & PD_DMA_OPS_MASK;
  1413. }
  1414. static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
  1415. {
  1416. u64 pte_root = 0;
  1417. u64 flags = 0;
  1418. if (domain->mode != PAGE_MODE_NONE)
  1419. pte_root = virt_to_phys(domain->pt_root);
  1420. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  1421. << DEV_ENTRY_MODE_SHIFT;
  1422. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
  1423. flags = amd_iommu_dev_table[devid].data[1];
  1424. if (ats)
  1425. flags |= DTE_FLAG_IOTLB;
  1426. if (domain->flags & PD_IOMMUV2_MASK) {
  1427. u64 gcr3 = __pa(domain->gcr3_tbl);
  1428. u64 glx = domain->glx;
  1429. u64 tmp;
  1430. pte_root |= DTE_FLAG_GV;
  1431. pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT;
  1432. /* First mask out possible old values for GCR3 table */
  1433. tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
  1434. flags &= ~tmp;
  1435. tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
  1436. flags &= ~tmp;
  1437. /* Encode GCR3 table into DTE */
  1438. tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A;
  1439. pte_root |= tmp;
  1440. tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B;
  1441. flags |= tmp;
  1442. tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C;
  1443. flags |= tmp;
  1444. }
  1445. flags &= ~(0xffffUL);
  1446. flags |= domain->id;
  1447. amd_iommu_dev_table[devid].data[1] = flags;
  1448. amd_iommu_dev_table[devid].data[0] = pte_root;
  1449. }
  1450. static void clear_dte_entry(u16 devid)
  1451. {
  1452. /* remove entry from the device table seen by the hardware */
  1453. amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
  1454. amd_iommu_dev_table[devid].data[1] = 0;
  1455. amd_iommu_apply_erratum_63(devid);
  1456. }
  1457. static void do_attach(struct iommu_dev_data *dev_data,
  1458. struct protection_domain *domain)
  1459. {
  1460. struct amd_iommu *iommu;
  1461. bool ats;
  1462. iommu = amd_iommu_rlookup_table[dev_data->devid];
  1463. ats = dev_data->ats.enabled;
  1464. /* Update data structures */
  1465. dev_data->domain = domain;
  1466. list_add(&dev_data->list, &domain->dev_list);
  1467. set_dte_entry(dev_data->devid, domain, ats);
  1468. /* Do reference counting */
  1469. domain->dev_iommu[iommu->index] += 1;
  1470. domain->dev_cnt += 1;
  1471. /* Flush the DTE entry */
  1472. device_flush_dte(dev_data);
  1473. }
  1474. static void do_detach(struct iommu_dev_data *dev_data)
  1475. {
  1476. struct amd_iommu *iommu;
  1477. iommu = amd_iommu_rlookup_table[dev_data->devid];
  1478. /* decrease reference counters */
  1479. dev_data->domain->dev_iommu[iommu->index] -= 1;
  1480. dev_data->domain->dev_cnt -= 1;
  1481. /* Update data structures */
  1482. dev_data->domain = NULL;
  1483. list_del(&dev_data->list);
  1484. clear_dte_entry(dev_data->devid);
  1485. /* Flush the DTE entry */
  1486. device_flush_dte(dev_data);
  1487. }
  1488. /*
  1489. * If a device is not yet associated with a domain, this function does
  1490. * assigns it visible for the hardware
  1491. */
  1492. static int __attach_device(struct iommu_dev_data *dev_data,
  1493. struct protection_domain *domain)
  1494. {
  1495. int ret;
  1496. /* lock domain */
  1497. spin_lock(&domain->lock);
  1498. if (dev_data->alias_data != NULL) {
  1499. struct iommu_dev_data *alias_data = dev_data->alias_data;
  1500. /* Some sanity checks */
  1501. ret = -EBUSY;
  1502. if (alias_data->domain != NULL &&
  1503. alias_data->domain != domain)
  1504. goto out_unlock;
  1505. if (dev_data->domain != NULL &&
  1506. dev_data->domain != domain)
  1507. goto out_unlock;
  1508. /* Do real assignment */
  1509. if (alias_data->domain == NULL)
  1510. do_attach(alias_data, domain);
  1511. atomic_inc(&alias_data->bind);
  1512. }
  1513. if (dev_data->domain == NULL)
  1514. do_attach(dev_data, domain);
  1515. atomic_inc(&dev_data->bind);
  1516. ret = 0;
  1517. out_unlock:
  1518. /* ready */
  1519. spin_unlock(&domain->lock);
  1520. return ret;
  1521. }
  1522. static void pdev_iommuv2_disable(struct pci_dev *pdev)
  1523. {
  1524. pci_disable_ats(pdev);
  1525. pci_disable_pri(pdev);
  1526. pci_disable_pasid(pdev);
  1527. }
  1528. static int pdev_iommuv2_enable(struct pci_dev *pdev)
  1529. {
  1530. int ret;
  1531. /* Only allow access to user-accessible pages */
  1532. ret = pci_enable_pasid(pdev, 0);
  1533. if (ret)
  1534. goto out_err;
  1535. /* First reset the PRI state of the device */
  1536. ret = pci_reset_pri(pdev);
  1537. if (ret)
  1538. goto out_err;
  1539. /* FIXME: Hardcode number of outstanding requests for now */
  1540. ret = pci_enable_pri(pdev, 32);
  1541. if (ret)
  1542. goto out_err;
  1543. ret = pci_enable_ats(pdev, PAGE_SHIFT);
  1544. if (ret)
  1545. goto out_err;
  1546. return 0;
  1547. out_err:
  1548. pci_disable_pri(pdev);
  1549. pci_disable_pasid(pdev);
  1550. return ret;
  1551. }
  1552. /* FIXME: Move this to PCI code */
  1553. #define PCI_PRI_TLP_OFF (1 << 2)
  1554. bool pci_pri_tlp_required(struct pci_dev *pdev)
  1555. {
  1556. u16 control;
  1557. int pos;
  1558. pos = pci_find_ext_capability(pdev, PCI_PRI_CAP);
  1559. if (!pos)
  1560. return false;
  1561. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  1562. return (control & PCI_PRI_TLP_OFF) ? true : false;
  1563. }
  1564. /*
  1565. * If a device is not yet associated with a domain, this function does
  1566. * assigns it visible for the hardware
  1567. */
  1568. static int attach_device(struct device *dev,
  1569. struct protection_domain *domain)
  1570. {
  1571. struct pci_dev *pdev = to_pci_dev(dev);
  1572. struct iommu_dev_data *dev_data;
  1573. unsigned long flags;
  1574. int ret;
  1575. dev_data = get_dev_data(dev);
  1576. if (domain->flags & PD_IOMMUV2_MASK) {
  1577. if (!dev_data->iommu_v2 || !dev_data->passthrough)
  1578. return -EINVAL;
  1579. if (pdev_iommuv2_enable(pdev) != 0)
  1580. return -EINVAL;
  1581. dev_data->ats.enabled = true;
  1582. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1583. dev_data->pri_tlp = pci_pri_tlp_required(pdev);
  1584. } else if (amd_iommu_iotlb_sup &&
  1585. pci_enable_ats(pdev, PAGE_SHIFT) == 0) {
  1586. dev_data->ats.enabled = true;
  1587. dev_data->ats.qdep = pci_ats_queue_depth(pdev);
  1588. }
  1589. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1590. ret = __attach_device(dev_data, domain);
  1591. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1592. /*
  1593. * We might boot into a crash-kernel here. The crashed kernel
  1594. * left the caches in the IOMMU dirty. So we have to flush
  1595. * here to evict all dirty stuff.
  1596. */
  1597. domain_flush_tlb_pde(domain);
  1598. return ret;
  1599. }
  1600. /*
  1601. * Removes a device from a protection domain (unlocked)
  1602. */
  1603. static void __detach_device(struct iommu_dev_data *dev_data)
  1604. {
  1605. struct protection_domain *domain;
  1606. unsigned long flags;
  1607. BUG_ON(!dev_data->domain);
  1608. domain = dev_data->domain;
  1609. spin_lock_irqsave(&domain->lock, flags);
  1610. if (dev_data->alias_data != NULL) {
  1611. struct iommu_dev_data *alias_data = dev_data->alias_data;
  1612. if (atomic_dec_and_test(&alias_data->bind))
  1613. do_detach(alias_data);
  1614. }
  1615. if (atomic_dec_and_test(&dev_data->bind))
  1616. do_detach(dev_data);
  1617. spin_unlock_irqrestore(&domain->lock, flags);
  1618. /*
  1619. * If we run in passthrough mode the device must be assigned to the
  1620. * passthrough domain if it is detached from any other domain.
  1621. * Make sure we can deassign from the pt_domain itself.
  1622. */
  1623. if (dev_data->passthrough &&
  1624. (dev_data->domain == NULL && domain != pt_domain))
  1625. __attach_device(dev_data, pt_domain);
  1626. }
  1627. /*
  1628. * Removes a device from a protection domain (with devtable_lock held)
  1629. */
  1630. static void detach_device(struct device *dev)
  1631. {
  1632. struct protection_domain *domain;
  1633. struct iommu_dev_data *dev_data;
  1634. unsigned long flags;
  1635. dev_data = get_dev_data(dev);
  1636. domain = dev_data->domain;
  1637. /* lock device table */
  1638. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1639. __detach_device(dev_data);
  1640. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1641. if (domain->flags & PD_IOMMUV2_MASK)
  1642. pdev_iommuv2_disable(to_pci_dev(dev));
  1643. else if (dev_data->ats.enabled)
  1644. pci_disable_ats(to_pci_dev(dev));
  1645. dev_data->ats.enabled = false;
  1646. }
  1647. /*
  1648. * Find out the protection domain structure for a given PCI device. This
  1649. * will give us the pointer to the page table root for example.
  1650. */
  1651. static struct protection_domain *domain_for_device(struct device *dev)
  1652. {
  1653. struct iommu_dev_data *dev_data;
  1654. struct protection_domain *dom = NULL;
  1655. unsigned long flags;
  1656. dev_data = get_dev_data(dev);
  1657. if (dev_data->domain)
  1658. return dev_data->domain;
  1659. if (dev_data->alias_data != NULL) {
  1660. struct iommu_dev_data *alias_data = dev_data->alias_data;
  1661. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1662. if (alias_data->domain != NULL) {
  1663. __attach_device(dev_data, alias_data->domain);
  1664. dom = alias_data->domain;
  1665. }
  1666. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1667. }
  1668. return dom;
  1669. }
  1670. static int device_change_notifier(struct notifier_block *nb,
  1671. unsigned long action, void *data)
  1672. {
  1673. struct dma_ops_domain *dma_domain;
  1674. struct protection_domain *domain;
  1675. struct iommu_dev_data *dev_data;
  1676. struct device *dev = data;
  1677. struct amd_iommu *iommu;
  1678. unsigned long flags;
  1679. u16 devid;
  1680. if (!check_device(dev))
  1681. return 0;
  1682. devid = get_device_id(dev);
  1683. iommu = amd_iommu_rlookup_table[devid];
  1684. dev_data = get_dev_data(dev);
  1685. switch (action) {
  1686. case BUS_NOTIFY_UNBOUND_DRIVER:
  1687. domain = domain_for_device(dev);
  1688. if (!domain)
  1689. goto out;
  1690. if (dev_data->passthrough)
  1691. break;
  1692. detach_device(dev);
  1693. break;
  1694. case BUS_NOTIFY_ADD_DEVICE:
  1695. iommu_init_device(dev);
  1696. domain = domain_for_device(dev);
  1697. /* allocate a protection domain if a device is added */
  1698. dma_domain = find_protection_domain(devid);
  1699. if (dma_domain)
  1700. goto out;
  1701. dma_domain = dma_ops_domain_alloc();
  1702. if (!dma_domain)
  1703. goto out;
  1704. dma_domain->target_dev = devid;
  1705. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  1706. list_add_tail(&dma_domain->list, &iommu_pd_list);
  1707. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  1708. break;
  1709. case BUS_NOTIFY_DEL_DEVICE:
  1710. iommu_uninit_device(dev);
  1711. default:
  1712. goto out;
  1713. }
  1714. iommu_completion_wait(iommu);
  1715. out:
  1716. return 0;
  1717. }
  1718. static struct notifier_block device_nb = {
  1719. .notifier_call = device_change_notifier,
  1720. };
  1721. void amd_iommu_init_notifier(void)
  1722. {
  1723. bus_register_notifier(&pci_bus_type, &device_nb);
  1724. }
  1725. /*****************************************************************************
  1726. *
  1727. * The next functions belong to the dma_ops mapping/unmapping code.
  1728. *
  1729. *****************************************************************************/
  1730. /*
  1731. * In the dma_ops path we only have the struct device. This function
  1732. * finds the corresponding IOMMU, the protection domain and the
  1733. * requestor id for a given device.
  1734. * If the device is not yet associated with a domain this is also done
  1735. * in this function.
  1736. */
  1737. static struct protection_domain *get_domain(struct device *dev)
  1738. {
  1739. struct protection_domain *domain;
  1740. struct dma_ops_domain *dma_dom;
  1741. u16 devid = get_device_id(dev);
  1742. if (!check_device(dev))
  1743. return ERR_PTR(-EINVAL);
  1744. domain = domain_for_device(dev);
  1745. if (domain != NULL && !dma_ops_domain(domain))
  1746. return ERR_PTR(-EBUSY);
  1747. if (domain != NULL)
  1748. return domain;
  1749. /* Device not bount yet - bind it */
  1750. dma_dom = find_protection_domain(devid);
  1751. if (!dma_dom)
  1752. dma_dom = amd_iommu_rlookup_table[devid]->default_dom;
  1753. attach_device(dev, &dma_dom->domain);
  1754. DUMP_printk("Using protection domain %d for device %s\n",
  1755. dma_dom->domain.id, dev_name(dev));
  1756. return &dma_dom->domain;
  1757. }
  1758. static void update_device_table(struct protection_domain *domain)
  1759. {
  1760. struct iommu_dev_data *dev_data;
  1761. list_for_each_entry(dev_data, &domain->dev_list, list)
  1762. set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled);
  1763. }
  1764. static void update_domain(struct protection_domain *domain)
  1765. {
  1766. if (!domain->updated)
  1767. return;
  1768. update_device_table(domain);
  1769. domain_flush_devices(domain);
  1770. domain_flush_tlb_pde(domain);
  1771. domain->updated = false;
  1772. }
  1773. /*
  1774. * This function fetches the PTE for a given address in the aperture
  1775. */
  1776. static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
  1777. unsigned long address)
  1778. {
  1779. struct aperture_range *aperture;
  1780. u64 *pte, *pte_page;
  1781. aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
  1782. if (!aperture)
  1783. return NULL;
  1784. pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
  1785. if (!pte) {
  1786. pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page,
  1787. GFP_ATOMIC);
  1788. aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page;
  1789. } else
  1790. pte += PM_LEVEL_INDEX(0, address);
  1791. update_domain(&dom->domain);
  1792. return pte;
  1793. }
  1794. /*
  1795. * This is the generic map function. It maps one 4kb page at paddr to
  1796. * the given address in the DMA address space for the domain.
  1797. */
  1798. static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
  1799. unsigned long address,
  1800. phys_addr_t paddr,
  1801. int direction)
  1802. {
  1803. u64 *pte, __pte;
  1804. WARN_ON(address > dom->aperture_size);
  1805. paddr &= PAGE_MASK;
  1806. pte = dma_ops_get_pte(dom, address);
  1807. if (!pte)
  1808. return DMA_ERROR_CODE;
  1809. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  1810. if (direction == DMA_TO_DEVICE)
  1811. __pte |= IOMMU_PTE_IR;
  1812. else if (direction == DMA_FROM_DEVICE)
  1813. __pte |= IOMMU_PTE_IW;
  1814. else if (direction == DMA_BIDIRECTIONAL)
  1815. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  1816. WARN_ON(*pte);
  1817. *pte = __pte;
  1818. return (dma_addr_t)address;
  1819. }
  1820. /*
  1821. * The generic unmapping function for on page in the DMA address space.
  1822. */
  1823. static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
  1824. unsigned long address)
  1825. {
  1826. struct aperture_range *aperture;
  1827. u64 *pte;
  1828. if (address >= dom->aperture_size)
  1829. return;
  1830. aperture = dom->aperture[APERTURE_RANGE_INDEX(address)];
  1831. if (!aperture)
  1832. return;
  1833. pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)];
  1834. if (!pte)
  1835. return;
  1836. pte += PM_LEVEL_INDEX(0, address);
  1837. WARN_ON(!*pte);
  1838. *pte = 0ULL;
  1839. }
  1840. /*
  1841. * This function contains common code for mapping of a physically
  1842. * contiguous memory region into DMA address space. It is used by all
  1843. * mapping functions provided with this IOMMU driver.
  1844. * Must be called with the domain lock held.
  1845. */
  1846. static dma_addr_t __map_single(struct device *dev,
  1847. struct dma_ops_domain *dma_dom,
  1848. phys_addr_t paddr,
  1849. size_t size,
  1850. int dir,
  1851. bool align,
  1852. u64 dma_mask)
  1853. {
  1854. dma_addr_t offset = paddr & ~PAGE_MASK;
  1855. dma_addr_t address, start, ret;
  1856. unsigned int pages;
  1857. unsigned long align_mask = 0;
  1858. int i;
  1859. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1860. paddr &= PAGE_MASK;
  1861. INC_STATS_COUNTER(total_map_requests);
  1862. if (pages > 1)
  1863. INC_STATS_COUNTER(cross_page);
  1864. if (align)
  1865. align_mask = (1UL << get_order(size)) - 1;
  1866. retry:
  1867. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
  1868. dma_mask);
  1869. if (unlikely(address == DMA_ERROR_CODE)) {
  1870. /*
  1871. * setting next_address here will let the address
  1872. * allocator only scan the new allocated range in the
  1873. * first run. This is a small optimization.
  1874. */
  1875. dma_dom->next_address = dma_dom->aperture_size;
  1876. if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
  1877. goto out;
  1878. /*
  1879. * aperture was successfully enlarged by 128 MB, try
  1880. * allocation again
  1881. */
  1882. goto retry;
  1883. }
  1884. start = address;
  1885. for (i = 0; i < pages; ++i) {
  1886. ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
  1887. if (ret == DMA_ERROR_CODE)
  1888. goto out_unmap;
  1889. paddr += PAGE_SIZE;
  1890. start += PAGE_SIZE;
  1891. }
  1892. address += offset;
  1893. ADD_STATS_COUNTER(alloced_io_mem, size);
  1894. if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
  1895. domain_flush_tlb(&dma_dom->domain);
  1896. dma_dom->need_flush = false;
  1897. } else if (unlikely(amd_iommu_np_cache))
  1898. domain_flush_pages(&dma_dom->domain, address, size);
  1899. out:
  1900. return address;
  1901. out_unmap:
  1902. for (--i; i >= 0; --i) {
  1903. start -= PAGE_SIZE;
  1904. dma_ops_domain_unmap(dma_dom, start);
  1905. }
  1906. dma_ops_free_addresses(dma_dom, address, pages);
  1907. return DMA_ERROR_CODE;
  1908. }
  1909. /*
  1910. * Does the reverse of the __map_single function. Must be called with
  1911. * the domain lock held too
  1912. */
  1913. static void __unmap_single(struct dma_ops_domain *dma_dom,
  1914. dma_addr_t dma_addr,
  1915. size_t size,
  1916. int dir)
  1917. {
  1918. dma_addr_t flush_addr;
  1919. dma_addr_t i, start;
  1920. unsigned int pages;
  1921. if ((dma_addr == DMA_ERROR_CODE) ||
  1922. (dma_addr + size > dma_dom->aperture_size))
  1923. return;
  1924. flush_addr = dma_addr;
  1925. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  1926. dma_addr &= PAGE_MASK;
  1927. start = dma_addr;
  1928. for (i = 0; i < pages; ++i) {
  1929. dma_ops_domain_unmap(dma_dom, start);
  1930. start += PAGE_SIZE;
  1931. }
  1932. SUB_STATS_COUNTER(alloced_io_mem, size);
  1933. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  1934. if (amd_iommu_unmap_flush || dma_dom->need_flush) {
  1935. domain_flush_pages(&dma_dom->domain, flush_addr, size);
  1936. dma_dom->need_flush = false;
  1937. }
  1938. }
  1939. /*
  1940. * The exported map_single function for dma_ops.
  1941. */
  1942. static dma_addr_t map_page(struct device *dev, struct page *page,
  1943. unsigned long offset, size_t size,
  1944. enum dma_data_direction dir,
  1945. struct dma_attrs *attrs)
  1946. {
  1947. unsigned long flags;
  1948. struct protection_domain *domain;
  1949. dma_addr_t addr;
  1950. u64 dma_mask;
  1951. phys_addr_t paddr = page_to_phys(page) + offset;
  1952. INC_STATS_COUNTER(cnt_map_single);
  1953. domain = get_domain(dev);
  1954. if (PTR_ERR(domain) == -EINVAL)
  1955. return (dma_addr_t)paddr;
  1956. else if (IS_ERR(domain))
  1957. return DMA_ERROR_CODE;
  1958. dma_mask = *dev->dma_mask;
  1959. spin_lock_irqsave(&domain->lock, flags);
  1960. addr = __map_single(dev, domain->priv, paddr, size, dir, false,
  1961. dma_mask);
  1962. if (addr == DMA_ERROR_CODE)
  1963. goto out;
  1964. domain_flush_complete(domain);
  1965. out:
  1966. spin_unlock_irqrestore(&domain->lock, flags);
  1967. return addr;
  1968. }
  1969. /*
  1970. * The exported unmap_single function for dma_ops.
  1971. */
  1972. static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
  1973. enum dma_data_direction dir, struct dma_attrs *attrs)
  1974. {
  1975. unsigned long flags;
  1976. struct protection_domain *domain;
  1977. INC_STATS_COUNTER(cnt_unmap_single);
  1978. domain = get_domain(dev);
  1979. if (IS_ERR(domain))
  1980. return;
  1981. spin_lock_irqsave(&domain->lock, flags);
  1982. __unmap_single(domain->priv, dma_addr, size, dir);
  1983. domain_flush_complete(domain);
  1984. spin_unlock_irqrestore(&domain->lock, flags);
  1985. }
  1986. /*
  1987. * This is a special map_sg function which is used if we should map a
  1988. * device which is not handled by an AMD IOMMU in the system.
  1989. */
  1990. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  1991. int nelems, int dir)
  1992. {
  1993. struct scatterlist *s;
  1994. int i;
  1995. for_each_sg(sglist, s, nelems, i) {
  1996. s->dma_address = (dma_addr_t)sg_phys(s);
  1997. s->dma_length = s->length;
  1998. }
  1999. return nelems;
  2000. }
  2001. /*
  2002. * The exported map_sg function for dma_ops (handles scatter-gather
  2003. * lists).
  2004. */
  2005. static int map_sg(struct device *dev, struct scatterlist *sglist,
  2006. int nelems, enum dma_data_direction dir,
  2007. struct dma_attrs *attrs)
  2008. {
  2009. unsigned long flags;
  2010. struct protection_domain *domain;
  2011. int i;
  2012. struct scatterlist *s;
  2013. phys_addr_t paddr;
  2014. int mapped_elems = 0;
  2015. u64 dma_mask;
  2016. INC_STATS_COUNTER(cnt_map_sg);
  2017. domain = get_domain(dev);
  2018. if (PTR_ERR(domain) == -EINVAL)
  2019. return map_sg_no_iommu(dev, sglist, nelems, dir);
  2020. else if (IS_ERR(domain))
  2021. return 0;
  2022. dma_mask = *dev->dma_mask;
  2023. spin_lock_irqsave(&domain->lock, flags);
  2024. for_each_sg(sglist, s, nelems, i) {
  2025. paddr = sg_phys(s);
  2026. s->dma_address = __map_single(dev, domain->priv,
  2027. paddr, s->length, dir, false,
  2028. dma_mask);
  2029. if (s->dma_address) {
  2030. s->dma_length = s->length;
  2031. mapped_elems++;
  2032. } else
  2033. goto unmap;
  2034. }
  2035. domain_flush_complete(domain);
  2036. out:
  2037. spin_unlock_irqrestore(&domain->lock, flags);
  2038. return mapped_elems;
  2039. unmap:
  2040. for_each_sg(sglist, s, mapped_elems, i) {
  2041. if (s->dma_address)
  2042. __unmap_single(domain->priv, s->dma_address,
  2043. s->dma_length, dir);
  2044. s->dma_address = s->dma_length = 0;
  2045. }
  2046. mapped_elems = 0;
  2047. goto out;
  2048. }
  2049. /*
  2050. * The exported map_sg function for dma_ops (handles scatter-gather
  2051. * lists).
  2052. */
  2053. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  2054. int nelems, enum dma_data_direction dir,
  2055. struct dma_attrs *attrs)
  2056. {
  2057. unsigned long flags;
  2058. struct protection_domain *domain;
  2059. struct scatterlist *s;
  2060. int i;
  2061. INC_STATS_COUNTER(cnt_unmap_sg);
  2062. domain = get_domain(dev);
  2063. if (IS_ERR(domain))
  2064. return;
  2065. spin_lock_irqsave(&domain->lock, flags);
  2066. for_each_sg(sglist, s, nelems, i) {
  2067. __unmap_single(domain->priv, s->dma_address,
  2068. s->dma_length, dir);
  2069. s->dma_address = s->dma_length = 0;
  2070. }
  2071. domain_flush_complete(domain);
  2072. spin_unlock_irqrestore(&domain->lock, flags);
  2073. }
  2074. /*
  2075. * The exported alloc_coherent function for dma_ops.
  2076. */
  2077. static void *alloc_coherent(struct device *dev, size_t size,
  2078. dma_addr_t *dma_addr, gfp_t flag)
  2079. {
  2080. unsigned long flags;
  2081. void *virt_addr;
  2082. struct protection_domain *domain;
  2083. phys_addr_t paddr;
  2084. u64 dma_mask = dev->coherent_dma_mask;
  2085. INC_STATS_COUNTER(cnt_alloc_coherent);
  2086. domain = get_domain(dev);
  2087. if (PTR_ERR(domain) == -EINVAL) {
  2088. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  2089. *dma_addr = __pa(virt_addr);
  2090. return virt_addr;
  2091. } else if (IS_ERR(domain))
  2092. return NULL;
  2093. dma_mask = dev->coherent_dma_mask;
  2094. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  2095. flag |= __GFP_ZERO;
  2096. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  2097. if (!virt_addr)
  2098. return NULL;
  2099. paddr = virt_to_phys(virt_addr);
  2100. if (!dma_mask)
  2101. dma_mask = *dev->dma_mask;
  2102. spin_lock_irqsave(&domain->lock, flags);
  2103. *dma_addr = __map_single(dev, domain->priv, paddr,
  2104. size, DMA_BIDIRECTIONAL, true, dma_mask);
  2105. if (*dma_addr == DMA_ERROR_CODE) {
  2106. spin_unlock_irqrestore(&domain->lock, flags);
  2107. goto out_free;
  2108. }
  2109. domain_flush_complete(domain);
  2110. spin_unlock_irqrestore(&domain->lock, flags);
  2111. return virt_addr;
  2112. out_free:
  2113. free_pages((unsigned long)virt_addr, get_order(size));
  2114. return NULL;
  2115. }
  2116. /*
  2117. * The exported free_coherent function for dma_ops.
  2118. */
  2119. static void free_coherent(struct device *dev, size_t size,
  2120. void *virt_addr, dma_addr_t dma_addr)
  2121. {
  2122. unsigned long flags;
  2123. struct protection_domain *domain;
  2124. INC_STATS_COUNTER(cnt_free_coherent);
  2125. domain = get_domain(dev);
  2126. if (IS_ERR(domain))
  2127. goto free_mem;
  2128. spin_lock_irqsave(&domain->lock, flags);
  2129. __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  2130. domain_flush_complete(domain);
  2131. spin_unlock_irqrestore(&domain->lock, flags);
  2132. free_mem:
  2133. free_pages((unsigned long)virt_addr, get_order(size));
  2134. }
  2135. /*
  2136. * This function is called by the DMA layer to find out if we can handle a
  2137. * particular device. It is part of the dma_ops.
  2138. */
  2139. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  2140. {
  2141. return check_device(dev);
  2142. }
  2143. /*
  2144. * The function for pre-allocating protection domains.
  2145. *
  2146. * If the driver core informs the DMA layer if a driver grabs a device
  2147. * we don't need to preallocate the protection domains anymore.
  2148. * For now we have to.
  2149. */
  2150. static void prealloc_protection_domains(void)
  2151. {
  2152. struct iommu_dev_data *dev_data;
  2153. struct dma_ops_domain *dma_dom;
  2154. struct pci_dev *dev = NULL;
  2155. u16 devid;
  2156. for_each_pci_dev(dev) {
  2157. /* Do we handle this device? */
  2158. if (!check_device(&dev->dev))
  2159. continue;
  2160. dev_data = get_dev_data(&dev->dev);
  2161. if (!amd_iommu_force_isolation && dev_data->iommu_v2) {
  2162. /* Make sure passthrough domain is allocated */
  2163. alloc_passthrough_domain();
  2164. dev_data->passthrough = true;
  2165. attach_device(&dev->dev, pt_domain);
  2166. pr_info("AMD-Vi: Using passthough domain for device %s\n",
  2167. dev_name(&dev->dev));
  2168. }
  2169. /* Is there already any domain for it? */
  2170. if (domain_for_device(&dev->dev))
  2171. continue;
  2172. devid = get_device_id(&dev->dev);
  2173. dma_dom = dma_ops_domain_alloc();
  2174. if (!dma_dom)
  2175. continue;
  2176. init_unity_mappings_for_device(dma_dom, devid);
  2177. dma_dom->target_dev = devid;
  2178. attach_device(&dev->dev, &dma_dom->domain);
  2179. list_add_tail(&dma_dom->list, &iommu_pd_list);
  2180. }
  2181. }
  2182. static struct dma_map_ops amd_iommu_dma_ops = {
  2183. .alloc_coherent = alloc_coherent,
  2184. .free_coherent = free_coherent,
  2185. .map_page = map_page,
  2186. .unmap_page = unmap_page,
  2187. .map_sg = map_sg,
  2188. .unmap_sg = unmap_sg,
  2189. .dma_supported = amd_iommu_dma_supported,
  2190. };
  2191. static unsigned device_dma_ops_init(void)
  2192. {
  2193. struct iommu_dev_data *dev_data;
  2194. struct pci_dev *pdev = NULL;
  2195. unsigned unhandled = 0;
  2196. for_each_pci_dev(pdev) {
  2197. if (!check_device(&pdev->dev)) {
  2198. unhandled += 1;
  2199. continue;
  2200. }
  2201. dev_data = get_dev_data(&pdev->dev);
  2202. if (!dev_data->passthrough)
  2203. pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops;
  2204. else
  2205. pdev->dev.archdata.dma_ops = &nommu_dma_ops;
  2206. }
  2207. return unhandled;
  2208. }
  2209. /*
  2210. * The function which clues the AMD IOMMU driver into dma_ops.
  2211. */
  2212. void __init amd_iommu_init_api(void)
  2213. {
  2214. bus_set_iommu(&pci_bus_type, &amd_iommu_ops);
  2215. }
  2216. int __init amd_iommu_init_dma_ops(void)
  2217. {
  2218. struct amd_iommu *iommu;
  2219. int ret, unhandled;
  2220. /*
  2221. * first allocate a default protection domain for every IOMMU we
  2222. * found in the system. Devices not assigned to any other
  2223. * protection domain will be assigned to the default one.
  2224. */
  2225. for_each_iommu(iommu) {
  2226. iommu->default_dom = dma_ops_domain_alloc();
  2227. if (iommu->default_dom == NULL)
  2228. return -ENOMEM;
  2229. iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
  2230. ret = iommu_init_unity_mappings(iommu);
  2231. if (ret)
  2232. goto free_domains;
  2233. }
  2234. /*
  2235. * Pre-allocate the protection domains for each device.
  2236. */
  2237. prealloc_protection_domains();
  2238. iommu_detected = 1;
  2239. swiotlb = 0;
  2240. /* Make the driver finally visible to the drivers */
  2241. unhandled = device_dma_ops_init();
  2242. if (unhandled && max_pfn > MAX_DMA32_PFN) {
  2243. /* There are unhandled devices - initialize swiotlb for them */
  2244. swiotlb = 1;
  2245. }
  2246. amd_iommu_stats_init();
  2247. return 0;
  2248. free_domains:
  2249. for_each_iommu(iommu) {
  2250. if (iommu->default_dom)
  2251. dma_ops_domain_free(iommu->default_dom);
  2252. }
  2253. return ret;
  2254. }
  2255. /*****************************************************************************
  2256. *
  2257. * The following functions belong to the exported interface of AMD IOMMU
  2258. *
  2259. * This interface allows access to lower level functions of the IOMMU
  2260. * like protection domain handling and assignement of devices to domains
  2261. * which is not possible with the dma_ops interface.
  2262. *
  2263. *****************************************************************************/
  2264. static void cleanup_domain(struct protection_domain *domain)
  2265. {
  2266. struct iommu_dev_data *dev_data, *next;
  2267. unsigned long flags;
  2268. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  2269. list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) {
  2270. __detach_device(dev_data);
  2271. atomic_set(&dev_data->bind, 0);
  2272. }
  2273. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  2274. }
  2275. static void protection_domain_free(struct protection_domain *domain)
  2276. {
  2277. if (!domain)
  2278. return;
  2279. del_domain_from_list(domain);
  2280. if (domain->id)
  2281. domain_id_free(domain->id);
  2282. kfree(domain);
  2283. }
  2284. static struct protection_domain *protection_domain_alloc(void)
  2285. {
  2286. struct protection_domain *domain;
  2287. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  2288. if (!domain)
  2289. return NULL;
  2290. spin_lock_init(&domain->lock);
  2291. mutex_init(&domain->api_lock);
  2292. domain->id = domain_id_alloc();
  2293. if (!domain->id)
  2294. goto out_err;
  2295. INIT_LIST_HEAD(&domain->dev_list);
  2296. add_domain_to_list(domain);
  2297. return domain;
  2298. out_err:
  2299. kfree(domain);
  2300. return NULL;
  2301. }
  2302. static int __init alloc_passthrough_domain(void)
  2303. {
  2304. if (pt_domain != NULL)
  2305. return 0;
  2306. /* allocate passthrough domain */
  2307. pt_domain = protection_domain_alloc();
  2308. if (!pt_domain)
  2309. return -ENOMEM;
  2310. pt_domain->mode = PAGE_MODE_NONE;
  2311. return 0;
  2312. }
  2313. static int amd_iommu_domain_init(struct iommu_domain *dom)
  2314. {
  2315. struct protection_domain *domain;
  2316. domain = protection_domain_alloc();
  2317. if (!domain)
  2318. goto out_free;
  2319. domain->mode = PAGE_MODE_3_LEVEL;
  2320. domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  2321. if (!domain->pt_root)
  2322. goto out_free;
  2323. dom->priv = domain;
  2324. return 0;
  2325. out_free:
  2326. protection_domain_free(domain);
  2327. return -ENOMEM;
  2328. }
  2329. static void amd_iommu_domain_destroy(struct iommu_domain *dom)
  2330. {
  2331. struct protection_domain *domain = dom->priv;
  2332. if (!domain)
  2333. return;
  2334. if (domain->dev_cnt > 0)
  2335. cleanup_domain(domain);
  2336. BUG_ON(domain->dev_cnt != 0);
  2337. if (domain->mode != PAGE_MODE_NONE)
  2338. free_pagetable(domain);
  2339. if (domain->flags & PD_IOMMUV2_MASK)
  2340. free_gcr3_table(domain);
  2341. protection_domain_free(domain);
  2342. dom->priv = NULL;
  2343. }
  2344. static void amd_iommu_detach_device(struct iommu_domain *dom,
  2345. struct device *dev)
  2346. {
  2347. struct iommu_dev_data *dev_data = dev->archdata.iommu;
  2348. struct amd_iommu *iommu;
  2349. u16 devid;
  2350. if (!check_device(dev))
  2351. return;
  2352. devid = get_device_id(dev);
  2353. if (dev_data->domain != NULL)
  2354. detach_device(dev);
  2355. iommu = amd_iommu_rlookup_table[devid];
  2356. if (!iommu)
  2357. return;
  2358. iommu_completion_wait(iommu);
  2359. }
  2360. static int amd_iommu_attach_device(struct iommu_domain *dom,
  2361. struct device *dev)
  2362. {
  2363. struct protection_domain *domain = dom->priv;
  2364. struct iommu_dev_data *dev_data;
  2365. struct amd_iommu *iommu;
  2366. int ret;
  2367. if (!check_device(dev))
  2368. return -EINVAL;
  2369. dev_data = dev->archdata.iommu;
  2370. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2371. if (!iommu)
  2372. return -EINVAL;
  2373. if (dev_data->domain)
  2374. detach_device(dev);
  2375. ret = attach_device(dev, domain);
  2376. iommu_completion_wait(iommu);
  2377. return ret;
  2378. }
  2379. static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova,
  2380. phys_addr_t paddr, int gfp_order, int iommu_prot)
  2381. {
  2382. unsigned long page_size = 0x1000UL << gfp_order;
  2383. struct protection_domain *domain = dom->priv;
  2384. int prot = 0;
  2385. int ret;
  2386. if (domain->mode == PAGE_MODE_NONE)
  2387. return -EINVAL;
  2388. if (iommu_prot & IOMMU_READ)
  2389. prot |= IOMMU_PROT_IR;
  2390. if (iommu_prot & IOMMU_WRITE)
  2391. prot |= IOMMU_PROT_IW;
  2392. mutex_lock(&domain->api_lock);
  2393. ret = iommu_map_page(domain, iova, paddr, prot, page_size);
  2394. mutex_unlock(&domain->api_lock);
  2395. return ret;
  2396. }
  2397. static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova,
  2398. int gfp_order)
  2399. {
  2400. struct protection_domain *domain = dom->priv;
  2401. unsigned long page_size, unmap_size;
  2402. if (domain->mode == PAGE_MODE_NONE)
  2403. return -EINVAL;
  2404. page_size = 0x1000UL << gfp_order;
  2405. mutex_lock(&domain->api_lock);
  2406. unmap_size = iommu_unmap_page(domain, iova, page_size);
  2407. mutex_unlock(&domain->api_lock);
  2408. domain_flush_tlb_pde(domain);
  2409. return get_order(unmap_size);
  2410. }
  2411. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  2412. unsigned long iova)
  2413. {
  2414. struct protection_domain *domain = dom->priv;
  2415. unsigned long offset_mask;
  2416. phys_addr_t paddr;
  2417. u64 *pte, __pte;
  2418. if (domain->mode == PAGE_MODE_NONE)
  2419. return iova;
  2420. pte = fetch_pte(domain, iova);
  2421. if (!pte || !IOMMU_PTE_PRESENT(*pte))
  2422. return 0;
  2423. if (PM_PTE_LEVEL(*pte) == 0)
  2424. offset_mask = PAGE_SIZE - 1;
  2425. else
  2426. offset_mask = PTE_PAGE_SIZE(*pte) - 1;
  2427. __pte = *pte & PM_ADDR_MASK;
  2428. paddr = (__pte & ~offset_mask) | (iova & offset_mask);
  2429. return paddr;
  2430. }
  2431. static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
  2432. unsigned long cap)
  2433. {
  2434. switch (cap) {
  2435. case IOMMU_CAP_CACHE_COHERENCY:
  2436. return 1;
  2437. }
  2438. return 0;
  2439. }
  2440. static struct iommu_ops amd_iommu_ops = {
  2441. .domain_init = amd_iommu_domain_init,
  2442. .domain_destroy = amd_iommu_domain_destroy,
  2443. .attach_dev = amd_iommu_attach_device,
  2444. .detach_dev = amd_iommu_detach_device,
  2445. .map = amd_iommu_map,
  2446. .unmap = amd_iommu_unmap,
  2447. .iova_to_phys = amd_iommu_iova_to_phys,
  2448. .domain_has_cap = amd_iommu_domain_has_cap,
  2449. };
  2450. /*****************************************************************************
  2451. *
  2452. * The next functions do a basic initialization of IOMMU for pass through
  2453. * mode
  2454. *
  2455. * In passthrough mode the IOMMU is initialized and enabled but not used for
  2456. * DMA-API translation.
  2457. *
  2458. *****************************************************************************/
  2459. int __init amd_iommu_init_passthrough(void)
  2460. {
  2461. struct iommu_dev_data *dev_data;
  2462. struct pci_dev *dev = NULL;
  2463. struct amd_iommu *iommu;
  2464. u16 devid;
  2465. int ret;
  2466. ret = alloc_passthrough_domain();
  2467. if (ret)
  2468. return ret;
  2469. for_each_pci_dev(dev) {
  2470. if (!check_device(&dev->dev))
  2471. continue;
  2472. dev_data = get_dev_data(&dev->dev);
  2473. dev_data->passthrough = true;
  2474. devid = get_device_id(&dev->dev);
  2475. iommu = amd_iommu_rlookup_table[devid];
  2476. if (!iommu)
  2477. continue;
  2478. attach_device(&dev->dev, pt_domain);
  2479. }
  2480. pr_info("AMD-Vi: Initialized for Passthrough Mode\n");
  2481. return 0;
  2482. }
  2483. /* IOMMUv2 specific functions */
  2484. int amd_iommu_register_ppr_notifier(struct notifier_block *nb)
  2485. {
  2486. return atomic_notifier_chain_register(&ppr_notifier, nb);
  2487. }
  2488. EXPORT_SYMBOL(amd_iommu_register_ppr_notifier);
  2489. int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb)
  2490. {
  2491. return atomic_notifier_chain_unregister(&ppr_notifier, nb);
  2492. }
  2493. EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier);
  2494. void amd_iommu_domain_direct_map(struct iommu_domain *dom)
  2495. {
  2496. struct protection_domain *domain = dom->priv;
  2497. unsigned long flags;
  2498. spin_lock_irqsave(&domain->lock, flags);
  2499. /* Update data structure */
  2500. domain->mode = PAGE_MODE_NONE;
  2501. domain->updated = true;
  2502. /* Make changes visible to IOMMUs */
  2503. update_domain(domain);
  2504. /* Page-table is not visible to IOMMU anymore, so free it */
  2505. free_pagetable(domain);
  2506. spin_unlock_irqrestore(&domain->lock, flags);
  2507. }
  2508. EXPORT_SYMBOL(amd_iommu_domain_direct_map);
  2509. int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)
  2510. {
  2511. struct protection_domain *domain = dom->priv;
  2512. unsigned long flags;
  2513. int levels, ret;
  2514. if (pasids <= 0 || pasids > (PASID_MASK + 1))
  2515. return -EINVAL;
  2516. /* Number of GCR3 table levels required */
  2517. for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9)
  2518. levels += 1;
  2519. if (levels > amd_iommu_max_glx_val)
  2520. return -EINVAL;
  2521. spin_lock_irqsave(&domain->lock, flags);
  2522. /*
  2523. * Save us all sanity checks whether devices already in the
  2524. * domain support IOMMUv2. Just force that the domain has no
  2525. * devices attached when it is switched into IOMMUv2 mode.
  2526. */
  2527. ret = -EBUSY;
  2528. if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK)
  2529. goto out;
  2530. ret = -ENOMEM;
  2531. domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC);
  2532. if (domain->gcr3_tbl == NULL)
  2533. goto out;
  2534. domain->glx = levels;
  2535. domain->flags |= PD_IOMMUV2_MASK;
  2536. domain->updated = true;
  2537. update_domain(domain);
  2538. ret = 0;
  2539. out:
  2540. spin_unlock_irqrestore(&domain->lock, flags);
  2541. return ret;
  2542. }
  2543. EXPORT_SYMBOL(amd_iommu_domain_enable_v2);
  2544. static int __flush_pasid(struct protection_domain *domain, int pasid,
  2545. u64 address, bool size)
  2546. {
  2547. struct iommu_dev_data *dev_data;
  2548. struct iommu_cmd cmd;
  2549. int i, ret;
  2550. if (!(domain->flags & PD_IOMMUV2_MASK))
  2551. return -EINVAL;
  2552. build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size);
  2553. /*
  2554. * IOMMU TLB needs to be flushed before Device TLB to
  2555. * prevent device TLB refill from IOMMU TLB
  2556. */
  2557. for (i = 0; i < amd_iommus_present; ++i) {
  2558. if (domain->dev_iommu[i] == 0)
  2559. continue;
  2560. ret = iommu_queue_command(amd_iommus[i], &cmd);
  2561. if (ret != 0)
  2562. goto out;
  2563. }
  2564. /* Wait until IOMMU TLB flushes are complete */
  2565. domain_flush_complete(domain);
  2566. /* Now flush device TLBs */
  2567. list_for_each_entry(dev_data, &domain->dev_list, list) {
  2568. struct amd_iommu *iommu;
  2569. int qdep;
  2570. BUG_ON(!dev_data->ats.enabled);
  2571. qdep = dev_data->ats.qdep;
  2572. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2573. build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid,
  2574. qdep, address, size);
  2575. ret = iommu_queue_command(iommu, &cmd);
  2576. if (ret != 0)
  2577. goto out;
  2578. }
  2579. /* Wait until all device TLBs are flushed */
  2580. domain_flush_complete(domain);
  2581. ret = 0;
  2582. out:
  2583. return ret;
  2584. }
  2585. static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid,
  2586. u64 address)
  2587. {
  2588. return __flush_pasid(domain, pasid, address, false);
  2589. }
  2590. int amd_iommu_flush_page(struct iommu_domain *dom, int pasid,
  2591. u64 address)
  2592. {
  2593. struct protection_domain *domain = dom->priv;
  2594. unsigned long flags;
  2595. int ret;
  2596. spin_lock_irqsave(&domain->lock, flags);
  2597. ret = __amd_iommu_flush_page(domain, pasid, address);
  2598. spin_unlock_irqrestore(&domain->lock, flags);
  2599. return ret;
  2600. }
  2601. EXPORT_SYMBOL(amd_iommu_flush_page);
  2602. static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid)
  2603. {
  2604. return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  2605. true);
  2606. }
  2607. int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid)
  2608. {
  2609. struct protection_domain *domain = dom->priv;
  2610. unsigned long flags;
  2611. int ret;
  2612. spin_lock_irqsave(&domain->lock, flags);
  2613. ret = __amd_iommu_flush_tlb(domain, pasid);
  2614. spin_unlock_irqrestore(&domain->lock, flags);
  2615. return ret;
  2616. }
  2617. EXPORT_SYMBOL(amd_iommu_flush_tlb);
  2618. static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc)
  2619. {
  2620. int index;
  2621. u64 *pte;
  2622. while (true) {
  2623. index = (pasid >> (9 * level)) & 0x1ff;
  2624. pte = &root[index];
  2625. if (level == 0)
  2626. break;
  2627. if (!(*pte & GCR3_VALID)) {
  2628. if (!alloc)
  2629. return NULL;
  2630. root = (void *)get_zeroed_page(GFP_ATOMIC);
  2631. if (root == NULL)
  2632. return NULL;
  2633. *pte = __pa(root) | GCR3_VALID;
  2634. }
  2635. root = __va(*pte & PAGE_MASK);
  2636. level -= 1;
  2637. }
  2638. return pte;
  2639. }
  2640. static int __set_gcr3(struct protection_domain *domain, int pasid,
  2641. unsigned long cr3)
  2642. {
  2643. u64 *pte;
  2644. if (domain->mode != PAGE_MODE_NONE)
  2645. return -EINVAL;
  2646. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true);
  2647. if (pte == NULL)
  2648. return -ENOMEM;
  2649. *pte = (cr3 & PAGE_MASK) | GCR3_VALID;
  2650. return __amd_iommu_flush_tlb(domain, pasid);
  2651. }
  2652. static int __clear_gcr3(struct protection_domain *domain, int pasid)
  2653. {
  2654. u64 *pte;
  2655. if (domain->mode != PAGE_MODE_NONE)
  2656. return -EINVAL;
  2657. pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false);
  2658. if (pte == NULL)
  2659. return 0;
  2660. *pte = 0;
  2661. return __amd_iommu_flush_tlb(domain, pasid);
  2662. }
  2663. int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid,
  2664. unsigned long cr3)
  2665. {
  2666. struct protection_domain *domain = dom->priv;
  2667. unsigned long flags;
  2668. int ret;
  2669. spin_lock_irqsave(&domain->lock, flags);
  2670. ret = __set_gcr3(domain, pasid, cr3);
  2671. spin_unlock_irqrestore(&domain->lock, flags);
  2672. return ret;
  2673. }
  2674. EXPORT_SYMBOL(amd_iommu_domain_set_gcr3);
  2675. int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid)
  2676. {
  2677. struct protection_domain *domain = dom->priv;
  2678. unsigned long flags;
  2679. int ret;
  2680. spin_lock_irqsave(&domain->lock, flags);
  2681. ret = __clear_gcr3(domain, pasid);
  2682. spin_unlock_irqrestore(&domain->lock, flags);
  2683. return ret;
  2684. }
  2685. EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3);
  2686. int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid,
  2687. int status, int tag)
  2688. {
  2689. struct iommu_dev_data *dev_data;
  2690. struct amd_iommu *iommu;
  2691. struct iommu_cmd cmd;
  2692. dev_data = get_dev_data(&pdev->dev);
  2693. iommu = amd_iommu_rlookup_table[dev_data->devid];
  2694. build_complete_ppr(&cmd, dev_data->devid, pasid, status,
  2695. tag, dev_data->pri_tlp);
  2696. return iommu_queue_command(iommu, &cmd);
  2697. }
  2698. EXPORT_SYMBOL(amd_iommu_complete_ppr);