amd_iommu.c 63 KB

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