amd_iommu.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. /*
  2. * Copyright (C) 2007-2008 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/gfp.h>
  21. #include <linux/bitops.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/iommu-helper.h>
  26. #include <linux/iommu.h>
  27. #include <asm/proto.h>
  28. #include <asm/iommu.h>
  29. #include <asm/gart.h>
  30. #include <asm/amd_iommu_types.h>
  31. #include <asm/amd_iommu.h>
  32. #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28))
  33. #define EXIT_LOOP_COUNT 10000000
  34. static DEFINE_RWLOCK(amd_iommu_devtable_lock);
  35. /* A list of preallocated protection domains */
  36. static LIST_HEAD(iommu_pd_list);
  37. static DEFINE_SPINLOCK(iommu_pd_list_lock);
  38. #ifdef CONFIG_IOMMU_API
  39. static struct iommu_ops amd_iommu_ops;
  40. #endif
  41. /*
  42. * general struct to manage commands send to an IOMMU
  43. */
  44. struct iommu_cmd {
  45. u32 data[4];
  46. };
  47. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  48. struct unity_map_entry *e);
  49. static struct dma_ops_domain *find_protection_domain(u16 devid);
  50. static u64* alloc_pte(struct protection_domain *dom,
  51. unsigned long address, u64
  52. **pte_page, gfp_t gfp);
  53. #ifdef CONFIG_AMD_IOMMU_STATS
  54. /*
  55. * Initialization code for statistics collection
  56. */
  57. DECLARE_STATS_COUNTER(compl_wait);
  58. DECLARE_STATS_COUNTER(cnt_map_single);
  59. DECLARE_STATS_COUNTER(cnt_unmap_single);
  60. DECLARE_STATS_COUNTER(cnt_map_sg);
  61. DECLARE_STATS_COUNTER(cnt_unmap_sg);
  62. DECLARE_STATS_COUNTER(cnt_alloc_coherent);
  63. DECLARE_STATS_COUNTER(cnt_free_coherent);
  64. DECLARE_STATS_COUNTER(cross_page);
  65. DECLARE_STATS_COUNTER(domain_flush_single);
  66. DECLARE_STATS_COUNTER(domain_flush_all);
  67. DECLARE_STATS_COUNTER(alloced_io_mem);
  68. DECLARE_STATS_COUNTER(total_map_requests);
  69. static struct dentry *stats_dir;
  70. static struct dentry *de_isolate;
  71. static struct dentry *de_fflush;
  72. static void amd_iommu_stats_add(struct __iommu_counter *cnt)
  73. {
  74. if (stats_dir == NULL)
  75. return;
  76. cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir,
  77. &cnt->value);
  78. }
  79. static void amd_iommu_stats_init(void)
  80. {
  81. stats_dir = debugfs_create_dir("amd-iommu", NULL);
  82. if (stats_dir == NULL)
  83. return;
  84. de_isolate = debugfs_create_bool("isolation", 0444, stats_dir,
  85. (u32 *)&amd_iommu_isolate);
  86. de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir,
  87. (u32 *)&amd_iommu_unmap_flush);
  88. amd_iommu_stats_add(&compl_wait);
  89. amd_iommu_stats_add(&cnt_map_single);
  90. amd_iommu_stats_add(&cnt_unmap_single);
  91. amd_iommu_stats_add(&cnt_map_sg);
  92. amd_iommu_stats_add(&cnt_unmap_sg);
  93. amd_iommu_stats_add(&cnt_alloc_coherent);
  94. amd_iommu_stats_add(&cnt_free_coherent);
  95. amd_iommu_stats_add(&cross_page);
  96. amd_iommu_stats_add(&domain_flush_single);
  97. amd_iommu_stats_add(&domain_flush_all);
  98. amd_iommu_stats_add(&alloced_io_mem);
  99. amd_iommu_stats_add(&total_map_requests);
  100. }
  101. #endif
  102. /* returns !0 if the IOMMU is caching non-present entries in its TLB */
  103. static int iommu_has_npcache(struct amd_iommu *iommu)
  104. {
  105. return iommu->cap & (1UL << IOMMU_CAP_NPCACHE);
  106. }
  107. /****************************************************************************
  108. *
  109. * Interrupt handling functions
  110. *
  111. ****************************************************************************/
  112. static void iommu_print_event(void *__evt)
  113. {
  114. u32 *event = __evt;
  115. int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  116. int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK;
  117. int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK;
  118. int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK;
  119. u64 address = (u64)(((u64)event[3]) << 32) | event[2];
  120. printk(KERN_ERR "AMD IOMMU: Event logged [");
  121. switch (type) {
  122. case EVENT_TYPE_ILL_DEV:
  123. printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x "
  124. "address=0x%016llx flags=0x%04x]\n",
  125. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  126. address, flags);
  127. break;
  128. case EVENT_TYPE_IO_FAULT:
  129. printk("IO_PAGE_FAULT device=%02x:%02x.%x "
  130. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  131. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  132. domid, address, flags);
  133. break;
  134. case EVENT_TYPE_DEV_TAB_ERR:
  135. printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  136. "address=0x%016llx flags=0x%04x]\n",
  137. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  138. address, flags);
  139. break;
  140. case EVENT_TYPE_PAGE_TAB_ERR:
  141. printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x "
  142. "domain=0x%04x address=0x%016llx flags=0x%04x]\n",
  143. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  144. domid, address, flags);
  145. break;
  146. case EVENT_TYPE_ILL_CMD:
  147. printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address);
  148. break;
  149. case EVENT_TYPE_CMD_HARD_ERR:
  150. printk("COMMAND_HARDWARE_ERROR address=0x%016llx "
  151. "flags=0x%04x]\n", address, flags);
  152. break;
  153. case EVENT_TYPE_IOTLB_INV_TO:
  154. printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x "
  155. "address=0x%016llx]\n",
  156. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  157. address);
  158. break;
  159. case EVENT_TYPE_INV_DEV_REQ:
  160. printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x "
  161. "address=0x%016llx flags=0x%04x]\n",
  162. PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid),
  163. address, flags);
  164. break;
  165. default:
  166. printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type);
  167. }
  168. }
  169. static void iommu_poll_events(struct amd_iommu *iommu)
  170. {
  171. u32 head, tail;
  172. unsigned long flags;
  173. spin_lock_irqsave(&iommu->lock, flags);
  174. head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  175. tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
  176. while (head != tail) {
  177. iommu_print_event(iommu->evt_buf + head);
  178. head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
  179. }
  180. writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
  181. spin_unlock_irqrestore(&iommu->lock, flags);
  182. }
  183. irqreturn_t amd_iommu_int_handler(int irq, void *data)
  184. {
  185. struct amd_iommu *iommu;
  186. list_for_each_entry(iommu, &amd_iommu_list, list)
  187. iommu_poll_events(iommu);
  188. return IRQ_HANDLED;
  189. }
  190. /****************************************************************************
  191. *
  192. * IOMMU command queuing functions
  193. *
  194. ****************************************************************************/
  195. /*
  196. * Writes the command to the IOMMUs command buffer and informs the
  197. * hardware about the new command. Must be called with iommu->lock held.
  198. */
  199. static int __iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  200. {
  201. u32 tail, head;
  202. u8 *target;
  203. tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  204. target = iommu->cmd_buf + tail;
  205. memcpy_toio(target, cmd, sizeof(*cmd));
  206. tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
  207. head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
  208. if (tail == head)
  209. return -ENOMEM;
  210. writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
  211. return 0;
  212. }
  213. /*
  214. * General queuing function for commands. Takes iommu->lock and calls
  215. * __iommu_queue_command().
  216. */
  217. static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
  218. {
  219. unsigned long flags;
  220. int ret;
  221. spin_lock_irqsave(&iommu->lock, flags);
  222. ret = __iommu_queue_command(iommu, cmd);
  223. if (!ret)
  224. iommu->need_sync = true;
  225. spin_unlock_irqrestore(&iommu->lock, flags);
  226. return ret;
  227. }
  228. /*
  229. * This function waits until an IOMMU has completed a completion
  230. * wait command
  231. */
  232. static void __iommu_wait_for_completion(struct amd_iommu *iommu)
  233. {
  234. int ready = 0;
  235. unsigned status = 0;
  236. unsigned long i = 0;
  237. INC_STATS_COUNTER(compl_wait);
  238. while (!ready && (i < EXIT_LOOP_COUNT)) {
  239. ++i;
  240. /* wait for the bit to become one */
  241. status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
  242. ready = status & MMIO_STATUS_COM_WAIT_INT_MASK;
  243. }
  244. /* set bit back to zero */
  245. status &= ~MMIO_STATUS_COM_WAIT_INT_MASK;
  246. writel(status, iommu->mmio_base + MMIO_STATUS_OFFSET);
  247. if (unlikely(i == EXIT_LOOP_COUNT))
  248. panic("AMD IOMMU: Completion wait loop failed\n");
  249. }
  250. /*
  251. * This function queues a completion wait command into the command
  252. * buffer of an IOMMU
  253. */
  254. static int __iommu_completion_wait(struct amd_iommu *iommu)
  255. {
  256. struct iommu_cmd cmd;
  257. memset(&cmd, 0, sizeof(cmd));
  258. cmd.data[0] = CMD_COMPL_WAIT_INT_MASK;
  259. CMD_SET_TYPE(&cmd, CMD_COMPL_WAIT);
  260. return __iommu_queue_command(iommu, &cmd);
  261. }
  262. /*
  263. * This function is called whenever we need to ensure that the IOMMU has
  264. * completed execution of all commands we sent. It sends a
  265. * COMPLETION_WAIT command and waits for it to finish. The IOMMU informs
  266. * us about that by writing a value to a physical address we pass with
  267. * the command.
  268. */
  269. static int iommu_completion_wait(struct amd_iommu *iommu)
  270. {
  271. int ret = 0;
  272. unsigned long flags;
  273. spin_lock_irqsave(&iommu->lock, flags);
  274. if (!iommu->need_sync)
  275. goto out;
  276. ret = __iommu_completion_wait(iommu);
  277. iommu->need_sync = false;
  278. if (ret)
  279. goto out;
  280. __iommu_wait_for_completion(iommu);
  281. out:
  282. spin_unlock_irqrestore(&iommu->lock, flags);
  283. return 0;
  284. }
  285. /*
  286. * Command send function for invalidating a device table entry
  287. */
  288. static int iommu_queue_inv_dev_entry(struct amd_iommu *iommu, u16 devid)
  289. {
  290. struct iommu_cmd cmd;
  291. int ret;
  292. BUG_ON(iommu == NULL);
  293. memset(&cmd, 0, sizeof(cmd));
  294. CMD_SET_TYPE(&cmd, CMD_INV_DEV_ENTRY);
  295. cmd.data[0] = devid;
  296. ret = iommu_queue_command(iommu, &cmd);
  297. return ret;
  298. }
  299. static void __iommu_build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,
  300. u16 domid, int pde, int s)
  301. {
  302. memset(cmd, 0, sizeof(*cmd));
  303. address &= PAGE_MASK;
  304. CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES);
  305. cmd->data[1] |= domid;
  306. cmd->data[2] = lower_32_bits(address);
  307. cmd->data[3] = upper_32_bits(address);
  308. if (s) /* size bit - we flush more than one 4kb page */
  309. cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK;
  310. if (pde) /* PDE bit - we wan't flush everything not only the PTEs */
  311. cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK;
  312. }
  313. /*
  314. * Generic command send function for invalidaing TLB entries
  315. */
  316. static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
  317. u64 address, u16 domid, int pde, int s)
  318. {
  319. struct iommu_cmd cmd;
  320. int ret;
  321. __iommu_build_inv_iommu_pages(&cmd, address, domid, pde, s);
  322. ret = iommu_queue_command(iommu, &cmd);
  323. return ret;
  324. }
  325. /*
  326. * TLB invalidation function which is called from the mapping functions.
  327. * It invalidates a single PTE if the range to flush is within a single
  328. * page. Otherwise it flushes the whole TLB of the IOMMU.
  329. */
  330. static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
  331. u64 address, size_t size)
  332. {
  333. int s = 0;
  334. unsigned pages = iommu_num_pages(address, size, PAGE_SIZE);
  335. address &= PAGE_MASK;
  336. if (pages > 1) {
  337. /*
  338. * If we have to flush more than one page, flush all
  339. * TLB entries for this domain
  340. */
  341. address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  342. s = 1;
  343. }
  344. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s);
  345. return 0;
  346. }
  347. /* Flush the whole IO/TLB for a given protection domain */
  348. static void iommu_flush_tlb(struct amd_iommu *iommu, u16 domid)
  349. {
  350. u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
  351. INC_STATS_COUNTER(domain_flush_single);
  352. iommu_queue_inv_iommu_pages(iommu, address, domid, 0, 1);
  353. }
  354. /*
  355. * This function is used to flush the IO/TLB for a given protection domain
  356. * on every IOMMU in the system
  357. */
  358. static void iommu_flush_domain(u16 domid)
  359. {
  360. unsigned long flags;
  361. struct amd_iommu *iommu;
  362. struct iommu_cmd cmd;
  363. INC_STATS_COUNTER(domain_flush_all);
  364. __iommu_build_inv_iommu_pages(&cmd, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
  365. domid, 1, 1);
  366. list_for_each_entry(iommu, &amd_iommu_list, list) {
  367. spin_lock_irqsave(&iommu->lock, flags);
  368. __iommu_queue_command(iommu, &cmd);
  369. __iommu_completion_wait(iommu);
  370. __iommu_wait_for_completion(iommu);
  371. spin_unlock_irqrestore(&iommu->lock, flags);
  372. }
  373. }
  374. /****************************************************************************
  375. *
  376. * The functions below are used the create the page table mappings for
  377. * unity mapped regions.
  378. *
  379. ****************************************************************************/
  380. /*
  381. * Generic mapping functions. It maps a physical address into a DMA
  382. * address space. It allocates the page table pages if necessary.
  383. * In the future it can be extended to a generic mapping function
  384. * supporting all features of AMD IOMMU page tables like level skipping
  385. * and full 64 bit address spaces.
  386. */
  387. static int iommu_map_page(struct protection_domain *dom,
  388. unsigned long bus_addr,
  389. unsigned long phys_addr,
  390. int prot)
  391. {
  392. u64 __pte, *pte;
  393. bus_addr = PAGE_ALIGN(bus_addr);
  394. phys_addr = PAGE_ALIGN(phys_addr);
  395. /* only support 512GB address spaces for now */
  396. if (bus_addr > IOMMU_MAP_SIZE_L3 || !(prot & IOMMU_PROT_MASK))
  397. return -EINVAL;
  398. pte = alloc_pte(dom, bus_addr, NULL, GFP_KERNEL);
  399. if (IOMMU_PTE_PRESENT(*pte))
  400. return -EBUSY;
  401. __pte = phys_addr | IOMMU_PTE_P;
  402. if (prot & IOMMU_PROT_IR)
  403. __pte |= IOMMU_PTE_IR;
  404. if (prot & IOMMU_PROT_IW)
  405. __pte |= IOMMU_PTE_IW;
  406. *pte = __pte;
  407. return 0;
  408. }
  409. static void iommu_unmap_page(struct protection_domain *dom,
  410. unsigned long bus_addr)
  411. {
  412. u64 *pte;
  413. pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(bus_addr)];
  414. if (!IOMMU_PTE_PRESENT(*pte))
  415. return;
  416. pte = IOMMU_PTE_PAGE(*pte);
  417. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  418. if (!IOMMU_PTE_PRESENT(*pte))
  419. return;
  420. pte = IOMMU_PTE_PAGE(*pte);
  421. pte = &pte[IOMMU_PTE_L1_INDEX(bus_addr)];
  422. *pte = 0;
  423. }
  424. /*
  425. * This function checks if a specific unity mapping entry is needed for
  426. * this specific IOMMU.
  427. */
  428. static int iommu_for_unity_map(struct amd_iommu *iommu,
  429. struct unity_map_entry *entry)
  430. {
  431. u16 bdf, i;
  432. for (i = entry->devid_start; i <= entry->devid_end; ++i) {
  433. bdf = amd_iommu_alias_table[i];
  434. if (amd_iommu_rlookup_table[bdf] == iommu)
  435. return 1;
  436. }
  437. return 0;
  438. }
  439. /*
  440. * Init the unity mappings for a specific IOMMU in the system
  441. *
  442. * Basically iterates over all unity mapping entries and applies them to
  443. * the default domain DMA of that IOMMU if necessary.
  444. */
  445. static int iommu_init_unity_mappings(struct amd_iommu *iommu)
  446. {
  447. struct unity_map_entry *entry;
  448. int ret;
  449. list_for_each_entry(entry, &amd_iommu_unity_map, list) {
  450. if (!iommu_for_unity_map(iommu, entry))
  451. continue;
  452. ret = dma_ops_unity_map(iommu->default_dom, entry);
  453. if (ret)
  454. return ret;
  455. }
  456. return 0;
  457. }
  458. /*
  459. * This function actually applies the mapping to the page table of the
  460. * dma_ops domain.
  461. */
  462. static int dma_ops_unity_map(struct dma_ops_domain *dma_dom,
  463. struct unity_map_entry *e)
  464. {
  465. u64 addr;
  466. int ret;
  467. for (addr = e->address_start; addr < e->address_end;
  468. addr += PAGE_SIZE) {
  469. ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot);
  470. if (ret)
  471. return ret;
  472. /*
  473. * if unity mapping is in aperture range mark the page
  474. * as allocated in the aperture
  475. */
  476. if (addr < dma_dom->aperture_size)
  477. __set_bit(addr >> PAGE_SHIFT,
  478. dma_dom->aperture.bitmap);
  479. }
  480. return 0;
  481. }
  482. /*
  483. * Inits the unity mappings required for a specific device
  484. */
  485. static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom,
  486. u16 devid)
  487. {
  488. struct unity_map_entry *e;
  489. int ret;
  490. list_for_each_entry(e, &amd_iommu_unity_map, list) {
  491. if (!(devid >= e->devid_start && devid <= e->devid_end))
  492. continue;
  493. ret = dma_ops_unity_map(dma_dom, e);
  494. if (ret)
  495. return ret;
  496. }
  497. return 0;
  498. }
  499. /****************************************************************************
  500. *
  501. * The next functions belong to the address allocator for the dma_ops
  502. * interface functions. They work like the allocators in the other IOMMU
  503. * drivers. Its basically a bitmap which marks the allocated pages in
  504. * the aperture. Maybe it could be enhanced in the future to a more
  505. * efficient allocator.
  506. *
  507. ****************************************************************************/
  508. /*
  509. * The address allocator core function.
  510. *
  511. * called with domain->lock held
  512. */
  513. static unsigned long dma_ops_alloc_addresses(struct device *dev,
  514. struct dma_ops_domain *dom,
  515. unsigned int pages,
  516. unsigned long align_mask,
  517. u64 dma_mask)
  518. {
  519. unsigned long limit;
  520. unsigned long address;
  521. unsigned long boundary_size;
  522. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  523. PAGE_SIZE) >> PAGE_SHIFT;
  524. limit = iommu_device_max_index(dom->aperture_size >> PAGE_SHIFT, 0,
  525. dma_mask >> PAGE_SHIFT);
  526. if (dom->next_bit >= limit) {
  527. dom->next_bit = 0;
  528. dom->need_flush = true;
  529. }
  530. address = iommu_area_alloc(dom->aperture.bitmap, limit, dom->next_bit,
  531. pages, 0 , boundary_size, align_mask);
  532. if (address == -1) {
  533. address = iommu_area_alloc(dom->aperture.bitmap, limit, 0,
  534. pages, 0, boundary_size,
  535. align_mask);
  536. dom->need_flush = true;
  537. }
  538. if (likely(address != -1)) {
  539. dom->next_bit = address + pages;
  540. address <<= PAGE_SHIFT;
  541. } else
  542. address = bad_dma_address;
  543. WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
  544. return address;
  545. }
  546. /*
  547. * The address free function.
  548. *
  549. * called with domain->lock held
  550. */
  551. static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  552. unsigned long address,
  553. unsigned int pages)
  554. {
  555. address >>= PAGE_SHIFT;
  556. iommu_area_free(dom->aperture.bitmap, address, pages);
  557. if (address >= dom->next_bit)
  558. dom->need_flush = true;
  559. }
  560. /****************************************************************************
  561. *
  562. * The next functions belong to the domain allocation. A domain is
  563. * allocated for every IOMMU as the default domain. If device isolation
  564. * is enabled, every device get its own domain. The most important thing
  565. * about domains is the page table mapping the DMA address space they
  566. * contain.
  567. *
  568. ****************************************************************************/
  569. static u16 domain_id_alloc(void)
  570. {
  571. unsigned long flags;
  572. int id;
  573. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  574. id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID);
  575. BUG_ON(id == 0);
  576. if (id > 0 && id < MAX_DOMAIN_ID)
  577. __set_bit(id, amd_iommu_pd_alloc_bitmap);
  578. else
  579. id = 0;
  580. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  581. return id;
  582. }
  583. static void domain_id_free(int id)
  584. {
  585. unsigned long flags;
  586. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  587. if (id > 0 && id < MAX_DOMAIN_ID)
  588. __clear_bit(id, amd_iommu_pd_alloc_bitmap);
  589. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  590. }
  591. /*
  592. * Used to reserve address ranges in the aperture (e.g. for exclusion
  593. * ranges.
  594. */
  595. static void dma_ops_reserve_addresses(struct dma_ops_domain *dom,
  596. unsigned long start_page,
  597. unsigned int pages)
  598. {
  599. unsigned int last_page = dom->aperture_size >> PAGE_SHIFT;
  600. if (start_page + pages > last_page)
  601. pages = last_page - start_page;
  602. iommu_area_reserve(dom->aperture.bitmap, start_page, pages);
  603. }
  604. static void free_pagetable(struct protection_domain *domain)
  605. {
  606. int i, j;
  607. u64 *p1, *p2, *p3;
  608. p1 = domain->pt_root;
  609. if (!p1)
  610. return;
  611. for (i = 0; i < 512; ++i) {
  612. if (!IOMMU_PTE_PRESENT(p1[i]))
  613. continue;
  614. p2 = IOMMU_PTE_PAGE(p1[i]);
  615. for (j = 0; j < 512; ++j) {
  616. if (!IOMMU_PTE_PRESENT(p2[j]))
  617. continue;
  618. p3 = IOMMU_PTE_PAGE(p2[j]);
  619. free_page((unsigned long)p3);
  620. }
  621. free_page((unsigned long)p2);
  622. }
  623. free_page((unsigned long)p1);
  624. domain->pt_root = NULL;
  625. }
  626. /*
  627. * Free a domain, only used if something went wrong in the
  628. * allocation path and we need to free an already allocated page table
  629. */
  630. static void dma_ops_domain_free(struct dma_ops_domain *dom)
  631. {
  632. if (!dom)
  633. return;
  634. free_pagetable(&dom->domain);
  635. free_page((unsigned long)dom->aperture.bitmap);
  636. kfree(dom);
  637. }
  638. /*
  639. * Allocates a new protection domain usable for the dma_ops functions.
  640. * It also intializes the page table and the address allocator data
  641. * structures required for the dma_ops interface
  642. */
  643. static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu,
  644. unsigned order)
  645. {
  646. struct dma_ops_domain *dma_dom;
  647. unsigned i, num_pte_pages;
  648. u64 *l2_pde;
  649. u64 address;
  650. /*
  651. * Currently the DMA aperture must be between 32 MB and 1GB in size
  652. */
  653. if ((order < 25) || (order > 30))
  654. return NULL;
  655. dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL);
  656. if (!dma_dom)
  657. return NULL;
  658. spin_lock_init(&dma_dom->domain.lock);
  659. dma_dom->domain.id = domain_id_alloc();
  660. if (dma_dom->domain.id == 0)
  661. goto free_dma_dom;
  662. dma_dom->domain.mode = PAGE_MODE_3_LEVEL;
  663. dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  664. dma_dom->domain.flags = PD_DMA_OPS_MASK;
  665. dma_dom->domain.priv = dma_dom;
  666. if (!dma_dom->domain.pt_root)
  667. goto free_dma_dom;
  668. dma_dom->aperture_size = APERTURE_RANGE_SIZE;
  669. dma_dom->aperture.bitmap = (void *)get_zeroed_page(GFP_KERNEL);
  670. if (!dma_dom->aperture.bitmap)
  671. goto free_dma_dom;
  672. /*
  673. * mark the first page as allocated so we never return 0 as
  674. * a valid dma-address. So we can use 0 as error value
  675. */
  676. dma_dom->aperture.bitmap[0] = 1;
  677. dma_dom->next_bit = 0;
  678. dma_dom->need_flush = false;
  679. dma_dom->target_dev = 0xffff;
  680. /* Intialize the exclusion range if necessary */
  681. if (iommu->exclusion_start &&
  682. iommu->exclusion_start < dma_dom->aperture_size) {
  683. unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
  684. int pages = iommu_num_pages(iommu->exclusion_start,
  685. iommu->exclusion_length,
  686. PAGE_SIZE);
  687. dma_ops_reserve_addresses(dma_dom, startpage, pages);
  688. }
  689. /*
  690. * At the last step, build the page tables so we don't need to
  691. * allocate page table pages in the dma_ops mapping/unmapping
  692. * path for the first 128MB of dma address space.
  693. */
  694. num_pte_pages = dma_dom->aperture_size / (PAGE_SIZE * 512);
  695. l2_pde = (u64 *)get_zeroed_page(GFP_KERNEL);
  696. if (l2_pde == NULL)
  697. goto free_dma_dom;
  698. dma_dom->domain.pt_root[0] = IOMMU_L2_PDE(virt_to_phys(l2_pde));
  699. for (i = 0; i < num_pte_pages; ++i) {
  700. u64 **pte_page = &dma_dom->aperture.pte_pages[i];
  701. *pte_page = (u64 *)get_zeroed_page(GFP_KERNEL);
  702. if (!*pte_page)
  703. goto free_dma_dom;
  704. address = virt_to_phys(*pte_page);
  705. l2_pde[i] = IOMMU_L1_PDE(address);
  706. }
  707. return dma_dom;
  708. free_dma_dom:
  709. dma_ops_domain_free(dma_dom);
  710. return NULL;
  711. }
  712. /*
  713. * little helper function to check whether a given protection domain is a
  714. * dma_ops domain
  715. */
  716. static bool dma_ops_domain(struct protection_domain *domain)
  717. {
  718. return domain->flags & PD_DMA_OPS_MASK;
  719. }
  720. /*
  721. * Find out the protection domain structure for a given PCI device. This
  722. * will give us the pointer to the page table root for example.
  723. */
  724. static struct protection_domain *domain_for_device(u16 devid)
  725. {
  726. struct protection_domain *dom;
  727. unsigned long flags;
  728. read_lock_irqsave(&amd_iommu_devtable_lock, flags);
  729. dom = amd_iommu_pd_table[devid];
  730. read_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  731. return dom;
  732. }
  733. /*
  734. * If a device is not yet associated with a domain, this function does
  735. * assigns it visible for the hardware
  736. */
  737. static void attach_device(struct amd_iommu *iommu,
  738. struct protection_domain *domain,
  739. u16 devid)
  740. {
  741. unsigned long flags;
  742. u64 pte_root = virt_to_phys(domain->pt_root);
  743. domain->dev_cnt += 1;
  744. pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
  745. << DEV_ENTRY_MODE_SHIFT;
  746. pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
  747. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  748. amd_iommu_dev_table[devid].data[0] = lower_32_bits(pte_root);
  749. amd_iommu_dev_table[devid].data[1] = upper_32_bits(pte_root);
  750. amd_iommu_dev_table[devid].data[2] = domain->id;
  751. amd_iommu_pd_table[devid] = domain;
  752. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  753. iommu_queue_inv_dev_entry(iommu, devid);
  754. }
  755. /*
  756. * Removes a device from a protection domain (unlocked)
  757. */
  758. static void __detach_device(struct protection_domain *domain, u16 devid)
  759. {
  760. /* lock domain */
  761. spin_lock(&domain->lock);
  762. /* remove domain from the lookup table */
  763. amd_iommu_pd_table[devid] = NULL;
  764. /* remove entry from the device table seen by the hardware */
  765. amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
  766. amd_iommu_dev_table[devid].data[1] = 0;
  767. amd_iommu_dev_table[devid].data[2] = 0;
  768. /* decrease reference counter */
  769. domain->dev_cnt -= 1;
  770. /* ready */
  771. spin_unlock(&domain->lock);
  772. }
  773. /*
  774. * Removes a device from a protection domain (with devtable_lock held)
  775. */
  776. static void detach_device(struct protection_domain *domain, u16 devid)
  777. {
  778. unsigned long flags;
  779. /* lock device table */
  780. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  781. __detach_device(domain, devid);
  782. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  783. }
  784. static int device_change_notifier(struct notifier_block *nb,
  785. unsigned long action, void *data)
  786. {
  787. struct device *dev = data;
  788. struct pci_dev *pdev = to_pci_dev(dev);
  789. u16 devid = calc_devid(pdev->bus->number, pdev->devfn);
  790. struct protection_domain *domain;
  791. struct dma_ops_domain *dma_domain;
  792. struct amd_iommu *iommu;
  793. int order = amd_iommu_aperture_order;
  794. unsigned long flags;
  795. if (devid > amd_iommu_last_bdf)
  796. goto out;
  797. devid = amd_iommu_alias_table[devid];
  798. iommu = amd_iommu_rlookup_table[devid];
  799. if (iommu == NULL)
  800. goto out;
  801. domain = domain_for_device(devid);
  802. if (domain && !dma_ops_domain(domain))
  803. WARN_ONCE(1, "AMD IOMMU WARNING: device %s already bound "
  804. "to a non-dma-ops domain\n", dev_name(dev));
  805. switch (action) {
  806. case BUS_NOTIFY_BOUND_DRIVER:
  807. if (domain)
  808. goto out;
  809. dma_domain = find_protection_domain(devid);
  810. if (!dma_domain)
  811. dma_domain = iommu->default_dom;
  812. attach_device(iommu, &dma_domain->domain, devid);
  813. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  814. "device %s\n", dma_domain->domain.id, dev_name(dev));
  815. break;
  816. case BUS_NOTIFY_UNBIND_DRIVER:
  817. if (!domain)
  818. goto out;
  819. detach_device(domain, devid);
  820. break;
  821. case BUS_NOTIFY_ADD_DEVICE:
  822. /* allocate a protection domain if a device is added */
  823. dma_domain = find_protection_domain(devid);
  824. if (dma_domain)
  825. goto out;
  826. dma_domain = dma_ops_domain_alloc(iommu, order);
  827. if (!dma_domain)
  828. goto out;
  829. dma_domain->target_dev = devid;
  830. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  831. list_add_tail(&dma_domain->list, &iommu_pd_list);
  832. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  833. break;
  834. default:
  835. goto out;
  836. }
  837. iommu_queue_inv_dev_entry(iommu, devid);
  838. iommu_completion_wait(iommu);
  839. out:
  840. return 0;
  841. }
  842. struct notifier_block device_nb = {
  843. .notifier_call = device_change_notifier,
  844. };
  845. /*****************************************************************************
  846. *
  847. * The next functions belong to the dma_ops mapping/unmapping code.
  848. *
  849. *****************************************************************************/
  850. /*
  851. * This function checks if the driver got a valid device from the caller to
  852. * avoid dereferencing invalid pointers.
  853. */
  854. static bool check_device(struct device *dev)
  855. {
  856. if (!dev || !dev->dma_mask)
  857. return false;
  858. return true;
  859. }
  860. /*
  861. * In this function the list of preallocated protection domains is traversed to
  862. * find the domain for a specific device
  863. */
  864. static struct dma_ops_domain *find_protection_domain(u16 devid)
  865. {
  866. struct dma_ops_domain *entry, *ret = NULL;
  867. unsigned long flags;
  868. if (list_empty(&iommu_pd_list))
  869. return NULL;
  870. spin_lock_irqsave(&iommu_pd_list_lock, flags);
  871. list_for_each_entry(entry, &iommu_pd_list, list) {
  872. if (entry->target_dev == devid) {
  873. ret = entry;
  874. break;
  875. }
  876. }
  877. spin_unlock_irqrestore(&iommu_pd_list_lock, flags);
  878. return ret;
  879. }
  880. /*
  881. * In the dma_ops path we only have the struct device. This function
  882. * finds the corresponding IOMMU, the protection domain and the
  883. * requestor id for a given device.
  884. * If the device is not yet associated with a domain this is also done
  885. * in this function.
  886. */
  887. static int get_device_resources(struct device *dev,
  888. struct amd_iommu **iommu,
  889. struct protection_domain **domain,
  890. u16 *bdf)
  891. {
  892. struct dma_ops_domain *dma_dom;
  893. struct pci_dev *pcidev;
  894. u16 _bdf;
  895. *iommu = NULL;
  896. *domain = NULL;
  897. *bdf = 0xffff;
  898. if (dev->bus != &pci_bus_type)
  899. return 0;
  900. pcidev = to_pci_dev(dev);
  901. _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  902. /* device not translated by any IOMMU in the system? */
  903. if (_bdf > amd_iommu_last_bdf)
  904. return 0;
  905. *bdf = amd_iommu_alias_table[_bdf];
  906. *iommu = amd_iommu_rlookup_table[*bdf];
  907. if (*iommu == NULL)
  908. return 0;
  909. *domain = domain_for_device(*bdf);
  910. if (*domain == NULL) {
  911. dma_dom = find_protection_domain(*bdf);
  912. if (!dma_dom)
  913. dma_dom = (*iommu)->default_dom;
  914. *domain = &dma_dom->domain;
  915. attach_device(*iommu, *domain, *bdf);
  916. printk(KERN_INFO "AMD IOMMU: Using protection domain %d for "
  917. "device %s\n", (*domain)->id, dev_name(dev));
  918. }
  919. if (domain_for_device(_bdf) == NULL)
  920. attach_device(*iommu, *domain, _bdf);
  921. return 1;
  922. }
  923. /*
  924. * If the pte_page is not yet allocated this function is called
  925. */
  926. static u64* alloc_pte(struct protection_domain *dom,
  927. unsigned long address, u64 **pte_page, gfp_t gfp)
  928. {
  929. u64 *pte, *page;
  930. pte = &dom->pt_root[IOMMU_PTE_L2_INDEX(address)];
  931. if (!IOMMU_PTE_PRESENT(*pte)) {
  932. page = (u64 *)get_zeroed_page(gfp);
  933. if (!page)
  934. return NULL;
  935. *pte = IOMMU_L2_PDE(virt_to_phys(page));
  936. }
  937. pte = IOMMU_PTE_PAGE(*pte);
  938. pte = &pte[IOMMU_PTE_L1_INDEX(address)];
  939. if (!IOMMU_PTE_PRESENT(*pte)) {
  940. page = (u64 *)get_zeroed_page(gfp);
  941. if (!page)
  942. return NULL;
  943. *pte = IOMMU_L1_PDE(virt_to_phys(page));
  944. }
  945. pte = IOMMU_PTE_PAGE(*pte);
  946. if (pte_page)
  947. *pte_page = pte;
  948. pte = &pte[IOMMU_PTE_L0_INDEX(address)];
  949. return pte;
  950. }
  951. /*
  952. * This function fetches the PTE for a given address in the aperture
  953. */
  954. static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
  955. unsigned long address)
  956. {
  957. struct aperture_range *aperture = &dom->aperture;
  958. u64 *pte, *pte_page;
  959. pte = aperture->pte_pages[IOMMU_PTE_L1_INDEX(address)];
  960. if (!pte) {
  961. pte = alloc_pte(&dom->domain, address, &pte_page, GFP_ATOMIC);
  962. aperture->pte_pages[IOMMU_PTE_L1_INDEX(address)] = pte_page;
  963. }
  964. return pte;
  965. }
  966. /*
  967. * This is the generic map function. It maps one 4kb page at paddr to
  968. * the given address in the DMA address space for the domain.
  969. */
  970. static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
  971. struct dma_ops_domain *dom,
  972. unsigned long address,
  973. phys_addr_t paddr,
  974. int direction)
  975. {
  976. u64 *pte, __pte;
  977. WARN_ON(address > dom->aperture_size);
  978. paddr &= PAGE_MASK;
  979. pte = dma_ops_get_pte(dom, address);
  980. if (!pte)
  981. return bad_dma_address;
  982. __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
  983. if (direction == DMA_TO_DEVICE)
  984. __pte |= IOMMU_PTE_IR;
  985. else if (direction == DMA_FROM_DEVICE)
  986. __pte |= IOMMU_PTE_IW;
  987. else if (direction == DMA_BIDIRECTIONAL)
  988. __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW;
  989. WARN_ON(*pte);
  990. *pte = __pte;
  991. return (dma_addr_t)address;
  992. }
  993. /*
  994. * The generic unmapping function for on page in the DMA address space.
  995. */
  996. static void dma_ops_domain_unmap(struct amd_iommu *iommu,
  997. struct dma_ops_domain *dom,
  998. unsigned long address)
  999. {
  1000. u64 *pte;
  1001. if (address >= dom->aperture_size)
  1002. return;
  1003. WARN_ON(address & ~PAGE_MASK || address >= dom->aperture_size);
  1004. pte = dom->aperture.pte_pages[IOMMU_PTE_L1_INDEX(address)];
  1005. pte += IOMMU_PTE_L0_INDEX(address);
  1006. WARN_ON(!*pte);
  1007. *pte = 0ULL;
  1008. }
  1009. /*
  1010. * This function contains common code for mapping of a physically
  1011. * contiguous memory region into DMA address space. It is used by all
  1012. * mapping functions provided with this IOMMU driver.
  1013. * Must be called with the domain lock held.
  1014. */
  1015. static dma_addr_t __map_single(struct device *dev,
  1016. struct amd_iommu *iommu,
  1017. struct dma_ops_domain *dma_dom,
  1018. phys_addr_t paddr,
  1019. size_t size,
  1020. int dir,
  1021. bool align,
  1022. u64 dma_mask)
  1023. {
  1024. dma_addr_t offset = paddr & ~PAGE_MASK;
  1025. dma_addr_t address, start, ret;
  1026. unsigned int pages;
  1027. unsigned long align_mask = 0;
  1028. int i;
  1029. pages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1030. paddr &= PAGE_MASK;
  1031. INC_STATS_COUNTER(total_map_requests);
  1032. if (pages > 1)
  1033. INC_STATS_COUNTER(cross_page);
  1034. if (align)
  1035. align_mask = (1UL << get_order(size)) - 1;
  1036. address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
  1037. dma_mask);
  1038. if (unlikely(address == bad_dma_address))
  1039. goto out;
  1040. start = address;
  1041. for (i = 0; i < pages; ++i) {
  1042. ret = dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
  1043. if (ret == bad_dma_address)
  1044. goto out_unmap;
  1045. paddr += PAGE_SIZE;
  1046. start += PAGE_SIZE;
  1047. }
  1048. address += offset;
  1049. ADD_STATS_COUNTER(alloced_io_mem, size);
  1050. if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
  1051. iommu_flush_tlb(iommu, dma_dom->domain.id);
  1052. dma_dom->need_flush = false;
  1053. } else if (unlikely(iommu_has_npcache(iommu)))
  1054. iommu_flush_pages(iommu, dma_dom->domain.id, address, size);
  1055. out:
  1056. return address;
  1057. out_unmap:
  1058. for (--i; i >= 0; --i) {
  1059. start -= PAGE_SIZE;
  1060. dma_ops_domain_unmap(iommu, dma_dom, start);
  1061. }
  1062. dma_ops_free_addresses(dma_dom, address, pages);
  1063. return bad_dma_address;
  1064. }
  1065. /*
  1066. * Does the reverse of the __map_single function. Must be called with
  1067. * the domain lock held too
  1068. */
  1069. static void __unmap_single(struct amd_iommu *iommu,
  1070. struct dma_ops_domain *dma_dom,
  1071. dma_addr_t dma_addr,
  1072. size_t size,
  1073. int dir)
  1074. {
  1075. dma_addr_t i, start;
  1076. unsigned int pages;
  1077. if ((dma_addr == bad_dma_address) ||
  1078. (dma_addr + size > dma_dom->aperture_size))
  1079. return;
  1080. pages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  1081. dma_addr &= PAGE_MASK;
  1082. start = dma_addr;
  1083. for (i = 0; i < pages; ++i) {
  1084. dma_ops_domain_unmap(iommu, dma_dom, start);
  1085. start += PAGE_SIZE;
  1086. }
  1087. SUB_STATS_COUNTER(alloced_io_mem, size);
  1088. dma_ops_free_addresses(dma_dom, dma_addr, pages);
  1089. if (amd_iommu_unmap_flush || dma_dom->need_flush) {
  1090. iommu_flush_pages(iommu, dma_dom->domain.id, dma_addr, size);
  1091. dma_dom->need_flush = false;
  1092. }
  1093. }
  1094. /*
  1095. * The exported map_single function for dma_ops.
  1096. */
  1097. static dma_addr_t map_page(struct device *dev, struct page *page,
  1098. unsigned long offset, size_t size,
  1099. enum dma_data_direction dir,
  1100. struct dma_attrs *attrs)
  1101. {
  1102. unsigned long flags;
  1103. struct amd_iommu *iommu;
  1104. struct protection_domain *domain;
  1105. u16 devid;
  1106. dma_addr_t addr;
  1107. u64 dma_mask;
  1108. phys_addr_t paddr = page_to_phys(page) + offset;
  1109. INC_STATS_COUNTER(cnt_map_single);
  1110. if (!check_device(dev))
  1111. return bad_dma_address;
  1112. dma_mask = *dev->dma_mask;
  1113. get_device_resources(dev, &iommu, &domain, &devid);
  1114. if (iommu == NULL || domain == NULL)
  1115. /* device not handled by any AMD IOMMU */
  1116. return (dma_addr_t)paddr;
  1117. if (!dma_ops_domain(domain))
  1118. return bad_dma_address;
  1119. spin_lock_irqsave(&domain->lock, flags);
  1120. addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false,
  1121. dma_mask);
  1122. if (addr == bad_dma_address)
  1123. goto out;
  1124. iommu_completion_wait(iommu);
  1125. out:
  1126. spin_unlock_irqrestore(&domain->lock, flags);
  1127. return addr;
  1128. }
  1129. /*
  1130. * The exported unmap_single function for dma_ops.
  1131. */
  1132. static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
  1133. enum dma_data_direction dir, struct dma_attrs *attrs)
  1134. {
  1135. unsigned long flags;
  1136. struct amd_iommu *iommu;
  1137. struct protection_domain *domain;
  1138. u16 devid;
  1139. INC_STATS_COUNTER(cnt_unmap_single);
  1140. if (!check_device(dev) ||
  1141. !get_device_resources(dev, &iommu, &domain, &devid))
  1142. /* device not handled by any AMD IOMMU */
  1143. return;
  1144. if (!dma_ops_domain(domain))
  1145. return;
  1146. spin_lock_irqsave(&domain->lock, flags);
  1147. __unmap_single(iommu, domain->priv, dma_addr, size, dir);
  1148. iommu_completion_wait(iommu);
  1149. spin_unlock_irqrestore(&domain->lock, flags);
  1150. }
  1151. /*
  1152. * This is a special map_sg function which is used if we should map a
  1153. * device which is not handled by an AMD IOMMU in the system.
  1154. */
  1155. static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist,
  1156. int nelems, int dir)
  1157. {
  1158. struct scatterlist *s;
  1159. int i;
  1160. for_each_sg(sglist, s, nelems, i) {
  1161. s->dma_address = (dma_addr_t)sg_phys(s);
  1162. s->dma_length = s->length;
  1163. }
  1164. return nelems;
  1165. }
  1166. /*
  1167. * The exported map_sg function for dma_ops (handles scatter-gather
  1168. * lists).
  1169. */
  1170. static int map_sg(struct device *dev, struct scatterlist *sglist,
  1171. int nelems, enum dma_data_direction dir,
  1172. struct dma_attrs *attrs)
  1173. {
  1174. unsigned long flags;
  1175. struct amd_iommu *iommu;
  1176. struct protection_domain *domain;
  1177. u16 devid;
  1178. int i;
  1179. struct scatterlist *s;
  1180. phys_addr_t paddr;
  1181. int mapped_elems = 0;
  1182. u64 dma_mask;
  1183. INC_STATS_COUNTER(cnt_map_sg);
  1184. if (!check_device(dev))
  1185. return 0;
  1186. dma_mask = *dev->dma_mask;
  1187. get_device_resources(dev, &iommu, &domain, &devid);
  1188. if (!iommu || !domain)
  1189. return map_sg_no_iommu(dev, sglist, nelems, dir);
  1190. if (!dma_ops_domain(domain))
  1191. return 0;
  1192. spin_lock_irqsave(&domain->lock, flags);
  1193. for_each_sg(sglist, s, nelems, i) {
  1194. paddr = sg_phys(s);
  1195. s->dma_address = __map_single(dev, iommu, domain->priv,
  1196. paddr, s->length, dir, false,
  1197. dma_mask);
  1198. if (s->dma_address) {
  1199. s->dma_length = s->length;
  1200. mapped_elems++;
  1201. } else
  1202. goto unmap;
  1203. }
  1204. iommu_completion_wait(iommu);
  1205. out:
  1206. spin_unlock_irqrestore(&domain->lock, flags);
  1207. return mapped_elems;
  1208. unmap:
  1209. for_each_sg(sglist, s, mapped_elems, i) {
  1210. if (s->dma_address)
  1211. __unmap_single(iommu, domain->priv, s->dma_address,
  1212. s->dma_length, dir);
  1213. s->dma_address = s->dma_length = 0;
  1214. }
  1215. mapped_elems = 0;
  1216. goto out;
  1217. }
  1218. /*
  1219. * The exported map_sg function for dma_ops (handles scatter-gather
  1220. * lists).
  1221. */
  1222. static void unmap_sg(struct device *dev, struct scatterlist *sglist,
  1223. int nelems, enum dma_data_direction dir,
  1224. struct dma_attrs *attrs)
  1225. {
  1226. unsigned long flags;
  1227. struct amd_iommu *iommu;
  1228. struct protection_domain *domain;
  1229. struct scatterlist *s;
  1230. u16 devid;
  1231. int i;
  1232. INC_STATS_COUNTER(cnt_unmap_sg);
  1233. if (!check_device(dev) ||
  1234. !get_device_resources(dev, &iommu, &domain, &devid))
  1235. return;
  1236. if (!dma_ops_domain(domain))
  1237. return;
  1238. spin_lock_irqsave(&domain->lock, flags);
  1239. for_each_sg(sglist, s, nelems, i) {
  1240. __unmap_single(iommu, domain->priv, s->dma_address,
  1241. s->dma_length, dir);
  1242. s->dma_address = s->dma_length = 0;
  1243. }
  1244. iommu_completion_wait(iommu);
  1245. spin_unlock_irqrestore(&domain->lock, flags);
  1246. }
  1247. /*
  1248. * The exported alloc_coherent function for dma_ops.
  1249. */
  1250. static void *alloc_coherent(struct device *dev, size_t size,
  1251. dma_addr_t *dma_addr, gfp_t flag)
  1252. {
  1253. unsigned long flags;
  1254. void *virt_addr;
  1255. struct amd_iommu *iommu;
  1256. struct protection_domain *domain;
  1257. u16 devid;
  1258. phys_addr_t paddr;
  1259. u64 dma_mask = dev->coherent_dma_mask;
  1260. INC_STATS_COUNTER(cnt_alloc_coherent);
  1261. if (!check_device(dev))
  1262. return NULL;
  1263. if (!get_device_resources(dev, &iommu, &domain, &devid))
  1264. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  1265. flag |= __GFP_ZERO;
  1266. virt_addr = (void *)__get_free_pages(flag, get_order(size));
  1267. if (!virt_addr)
  1268. return 0;
  1269. paddr = virt_to_phys(virt_addr);
  1270. if (!iommu || !domain) {
  1271. *dma_addr = (dma_addr_t)paddr;
  1272. return virt_addr;
  1273. }
  1274. if (!dma_ops_domain(domain))
  1275. goto out_free;
  1276. if (!dma_mask)
  1277. dma_mask = *dev->dma_mask;
  1278. spin_lock_irqsave(&domain->lock, flags);
  1279. *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
  1280. size, DMA_BIDIRECTIONAL, true, dma_mask);
  1281. if (*dma_addr == bad_dma_address)
  1282. goto out_free;
  1283. iommu_completion_wait(iommu);
  1284. spin_unlock_irqrestore(&domain->lock, flags);
  1285. return virt_addr;
  1286. out_free:
  1287. free_pages((unsigned long)virt_addr, get_order(size));
  1288. return NULL;
  1289. }
  1290. /*
  1291. * The exported free_coherent function for dma_ops.
  1292. */
  1293. static void free_coherent(struct device *dev, size_t size,
  1294. void *virt_addr, dma_addr_t dma_addr)
  1295. {
  1296. unsigned long flags;
  1297. struct amd_iommu *iommu;
  1298. struct protection_domain *domain;
  1299. u16 devid;
  1300. INC_STATS_COUNTER(cnt_free_coherent);
  1301. if (!check_device(dev))
  1302. return;
  1303. get_device_resources(dev, &iommu, &domain, &devid);
  1304. if (!iommu || !domain)
  1305. goto free_mem;
  1306. if (!dma_ops_domain(domain))
  1307. goto free_mem;
  1308. spin_lock_irqsave(&domain->lock, flags);
  1309. __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
  1310. iommu_completion_wait(iommu);
  1311. spin_unlock_irqrestore(&domain->lock, flags);
  1312. free_mem:
  1313. free_pages((unsigned long)virt_addr, get_order(size));
  1314. }
  1315. /*
  1316. * This function is called by the DMA layer to find out if we can handle a
  1317. * particular device. It is part of the dma_ops.
  1318. */
  1319. static int amd_iommu_dma_supported(struct device *dev, u64 mask)
  1320. {
  1321. u16 bdf;
  1322. struct pci_dev *pcidev;
  1323. /* No device or no PCI device */
  1324. if (!dev || dev->bus != &pci_bus_type)
  1325. return 0;
  1326. pcidev = to_pci_dev(dev);
  1327. bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
  1328. /* Out of our scope? */
  1329. if (bdf > amd_iommu_last_bdf)
  1330. return 0;
  1331. return 1;
  1332. }
  1333. /*
  1334. * The function for pre-allocating protection domains.
  1335. *
  1336. * If the driver core informs the DMA layer if a driver grabs a device
  1337. * we don't need to preallocate the protection domains anymore.
  1338. * For now we have to.
  1339. */
  1340. static void prealloc_protection_domains(void)
  1341. {
  1342. struct pci_dev *dev = NULL;
  1343. struct dma_ops_domain *dma_dom;
  1344. struct amd_iommu *iommu;
  1345. int order = amd_iommu_aperture_order;
  1346. u16 devid;
  1347. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  1348. devid = calc_devid(dev->bus->number, dev->devfn);
  1349. if (devid > amd_iommu_last_bdf)
  1350. continue;
  1351. devid = amd_iommu_alias_table[devid];
  1352. if (domain_for_device(devid))
  1353. continue;
  1354. iommu = amd_iommu_rlookup_table[devid];
  1355. if (!iommu)
  1356. continue;
  1357. dma_dom = dma_ops_domain_alloc(iommu, order);
  1358. if (!dma_dom)
  1359. continue;
  1360. init_unity_mappings_for_device(dma_dom, devid);
  1361. dma_dom->target_dev = devid;
  1362. list_add_tail(&dma_dom->list, &iommu_pd_list);
  1363. }
  1364. }
  1365. static struct dma_map_ops amd_iommu_dma_ops = {
  1366. .alloc_coherent = alloc_coherent,
  1367. .free_coherent = free_coherent,
  1368. .map_page = map_page,
  1369. .unmap_page = unmap_page,
  1370. .map_sg = map_sg,
  1371. .unmap_sg = unmap_sg,
  1372. .dma_supported = amd_iommu_dma_supported,
  1373. };
  1374. /*
  1375. * The function which clues the AMD IOMMU driver into dma_ops.
  1376. */
  1377. int __init amd_iommu_init_dma_ops(void)
  1378. {
  1379. struct amd_iommu *iommu;
  1380. int order = amd_iommu_aperture_order;
  1381. int ret;
  1382. /*
  1383. * first allocate a default protection domain for every IOMMU we
  1384. * found in the system. Devices not assigned to any other
  1385. * protection domain will be assigned to the default one.
  1386. */
  1387. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1388. iommu->default_dom = dma_ops_domain_alloc(iommu, order);
  1389. if (iommu->default_dom == NULL)
  1390. return -ENOMEM;
  1391. iommu->default_dom->domain.flags |= PD_DEFAULT_MASK;
  1392. ret = iommu_init_unity_mappings(iommu);
  1393. if (ret)
  1394. goto free_domains;
  1395. }
  1396. /*
  1397. * If device isolation is enabled, pre-allocate the protection
  1398. * domains for each device.
  1399. */
  1400. if (amd_iommu_isolate)
  1401. prealloc_protection_domains();
  1402. iommu_detected = 1;
  1403. force_iommu = 1;
  1404. bad_dma_address = 0;
  1405. #ifdef CONFIG_GART_IOMMU
  1406. gart_iommu_aperture_disabled = 1;
  1407. gart_iommu_aperture = 0;
  1408. #endif
  1409. /* Make the driver finally visible to the drivers */
  1410. dma_ops = &amd_iommu_dma_ops;
  1411. register_iommu(&amd_iommu_ops);
  1412. bus_register_notifier(&pci_bus_type, &device_nb);
  1413. amd_iommu_stats_init();
  1414. return 0;
  1415. free_domains:
  1416. list_for_each_entry(iommu, &amd_iommu_list, list) {
  1417. if (iommu->default_dom)
  1418. dma_ops_domain_free(iommu->default_dom);
  1419. }
  1420. return ret;
  1421. }
  1422. /*****************************************************************************
  1423. *
  1424. * The following functions belong to the exported interface of AMD IOMMU
  1425. *
  1426. * This interface allows access to lower level functions of the IOMMU
  1427. * like protection domain handling and assignement of devices to domains
  1428. * which is not possible with the dma_ops interface.
  1429. *
  1430. *****************************************************************************/
  1431. static void cleanup_domain(struct protection_domain *domain)
  1432. {
  1433. unsigned long flags;
  1434. u16 devid;
  1435. write_lock_irqsave(&amd_iommu_devtable_lock, flags);
  1436. for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
  1437. if (amd_iommu_pd_table[devid] == domain)
  1438. __detach_device(domain, devid);
  1439. write_unlock_irqrestore(&amd_iommu_devtable_lock, flags);
  1440. }
  1441. static int amd_iommu_domain_init(struct iommu_domain *dom)
  1442. {
  1443. struct protection_domain *domain;
  1444. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  1445. if (!domain)
  1446. return -ENOMEM;
  1447. spin_lock_init(&domain->lock);
  1448. domain->mode = PAGE_MODE_3_LEVEL;
  1449. domain->id = domain_id_alloc();
  1450. if (!domain->id)
  1451. goto out_free;
  1452. domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL);
  1453. if (!domain->pt_root)
  1454. goto out_free;
  1455. dom->priv = domain;
  1456. return 0;
  1457. out_free:
  1458. kfree(domain);
  1459. return -ENOMEM;
  1460. }
  1461. static void amd_iommu_domain_destroy(struct iommu_domain *dom)
  1462. {
  1463. struct protection_domain *domain = dom->priv;
  1464. if (!domain)
  1465. return;
  1466. if (domain->dev_cnt > 0)
  1467. cleanup_domain(domain);
  1468. BUG_ON(domain->dev_cnt != 0);
  1469. free_pagetable(domain);
  1470. domain_id_free(domain->id);
  1471. kfree(domain);
  1472. dom->priv = NULL;
  1473. }
  1474. static void amd_iommu_detach_device(struct iommu_domain *dom,
  1475. struct device *dev)
  1476. {
  1477. struct protection_domain *domain = dom->priv;
  1478. struct amd_iommu *iommu;
  1479. struct pci_dev *pdev;
  1480. u16 devid;
  1481. if (dev->bus != &pci_bus_type)
  1482. return;
  1483. pdev = to_pci_dev(dev);
  1484. devid = calc_devid(pdev->bus->number, pdev->devfn);
  1485. if (devid > 0)
  1486. detach_device(domain, devid);
  1487. iommu = amd_iommu_rlookup_table[devid];
  1488. if (!iommu)
  1489. return;
  1490. iommu_queue_inv_dev_entry(iommu, devid);
  1491. iommu_completion_wait(iommu);
  1492. }
  1493. static int amd_iommu_attach_device(struct iommu_domain *dom,
  1494. struct device *dev)
  1495. {
  1496. struct protection_domain *domain = dom->priv;
  1497. struct protection_domain *old_domain;
  1498. struct amd_iommu *iommu;
  1499. struct pci_dev *pdev;
  1500. u16 devid;
  1501. if (dev->bus != &pci_bus_type)
  1502. return -EINVAL;
  1503. pdev = to_pci_dev(dev);
  1504. devid = calc_devid(pdev->bus->number, pdev->devfn);
  1505. if (devid >= amd_iommu_last_bdf ||
  1506. devid != amd_iommu_alias_table[devid])
  1507. return -EINVAL;
  1508. iommu = amd_iommu_rlookup_table[devid];
  1509. if (!iommu)
  1510. return -EINVAL;
  1511. old_domain = domain_for_device(devid);
  1512. if (old_domain)
  1513. return -EBUSY;
  1514. attach_device(iommu, domain, devid);
  1515. iommu_completion_wait(iommu);
  1516. return 0;
  1517. }
  1518. static int amd_iommu_map_range(struct iommu_domain *dom,
  1519. unsigned long iova, phys_addr_t paddr,
  1520. size_t size, int iommu_prot)
  1521. {
  1522. struct protection_domain *domain = dom->priv;
  1523. unsigned long i, npages = iommu_num_pages(paddr, size, PAGE_SIZE);
  1524. int prot = 0;
  1525. int ret;
  1526. if (iommu_prot & IOMMU_READ)
  1527. prot |= IOMMU_PROT_IR;
  1528. if (iommu_prot & IOMMU_WRITE)
  1529. prot |= IOMMU_PROT_IW;
  1530. iova &= PAGE_MASK;
  1531. paddr &= PAGE_MASK;
  1532. for (i = 0; i < npages; ++i) {
  1533. ret = iommu_map_page(domain, iova, paddr, prot);
  1534. if (ret)
  1535. return ret;
  1536. iova += PAGE_SIZE;
  1537. paddr += PAGE_SIZE;
  1538. }
  1539. return 0;
  1540. }
  1541. static void amd_iommu_unmap_range(struct iommu_domain *dom,
  1542. unsigned long iova, size_t size)
  1543. {
  1544. struct protection_domain *domain = dom->priv;
  1545. unsigned long i, npages = iommu_num_pages(iova, size, PAGE_SIZE);
  1546. iova &= PAGE_MASK;
  1547. for (i = 0; i < npages; ++i) {
  1548. iommu_unmap_page(domain, iova);
  1549. iova += PAGE_SIZE;
  1550. }
  1551. iommu_flush_domain(domain->id);
  1552. }
  1553. static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
  1554. unsigned long iova)
  1555. {
  1556. struct protection_domain *domain = dom->priv;
  1557. unsigned long offset = iova & ~PAGE_MASK;
  1558. phys_addr_t paddr;
  1559. u64 *pte;
  1560. pte = &domain->pt_root[IOMMU_PTE_L2_INDEX(iova)];
  1561. if (!IOMMU_PTE_PRESENT(*pte))
  1562. return 0;
  1563. pte = IOMMU_PTE_PAGE(*pte);
  1564. pte = &pte[IOMMU_PTE_L1_INDEX(iova)];
  1565. if (!IOMMU_PTE_PRESENT(*pte))
  1566. return 0;
  1567. pte = IOMMU_PTE_PAGE(*pte);
  1568. pte = &pte[IOMMU_PTE_L0_INDEX(iova)];
  1569. if (!IOMMU_PTE_PRESENT(*pte))
  1570. return 0;
  1571. paddr = *pte & IOMMU_PAGE_MASK;
  1572. paddr |= offset;
  1573. return paddr;
  1574. }
  1575. static int amd_iommu_domain_has_cap(struct iommu_domain *domain,
  1576. unsigned long cap)
  1577. {
  1578. return 0;
  1579. }
  1580. static struct iommu_ops amd_iommu_ops = {
  1581. .domain_init = amd_iommu_domain_init,
  1582. .domain_destroy = amd_iommu_domain_destroy,
  1583. .attach_dev = amd_iommu_attach_device,
  1584. .detach_dev = amd_iommu_detach_device,
  1585. .map = amd_iommu_map_range,
  1586. .unmap = amd_iommu_unmap_range,
  1587. .iova_to_phys = amd_iommu_iova_to_phys,
  1588. .domain_has_cap = amd_iommu_domain_has_cap,
  1589. };