amd_iommu.c 64 KB

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